qemu: Adjust qemu wakeup
[qemu/ar7.git] / qemu-io.c
blobf4b8efccebd44b284185110097695008ddb83cc4
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/main-loop.h"
19 #include "block/block_int.h"
20 #include "trace/control.h"
22 #define CMD_NOFILE_OK 0x01
24 char *progname;
26 BlockDriverState *qemuio_bs;
27 extern int qemuio_misalign;
29 /* qemu-io commands passed using -c */
30 static int ncmdline;
31 static char **cmdline;
33 static int close_f(BlockDriverState *bs, int argc, char **argv)
35 bdrv_unref(bs);
36 qemuio_bs = NULL;
37 return 0;
40 static const cmdinfo_t close_cmd = {
41 .name = "close",
42 .altname = "c",
43 .cfunc = close_f,
44 .oneline = "close the current open file",
47 static int openfile(char *name, int flags, int growable)
49 Error *local_err = NULL;
51 if (qemuio_bs) {
52 fprintf(stderr, "file open already, try 'help close'\n");
53 return 1;
56 if (growable) {
57 if (bdrv_file_open(&qemuio_bs, name, NULL, flags, &local_err)) {
58 fprintf(stderr, "%s: can't open device %s: %s\n", progname, name,
59 error_get_pretty(local_err));
60 error_free(local_err);
61 return 1;
63 } else {
64 qemuio_bs = bdrv_new("hda");
66 if (bdrv_open(qemuio_bs, name, NULL, flags, NULL, &local_err) < 0) {
67 fprintf(stderr, "%s: can't open device %s: %s\n", progname, name,
68 error_get_pretty(local_err));
69 error_free(local_err);
70 bdrv_unref(qemuio_bs);
71 qemuio_bs = NULL;
72 return 1;
76 return 0;
79 static void open_help(void)
81 printf(
82 "\n"
83 " opens a new file in the requested mode\n"
84 "\n"
85 " Example:\n"
86 " 'open -Cn /tmp/data' - creates/opens data file read-write and uncached\n"
87 "\n"
88 " Opens a file for subsequent use by all of the other qemu-io commands.\n"
89 " -r, -- open file read-only\n"
90 " -s, -- use snapshot file\n"
91 " -n, -- disable host cache\n"
92 " -g, -- allow file to grow (only applies to protocols)"
93 "\n");
96 static int open_f(BlockDriverState *bs, int argc, char **argv);
98 static const cmdinfo_t open_cmd = {
99 .name = "open",
100 .altname = "o",
101 .cfunc = open_f,
102 .argmin = 1,
103 .argmax = -1,
104 .flags = CMD_NOFILE_OK,
105 .args = "[-Crsn] [path]",
106 .oneline = "open the file specified by path",
107 .help = open_help,
110 static int open_f(BlockDriverState *bs, int argc, char **argv)
112 int flags = 0;
113 int readonly = 0;
114 int growable = 0;
115 int c;
117 while ((c = getopt(argc, argv, "snrg")) != EOF) {
118 switch (c) {
119 case 's':
120 flags |= BDRV_O_SNAPSHOT;
121 break;
122 case 'n':
123 flags |= BDRV_O_NOCACHE | BDRV_O_CACHE_WB;
124 break;
125 case 'r':
126 readonly = 1;
127 break;
128 case 'g':
129 growable = 1;
130 break;
131 default:
132 return qemuio_command_usage(&open_cmd);
136 if (!readonly) {
137 flags |= BDRV_O_RDWR;
140 if (optind != argc - 1) {
141 return qemuio_command_usage(&open_cmd);
144 return openfile(argv[optind], flags, growable);
147 static int quit_f(BlockDriverState *bs, int argc, char **argv)
149 return 1;
152 static const cmdinfo_t quit_cmd = {
153 .name = "quit",
154 .altname = "q",
155 .cfunc = quit_f,
156 .argmin = -1,
157 .argmax = -1,
158 .flags = CMD_FLAG_GLOBAL,
159 .oneline = "exit the program",
162 static void usage(const char *name)
164 printf(
165 "Usage: %s [-h] [-V] [-rsnm] [-c cmd] ... [file]\n"
166 "QEMU Disk exerciser\n"
167 "\n"
168 " -c, --cmd command to execute\n"
169 " -r, --read-only export read-only\n"
170 " -s, --snapshot use snapshot file\n"
171 " -n, --nocache disable host cache\n"
172 " -g, --growable allow file to grow (only applies to protocols)\n"
173 " -m, --misalign misalign allocations for O_DIRECT\n"
174 " -k, --native-aio use kernel AIO implementation (on Linux only)\n"
175 " -t, --cache=MODE use the given cache mode for the image\n"
176 " -T, --trace FILE enable trace events listed in the given file\n"
177 " -h, --help display this help and exit\n"
178 " -V, --version output version information and exit\n"
179 "\n",
180 name);
184 #if defined(ENABLE_READLINE)
185 # include <readline/history.h>
186 # include <readline/readline.h>
187 #elif defined(ENABLE_EDITLINE)
188 # include <histedit.h>
189 #endif
191 static char *get_prompt(void)
193 static char prompt[FILENAME_MAX + 2 /*"> "*/ + 1 /*"\0"*/ ];
195 if (!prompt[0]) {
196 snprintf(prompt, sizeof(prompt), "%s> ", progname);
199 return prompt;
202 #if defined(ENABLE_READLINE)
203 static char *fetchline(void)
205 char *line = readline(get_prompt());
206 if (line && *line) {
207 add_history(line);
209 return line;
211 #elif defined(ENABLE_EDITLINE)
212 static char *el_get_prompt(EditLine *e)
214 return get_prompt();
217 static char *fetchline(void)
219 static EditLine *el;
220 static History *hist;
221 HistEvent hevent;
222 char *line;
223 int count;
225 if (!el) {
226 hist = history_init();
227 history(hist, &hevent, H_SETSIZE, 100);
228 el = el_init(progname, stdin, stdout, stderr);
229 el_source(el, NULL);
230 el_set(el, EL_SIGNAL, 1);
231 el_set(el, EL_PROMPT, el_get_prompt);
232 el_set(el, EL_HIST, history, (const char *)hist);
234 line = strdup(el_gets(el, &count));
235 if (line) {
236 if (count > 0) {
237 line[count-1] = '\0';
239 if (*line) {
240 history(hist, &hevent, H_ENTER, line);
243 return line;
245 #else
246 # define MAXREADLINESZ 1024
247 static char *fetchline(void)
249 char *p, *line = g_malloc(MAXREADLINESZ);
251 if (!fgets(line, MAXREADLINESZ, stdin)) {
252 g_free(line);
253 return NULL;
256 p = line + strlen(line);
257 if (p != line && p[-1] == '\n') {
258 p[-1] = '\0';
261 return line;
263 #endif
265 static void prep_fetchline(void *opaque)
267 int *fetchable = opaque;
269 qemu_set_fd_handler(STDIN_FILENO, NULL, NULL, NULL);
270 *fetchable= 1;
273 static void command_loop(void)
275 int i, done = 0, fetchable = 0, prompted = 0;
276 char *input;
278 for (i = 0; !done && i < ncmdline; i++) {
279 done = qemuio_command(qemuio_bs, cmdline[i]);
281 if (cmdline) {
282 g_free(cmdline);
283 return;
286 while (!done) {
287 if (!prompted) {
288 printf("%s", get_prompt());
289 fflush(stdout);
290 qemu_set_fd_handler(STDIN_FILENO, prep_fetchline, NULL, &fetchable);
291 prompted = 1;
294 main_loop_wait(false);
296 if (!fetchable) {
297 continue;
300 input = fetchline();
301 if (input == NULL) {
302 break;
304 done = qemuio_command(qemuio_bs, input);
305 g_free(input);
307 prompted = 0;
308 fetchable = 0;
310 qemu_set_fd_handler(STDIN_FILENO, NULL, NULL, NULL);
313 static void add_user_command(char *optarg)
315 cmdline = g_realloc(cmdline, ++ncmdline * sizeof(char *));
316 cmdline[ncmdline-1] = optarg;
319 int main(int argc, char **argv)
321 int readonly = 0;
322 int growable = 0;
323 const char *sopt = "hVc:d:rsnmgkt:T:";
324 const struct option lopt[] = {
325 { "help", 0, NULL, 'h' },
326 { "version", 0, NULL, 'V' },
327 { "offset", 1, NULL, 'o' },
328 { "cmd", 1, NULL, 'c' },
329 { "read-only", 0, NULL, 'r' },
330 { "snapshot", 0, NULL, 's' },
331 { "nocache", 0, NULL, 'n' },
332 { "misalign", 0, NULL, 'm' },
333 { "growable", 0, NULL, 'g' },
334 { "native-aio", 0, NULL, 'k' },
335 { "discard", 1, NULL, 'd' },
336 { "cache", 1, NULL, 't' },
337 { "trace", 1, NULL, 'T' },
338 { NULL, 0, NULL, 0 }
340 int c;
341 int opt_index = 0;
342 int flags = BDRV_O_UNMAP;
344 #ifdef CONFIG_POSIX
345 signal(SIGPIPE, SIG_IGN);
346 #endif
348 progname = basename(argv[0]);
350 while ((c = getopt_long(argc, argv, sopt, lopt, &opt_index)) != -1) {
351 switch (c) {
352 case 's':
353 flags |= BDRV_O_SNAPSHOT;
354 break;
355 case 'n':
356 flags |= BDRV_O_NOCACHE | BDRV_O_CACHE_WB;
357 break;
358 case 'd':
359 if (bdrv_parse_discard_flags(optarg, &flags) < 0) {
360 error_report("Invalid discard option: %s", optarg);
361 exit(1);
363 break;
364 case 'c':
365 add_user_command(optarg);
366 break;
367 case 'r':
368 readonly = 1;
369 break;
370 case 'm':
371 qemuio_misalign = 1;
372 break;
373 case 'g':
374 growable = 1;
375 break;
376 case 'k':
377 flags |= BDRV_O_NATIVE_AIO;
378 break;
379 case 't':
380 if (bdrv_parse_cache_flags(optarg, &flags) < 0) {
381 error_report("Invalid cache option: %s", optarg);
382 exit(1);
384 break;
385 case 'T':
386 if (!trace_backend_init(optarg, NULL)) {
387 exit(1); /* error message will have been printed */
389 break;
390 case 'V':
391 printf("%s version %s\n", progname, QEMU_VERSION);
392 exit(0);
393 case 'h':
394 usage(progname);
395 exit(0);
396 default:
397 usage(progname);
398 exit(1);
402 if ((argc - optind) > 1) {
403 usage(progname);
404 exit(1);
407 qemu_init_main_loop();
408 bdrv_init();
410 /* initialize commands */
411 qemuio_add_command(&quit_cmd);
412 qemuio_add_command(&open_cmd);
413 qemuio_add_command(&close_cmd);
415 /* open the device */
416 if (!readonly) {
417 flags |= BDRV_O_RDWR;
420 if ((argc - optind) == 1) {
421 openfile(argv[optind], flags, growable);
423 command_loop();
426 * Make sure all outstanding requests complete before the program exits.
428 bdrv_drain_all();
430 if (qemuio_bs) {
431 bdrv_unref(qemuio_bs);
433 return 0;