blob: d47228a963ade968a2560288fd93654d6b2d6e56 [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"
Markus Armbrusterd49b6832015-03-17 18:29:20 +010018#include "qemu/error-report.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010019#include "qemu/main-loop.h"
Max Reitzb543c5c2013-10-11 14:02:10 +020020#include "qemu/option.h"
21#include "qemu/config-file.h"
Stefan Hajnoczi0cf17e12013-11-14 11:54:17 +010022#include "qemu/readline.h"
Markus Armbrusterd49b6832015-03-17 18:29:20 +010023#include "qapi/qmp/qstring.h"
Markus Armbruster26f54e92014-10-07 13:59:04 +020024#include "sysemu/block-backend.h"
Paolo Bonzini737e1502012-12-17 18:19:44 +010025#include "block/block_int.h"
Stefan Hajnoczid7bb72c2012-03-12 16:36:07 +000026#include "trace/control.h"
aliguorie3aff4f2009-04-05 19:14:04 +000027
Devin Nakamura43642b32011-07-11 11:22:16 -040028#define CMD_NOFILE_OK 0x01
aliguorie3aff4f2009-04-05 19:14:04 +000029
Stefan Weilf9883882014-03-05 22:23:00 +010030static char *progname;
aliguorie3aff4f2009-04-05 19:14:04 +000031
Markus Armbruster26f54e92014-10-07 13:59:04 +020032static BlockBackend *qemuio_blk;
Kevin Wolf191c2892010-09-16 13:18:08 +020033
Kevin Wolfd1174f12013-06-05 14:19:37 +020034/* qemu-io commands passed using -c */
35static int ncmdline;
36static char **cmdline;
37
Stefan Hajnoczi0cf17e12013-11-14 11:54:17 +010038static ReadLineState *readline_state;
39
Max Reitz4c7b7e92015-02-05 13:58:22 -050040static int close_f(BlockBackend *blk, int argc, char **argv)
aliguorie3aff4f2009-04-05 19:14:04 +000041{
Markus Armbruster26f54e92014-10-07 13:59:04 +020042 blk_unref(qemuio_blk);
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;
Daniel P. Berrange8caf0212015-05-12 17:09:21 +010057 BlockDriverState *bs;
Max Reitz34b5d2c2013-09-05 14:45:29 +020058
Max Reitz1b58b432015-02-05 13:58:20 -050059 if (qemuio_blk) {
Markus Armbrusterb9884682015-12-18 16:35:18 +010060 error_report("file open already, try 'help close'");
Markus Armbruster29f26012014-05-28 11:16:59 +020061 QDECREF(opts);
Devin Nakamura43642b32011-07-11 11:22:16 -040062 return 1;
63 }
aliguorie3aff4f2009-04-05 19:14:04 +000064
Max Reitz1b58b432015-02-05 13:58:20 -050065 qemuio_blk = blk_new_open("hda", name, NULL, opts, flags, &local_err);
66 if (!qemuio_blk) {
Markus Armbrusterb9884682015-12-18 16:35:18 +010067 error_reportf_err(local_err, "can't open%s%s: ",
68 name ? " device " : "", name ?: "");
Markus Armbrusterdbb651c2014-09-08 18:50:58 +020069 return 1;
Devin Nakamura43642b32011-07-11 11:22:16 -040070 }
Christoph Hellwig1db69472009-07-15 23:11:21 +020071
Daniel P. Berrange8caf0212015-05-12 17:09:21 +010072 bs = blk_bs(qemuio_blk);
73 if (bdrv_is_encrypted(bs)) {
74 char password[256];
75 printf("Disk image '%s' is encrypted.\n", name);
76 if (qemu_read_password(password, sizeof(password)) < 0) {
77 error_report("No password given");
78 goto error;
79 }
80 if (bdrv_set_key(bs, password) < 0) {
81 error_report("invalid password");
82 goto error;
83 }
84 }
85
86
Devin Nakamura43642b32011-07-11 11:22:16 -040087 return 0;
Daniel P. Berrange8caf0212015-05-12 17:09:21 +010088
89 error:
90 blk_unref(qemuio_blk);
91 qemuio_blk = NULL;
92 return 1;
aliguorie3aff4f2009-04-05 19:14:04 +000093}
94
Devin Nakamura43642b32011-07-11 11:22:16 -040095static void open_help(void)
aliguorie3aff4f2009-04-05 19:14:04 +000096{
Devin Nakamura43642b32011-07-11 11:22:16 -040097 printf(
aliguorie3aff4f2009-04-05 19:14:04 +000098"\n"
99" opens a new file in the requested mode\n"
100"\n"
101" Example:\n"
102" 'open -Cn /tmp/data' - creates/opens data file read-write and uncached\n"
103"\n"
104" Opens a file for subsequent use by all of the other qemu-io commands.\n"
aliguorie3aff4f2009-04-05 19:14:04 +0000105" -r, -- open file read-only\n"
106" -s, -- use snapshot file\n"
107" -n, -- disable host cache\n"
Max Reitzb543c5c2013-10-11 14:02:10 +0200108" -o, -- options to be given to the block driver"
aliguorie3aff4f2009-04-05 19:14:04 +0000109"\n");
110}
111
Max Reitz4c7b7e92015-02-05 13:58:22 -0500112static int open_f(BlockBackend *blk, int argc, char **argv);
Blue Swirl22a2bdc2009-11-21 09:06:46 +0000113
114static const cmdinfo_t open_cmd = {
Devin Nakamura43642b32011-07-11 11:22:16 -0400115 .name = "open",
116 .altname = "o",
117 .cfunc = open_f,
118 .argmin = 1,
119 .argmax = -1,
120 .flags = CMD_NOFILE_OK,
Max Reitzb543c5c2013-10-11 14:02:10 +0200121 .args = "[-Crsn] [-o options] [path]",
Devin Nakamura43642b32011-07-11 11:22:16 -0400122 .oneline = "open the file specified by path",
123 .help = open_help,
Blue Swirl22a2bdc2009-11-21 09:06:46 +0000124};
aliguorie3aff4f2009-04-05 19:14:04 +0000125
Max Reitzb543c5c2013-10-11 14:02:10 +0200126static QemuOptsList empty_opts = {
127 .name = "drive",
Markus Armbruster443422f2014-05-28 11:16:58 +0200128 .merge_lists = true,
Max Reitzb543c5c2013-10-11 14:02:10 +0200129 .head = QTAILQ_HEAD_INITIALIZER(empty_opts.head),
130 .desc = {
131 /* no elements => accept any params */
132 { /* end of list */ }
133 },
134};
135
Max Reitz4c7b7e92015-02-05 13:58:22 -0500136static int open_f(BlockBackend *blk, int argc, char **argv)
aliguorie3aff4f2009-04-05 19:14:04 +0000137{
Devin Nakamura43642b32011-07-11 11:22:16 -0400138 int flags = 0;
139 int readonly = 0;
Devin Nakamura43642b32011-07-11 11:22:16 -0400140 int c;
Max Reitzb543c5c2013-10-11 14:02:10 +0200141 QemuOpts *qopts;
Markus Armbruster443422f2014-05-28 11:16:58 +0200142 QDict *opts;
aliguorie3aff4f2009-04-05 19:14:04 +0000143
Eric Blakeb062ad82015-05-12 09:10:56 -0600144 while ((c = getopt(argc, argv, "snrgo:")) != -1) {
Devin Nakamura43642b32011-07-11 11:22:16 -0400145 switch (c) {
146 case 's':
147 flags |= BDRV_O_SNAPSHOT;
148 break;
149 case 'n':
150 flags |= BDRV_O_NOCACHE | BDRV_O_CACHE_WB;
151 break;
152 case 'r':
153 readonly = 1;
154 break;
Max Reitzb543c5c2013-10-11 14:02:10 +0200155 case 'o':
Markus Armbruster70b94332015-02-13 12:50:26 +0100156 if (!qemu_opts_parse_noisily(&empty_opts, optarg, false)) {
Markus Armbruster443422f2014-05-28 11:16:58 +0200157 qemu_opts_reset(&empty_opts);
Max Reitzb543c5c2013-10-11 14:02:10 +0200158 return 0;
159 }
Max Reitzb543c5c2013-10-11 14:02:10 +0200160 break;
Devin Nakamura43642b32011-07-11 11:22:16 -0400161 default:
Markus Armbruster443422f2014-05-28 11:16:58 +0200162 qemu_opts_reset(&empty_opts);
Kevin Wolfc2cdf5c2013-06-05 14:19:36 +0200163 return qemuio_command_usage(&open_cmd);
Naphtali Spreif5edb012010-01-17 16:48:13 +0200164 }
Devin Nakamura43642b32011-07-11 11:22:16 -0400165 }
aliguorie3aff4f2009-04-05 19:14:04 +0000166
Devin Nakamura43642b32011-07-11 11:22:16 -0400167 if (!readonly) {
168 flags |= BDRV_O_RDWR;
169 }
aliguorie3aff4f2009-04-05 19:14:04 +0000170
Markus Armbruster443422f2014-05-28 11:16:58 +0200171 qopts = qemu_opts_find(&empty_opts, NULL);
172 opts = qopts ? qemu_opts_to_qdict(qopts, NULL) : NULL;
173 qemu_opts_reset(&empty_opts);
174
Max Reitzfd0fee32013-12-20 19:28:20 +0100175 if (optind == argc - 1) {
Max Reitz10d9d752015-02-05 13:58:21 -0500176 return openfile(argv[optind], flags, opts);
Max Reitzfd0fee32013-12-20 19:28:20 +0100177 } else if (optind == argc) {
Max Reitz10d9d752015-02-05 13:58:21 -0500178 return openfile(NULL, flags, opts);
Max Reitzfd0fee32013-12-20 19:28:20 +0100179 } else {
Markus Armbruster29f26012014-05-28 11:16:59 +0200180 QDECREF(opts);
Kevin Wolfc2cdf5c2013-06-05 14:19:36 +0200181 return qemuio_command_usage(&open_cmd);
Devin Nakamura43642b32011-07-11 11:22:16 -0400182 }
aliguorie3aff4f2009-04-05 19:14:04 +0000183}
184
Max Reitz4c7b7e92015-02-05 13:58:22 -0500185static int quit_f(BlockBackend *blk, int argc, char **argv)
Kevin Wolfe681be72013-06-05 14:19:34 +0200186{
187 return 1;
188}
189
190static const cmdinfo_t quit_cmd = {
191 .name = "quit",
192 .altname = "q",
193 .cfunc = quit_f,
194 .argmin = -1,
195 .argmax = -1,
196 .flags = CMD_FLAG_GLOBAL,
197 .oneline = "exit the program",
198};
199
aliguorie3aff4f2009-04-05 19:14:04 +0000200static void usage(const char *name)
201{
Devin Nakamura43642b32011-07-11 11:22:16 -0400202 printf(
Kevin Wolfbe6273d2014-11-20 16:27:06 +0100203"Usage: %s [-h] [-V] [-rsnm] [-f FMT] [-c STRING] ... [file]\n"
Stefan Weil84844a22009-06-22 15:08:47 +0200204"QEMU Disk exerciser\n"
aliguorie3aff4f2009-04-05 19:14:04 +0000205"\n"
Maria Kustovad208cc32014-03-18 09:59:19 +0400206" -c, --cmd STRING execute command with its arguments\n"
207" from the given string\n"
Kevin Wolfbe6273d2014-11-20 16:27:06 +0100208" -f, --format FMT specifies the block driver to use\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"
212" -m, --misalign misalign allocations for O_DIRECT\n"
Christoph Hellwig5c6c3a62009-08-20 16:58:35 +0200213" -k, --native-aio use kernel AIO implementation (on Linux only)\n"
Kevin Wolf592fa072012-04-18 12:07:39 +0200214" -t, --cache=MODE use the given cache mode for the image\n"
Stefan Hajnoczid7bb72c2012-03-12 16:36:07 +0000215" -T, --trace FILE enable trace events listed in the given file\n"
aliguorie3aff4f2009-04-05 19:14:04 +0000216" -h, --help display this help and exit\n"
217" -V, --version output version information and exit\n"
Maria Kustovad208cc32014-03-18 09:59:19 +0400218"\n"
219"See '%s -c help' for information on available commands."
aliguorie3aff4f2009-04-05 19:14:04 +0000220"\n",
Maria Kustovad208cc32014-03-18 09:59:19 +0400221 name, name);
aliguorie3aff4f2009-04-05 19:14:04 +0000222}
223
Kevin Wolfd1174f12013-06-05 14:19:37 +0200224static char *get_prompt(void)
225{
226 static char prompt[FILENAME_MAX + 2 /*"> "*/ + 1 /*"\0"*/ ];
227
228 if (!prompt[0]) {
229 snprintf(prompt, sizeof(prompt), "%s> ", progname);
230 }
231
232 return prompt;
233}
234
Stefan Weild5d15072014-01-25 18:18:23 +0100235static void GCC_FMT_ATTR(2, 3) readline_printf_func(void *opaque,
236 const char *fmt, ...)
Kevin Wolfd1174f12013-06-05 14:19:37 +0200237{
Stefan Hajnoczi0cf17e12013-11-14 11:54:17 +0100238 va_list ap;
239 va_start(ap, fmt);
240 vprintf(fmt, ap);
241 va_end(ap);
242}
243
244static void readline_flush_func(void *opaque)
245{
246 fflush(stdout);
247}
248
249static void readline_func(void *opaque, const char *str, void *readline_opaque)
250{
251 char **line = readline_opaque;
252 *line = g_strdup(str);
253}
254
Stefan Hajnoczi46940202013-11-14 11:54:18 +0100255static void completion_match(const char *cmd, void *opaque)
256{
257 readline_add_completion(readline_state, cmd);
258}
259
Stefan Hajnoczi0cf17e12013-11-14 11:54:17 +0100260static void readline_completion_func(void *opaque, const char *str)
261{
Stefan Hajnoczi46940202013-11-14 11:54:18 +0100262 readline_set_completion_index(readline_state, strlen(str));
263 qemuio_complete_command(str, completion_match, NULL);
Stefan Hajnoczi0cf17e12013-11-14 11:54:17 +0100264}
265
266static char *fetchline_readline(void)
267{
268 char *line = NULL;
269
270 readline_start(readline_state, get_prompt(), 0, readline_func, &line);
271 while (!line) {
272 int ch = getchar();
273 if (ch == EOF) {
274 break;
275 }
276 readline_handle_byte(readline_state, ch);
Kevin Wolfd1174f12013-06-05 14:19:37 +0200277 }
278 return line;
279}
Kevin Wolfd1174f12013-06-05 14:19:37 +0200280
Stefan Hajnoczi0cf17e12013-11-14 11:54:17 +0100281#define MAXREADLINESZ 1024
282static char *fetchline_fgets(void)
Kevin Wolfd1174f12013-06-05 14:19:37 +0200283{
284 char *p, *line = g_malloc(MAXREADLINESZ);
285
286 if (!fgets(line, MAXREADLINESZ, stdin)) {
287 g_free(line);
288 return NULL;
289 }
290
291 p = line + strlen(line);
292 if (p != line && p[-1] == '\n') {
293 p[-1] = '\0';
294 }
295
296 return line;
297}
Stefan Hajnoczi0cf17e12013-11-14 11:54:17 +0100298
299static char *fetchline(void)
300{
301 if (readline_state) {
302 return fetchline_readline();
303 } else {
304 return fetchline_fgets();
305 }
306}
Kevin Wolfd1174f12013-06-05 14:19:37 +0200307
308static void prep_fetchline(void *opaque)
309{
310 int *fetchable = opaque;
311
312 qemu_set_fd_handler(STDIN_FILENO, NULL, NULL, NULL);
313 *fetchable= 1;
314}
315
316static void command_loop(void)
317{
318 int i, done = 0, fetchable = 0, prompted = 0;
319 char *input;
320
321 for (i = 0; !done && i < ncmdline; i++) {
Max Reitz4c7b7e92015-02-05 13:58:22 -0500322 done = qemuio_command(qemuio_blk, cmdline[i]);
Kevin Wolfd1174f12013-06-05 14:19:37 +0200323 }
324 if (cmdline) {
325 g_free(cmdline);
326 return;
327 }
328
329 while (!done) {
330 if (!prompted) {
331 printf("%s", get_prompt());
332 fflush(stdout);
333 qemu_set_fd_handler(STDIN_FILENO, prep_fetchline, NULL, &fetchable);
334 prompted = 1;
335 }
336
337 main_loop_wait(false);
338
339 if (!fetchable) {
340 continue;
341 }
342
343 input = fetchline();
344 if (input == NULL) {
345 break;
346 }
Max Reitz4c7b7e92015-02-05 13:58:22 -0500347 done = qemuio_command(qemuio_blk, input);
Kevin Wolfd1174f12013-06-05 14:19:37 +0200348 g_free(input);
349
350 prompted = 0;
351 fetchable = 0;
352 }
353 qemu_set_fd_handler(STDIN_FILENO, NULL, NULL, NULL);
354}
355
356static void add_user_command(char *optarg)
357{
Markus Armbruster5839e532014-08-19 10:31:08 +0200358 cmdline = g_renew(char *, cmdline, ++ncmdline);
Kevin Wolfd1174f12013-06-05 14:19:37 +0200359 cmdline[ncmdline-1] = optarg;
360}
361
Stefan Hajnoczi0cf17e12013-11-14 11:54:17 +0100362static void reenable_tty_echo(void)
363{
364 qemu_set_tty_echo(STDIN_FILENO, true);
365}
366
aliguorie3aff4f2009-04-05 19:14:04 +0000367int main(int argc, char **argv)
368{
Devin Nakamura43642b32011-07-11 11:22:16 -0400369 int readonly = 0;
Kevin Wolfbe6273d2014-11-20 16:27:06 +0100370 const char *sopt = "hVc:d:f: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' },
Kevin Wolfbe6273d2014-11-20 16:27:06 +0100376 { "format", 1, NULL, 'f' },
Devin Nakamura43642b32011-07-11 11:22:16 -0400377 { "read-only", 0, NULL, 'r' },
378 { "snapshot", 0, NULL, 's' },
379 { "nocache", 0, NULL, 'n' },
380 { "misalign", 0, NULL, 'm' },
Devin Nakamura43642b32011-07-11 11:22:16 -0400381 { "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;
Chrysostomos Nanakos2f78e492014-09-18 14:30:49 +0300390 Error *local_error = NULL;
Max Reitz1b58b432015-02-05 13:58:20 -0500391 QDict *opts = NULL;
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
Kevin Wolfbe6273d2014-11-20 16:27:06 +0100400 bdrv_init();
401
Devin Nakamura43642b32011-07-11 11:22:16 -0400402 while ((c = getopt_long(argc, argv, sopt, lopt, &opt_index)) != -1) {
403 switch (c) {
404 case 's':
405 flags |= BDRV_O_SNAPSHOT;
406 break;
407 case 'n':
408 flags |= BDRV_O_NOCACHE | BDRV_O_CACHE_WB;
409 break;
Paolo Bonzini9e8f1832013-02-08 14:06:11 +0100410 case 'd':
411 if (bdrv_parse_discard_flags(optarg, &flags) < 0) {
412 error_report("Invalid discard option: %s", optarg);
413 exit(1);
414 }
415 break;
Kevin Wolfbe6273d2014-11-20 16:27:06 +0100416 case 'f':
Max Reitz1b58b432015-02-05 13:58:20 -0500417 if (!opts) {
418 opts = qdict_new();
Kevin Wolfbe6273d2014-11-20 16:27:06 +0100419 }
Max Reitz1b58b432015-02-05 13:58:20 -0500420 qdict_put(opts, "driver", qstring_from_str(optarg));
Kevin Wolfbe6273d2014-11-20 16:27:06 +0100421 break;
Devin Nakamura43642b32011-07-11 11:22:16 -0400422 case 'c':
423 add_user_command(optarg);
424 break;
425 case 'r':
426 readonly = 1;
427 break;
428 case 'm':
Stefan Weilf9883882014-03-05 22:23:00 +0100429 qemuio_misalign = true;
Devin Nakamura43642b32011-07-11 11:22:16 -0400430 break;
Devin Nakamura43642b32011-07-11 11:22:16 -0400431 case 'k':
432 flags |= BDRV_O_NATIVE_AIO;
433 break;
Kevin Wolf592fa072012-04-18 12:07:39 +0200434 case 't':
435 if (bdrv_parse_cache_flags(optarg, &flags) < 0) {
436 error_report("Invalid cache option: %s", optarg);
437 exit(1);
438 }
439 break;
Stefan Hajnoczid7bb72c2012-03-12 16:36:07 +0000440 case 'T':
LluĂ­s Vilanova5b808272014-05-27 15:02:14 +0200441 if (!trace_init_backends(optarg, NULL)) {
Stefan Hajnoczid7bb72c2012-03-12 16:36:07 +0000442 exit(1); /* error message will have been printed */
443 }
444 break;
Devin Nakamura43642b32011-07-11 11:22:16 -0400445 case 'V':
Kevin Wolf02da3862013-06-05 14:19:40 +0200446 printf("%s version %s\n", progname, QEMU_VERSION);
Devin Nakamura43642b32011-07-11 11:22:16 -0400447 exit(0);
448 case 'h':
449 usage(progname);
450 exit(0);
451 default:
452 usage(progname);
453 exit(1);
Naphtali Spreif5edb012010-01-17 16:48:13 +0200454 }
Devin Nakamura43642b32011-07-11 11:22:16 -0400455 }
aliguorie3aff4f2009-04-05 19:14:04 +0000456
Devin Nakamura43642b32011-07-11 11:22:16 -0400457 if ((argc - optind) > 1) {
458 usage(progname);
459 exit(1);
460 }
aliguorie3aff4f2009-04-05 19:14:04 +0000461
Chrysostomos Nanakos2f78e492014-09-18 14:30:49 +0300462 if (qemu_init_main_loop(&local_error)) {
Markus Armbruster565f65d2015-02-12 13:55:05 +0100463 error_report_err(local_error);
Chrysostomos Nanakos2f78e492014-09-18 14:30:49 +0300464 exit(1);
465 }
Zhi Yong Wua57d1142012-02-19 22:24:59 +0800466
Devin Nakamura43642b32011-07-11 11:22:16 -0400467 /* initialize commands */
Kevin Wolfc2cdf5c2013-06-05 14:19:36 +0200468 qemuio_add_command(&quit_cmd);
469 qemuio_add_command(&open_cmd);
470 qemuio_add_command(&close_cmd);
Devin Nakamura43642b32011-07-11 11:22:16 -0400471
Stefan Hajnoczi0cf17e12013-11-14 11:54:17 +0100472 if (isatty(STDIN_FILENO)) {
473 readline_state = readline_init(readline_printf_func,
474 readline_flush_func,
475 NULL,
476 readline_completion_func);
477 qemu_set_tty_echo(STDIN_FILENO, false);
478 atexit(reenable_tty_echo);
479 }
480
Devin Nakamura43642b32011-07-11 11:22:16 -0400481 /* open the device */
482 if (!readonly) {
483 flags |= BDRV_O_RDWR;
484 }
485
486 if ((argc - optind) == 1) {
Max Reitz10d9d752015-02-05 13:58:21 -0500487 openfile(argv[optind], flags, opts);
Devin Nakamura43642b32011-07-11 11:22:16 -0400488 }
489 command_loop();
490
491 /*
Stefan Hajnoczi922453b2011-11-30 12:23:43 +0000492 * Make sure all outstanding requests complete before the program exits.
Devin Nakamura43642b32011-07-11 11:22:16 -0400493 */
Stefan Hajnoczi922453b2011-11-30 12:23:43 +0000494 bdrv_drain_all();
Devin Nakamura43642b32011-07-11 11:22:16 -0400495
Markus Armbruster26f54e92014-10-07 13:59:04 +0200496 blk_unref(qemuio_blk);
Stefan Hajnoczi0cf17e12013-11-14 11:54:17 +0100497 g_free(readline_state);
Devin Nakamura43642b32011-07-11 11:22:16 -0400498 return 0;
aliguorie3aff4f2009-04-05 19:14:04 +0000499}