aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Command line utility to exercise the QEMU I/O path. |
| 3 | * |
| 4 | * Copyright (C) 2009 Red Hat, Inc. |
| 5 | * Copyright (c) 2003-2005 Silicon Graphics, Inc. |
| 6 | * |
| 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 | */ |
Stefan Weil | c32d766 | 2009-08-31 22:16:16 +0200 | [diff] [blame] | 10 | #include <sys/time.h> |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 11 | #include <sys/types.h> |
| 12 | #include <stdarg.h> |
| 13 | #include <stdio.h> |
| 14 | #include <getopt.h> |
Stefan Weil | c32d766 | 2009-08-31 22:16:16 +0200 | [diff] [blame] | 15 | #include <libgen.h> |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 16 | |
Kevin Wolf | 3d21994 | 2013-06-05 14:19:39 +0200 | [diff] [blame] | 17 | #include "qemu-io.h" |
Markus Armbruster | d49b683 | 2015-03-17 18:29:20 +0100 | [diff] [blame] | 18 | #include "qemu/error-report.h" |
Paolo Bonzini | 1de7afc | 2012-12-17 18:20:00 +0100 | [diff] [blame] | 19 | #include "qemu/main-loop.h" |
Max Reitz | b543c5c | 2013-10-11 14:02:10 +0200 | [diff] [blame] | 20 | #include "qemu/option.h" |
| 21 | #include "qemu/config-file.h" |
Stefan Hajnoczi | 0cf17e1 | 2013-11-14 11:54:17 +0100 | [diff] [blame] | 22 | #include "qemu/readline.h" |
Markus Armbruster | d49b683 | 2015-03-17 18:29:20 +0100 | [diff] [blame] | 23 | #include "qapi/qmp/qstring.h" |
Markus Armbruster | 26f54e9 | 2014-10-07 13:59:04 +0200 | [diff] [blame] | 24 | #include "sysemu/block-backend.h" |
Paolo Bonzini | 737e150 | 2012-12-17 18:19:44 +0100 | [diff] [blame] | 25 | #include "block/block_int.h" |
Stefan Hajnoczi | d7bb72c | 2012-03-12 16:36:07 +0000 | [diff] [blame] | 26 | #include "trace/control.h" |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 27 | |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 28 | #define CMD_NOFILE_OK 0x01 |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 29 | |
Stefan Weil | f988388 | 2014-03-05 22:23:00 +0100 | [diff] [blame] | 30 | static char *progname; |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 31 | |
Markus Armbruster | 26f54e9 | 2014-10-07 13:59:04 +0200 | [diff] [blame] | 32 | static BlockBackend *qemuio_blk; |
Kevin Wolf | 191c289 | 2010-09-16 13:18:08 +0200 | [diff] [blame] | 33 | |
Kevin Wolf | d1174f1 | 2013-06-05 14:19:37 +0200 | [diff] [blame] | 34 | /* qemu-io commands passed using -c */ |
| 35 | static int ncmdline; |
| 36 | static char **cmdline; |
| 37 | |
Stefan Hajnoczi | 0cf17e1 | 2013-11-14 11:54:17 +0100 | [diff] [blame] | 38 | static ReadLineState *readline_state; |
| 39 | |
Max Reitz | 4c7b7e9 | 2015-02-05 13:58:22 -0500 | [diff] [blame] | 40 | static int close_f(BlockBackend *blk, int argc, char **argv) |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 41 | { |
Markus Armbruster | 26f54e9 | 2014-10-07 13:59:04 +0200 | [diff] [blame] | 42 | blk_unref(qemuio_blk); |
Markus Armbruster | 26f54e9 | 2014-10-07 13:59:04 +0200 | [diff] [blame] | 43 | qemuio_blk = NULL; |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 44 | return 0; |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 45 | } |
| 46 | |
| 47 | static const cmdinfo_t close_cmd = { |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 48 | .name = "close", |
| 49 | .altname = "c", |
| 50 | .cfunc = close_f, |
| 51 | .oneline = "close the current open file", |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 52 | }; |
| 53 | |
Max Reitz | 10d9d75 | 2015-02-05 13:58:21 -0500 | [diff] [blame] | 54 | static int openfile(char *name, int flags, QDict *opts) |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 55 | { |
Max Reitz | 34b5d2c | 2013-09-05 14:45:29 +0200 | [diff] [blame] | 56 | Error *local_err = NULL; |
Daniel P. Berrange | 8caf021 | 2015-05-12 17:09:21 +0100 | [diff] [blame] | 57 | BlockDriverState *bs; |
Max Reitz | 34b5d2c | 2013-09-05 14:45:29 +0200 | [diff] [blame] | 58 | |
Max Reitz | 1b58b43 | 2015-02-05 13:58:20 -0500 | [diff] [blame] | 59 | if (qemuio_blk) { |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 60 | fprintf(stderr, "file open already, try 'help close'\n"); |
Markus Armbruster | 29f2601 | 2014-05-28 11:16:59 +0200 | [diff] [blame] | 61 | QDECREF(opts); |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 62 | return 1; |
| 63 | } |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 64 | |
Max Reitz | 1b58b43 | 2015-02-05 13:58:20 -0500 | [diff] [blame] | 65 | qemuio_blk = blk_new_open("hda", name, NULL, opts, flags, &local_err); |
| 66 | if (!qemuio_blk) { |
Markus Armbruster | dbb651c | 2014-09-08 18:50:58 +0200 | [diff] [blame] | 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); |
Markus Armbruster | dbb651c | 2014-09-08 18:50:58 +0200 | [diff] [blame] | 71 | return 1; |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 72 | } |
Christoph Hellwig | 1db6947 | 2009-07-15 23:11:21 +0200 | [diff] [blame] | 73 | |
Daniel P. Berrange | 8caf021 | 2015-05-12 17:09:21 +0100 | [diff] [blame] | 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; |
| 81 | } |
| 82 | if (bdrv_set_key(bs, password) < 0) { |
| 83 | error_report("invalid password"); |
| 84 | goto error; |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 89 | return 0; |
Daniel P. Berrange | 8caf021 | 2015-05-12 17:09:21 +0100 | [diff] [blame] | 90 | |
| 91 | error: |
| 92 | blk_unref(qemuio_blk); |
| 93 | qemuio_blk = NULL; |
| 94 | return 1; |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 95 | } |
| 96 | |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 97 | static void open_help(void) |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 98 | { |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 99 | printf( |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 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" |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 107 | " -r, -- open file read-only\n" |
| 108 | " -s, -- use snapshot file\n" |
| 109 | " -n, -- disable host cache\n" |
Max Reitz | b543c5c | 2013-10-11 14:02:10 +0200 | [diff] [blame] | 110 | " -o, -- options to be given to the block driver" |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 111 | "\n"); |
| 112 | } |
| 113 | |
Max Reitz | 4c7b7e9 | 2015-02-05 13:58:22 -0500 | [diff] [blame] | 114 | static int open_f(BlockBackend *blk, int argc, char **argv); |
Blue Swirl | 22a2bdc | 2009-11-21 09:06:46 +0000 | [diff] [blame] | 115 | |
| 116 | static const cmdinfo_t open_cmd = { |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 117 | .name = "open", |
| 118 | .altname = "o", |
| 119 | .cfunc = open_f, |
| 120 | .argmin = 1, |
| 121 | .argmax = -1, |
| 122 | .flags = CMD_NOFILE_OK, |
Max Reitz | b543c5c | 2013-10-11 14:02:10 +0200 | [diff] [blame] | 123 | .args = "[-Crsn] [-o options] [path]", |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 124 | .oneline = "open the file specified by path", |
| 125 | .help = open_help, |
Blue Swirl | 22a2bdc | 2009-11-21 09:06:46 +0000 | [diff] [blame] | 126 | }; |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 127 | |
Max Reitz | b543c5c | 2013-10-11 14:02:10 +0200 | [diff] [blame] | 128 | static QemuOptsList empty_opts = { |
| 129 | .name = "drive", |
Markus Armbruster | 443422f | 2014-05-28 11:16:58 +0200 | [diff] [blame] | 130 | .merge_lists = true, |
Max Reitz | b543c5c | 2013-10-11 14:02:10 +0200 | [diff] [blame] | 131 | .head = QTAILQ_HEAD_INITIALIZER(empty_opts.head), |
| 132 | .desc = { |
| 133 | /* no elements => accept any params */ |
| 134 | { /* end of list */ } |
| 135 | }, |
| 136 | }; |
| 137 | |
Max Reitz | 4c7b7e9 | 2015-02-05 13:58:22 -0500 | [diff] [blame] | 138 | static int open_f(BlockBackend *blk, int argc, char **argv) |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 139 | { |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 140 | int flags = 0; |
| 141 | int readonly = 0; |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 142 | int c; |
Max Reitz | b543c5c | 2013-10-11 14:02:10 +0200 | [diff] [blame] | 143 | QemuOpts *qopts; |
Markus Armbruster | 443422f | 2014-05-28 11:16:58 +0200 | [diff] [blame] | 144 | QDict *opts; |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 145 | |
Eric Blake | b062ad8 | 2015-05-12 09:10:56 -0600 | [diff] [blame] | 146 | while ((c = getopt(argc, argv, "snrgo:")) != -1) { |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 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; |
Max Reitz | b543c5c | 2013-10-11 14:02:10 +0200 | [diff] [blame] | 157 | case 'o': |
Markus Armbruster | 70b9433 | 2015-02-13 12:50:26 +0100 | [diff] [blame] | 158 | if (!qemu_opts_parse_noisily(&empty_opts, optarg, false)) { |
Max Reitz | b543c5c | 2013-10-11 14:02:10 +0200 | [diff] [blame] | 159 | printf("could not parse option list -- %s\n", optarg); |
Markus Armbruster | 443422f | 2014-05-28 11:16:58 +0200 | [diff] [blame] | 160 | qemu_opts_reset(&empty_opts); |
Max Reitz | b543c5c | 2013-10-11 14:02:10 +0200 | [diff] [blame] | 161 | return 0; |
| 162 | } |
Max Reitz | b543c5c | 2013-10-11 14:02:10 +0200 | [diff] [blame] | 163 | break; |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 164 | default: |
Markus Armbruster | 443422f | 2014-05-28 11:16:58 +0200 | [diff] [blame] | 165 | qemu_opts_reset(&empty_opts); |
Kevin Wolf | c2cdf5c | 2013-06-05 14:19:36 +0200 | [diff] [blame] | 166 | return qemuio_command_usage(&open_cmd); |
Naphtali Sprei | f5edb01 | 2010-01-17 16:48:13 +0200 | [diff] [blame] | 167 | } |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 168 | } |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 169 | |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 170 | if (!readonly) { |
| 171 | flags |= BDRV_O_RDWR; |
| 172 | } |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 173 | |
Markus Armbruster | 443422f | 2014-05-28 11:16:58 +0200 | [diff] [blame] | 174 | qopts = qemu_opts_find(&empty_opts, NULL); |
| 175 | opts = qopts ? qemu_opts_to_qdict(qopts, NULL) : NULL; |
| 176 | qemu_opts_reset(&empty_opts); |
| 177 | |
Max Reitz | fd0fee3 | 2013-12-20 19:28:20 +0100 | [diff] [blame] | 178 | if (optind == argc - 1) { |
Max Reitz | 10d9d75 | 2015-02-05 13:58:21 -0500 | [diff] [blame] | 179 | return openfile(argv[optind], flags, opts); |
Max Reitz | fd0fee3 | 2013-12-20 19:28:20 +0100 | [diff] [blame] | 180 | } else if (optind == argc) { |
Max Reitz | 10d9d75 | 2015-02-05 13:58:21 -0500 | [diff] [blame] | 181 | return openfile(NULL, flags, opts); |
Max Reitz | fd0fee3 | 2013-12-20 19:28:20 +0100 | [diff] [blame] | 182 | } else { |
Markus Armbruster | 29f2601 | 2014-05-28 11:16:59 +0200 | [diff] [blame] | 183 | QDECREF(opts); |
Kevin Wolf | c2cdf5c | 2013-06-05 14:19:36 +0200 | [diff] [blame] | 184 | return qemuio_command_usage(&open_cmd); |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 185 | } |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 186 | } |
| 187 | |
Max Reitz | 4c7b7e9 | 2015-02-05 13:58:22 -0500 | [diff] [blame] | 188 | static int quit_f(BlockBackend *blk, int argc, char **argv) |
Kevin Wolf | e681be7 | 2013-06-05 14:19:34 +0200 | [diff] [blame] | 189 | { |
| 190 | return 1; |
| 191 | } |
| 192 | |
| 193 | static const cmdinfo_t quit_cmd = { |
| 194 | .name = "quit", |
| 195 | .altname = "q", |
| 196 | .cfunc = quit_f, |
| 197 | .argmin = -1, |
| 198 | .argmax = -1, |
| 199 | .flags = CMD_FLAG_GLOBAL, |
| 200 | .oneline = "exit the program", |
| 201 | }; |
| 202 | |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 203 | static void usage(const char *name) |
| 204 | { |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 205 | printf( |
Kevin Wolf | be6273d | 2014-11-20 16:27:06 +0100 | [diff] [blame] | 206 | "Usage: %s [-h] [-V] [-rsnm] [-f FMT] [-c STRING] ... [file]\n" |
Stefan Weil | 84844a2 | 2009-06-22 15:08:47 +0200 | [diff] [blame] | 207 | "QEMU Disk exerciser\n" |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 208 | "\n" |
Maria Kustova | d208cc3 | 2014-03-18 09:59:19 +0400 | [diff] [blame] | 209 | " -c, --cmd STRING execute command with its arguments\n" |
| 210 | " from the given string\n" |
Kevin Wolf | be6273d | 2014-11-20 16:27:06 +0100 | [diff] [blame] | 211 | " -f, --format FMT specifies the block driver to use\n" |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 212 | " -r, --read-only export read-only\n" |
| 213 | " -s, --snapshot use snapshot file\n" |
| 214 | " -n, --nocache disable host cache\n" |
| 215 | " -m, --misalign misalign allocations for O_DIRECT\n" |
Christoph Hellwig | 5c6c3a6 | 2009-08-20 16:58:35 +0200 | [diff] [blame] | 216 | " -k, --native-aio use kernel AIO implementation (on Linux only)\n" |
Kevin Wolf | 592fa07 | 2012-04-18 12:07:39 +0200 | [diff] [blame] | 217 | " -t, --cache=MODE use the given cache mode for the image\n" |
Stefan Hajnoczi | d7bb72c | 2012-03-12 16:36:07 +0000 | [diff] [blame] | 218 | " -T, --trace FILE enable trace events listed in the given file\n" |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 219 | " -h, --help display this help and exit\n" |
| 220 | " -V, --version output version information and exit\n" |
Maria Kustova | d208cc3 | 2014-03-18 09:59:19 +0400 | [diff] [blame] | 221 | "\n" |
| 222 | "See '%s -c help' for information on available commands." |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 223 | "\n", |
Maria Kustova | d208cc3 | 2014-03-18 09:59:19 +0400 | [diff] [blame] | 224 | name, name); |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 225 | } |
| 226 | |
Kevin Wolf | d1174f1 | 2013-06-05 14:19:37 +0200 | [diff] [blame] | 227 | static char *get_prompt(void) |
| 228 | { |
| 229 | static char prompt[FILENAME_MAX + 2 /*"> "*/ + 1 /*"\0"*/ ]; |
| 230 | |
| 231 | if (!prompt[0]) { |
| 232 | snprintf(prompt, sizeof(prompt), "%s> ", progname); |
| 233 | } |
| 234 | |
| 235 | return prompt; |
| 236 | } |
| 237 | |
Stefan Weil | d5d1507 | 2014-01-25 18:18:23 +0100 | [diff] [blame] | 238 | static void GCC_FMT_ATTR(2, 3) readline_printf_func(void *opaque, |
| 239 | const char *fmt, ...) |
Kevin Wolf | d1174f1 | 2013-06-05 14:19:37 +0200 | [diff] [blame] | 240 | { |
Stefan Hajnoczi | 0cf17e1 | 2013-11-14 11:54:17 +0100 | [diff] [blame] | 241 | va_list ap; |
| 242 | va_start(ap, fmt); |
| 243 | vprintf(fmt, ap); |
| 244 | va_end(ap); |
| 245 | } |
| 246 | |
| 247 | static void readline_flush_func(void *opaque) |
| 248 | { |
| 249 | fflush(stdout); |
| 250 | } |
| 251 | |
| 252 | static void readline_func(void *opaque, const char *str, void *readline_opaque) |
| 253 | { |
| 254 | char **line = readline_opaque; |
| 255 | *line = g_strdup(str); |
| 256 | } |
| 257 | |
Stefan Hajnoczi | 4694020 | 2013-11-14 11:54:18 +0100 | [diff] [blame] | 258 | static void completion_match(const char *cmd, void *opaque) |
| 259 | { |
| 260 | readline_add_completion(readline_state, cmd); |
| 261 | } |
| 262 | |
Stefan Hajnoczi | 0cf17e1 | 2013-11-14 11:54:17 +0100 | [diff] [blame] | 263 | static void readline_completion_func(void *opaque, const char *str) |
| 264 | { |
Stefan Hajnoczi | 4694020 | 2013-11-14 11:54:18 +0100 | [diff] [blame] | 265 | readline_set_completion_index(readline_state, strlen(str)); |
| 266 | qemuio_complete_command(str, completion_match, NULL); |
Stefan Hajnoczi | 0cf17e1 | 2013-11-14 11:54:17 +0100 | [diff] [blame] | 267 | } |
| 268 | |
| 269 | static char *fetchline_readline(void) |
| 270 | { |
| 271 | char *line = NULL; |
| 272 | |
| 273 | readline_start(readline_state, get_prompt(), 0, readline_func, &line); |
| 274 | while (!line) { |
| 275 | int ch = getchar(); |
| 276 | if (ch == EOF) { |
| 277 | break; |
| 278 | } |
| 279 | readline_handle_byte(readline_state, ch); |
Kevin Wolf | d1174f1 | 2013-06-05 14:19:37 +0200 | [diff] [blame] | 280 | } |
| 281 | return line; |
| 282 | } |
Kevin Wolf | d1174f1 | 2013-06-05 14:19:37 +0200 | [diff] [blame] | 283 | |
Stefan Hajnoczi | 0cf17e1 | 2013-11-14 11:54:17 +0100 | [diff] [blame] | 284 | #define MAXREADLINESZ 1024 |
| 285 | static char *fetchline_fgets(void) |
Kevin Wolf | d1174f1 | 2013-06-05 14:19:37 +0200 | [diff] [blame] | 286 | { |
| 287 | char *p, *line = g_malloc(MAXREADLINESZ); |
| 288 | |
| 289 | if (!fgets(line, MAXREADLINESZ, stdin)) { |
| 290 | g_free(line); |
| 291 | return NULL; |
| 292 | } |
| 293 | |
| 294 | p = line + strlen(line); |
| 295 | if (p != line && p[-1] == '\n') { |
| 296 | p[-1] = '\0'; |
| 297 | } |
| 298 | |
| 299 | return line; |
| 300 | } |
Stefan Hajnoczi | 0cf17e1 | 2013-11-14 11:54:17 +0100 | [diff] [blame] | 301 | |
| 302 | static char *fetchline(void) |
| 303 | { |
| 304 | if (readline_state) { |
| 305 | return fetchline_readline(); |
| 306 | } else { |
| 307 | return fetchline_fgets(); |
| 308 | } |
| 309 | } |
Kevin Wolf | d1174f1 | 2013-06-05 14:19:37 +0200 | [diff] [blame] | 310 | |
| 311 | static void prep_fetchline(void *opaque) |
| 312 | { |
| 313 | int *fetchable = opaque; |
| 314 | |
| 315 | qemu_set_fd_handler(STDIN_FILENO, NULL, NULL, NULL); |
| 316 | *fetchable= 1; |
| 317 | } |
| 318 | |
| 319 | static void command_loop(void) |
| 320 | { |
| 321 | int i, done = 0, fetchable = 0, prompted = 0; |
| 322 | char *input; |
| 323 | |
| 324 | for (i = 0; !done && i < ncmdline; i++) { |
Max Reitz | 4c7b7e9 | 2015-02-05 13:58:22 -0500 | [diff] [blame] | 325 | done = qemuio_command(qemuio_blk, cmdline[i]); |
Kevin Wolf | d1174f1 | 2013-06-05 14:19:37 +0200 | [diff] [blame] | 326 | } |
| 327 | if (cmdline) { |
| 328 | g_free(cmdline); |
| 329 | return; |
| 330 | } |
| 331 | |
| 332 | while (!done) { |
| 333 | if (!prompted) { |
| 334 | printf("%s", get_prompt()); |
| 335 | fflush(stdout); |
| 336 | qemu_set_fd_handler(STDIN_FILENO, prep_fetchline, NULL, &fetchable); |
| 337 | prompted = 1; |
| 338 | } |
| 339 | |
| 340 | main_loop_wait(false); |
| 341 | |
| 342 | if (!fetchable) { |
| 343 | continue; |
| 344 | } |
| 345 | |
| 346 | input = fetchline(); |
| 347 | if (input == NULL) { |
| 348 | break; |
| 349 | } |
Max Reitz | 4c7b7e9 | 2015-02-05 13:58:22 -0500 | [diff] [blame] | 350 | done = qemuio_command(qemuio_blk, input); |
Kevin Wolf | d1174f1 | 2013-06-05 14:19:37 +0200 | [diff] [blame] | 351 | g_free(input); |
| 352 | |
| 353 | prompted = 0; |
| 354 | fetchable = 0; |
| 355 | } |
| 356 | qemu_set_fd_handler(STDIN_FILENO, NULL, NULL, NULL); |
| 357 | } |
| 358 | |
| 359 | static void add_user_command(char *optarg) |
| 360 | { |
Markus Armbruster | 5839e53 | 2014-08-19 10:31:08 +0200 | [diff] [blame] | 361 | cmdline = g_renew(char *, cmdline, ++ncmdline); |
Kevin Wolf | d1174f1 | 2013-06-05 14:19:37 +0200 | [diff] [blame] | 362 | cmdline[ncmdline-1] = optarg; |
| 363 | } |
| 364 | |
Stefan Hajnoczi | 0cf17e1 | 2013-11-14 11:54:17 +0100 | [diff] [blame] | 365 | static void reenable_tty_echo(void) |
| 366 | { |
| 367 | qemu_set_tty_echo(STDIN_FILENO, true); |
| 368 | } |
| 369 | |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 370 | int main(int argc, char **argv) |
| 371 | { |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 372 | int readonly = 0; |
Kevin Wolf | be6273d | 2014-11-20 16:27:06 +0100 | [diff] [blame] | 373 | const char *sopt = "hVc:d:f:rsnmgkt:T:"; |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 374 | const struct option lopt[] = { |
| 375 | { "help", 0, NULL, 'h' }, |
| 376 | { "version", 0, NULL, 'V' }, |
| 377 | { "offset", 1, NULL, 'o' }, |
| 378 | { "cmd", 1, NULL, 'c' }, |
Kevin Wolf | be6273d | 2014-11-20 16:27:06 +0100 | [diff] [blame] | 379 | { "format", 1, NULL, 'f' }, |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 380 | { "read-only", 0, NULL, 'r' }, |
| 381 | { "snapshot", 0, NULL, 's' }, |
| 382 | { "nocache", 0, NULL, 'n' }, |
| 383 | { "misalign", 0, NULL, 'm' }, |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 384 | { "native-aio", 0, NULL, 'k' }, |
Paolo Bonzini | 9e8f183 | 2013-02-08 14:06:11 +0100 | [diff] [blame] | 385 | { "discard", 1, NULL, 'd' }, |
Kevin Wolf | 592fa07 | 2012-04-18 12:07:39 +0200 | [diff] [blame] | 386 | { "cache", 1, NULL, 't' }, |
Stefan Hajnoczi | d7bb72c | 2012-03-12 16:36:07 +0000 | [diff] [blame] | 387 | { "trace", 1, NULL, 'T' }, |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 388 | { NULL, 0, NULL, 0 } |
| 389 | }; |
| 390 | int c; |
| 391 | int opt_index = 0; |
Paolo Bonzini | 9e8f183 | 2013-02-08 14:06:11 +0100 | [diff] [blame] | 392 | int flags = BDRV_O_UNMAP; |
Chrysostomos Nanakos | 2f78e49 | 2014-09-18 14:30:49 +0300 | [diff] [blame] | 393 | Error *local_error = NULL; |
Max Reitz | 1b58b43 | 2015-02-05 13:58:20 -0500 | [diff] [blame] | 394 | QDict *opts = NULL; |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 395 | |
MORITA Kazutaka | 526eda1 | 2013-07-23 17:30:11 +0900 | [diff] [blame] | 396 | #ifdef CONFIG_POSIX |
| 397 | signal(SIGPIPE, SIG_IGN); |
| 398 | #endif |
| 399 | |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 400 | progname = basename(argv[0]); |
Fam Zheng | 10f5bff | 2014-02-10 14:48:51 +0800 | [diff] [blame] | 401 | qemu_init_exec_dir(argv[0]); |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 402 | |
Kevin Wolf | be6273d | 2014-11-20 16:27:06 +0100 | [diff] [blame] | 403 | bdrv_init(); |
| 404 | |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 405 | while ((c = getopt_long(argc, argv, sopt, lopt, &opt_index)) != -1) { |
| 406 | switch (c) { |
| 407 | case 's': |
| 408 | flags |= BDRV_O_SNAPSHOT; |
| 409 | break; |
| 410 | case 'n': |
| 411 | flags |= BDRV_O_NOCACHE | BDRV_O_CACHE_WB; |
| 412 | break; |
Paolo Bonzini | 9e8f183 | 2013-02-08 14:06:11 +0100 | [diff] [blame] | 413 | case 'd': |
| 414 | if (bdrv_parse_discard_flags(optarg, &flags) < 0) { |
| 415 | error_report("Invalid discard option: %s", optarg); |
| 416 | exit(1); |
| 417 | } |
| 418 | break; |
Kevin Wolf | be6273d | 2014-11-20 16:27:06 +0100 | [diff] [blame] | 419 | case 'f': |
Max Reitz | 1b58b43 | 2015-02-05 13:58:20 -0500 | [diff] [blame] | 420 | if (!opts) { |
| 421 | opts = qdict_new(); |
Kevin Wolf | be6273d | 2014-11-20 16:27:06 +0100 | [diff] [blame] | 422 | } |
Max Reitz | 1b58b43 | 2015-02-05 13:58:20 -0500 | [diff] [blame] | 423 | qdict_put(opts, "driver", qstring_from_str(optarg)); |
Kevin Wolf | be6273d | 2014-11-20 16:27:06 +0100 | [diff] [blame] | 424 | break; |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 425 | case 'c': |
| 426 | add_user_command(optarg); |
| 427 | break; |
| 428 | case 'r': |
| 429 | readonly = 1; |
| 430 | break; |
| 431 | case 'm': |
Stefan Weil | f988388 | 2014-03-05 22:23:00 +0100 | [diff] [blame] | 432 | qemuio_misalign = true; |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 433 | break; |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 434 | case 'k': |
| 435 | flags |= BDRV_O_NATIVE_AIO; |
| 436 | break; |
Kevin Wolf | 592fa07 | 2012-04-18 12:07:39 +0200 | [diff] [blame] | 437 | case 't': |
| 438 | if (bdrv_parse_cache_flags(optarg, &flags) < 0) { |
| 439 | error_report("Invalid cache option: %s", optarg); |
| 440 | exit(1); |
| 441 | } |
| 442 | break; |
Stefan Hajnoczi | d7bb72c | 2012-03-12 16:36:07 +0000 | [diff] [blame] | 443 | case 'T': |
LluĂs Vilanova | 5b80827 | 2014-05-27 15:02:14 +0200 | [diff] [blame] | 444 | if (!trace_init_backends(optarg, NULL)) { |
Stefan Hajnoczi | d7bb72c | 2012-03-12 16:36:07 +0000 | [diff] [blame] | 445 | exit(1); /* error message will have been printed */ |
| 446 | } |
| 447 | break; |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 448 | case 'V': |
Kevin Wolf | 02da386 | 2013-06-05 14:19:40 +0200 | [diff] [blame] | 449 | printf("%s version %s\n", progname, QEMU_VERSION); |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 450 | exit(0); |
| 451 | case 'h': |
| 452 | usage(progname); |
| 453 | exit(0); |
| 454 | default: |
| 455 | usage(progname); |
| 456 | exit(1); |
Naphtali Sprei | f5edb01 | 2010-01-17 16:48:13 +0200 | [diff] [blame] | 457 | } |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 458 | } |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 459 | |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 460 | if ((argc - optind) > 1) { |
| 461 | usage(progname); |
| 462 | exit(1); |
| 463 | } |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 464 | |
Chrysostomos Nanakos | 2f78e49 | 2014-09-18 14:30:49 +0300 | [diff] [blame] | 465 | if (qemu_init_main_loop(&local_error)) { |
Markus Armbruster | 565f65d | 2015-02-12 13:55:05 +0100 | [diff] [blame] | 466 | error_report_err(local_error); |
Chrysostomos Nanakos | 2f78e49 | 2014-09-18 14:30:49 +0300 | [diff] [blame] | 467 | exit(1); |
| 468 | } |
Zhi Yong Wu | a57d114 | 2012-02-19 22:24:59 +0800 | [diff] [blame] | 469 | |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 470 | /* initialize commands */ |
Kevin Wolf | c2cdf5c | 2013-06-05 14:19:36 +0200 | [diff] [blame] | 471 | qemuio_add_command(&quit_cmd); |
| 472 | qemuio_add_command(&open_cmd); |
| 473 | qemuio_add_command(&close_cmd); |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 474 | |
Stefan Hajnoczi | 0cf17e1 | 2013-11-14 11:54:17 +0100 | [diff] [blame] | 475 | if (isatty(STDIN_FILENO)) { |
| 476 | readline_state = readline_init(readline_printf_func, |
| 477 | readline_flush_func, |
| 478 | NULL, |
| 479 | readline_completion_func); |
| 480 | qemu_set_tty_echo(STDIN_FILENO, false); |
| 481 | atexit(reenable_tty_echo); |
| 482 | } |
| 483 | |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 484 | /* open the device */ |
| 485 | if (!readonly) { |
| 486 | flags |= BDRV_O_RDWR; |
| 487 | } |
| 488 | |
| 489 | if ((argc - optind) == 1) { |
Max Reitz | 10d9d75 | 2015-02-05 13:58:21 -0500 | [diff] [blame] | 490 | openfile(argv[optind], flags, opts); |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 491 | } |
| 492 | command_loop(); |
| 493 | |
| 494 | /* |
Stefan Hajnoczi | 922453b | 2011-11-30 12:23:43 +0000 | [diff] [blame] | 495 | * Make sure all outstanding requests complete before the program exits. |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 496 | */ |
Stefan Hajnoczi | 922453b | 2011-11-30 12:23:43 +0000 | [diff] [blame] | 497 | bdrv_drain_all(); |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 498 | |
Markus Armbruster | 26f54e9 | 2014-10-07 13:59:04 +0200 | [diff] [blame] | 499 | blk_unref(qemuio_blk); |
Stefan Hajnoczi | 0cf17e1 | 2013-11-14 11:54:17 +0100 | [diff] [blame] | 500 | g_free(readline_state); |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 501 | return 0; |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 502 | } |