slirp: Fix non blocking connect for w32
[qemu/ar7.git] / qemu-io.c
blob487125fb47a3c6a5d141e77a67b7938b30910172
1 /*
2 * Command line utility to exercise the QEMU I/O path.
4 * Copyright (C) 2009 Red Hat, Inc.
5 * Copyright (c) 2003-2005 Silicon Graphics, Inc.
7 * This work is licensed under the terms of the GNU GPL, version 2 or later.
8 * See the COPYING file in the top-level directory.
9 */
10 #include <sys/time.h>
11 #include <sys/types.h>
12 #include <stdarg.h>
13 #include <stdio.h>
14 #include <getopt.h>
15 #include <libgen.h>
17 #include "qemu-io.h"
18 #include "qemu/error-report.h"
19 #include "qemu/main-loop.h"
20 #include "qemu/option.h"
21 #include "qemu/config-file.h"
22 #include "qemu/readline.h"
23 #include "qapi/qmp/qstring.h"
24 #include "sysemu/block-backend.h"
25 #include "block/block_int.h"
26 #include "trace/control.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;
38 static ReadLineState *readline_state;
40 static int close_f(BlockBackend *blk, int argc, char **argv)
42 blk_unref(qemuio_blk);
43 qemuio_blk = NULL;
44 return 0;
47 static const cmdinfo_t close_cmd = {
48 .name = "close",
49 .altname = "c",
50 .cfunc = close_f,
51 .oneline = "close the current open file",
54 static int openfile(char *name, int flags, QDict *opts)
56 Error *local_err = NULL;
57 BlockDriverState *bs;
59 if (qemuio_blk) {
60 fprintf(stderr, "file open already, try 'help close'\n");
61 QDECREF(opts);
62 return 1;
65 qemuio_blk = blk_new_open("hda", name, NULL, opts, flags, &local_err);
66 if (!qemuio_blk) {
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);
71 return 1;
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;
82 if (bdrv_set_key(bs, password) < 0) {
83 error_report("invalid password");
84 goto error;
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 int c;
143 QemuOpts *qopts;
144 QDict *opts;
146 while ((c = getopt(argc, argv, "snrgo:")) != -1) {
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;
157 case 'o':
158 if (!qemu_opts_parse_noisily(&empty_opts, optarg, false)) {
159 printf("could not parse option list -- %s\n", optarg);
160 qemu_opts_reset(&empty_opts);
161 return 0;
163 break;
164 default:
165 qemu_opts_reset(&empty_opts);
166 return qemuio_command_usage(&open_cmd);
170 if (!readonly) {
171 flags |= BDRV_O_RDWR;
174 qopts = qemu_opts_find(&empty_opts, NULL);
175 opts = qopts ? qemu_opts_to_qdict(qopts, NULL) : NULL;
176 qemu_opts_reset(&empty_opts);
178 if (optind == argc - 1) {
179 return openfile(argv[optind], flags, opts);
180 } else if (optind == argc) {
181 return openfile(NULL, flags, opts);
182 } else {
183 QDECREF(opts);
184 return qemuio_command_usage(&open_cmd);
188 static int quit_f(BlockBackend *blk, int argc, char **argv)
190 return 1;
193 #if defined(CONFIG_FVD)
194 # include "qemu-io-sim.c"
195 #endif
197 static const cmdinfo_t quit_cmd = {
198 .name = "quit",
199 .altname = "q",
200 .cfunc = quit_f,
201 .argmin = -1,
202 .argmax = -1,
203 .flags = CMD_FLAG_GLOBAL,
204 .oneline = "exit the program",
207 static void usage(const char *name)
209 printf(
210 "Usage: %s [-h] [-V] [-rsnm] [-f FMT] [-c STRING] ... [file]\n"
211 "QEMU Disk exerciser\n"
212 "\n"
213 " -c, --cmd STRING execute command with its arguments\n"
214 " from the given string\n"
215 " -f, --format FMT specifies the block driver to use\n"
216 " -r, --read-only export read-only\n"
217 " -s, --snapshot use snapshot file\n"
218 " -n, --nocache disable host cache\n"
219 " -m, --misalign misalign allocations for O_DIRECT\n"
220 " -k, --native-aio use kernel AIO implementation (on Linux only)\n"
221 " -t, --cache=MODE use the given cache mode for the image\n"
222 " -T, --trace FILE enable trace events listed in the given file\n"
223 " -h, --help display this help and exit\n"
224 " -V, --version output version information and exit\n"
225 "\n"
226 "See '%s -c help' for information on available commands."
227 "\n",
228 name, name);
231 static char *get_prompt(void)
233 static char prompt[FILENAME_MAX + 2 /*"> "*/ + 1 /*"\0"*/ ];
235 if (!prompt[0]) {
236 snprintf(prompt, sizeof(prompt), "%s> ", progname);
239 return prompt;
242 static void GCC_FMT_ATTR(2, 3) readline_printf_func(void *opaque,
243 const char *fmt, ...)
245 va_list ap;
246 va_start(ap, fmt);
247 vprintf(fmt, ap);
248 va_end(ap);
251 static void readline_flush_func(void *opaque)
253 fflush(stdout);
256 static void readline_func(void *opaque, const char *str, void *readline_opaque)
258 char **line = readline_opaque;
259 *line = g_strdup(str);
262 static void completion_match(const char *cmd, void *opaque)
264 readline_add_completion(readline_state, cmd);
267 static void readline_completion_func(void *opaque, const char *str)
269 readline_set_completion_index(readline_state, strlen(str));
270 qemuio_complete_command(str, completion_match, NULL);
273 static char *fetchline_readline(void)
275 char *line = NULL;
277 readline_start(readline_state, get_prompt(), 0, readline_func, &line);
278 while (!line) {
279 int ch = getchar();
280 if (ch == EOF) {
281 break;
283 readline_handle_byte(readline_state, ch);
285 return line;
288 #define MAXREADLINESZ 1024
289 static char *fetchline_fgets(void)
291 char *p, *line = g_malloc(MAXREADLINESZ);
293 if (!fgets(line, MAXREADLINESZ, stdin)) {
294 g_free(line);
295 return NULL;
298 p = line + strlen(line);
299 if (p != line && p[-1] == '\n') {
300 p[-1] = '\0';
303 return line;
306 static char *fetchline(void)
308 if (readline_state) {
309 return fetchline_readline();
310 } else {
311 return fetchline_fgets();
315 static void prep_fetchline(void *opaque)
317 int *fetchable = opaque;
319 qemu_set_fd_handler(STDIN_FILENO, NULL, NULL, NULL);
320 *fetchable= 1;
323 static void command_loop(void)
325 int i, done = 0, fetchable = 0, prompted = 0;
326 char *input;
328 for (i = 0; !done && i < ncmdline; i++) {
329 done = qemuio_command(qemuio_blk, cmdline[i]);
331 if (cmdline) {
332 g_free(cmdline);
333 return;
336 while (!done) {
337 if (!prompted) {
338 printf("%s", get_prompt());
339 fflush(stdout);
340 qemu_set_fd_handler(STDIN_FILENO, prep_fetchline, NULL, &fetchable);
341 prompted = 1;
344 main_loop_wait(false);
346 if (!fetchable) {
347 continue;
350 input = fetchline();
351 if (input == NULL) {
352 break;
354 done = qemuio_command(qemuio_blk, input);
355 g_free(input);
357 prompted = 0;
358 fetchable = 0;
360 qemu_set_fd_handler(STDIN_FILENO, NULL, NULL, NULL);
363 static void add_user_command(char *optarg)
365 cmdline = g_renew(char *, cmdline, ++ncmdline);
366 cmdline[ncmdline-1] = optarg;
369 static void reenable_tty_echo(void)
371 qemu_set_tty_echo(STDIN_FILENO, true);
374 int main(int argc, char **argv)
376 int readonly = 0;
377 const char *sopt = "hVc:d:f:rsnmgkt:T:";
378 const struct option lopt[] = {
379 { "help", 0, NULL, 'h' },
380 { "version", 0, NULL, 'V' },
381 { "offset", 1, NULL, 'o' },
382 { "cmd", 1, NULL, 'c' },
383 { "format", 1, NULL, 'f' },
384 { "read-only", 0, NULL, 'r' },
385 { "snapshot", 0, NULL, 's' },
386 { "nocache", 0, NULL, 'n' },
387 { "misalign", 0, NULL, 'm' },
388 { "native-aio", 0, NULL, 'k' },
389 { "discard", 1, NULL, 'd' },
390 { "cache", 1, NULL, 't' },
391 { "trace", 1, NULL, 'T' },
392 { NULL, 0, NULL, 0 }
394 int c;
395 int opt_index = 0;
396 int flags = BDRV_O_UNMAP;
397 Error *local_error = NULL;
398 QDict *opts = NULL;
400 #ifdef CONFIG_POSIX
401 signal(SIGPIPE, SIG_IGN);
402 #endif
404 progname = basename(argv[0]);
405 qemu_init_exec_dir(argv[0]);
407 bdrv_init();
409 while ((c = getopt_long(argc, argv, sopt, lopt, &opt_index)) != -1) {
410 switch (c) {
411 case 's':
412 flags |= BDRV_O_SNAPSHOT;
413 break;
414 case 'n':
415 flags |= BDRV_O_NOCACHE | BDRV_O_CACHE_WB;
416 break;
417 case 'd':
418 if (bdrv_parse_discard_flags(optarg, &flags) < 0) {
419 error_report("Invalid discard option: %s", optarg);
420 exit(1);
422 break;
423 case 'f':
424 if (!opts) {
425 opts = qdict_new();
427 qdict_put(opts, "driver", qstring_from_str(optarg));
428 break;
429 case 'c':
430 add_user_command(optarg);
431 break;
432 case 'r':
433 readonly = 1;
434 break;
435 case 'm':
436 qemuio_misalign = true;
437 break;
438 case 'k':
439 flags |= BDRV_O_NATIVE_AIO;
440 break;
441 case 't':
442 if (bdrv_parse_cache_flags(optarg, &flags) < 0) {
443 error_report("Invalid cache option: %s", optarg);
444 exit(1);
446 break;
447 case 'T':
448 if (!trace_init_backends(optarg, NULL)) {
449 exit(1); /* error message will have been printed */
451 break;
452 case 'V':
453 printf("%s version %s\n", progname, QEMU_VERSION);
454 exit(0);
455 case 'h':
456 usage(progname);
457 exit(0);
458 default:
459 usage(progname);
460 exit(1);
464 if ((argc - optind) > 1) {
465 usage(progname);
466 exit(1);
469 if (qemu_init_main_loop(&local_error)) {
470 error_report_err(local_error);
471 exit(1);
474 /* initialize commands */
475 qemuio_add_command(&quit_cmd);
476 qemuio_add_command(&open_cmd);
477 qemuio_add_command(&close_cmd);
479 if (isatty(STDIN_FILENO)) {
480 readline_state = readline_init(readline_printf_func,
481 readline_flush_func,
482 NULL,
483 readline_completion_func);
484 qemu_set_tty_echo(STDIN_FILENO, false);
485 atexit(reenable_tty_echo);
488 /* open the device */
489 if (!readonly) {
490 flags |= BDRV_O_RDWR;
493 if ((argc - optind) == 1) {
494 openfile(argv[optind], flags, opts);
496 command_loop();
499 * Make sure all outstanding requests complete before the program exits.
501 bdrv_drain_all();
503 blk_unref(qemuio_blk);
504 g_free(readline_state);
505 return 0;