blob: f63e771d9860ef6f513afdda5a4133875c5d50c2 [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 {
Max Reitz34b5d2c2013-09-05 14:45:29 +020065 fprintf(stderr, "%s: can't open device %s: %s\n", progname, name,
66 error_get_pretty(local_err));
67 error_free(local_err);
Devin Nakamura43642b32011-07-11 11:22:16 -040068 return 1;
69 }
70 } else {
Kevin Wolf98522f62014-04-17 13:16:01 +020071 qemuio_bs = bdrv_new("hda", &error_abort);
Christoph Hellwig6db95602010-04-05 16:53:57 +020072
Max Reitzddf56362014-02-18 18:33:06 +010073 if (bdrv_open(&qemuio_bs, name, NULL, opts, flags, NULL, &local_err)
74 < 0)
75 {
Max Reitz34b5d2c2013-09-05 14:45:29 +020076 fprintf(stderr, "%s: can't open device %s: %s\n", progname, name,
77 error_get_pretty(local_err));
78 error_free(local_err);
Fam Zheng4f6fd342013-08-23 09:14:47 +080079 bdrv_unref(qemuio_bs);
Kevin Wolf734c3b82013-06-05 14:19:30 +020080 qemuio_bs = NULL;
Devin Nakamura43642b32011-07-11 11:22:16 -040081 return 1;
82 }
83 }
Christoph Hellwig1db69472009-07-15 23:11:21 +020084
Devin Nakamura43642b32011-07-11 11:22:16 -040085 return 0;
aliguorie3aff4f2009-04-05 19:14:04 +000086}
87
Devin Nakamura43642b32011-07-11 11:22:16 -040088static void open_help(void)
aliguorie3aff4f2009-04-05 19:14:04 +000089{
Devin Nakamura43642b32011-07-11 11:22:16 -040090 printf(
aliguorie3aff4f2009-04-05 19:14:04 +000091"\n"
92" opens a new file in the requested mode\n"
93"\n"
94" Example:\n"
95" 'open -Cn /tmp/data' - creates/opens data file read-write and uncached\n"
96"\n"
97" Opens a file for subsequent use by all of the other qemu-io commands.\n"
aliguorie3aff4f2009-04-05 19:14:04 +000098" -r, -- open file read-only\n"
99" -s, -- use snapshot file\n"
100" -n, -- disable host cache\n"
Max Reitzb543c5c2013-10-11 14:02:10 +0200101" -g, -- allow file to grow (only applies to protocols)\n"
102" -o, -- options to be given to the block driver"
aliguorie3aff4f2009-04-05 19:14:04 +0000103"\n");
104}
105
Kevin Wolf734c3b82013-06-05 14:19:30 +0200106static int open_f(BlockDriverState *bs, int argc, char **argv);
Blue Swirl22a2bdc2009-11-21 09:06:46 +0000107
108static const cmdinfo_t open_cmd = {
Devin Nakamura43642b32011-07-11 11:22:16 -0400109 .name = "open",
110 .altname = "o",
111 .cfunc = open_f,
112 .argmin = 1,
113 .argmax = -1,
114 .flags = CMD_NOFILE_OK,
Max Reitzb543c5c2013-10-11 14:02:10 +0200115 .args = "[-Crsn] [-o options] [path]",
Devin Nakamura43642b32011-07-11 11:22:16 -0400116 .oneline = "open the file specified by path",
117 .help = open_help,
Blue Swirl22a2bdc2009-11-21 09:06:46 +0000118};
aliguorie3aff4f2009-04-05 19:14:04 +0000119
Max Reitzb543c5c2013-10-11 14:02:10 +0200120static QemuOptsList empty_opts = {
121 .name = "drive",
Markus Armbruster443422f2014-05-28 11:16:58 +0200122 .merge_lists = true,
Max Reitzb543c5c2013-10-11 14:02:10 +0200123 .head = QTAILQ_HEAD_INITIALIZER(empty_opts.head),
124 .desc = {
125 /* no elements => accept any params */
126 { /* end of list */ }
127 },
128};
129
Kevin Wolf734c3b82013-06-05 14:19:30 +0200130static int open_f(BlockDriverState *bs, int argc, char **argv)
aliguorie3aff4f2009-04-05 19:14:04 +0000131{
Devin Nakamura43642b32011-07-11 11:22:16 -0400132 int flags = 0;
133 int readonly = 0;
134 int growable = 0;
135 int c;
Max Reitzb543c5c2013-10-11 14:02:10 +0200136 QemuOpts *qopts;
Markus Armbruster443422f2014-05-28 11:16:58 +0200137 QDict *opts;
aliguorie3aff4f2009-04-05 19:14:04 +0000138
Max Reitzb543c5c2013-10-11 14:02:10 +0200139 while ((c = getopt(argc, argv, "snrgo:")) != EOF) {
Devin Nakamura43642b32011-07-11 11:22:16 -0400140 switch (c) {
141 case 's':
142 flags |= BDRV_O_SNAPSHOT;
143 break;
144 case 'n':
145 flags |= BDRV_O_NOCACHE | BDRV_O_CACHE_WB;
146 break;
147 case 'r':
148 readonly = 1;
149 break;
150 case 'g':
151 growable = 1;
152 break;
Max Reitzb543c5c2013-10-11 14:02:10 +0200153 case 'o':
Markus Armbruster443422f2014-05-28 11:16:58 +0200154 if (!qemu_opts_parse(&empty_opts, optarg, 0)) {
Max Reitzb543c5c2013-10-11 14:02:10 +0200155 printf("could not parse option list -- %s\n", optarg);
Markus Armbruster443422f2014-05-28 11:16:58 +0200156 qemu_opts_reset(&empty_opts);
Max Reitzb543c5c2013-10-11 14:02:10 +0200157 return 0;
158 }
Max Reitzb543c5c2013-10-11 14:02:10 +0200159 break;
Devin Nakamura43642b32011-07-11 11:22:16 -0400160 default:
Markus Armbruster443422f2014-05-28 11:16:58 +0200161 qemu_opts_reset(&empty_opts);
Kevin Wolfc2cdf5c2013-06-05 14:19:36 +0200162 return qemuio_command_usage(&open_cmd);
Naphtali Spreif5edb012010-01-17 16:48:13 +0200163 }
Devin Nakamura43642b32011-07-11 11:22:16 -0400164 }
aliguorie3aff4f2009-04-05 19:14:04 +0000165
Devin Nakamura43642b32011-07-11 11:22:16 -0400166 if (!readonly) {
167 flags |= BDRV_O_RDWR;
168 }
aliguorie3aff4f2009-04-05 19:14:04 +0000169
Markus Armbruster443422f2014-05-28 11:16:58 +0200170 qopts = qemu_opts_find(&empty_opts, NULL);
171 opts = qopts ? qemu_opts_to_qdict(qopts, NULL) : NULL;
172 qemu_opts_reset(&empty_opts);
173
Max Reitzfd0fee32013-12-20 19:28:20 +0100174 if (optind == argc - 1) {
175 return openfile(argv[optind], flags, growable, opts);
176 } else if (optind == argc) {
177 return openfile(NULL, flags, growable, opts);
178 } else {
Markus Armbruster29f26012014-05-28 11:16:59 +0200179 QDECREF(opts);
Kevin Wolfc2cdf5c2013-06-05 14:19:36 +0200180 return qemuio_command_usage(&open_cmd);
Devin Nakamura43642b32011-07-11 11:22:16 -0400181 }
aliguorie3aff4f2009-04-05 19:14:04 +0000182}
183
Kevin Wolfe681be72013-06-05 14:19:34 +0200184static int quit_f(BlockDriverState *bs, int argc, char **argv)
185{
186 return 1;
187}
188
189static const cmdinfo_t quit_cmd = {
190 .name = "quit",
191 .altname = "q",
192 .cfunc = quit_f,
193 .argmin = -1,
194 .argmax = -1,
195 .flags = CMD_FLAG_GLOBAL,
196 .oneline = "exit the program",
197};
198
aliguorie3aff4f2009-04-05 19:14:04 +0000199static void usage(const char *name)
200{
Devin Nakamura43642b32011-07-11 11:22:16 -0400201 printf(
Maria Kustovad208cc32014-03-18 09:59:19 +0400202"Usage: %s [-h] [-V] [-rsnm] [-c STRING] ... [file]\n"
Stefan Weil84844a22009-06-22 15:08:47 +0200203"QEMU Disk exerciser\n"
aliguorie3aff4f2009-04-05 19:14:04 +0000204"\n"
Maria Kustovad208cc32014-03-18 09:59:19 +0400205" -c, --cmd STRING execute command with its arguments\n"
206" from the given string\n"
aliguorie3aff4f2009-04-05 19:14:04 +0000207" -r, --read-only export read-only\n"
208" -s, --snapshot use snapshot file\n"
209" -n, --nocache disable host cache\n"
Christoph Hellwig1db69472009-07-15 23:11:21 +0200210" -g, --growable allow file to grow (only applies to protocols)\n"
aliguorie3aff4f2009-04-05 19:14:04 +0000211" -m, --misalign misalign allocations for O_DIRECT\n"
Christoph Hellwig5c6c3a62009-08-20 16:58:35 +0200212" -k, --native-aio use kernel AIO implementation (on Linux only)\n"
Kevin Wolf592fa072012-04-18 12:07:39 +0200213" -t, --cache=MODE use the given cache mode for the image\n"
Stefan Hajnoczid7bb72c2012-03-12 16:36:07 +0000214" -T, --trace FILE enable trace events listed in the given file\n"
aliguorie3aff4f2009-04-05 19:14:04 +0000215" -h, --help display this help and exit\n"
216" -V, --version output version information and exit\n"
Maria Kustovad208cc32014-03-18 09:59:19 +0400217"\n"
218"See '%s -c help' for information on available commands."
aliguorie3aff4f2009-04-05 19:14:04 +0000219"\n",
Maria Kustovad208cc32014-03-18 09:59:19 +0400220 name, name);
aliguorie3aff4f2009-04-05 19:14:04 +0000221}
222
Kevin Wolfd1174f12013-06-05 14:19:37 +0200223static char *get_prompt(void)
224{
225 static char prompt[FILENAME_MAX + 2 /*"> "*/ + 1 /*"\0"*/ ];
226
227 if (!prompt[0]) {
228 snprintf(prompt, sizeof(prompt), "%s> ", progname);
229 }
230
231 return prompt;
232}
233
Stefan Weild5d15072014-01-25 18:18:23 +0100234static void GCC_FMT_ATTR(2, 3) readline_printf_func(void *opaque,
235 const char *fmt, ...)
Kevin Wolfd1174f12013-06-05 14:19:37 +0200236{
Stefan Hajnoczi0cf17e12013-11-14 11:54:17 +0100237 va_list ap;
238 va_start(ap, fmt);
239 vprintf(fmt, ap);
240 va_end(ap);
241}
242
243static void readline_flush_func(void *opaque)
244{
245 fflush(stdout);
246}
247
248static void readline_func(void *opaque, const char *str, void *readline_opaque)
249{
250 char **line = readline_opaque;
251 *line = g_strdup(str);
252}
253
Stefan Hajnoczi46940202013-11-14 11:54:18 +0100254static void completion_match(const char *cmd, void *opaque)
255{
256 readline_add_completion(readline_state, cmd);
257}
258
Stefan Hajnoczi0cf17e12013-11-14 11:54:17 +0100259static void readline_completion_func(void *opaque, const char *str)
260{
Stefan Hajnoczi46940202013-11-14 11:54:18 +0100261 readline_set_completion_index(readline_state, strlen(str));
262 qemuio_complete_command(str, completion_match, NULL);
Stefan Hajnoczi0cf17e12013-11-14 11:54:17 +0100263}
264
265static char *fetchline_readline(void)
266{
267 char *line = NULL;
268
269 readline_start(readline_state, get_prompt(), 0, readline_func, &line);
270 while (!line) {
271 int ch = getchar();
272 if (ch == EOF) {
273 break;
274 }
275 readline_handle_byte(readline_state, ch);
Kevin Wolfd1174f12013-06-05 14:19:37 +0200276 }
277 return line;
278}
Kevin Wolfd1174f12013-06-05 14:19:37 +0200279
Stefan Hajnoczi0cf17e12013-11-14 11:54:17 +0100280#define MAXREADLINESZ 1024
281static char *fetchline_fgets(void)
Kevin Wolfd1174f12013-06-05 14:19:37 +0200282{
283 char *p, *line = g_malloc(MAXREADLINESZ);
284
285 if (!fgets(line, MAXREADLINESZ, stdin)) {
286 g_free(line);
287 return NULL;
288 }
289
290 p = line + strlen(line);
291 if (p != line && p[-1] == '\n') {
292 p[-1] = '\0';
293 }
294
295 return line;
296}
Stefan Hajnoczi0cf17e12013-11-14 11:54:17 +0100297
298static char *fetchline(void)
299{
300 if (readline_state) {
301 return fetchline_readline();
302 } else {
303 return fetchline_fgets();
304 }
305}
Kevin Wolfd1174f12013-06-05 14:19:37 +0200306
307static void prep_fetchline(void *opaque)
308{
309 int *fetchable = opaque;
310
311 qemu_set_fd_handler(STDIN_FILENO, NULL, NULL, NULL);
312 *fetchable= 1;
313}
314
315static void command_loop(void)
316{
317 int i, done = 0, fetchable = 0, prompted = 0;
318 char *input;
319
320 for (i = 0; !done && i < ncmdline; i++) {
Kevin Wolf3d219942013-06-05 14:19:39 +0200321 done = qemuio_command(qemuio_bs, cmdline[i]);
Kevin Wolfd1174f12013-06-05 14:19:37 +0200322 }
323 if (cmdline) {
324 g_free(cmdline);
325 return;
326 }
327
328 while (!done) {
329 if (!prompted) {
330 printf("%s", get_prompt());
331 fflush(stdout);
332 qemu_set_fd_handler(STDIN_FILENO, prep_fetchline, NULL, &fetchable);
333 prompted = 1;
334 }
335
336 main_loop_wait(false);
337
338 if (!fetchable) {
339 continue;
340 }
341
342 input = fetchline();
343 if (input == NULL) {
344 break;
345 }
Kevin Wolf3d219942013-06-05 14:19:39 +0200346 done = qemuio_command(qemuio_bs, input);
Kevin Wolfd1174f12013-06-05 14:19:37 +0200347 g_free(input);
348
349 prompted = 0;
350 fetchable = 0;
351 }
352 qemu_set_fd_handler(STDIN_FILENO, NULL, NULL, NULL);
353}
354
355static void add_user_command(char *optarg)
356{
357 cmdline = g_realloc(cmdline, ++ncmdline * sizeof(char *));
358 cmdline[ncmdline-1] = optarg;
359}
360
Stefan Hajnoczi0cf17e12013-11-14 11:54:17 +0100361static void reenable_tty_echo(void)
362{
363 qemu_set_tty_echo(STDIN_FILENO, true);
364}
365
aliguorie3aff4f2009-04-05 19:14:04 +0000366int main(int argc, char **argv)
367{
Devin Nakamura43642b32011-07-11 11:22:16 -0400368 int readonly = 0;
369 int growable = 0;
Paolo Bonzini9e8f1832013-02-08 14:06:11 +0100370 const char *sopt = "hVc:d:rsnmgkt:T:";
Devin Nakamura43642b32011-07-11 11:22:16 -0400371 const struct option lopt[] = {
372 { "help", 0, NULL, 'h' },
373 { "version", 0, NULL, 'V' },
374 { "offset", 1, NULL, 'o' },
375 { "cmd", 1, NULL, 'c' },
376 { "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;
aliguorie3aff4f2009-04-05 19:14:04 +0000390
MORITA Kazutaka526eda12013-07-23 17:30:11 +0900391#ifdef CONFIG_POSIX
392 signal(SIGPIPE, SIG_IGN);
393#endif
394
Devin Nakamura43642b32011-07-11 11:22:16 -0400395 progname = basename(argv[0]);
Fam Zheng10f5bff2014-02-10 14:48:51 +0800396 qemu_init_exec_dir(argv[0]);
aliguorie3aff4f2009-04-05 19:14:04 +0000397
Devin Nakamura43642b32011-07-11 11:22:16 -0400398 while ((c = getopt_long(argc, argv, sopt, lopt, &opt_index)) != -1) {
399 switch (c) {
400 case 's':
401 flags |= BDRV_O_SNAPSHOT;
402 break;
403 case 'n':
404 flags |= BDRV_O_NOCACHE | BDRV_O_CACHE_WB;
405 break;
Paolo Bonzini9e8f1832013-02-08 14:06:11 +0100406 case 'd':
407 if (bdrv_parse_discard_flags(optarg, &flags) < 0) {
408 error_report("Invalid discard option: %s", optarg);
409 exit(1);
410 }
411 break;
Devin Nakamura43642b32011-07-11 11:22:16 -0400412 case 'c':
413 add_user_command(optarg);
414 break;
415 case 'r':
416 readonly = 1;
417 break;
418 case 'm':
Stefan Weilf9883882014-03-05 22:23:00 +0100419 qemuio_misalign = true;
Devin Nakamura43642b32011-07-11 11:22:16 -0400420 break;
421 case 'g':
422 growable = 1;
423 break;
424 case 'k':
425 flags |= BDRV_O_NATIVE_AIO;
426 break;
Kevin Wolf592fa072012-04-18 12:07:39 +0200427 case 't':
428 if (bdrv_parse_cache_flags(optarg, &flags) < 0) {
429 error_report("Invalid cache option: %s", optarg);
430 exit(1);
431 }
432 break;
Stefan Hajnoczid7bb72c2012-03-12 16:36:07 +0000433 case 'T':
434 if (!trace_backend_init(optarg, NULL)) {
435 exit(1); /* error message will have been printed */
436 }
437 break;
Devin Nakamura43642b32011-07-11 11:22:16 -0400438 case 'V':
Kevin Wolf02da3862013-06-05 14:19:40 +0200439 printf("%s version %s\n", progname, QEMU_VERSION);
Devin Nakamura43642b32011-07-11 11:22:16 -0400440 exit(0);
441 case 'h':
442 usage(progname);
443 exit(0);
444 default:
445 usage(progname);
446 exit(1);
Naphtali Spreif5edb012010-01-17 16:48:13 +0200447 }
Devin Nakamura43642b32011-07-11 11:22:16 -0400448 }
aliguorie3aff4f2009-04-05 19:14:04 +0000449
Devin Nakamura43642b32011-07-11 11:22:16 -0400450 if ((argc - optind) > 1) {
451 usage(progname);
452 exit(1);
453 }
aliguorie3aff4f2009-04-05 19:14:04 +0000454
Zhi Yong Wua57d1142012-02-19 22:24:59 +0800455 qemu_init_main_loop();
Paolo Bonzini2592c592012-11-03 18:10:17 +0100456 bdrv_init();
Zhi Yong Wua57d1142012-02-19 22:24:59 +0800457
Devin Nakamura43642b32011-07-11 11:22:16 -0400458 /* initialize commands */
Kevin Wolfc2cdf5c2013-06-05 14:19:36 +0200459 qemuio_add_command(&quit_cmd);
460 qemuio_add_command(&open_cmd);
461 qemuio_add_command(&close_cmd);
Devin Nakamura43642b32011-07-11 11:22:16 -0400462
Stefan Hajnoczi0cf17e12013-11-14 11:54:17 +0100463 if (isatty(STDIN_FILENO)) {
464 readline_state = readline_init(readline_printf_func,
465 readline_flush_func,
466 NULL,
467 readline_completion_func);
468 qemu_set_tty_echo(STDIN_FILENO, false);
469 atexit(reenable_tty_echo);
470 }
471
Devin Nakamura43642b32011-07-11 11:22:16 -0400472 /* open the device */
473 if (!readonly) {
474 flags |= BDRV_O_RDWR;
475 }
476
477 if ((argc - optind) == 1) {
Max Reitzb543c5c2013-10-11 14:02:10 +0200478 openfile(argv[optind], flags, growable, NULL);
Devin Nakamura43642b32011-07-11 11:22:16 -0400479 }
480 command_loop();
481
482 /*
Stefan Hajnoczi922453b2011-11-30 12:23:43 +0000483 * Make sure all outstanding requests complete before the program exits.
Devin Nakamura43642b32011-07-11 11:22:16 -0400484 */
Stefan Hajnoczi922453b2011-11-30 12:23:43 +0000485 bdrv_drain_all();
Devin Nakamura43642b32011-07-11 11:22:16 -0400486
Kevin Wolf734c3b82013-06-05 14:19:30 +0200487 if (qemuio_bs) {
Fam Zheng4f6fd342013-08-23 09:14:47 +0800488 bdrv_unref(qemuio_bs);
Devin Nakamura43642b32011-07-11 11:22:16 -0400489 }
Stefan Hajnoczi0cf17e12013-11-14 11:54:17 +0100490 g_free(readline_state);
Devin Nakamura43642b32011-07-11 11:22:16 -0400491 return 0;
aliguorie3aff4f2009-04-05 19:14:04 +0000492}