blob: b55a550f27097af016bc8b02f7bc7fc40f83e955 [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"
Paolo Bonzini737e1502012-12-17 18:19:44 +010022#include "block/block_int.h"
Stefan Hajnoczid7bb72c2012-03-12 16:36:07 +000023#include "trace/control.h"
aliguorie3aff4f2009-04-05 19:14:04 +000024
Devin Nakamura43642b32011-07-11 11:22:16 -040025#define CMD_NOFILE_OK 0x01
aliguorie3aff4f2009-04-05 19:14:04 +000026
Stefan Weilf9883882014-03-05 22:23:00 +010027static char *progname;
aliguorie3aff4f2009-04-05 19:14:04 +000028
Stefan Weilf9883882014-03-05 22:23:00 +010029static BlockDriverState *qemuio_bs;
Kevin Wolf191c2892010-09-16 13:18:08 +020030
Kevin Wolfd1174f12013-06-05 14:19:37 +020031/* qemu-io commands passed using -c */
32static int ncmdline;
33static char **cmdline;
34
Stefan Hajnoczi0cf17e12013-11-14 11:54:17 +010035static ReadLineState *readline_state;
36
Kevin Wolf734c3b82013-06-05 14:19:30 +020037static int close_f(BlockDriverState *bs, int argc, char **argv)
aliguorie3aff4f2009-04-05 19:14:04 +000038{
Fam Zheng4f6fd342013-08-23 09:14:47 +080039 bdrv_unref(bs);
Kevin Wolf734c3b82013-06-05 14:19:30 +020040 qemuio_bs = NULL;
Devin Nakamura43642b32011-07-11 11:22:16 -040041 return 0;
aliguorie3aff4f2009-04-05 19:14:04 +000042}
43
44static const cmdinfo_t close_cmd = {
Devin Nakamura43642b32011-07-11 11:22:16 -040045 .name = "close",
46 .altname = "c",
47 .cfunc = close_f,
48 .oneline = "close the current open file",
aliguorie3aff4f2009-04-05 19:14:04 +000049};
50
Max Reitzb543c5c2013-10-11 14:02:10 +020051static int openfile(char *name, int flags, int growable, QDict *opts)
aliguorie3aff4f2009-04-05 19:14:04 +000052{
Max Reitz34b5d2c2013-09-05 14:45:29 +020053 Error *local_err = NULL;
54
Kevin Wolf734c3b82013-06-05 14:19:30 +020055 if (qemuio_bs) {
Devin Nakamura43642b32011-07-11 11:22:16 -040056 fprintf(stderr, "file open already, try 'help close'\n");
Markus Armbruster29f26012014-05-28 11:16:59 +020057 QDECREF(opts);
Devin Nakamura43642b32011-07-11 11:22:16 -040058 return 1;
59 }
aliguorie3aff4f2009-04-05 19:14:04 +000060
Devin Nakamura43642b32011-07-11 11:22:16 -040061 if (growable) {
Max Reitz2e401342014-02-18 18:33:07 +010062 if (bdrv_open(&qemuio_bs, name, NULL, opts, flags | BDRV_O_PROTOCOL,
63 NULL, &local_err))
64 {
Markus Armbruster543f7be2014-05-28 11:17:00 +020065 fprintf(stderr, "%s: can't open%s%s: %s\n", progname,
66 name ? " device " : "", name ?: "",
Max Reitz34b5d2c2013-09-05 14:45:29 +020067 error_get_pretty(local_err));
68 error_free(local_err);
Devin Nakamura43642b32011-07-11 11:22:16 -040069 return 1;
70 }
71 } else {
Kevin Wolf98522f62014-04-17 13:16:01 +020072 qemuio_bs = bdrv_new("hda", &error_abort);
Christoph Hellwig6db95602010-04-05 16:53:57 +020073
Max Reitzddf56362014-02-18 18:33:06 +010074 if (bdrv_open(&qemuio_bs, name, NULL, opts, flags, NULL, &local_err)
75 < 0)
76 {
Markus Armbruster543f7be2014-05-28 11:17:00 +020077 fprintf(stderr, "%s: can't open%s%s: %s\n", progname,
78 name ? " device " : "", name ?: "",
Max Reitz34b5d2c2013-09-05 14:45:29 +020079 error_get_pretty(local_err));
80 error_free(local_err);
Fam Zheng4f6fd342013-08-23 09:14:47 +080081 bdrv_unref(qemuio_bs);
Kevin Wolf734c3b82013-06-05 14:19:30 +020082 qemuio_bs = NULL;
Devin Nakamura43642b32011-07-11 11:22:16 -040083 return 1;
84 }
85 }
Christoph Hellwig1db69472009-07-15 23:11:21 +020086
Devin Nakamura43642b32011-07-11 11:22:16 -040087 return 0;
aliguorie3aff4f2009-04-05 19:14:04 +000088}
89
Devin Nakamura43642b32011-07-11 11:22:16 -040090static void open_help(void)
aliguorie3aff4f2009-04-05 19:14:04 +000091{
Devin Nakamura43642b32011-07-11 11:22:16 -040092 printf(
aliguorie3aff4f2009-04-05 19:14:04 +000093"\n"
94" opens a new file in the requested mode\n"
95"\n"
96" Example:\n"
97" 'open -Cn /tmp/data' - creates/opens data file read-write and uncached\n"
98"\n"
99" Opens a file for subsequent use by all of the other qemu-io commands.\n"
aliguorie3aff4f2009-04-05 19:14:04 +0000100" -r, -- open file read-only\n"
101" -s, -- use snapshot file\n"
102" -n, -- disable host cache\n"
Max Reitzb543c5c2013-10-11 14:02:10 +0200103" -g, -- allow file to grow (only applies to protocols)\n"
104" -o, -- options to be given to the block driver"
aliguorie3aff4f2009-04-05 19:14:04 +0000105"\n");
106}
107
Kevin Wolf734c3b82013-06-05 14:19:30 +0200108static int open_f(BlockDriverState *bs, int argc, char **argv);
Blue Swirl22a2bdc2009-11-21 09:06:46 +0000109
110static const cmdinfo_t open_cmd = {
Devin Nakamura43642b32011-07-11 11:22:16 -0400111 .name = "open",
112 .altname = "o",
113 .cfunc = open_f,
114 .argmin = 1,
115 .argmax = -1,
116 .flags = CMD_NOFILE_OK,
Max Reitzb543c5c2013-10-11 14:02:10 +0200117 .args = "[-Crsn] [-o options] [path]",
Devin Nakamura43642b32011-07-11 11:22:16 -0400118 .oneline = "open the file specified by path",
119 .help = open_help,
Blue Swirl22a2bdc2009-11-21 09:06:46 +0000120};
aliguorie3aff4f2009-04-05 19:14:04 +0000121
Max Reitzb543c5c2013-10-11 14:02:10 +0200122static QemuOptsList empty_opts = {
123 .name = "drive",
Markus Armbruster443422f2014-05-28 11:16:58 +0200124 .merge_lists = true,
Max Reitzb543c5c2013-10-11 14:02:10 +0200125 .head = QTAILQ_HEAD_INITIALIZER(empty_opts.head),
126 .desc = {
127 /* no elements => accept any params */
128 { /* end of list */ }
129 },
130};
131
Kevin Wolf734c3b82013-06-05 14:19:30 +0200132static int open_f(BlockDriverState *bs, int argc, char **argv)
aliguorie3aff4f2009-04-05 19:14:04 +0000133{
Devin Nakamura43642b32011-07-11 11:22:16 -0400134 int flags = 0;
135 int readonly = 0;
136 int growable = 0;
137 int c;
Max Reitzb543c5c2013-10-11 14:02:10 +0200138 QemuOpts *qopts;
Markus Armbruster443422f2014-05-28 11:16:58 +0200139 QDict *opts;
aliguorie3aff4f2009-04-05 19:14:04 +0000140
Max Reitzb543c5c2013-10-11 14:02:10 +0200141 while ((c = getopt(argc, argv, "snrgo:")) != EOF) {
Devin Nakamura43642b32011-07-11 11:22:16 -0400142 switch (c) {
143 case 's':
144 flags |= BDRV_O_SNAPSHOT;
145 break;
146 case 'n':
147 flags |= BDRV_O_NOCACHE | BDRV_O_CACHE_WB;
148 break;
149 case 'r':
150 readonly = 1;
151 break;
152 case 'g':
153 growable = 1;
154 break;
Max Reitzb543c5c2013-10-11 14:02:10 +0200155 case 'o':
Markus Armbruster443422f2014-05-28 11:16:58 +0200156 if (!qemu_opts_parse(&empty_opts, optarg, 0)) {
Max Reitzb543c5c2013-10-11 14:02:10 +0200157 printf("could not parse option list -- %s\n", optarg);
Markus Armbruster443422f2014-05-28 11:16:58 +0200158 qemu_opts_reset(&empty_opts);
Max Reitzb543c5c2013-10-11 14:02:10 +0200159 return 0;
160 }
Max Reitzb543c5c2013-10-11 14:02:10 +0200161 break;
Devin Nakamura43642b32011-07-11 11:22:16 -0400162 default:
Markus Armbruster443422f2014-05-28 11:16:58 +0200163 qemu_opts_reset(&empty_opts);
Kevin Wolfc2cdf5c2013-06-05 14:19:36 +0200164 return qemuio_command_usage(&open_cmd);
Naphtali Spreif5edb012010-01-17 16:48:13 +0200165 }
Devin Nakamura43642b32011-07-11 11:22:16 -0400166 }
aliguorie3aff4f2009-04-05 19:14:04 +0000167
Devin Nakamura43642b32011-07-11 11:22:16 -0400168 if (!readonly) {
169 flags |= BDRV_O_RDWR;
170 }
aliguorie3aff4f2009-04-05 19:14:04 +0000171
Markus Armbruster443422f2014-05-28 11:16:58 +0200172 qopts = qemu_opts_find(&empty_opts, NULL);
173 opts = qopts ? qemu_opts_to_qdict(qopts, NULL) : NULL;
174 qemu_opts_reset(&empty_opts);
175
Max Reitzfd0fee32013-12-20 19:28:20 +0100176 if (optind == argc - 1) {
177 return openfile(argv[optind], flags, growable, opts);
178 } else if (optind == argc) {
179 return openfile(NULL, flags, growable, opts);
180 } else {
Markus Armbruster29f26012014-05-28 11:16:59 +0200181 QDECREF(opts);
Kevin Wolfc2cdf5c2013-06-05 14:19:36 +0200182 return qemuio_command_usage(&open_cmd);
Devin Nakamura43642b32011-07-11 11:22:16 -0400183 }
aliguorie3aff4f2009-04-05 19:14:04 +0000184}
185
Kevin Wolfe681be72013-06-05 14:19:34 +0200186static int quit_f(BlockDriverState *bs, int argc, char **argv)
187{
188 return 1;
189}
190
191static const cmdinfo_t quit_cmd = {
192 .name = "quit",
193 .altname = "q",
194 .cfunc = quit_f,
195 .argmin = -1,
196 .argmax = -1,
197 .flags = CMD_FLAG_GLOBAL,
198 .oneline = "exit the program",
199};
200
aliguorie3aff4f2009-04-05 19:14:04 +0000201static void usage(const char *name)
202{
Devin Nakamura43642b32011-07-11 11:22:16 -0400203 printf(
Maria Kustovad208cc32014-03-18 09:59:19 +0400204"Usage: %s [-h] [-V] [-rsnm] [-c STRING] ... [file]\n"
Stefan Weil84844a22009-06-22 15:08:47 +0200205"QEMU Disk exerciser\n"
aliguorie3aff4f2009-04-05 19:14:04 +0000206"\n"
Maria Kustovad208cc32014-03-18 09:59:19 +0400207" -c, --cmd STRING execute command with its arguments\n"
208" from the given string\n"
aliguorie3aff4f2009-04-05 19:14:04 +0000209" -r, --read-only export read-only\n"
210" -s, --snapshot use snapshot file\n"
211" -n, --nocache disable host cache\n"
Christoph Hellwig1db69472009-07-15 23:11:21 +0200212" -g, --growable allow file to grow (only applies to protocols)\n"
aliguorie3aff4f2009-04-05 19:14:04 +0000213" -m, --misalign misalign allocations for O_DIRECT\n"
Christoph Hellwig5c6c3a62009-08-20 16:58:35 +0200214" -k, --native-aio use kernel AIO implementation (on Linux only)\n"
Kevin Wolf592fa072012-04-18 12:07:39 +0200215" -t, --cache=MODE use the given cache mode for the image\n"
Stefan Hajnoczid7bb72c2012-03-12 16:36:07 +0000216" -T, --trace FILE enable trace events listed in the given file\n"
aliguorie3aff4f2009-04-05 19:14:04 +0000217" -h, --help display this help and exit\n"
218" -V, --version output version information and exit\n"
Maria Kustovad208cc32014-03-18 09:59:19 +0400219"\n"
220"See '%s -c help' for information on available commands."
aliguorie3aff4f2009-04-05 19:14:04 +0000221"\n",
Maria Kustovad208cc32014-03-18 09:59:19 +0400222 name, name);
aliguorie3aff4f2009-04-05 19:14:04 +0000223}
224
Kevin Wolfd1174f12013-06-05 14:19:37 +0200225static char *get_prompt(void)
226{
227 static char prompt[FILENAME_MAX + 2 /*"> "*/ + 1 /*"\0"*/ ];
228
229 if (!prompt[0]) {
230 snprintf(prompt, sizeof(prompt), "%s> ", progname);
231 }
232
233 return prompt;
234}
235
Stefan Weild5d15072014-01-25 18:18:23 +0100236static void GCC_FMT_ATTR(2, 3) readline_printf_func(void *opaque,
237 const char *fmt, ...)
Kevin Wolfd1174f12013-06-05 14:19:37 +0200238{
Stefan Hajnoczi0cf17e12013-11-14 11:54:17 +0100239 va_list ap;
240 va_start(ap, fmt);
241 vprintf(fmt, ap);
242 va_end(ap);
243}
244
245static void readline_flush_func(void *opaque)
246{
247 fflush(stdout);
248}
249
250static void readline_func(void *opaque, const char *str, void *readline_opaque)
251{
252 char **line = readline_opaque;
253 *line = g_strdup(str);
254}
255
Stefan Hajnoczi46940202013-11-14 11:54:18 +0100256static void completion_match(const char *cmd, void *opaque)
257{
258 readline_add_completion(readline_state, cmd);
259}
260
Stefan Hajnoczi0cf17e12013-11-14 11:54:17 +0100261static void readline_completion_func(void *opaque, const char *str)
262{
Stefan Hajnoczi46940202013-11-14 11:54:18 +0100263 readline_set_completion_index(readline_state, strlen(str));
264 qemuio_complete_command(str, completion_match, NULL);
Stefan Hajnoczi0cf17e12013-11-14 11:54:17 +0100265}
266
267static char *fetchline_readline(void)
268{
269 char *line = NULL;
270
271 readline_start(readline_state, get_prompt(), 0, readline_func, &line);
272 while (!line) {
273 int ch = getchar();
274 if (ch == EOF) {
275 break;
276 }
277 readline_handle_byte(readline_state, ch);
Kevin Wolfd1174f12013-06-05 14:19:37 +0200278 }
279 return line;
280}
Kevin Wolfd1174f12013-06-05 14:19:37 +0200281
Stefan Hajnoczi0cf17e12013-11-14 11:54:17 +0100282#define MAXREADLINESZ 1024
283static char *fetchline_fgets(void)
Kevin Wolfd1174f12013-06-05 14:19:37 +0200284{
285 char *p, *line = g_malloc(MAXREADLINESZ);
286
287 if (!fgets(line, MAXREADLINESZ, stdin)) {
288 g_free(line);
289 return NULL;
290 }
291
292 p = line + strlen(line);
293 if (p != line && p[-1] == '\n') {
294 p[-1] = '\0';
295 }
296
297 return line;
298}
Stefan Hajnoczi0cf17e12013-11-14 11:54:17 +0100299
300static char *fetchline(void)
301{
302 if (readline_state) {
303 return fetchline_readline();
304 } else {
305 return fetchline_fgets();
306 }
307}
Kevin Wolfd1174f12013-06-05 14:19:37 +0200308
309static void prep_fetchline(void *opaque)
310{
311 int *fetchable = opaque;
312
313 qemu_set_fd_handler(STDIN_FILENO, NULL, NULL, NULL);
314 *fetchable= 1;
315}
316
317static void command_loop(void)
318{
319 int i, done = 0, fetchable = 0, prompted = 0;
320 char *input;
321
322 for (i = 0; !done && i < ncmdline; i++) {
Kevin Wolf3d219942013-06-05 14:19:39 +0200323 done = qemuio_command(qemuio_bs, cmdline[i]);
Kevin Wolfd1174f12013-06-05 14:19:37 +0200324 }
325 if (cmdline) {
326 g_free(cmdline);
327 return;
328 }
329
330 while (!done) {
331 if (!prompted) {
332 printf("%s", get_prompt());
333 fflush(stdout);
334 qemu_set_fd_handler(STDIN_FILENO, prep_fetchline, NULL, &fetchable);
335 prompted = 1;
336 }
337
338 main_loop_wait(false);
339
340 if (!fetchable) {
341 continue;
342 }
343
344 input = fetchline();
345 if (input == NULL) {
346 break;
347 }
Kevin Wolf3d219942013-06-05 14:19:39 +0200348 done = qemuio_command(qemuio_bs, input);
Kevin Wolfd1174f12013-06-05 14:19:37 +0200349 g_free(input);
350
351 prompted = 0;
352 fetchable = 0;
353 }
354 qemu_set_fd_handler(STDIN_FILENO, NULL, NULL, NULL);
355}
356
357static void add_user_command(char *optarg)
358{
359 cmdline = g_realloc(cmdline, ++ncmdline * sizeof(char *));
360 cmdline[ncmdline-1] = optarg;
361}
362
Stefan Hajnoczi0cf17e12013-11-14 11:54:17 +0100363static void reenable_tty_echo(void)
364{
365 qemu_set_tty_echo(STDIN_FILENO, true);
366}
367
aliguorie3aff4f2009-04-05 19:14:04 +0000368int main(int argc, char **argv)
369{
Devin Nakamura43642b32011-07-11 11:22:16 -0400370 int readonly = 0;
371 int growable = 0;
Paolo Bonzini9e8f1832013-02-08 14:06:11 +0100372 const char *sopt = "hVc:d:rsnmgkt:T:";
Devin Nakamura43642b32011-07-11 11:22:16 -0400373 const struct option lopt[] = {
374 { "help", 0, NULL, 'h' },
375 { "version", 0, NULL, 'V' },
376 { "offset", 1, NULL, 'o' },
377 { "cmd", 1, NULL, 'c' },
378 { "read-only", 0, NULL, 'r' },
379 { "snapshot", 0, NULL, 's' },
380 { "nocache", 0, NULL, 'n' },
381 { "misalign", 0, NULL, 'm' },
382 { "growable", 0, NULL, 'g' },
383 { "native-aio", 0, NULL, 'k' },
Paolo Bonzini9e8f1832013-02-08 14:06:11 +0100384 { "discard", 1, NULL, 'd' },
Kevin Wolf592fa072012-04-18 12:07:39 +0200385 { "cache", 1, NULL, 't' },
Stefan Hajnoczid7bb72c2012-03-12 16:36:07 +0000386 { "trace", 1, NULL, 'T' },
Devin Nakamura43642b32011-07-11 11:22:16 -0400387 { NULL, 0, NULL, 0 }
388 };
389 int c;
390 int opt_index = 0;
Paolo Bonzini9e8f1832013-02-08 14:06:11 +0100391 int flags = BDRV_O_UNMAP;
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
Devin Nakamura43642b32011-07-11 11:22:16 -0400400 while ((c = getopt_long(argc, argv, sopt, lopt, &opt_index)) != -1) {
401 switch (c) {
402 case 's':
403 flags |= BDRV_O_SNAPSHOT;
404 break;
405 case 'n':
406 flags |= BDRV_O_NOCACHE | BDRV_O_CACHE_WB;
407 break;
Paolo Bonzini9e8f1832013-02-08 14:06:11 +0100408 case 'd':
409 if (bdrv_parse_discard_flags(optarg, &flags) < 0) {
410 error_report("Invalid discard option: %s", optarg);
411 exit(1);
412 }
413 break;
Devin Nakamura43642b32011-07-11 11:22:16 -0400414 case 'c':
415 add_user_command(optarg);
416 break;
417 case 'r':
418 readonly = 1;
419 break;
420 case 'm':
Stefan Weilf9883882014-03-05 22:23:00 +0100421 qemuio_misalign = true;
Devin Nakamura43642b32011-07-11 11:22:16 -0400422 break;
423 case 'g':
424 growable = 1;
425 break;
426 case 'k':
427 flags |= BDRV_O_NATIVE_AIO;
428 break;
Kevin Wolf592fa072012-04-18 12:07:39 +0200429 case 't':
430 if (bdrv_parse_cache_flags(optarg, &flags) < 0) {
431 error_report("Invalid cache option: %s", optarg);
432 exit(1);
433 }
434 break;
Stefan Hajnoczid7bb72c2012-03-12 16:36:07 +0000435 case 'T':
LluĂ­s Vilanova5b808272014-05-27 15:02:14 +0200436 if (!trace_init_backends(optarg, NULL)) {
Stefan Hajnoczid7bb72c2012-03-12 16:36:07 +0000437 exit(1); /* error message will have been printed */
438 }
439 break;
Devin Nakamura43642b32011-07-11 11:22:16 -0400440 case 'V':
Kevin Wolf02da3862013-06-05 14:19:40 +0200441 printf("%s version %s\n", progname, QEMU_VERSION);
Devin Nakamura43642b32011-07-11 11:22:16 -0400442 exit(0);
443 case 'h':
444 usage(progname);
445 exit(0);
446 default:
447 usage(progname);
448 exit(1);
Naphtali Spreif5edb012010-01-17 16:48:13 +0200449 }
Devin Nakamura43642b32011-07-11 11:22:16 -0400450 }
aliguorie3aff4f2009-04-05 19:14:04 +0000451
Devin Nakamura43642b32011-07-11 11:22:16 -0400452 if ((argc - optind) > 1) {
453 usage(progname);
454 exit(1);
455 }
aliguorie3aff4f2009-04-05 19:14:04 +0000456
Zhi Yong Wua57d1142012-02-19 22:24:59 +0800457 qemu_init_main_loop();
Paolo Bonzini2592c592012-11-03 18:10:17 +0100458 bdrv_init();
Zhi Yong Wua57d1142012-02-19 22:24:59 +0800459
Devin Nakamura43642b32011-07-11 11:22:16 -0400460 /* initialize commands */
Kevin Wolfc2cdf5c2013-06-05 14:19:36 +0200461 qemuio_add_command(&quit_cmd);
462 qemuio_add_command(&open_cmd);
463 qemuio_add_command(&close_cmd);
Devin Nakamura43642b32011-07-11 11:22:16 -0400464
Stefan Hajnoczi0cf17e12013-11-14 11:54:17 +0100465 if (isatty(STDIN_FILENO)) {
466 readline_state = readline_init(readline_printf_func,
467 readline_flush_func,
468 NULL,
469 readline_completion_func);
470 qemu_set_tty_echo(STDIN_FILENO, false);
471 atexit(reenable_tty_echo);
472 }
473
Devin Nakamura43642b32011-07-11 11:22:16 -0400474 /* open the device */
475 if (!readonly) {
476 flags |= BDRV_O_RDWR;
477 }
478
479 if ((argc - optind) == 1) {
Max Reitzb543c5c2013-10-11 14:02:10 +0200480 openfile(argv[optind], flags, growable, NULL);
Devin Nakamura43642b32011-07-11 11:22:16 -0400481 }
482 command_loop();
483
484 /*
Stefan Hajnoczi922453b2011-11-30 12:23:43 +0000485 * Make sure all outstanding requests complete before the program exits.
Devin Nakamura43642b32011-07-11 11:22:16 -0400486 */
Stefan Hajnoczi922453b2011-11-30 12:23:43 +0000487 bdrv_drain_all();
Devin Nakamura43642b32011-07-11 11:22:16 -0400488
Kevin Wolf734c3b82013-06-05 14:19:30 +0200489 if (qemuio_bs) {
Fam Zheng4f6fd342013-08-23 09:14:47 +0800490 bdrv_unref(qemuio_bs);
Devin Nakamura43642b32011-07-11 11:22:16 -0400491 }
Stefan Hajnoczi0cf17e12013-11-14 11:54:17 +0100492 g_free(readline_state);
Devin Nakamura43642b32011-07-11 11:22:16 -0400493 return 0;
aliguorie3aff4f2009-04-05 19:14:04 +0000494}