blob: 60f84dd72a5eb9e632855e37905cf2e96c2b54e0 [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 Reitzb543c5c2013-10-11 14:02:10 +020054static 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
Kevin Wolf734c3b82013-06-05 14:19:30 +020058 if (qemuio_bs) {
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 Armbruster7e7d56d2014-10-07 13:59:05 +020064 qemuio_blk = blk_new_with_bs("hda", &error_abort);
65 qemuio_bs = blk_bs(qemuio_blk);
Christoph Hellwig6db95602010-04-05 16:53:57 +020066
Markus Armbrusterdbb651c2014-09-08 18:50:58 +020067 if (growable) {
68 flags |= BDRV_O_PROTOCOL;
69 }
70
71 if (bdrv_open(&qemuio_bs, name, NULL, opts, flags, NULL, &local_err) < 0) {
72 fprintf(stderr, "%s: can't open%s%s: %s\n", progname,
73 name ? " device " : "", name ?: "",
74 error_get_pretty(local_err));
75 error_free(local_err);
Markus Armbruster26f54e92014-10-07 13:59:04 +020076 blk_unref(qemuio_blk);
Markus Armbrusterdbb651c2014-09-08 18:50:58 +020077 qemuio_bs = NULL;
Markus Armbruster26f54e92014-10-07 13:59:04 +020078 qemuio_blk = NULL;
Markus Armbrusterdbb651c2014-09-08 18:50:58 +020079 return 1;
Devin Nakamura43642b32011-07-11 11:22:16 -040080 }
Christoph Hellwig1db69472009-07-15 23:11:21 +020081
Devin Nakamura43642b32011-07-11 11:22:16 -040082 return 0;
aliguorie3aff4f2009-04-05 19:14:04 +000083}
84
Devin Nakamura43642b32011-07-11 11:22:16 -040085static void open_help(void)
aliguorie3aff4f2009-04-05 19:14:04 +000086{
Devin Nakamura43642b32011-07-11 11:22:16 -040087 printf(
aliguorie3aff4f2009-04-05 19:14:04 +000088"\n"
89" opens a new file in the requested mode\n"
90"\n"
91" Example:\n"
92" 'open -Cn /tmp/data' - creates/opens data file read-write and uncached\n"
93"\n"
94" Opens a file for subsequent use by all of the other qemu-io commands.\n"
aliguorie3aff4f2009-04-05 19:14:04 +000095" -r, -- open file read-only\n"
96" -s, -- use snapshot file\n"
97" -n, -- disable host cache\n"
Max Reitzb543c5c2013-10-11 14:02:10 +020098" -g, -- allow file to grow (only applies to protocols)\n"
99" -o, -- options to be given to the block driver"
aliguorie3aff4f2009-04-05 19:14:04 +0000100"\n");
101}
102
Kevin Wolf734c3b82013-06-05 14:19:30 +0200103static int open_f(BlockDriverState *bs, int argc, char **argv);
Blue Swirl22a2bdc2009-11-21 09:06:46 +0000104
105static const cmdinfo_t open_cmd = {
Devin Nakamura43642b32011-07-11 11:22:16 -0400106 .name = "open",
107 .altname = "o",
108 .cfunc = open_f,
109 .argmin = 1,
110 .argmax = -1,
111 .flags = CMD_NOFILE_OK,
Max Reitzb543c5c2013-10-11 14:02:10 +0200112 .args = "[-Crsn] [-o options] [path]",
Devin Nakamura43642b32011-07-11 11:22:16 -0400113 .oneline = "open the file specified by path",
114 .help = open_help,
Blue Swirl22a2bdc2009-11-21 09:06:46 +0000115};
aliguorie3aff4f2009-04-05 19:14:04 +0000116
Max Reitzb543c5c2013-10-11 14:02:10 +0200117static QemuOptsList empty_opts = {
118 .name = "drive",
Markus Armbruster443422f2014-05-28 11:16:58 +0200119 .merge_lists = true,
Max Reitzb543c5c2013-10-11 14:02:10 +0200120 .head = QTAILQ_HEAD_INITIALIZER(empty_opts.head),
121 .desc = {
122 /* no elements => accept any params */
123 { /* end of list */ }
124 },
125};
126
Kevin Wolf734c3b82013-06-05 14:19:30 +0200127static int open_f(BlockDriverState *bs, int argc, char **argv)
aliguorie3aff4f2009-04-05 19:14:04 +0000128{
Devin Nakamura43642b32011-07-11 11:22:16 -0400129 int flags = 0;
130 int readonly = 0;
131 int growable = 0;
132 int c;
Max Reitzb543c5c2013-10-11 14:02:10 +0200133 QemuOpts *qopts;
Markus Armbruster443422f2014-05-28 11:16:58 +0200134 QDict *opts;
aliguorie3aff4f2009-04-05 19:14:04 +0000135
Max Reitzb543c5c2013-10-11 14:02:10 +0200136 while ((c = getopt(argc, argv, "snrgo:")) != EOF) {
Devin Nakamura43642b32011-07-11 11:22:16 -0400137 switch (c) {
138 case 's':
139 flags |= BDRV_O_SNAPSHOT;
140 break;
141 case 'n':
142 flags |= BDRV_O_NOCACHE | BDRV_O_CACHE_WB;
143 break;
144 case 'r':
145 readonly = 1;
146 break;
147 case 'g':
148 growable = 1;
149 break;
Max Reitzb543c5c2013-10-11 14:02:10 +0200150 case 'o':
Markus Armbruster443422f2014-05-28 11:16:58 +0200151 if (!qemu_opts_parse(&empty_opts, optarg, 0)) {
Max Reitzb543c5c2013-10-11 14:02:10 +0200152 printf("could not parse option list -- %s\n", optarg);
Markus Armbruster443422f2014-05-28 11:16:58 +0200153 qemu_opts_reset(&empty_opts);
Max Reitzb543c5c2013-10-11 14:02:10 +0200154 return 0;
155 }
Max Reitzb543c5c2013-10-11 14:02:10 +0200156 break;
Devin Nakamura43642b32011-07-11 11:22:16 -0400157 default:
Markus Armbruster443422f2014-05-28 11:16:58 +0200158 qemu_opts_reset(&empty_opts);
Kevin Wolfc2cdf5c2013-06-05 14:19:36 +0200159 return qemuio_command_usage(&open_cmd);
Naphtali Spreif5edb012010-01-17 16:48:13 +0200160 }
Devin Nakamura43642b32011-07-11 11:22:16 -0400161 }
aliguorie3aff4f2009-04-05 19:14:04 +0000162
Devin Nakamura43642b32011-07-11 11:22:16 -0400163 if (!readonly) {
164 flags |= BDRV_O_RDWR;
165 }
aliguorie3aff4f2009-04-05 19:14:04 +0000166
Markus Armbruster443422f2014-05-28 11:16:58 +0200167 qopts = qemu_opts_find(&empty_opts, NULL);
168 opts = qopts ? qemu_opts_to_qdict(qopts, NULL) : NULL;
169 qemu_opts_reset(&empty_opts);
170
Max Reitzfd0fee32013-12-20 19:28:20 +0100171 if (optind == argc - 1) {
172 return openfile(argv[optind], flags, growable, opts);
173 } else if (optind == argc) {
174 return openfile(NULL, flags, growable, opts);
175 } else {
Markus Armbruster29f26012014-05-28 11:16:59 +0200176 QDECREF(opts);
Kevin Wolfc2cdf5c2013-06-05 14:19:36 +0200177 return qemuio_command_usage(&open_cmd);
Devin Nakamura43642b32011-07-11 11:22:16 -0400178 }
aliguorie3aff4f2009-04-05 19:14:04 +0000179}
180
Kevin Wolfe681be72013-06-05 14:19:34 +0200181static int quit_f(BlockDriverState *bs, int argc, char **argv)
182{
183 return 1;
184}
185
186static const cmdinfo_t quit_cmd = {
187 .name = "quit",
188 .altname = "q",
189 .cfunc = quit_f,
190 .argmin = -1,
191 .argmax = -1,
192 .flags = CMD_FLAG_GLOBAL,
193 .oneline = "exit the program",
194};
195
aliguorie3aff4f2009-04-05 19:14:04 +0000196static void usage(const char *name)
197{
Devin Nakamura43642b32011-07-11 11:22:16 -0400198 printf(
Maria Kustovad208cc32014-03-18 09:59:19 +0400199"Usage: %s [-h] [-V] [-rsnm] [-c STRING] ... [file]\n"
Stefan Weil84844a22009-06-22 15:08:47 +0200200"QEMU Disk exerciser\n"
aliguorie3aff4f2009-04-05 19:14:04 +0000201"\n"
Maria Kustovad208cc32014-03-18 09:59:19 +0400202" -c, --cmd STRING execute command with its arguments\n"
203" from the given string\n"
aliguorie3aff4f2009-04-05 19:14:04 +0000204" -r, --read-only export read-only\n"
205" -s, --snapshot use snapshot file\n"
206" -n, --nocache disable host cache\n"
Christoph Hellwig1db69472009-07-15 23:11:21 +0200207" -g, --growable allow file to grow (only applies to protocols)\n"
aliguorie3aff4f2009-04-05 19:14:04 +0000208" -m, --misalign misalign allocations for O_DIRECT\n"
Christoph Hellwig5c6c3a62009-08-20 16:58:35 +0200209" -k, --native-aio use kernel AIO implementation (on Linux only)\n"
Kevin Wolf592fa072012-04-18 12:07:39 +0200210" -t, --cache=MODE use the given cache mode for the image\n"
Stefan Hajnoczid7bb72c2012-03-12 16:36:07 +0000211" -T, --trace FILE enable trace events listed in the given file\n"
aliguorie3aff4f2009-04-05 19:14:04 +0000212" -h, --help display this help and exit\n"
213" -V, --version output version information and exit\n"
Maria Kustovad208cc32014-03-18 09:59:19 +0400214"\n"
215"See '%s -c help' for information on available commands."
aliguorie3aff4f2009-04-05 19:14:04 +0000216"\n",
Maria Kustovad208cc32014-03-18 09:59:19 +0400217 name, name);
aliguorie3aff4f2009-04-05 19:14:04 +0000218}
219
Kevin Wolfd1174f12013-06-05 14:19:37 +0200220static char *get_prompt(void)
221{
222 static char prompt[FILENAME_MAX + 2 /*"> "*/ + 1 /*"\0"*/ ];
223
224 if (!prompt[0]) {
225 snprintf(prompt, sizeof(prompt), "%s> ", progname);
226 }
227
228 return prompt;
229}
230
Stefan Weild5d15072014-01-25 18:18:23 +0100231static void GCC_FMT_ATTR(2, 3) readline_printf_func(void *opaque,
232 const char *fmt, ...)
Kevin Wolfd1174f12013-06-05 14:19:37 +0200233{
Stefan Hajnoczi0cf17e12013-11-14 11:54:17 +0100234 va_list ap;
235 va_start(ap, fmt);
236 vprintf(fmt, ap);
237 va_end(ap);
238}
239
240static void readline_flush_func(void *opaque)
241{
242 fflush(stdout);
243}
244
245static void readline_func(void *opaque, const char *str, void *readline_opaque)
246{
247 char **line = readline_opaque;
248 *line = g_strdup(str);
249}
250
Stefan Hajnoczi46940202013-11-14 11:54:18 +0100251static void completion_match(const char *cmd, void *opaque)
252{
253 readline_add_completion(readline_state, cmd);
254}
255
Stefan Hajnoczi0cf17e12013-11-14 11:54:17 +0100256static void readline_completion_func(void *opaque, const char *str)
257{
Stefan Hajnoczi46940202013-11-14 11:54:18 +0100258 readline_set_completion_index(readline_state, strlen(str));
259 qemuio_complete_command(str, completion_match, NULL);
Stefan Hajnoczi0cf17e12013-11-14 11:54:17 +0100260}
261
262static char *fetchline_readline(void)
263{
264 char *line = NULL;
265
266 readline_start(readline_state, get_prompt(), 0, readline_func, &line);
267 while (!line) {
268 int ch = getchar();
269 if (ch == EOF) {
270 break;
271 }
272 readline_handle_byte(readline_state, ch);
Kevin Wolfd1174f12013-06-05 14:19:37 +0200273 }
274 return line;
275}
Kevin Wolfd1174f12013-06-05 14:19:37 +0200276
Stefan Hajnoczi0cf17e12013-11-14 11:54:17 +0100277#define MAXREADLINESZ 1024
278static char *fetchline_fgets(void)
Kevin Wolfd1174f12013-06-05 14:19:37 +0200279{
280 char *p, *line = g_malloc(MAXREADLINESZ);
281
282 if (!fgets(line, MAXREADLINESZ, stdin)) {
283 g_free(line);
284 return NULL;
285 }
286
287 p = line + strlen(line);
288 if (p != line && p[-1] == '\n') {
289 p[-1] = '\0';
290 }
291
292 return line;
293}
Stefan Hajnoczi0cf17e12013-11-14 11:54:17 +0100294
295static char *fetchline(void)
296{
297 if (readline_state) {
298 return fetchline_readline();
299 } else {
300 return fetchline_fgets();
301 }
302}
Kevin Wolfd1174f12013-06-05 14:19:37 +0200303
304static void prep_fetchline(void *opaque)
305{
306 int *fetchable = opaque;
307
308 qemu_set_fd_handler(STDIN_FILENO, NULL, NULL, NULL);
309 *fetchable= 1;
310}
311
312static void command_loop(void)
313{
314 int i, done = 0, fetchable = 0, prompted = 0;
315 char *input;
316
317 for (i = 0; !done && i < ncmdline; i++) {
Kevin Wolf3d219942013-06-05 14:19:39 +0200318 done = qemuio_command(qemuio_bs, cmdline[i]);
Kevin Wolfd1174f12013-06-05 14:19:37 +0200319 }
320 if (cmdline) {
321 g_free(cmdline);
322 return;
323 }
324
325 while (!done) {
326 if (!prompted) {
327 printf("%s", get_prompt());
328 fflush(stdout);
329 qemu_set_fd_handler(STDIN_FILENO, prep_fetchline, NULL, &fetchable);
330 prompted = 1;
331 }
332
333 main_loop_wait(false);
334
335 if (!fetchable) {
336 continue;
337 }
338
339 input = fetchline();
340 if (input == NULL) {
341 break;
342 }
Kevin Wolf3d219942013-06-05 14:19:39 +0200343 done = qemuio_command(qemuio_bs, input);
Kevin Wolfd1174f12013-06-05 14:19:37 +0200344 g_free(input);
345
346 prompted = 0;
347 fetchable = 0;
348 }
349 qemu_set_fd_handler(STDIN_FILENO, NULL, NULL, NULL);
350}
351
352static void add_user_command(char *optarg)
353{
Markus Armbruster5839e532014-08-19 10:31:08 +0200354 cmdline = g_renew(char *, cmdline, ++ncmdline);
Kevin Wolfd1174f12013-06-05 14:19:37 +0200355 cmdline[ncmdline-1] = optarg;
356}
357
Stefan Hajnoczi0cf17e12013-11-14 11:54:17 +0100358static void reenable_tty_echo(void)
359{
360 qemu_set_tty_echo(STDIN_FILENO, true);
361}
362
aliguorie3aff4f2009-04-05 19:14:04 +0000363int main(int argc, char **argv)
364{
Devin Nakamura43642b32011-07-11 11:22:16 -0400365 int readonly = 0;
366 int growable = 0;
Paolo Bonzini9e8f1832013-02-08 14:06:11 +0100367 const char *sopt = "hVc:d:rsnmgkt:T:";
Devin Nakamura43642b32011-07-11 11:22:16 -0400368 const struct option lopt[] = {
369 { "help", 0, NULL, 'h' },
370 { "version", 0, NULL, 'V' },
371 { "offset", 1, NULL, 'o' },
372 { "cmd", 1, NULL, 'c' },
373 { "read-only", 0, NULL, 'r' },
374 { "snapshot", 0, NULL, 's' },
375 { "nocache", 0, NULL, 'n' },
376 { "misalign", 0, NULL, 'm' },
377 { "growable", 0, NULL, 'g' },
378 { "native-aio", 0, NULL, 'k' },
Paolo Bonzini9e8f1832013-02-08 14:06:11 +0100379 { "discard", 1, NULL, 'd' },
Kevin Wolf592fa072012-04-18 12:07:39 +0200380 { "cache", 1, NULL, 't' },
Stefan Hajnoczid7bb72c2012-03-12 16:36:07 +0000381 { "trace", 1, NULL, 'T' },
Devin Nakamura43642b32011-07-11 11:22:16 -0400382 { NULL, 0, NULL, 0 }
383 };
384 int c;
385 int opt_index = 0;
Paolo Bonzini9e8f1832013-02-08 14:06:11 +0100386 int flags = BDRV_O_UNMAP;
Chrysostomos Nanakos2f78e492014-09-18 14:30:49 +0300387 Error *local_error = NULL;
aliguorie3aff4f2009-04-05 19:14:04 +0000388
MORITA Kazutaka526eda12013-07-23 17:30:11 +0900389#ifdef CONFIG_POSIX
390 signal(SIGPIPE, SIG_IGN);
391#endif
392
Devin Nakamura43642b32011-07-11 11:22:16 -0400393 progname = basename(argv[0]);
Fam Zheng10f5bff2014-02-10 14:48:51 +0800394 qemu_init_exec_dir(argv[0]);
aliguorie3aff4f2009-04-05 19:14:04 +0000395
Devin Nakamura43642b32011-07-11 11:22:16 -0400396 while ((c = getopt_long(argc, argv, sopt, lopt, &opt_index)) != -1) {
397 switch (c) {
398 case 's':
399 flags |= BDRV_O_SNAPSHOT;
400 break;
401 case 'n':
402 flags |= BDRV_O_NOCACHE | BDRV_O_CACHE_WB;
403 break;
Paolo Bonzini9e8f1832013-02-08 14:06:11 +0100404 case 'd':
405 if (bdrv_parse_discard_flags(optarg, &flags) < 0) {
406 error_report("Invalid discard option: %s", optarg);
407 exit(1);
408 }
409 break;
Devin Nakamura43642b32011-07-11 11:22:16 -0400410 case 'c':
411 add_user_command(optarg);
412 break;
413 case 'r':
414 readonly = 1;
415 break;
416 case 'm':
Stefan Weilf9883882014-03-05 22:23:00 +0100417 qemuio_misalign = true;
Devin Nakamura43642b32011-07-11 11:22:16 -0400418 break;
419 case 'g':
420 growable = 1;
421 break;
422 case 'k':
423 flags |= BDRV_O_NATIVE_AIO;
424 break;
Kevin Wolf592fa072012-04-18 12:07:39 +0200425 case 't':
426 if (bdrv_parse_cache_flags(optarg, &flags) < 0) {
427 error_report("Invalid cache option: %s", optarg);
428 exit(1);
429 }
430 break;
Stefan Hajnoczid7bb72c2012-03-12 16:36:07 +0000431 case 'T':
LluĂ­s Vilanova5b808272014-05-27 15:02:14 +0200432 if (!trace_init_backends(optarg, NULL)) {
Stefan Hajnoczid7bb72c2012-03-12 16:36:07 +0000433 exit(1); /* error message will have been printed */
434 }
435 break;
Devin Nakamura43642b32011-07-11 11:22:16 -0400436 case 'V':
Kevin Wolf02da3862013-06-05 14:19:40 +0200437 printf("%s version %s\n", progname, QEMU_VERSION);
Devin Nakamura43642b32011-07-11 11:22:16 -0400438 exit(0);
439 case 'h':
440 usage(progname);
441 exit(0);
442 default:
443 usage(progname);
444 exit(1);
Naphtali Spreif5edb012010-01-17 16:48:13 +0200445 }
Devin Nakamura43642b32011-07-11 11:22:16 -0400446 }
aliguorie3aff4f2009-04-05 19:14:04 +0000447
Devin Nakamura43642b32011-07-11 11:22:16 -0400448 if ((argc - optind) > 1) {
449 usage(progname);
450 exit(1);
451 }
aliguorie3aff4f2009-04-05 19:14:04 +0000452
Chrysostomos Nanakos2f78e492014-09-18 14:30:49 +0300453 if (qemu_init_main_loop(&local_error)) {
454 error_report("%s", error_get_pretty(local_error));
455 error_free(local_error);
456 exit(1);
457 }
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
Markus Armbruster26f54e92014-10-07 13:59:04 +0200489 blk_unref(qemuio_blk);
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}