oslib-posix: Fix compiler warning
[qemu/ar7.git] / qemu-io.c
blobdcbe9f4dc70a0f2a43d18fdba14d5ba020be423b
1 /*
2 * Command line utility to exercise the QEMU I/O path.
4 * Copyright (C) 2009 Red Hat, Inc.
5 * Copyright (c) 2003-2005 Silicon Graphics, Inc.
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 */
10 #include "qemu/osdep.h"
11 #include <getopt.h>
12 #include <libgen.h>
14 #include "qapi/error.h"
15 #include "qemu-io.h"
16 #include "qemu/error-report.h"
17 #include "qemu/main-loop.h"
18 #include "qemu/option.h"
19 #include "qemu/config-file.h"
20 #include "qemu/readline.h"
21 #include "qemu/log.h"
22 #include "qapi/qmp/qstring.h"
23 #include "qapi/qmp/qbool.h"
24 #include "qom/object_interfaces.h"
25 #include "sysemu/block-backend.h"
26 #include "block/block_int.h"
27 #include "trace/control.h"
28 #include "crypto/init.h"
29 #include "qemu-version.h"
31 #define CMD_NOFILE_OK 0x01
33 static char *progname;
35 static BlockBackend *qemuio_blk;
37 /* qemu-io commands passed using -c */
38 static int ncmdline;
39 static char **cmdline;
40 static bool imageOpts;
42 static ReadLineState *readline_state;
44 static int close_f(BlockBackend *blk, int argc, char **argv)
46 blk_unref(qemuio_blk);
47 qemuio_blk = NULL;
48 return 0;
51 static const cmdinfo_t close_cmd = {
52 .name = "close",
53 .altname = "c",
54 .cfunc = close_f,
55 .oneline = "close the current open file",
58 static int openfile(char *name, int flags, bool writethrough, bool force_share,
59 QDict *opts)
61 Error *local_err = NULL;
63 if (qemuio_blk) {
64 error_report("file open already, try 'help close'");
65 QDECREF(opts);
66 return 1;
69 if (force_share) {
70 if (!opts) {
71 opts = qdict_new();
73 if (qdict_haskey(opts, BDRV_OPT_FORCE_SHARE)
74 && !qdict_get_bool(opts, BDRV_OPT_FORCE_SHARE)) {
75 error_report("-U conflicts with image options");
76 QDECREF(opts);
77 return 1;
79 qdict_put_bool(opts, BDRV_OPT_FORCE_SHARE, true);
81 qemuio_blk = blk_new_open(name, NULL, opts, flags, &local_err);
82 if (!qemuio_blk) {
83 error_reportf_err(local_err, "can't open%s%s: ",
84 name ? " device " : "", name ?: "");
85 return 1;
88 blk_set_enable_write_cache(qemuio_blk, !writethrough);
90 return 0;
93 static void open_help(void)
95 printf(
96 "\n"
97 " opens a new file in the requested mode\n"
98 "\n"
99 " Example:\n"
100 " 'open -n -o driver=raw /tmp/data' - opens raw data file read-write, uncached\n"
101 "\n"
102 " Opens a file for subsequent use by all of the other qemu-io commands.\n"
103 " -r, -- open file read-only\n"
104 " -s, -- use snapshot file\n"
105 " -C, -- use copy-on-read\n"
106 " -n, -- disable host cache, short for -t none\n"
107 " -U, -- force shared permissions\n"
108 " -k, -- use kernel AIO implementation (on Linux only)\n"
109 " -t, -- use the given cache mode for the image\n"
110 " -d, -- use the given discard mode for the image\n"
111 " -o, -- options to be given to the block driver"
112 "\n");
115 static int open_f(BlockBackend *blk, int argc, char **argv);
117 static const cmdinfo_t open_cmd = {
118 .name = "open",
119 .altname = "o",
120 .cfunc = open_f,
121 .argmin = 1,
122 .argmax = -1,
123 .flags = CMD_NOFILE_OK,
124 .args = "[-rsCnkU] [-t cache] [-d discard] [-o options] [path]",
125 .oneline = "open the file specified by path",
126 .help = open_help,
129 static QemuOptsList empty_opts = {
130 .name = "drive",
131 .merge_lists = true,
132 .head = QTAILQ_HEAD_INITIALIZER(empty_opts.head),
133 .desc = {
134 /* no elements => accept any params */
135 { /* end of list */ }
139 static int open_f(BlockBackend *blk, int argc, char **argv)
141 int flags = BDRV_O_UNMAP;
142 int readonly = 0;
143 bool writethrough = true;
144 int c;
145 QemuOpts *qopts;
146 QDict *opts;
147 bool force_share = false;
149 while ((c = getopt(argc, argv, "snCro:kt:d:U")) != -1) {
150 switch (c) {
151 case 's':
152 flags |= BDRV_O_SNAPSHOT;
153 break;
154 case 'n':
155 flags |= BDRV_O_NOCACHE;
156 writethrough = false;
157 break;
158 case 'C':
159 flags |= BDRV_O_COPY_ON_READ;
160 break;
161 case 'r':
162 readonly = 1;
163 break;
164 case 'k':
165 flags |= BDRV_O_NATIVE_AIO;
166 break;
167 case 't':
168 if (bdrv_parse_cache_mode(optarg, &flags, &writethrough) < 0) {
169 error_report("Invalid cache option: %s", optarg);
170 qemu_opts_reset(&empty_opts);
171 return 0;
173 break;
174 case 'd':
175 if (bdrv_parse_discard_flags(optarg, &flags) < 0) {
176 error_report("Invalid discard option: %s", optarg);
177 qemu_opts_reset(&empty_opts);
178 return 0;
180 break;
181 case 'o':
182 if (imageOpts) {
183 printf("--image-opts and 'open -o' are mutually exclusive\n");
184 qemu_opts_reset(&empty_opts);
185 return 0;
187 if (!qemu_opts_parse_noisily(&empty_opts, optarg, false)) {
188 qemu_opts_reset(&empty_opts);
189 return 0;
191 break;
192 case 'U':
193 force_share = true;
194 break;
195 default:
196 qemu_opts_reset(&empty_opts);
197 return qemuio_command_usage(&open_cmd);
201 if (!readonly) {
202 flags |= BDRV_O_RDWR;
205 if (imageOpts && (optind == argc - 1)) {
206 if (!qemu_opts_parse_noisily(&empty_opts, argv[optind], false)) {
207 qemu_opts_reset(&empty_opts);
208 return 0;
210 optind++;
213 qopts = qemu_opts_find(&empty_opts, NULL);
214 opts = qopts ? qemu_opts_to_qdict(qopts, NULL) : NULL;
215 qemu_opts_reset(&empty_opts);
217 if (optind == argc - 1) {
218 openfile(argv[optind], flags, writethrough, force_share, opts);
219 } else if (optind == argc) {
220 openfile(NULL, flags, writethrough, force_share, opts);
221 } else {
222 QDECREF(opts);
223 qemuio_command_usage(&open_cmd);
225 return 0;
228 static int quit_f(BlockBackend *blk, int argc, char **argv)
230 return 1;
233 #if defined(CONFIG_FVD)
234 # include "qemu-io-sim.c"
235 #endif
237 static const cmdinfo_t quit_cmd = {
238 .name = "quit",
239 .altname = "q",
240 .cfunc = quit_f,
241 .argmin = -1,
242 .argmax = -1,
243 .flags = CMD_FLAG_GLOBAL,
244 .oneline = "exit the program",
247 static void usage(const char *name)
249 printf(
250 "Usage: %s [OPTIONS]... [-c STRING]... [file]\n"
251 "QEMU Disk exerciser\n"
252 "\n"
253 " --object OBJECTDEF define an object such as 'secret' for\n"
254 " passwords and/or encryption keys\n"
255 " --image-opts treat file as option string\n"
256 " -c, --cmd STRING execute command with its arguments\n"
257 " from the given string\n"
258 " -f, --format FMT specifies the block driver to use\n"
259 " -r, --read-only export read-only\n"
260 " -s, --snapshot use snapshot file\n"
261 " -n, --nocache disable host cache, short for -t none\n"
262 " -C, --copy-on-read enable copy-on-read\n"
263 " -m, --misalign misalign allocations for O_DIRECT\n"
264 " -k, --native-aio use kernel AIO implementation (on Linux only)\n"
265 " -t, --cache=MODE use the given cache mode for the image\n"
266 " -d, --discard=MODE use the given discard mode for the image\n"
267 " -T, --trace [[enable=]<pattern>][,events=<file>][,file=<file>]\n"
268 " specify tracing options\n"
269 " see qemu-img(1) man page for full description\n"
270 " -U, --force-share force shared permissions\n"
271 " -h, --help display this help and exit\n"
272 " -V, --version output version information and exit\n"
273 "\n"
274 "See '%s -c help' for information on available commands.\n"
275 "\n"
276 QEMU_HELP_BOTTOM "\n",
277 name, name);
280 static char *get_prompt(void)
282 static char prompt[FILENAME_MAX + 2 /*"> "*/ + 1 /*"\0"*/ ];
284 if (!prompt[0]) {
285 snprintf(prompt, sizeof(prompt), "%s> ", progname);
288 return prompt;
291 static void GCC_FMT_ATTR(2, 3) readline_printf_func(void *opaque,
292 const char *fmt, ...)
294 va_list ap;
295 va_start(ap, fmt);
296 vprintf(fmt, ap);
297 va_end(ap);
300 static void readline_flush_func(void *opaque)
302 fflush(stdout);
305 static void readline_func(void *opaque, const char *str, void *readline_opaque)
307 char **line = readline_opaque;
308 *line = g_strdup(str);
311 static void completion_match(const char *cmd, void *opaque)
313 readline_add_completion(readline_state, cmd);
316 static void readline_completion_func(void *opaque, const char *str)
318 readline_set_completion_index(readline_state, strlen(str));
319 qemuio_complete_command(str, completion_match, NULL);
322 static char *fetchline_readline(void)
324 char *line = NULL;
326 readline_start(readline_state, get_prompt(), 0, readline_func, &line);
327 while (!line) {
328 int ch = getchar();
329 if (ch == EOF) {
330 break;
332 readline_handle_byte(readline_state, ch);
334 return line;
337 #define MAXREADLINESZ 1024
338 static char *fetchline_fgets(void)
340 char *p, *line = g_malloc(MAXREADLINESZ);
342 if (!fgets(line, MAXREADLINESZ, stdin)) {
343 g_free(line);
344 return NULL;
347 p = line + strlen(line);
348 if (p != line && p[-1] == '\n') {
349 p[-1] = '\0';
352 return line;
355 static char *fetchline(void)
357 if (readline_state) {
358 return fetchline_readline();
359 } else {
360 return fetchline_fgets();
364 static void prep_fetchline(void *opaque)
366 int *fetchable = opaque;
368 qemu_set_fd_handler(STDIN_FILENO, NULL, NULL, NULL);
369 *fetchable= 1;
372 static void command_loop(void)
374 int i, done = 0, fetchable = 0, prompted = 0;
375 char *input;
377 for (i = 0; !done && i < ncmdline; i++) {
378 done = qemuio_command(qemuio_blk, cmdline[i]);
380 if (cmdline) {
381 g_free(cmdline);
382 return;
385 while (!done) {
386 if (!prompted) {
387 printf("%s", get_prompt());
388 fflush(stdout);
389 qemu_set_fd_handler(STDIN_FILENO, prep_fetchline, NULL, &fetchable);
390 prompted = 1;
393 main_loop_wait(false);
395 if (!fetchable) {
396 continue;
399 input = fetchline();
400 if (input == NULL) {
401 break;
403 done = qemuio_command(qemuio_blk, input);
404 g_free(input);
406 prompted = 0;
407 fetchable = 0;
409 qemu_set_fd_handler(STDIN_FILENO, NULL, NULL, NULL);
412 static void add_user_command(char *optarg)
414 cmdline = g_renew(char *, cmdline, ++ncmdline);
415 cmdline[ncmdline-1] = optarg;
418 static void reenable_tty_echo(void)
420 qemu_set_tty_echo(STDIN_FILENO, true);
423 enum {
424 OPTION_OBJECT = 256,
425 OPTION_IMAGE_OPTS = 257,
428 static QemuOptsList qemu_object_opts = {
429 .name = "object",
430 .implied_opt_name = "qom-type",
431 .head = QTAILQ_HEAD_INITIALIZER(qemu_object_opts.head),
432 .desc = {
438 static QemuOptsList file_opts = {
439 .name = "file",
440 .implied_opt_name = "file",
441 .head = QTAILQ_HEAD_INITIALIZER(file_opts.head),
442 .desc = {
443 /* no elements => accept any params */
444 { /* end of list */ }
448 int main(int argc, char **argv)
450 int readonly = 0;
451 const char *sopt = "hVc:d:f:rsnCmkt:T:U";
452 const struct option lopt[] = {
453 { "help", no_argument, NULL, 'h' },
454 { "version", no_argument, NULL, 'V' },
455 { "cmd", required_argument, NULL, 'c' },
456 { "format", required_argument, NULL, 'f' },
457 { "read-only", no_argument, NULL, 'r' },
458 { "snapshot", no_argument, NULL, 's' },
459 { "nocache", no_argument, NULL, 'n' },
460 { "copy-on-read", no_argument, NULL, 'C' },
461 { "misalign", no_argument, NULL, 'm' },
462 { "native-aio", no_argument, NULL, 'k' },
463 { "discard", required_argument, NULL, 'd' },
464 { "cache", required_argument, NULL, 't' },
465 { "trace", required_argument, NULL, 'T' },
466 { "object", required_argument, NULL, OPTION_OBJECT },
467 { "image-opts", no_argument, NULL, OPTION_IMAGE_OPTS },
468 { "force-share", no_argument, 0, 'U'},
469 { NULL, 0, NULL, 0 }
471 int c;
472 int opt_index = 0;
473 int flags = BDRV_O_UNMAP;
474 bool writethrough = true;
475 Error *local_error = NULL;
476 QDict *opts = NULL;
477 const char *format = NULL;
478 char *trace_file = NULL;
479 bool force_share = false;
481 #ifdef CONFIG_POSIX
482 signal(SIGPIPE, SIG_IGN);
483 #endif
485 module_call_init(MODULE_INIT_TRACE);
486 progname = basename(argv[0]);
487 qemu_init_exec_dir(argv[0]);
489 qcrypto_init(&error_fatal);
491 module_call_init(MODULE_INIT_QOM);
492 qemu_add_opts(&qemu_object_opts);
493 qemu_add_opts(&qemu_trace_opts);
494 bdrv_init();
496 while ((c = getopt_long(argc, argv, sopt, lopt, &opt_index)) != -1) {
497 switch (c) {
498 case 's':
499 flags |= BDRV_O_SNAPSHOT;
500 break;
501 case 'n':
502 flags |= BDRV_O_NOCACHE;
503 writethrough = false;
504 break;
505 case 'C':
506 flags |= BDRV_O_COPY_ON_READ;
507 break;
508 case 'd':
509 if (bdrv_parse_discard_flags(optarg, &flags) < 0) {
510 error_report("Invalid discard option: %s", optarg);
511 exit(1);
513 break;
514 case 'f':
515 format = optarg;
516 break;
517 case 'c':
518 add_user_command(optarg);
519 break;
520 case 'r':
521 readonly = 1;
522 break;
523 case 'm':
524 qemuio_misalign = true;
525 break;
526 case 'k':
527 flags |= BDRV_O_NATIVE_AIO;
528 break;
529 case 't':
530 if (bdrv_parse_cache_mode(optarg, &flags, &writethrough) < 0) {
531 error_report("Invalid cache option: %s", optarg);
532 exit(1);
534 break;
535 case 'T':
536 g_free(trace_file);
537 trace_file = trace_opt_parse(optarg);
538 break;
539 case 'V':
540 printf("%s version " QEMU_VERSION QEMU_PKGVERSION "\n"
541 QEMU_COPYRIGHT "\n", progname);
542 exit(0);
543 case 'h':
544 usage(progname);
545 exit(0);
546 case 'U':
547 force_share = true;
548 break;
549 case OPTION_OBJECT: {
550 QemuOpts *qopts;
551 qopts = qemu_opts_parse_noisily(&qemu_object_opts,
552 optarg, true);
553 if (!qopts) {
554 exit(1);
556 } break;
557 case OPTION_IMAGE_OPTS:
558 imageOpts = true;
559 break;
560 default:
561 usage(progname);
562 exit(1);
566 if ((argc - optind) > 1) {
567 usage(progname);
568 exit(1);
571 if (format && imageOpts) {
572 error_report("--image-opts and -f are mutually exclusive");
573 exit(1);
576 if (qemu_init_main_loop(&local_error)) {
577 error_report_err(local_error);
578 exit(1);
581 if (qemu_opts_foreach(&qemu_object_opts,
582 user_creatable_add_opts_foreach,
583 NULL, NULL)) {
584 exit(1);
587 if (!trace_init_backends()) {
588 exit(1);
590 trace_init_file(trace_file);
591 qemu_set_log(LOG_TRACE);
593 /* initialize commands */
594 qemuio_add_command(&quit_cmd);
595 qemuio_add_command(&open_cmd);
596 qemuio_add_command(&close_cmd);
598 if (isatty(STDIN_FILENO)) {
599 readline_state = readline_init(readline_printf_func,
600 readline_flush_func,
601 NULL,
602 readline_completion_func);
603 qemu_set_tty_echo(STDIN_FILENO, false);
604 atexit(reenable_tty_echo);
607 /* open the device */
608 if (!readonly) {
609 flags |= BDRV_O_RDWR;
612 if ((argc - optind) == 1) {
613 if (imageOpts) {
614 QemuOpts *qopts = NULL;
615 qopts = qemu_opts_parse_noisily(&file_opts, argv[optind], false);
616 if (!qopts) {
617 exit(1);
619 opts = qemu_opts_to_qdict(qopts, NULL);
620 if (openfile(NULL, flags, writethrough, force_share, opts)) {
621 exit(1);
623 } else {
624 if (format) {
625 opts = qdict_new();
626 qdict_put_str(opts, "driver", format);
628 if (openfile(argv[optind], flags, writethrough,
629 force_share, opts)) {
630 exit(1);
634 command_loop();
637 * Make sure all outstanding requests complete before the program exits.
639 bdrv_drain_all();
641 blk_unref(qemuio_blk);
642 g_free(readline_state);
643 return 0;