Use special code for sigsetjmp only in cpu-exec.c
[qemu/ar7.git] / qemu-io.c
blobd88f144f05d26d02630d7cda132bf5f016fd828e
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 fprintf(stderr, "file open already, try 'help close'\n");
61 QDECREF(opts);
62 return 1;
65 qemuio_blk = blk_new_open("hda", name, NULL, opts, flags, &local_err);
66 if (!qemuio_blk) {
67 fprintf(stderr, "%s: can't open%s%s: %s\n", progname,
68 name ? " device " : "", name ?: "",
69 error_get_pretty(local_err));
70 error_free(local_err);
71 return 1;
74 bs = blk_bs(qemuio_blk);
75 if (bdrv_is_encrypted(bs)) {
76 char password[256];
77 printf("Disk image '%s' is encrypted.\n", name);
78 if (qemu_read_password(password, sizeof(password)) < 0) {
79 error_report("No password given");
80 goto error;
82 if (bdrv_set_key(bs, password) < 0) {
83 error_report("invalid password");
84 goto error;
89 return 0;
91 error:
92 blk_unref(qemuio_blk);
93 qemuio_blk = NULL;
94 return 1;
97 static void open_help(void)
99 printf(
100 "\n"
101 " opens a new file in the requested mode\n"
102 "\n"
103 " Example:\n"
104 " 'open -Cn /tmp/data' - creates/opens data file read-write and uncached\n"
105 "\n"
106 " Opens a file for subsequent use by all of the other qemu-io commands.\n"
107 " -r, -- open file read-only\n"
108 " -s, -- use snapshot file\n"
109 " -n, -- disable host cache\n"
110 " -o, -- options to be given to the block driver"
111 "\n");
114 static int open_f(BlockBackend *blk, int argc, char **argv);
116 static const cmdinfo_t open_cmd = {
117 .name = "open",
118 .altname = "o",
119 .cfunc = open_f,
120 .argmin = 1,
121 .argmax = -1,
122 .flags = CMD_NOFILE_OK,
123 .args = "[-Crsn] [-o options] [path]",
124 .oneline = "open the file specified by path",
125 .help = open_help,
128 static QemuOptsList empty_opts = {
129 .name = "drive",
130 .merge_lists = true,
131 .head = QTAILQ_HEAD_INITIALIZER(empty_opts.head),
132 .desc = {
133 /* no elements => accept any params */
134 { /* end of list */ }
138 static int open_f(BlockBackend *blk, int argc, char **argv)
140 int flags = 0;
141 int readonly = 0;
142 int c;
143 QemuOpts *qopts;
144 QDict *opts;
146 while ((c = getopt(argc, argv, "snrgo:")) != -1) {
147 switch (c) {
148 case 's':
149 flags |= BDRV_O_SNAPSHOT;
150 break;
151 case 'n':
152 flags |= BDRV_O_NOCACHE | BDRV_O_CACHE_WB;
153 break;
154 case 'r':
155 readonly = 1;
156 break;
157 case 'o':
158 if (!qemu_opts_parse_noisily(&empty_opts, optarg, false)) {
159 qemu_opts_reset(&empty_opts);
160 return 0;
162 break;
163 default:
164 qemu_opts_reset(&empty_opts);
165 return qemuio_command_usage(&open_cmd);
169 if (!readonly) {
170 flags |= BDRV_O_RDWR;
173 qopts = qemu_opts_find(&empty_opts, NULL);
174 opts = qopts ? qemu_opts_to_qdict(qopts, NULL) : NULL;
175 qemu_opts_reset(&empty_opts);
177 if (optind == argc - 1) {
178 return openfile(argv[optind], flags, opts);
179 } else if (optind == argc) {
180 return openfile(NULL, flags, opts);
181 } else {
182 QDECREF(opts);
183 return qemuio_command_usage(&open_cmd);
187 static int quit_f(BlockBackend *blk, int argc, char **argv)
189 return 1;
192 #if defined(CONFIG_FVD)
193 # include "qemu-io-sim.c"
194 #endif
196 static const cmdinfo_t quit_cmd = {
197 .name = "quit",
198 .altname = "q",
199 .cfunc = quit_f,
200 .argmin = -1,
201 .argmax = -1,
202 .flags = CMD_FLAG_GLOBAL,
203 .oneline = "exit the program",
206 static void usage(const char *name)
208 printf(
209 "Usage: %s [-h] [-V] [-rsnm] [-f FMT] [-c STRING] ... [file]\n"
210 "QEMU Disk exerciser\n"
211 "\n"
212 " -c, --cmd STRING execute command with its arguments\n"
213 " from the given string\n"
214 " -f, --format FMT specifies the block driver to use\n"
215 " -r, --read-only export read-only\n"
216 " -s, --snapshot use snapshot file\n"
217 " -n, --nocache disable host cache\n"
218 " -m, --misalign misalign allocations for O_DIRECT\n"
219 " -k, --native-aio use kernel AIO implementation (on Linux only)\n"
220 " -t, --cache=MODE use the given cache mode for the image\n"
221 " -T, --trace FILE enable trace events listed in the given file\n"
222 " -h, --help display this help and exit\n"
223 " -V, --version output version information and exit\n"
224 "\n"
225 "See '%s -c help' for information on available commands."
226 "\n",
227 name, name);
230 static char *get_prompt(void)
232 static char prompt[FILENAME_MAX + 2 /*"> "*/ + 1 /*"\0"*/ ];
234 if (!prompt[0]) {
235 snprintf(prompt, sizeof(prompt), "%s> ", progname);
238 return prompt;
241 static void GCC_FMT_ATTR(2, 3) readline_printf_func(void *opaque,
242 const char *fmt, ...)
244 va_list ap;
245 va_start(ap, fmt);
246 vprintf(fmt, ap);
247 va_end(ap);
250 static void readline_flush_func(void *opaque)
252 fflush(stdout);
255 static void readline_func(void *opaque, const char *str, void *readline_opaque)
257 char **line = readline_opaque;
258 *line = g_strdup(str);
261 static void completion_match(const char *cmd, void *opaque)
263 readline_add_completion(readline_state, cmd);
266 static void readline_completion_func(void *opaque, const char *str)
268 readline_set_completion_index(readline_state, strlen(str));
269 qemuio_complete_command(str, completion_match, NULL);
272 static char *fetchline_readline(void)
274 char *line = NULL;
276 readline_start(readline_state, get_prompt(), 0, readline_func, &line);
277 while (!line) {
278 int ch = getchar();
279 if (ch == EOF) {
280 break;
282 readline_handle_byte(readline_state, ch);
284 return line;
287 #define MAXREADLINESZ 1024
288 static char *fetchline_fgets(void)
290 char *p, *line = g_malloc(MAXREADLINESZ);
292 if (!fgets(line, MAXREADLINESZ, stdin)) {
293 g_free(line);
294 return NULL;
297 p = line + strlen(line);
298 if (p != line && p[-1] == '\n') {
299 p[-1] = '\0';
302 return line;
305 static char *fetchline(void)
307 if (readline_state) {
308 return fetchline_readline();
309 } else {
310 return fetchline_fgets();
314 static void prep_fetchline(void *opaque)
316 int *fetchable = opaque;
318 qemu_set_fd_handler(STDIN_FILENO, NULL, NULL, NULL);
319 *fetchable= 1;
322 static void command_loop(void)
324 int i, done = 0, fetchable = 0, prompted = 0;
325 char *input;
327 for (i = 0; !done && i < ncmdline; i++) {
328 done = qemuio_command(qemuio_blk, cmdline[i]);
330 if (cmdline) {
331 g_free(cmdline);
332 return;
335 while (!done) {
336 if (!prompted) {
337 printf("%s", get_prompt());
338 fflush(stdout);
339 qemu_set_fd_handler(STDIN_FILENO, prep_fetchline, NULL, &fetchable);
340 prompted = 1;
343 main_loop_wait(false);
345 if (!fetchable) {
346 continue;
349 input = fetchline();
350 if (input == NULL) {
351 break;
353 done = qemuio_command(qemuio_blk, input);
354 g_free(input);
356 prompted = 0;
357 fetchable = 0;
359 qemu_set_fd_handler(STDIN_FILENO, NULL, NULL, NULL);
362 static void add_user_command(char *optarg)
364 cmdline = g_renew(char *, cmdline, ++ncmdline);
365 cmdline[ncmdline-1] = optarg;
368 static void reenable_tty_echo(void)
370 qemu_set_tty_echo(STDIN_FILENO, true);
373 int main(int argc, char **argv)
375 int readonly = 0;
376 const char *sopt = "hVc:d:f: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 { "format", 1, NULL, 'f' },
383 { "read-only", 0, NULL, 'r' },
384 { "snapshot", 0, NULL, 's' },
385 { "nocache", 0, NULL, 'n' },
386 { "misalign", 0, NULL, 'm' },
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;
396 Error *local_error = NULL;
397 QDict *opts = NULL;
399 #ifdef CONFIG_POSIX
400 signal(SIGPIPE, SIG_IGN);
401 #endif
403 progname = basename(argv[0]);
404 qemu_init_exec_dir(argv[0]);
406 bdrv_init();
408 while ((c = getopt_long(argc, argv, sopt, lopt, &opt_index)) != -1) {
409 switch (c) {
410 case 's':
411 flags |= BDRV_O_SNAPSHOT;
412 break;
413 case 'n':
414 flags |= BDRV_O_NOCACHE | BDRV_O_CACHE_WB;
415 break;
416 case 'd':
417 if (bdrv_parse_discard_flags(optarg, &flags) < 0) {
418 error_report("Invalid discard option: %s", optarg);
419 exit(1);
421 break;
422 case 'f':
423 if (!opts) {
424 opts = qdict_new();
426 qdict_put(opts, "driver", qstring_from_str(optarg));
427 break;
428 case 'c':
429 add_user_command(optarg);
430 break;
431 case 'r':
432 readonly = 1;
433 break;
434 case 'm':
435 qemuio_misalign = true;
436 break;
437 case 'k':
438 flags |= BDRV_O_NATIVE_AIO;
439 break;
440 case 't':
441 if (bdrv_parse_cache_flags(optarg, &flags) < 0) {
442 error_report("Invalid cache option: %s", optarg);
443 exit(1);
445 break;
446 case 'T':
447 if (!trace_init_backends(optarg, NULL)) {
448 exit(1); /* error message will have been printed */
450 break;
451 case 'V':
452 printf("%s version %s\n", progname, QEMU_VERSION);
453 exit(0);
454 case 'h':
455 usage(progname);
456 exit(0);
457 default:
458 usage(progname);
459 exit(1);
463 if ((argc - optind) > 1) {
464 usage(progname);
465 exit(1);
468 if (qemu_init_main_loop(&local_error)) {
469 error_report_err(local_error);
470 exit(1);
473 /* initialize commands */
474 qemuio_add_command(&quit_cmd);
475 qemuio_add_command(&open_cmd);
476 qemuio_add_command(&close_cmd);
478 if (isatty(STDIN_FILENO)) {
479 readline_state = readline_init(readline_printf_func,
480 readline_flush_func,
481 NULL,
482 readline_completion_func);
483 qemu_set_tty_echo(STDIN_FILENO, false);
484 atexit(reenable_tty_echo);
487 /* open the device */
488 if (!readonly) {
489 flags |= BDRV_O_RDWR;
492 if ((argc - optind) == 1) {
493 openfile(argv[optind], flags, opts);
495 command_loop();
498 * Make sure all outstanding requests complete before the program exits.
500 bdrv_drain_all();
502 blk_unref(qemuio_blk);
503 g_free(readline_state);
504 return 0;