Merge remote-tracking branch 'remotes/mjt/tags/trivial-patches-2014-09-22' into staging
[qemu/cris-port.git] / qemu-io.c
blobd2ab6946e8f28a42acec4d75a46f084cf4b43524
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 static const cmdinfo_t quit_cmd = {
182 .name = "quit",
183 .altname = "q",
184 .cfunc = quit_f,
185 .argmin = -1,
186 .argmax = -1,
187 .flags = CMD_FLAG_GLOBAL,
188 .oneline = "exit the program",
191 static void usage(const char *name)
193 printf(
194 "Usage: %s [-h] [-V] [-rsnm] [-c STRING] ... [file]\n"
195 "QEMU Disk exerciser\n"
196 "\n"
197 " -c, --cmd STRING execute command with its arguments\n"
198 " from the given string\n"
199 " -r, --read-only export read-only\n"
200 " -s, --snapshot use snapshot file\n"
201 " -n, --nocache disable host cache\n"
202 " -g, --growable allow file to grow (only applies to protocols)\n"
203 " -m, --misalign misalign allocations for O_DIRECT\n"
204 " -k, --native-aio use kernel AIO implementation (on Linux only)\n"
205 " -t, --cache=MODE use the given cache mode for the image\n"
206 " -T, --trace FILE enable trace events listed in the given file\n"
207 " -h, --help display this help and exit\n"
208 " -V, --version output version information and exit\n"
209 "\n"
210 "See '%s -c help' for information on available commands."
211 "\n",
212 name, 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 GCC_FMT_ATTR(2, 3) readline_printf_func(void *opaque,
227 const char *fmt, ...)
229 va_list ap;
230 va_start(ap, fmt);
231 vprintf(fmt, ap);
232 va_end(ap);
235 static void readline_flush_func(void *opaque)
237 fflush(stdout);
240 static void readline_func(void *opaque, const char *str, void *readline_opaque)
242 char **line = readline_opaque;
243 *line = g_strdup(str);
246 static void completion_match(const char *cmd, void *opaque)
248 readline_add_completion(readline_state, cmd);
251 static void readline_completion_func(void *opaque, const char *str)
253 readline_set_completion_index(readline_state, strlen(str));
254 qemuio_complete_command(str, completion_match, NULL);
257 static char *fetchline_readline(void)
259 char *line = NULL;
261 readline_start(readline_state, get_prompt(), 0, readline_func, &line);
262 while (!line) {
263 int ch = getchar();
264 if (ch == EOF) {
265 break;
267 readline_handle_byte(readline_state, ch);
269 return line;
272 #define MAXREADLINESZ 1024
273 static char *fetchline_fgets(void)
275 char *p, *line = g_malloc(MAXREADLINESZ);
277 if (!fgets(line, MAXREADLINESZ, stdin)) {
278 g_free(line);
279 return NULL;
282 p = line + strlen(line);
283 if (p != line && p[-1] == '\n') {
284 p[-1] = '\0';
287 return line;
290 static char *fetchline(void)
292 if (readline_state) {
293 return fetchline_readline();
294 } else {
295 return fetchline_fgets();
299 static void prep_fetchline(void *opaque)
301 int *fetchable = opaque;
303 qemu_set_fd_handler(STDIN_FILENO, NULL, NULL, NULL);
304 *fetchable= 1;
307 static void command_loop(void)
309 int i, done = 0, fetchable = 0, prompted = 0;
310 char *input;
312 for (i = 0; !done && i < ncmdline; i++) {
313 done = qemuio_command(qemuio_bs, cmdline[i]);
315 if (cmdline) {
316 g_free(cmdline);
317 return;
320 while (!done) {
321 if (!prompted) {
322 printf("%s", get_prompt());
323 fflush(stdout);
324 qemu_set_fd_handler(STDIN_FILENO, prep_fetchline, NULL, &fetchable);
325 prompted = 1;
328 main_loop_wait(false);
330 if (!fetchable) {
331 continue;
334 input = fetchline();
335 if (input == NULL) {
336 break;
338 done = qemuio_command(qemuio_bs, input);
339 g_free(input);
341 prompted = 0;
342 fetchable = 0;
344 qemu_set_fd_handler(STDIN_FILENO, NULL, NULL, NULL);
347 static void add_user_command(char *optarg)
349 cmdline = g_renew(char *, cmdline, ++ncmdline);
350 cmdline[ncmdline-1] = optarg;
353 static void reenable_tty_echo(void)
355 qemu_set_tty_echo(STDIN_FILENO, true);
358 int main(int argc, char **argv)
360 int readonly = 0;
361 int growable = 0;
362 const char *sopt = "hVc:d:rsnmgkt:T:";
363 const struct option lopt[] = {
364 { "help", 0, NULL, 'h' },
365 { "version", 0, NULL, 'V' },
366 { "offset", 1, NULL, 'o' },
367 { "cmd", 1, NULL, 'c' },
368 { "read-only", 0, NULL, 'r' },
369 { "snapshot", 0, NULL, 's' },
370 { "nocache", 0, NULL, 'n' },
371 { "misalign", 0, NULL, 'm' },
372 { "growable", 0, NULL, 'g' },
373 { "native-aio", 0, NULL, 'k' },
374 { "discard", 1, NULL, 'd' },
375 { "cache", 1, NULL, 't' },
376 { "trace", 1, NULL, 'T' },
377 { NULL, 0, NULL, 0 }
379 int c;
380 int opt_index = 0;
381 int flags = BDRV_O_UNMAP;
383 #ifdef CONFIG_POSIX
384 signal(SIGPIPE, SIG_IGN);
385 #endif
387 progname = basename(argv[0]);
388 qemu_init_exec_dir(argv[0]);
390 while ((c = getopt_long(argc, argv, sopt, lopt, &opt_index)) != -1) {
391 switch (c) {
392 case 's':
393 flags |= BDRV_O_SNAPSHOT;
394 break;
395 case 'n':
396 flags |= BDRV_O_NOCACHE | BDRV_O_CACHE_WB;
397 break;
398 case 'd':
399 if (bdrv_parse_discard_flags(optarg, &flags) < 0) {
400 error_report("Invalid discard option: %s", optarg);
401 exit(1);
403 break;
404 case 'c':
405 add_user_command(optarg);
406 break;
407 case 'r':
408 readonly = 1;
409 break;
410 case 'm':
411 qemuio_misalign = true;
412 break;
413 case 'g':
414 growable = 1;
415 break;
416 case 'k':
417 flags |= BDRV_O_NATIVE_AIO;
418 break;
419 case 't':
420 if (bdrv_parse_cache_flags(optarg, &flags) < 0) {
421 error_report("Invalid cache option: %s", optarg);
422 exit(1);
424 break;
425 case 'T':
426 if (!trace_init_backends(optarg, NULL)) {
427 exit(1); /* error message will have been printed */
429 break;
430 case 'V':
431 printf("%s version %s\n", progname, QEMU_VERSION);
432 exit(0);
433 case 'h':
434 usage(progname);
435 exit(0);
436 default:
437 usage(progname);
438 exit(1);
442 if ((argc - optind) > 1) {
443 usage(progname);
444 exit(1);
447 qemu_init_main_loop();
448 bdrv_init();
450 /* initialize commands */
451 qemuio_add_command(&quit_cmd);
452 qemuio_add_command(&open_cmd);
453 qemuio_add_command(&close_cmd);
455 if (isatty(STDIN_FILENO)) {
456 readline_state = readline_init(readline_printf_func,
457 readline_flush_func,
458 NULL,
459 readline_completion_func);
460 qemu_set_tty_echo(STDIN_FILENO, false);
461 atexit(reenable_tty_echo);
464 /* open the device */
465 if (!readonly) {
466 flags |= BDRV_O_RDWR;
469 if ((argc - optind) == 1) {
470 openfile(argv[optind], flags, growable, NULL);
472 command_loop();
475 * Make sure all outstanding requests complete before the program exits.
477 bdrv_drain_all();
479 if (qemuio_bs) {
480 bdrv_unref(qemuio_bs);
482 g_free(readline_state);
483 return 0;