Merge remote-tracking branch 'qemu/master'
[qemu/ar7.git] / qemu-io.c
blobf177ea3bcd56c6eb549a6f427758019d8705d3ba
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 "block/block_int.h"
23 #include "trace/control.h"
25 #define CMD_NOFILE_OK 0x01
27 static char *progname;
29 static BlockDriverState *qemuio_bs;
31 /* qemu-io commands passed using -c */
32 static int ncmdline;
33 static char **cmdline;
35 static ReadLineState *readline_state;
37 static int close_f(BlockDriverState *bs, int argc, char **argv)
39 bdrv_unref(bs);
40 qemuio_bs = NULL;
41 return 0;
44 static const cmdinfo_t close_cmd = {
45 .name = "close",
46 .altname = "c",
47 .cfunc = close_f,
48 .oneline = "close the current open file",
51 static int openfile(char *name, int flags, int growable, QDict *opts)
53 Error *local_err = NULL;
55 if (qemuio_bs) {
56 fprintf(stderr, "file open already, try 'help close'\n");
57 QDECREF(opts);
58 return 1;
61 if (growable) {
62 if (bdrv_open(&qemuio_bs, name, NULL, opts, flags | BDRV_O_PROTOCOL,
63 NULL, &local_err))
65 fprintf(stderr, "%s: can't open%s%s: %s\n", progname,
66 name ? " device " : "", name ?: "",
67 error_get_pretty(local_err));
68 error_free(local_err);
69 return 1;
71 } else {
72 qemuio_bs = bdrv_new("hda", &error_abort);
74 if (bdrv_open(&qemuio_bs, name, NULL, opts, flags, NULL, &local_err)
75 < 0)
77 fprintf(stderr, "%s: can't open%s%s: %s\n", progname,
78 name ? " device " : "", name ?: "",
79 error_get_pretty(local_err));
80 error_free(local_err);
81 bdrv_unref(qemuio_bs);
82 qemuio_bs = NULL;
83 return 1;
87 return 0;
90 static void open_help(void)
92 printf(
93 "\n"
94 " opens a new file in the requested mode\n"
95 "\n"
96 " Example:\n"
97 " 'open -Cn /tmp/data' - creates/opens data file read-write and uncached\n"
98 "\n"
99 " Opens a file for subsequent use by all of the other qemu-io commands.\n"
100 " -r, -- open file read-only\n"
101 " -s, -- use snapshot file\n"
102 " -n, -- disable host cache\n"
103 " -g, -- allow file to grow (only applies to protocols)\n"
104 " -o, -- options to be given to the block driver"
105 "\n");
108 static int open_f(BlockDriverState *bs, int argc, char **argv);
110 static const cmdinfo_t open_cmd = {
111 .name = "open",
112 .altname = "o",
113 .cfunc = open_f,
114 .argmin = 1,
115 .argmax = -1,
116 .flags = CMD_NOFILE_OK,
117 .args = "[-Crsn] [-o options] [path]",
118 .oneline = "open the file specified by path",
119 .help = open_help,
122 static QemuOptsList empty_opts = {
123 .name = "drive",
124 .merge_lists = true,
125 .head = QTAILQ_HEAD_INITIALIZER(empty_opts.head),
126 .desc = {
127 /* no elements => accept any params */
128 { /* end of list */ }
132 static int open_f(BlockDriverState *bs, int argc, char **argv)
134 int flags = 0;
135 int readonly = 0;
136 int growable = 0;
137 int c;
138 QemuOpts *qopts;
139 QDict *opts;
141 while ((c = getopt(argc, argv, "snrgo:")) != EOF) {
142 switch (c) {
143 case 's':
144 flags |= BDRV_O_SNAPSHOT;
145 break;
146 case 'n':
147 flags |= BDRV_O_NOCACHE | BDRV_O_CACHE_WB;
148 break;
149 case 'r':
150 readonly = 1;
151 break;
152 case 'g':
153 growable = 1;
154 break;
155 case 'o':
156 if (!qemu_opts_parse(&empty_opts, optarg, 0)) {
157 printf("could not parse option list -- %s\n", optarg);
158 qemu_opts_reset(&empty_opts);
159 return 0;
161 break;
162 default:
163 qemu_opts_reset(&empty_opts);
164 return qemuio_command_usage(&open_cmd);
168 if (!readonly) {
169 flags |= BDRV_O_RDWR;
172 qopts = qemu_opts_find(&empty_opts, NULL);
173 opts = qopts ? qemu_opts_to_qdict(qopts, NULL) : NULL;
174 qemu_opts_reset(&empty_opts);
176 if (optind == argc - 1) {
177 return openfile(argv[optind], flags, growable, opts);
178 } else if (optind == argc) {
179 return openfile(NULL, flags, growable, opts);
180 } else {
181 QDECREF(opts);
182 return qemuio_command_usage(&open_cmd);
186 static int quit_f(BlockDriverState *bs, int argc, char **argv)
188 return 1;
191 #if !defined(_WIN32)
192 # include "qemu-io-sim.c"
193 #endif
195 static const cmdinfo_t quit_cmd = {
196 .name = "quit",
197 .altname = "q",
198 .cfunc = quit_f,
199 .argmin = -1,
200 .argmax = -1,
201 .flags = CMD_FLAG_GLOBAL,
202 .oneline = "exit the program",
205 static void usage(const char *name)
207 printf(
208 "Usage: %s [-h] [-V] [-rsnm] [-c STRING] ... [file]\n"
209 "QEMU Disk exerciser\n"
210 "\n"
211 " -c, --cmd STRING execute command with its arguments\n"
212 " from the given string\n"
213 " -r, --read-only export read-only\n"
214 " -s, --snapshot use snapshot file\n"
215 " -n, --nocache disable host cache\n"
216 " -g, --growable allow file to grow (only applies to protocols)\n"
217 " -m, --misalign misalign allocations for O_DIRECT\n"
218 " -k, --native-aio use kernel AIO implementation (on Linux only)\n"
219 " -t, --cache=MODE use the given cache mode for the image\n"
220 " -T, --trace FILE enable trace events listed in the given file\n"
221 " -h, --help display this help and exit\n"
222 " -V, --version output version information and exit\n"
223 "\n"
224 "See '%s -c help' for information on available commands."
225 "\n",
226 name, name);
229 static char *get_prompt(void)
231 static char prompt[FILENAME_MAX + 2 /*"> "*/ + 1 /*"\0"*/ ];
233 if (!prompt[0]) {
234 snprintf(prompt, sizeof(prompt), "%s> ", progname);
237 return prompt;
240 static void GCC_FMT_ATTR(2, 3) readline_printf_func(void *opaque,
241 const char *fmt, ...)
243 va_list ap;
244 va_start(ap, fmt);
245 vprintf(fmt, ap);
246 va_end(ap);
249 static void readline_flush_func(void *opaque)
251 fflush(stdout);
254 static void readline_func(void *opaque, const char *str, void *readline_opaque)
256 char **line = readline_opaque;
257 *line = g_strdup(str);
260 static void completion_match(const char *cmd, void *opaque)
262 readline_add_completion(readline_state, cmd);
265 static void readline_completion_func(void *opaque, const char *str)
267 readline_set_completion_index(readline_state, strlen(str));
268 qemuio_complete_command(str, completion_match, NULL);
271 static char *fetchline_readline(void)
273 char *line = NULL;
275 readline_start(readline_state, get_prompt(), 0, readline_func, &line);
276 while (!line) {
277 int ch = getchar();
278 if (ch == EOF) {
279 break;
281 readline_handle_byte(readline_state, ch);
283 return line;
286 #define MAXREADLINESZ 1024
287 static char *fetchline_fgets(void)
289 char *p, *line = g_malloc(MAXREADLINESZ);
291 if (!fgets(line, MAXREADLINESZ, stdin)) {
292 g_free(line);
293 return NULL;
296 p = line + strlen(line);
297 if (p != line && p[-1] == '\n') {
298 p[-1] = '\0';
301 return line;
304 static char *fetchline(void)
306 if (readline_state) {
307 return fetchline_readline();
308 } else {
309 return fetchline_fgets();
313 static void prep_fetchline(void *opaque)
315 int *fetchable = opaque;
317 qemu_set_fd_handler(STDIN_FILENO, NULL, NULL, NULL);
318 *fetchable= 1;
321 static void command_loop(void)
323 int i, done = 0, fetchable = 0, prompted = 0;
324 char *input;
326 for (i = 0; !done && i < ncmdline; i++) {
327 done = qemuio_command(qemuio_bs, cmdline[i]);
329 if (cmdline) {
330 g_free(cmdline);
331 return;
334 while (!done) {
335 if (!prompted) {
336 printf("%s", get_prompt());
337 fflush(stdout);
338 qemu_set_fd_handler(STDIN_FILENO, prep_fetchline, NULL, &fetchable);
339 prompted = 1;
342 main_loop_wait(false);
344 if (!fetchable) {
345 continue;
348 input = fetchline();
349 if (input == NULL) {
350 break;
352 done = qemuio_command(qemuio_bs, input);
353 g_free(input);
355 prompted = 0;
356 fetchable = 0;
358 qemu_set_fd_handler(STDIN_FILENO, NULL, NULL, NULL);
361 static void add_user_command(char *optarg)
363 cmdline = g_realloc(cmdline, ++ncmdline * sizeof(char *));
364 cmdline[ncmdline-1] = optarg;
367 static void reenable_tty_echo(void)
369 qemu_set_tty_echo(STDIN_FILENO, true);
372 int main(int argc, char **argv)
374 int readonly = 0;
375 int growable = 0;
376 const char *sopt = "hVc:d:rsnmgkt:T:";
377 const struct option lopt[] = {
378 { "help", 0, NULL, 'h' },
379 { "version", 0, NULL, 'V' },
380 { "offset", 1, NULL, 'o' },
381 { "cmd", 1, NULL, 'c' },
382 { "read-only", 0, NULL, 'r' },
383 { "snapshot", 0, NULL, 's' },
384 { "nocache", 0, NULL, 'n' },
385 { "misalign", 0, NULL, 'm' },
386 { "growable", 0, NULL, 'g' },
387 { "native-aio", 0, NULL, 'k' },
388 { "discard", 1, NULL, 'd' },
389 { "cache", 1, NULL, 't' },
390 { "trace", 1, NULL, 'T' },
391 { NULL, 0, NULL, 0 }
393 int c;
394 int opt_index = 0;
395 int flags = BDRV_O_UNMAP;
397 #ifdef CONFIG_POSIX
398 signal(SIGPIPE, SIG_IGN);
399 #endif
401 progname = basename(argv[0]);
402 qemu_init_exec_dir(argv[0]);
404 while ((c = getopt_long(argc, argv, sopt, lopt, &opt_index)) != -1) {
405 switch (c) {
406 case 's':
407 flags |= BDRV_O_SNAPSHOT;
408 break;
409 case 'n':
410 flags |= BDRV_O_NOCACHE | BDRV_O_CACHE_WB;
411 break;
412 case 'd':
413 if (bdrv_parse_discard_flags(optarg, &flags) < 0) {
414 error_report("Invalid discard option: %s", optarg);
415 exit(1);
417 break;
418 case 'c':
419 add_user_command(optarg);
420 break;
421 case 'r':
422 readonly = 1;
423 break;
424 case 'm':
425 qemuio_misalign = true;
426 break;
427 case 'g':
428 growable = 1;
429 break;
430 case 'k':
431 flags |= BDRV_O_NATIVE_AIO;
432 break;
433 case 't':
434 if (bdrv_parse_cache_flags(optarg, &flags) < 0) {
435 error_report("Invalid cache option: %s", optarg);
436 exit(1);
438 break;
439 case 'T':
440 if (!trace_init_backends(optarg, NULL)) {
441 exit(1); /* error message will have been printed */
443 break;
444 case 'V':
445 printf("%s version %s\n", progname, QEMU_VERSION);
446 exit(0);
447 case 'h':
448 usage(progname);
449 exit(0);
450 default:
451 usage(progname);
452 exit(1);
456 if ((argc - optind) > 1) {
457 usage(progname);
458 exit(1);
461 qemu_init_main_loop();
462 bdrv_init();
464 /* initialize commands */
465 qemuio_add_command(&quit_cmd);
466 qemuio_add_command(&open_cmd);
467 qemuio_add_command(&close_cmd);
469 if (isatty(STDIN_FILENO)) {
470 readline_state = readline_init(readline_printf_func,
471 readline_flush_func,
472 NULL,
473 readline_completion_func);
474 qemu_set_tty_echo(STDIN_FILENO, false);
475 atexit(reenable_tty_echo);
478 /* open the device */
479 if (!readonly) {
480 flags |= BDRV_O_RDWR;
483 if ((argc - optind) == 1) {
484 openfile(argv[optind], flags, growable, NULL);
486 command_loop();
489 * Make sure all outstanding requests complete before the program exits.
491 bdrv_drain_all();
493 if (qemuio_bs) {
494 bdrv_unref(qemuio_bs);
496 g_free(readline_state);
497 return 0;