blob: 83c48f4149ddd686c2583f6dc8de70d892f42512 [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 */
Peter Maydell80c71a22016-01-18 18:01:42 +000010#include "qemu/osdep.h"
aliguorie3aff4f2009-04-05 19:14:04 +000011#include <getopt.h>
Stefan Weilc32d7662009-08-31 22:16:16 +020012#include <libgen.h>
aliguorie3aff4f2009-04-05 19:14:04 +000013
Kevin Wolf3d219942013-06-05 14:19:39 +020014#include "qemu-io.h"
Markus Armbrusterd49b6832015-03-17 18:29:20 +010015#include "qemu/error-report.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010016#include "qemu/main-loop.h"
Max Reitzb543c5c2013-10-11 14:02:10 +020017#include "qemu/option.h"
18#include "qemu/config-file.h"
Stefan Hajnoczi0cf17e12013-11-14 11:54:17 +010019#include "qemu/readline.h"
Markus Armbrusterd49b6832015-03-17 18:29:20 +010020#include "qapi/qmp/qstring.h"
Markus Armbruster26f54e92014-10-07 13:59:04 +020021#include "sysemu/block-backend.h"
Paolo Bonzini737e1502012-12-17 18:19:44 +010022#include "block/block_int.h"
Stefan Hajnoczid7bb72c2012-03-12 16:36:07 +000023#include "trace/control.h"
aliguorie3aff4f2009-04-05 19:14:04 +000024
Devin Nakamura43642b32011-07-11 11:22:16 -040025#define CMD_NOFILE_OK 0x01
aliguorie3aff4f2009-04-05 19:14:04 +000026
Stefan Weilf9883882014-03-05 22:23:00 +010027static char *progname;
aliguorie3aff4f2009-04-05 19:14:04 +000028
Markus Armbruster26f54e92014-10-07 13:59:04 +020029static BlockBackend *qemuio_blk;
Kevin Wolf191c2892010-09-16 13:18:08 +020030
Kevin Wolfd1174f12013-06-05 14:19:37 +020031/* qemu-io commands passed using -c */
32static int ncmdline;
33static char **cmdline;
34
Stefan Hajnoczi0cf17e12013-11-14 11:54:17 +010035static ReadLineState *readline_state;
36
Max Reitz4c7b7e92015-02-05 13:58:22 -050037static int close_f(BlockBackend *blk, int argc, char **argv)
aliguorie3aff4f2009-04-05 19:14:04 +000038{
Markus Armbruster26f54e92014-10-07 13:59:04 +020039 blk_unref(qemuio_blk);
Markus Armbruster26f54e92014-10-07 13:59:04 +020040 qemuio_blk = NULL;
Devin Nakamura43642b32011-07-11 11:22:16 -040041 return 0;
aliguorie3aff4f2009-04-05 19:14:04 +000042}
43
44static const cmdinfo_t close_cmd = {
Devin Nakamura43642b32011-07-11 11:22:16 -040045 .name = "close",
46 .altname = "c",
47 .cfunc = close_f,
48 .oneline = "close the current open file",
aliguorie3aff4f2009-04-05 19:14:04 +000049};
50
Max Reitz10d9d752015-02-05 13:58:21 -050051static int openfile(char *name, int flags, QDict *opts)
aliguorie3aff4f2009-04-05 19:14:04 +000052{
Max Reitz34b5d2c2013-09-05 14:45:29 +020053 Error *local_err = NULL;
Daniel P. Berrange8caf0212015-05-12 17:09:21 +010054 BlockDriverState *bs;
Max Reitz34b5d2c2013-09-05 14:45:29 +020055
Max Reitz1b58b432015-02-05 13:58:20 -050056 if (qemuio_blk) {
Markus Armbrusterb9884682015-12-18 16:35:18 +010057 error_report("file open already, try 'help close'");
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 Armbrusterb9884682015-12-18 16:35:18 +010064 error_reportf_err(local_err, "can't open%s%s: ",
65 name ? " device " : "", name ?: "");
Markus Armbrusterdbb651c2014-09-08 18:50:58 +020066 return 1;
Devin Nakamura43642b32011-07-11 11:22:16 -040067 }
Christoph Hellwig1db69472009-07-15 23:11:21 +020068
Daniel P. Berrange8caf0212015-05-12 17:09:21 +010069 bs = blk_bs(qemuio_blk);
70 if (bdrv_is_encrypted(bs)) {
71 char password[256];
72 printf("Disk image '%s' is encrypted.\n", name);
73 if (qemu_read_password(password, sizeof(password)) < 0) {
74 error_report("No password given");
75 goto error;
76 }
77 if (bdrv_set_key(bs, password) < 0) {
78 error_report("invalid password");
79 goto error;
80 }
81 }
82
83
Devin Nakamura43642b32011-07-11 11:22:16 -040084 return 0;
Daniel P. Berrange8caf0212015-05-12 17:09:21 +010085
86 error:
87 blk_unref(qemuio_blk);
88 qemuio_blk = NULL;
89 return 1;
aliguorie3aff4f2009-04-05 19:14:04 +000090}
91
Devin Nakamura43642b32011-07-11 11:22:16 -040092static void open_help(void)
aliguorie3aff4f2009-04-05 19:14:04 +000093{
Devin Nakamura43642b32011-07-11 11:22:16 -040094 printf(
aliguorie3aff4f2009-04-05 19:14:04 +000095"\n"
96" opens a new file in the requested mode\n"
97"\n"
98" Example:\n"
99" 'open -Cn /tmp/data' - creates/opens data file read-write and uncached\n"
100"\n"
101" Opens a file for subsequent use by all of the other qemu-io commands.\n"
aliguorie3aff4f2009-04-05 19:14:04 +0000102" -r, -- open file read-only\n"
103" -s, -- use snapshot file\n"
104" -n, -- disable host cache\n"
Max Reitzb543c5c2013-10-11 14:02:10 +0200105" -o, -- options to be given to the block driver"
aliguorie3aff4f2009-04-05 19:14:04 +0000106"\n");
107}
108
Max Reitz4c7b7e92015-02-05 13:58:22 -0500109static int open_f(BlockBackend *blk, int argc, char **argv);
Blue Swirl22a2bdc2009-11-21 09:06:46 +0000110
111static const cmdinfo_t open_cmd = {
Devin Nakamura43642b32011-07-11 11:22:16 -0400112 .name = "open",
113 .altname = "o",
114 .cfunc = open_f,
115 .argmin = 1,
116 .argmax = -1,
117 .flags = CMD_NOFILE_OK,
Max Reitzb543c5c2013-10-11 14:02:10 +0200118 .args = "[-Crsn] [-o options] [path]",
Devin Nakamura43642b32011-07-11 11:22:16 -0400119 .oneline = "open the file specified by path",
120 .help = open_help,
Blue Swirl22a2bdc2009-11-21 09:06:46 +0000121};
aliguorie3aff4f2009-04-05 19:14:04 +0000122
Max Reitzb543c5c2013-10-11 14:02:10 +0200123static QemuOptsList empty_opts = {
124 .name = "drive",
Markus Armbruster443422f2014-05-28 11:16:58 +0200125 .merge_lists = true,
Max Reitzb543c5c2013-10-11 14:02:10 +0200126 .head = QTAILQ_HEAD_INITIALIZER(empty_opts.head),
127 .desc = {
128 /* no elements => accept any params */
129 { /* end of list */ }
130 },
131};
132
Max Reitz4c7b7e92015-02-05 13:58:22 -0500133static int open_f(BlockBackend *blk, int argc, char **argv)
aliguorie3aff4f2009-04-05 19:14:04 +0000134{
Devin Nakamura43642b32011-07-11 11:22:16 -0400135 int flags = 0;
136 int readonly = 0;
Devin Nakamura43642b32011-07-11 11:22:16 -0400137 int c;
Max Reitzb543c5c2013-10-11 14:02:10 +0200138 QemuOpts *qopts;
Markus Armbruster443422f2014-05-28 11:16:58 +0200139 QDict *opts;
aliguorie3aff4f2009-04-05 19:14:04 +0000140
Eric Blakeb062ad82015-05-12 09:10:56 -0600141 while ((c = getopt(argc, argv, "snrgo:")) != -1) {
Devin Nakamura43642b32011-07-11 11:22:16 -0400142 switch (c) {
143 case 's':
144 flags |= BDRV_O_SNAPSHOT;
145 break;
146 case 'n':
147 flags |= BDRV_O_NOCACHE | BDRV_O_CACHE_WB;
148 break;
149 case 'r':
150 readonly = 1;
151 break;
Max Reitzb543c5c2013-10-11 14:02:10 +0200152 case 'o':
Markus Armbruster70b94332015-02-13 12:50:26 +0100153 if (!qemu_opts_parse_noisily(&empty_opts, optarg, false)) {
Markus Armbruster443422f2014-05-28 11:16:58 +0200154 qemu_opts_reset(&empty_opts);
Max Reitzb543c5c2013-10-11 14:02:10 +0200155 return 0;
156 }
Max Reitzb543c5c2013-10-11 14:02:10 +0200157 break;
Devin Nakamura43642b32011-07-11 11:22:16 -0400158 default:
Markus Armbruster443422f2014-05-28 11:16:58 +0200159 qemu_opts_reset(&empty_opts);
Kevin Wolfc2cdf5c2013-06-05 14:19:36 +0200160 return qemuio_command_usage(&open_cmd);
Naphtali Spreif5edb012010-01-17 16:48:13 +0200161 }
Devin Nakamura43642b32011-07-11 11:22:16 -0400162 }
aliguorie3aff4f2009-04-05 19:14:04 +0000163
Devin Nakamura43642b32011-07-11 11:22:16 -0400164 if (!readonly) {
165 flags |= BDRV_O_RDWR;
166 }
aliguorie3aff4f2009-04-05 19:14:04 +0000167
Markus Armbruster443422f2014-05-28 11:16:58 +0200168 qopts = qemu_opts_find(&empty_opts, NULL);
169 opts = qopts ? qemu_opts_to_qdict(qopts, NULL) : NULL;
170 qemu_opts_reset(&empty_opts);
171
Max Reitzfd0fee32013-12-20 19:28:20 +0100172 if (optind == argc - 1) {
Max Reitz10d9d752015-02-05 13:58:21 -0500173 return openfile(argv[optind], flags, opts);
Max Reitzfd0fee32013-12-20 19:28:20 +0100174 } else if (optind == argc) {
Max Reitz10d9d752015-02-05 13:58:21 -0500175 return openfile(NULL, flags, opts);
Max Reitzfd0fee32013-12-20 19:28:20 +0100176 } else {
Markus Armbruster29f26012014-05-28 11:16:59 +0200177 QDECREF(opts);
Kevin Wolfc2cdf5c2013-06-05 14:19:36 +0200178 return qemuio_command_usage(&open_cmd);
Devin Nakamura43642b32011-07-11 11:22:16 -0400179 }
aliguorie3aff4f2009-04-05 19:14:04 +0000180}
181
Max Reitz4c7b7e92015-02-05 13:58:22 -0500182static int quit_f(BlockBackend *blk, int argc, char **argv)
Kevin Wolfe681be72013-06-05 14:19:34 +0200183{
184 return 1;
185}
186
187static const cmdinfo_t quit_cmd = {
188 .name = "quit",
189 .altname = "q",
190 .cfunc = quit_f,
191 .argmin = -1,
192 .argmax = -1,
193 .flags = CMD_FLAG_GLOBAL,
194 .oneline = "exit the program",
195};
196
aliguorie3aff4f2009-04-05 19:14:04 +0000197static void usage(const char *name)
198{
Devin Nakamura43642b32011-07-11 11:22:16 -0400199 printf(
Kevin Wolfbe6273d2014-11-20 16:27:06 +0100200"Usage: %s [-h] [-V] [-rsnm] [-f FMT] [-c STRING] ... [file]\n"
Stefan Weil84844a22009-06-22 15:08:47 +0200201"QEMU Disk exerciser\n"
aliguorie3aff4f2009-04-05 19:14:04 +0000202"\n"
Maria Kustovad208cc32014-03-18 09:59:19 +0400203" -c, --cmd STRING execute command with its arguments\n"
204" from the given string\n"
Kevin Wolfbe6273d2014-11-20 16:27:06 +0100205" -f, --format FMT specifies the block driver to use\n"
aliguorie3aff4f2009-04-05 19:14:04 +0000206" -r, --read-only export read-only\n"
207" -s, --snapshot use snapshot file\n"
208" -n, --nocache disable host cache\n"
209" -m, --misalign misalign allocations for O_DIRECT\n"
Christoph Hellwig5c6c3a62009-08-20 16:58:35 +0200210" -k, --native-aio use kernel AIO implementation (on Linux only)\n"
Kevin Wolf592fa072012-04-18 12:07:39 +0200211" -t, --cache=MODE use the given cache mode for the image\n"
Stefan Hajnoczid7bb72c2012-03-12 16:36:07 +0000212" -T, --trace FILE enable trace events listed in the given file\n"
aliguorie3aff4f2009-04-05 19:14:04 +0000213" -h, --help display this help and exit\n"
214" -V, --version output version information and exit\n"
Maria Kustovad208cc32014-03-18 09:59:19 +0400215"\n"
216"See '%s -c help' for information on available commands."
aliguorie3aff4f2009-04-05 19:14:04 +0000217"\n",
Maria Kustovad208cc32014-03-18 09:59:19 +0400218 name, name);
aliguorie3aff4f2009-04-05 19:14:04 +0000219}
220
Kevin Wolfd1174f12013-06-05 14:19:37 +0200221static char *get_prompt(void)
222{
223 static char prompt[FILENAME_MAX + 2 /*"> "*/ + 1 /*"\0"*/ ];
224
225 if (!prompt[0]) {
226 snprintf(prompt, sizeof(prompt), "%s> ", progname);
227 }
228
229 return prompt;
230}
231
Stefan Weild5d15072014-01-25 18:18:23 +0100232static void GCC_FMT_ATTR(2, 3) readline_printf_func(void *opaque,
233 const char *fmt, ...)
Kevin Wolfd1174f12013-06-05 14:19:37 +0200234{
Stefan Hajnoczi0cf17e12013-11-14 11:54:17 +0100235 va_list ap;
236 va_start(ap, fmt);
237 vprintf(fmt, ap);
238 va_end(ap);
239}
240
241static void readline_flush_func(void *opaque)
242{
243 fflush(stdout);
244}
245
246static void readline_func(void *opaque, const char *str, void *readline_opaque)
247{
248 char **line = readline_opaque;
249 *line = g_strdup(str);
250}
251
Stefan Hajnoczi46940202013-11-14 11:54:18 +0100252static void completion_match(const char *cmd, void *opaque)
253{
254 readline_add_completion(readline_state, cmd);
255}
256
Stefan Hajnoczi0cf17e12013-11-14 11:54:17 +0100257static void readline_completion_func(void *opaque, const char *str)
258{
Stefan Hajnoczi46940202013-11-14 11:54:18 +0100259 readline_set_completion_index(readline_state, strlen(str));
260 qemuio_complete_command(str, completion_match, NULL);
Stefan Hajnoczi0cf17e12013-11-14 11:54:17 +0100261}
262
263static char *fetchline_readline(void)
264{
265 char *line = NULL;
266
267 readline_start(readline_state, get_prompt(), 0, readline_func, &line);
268 while (!line) {
269 int ch = getchar();
270 if (ch == EOF) {
271 break;
272 }
273 readline_handle_byte(readline_state, ch);
Kevin Wolfd1174f12013-06-05 14:19:37 +0200274 }
275 return line;
276}
Kevin Wolfd1174f12013-06-05 14:19:37 +0200277
Stefan Hajnoczi0cf17e12013-11-14 11:54:17 +0100278#define MAXREADLINESZ 1024
279static char *fetchline_fgets(void)
Kevin Wolfd1174f12013-06-05 14:19:37 +0200280{
281 char *p, *line = g_malloc(MAXREADLINESZ);
282
283 if (!fgets(line, MAXREADLINESZ, stdin)) {
284 g_free(line);
285 return NULL;
286 }
287
288 p = line + strlen(line);
289 if (p != line && p[-1] == '\n') {
290 p[-1] = '\0';
291 }
292
293 return line;
294}
Stefan Hajnoczi0cf17e12013-11-14 11:54:17 +0100295
296static char *fetchline(void)
297{
298 if (readline_state) {
299 return fetchline_readline();
300 } else {
301 return fetchline_fgets();
302 }
303}
Kevin Wolfd1174f12013-06-05 14:19:37 +0200304
305static void prep_fetchline(void *opaque)
306{
307 int *fetchable = opaque;
308
309 qemu_set_fd_handler(STDIN_FILENO, NULL, NULL, NULL);
310 *fetchable= 1;
311}
312
313static void command_loop(void)
314{
315 int i, done = 0, fetchable = 0, prompted = 0;
316 char *input;
317
318 for (i = 0; !done && i < ncmdline; i++) {
Max Reitz4c7b7e92015-02-05 13:58:22 -0500319 done = qemuio_command(qemuio_blk, cmdline[i]);
Kevin Wolfd1174f12013-06-05 14:19:37 +0200320 }
321 if (cmdline) {
322 g_free(cmdline);
323 return;
324 }
325
326 while (!done) {
327 if (!prompted) {
328 printf("%s", get_prompt());
329 fflush(stdout);
330 qemu_set_fd_handler(STDIN_FILENO, prep_fetchline, NULL, &fetchable);
331 prompted = 1;
332 }
333
334 main_loop_wait(false);
335
336 if (!fetchable) {
337 continue;
338 }
339
340 input = fetchline();
341 if (input == NULL) {
342 break;
343 }
Max Reitz4c7b7e92015-02-05 13:58:22 -0500344 done = qemuio_command(qemuio_blk, input);
Kevin Wolfd1174f12013-06-05 14:19:37 +0200345 g_free(input);
346
347 prompted = 0;
348 fetchable = 0;
349 }
350 qemu_set_fd_handler(STDIN_FILENO, NULL, NULL, NULL);
351}
352
353static void add_user_command(char *optarg)
354{
Markus Armbruster5839e532014-08-19 10:31:08 +0200355 cmdline = g_renew(char *, cmdline, ++ncmdline);
Kevin Wolfd1174f12013-06-05 14:19:37 +0200356 cmdline[ncmdline-1] = optarg;
357}
358
Stefan Hajnoczi0cf17e12013-11-14 11:54:17 +0100359static void reenable_tty_echo(void)
360{
361 qemu_set_tty_echo(STDIN_FILENO, true);
362}
363
aliguorie3aff4f2009-04-05 19:14:04 +0000364int main(int argc, char **argv)
365{
Devin Nakamura43642b32011-07-11 11:22:16 -0400366 int readonly = 0;
Kevin Wolfbe6273d2014-11-20 16:27:06 +0100367 const char *sopt = "hVc:d:f: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' },
Kevin Wolfbe6273d2014-11-20 16:27:06 +0100373 { "format", 1, NULL, 'f' },
Devin Nakamura43642b32011-07-11 11:22:16 -0400374 { "read-only", 0, NULL, 'r' },
375 { "snapshot", 0, NULL, 's' },
376 { "nocache", 0, NULL, 'n' },
377 { "misalign", 0, NULL, 'm' },
Devin Nakamura43642b32011-07-11 11:22:16 -0400378 { "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;
Max Reitz1b58b432015-02-05 13:58:20 -0500388 QDict *opts = NULL;
aliguorie3aff4f2009-04-05 19:14:04 +0000389
MORITA Kazutaka526eda12013-07-23 17:30:11 +0900390#ifdef CONFIG_POSIX
391 signal(SIGPIPE, SIG_IGN);
392#endif
393
Devin Nakamura43642b32011-07-11 11:22:16 -0400394 progname = basename(argv[0]);
Fam Zheng10f5bff2014-02-10 14:48:51 +0800395 qemu_init_exec_dir(argv[0]);
aliguorie3aff4f2009-04-05 19:14:04 +0000396
Kevin Wolfbe6273d2014-11-20 16:27:06 +0100397 bdrv_init();
398
Devin Nakamura43642b32011-07-11 11:22:16 -0400399 while ((c = getopt_long(argc, argv, sopt, lopt, &opt_index)) != -1) {
400 switch (c) {
401 case 's':
402 flags |= BDRV_O_SNAPSHOT;
403 break;
404 case 'n':
405 flags |= BDRV_O_NOCACHE | BDRV_O_CACHE_WB;
406 break;
Paolo Bonzini9e8f1832013-02-08 14:06:11 +0100407 case 'd':
408 if (bdrv_parse_discard_flags(optarg, &flags) < 0) {
409 error_report("Invalid discard option: %s", optarg);
410 exit(1);
411 }
412 break;
Kevin Wolfbe6273d2014-11-20 16:27:06 +0100413 case 'f':
Max Reitz1b58b432015-02-05 13:58:20 -0500414 if (!opts) {
415 opts = qdict_new();
Kevin Wolfbe6273d2014-11-20 16:27:06 +0100416 }
Max Reitz1b58b432015-02-05 13:58:20 -0500417 qdict_put(opts, "driver", qstring_from_str(optarg));
Kevin Wolfbe6273d2014-11-20 16:27:06 +0100418 break;
Devin Nakamura43642b32011-07-11 11:22:16 -0400419 case 'c':
420 add_user_command(optarg);
421 break;
422 case 'r':
423 readonly = 1;
424 break;
425 case 'm':
Stefan Weilf9883882014-03-05 22:23:00 +0100426 qemuio_misalign = true;
Devin Nakamura43642b32011-07-11 11:22:16 -0400427 break;
Devin Nakamura43642b32011-07-11 11:22:16 -0400428 case 'k':
429 flags |= BDRV_O_NATIVE_AIO;
430 break;
Kevin Wolf592fa072012-04-18 12:07:39 +0200431 case 't':
432 if (bdrv_parse_cache_flags(optarg, &flags) < 0) {
433 error_report("Invalid cache option: %s", optarg);
434 exit(1);
435 }
436 break;
Stefan Hajnoczid7bb72c2012-03-12 16:36:07 +0000437 case 'T':
Paolo Bonzini41fc57e2016-01-07 16:55:24 +0300438 if (!trace_init_backends()) {
Stefan Hajnoczid7bb72c2012-03-12 16:36:07 +0000439 exit(1); /* error message will have been printed */
440 }
441 break;
Devin Nakamura43642b32011-07-11 11:22:16 -0400442 case 'V':
Kevin Wolf02da3862013-06-05 14:19:40 +0200443 printf("%s version %s\n", progname, QEMU_VERSION);
Devin Nakamura43642b32011-07-11 11:22:16 -0400444 exit(0);
445 case 'h':
446 usage(progname);
447 exit(0);
448 default:
449 usage(progname);
450 exit(1);
Naphtali Spreif5edb012010-01-17 16:48:13 +0200451 }
Devin Nakamura43642b32011-07-11 11:22:16 -0400452 }
aliguorie3aff4f2009-04-05 19:14:04 +0000453
Devin Nakamura43642b32011-07-11 11:22:16 -0400454 if ((argc - optind) > 1) {
455 usage(progname);
456 exit(1);
457 }
aliguorie3aff4f2009-04-05 19:14:04 +0000458
Chrysostomos Nanakos2f78e492014-09-18 14:30:49 +0300459 if (qemu_init_main_loop(&local_error)) {
Markus Armbruster565f65d2015-02-12 13:55:05 +0100460 error_report_err(local_error);
Chrysostomos Nanakos2f78e492014-09-18 14:30:49 +0300461 exit(1);
462 }
Zhi Yong Wua57d1142012-02-19 22:24:59 +0800463
Devin Nakamura43642b32011-07-11 11:22:16 -0400464 /* initialize commands */
Kevin Wolfc2cdf5c2013-06-05 14:19:36 +0200465 qemuio_add_command(&quit_cmd);
466 qemuio_add_command(&open_cmd);
467 qemuio_add_command(&close_cmd);
Devin Nakamura43642b32011-07-11 11:22:16 -0400468
Stefan Hajnoczi0cf17e12013-11-14 11:54:17 +0100469 if (isatty(STDIN_FILENO)) {
470 readline_state = readline_init(readline_printf_func,
471 readline_flush_func,
472 NULL,
473 readline_completion_func);
474 qemu_set_tty_echo(STDIN_FILENO, false);
475 atexit(reenable_tty_echo);
476 }
477
Devin Nakamura43642b32011-07-11 11:22:16 -0400478 /* open the device */
479 if (!readonly) {
480 flags |= BDRV_O_RDWR;
481 }
482
483 if ((argc - optind) == 1) {
Max Reitz10d9d752015-02-05 13:58:21 -0500484 openfile(argv[optind], flags, opts);
Devin Nakamura43642b32011-07-11 11:22:16 -0400485 }
486 command_loop();
487
488 /*
Stefan Hajnoczi922453b2011-11-30 12:23:43 +0000489 * Make sure all outstanding requests complete before the program exits.
Devin Nakamura43642b32011-07-11 11:22:16 -0400490 */
Stefan Hajnoczi922453b2011-11-30 12:23:43 +0000491 bdrv_drain_all();
Devin Nakamura43642b32011-07-11 11:22:16 -0400492
Markus Armbruster26f54e92014-10-07 13:59:04 +0200493 blk_unref(qemuio_blk);
Stefan Hajnoczi0cf17e12013-11-14 11:54:17 +0100494 g_free(readline_state);
Devin Nakamura43642b32011-07-11 11:22:16 -0400495 return 0;
aliguorie3aff4f2009-04-05 19:14:04 +0000496}