]> git.ipfire.org Git - thirdparty/qemu.git/blame - qemu-io.c
target/m68k: implement fmove.l #<data>,FPCR
[thirdparty/qemu.git] / qemu-io.c
CommitLineData
e3aff4f6
AL
1/*
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 */
452fcdbc 10
80c71a24 11#include "qemu/osdep.h"
e3aff4f6 12#include <getopt.h>
c32d766a 13#include <libgen.h>
0e448a05
DB
14#ifndef _WIN32
15#include <termios.h>
16#endif
e3aff4f6 17
a8d25326 18#include "qemu-common.h"
da34e65c 19#include "qapi/error.h"
3d21994f 20#include "qemu-io.h"
d49b6836 21#include "qemu/error-report.h"
1de7afc9 22#include "qemu/main-loop.h"
0b8fa32f 23#include "qemu/module.h"
b543c5cd
HR
24#include "qemu/option.h"
25#include "qemu/config-file.h"
0cf17e18 26#include "qemu/readline.h"
e9a80859 27#include "qemu/log.h"
d49b6836 28#include "qapi/qmp/qstring.h"
452fcdbc 29#include "qapi/qmp/qdict.h"
9ba371b6 30#include "qom/object_interfaces.h"
26f54e9a 31#include "sysemu/block-backend.h"
737e150e 32#include "block/block_int.h"
d7bb72c8 33#include "trace/control.h"
c2297088 34#include "crypto/init.h"
4face32a 35#include "qemu-version.h"
e3aff4f6 36
43642b38 37#define CMD_NOFILE_OK 0x01
e3aff4f6 38
26f54e9a 39static BlockBackend *qemuio_blk;
b444d0e9 40static bool quit_qemu_io;
191c2890 41
d1174f13
KW
42/* qemu-io commands passed using -c */
43static int ncmdline;
44static char **cmdline;
499afa25 45static bool imageOpts;
d1174f13 46
0cf17e18
SH
47static ReadLineState *readline_state;
48
0e448a05
DB
49static int ttyEOF;
50
51static int get_eof_char(void)
52{
53#ifdef _WIN32
54 return 0x4; /* Ctrl-D */
55#else
56 struct termios tty;
57 if (tcgetattr(STDIN_FILENO, &tty) != 0) {
58 if (errno == ENOTTY) {
59 return 0x0; /* just expect read() == 0 */
60 } else {
61 return 0x4; /* Ctrl-D */
62 }
63 }
64
65 return tty.c_cc[VEOF];
66#endif
67}
68
b32d7a39 69static int close_f(BlockBackend *blk, int argc, char **argv)
e3aff4f6 70{
26f54e9a 71 blk_unref(qemuio_blk);
26f54e9a 72 qemuio_blk = NULL;
b32d7a39 73 return 0;
e3aff4f6
AL
74}
75
76static const cmdinfo_t close_cmd = {
43642b38
DN
77 .name = "close",
78 .altname = "c",
79 .cfunc = close_f,
80 .oneline = "close the current open file",
e3aff4f6
AL
81};
82
459571f7
FZ
83static int openfile(char *name, int flags, bool writethrough, bool force_share,
84 QDict *opts)
e3aff4f6 85{
34b5d2c6
HR
86 Error *local_err = NULL;
87
1b58b438 88 if (qemuio_blk) {
b9884681 89 error_report("file open already, try 'help close'");
cb3e7f08 90 qobject_unref(opts);
43642b38
DN
91 return 1;
92 }
93
459571f7
FZ
94 if (force_share) {
95 if (!opts) {
96 opts = qdict_new();
97 }
98 if (qdict_haskey(opts, BDRV_OPT_FORCE_SHARE)
2a01c01f 99 && strcmp(qdict_get_str(opts, BDRV_OPT_FORCE_SHARE), "on")) {
459571f7 100 error_report("-U conflicts with image options");
cb3e7f08 101 qobject_unref(opts);
459571f7
FZ
102 return 1;
103 }
2a01c01f 104 qdict_put_str(opts, BDRV_OPT_FORCE_SHARE, "on");
459571f7 105 }
efaa7c4e 106 qemuio_blk = blk_new_open(name, NULL, opts, flags, &local_err);
1b58b438 107 if (!qemuio_blk) {
b9884681
MA
108 error_reportf_err(local_err, "can't open%s%s: ",
109 name ? " device " : "", name ?: "");
dbb651c4 110 return 1;
43642b38
DN
111 }
112
e151fc16 113 blk_set_enable_write_cache(qemuio_blk, !writethrough);
8caf0212 114
43642b38 115 return 0;
e3aff4f6
AL
116}
117
43642b38 118static void open_help(void)
e3aff4f6 119{
43642b38 120 printf(
e3aff4f6
AL
121"\n"
122" opens a new file in the requested mode\n"
123"\n"
124" Example:\n"
e4e12bb2 125" 'open -n -o driver=raw /tmp/data' - opens raw data file read-write, uncached\n"
e3aff4f6
AL
126"\n"
127" Opens a file for subsequent use by all of the other qemu-io commands.\n"
e3aff4f6
AL
128" -r, -- open file read-only\n"
129" -s, -- use snapshot file\n"
0f40444c 130" -C, -- use copy-on-read\n"
b8d970f1 131" -n, -- disable host cache, short for -t none\n"
459571f7 132" -U, -- force shared permissions\n"
1c5a2aec
AM
133" -k, -- use kernel AIO implementation (Linux only, prefer use of -i)\n"
134" -i, -- use AIO mode (threads, native or io_uring)\n"
b8d970f1
EB
135" -t, -- use the given cache mode for the image\n"
136" -d, -- use the given discard mode for the image\n"
b543c5cd 137" -o, -- options to be given to the block driver"
e3aff4f6
AL
138"\n");
139}
140
b32d7a39 141static int open_f(BlockBackend *blk, int argc, char **argv);
22a2bdcb
BS
142
143static const cmdinfo_t open_cmd = {
43642b38
DN
144 .name = "open",
145 .altname = "o",
146 .cfunc = open_f,
147 .argmin = 1,
148 .argmax = -1,
149 .flags = CMD_NOFILE_OK,
0f40444c 150 .args = "[-rsCnkU] [-t cache] [-d discard] [-o options] [path]",
43642b38
DN
151 .oneline = "open the file specified by path",
152 .help = open_help,
22a2bdcb 153};
e3aff4f6 154
b543c5cd
HR
155static QemuOptsList empty_opts = {
156 .name = "drive",
443422fd 157 .merge_lists = true,
b543c5cd
HR
158 .head = QTAILQ_HEAD_INITIALIZER(empty_opts.head),
159 .desc = {
160 /* no elements => accept any params */
161 { /* end of list */ }
162 },
163};
164
b32d7a39 165static int open_f(BlockBackend *blk, int argc, char **argv)
e3aff4f6 166{
b8d970f1 167 int flags = BDRV_O_UNMAP;
43642b38 168 int readonly = 0;
e151fc16 169 bool writethrough = true;
43642b38 170 int c;
b32d7a39 171 int ret;
b543c5cd 172 QemuOpts *qopts;
443422fd 173 QDict *opts;
459571f7 174 bool force_share = false;
43642b38 175
1c5a2aec 176 while ((c = getopt(argc, argv, "snCro:ki:t:d:U")) != -1) {
43642b38
DN
177 switch (c) {
178 case 's':
179 flags |= BDRV_O_SNAPSHOT;
180 break;
181 case 'n':
e151fc16
KW
182 flags |= BDRV_O_NOCACHE;
183 writethrough = false;
43642b38 184 break;
0f40444c
EB
185 case 'C':
186 flags |= BDRV_O_COPY_ON_READ;
187 break;
43642b38
DN
188 case 'r':
189 readonly = 1;
190 break;
b8d970f1
EB
191 case 'k':
192 flags |= BDRV_O_NATIVE_AIO;
193 break;
194 case 't':
195 if (bdrv_parse_cache_mode(optarg, &flags, &writethrough) < 0) {
196 error_report("Invalid cache option: %s", optarg);
197 qemu_opts_reset(&empty_opts);
b32d7a39 198 return -EINVAL;
b8d970f1
EB
199 }
200 break;
201 case 'd':
202 if (bdrv_parse_discard_flags(optarg, &flags) < 0) {
203 error_report("Invalid discard option: %s", optarg);
204 qemu_opts_reset(&empty_opts);
b32d7a39 205 return -EINVAL;
b8d970f1
EB
206 }
207 break;
1c5a2aec
AM
208 case 'i':
209 if (bdrv_parse_aio(optarg, &flags) < 0) {
210 error_report("Invalid aio option: %s", optarg);
211 qemu_opts_reset(&empty_opts);
212 return -EINVAL;
213 }
214 break;
b543c5cd 215 case 'o':
499afa25
DB
216 if (imageOpts) {
217 printf("--image-opts and 'open -o' are mutually exclusive\n");
b8d970f1 218 qemu_opts_reset(&empty_opts);
b32d7a39 219 return -EINVAL;
499afa25 220 }
70b94331 221 if (!qemu_opts_parse_noisily(&empty_opts, optarg, false)) {
443422fd 222 qemu_opts_reset(&empty_opts);
b32d7a39 223 return -EINVAL;
b543c5cd 224 }
b543c5cd 225 break;
459571f7
FZ
226 case 'U':
227 force_share = true;
228 break;
43642b38 229 default:
443422fd 230 qemu_opts_reset(&empty_opts);
b444d0e9 231 qemuio_command_usage(&open_cmd);
b32d7a39 232 return -EINVAL;
f5edb014 233 }
43642b38
DN
234 }
235
236 if (!readonly) {
237 flags |= BDRV_O_RDWR;
238 }
e3aff4f6 239
499afa25
DB
240 if (imageOpts && (optind == argc - 1)) {
241 if (!qemu_opts_parse_noisily(&empty_opts, argv[optind], false)) {
242 qemu_opts_reset(&empty_opts);
b32d7a39 243 return -EINVAL;
499afa25
DB
244 }
245 optind++;
246 }
247
443422fd
MA
248 qopts = qemu_opts_find(&empty_opts, NULL);
249 opts = qopts ? qemu_opts_to_qdict(qopts, NULL) : NULL;
250 qemu_opts_reset(&empty_opts);
251
fd0fee34 252 if (optind == argc - 1) {
b32d7a39 253 ret = openfile(argv[optind], flags, writethrough, force_share, opts);
fd0fee34 254 } else if (optind == argc) {
b32d7a39 255 ret = openfile(NULL, flags, writethrough, force_share, opts);
fd0fee34 256 } else {
cb3e7f08 257 qobject_unref(opts);
64ebf556 258 qemuio_command_usage(&open_cmd);
b32d7a39
HR
259 return -EINVAL;
260 }
261
262 if (ret) {
263 return -EINVAL;
43642b38 264 }
b32d7a39
HR
265
266 return 0;
e3aff4f6
AL
267}
268
b32d7a39 269static int quit_f(BlockBackend *blk, int argc, char **argv)
e681be7e 270{
b444d0e9 271 quit_qemu_io = true;
b32d7a39 272 return 0;
e681be7e
KW
273}
274
275static const cmdinfo_t quit_cmd = {
276 .name = "quit",
277 .altname = "q",
278 .cfunc = quit_f,
279 .argmin = -1,
280 .argmax = -1,
281 .flags = CMD_FLAG_GLOBAL,
282 .oneline = "exit the program",
283};
284
e3aff4f6
AL
285static void usage(const char *name)
286{
43642b38 287 printf(
e4e12bb2 288"Usage: %s [OPTIONS]... [-c STRING]... [file]\n"
84844a20 289"QEMU Disk exerciser\n"
e3aff4f6 290"\n"
9ba371b6
DB
291" --object OBJECTDEF define an object such as 'secret' for\n"
292" passwords and/or encryption keys\n"
e4e12bb2 293" --image-opts treat file as option string\n"
d208cc35
MK
294" -c, --cmd STRING execute command with its arguments\n"
295" from the given string\n"
be6273da 296" -f, --format FMT specifies the block driver to use\n"
e3aff4f6
AL
297" -r, --read-only export read-only\n"
298" -s, --snapshot use snapshot file\n"
e4e12bb2 299" -n, --nocache disable host cache, short for -t none\n"
0f40444c 300" -C, --copy-on-read enable copy-on-read\n"
e3aff4f6 301" -m, --misalign misalign allocations for O_DIRECT\n"
1c5a2aec
AM
302" -k, --native-aio use kernel AIO implementation\n"
303" (Linux only, prefer use of -i)\n"
304" -i, --aio=MODE use AIO mode (threads, native or io_uring)\n"
592fa070 305" -t, --cache=MODE use the given cache mode for the image\n"
e4e12bb2 306" -d, --discard=MODE use the given discard mode for the image\n"
e9a80859
DL
307" -T, --trace [[enable=]<pattern>][,events=<file>][,file=<file>]\n"
308" specify tracing options\n"
309" see qemu-img(1) man page for full description\n"
459571f7 310" -U, --force-share force shared permissions\n"
e3aff4f6
AL
311" -h, --help display this help and exit\n"
312" -V, --version output version information and exit\n"
d208cc35 313"\n"
f5048cb7
EB
314"See '%s -c help' for information on available commands.\n"
315"\n"
316QEMU_HELP_BOTTOM "\n",
d208cc35 317 name, name);
e3aff4f6
AL
318}
319
d1174f13
KW
320static char *get_prompt(void)
321{
322 static char prompt[FILENAME_MAX + 2 /*"> "*/ + 1 /*"\0"*/ ];
323
324 if (!prompt[0]) {
99e98d7c 325 snprintf(prompt, sizeof(prompt), "%s> ", error_get_progname());
d1174f13
KW
326 }
327
328 return prompt;
329}
330
d5d1507b
SW
331static void GCC_FMT_ATTR(2, 3) readline_printf_func(void *opaque,
332 const char *fmt, ...)
d1174f13 333{
0cf17e18
SH
334 va_list ap;
335 va_start(ap, fmt);
336 vprintf(fmt, ap);
337 va_end(ap);
d1174f13 338}
0cf17e18
SH
339
340static void readline_flush_func(void *opaque)
d1174f13 341{
0cf17e18 342 fflush(stdout);
d1174f13
KW
343}
344
0cf17e18 345static void readline_func(void *opaque, const char *str, void *readline_opaque)
d1174f13 346{
0cf17e18
SH
347 char **line = readline_opaque;
348 *line = g_strdup(str);
349}
350
4694020d
SH
351static void completion_match(const char *cmd, void *opaque)
352{
353 readline_add_completion(readline_state, cmd);
354}
355
0cf17e18
SH
356static void readline_completion_func(void *opaque, const char *str)
357{
4694020d
SH
358 readline_set_completion_index(readline_state, strlen(str));
359 qemuio_complete_command(str, completion_match, NULL);
0cf17e18
SH
360}
361
362static char *fetchline_readline(void)
363{
364 char *line = NULL;
365
366 readline_start(readline_state, get_prompt(), 0, readline_func, &line);
367 while (!line) {
368 int ch = getchar();
0e448a05
DB
369 if (ttyEOF != 0x0 && ch == ttyEOF) {
370 printf("\n");
0cf17e18 371 break;
d1174f13 372 }
0cf17e18 373 readline_handle_byte(readline_state, ch);
d1174f13
KW
374 }
375 return line;
376}
0cf17e18
SH
377
378#define MAXREADLINESZ 1024
379static char *fetchline_fgets(void)
d1174f13
KW
380{
381 char *p, *line = g_malloc(MAXREADLINESZ);
382
383 if (!fgets(line, MAXREADLINESZ, stdin)) {
384 g_free(line);
385 return NULL;
386 }
387
388 p = line + strlen(line);
389 if (p != line && p[-1] == '\n') {
390 p[-1] = '\0';
391 }
392
393 return line;
394}
0cf17e18
SH
395
396static char *fetchline(void)
397{
398 if (readline_state) {
399 return fetchline_readline();
400 } else {
401 return fetchline_fgets();
402 }
403}
d1174f13
KW
404
405static void prep_fetchline(void *opaque)
406{
407 int *fetchable = opaque;
408
409 qemu_set_fd_handler(STDIN_FILENO, NULL, NULL, NULL);
410 *fetchable= 1;
411}
412
6b3aa848 413static int command_loop(void)
d1174f13 414{
b444d0e9 415 int i, fetchable = 0, prompted = 0;
6b3aa848 416 int ret, last_error = 0;
d1174f13
KW
417 char *input;
418
b444d0e9 419 for (i = 0; !quit_qemu_io && i < ncmdline; i++) {
6b3aa848
HR
420 ret = qemuio_command(qemuio_blk, cmdline[i]);
421 if (ret < 0) {
422 last_error = ret;
423 }
d1174f13
KW
424 }
425 if (cmdline) {
426 g_free(cmdline);
6b3aa848 427 return last_error;
d1174f13
KW
428 }
429
b444d0e9 430 while (!quit_qemu_io) {
d1174f13
KW
431 if (!prompted) {
432 printf("%s", get_prompt());
433 fflush(stdout);
434 qemu_set_fd_handler(STDIN_FILENO, prep_fetchline, NULL, &fetchable);
435 prompted = 1;
436 }
437
438 main_loop_wait(false);
439
440 if (!fetchable) {
441 continue;
442 }
443
444 input = fetchline();
445 if (input == NULL) {
446 break;
447 }
6b3aa848 448 ret = qemuio_command(qemuio_blk, input);
d1174f13
KW
449 g_free(input);
450
6b3aa848
HR
451 if (ret < 0) {
452 last_error = ret;
453 }
454
d1174f13
KW
455 prompted = 0;
456 fetchable = 0;
457 }
458 qemu_set_fd_handler(STDIN_FILENO, NULL, NULL, NULL);
6b3aa848
HR
459
460 return last_error;
d1174f13
KW
461}
462
463static void add_user_command(char *optarg)
464{
5839e53b 465 cmdline = g_renew(char *, cmdline, ++ncmdline);
d1174f13
KW
466 cmdline[ncmdline-1] = optarg;
467}
468
0cf17e18
SH
469static void reenable_tty_echo(void)
470{
471 qemu_set_tty_echo(STDIN_FILENO, true);
472}
473
9ba371b6
DB
474enum {
475 OPTION_OBJECT = 256,
499afa25 476 OPTION_IMAGE_OPTS = 257,
9ba371b6
DB
477};
478
479static QemuOptsList qemu_object_opts = {
480 .name = "object",
481 .implied_opt_name = "qom-type",
482 .head = QTAILQ_HEAD_INITIALIZER(qemu_object_opts.head),
483 .desc = {
484 { }
485 },
486};
487
4fa1f0dc
KW
488static bool qemu_io_object_print_help(const char *type, QemuOpts *opts)
489{
490 if (user_creatable_print_help(type, opts)) {
491 exit(0);
492 }
493 return true;
494}
9ba371b6 495
499afa25
DB
496static QemuOptsList file_opts = {
497 .name = "file",
498 .implied_opt_name = "file",
499 .head = QTAILQ_HEAD_INITIALIZER(file_opts.head),
500 .desc = {
501 /* no elements => accept any params */
502 { /* end of list */ }
503 },
504};
505
e3aff4f6
AL
506int main(int argc, char **argv)
507{
43642b38 508 int readonly = 0;
1c5a2aec 509 const char *sopt = "hVc:d:f:rsnCmki:t:T:U";
43642b38 510 const struct option lopt[] = {
a513416e
DB
511 { "help", no_argument, NULL, 'h' },
512 { "version", no_argument, NULL, 'V' },
a513416e
DB
513 { "cmd", required_argument, NULL, 'c' },
514 { "format", required_argument, NULL, 'f' },
515 { "read-only", no_argument, NULL, 'r' },
516 { "snapshot", no_argument, NULL, 's' },
517 { "nocache", no_argument, NULL, 'n' },
0f40444c 518 { "copy-on-read", no_argument, NULL, 'C' },
a513416e
DB
519 { "misalign", no_argument, NULL, 'm' },
520 { "native-aio", no_argument, NULL, 'k' },
1c5a2aec 521 { "aio", required_argument, NULL, 'i' },
a513416e
DB
522 { "discard", required_argument, NULL, 'd' },
523 { "cache", required_argument, NULL, 't' },
524 { "trace", required_argument, NULL, 'T' },
525 { "object", required_argument, NULL, OPTION_OBJECT },
526 { "image-opts", no_argument, NULL, OPTION_IMAGE_OPTS },
459571f7 527 { "force-share", no_argument, 0, 'U'},
43642b38
DN
528 { NULL, 0, NULL, 0 }
529 };
530 int c;
531 int opt_index = 0;
9e8f1835 532 int flags = BDRV_O_UNMAP;
6b3aa848 533 int ret;
e151fc16 534 bool writethrough = true;
2f78e491 535 Error *local_error = NULL;
1b58b438 536 QDict *opts = NULL;
499afa25 537 const char *format = NULL;
e9a80859 538 char *trace_file = NULL;
459571f7 539 bool force_share = false;
43642b38 540
526eda14
MK
541#ifdef CONFIG_POSIX
542 signal(SIGPIPE, SIG_IGN);
543#endif
544
f5852efa 545 error_init(argv[0]);
fe4db84d 546 module_call_init(MODULE_INIT_TRACE);
10f5bff6 547 qemu_init_exec_dir(argv[0]);
43642b38 548
e8f2d272 549 qcrypto_init(&error_fatal);
c2297088 550
064097d9 551 module_call_init(MODULE_INIT_QOM);
9ba371b6 552 qemu_add_opts(&qemu_object_opts);
e9a80859 553 qemu_add_opts(&qemu_trace_opts);
be6273da
KW
554 bdrv_init();
555
43642b38
DN
556 while ((c = getopt_long(argc, argv, sopt, lopt, &opt_index)) != -1) {
557 switch (c) {
558 case 's':
559 flags |= BDRV_O_SNAPSHOT;
560 break;
561 case 'n':
e151fc16
KW
562 flags |= BDRV_O_NOCACHE;
563 writethrough = false;
43642b38 564 break;
0f40444c
EB
565 case 'C':
566 flags |= BDRV_O_COPY_ON_READ;
567 break;
9e8f1835
PB
568 case 'd':
569 if (bdrv_parse_discard_flags(optarg, &flags) < 0) {
570 error_report("Invalid discard option: %s", optarg);
571 exit(1);
572 }
573 break;
be6273da 574 case 'f':
499afa25 575 format = optarg;
be6273da 576 break;
43642b38
DN
577 case 'c':
578 add_user_command(optarg);
579 break;
580 case 'r':
581 readonly = 1;
582 break;
583 case 'm':
f9883880 584 qemuio_misalign = true;
43642b38 585 break;
43642b38
DN
586 case 'k':
587 flags |= BDRV_O_NATIVE_AIO;
588 break;
1c5a2aec
AM
589 case 'i':
590 if (bdrv_parse_aio(optarg, &flags) < 0) {
591 error_report("Invalid aio option: %s", optarg);
592 exit(1);
593 }
594 break;
592fa070 595 case 't':
e151fc16 596 if (bdrv_parse_cache_mode(optarg, &flags, &writethrough) < 0) {
592fa070
KW
597 error_report("Invalid cache option: %s", optarg);
598 exit(1);
599 }
600 break;
d7bb72c8 601 case 'T':
e9a80859
DL
602 g_free(trace_file);
603 trace_file = trace_opt_parse(optarg);
d7bb72c8 604 break;
43642b38 605 case 'V':
7e563bfb 606 printf("%s version " QEMU_FULL_VERSION "\n"
99e98d7c 607 QEMU_COPYRIGHT "\n", error_get_progname());
43642b38
DN
608 exit(0);
609 case 'h':
99e98d7c 610 usage(error_get_progname());
43642b38 611 exit(0);
459571f7
FZ
612 case 'U':
613 force_share = true;
614 break;
9ba371b6 615 case OPTION_OBJECT: {
499afa25 616 QemuOpts *qopts;
9ba371b6
DB
617 qopts = qemu_opts_parse_noisily(&qemu_object_opts,
618 optarg, true);
619 if (!qopts) {
620 exit(1);
621 }
622 } break;
499afa25
DB
623 case OPTION_IMAGE_OPTS:
624 imageOpts = true;
625 break;
43642b38 626 default:
99e98d7c 627 usage(error_get_progname());
43642b38 628 exit(1);
f5edb014 629 }
43642b38
DN
630 }
631
632 if ((argc - optind) > 1) {
99e98d7c 633 usage(error_get_progname());
43642b38
DN
634 exit(1);
635 }
e3aff4f6 636
499afa25
DB
637 if (format && imageOpts) {
638 error_report("--image-opts and -f are mutually exclusive");
639 exit(1);
640 }
641
2f78e491 642 if (qemu_init_main_loop(&local_error)) {
565f65d2 643 error_report_err(local_error);
2f78e491
CN
644 exit(1);
645 }
a57d1143 646
7e1e0c11
MA
647 qemu_opts_foreach(&qemu_object_opts,
648 user_creatable_add_opts_foreach,
4fa1f0dc 649 qemu_io_object_print_help, &error_fatal);
9ba371b6 650
e9a80859
DL
651 if (!trace_init_backends()) {
652 exit(1);
653 }
654 trace_init_file(trace_file);
655 qemu_set_log(LOG_TRACE);
656
43642b38 657 /* initialize commands */
c2cdf5c5
KW
658 qemuio_add_command(&quit_cmd);
659 qemuio_add_command(&open_cmd);
660 qemuio_add_command(&close_cmd);
43642b38 661
0cf17e18 662 if (isatty(STDIN_FILENO)) {
0e448a05 663 ttyEOF = get_eof_char();
0cf17e18
SH
664 readline_state = readline_init(readline_printf_func,
665 readline_flush_func,
666 NULL,
667 readline_completion_func);
668 qemu_set_tty_echo(STDIN_FILENO, false);
669 atexit(reenable_tty_echo);
670 }
671
43642b38
DN
672 /* open the device */
673 if (!readonly) {
674 flags |= BDRV_O_RDWR;
675 }
676
677 if ((argc - optind) == 1) {
499afa25
DB
678 if (imageOpts) {
679 QemuOpts *qopts = NULL;
680 qopts = qemu_opts_parse_noisily(&file_opts, argv[optind], false);
681 if (!qopts) {
682 exit(1);
683 }
684 opts = qemu_opts_to_qdict(qopts, NULL);
459571f7 685 if (openfile(NULL, flags, writethrough, force_share, opts)) {
b7aa1315
NS
686 exit(1);
687 }
499afa25
DB
688 } else {
689 if (format) {
690 opts = qdict_new();
46f5ac20 691 qdict_put_str(opts, "driver", format);
499afa25 692 }
459571f7
FZ
693 if (openfile(argv[optind], flags, writethrough,
694 force_share, opts)) {
b7aa1315
NS
695 exit(1);
696 }
499afa25 697 }
43642b38 698 }
6b3aa848 699 ret = command_loop();
e3aff4f6 700
43642b38 701 /*
922453bc 702 * Make sure all outstanding requests complete before the program exits.
43642b38 703 */
922453bc 704 bdrv_drain_all();
95533d5f 705
26f54e9a 706 blk_unref(qemuio_blk);
0cf17e18 707 g_free(readline_state);
6b3aa848
HR
708
709 if (ret < 0) {
710 return 1;
711 } else {
712 return 0;
713 }
e3aff4f6 714}