scripts/kvm/kvm_stat: Mark globals in functions
[qemu.git] / qemu-io.c
blobd47228a963ade968a2560288fd93654d6b2d6e56
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/error-report.h"
19 #include "qemu/main-loop.h"
20 #include "qemu/option.h"
21 #include "qemu/config-file.h"
22 #include "qemu/readline.h"
23 #include "qapi/qmp/qstring.h"
24 #include "sysemu/block-backend.h"
25 #include "block/block_int.h"
26 #include "trace/control.h"
28 #define CMD_NOFILE_OK 0x01
30 static char *progname;
32 static BlockBackend *qemuio_blk;
34 /* qemu-io commands passed using -c */
35 static int ncmdline;
36 static char **cmdline;
38 static ReadLineState *readline_state;
40 static int close_f(BlockBackend *blk, int argc, char **argv)
42 blk_unref(qemuio_blk);
43 qemuio_blk = NULL;
44 return 0;
47 static const cmdinfo_t close_cmd = {
48 .name = "close",
49 .altname = "c",
50 .cfunc = close_f,
51 .oneline = "close the current open file",
54 static int openfile(char *name, int flags, QDict *opts)
56 Error *local_err = NULL;
57 BlockDriverState *bs;
59 if (qemuio_blk) {
60 error_report("file open already, try 'help close'");
61 QDECREF(opts);
62 return 1;
65 qemuio_blk = blk_new_open("hda", name, NULL, opts, flags, &local_err);
66 if (!qemuio_blk) {
67 error_reportf_err(local_err, "can't open%s%s: ",
68 name ? " device " : "", name ?: "");
69 return 1;
72 bs = blk_bs(qemuio_blk);
73 if (bdrv_is_encrypted(bs)) {
74 char password[256];
75 printf("Disk image '%s' is encrypted.\n", name);
76 if (qemu_read_password(password, sizeof(password)) < 0) {
77 error_report("No password given");
78 goto error;
80 if (bdrv_set_key(bs, password) < 0) {
81 error_report("invalid password");
82 goto error;
87 return 0;
89 error:
90 blk_unref(qemuio_blk);
91 qemuio_blk = NULL;
92 return 1;
95 static void open_help(void)
97 printf(
98 "\n"
99 " opens a new file in the requested mode\n"
100 "\n"
101 " Example:\n"
102 " 'open -Cn /tmp/data' - creates/opens data file read-write and uncached\n"
103 "\n"
104 " Opens a file for subsequent use by all of the other qemu-io commands.\n"
105 " -r, -- open file read-only\n"
106 " -s, -- use snapshot file\n"
107 " -n, -- disable host cache\n"
108 " -o, -- options to be given to the block driver"
109 "\n");
112 static int open_f(BlockBackend *blk, int argc, char **argv);
114 static const cmdinfo_t open_cmd = {
115 .name = "open",
116 .altname = "o",
117 .cfunc = open_f,
118 .argmin = 1,
119 .argmax = -1,
120 .flags = CMD_NOFILE_OK,
121 .args = "[-Crsn] [-o options] [path]",
122 .oneline = "open the file specified by path",
123 .help = open_help,
126 static QemuOptsList empty_opts = {
127 .name = "drive",
128 .merge_lists = true,
129 .head = QTAILQ_HEAD_INITIALIZER(empty_opts.head),
130 .desc = {
131 /* no elements => accept any params */
132 { /* end of list */ }
136 static int open_f(BlockBackend *blk, int argc, char **argv)
138 int flags = 0;
139 int readonly = 0;
140 int c;
141 QemuOpts *qopts;
142 QDict *opts;
144 while ((c = getopt(argc, argv, "snrgo:")) != -1) {
145 switch (c) {
146 case 's':
147 flags |= BDRV_O_SNAPSHOT;
148 break;
149 case 'n':
150 flags |= BDRV_O_NOCACHE | BDRV_O_CACHE_WB;
151 break;
152 case 'r':
153 readonly = 1;
154 break;
155 case 'o':
156 if (!qemu_opts_parse_noisily(&empty_opts, optarg, false)) {
157 qemu_opts_reset(&empty_opts);
158 return 0;
160 break;
161 default:
162 qemu_opts_reset(&empty_opts);
163 return qemuio_command_usage(&open_cmd);
167 if (!readonly) {
168 flags |= BDRV_O_RDWR;
171 qopts = qemu_opts_find(&empty_opts, NULL);
172 opts = qopts ? qemu_opts_to_qdict(qopts, NULL) : NULL;
173 qemu_opts_reset(&empty_opts);
175 if (optind == argc - 1) {
176 return openfile(argv[optind], flags, opts);
177 } else if (optind == argc) {
178 return openfile(NULL, flags, opts);
179 } else {
180 QDECREF(opts);
181 return qemuio_command_usage(&open_cmd);
185 static int quit_f(BlockBackend *blk, int argc, char **argv)
187 return 1;
190 static const cmdinfo_t quit_cmd = {
191 .name = "quit",
192 .altname = "q",
193 .cfunc = quit_f,
194 .argmin = -1,
195 .argmax = -1,
196 .flags = CMD_FLAG_GLOBAL,
197 .oneline = "exit the program",
200 static void usage(const char *name)
202 printf(
203 "Usage: %s [-h] [-V] [-rsnm] [-f FMT] [-c STRING] ... [file]\n"
204 "QEMU Disk exerciser\n"
205 "\n"
206 " -c, --cmd STRING execute command with its arguments\n"
207 " from the given string\n"
208 " -f, --format FMT specifies the block driver to use\n"
209 " -r, --read-only export read-only\n"
210 " -s, --snapshot use snapshot file\n"
211 " -n, --nocache disable host cache\n"
212 " -m, --misalign misalign allocations for O_DIRECT\n"
213 " -k, --native-aio use kernel AIO implementation (on Linux only)\n"
214 " -t, --cache=MODE use the given cache mode for the image\n"
215 " -T, --trace FILE enable trace events listed in the given file\n"
216 " -h, --help display this help and exit\n"
217 " -V, --version output version information and exit\n"
218 "\n"
219 "See '%s -c help' for information on available commands."
220 "\n",
221 name, name);
224 static char *get_prompt(void)
226 static char prompt[FILENAME_MAX + 2 /*"> "*/ + 1 /*"\0"*/ ];
228 if (!prompt[0]) {
229 snprintf(prompt, sizeof(prompt), "%s> ", progname);
232 return prompt;
235 static void GCC_FMT_ATTR(2, 3) readline_printf_func(void *opaque,
236 const char *fmt, ...)
238 va_list ap;
239 va_start(ap, fmt);
240 vprintf(fmt, ap);
241 va_end(ap);
244 static void readline_flush_func(void *opaque)
246 fflush(stdout);
249 static void readline_func(void *opaque, const char *str, void *readline_opaque)
251 char **line = readline_opaque;
252 *line = g_strdup(str);
255 static void completion_match(const char *cmd, void *opaque)
257 readline_add_completion(readline_state, cmd);
260 static void readline_completion_func(void *opaque, const char *str)
262 readline_set_completion_index(readline_state, strlen(str));
263 qemuio_complete_command(str, completion_match, NULL);
266 static char *fetchline_readline(void)
268 char *line = NULL;
270 readline_start(readline_state, get_prompt(), 0, readline_func, &line);
271 while (!line) {
272 int ch = getchar();
273 if (ch == EOF) {
274 break;
276 readline_handle_byte(readline_state, ch);
278 return line;
281 #define MAXREADLINESZ 1024
282 static char *fetchline_fgets(void)
284 char *p, *line = g_malloc(MAXREADLINESZ);
286 if (!fgets(line, MAXREADLINESZ, stdin)) {
287 g_free(line);
288 return NULL;
291 p = line + strlen(line);
292 if (p != line && p[-1] == '\n') {
293 p[-1] = '\0';
296 return line;
299 static char *fetchline(void)
301 if (readline_state) {
302 return fetchline_readline();
303 } else {
304 return fetchline_fgets();
308 static void prep_fetchline(void *opaque)
310 int *fetchable = opaque;
312 qemu_set_fd_handler(STDIN_FILENO, NULL, NULL, NULL);
313 *fetchable= 1;
316 static void command_loop(void)
318 int i, done = 0, fetchable = 0, prompted = 0;
319 char *input;
321 for (i = 0; !done && i < ncmdline; i++) {
322 done = qemuio_command(qemuio_blk, cmdline[i]);
324 if (cmdline) {
325 g_free(cmdline);
326 return;
329 while (!done) {
330 if (!prompted) {
331 printf("%s", get_prompt());
332 fflush(stdout);
333 qemu_set_fd_handler(STDIN_FILENO, prep_fetchline, NULL, &fetchable);
334 prompted = 1;
337 main_loop_wait(false);
339 if (!fetchable) {
340 continue;
343 input = fetchline();
344 if (input == NULL) {
345 break;
347 done = qemuio_command(qemuio_blk, input);
348 g_free(input);
350 prompted = 0;
351 fetchable = 0;
353 qemu_set_fd_handler(STDIN_FILENO, NULL, NULL, NULL);
356 static void add_user_command(char *optarg)
358 cmdline = g_renew(char *, cmdline, ++ncmdline);
359 cmdline[ncmdline-1] = optarg;
362 static void reenable_tty_echo(void)
364 qemu_set_tty_echo(STDIN_FILENO, true);
367 int main(int argc, char **argv)
369 int readonly = 0;
370 const char *sopt = "hVc:d:f:rsnmgkt:T:";
371 const struct option lopt[] = {
372 { "help", 0, NULL, 'h' },
373 { "version", 0, NULL, 'V' },
374 { "offset", 1, NULL, 'o' },
375 { "cmd", 1, NULL, 'c' },
376 { "format", 1, NULL, 'f' },
377 { "read-only", 0, NULL, 'r' },
378 { "snapshot", 0, NULL, 's' },
379 { "nocache", 0, NULL, 'n' },
380 { "misalign", 0, NULL, 'm' },
381 { "native-aio", 0, NULL, 'k' },
382 { "discard", 1, NULL, 'd' },
383 { "cache", 1, NULL, 't' },
384 { "trace", 1, NULL, 'T' },
385 { NULL, 0, NULL, 0 }
387 int c;
388 int opt_index = 0;
389 int flags = BDRV_O_UNMAP;
390 Error *local_error = NULL;
391 QDict *opts = NULL;
393 #ifdef CONFIG_POSIX
394 signal(SIGPIPE, SIG_IGN);
395 #endif
397 progname = basename(argv[0]);
398 qemu_init_exec_dir(argv[0]);
400 bdrv_init();
402 while ((c = getopt_long(argc, argv, sopt, lopt, &opt_index)) != -1) {
403 switch (c) {
404 case 's':
405 flags |= BDRV_O_SNAPSHOT;
406 break;
407 case 'n':
408 flags |= BDRV_O_NOCACHE | BDRV_O_CACHE_WB;
409 break;
410 case 'd':
411 if (bdrv_parse_discard_flags(optarg, &flags) < 0) {
412 error_report("Invalid discard option: %s", optarg);
413 exit(1);
415 break;
416 case 'f':
417 if (!opts) {
418 opts = qdict_new();
420 qdict_put(opts, "driver", qstring_from_str(optarg));
421 break;
422 case 'c':
423 add_user_command(optarg);
424 break;
425 case 'r':
426 readonly = 1;
427 break;
428 case 'm':
429 qemuio_misalign = true;
430 break;
431 case 'k':
432 flags |= BDRV_O_NATIVE_AIO;
433 break;
434 case 't':
435 if (bdrv_parse_cache_flags(optarg, &flags) < 0) {
436 error_report("Invalid cache option: %s", optarg);
437 exit(1);
439 break;
440 case 'T':
441 if (!trace_init_backends(optarg, NULL)) {
442 exit(1); /* error message will have been printed */
444 break;
445 case 'V':
446 printf("%s version %s\n", progname, QEMU_VERSION);
447 exit(0);
448 case 'h':
449 usage(progname);
450 exit(0);
451 default:
452 usage(progname);
453 exit(1);
457 if ((argc - optind) > 1) {
458 usage(progname);
459 exit(1);
462 if (qemu_init_main_loop(&local_error)) {
463 error_report_err(local_error);
464 exit(1);
467 /* initialize commands */
468 qemuio_add_command(&quit_cmd);
469 qemuio_add_command(&open_cmd);
470 qemuio_add_command(&close_cmd);
472 if (isatty(STDIN_FILENO)) {
473 readline_state = readline_init(readline_printf_func,
474 readline_flush_func,
475 NULL,
476 readline_completion_func);
477 qemu_set_tty_echo(STDIN_FILENO, false);
478 atexit(reenable_tty_echo);
481 /* open the device */
482 if (!readonly) {
483 flags |= BDRV_O_RDWR;
486 if ((argc - optind) == 1) {
487 openfile(argv[optind], flags, opts);
489 command_loop();
492 * Make sure all outstanding requests complete before the program exits.
494 bdrv_drain_all();
496 blk_unref(qemuio_blk);
497 g_free(readline_state);
498 return 0;