blob: a85522a1b3f0bdff63fe91fe6aa11c6df1db3cf5 [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 Reitz10d9d752015-02-05 13:58:21 -050054static int openfile(char *name, int flags, 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
Max Reitz1b58b432015-02-05 13:58:20 -050064 qemuio_blk = blk_new_open("hda", name, NULL, opts, flags, &local_err);
65 if (!qemuio_blk) {
Markus Armbrusterdbb651c2014-09-08 18:50:58 +020066 fprintf(stderr, "%s: can't open%s%s: %s\n", progname,
67 name ? " device " : "", name ?: "",
68 error_get_pretty(local_err));
69 error_free(local_err);
Markus Armbrusterdbb651c2014-09-08 18:50:58 +020070 return 1;
Devin Nakamura43642b32011-07-11 11:22:16 -040071 }
Max Reitz1b58b432015-02-05 13:58:20 -050072 qemuio_bs = blk_bs(qemuio_blk);
Christoph Hellwig1db69472009-07-15 23:11:21 +020073
Devin Nakamura43642b32011-07-11 11:22:16 -040074 return 0;
aliguorie3aff4f2009-04-05 19:14:04 +000075}
76
Devin Nakamura43642b32011-07-11 11:22:16 -040077static void open_help(void)
aliguorie3aff4f2009-04-05 19:14:04 +000078{
Devin Nakamura43642b32011-07-11 11:22:16 -040079 printf(
aliguorie3aff4f2009-04-05 19:14:04 +000080"\n"
81" opens a new file in the requested mode\n"
82"\n"
83" Example:\n"
84" 'open -Cn /tmp/data' - creates/opens data file read-write and uncached\n"
85"\n"
86" Opens a file for subsequent use by all of the other qemu-io commands.\n"
aliguorie3aff4f2009-04-05 19:14:04 +000087" -r, -- open file read-only\n"
88" -s, -- use snapshot file\n"
89" -n, -- disable host cache\n"
Max Reitzb543c5c2013-10-11 14:02:10 +020090" -o, -- options to be given to the block driver"
aliguorie3aff4f2009-04-05 19:14:04 +000091"\n");
92}
93
Kevin Wolf734c3b82013-06-05 14:19:30 +020094static int open_f(BlockDriverState *bs, int argc, char **argv);
Blue Swirl22a2bdc2009-11-21 09:06:46 +000095
96static const cmdinfo_t open_cmd = {
Devin Nakamura43642b32011-07-11 11:22:16 -040097 .name = "open",
98 .altname = "o",
99 .cfunc = open_f,
100 .argmin = 1,
101 .argmax = -1,
102 .flags = CMD_NOFILE_OK,
Max Reitzb543c5c2013-10-11 14:02:10 +0200103 .args = "[-Crsn] [-o options] [path]",
Devin Nakamura43642b32011-07-11 11:22:16 -0400104 .oneline = "open the file specified by path",
105 .help = open_help,
Blue Swirl22a2bdc2009-11-21 09:06:46 +0000106};
aliguorie3aff4f2009-04-05 19:14:04 +0000107
Max Reitzb543c5c2013-10-11 14:02:10 +0200108static QemuOptsList empty_opts = {
109 .name = "drive",
Markus Armbruster443422f2014-05-28 11:16:58 +0200110 .merge_lists = true,
Max Reitzb543c5c2013-10-11 14:02:10 +0200111 .head = QTAILQ_HEAD_INITIALIZER(empty_opts.head),
112 .desc = {
113 /* no elements => accept any params */
114 { /* end of list */ }
115 },
116};
117
Kevin Wolf734c3b82013-06-05 14:19:30 +0200118static int open_f(BlockDriverState *bs, int argc, char **argv)
aliguorie3aff4f2009-04-05 19:14:04 +0000119{
Devin Nakamura43642b32011-07-11 11:22:16 -0400120 int flags = 0;
121 int readonly = 0;
Devin Nakamura43642b32011-07-11 11:22:16 -0400122 int c;
Max Reitzb543c5c2013-10-11 14:02:10 +0200123 QemuOpts *qopts;
Markus Armbruster443422f2014-05-28 11:16:58 +0200124 QDict *opts;
aliguorie3aff4f2009-04-05 19:14:04 +0000125
Max Reitzb543c5c2013-10-11 14:02:10 +0200126 while ((c = getopt(argc, argv, "snrgo:")) != EOF) {
Devin Nakamura43642b32011-07-11 11:22:16 -0400127 switch (c) {
128 case 's':
129 flags |= BDRV_O_SNAPSHOT;
130 break;
131 case 'n':
132 flags |= BDRV_O_NOCACHE | BDRV_O_CACHE_WB;
133 break;
134 case 'r':
135 readonly = 1;
136 break;
Max Reitzb543c5c2013-10-11 14:02:10 +0200137 case 'o':
Markus Armbruster443422f2014-05-28 11:16:58 +0200138 if (!qemu_opts_parse(&empty_opts, optarg, 0)) {
Max Reitzb543c5c2013-10-11 14:02:10 +0200139 printf("could not parse option list -- %s\n", optarg);
Markus Armbruster443422f2014-05-28 11:16:58 +0200140 qemu_opts_reset(&empty_opts);
Max Reitzb543c5c2013-10-11 14:02:10 +0200141 return 0;
142 }
Max Reitzb543c5c2013-10-11 14:02:10 +0200143 break;
Devin Nakamura43642b32011-07-11 11:22:16 -0400144 default:
Markus Armbruster443422f2014-05-28 11:16:58 +0200145 qemu_opts_reset(&empty_opts);
Kevin Wolfc2cdf5c2013-06-05 14:19:36 +0200146 return qemuio_command_usage(&open_cmd);
Naphtali Spreif5edb012010-01-17 16:48:13 +0200147 }
Devin Nakamura43642b32011-07-11 11:22:16 -0400148 }
aliguorie3aff4f2009-04-05 19:14:04 +0000149
Devin Nakamura43642b32011-07-11 11:22:16 -0400150 if (!readonly) {
151 flags |= BDRV_O_RDWR;
152 }
aliguorie3aff4f2009-04-05 19:14:04 +0000153
Markus Armbruster443422f2014-05-28 11:16:58 +0200154 qopts = qemu_opts_find(&empty_opts, NULL);
155 opts = qopts ? qemu_opts_to_qdict(qopts, NULL) : NULL;
156 qemu_opts_reset(&empty_opts);
157
Max Reitzfd0fee32013-12-20 19:28:20 +0100158 if (optind == argc - 1) {
Max Reitz10d9d752015-02-05 13:58:21 -0500159 return openfile(argv[optind], flags, opts);
Max Reitzfd0fee32013-12-20 19:28:20 +0100160 } else if (optind == argc) {
Max Reitz10d9d752015-02-05 13:58:21 -0500161 return openfile(NULL, flags, opts);
Max Reitzfd0fee32013-12-20 19:28:20 +0100162 } else {
Markus Armbruster29f26012014-05-28 11:16:59 +0200163 QDECREF(opts);
Kevin Wolfc2cdf5c2013-06-05 14:19:36 +0200164 return qemuio_command_usage(&open_cmd);
Devin Nakamura43642b32011-07-11 11:22:16 -0400165 }
aliguorie3aff4f2009-04-05 19:14:04 +0000166}
167
Kevin Wolfe681be72013-06-05 14:19:34 +0200168static int quit_f(BlockDriverState *bs, int argc, char **argv)
169{
170 return 1;
171}
172
173static const cmdinfo_t quit_cmd = {
174 .name = "quit",
175 .altname = "q",
176 .cfunc = quit_f,
177 .argmin = -1,
178 .argmax = -1,
179 .flags = CMD_FLAG_GLOBAL,
180 .oneline = "exit the program",
181};
182
aliguorie3aff4f2009-04-05 19:14:04 +0000183static void usage(const char *name)
184{
Devin Nakamura43642b32011-07-11 11:22:16 -0400185 printf(
Kevin Wolfbe6273d2014-11-20 16:27:06 +0100186"Usage: %s [-h] [-V] [-rsnm] [-f FMT] [-c STRING] ... [file]\n"
Stefan Weil84844a22009-06-22 15:08:47 +0200187"QEMU Disk exerciser\n"
aliguorie3aff4f2009-04-05 19:14:04 +0000188"\n"
Maria Kustovad208cc32014-03-18 09:59:19 +0400189" -c, --cmd STRING execute command with its arguments\n"
190" from the given string\n"
Kevin Wolfbe6273d2014-11-20 16:27:06 +0100191" -f, --format FMT specifies the block driver to use\n"
aliguorie3aff4f2009-04-05 19:14:04 +0000192" -r, --read-only export read-only\n"
193" -s, --snapshot use snapshot file\n"
194" -n, --nocache disable host cache\n"
195" -m, --misalign misalign allocations for O_DIRECT\n"
Christoph Hellwig5c6c3a62009-08-20 16:58:35 +0200196" -k, --native-aio use kernel AIO implementation (on Linux only)\n"
Kevin Wolf592fa072012-04-18 12:07:39 +0200197" -t, --cache=MODE use the given cache mode for the image\n"
Stefan Hajnoczid7bb72c2012-03-12 16:36:07 +0000198" -T, --trace FILE enable trace events listed in the given file\n"
aliguorie3aff4f2009-04-05 19:14:04 +0000199" -h, --help display this help and exit\n"
200" -V, --version output version information and exit\n"
Maria Kustovad208cc32014-03-18 09:59:19 +0400201"\n"
202"See '%s -c help' for information on available commands."
aliguorie3aff4f2009-04-05 19:14:04 +0000203"\n",
Maria Kustovad208cc32014-03-18 09:59:19 +0400204 name, name);
aliguorie3aff4f2009-04-05 19:14:04 +0000205}
206
Kevin Wolfd1174f12013-06-05 14:19:37 +0200207static char *get_prompt(void)
208{
209 static char prompt[FILENAME_MAX + 2 /*"> "*/ + 1 /*"\0"*/ ];
210
211 if (!prompt[0]) {
212 snprintf(prompt, sizeof(prompt), "%s> ", progname);
213 }
214
215 return prompt;
216}
217
Stefan Weild5d15072014-01-25 18:18:23 +0100218static void GCC_FMT_ATTR(2, 3) readline_printf_func(void *opaque,
219 const char *fmt, ...)
Kevin Wolfd1174f12013-06-05 14:19:37 +0200220{
Stefan Hajnoczi0cf17e12013-11-14 11:54:17 +0100221 va_list ap;
222 va_start(ap, fmt);
223 vprintf(fmt, ap);
224 va_end(ap);
225}
226
227static void readline_flush_func(void *opaque)
228{
229 fflush(stdout);
230}
231
232static void readline_func(void *opaque, const char *str, void *readline_opaque)
233{
234 char **line = readline_opaque;
235 *line = g_strdup(str);
236}
237
Stefan Hajnoczi46940202013-11-14 11:54:18 +0100238static void completion_match(const char *cmd, void *opaque)
239{
240 readline_add_completion(readline_state, cmd);
241}
242
Stefan Hajnoczi0cf17e12013-11-14 11:54:17 +0100243static void readline_completion_func(void *opaque, const char *str)
244{
Stefan Hajnoczi46940202013-11-14 11:54:18 +0100245 readline_set_completion_index(readline_state, strlen(str));
246 qemuio_complete_command(str, completion_match, NULL);
Stefan Hajnoczi0cf17e12013-11-14 11:54:17 +0100247}
248
249static char *fetchline_readline(void)
250{
251 char *line = NULL;
252
253 readline_start(readline_state, get_prompt(), 0, readline_func, &line);
254 while (!line) {
255 int ch = getchar();
256 if (ch == EOF) {
257 break;
258 }
259 readline_handle_byte(readline_state, ch);
Kevin Wolfd1174f12013-06-05 14:19:37 +0200260 }
261 return line;
262}
Kevin Wolfd1174f12013-06-05 14:19:37 +0200263
Stefan Hajnoczi0cf17e12013-11-14 11:54:17 +0100264#define MAXREADLINESZ 1024
265static char *fetchline_fgets(void)
Kevin Wolfd1174f12013-06-05 14:19:37 +0200266{
267 char *p, *line = g_malloc(MAXREADLINESZ);
268
269 if (!fgets(line, MAXREADLINESZ, stdin)) {
270 g_free(line);
271 return NULL;
272 }
273
274 p = line + strlen(line);
275 if (p != line && p[-1] == '\n') {
276 p[-1] = '\0';
277 }
278
279 return line;
280}
Stefan Hajnoczi0cf17e12013-11-14 11:54:17 +0100281
282static char *fetchline(void)
283{
284 if (readline_state) {
285 return fetchline_readline();
286 } else {
287 return fetchline_fgets();
288 }
289}
Kevin Wolfd1174f12013-06-05 14:19:37 +0200290
291static void prep_fetchline(void *opaque)
292{
293 int *fetchable = opaque;
294
295 qemu_set_fd_handler(STDIN_FILENO, NULL, NULL, NULL);
296 *fetchable= 1;
297}
298
299static void command_loop(void)
300{
301 int i, done = 0, fetchable = 0, prompted = 0;
302 char *input;
303
304 for (i = 0; !done && i < ncmdline; i++) {
Kevin Wolf3d219942013-06-05 14:19:39 +0200305 done = qemuio_command(qemuio_bs, cmdline[i]);
Kevin Wolfd1174f12013-06-05 14:19:37 +0200306 }
307 if (cmdline) {
308 g_free(cmdline);
309 return;
310 }
311
312 while (!done) {
313 if (!prompted) {
314 printf("%s", get_prompt());
315 fflush(stdout);
316 qemu_set_fd_handler(STDIN_FILENO, prep_fetchline, NULL, &fetchable);
317 prompted = 1;
318 }
319
320 main_loop_wait(false);
321
322 if (!fetchable) {
323 continue;
324 }
325
326 input = fetchline();
327 if (input == NULL) {
328 break;
329 }
Kevin Wolf3d219942013-06-05 14:19:39 +0200330 done = qemuio_command(qemuio_bs, input);
Kevin Wolfd1174f12013-06-05 14:19:37 +0200331 g_free(input);
332
333 prompted = 0;
334 fetchable = 0;
335 }
336 qemu_set_fd_handler(STDIN_FILENO, NULL, NULL, NULL);
337}
338
339static void add_user_command(char *optarg)
340{
Markus Armbruster5839e532014-08-19 10:31:08 +0200341 cmdline = g_renew(char *, cmdline, ++ncmdline);
Kevin Wolfd1174f12013-06-05 14:19:37 +0200342 cmdline[ncmdline-1] = optarg;
343}
344
Stefan Hajnoczi0cf17e12013-11-14 11:54:17 +0100345static void reenable_tty_echo(void)
346{
347 qemu_set_tty_echo(STDIN_FILENO, true);
348}
349
aliguorie3aff4f2009-04-05 19:14:04 +0000350int main(int argc, char **argv)
351{
Devin Nakamura43642b32011-07-11 11:22:16 -0400352 int readonly = 0;
Kevin Wolfbe6273d2014-11-20 16:27:06 +0100353 const char *sopt = "hVc:d:f:rsnmgkt:T:";
Devin Nakamura43642b32011-07-11 11:22:16 -0400354 const struct option lopt[] = {
355 { "help", 0, NULL, 'h' },
356 { "version", 0, NULL, 'V' },
357 { "offset", 1, NULL, 'o' },
358 { "cmd", 1, NULL, 'c' },
Kevin Wolfbe6273d2014-11-20 16:27:06 +0100359 { "format", 1, NULL, 'f' },
Devin Nakamura43642b32011-07-11 11:22:16 -0400360 { "read-only", 0, NULL, 'r' },
361 { "snapshot", 0, NULL, 's' },
362 { "nocache", 0, NULL, 'n' },
363 { "misalign", 0, NULL, 'm' },
Devin Nakamura43642b32011-07-11 11:22:16 -0400364 { "native-aio", 0, NULL, 'k' },
Paolo Bonzini9e8f1832013-02-08 14:06:11 +0100365 { "discard", 1, NULL, 'd' },
Kevin Wolf592fa072012-04-18 12:07:39 +0200366 { "cache", 1, NULL, 't' },
Stefan Hajnoczid7bb72c2012-03-12 16:36:07 +0000367 { "trace", 1, NULL, 'T' },
Devin Nakamura43642b32011-07-11 11:22:16 -0400368 { NULL, 0, NULL, 0 }
369 };
370 int c;
371 int opt_index = 0;
Paolo Bonzini9e8f1832013-02-08 14:06:11 +0100372 int flags = BDRV_O_UNMAP;
Chrysostomos Nanakos2f78e492014-09-18 14:30:49 +0300373 Error *local_error = NULL;
Max Reitz1b58b432015-02-05 13:58:20 -0500374 QDict *opts = NULL;
aliguorie3aff4f2009-04-05 19:14:04 +0000375
MORITA Kazutaka526eda12013-07-23 17:30:11 +0900376#ifdef CONFIG_POSIX
377 signal(SIGPIPE, SIG_IGN);
378#endif
379
Devin Nakamura43642b32011-07-11 11:22:16 -0400380 progname = basename(argv[0]);
Fam Zheng10f5bff2014-02-10 14:48:51 +0800381 qemu_init_exec_dir(argv[0]);
aliguorie3aff4f2009-04-05 19:14:04 +0000382
Kevin Wolfbe6273d2014-11-20 16:27:06 +0100383 bdrv_init();
384
Devin Nakamura43642b32011-07-11 11:22:16 -0400385 while ((c = getopt_long(argc, argv, sopt, lopt, &opt_index)) != -1) {
386 switch (c) {
387 case 's':
388 flags |= BDRV_O_SNAPSHOT;
389 break;
390 case 'n':
391 flags |= BDRV_O_NOCACHE | BDRV_O_CACHE_WB;
392 break;
Paolo Bonzini9e8f1832013-02-08 14:06:11 +0100393 case 'd':
394 if (bdrv_parse_discard_flags(optarg, &flags) < 0) {
395 error_report("Invalid discard option: %s", optarg);
396 exit(1);
397 }
398 break;
Kevin Wolfbe6273d2014-11-20 16:27:06 +0100399 case 'f':
Max Reitz1b58b432015-02-05 13:58:20 -0500400 if (!opts) {
401 opts = qdict_new();
Kevin Wolfbe6273d2014-11-20 16:27:06 +0100402 }
Max Reitz1b58b432015-02-05 13:58:20 -0500403 qdict_put(opts, "driver", qstring_from_str(optarg));
Kevin Wolfbe6273d2014-11-20 16:27:06 +0100404 break;
Devin Nakamura43642b32011-07-11 11:22:16 -0400405 case 'c':
406 add_user_command(optarg);
407 break;
408 case 'r':
409 readonly = 1;
410 break;
411 case 'm':
Stefan Weilf9883882014-03-05 22:23:00 +0100412 qemuio_misalign = true;
Devin Nakamura43642b32011-07-11 11:22:16 -0400413 break;
Devin Nakamura43642b32011-07-11 11:22:16 -0400414 case 'k':
415 flags |= BDRV_O_NATIVE_AIO;
416 break;
Kevin Wolf592fa072012-04-18 12:07:39 +0200417 case 't':
418 if (bdrv_parse_cache_flags(optarg, &flags) < 0) {
419 error_report("Invalid cache option: %s", optarg);
420 exit(1);
421 }
422 break;
Stefan Hajnoczid7bb72c2012-03-12 16:36:07 +0000423 case 'T':
LluĂ­s Vilanova5b808272014-05-27 15:02:14 +0200424 if (!trace_init_backends(optarg, NULL)) {
Stefan Hajnoczid7bb72c2012-03-12 16:36:07 +0000425 exit(1); /* error message will have been printed */
426 }
427 break;
Devin Nakamura43642b32011-07-11 11:22:16 -0400428 case 'V':
Kevin Wolf02da3862013-06-05 14:19:40 +0200429 printf("%s version %s\n", progname, QEMU_VERSION);
Devin Nakamura43642b32011-07-11 11:22:16 -0400430 exit(0);
431 case 'h':
432 usage(progname);
433 exit(0);
434 default:
435 usage(progname);
436 exit(1);
Naphtali Spreif5edb012010-01-17 16:48:13 +0200437 }
Devin Nakamura43642b32011-07-11 11:22:16 -0400438 }
aliguorie3aff4f2009-04-05 19:14:04 +0000439
Devin Nakamura43642b32011-07-11 11:22:16 -0400440 if ((argc - optind) > 1) {
441 usage(progname);
442 exit(1);
443 }
aliguorie3aff4f2009-04-05 19:14:04 +0000444
Chrysostomos Nanakos2f78e492014-09-18 14:30:49 +0300445 if (qemu_init_main_loop(&local_error)) {
446 error_report("%s", error_get_pretty(local_error));
447 error_free(local_error);
448 exit(1);
449 }
Zhi Yong Wua57d1142012-02-19 22:24:59 +0800450
Devin Nakamura43642b32011-07-11 11:22:16 -0400451 /* initialize commands */
Kevin Wolfc2cdf5c2013-06-05 14:19:36 +0200452 qemuio_add_command(&quit_cmd);
453 qemuio_add_command(&open_cmd);
454 qemuio_add_command(&close_cmd);
Devin Nakamura43642b32011-07-11 11:22:16 -0400455
Stefan Hajnoczi0cf17e12013-11-14 11:54:17 +0100456 if (isatty(STDIN_FILENO)) {
457 readline_state = readline_init(readline_printf_func,
458 readline_flush_func,
459 NULL,
460 readline_completion_func);
461 qemu_set_tty_echo(STDIN_FILENO, false);
462 atexit(reenable_tty_echo);
463 }
464
Devin Nakamura43642b32011-07-11 11:22:16 -0400465 /* open the device */
466 if (!readonly) {
467 flags |= BDRV_O_RDWR;
468 }
469
470 if ((argc - optind) == 1) {
Max Reitz10d9d752015-02-05 13:58:21 -0500471 openfile(argv[optind], flags, opts);
Devin Nakamura43642b32011-07-11 11:22:16 -0400472 }
473 command_loop();
474
475 /*
Stefan Hajnoczi922453b2011-11-30 12:23:43 +0000476 * Make sure all outstanding requests complete before the program exits.
Devin Nakamura43642b32011-07-11 11:22:16 -0400477 */
Stefan Hajnoczi922453b2011-11-30 12:23:43 +0000478 bdrv_drain_all();
Devin Nakamura43642b32011-07-11 11:22:16 -0400479
Markus Armbruster26f54e92014-10-07 13:59:04 +0200480 blk_unref(qemuio_blk);
Stefan Hajnoczi0cf17e12013-11-14 11:54:17 +0100481 g_free(readline_state);
Devin Nakamura43642b32011-07-11 11:22:16 -0400482 return 0;
aliguorie3aff4f2009-04-05 19:14:04 +0000483}