blob: 269f17cc2cced0e015d518067446c7ff192d7964 [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) {
Devin Nakamura43642b32011-07-11 11:22:16 -040060 fprintf(stderr, "file open already, try 'help close'\n");
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 Armbrusterdbb651c2014-09-08 18:50:58 +020067 fprintf(stderr, "%s: can't open%s%s: %s\n", progname,
68 name ? " device " : "", name ?: "",
69 error_get_pretty(local_err));
70 error_free(local_err);
Markus Armbrusterdbb651c2014-09-08 18:50:58 +020071 return 1;
Devin Nakamura43642b32011-07-11 11:22:16 -040072 }
Christoph Hellwig1db69472009-07-15 23:11:21 +020073
Daniel P. Berrange8caf0212015-05-12 17:09:21 +010074 bs = blk_bs(qemuio_blk);
75 if (bdrv_is_encrypted(bs)) {
76 char password[256];
77 printf("Disk image '%s' is encrypted.\n", name);
78 if (qemu_read_password(password, sizeof(password)) < 0) {
79 error_report("No password given");
80 goto error;
81 }
82 if (bdrv_set_key(bs, password) < 0) {
83 error_report("invalid password");
84 goto error;
85 }
86 }
87
88
Devin Nakamura43642b32011-07-11 11:22:16 -040089 return 0;
Daniel P. Berrange8caf0212015-05-12 17:09:21 +010090
91 error:
92 blk_unref(qemuio_blk);
93 qemuio_blk = NULL;
94 return 1;
aliguorie3aff4f2009-04-05 19:14:04 +000095}
96
Devin Nakamura43642b32011-07-11 11:22:16 -040097static void open_help(void)
aliguorie3aff4f2009-04-05 19:14:04 +000098{
Devin Nakamura43642b32011-07-11 11:22:16 -040099 printf(
aliguorie3aff4f2009-04-05 19:14:04 +0000100"\n"
101" opens a new file in the requested mode\n"
102"\n"
103" Example:\n"
104" 'open -Cn /tmp/data' - creates/opens data file read-write and uncached\n"
105"\n"
106" Opens a file for subsequent use by all of the other qemu-io commands.\n"
aliguorie3aff4f2009-04-05 19:14:04 +0000107" -r, -- open file read-only\n"
108" -s, -- use snapshot file\n"
109" -n, -- disable host cache\n"
Max Reitzb543c5c2013-10-11 14:02:10 +0200110" -o, -- options to be given to the block driver"
aliguorie3aff4f2009-04-05 19:14:04 +0000111"\n");
112}
113
Max Reitz4c7b7e92015-02-05 13:58:22 -0500114static int open_f(BlockBackend *blk, int argc, char **argv);
Blue Swirl22a2bdc2009-11-21 09:06:46 +0000115
116static const cmdinfo_t open_cmd = {
Devin Nakamura43642b32011-07-11 11:22:16 -0400117 .name = "open",
118 .altname = "o",
119 .cfunc = open_f,
120 .argmin = 1,
121 .argmax = -1,
122 .flags = CMD_NOFILE_OK,
Max Reitzb543c5c2013-10-11 14:02:10 +0200123 .args = "[-Crsn] [-o options] [path]",
Devin Nakamura43642b32011-07-11 11:22:16 -0400124 .oneline = "open the file specified by path",
125 .help = open_help,
Blue Swirl22a2bdc2009-11-21 09:06:46 +0000126};
aliguorie3aff4f2009-04-05 19:14:04 +0000127
Max Reitzb543c5c2013-10-11 14:02:10 +0200128static QemuOptsList empty_opts = {
129 .name = "drive",
Markus Armbruster443422f2014-05-28 11:16:58 +0200130 .merge_lists = true,
Max Reitzb543c5c2013-10-11 14:02:10 +0200131 .head = QTAILQ_HEAD_INITIALIZER(empty_opts.head),
132 .desc = {
133 /* no elements => accept any params */
134 { /* end of list */ }
135 },
136};
137
Max Reitz4c7b7e92015-02-05 13:58:22 -0500138static int open_f(BlockBackend *blk, int argc, char **argv)
aliguorie3aff4f2009-04-05 19:14:04 +0000139{
Devin Nakamura43642b32011-07-11 11:22:16 -0400140 int flags = 0;
141 int readonly = 0;
Devin Nakamura43642b32011-07-11 11:22:16 -0400142 int c;
Max Reitzb543c5c2013-10-11 14:02:10 +0200143 QemuOpts *qopts;
Markus Armbruster443422f2014-05-28 11:16:58 +0200144 QDict *opts;
aliguorie3aff4f2009-04-05 19:14:04 +0000145
Eric Blakeb062ad82015-05-12 09:10:56 -0600146 while ((c = getopt(argc, argv, "snrgo:")) != -1) {
Devin Nakamura43642b32011-07-11 11:22:16 -0400147 switch (c) {
148 case 's':
149 flags |= BDRV_O_SNAPSHOT;
150 break;
151 case 'n':
152 flags |= BDRV_O_NOCACHE | BDRV_O_CACHE_WB;
153 break;
154 case 'r':
155 readonly = 1;
156 break;
Max Reitzb543c5c2013-10-11 14:02:10 +0200157 case 'o':
Markus Armbruster70b94332015-02-13 12:50:26 +0100158 if (!qemu_opts_parse_noisily(&empty_opts, optarg, false)) {
Markus Armbruster443422f2014-05-28 11:16:58 +0200159 qemu_opts_reset(&empty_opts);
Max Reitzb543c5c2013-10-11 14:02:10 +0200160 return 0;
161 }
Max Reitzb543c5c2013-10-11 14:02:10 +0200162 break;
Devin Nakamura43642b32011-07-11 11:22:16 -0400163 default:
Markus Armbruster443422f2014-05-28 11:16:58 +0200164 qemu_opts_reset(&empty_opts);
Kevin Wolfc2cdf5c2013-06-05 14:19:36 +0200165 return qemuio_command_usage(&open_cmd);
Naphtali Spreif5edb012010-01-17 16:48:13 +0200166 }
Devin Nakamura43642b32011-07-11 11:22:16 -0400167 }
aliguorie3aff4f2009-04-05 19:14:04 +0000168
Devin Nakamura43642b32011-07-11 11:22:16 -0400169 if (!readonly) {
170 flags |= BDRV_O_RDWR;
171 }
aliguorie3aff4f2009-04-05 19:14:04 +0000172
Markus Armbruster443422f2014-05-28 11:16:58 +0200173 qopts = qemu_opts_find(&empty_opts, NULL);
174 opts = qopts ? qemu_opts_to_qdict(qopts, NULL) : NULL;
175 qemu_opts_reset(&empty_opts);
176
Max Reitzfd0fee32013-12-20 19:28:20 +0100177 if (optind == argc - 1) {
Max Reitz10d9d752015-02-05 13:58:21 -0500178 return openfile(argv[optind], flags, opts);
Max Reitzfd0fee32013-12-20 19:28:20 +0100179 } else if (optind == argc) {
Max Reitz10d9d752015-02-05 13:58:21 -0500180 return openfile(NULL, flags, opts);
Max Reitzfd0fee32013-12-20 19:28:20 +0100181 } else {
Markus Armbruster29f26012014-05-28 11:16:59 +0200182 QDECREF(opts);
Kevin Wolfc2cdf5c2013-06-05 14:19:36 +0200183 return qemuio_command_usage(&open_cmd);
Devin Nakamura43642b32011-07-11 11:22:16 -0400184 }
aliguorie3aff4f2009-04-05 19:14:04 +0000185}
186
Max Reitz4c7b7e92015-02-05 13:58:22 -0500187static int quit_f(BlockBackend *blk, int argc, char **argv)
Kevin Wolfe681be72013-06-05 14:19:34 +0200188{
189 return 1;
190}
191
192static const cmdinfo_t quit_cmd = {
193 .name = "quit",
194 .altname = "q",
195 .cfunc = quit_f,
196 .argmin = -1,
197 .argmax = -1,
198 .flags = CMD_FLAG_GLOBAL,
199 .oneline = "exit the program",
200};
201
aliguorie3aff4f2009-04-05 19:14:04 +0000202static void usage(const char *name)
203{
Devin Nakamura43642b32011-07-11 11:22:16 -0400204 printf(
Kevin Wolfbe6273d2014-11-20 16:27:06 +0100205"Usage: %s [-h] [-V] [-rsnm] [-f FMT] [-c STRING] ... [file]\n"
Stefan Weil84844a22009-06-22 15:08:47 +0200206"QEMU Disk exerciser\n"
aliguorie3aff4f2009-04-05 19:14:04 +0000207"\n"
Maria Kustovad208cc32014-03-18 09:59:19 +0400208" -c, --cmd STRING execute command with its arguments\n"
209" from the given string\n"
Kevin Wolfbe6273d2014-11-20 16:27:06 +0100210" -f, --format FMT specifies the block driver to use\n"
aliguorie3aff4f2009-04-05 19:14:04 +0000211" -r, --read-only export read-only\n"
212" -s, --snapshot use snapshot file\n"
213" -n, --nocache disable host cache\n"
214" -m, --misalign misalign allocations for O_DIRECT\n"
Christoph Hellwig5c6c3a62009-08-20 16:58:35 +0200215" -k, --native-aio use kernel AIO implementation (on Linux only)\n"
Kevin Wolf592fa072012-04-18 12:07:39 +0200216" -t, --cache=MODE use the given cache mode for the image\n"
Stefan Hajnoczid7bb72c2012-03-12 16:36:07 +0000217" -T, --trace FILE enable trace events listed in the given file\n"
aliguorie3aff4f2009-04-05 19:14:04 +0000218" -h, --help display this help and exit\n"
219" -V, --version output version information and exit\n"
Maria Kustovad208cc32014-03-18 09:59:19 +0400220"\n"
221"See '%s -c help' for information on available commands."
aliguorie3aff4f2009-04-05 19:14:04 +0000222"\n",
Maria Kustovad208cc32014-03-18 09:59:19 +0400223 name, name);
aliguorie3aff4f2009-04-05 19:14:04 +0000224}
225
Kevin Wolfd1174f12013-06-05 14:19:37 +0200226static char *get_prompt(void)
227{
228 static char prompt[FILENAME_MAX + 2 /*"> "*/ + 1 /*"\0"*/ ];
229
230 if (!prompt[0]) {
231 snprintf(prompt, sizeof(prompt), "%s> ", progname);
232 }
233
234 return prompt;
235}
236
Stefan Weild5d15072014-01-25 18:18:23 +0100237static void GCC_FMT_ATTR(2, 3) readline_printf_func(void *opaque,
238 const char *fmt, ...)
Kevin Wolfd1174f12013-06-05 14:19:37 +0200239{
Stefan Hajnoczi0cf17e12013-11-14 11:54:17 +0100240 va_list ap;
241 va_start(ap, fmt);
242 vprintf(fmt, ap);
243 va_end(ap);
244}
245
246static void readline_flush_func(void *opaque)
247{
248 fflush(stdout);
249}
250
251static void readline_func(void *opaque, const char *str, void *readline_opaque)
252{
253 char **line = readline_opaque;
254 *line = g_strdup(str);
255}
256
Stefan Hajnoczi46940202013-11-14 11:54:18 +0100257static void completion_match(const char *cmd, void *opaque)
258{
259 readline_add_completion(readline_state, cmd);
260}
261
Stefan Hajnoczi0cf17e12013-11-14 11:54:17 +0100262static void readline_completion_func(void *opaque, const char *str)
263{
Stefan Hajnoczi46940202013-11-14 11:54:18 +0100264 readline_set_completion_index(readline_state, strlen(str));
265 qemuio_complete_command(str, completion_match, NULL);
Stefan Hajnoczi0cf17e12013-11-14 11:54:17 +0100266}
267
268static char *fetchline_readline(void)
269{
270 char *line = NULL;
271
272 readline_start(readline_state, get_prompt(), 0, readline_func, &line);
273 while (!line) {
274 int ch = getchar();
275 if (ch == EOF) {
276 break;
277 }
278 readline_handle_byte(readline_state, ch);
Kevin Wolfd1174f12013-06-05 14:19:37 +0200279 }
280 return line;
281}
Kevin Wolfd1174f12013-06-05 14:19:37 +0200282
Stefan Hajnoczi0cf17e12013-11-14 11:54:17 +0100283#define MAXREADLINESZ 1024
284static char *fetchline_fgets(void)
Kevin Wolfd1174f12013-06-05 14:19:37 +0200285{
286 char *p, *line = g_malloc(MAXREADLINESZ);
287
288 if (!fgets(line, MAXREADLINESZ, stdin)) {
289 g_free(line);
290 return NULL;
291 }
292
293 p = line + strlen(line);
294 if (p != line && p[-1] == '\n') {
295 p[-1] = '\0';
296 }
297
298 return line;
299}
Stefan Hajnoczi0cf17e12013-11-14 11:54:17 +0100300
301static char *fetchline(void)
302{
303 if (readline_state) {
304 return fetchline_readline();
305 } else {
306 return fetchline_fgets();
307 }
308}
Kevin Wolfd1174f12013-06-05 14:19:37 +0200309
310static void prep_fetchline(void *opaque)
311{
312 int *fetchable = opaque;
313
314 qemu_set_fd_handler(STDIN_FILENO, NULL, NULL, NULL);
315 *fetchable= 1;
316}
317
318static void command_loop(void)
319{
320 int i, done = 0, fetchable = 0, prompted = 0;
321 char *input;
322
323 for (i = 0; !done && i < ncmdline; i++) {
Max Reitz4c7b7e92015-02-05 13:58:22 -0500324 done = qemuio_command(qemuio_blk, cmdline[i]);
Kevin Wolfd1174f12013-06-05 14:19:37 +0200325 }
326 if (cmdline) {
327 g_free(cmdline);
328 return;
329 }
330
331 while (!done) {
332 if (!prompted) {
333 printf("%s", get_prompt());
334 fflush(stdout);
335 qemu_set_fd_handler(STDIN_FILENO, prep_fetchline, NULL, &fetchable);
336 prompted = 1;
337 }
338
339 main_loop_wait(false);
340
341 if (!fetchable) {
342 continue;
343 }
344
345 input = fetchline();
346 if (input == NULL) {
347 break;
348 }
Max Reitz4c7b7e92015-02-05 13:58:22 -0500349 done = qemuio_command(qemuio_blk, input);
Kevin Wolfd1174f12013-06-05 14:19:37 +0200350 g_free(input);
351
352 prompted = 0;
353 fetchable = 0;
354 }
355 qemu_set_fd_handler(STDIN_FILENO, NULL, NULL, NULL);
356}
357
358static void add_user_command(char *optarg)
359{
Markus Armbruster5839e532014-08-19 10:31:08 +0200360 cmdline = g_renew(char *, cmdline, ++ncmdline);
Kevin Wolfd1174f12013-06-05 14:19:37 +0200361 cmdline[ncmdline-1] = optarg;
362}
363
Stefan Hajnoczi0cf17e12013-11-14 11:54:17 +0100364static void reenable_tty_echo(void)
365{
366 qemu_set_tty_echo(STDIN_FILENO, true);
367}
368
aliguorie3aff4f2009-04-05 19:14:04 +0000369int main(int argc, char **argv)
370{
Devin Nakamura43642b32011-07-11 11:22:16 -0400371 int readonly = 0;
Kevin Wolfbe6273d2014-11-20 16:27:06 +0100372 const char *sopt = "hVc:d:f:rsnmgkt:T:";
Devin Nakamura43642b32011-07-11 11:22:16 -0400373 const struct option lopt[] = {
374 { "help", 0, NULL, 'h' },
375 { "version", 0, NULL, 'V' },
376 { "offset", 1, NULL, 'o' },
377 { "cmd", 1, NULL, 'c' },
Kevin Wolfbe6273d2014-11-20 16:27:06 +0100378 { "format", 1, NULL, 'f' },
Devin Nakamura43642b32011-07-11 11:22:16 -0400379 { "read-only", 0, NULL, 'r' },
380 { "snapshot", 0, NULL, 's' },
381 { "nocache", 0, NULL, 'n' },
382 { "misalign", 0, NULL, 'm' },
Devin Nakamura43642b32011-07-11 11:22:16 -0400383 { "native-aio", 0, NULL, 'k' },
Paolo Bonzini9e8f1832013-02-08 14:06:11 +0100384 { "discard", 1, NULL, 'd' },
Kevin Wolf592fa072012-04-18 12:07:39 +0200385 { "cache", 1, NULL, 't' },
Stefan Hajnoczid7bb72c2012-03-12 16:36:07 +0000386 { "trace", 1, NULL, 'T' },
Devin Nakamura43642b32011-07-11 11:22:16 -0400387 { NULL, 0, NULL, 0 }
388 };
389 int c;
390 int opt_index = 0;
Paolo Bonzini9e8f1832013-02-08 14:06:11 +0100391 int flags = BDRV_O_UNMAP;
Chrysostomos Nanakos2f78e492014-09-18 14:30:49 +0300392 Error *local_error = NULL;
Max Reitz1b58b432015-02-05 13:58:20 -0500393 QDict *opts = NULL;
aliguorie3aff4f2009-04-05 19:14:04 +0000394
MORITA Kazutaka526eda12013-07-23 17:30:11 +0900395#ifdef CONFIG_POSIX
396 signal(SIGPIPE, SIG_IGN);
397#endif
398
Devin Nakamura43642b32011-07-11 11:22:16 -0400399 progname = basename(argv[0]);
Fam Zheng10f5bff2014-02-10 14:48:51 +0800400 qemu_init_exec_dir(argv[0]);
aliguorie3aff4f2009-04-05 19:14:04 +0000401
Kevin Wolfbe6273d2014-11-20 16:27:06 +0100402 bdrv_init();
403
Devin Nakamura43642b32011-07-11 11:22:16 -0400404 while ((c = getopt_long(argc, argv, sopt, lopt, &opt_index)) != -1) {
405 switch (c) {
406 case 's':
407 flags |= BDRV_O_SNAPSHOT;
408 break;
409 case 'n':
410 flags |= BDRV_O_NOCACHE | BDRV_O_CACHE_WB;
411 break;
Paolo Bonzini9e8f1832013-02-08 14:06:11 +0100412 case 'd':
413 if (bdrv_parse_discard_flags(optarg, &flags) < 0) {
414 error_report("Invalid discard option: %s", optarg);
415 exit(1);
416 }
417 break;
Kevin Wolfbe6273d2014-11-20 16:27:06 +0100418 case 'f':
Max Reitz1b58b432015-02-05 13:58:20 -0500419 if (!opts) {
420 opts = qdict_new();
Kevin Wolfbe6273d2014-11-20 16:27:06 +0100421 }
Max Reitz1b58b432015-02-05 13:58:20 -0500422 qdict_put(opts, "driver", qstring_from_str(optarg));
Kevin Wolfbe6273d2014-11-20 16:27:06 +0100423 break;
Devin Nakamura43642b32011-07-11 11:22:16 -0400424 case 'c':
425 add_user_command(optarg);
426 break;
427 case 'r':
428 readonly = 1;
429 break;
430 case 'm':
Stefan Weilf9883882014-03-05 22:23:00 +0100431 qemuio_misalign = true;
Devin Nakamura43642b32011-07-11 11:22:16 -0400432 break;
Devin Nakamura43642b32011-07-11 11:22:16 -0400433 case 'k':
434 flags |= BDRV_O_NATIVE_AIO;
435 break;
Kevin Wolf592fa072012-04-18 12:07:39 +0200436 case 't':
437 if (bdrv_parse_cache_flags(optarg, &flags) < 0) {
438 error_report("Invalid cache option: %s", optarg);
439 exit(1);
440 }
441 break;
Stefan Hajnoczid7bb72c2012-03-12 16:36:07 +0000442 case 'T':
LluĂ­s Vilanova5b808272014-05-27 15:02:14 +0200443 if (!trace_init_backends(optarg, NULL)) {
Stefan Hajnoczid7bb72c2012-03-12 16:36:07 +0000444 exit(1); /* error message will have been printed */
445 }
446 break;
Devin Nakamura43642b32011-07-11 11:22:16 -0400447 case 'V':
Kevin Wolf02da3862013-06-05 14:19:40 +0200448 printf("%s version %s\n", progname, QEMU_VERSION);
Devin Nakamura43642b32011-07-11 11:22:16 -0400449 exit(0);
450 case 'h':
451 usage(progname);
452 exit(0);
453 default:
454 usage(progname);
455 exit(1);
Naphtali Spreif5edb012010-01-17 16:48:13 +0200456 }
Devin Nakamura43642b32011-07-11 11:22:16 -0400457 }
aliguorie3aff4f2009-04-05 19:14:04 +0000458
Devin Nakamura43642b32011-07-11 11:22:16 -0400459 if ((argc - optind) > 1) {
460 usage(progname);
461 exit(1);
462 }
aliguorie3aff4f2009-04-05 19:14:04 +0000463
Chrysostomos Nanakos2f78e492014-09-18 14:30:49 +0300464 if (qemu_init_main_loop(&local_error)) {
Markus Armbruster565f65d2015-02-12 13:55:05 +0100465 error_report_err(local_error);
Chrysostomos Nanakos2f78e492014-09-18 14:30:49 +0300466 exit(1);
467 }
Zhi Yong Wua57d1142012-02-19 22:24:59 +0800468
Devin Nakamura43642b32011-07-11 11:22:16 -0400469 /* initialize commands */
Kevin Wolfc2cdf5c2013-06-05 14:19:36 +0200470 qemuio_add_command(&quit_cmd);
471 qemuio_add_command(&open_cmd);
472 qemuio_add_command(&close_cmd);
Devin Nakamura43642b32011-07-11 11:22:16 -0400473
Stefan Hajnoczi0cf17e12013-11-14 11:54:17 +0100474 if (isatty(STDIN_FILENO)) {
475 readline_state = readline_init(readline_printf_func,
476 readline_flush_func,
477 NULL,
478 readline_completion_func);
479 qemu_set_tty_echo(STDIN_FILENO, false);
480 atexit(reenable_tty_echo);
481 }
482
Devin Nakamura43642b32011-07-11 11:22:16 -0400483 /* open the device */
484 if (!readonly) {
485 flags |= BDRV_O_RDWR;
486 }
487
488 if ((argc - optind) == 1) {
Max Reitz10d9d752015-02-05 13:58:21 -0500489 openfile(argv[optind], flags, opts);
Devin Nakamura43642b32011-07-11 11:22:16 -0400490 }
491 command_loop();
492
493 /*
Stefan Hajnoczi922453b2011-11-30 12:23:43 +0000494 * Make sure all outstanding requests complete before the program exits.
Devin Nakamura43642b32011-07-11 11:22:16 -0400495 */
Stefan Hajnoczi922453b2011-11-30 12:23:43 +0000496 bdrv_drain_all();
Devin Nakamura43642b32011-07-11 11:22:16 -0400497
Markus Armbruster26f54e92014-10-07 13:59:04 +0200498 blk_unref(qemuio_blk);
Stefan Hajnoczi0cf17e12013-11-14 11:54:17 +0100499 g_free(readline_state);
Devin Nakamura43642b32011-07-11 11:22:16 -0400500 return 0;
aliguorie3aff4f2009-04-05 19:14:04 +0000501}