Merge remote-tracking branch 'qemu/master'
[qemu/ar7.git] / qemu-io.c
blob49a63d8c89f3a18f6cb844efdd3b8b266e10d174
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 qemuio_bs = bdrv_new("hda", &error_abort);
63 if (growable) {
64 flags |= BDRV_O_PROTOCOL;
67 if (bdrv_open(&qemuio_bs, name, NULL, opts, flags, NULL, &local_err) < 0) {
68 fprintf(stderr, "%s: can't open%s%s: %s\n", progname,
69 name ? " device " : "", name ?: "",
70 error_get_pretty(local_err));
71 error_free(local_err);
72 bdrv_unref(qemuio_bs);
73 qemuio_bs = NULL;
74 return 1;
77 return 0;
80 static void open_help(void)
82 printf(
83 "\n"
84 " opens a new file in the requested mode\n"
85 "\n"
86 " Example:\n"
87 " 'open -Cn /tmp/data' - creates/opens data file read-write and uncached\n"
88 "\n"
89 " Opens a file for subsequent use by all of the other qemu-io commands.\n"
90 " -r, -- open file read-only\n"
91 " -s, -- use snapshot file\n"
92 " -n, -- disable host cache\n"
93 " -g, -- allow file to grow (only applies to protocols)\n"
94 " -o, -- options to be given to the block driver"
95 "\n");
98 static int open_f(BlockDriverState *bs, int argc, char **argv);
100 static const cmdinfo_t open_cmd = {
101 .name = "open",
102 .altname = "o",
103 .cfunc = open_f,
104 .argmin = 1,
105 .argmax = -1,
106 .flags = CMD_NOFILE_OK,
107 .args = "[-Crsn] [-o options] [path]",
108 .oneline = "open the file specified by path",
109 .help = open_help,
112 static QemuOptsList empty_opts = {
113 .name = "drive",
114 .merge_lists = true,
115 .head = QTAILQ_HEAD_INITIALIZER(empty_opts.head),
116 .desc = {
117 /* no elements => accept any params */
118 { /* end of list */ }
122 static int open_f(BlockDriverState *bs, int argc, char **argv)
124 int flags = 0;
125 int readonly = 0;
126 int growable = 0;
127 int c;
128 QemuOpts *qopts;
129 QDict *opts;
131 while ((c = getopt(argc, argv, "snrgo:")) != EOF) {
132 switch (c) {
133 case 's':
134 flags |= BDRV_O_SNAPSHOT;
135 break;
136 case 'n':
137 flags |= BDRV_O_NOCACHE | BDRV_O_CACHE_WB;
138 break;
139 case 'r':
140 readonly = 1;
141 break;
142 case 'g':
143 growable = 1;
144 break;
145 case 'o':
146 if (!qemu_opts_parse(&empty_opts, optarg, 0)) {
147 printf("could not parse option list -- %s\n", optarg);
148 qemu_opts_reset(&empty_opts);
149 return 0;
151 break;
152 default:
153 qemu_opts_reset(&empty_opts);
154 return qemuio_command_usage(&open_cmd);
158 if (!readonly) {
159 flags |= BDRV_O_RDWR;
162 qopts = qemu_opts_find(&empty_opts, NULL);
163 opts = qopts ? qemu_opts_to_qdict(qopts, NULL) : NULL;
164 qemu_opts_reset(&empty_opts);
166 if (optind == argc - 1) {
167 return openfile(argv[optind], flags, growable, opts);
168 } else if (optind == argc) {
169 return openfile(NULL, flags, growable, opts);
170 } else {
171 QDECREF(opts);
172 return qemuio_command_usage(&open_cmd);
176 static int quit_f(BlockDriverState *bs, int argc, char **argv)
178 return 1;
181 #if !defined(_WIN32)
182 # include "qemu-io-sim.c"
183 #endif
185 static const cmdinfo_t quit_cmd = {
186 .name = "quit",
187 .altname = "q",
188 .cfunc = quit_f,
189 .argmin = -1,
190 .argmax = -1,
191 .flags = CMD_FLAG_GLOBAL,
192 .oneline = "exit the program",
195 static void usage(const char *name)
197 printf(
198 "Usage: %s [-h] [-V] [-rsnm] [-c STRING] ... [file]\n"
199 "QEMU Disk exerciser\n"
200 "\n"
201 " -c, --cmd STRING execute command with its arguments\n"
202 " from the given string\n"
203 " -r, --read-only export read-only\n"
204 " -s, --snapshot use snapshot file\n"
205 " -n, --nocache disable host cache\n"
206 " -g, --growable allow file to grow (only applies to protocols)\n"
207 " -m, --misalign misalign allocations for O_DIRECT\n"
208 " -k, --native-aio use kernel AIO implementation (on Linux only)\n"
209 " -t, --cache=MODE use the given cache mode for the image\n"
210 " -T, --trace FILE enable trace events listed in the given file\n"
211 " -h, --help display this help and exit\n"
212 " -V, --version output version information and exit\n"
213 "\n"
214 "See '%s -c help' for information on available commands."
215 "\n",
216 name, name);
219 static char *get_prompt(void)
221 static char prompt[FILENAME_MAX + 2 /*"> "*/ + 1 /*"\0"*/ ];
223 if (!prompt[0]) {
224 snprintf(prompt, sizeof(prompt), "%s> ", progname);
227 return prompt;
230 static void GCC_FMT_ATTR(2, 3) readline_printf_func(void *opaque,
231 const char *fmt, ...)
233 va_list ap;
234 va_start(ap, fmt);
235 vprintf(fmt, ap);
236 va_end(ap);
239 static void readline_flush_func(void *opaque)
241 fflush(stdout);
244 static void readline_func(void *opaque, const char *str, void *readline_opaque)
246 char **line = readline_opaque;
247 *line = g_strdup(str);
250 static void completion_match(const char *cmd, void *opaque)
252 readline_add_completion(readline_state, cmd);
255 static void readline_completion_func(void *opaque, const char *str)
257 readline_set_completion_index(readline_state, strlen(str));
258 qemuio_complete_command(str, completion_match, NULL);
261 static char *fetchline_readline(void)
263 char *line = NULL;
265 readline_start(readline_state, get_prompt(), 0, readline_func, &line);
266 while (!line) {
267 int ch = getchar();
268 if (ch == EOF) {
269 break;
271 readline_handle_byte(readline_state, ch);
273 return line;
276 #define MAXREADLINESZ 1024
277 static char *fetchline_fgets(void)
279 char *p, *line = g_malloc(MAXREADLINESZ);
281 if (!fgets(line, MAXREADLINESZ, stdin)) {
282 g_free(line);
283 return NULL;
286 p = line + strlen(line);
287 if (p != line && p[-1] == '\n') {
288 p[-1] = '\0';
291 return line;
294 static char *fetchline(void)
296 if (readline_state) {
297 return fetchline_readline();
298 } else {
299 return fetchline_fgets();
303 static void prep_fetchline(void *opaque)
305 int *fetchable = opaque;
307 qemu_set_fd_handler(STDIN_FILENO, NULL, NULL, NULL);
308 *fetchable= 1;
311 static void command_loop(void)
313 int i, done = 0, fetchable = 0, prompted = 0;
314 char *input;
316 for (i = 0; !done && i < ncmdline; i++) {
317 done = qemuio_command(qemuio_bs, cmdline[i]);
319 if (cmdline) {
320 g_free(cmdline);
321 return;
324 while (!done) {
325 if (!prompted) {
326 printf("%s", get_prompt());
327 fflush(stdout);
328 qemu_set_fd_handler(STDIN_FILENO, prep_fetchline, NULL, &fetchable);
329 prompted = 1;
332 main_loop_wait(false);
334 if (!fetchable) {
335 continue;
338 input = fetchline();
339 if (input == NULL) {
340 break;
342 done = qemuio_command(qemuio_bs, input);
343 g_free(input);
345 prompted = 0;
346 fetchable = 0;
348 qemu_set_fd_handler(STDIN_FILENO, NULL, NULL, NULL);
351 static void add_user_command(char *optarg)
353 cmdline = g_renew(char *, cmdline, ++ncmdline);
354 cmdline[ncmdline-1] = optarg;
357 static void reenable_tty_echo(void)
359 qemu_set_tty_echo(STDIN_FILENO, true);
362 int main(int argc, char **argv)
364 int readonly = 0;
365 int growable = 0;
366 const char *sopt = "hVc:d:rsnmgkt:T:";
367 const struct option lopt[] = {
368 { "help", 0, NULL, 'h' },
369 { "version", 0, NULL, 'V' },
370 { "offset", 1, NULL, 'o' },
371 { "cmd", 1, NULL, 'c' },
372 { "read-only", 0, NULL, 'r' },
373 { "snapshot", 0, NULL, 's' },
374 { "nocache", 0, NULL, 'n' },
375 { "misalign", 0, NULL, 'm' },
376 { "growable", 0, NULL, 'g' },
377 { "native-aio", 0, NULL, 'k' },
378 { "discard", 1, NULL, 'd' },
379 { "cache", 1, NULL, 't' },
380 { "trace", 1, NULL, 'T' },
381 { NULL, 0, NULL, 0 }
383 int c;
384 int opt_index = 0;
385 int flags = BDRV_O_UNMAP;
386 Error *local_error = NULL;
388 #ifdef CONFIG_POSIX
389 signal(SIGPIPE, SIG_IGN);
390 #endif
392 progname = basename(argv[0]);
393 qemu_init_exec_dir(argv[0]);
395 while ((c = getopt_long(argc, argv, sopt, lopt, &opt_index)) != -1) {
396 switch (c) {
397 case 's':
398 flags |= BDRV_O_SNAPSHOT;
399 break;
400 case 'n':
401 flags |= BDRV_O_NOCACHE | BDRV_O_CACHE_WB;
402 break;
403 case 'd':
404 if (bdrv_parse_discard_flags(optarg, &flags) < 0) {
405 error_report("Invalid discard option: %s", optarg);
406 exit(1);
408 break;
409 case 'c':
410 add_user_command(optarg);
411 break;
412 case 'r':
413 readonly = 1;
414 break;
415 case 'm':
416 qemuio_misalign = true;
417 break;
418 case 'g':
419 growable = 1;
420 break;
421 case 'k':
422 flags |= BDRV_O_NATIVE_AIO;
423 break;
424 case 't':
425 if (bdrv_parse_cache_flags(optarg, &flags) < 0) {
426 error_report("Invalid cache option: %s", optarg);
427 exit(1);
429 break;
430 case 'T':
431 if (!trace_init_backends(optarg, NULL)) {
432 exit(1); /* error message will have been printed */
434 break;
435 case 'V':
436 printf("%s version %s\n", progname, QEMU_VERSION);
437 exit(0);
438 case 'h':
439 usage(progname);
440 exit(0);
441 default:
442 usage(progname);
443 exit(1);
447 if ((argc - optind) > 1) {
448 usage(progname);
449 exit(1);
452 if (qemu_init_main_loop(&local_error)) {
453 error_report("%s", error_get_pretty(local_error));
454 error_free(local_error);
455 exit(1);
457 bdrv_init();
459 /* initialize commands */
460 qemuio_add_command(&quit_cmd);
461 qemuio_add_command(&open_cmd);
462 qemuio_add_command(&close_cmd);
464 if (isatty(STDIN_FILENO)) {
465 readline_state = readline_init(readline_printf_func,
466 readline_flush_func,
467 NULL,
468 readline_completion_func);
469 qemu_set_tty_echo(STDIN_FILENO, false);
470 atexit(reenable_tty_echo);
473 /* open the device */
474 if (!readonly) {
475 flags |= BDRV_O_RDWR;
478 if ((argc - optind) == 1) {
479 openfile(argv[optind], flags, growable, NULL);
481 command_loop();
484 * Make sure all outstanding requests complete before the program exits.
486 bdrv_drain_all();
488 if (qemuio_bs) {
489 bdrv_unref(qemuio_bs);
491 g_free(readline_state);
492 return 0;