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