nsis: Remove duplicate installation of documentation
[qemu/ar7.git] / qemu-io.c
blob193950f1348ab760286edc372bce05293a1c72d2
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 "qemu/sockets.h"
29 #include "qapi/qmp/qstring.h"
30 #include "qapi/qmp/qdict.h"
31 #include "qom/object_interfaces.h"
32 #include "sysemu/block-backend.h"
33 #include "block/block_int.h"
34 #include "trace/control.h"
35 #include "crypto/init.h"
36 #include "qemu-version.h"
38 #define CMD_NOFILE_OK 0x01
40 static BlockBackend *qemuio_blk;
41 static bool quit_qemu_io;
43 /* qemu-io commands passed using -c */
44 static int ncmdline;
45 static char **cmdline;
46 static bool imageOpts;
48 static ReadLineState *readline_state;
50 static int ttyEOF;
52 static int get_eof_char(void)
54 #ifdef _WIN32
55 return 0x4; /* Ctrl-D */
56 #else
57 struct termios tty;
58 if (tcgetattr(STDIN_FILENO, &tty) != 0) {
59 if (errno == ENOTTY) {
60 return 0x0; /* just expect read() == 0 */
61 } else {
62 return 0x4; /* Ctrl-D */
66 return tty.c_cc[VEOF];
67 #endif
70 static int close_f(BlockBackend *blk, int argc, char **argv)
72 blk_unref(qemuio_blk);
73 qemuio_blk = NULL;
74 return 0;
77 static const cmdinfo_t close_cmd = {
78 .name = "close",
79 .altname = "c",
80 .cfunc = close_f,
81 .oneline = "close the current open file",
84 static int openfile(char *name, int flags, bool writethrough, bool force_share,
85 QDict *opts)
87 Error *local_err = NULL;
89 if (qemuio_blk) {
90 error_report("file open already, try 'help close'");
91 qobject_unref(opts);
92 return 1;
95 if (force_share) {
96 if (!opts) {
97 opts = qdict_new();
99 if (qdict_haskey(opts, BDRV_OPT_FORCE_SHARE)
100 && strcmp(qdict_get_str(opts, BDRV_OPT_FORCE_SHARE), "on")) {
101 error_report("-U conflicts with image options");
102 qobject_unref(opts);
103 return 1;
105 qdict_put_str(opts, BDRV_OPT_FORCE_SHARE, "on");
107 qemuio_blk = blk_new_open(name, NULL, opts, flags, &local_err);
108 if (!qemuio_blk) {
109 error_reportf_err(local_err, "can't open%s%s: ",
110 name ? " device " : "", name ?: "");
111 return 1;
114 blk_set_enable_write_cache(qemuio_blk, !writethrough);
116 return 0;
119 static void open_help(void)
121 printf(
122 "\n"
123 " opens a new file in the requested mode\n"
124 "\n"
125 " Example:\n"
126 " 'open -n -o driver=raw /tmp/data' - opens raw data file read-write, uncached\n"
127 "\n"
128 " Opens a file for subsequent use by all of the other qemu-io commands.\n"
129 " -r, -- open file read-only\n"
130 " -s, -- use snapshot file\n"
131 " -C, -- use copy-on-read\n"
132 " -n, -- disable host cache, short for -t none\n"
133 " -U, -- force shared permissions\n"
134 " -k, -- use kernel AIO implementation (Linux only, prefer use of -i)\n"
135 " -i, -- use AIO mode (threads, native or io_uring)\n"
136 " -t, -- use the given cache mode for the image\n"
137 " -d, -- use the given discard mode for the image\n"
138 " -o, -- options to be given to the block driver"
139 "\n");
142 static int open_f(BlockBackend *blk, int argc, char **argv);
144 static const cmdinfo_t open_cmd = {
145 .name = "open",
146 .altname = "o",
147 .cfunc = open_f,
148 .argmin = 1,
149 .argmax = -1,
150 .flags = CMD_NOFILE_OK,
151 .args = "[-rsCnkU] [-t cache] [-d discard] [-o options] [path]",
152 .oneline = "open the file specified by path",
153 .help = open_help,
156 static QemuOptsList empty_opts = {
157 .name = "drive",
158 .merge_lists = true,
159 .head = QTAILQ_HEAD_INITIALIZER(empty_opts.head),
160 .desc = {
161 /* no elements => accept any params */
162 { /* end of list */ }
166 static int open_f(BlockBackend *blk, int argc, char **argv)
168 int flags = BDRV_O_UNMAP;
169 int readonly = 0;
170 bool writethrough = true;
171 int c;
172 int ret;
173 QemuOpts *qopts;
174 QDict *opts;
175 bool force_share = false;
177 while ((c = getopt(argc, argv, "snCro:ki:t:d:U")) != -1) {
178 switch (c) {
179 case 's':
180 flags |= BDRV_O_SNAPSHOT;
181 break;
182 case 'n':
183 flags |= BDRV_O_NOCACHE;
184 writethrough = false;
185 break;
186 case 'C':
187 flags |= BDRV_O_COPY_ON_READ;
188 break;
189 case 'r':
190 readonly = 1;
191 break;
192 case 'k':
193 flags |= BDRV_O_NATIVE_AIO;
194 break;
195 case 't':
196 if (bdrv_parse_cache_mode(optarg, &flags, &writethrough) < 0) {
197 error_report("Invalid cache option: %s", optarg);
198 qemu_opts_reset(&empty_opts);
199 return -EINVAL;
201 break;
202 case 'd':
203 if (bdrv_parse_discard_flags(optarg, &flags) < 0) {
204 error_report("Invalid discard option: %s", optarg);
205 qemu_opts_reset(&empty_opts);
206 return -EINVAL;
208 break;
209 case 'i':
210 if (bdrv_parse_aio(optarg, &flags) < 0) {
211 error_report("Invalid aio option: %s", optarg);
212 qemu_opts_reset(&empty_opts);
213 return -EINVAL;
215 break;
216 case 'o':
217 if (imageOpts) {
218 printf("--image-opts and 'open -o' are mutually exclusive\n");
219 qemu_opts_reset(&empty_opts);
220 return -EINVAL;
222 if (!qemu_opts_parse_noisily(&empty_opts, optarg, false)) {
223 qemu_opts_reset(&empty_opts);
224 return -EINVAL;
226 break;
227 case 'U':
228 force_share = true;
229 break;
230 default:
231 qemu_opts_reset(&empty_opts);
232 qemuio_command_usage(&open_cmd);
233 return -EINVAL;
237 if (!readonly) {
238 flags |= BDRV_O_RDWR;
241 if (imageOpts && (optind == argc - 1)) {
242 if (!qemu_opts_parse_noisily(&empty_opts, argv[optind], false)) {
243 qemu_opts_reset(&empty_opts);
244 return -EINVAL;
246 optind++;
249 qopts = qemu_opts_find(&empty_opts, NULL);
250 opts = qopts ? qemu_opts_to_qdict(qopts, NULL) : NULL;
251 qemu_opts_reset(&empty_opts);
253 if (optind == argc - 1) {
254 ret = openfile(argv[optind], flags, writethrough, force_share, opts);
255 } else if (optind == argc) {
256 ret = openfile(NULL, flags, writethrough, force_share, opts);
257 } else {
258 qobject_unref(opts);
259 qemuio_command_usage(&open_cmd);
260 return -EINVAL;
263 if (ret) {
264 return -EINVAL;
267 return 0;
270 static int quit_f(BlockBackend *blk, int argc, char **argv)
272 quit_qemu_io = true;
273 return 0;
276 #if defined(CONFIG_FVD)
277 # include "qemu-io-sim.c"
278 #endif
280 static const cmdinfo_t quit_cmd = {
281 .name = "quit",
282 .altname = "q",
283 .cfunc = quit_f,
284 .argmin = -1,
285 .argmax = -1,
286 .flags = CMD_FLAG_GLOBAL,
287 .oneline = "exit the program",
290 static void usage(const char *name)
292 printf(
293 "Usage: %s [OPTIONS]... [-c STRING]... [file]\n"
294 "QEMU Disk exerciser\n"
295 "\n"
296 " --object OBJECTDEF define an object such as 'secret' for\n"
297 " passwords and/or encryption keys\n"
298 " --image-opts treat file as option string\n"
299 " -c, --cmd STRING execute command with its arguments\n"
300 " from the given string\n"
301 " -f, --format FMT specifies the block driver to use\n"
302 " -r, --read-only export read-only\n"
303 " -s, --snapshot use snapshot file\n"
304 " -n, --nocache disable host cache, short for -t none\n"
305 " -C, --copy-on-read enable copy-on-read\n"
306 " -m, --misalign misalign allocations for O_DIRECT\n"
307 " -k, --native-aio use kernel AIO implementation\n"
308 " (Linux only, prefer use of -i)\n"
309 " -i, --aio=MODE use AIO mode (threads, native or io_uring)\n"
310 " -t, --cache=MODE use the given cache mode for the image\n"
311 " -d, --discard=MODE use the given discard mode for the image\n"
312 " -T, --trace [[enable=]<pattern>][,events=<file>][,file=<file>]\n"
313 " specify tracing options\n"
314 " see qemu-img(1) man page for full description\n"
315 " -U, --force-share force shared permissions\n"
316 " -h, --help display this help and exit\n"
317 " -V, --version output version information and exit\n"
318 "\n"
319 "See '%s -c help' for information on available commands.\n"
320 "\n"
321 QEMU_HELP_BOTTOM "\n",
322 name, name);
325 static char *get_prompt(void)
327 static char prompt[FILENAME_MAX + 2 /*"> "*/ + 1 /*"\0"*/ ];
329 if (!prompt[0]) {
330 snprintf(prompt, sizeof(prompt), "%s> ", error_get_progname());
333 return prompt;
336 static void GCC_FMT_ATTR(2, 3) readline_printf_func(void *opaque,
337 const char *fmt, ...)
339 va_list ap;
340 va_start(ap, fmt);
341 vprintf(fmt, ap);
342 va_end(ap);
345 static void readline_flush_func(void *opaque)
347 fflush(stdout);
350 static void readline_func(void *opaque, const char *str, void *readline_opaque)
352 char **line = readline_opaque;
353 *line = g_strdup(str);
356 static void completion_match(const char *cmd, void *opaque)
358 readline_add_completion(readline_state, cmd);
361 static void readline_completion_func(void *opaque, const char *str)
363 readline_set_completion_index(readline_state, strlen(str));
364 qemuio_complete_command(str, completion_match, NULL);
367 static char *fetchline_readline(void)
369 char *line = NULL;
371 readline_start(readline_state, get_prompt(), 0, readline_func, &line);
372 while (!line) {
373 int ch = getchar();
374 if (ttyEOF != 0x0 && ch == ttyEOF) {
375 printf("\n");
376 break;
378 readline_handle_byte(readline_state, ch);
380 return line;
383 #define MAXREADLINESZ 1024
384 static char *fetchline_fgets(void)
386 char *p, *line = g_malloc(MAXREADLINESZ);
388 if (!fgets(line, MAXREADLINESZ, stdin)) {
389 g_free(line);
390 return NULL;
393 p = line + strlen(line);
394 if (p != line && p[-1] == '\n') {
395 p[-1] = '\0';
398 return line;
401 static char *fetchline(void)
403 if (readline_state) {
404 return fetchline_readline();
405 } else {
406 return fetchline_fgets();
410 static void prep_fetchline(void *opaque)
412 int *fetchable = opaque;
414 qemu_set_fd_handler(STDIN_FILENO, NULL, NULL, NULL);
415 *fetchable= 1;
418 static int command_loop(void)
420 int i, fetchable = 0, prompted = 0;
421 int ret, last_error = 0;
422 char *input;
424 for (i = 0; !quit_qemu_io && i < ncmdline; i++) {
425 ret = qemuio_command(qemuio_blk, cmdline[i]);
426 if (ret < 0) {
427 last_error = ret;
430 if (cmdline) {
431 g_free(cmdline);
432 return last_error;
435 while (!quit_qemu_io) {
436 if (!prompted) {
437 printf("%s", get_prompt());
438 fflush(stdout);
439 qemu_set_fd_handler(STDIN_FILENO, prep_fetchline, NULL, &fetchable);
440 prompted = 1;
443 main_loop_wait(false);
445 if (!fetchable) {
446 continue;
449 input = fetchline();
450 if (input == NULL) {
451 break;
453 ret = qemuio_command(qemuio_blk, input);
454 g_free(input);
456 if (ret < 0) {
457 last_error = ret;
460 prompted = 0;
461 fetchable = 0;
463 qemu_set_fd_handler(STDIN_FILENO, NULL, NULL, NULL);
465 return last_error;
468 static void add_user_command(char *optarg)
470 cmdline = g_renew(char *, cmdline, ++ncmdline);
471 cmdline[ncmdline-1] = optarg;
474 static void reenable_tty_echo(void)
476 qemu_set_tty_echo(STDIN_FILENO, true);
479 enum {
480 OPTION_OBJECT = 256,
481 OPTION_IMAGE_OPTS = 257,
484 static QemuOptsList file_opts = {
485 .name = "file",
486 .implied_opt_name = "file",
487 .head = QTAILQ_HEAD_INITIALIZER(file_opts.head),
488 .desc = {
489 /* no elements => accept any params */
490 { /* end of list */ }
494 int main(int argc, char **argv)
496 int readonly = 0;
497 const char *sopt = "hVc:d:f:rsnCmki:t:T:U";
498 const struct option lopt[] = {
499 { "help", no_argument, NULL, 'h' },
500 { "version", no_argument, NULL, 'V' },
501 { "cmd", required_argument, NULL, 'c' },
502 { "format", required_argument, NULL, 'f' },
503 { "read-only", no_argument, NULL, 'r' },
504 { "snapshot", no_argument, NULL, 's' },
505 { "nocache", no_argument, NULL, 'n' },
506 { "copy-on-read", no_argument, NULL, 'C' },
507 { "misalign", no_argument, NULL, 'm' },
508 { "native-aio", no_argument, NULL, 'k' },
509 { "aio", required_argument, NULL, 'i' },
510 { "discard", required_argument, NULL, 'd' },
511 { "cache", required_argument, NULL, 't' },
512 { "trace", required_argument, NULL, 'T' },
513 { "object", required_argument, NULL, OPTION_OBJECT },
514 { "image-opts", no_argument, NULL, OPTION_IMAGE_OPTS },
515 { "force-share", no_argument, 0, 'U'},
516 { NULL, 0, NULL, 0 }
518 int c;
519 int opt_index = 0;
520 int flags = BDRV_O_UNMAP;
521 int ret;
522 bool writethrough = true;
523 Error *local_error = NULL;
524 QDict *opts = NULL;
525 const char *format = NULL;
526 bool force_share = false;
528 #ifdef CONFIG_POSIX
529 signal(SIGPIPE, SIG_IGN);
530 #endif
532 socket_init();
533 error_init(argv[0]);
534 module_call_init(MODULE_INIT_TRACE);
535 qemu_init_exec_dir(argv[0]);
537 qcrypto_init(&error_fatal);
539 module_call_init(MODULE_INIT_QOM);
540 qemu_add_opts(&qemu_trace_opts);
541 bdrv_init();
543 while ((c = getopt_long(argc, argv, sopt, lopt, &opt_index)) != -1) {
544 switch (c) {
545 case 's':
546 flags |= BDRV_O_SNAPSHOT;
547 break;
548 case 'n':
549 flags |= BDRV_O_NOCACHE;
550 writethrough = false;
551 break;
552 case 'C':
553 flags |= BDRV_O_COPY_ON_READ;
554 break;
555 case 'd':
556 if (bdrv_parse_discard_flags(optarg, &flags) < 0) {
557 error_report("Invalid discard option: %s", optarg);
558 exit(1);
560 break;
561 case 'f':
562 format = optarg;
563 break;
564 case 'c':
565 add_user_command(optarg);
566 break;
567 case 'r':
568 readonly = 1;
569 break;
570 case 'm':
571 qemuio_misalign = true;
572 break;
573 case 'k':
574 flags |= BDRV_O_NATIVE_AIO;
575 break;
576 case 'i':
577 if (bdrv_parse_aio(optarg, &flags) < 0) {
578 error_report("Invalid aio option: %s", optarg);
579 exit(1);
581 break;
582 case 't':
583 if (bdrv_parse_cache_mode(optarg, &flags, &writethrough) < 0) {
584 error_report("Invalid cache option: %s", optarg);
585 exit(1);
587 break;
588 case 'T':
589 trace_opt_parse(optarg);
590 break;
591 case 'V':
592 printf("%s version " QEMU_FULL_VERSION "\n"
593 QEMU_COPYRIGHT "\n", error_get_progname());
594 exit(0);
595 case 'h':
596 usage(error_get_progname());
597 exit(0);
598 case 'U':
599 force_share = true;
600 break;
601 case OPTION_OBJECT:
602 user_creatable_process_cmdline(optarg);
603 break;
604 case OPTION_IMAGE_OPTS:
605 imageOpts = true;
606 break;
607 default:
608 usage(error_get_progname());
609 exit(1);
613 if ((argc - optind) > 1) {
614 usage(error_get_progname());
615 exit(1);
618 if (format && imageOpts) {
619 error_report("--image-opts and -f are mutually exclusive");
620 exit(1);
623 if (qemu_init_main_loop(&local_error)) {
624 error_report_err(local_error);
625 exit(1);
628 if (!trace_init_backends()) {
629 exit(1);
631 trace_init_file();
632 qemu_set_log(LOG_TRACE);
634 /* initialize commands */
635 qemuio_add_command(&quit_cmd);
636 qemuio_add_command(&open_cmd);
637 qemuio_add_command(&close_cmd);
639 if (isatty(STDIN_FILENO)) {
640 ttyEOF = get_eof_char();
641 readline_state = readline_init(readline_printf_func,
642 readline_flush_func,
643 NULL,
644 readline_completion_func);
645 qemu_set_tty_echo(STDIN_FILENO, false);
646 atexit(reenable_tty_echo);
649 /* open the device */
650 if (!readonly) {
651 flags |= BDRV_O_RDWR;
654 if ((argc - optind) == 1) {
655 if (imageOpts) {
656 QemuOpts *qopts = NULL;
657 qopts = qemu_opts_parse_noisily(&file_opts, argv[optind], false);
658 if (!qopts) {
659 exit(1);
661 opts = qemu_opts_to_qdict(qopts, NULL);
662 if (openfile(NULL, flags, writethrough, force_share, opts)) {
663 exit(1);
665 } else {
666 if (format) {
667 opts = qdict_new();
668 qdict_put_str(opts, "driver", format);
670 if (openfile(argv[optind], flags, writethrough,
671 force_share, opts)) {
672 exit(1);
676 ret = command_loop();
679 * Make sure all outstanding requests complete before the program exits.
681 bdrv_drain_all();
683 blk_unref(qemuio_blk);
684 g_free(readline_state);
686 if (ret < 0) {
687 return 1;
688 } else {
689 return 0;