blob: 8e41080cc9b9eec8acbd67f7838440d6d51a3b65 [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;
Kevin Wolf191c2892010-09-16 13:18:08 +020031
Kevin Wolfd1174f12013-06-05 14:19:37 +020032/* qemu-io commands passed using -c */
33static int ncmdline;
34static char **cmdline;
35
Stefan Hajnoczi0cf17e12013-11-14 11:54:17 +010036static ReadLineState *readline_state;
37
Max Reitz4c7b7e92015-02-05 13:58:22 -050038static int close_f(BlockBackend *blk, int argc, char **argv)
aliguorie3aff4f2009-04-05 19:14:04 +000039{
Markus Armbruster26f54e92014-10-07 13:59:04 +020040 blk_unref(qemuio_blk);
Markus Armbruster26f54e92014-10-07 13:59:04 +020041 qemuio_blk = NULL;
Devin Nakamura43642b32011-07-11 11:22:16 -040042 return 0;
aliguorie3aff4f2009-04-05 19:14:04 +000043}
44
45static const cmdinfo_t close_cmd = {
Devin Nakamura43642b32011-07-11 11:22:16 -040046 .name = "close",
47 .altname = "c",
48 .cfunc = close_f,
49 .oneline = "close the current open file",
aliguorie3aff4f2009-04-05 19:14:04 +000050};
51
Max Reitz10d9d752015-02-05 13:58:21 -050052static int openfile(char *name, int flags, QDict *opts)
aliguorie3aff4f2009-04-05 19:14:04 +000053{
Max Reitz34b5d2c2013-09-05 14:45:29 +020054 Error *local_err = NULL;
55
Max Reitz1b58b432015-02-05 13:58:20 -050056 if (qemuio_blk) {
Devin Nakamura43642b32011-07-11 11:22:16 -040057 fprintf(stderr, "file open already, try 'help close'\n");
Markus Armbruster29f26012014-05-28 11:16:59 +020058 QDECREF(opts);
Devin Nakamura43642b32011-07-11 11:22:16 -040059 return 1;
60 }
aliguorie3aff4f2009-04-05 19:14:04 +000061
Max Reitz1b58b432015-02-05 13:58:20 -050062 qemuio_blk = blk_new_open("hda", name, NULL, opts, flags, &local_err);
63 if (!qemuio_blk) {
Markus Armbrusterdbb651c2014-09-08 18:50:58 +020064 fprintf(stderr, "%s: can't open%s%s: %s\n", progname,
65 name ? " device " : "", name ?: "",
66 error_get_pretty(local_err));
67 error_free(local_err);
Markus Armbrusterdbb651c2014-09-08 18:50:58 +020068 return 1;
Devin Nakamura43642b32011-07-11 11:22:16 -040069 }
Christoph Hellwig1db69472009-07-15 23:11:21 +020070
Devin Nakamura43642b32011-07-11 11:22:16 -040071 return 0;
aliguorie3aff4f2009-04-05 19:14:04 +000072}
73
Devin Nakamura43642b32011-07-11 11:22:16 -040074static void open_help(void)
aliguorie3aff4f2009-04-05 19:14:04 +000075{
Devin Nakamura43642b32011-07-11 11:22:16 -040076 printf(
aliguorie3aff4f2009-04-05 19:14:04 +000077"\n"
78" opens a new file in the requested mode\n"
79"\n"
80" Example:\n"
81" 'open -Cn /tmp/data' - creates/opens data file read-write and uncached\n"
82"\n"
83" Opens a file for subsequent use by all of the other qemu-io commands.\n"
aliguorie3aff4f2009-04-05 19:14:04 +000084" -r, -- open file read-only\n"
85" -s, -- use snapshot file\n"
86" -n, -- disable host cache\n"
Max Reitzb543c5c2013-10-11 14:02:10 +020087" -o, -- options to be given to the block driver"
aliguorie3aff4f2009-04-05 19:14:04 +000088"\n");
89}
90
Max Reitz4c7b7e92015-02-05 13:58:22 -050091static int open_f(BlockBackend *blk, int argc, char **argv);
Blue Swirl22a2bdc2009-11-21 09:06:46 +000092
93static const cmdinfo_t open_cmd = {
Devin Nakamura43642b32011-07-11 11:22:16 -040094 .name = "open",
95 .altname = "o",
96 .cfunc = open_f,
97 .argmin = 1,
98 .argmax = -1,
99 .flags = CMD_NOFILE_OK,
Max Reitzb543c5c2013-10-11 14:02:10 +0200100 .args = "[-Crsn] [-o options] [path]",
Devin Nakamura43642b32011-07-11 11:22:16 -0400101 .oneline = "open the file specified by path",
102 .help = open_help,
Blue Swirl22a2bdc2009-11-21 09:06:46 +0000103};
aliguorie3aff4f2009-04-05 19:14:04 +0000104
Max Reitzb543c5c2013-10-11 14:02:10 +0200105static QemuOptsList empty_opts = {
106 .name = "drive",
Markus Armbruster443422f2014-05-28 11:16:58 +0200107 .merge_lists = true,
Max Reitzb543c5c2013-10-11 14:02:10 +0200108 .head = QTAILQ_HEAD_INITIALIZER(empty_opts.head),
109 .desc = {
110 /* no elements => accept any params */
111 { /* end of list */ }
112 },
113};
114
Max Reitz4c7b7e92015-02-05 13:58:22 -0500115static int open_f(BlockBackend *blk, int argc, char **argv)
aliguorie3aff4f2009-04-05 19:14:04 +0000116{
Devin Nakamura43642b32011-07-11 11:22:16 -0400117 int flags = 0;
118 int readonly = 0;
Devin Nakamura43642b32011-07-11 11:22:16 -0400119 int c;
Max Reitzb543c5c2013-10-11 14:02:10 +0200120 QemuOpts *qopts;
Markus Armbruster443422f2014-05-28 11:16:58 +0200121 QDict *opts;
aliguorie3aff4f2009-04-05 19:14:04 +0000122
Max Reitzb543c5c2013-10-11 14:02:10 +0200123 while ((c = getopt(argc, argv, "snrgo:")) != EOF) {
Devin Nakamura43642b32011-07-11 11:22:16 -0400124 switch (c) {
125 case 's':
126 flags |= BDRV_O_SNAPSHOT;
127 break;
128 case 'n':
129 flags |= BDRV_O_NOCACHE | BDRV_O_CACHE_WB;
130 break;
131 case 'r':
132 readonly = 1;
133 break;
Max Reitzb543c5c2013-10-11 14:02:10 +0200134 case 'o':
Markus Armbruster443422f2014-05-28 11:16:58 +0200135 if (!qemu_opts_parse(&empty_opts, optarg, 0)) {
Max Reitzb543c5c2013-10-11 14:02:10 +0200136 printf("could not parse option list -- %s\n", optarg);
Markus Armbruster443422f2014-05-28 11:16:58 +0200137 qemu_opts_reset(&empty_opts);
Max Reitzb543c5c2013-10-11 14:02:10 +0200138 return 0;
139 }
Max Reitzb543c5c2013-10-11 14:02:10 +0200140 break;
Devin Nakamura43642b32011-07-11 11:22:16 -0400141 default:
Markus Armbruster443422f2014-05-28 11:16:58 +0200142 qemu_opts_reset(&empty_opts);
Kevin Wolfc2cdf5c2013-06-05 14:19:36 +0200143 return qemuio_command_usage(&open_cmd);
Naphtali Spreif5edb012010-01-17 16:48:13 +0200144 }
Devin Nakamura43642b32011-07-11 11:22:16 -0400145 }
aliguorie3aff4f2009-04-05 19:14:04 +0000146
Devin Nakamura43642b32011-07-11 11:22:16 -0400147 if (!readonly) {
148 flags |= BDRV_O_RDWR;
149 }
aliguorie3aff4f2009-04-05 19:14:04 +0000150
Markus Armbruster443422f2014-05-28 11:16:58 +0200151 qopts = qemu_opts_find(&empty_opts, NULL);
152 opts = qopts ? qemu_opts_to_qdict(qopts, NULL) : NULL;
153 qemu_opts_reset(&empty_opts);
154
Max Reitzfd0fee32013-12-20 19:28:20 +0100155 if (optind == argc - 1) {
Max Reitz10d9d752015-02-05 13:58:21 -0500156 return openfile(argv[optind], flags, opts);
Max Reitzfd0fee32013-12-20 19:28:20 +0100157 } else if (optind == argc) {
Max Reitz10d9d752015-02-05 13:58:21 -0500158 return openfile(NULL, flags, opts);
Max Reitzfd0fee32013-12-20 19:28:20 +0100159 } else {
Markus Armbruster29f26012014-05-28 11:16:59 +0200160 QDECREF(opts);
Kevin Wolfc2cdf5c2013-06-05 14:19:36 +0200161 return qemuio_command_usage(&open_cmd);
Devin Nakamura43642b32011-07-11 11:22:16 -0400162 }
aliguorie3aff4f2009-04-05 19:14:04 +0000163}
164
Max Reitz4c7b7e92015-02-05 13:58:22 -0500165static int quit_f(BlockBackend *blk, int argc, char **argv)
Kevin Wolfe681be72013-06-05 14:19:34 +0200166{
167 return 1;
168}
169
170static const cmdinfo_t quit_cmd = {
171 .name = "quit",
172 .altname = "q",
173 .cfunc = quit_f,
174 .argmin = -1,
175 .argmax = -1,
176 .flags = CMD_FLAG_GLOBAL,
177 .oneline = "exit the program",
178};
179
aliguorie3aff4f2009-04-05 19:14:04 +0000180static void usage(const char *name)
181{
Devin Nakamura43642b32011-07-11 11:22:16 -0400182 printf(
Kevin Wolfbe6273d2014-11-20 16:27:06 +0100183"Usage: %s [-h] [-V] [-rsnm] [-f FMT] [-c STRING] ... [file]\n"
Stefan Weil84844a22009-06-22 15:08:47 +0200184"QEMU Disk exerciser\n"
aliguorie3aff4f2009-04-05 19:14:04 +0000185"\n"
Maria Kustovad208cc32014-03-18 09:59:19 +0400186" -c, --cmd STRING execute command with its arguments\n"
187" from the given string\n"
Kevin Wolfbe6273d2014-11-20 16:27:06 +0100188" -f, --format FMT specifies the block driver to use\n"
aliguorie3aff4f2009-04-05 19:14:04 +0000189" -r, --read-only export read-only\n"
190" -s, --snapshot use snapshot file\n"
191" -n, --nocache disable host cache\n"
192" -m, --misalign misalign allocations for O_DIRECT\n"
Christoph Hellwig5c6c3a62009-08-20 16:58:35 +0200193" -k, --native-aio use kernel AIO implementation (on Linux only)\n"
Kevin Wolf592fa072012-04-18 12:07:39 +0200194" -t, --cache=MODE use the given cache mode for the image\n"
Stefan Hajnoczid7bb72c2012-03-12 16:36:07 +0000195" -T, --trace FILE enable trace events listed in the given file\n"
aliguorie3aff4f2009-04-05 19:14:04 +0000196" -h, --help display this help and exit\n"
197" -V, --version output version information and exit\n"
Maria Kustovad208cc32014-03-18 09:59:19 +0400198"\n"
199"See '%s -c help' for information on available commands."
aliguorie3aff4f2009-04-05 19:14:04 +0000200"\n",
Maria Kustovad208cc32014-03-18 09:59:19 +0400201 name, name);
aliguorie3aff4f2009-04-05 19:14:04 +0000202}
203
Kevin Wolfd1174f12013-06-05 14:19:37 +0200204static char *get_prompt(void)
205{
206 static char prompt[FILENAME_MAX + 2 /*"> "*/ + 1 /*"\0"*/ ];
207
208 if (!prompt[0]) {
209 snprintf(prompt, sizeof(prompt), "%s> ", progname);
210 }
211
212 return prompt;
213}
214
Stefan Weild5d15072014-01-25 18:18:23 +0100215static void GCC_FMT_ATTR(2, 3) readline_printf_func(void *opaque,
216 const char *fmt, ...)
Kevin Wolfd1174f12013-06-05 14:19:37 +0200217{
Stefan Hajnoczi0cf17e12013-11-14 11:54:17 +0100218 va_list ap;
219 va_start(ap, fmt);
220 vprintf(fmt, ap);
221 va_end(ap);
222}
223
224static void readline_flush_func(void *opaque)
225{
226 fflush(stdout);
227}
228
229static void readline_func(void *opaque, const char *str, void *readline_opaque)
230{
231 char **line = readline_opaque;
232 *line = g_strdup(str);
233}
234
Stefan Hajnoczi46940202013-11-14 11:54:18 +0100235static void completion_match(const char *cmd, void *opaque)
236{
237 readline_add_completion(readline_state, cmd);
238}
239
Stefan Hajnoczi0cf17e12013-11-14 11:54:17 +0100240static void readline_completion_func(void *opaque, const char *str)
241{
Stefan Hajnoczi46940202013-11-14 11:54:18 +0100242 readline_set_completion_index(readline_state, strlen(str));
243 qemuio_complete_command(str, completion_match, NULL);
Stefan Hajnoczi0cf17e12013-11-14 11:54:17 +0100244}
245
246static char *fetchline_readline(void)
247{
248 char *line = NULL;
249
250 readline_start(readline_state, get_prompt(), 0, readline_func, &line);
251 while (!line) {
252 int ch = getchar();
253 if (ch == EOF) {
254 break;
255 }
256 readline_handle_byte(readline_state, ch);
Kevin Wolfd1174f12013-06-05 14:19:37 +0200257 }
258 return line;
259}
Kevin Wolfd1174f12013-06-05 14:19:37 +0200260
Stefan Hajnoczi0cf17e12013-11-14 11:54:17 +0100261#define MAXREADLINESZ 1024
262static char *fetchline_fgets(void)
Kevin Wolfd1174f12013-06-05 14:19:37 +0200263{
264 char *p, *line = g_malloc(MAXREADLINESZ);
265
266 if (!fgets(line, MAXREADLINESZ, stdin)) {
267 g_free(line);
268 return NULL;
269 }
270
271 p = line + strlen(line);
272 if (p != line && p[-1] == '\n') {
273 p[-1] = '\0';
274 }
275
276 return line;
277}
Stefan Hajnoczi0cf17e12013-11-14 11:54:17 +0100278
279static char *fetchline(void)
280{
281 if (readline_state) {
282 return fetchline_readline();
283 } else {
284 return fetchline_fgets();
285 }
286}
Kevin Wolfd1174f12013-06-05 14:19:37 +0200287
288static void prep_fetchline(void *opaque)
289{
290 int *fetchable = opaque;
291
292 qemu_set_fd_handler(STDIN_FILENO, NULL, NULL, NULL);
293 *fetchable= 1;
294}
295
296static void command_loop(void)
297{
298 int i, done = 0, fetchable = 0, prompted = 0;
299 char *input;
300
301 for (i = 0; !done && i < ncmdline; i++) {
Max Reitz4c7b7e92015-02-05 13:58:22 -0500302 done = qemuio_command(qemuio_blk, cmdline[i]);
Kevin Wolfd1174f12013-06-05 14:19:37 +0200303 }
304 if (cmdline) {
305 g_free(cmdline);
306 return;
307 }
308
309 while (!done) {
310 if (!prompted) {
311 printf("%s", get_prompt());
312 fflush(stdout);
313 qemu_set_fd_handler(STDIN_FILENO, prep_fetchline, NULL, &fetchable);
314 prompted = 1;
315 }
316
317 main_loop_wait(false);
318
319 if (!fetchable) {
320 continue;
321 }
322
323 input = fetchline();
324 if (input == NULL) {
325 break;
326 }
Max Reitz4c7b7e92015-02-05 13:58:22 -0500327 done = qemuio_command(qemuio_blk, input);
Kevin Wolfd1174f12013-06-05 14:19:37 +0200328 g_free(input);
329
330 prompted = 0;
331 fetchable = 0;
332 }
333 qemu_set_fd_handler(STDIN_FILENO, NULL, NULL, NULL);
334}
335
336static void add_user_command(char *optarg)
337{
Markus Armbruster5839e532014-08-19 10:31:08 +0200338 cmdline = g_renew(char *, cmdline, ++ncmdline);
Kevin Wolfd1174f12013-06-05 14:19:37 +0200339 cmdline[ncmdline-1] = optarg;
340}
341
Stefan Hajnoczi0cf17e12013-11-14 11:54:17 +0100342static void reenable_tty_echo(void)
343{
344 qemu_set_tty_echo(STDIN_FILENO, true);
345}
346
aliguorie3aff4f2009-04-05 19:14:04 +0000347int main(int argc, char **argv)
348{
Devin Nakamura43642b32011-07-11 11:22:16 -0400349 int readonly = 0;
Kevin Wolfbe6273d2014-11-20 16:27:06 +0100350 const char *sopt = "hVc:d:f:rsnmgkt:T:";
Devin Nakamura43642b32011-07-11 11:22:16 -0400351 const struct option lopt[] = {
352 { "help", 0, NULL, 'h' },
353 { "version", 0, NULL, 'V' },
354 { "offset", 1, NULL, 'o' },
355 { "cmd", 1, NULL, 'c' },
Kevin Wolfbe6273d2014-11-20 16:27:06 +0100356 { "format", 1, NULL, 'f' },
Devin Nakamura43642b32011-07-11 11:22:16 -0400357 { "read-only", 0, NULL, 'r' },
358 { "snapshot", 0, NULL, 's' },
359 { "nocache", 0, NULL, 'n' },
360 { "misalign", 0, NULL, 'm' },
Devin Nakamura43642b32011-07-11 11:22:16 -0400361 { "native-aio", 0, NULL, 'k' },
Paolo Bonzini9e8f1832013-02-08 14:06:11 +0100362 { "discard", 1, NULL, 'd' },
Kevin Wolf592fa072012-04-18 12:07:39 +0200363 { "cache", 1, NULL, 't' },
Stefan Hajnoczid7bb72c2012-03-12 16:36:07 +0000364 { "trace", 1, NULL, 'T' },
Devin Nakamura43642b32011-07-11 11:22:16 -0400365 { NULL, 0, NULL, 0 }
366 };
367 int c;
368 int opt_index = 0;
Paolo Bonzini9e8f1832013-02-08 14:06:11 +0100369 int flags = BDRV_O_UNMAP;
Chrysostomos Nanakos2f78e492014-09-18 14:30:49 +0300370 Error *local_error = NULL;
Max Reitz1b58b432015-02-05 13:58:20 -0500371 QDict *opts = NULL;
aliguorie3aff4f2009-04-05 19:14:04 +0000372
MORITA Kazutaka526eda12013-07-23 17:30:11 +0900373#ifdef CONFIG_POSIX
374 signal(SIGPIPE, SIG_IGN);
375#endif
376
Devin Nakamura43642b32011-07-11 11:22:16 -0400377 progname = basename(argv[0]);
Fam Zheng10f5bff2014-02-10 14:48:51 +0800378 qemu_init_exec_dir(argv[0]);
aliguorie3aff4f2009-04-05 19:14:04 +0000379
Kevin Wolfbe6273d2014-11-20 16:27:06 +0100380 bdrv_init();
381
Devin Nakamura43642b32011-07-11 11:22:16 -0400382 while ((c = getopt_long(argc, argv, sopt, lopt, &opt_index)) != -1) {
383 switch (c) {
384 case 's':
385 flags |= BDRV_O_SNAPSHOT;
386 break;
387 case 'n':
388 flags |= BDRV_O_NOCACHE | BDRV_O_CACHE_WB;
389 break;
Paolo Bonzini9e8f1832013-02-08 14:06:11 +0100390 case 'd':
391 if (bdrv_parse_discard_flags(optarg, &flags) < 0) {
392 error_report("Invalid discard option: %s", optarg);
393 exit(1);
394 }
395 break;
Kevin Wolfbe6273d2014-11-20 16:27:06 +0100396 case 'f':
Max Reitz1b58b432015-02-05 13:58:20 -0500397 if (!opts) {
398 opts = qdict_new();
Kevin Wolfbe6273d2014-11-20 16:27:06 +0100399 }
Max Reitz1b58b432015-02-05 13:58:20 -0500400 qdict_put(opts, "driver", qstring_from_str(optarg));
Kevin Wolfbe6273d2014-11-20 16:27:06 +0100401 break;
Devin Nakamura43642b32011-07-11 11:22:16 -0400402 case 'c':
403 add_user_command(optarg);
404 break;
405 case 'r':
406 readonly = 1;
407 break;
408 case 'm':
Stefan Weilf9883882014-03-05 22:23:00 +0100409 qemuio_misalign = true;
Devin Nakamura43642b32011-07-11 11:22:16 -0400410 break;
Devin Nakamura43642b32011-07-11 11:22:16 -0400411 case 'k':
412 flags |= BDRV_O_NATIVE_AIO;
413 break;
Kevin Wolf592fa072012-04-18 12:07:39 +0200414 case 't':
415 if (bdrv_parse_cache_flags(optarg, &flags) < 0) {
416 error_report("Invalid cache option: %s", optarg);
417 exit(1);
418 }
419 break;
Stefan Hajnoczid7bb72c2012-03-12 16:36:07 +0000420 case 'T':
LluĂ­s Vilanova5b808272014-05-27 15:02:14 +0200421 if (!trace_init_backends(optarg, NULL)) {
Stefan Hajnoczid7bb72c2012-03-12 16:36:07 +0000422 exit(1); /* error message will have been printed */
423 }
424 break;
Devin Nakamura43642b32011-07-11 11:22:16 -0400425 case 'V':
Kevin Wolf02da3862013-06-05 14:19:40 +0200426 printf("%s version %s\n", progname, QEMU_VERSION);
Devin Nakamura43642b32011-07-11 11:22:16 -0400427 exit(0);
428 case 'h':
429 usage(progname);
430 exit(0);
431 default:
432 usage(progname);
433 exit(1);
Naphtali Spreif5edb012010-01-17 16:48:13 +0200434 }
Devin Nakamura43642b32011-07-11 11:22:16 -0400435 }
aliguorie3aff4f2009-04-05 19:14:04 +0000436
Devin Nakamura43642b32011-07-11 11:22:16 -0400437 if ((argc - optind) > 1) {
438 usage(progname);
439 exit(1);
440 }
aliguorie3aff4f2009-04-05 19:14:04 +0000441
Chrysostomos Nanakos2f78e492014-09-18 14:30:49 +0300442 if (qemu_init_main_loop(&local_error)) {
Markus Armbruster565f65d2015-02-12 13:55:05 +0100443 error_report_err(local_error);
Chrysostomos Nanakos2f78e492014-09-18 14:30:49 +0300444 exit(1);
445 }
Zhi Yong Wua57d1142012-02-19 22:24:59 +0800446
Devin Nakamura43642b32011-07-11 11:22:16 -0400447 /* initialize commands */
Kevin Wolfc2cdf5c2013-06-05 14:19:36 +0200448 qemuio_add_command(&quit_cmd);
449 qemuio_add_command(&open_cmd);
450 qemuio_add_command(&close_cmd);
Devin Nakamura43642b32011-07-11 11:22:16 -0400451
Stefan Hajnoczi0cf17e12013-11-14 11:54:17 +0100452 if (isatty(STDIN_FILENO)) {
453 readline_state = readline_init(readline_printf_func,
454 readline_flush_func,
455 NULL,
456 readline_completion_func);
457 qemu_set_tty_echo(STDIN_FILENO, false);
458 atexit(reenable_tty_echo);
459 }
460
Devin Nakamura43642b32011-07-11 11:22:16 -0400461 /* open the device */
462 if (!readonly) {
463 flags |= BDRV_O_RDWR;
464 }
465
466 if ((argc - optind) == 1) {
Max Reitz10d9d752015-02-05 13:58:21 -0500467 openfile(argv[optind], flags, opts);
Devin Nakamura43642b32011-07-11 11:22:16 -0400468 }
469 command_loop();
470
471 /*
Stefan Hajnoczi922453b2011-11-30 12:23:43 +0000472 * Make sure all outstanding requests complete before the program exits.
Devin Nakamura43642b32011-07-11 11:22:16 -0400473 */
Stefan Hajnoczi922453b2011-11-30 12:23:43 +0000474 bdrv_drain_all();
Devin Nakamura43642b32011-07-11 11:22:16 -0400475
Markus Armbruster26f54e92014-10-07 13:59:04 +0200476 blk_unref(qemuio_blk);
Stefan Hajnoczi0cf17e12013-11-14 11:54:17 +0100477 g_free(readline_state);
Devin Nakamura43642b32011-07-11 11:22:16 -0400478 return 0;
aliguorie3aff4f2009-04-05 19:14:04 +0000479}