target/ppc: Avoid taking "env" parameter to mmu-hash64 functions
[qemu.git] / qemu-io.c
blobe692c555e05d7fac139c0f0c0bd1fae1c9afcf01
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 "qapi/error.h"
19 #include "qemu-io.h"
20 #include "qemu/error-report.h"
21 #include "qemu/main-loop.h"
22 #include "qemu/option.h"
23 #include "qemu/config-file.h"
24 #include "qemu/readline.h"
25 #include "qemu/log.h"
26 #include "qapi/qmp/qstring.h"
27 #include "qapi/qmp/qdict.h"
28 #include "qom/object_interfaces.h"
29 #include "sysemu/block-backend.h"
30 #include "block/block_int.h"
31 #include "trace/control.h"
32 #include "crypto/init.h"
33 #include "qemu-version.h"
35 #define CMD_NOFILE_OK 0x01
37 static char *progname;
39 static BlockBackend *qemuio_blk;
41 /* qemu-io commands passed using -c */
42 static int ncmdline;
43 static char **cmdline;
44 static bool imageOpts;
46 static ReadLineState *readline_state;
48 static int ttyEOF;
50 static int get_eof_char(void)
52 #ifdef _WIN32
53 return 0x4; /* Ctrl-D */
54 #else
55 struct termios tty;
56 if (tcgetattr(STDIN_FILENO, &tty) != 0) {
57 if (errno == ENOTTY) {
58 return 0x0; /* just expect read() == 0 */
59 } else {
60 return 0x4; /* Ctrl-D */
64 return tty.c_cc[VEOF];
65 #endif
68 static int close_f(BlockBackend *blk, int argc, char **argv)
70 blk_unref(qemuio_blk);
71 qemuio_blk = NULL;
72 return 0;
75 static const cmdinfo_t close_cmd = {
76 .name = "close",
77 .altname = "c",
78 .cfunc = close_f,
79 .oneline = "close the current open file",
82 static int openfile(char *name, int flags, bool writethrough, bool force_share,
83 QDict *opts)
85 Error *local_err = NULL;
87 if (qemuio_blk) {
88 error_report("file open already, try 'help close'");
89 QDECREF(opts);
90 return 1;
93 if (force_share) {
94 if (!opts) {
95 opts = qdict_new();
97 if (qdict_haskey(opts, BDRV_OPT_FORCE_SHARE)
98 && !qdict_get_bool(opts, BDRV_OPT_FORCE_SHARE)) {
99 error_report("-U conflicts with image options");
100 QDECREF(opts);
101 return 1;
103 qdict_put_bool(opts, BDRV_OPT_FORCE_SHARE, true);
105 qemuio_blk = blk_new_open(name, NULL, opts, flags, &local_err);
106 if (!qemuio_blk) {
107 error_reportf_err(local_err, "can't open%s%s: ",
108 name ? " device " : "", name ?: "");
109 return 1;
112 blk_set_enable_write_cache(qemuio_blk, !writethrough);
114 return 0;
117 static void open_help(void)
119 printf(
120 "\n"
121 " opens a new file in the requested mode\n"
122 "\n"
123 " Example:\n"
124 " 'open -n -o driver=raw /tmp/data' - opens raw data file read-write, uncached\n"
125 "\n"
126 " Opens a file for subsequent use by all of the other qemu-io commands.\n"
127 " -r, -- open file read-only\n"
128 " -s, -- use snapshot file\n"
129 " -C, -- use copy-on-read\n"
130 " -n, -- disable host cache, short for -t none\n"
131 " -U, -- force shared permissions\n"
132 " -k, -- use kernel AIO implementation (on Linux only)\n"
133 " -t, -- use the given cache mode for the image\n"
134 " -d, -- use the given discard mode for the image\n"
135 " -o, -- options to be given to the block driver"
136 "\n");
139 static int open_f(BlockBackend *blk, int argc, char **argv);
141 static const cmdinfo_t open_cmd = {
142 .name = "open",
143 .altname = "o",
144 .cfunc = open_f,
145 .argmin = 1,
146 .argmax = -1,
147 .flags = CMD_NOFILE_OK,
148 .args = "[-rsCnkU] [-t cache] [-d discard] [-o options] [path]",
149 .oneline = "open the file specified by path",
150 .help = open_help,
153 static QemuOptsList empty_opts = {
154 .name = "drive",
155 .merge_lists = true,
156 .head = QTAILQ_HEAD_INITIALIZER(empty_opts.head),
157 .desc = {
158 /* no elements => accept any params */
159 { /* end of list */ }
163 static int open_f(BlockBackend *blk, int argc, char **argv)
165 int flags = BDRV_O_UNMAP;
166 int readonly = 0;
167 bool writethrough = true;
168 int c;
169 QemuOpts *qopts;
170 QDict *opts;
171 bool force_share = false;
173 while ((c = getopt(argc, argv, "snCro:kt:d:U")) != -1) {
174 switch (c) {
175 case 's':
176 flags |= BDRV_O_SNAPSHOT;
177 break;
178 case 'n':
179 flags |= BDRV_O_NOCACHE;
180 writethrough = false;
181 break;
182 case 'C':
183 flags |= BDRV_O_COPY_ON_READ;
184 break;
185 case 'r':
186 readonly = 1;
187 break;
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);
195 return 0;
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);
202 return 0;
204 break;
205 case 'o':
206 if (imageOpts) {
207 printf("--image-opts and 'open -o' are mutually exclusive\n");
208 qemu_opts_reset(&empty_opts);
209 return 0;
211 if (!qemu_opts_parse_noisily(&empty_opts, optarg, false)) {
212 qemu_opts_reset(&empty_opts);
213 return 0;
215 break;
216 case 'U':
217 force_share = true;
218 break;
219 default:
220 qemu_opts_reset(&empty_opts);
221 return qemuio_command_usage(&open_cmd);
225 if (!readonly) {
226 flags |= BDRV_O_RDWR;
229 if (imageOpts && (optind == argc - 1)) {
230 if (!qemu_opts_parse_noisily(&empty_opts, argv[optind], false)) {
231 qemu_opts_reset(&empty_opts);
232 return 0;
234 optind++;
237 qopts = qemu_opts_find(&empty_opts, NULL);
238 opts = qopts ? qemu_opts_to_qdict(qopts, NULL) : NULL;
239 qemu_opts_reset(&empty_opts);
241 if (optind == argc - 1) {
242 openfile(argv[optind], flags, writethrough, force_share, opts);
243 } else if (optind == argc) {
244 openfile(NULL, flags, writethrough, force_share, opts);
245 } else {
246 QDECREF(opts);
247 qemuio_command_usage(&open_cmd);
249 return 0;
252 static int quit_f(BlockBackend *blk, int argc, char **argv)
254 return 1;
257 static const cmdinfo_t quit_cmd = {
258 .name = "quit",
259 .altname = "q",
260 .cfunc = quit_f,
261 .argmin = -1,
262 .argmax = -1,
263 .flags = CMD_FLAG_GLOBAL,
264 .oneline = "exit the program",
267 static void usage(const char *name)
269 printf(
270 "Usage: %s [OPTIONS]... [-c STRING]... [file]\n"
271 "QEMU Disk exerciser\n"
272 "\n"
273 " --object OBJECTDEF define an object such as 'secret' for\n"
274 " passwords and/or encryption keys\n"
275 " --image-opts treat file as option string\n"
276 " -c, --cmd STRING execute command with its arguments\n"
277 " from the given string\n"
278 " -f, --format FMT specifies the block driver to use\n"
279 " -r, --read-only export read-only\n"
280 " -s, --snapshot use snapshot file\n"
281 " -n, --nocache disable host cache, short for -t none\n"
282 " -C, --copy-on-read enable copy-on-read\n"
283 " -m, --misalign misalign allocations for O_DIRECT\n"
284 " -k, --native-aio use kernel AIO implementation (on Linux only)\n"
285 " -t, --cache=MODE use the given cache mode for the image\n"
286 " -d, --discard=MODE use the given discard mode for the image\n"
287 " -T, --trace [[enable=]<pattern>][,events=<file>][,file=<file>]\n"
288 " specify tracing options\n"
289 " see qemu-img(1) man page for full description\n"
290 " -U, --force-share force shared permissions\n"
291 " -h, --help display this help and exit\n"
292 " -V, --version output version information and exit\n"
293 "\n"
294 "See '%s -c help' for information on available commands.\n"
295 "\n"
296 QEMU_HELP_BOTTOM "\n",
297 name, name);
300 static char *get_prompt(void)
302 static char prompt[FILENAME_MAX + 2 /*"> "*/ + 1 /*"\0"*/ ];
304 if (!prompt[0]) {
305 snprintf(prompt, sizeof(prompt), "%s> ", progname);
308 return prompt;
311 static void GCC_FMT_ATTR(2, 3) readline_printf_func(void *opaque,
312 const char *fmt, ...)
314 va_list ap;
315 va_start(ap, fmt);
316 vprintf(fmt, ap);
317 va_end(ap);
320 static void readline_flush_func(void *opaque)
322 fflush(stdout);
325 static void readline_func(void *opaque, const char *str, void *readline_opaque)
327 char **line = readline_opaque;
328 *line = g_strdup(str);
331 static void completion_match(const char *cmd, void *opaque)
333 readline_add_completion(readline_state, cmd);
336 static void readline_completion_func(void *opaque, const char *str)
338 readline_set_completion_index(readline_state, strlen(str));
339 qemuio_complete_command(str, completion_match, NULL);
342 static char *fetchline_readline(void)
344 char *line = NULL;
346 readline_start(readline_state, get_prompt(), 0, readline_func, &line);
347 while (!line) {
348 int ch = getchar();
349 if (ttyEOF != 0x0 && ch == ttyEOF) {
350 printf("\n");
351 break;
353 readline_handle_byte(readline_state, ch);
355 return line;
358 #define MAXREADLINESZ 1024
359 static char *fetchline_fgets(void)
361 char *p, *line = g_malloc(MAXREADLINESZ);
363 if (!fgets(line, MAXREADLINESZ, stdin)) {
364 g_free(line);
365 return NULL;
368 p = line + strlen(line);
369 if (p != line && p[-1] == '\n') {
370 p[-1] = '\0';
373 return line;
376 static char *fetchline(void)
378 if (readline_state) {
379 return fetchline_readline();
380 } else {
381 return fetchline_fgets();
385 static void prep_fetchline(void *opaque)
387 int *fetchable = opaque;
389 qemu_set_fd_handler(STDIN_FILENO, NULL, NULL, NULL);
390 *fetchable= 1;
393 static void command_loop(void)
395 int i, done = 0, fetchable = 0, prompted = 0;
396 char *input;
398 for (i = 0; !done && i < ncmdline; i++) {
399 done = qemuio_command(qemuio_blk, cmdline[i]);
401 if (cmdline) {
402 g_free(cmdline);
403 return;
406 while (!done) {
407 if (!prompted) {
408 printf("%s", get_prompt());
409 fflush(stdout);
410 qemu_set_fd_handler(STDIN_FILENO, prep_fetchline, NULL, &fetchable);
411 prompted = 1;
414 main_loop_wait(false);
416 if (!fetchable) {
417 continue;
420 input = fetchline();
421 if (input == NULL) {
422 break;
424 done = qemuio_command(qemuio_blk, input);
425 g_free(input);
427 prompted = 0;
428 fetchable = 0;
430 qemu_set_fd_handler(STDIN_FILENO, NULL, NULL, NULL);
433 static void add_user_command(char *optarg)
435 cmdline = g_renew(char *, cmdline, ++ncmdline);
436 cmdline[ncmdline-1] = optarg;
439 static void reenable_tty_echo(void)
441 qemu_set_tty_echo(STDIN_FILENO, true);
444 enum {
445 OPTION_OBJECT = 256,
446 OPTION_IMAGE_OPTS = 257,
449 static QemuOptsList qemu_object_opts = {
450 .name = "object",
451 .implied_opt_name = "qom-type",
452 .head = QTAILQ_HEAD_INITIALIZER(qemu_object_opts.head),
453 .desc = {
459 static QemuOptsList file_opts = {
460 .name = "file",
461 .implied_opt_name = "file",
462 .head = QTAILQ_HEAD_INITIALIZER(file_opts.head),
463 .desc = {
464 /* no elements => accept any params */
465 { /* end of list */ }
469 int main(int argc, char **argv)
471 int readonly = 0;
472 const char *sopt = "hVc:d:f:rsnCmkt:T:U";
473 const struct option lopt[] = {
474 { "help", no_argument, NULL, 'h' },
475 { "version", no_argument, NULL, 'V' },
476 { "cmd", required_argument, NULL, 'c' },
477 { "format", required_argument, NULL, 'f' },
478 { "read-only", no_argument, NULL, 'r' },
479 { "snapshot", no_argument, NULL, 's' },
480 { "nocache", no_argument, NULL, 'n' },
481 { "copy-on-read", no_argument, NULL, 'C' },
482 { "misalign", no_argument, NULL, 'm' },
483 { "native-aio", no_argument, NULL, 'k' },
484 { "discard", required_argument, NULL, 'd' },
485 { "cache", required_argument, NULL, 't' },
486 { "trace", required_argument, NULL, 'T' },
487 { "object", required_argument, NULL, OPTION_OBJECT },
488 { "image-opts", no_argument, NULL, OPTION_IMAGE_OPTS },
489 { "force-share", no_argument, 0, 'U'},
490 { NULL, 0, NULL, 0 }
492 int c;
493 int opt_index = 0;
494 int flags = BDRV_O_UNMAP;
495 bool writethrough = true;
496 Error *local_error = NULL;
497 QDict *opts = NULL;
498 const char *format = NULL;
499 char *trace_file = NULL;
500 bool force_share = false;
502 #ifdef CONFIG_POSIX
503 signal(SIGPIPE, SIG_IGN);
504 #endif
506 module_call_init(MODULE_INIT_TRACE);
507 progname = g_path_get_basename(argv[0]);
508 qemu_init_exec_dir(argv[0]);
510 qcrypto_init(&error_fatal);
512 module_call_init(MODULE_INIT_QOM);
513 qemu_add_opts(&qemu_object_opts);
514 qemu_add_opts(&qemu_trace_opts);
515 bdrv_init();
517 while ((c = getopt_long(argc, argv, sopt, lopt, &opt_index)) != -1) {
518 switch (c) {
519 case 's':
520 flags |= BDRV_O_SNAPSHOT;
521 break;
522 case 'n':
523 flags |= BDRV_O_NOCACHE;
524 writethrough = false;
525 break;
526 case 'C':
527 flags |= BDRV_O_COPY_ON_READ;
528 break;
529 case 'd':
530 if (bdrv_parse_discard_flags(optarg, &flags) < 0) {
531 error_report("Invalid discard option: %s", optarg);
532 exit(1);
534 break;
535 case 'f':
536 format = optarg;
537 break;
538 case 'c':
539 add_user_command(optarg);
540 break;
541 case 'r':
542 readonly = 1;
543 break;
544 case 'm':
545 qemuio_misalign = true;
546 break;
547 case 'k':
548 flags |= BDRV_O_NATIVE_AIO;
549 break;
550 case 't':
551 if (bdrv_parse_cache_mode(optarg, &flags, &writethrough) < 0) {
552 error_report("Invalid cache option: %s", optarg);
553 exit(1);
555 break;
556 case 'T':
557 g_free(trace_file);
558 trace_file = trace_opt_parse(optarg);
559 break;
560 case 'V':
561 printf("%s version " QEMU_FULL_VERSION "\n"
562 QEMU_COPYRIGHT "\n", progname);
563 exit(0);
564 case 'h':
565 usage(progname);
566 exit(0);
567 case 'U':
568 force_share = true;
569 break;
570 case OPTION_OBJECT: {
571 QemuOpts *qopts;
572 qopts = qemu_opts_parse_noisily(&qemu_object_opts,
573 optarg, true);
574 if (!qopts) {
575 exit(1);
577 } break;
578 case OPTION_IMAGE_OPTS:
579 imageOpts = true;
580 break;
581 default:
582 usage(progname);
583 exit(1);
587 if ((argc - optind) > 1) {
588 usage(progname);
589 exit(1);
592 if (format && imageOpts) {
593 error_report("--image-opts and -f are mutually exclusive");
594 exit(1);
597 if (qemu_init_main_loop(&local_error)) {
598 error_report_err(local_error);
599 exit(1);
602 if (qemu_opts_foreach(&qemu_object_opts,
603 user_creatable_add_opts_foreach,
604 NULL, NULL)) {
605 exit(1);
608 if (!trace_init_backends()) {
609 exit(1);
611 trace_init_file(trace_file);
612 qemu_set_log(LOG_TRACE);
614 /* initialize commands */
615 qemuio_add_command(&quit_cmd);
616 qemuio_add_command(&open_cmd);
617 qemuio_add_command(&close_cmd);
619 if (isatty(STDIN_FILENO)) {
620 ttyEOF = get_eof_char();
621 readline_state = readline_init(readline_printf_func,
622 readline_flush_func,
623 NULL,
624 readline_completion_func);
625 qemu_set_tty_echo(STDIN_FILENO, false);
626 atexit(reenable_tty_echo);
629 /* open the device */
630 if (!readonly) {
631 flags |= BDRV_O_RDWR;
634 if ((argc - optind) == 1) {
635 if (imageOpts) {
636 QemuOpts *qopts = NULL;
637 qopts = qemu_opts_parse_noisily(&file_opts, argv[optind], false);
638 if (!qopts) {
639 exit(1);
641 opts = qemu_opts_to_qdict(qopts, NULL);
642 if (openfile(NULL, flags, writethrough, force_share, opts)) {
643 exit(1);
645 } else {
646 if (format) {
647 opts = qdict_new();
648 qdict_put_str(opts, "driver", format);
650 if (openfile(argv[optind], flags, writethrough,
651 force_share, opts)) {
652 exit(1);
656 command_loop();
659 * Make sure all outstanding requests complete before the program exits.
661 bdrv_drain_all();
663 blk_unref(qemuio_blk);
664 g_free(readline_state);
665 return 0;