Merge remote-tracking branch 'qemu/master'
[qemu/ar7.git] / qemu-io.c
blobd224668941556f361c53ccd719de9221573a2732
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 <sys/time.h>
11 #include <sys/types.h>
12 #include <stdarg.h>
13 #include <stdio.h>
14 #include <getopt.h>
15 #include <libgen.h>
17 #include "qemu-io.h"
18 #include "qemu/main-loop.h"
19 #include "qemu/option.h"
20 #include "qemu/config-file.h"
21 #include "qemu/readline.h"
22 #include "sysemu/block-backend.h"
23 #include "block/block_int.h"
24 #include "trace/control.h"
26 #define CMD_NOFILE_OK 0x01
28 static char *progname;
30 static BlockBackend *qemuio_blk;
32 /* qemu-io commands passed using -c */
33 static int ncmdline;
34 static char **cmdline;
36 static ReadLineState *readline_state;
38 static int close_f(BlockBackend *blk, int argc, char **argv)
40 blk_unref(qemuio_blk);
41 qemuio_blk = NULL;
42 return 0;
45 static const cmdinfo_t close_cmd = {
46 .name = "close",
47 .altname = "c",
48 .cfunc = close_f,
49 .oneline = "close the current open file",
52 static int openfile(char *name, int flags, QDict *opts)
54 Error *local_err = NULL;
56 if (qemuio_blk) {
57 fprintf(stderr, "file open already, try 'help close'\n");
58 QDECREF(opts);
59 return 1;
62 qemuio_blk = blk_new_open("hda", name, NULL, opts, flags, &local_err);
63 if (!qemuio_blk) {
64 fprintf(stderr, "%s: can't open%s%s: %s\n", progname,
65 name ? " device " : "", name ?: "",
66 error_get_pretty(local_err));
67 error_free(local_err);
68 return 1;
71 return 0;
74 static void open_help(void)
76 printf(
77 "\n"
78 " opens a new file in the requested mode\n"
79 "\n"
80 " Example:\n"
81 " 'open -Cn /tmp/data' - creates/opens data file read-write and uncached\n"
82 "\n"
83 " Opens a file for subsequent use by all of the other qemu-io commands.\n"
84 " -r, -- open file read-only\n"
85 " -s, -- use snapshot file\n"
86 " -n, -- disable host cache\n"
87 " -o, -- options to be given to the block driver"
88 "\n");
91 static int open_f(BlockBackend *blk, int argc, char **argv);
93 static const cmdinfo_t open_cmd = {
94 .name = "open",
95 .altname = "o",
96 .cfunc = open_f,
97 .argmin = 1,
98 .argmax = -1,
99 .flags = CMD_NOFILE_OK,
100 .args = "[-Crsn] [-o options] [path]",
101 .oneline = "open the file specified by path",
102 .help = open_help,
105 static QemuOptsList empty_opts = {
106 .name = "drive",
107 .merge_lists = true,
108 .head = QTAILQ_HEAD_INITIALIZER(empty_opts.head),
109 .desc = {
110 /* no elements => accept any params */
111 { /* end of list */ }
115 static int open_f(BlockBackend *blk, int argc, char **argv)
117 int flags = 0;
118 int readonly = 0;
119 int c;
120 QemuOpts *qopts;
121 QDict *opts;
123 while ((c = getopt(argc, argv, "snrgo:")) != EOF) {
124 switch (c) {
125 case 's':
126 flags |= BDRV_O_SNAPSHOT;
127 break;
128 case 'n':
129 flags |= BDRV_O_NOCACHE | BDRV_O_CACHE_WB;
130 break;
131 case 'r':
132 readonly = 1;
133 break;
134 case 'o':
135 if (!qemu_opts_parse(&empty_opts, optarg, 0)) {
136 printf("could not parse option list -- %s\n", optarg);
137 qemu_opts_reset(&empty_opts);
138 return 0;
140 break;
141 default:
142 qemu_opts_reset(&empty_opts);
143 return qemuio_command_usage(&open_cmd);
147 if (!readonly) {
148 flags |= BDRV_O_RDWR;
151 qopts = qemu_opts_find(&empty_opts, NULL);
152 opts = qopts ? qemu_opts_to_qdict(qopts, NULL) : NULL;
153 qemu_opts_reset(&empty_opts);
155 if (optind == argc - 1) {
156 return openfile(argv[optind], flags, opts);
157 } else if (optind == argc) {
158 return openfile(NULL, flags, opts);
159 } else {
160 QDECREF(opts);
161 return qemuio_command_usage(&open_cmd);
165 static int quit_f(BlockBackend *blk, int argc, char **argv)
167 return 1;
170 #if defined(CONFIG_FVD)
171 # include "qemu-io-sim.c"
172 #endif
174 static const cmdinfo_t quit_cmd = {
175 .name = "quit",
176 .altname = "q",
177 .cfunc = quit_f,
178 .argmin = -1,
179 .argmax = -1,
180 .flags = CMD_FLAG_GLOBAL,
181 .oneline = "exit the program",
184 static void usage(const char *name)
186 printf(
187 "Usage: %s [-h] [-V] [-rsnm] [-f FMT] [-c STRING] ... [file]\n"
188 "QEMU Disk exerciser\n"
189 "\n"
190 " -c, --cmd STRING execute command with its arguments\n"
191 " from the given string\n"
192 " -f, --format FMT specifies the block driver to use\n"
193 " -r, --read-only export read-only\n"
194 " -s, --snapshot use snapshot file\n"
195 " -n, --nocache disable host cache\n"
196 " -m, --misalign misalign allocations for O_DIRECT\n"
197 " -k, --native-aio use kernel AIO implementation (on Linux only)\n"
198 " -t, --cache=MODE use the given cache mode for the image\n"
199 " -T, --trace FILE enable trace events listed in the given file\n"
200 " -h, --help display this help and exit\n"
201 " -V, --version output version information and exit\n"
202 "\n"
203 "See '%s -c help' for information on available commands."
204 "\n",
205 name, name);
208 static char *get_prompt(void)
210 static char prompt[FILENAME_MAX + 2 /*"> "*/ + 1 /*"\0"*/ ];
212 if (!prompt[0]) {
213 snprintf(prompt, sizeof(prompt), "%s> ", progname);
216 return prompt;
219 static void GCC_FMT_ATTR(2, 3) readline_printf_func(void *opaque,
220 const char *fmt, ...)
222 va_list ap;
223 va_start(ap, fmt);
224 vprintf(fmt, ap);
225 va_end(ap);
228 static void readline_flush_func(void *opaque)
230 fflush(stdout);
233 static void readline_func(void *opaque, const char *str, void *readline_opaque)
235 char **line = readline_opaque;
236 *line = g_strdup(str);
239 static void completion_match(const char *cmd, void *opaque)
241 readline_add_completion(readline_state, cmd);
244 static void readline_completion_func(void *opaque, const char *str)
246 readline_set_completion_index(readline_state, strlen(str));
247 qemuio_complete_command(str, completion_match, NULL);
250 static char *fetchline_readline(void)
252 char *line = NULL;
254 readline_start(readline_state, get_prompt(), 0, readline_func, &line);
255 while (!line) {
256 int ch = getchar();
257 if (ch == EOF) {
258 break;
260 readline_handle_byte(readline_state, ch);
262 return line;
265 #define MAXREADLINESZ 1024
266 static char *fetchline_fgets(void)
268 char *p, *line = g_malloc(MAXREADLINESZ);
270 if (!fgets(line, MAXREADLINESZ, stdin)) {
271 g_free(line);
272 return NULL;
275 p = line + strlen(line);
276 if (p != line && p[-1] == '\n') {
277 p[-1] = '\0';
280 return line;
283 static char *fetchline(void)
285 if (readline_state) {
286 return fetchline_readline();
287 } else {
288 return fetchline_fgets();
292 static void prep_fetchline(void *opaque)
294 int *fetchable = opaque;
296 qemu_set_fd_handler(STDIN_FILENO, NULL, NULL, NULL);
297 *fetchable= 1;
300 static void command_loop(void)
302 int i, done = 0, fetchable = 0, prompted = 0;
303 char *input;
305 for (i = 0; !done && i < ncmdline; i++) {
306 done = qemuio_command(qemuio_blk, cmdline[i]);
308 if (cmdline) {
309 g_free(cmdline);
310 return;
313 while (!done) {
314 if (!prompted) {
315 printf("%s", get_prompt());
316 fflush(stdout);
317 qemu_set_fd_handler(STDIN_FILENO, prep_fetchline, NULL, &fetchable);
318 prompted = 1;
321 main_loop_wait(false);
323 if (!fetchable) {
324 continue;
327 input = fetchline();
328 if (input == NULL) {
329 break;
331 done = qemuio_command(qemuio_blk, input);
332 g_free(input);
334 prompted = 0;
335 fetchable = 0;
337 qemu_set_fd_handler(STDIN_FILENO, NULL, NULL, NULL);
340 static void add_user_command(char *optarg)
342 cmdline = g_renew(char *, cmdline, ++ncmdline);
343 cmdline[ncmdline-1] = optarg;
346 static void reenable_tty_echo(void)
348 qemu_set_tty_echo(STDIN_FILENO, true);
351 int main(int argc, char **argv)
353 int readonly = 0;
354 const char *sopt = "hVc:d:f:rsnmgkt:T:";
355 const struct option lopt[] = {
356 { "help", 0, NULL, 'h' },
357 { "version", 0, NULL, 'V' },
358 { "offset", 1, NULL, 'o' },
359 { "cmd", 1, NULL, 'c' },
360 { "format", 1, NULL, 'f' },
361 { "read-only", 0, NULL, 'r' },
362 { "snapshot", 0, NULL, 's' },
363 { "nocache", 0, NULL, 'n' },
364 { "misalign", 0, NULL, 'm' },
365 { "native-aio", 0, NULL, 'k' },
366 { "discard", 1, NULL, 'd' },
367 { "cache", 1, NULL, 't' },
368 { "trace", 1, NULL, 'T' },
369 { NULL, 0, NULL, 0 }
371 int c;
372 int opt_index = 0;
373 int flags = BDRV_O_UNMAP;
374 Error *local_error = NULL;
375 QDict *opts = NULL;
377 #ifdef CONFIG_POSIX
378 signal(SIGPIPE, SIG_IGN);
379 #endif
381 progname = basename(argv[0]);
382 qemu_init_exec_dir(argv[0]);
384 bdrv_init();
386 while ((c = getopt_long(argc, argv, sopt, lopt, &opt_index)) != -1) {
387 switch (c) {
388 case 's':
389 flags |= BDRV_O_SNAPSHOT;
390 break;
391 case 'n':
392 flags |= BDRV_O_NOCACHE | BDRV_O_CACHE_WB;
393 break;
394 case 'd':
395 if (bdrv_parse_discard_flags(optarg, &flags) < 0) {
396 error_report("Invalid discard option: %s", optarg);
397 exit(1);
399 break;
400 case 'f':
401 if (!opts) {
402 opts = qdict_new();
404 qdict_put(opts, "driver", qstring_from_str(optarg));
405 break;
406 case 'c':
407 add_user_command(optarg);
408 break;
409 case 'r':
410 readonly = 1;
411 break;
412 case 'm':
413 qemuio_misalign = true;
414 break;
415 case 'k':
416 flags |= BDRV_O_NATIVE_AIO;
417 break;
418 case 't':
419 if (bdrv_parse_cache_flags(optarg, &flags) < 0) {
420 error_report("Invalid cache option: %s", optarg);
421 exit(1);
423 break;
424 case 'T':
425 if (!trace_init_backends(optarg, NULL)) {
426 exit(1); /* error message will have been printed */
428 break;
429 case 'V':
430 printf("%s version %s\n", progname, QEMU_VERSION);
431 exit(0);
432 case 'h':
433 usage(progname);
434 exit(0);
435 default:
436 usage(progname);
437 exit(1);
441 if ((argc - optind) > 1) {
442 usage(progname);
443 exit(1);
446 if (qemu_init_main_loop(&local_error)) {
447 error_report_err(local_error);
448 exit(1);
451 /* initialize commands */
452 qemuio_add_command(&quit_cmd);
453 qemuio_add_command(&open_cmd);
454 qemuio_add_command(&close_cmd);
456 if (isatty(STDIN_FILENO)) {
457 readline_state = readline_init(readline_printf_func,
458 readline_flush_func,
459 NULL,
460 readline_completion_func);
461 qemu_set_tty_echo(STDIN_FILENO, false);
462 atexit(reenable_tty_echo);
465 /* open the device */
466 if (!readonly) {
467 flags |= BDRV_O_RDWR;
470 if ((argc - optind) == 1) {
471 openfile(argv[optind], flags, opts);
473 command_loop();
476 * Make sure all outstanding requests complete before the program exits.
478 bdrv_drain_all();
480 blk_unref(qemuio_blk);
481 g_free(readline_state);
482 return 0;