s390x/kvm: helper to set the SIGP status in SigpInfo
[qemu.git] / qemu-io.c
blob8e41080cc9b9eec8acbd67f7838440d6d51a3b65
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 "sysemu/block-backend.h"
23 #include "block/block_int.h"
24 #include "trace/control.h"
26 #define CMD_NOFILE_OK 0x01
28 static char *progname;
30 static BlockBackend *qemuio_blk;
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(BlockBackend *blk, int argc, char **argv)
40 blk_unref(qemuio_blk);
41 qemuio_blk = 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, QDict *opts)
54 Error *local_err = NULL;
56 if (qemuio_blk) {
57 fprintf(stderr, "file open already, try 'help close'\n");
58 QDECREF(opts);
59 return 1;
62 qemuio_blk = blk_new_open("hda", name, NULL, opts, flags, &local_err);
63 if (!qemuio_blk) {
64 fprintf(stderr, "%s: can't open%s%s: %s\n", progname,
65 name ? " device " : "", name ?: "",
66 error_get_pretty(local_err));
67 error_free(local_err);
68 return 1;
71 return 0;
74 static void open_help(void)
76 printf(
77 "\n"
78 " opens a new file in the requested mode\n"
79 "\n"
80 " Example:\n"
81 " 'open -Cn /tmp/data' - creates/opens data file read-write and uncached\n"
82 "\n"
83 " Opens a file for subsequent use by all of the other qemu-io commands.\n"
84 " -r, -- open file read-only\n"
85 " -s, -- use snapshot file\n"
86 " -n, -- disable host cache\n"
87 " -o, -- options to be given to the block driver"
88 "\n");
91 static int open_f(BlockBackend *blk, int argc, char **argv);
93 static const cmdinfo_t open_cmd = {
94 .name = "open",
95 .altname = "o",
96 .cfunc = open_f,
97 .argmin = 1,
98 .argmax = -1,
99 .flags = CMD_NOFILE_OK,
100 .args = "[-Crsn] [-o options] [path]",
101 .oneline = "open the file specified by path",
102 .help = open_help,
105 static QemuOptsList empty_opts = {
106 .name = "drive",
107 .merge_lists = true,
108 .head = QTAILQ_HEAD_INITIALIZER(empty_opts.head),
109 .desc = {
110 /* no elements => accept any params */
111 { /* end of list */ }
115 static int open_f(BlockBackend *blk, int argc, char **argv)
117 int flags = 0;
118 int readonly = 0;
119 int c;
120 QemuOpts *qopts;
121 QDict *opts;
123 while ((c = getopt(argc, argv, "snrgo:")) != EOF) {
124 switch (c) {
125 case 's':
126 flags |= BDRV_O_SNAPSHOT;
127 break;
128 case 'n':
129 flags |= BDRV_O_NOCACHE | BDRV_O_CACHE_WB;
130 break;
131 case 'r':
132 readonly = 1;
133 break;
134 case 'o':
135 if (!qemu_opts_parse(&empty_opts, optarg, 0)) {
136 printf("could not parse option list -- %s\n", optarg);
137 qemu_opts_reset(&empty_opts);
138 return 0;
140 break;
141 default:
142 qemu_opts_reset(&empty_opts);
143 return qemuio_command_usage(&open_cmd);
147 if (!readonly) {
148 flags |= BDRV_O_RDWR;
151 qopts = qemu_opts_find(&empty_opts, NULL);
152 opts = qopts ? qemu_opts_to_qdict(qopts, NULL) : NULL;
153 qemu_opts_reset(&empty_opts);
155 if (optind == argc - 1) {
156 return openfile(argv[optind], flags, opts);
157 } else if (optind == argc) {
158 return openfile(NULL, flags, opts);
159 } else {
160 QDECREF(opts);
161 return qemuio_command_usage(&open_cmd);
165 static int quit_f(BlockBackend *blk, int argc, char **argv)
167 return 1;
170 static const cmdinfo_t quit_cmd = {
171 .name = "quit",
172 .altname = "q",
173 .cfunc = quit_f,
174 .argmin = -1,
175 .argmax = -1,
176 .flags = CMD_FLAG_GLOBAL,
177 .oneline = "exit the program",
180 static void usage(const char *name)
182 printf(
183 "Usage: %s [-h] [-V] [-rsnm] [-f FMT] [-c STRING] ... [file]\n"
184 "QEMU Disk exerciser\n"
185 "\n"
186 " -c, --cmd STRING execute command with its arguments\n"
187 " from the given string\n"
188 " -f, --format FMT specifies the block driver to use\n"
189 " -r, --read-only export read-only\n"
190 " -s, --snapshot use snapshot file\n"
191 " -n, --nocache disable host cache\n"
192 " -m, --misalign misalign allocations for O_DIRECT\n"
193 " -k, --native-aio use kernel AIO implementation (on Linux only)\n"
194 " -t, --cache=MODE use the given cache mode for the image\n"
195 " -T, --trace FILE enable trace events listed in the given file\n"
196 " -h, --help display this help and exit\n"
197 " -V, --version output version information and exit\n"
198 "\n"
199 "See '%s -c help' for information on available commands."
200 "\n",
201 name, name);
204 static char *get_prompt(void)
206 static char prompt[FILENAME_MAX + 2 /*"> "*/ + 1 /*"\0"*/ ];
208 if (!prompt[0]) {
209 snprintf(prompt, sizeof(prompt), "%s> ", progname);
212 return prompt;
215 static void GCC_FMT_ATTR(2, 3) readline_printf_func(void *opaque,
216 const char *fmt, ...)
218 va_list ap;
219 va_start(ap, fmt);
220 vprintf(fmt, ap);
221 va_end(ap);
224 static void readline_flush_func(void *opaque)
226 fflush(stdout);
229 static void readline_func(void *opaque, const char *str, void *readline_opaque)
231 char **line = readline_opaque;
232 *line = g_strdup(str);
235 static void completion_match(const char *cmd, void *opaque)
237 readline_add_completion(readline_state, cmd);
240 static void readline_completion_func(void *opaque, const char *str)
242 readline_set_completion_index(readline_state, strlen(str));
243 qemuio_complete_command(str, completion_match, NULL);
246 static char *fetchline_readline(void)
248 char *line = NULL;
250 readline_start(readline_state, get_prompt(), 0, readline_func, &line);
251 while (!line) {
252 int ch = getchar();
253 if (ch == EOF) {
254 break;
256 readline_handle_byte(readline_state, ch);
258 return line;
261 #define MAXREADLINESZ 1024
262 static char *fetchline_fgets(void)
264 char *p, *line = g_malloc(MAXREADLINESZ);
266 if (!fgets(line, MAXREADLINESZ, stdin)) {
267 g_free(line);
268 return NULL;
271 p = line + strlen(line);
272 if (p != line && p[-1] == '\n') {
273 p[-1] = '\0';
276 return line;
279 static char *fetchline(void)
281 if (readline_state) {
282 return fetchline_readline();
283 } else {
284 return fetchline_fgets();
288 static void prep_fetchline(void *opaque)
290 int *fetchable = opaque;
292 qemu_set_fd_handler(STDIN_FILENO, NULL, NULL, NULL);
293 *fetchable= 1;
296 static void command_loop(void)
298 int i, done = 0, fetchable = 0, prompted = 0;
299 char *input;
301 for (i = 0; !done && i < ncmdline; i++) {
302 done = qemuio_command(qemuio_blk, cmdline[i]);
304 if (cmdline) {
305 g_free(cmdline);
306 return;
309 while (!done) {
310 if (!prompted) {
311 printf("%s", get_prompt());
312 fflush(stdout);
313 qemu_set_fd_handler(STDIN_FILENO, prep_fetchline, NULL, &fetchable);
314 prompted = 1;
317 main_loop_wait(false);
319 if (!fetchable) {
320 continue;
323 input = fetchline();
324 if (input == NULL) {
325 break;
327 done = qemuio_command(qemuio_blk, input);
328 g_free(input);
330 prompted = 0;
331 fetchable = 0;
333 qemu_set_fd_handler(STDIN_FILENO, NULL, NULL, NULL);
336 static void add_user_command(char *optarg)
338 cmdline = g_renew(char *, cmdline, ++ncmdline);
339 cmdline[ncmdline-1] = optarg;
342 static void reenable_tty_echo(void)
344 qemu_set_tty_echo(STDIN_FILENO, true);
347 int main(int argc, char **argv)
349 int readonly = 0;
350 const char *sopt = "hVc:d:f:rsnmgkt:T:";
351 const struct option lopt[] = {
352 { "help", 0, NULL, 'h' },
353 { "version", 0, NULL, 'V' },
354 { "offset", 1, NULL, 'o' },
355 { "cmd", 1, NULL, 'c' },
356 { "format", 1, NULL, 'f' },
357 { "read-only", 0, NULL, 'r' },
358 { "snapshot", 0, NULL, 's' },
359 { "nocache", 0, NULL, 'n' },
360 { "misalign", 0, NULL, 'm' },
361 { "native-aio", 0, NULL, 'k' },
362 { "discard", 1, NULL, 'd' },
363 { "cache", 1, NULL, 't' },
364 { "trace", 1, NULL, 'T' },
365 { NULL, 0, NULL, 0 }
367 int c;
368 int opt_index = 0;
369 int flags = BDRV_O_UNMAP;
370 Error *local_error = NULL;
371 QDict *opts = NULL;
373 #ifdef CONFIG_POSIX
374 signal(SIGPIPE, SIG_IGN);
375 #endif
377 progname = basename(argv[0]);
378 qemu_init_exec_dir(argv[0]);
380 bdrv_init();
382 while ((c = getopt_long(argc, argv, sopt, lopt, &opt_index)) != -1) {
383 switch (c) {
384 case 's':
385 flags |= BDRV_O_SNAPSHOT;
386 break;
387 case 'n':
388 flags |= BDRV_O_NOCACHE | BDRV_O_CACHE_WB;
389 break;
390 case 'd':
391 if (bdrv_parse_discard_flags(optarg, &flags) < 0) {
392 error_report("Invalid discard option: %s", optarg);
393 exit(1);
395 break;
396 case 'f':
397 if (!opts) {
398 opts = qdict_new();
400 qdict_put(opts, "driver", qstring_from_str(optarg));
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 = true;
410 break;
411 case 'k':
412 flags |= BDRV_O_NATIVE_AIO;
413 break;
414 case 't':
415 if (bdrv_parse_cache_flags(optarg, &flags) < 0) {
416 error_report("Invalid cache option: %s", optarg);
417 exit(1);
419 break;
420 case 'T':
421 if (!trace_init_backends(optarg, NULL)) {
422 exit(1); /* error message will have been printed */
424 break;
425 case 'V':
426 printf("%s version %s\n", progname, QEMU_VERSION);
427 exit(0);
428 case 'h':
429 usage(progname);
430 exit(0);
431 default:
432 usage(progname);
433 exit(1);
437 if ((argc - optind) > 1) {
438 usage(progname);
439 exit(1);
442 if (qemu_init_main_loop(&local_error)) {
443 error_report_err(local_error);
444 exit(1);
447 /* initialize commands */
448 qemuio_add_command(&quit_cmd);
449 qemuio_add_command(&open_cmd);
450 qemuio_add_command(&close_cmd);
452 if (isatty(STDIN_FILENO)) {
453 readline_state = readline_init(readline_printf_func,
454 readline_flush_func,
455 NULL,
456 readline_completion_func);
457 qemu_set_tty_echo(STDIN_FILENO, false);
458 atexit(reenable_tty_echo);
461 /* open the device */
462 if (!readonly) {
463 flags |= BDRV_O_RDWR;
466 if ((argc - optind) == 1) {
467 openfile(argv[optind], flags, opts);
469 command_loop();
472 * Make sure all outstanding requests complete before the program exits.
474 bdrv_drain_all();
476 blk_unref(qemuio_blk);
477 g_free(readline_state);
478 return 0;