blob: fa072df805147d018fb89f09fc1e73180f8fcf07 [file] [log] [blame]
aliguorie3aff4f2009-04-05 19:14:04 +00001/*
2 * Command line utility to exercise the QEMU I/O path.
3 *
4 * Copyright (C) 2009 Red Hat, Inc.
5 * Copyright (c) 2003-2005 Silicon Graphics, Inc.
6 *
7 * This work is licensed under the terms of the GNU GPL, version 2 or later.
8 * See the COPYING file in the top-level directory.
9 */
Stefan Weilc32d7662009-08-31 22:16:16 +020010#include <sys/time.h>
aliguorie3aff4f2009-04-05 19:14:04 +000011#include <sys/types.h>
12#include <stdarg.h>
13#include <stdio.h>
14#include <getopt.h>
Stefan Weilc32d7662009-08-31 22:16:16 +020015#include <libgen.h>
aliguorie3aff4f2009-04-05 19:14:04 +000016
Kevin Wolf3d219942013-06-05 14:19:39 +020017#include "qemu-io.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010018#include "qemu/main-loop.h"
Max Reitzb543c5c2013-10-11 14:02:10 +020019#include "qemu/option.h"
20#include "qemu/config-file.h"
Stefan Hajnoczi0cf17e12013-11-14 11:54:17 +010021#include "qemu/readline.h"
Markus Armbruster26f54e92014-10-07 13:59:04 +020022#include "sysemu/block-backend.h"
Paolo Bonzini737e1502012-12-17 18:19:44 +010023#include "block/block_int.h"
Stefan Hajnoczid7bb72c2012-03-12 16:36:07 +000024#include "trace/control.h"
aliguorie3aff4f2009-04-05 19:14:04 +000025
Devin Nakamura43642b32011-07-11 11:22:16 -040026#define CMD_NOFILE_OK 0x01
aliguorie3aff4f2009-04-05 19:14:04 +000027
Stefan Weilf9883882014-03-05 22:23:00 +010028static char *progname;
aliguorie3aff4f2009-04-05 19:14:04 +000029
Markus Armbruster26f54e92014-10-07 13:59:04 +020030static BlockBackend *qemuio_blk;
Stefan Weilf9883882014-03-05 22:23:00 +010031static BlockDriverState *qemuio_bs;
Kevin Wolf191c2892010-09-16 13:18:08 +020032
Kevin Wolfd1174f12013-06-05 14:19:37 +020033/* qemu-io commands passed using -c */
34static int ncmdline;
35static char **cmdline;
36
Stefan Hajnoczi0cf17e12013-11-14 11:54:17 +010037static ReadLineState *readline_state;
38
Kevin Wolf734c3b82013-06-05 14:19:30 +020039static int close_f(BlockDriverState *bs, int argc, char **argv)
aliguorie3aff4f2009-04-05 19:14:04 +000040{
Markus Armbruster26f54e92014-10-07 13:59:04 +020041 blk_unref(qemuio_blk);
Kevin Wolf734c3b82013-06-05 14:19:30 +020042 qemuio_bs = NULL;
Markus Armbruster26f54e92014-10-07 13:59:04 +020043 qemuio_blk = NULL;
Devin Nakamura43642b32011-07-11 11:22:16 -040044 return 0;
aliguorie3aff4f2009-04-05 19:14:04 +000045}
46
47static const cmdinfo_t close_cmd = {
Devin Nakamura43642b32011-07-11 11:22:16 -040048 .name = "close",
49 .altname = "c",
50 .cfunc = close_f,
51 .oneline = "close the current open file",
aliguorie3aff4f2009-04-05 19:14:04 +000052};
53
Max Reitz1b58b432015-02-05 13:58:20 -050054static int openfile(char *name, int flags, int growable, QDict *opts)
aliguorie3aff4f2009-04-05 19:14:04 +000055{
Max Reitz34b5d2c2013-09-05 14:45:29 +020056 Error *local_err = NULL;
57
Max Reitz1b58b432015-02-05 13:58:20 -050058 if (qemuio_blk) {
Devin Nakamura43642b32011-07-11 11:22:16 -040059 fprintf(stderr, "file open already, try 'help close'\n");
Markus Armbruster29f26012014-05-28 11:16:59 +020060 QDECREF(opts);
Devin Nakamura43642b32011-07-11 11:22:16 -040061 return 1;
62 }
aliguorie3aff4f2009-04-05 19:14:04 +000063
Markus Armbrusterdbb651c2014-09-08 18:50:58 +020064 if (growable) {
65 flags |= BDRV_O_PROTOCOL;
66 }
67
Max Reitz1b58b432015-02-05 13:58:20 -050068 qemuio_blk = blk_new_open("hda", name, NULL, opts, flags, &local_err);
69 if (!qemuio_blk) {
Markus Armbrusterdbb651c2014-09-08 18:50:58 +020070 fprintf(stderr, "%s: can't open%s%s: %s\n", progname,
71 name ? " device " : "", name ?: "",
72 error_get_pretty(local_err));
73 error_free(local_err);
Markus Armbrusterdbb651c2014-09-08 18:50:58 +020074 return 1;
Devin Nakamura43642b32011-07-11 11:22:16 -040075 }
Max Reitz1b58b432015-02-05 13:58:20 -050076 qemuio_bs = blk_bs(qemuio_blk);
Christoph Hellwig1db69472009-07-15 23:11:21 +020077
Devin Nakamura43642b32011-07-11 11:22:16 -040078 return 0;
aliguorie3aff4f2009-04-05 19:14:04 +000079}
80
Devin Nakamura43642b32011-07-11 11:22:16 -040081static void open_help(void)
aliguorie3aff4f2009-04-05 19:14:04 +000082{
Devin Nakamura43642b32011-07-11 11:22:16 -040083 printf(
aliguorie3aff4f2009-04-05 19:14:04 +000084"\n"
85" opens a new file in the requested mode\n"
86"\n"
87" Example:\n"
88" 'open -Cn /tmp/data' - creates/opens data file read-write and uncached\n"
89"\n"
90" Opens a file for subsequent use by all of the other qemu-io commands.\n"
aliguorie3aff4f2009-04-05 19:14:04 +000091" -r, -- open file read-only\n"
92" -s, -- use snapshot file\n"
93" -n, -- disable host cache\n"
Max Reitzb543c5c2013-10-11 14:02:10 +020094" -g, -- allow file to grow (only applies to protocols)\n"
95" -o, -- options to be given to the block driver"
aliguorie3aff4f2009-04-05 19:14:04 +000096"\n");
97}
98
Kevin Wolf734c3b82013-06-05 14:19:30 +020099static int open_f(BlockDriverState *bs, int argc, char **argv);
Blue Swirl22a2bdc2009-11-21 09:06:46 +0000100
101static const cmdinfo_t open_cmd = {
Devin Nakamura43642b32011-07-11 11:22:16 -0400102 .name = "open",
103 .altname = "o",
104 .cfunc = open_f,
105 .argmin = 1,
106 .argmax = -1,
107 .flags = CMD_NOFILE_OK,
Max Reitzb543c5c2013-10-11 14:02:10 +0200108 .args = "[-Crsn] [-o options] [path]",
Devin Nakamura43642b32011-07-11 11:22:16 -0400109 .oneline = "open the file specified by path",
110 .help = open_help,
Blue Swirl22a2bdc2009-11-21 09:06:46 +0000111};
aliguorie3aff4f2009-04-05 19:14:04 +0000112
Max Reitzb543c5c2013-10-11 14:02:10 +0200113static QemuOptsList empty_opts = {
114 .name = "drive",
Markus Armbruster443422f2014-05-28 11:16:58 +0200115 .merge_lists = true,
Max Reitzb543c5c2013-10-11 14:02:10 +0200116 .head = QTAILQ_HEAD_INITIALIZER(empty_opts.head),
117 .desc = {
118 /* no elements => accept any params */
119 { /* end of list */ }
120 },
121};
122
Kevin Wolf734c3b82013-06-05 14:19:30 +0200123static int open_f(BlockDriverState *bs, int argc, char **argv)
aliguorie3aff4f2009-04-05 19:14:04 +0000124{
Devin Nakamura43642b32011-07-11 11:22:16 -0400125 int flags = 0;
126 int readonly = 0;
127 int growable = 0;
128 int c;
Max Reitzb543c5c2013-10-11 14:02:10 +0200129 QemuOpts *qopts;
Markus Armbruster443422f2014-05-28 11:16:58 +0200130 QDict *opts;
aliguorie3aff4f2009-04-05 19:14:04 +0000131
Max Reitzb543c5c2013-10-11 14:02:10 +0200132 while ((c = getopt(argc, argv, "snrgo:")) != EOF) {
Devin Nakamura43642b32011-07-11 11:22:16 -0400133 switch (c) {
134 case 's':
135 flags |= BDRV_O_SNAPSHOT;
136 break;
137 case 'n':
138 flags |= BDRV_O_NOCACHE | BDRV_O_CACHE_WB;
139 break;
140 case 'r':
141 readonly = 1;
142 break;
143 case 'g':
144 growable = 1;
145 break;
Max Reitzb543c5c2013-10-11 14:02:10 +0200146 case 'o':
Markus Armbruster443422f2014-05-28 11:16:58 +0200147 if (!qemu_opts_parse(&empty_opts, optarg, 0)) {
Max Reitzb543c5c2013-10-11 14:02:10 +0200148 printf("could not parse option list -- %s\n", optarg);
Markus Armbruster443422f2014-05-28 11:16:58 +0200149 qemu_opts_reset(&empty_opts);
Max Reitzb543c5c2013-10-11 14:02:10 +0200150 return 0;
151 }
Max Reitzb543c5c2013-10-11 14:02:10 +0200152 break;
Devin Nakamura43642b32011-07-11 11:22:16 -0400153 default:
Markus Armbruster443422f2014-05-28 11:16:58 +0200154 qemu_opts_reset(&empty_opts);
Kevin Wolfc2cdf5c2013-06-05 14:19:36 +0200155 return qemuio_command_usage(&open_cmd);
Naphtali Spreif5edb012010-01-17 16:48:13 +0200156 }
Devin Nakamura43642b32011-07-11 11:22:16 -0400157 }
aliguorie3aff4f2009-04-05 19:14:04 +0000158
Devin Nakamura43642b32011-07-11 11:22:16 -0400159 if (!readonly) {
160 flags |= BDRV_O_RDWR;
161 }
aliguorie3aff4f2009-04-05 19:14:04 +0000162
Markus Armbruster443422f2014-05-28 11:16:58 +0200163 qopts = qemu_opts_find(&empty_opts, NULL);
164 opts = qopts ? qemu_opts_to_qdict(qopts, NULL) : NULL;
165 qemu_opts_reset(&empty_opts);
166
Max Reitzfd0fee32013-12-20 19:28:20 +0100167 if (optind == argc - 1) {
Max Reitz1b58b432015-02-05 13:58:20 -0500168 return openfile(argv[optind], flags, growable, opts);
Max Reitzfd0fee32013-12-20 19:28:20 +0100169 } else if (optind == argc) {
Max Reitz1b58b432015-02-05 13:58:20 -0500170 return openfile(NULL, flags, growable, opts);
Max Reitzfd0fee32013-12-20 19:28:20 +0100171 } else {
Markus Armbruster29f26012014-05-28 11:16:59 +0200172 QDECREF(opts);
Kevin Wolfc2cdf5c2013-06-05 14:19:36 +0200173 return qemuio_command_usage(&open_cmd);
Devin Nakamura43642b32011-07-11 11:22:16 -0400174 }
aliguorie3aff4f2009-04-05 19:14:04 +0000175}
176
Kevin Wolfe681be72013-06-05 14:19:34 +0200177static int quit_f(BlockDriverState *bs, int argc, char **argv)
178{
179 return 1;
180}
181
182static const cmdinfo_t quit_cmd = {
183 .name = "quit",
184 .altname = "q",
185 .cfunc = quit_f,
186 .argmin = -1,
187 .argmax = -1,
188 .flags = CMD_FLAG_GLOBAL,
189 .oneline = "exit the program",
190};
191
aliguorie3aff4f2009-04-05 19:14:04 +0000192static void usage(const char *name)
193{
Devin Nakamura43642b32011-07-11 11:22:16 -0400194 printf(
Kevin Wolfbe6273d2014-11-20 16:27:06 +0100195"Usage: %s [-h] [-V] [-rsnm] [-f FMT] [-c STRING] ... [file]\n"
Stefan Weil84844a22009-06-22 15:08:47 +0200196"QEMU Disk exerciser\n"
aliguorie3aff4f2009-04-05 19:14:04 +0000197"\n"
Maria Kustovad208cc32014-03-18 09:59:19 +0400198" -c, --cmd STRING execute command with its arguments\n"
199" from the given string\n"
Kevin Wolfbe6273d2014-11-20 16:27:06 +0100200" -f, --format FMT specifies the block driver to use\n"
aliguorie3aff4f2009-04-05 19:14:04 +0000201" -r, --read-only export read-only\n"
202" -s, --snapshot use snapshot file\n"
203" -n, --nocache disable host cache\n"
Christoph Hellwig1db69472009-07-15 23:11:21 +0200204" -g, --growable allow file to grow (only applies to protocols)\n"
aliguorie3aff4f2009-04-05 19:14:04 +0000205" -m, --misalign misalign allocations for O_DIRECT\n"
Christoph Hellwig5c6c3a62009-08-20 16:58:35 +0200206" -k, --native-aio use kernel AIO implementation (on Linux only)\n"
Kevin Wolf592fa072012-04-18 12:07:39 +0200207" -t, --cache=MODE use the given cache mode for the image\n"
Stefan Hajnoczid7bb72c2012-03-12 16:36:07 +0000208" -T, --trace FILE enable trace events listed in the given file\n"
aliguorie3aff4f2009-04-05 19:14:04 +0000209" -h, --help display this help and exit\n"
210" -V, --version output version information and exit\n"
Maria Kustovad208cc32014-03-18 09:59:19 +0400211"\n"
212"See '%s -c help' for information on available commands."
aliguorie3aff4f2009-04-05 19:14:04 +0000213"\n",
Maria Kustovad208cc32014-03-18 09:59:19 +0400214 name, name);
aliguorie3aff4f2009-04-05 19:14:04 +0000215}
216
Kevin Wolfd1174f12013-06-05 14:19:37 +0200217static char *get_prompt(void)
218{
219 static char prompt[FILENAME_MAX + 2 /*"> "*/ + 1 /*"\0"*/ ];
220
221 if (!prompt[0]) {
222 snprintf(prompt, sizeof(prompt), "%s> ", progname);
223 }
224
225 return prompt;
226}
227
Stefan Weild5d15072014-01-25 18:18:23 +0100228static void GCC_FMT_ATTR(2, 3) readline_printf_func(void *opaque,
229 const char *fmt, ...)
Kevin Wolfd1174f12013-06-05 14:19:37 +0200230{
Stefan Hajnoczi0cf17e12013-11-14 11:54:17 +0100231 va_list ap;
232 va_start(ap, fmt);
233 vprintf(fmt, ap);
234 va_end(ap);
235}
236
237static void readline_flush_func(void *opaque)
238{
239 fflush(stdout);
240}
241
242static void readline_func(void *opaque, const char *str, void *readline_opaque)
243{
244 char **line = readline_opaque;
245 *line = g_strdup(str);
246}
247
Stefan Hajnoczi46940202013-11-14 11:54:18 +0100248static void completion_match(const char *cmd, void *opaque)
249{
250 readline_add_completion(readline_state, cmd);
251}
252
Stefan Hajnoczi0cf17e12013-11-14 11:54:17 +0100253static void readline_completion_func(void *opaque, const char *str)
254{
Stefan Hajnoczi46940202013-11-14 11:54:18 +0100255 readline_set_completion_index(readline_state, strlen(str));
256 qemuio_complete_command(str, completion_match, NULL);
Stefan Hajnoczi0cf17e12013-11-14 11:54:17 +0100257}
258
259static char *fetchline_readline(void)
260{
261 char *line = NULL;
262
263 readline_start(readline_state, get_prompt(), 0, readline_func, &line);
264 while (!line) {
265 int ch = getchar();
266 if (ch == EOF) {
267 break;
268 }
269 readline_handle_byte(readline_state, ch);
Kevin Wolfd1174f12013-06-05 14:19:37 +0200270 }
271 return line;
272}
Kevin Wolfd1174f12013-06-05 14:19:37 +0200273
Stefan Hajnoczi0cf17e12013-11-14 11:54:17 +0100274#define MAXREADLINESZ 1024
275static char *fetchline_fgets(void)
Kevin Wolfd1174f12013-06-05 14:19:37 +0200276{
277 char *p, *line = g_malloc(MAXREADLINESZ);
278
279 if (!fgets(line, MAXREADLINESZ, stdin)) {
280 g_free(line);
281 return NULL;
282 }
283
284 p = line + strlen(line);
285 if (p != line && p[-1] == '\n') {
286 p[-1] = '\0';
287 }
288
289 return line;
290}
Stefan Hajnoczi0cf17e12013-11-14 11:54:17 +0100291
292static char *fetchline(void)
293{
294 if (readline_state) {
295 return fetchline_readline();
296 } else {
297 return fetchline_fgets();
298 }
299}
Kevin Wolfd1174f12013-06-05 14:19:37 +0200300
301static void prep_fetchline(void *opaque)
302{
303 int *fetchable = opaque;
304
305 qemu_set_fd_handler(STDIN_FILENO, NULL, NULL, NULL);
306 *fetchable= 1;
307}
308
309static void command_loop(void)
310{
311 int i, done = 0, fetchable = 0, prompted = 0;
312 char *input;
313
314 for (i = 0; !done && i < ncmdline; i++) {
Kevin Wolf3d219942013-06-05 14:19:39 +0200315 done = qemuio_command(qemuio_bs, cmdline[i]);
Kevin Wolfd1174f12013-06-05 14:19:37 +0200316 }
317 if (cmdline) {
318 g_free(cmdline);
319 return;
320 }
321
322 while (!done) {
323 if (!prompted) {
324 printf("%s", get_prompt());
325 fflush(stdout);
326 qemu_set_fd_handler(STDIN_FILENO, prep_fetchline, NULL, &fetchable);
327 prompted = 1;
328 }
329
330 main_loop_wait(false);
331
332 if (!fetchable) {
333 continue;
334 }
335
336 input = fetchline();
337 if (input == NULL) {
338 break;
339 }
Kevin Wolf3d219942013-06-05 14:19:39 +0200340 done = qemuio_command(qemuio_bs, input);
Kevin Wolfd1174f12013-06-05 14:19:37 +0200341 g_free(input);
342
343 prompted = 0;
344 fetchable = 0;
345 }
346 qemu_set_fd_handler(STDIN_FILENO, NULL, NULL, NULL);
347}
348
349static void add_user_command(char *optarg)
350{
Markus Armbruster5839e532014-08-19 10:31:08 +0200351 cmdline = g_renew(char *, cmdline, ++ncmdline);
Kevin Wolfd1174f12013-06-05 14:19:37 +0200352 cmdline[ncmdline-1] = optarg;
353}
354
Stefan Hajnoczi0cf17e12013-11-14 11:54:17 +0100355static void reenable_tty_echo(void)
356{
357 qemu_set_tty_echo(STDIN_FILENO, true);
358}
359
aliguorie3aff4f2009-04-05 19:14:04 +0000360int main(int argc, char **argv)
361{
Devin Nakamura43642b32011-07-11 11:22:16 -0400362 int readonly = 0;
363 int growable = 0;
Kevin Wolfbe6273d2014-11-20 16:27:06 +0100364 const char *sopt = "hVc:d:f:rsnmgkt:T:";
Devin Nakamura43642b32011-07-11 11:22:16 -0400365 const struct option lopt[] = {
366 { "help", 0, NULL, 'h' },
367 { "version", 0, NULL, 'V' },
368 { "offset", 1, NULL, 'o' },
369 { "cmd", 1, NULL, 'c' },
Kevin Wolfbe6273d2014-11-20 16:27:06 +0100370 { "format", 1, NULL, 'f' },
Devin Nakamura43642b32011-07-11 11:22:16 -0400371 { "read-only", 0, NULL, 'r' },
372 { "snapshot", 0, NULL, 's' },
373 { "nocache", 0, NULL, 'n' },
374 { "misalign", 0, NULL, 'm' },
375 { "growable", 0, NULL, 'g' },
376 { "native-aio", 0, NULL, 'k' },
Paolo Bonzini9e8f1832013-02-08 14:06:11 +0100377 { "discard", 1, NULL, 'd' },
Kevin Wolf592fa072012-04-18 12:07:39 +0200378 { "cache", 1, NULL, 't' },
Stefan Hajnoczid7bb72c2012-03-12 16:36:07 +0000379 { "trace", 1, NULL, 'T' },
Devin Nakamura43642b32011-07-11 11:22:16 -0400380 { NULL, 0, NULL, 0 }
381 };
382 int c;
383 int opt_index = 0;
Paolo Bonzini9e8f1832013-02-08 14:06:11 +0100384 int flags = BDRV_O_UNMAP;
Chrysostomos Nanakos2f78e492014-09-18 14:30:49 +0300385 Error *local_error = NULL;
Max Reitz1b58b432015-02-05 13:58:20 -0500386 QDict *opts = NULL;
aliguorie3aff4f2009-04-05 19:14:04 +0000387
MORITA Kazutaka526eda12013-07-23 17:30:11 +0900388#ifdef CONFIG_POSIX
389 signal(SIGPIPE, SIG_IGN);
390#endif
391
Devin Nakamura43642b32011-07-11 11:22:16 -0400392 progname = basename(argv[0]);
Fam Zheng10f5bff2014-02-10 14:48:51 +0800393 qemu_init_exec_dir(argv[0]);
aliguorie3aff4f2009-04-05 19:14:04 +0000394
Kevin Wolfbe6273d2014-11-20 16:27:06 +0100395 bdrv_init();
396
Devin Nakamura43642b32011-07-11 11:22:16 -0400397 while ((c = getopt_long(argc, argv, sopt, lopt, &opt_index)) != -1) {
398 switch (c) {
399 case 's':
400 flags |= BDRV_O_SNAPSHOT;
401 break;
402 case 'n':
403 flags |= BDRV_O_NOCACHE | BDRV_O_CACHE_WB;
404 break;
Paolo Bonzini9e8f1832013-02-08 14:06:11 +0100405 case 'd':
406 if (bdrv_parse_discard_flags(optarg, &flags) < 0) {
407 error_report("Invalid discard option: %s", optarg);
408 exit(1);
409 }
410 break;
Kevin Wolfbe6273d2014-11-20 16:27:06 +0100411 case 'f':
Max Reitz1b58b432015-02-05 13:58:20 -0500412 if (!opts) {
413 opts = qdict_new();
Kevin Wolfbe6273d2014-11-20 16:27:06 +0100414 }
Max Reitz1b58b432015-02-05 13:58:20 -0500415 qdict_put(opts, "driver", qstring_from_str(optarg));
Kevin Wolfbe6273d2014-11-20 16:27:06 +0100416 break;
Devin Nakamura43642b32011-07-11 11:22:16 -0400417 case 'c':
418 add_user_command(optarg);
419 break;
420 case 'r':
421 readonly = 1;
422 break;
423 case 'm':
Stefan Weilf9883882014-03-05 22:23:00 +0100424 qemuio_misalign = true;
Devin Nakamura43642b32011-07-11 11:22:16 -0400425 break;
426 case 'g':
427 growable = 1;
428 break;
429 case 'k':
430 flags |= BDRV_O_NATIVE_AIO;
431 break;
Kevin Wolf592fa072012-04-18 12:07:39 +0200432 case 't':
433 if (bdrv_parse_cache_flags(optarg, &flags) < 0) {
434 error_report("Invalid cache option: %s", optarg);
435 exit(1);
436 }
437 break;
Stefan Hajnoczid7bb72c2012-03-12 16:36:07 +0000438 case 'T':
LluĂ­s Vilanova5b808272014-05-27 15:02:14 +0200439 if (!trace_init_backends(optarg, NULL)) {
Stefan Hajnoczid7bb72c2012-03-12 16:36:07 +0000440 exit(1); /* error message will have been printed */
441 }
442 break;
Devin Nakamura43642b32011-07-11 11:22:16 -0400443 case 'V':
Kevin Wolf02da3862013-06-05 14:19:40 +0200444 printf("%s version %s\n", progname, QEMU_VERSION);
Devin Nakamura43642b32011-07-11 11:22:16 -0400445 exit(0);
446 case 'h':
447 usage(progname);
448 exit(0);
449 default:
450 usage(progname);
451 exit(1);
Naphtali Spreif5edb012010-01-17 16:48:13 +0200452 }
Devin Nakamura43642b32011-07-11 11:22:16 -0400453 }
aliguorie3aff4f2009-04-05 19:14:04 +0000454
Devin Nakamura43642b32011-07-11 11:22:16 -0400455 if ((argc - optind) > 1) {
456 usage(progname);
457 exit(1);
458 }
aliguorie3aff4f2009-04-05 19:14:04 +0000459
Chrysostomos Nanakos2f78e492014-09-18 14:30:49 +0300460 if (qemu_init_main_loop(&local_error)) {
461 error_report("%s", error_get_pretty(local_error));
462 error_free(local_error);
463 exit(1);
464 }
Zhi Yong Wua57d1142012-02-19 22:24:59 +0800465
Devin Nakamura43642b32011-07-11 11:22:16 -0400466 /* initialize commands */
Kevin Wolfc2cdf5c2013-06-05 14:19:36 +0200467 qemuio_add_command(&quit_cmd);
468 qemuio_add_command(&open_cmd);
469 qemuio_add_command(&close_cmd);
Devin Nakamura43642b32011-07-11 11:22:16 -0400470
Stefan Hajnoczi0cf17e12013-11-14 11:54:17 +0100471 if (isatty(STDIN_FILENO)) {
472 readline_state = readline_init(readline_printf_func,
473 readline_flush_func,
474 NULL,
475 readline_completion_func);
476 qemu_set_tty_echo(STDIN_FILENO, false);
477 atexit(reenable_tty_echo);
478 }
479
Devin Nakamura43642b32011-07-11 11:22:16 -0400480 /* open the device */
481 if (!readonly) {
482 flags |= BDRV_O_RDWR;
483 }
484
485 if ((argc - optind) == 1) {
Max Reitz1b58b432015-02-05 13:58:20 -0500486 openfile(argv[optind], flags, growable, opts);
Devin Nakamura43642b32011-07-11 11:22:16 -0400487 }
488 command_loop();
489
490 /*
Stefan Hajnoczi922453b2011-11-30 12:23:43 +0000491 * Make sure all outstanding requests complete before the program exits.
Devin Nakamura43642b32011-07-11 11:22:16 -0400492 */
Stefan Hajnoczi922453b2011-11-30 12:23:43 +0000493 bdrv_drain_all();
Devin Nakamura43642b32011-07-11 11:22:16 -0400494
Markus Armbruster26f54e92014-10-07 13:59:04 +0200495 blk_unref(qemuio_blk);
Stefan Hajnoczi0cf17e12013-11-14 11:54:17 +0100496 g_free(readline_state);
Devin Nakamura43642b32011-07-11 11:22:16 -0400497 return 0;
aliguorie3aff4f2009-04-05 19:14:04 +0000498}