blob: d7a32e432292056c223d872c805964c2672d04eb [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
Kevin Wolfbe6273d2014-11-20 16:27:06 +010054static int openfile(char *name, BlockDriver *drv, int flags, int growable,
55 QDict *opts)
aliguorie3aff4f2009-04-05 19:14:04 +000056{
Max Reitz34b5d2c2013-09-05 14:45:29 +020057 Error *local_err = NULL;
58
Kevin Wolf734c3b82013-06-05 14:19:30 +020059 if (qemuio_bs) {
Devin Nakamura43642b32011-07-11 11:22:16 -040060 fprintf(stderr, "file open already, try 'help close'\n");
Markus Armbruster29f26012014-05-28 11:16:59 +020061 QDECREF(opts);
Devin Nakamura43642b32011-07-11 11:22:16 -040062 return 1;
63 }
aliguorie3aff4f2009-04-05 19:14:04 +000064
Markus Armbruster7e7d56d2014-10-07 13:59:05 +020065 qemuio_blk = blk_new_with_bs("hda", &error_abort);
66 qemuio_bs = blk_bs(qemuio_blk);
Christoph Hellwig6db95602010-04-05 16:53:57 +020067
Markus Armbrusterdbb651c2014-09-08 18:50:58 +020068 if (growable) {
69 flags |= BDRV_O_PROTOCOL;
70 }
71
Kevin Wolfbe6273d2014-11-20 16:27:06 +010072 if (bdrv_open(&qemuio_bs, name, NULL, opts, flags, drv, &local_err) < 0) {
Markus Armbrusterdbb651c2014-09-08 18:50:58 +020073 fprintf(stderr, "%s: can't open%s%s: %s\n", progname,
74 name ? " device " : "", name ?: "",
75 error_get_pretty(local_err));
76 error_free(local_err);
Markus Armbruster26f54e92014-10-07 13:59:04 +020077 blk_unref(qemuio_blk);
Markus Armbrusterdbb651c2014-09-08 18:50:58 +020078 qemuio_bs = NULL;
Markus Armbruster26f54e92014-10-07 13:59:04 +020079 qemuio_blk = NULL;
Markus Armbrusterdbb651c2014-09-08 18:50:58 +020080 return 1;
Devin Nakamura43642b32011-07-11 11:22:16 -040081 }
Christoph Hellwig1db69472009-07-15 23:11:21 +020082
Devin Nakamura43642b32011-07-11 11:22:16 -040083 return 0;
aliguorie3aff4f2009-04-05 19:14:04 +000084}
85
Devin Nakamura43642b32011-07-11 11:22:16 -040086static void open_help(void)
aliguorie3aff4f2009-04-05 19:14:04 +000087{
Devin Nakamura43642b32011-07-11 11:22:16 -040088 printf(
aliguorie3aff4f2009-04-05 19:14:04 +000089"\n"
90" opens a new file in the requested mode\n"
91"\n"
92" Example:\n"
93" 'open -Cn /tmp/data' - creates/opens data file read-write and uncached\n"
94"\n"
95" Opens a file for subsequent use by all of the other qemu-io commands.\n"
aliguorie3aff4f2009-04-05 19:14:04 +000096" -r, -- open file read-only\n"
97" -s, -- use snapshot file\n"
98" -n, -- disable host cache\n"
Max Reitzb543c5c2013-10-11 14:02:10 +020099" -g, -- allow file to grow (only applies to protocols)\n"
100" -o, -- options to be given to the block driver"
aliguorie3aff4f2009-04-05 19:14:04 +0000101"\n");
102}
103
Kevin Wolf734c3b82013-06-05 14:19:30 +0200104static int open_f(BlockDriverState *bs, int argc, char **argv);
Blue Swirl22a2bdc2009-11-21 09:06:46 +0000105
106static const cmdinfo_t open_cmd = {
Devin Nakamura43642b32011-07-11 11:22:16 -0400107 .name = "open",
108 .altname = "o",
109 .cfunc = open_f,
110 .argmin = 1,
111 .argmax = -1,
112 .flags = CMD_NOFILE_OK,
Max Reitzb543c5c2013-10-11 14:02:10 +0200113 .args = "[-Crsn] [-o options] [path]",
Devin Nakamura43642b32011-07-11 11:22:16 -0400114 .oneline = "open the file specified by path",
115 .help = open_help,
Blue Swirl22a2bdc2009-11-21 09:06:46 +0000116};
aliguorie3aff4f2009-04-05 19:14:04 +0000117
Max Reitzb543c5c2013-10-11 14:02:10 +0200118static QemuOptsList empty_opts = {
119 .name = "drive",
Markus Armbruster443422f2014-05-28 11:16:58 +0200120 .merge_lists = true,
Max Reitzb543c5c2013-10-11 14:02:10 +0200121 .head = QTAILQ_HEAD_INITIALIZER(empty_opts.head),
122 .desc = {
123 /* no elements => accept any params */
124 { /* end of list */ }
125 },
126};
127
Kevin Wolf734c3b82013-06-05 14:19:30 +0200128static int open_f(BlockDriverState *bs, int argc, char **argv)
aliguorie3aff4f2009-04-05 19:14:04 +0000129{
Devin Nakamura43642b32011-07-11 11:22:16 -0400130 int flags = 0;
131 int readonly = 0;
132 int growable = 0;
133 int c;
Max Reitzb543c5c2013-10-11 14:02:10 +0200134 QemuOpts *qopts;
Markus Armbruster443422f2014-05-28 11:16:58 +0200135 QDict *opts;
aliguorie3aff4f2009-04-05 19:14:04 +0000136
Max Reitzb543c5c2013-10-11 14:02:10 +0200137 while ((c = getopt(argc, argv, "snrgo:")) != EOF) {
Devin Nakamura43642b32011-07-11 11:22:16 -0400138 switch (c) {
139 case 's':
140 flags |= BDRV_O_SNAPSHOT;
141 break;
142 case 'n':
143 flags |= BDRV_O_NOCACHE | BDRV_O_CACHE_WB;
144 break;
145 case 'r':
146 readonly = 1;
147 break;
148 case 'g':
149 growable = 1;
150 break;
Max Reitzb543c5c2013-10-11 14:02:10 +0200151 case 'o':
Markus Armbruster443422f2014-05-28 11:16:58 +0200152 if (!qemu_opts_parse(&empty_opts, optarg, 0)) {
Max Reitzb543c5c2013-10-11 14:02:10 +0200153 printf("could not parse option list -- %s\n", optarg);
Markus Armbruster443422f2014-05-28 11:16:58 +0200154 qemu_opts_reset(&empty_opts);
Max Reitzb543c5c2013-10-11 14:02:10 +0200155 return 0;
156 }
Max Reitzb543c5c2013-10-11 14:02:10 +0200157 break;
Devin Nakamura43642b32011-07-11 11:22:16 -0400158 default:
Markus Armbruster443422f2014-05-28 11:16:58 +0200159 qemu_opts_reset(&empty_opts);
Kevin Wolfc2cdf5c2013-06-05 14:19:36 +0200160 return qemuio_command_usage(&open_cmd);
Naphtali Spreif5edb012010-01-17 16:48:13 +0200161 }
Devin Nakamura43642b32011-07-11 11:22:16 -0400162 }
aliguorie3aff4f2009-04-05 19:14:04 +0000163
Devin Nakamura43642b32011-07-11 11:22:16 -0400164 if (!readonly) {
165 flags |= BDRV_O_RDWR;
166 }
aliguorie3aff4f2009-04-05 19:14:04 +0000167
Markus Armbruster443422f2014-05-28 11:16:58 +0200168 qopts = qemu_opts_find(&empty_opts, NULL);
169 opts = qopts ? qemu_opts_to_qdict(qopts, NULL) : NULL;
170 qemu_opts_reset(&empty_opts);
171
Max Reitzfd0fee32013-12-20 19:28:20 +0100172 if (optind == argc - 1) {
Kevin Wolfbe6273d2014-11-20 16:27:06 +0100173 return openfile(argv[optind], NULL, flags, growable, opts);
Max Reitzfd0fee32013-12-20 19:28:20 +0100174 } else if (optind == argc) {
Kevin Wolfbe6273d2014-11-20 16:27:06 +0100175 return openfile(NULL, NULL, flags, growable, opts);
Max Reitzfd0fee32013-12-20 19:28:20 +0100176 } else {
Markus Armbruster29f26012014-05-28 11:16:59 +0200177 QDECREF(opts);
Kevin Wolfc2cdf5c2013-06-05 14:19:36 +0200178 return qemuio_command_usage(&open_cmd);
Devin Nakamura43642b32011-07-11 11:22:16 -0400179 }
aliguorie3aff4f2009-04-05 19:14:04 +0000180}
181
Kevin Wolfe681be72013-06-05 14:19:34 +0200182static int quit_f(BlockDriverState *bs, int argc, char **argv)
183{
184 return 1;
185}
186
187static const cmdinfo_t quit_cmd = {
188 .name = "quit",
189 .altname = "q",
190 .cfunc = quit_f,
191 .argmin = -1,
192 .argmax = -1,
193 .flags = CMD_FLAG_GLOBAL,
194 .oneline = "exit the program",
195};
196
aliguorie3aff4f2009-04-05 19:14:04 +0000197static void usage(const char *name)
198{
Devin Nakamura43642b32011-07-11 11:22:16 -0400199 printf(
Kevin Wolfbe6273d2014-11-20 16:27:06 +0100200"Usage: %s [-h] [-V] [-rsnm] [-f FMT] [-c STRING] ... [file]\n"
Stefan Weil84844a22009-06-22 15:08:47 +0200201"QEMU Disk exerciser\n"
aliguorie3aff4f2009-04-05 19:14:04 +0000202"\n"
Maria Kustovad208cc32014-03-18 09:59:19 +0400203" -c, --cmd STRING execute command with its arguments\n"
204" from the given string\n"
Kevin Wolfbe6273d2014-11-20 16:27:06 +0100205" -f, --format FMT specifies the block driver to use\n"
aliguorie3aff4f2009-04-05 19:14:04 +0000206" -r, --read-only export read-only\n"
207" -s, --snapshot use snapshot file\n"
208" -n, --nocache disable host cache\n"
Christoph Hellwig1db69472009-07-15 23:11:21 +0200209" -g, --growable allow file to grow (only applies to protocols)\n"
aliguorie3aff4f2009-04-05 19:14:04 +0000210" -m, --misalign misalign allocations for O_DIRECT\n"
Christoph Hellwig5c6c3a62009-08-20 16:58:35 +0200211" -k, --native-aio use kernel AIO implementation (on Linux only)\n"
Kevin Wolf592fa072012-04-18 12:07:39 +0200212" -t, --cache=MODE use the given cache mode for the image\n"
Stefan Hajnoczid7bb72c2012-03-12 16:36:07 +0000213" -T, --trace FILE enable trace events listed in the given file\n"
aliguorie3aff4f2009-04-05 19:14:04 +0000214" -h, --help display this help and exit\n"
215" -V, --version output version information and exit\n"
Maria Kustovad208cc32014-03-18 09:59:19 +0400216"\n"
217"See '%s -c help' for information on available commands."
aliguorie3aff4f2009-04-05 19:14:04 +0000218"\n",
Maria Kustovad208cc32014-03-18 09:59:19 +0400219 name, name);
aliguorie3aff4f2009-04-05 19:14:04 +0000220}
221
Kevin Wolfd1174f12013-06-05 14:19:37 +0200222static char *get_prompt(void)
223{
224 static char prompt[FILENAME_MAX + 2 /*"> "*/ + 1 /*"\0"*/ ];
225
226 if (!prompt[0]) {
227 snprintf(prompt, sizeof(prompt), "%s> ", progname);
228 }
229
230 return prompt;
231}
232
Stefan Weild5d15072014-01-25 18:18:23 +0100233static void GCC_FMT_ATTR(2, 3) readline_printf_func(void *opaque,
234 const char *fmt, ...)
Kevin Wolfd1174f12013-06-05 14:19:37 +0200235{
Stefan Hajnoczi0cf17e12013-11-14 11:54:17 +0100236 va_list ap;
237 va_start(ap, fmt);
238 vprintf(fmt, ap);
239 va_end(ap);
240}
241
242static void readline_flush_func(void *opaque)
243{
244 fflush(stdout);
245}
246
247static void readline_func(void *opaque, const char *str, void *readline_opaque)
248{
249 char **line = readline_opaque;
250 *line = g_strdup(str);
251}
252
Stefan Hajnoczi46940202013-11-14 11:54:18 +0100253static void completion_match(const char *cmd, void *opaque)
254{
255 readline_add_completion(readline_state, cmd);
256}
257
Stefan Hajnoczi0cf17e12013-11-14 11:54:17 +0100258static void readline_completion_func(void *opaque, const char *str)
259{
Stefan Hajnoczi46940202013-11-14 11:54:18 +0100260 readline_set_completion_index(readline_state, strlen(str));
261 qemuio_complete_command(str, completion_match, NULL);
Stefan Hajnoczi0cf17e12013-11-14 11:54:17 +0100262}
263
264static char *fetchline_readline(void)
265{
266 char *line = NULL;
267
268 readline_start(readline_state, get_prompt(), 0, readline_func, &line);
269 while (!line) {
270 int ch = getchar();
271 if (ch == EOF) {
272 break;
273 }
274 readline_handle_byte(readline_state, ch);
Kevin Wolfd1174f12013-06-05 14:19:37 +0200275 }
276 return line;
277}
Kevin Wolfd1174f12013-06-05 14:19:37 +0200278
Stefan Hajnoczi0cf17e12013-11-14 11:54:17 +0100279#define MAXREADLINESZ 1024
280static char *fetchline_fgets(void)
Kevin Wolfd1174f12013-06-05 14:19:37 +0200281{
282 char *p, *line = g_malloc(MAXREADLINESZ);
283
284 if (!fgets(line, MAXREADLINESZ, stdin)) {
285 g_free(line);
286 return NULL;
287 }
288
289 p = line + strlen(line);
290 if (p != line && p[-1] == '\n') {
291 p[-1] = '\0';
292 }
293
294 return line;
295}
Stefan Hajnoczi0cf17e12013-11-14 11:54:17 +0100296
297static char *fetchline(void)
298{
299 if (readline_state) {
300 return fetchline_readline();
301 } else {
302 return fetchline_fgets();
303 }
304}
Kevin Wolfd1174f12013-06-05 14:19:37 +0200305
306static void prep_fetchline(void *opaque)
307{
308 int *fetchable = opaque;
309
310 qemu_set_fd_handler(STDIN_FILENO, NULL, NULL, NULL);
311 *fetchable= 1;
312}
313
314static void command_loop(void)
315{
316 int i, done = 0, fetchable = 0, prompted = 0;
317 char *input;
318
319 for (i = 0; !done && i < ncmdline; i++) {
Kevin Wolf3d219942013-06-05 14:19:39 +0200320 done = qemuio_command(qemuio_bs, cmdline[i]);
Kevin Wolfd1174f12013-06-05 14:19:37 +0200321 }
322 if (cmdline) {
323 g_free(cmdline);
324 return;
325 }
326
327 while (!done) {
328 if (!prompted) {
329 printf("%s", get_prompt());
330 fflush(stdout);
331 qemu_set_fd_handler(STDIN_FILENO, prep_fetchline, NULL, &fetchable);
332 prompted = 1;
333 }
334
335 main_loop_wait(false);
336
337 if (!fetchable) {
338 continue;
339 }
340
341 input = fetchline();
342 if (input == NULL) {
343 break;
344 }
Kevin Wolf3d219942013-06-05 14:19:39 +0200345 done = qemuio_command(qemuio_bs, input);
Kevin Wolfd1174f12013-06-05 14:19:37 +0200346 g_free(input);
347
348 prompted = 0;
349 fetchable = 0;
350 }
351 qemu_set_fd_handler(STDIN_FILENO, NULL, NULL, NULL);
352}
353
354static void add_user_command(char *optarg)
355{
Markus Armbruster5839e532014-08-19 10:31:08 +0200356 cmdline = g_renew(char *, cmdline, ++ncmdline);
Kevin Wolfd1174f12013-06-05 14:19:37 +0200357 cmdline[ncmdline-1] = optarg;
358}
359
Stefan Hajnoczi0cf17e12013-11-14 11:54:17 +0100360static void reenable_tty_echo(void)
361{
362 qemu_set_tty_echo(STDIN_FILENO, true);
363}
364
aliguorie3aff4f2009-04-05 19:14:04 +0000365int main(int argc, char **argv)
366{
Devin Nakamura43642b32011-07-11 11:22:16 -0400367 int readonly = 0;
368 int growable = 0;
Kevin Wolfbe6273d2014-11-20 16:27:06 +0100369 const char *sopt = "hVc:d:f:rsnmgkt:T:";
Devin Nakamura43642b32011-07-11 11:22:16 -0400370 const struct option lopt[] = {
371 { "help", 0, NULL, 'h' },
372 { "version", 0, NULL, 'V' },
373 { "offset", 1, NULL, 'o' },
374 { "cmd", 1, NULL, 'c' },
Kevin Wolfbe6273d2014-11-20 16:27:06 +0100375 { "format", 1, NULL, 'f' },
Devin Nakamura43642b32011-07-11 11:22:16 -0400376 { "read-only", 0, NULL, 'r' },
377 { "snapshot", 0, NULL, 's' },
378 { "nocache", 0, NULL, 'n' },
379 { "misalign", 0, NULL, 'm' },
380 { "growable", 0, NULL, 'g' },
381 { "native-aio", 0, NULL, 'k' },
Paolo Bonzini9e8f1832013-02-08 14:06:11 +0100382 { "discard", 1, NULL, 'd' },
Kevin Wolf592fa072012-04-18 12:07:39 +0200383 { "cache", 1, NULL, 't' },
Stefan Hajnoczid7bb72c2012-03-12 16:36:07 +0000384 { "trace", 1, NULL, 'T' },
Devin Nakamura43642b32011-07-11 11:22:16 -0400385 { NULL, 0, NULL, 0 }
386 };
387 int c;
388 int opt_index = 0;
Paolo Bonzini9e8f1832013-02-08 14:06:11 +0100389 int flags = BDRV_O_UNMAP;
Kevin Wolfbe6273d2014-11-20 16:27:06 +0100390 BlockDriver *drv = NULL;
Chrysostomos Nanakos2f78e492014-09-18 14:30:49 +0300391 Error *local_error = NULL;
aliguorie3aff4f2009-04-05 19:14:04 +0000392
MORITA Kazutaka526eda12013-07-23 17:30:11 +0900393#ifdef CONFIG_POSIX
394 signal(SIGPIPE, SIG_IGN);
395#endif
396
Devin Nakamura43642b32011-07-11 11:22:16 -0400397 progname = basename(argv[0]);
Fam Zheng10f5bff2014-02-10 14:48:51 +0800398 qemu_init_exec_dir(argv[0]);
aliguorie3aff4f2009-04-05 19:14:04 +0000399
Kevin Wolfbe6273d2014-11-20 16:27:06 +0100400 bdrv_init();
401
Devin Nakamura43642b32011-07-11 11:22:16 -0400402 while ((c = getopt_long(argc, argv, sopt, lopt, &opt_index)) != -1) {
403 switch (c) {
404 case 's':
405 flags |= BDRV_O_SNAPSHOT;
406 break;
407 case 'n':
408 flags |= BDRV_O_NOCACHE | BDRV_O_CACHE_WB;
409 break;
Paolo Bonzini9e8f1832013-02-08 14:06:11 +0100410 case 'd':
411 if (bdrv_parse_discard_flags(optarg, &flags) < 0) {
412 error_report("Invalid discard option: %s", optarg);
413 exit(1);
414 }
415 break;
Kevin Wolfbe6273d2014-11-20 16:27:06 +0100416 case 'f':
417 drv = bdrv_find_format(optarg);
418 if (!drv) {
419 error_report("Invalid format '%s'", optarg);
420 exit(EXIT_FAILURE);
421 }
422 break;
Devin Nakamura43642b32011-07-11 11:22:16 -0400423 case 'c':
424 add_user_command(optarg);
425 break;
426 case 'r':
427 readonly = 1;
428 break;
429 case 'm':
Stefan Weilf9883882014-03-05 22:23:00 +0100430 qemuio_misalign = true;
Devin Nakamura43642b32011-07-11 11:22:16 -0400431 break;
432 case 'g':
433 growable = 1;
434 break;
435 case 'k':
436 flags |= BDRV_O_NATIVE_AIO;
437 break;
Kevin Wolf592fa072012-04-18 12:07:39 +0200438 case 't':
439 if (bdrv_parse_cache_flags(optarg, &flags) < 0) {
440 error_report("Invalid cache option: %s", optarg);
441 exit(1);
442 }
443 break;
Stefan Hajnoczid7bb72c2012-03-12 16:36:07 +0000444 case 'T':
LluĂ­s Vilanova5b808272014-05-27 15:02:14 +0200445 if (!trace_init_backends(optarg, NULL)) {
Stefan Hajnoczid7bb72c2012-03-12 16:36:07 +0000446 exit(1); /* error message will have been printed */
447 }
448 break;
Devin Nakamura43642b32011-07-11 11:22:16 -0400449 case 'V':
Kevin Wolf02da3862013-06-05 14:19:40 +0200450 printf("%s version %s\n", progname, QEMU_VERSION);
Devin Nakamura43642b32011-07-11 11:22:16 -0400451 exit(0);
452 case 'h':
453 usage(progname);
454 exit(0);
455 default:
456 usage(progname);
457 exit(1);
Naphtali Spreif5edb012010-01-17 16:48:13 +0200458 }
Devin Nakamura43642b32011-07-11 11:22:16 -0400459 }
aliguorie3aff4f2009-04-05 19:14:04 +0000460
Devin Nakamura43642b32011-07-11 11:22:16 -0400461 if ((argc - optind) > 1) {
462 usage(progname);
463 exit(1);
464 }
aliguorie3aff4f2009-04-05 19:14:04 +0000465
Chrysostomos Nanakos2f78e492014-09-18 14:30:49 +0300466 if (qemu_init_main_loop(&local_error)) {
Markus Armbruster565f65d2015-02-12 13:55:05 +0100467 error_report_err(local_error);
Chrysostomos Nanakos2f78e492014-09-18 14:30:49 +0300468 exit(1);
469 }
Zhi Yong Wua57d1142012-02-19 22:24:59 +0800470
Devin Nakamura43642b32011-07-11 11:22:16 -0400471 /* initialize commands */
Kevin Wolfc2cdf5c2013-06-05 14:19:36 +0200472 qemuio_add_command(&quit_cmd);
473 qemuio_add_command(&open_cmd);
474 qemuio_add_command(&close_cmd);
Devin Nakamura43642b32011-07-11 11:22:16 -0400475
Stefan Hajnoczi0cf17e12013-11-14 11:54:17 +0100476 if (isatty(STDIN_FILENO)) {
477 readline_state = readline_init(readline_printf_func,
478 readline_flush_func,
479 NULL,
480 readline_completion_func);
481 qemu_set_tty_echo(STDIN_FILENO, false);
482 atexit(reenable_tty_echo);
483 }
484
Devin Nakamura43642b32011-07-11 11:22:16 -0400485 /* open the device */
486 if (!readonly) {
487 flags |= BDRV_O_RDWR;
488 }
489
490 if ((argc - optind) == 1) {
Kevin Wolfbe6273d2014-11-20 16:27:06 +0100491 openfile(argv[optind], drv, flags, growable, NULL);
Devin Nakamura43642b32011-07-11 11:22:16 -0400492 }
493 command_loop();
494
495 /*
Stefan Hajnoczi922453b2011-11-30 12:23:43 +0000496 * Make sure all outstanding requests complete before the program exits.
Devin Nakamura43642b32011-07-11 11:22:16 -0400497 */
Stefan Hajnoczi922453b2011-11-30 12:23:43 +0000498 bdrv_drain_all();
Devin Nakamura43642b32011-07-11 11:22:16 -0400499
Markus Armbruster26f54e92014-10-07 13:59:04 +0200500 blk_unref(qemuio_blk);
Stefan Hajnoczi0cf17e12013-11-14 11:54:17 +0100501 g_free(readline_state);
Devin Nakamura43642b32011-07-11 11:22:16 -0400502 return 0;
aliguorie3aff4f2009-04-05 19:14:04 +0000503}