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