blob: c24f6be24e3af7b6fb8a6d28baac31b1583905d4 [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;
Daniel P. Berrange8caf0212015-05-12 17:09:21 +010055 BlockDriverState *bs;
Max Reitz34b5d2c2013-09-05 14:45:29 +020056
Max Reitz1b58b432015-02-05 13:58:20 -050057 if (qemuio_blk) {
Devin Nakamura43642b32011-07-11 11:22:16 -040058 fprintf(stderr, "file open already, try 'help close'\n");
Markus Armbruster29f26012014-05-28 11:16:59 +020059 QDECREF(opts);
Devin Nakamura43642b32011-07-11 11:22:16 -040060 return 1;
61 }
aliguorie3aff4f2009-04-05 19:14:04 +000062
Max Reitz1b58b432015-02-05 13:58:20 -050063 qemuio_blk = blk_new_open("hda", name, NULL, opts, flags, &local_err);
64 if (!qemuio_blk) {
Markus Armbrusterdbb651c2014-09-08 18:50:58 +020065 fprintf(stderr, "%s: can't open%s%s: %s\n", progname,
66 name ? " device " : "", name ?: "",
67 error_get_pretty(local_err));
68 error_free(local_err);
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)) {
Max Reitzb543c5c2013-10-11 14:02:10 +0200157 printf("could not parse option list -- %s\n", optarg);
Markus Armbruster443422f2014-05-28 11:16:58 +0200158 qemu_opts_reset(&empty_opts);
Max Reitzb543c5c2013-10-11 14:02:10 +0200159 return 0;
160 }
Max Reitzb543c5c2013-10-11 14:02:10 +0200161 break;
Devin Nakamura43642b32011-07-11 11:22:16 -0400162 default:
Markus Armbruster443422f2014-05-28 11:16:58 +0200163 qemu_opts_reset(&empty_opts);
Kevin Wolfc2cdf5c2013-06-05 14:19:36 +0200164 return qemuio_command_usage(&open_cmd);
Naphtali Spreif5edb012010-01-17 16:48:13 +0200165 }
Devin Nakamura43642b32011-07-11 11:22:16 -0400166 }
aliguorie3aff4f2009-04-05 19:14:04 +0000167
Devin Nakamura43642b32011-07-11 11:22:16 -0400168 if (!readonly) {
169 flags |= BDRV_O_RDWR;
170 }
aliguorie3aff4f2009-04-05 19:14:04 +0000171
Markus Armbruster443422f2014-05-28 11:16:58 +0200172 qopts = qemu_opts_find(&empty_opts, NULL);
173 opts = qopts ? qemu_opts_to_qdict(qopts, NULL) : NULL;
174 qemu_opts_reset(&empty_opts);
175
Max Reitzfd0fee32013-12-20 19:28:20 +0100176 if (optind == argc - 1) {
Max Reitz10d9d752015-02-05 13:58:21 -0500177 return openfile(argv[optind], flags, opts);
Max Reitzfd0fee32013-12-20 19:28:20 +0100178 } else if (optind == argc) {
Max Reitz10d9d752015-02-05 13:58:21 -0500179 return openfile(NULL, flags, opts);
Max Reitzfd0fee32013-12-20 19:28:20 +0100180 } else {
Markus Armbruster29f26012014-05-28 11:16:59 +0200181 QDECREF(opts);
Kevin Wolfc2cdf5c2013-06-05 14:19:36 +0200182 return qemuio_command_usage(&open_cmd);
Devin Nakamura43642b32011-07-11 11:22:16 -0400183 }
aliguorie3aff4f2009-04-05 19:14:04 +0000184}
185
Max Reitz4c7b7e92015-02-05 13:58:22 -0500186static int quit_f(BlockBackend *blk, int argc, char **argv)
Kevin Wolfe681be72013-06-05 14:19:34 +0200187{
188 return 1;
189}
190
191static const cmdinfo_t quit_cmd = {
192 .name = "quit",
193 .altname = "q",
194 .cfunc = quit_f,
195 .argmin = -1,
196 .argmax = -1,
197 .flags = CMD_FLAG_GLOBAL,
198 .oneline = "exit the program",
199};
200
aliguorie3aff4f2009-04-05 19:14:04 +0000201static void usage(const char *name)
202{
Devin Nakamura43642b32011-07-11 11:22:16 -0400203 printf(
Kevin Wolfbe6273d2014-11-20 16:27:06 +0100204"Usage: %s [-h] [-V] [-rsnm] [-f FMT] [-c STRING] ... [file]\n"
Stefan Weil84844a22009-06-22 15:08:47 +0200205"QEMU Disk exerciser\n"
aliguorie3aff4f2009-04-05 19:14:04 +0000206"\n"
Maria Kustovad208cc32014-03-18 09:59:19 +0400207" -c, --cmd STRING execute command with its arguments\n"
208" from the given string\n"
Kevin Wolfbe6273d2014-11-20 16:27:06 +0100209" -f, --format FMT specifies the block driver to use\n"
aliguorie3aff4f2009-04-05 19:14:04 +0000210" -r, --read-only export read-only\n"
211" -s, --snapshot use snapshot file\n"
212" -n, --nocache disable host cache\n"
213" -m, --misalign misalign allocations for O_DIRECT\n"
Christoph Hellwig5c6c3a62009-08-20 16:58:35 +0200214" -k, --native-aio use kernel AIO implementation (on Linux only)\n"
Kevin Wolf592fa072012-04-18 12:07:39 +0200215" -t, --cache=MODE use the given cache mode for the image\n"
Stefan Hajnoczid7bb72c2012-03-12 16:36:07 +0000216" -T, --trace FILE enable trace events listed in the given file\n"
aliguorie3aff4f2009-04-05 19:14:04 +0000217" -h, --help display this help and exit\n"
218" -V, --version output version information and exit\n"
Maria Kustovad208cc32014-03-18 09:59:19 +0400219"\n"
220"See '%s -c help' for information on available commands."
aliguorie3aff4f2009-04-05 19:14:04 +0000221"\n",
Maria Kustovad208cc32014-03-18 09:59:19 +0400222 name, name);
aliguorie3aff4f2009-04-05 19:14:04 +0000223}
224
Kevin Wolfd1174f12013-06-05 14:19:37 +0200225static char *get_prompt(void)
226{
227 static char prompt[FILENAME_MAX + 2 /*"> "*/ + 1 /*"\0"*/ ];
228
229 if (!prompt[0]) {
230 snprintf(prompt, sizeof(prompt), "%s> ", progname);
231 }
232
233 return prompt;
234}
235
Stefan Weild5d15072014-01-25 18:18:23 +0100236static void GCC_FMT_ATTR(2, 3) readline_printf_func(void *opaque,
237 const char *fmt, ...)
Kevin Wolfd1174f12013-06-05 14:19:37 +0200238{
Stefan Hajnoczi0cf17e12013-11-14 11:54:17 +0100239 va_list ap;
240 va_start(ap, fmt);
241 vprintf(fmt, ap);
242 va_end(ap);
243}
244
245static void readline_flush_func(void *opaque)
246{
247 fflush(stdout);
248}
249
250static void readline_func(void *opaque, const char *str, void *readline_opaque)
251{
252 char **line = readline_opaque;
253 *line = g_strdup(str);
254}
255
Stefan Hajnoczi46940202013-11-14 11:54:18 +0100256static void completion_match(const char *cmd, void *opaque)
257{
258 readline_add_completion(readline_state, cmd);
259}
260
Stefan Hajnoczi0cf17e12013-11-14 11:54:17 +0100261static void readline_completion_func(void *opaque, const char *str)
262{
Stefan Hajnoczi46940202013-11-14 11:54:18 +0100263 readline_set_completion_index(readline_state, strlen(str));
264 qemuio_complete_command(str, completion_match, NULL);
Stefan Hajnoczi0cf17e12013-11-14 11:54:17 +0100265}
266
267static char *fetchline_readline(void)
268{
269 char *line = NULL;
270
271 readline_start(readline_state, get_prompt(), 0, readline_func, &line);
272 while (!line) {
273 int ch = getchar();
274 if (ch == EOF) {
275 break;
276 }
277 readline_handle_byte(readline_state, ch);
Kevin Wolfd1174f12013-06-05 14:19:37 +0200278 }
279 return line;
280}
Kevin Wolfd1174f12013-06-05 14:19:37 +0200281
Stefan Hajnoczi0cf17e12013-11-14 11:54:17 +0100282#define MAXREADLINESZ 1024
283static char *fetchline_fgets(void)
Kevin Wolfd1174f12013-06-05 14:19:37 +0200284{
285 char *p, *line = g_malloc(MAXREADLINESZ);
286
287 if (!fgets(line, MAXREADLINESZ, stdin)) {
288 g_free(line);
289 return NULL;
290 }
291
292 p = line + strlen(line);
293 if (p != line && p[-1] == '\n') {
294 p[-1] = '\0';
295 }
296
297 return line;
298}
Stefan Hajnoczi0cf17e12013-11-14 11:54:17 +0100299
300static char *fetchline(void)
301{
302 if (readline_state) {
303 return fetchline_readline();
304 } else {
305 return fetchline_fgets();
306 }
307}
Kevin Wolfd1174f12013-06-05 14:19:37 +0200308
309static void prep_fetchline(void *opaque)
310{
311 int *fetchable = opaque;
312
313 qemu_set_fd_handler(STDIN_FILENO, NULL, NULL, NULL);
314 *fetchable= 1;
315}
316
317static void command_loop(void)
318{
319 int i, done = 0, fetchable = 0, prompted = 0;
320 char *input;
321
322 for (i = 0; !done && i < ncmdline; i++) {
Max Reitz4c7b7e92015-02-05 13:58:22 -0500323 done = qemuio_command(qemuio_blk, cmdline[i]);
Kevin Wolfd1174f12013-06-05 14:19:37 +0200324 }
325 if (cmdline) {
326 g_free(cmdline);
327 return;
328 }
329
330 while (!done) {
331 if (!prompted) {
332 printf("%s", get_prompt());
333 fflush(stdout);
334 qemu_set_fd_handler(STDIN_FILENO, prep_fetchline, NULL, &fetchable);
335 prompted = 1;
336 }
337
338 main_loop_wait(false);
339
340 if (!fetchable) {
341 continue;
342 }
343
344 input = fetchline();
345 if (input == NULL) {
346 break;
347 }
Max Reitz4c7b7e92015-02-05 13:58:22 -0500348 done = qemuio_command(qemuio_blk, input);
Kevin Wolfd1174f12013-06-05 14:19:37 +0200349 g_free(input);
350
351 prompted = 0;
352 fetchable = 0;
353 }
354 qemu_set_fd_handler(STDIN_FILENO, NULL, NULL, NULL);
355}
356
357static void add_user_command(char *optarg)
358{
Markus Armbruster5839e532014-08-19 10:31:08 +0200359 cmdline = g_renew(char *, cmdline, ++ncmdline);
Kevin Wolfd1174f12013-06-05 14:19:37 +0200360 cmdline[ncmdline-1] = optarg;
361}
362
Stefan Hajnoczi0cf17e12013-11-14 11:54:17 +0100363static void reenable_tty_echo(void)
364{
365 qemu_set_tty_echo(STDIN_FILENO, true);
366}
367
aliguorie3aff4f2009-04-05 19:14:04 +0000368int main(int argc, char **argv)
369{
Devin Nakamura43642b32011-07-11 11:22:16 -0400370 int readonly = 0;
Kevin Wolfbe6273d2014-11-20 16:27:06 +0100371 const char *sopt = "hVc:d:f:rsnmgkt:T:";
Devin Nakamura43642b32011-07-11 11:22:16 -0400372 const struct option lopt[] = {
373 { "help", 0, NULL, 'h' },
374 { "version", 0, NULL, 'V' },
375 { "offset", 1, NULL, 'o' },
376 { "cmd", 1, NULL, 'c' },
Kevin Wolfbe6273d2014-11-20 16:27:06 +0100377 { "format", 1, NULL, 'f' },
Devin Nakamura43642b32011-07-11 11:22:16 -0400378 { "read-only", 0, NULL, 'r' },
379 { "snapshot", 0, NULL, 's' },
380 { "nocache", 0, NULL, 'n' },
381 { "misalign", 0, NULL, 'm' },
Devin Nakamura43642b32011-07-11 11:22:16 -0400382 { "native-aio", 0, NULL, 'k' },
Paolo Bonzini9e8f1832013-02-08 14:06:11 +0100383 { "discard", 1, NULL, 'd' },
Kevin Wolf592fa072012-04-18 12:07:39 +0200384 { "cache", 1, NULL, 't' },
Stefan Hajnoczid7bb72c2012-03-12 16:36:07 +0000385 { "trace", 1, NULL, 'T' },
Devin Nakamura43642b32011-07-11 11:22:16 -0400386 { NULL, 0, NULL, 0 }
387 };
388 int c;
389 int opt_index = 0;
Paolo Bonzini9e8f1832013-02-08 14:06:11 +0100390 int flags = BDRV_O_UNMAP;
Chrysostomos Nanakos2f78e492014-09-18 14:30:49 +0300391 Error *local_error = NULL;
Max Reitz1b58b432015-02-05 13:58:20 -0500392 QDict *opts = NULL;
aliguorie3aff4f2009-04-05 19:14:04 +0000393
MORITA Kazutaka526eda12013-07-23 17:30:11 +0900394#ifdef CONFIG_POSIX
395 signal(SIGPIPE, SIG_IGN);
396#endif
397
Devin Nakamura43642b32011-07-11 11:22:16 -0400398 progname = basename(argv[0]);
Fam Zheng10f5bff2014-02-10 14:48:51 +0800399 qemu_init_exec_dir(argv[0]);
aliguorie3aff4f2009-04-05 19:14:04 +0000400
Kevin Wolfbe6273d2014-11-20 16:27:06 +0100401 bdrv_init();
402
Devin Nakamura43642b32011-07-11 11:22:16 -0400403 while ((c = getopt_long(argc, argv, sopt, lopt, &opt_index)) != -1) {
404 switch (c) {
405 case 's':
406 flags |= BDRV_O_SNAPSHOT;
407 break;
408 case 'n':
409 flags |= BDRV_O_NOCACHE | BDRV_O_CACHE_WB;
410 break;
Paolo Bonzini9e8f1832013-02-08 14:06:11 +0100411 case 'd':
412 if (bdrv_parse_discard_flags(optarg, &flags) < 0) {
413 error_report("Invalid discard option: %s", optarg);
414 exit(1);
415 }
416 break;
Kevin Wolfbe6273d2014-11-20 16:27:06 +0100417 case 'f':
Max Reitz1b58b432015-02-05 13:58:20 -0500418 if (!opts) {
419 opts = qdict_new();
Kevin Wolfbe6273d2014-11-20 16:27:06 +0100420 }
Max Reitz1b58b432015-02-05 13:58:20 -0500421 qdict_put(opts, "driver", qstring_from_str(optarg));
Kevin Wolfbe6273d2014-11-20 16:27:06 +0100422 break;
Devin Nakamura43642b32011-07-11 11:22:16 -0400423 case 'c':
424 add_user_command(optarg);
425 break;
426 case 'r':
427 readonly = 1;
428 break;
429 case 'm':
Stefan Weilf9883882014-03-05 22:23:00 +0100430 qemuio_misalign = true;
Devin Nakamura43642b32011-07-11 11:22:16 -0400431 break;
Devin Nakamura43642b32011-07-11 11:22:16 -0400432 case 'k':
433 flags |= BDRV_O_NATIVE_AIO;
434 break;
Kevin Wolf592fa072012-04-18 12:07:39 +0200435 case 't':
436 if (bdrv_parse_cache_flags(optarg, &flags) < 0) {
437 error_report("Invalid cache option: %s", optarg);
438 exit(1);
439 }
440 break;
Stefan Hajnoczid7bb72c2012-03-12 16:36:07 +0000441 case 'T':
LluĂ­s Vilanova5b808272014-05-27 15:02:14 +0200442 if (!trace_init_backends(optarg, NULL)) {
Stefan Hajnoczid7bb72c2012-03-12 16:36:07 +0000443 exit(1); /* error message will have been printed */
444 }
445 break;
Devin Nakamura43642b32011-07-11 11:22:16 -0400446 case 'V':
Kevin Wolf02da3862013-06-05 14:19:40 +0200447 printf("%s version %s\n", progname, QEMU_VERSION);
Devin Nakamura43642b32011-07-11 11:22:16 -0400448 exit(0);
449 case 'h':
450 usage(progname);
451 exit(0);
452 default:
453 usage(progname);
454 exit(1);
Naphtali Spreif5edb012010-01-17 16:48:13 +0200455 }
Devin Nakamura43642b32011-07-11 11:22:16 -0400456 }
aliguorie3aff4f2009-04-05 19:14:04 +0000457
Devin Nakamura43642b32011-07-11 11:22:16 -0400458 if ((argc - optind) > 1) {
459 usage(progname);
460 exit(1);
461 }
aliguorie3aff4f2009-04-05 19:14:04 +0000462
Chrysostomos Nanakos2f78e492014-09-18 14:30:49 +0300463 if (qemu_init_main_loop(&local_error)) {
Markus Armbruster565f65d2015-02-12 13:55:05 +0100464 error_report_err(local_error);
Chrysostomos Nanakos2f78e492014-09-18 14:30:49 +0300465 exit(1);
466 }
Zhi Yong Wua57d1142012-02-19 22:24:59 +0800467
Devin Nakamura43642b32011-07-11 11:22:16 -0400468 /* initialize commands */
Kevin Wolfc2cdf5c2013-06-05 14:19:36 +0200469 qemuio_add_command(&quit_cmd);
470 qemuio_add_command(&open_cmd);
471 qemuio_add_command(&close_cmd);
Devin Nakamura43642b32011-07-11 11:22:16 -0400472
Stefan Hajnoczi0cf17e12013-11-14 11:54:17 +0100473 if (isatty(STDIN_FILENO)) {
474 readline_state = readline_init(readline_printf_func,
475 readline_flush_func,
476 NULL,
477 readline_completion_func);
478 qemu_set_tty_echo(STDIN_FILENO, false);
479 atexit(reenable_tty_echo);
480 }
481
Devin Nakamura43642b32011-07-11 11:22:16 -0400482 /* open the device */
483 if (!readonly) {
484 flags |= BDRV_O_RDWR;
485 }
486
487 if ((argc - optind) == 1) {
Max Reitz10d9d752015-02-05 13:58:21 -0500488 openfile(argv[optind], flags, opts);
Devin Nakamura43642b32011-07-11 11:22:16 -0400489 }
490 command_loop();
491
492 /*
Stefan Hajnoczi922453b2011-11-30 12:23:43 +0000493 * Make sure all outstanding requests complete before the program exits.
Devin Nakamura43642b32011-07-11 11:22:16 -0400494 */
Stefan Hajnoczi922453b2011-11-30 12:23:43 +0000495 bdrv_drain_all();
Devin Nakamura43642b32011-07-11 11:22:16 -0400496
Markus Armbruster26f54e92014-10-07 13:59:04 +0200497 blk_unref(qemuio_blk);
Stefan Hajnoczi0cf17e12013-11-14 11:54:17 +0100498 g_free(readline_state);
Devin Nakamura43642b32011-07-11 11:22:16 -0400499 return 0;
aliguorie3aff4f2009-04-05 19:14:04 +0000500}