Merge remote-tracking branch 'qemu/master'
[qemu/ar7.git] / qemu-io.c
blob5755a5a44f9742f30e52761b9b324978cb6a2a79
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 char *progname;
29 BlockDriverState *qemuio_bs;
30 extern int qemuio_misalign;
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(BlockDriverState *bs, int argc, char **argv)
40 bdrv_unref(bs);
41 qemuio_bs = 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, int growable, QDict *opts)
54 Error *local_err = NULL;
56 if (qemuio_bs) {
57 fprintf(stderr, "file open already, try 'help close'\n");
58 return 1;
61 if (growable) {
62 if (bdrv_file_open(&qemuio_bs, name, NULL, opts, flags, &local_err)) {
63 fprintf(stderr, "%s: can't open device %s: %s\n", progname, name,
64 error_get_pretty(local_err));
65 error_free(local_err);
66 return 1;
68 } else {
69 qemuio_bs = bdrv_new("hda");
71 if (bdrv_open(qemuio_bs, name, opts, flags, NULL, &local_err) < 0) {
72 fprintf(stderr, "%s: can't open device %s: %s\n", progname, name,
73 error_get_pretty(local_err));
74 error_free(local_err);
75 bdrv_unref(qemuio_bs);
76 qemuio_bs = NULL;
77 return 1;
81 return 0;
84 static void open_help(void)
86 printf(
87 "\n"
88 " opens a new file in the requested mode\n"
89 "\n"
90 " Example:\n"
91 " 'open -Cn /tmp/data' - creates/opens data file read-write and uncached\n"
92 "\n"
93 " Opens a file for subsequent use by all of the other qemu-io commands.\n"
94 " -r, -- open file read-only\n"
95 " -s, -- use snapshot file\n"
96 " -n, -- disable host cache\n"
97 " -g, -- allow file to grow (only applies to protocols)\n"
98 " -o, -- options to be given to the block driver"
99 "\n");
102 static int open_f(BlockDriverState *bs, int argc, char **argv);
104 static const cmdinfo_t open_cmd = {
105 .name = "open",
106 .altname = "o",
107 .cfunc = open_f,
108 .argmin = 1,
109 .argmax = -1,
110 .flags = CMD_NOFILE_OK,
111 .args = "[-Crsn] [-o options] [path]",
112 .oneline = "open the file specified by path",
113 .help = open_help,
116 static QemuOptsList empty_opts = {
117 .name = "drive",
118 .head = QTAILQ_HEAD_INITIALIZER(empty_opts.head),
119 .desc = {
120 /* no elements => accept any params */
121 { /* end of list */ }
125 static int open_f(BlockDriverState *bs, int argc, char **argv)
127 int flags = 0;
128 int readonly = 0;
129 int growable = 0;
130 int c;
131 QemuOpts *qopts;
132 QDict *opts = NULL;
134 while ((c = getopt(argc, argv, "snrgo:")) != EOF) {
135 switch (c) {
136 case 's':
137 flags |= BDRV_O_SNAPSHOT;
138 break;
139 case 'n':
140 flags |= BDRV_O_NOCACHE | BDRV_O_CACHE_WB;
141 break;
142 case 'r':
143 readonly = 1;
144 break;
145 case 'g':
146 growable = 1;
147 break;
148 case 'o':
149 qopts = qemu_opts_parse(&empty_opts, optarg, 0);
150 if (qopts == NULL) {
151 printf("could not parse option list -- %s\n", optarg);
152 return 0;
154 opts = qemu_opts_to_qdict(qopts, opts);
155 qemu_opts_del(qopts);
156 break;
157 default:
158 return qemuio_command_usage(&open_cmd);
162 if (!readonly) {
163 flags |= BDRV_O_RDWR;
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 return qemuio_command_usage(&open_cmd);
175 static int quit_f(BlockDriverState *bs, int argc, char **argv)
177 return 1;
180 #if !defined(_WIN32)
181 # include "qemu-io-sim.c"
182 #endif
184 static const cmdinfo_t quit_cmd = {
185 .name = "quit",
186 .altname = "q",
187 .cfunc = quit_f,
188 .argmin = -1,
189 .argmax = -1,
190 .flags = CMD_FLAG_GLOBAL,
191 .oneline = "exit the program",
194 static void usage(const char *name)
196 printf(
197 "Usage: %s [-h] [-V] [-rsnm] [-c cmd] ... [file]\n"
198 "QEMU Disk exerciser\n"
199 "\n"
200 " -c, --cmd command to execute\n"
201 " -r, --read-only export read-only\n"
202 " -s, --snapshot use snapshot file\n"
203 " -n, --nocache disable host cache\n"
204 " -g, --growable allow file to grow (only applies to protocols)\n"
205 " -m, --misalign misalign allocations for O_DIRECT\n"
206 " -k, --native-aio use kernel AIO implementation (on Linux only)\n"
207 " -t, --cache=MODE use the given cache mode for the image\n"
208 " -T, --trace FILE enable trace events listed in the given file\n"
209 " -h, --help display this help and exit\n"
210 " -V, --version output version information and exit\n"
211 "\n",
212 name);
215 static char *get_prompt(void)
217 static char prompt[FILENAME_MAX + 2 /*"> "*/ + 1 /*"\0"*/ ];
219 if (!prompt[0]) {
220 snprintf(prompt, sizeof(prompt), "%s> ", progname);
223 return prompt;
226 static void readline_printf_func(void *opaque, const char *fmt, ...)
228 va_list ap;
229 va_start(ap, fmt);
230 vprintf(fmt, ap);
231 va_end(ap);
234 static void readline_flush_func(void *opaque)
236 fflush(stdout);
239 static void readline_func(void *opaque, const char *str, void *readline_opaque)
241 char **line = readline_opaque;
242 *line = g_strdup(str);
245 static void completion_match(const char *cmd, void *opaque)
247 readline_add_completion(readline_state, cmd);
250 static void readline_completion_func(void *opaque, const char *str)
252 readline_set_completion_index(readline_state, strlen(str));
253 qemuio_complete_command(str, completion_match, NULL);
256 static char *fetchline_readline(void)
258 char *line = NULL;
260 readline_start(readline_state, get_prompt(), 0, readline_func, &line);
261 while (!line) {
262 int ch = getchar();
263 if (ch == EOF) {
264 break;
266 readline_handle_byte(readline_state, ch);
268 return line;
271 #define MAXREADLINESZ 1024
272 static char *fetchline_fgets(void)
274 char *p, *line = g_malloc(MAXREADLINESZ);
276 if (!fgets(line, MAXREADLINESZ, stdin)) {
277 g_free(line);
278 return NULL;
281 p = line + strlen(line);
282 if (p != line && p[-1] == '\n') {
283 p[-1] = '\0';
286 return line;
289 static char *fetchline(void)
291 if (readline_state) {
292 return fetchline_readline();
293 } else {
294 return fetchline_fgets();
298 static void prep_fetchline(void *opaque)
300 int *fetchable = opaque;
302 qemu_set_fd_handler(STDIN_FILENO, NULL, NULL, NULL);
303 *fetchable= 1;
306 static void command_loop(void)
308 int i, done = 0, fetchable = 0, prompted = 0;
309 char *input;
311 for (i = 0; !done && i < ncmdline; i++) {
312 done = qemuio_command(qemuio_bs, cmdline[i]);
314 if (cmdline) {
315 g_free(cmdline);
316 return;
319 while (!done) {
320 if (!prompted) {
321 printf("%s", get_prompt());
322 fflush(stdout);
323 qemu_set_fd_handler(STDIN_FILENO, prep_fetchline, NULL, &fetchable);
324 prompted = 1;
327 main_loop_wait(false);
329 if (!fetchable) {
330 continue;
333 input = fetchline();
334 if (input == NULL) {
335 break;
337 done = qemuio_command(qemuio_bs, input);
338 g_free(input);
340 prompted = 0;
341 fetchable = 0;
343 qemu_set_fd_handler(STDIN_FILENO, NULL, NULL, NULL);
346 static void add_user_command(char *optarg)
348 cmdline = g_realloc(cmdline, ++ncmdline * sizeof(char *));
349 cmdline[ncmdline-1] = optarg;
352 static void reenable_tty_echo(void)
354 qemu_set_tty_echo(STDIN_FILENO, true);
357 int main(int argc, char **argv)
359 int readonly = 0;
360 int growable = 0;
361 const char *sopt = "hVc:d:rsnmgkt:T:";
362 const struct option lopt[] = {
363 { "help", 0, NULL, 'h' },
364 { "version", 0, NULL, 'V' },
365 { "offset", 1, NULL, 'o' },
366 { "cmd", 1, NULL, 'c' },
367 { "read-only", 0, NULL, 'r' },
368 { "snapshot", 0, NULL, 's' },
369 { "nocache", 0, NULL, 'n' },
370 { "misalign", 0, NULL, 'm' },
371 { "growable", 0, NULL, 'g' },
372 { "native-aio", 0, NULL, 'k' },
373 { "discard", 1, NULL, 'd' },
374 { "cache", 1, NULL, 't' },
375 { "trace", 1, NULL, 'T' },
376 { NULL, 0, NULL, 0 }
378 int c;
379 int opt_index = 0;
380 int flags = BDRV_O_UNMAP;
382 #ifdef CONFIG_POSIX
383 signal(SIGPIPE, SIG_IGN);
384 #endif
386 progname = basename(argv[0]);
388 while ((c = getopt_long(argc, argv, sopt, lopt, &opt_index)) != -1) {
389 switch (c) {
390 case 's':
391 flags |= BDRV_O_SNAPSHOT;
392 break;
393 case 'n':
394 flags |= BDRV_O_NOCACHE | BDRV_O_CACHE_WB;
395 break;
396 case 'd':
397 if (bdrv_parse_discard_flags(optarg, &flags) < 0) {
398 error_report("Invalid discard option: %s", optarg);
399 exit(1);
401 break;
402 case 'c':
403 add_user_command(optarg);
404 break;
405 case 'r':
406 readonly = 1;
407 break;
408 case 'm':
409 qemuio_misalign = 1;
410 break;
411 case 'g':
412 growable = 1;
413 break;
414 case 'k':
415 flags |= BDRV_O_NATIVE_AIO;
416 break;
417 case 't':
418 if (bdrv_parse_cache_flags(optarg, &flags) < 0) {
419 error_report("Invalid cache option: %s", optarg);
420 exit(1);
422 break;
423 case 'T':
424 if (!trace_backend_init(optarg, NULL)) {
425 exit(1); /* error message will have been printed */
427 break;
428 case 'V':
429 printf("%s version %s\n", progname, QEMU_VERSION);
430 exit(0);
431 case 'h':
432 usage(progname);
433 exit(0);
434 default:
435 usage(progname);
436 exit(1);
440 if ((argc - optind) > 1) {
441 usage(progname);
442 exit(1);
445 qemu_init_main_loop();
446 bdrv_init();
448 /* initialize commands */
449 qemuio_add_command(&quit_cmd);
450 qemuio_add_command(&open_cmd);
451 qemuio_add_command(&close_cmd);
453 if (isatty(STDIN_FILENO)) {
454 readline_state = readline_init(readline_printf_func,
455 readline_flush_func,
456 NULL,
457 readline_completion_func);
458 qemu_set_tty_echo(STDIN_FILENO, false);
459 atexit(reenable_tty_echo);
462 /* open the device */
463 if (!readonly) {
464 flags |= BDRV_O_RDWR;
467 if ((argc - optind) == 1) {
468 openfile(argv[optind], flags, growable, NULL);
470 command_loop();
473 * Make sure all outstanding requests complete before the program exits.
475 bdrv_drain_all();
477 if (qemuio_bs) {
478 bdrv_unref(qemuio_bs);
480 g_free(readline_state);
481 return 0;