virtio-input: fix emulated tablet axis ranges
[qemu/kevin.git] / qemu-io.c
blob288bba84b9c229c4158777bc28c287881dd0bf0a
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 "qemu/osdep.h"
11 #include <getopt.h>
12 #include <libgen.h>
14 #include "qapi/error.h"
15 #include "qemu-io.h"
16 #include "qemu/error-report.h"
17 #include "qemu/main-loop.h"
18 #include "qemu/option.h"
19 #include "qemu/config-file.h"
20 #include "qemu/readline.h"
21 #include "qapi/qmp/qstring.h"
22 #include "qom/object_interfaces.h"
23 #include "sysemu/block-backend.h"
24 #include "block/block_int.h"
25 #include "trace/control.h"
26 #include "crypto/init.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;
37 static bool imageOpts;
39 static ReadLineState *readline_state;
41 static int close_f(BlockBackend *blk, int argc, char **argv)
43 blk_unref(qemuio_blk);
44 qemuio_blk = NULL;
45 return 0;
48 static const cmdinfo_t close_cmd = {
49 .name = "close",
50 .altname = "c",
51 .cfunc = close_f,
52 .oneline = "close the current open file",
55 static int openfile(char *name, int flags, bool writethrough, QDict *opts)
57 Error *local_err = NULL;
58 BlockDriverState *bs;
60 if (qemuio_blk) {
61 error_report("file open already, try 'help close'");
62 QDECREF(opts);
63 return 1;
66 qemuio_blk = blk_new_open(name, NULL, opts, flags, &local_err);
67 if (!qemuio_blk) {
68 error_reportf_err(local_err, "can't open%s%s: ",
69 name ? " device " : "", name ?: "");
70 return 1;
73 bs = blk_bs(qemuio_blk);
74 if (bdrv_is_encrypted(bs) && bdrv_key_required(bs)) {
75 char password[256];
76 printf("Disk image '%s' is encrypted.\n", name);
77 if (qemu_read_password(password, sizeof(password)) < 0) {
78 error_report("No password given");
79 goto error;
81 if (bdrv_set_key(bs, password) < 0) {
82 error_report("invalid password");
83 goto error;
87 blk_set_enable_write_cache(qemuio_blk, !writethrough);
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 bool writethrough = true;
143 int c;
144 QemuOpts *qopts;
145 QDict *opts;
147 while ((c = getopt(argc, argv, "snrgo:")) != -1) {
148 switch (c) {
149 case 's':
150 flags |= BDRV_O_SNAPSHOT;
151 break;
152 case 'n':
153 flags |= BDRV_O_NOCACHE;
154 writethrough = false;
155 break;
156 case 'r':
157 readonly = 1;
158 break;
159 case 'o':
160 if (imageOpts) {
161 printf("--image-opts and 'open -o' are mutually exclusive\n");
162 return 0;
164 if (!qemu_opts_parse_noisily(&empty_opts, optarg, false)) {
165 qemu_opts_reset(&empty_opts);
166 return 0;
168 break;
169 default:
170 qemu_opts_reset(&empty_opts);
171 return qemuio_command_usage(&open_cmd);
175 if (!readonly) {
176 flags |= BDRV_O_RDWR;
179 if (imageOpts && (optind == argc - 1)) {
180 if (!qemu_opts_parse_noisily(&empty_opts, argv[optind], false)) {
181 qemu_opts_reset(&empty_opts);
182 return 0;
184 optind++;
187 qopts = qemu_opts_find(&empty_opts, NULL);
188 opts = qopts ? qemu_opts_to_qdict(qopts, NULL) : NULL;
189 qemu_opts_reset(&empty_opts);
191 if (optind == argc - 1) {
192 return openfile(argv[optind], flags, writethrough, opts);
193 } else if (optind == argc) {
194 return openfile(NULL, flags, writethrough, opts);
195 } else {
196 QDECREF(opts);
197 return qemuio_command_usage(&open_cmd);
201 static int quit_f(BlockBackend *blk, int argc, char **argv)
203 return 1;
206 static const cmdinfo_t quit_cmd = {
207 .name = "quit",
208 .altname = "q",
209 .cfunc = quit_f,
210 .argmin = -1,
211 .argmax = -1,
212 .flags = CMD_FLAG_GLOBAL,
213 .oneline = "exit the program",
216 static void usage(const char *name)
218 printf(
219 "Usage: %s [-h] [-V] [-rsnm] [-f FMT] [-c STRING] ... [file]\n"
220 "QEMU Disk exerciser\n"
221 "\n"
222 " --object OBJECTDEF define an object such as 'secret' for\n"
223 " passwords and/or encryption keys\n"
224 " -c, --cmd STRING execute command with its arguments\n"
225 " from the given string\n"
226 " -f, --format FMT specifies the block driver to use\n"
227 " -r, --read-only export read-only\n"
228 " -s, --snapshot use snapshot file\n"
229 " -n, --nocache disable host cache\n"
230 " -m, --misalign misalign allocations for O_DIRECT\n"
231 " -k, --native-aio use kernel AIO implementation (on Linux only)\n"
232 " -t, --cache=MODE use the given cache mode for the image\n"
233 " -T, --trace FILE enable trace events listed in the given file\n"
234 " -h, --help display this help and exit\n"
235 " -V, --version output version information and exit\n"
236 "\n"
237 "See '%s -c help' for information on available commands."
238 "\n",
239 name, name);
242 static char *get_prompt(void)
244 static char prompt[FILENAME_MAX + 2 /*"> "*/ + 1 /*"\0"*/ ];
246 if (!prompt[0]) {
247 snprintf(prompt, sizeof(prompt), "%s> ", progname);
250 return prompt;
253 static void GCC_FMT_ATTR(2, 3) readline_printf_func(void *opaque,
254 const char *fmt, ...)
256 va_list ap;
257 va_start(ap, fmt);
258 vprintf(fmt, ap);
259 va_end(ap);
262 static void readline_flush_func(void *opaque)
264 fflush(stdout);
267 static void readline_func(void *opaque, const char *str, void *readline_opaque)
269 char **line = readline_opaque;
270 *line = g_strdup(str);
273 static void completion_match(const char *cmd, void *opaque)
275 readline_add_completion(readline_state, cmd);
278 static void readline_completion_func(void *opaque, const char *str)
280 readline_set_completion_index(readline_state, strlen(str));
281 qemuio_complete_command(str, completion_match, NULL);
284 static char *fetchline_readline(void)
286 char *line = NULL;
288 readline_start(readline_state, get_prompt(), 0, readline_func, &line);
289 while (!line) {
290 int ch = getchar();
291 if (ch == EOF) {
292 break;
294 readline_handle_byte(readline_state, ch);
296 return line;
299 #define MAXREADLINESZ 1024
300 static char *fetchline_fgets(void)
302 char *p, *line = g_malloc(MAXREADLINESZ);
304 if (!fgets(line, MAXREADLINESZ, stdin)) {
305 g_free(line);
306 return NULL;
309 p = line + strlen(line);
310 if (p != line && p[-1] == '\n') {
311 p[-1] = '\0';
314 return line;
317 static char *fetchline(void)
319 if (readline_state) {
320 return fetchline_readline();
321 } else {
322 return fetchline_fgets();
326 static void prep_fetchline(void *opaque)
328 int *fetchable = opaque;
330 qemu_set_fd_handler(STDIN_FILENO, NULL, NULL, NULL);
331 *fetchable= 1;
334 static void command_loop(void)
336 int i, done = 0, fetchable = 0, prompted = 0;
337 char *input;
339 for (i = 0; !done && i < ncmdline; i++) {
340 done = qemuio_command(qemuio_blk, cmdline[i]);
342 if (cmdline) {
343 g_free(cmdline);
344 return;
347 while (!done) {
348 if (!prompted) {
349 printf("%s", get_prompt());
350 fflush(stdout);
351 qemu_set_fd_handler(STDIN_FILENO, prep_fetchline, NULL, &fetchable);
352 prompted = 1;
355 main_loop_wait(false);
357 if (!fetchable) {
358 continue;
361 input = fetchline();
362 if (input == NULL) {
363 break;
365 done = qemuio_command(qemuio_blk, input);
366 g_free(input);
368 prompted = 0;
369 fetchable = 0;
371 qemu_set_fd_handler(STDIN_FILENO, NULL, NULL, NULL);
374 static void add_user_command(char *optarg)
376 cmdline = g_renew(char *, cmdline, ++ncmdline);
377 cmdline[ncmdline-1] = optarg;
380 static void reenable_tty_echo(void)
382 qemu_set_tty_echo(STDIN_FILENO, true);
385 enum {
386 OPTION_OBJECT = 256,
387 OPTION_IMAGE_OPTS = 257,
390 static QemuOptsList qemu_object_opts = {
391 .name = "object",
392 .implied_opt_name = "qom-type",
393 .head = QTAILQ_HEAD_INITIALIZER(qemu_object_opts.head),
394 .desc = {
400 static QemuOptsList file_opts = {
401 .name = "file",
402 .implied_opt_name = "file",
403 .head = QTAILQ_HEAD_INITIALIZER(file_opts.head),
404 .desc = {
405 /* no elements => accept any params */
406 { /* end of list */ }
410 int main(int argc, char **argv)
412 int readonly = 0;
413 const char *sopt = "hVc:d:f:rsnmgkt:T:";
414 const struct option lopt[] = {
415 { "help", no_argument, NULL, 'h' },
416 { "version", no_argument, NULL, 'V' },
417 { "offset", required_argument, NULL, 'o' },
418 { "cmd", required_argument, NULL, 'c' },
419 { "format", required_argument, NULL, 'f' },
420 { "read-only", no_argument, NULL, 'r' },
421 { "snapshot", no_argument, NULL, 's' },
422 { "nocache", no_argument, NULL, 'n' },
423 { "misalign", no_argument, NULL, 'm' },
424 { "native-aio", no_argument, NULL, 'k' },
425 { "discard", required_argument, NULL, 'd' },
426 { "cache", required_argument, NULL, 't' },
427 { "trace", required_argument, NULL, 'T' },
428 { "object", required_argument, NULL, OPTION_OBJECT },
429 { "image-opts", no_argument, NULL, OPTION_IMAGE_OPTS },
430 { NULL, 0, NULL, 0 }
432 int c;
433 int opt_index = 0;
434 int flags = BDRV_O_UNMAP;
435 bool writethrough = true;
436 Error *local_error = NULL;
437 QDict *opts = NULL;
438 const char *format = NULL;
440 #ifdef CONFIG_POSIX
441 signal(SIGPIPE, SIG_IGN);
442 #endif
444 progname = basename(argv[0]);
445 qemu_init_exec_dir(argv[0]);
447 if (qcrypto_init(&local_error) < 0) {
448 error_reportf_err(local_error, "cannot initialize crypto: ");
449 exit(1);
452 module_call_init(MODULE_INIT_QOM);
453 qemu_add_opts(&qemu_object_opts);
454 bdrv_init();
456 while ((c = getopt_long(argc, argv, sopt, lopt, &opt_index)) != -1) {
457 switch (c) {
458 case 's':
459 flags |= BDRV_O_SNAPSHOT;
460 break;
461 case 'n':
462 flags |= BDRV_O_NOCACHE;
463 writethrough = false;
464 break;
465 case 'd':
466 if (bdrv_parse_discard_flags(optarg, &flags) < 0) {
467 error_report("Invalid discard option: %s", optarg);
468 exit(1);
470 break;
471 case 'f':
472 format = optarg;
473 break;
474 case 'c':
475 add_user_command(optarg);
476 break;
477 case 'r':
478 readonly = 1;
479 break;
480 case 'm':
481 qemuio_misalign = true;
482 break;
483 case 'k':
484 flags |= BDRV_O_NATIVE_AIO;
485 break;
486 case 't':
487 if (bdrv_parse_cache_mode(optarg, &flags, &writethrough) < 0) {
488 error_report("Invalid cache option: %s", optarg);
489 exit(1);
491 break;
492 case 'T':
493 if (!trace_init_backends()) {
494 exit(1); /* error message will have been printed */
496 break;
497 case 'V':
498 printf("%s version %s\n", progname, QEMU_VERSION);
499 exit(0);
500 case 'h':
501 usage(progname);
502 exit(0);
503 case OPTION_OBJECT: {
504 QemuOpts *qopts;
505 qopts = qemu_opts_parse_noisily(&qemu_object_opts,
506 optarg, true);
507 if (!qopts) {
508 exit(1);
510 } break;
511 case OPTION_IMAGE_OPTS:
512 imageOpts = true;
513 break;
514 default:
515 usage(progname);
516 exit(1);
520 if ((argc - optind) > 1) {
521 usage(progname);
522 exit(1);
525 if (format && imageOpts) {
526 error_report("--image-opts and -f are mutually exclusive");
527 exit(1);
530 if (qemu_init_main_loop(&local_error)) {
531 error_report_err(local_error);
532 exit(1);
535 if (qemu_opts_foreach(&qemu_object_opts,
536 user_creatable_add_opts_foreach,
537 NULL, &local_error)) {
538 error_report_err(local_error);
539 exit(1);
542 /* initialize commands */
543 qemuio_add_command(&quit_cmd);
544 qemuio_add_command(&open_cmd);
545 qemuio_add_command(&close_cmd);
547 if (isatty(STDIN_FILENO)) {
548 readline_state = readline_init(readline_printf_func,
549 readline_flush_func,
550 NULL,
551 readline_completion_func);
552 qemu_set_tty_echo(STDIN_FILENO, false);
553 atexit(reenable_tty_echo);
556 /* open the device */
557 if (!readonly) {
558 flags |= BDRV_O_RDWR;
561 if ((argc - optind) == 1) {
562 if (imageOpts) {
563 QemuOpts *qopts = NULL;
564 qopts = qemu_opts_parse_noisily(&file_opts, argv[optind], false);
565 if (!qopts) {
566 exit(1);
568 opts = qemu_opts_to_qdict(qopts, NULL);
569 openfile(NULL, flags, writethrough, opts);
570 } else {
571 if (format) {
572 opts = qdict_new();
573 qdict_put(opts, "driver", qstring_from_str(format));
575 openfile(argv[optind], flags, writethrough, opts);
578 command_loop();
581 * Make sure all outstanding requests complete before the program exits.
583 bdrv_drain_all();
585 blk_unref(qemuio_blk);
586 g_free(readline_state);
587 return 0;