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)) { |
Markus Armbruster | 443422f | 2014-05-28 11:16:58 +0200 | [diff] [blame] | 159 | qemu_opts_reset(&empty_opts); |
Max Reitz | b543c5c | 2013-10-11 14:02:10 +0200 | [diff] [blame] | 160 | return 0; |
| 161 | } |
Max Reitz | b543c5c | 2013-10-11 14:02:10 +0200 | [diff] [blame] | 162 | break; |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 163 | default: |
Markus Armbruster | 443422f | 2014-05-28 11:16:58 +0200 | [diff] [blame] | 164 | qemu_opts_reset(&empty_opts); |
Kevin Wolf | c2cdf5c | 2013-06-05 14:19:36 +0200 | [diff] [blame] | 165 | return qemuio_command_usage(&open_cmd); |
Naphtali Sprei | f5edb01 | 2010-01-17 16:48:13 +0200 | [diff] [blame] | 166 | } |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 167 | } |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 168 | |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 169 | if (!readonly) { |
| 170 | flags |= BDRV_O_RDWR; |
| 171 | } |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 172 | |
Markus Armbruster | 443422f | 2014-05-28 11:16:58 +0200 | [diff] [blame] | 173 | qopts = qemu_opts_find(&empty_opts, NULL); |
| 174 | opts = qopts ? qemu_opts_to_qdict(qopts, NULL) : NULL; |
| 175 | qemu_opts_reset(&empty_opts); |
| 176 | |
Max Reitz | fd0fee3 | 2013-12-20 19:28:20 +0100 | [diff] [blame] | 177 | if (optind == argc - 1) { |
Max Reitz | 10d9d75 | 2015-02-05 13:58:21 -0500 | [diff] [blame] | 178 | return openfile(argv[optind], flags, opts); |
Max Reitz | fd0fee3 | 2013-12-20 19:28:20 +0100 | [diff] [blame] | 179 | } else if (optind == argc) { |
Max Reitz | 10d9d75 | 2015-02-05 13:58:21 -0500 | [diff] [blame] | 180 | return openfile(NULL, flags, opts); |
Max Reitz | fd0fee3 | 2013-12-20 19:28:20 +0100 | [diff] [blame] | 181 | } else { |
Markus Armbruster | 29f2601 | 2014-05-28 11:16:59 +0200 | [diff] [blame] | 182 | QDECREF(opts); |
Kevin Wolf | c2cdf5c | 2013-06-05 14:19:36 +0200 | [diff] [blame] | 183 | return qemuio_command_usage(&open_cmd); |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 184 | } |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 185 | } |
| 186 | |
Max Reitz | 4c7b7e9 | 2015-02-05 13:58:22 -0500 | [diff] [blame] | 187 | static int quit_f(BlockBackend *blk, int argc, char **argv) |
Kevin Wolf | e681be7 | 2013-06-05 14:19:34 +0200 | [diff] [blame] | 188 | { |
| 189 | return 1; |
| 190 | } |
| 191 | |
| 192 | static const cmdinfo_t quit_cmd = { |
| 193 | .name = "quit", |
| 194 | .altname = "q", |
| 195 | .cfunc = quit_f, |
| 196 | .argmin = -1, |
| 197 | .argmax = -1, |
| 198 | .flags = CMD_FLAG_GLOBAL, |
| 199 | .oneline = "exit the program", |
| 200 | }; |
| 201 | |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 202 | static void usage(const char *name) |
| 203 | { |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 204 | printf( |
Kevin Wolf | be6273d | 2014-11-20 16:27:06 +0100 | [diff] [blame] | 205 | "Usage: %s [-h] [-V] [-rsnm] [-f FMT] [-c STRING] ... [file]\n" |
Stefan Weil | 84844a2 | 2009-06-22 15:08:47 +0200 | [diff] [blame] | 206 | "QEMU Disk exerciser\n" |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 207 | "\n" |
Maria Kustova | d208cc3 | 2014-03-18 09:59:19 +0400 | [diff] [blame] | 208 | " -c, --cmd STRING execute command with its arguments\n" |
| 209 | " from the given string\n" |
Kevin Wolf | be6273d | 2014-11-20 16:27:06 +0100 | [diff] [blame] | 210 | " -f, --format FMT specifies the block driver to use\n" |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 211 | " -r, --read-only export read-only\n" |
| 212 | " -s, --snapshot use snapshot file\n" |
| 213 | " -n, --nocache disable host cache\n" |
| 214 | " -m, --misalign misalign allocations for O_DIRECT\n" |
Christoph Hellwig | 5c6c3a6 | 2009-08-20 16:58:35 +0200 | [diff] [blame] | 215 | " -k, --native-aio use kernel AIO implementation (on Linux only)\n" |
Kevin Wolf | 592fa07 | 2012-04-18 12:07:39 +0200 | [diff] [blame] | 216 | " -t, --cache=MODE use the given cache mode for the image\n" |
Stefan Hajnoczi | d7bb72c | 2012-03-12 16:36:07 +0000 | [diff] [blame] | 217 | " -T, --trace FILE enable trace events listed in the given file\n" |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 218 | " -h, --help display this help and exit\n" |
| 219 | " -V, --version output version information and exit\n" |
Maria Kustova | d208cc3 | 2014-03-18 09:59:19 +0400 | [diff] [blame] | 220 | "\n" |
| 221 | "See '%s -c help' for information on available commands." |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 222 | "\n", |
Maria Kustova | d208cc3 | 2014-03-18 09:59:19 +0400 | [diff] [blame] | 223 | name, name); |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 224 | } |
| 225 | |
Kevin Wolf | d1174f1 | 2013-06-05 14:19:37 +0200 | [diff] [blame] | 226 | static char *get_prompt(void) |
| 227 | { |
| 228 | static char prompt[FILENAME_MAX + 2 /*"> "*/ + 1 /*"\0"*/ ]; |
| 229 | |
| 230 | if (!prompt[0]) { |
| 231 | snprintf(prompt, sizeof(prompt), "%s> ", progname); |
| 232 | } |
| 233 | |
| 234 | return prompt; |
| 235 | } |
| 236 | |
Stefan Weil | d5d1507 | 2014-01-25 18:18:23 +0100 | [diff] [blame] | 237 | static void GCC_FMT_ATTR(2, 3) readline_printf_func(void *opaque, |
| 238 | const char *fmt, ...) |
Kevin Wolf | d1174f1 | 2013-06-05 14:19:37 +0200 | [diff] [blame] | 239 | { |
Stefan Hajnoczi | 0cf17e1 | 2013-11-14 11:54:17 +0100 | [diff] [blame] | 240 | va_list ap; |
| 241 | va_start(ap, fmt); |
| 242 | vprintf(fmt, ap); |
| 243 | va_end(ap); |
| 244 | } |
| 245 | |
| 246 | static void readline_flush_func(void *opaque) |
| 247 | { |
| 248 | fflush(stdout); |
| 249 | } |
| 250 | |
| 251 | static void readline_func(void *opaque, const char *str, void *readline_opaque) |
| 252 | { |
| 253 | char **line = readline_opaque; |
| 254 | *line = g_strdup(str); |
| 255 | } |
| 256 | |
Stefan Hajnoczi | 4694020 | 2013-11-14 11:54:18 +0100 | [diff] [blame] | 257 | static void completion_match(const char *cmd, void *opaque) |
| 258 | { |
| 259 | readline_add_completion(readline_state, cmd); |
| 260 | } |
| 261 | |
Stefan Hajnoczi | 0cf17e1 | 2013-11-14 11:54:17 +0100 | [diff] [blame] | 262 | static void readline_completion_func(void *opaque, const char *str) |
| 263 | { |
Stefan Hajnoczi | 4694020 | 2013-11-14 11:54:18 +0100 | [diff] [blame] | 264 | readline_set_completion_index(readline_state, strlen(str)); |
| 265 | qemuio_complete_command(str, completion_match, NULL); |
Stefan Hajnoczi | 0cf17e1 | 2013-11-14 11:54:17 +0100 | [diff] [blame] | 266 | } |
| 267 | |
| 268 | static char *fetchline_readline(void) |
| 269 | { |
| 270 | char *line = NULL; |
| 271 | |
| 272 | readline_start(readline_state, get_prompt(), 0, readline_func, &line); |
| 273 | while (!line) { |
| 274 | int ch = getchar(); |
| 275 | if (ch == EOF) { |
| 276 | break; |
| 277 | } |
| 278 | readline_handle_byte(readline_state, ch); |
Kevin Wolf | d1174f1 | 2013-06-05 14:19:37 +0200 | [diff] [blame] | 279 | } |
| 280 | return line; |
| 281 | } |
Kevin Wolf | d1174f1 | 2013-06-05 14:19:37 +0200 | [diff] [blame] | 282 | |
Stefan Hajnoczi | 0cf17e1 | 2013-11-14 11:54:17 +0100 | [diff] [blame] | 283 | #define MAXREADLINESZ 1024 |
| 284 | static char *fetchline_fgets(void) |
Kevin Wolf | d1174f1 | 2013-06-05 14:19:37 +0200 | [diff] [blame] | 285 | { |
| 286 | char *p, *line = g_malloc(MAXREADLINESZ); |
| 287 | |
| 288 | if (!fgets(line, MAXREADLINESZ, stdin)) { |
| 289 | g_free(line); |
| 290 | return NULL; |
| 291 | } |
| 292 | |
| 293 | p = line + strlen(line); |
| 294 | if (p != line && p[-1] == '\n') { |
| 295 | p[-1] = '\0'; |
| 296 | } |
| 297 | |
| 298 | return line; |
| 299 | } |
Stefan Hajnoczi | 0cf17e1 | 2013-11-14 11:54:17 +0100 | [diff] [blame] | 300 | |
| 301 | static char *fetchline(void) |
| 302 | { |
| 303 | if (readline_state) { |
| 304 | return fetchline_readline(); |
| 305 | } else { |
| 306 | return fetchline_fgets(); |
| 307 | } |
| 308 | } |
Kevin Wolf | d1174f1 | 2013-06-05 14:19:37 +0200 | [diff] [blame] | 309 | |
| 310 | static void prep_fetchline(void *opaque) |
| 311 | { |
| 312 | int *fetchable = opaque; |
| 313 | |
| 314 | qemu_set_fd_handler(STDIN_FILENO, NULL, NULL, NULL); |
| 315 | *fetchable= 1; |
| 316 | } |
| 317 | |
| 318 | static void command_loop(void) |
| 319 | { |
| 320 | int i, done = 0, fetchable = 0, prompted = 0; |
| 321 | char *input; |
| 322 | |
| 323 | for (i = 0; !done && i < ncmdline; i++) { |
Max Reitz | 4c7b7e9 | 2015-02-05 13:58:22 -0500 | [diff] [blame] | 324 | done = qemuio_command(qemuio_blk, cmdline[i]); |
Kevin Wolf | d1174f1 | 2013-06-05 14:19:37 +0200 | [diff] [blame] | 325 | } |
| 326 | if (cmdline) { |
| 327 | g_free(cmdline); |
| 328 | return; |
| 329 | } |
| 330 | |
| 331 | while (!done) { |
| 332 | if (!prompted) { |
| 333 | printf("%s", get_prompt()); |
| 334 | fflush(stdout); |
| 335 | qemu_set_fd_handler(STDIN_FILENO, prep_fetchline, NULL, &fetchable); |
| 336 | prompted = 1; |
| 337 | } |
| 338 | |
| 339 | main_loop_wait(false); |
| 340 | |
| 341 | if (!fetchable) { |
| 342 | continue; |
| 343 | } |
| 344 | |
| 345 | input = fetchline(); |
| 346 | if (input == NULL) { |
| 347 | break; |
| 348 | } |
Max Reitz | 4c7b7e9 | 2015-02-05 13:58:22 -0500 | [diff] [blame] | 349 | done = qemuio_command(qemuio_blk, input); |
Kevin Wolf | d1174f1 | 2013-06-05 14:19:37 +0200 | [diff] [blame] | 350 | g_free(input); |
| 351 | |
| 352 | prompted = 0; |
| 353 | fetchable = 0; |
| 354 | } |
| 355 | qemu_set_fd_handler(STDIN_FILENO, NULL, NULL, NULL); |
| 356 | } |
| 357 | |
| 358 | static void add_user_command(char *optarg) |
| 359 | { |
Markus Armbruster | 5839e53 | 2014-08-19 10:31:08 +0200 | [diff] [blame] | 360 | cmdline = g_renew(char *, cmdline, ++ncmdline); |
Kevin Wolf | d1174f1 | 2013-06-05 14:19:37 +0200 | [diff] [blame] | 361 | cmdline[ncmdline-1] = optarg; |
| 362 | } |
| 363 | |
Stefan Hajnoczi | 0cf17e1 | 2013-11-14 11:54:17 +0100 | [diff] [blame] | 364 | static void reenable_tty_echo(void) |
| 365 | { |
| 366 | qemu_set_tty_echo(STDIN_FILENO, true); |
| 367 | } |
| 368 | |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 369 | int main(int argc, char **argv) |
| 370 | { |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 371 | int readonly = 0; |
Kevin Wolf | be6273d | 2014-11-20 16:27:06 +0100 | [diff] [blame] | 372 | const char *sopt = "hVc:d:f:rsnmgkt:T:"; |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 373 | const struct option lopt[] = { |
| 374 | { "help", 0, NULL, 'h' }, |
| 375 | { "version", 0, NULL, 'V' }, |
| 376 | { "offset", 1, NULL, 'o' }, |
| 377 | { "cmd", 1, NULL, 'c' }, |
Kevin Wolf | be6273d | 2014-11-20 16:27:06 +0100 | [diff] [blame] | 378 | { "format", 1, NULL, 'f' }, |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 379 | { "read-only", 0, NULL, 'r' }, |
| 380 | { "snapshot", 0, NULL, 's' }, |
| 381 | { "nocache", 0, NULL, 'n' }, |
| 382 | { "misalign", 0, NULL, 'm' }, |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 383 | { "native-aio", 0, NULL, 'k' }, |
Paolo Bonzini | 9e8f183 | 2013-02-08 14:06:11 +0100 | [diff] [blame] | 384 | { "discard", 1, NULL, 'd' }, |
Kevin Wolf | 592fa07 | 2012-04-18 12:07:39 +0200 | [diff] [blame] | 385 | { "cache", 1, NULL, 't' }, |
Stefan Hajnoczi | d7bb72c | 2012-03-12 16:36:07 +0000 | [diff] [blame] | 386 | { "trace", 1, NULL, 'T' }, |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 387 | { NULL, 0, NULL, 0 } |
| 388 | }; |
| 389 | int c; |
| 390 | int opt_index = 0; |
Paolo Bonzini | 9e8f183 | 2013-02-08 14:06:11 +0100 | [diff] [blame] | 391 | int flags = BDRV_O_UNMAP; |
Chrysostomos Nanakos | 2f78e49 | 2014-09-18 14:30:49 +0300 | [diff] [blame] | 392 | Error *local_error = NULL; |
Max Reitz | 1b58b43 | 2015-02-05 13:58:20 -0500 | [diff] [blame] | 393 | QDict *opts = NULL; |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 394 | |
MORITA Kazutaka | 526eda1 | 2013-07-23 17:30:11 +0900 | [diff] [blame] | 395 | #ifdef CONFIG_POSIX |
| 396 | signal(SIGPIPE, SIG_IGN); |
| 397 | #endif |
| 398 | |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 399 | progname = basename(argv[0]); |
Fam Zheng | 10f5bff | 2014-02-10 14:48:51 +0800 | [diff] [blame] | 400 | qemu_init_exec_dir(argv[0]); |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 401 | |
Kevin Wolf | be6273d | 2014-11-20 16:27:06 +0100 | [diff] [blame] | 402 | bdrv_init(); |
| 403 | |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 404 | while ((c = getopt_long(argc, argv, sopt, lopt, &opt_index)) != -1) { |
| 405 | switch (c) { |
| 406 | case 's': |
| 407 | flags |= BDRV_O_SNAPSHOT; |
| 408 | break; |
| 409 | case 'n': |
| 410 | flags |= BDRV_O_NOCACHE | BDRV_O_CACHE_WB; |
| 411 | break; |
Paolo Bonzini | 9e8f183 | 2013-02-08 14:06:11 +0100 | [diff] [blame] | 412 | case 'd': |
| 413 | if (bdrv_parse_discard_flags(optarg, &flags) < 0) { |
| 414 | error_report("Invalid discard option: %s", optarg); |
| 415 | exit(1); |
| 416 | } |
| 417 | break; |
Kevin Wolf | be6273d | 2014-11-20 16:27:06 +0100 | [diff] [blame] | 418 | case 'f': |
Max Reitz | 1b58b43 | 2015-02-05 13:58:20 -0500 | [diff] [blame] | 419 | if (!opts) { |
| 420 | opts = qdict_new(); |
Kevin Wolf | be6273d | 2014-11-20 16:27:06 +0100 | [diff] [blame] | 421 | } |
Max Reitz | 1b58b43 | 2015-02-05 13:58:20 -0500 | [diff] [blame] | 422 | qdict_put(opts, "driver", qstring_from_str(optarg)); |
Kevin Wolf | be6273d | 2014-11-20 16:27:06 +0100 | [diff] [blame] | 423 | break; |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 424 | case 'c': |
| 425 | add_user_command(optarg); |
| 426 | break; |
| 427 | case 'r': |
| 428 | readonly = 1; |
| 429 | break; |
| 430 | case 'm': |
Stefan Weil | f988388 | 2014-03-05 22:23:00 +0100 | [diff] [blame] | 431 | qemuio_misalign = true; |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 432 | break; |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 433 | case 'k': |
| 434 | flags |= BDRV_O_NATIVE_AIO; |
| 435 | break; |
Kevin Wolf | 592fa07 | 2012-04-18 12:07:39 +0200 | [diff] [blame] | 436 | case 't': |
| 437 | if (bdrv_parse_cache_flags(optarg, &flags) < 0) { |
| 438 | error_report("Invalid cache option: %s", optarg); |
| 439 | exit(1); |
| 440 | } |
| 441 | break; |
Stefan Hajnoczi | d7bb72c | 2012-03-12 16:36:07 +0000 | [diff] [blame] | 442 | case 'T': |
LluĂs Vilanova | 5b80827 | 2014-05-27 15:02:14 +0200 | [diff] [blame] | 443 | if (!trace_init_backends(optarg, NULL)) { |
Stefan Hajnoczi | d7bb72c | 2012-03-12 16:36:07 +0000 | [diff] [blame] | 444 | exit(1); /* error message will have been printed */ |
| 445 | } |
| 446 | break; |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 447 | case 'V': |
Kevin Wolf | 02da386 | 2013-06-05 14:19:40 +0200 | [diff] [blame] | 448 | printf("%s version %s\n", progname, QEMU_VERSION); |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 449 | exit(0); |
| 450 | case 'h': |
| 451 | usage(progname); |
| 452 | exit(0); |
| 453 | default: |
| 454 | usage(progname); |
| 455 | exit(1); |
Naphtali Sprei | f5edb01 | 2010-01-17 16:48:13 +0200 | [diff] [blame] | 456 | } |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 457 | } |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 458 | |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 459 | if ((argc - optind) > 1) { |
| 460 | usage(progname); |
| 461 | exit(1); |
| 462 | } |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 463 | |
Chrysostomos Nanakos | 2f78e49 | 2014-09-18 14:30:49 +0300 | [diff] [blame] | 464 | if (qemu_init_main_loop(&local_error)) { |
Markus Armbruster | 565f65d | 2015-02-12 13:55:05 +0100 | [diff] [blame] | 465 | error_report_err(local_error); |
Chrysostomos Nanakos | 2f78e49 | 2014-09-18 14:30:49 +0300 | [diff] [blame] | 466 | exit(1); |
| 467 | } |
Zhi Yong Wu | a57d114 | 2012-02-19 22:24:59 +0800 | [diff] [blame] | 468 | |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 469 | /* initialize commands */ |
Kevin Wolf | c2cdf5c | 2013-06-05 14:19:36 +0200 | [diff] [blame] | 470 | qemuio_add_command(&quit_cmd); |
| 471 | qemuio_add_command(&open_cmd); |
| 472 | qemuio_add_command(&close_cmd); |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 473 | |
Stefan Hajnoczi | 0cf17e1 | 2013-11-14 11:54:17 +0100 | [diff] [blame] | 474 | if (isatty(STDIN_FILENO)) { |
| 475 | readline_state = readline_init(readline_printf_func, |
| 476 | readline_flush_func, |
| 477 | NULL, |
| 478 | readline_completion_func); |
| 479 | qemu_set_tty_echo(STDIN_FILENO, false); |
| 480 | atexit(reenable_tty_echo); |
| 481 | } |
| 482 | |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 483 | /* open the device */ |
| 484 | if (!readonly) { |
| 485 | flags |= BDRV_O_RDWR; |
| 486 | } |
| 487 | |
| 488 | if ((argc - optind) == 1) { |
Max Reitz | 10d9d75 | 2015-02-05 13:58:21 -0500 | [diff] [blame] | 489 | openfile(argv[optind], flags, opts); |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 490 | } |
| 491 | command_loop(); |
| 492 | |
| 493 | /* |
Stefan Hajnoczi | 922453b | 2011-11-30 12:23:43 +0000 | [diff] [blame] | 494 | * Make sure all outstanding requests complete before the program exits. |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 495 | */ |
Stefan Hajnoczi | 922453b | 2011-11-30 12:23:43 +0000 | [diff] [blame] | 496 | bdrv_drain_all(); |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 497 | |
Markus Armbruster | 26f54e9 | 2014-10-07 13:59:04 +0200 | [diff] [blame] | 498 | blk_unref(qemuio_blk); |
Stefan Hajnoczi | 0cf17e1 | 2013-11-14 11:54:17 +0100 | [diff] [blame] | 499 | g_free(readline_state); |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 500 | return 0; |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 501 | } |