Add more GCC_FMT_ATTR
[qemu/ar7.git] / qemu-io.c
blob837c9783b440578a7688c885112ce0b8a1f7df77
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 */
11 #include "qemu/osdep.h"
12 #include <getopt.h>
13 #include <libgen.h>
14 #ifndef _WIN32
15 #include <termios.h>
16 #endif
18 #include "qemu-common.h"
19 #include "qapi/error.h"
20 #include "qemu-io.h"
21 #include "qemu/error-report.h"
22 #include "qemu/main-loop.h"
23 #include "qemu/module.h"
24 #include "qemu/option.h"
25 #include "qemu/config-file.h"
26 #include "qemu/readline.h"
27 #include "qemu/log.h"
28 #include "qapi/qmp/qstring.h"
29 #include "qapi/qmp/qdict.h"
30 #include "qom/object_interfaces.h"
31 #include "sysemu/block-backend.h"
32 #include "block/block_int.h"
33 #include "trace/control.h"
34 #include "crypto/init.h"
35 #include "qemu-version.h"
37 #define CMD_NOFILE_OK 0x01
39 static BlockBackend *qemuio_blk;
40 static bool quit_qemu_io;
42 /* qemu-io commands passed using -c */
43 static int ncmdline;
44 static char **cmdline;
45 static bool imageOpts;
47 static ReadLineState *readline_state;
49 static int ttyEOF;
51 static int get_eof_char(void)
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 */
65 return tty.c_cc[VEOF];
66 #endif
69 static int close_f(BlockBackend *blk, int argc, char **argv)
71 blk_unref(qemuio_blk);
72 qemuio_blk = NULL;
73 return 0;
76 static const cmdinfo_t close_cmd = {
77 .name = "close",
78 .altname = "c",
79 .cfunc = close_f,
80 .oneline = "close the current open file",
83 static int openfile(char *name, int flags, bool writethrough, bool force_share,
84 QDict *opts)
86 Error *local_err = NULL;
88 if (qemuio_blk) {
89 error_report("file open already, try 'help close'");
90 qobject_unref(opts);
91 return 1;
94 if (force_share) {
95 if (!opts) {
96 opts = qdict_new();
98 if (qdict_haskey(opts, BDRV_OPT_FORCE_SHARE)
99 && strcmp(qdict_get_str(opts, BDRV_OPT_FORCE_SHARE), "on")) {
100 error_report("-U conflicts with image options");
101 qobject_unref(opts);
102 return 1;
104 qdict_put_str(opts, BDRV_OPT_FORCE_SHARE, "on");
106 qemuio_blk = blk_new_open(name, NULL, opts, flags, &local_err);
107 if (!qemuio_blk) {
108 error_reportf_err(local_err, "can't open%s%s: ",
109 name ? " device " : "", name ?: "");
110 return 1;
113 blk_set_enable_write_cache(qemuio_blk, !writethrough);
115 return 0;
118 static void open_help(void)
120 printf(
121 "\n"
122 " opens a new file in the requested mode\n"
123 "\n"
124 " Example:\n"
125 " 'open -n -o driver=raw /tmp/data' - opens raw data file read-write, uncached\n"
126 "\n"
127 " Opens a file for subsequent use by all of the other qemu-io commands.\n"
128 " -r, -- open file read-only\n"
129 " -s, -- use snapshot file\n"
130 " -C, -- use copy-on-read\n"
131 " -n, -- disable host cache, short for -t none\n"
132 " -U, -- force shared permissions\n"
133 " -k, -- use kernel AIO implementation (Linux only, prefer use of -i)\n"
134 " -i, -- use AIO mode (threads, native or io_uring)\n"
135 " -t, -- use the given cache mode for the image\n"
136 " -d, -- use the given discard mode for the image\n"
137 " -o, -- options to be given to the block driver"
138 "\n");
141 static int open_f(BlockBackend *blk, int argc, char **argv);
143 static const cmdinfo_t open_cmd = {
144 .name = "open",
145 .altname = "o",
146 .cfunc = open_f,
147 .argmin = 1,
148 .argmax = -1,
149 .flags = CMD_NOFILE_OK,
150 .args = "[-rsCnkU] [-t cache] [-d discard] [-o options] [path]",
151 .oneline = "open the file specified by path",
152 .help = open_help,
155 static QemuOptsList empty_opts = {
156 .name = "drive",
157 .merge_lists = true,
158 .head = QTAILQ_HEAD_INITIALIZER(empty_opts.head),
159 .desc = {
160 /* no elements => accept any params */
161 { /* end of list */ }
165 static int open_f(BlockBackend *blk, int argc, char **argv)
167 int flags = BDRV_O_UNMAP;
168 int readonly = 0;
169 bool writethrough = true;
170 int c;
171 int ret;
172 QemuOpts *qopts;
173 QDict *opts;
174 bool force_share = false;
176 while ((c = getopt(argc, argv, "snCro:ki:t:d:U")) != -1) {
177 switch (c) {
178 case 's':
179 flags |= BDRV_O_SNAPSHOT;
180 break;
181 case 'n':
182 flags |= BDRV_O_NOCACHE;
183 writethrough = false;
184 break;
185 case 'C':
186 flags |= BDRV_O_COPY_ON_READ;
187 break;
188 case 'r':
189 readonly = 1;
190 break;
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);
198 return -EINVAL;
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);
205 return -EINVAL;
207 break;
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;
214 break;
215 case 'o':
216 if (imageOpts) {
217 printf("--image-opts and 'open -o' are mutually exclusive\n");
218 qemu_opts_reset(&empty_opts);
219 return -EINVAL;
221 if (!qemu_opts_parse_noisily(&empty_opts, optarg, false)) {
222 qemu_opts_reset(&empty_opts);
223 return -EINVAL;
225 break;
226 case 'U':
227 force_share = true;
228 break;
229 default:
230 qemu_opts_reset(&empty_opts);
231 qemuio_command_usage(&open_cmd);
232 return -EINVAL;
236 if (!readonly) {
237 flags |= BDRV_O_RDWR;
240 if (imageOpts && (optind == argc - 1)) {
241 if (!qemu_opts_parse_noisily(&empty_opts, argv[optind], false)) {
242 qemu_opts_reset(&empty_opts);
243 return -EINVAL;
245 optind++;
248 qopts = qemu_opts_find(&empty_opts, NULL);
249 opts = qopts ? qemu_opts_to_qdict(qopts, NULL) : NULL;
250 qemu_opts_reset(&empty_opts);
252 if (optind == argc - 1) {
253 ret = openfile(argv[optind], flags, writethrough, force_share, opts);
254 } else if (optind == argc) {
255 ret = openfile(NULL, flags, writethrough, force_share, opts);
256 } else {
257 qobject_unref(opts);
258 qemuio_command_usage(&open_cmd);
259 return -EINVAL;
262 if (ret) {
263 return -EINVAL;
266 return 0;
269 static int quit_f(BlockBackend *blk, int argc, char **argv)
271 quit_qemu_io = true;
272 return 0;
275 #if defined(CONFIG_FVD)
276 # include "qemu-io-sim.c"
277 #endif
279 static const cmdinfo_t quit_cmd = {
280 .name = "quit",
281 .altname = "q",
282 .cfunc = quit_f,
283 .argmin = -1,
284 .argmax = -1,
285 .flags = CMD_FLAG_GLOBAL,
286 .oneline = "exit the program",
289 static void usage(const char *name)
291 printf(
292 "Usage: %s [OPTIONS]... [-c STRING]... [file]\n"
293 "QEMU Disk exerciser\n"
294 "\n"
295 " --object OBJECTDEF define an object such as 'secret' for\n"
296 " passwords and/or encryption keys\n"
297 " --image-opts treat file as option string\n"
298 " -c, --cmd STRING execute command with its arguments\n"
299 " from the given string\n"
300 " -f, --format FMT specifies the block driver to use\n"
301 " -r, --read-only export read-only\n"
302 " -s, --snapshot use snapshot file\n"
303 " -n, --nocache disable host cache, short for -t none\n"
304 " -C, --copy-on-read enable copy-on-read\n"
305 " -m, --misalign misalign allocations for O_DIRECT\n"
306 " -k, --native-aio use kernel AIO implementation\n"
307 " (Linux only, prefer use of -i)\n"
308 " -i, --aio=MODE use AIO mode (threads, native or io_uring)\n"
309 " -t, --cache=MODE use the given cache mode for the image\n"
310 " -d, --discard=MODE use the given discard mode for the image\n"
311 " -T, --trace [[enable=]<pattern>][,events=<file>][,file=<file>]\n"
312 " specify tracing options\n"
313 " see qemu-img(1) man page for full description\n"
314 " -U, --force-share force shared permissions\n"
315 " -h, --help display this help and exit\n"
316 " -V, --version output version information and exit\n"
317 "\n"
318 "See '%s -c help' for information on available commands.\n"
319 "\n"
320 QEMU_HELP_BOTTOM "\n",
321 name, name);
324 static char *get_prompt(void)
326 static char prompt[FILENAME_MAX + 2 /*"> "*/ + 1 /*"\0"*/ ];
328 if (!prompt[0]) {
329 snprintf(prompt, sizeof(prompt), "%s> ", error_get_progname());
332 return prompt;
335 static void GCC_FMT_ATTR(2, 3) readline_printf_func(void *opaque,
336 const char *fmt, ...)
338 va_list ap;
339 va_start(ap, fmt);
340 vprintf(fmt, ap);
341 va_end(ap);
344 static void readline_flush_func(void *opaque)
346 fflush(stdout);
349 static void readline_func(void *opaque, const char *str, void *readline_opaque)
351 char **line = readline_opaque;
352 *line = g_strdup(str);
355 static void completion_match(const char *cmd, void *opaque)
357 readline_add_completion(readline_state, cmd);
360 static void readline_completion_func(void *opaque, const char *str)
362 readline_set_completion_index(readline_state, strlen(str));
363 qemuio_complete_command(str, completion_match, NULL);
366 static char *fetchline_readline(void)
368 char *line = NULL;
370 readline_start(readline_state, get_prompt(), 0, readline_func, &line);
371 while (!line) {
372 int ch = getchar();
373 if (ttyEOF != 0x0 && ch == ttyEOF) {
374 printf("\n");
375 break;
377 readline_handle_byte(readline_state, ch);
379 return line;
382 #define MAXREADLINESZ 1024
383 static char *fetchline_fgets(void)
385 char *p, *line = g_malloc(MAXREADLINESZ);
387 if (!fgets(line, MAXREADLINESZ, stdin)) {
388 g_free(line);
389 return NULL;
392 p = line + strlen(line);
393 if (p != line && p[-1] == '\n') {
394 p[-1] = '\0';
397 return line;
400 static char *fetchline(void)
402 if (readline_state) {
403 return fetchline_readline();
404 } else {
405 return fetchline_fgets();
409 static void prep_fetchline(void *opaque)
411 int *fetchable = opaque;
413 qemu_set_fd_handler(STDIN_FILENO, NULL, NULL, NULL);
414 *fetchable= 1;
417 static int command_loop(void)
419 int i, fetchable = 0, prompted = 0;
420 int ret, last_error = 0;
421 char *input;
423 for (i = 0; !quit_qemu_io && i < ncmdline; i++) {
424 ret = qemuio_command(qemuio_blk, cmdline[i]);
425 if (ret < 0) {
426 last_error = ret;
429 if (cmdline) {
430 g_free(cmdline);
431 return last_error;
434 while (!quit_qemu_io) {
435 if (!prompted) {
436 printf("%s", get_prompt());
437 fflush(stdout);
438 qemu_set_fd_handler(STDIN_FILENO, prep_fetchline, NULL, &fetchable);
439 prompted = 1;
442 main_loop_wait(false);
444 if (!fetchable) {
445 continue;
448 input = fetchline();
449 if (input == NULL) {
450 break;
452 ret = qemuio_command(qemuio_blk, input);
453 g_free(input);
455 if (ret < 0) {
456 last_error = ret;
459 prompted = 0;
460 fetchable = 0;
462 qemu_set_fd_handler(STDIN_FILENO, NULL, NULL, NULL);
464 return last_error;
467 static void add_user_command(char *optarg)
469 cmdline = g_renew(char *, cmdline, ++ncmdline);
470 cmdline[ncmdline-1] = optarg;
473 static void reenable_tty_echo(void)
475 qemu_set_tty_echo(STDIN_FILENO, true);
478 enum {
479 OPTION_OBJECT = 256,
480 OPTION_IMAGE_OPTS = 257,
483 static QemuOptsList qemu_object_opts = {
484 .name = "object",
485 .implied_opt_name = "qom-type",
486 .head = QTAILQ_HEAD_INITIALIZER(qemu_object_opts.head),
487 .desc = {
492 static bool qemu_io_object_print_help(const char *type, QemuOpts *opts)
494 if (user_creatable_print_help(type, opts)) {
495 exit(0);
497 return true;
500 static QemuOptsList file_opts = {
501 .name = "file",
502 .implied_opt_name = "file",
503 .head = QTAILQ_HEAD_INITIALIZER(file_opts.head),
504 .desc = {
505 /* no elements => accept any params */
506 { /* end of list */ }
510 int main(int argc, char **argv)
512 int readonly = 0;
513 const char *sopt = "hVc:d:f:rsnCmki:t:T:U";
514 const struct option lopt[] = {
515 { "help", no_argument, NULL, 'h' },
516 { "version", no_argument, NULL, 'V' },
517 { "cmd", required_argument, NULL, 'c' },
518 { "format", required_argument, NULL, 'f' },
519 { "read-only", no_argument, NULL, 'r' },
520 { "snapshot", no_argument, NULL, 's' },
521 { "nocache", no_argument, NULL, 'n' },
522 { "copy-on-read", no_argument, NULL, 'C' },
523 { "misalign", no_argument, NULL, 'm' },
524 { "native-aio", no_argument, NULL, 'k' },
525 { "aio", required_argument, NULL, 'i' },
526 { "discard", required_argument, NULL, 'd' },
527 { "cache", required_argument, NULL, 't' },
528 { "trace", required_argument, NULL, 'T' },
529 { "object", required_argument, NULL, OPTION_OBJECT },
530 { "image-opts", no_argument, NULL, OPTION_IMAGE_OPTS },
531 { "force-share", no_argument, 0, 'U'},
532 { NULL, 0, NULL, 0 }
534 int c;
535 int opt_index = 0;
536 int flags = BDRV_O_UNMAP;
537 int ret;
538 bool writethrough = true;
539 Error *local_error = NULL;
540 QDict *opts = NULL;
541 const char *format = NULL;
542 char *trace_file = NULL;
543 bool force_share = false;
545 #ifdef CONFIG_POSIX
546 signal(SIGPIPE, SIG_IGN);
547 #endif
549 error_init(argv[0]);
550 module_call_init(MODULE_INIT_TRACE);
551 qemu_init_exec_dir(argv[0]);
553 qcrypto_init(&error_fatal);
555 module_call_init(MODULE_INIT_QOM);
556 qemu_add_opts(&qemu_object_opts);
557 qemu_add_opts(&qemu_trace_opts);
558 bdrv_init();
560 while ((c = getopt_long(argc, argv, sopt, lopt, &opt_index)) != -1) {
561 switch (c) {
562 case 's':
563 flags |= BDRV_O_SNAPSHOT;
564 break;
565 case 'n':
566 flags |= BDRV_O_NOCACHE;
567 writethrough = false;
568 break;
569 case 'C':
570 flags |= BDRV_O_COPY_ON_READ;
571 break;
572 case 'd':
573 if (bdrv_parse_discard_flags(optarg, &flags) < 0) {
574 error_report("Invalid discard option: %s", optarg);
575 exit(1);
577 break;
578 case 'f':
579 format = optarg;
580 break;
581 case 'c':
582 add_user_command(optarg);
583 break;
584 case 'r':
585 readonly = 1;
586 break;
587 case 'm':
588 qemuio_misalign = true;
589 break;
590 case 'k':
591 flags |= BDRV_O_NATIVE_AIO;
592 break;
593 case 'i':
594 if (bdrv_parse_aio(optarg, &flags) < 0) {
595 error_report("Invalid aio option: %s", optarg);
596 exit(1);
598 break;
599 case 't':
600 if (bdrv_parse_cache_mode(optarg, &flags, &writethrough) < 0) {
601 error_report("Invalid cache option: %s", optarg);
602 exit(1);
604 break;
605 case 'T':
606 g_free(trace_file);
607 trace_file = trace_opt_parse(optarg);
608 break;
609 case 'V':
610 printf("%s version " QEMU_FULL_VERSION "\n"
611 QEMU_COPYRIGHT "\n", error_get_progname());
612 exit(0);
613 case 'h':
614 usage(error_get_progname());
615 exit(0);
616 case 'U':
617 force_share = true;
618 break;
619 case OPTION_OBJECT: {
620 QemuOpts *qopts;
621 qopts = qemu_opts_parse_noisily(&qemu_object_opts,
622 optarg, true);
623 if (!qopts) {
624 exit(1);
626 } break;
627 case OPTION_IMAGE_OPTS:
628 imageOpts = true;
629 break;
630 default:
631 usage(error_get_progname());
632 exit(1);
636 if ((argc - optind) > 1) {
637 usage(error_get_progname());
638 exit(1);
641 if (format && imageOpts) {
642 error_report("--image-opts and -f are mutually exclusive");
643 exit(1);
646 if (qemu_init_main_loop(&local_error)) {
647 error_report_err(local_error);
648 exit(1);
651 qemu_opts_foreach(&qemu_object_opts,
652 user_creatable_add_opts_foreach,
653 qemu_io_object_print_help, &error_fatal);
655 if (!trace_init_backends()) {
656 exit(1);
658 trace_init_file(trace_file);
659 qemu_set_log(LOG_TRACE);
661 /* initialize commands */
662 qemuio_add_command(&quit_cmd);
663 qemuio_add_command(&open_cmd);
664 qemuio_add_command(&close_cmd);
666 if (isatty(STDIN_FILENO)) {
667 ttyEOF = get_eof_char();
668 readline_state = readline_init(readline_printf_func,
669 readline_flush_func,
670 NULL,
671 readline_completion_func);
672 qemu_set_tty_echo(STDIN_FILENO, false);
673 atexit(reenable_tty_echo);
676 /* open the device */
677 if (!readonly) {
678 flags |= BDRV_O_RDWR;
681 if ((argc - optind) == 1) {
682 if (imageOpts) {
683 QemuOpts *qopts = NULL;
684 qopts = qemu_opts_parse_noisily(&file_opts, argv[optind], false);
685 if (!qopts) {
686 exit(1);
688 opts = qemu_opts_to_qdict(qopts, NULL);
689 if (openfile(NULL, flags, writethrough, force_share, opts)) {
690 exit(1);
692 } else {
693 if (format) {
694 opts = qdict_new();
695 qdict_put_str(opts, "driver", format);
697 if (openfile(argv[optind], flags, writethrough,
698 force_share, opts)) {
699 exit(1);
703 ret = command_loop();
706 * Make sure all outstanding requests complete before the program exits.
708 bdrv_drain_all();
710 blk_unref(qemuio_blk);
711 g_free(readline_state);
713 if (ret < 0) {
714 return 1;
715 } else {
716 return 0;