blob: bf902302e9a5be29a2616d9926e36dfc5e5e76e3 [file] [log] [blame]
aliguorie3aff4f2009-04-05 19:14:04 +00001/*
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 */
Markus Armbruster452fcdb2018-02-01 12:18:39 +010010
Peter Maydell80c71a22016-01-18 18:01:42 +000011#include "qemu/osdep.h"
aliguorie3aff4f2009-04-05 19:14:04 +000012#include <getopt.h>
Stefan Weilc32d7662009-08-31 22:16:16 +020013#include <libgen.h>
Daniel P. Berrange0e448a02018-02-12 18:48:49 +000014#ifndef _WIN32
15#include <termios.h>
16#endif
aliguorie3aff4f2009-04-05 19:14:04 +000017
Markus Armbrustera8d25322019-05-23 16:35:08 +020018#include "qemu-common.h"
Markus Armbrusterda34e652016-03-14 09:01:28 +010019#include "qapi/error.h"
Kevin Wolf3d219942013-06-05 14:19:39 +020020#include "qemu-io.h"
Markus Armbrusterd49b6832015-03-17 18:29:20 +010021#include "qemu/error-report.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010022#include "qemu/main-loop.h"
Markus Armbruster0b8fa322019-05-23 16:35:07 +020023#include "qemu/module.h"
Max Reitzb543c5c2013-10-11 14:02:10 +020024#include "qemu/option.h"
25#include "qemu/config-file.h"
Stefan Hajnoczi0cf17e12013-11-14 11:54:17 +010026#include "qemu/readline.h"
Denis V. Luneve9a80852016-06-17 17:44:11 +030027#include "qemu/log.h"
Daniel P. Berrangé98c5d2e2020-08-25 11:38:48 +010028#include "qemu/sockets.h"
Markus Armbrusterd49b6832015-03-17 18:29:20 +010029#include "qapi/qmp/qstring.h"
Markus Armbruster452fcdb2018-02-01 12:18:39 +010030#include "qapi/qmp/qdict.h"
Daniel P. Berrange9ba371b2016-02-17 10:10:16 +000031#include "qom/object_interfaces.h"
Markus Armbruster26f54e92014-10-07 13:59:04 +020032#include "sysemu/block-backend.h"
Paolo Bonzini737e1502012-12-17 18:19:44 +010033#include "block/block_int.h"
Stefan Hajnoczid7bb72c2012-03-12 16:36:07 +000034#include "trace/control.h"
Daniel P. Berrangec2297082016-04-06 12:12:06 +010035#include "crypto/init.h"
Eric Blake4face322017-08-03 11:33:51 -050036#include "qemu-version.h"
aliguorie3aff4f2009-04-05 19:14:04 +000037
Devin Nakamura43642b32011-07-11 11:22:16 -040038#define CMD_NOFILE_OK 0x01
aliguorie3aff4f2009-04-05 19:14:04 +000039
Markus Armbruster26f54e92014-10-07 13:59:04 +020040static BlockBackend *qemuio_blk;
Max Reitzb444d0e2018-05-09 21:42:58 +020041static bool quit_qemu_io;
Kevin Wolf191c2892010-09-16 13:18:08 +020042
Kevin Wolfd1174f12013-06-05 14:19:37 +020043/* qemu-io commands passed using -c */
44static int ncmdline;
45static char **cmdline;
Daniel P. Berrange499afa22016-02-17 10:10:18 +000046static bool imageOpts;
Kevin Wolfd1174f12013-06-05 14:19:37 +020047
Stefan Hajnoczi0cf17e12013-11-14 11:54:17 +010048static ReadLineState *readline_state;
49
Daniel P. Berrange0e448a02018-02-12 18:48:49 +000050static int ttyEOF;
51
52static int get_eof_char(void)
53{
54#ifdef _WIN32
55 return 0x4; /* Ctrl-D */
56#else
57 struct termios tty;
58 if (tcgetattr(STDIN_FILENO, &tty) != 0) {
59 if (errno == ENOTTY) {
60 return 0x0; /* just expect read() == 0 */
61 } else {
62 return 0x4; /* Ctrl-D */
63 }
64 }
65
66 return tty.c_cc[VEOF];
67#endif
68}
69
Max Reitzb32d7a32018-05-09 21:42:59 +020070static int close_f(BlockBackend *blk, int argc, char **argv)
aliguorie3aff4f2009-04-05 19:14:04 +000071{
Markus Armbruster26f54e92014-10-07 13:59:04 +020072 blk_unref(qemuio_blk);
Markus Armbruster26f54e92014-10-07 13:59:04 +020073 qemuio_blk = NULL;
Max Reitzb32d7a32018-05-09 21:42:59 +020074 return 0;
aliguorie3aff4f2009-04-05 19:14:04 +000075}
76
77static const cmdinfo_t close_cmd = {
Devin Nakamura43642b32011-07-11 11:22:16 -040078 .name = "close",
79 .altname = "c",
80 .cfunc = close_f,
81 .oneline = "close the current open file",
aliguorie3aff4f2009-04-05 19:14:04 +000082};
83
Fam Zheng459571f2017-05-03 00:35:41 +080084static int openfile(char *name, int flags, bool writethrough, bool force_share,
85 QDict *opts)
aliguorie3aff4f2009-04-05 19:14:04 +000086{
Max Reitz34b5d2c2013-09-05 14:45:29 +020087 Error *local_err = NULL;
88
Max Reitz1b58b432015-02-05 13:58:20 -050089 if (qemuio_blk) {
Markus Armbrusterb9884682015-12-18 16:35:18 +010090 error_report("file open already, try 'help close'");
Marc-André Lureaucb3e7f02018-04-19 17:01:43 +020091 qobject_unref(opts);
Devin Nakamura43642b32011-07-11 11:22:16 -040092 return 1;
93 }
aliguorie3aff4f2009-04-05 19:14:04 +000094
Fam Zheng459571f2017-05-03 00:35:41 +080095 if (force_share) {
96 if (!opts) {
97 opts = qdict_new();
98 }
99 if (qdict_haskey(opts, BDRV_OPT_FORCE_SHARE)
Max Reitz2a01c012018-05-02 22:20:49 +0200100 && strcmp(qdict_get_str(opts, BDRV_OPT_FORCE_SHARE), "on")) {
Fam Zheng459571f2017-05-03 00:35:41 +0800101 error_report("-U conflicts with image options");
Marc-André Lureaucb3e7f02018-04-19 17:01:43 +0200102 qobject_unref(opts);
Fam Zheng459571f2017-05-03 00:35:41 +0800103 return 1;
104 }
Max Reitz2a01c012018-05-02 22:20:49 +0200105 qdict_put_str(opts, BDRV_OPT_FORCE_SHARE, "on");
Fam Zheng459571f2017-05-03 00:35:41 +0800106 }
Max Reitzefaa7c42016-03-16 19:54:38 +0100107 qemuio_blk = blk_new_open(name, NULL, opts, flags, &local_err);
Max Reitz1b58b432015-02-05 13:58:20 -0500108 if (!qemuio_blk) {
Markus Armbrusterb9884682015-12-18 16:35:18 +0100109 error_reportf_err(local_err, "can't open%s%s: ",
110 name ? " device " : "", name ?: "");
Markus Armbrusterdbb651c2014-09-08 18:50:58 +0200111 return 1;
Devin Nakamura43642b32011-07-11 11:22:16 -0400112 }
Christoph Hellwig1db69472009-07-15 23:11:21 +0200113
Kevin Wolfe151fc12016-03-15 12:52:30 +0100114 blk_set_enable_write_cache(qemuio_blk, !writethrough);
Daniel P. Berrange8caf0212015-05-12 17:09:21 +0100115
Devin Nakamura43642b32011-07-11 11:22:16 -0400116 return 0;
aliguorie3aff4f2009-04-05 19:14:04 +0000117}
118
Devin Nakamura43642b32011-07-11 11:22:16 -0400119static void open_help(void)
aliguorie3aff4f2009-04-05 19:14:04 +0000120{
Devin Nakamura43642b32011-07-11 11:22:16 -0400121 printf(
aliguorie3aff4f2009-04-05 19:14:04 +0000122"\n"
123" opens a new file in the requested mode\n"
124"\n"
125" Example:\n"
Eric Blakee4e12bb2016-05-07 21:16:40 -0600126" 'open -n -o driver=raw /tmp/data' - opens raw data file read-write, uncached\n"
aliguorie3aff4f2009-04-05 19:14:04 +0000127"\n"
128" Opens a file for subsequent use by all of the other qemu-io commands.\n"
aliguorie3aff4f2009-04-05 19:14:04 +0000129" -r, -- open file read-only\n"
130" -s, -- use snapshot file\n"
Eric Blake0f404442017-10-05 14:02:43 -0500131" -C, -- use copy-on-read\n"
Eric Blakeb8d970f2016-05-07 21:16:41 -0600132" -n, -- disable host cache, short for -t none\n"
Fam Zheng459571f2017-05-03 00:35:41 +0800133" -U, -- force shared permissions\n"
Aarushi Mehta1c5a2ae2020-01-20 14:18:54 +0000134" -k, -- use kernel AIO implementation (Linux only, prefer use of -i)\n"
135" -i, -- use AIO mode (threads, native or io_uring)\n"
Eric Blakeb8d970f2016-05-07 21:16:41 -0600136" -t, -- use the given cache mode for the image\n"
137" -d, -- use the given discard mode for the image\n"
Max Reitzb543c5c2013-10-11 14:02:10 +0200138" -o, -- options to be given to the block driver"
aliguorie3aff4f2009-04-05 19:14:04 +0000139"\n");
140}
141
Max Reitzb32d7a32018-05-09 21:42:59 +0200142static int open_f(BlockBackend *blk, int argc, char **argv);
Blue Swirl22a2bdc2009-11-21 09:06:46 +0000143
144static const cmdinfo_t open_cmd = {
Devin Nakamura43642b32011-07-11 11:22:16 -0400145 .name = "open",
146 .altname = "o",
147 .cfunc = open_f,
148 .argmin = 1,
149 .argmax = -1,
150 .flags = CMD_NOFILE_OK,
Eric Blake0f404442017-10-05 14:02:43 -0500151 .args = "[-rsCnkU] [-t cache] [-d discard] [-o options] [path]",
Devin Nakamura43642b32011-07-11 11:22:16 -0400152 .oneline = "open the file specified by path",
153 .help = open_help,
Blue Swirl22a2bdc2009-11-21 09:06:46 +0000154};
aliguorie3aff4f2009-04-05 19:14:04 +0000155
Max Reitzb543c5c2013-10-11 14:02:10 +0200156static QemuOptsList empty_opts = {
157 .name = "drive",
Markus Armbruster443422f2014-05-28 11:16:58 +0200158 .merge_lists = true,
Max Reitzb543c5c2013-10-11 14:02:10 +0200159 .head = QTAILQ_HEAD_INITIALIZER(empty_opts.head),
160 .desc = {
161 /* no elements => accept any params */
162 { /* end of list */ }
163 },
164};
165
Max Reitzb32d7a32018-05-09 21:42:59 +0200166static int open_f(BlockBackend *blk, int argc, char **argv)
aliguorie3aff4f2009-04-05 19:14:04 +0000167{
Eric Blakeb8d970f2016-05-07 21:16:41 -0600168 int flags = BDRV_O_UNMAP;
Devin Nakamura43642b32011-07-11 11:22:16 -0400169 int readonly = 0;
Kevin Wolfe151fc12016-03-15 12:52:30 +0100170 bool writethrough = true;
Devin Nakamura43642b32011-07-11 11:22:16 -0400171 int c;
Max Reitzb32d7a32018-05-09 21:42:59 +0200172 int ret;
Max Reitzb543c5c2013-10-11 14:02:10 +0200173 QemuOpts *qopts;
Markus Armbruster443422f2014-05-28 11:16:58 +0200174 QDict *opts;
Fam Zheng459571f2017-05-03 00:35:41 +0800175 bool force_share = false;
aliguorie3aff4f2009-04-05 19:14:04 +0000176
Aarushi Mehta1c5a2ae2020-01-20 14:18:54 +0000177 while ((c = getopt(argc, argv, "snCro:ki:t:d:U")) != -1) {
Devin Nakamura43642b32011-07-11 11:22:16 -0400178 switch (c) {
179 case 's':
180 flags |= BDRV_O_SNAPSHOT;
181 break;
182 case 'n':
Kevin Wolfe151fc12016-03-15 12:52:30 +0100183 flags |= BDRV_O_NOCACHE;
184 writethrough = false;
Devin Nakamura43642b32011-07-11 11:22:16 -0400185 break;
Eric Blake0f404442017-10-05 14:02:43 -0500186 case 'C':
187 flags |= BDRV_O_COPY_ON_READ;
188 break;
Devin Nakamura43642b32011-07-11 11:22:16 -0400189 case 'r':
190 readonly = 1;
191 break;
Eric Blakeb8d970f2016-05-07 21:16:41 -0600192 case 'k':
193 flags |= BDRV_O_NATIVE_AIO;
194 break;
195 case 't':
196 if (bdrv_parse_cache_mode(optarg, &flags, &writethrough) < 0) {
197 error_report("Invalid cache option: %s", optarg);
198 qemu_opts_reset(&empty_opts);
Max Reitzb32d7a32018-05-09 21:42:59 +0200199 return -EINVAL;
Eric Blakeb8d970f2016-05-07 21:16:41 -0600200 }
201 break;
202 case 'd':
203 if (bdrv_parse_discard_flags(optarg, &flags) < 0) {
204 error_report("Invalid discard option: %s", optarg);
205 qemu_opts_reset(&empty_opts);
Max Reitzb32d7a32018-05-09 21:42:59 +0200206 return -EINVAL;
Eric Blakeb8d970f2016-05-07 21:16:41 -0600207 }
208 break;
Aarushi Mehta1c5a2ae2020-01-20 14:18:54 +0000209 case 'i':
210 if (bdrv_parse_aio(optarg, &flags) < 0) {
211 error_report("Invalid aio option: %s", optarg);
212 qemu_opts_reset(&empty_opts);
213 return -EINVAL;
214 }
215 break;
Max Reitzb543c5c2013-10-11 14:02:10 +0200216 case 'o':
Daniel P. Berrange499afa22016-02-17 10:10:18 +0000217 if (imageOpts) {
218 printf("--image-opts and 'open -o' are mutually exclusive\n");
Eric Blakeb8d970f2016-05-07 21:16:41 -0600219 qemu_opts_reset(&empty_opts);
Max Reitzb32d7a32018-05-09 21:42:59 +0200220 return -EINVAL;
Daniel P. Berrange499afa22016-02-17 10:10:18 +0000221 }
Markus Armbruster70b94332015-02-13 12:50:26 +0100222 if (!qemu_opts_parse_noisily(&empty_opts, optarg, false)) {
Markus Armbruster443422f2014-05-28 11:16:58 +0200223 qemu_opts_reset(&empty_opts);
Max Reitzb32d7a32018-05-09 21:42:59 +0200224 return -EINVAL;
Max Reitzb543c5c2013-10-11 14:02:10 +0200225 }
Max Reitzb543c5c2013-10-11 14:02:10 +0200226 break;
Fam Zheng459571f2017-05-03 00:35:41 +0800227 case 'U':
228 force_share = true;
229 break;
Devin Nakamura43642b32011-07-11 11:22:16 -0400230 default:
Markus Armbruster443422f2014-05-28 11:16:58 +0200231 qemu_opts_reset(&empty_opts);
Max Reitzb444d0e2018-05-09 21:42:58 +0200232 qemuio_command_usage(&open_cmd);
Max Reitzb32d7a32018-05-09 21:42:59 +0200233 return -EINVAL;
Naphtali Spreif5edb012010-01-17 16:48:13 +0200234 }
Devin Nakamura43642b32011-07-11 11:22:16 -0400235 }
aliguorie3aff4f2009-04-05 19:14:04 +0000236
Devin Nakamura43642b32011-07-11 11:22:16 -0400237 if (!readonly) {
238 flags |= BDRV_O_RDWR;
239 }
aliguorie3aff4f2009-04-05 19:14:04 +0000240
Daniel P. Berrange499afa22016-02-17 10:10:18 +0000241 if (imageOpts && (optind == argc - 1)) {
242 if (!qemu_opts_parse_noisily(&empty_opts, argv[optind], false)) {
243 qemu_opts_reset(&empty_opts);
Max Reitzb32d7a32018-05-09 21:42:59 +0200244 return -EINVAL;
Daniel P. Berrange499afa22016-02-17 10:10:18 +0000245 }
246 optind++;
247 }
248
Markus Armbruster443422f2014-05-28 11:16:58 +0200249 qopts = qemu_opts_find(&empty_opts, NULL);
250 opts = qopts ? qemu_opts_to_qdict(qopts, NULL) : NULL;
251 qemu_opts_reset(&empty_opts);
252
Max Reitzfd0fee32013-12-20 19:28:20 +0100253 if (optind == argc - 1) {
Max Reitzb32d7a32018-05-09 21:42:59 +0200254 ret = openfile(argv[optind], flags, writethrough, force_share, opts);
Max Reitzfd0fee32013-12-20 19:28:20 +0100255 } else if (optind == argc) {
Max Reitzb32d7a32018-05-09 21:42:59 +0200256 ret = openfile(NULL, flags, writethrough, force_share, opts);
Max Reitzfd0fee32013-12-20 19:28:20 +0100257 } else {
Marc-André Lureaucb3e7f02018-04-19 17:01:43 +0200258 qobject_unref(opts);
Eric Blake64ebf552017-06-05 15:38:42 -0500259 qemuio_command_usage(&open_cmd);
Max Reitzb32d7a32018-05-09 21:42:59 +0200260 return -EINVAL;
Devin Nakamura43642b32011-07-11 11:22:16 -0400261 }
Max Reitzb32d7a32018-05-09 21:42:59 +0200262
263 if (ret) {
264 return -EINVAL;
265 }
266
267 return 0;
aliguorie3aff4f2009-04-05 19:14:04 +0000268}
269
Max Reitzb32d7a32018-05-09 21:42:59 +0200270static int quit_f(BlockBackend *blk, int argc, char **argv)
Kevin Wolfe681be72013-06-05 14:19:34 +0200271{
Max Reitzb444d0e2018-05-09 21:42:58 +0200272 quit_qemu_io = true;
Max Reitzb32d7a32018-05-09 21:42:59 +0200273 return 0;
Kevin Wolfe681be72013-06-05 14:19:34 +0200274}
275
276static const cmdinfo_t quit_cmd = {
277 .name = "quit",
278 .altname = "q",
279 .cfunc = quit_f,
280 .argmin = -1,
281 .argmax = -1,
282 .flags = CMD_FLAG_GLOBAL,
283 .oneline = "exit the program",
284};
285
aliguorie3aff4f2009-04-05 19:14:04 +0000286static void usage(const char *name)
287{
Devin Nakamura43642b32011-07-11 11:22:16 -0400288 printf(
Eric Blakee4e12bb2016-05-07 21:16:40 -0600289"Usage: %s [OPTIONS]... [-c STRING]... [file]\n"
Stefan Weil84844a22009-06-22 15:08:47 +0200290"QEMU Disk exerciser\n"
aliguorie3aff4f2009-04-05 19:14:04 +0000291"\n"
Daniel P. Berrange9ba371b2016-02-17 10:10:16 +0000292" --object OBJECTDEF define an object such as 'secret' for\n"
293" passwords and/or encryption keys\n"
Eric Blakee4e12bb2016-05-07 21:16:40 -0600294" --image-opts treat file as option string\n"
Maria Kustovad208cc32014-03-18 09:59:19 +0400295" -c, --cmd STRING execute command with its arguments\n"
296" from the given string\n"
Kevin Wolfbe6273d2014-11-20 16:27:06 +0100297" -f, --format FMT specifies the block driver to use\n"
aliguorie3aff4f2009-04-05 19:14:04 +0000298" -r, --read-only export read-only\n"
299" -s, --snapshot use snapshot file\n"
Eric Blakee4e12bb2016-05-07 21:16:40 -0600300" -n, --nocache disable host cache, short for -t none\n"
Eric Blake0f404442017-10-05 14:02:43 -0500301" -C, --copy-on-read enable copy-on-read\n"
aliguorie3aff4f2009-04-05 19:14:04 +0000302" -m, --misalign misalign allocations for O_DIRECT\n"
Aarushi Mehta1c5a2ae2020-01-20 14:18:54 +0000303" -k, --native-aio use kernel AIO implementation\n"
304" (Linux only, prefer use of -i)\n"
305" -i, --aio=MODE use AIO mode (threads, native or io_uring)\n"
Kevin Wolf592fa072012-04-18 12:07:39 +0200306" -t, --cache=MODE use the given cache mode for the image\n"
Eric Blakee4e12bb2016-05-07 21:16:40 -0600307" -d, --discard=MODE use the given discard mode for the image\n"
Denis V. Luneve9a80852016-06-17 17:44:11 +0300308" -T, --trace [[enable=]<pattern>][,events=<file>][,file=<file>]\n"
309" specify tracing options\n"
310" see qemu-img(1) man page for full description\n"
Fam Zheng459571f2017-05-03 00:35:41 +0800311" -U, --force-share force shared permissions\n"
aliguorie3aff4f2009-04-05 19:14:04 +0000312" -h, --help display this help and exit\n"
313" -V, --version output version information and exit\n"
Maria Kustovad208cc32014-03-18 09:59:19 +0400314"\n"
Eric Blakef5048cb2017-08-03 11:33:53 -0500315"See '%s -c help' for information on available commands.\n"
316"\n"
317QEMU_HELP_BOTTOM "\n",
Maria Kustovad208cc32014-03-18 09:59:19 +0400318 name, name);
aliguorie3aff4f2009-04-05 19:14:04 +0000319}
320
Kevin Wolfd1174f12013-06-05 14:19:37 +0200321static char *get_prompt(void)
322{
323 static char prompt[FILENAME_MAX + 2 /*"> "*/ + 1 /*"\0"*/ ];
324
325 if (!prompt[0]) {
Christophe Fergeau99e98d72019-01-31 17:46:13 +0100326 snprintf(prompt, sizeof(prompt), "%s> ", error_get_progname());
Kevin Wolfd1174f12013-06-05 14:19:37 +0200327 }
328
329 return prompt;
330}
331
Stefan Weild5d15072014-01-25 18:18:23 +0100332static void GCC_FMT_ATTR(2, 3) readline_printf_func(void *opaque,
333 const char *fmt, ...)
Kevin Wolfd1174f12013-06-05 14:19:37 +0200334{
Stefan Hajnoczi0cf17e12013-11-14 11:54:17 +0100335 va_list ap;
336 va_start(ap, fmt);
337 vprintf(fmt, ap);
338 va_end(ap);
339}
340
341static void readline_flush_func(void *opaque)
342{
343 fflush(stdout);
344}
345
346static void readline_func(void *opaque, const char *str, void *readline_opaque)
347{
348 char **line = readline_opaque;
349 *line = g_strdup(str);
350}
351
Stefan Hajnoczi46940202013-11-14 11:54:18 +0100352static void completion_match(const char *cmd, void *opaque)
353{
354 readline_add_completion(readline_state, cmd);
355}
356
Stefan Hajnoczi0cf17e12013-11-14 11:54:17 +0100357static void readline_completion_func(void *opaque, const char *str)
358{
Stefan Hajnoczi46940202013-11-14 11:54:18 +0100359 readline_set_completion_index(readline_state, strlen(str));
360 qemuio_complete_command(str, completion_match, NULL);
Stefan Hajnoczi0cf17e12013-11-14 11:54:17 +0100361}
362
363static char *fetchline_readline(void)
364{
365 char *line = NULL;
366
367 readline_start(readline_state, get_prompt(), 0, readline_func, &line);
368 while (!line) {
369 int ch = getchar();
Daniel P. Berrange0e448a02018-02-12 18:48:49 +0000370 if (ttyEOF != 0x0 && ch == ttyEOF) {
371 printf("\n");
Stefan Hajnoczi0cf17e12013-11-14 11:54:17 +0100372 break;
373 }
374 readline_handle_byte(readline_state, ch);
Kevin Wolfd1174f12013-06-05 14:19:37 +0200375 }
376 return line;
377}
Kevin Wolfd1174f12013-06-05 14:19:37 +0200378
Stefan Hajnoczi0cf17e12013-11-14 11:54:17 +0100379#define MAXREADLINESZ 1024
380static char *fetchline_fgets(void)
Kevin Wolfd1174f12013-06-05 14:19:37 +0200381{
382 char *p, *line = g_malloc(MAXREADLINESZ);
383
384 if (!fgets(line, MAXREADLINESZ, stdin)) {
385 g_free(line);
386 return NULL;
387 }
388
389 p = line + strlen(line);
390 if (p != line && p[-1] == '\n') {
391 p[-1] = '\0';
392 }
393
394 return line;
395}
Stefan Hajnoczi0cf17e12013-11-14 11:54:17 +0100396
397static char *fetchline(void)
398{
399 if (readline_state) {
400 return fetchline_readline();
401 } else {
402 return fetchline_fgets();
403 }
404}
Kevin Wolfd1174f12013-06-05 14:19:37 +0200405
406static void prep_fetchline(void *opaque)
407{
408 int *fetchable = opaque;
409
410 qemu_set_fd_handler(STDIN_FILENO, NULL, NULL, NULL);
411 *fetchable= 1;
412}
413
Max Reitz6b3aa842018-05-09 21:43:00 +0200414static int command_loop(void)
Kevin Wolfd1174f12013-06-05 14:19:37 +0200415{
Max Reitzb444d0e2018-05-09 21:42:58 +0200416 int i, fetchable = 0, prompted = 0;
Max Reitz6b3aa842018-05-09 21:43:00 +0200417 int ret, last_error = 0;
Kevin Wolfd1174f12013-06-05 14:19:37 +0200418 char *input;
419
Max Reitzb444d0e2018-05-09 21:42:58 +0200420 for (i = 0; !quit_qemu_io && i < ncmdline; i++) {
Max Reitz6b3aa842018-05-09 21:43:00 +0200421 ret = qemuio_command(qemuio_blk, cmdline[i]);
422 if (ret < 0) {
423 last_error = ret;
424 }
Kevin Wolfd1174f12013-06-05 14:19:37 +0200425 }
426 if (cmdline) {
427 g_free(cmdline);
Max Reitz6b3aa842018-05-09 21:43:00 +0200428 return last_error;
Kevin Wolfd1174f12013-06-05 14:19:37 +0200429 }
430
Max Reitzb444d0e2018-05-09 21:42:58 +0200431 while (!quit_qemu_io) {
Kevin Wolfd1174f12013-06-05 14:19:37 +0200432 if (!prompted) {
433 printf("%s", get_prompt());
434 fflush(stdout);
435 qemu_set_fd_handler(STDIN_FILENO, prep_fetchline, NULL, &fetchable);
436 prompted = 1;
437 }
438
439 main_loop_wait(false);
440
441 if (!fetchable) {
442 continue;
443 }
444
445 input = fetchline();
446 if (input == NULL) {
447 break;
448 }
Max Reitz6b3aa842018-05-09 21:43:00 +0200449 ret = qemuio_command(qemuio_blk, input);
Kevin Wolfd1174f12013-06-05 14:19:37 +0200450 g_free(input);
451
Max Reitz6b3aa842018-05-09 21:43:00 +0200452 if (ret < 0) {
453 last_error = ret;
454 }
455
Kevin Wolfd1174f12013-06-05 14:19:37 +0200456 prompted = 0;
457 fetchable = 0;
458 }
459 qemu_set_fd_handler(STDIN_FILENO, NULL, NULL, NULL);
Max Reitz6b3aa842018-05-09 21:43:00 +0200460
461 return last_error;
Kevin Wolfd1174f12013-06-05 14:19:37 +0200462}
463
464static void add_user_command(char *optarg)
465{
Markus Armbruster5839e532014-08-19 10:31:08 +0200466 cmdline = g_renew(char *, cmdline, ++ncmdline);
Kevin Wolfd1174f12013-06-05 14:19:37 +0200467 cmdline[ncmdline-1] = optarg;
468}
469
Stefan Hajnoczi0cf17e12013-11-14 11:54:17 +0100470static void reenable_tty_echo(void)
471{
472 qemu_set_tty_echo(STDIN_FILENO, true);
473}
474
Daniel P. Berrange9ba371b2016-02-17 10:10:16 +0000475enum {
476 OPTION_OBJECT = 256,
Daniel P. Berrange499afa22016-02-17 10:10:18 +0000477 OPTION_IMAGE_OPTS = 257,
Daniel P. Berrange9ba371b2016-02-17 10:10:16 +0000478};
479
Daniel P. Berrange499afa22016-02-17 10:10:18 +0000480static QemuOptsList file_opts = {
481 .name = "file",
482 .implied_opt_name = "file",
483 .head = QTAILQ_HEAD_INITIALIZER(file_opts.head),
484 .desc = {
485 /* no elements => accept any params */
486 { /* end of list */ }
487 },
488};
489
aliguorie3aff4f2009-04-05 19:14:04 +0000490int main(int argc, char **argv)
491{
Devin Nakamura43642b32011-07-11 11:22:16 -0400492 int readonly = 0;
Aarushi Mehta1c5a2ae2020-01-20 14:18:54 +0000493 const char *sopt = "hVc:d:f:rsnCmki:t:T:U";
Devin Nakamura43642b32011-07-11 11:22:16 -0400494 const struct option lopt[] = {
Daniel P. Berrangea5134162016-02-17 10:10:23 +0000495 { "help", no_argument, NULL, 'h' },
496 { "version", no_argument, NULL, 'V' },
Daniel P. Berrangea5134162016-02-17 10:10:23 +0000497 { "cmd", required_argument, NULL, 'c' },
498 { "format", required_argument, NULL, 'f' },
499 { "read-only", no_argument, NULL, 'r' },
500 { "snapshot", no_argument, NULL, 's' },
501 { "nocache", no_argument, NULL, 'n' },
Eric Blake0f404442017-10-05 14:02:43 -0500502 { "copy-on-read", no_argument, NULL, 'C' },
Daniel P. Berrangea5134162016-02-17 10:10:23 +0000503 { "misalign", no_argument, NULL, 'm' },
504 { "native-aio", no_argument, NULL, 'k' },
Aarushi Mehta1c5a2ae2020-01-20 14:18:54 +0000505 { "aio", required_argument, NULL, 'i' },
Daniel P. Berrangea5134162016-02-17 10:10:23 +0000506 { "discard", required_argument, NULL, 'd' },
507 { "cache", required_argument, NULL, 't' },
508 { "trace", required_argument, NULL, 'T' },
509 { "object", required_argument, NULL, OPTION_OBJECT },
510 { "image-opts", no_argument, NULL, OPTION_IMAGE_OPTS },
Fam Zheng459571f2017-05-03 00:35:41 +0800511 { "force-share", no_argument, 0, 'U'},
Devin Nakamura43642b32011-07-11 11:22:16 -0400512 { NULL, 0, NULL, 0 }
513 };
514 int c;
515 int opt_index = 0;
Paolo Bonzini9e8f1832013-02-08 14:06:11 +0100516 int flags = BDRV_O_UNMAP;
Max Reitz6b3aa842018-05-09 21:43:00 +0200517 int ret;
Kevin Wolfe151fc12016-03-15 12:52:30 +0100518 bool writethrough = true;
Chrysostomos Nanakos2f78e492014-09-18 14:30:49 +0300519 Error *local_error = NULL;
Max Reitz1b58b432015-02-05 13:58:20 -0500520 QDict *opts = NULL;
Daniel P. Berrange499afa22016-02-17 10:10:18 +0000521 const char *format = NULL;
Fam Zheng459571f2017-05-03 00:35:41 +0800522 bool force_share = false;
aliguorie3aff4f2009-04-05 19:14:04 +0000523
MORITA Kazutaka526eda12013-07-23 17:30:11 +0900524#ifdef CONFIG_POSIX
525 signal(SIGPIPE, SIG_IGN);
526#endif
527
Daniel P. Berrangé98c5d2e2020-08-25 11:38:48 +0100528 socket_init();
Christophe Fergeauf5852ef2019-01-31 17:46:14 +0100529 error_init(argv[0]);
Daniel P. Berrangefe4db842016-10-04 14:35:52 +0100530 module_call_init(MODULE_INIT_TRACE);
Fam Zheng10f5bff2014-02-10 14:48:51 +0800531 qemu_init_exec_dir(argv[0]);
aliguorie3aff4f2009-04-05 19:14:04 +0000532
Eduardo Habkoste8f2d272016-05-12 11:10:04 -0300533 qcrypto_init(&error_fatal);
Daniel P. Berrangec2297082016-04-06 12:12:06 +0100534
Daniel P. Berrange064097d2016-02-10 18:41:01 +0000535 module_call_init(MODULE_INIT_QOM);
Denis V. Luneve9a80852016-06-17 17:44:11 +0300536 qemu_add_opts(&qemu_trace_opts);
Kevin Wolfbe6273d2014-11-20 16:27:06 +0100537 bdrv_init();
538
Devin Nakamura43642b32011-07-11 11:22:16 -0400539 while ((c = getopt_long(argc, argv, sopt, lopt, &opt_index)) != -1) {
540 switch (c) {
541 case 's':
542 flags |= BDRV_O_SNAPSHOT;
543 break;
544 case 'n':
Kevin Wolfe151fc12016-03-15 12:52:30 +0100545 flags |= BDRV_O_NOCACHE;
546 writethrough = false;
Devin Nakamura43642b32011-07-11 11:22:16 -0400547 break;
Eric Blake0f404442017-10-05 14:02:43 -0500548 case 'C':
549 flags |= BDRV_O_COPY_ON_READ;
550 break;
Paolo Bonzini9e8f1832013-02-08 14:06:11 +0100551 case 'd':
552 if (bdrv_parse_discard_flags(optarg, &flags) < 0) {
553 error_report("Invalid discard option: %s", optarg);
554 exit(1);
555 }
556 break;
Kevin Wolfbe6273d2014-11-20 16:27:06 +0100557 case 'f':
Daniel P. Berrange499afa22016-02-17 10:10:18 +0000558 format = optarg;
Kevin Wolfbe6273d2014-11-20 16:27:06 +0100559 break;
Devin Nakamura43642b32011-07-11 11:22:16 -0400560 case 'c':
561 add_user_command(optarg);
562 break;
563 case 'r':
564 readonly = 1;
565 break;
566 case 'm':
Stefan Weilf9883882014-03-05 22:23:00 +0100567 qemuio_misalign = true;
Devin Nakamura43642b32011-07-11 11:22:16 -0400568 break;
Devin Nakamura43642b32011-07-11 11:22:16 -0400569 case 'k':
570 flags |= BDRV_O_NATIVE_AIO;
571 break;
Aarushi Mehta1c5a2ae2020-01-20 14:18:54 +0000572 case 'i':
573 if (bdrv_parse_aio(optarg, &flags) < 0) {
574 error_report("Invalid aio option: %s", optarg);
575 exit(1);
576 }
577 break;
Kevin Wolf592fa072012-04-18 12:07:39 +0200578 case 't':
Kevin Wolfe151fc12016-03-15 12:52:30 +0100579 if (bdrv_parse_cache_mode(optarg, &flags, &writethrough) < 0) {
Kevin Wolf592fa072012-04-18 12:07:39 +0200580 error_report("Invalid cache option: %s", optarg);
581 exit(1);
582 }
583 break;
Stefan Hajnoczid7bb72c2012-03-12 16:36:07 +0000584 case 'T':
Paolo Bonzini92eecff2020-11-02 06:58:41 -0500585 trace_opt_parse(optarg);
Stefan Hajnoczid7bb72c2012-03-12 16:36:07 +0000586 break;
Devin Nakamura43642b32011-07-11 11:22:16 -0400587 case 'V':
Thomas Huth7e563bf2018-02-15 12:06:47 +0100588 printf("%s version " QEMU_FULL_VERSION "\n"
Christophe Fergeau99e98d72019-01-31 17:46:13 +0100589 QEMU_COPYRIGHT "\n", error_get_progname());
Devin Nakamura43642b32011-07-11 11:22:16 -0400590 exit(0);
591 case 'h':
Christophe Fergeau99e98d72019-01-31 17:46:13 +0100592 usage(error_get_progname());
Devin Nakamura43642b32011-07-11 11:22:16 -0400593 exit(0);
Fam Zheng459571f2017-05-03 00:35:41 +0800594 case 'U':
595 force_share = true;
596 break;
Kevin Wolfb3e79bc2021-02-17 12:56:45 +0100597 case OPTION_OBJECT:
598 user_creatable_process_cmdline(optarg);
599 break;
Daniel P. Berrange499afa22016-02-17 10:10:18 +0000600 case OPTION_IMAGE_OPTS:
601 imageOpts = true;
602 break;
Devin Nakamura43642b32011-07-11 11:22:16 -0400603 default:
Christophe Fergeau99e98d72019-01-31 17:46:13 +0100604 usage(error_get_progname());
Devin Nakamura43642b32011-07-11 11:22:16 -0400605 exit(1);
Naphtali Spreif5edb012010-01-17 16:48:13 +0200606 }
Devin Nakamura43642b32011-07-11 11:22:16 -0400607 }
aliguorie3aff4f2009-04-05 19:14:04 +0000608
Devin Nakamura43642b32011-07-11 11:22:16 -0400609 if ((argc - optind) > 1) {
Christophe Fergeau99e98d72019-01-31 17:46:13 +0100610 usage(error_get_progname());
Devin Nakamura43642b32011-07-11 11:22:16 -0400611 exit(1);
612 }
aliguorie3aff4f2009-04-05 19:14:04 +0000613
Daniel P. Berrange499afa22016-02-17 10:10:18 +0000614 if (format && imageOpts) {
615 error_report("--image-opts and -f are mutually exclusive");
616 exit(1);
617 }
618
Chrysostomos Nanakos2f78e492014-09-18 14:30:49 +0300619 if (qemu_init_main_loop(&local_error)) {
Markus Armbruster565f65d2015-02-12 13:55:05 +0100620 error_report_err(local_error);
Chrysostomos Nanakos2f78e492014-09-18 14:30:49 +0300621 exit(1);
622 }
Zhi Yong Wua57d1142012-02-19 22:24:59 +0800623
Denis V. Luneve9a80852016-06-17 17:44:11 +0300624 if (!trace_init_backends()) {
625 exit(1);
626 }
Paolo Bonzini92eecff2020-11-02 06:58:41 -0500627 trace_init_file();
Denis V. Luneve9a80852016-06-17 17:44:11 +0300628 qemu_set_log(LOG_TRACE);
629
Devin Nakamura43642b32011-07-11 11:22:16 -0400630 /* initialize commands */
Kevin Wolfc2cdf5c2013-06-05 14:19:36 +0200631 qemuio_add_command(&quit_cmd);
632 qemuio_add_command(&open_cmd);
633 qemuio_add_command(&close_cmd);
Devin Nakamura43642b32011-07-11 11:22:16 -0400634
Stefan Hajnoczi0cf17e12013-11-14 11:54:17 +0100635 if (isatty(STDIN_FILENO)) {
Daniel P. Berrange0e448a02018-02-12 18:48:49 +0000636 ttyEOF = get_eof_char();
Stefan Hajnoczi0cf17e12013-11-14 11:54:17 +0100637 readline_state = readline_init(readline_printf_func,
638 readline_flush_func,
639 NULL,
640 readline_completion_func);
641 qemu_set_tty_echo(STDIN_FILENO, false);
642 atexit(reenable_tty_echo);
643 }
644
Devin Nakamura43642b32011-07-11 11:22:16 -0400645 /* open the device */
646 if (!readonly) {
647 flags |= BDRV_O_RDWR;
648 }
649
650 if ((argc - optind) == 1) {
Daniel P. Berrange499afa22016-02-17 10:10:18 +0000651 if (imageOpts) {
652 QemuOpts *qopts = NULL;
653 qopts = qemu_opts_parse_noisily(&file_opts, argv[optind], false);
654 if (!qopts) {
655 exit(1);
656 }
657 opts = qemu_opts_to_qdict(qopts, NULL);
Fam Zheng459571f2017-05-03 00:35:41 +0800658 if (openfile(NULL, flags, writethrough, force_share, opts)) {
Nir Sofferb7aa1312017-02-01 02:31:18 +0200659 exit(1);
660 }
Daniel P. Berrange499afa22016-02-17 10:10:18 +0000661 } else {
662 if (format) {
663 opts = qdict_new();
Eric Blake46f5ac22017-04-27 16:58:17 -0500664 qdict_put_str(opts, "driver", format);
Daniel P. Berrange499afa22016-02-17 10:10:18 +0000665 }
Fam Zheng459571f2017-05-03 00:35:41 +0800666 if (openfile(argv[optind], flags, writethrough,
667 force_share, opts)) {
Nir Sofferb7aa1312017-02-01 02:31:18 +0200668 exit(1);
669 }
Daniel P. Berrange499afa22016-02-17 10:10:18 +0000670 }
Devin Nakamura43642b32011-07-11 11:22:16 -0400671 }
Max Reitz6b3aa842018-05-09 21:43:00 +0200672 ret = command_loop();
Devin Nakamura43642b32011-07-11 11:22:16 -0400673
674 /*
Stefan Hajnoczi922453b2011-11-30 12:23:43 +0000675 * Make sure all outstanding requests complete before the program exits.
Devin Nakamura43642b32011-07-11 11:22:16 -0400676 */
Stefan Hajnoczi922453b2011-11-30 12:23:43 +0000677 bdrv_drain_all();
Devin Nakamura43642b32011-07-11 11:22:16 -0400678
Markus Armbruster26f54e92014-10-07 13:59:04 +0200679 blk_unref(qemuio_blk);
Stefan Hajnoczi0cf17e12013-11-14 11:54:17 +0100680 g_free(readline_state);
Max Reitz6b3aa842018-05-09 21:43:00 +0200681
682 if (ret < 0) {
683 return 1;
684 } else {
685 return 0;
686 }
aliguorie3aff4f2009-04-05 19:14:04 +0000687}