blob: 608c63e82a255e080b6e28377c4f17d2b851ab0d [file] [log] [blame]
thscd831bd2008-07-03 10:23:51 +00001/*
bellard7a5ca862008-05-27 21:13:40 +00002 * Copyright (C) 2005 Anthony Liguori <anthony@codemonkey.ws>
3 *
4 * Network Block Device
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; under version 2 of the License.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
Blue Swirl8167ee82009-07-16 20:47:01 +000016 * along with this program; if not, see <http://www.gnu.org/licenses/>.
bellard7a5ca862008-05-27 21:13:40 +000017 */
18
Peter Maydelld38ea872016-01-29 17:50:05 +000019#include "qemu/osdep.h"
Eric Blakec2a3d7d2017-07-21 08:50:47 -050020#include <getopt.h>
21#include <libgen.h>
22#include <pthread.h>
23
Markus Armbrustera8d25322019-05-23 16:35:08 +020024#include "qemu-common.h"
Markus Armbrusterda34e652016-03-14 09:01:28 +010025#include "qapi/error.h"
Veronia Bahaaf348b6d2016-03-20 19:16:19 +020026#include "qemu/cutils.h"
Markus Armbruster26f54e92014-10-07 13:59:04 +020027#include "sysemu/block-backend.h"
Stefan Hajnoczicbc20bf2020-09-29 13:55:15 +010028#include "sysemu/runstate.h" /* for qemu_system_killed() prototype */
Peter Lievenb3838a42014-08-13 19:20:18 +020029#include "block/block_int.h"
Paolo Bonzini737e1502012-12-17 18:19:44 +010030#include "block/nbd.h"
Alex Bligh6a1751b2013-08-21 16:02:47 +010031#include "qemu/main-loop.h"
Markus Armbruster0b8fa322019-05-23 16:35:07 +020032#include "qemu/module.h"
Markus Armbruster922a01a2018-02-01 12:18:46 +010033#include "qemu/option.h"
Paolo Bonzini537b41f2014-02-17 14:43:51 +010034#include "qemu/error-report.h"
Daniel P. Berrange0ab3b332016-02-10 18:41:00 +000035#include "qemu/config-file.h"
Paolo Bonzini58369e22016-03-15 17:22:36 +010036#include "qemu/bswap.h"
Denis V. Lunev39ca4632016-06-17 17:44:12 +030037#include "qemu/log.h"
Paolo Bonzini53fabd42017-03-16 16:29:45 +010038#include "qemu/systemd.h"
Wenchao Xia8c116b02013-12-04 17:10:55 +080039#include "block/snapshot.h"
Markus Armbruster452fcdb2018-02-01 12:18:39 +010040#include "qapi/qmp/qdict.h"
Markus Armbrusterd49b6832015-03-17 18:29:20 +010041#include "qapi/qmp/qstring.h"
Daniel P. Berrange0ab3b332016-02-10 18:41:00 +000042#include "qom/object_interfaces.h"
Daniel P. Berranged0d6ff52016-02-10 18:41:02 +000043#include "io/channel-socket.h"
Daniel P. Berrangee4849c12017-12-18 10:16:43 +000044#include "io/net-listener.h"
Daniel P. Berrangec2297082016-04-06 12:12:06 +010045#include "crypto/init.h"
Denis V. Lunev39ca4632016-06-17 17:44:12 +030046#include "trace/control.h"
Eric Blakebe377132017-07-21 08:50:46 -050047#include "qemu-version.h"
bellard7a5ca862008-05-27 21:13:40 +000048
Eric Blake3c1fa352018-12-15 07:53:08 -060049#ifdef __linux__
50#define HAVE_NBD_DEVICE 1
51#else
52#define HAVE_NBD_DEVICE 0
53#endif
54
Peter Lieven713cc672014-08-13 19:20:19 +020055#define SOCKET_PATH "/var/lock/qemu-nbd-%s"
Daniel P. Berrangefa8b7ce2016-02-17 10:10:21 +000056#define QEMU_NBD_OPT_CACHE 256
57#define QEMU_NBD_OPT_AIO 257
58#define QEMU_NBD_OPT_DISCARD 258
59#define QEMU_NBD_OPT_DETECT_ZEROES 259
60#define QEMU_NBD_OPT_OBJECT 260
61#define QEMU_NBD_OPT_TLSCREDS 261
62#define QEMU_NBD_OPT_IMAGE_OPTS 262
Max Reitzffb31e12016-09-28 22:46:42 +020063#define QEMU_NBD_OPT_FORK 263
Daniel P. Berrangeb25e12d2019-02-27 16:20:33 +000064#define QEMU_NBD_OPT_TLSAUTHZ 264
Max Reitz637bc5a2019-05-08 23:18:16 +020065#define QEMU_NBD_OPT_PID_FILE 265
bellard7a5ca862008-05-27 21:13:40 +000066
Eric Blakebd31c212016-05-06 10:26:42 -060067#define MBR_SIZE 512
68
blueswir1b1d8e522008-10-26 13:43:07 +000069static int verbose;
Paolo Bonzinia517e882011-11-04 15:51:21 +010070static char *srcpath;
Markus Armbrusterbd269eb2017-04-26 09:36:41 +020071static SocketAddress *saddr;
Paolo Bonzini7860a382012-09-18 13:31:56 +020072static int persistent = 0;
Kevin Wolfd794f7f2020-09-24 17:26:56 +020073static enum { RUNNING, TERMINATE, TERMINATED } state;
Paolo Bonzinia61c6782011-09-12 17:28:11 +020074static int shared = 1;
75static int nb_fds;
Daniel P. Berrangee4849c12017-12-18 10:16:43 +000076static QIONetListener *server;
Daniel P. Berrange145614a2016-02-10 18:41:13 +000077static QCryptoTLSCreds *tlscreds;
Daniel P. Berrangeb25e12d2019-02-27 16:20:33 +000078static const char *tlsauthz;
bellard7a5ca862008-05-27 21:13:40 +000079
80static void usage(const char *name)
81{
Paolo Bonzinib033cd82012-07-18 14:50:52 +020082 (printf) (
bellard7a5ca862008-05-27 21:13:40 +000083"Usage: %s [OPTIONS] FILE\n"
Eric Blake68b96f12019-01-17 13:36:56 -060084" or: %s -L [OPTIONS]\n"
85"QEMU Disk Network Block Device Utility\n"
bellard7a5ca862008-05-27 21:13:40 +000086"\n"
Peter Lieven713cc672014-08-13 19:20:19 +020087" -h, --help display this help and exit\n"
88" -V, --version output version information and exit\n"
bellard7a5ca862008-05-27 21:13:40 +000089"\n"
Paolo Bonzinib033cd82012-07-18 14:50:52 +020090"Connection properties:\n"
Peter Lieven713cc672014-08-13 19:20:19 +020091" -p, --port=PORT port to listen on (default `%d')\n"
92" -b, --bind=IFACE interface to bind to (default `0.0.0.0')\n"
93" -k, --socket=PATH path to the unix socket\n"
94" (default '"SOCKET_PATH"')\n"
95" -e, --shared=NUM device can be shared by NUM clients (default '1')\n"
96" -t, --persistent don't exit on the last connection\n"
97" -v, --verbose display extra debugging information\n"
Vladimir Sementsov-Ogievskiyf5cd0bb2018-10-03 20:02:27 +030098" -x, --export-name=NAME expose export by name (default is empty string)\n"
99" -D, --description=TEXT export a human-readable description\n"
Paolo Bonzinib033cd82012-07-18 14:50:52 +0200100"\n"
101"Exposing part of the image:\n"
Peter Lieven713cc672014-08-13 19:20:19 +0200102" -o, --offset=OFFSET offset into the image\n"
Eric Blakedbc7b012020-10-27 00:05:55 -0500103" -A, --allocation-depth expose the allocation depth\n"
Eric Blake636192c2019-01-11 13:47:20 -0600104" -B, --bitmap=NAME expose a persistent dirty bitmap\n"
Paolo Bonzinib033cd82012-07-18 14:50:52 +0200105"\n"
Daniel P. Berrange0ab3b332016-02-10 18:41:00 +0000106"General purpose options:\n"
Eric Blake68b96f12019-01-17 13:36:56 -0600107" -L, --list list exports available from another NBD server\n"
Daniel P. Berrange0ab3b332016-02-10 18:41:00 +0000108" --object type,id=ID,... define an object such as 'secret' for providing\n"
109" passwords and/or encryption keys\n"
Eric Blakef7812df2018-10-03 13:04:26 -0500110" --tls-creds=ID use id of an earlier --object to provide TLS\n"
Daniel P. Berrangeb25e12d2019-02-27 16:20:33 +0000111" --tls-authz=ID use id of an earlier --object to provide\n"
112" authorization\n"
Denis V. Lunev39ca4632016-06-17 17:44:12 +0300113" -T, --trace [[enable=]<pattern>][,events=<file>][,file=<file>]\n"
114" specify tracing options\n"
Max Reitzffb31e12016-09-28 22:46:42 +0200115" --fork fork off the server process and exit the parent\n"
116" once the server is running\n"
Max Reitz637bc5a2019-05-08 23:18:16 +0200117" --pid-file=PATH store the server's process ID in the given file\n"
Eric Blake3c1fa352018-12-15 07:53:08 -0600118#if HAVE_NBD_DEVICE
119"\n"
Paolo Bonzinib033cd82012-07-18 14:50:52 +0200120"Kernel NBD client support:\n"
Peter Lieven713cc672014-08-13 19:20:19 +0200121" -c, --connect=DEV connect FILE to the local NBD device DEV\n"
122" -d, --disconnect disconnect the specified device\n"
Paolo Bonzinib033cd82012-07-18 14:50:52 +0200123#endif
124"\n"
125"Block device options:\n"
Peter Lieven713cc672014-08-13 19:20:19 +0200126" -f, --format=FORMAT set image format (raw, qcow2, ...)\n"
127" -r, --read-only export read-only\n"
128" -s, --snapshot use FILE as an external snapshot, create a temporary\n"
129" file with backing_file=FILE, redirect the write to\n"
130" the temporary one\n"
Wenchao Xia8c116b02013-12-04 17:10:55 +0800131" -l, --load-snapshot=SNAPSHOT_PARAM\n"
Peter Lieven713cc672014-08-13 19:20:19 +0200132" load an internal snapshot inside FILE and export it\n"
133" as an read-only device, SNAPSHOT_PARAM format is\n"
134" 'snapshot.id=[ID],snapshot.name=[NAME]', or\n"
135" '[ID_OR_NAME]'\n"
136" -n, --nocache disable host cache\n"
137" --cache=MODE set cache mode (none, writeback, ...)\n"
Aarushi Mehta76802742020-01-20 14:18:56 +0000138" --aio=MODE set AIO mode (native, io_uring or threads)\n"
Peter Lievenb3838a42014-08-13 19:20:18 +0200139" --discard=MODE set discard mode (ignore, unmap)\n"
Andrey Korolyov6883de62015-07-31 22:12:54 +0300140" --detect-zeroes=MODE set detect-zeroes mode (off, on, unmap)\n"
Daniel P. Berrange77c9aae2016-02-17 10:10:19 +0000141" --image-opts treat FILE as a full set of image options\n"
Paolo Bonzinib033cd82012-07-18 14:50:52 +0200142"\n"
Eric Blakef5048cb2017-08-03 11:33:53 -0500143QEMU_HELP_BOTTOM "\n"
Eric Blake68b96f12019-01-17 13:36:56 -0600144 , name, name, NBD_DEFAULT_PORT, "DEVICE");
bellard7a5ca862008-05-27 21:13:40 +0000145}
146
147static void version(const char *name)
148{
149 printf(
Thomas Huth7e563bf2018-02-15 12:06:47 +0100150"%s " QEMU_FULL_VERSION "\n"
bellard7a5ca862008-05-27 21:13:40 +0000151"Written by Anthony Liguori.\n"
152"\n"
Eric Blakebe377132017-07-21 08:50:46 -0500153QEMU_COPYRIGHT "\n"
bellard7a5ca862008-05-27 21:13:40 +0000154"This is free software; see the source for copying conditions. There is NO\n"
155"warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n"
ths315bc7a2008-07-18 18:06:23 +0000156 , name);
bellard7a5ca862008-05-27 21:13:40 +0000157}
158
Eric Blake029a88c2020-09-30 07:11:01 -0500159#ifdef CONFIG_POSIX
Stefan Hajnoczicbc20bf2020-09-29 13:55:15 +0100160/*
161 * The client thread uses SIGTERM to interrupt the server. A signal
162 * handler ensures that "qemu-nbd -v -c" exits with a nice status code.
163 */
164void qemu_system_killed(int signum, pid_t pid)
Paolo Bonzinibb345112011-11-04 15:51:19 +0100165{
Stefan Hajnoczid73415a2020-09-23 11:56:46 +0100166 qatomic_cmpxchg(&state, RUNNING, TERMINATE);
Paolo Bonzinia61c6782011-09-12 17:28:11 +0200167 qemu_notify_event();
Paolo Bonzinibb345112011-11-04 15:51:19 +0100168}
Eric Blake029a88c2020-09-30 07:11:01 -0500169#endif /* CONFIG_POSIX */
Paolo Bonzini537b41f2014-02-17 14:43:51 +0100170
Eric Blake68b96f12019-01-17 13:36:56 -0600171static int qemu_nbd_client_list(SocketAddress *saddr, QCryptoTLSCreds *tls,
172 const char *hostname)
173{
174 int ret = EXIT_FAILURE;
175 int rc;
176 Error *err = NULL;
177 QIOChannelSocket *sioc;
178 NBDExportInfo *list;
179 int i, j;
180
181 sioc = qio_channel_socket_new();
182 if (qio_channel_socket_connect_sync(sioc, saddr, &err) < 0) {
183 error_report_err(err);
Alex Chen992809b2020-11-30 12:36:51 +0000184 goto out;
Eric Blake68b96f12019-01-17 13:36:56 -0600185 }
186 rc = nbd_receive_export_list(QIO_CHANNEL(sioc), tls, hostname, &list,
187 &err);
188 if (rc < 0) {
189 if (err) {
190 error_report_err(err);
191 }
192 goto out;
193 }
194 printf("exports available: %d\n", rc);
195 for (i = 0; i < rc; i++) {
196 printf(" export: '%s'\n", list[i].name);
197 if (list[i].description && *list[i].description) {
198 printf(" description: %s\n", list[i].description);
199 }
200 if (list[i].flags & NBD_FLAG_HAS_FLAGS) {
Max Reitzc4e2aff2019-04-05 21:16:35 +0200201 static const char *const flag_names[] = {
202 [NBD_FLAG_READ_ONLY_BIT] = "readonly",
203 [NBD_FLAG_SEND_FLUSH_BIT] = "flush",
204 [NBD_FLAG_SEND_FUA_BIT] = "fua",
205 [NBD_FLAG_ROTATIONAL_BIT] = "rotational",
206 [NBD_FLAG_SEND_TRIM_BIT] = "trim",
207 [NBD_FLAG_SEND_WRITE_ZEROES_BIT] = "zeroes",
208 [NBD_FLAG_SEND_DF_BIT] = "df",
209 [NBD_FLAG_CAN_MULTI_CONN_BIT] = "multi",
210 [NBD_FLAG_SEND_RESIZE_BIT] = "resize",
211 [NBD_FLAG_SEND_CACHE_BIT] = "cache",
Eric Blake0a479542019-08-23 09:37:23 -0500212 [NBD_FLAG_SEND_FAST_ZERO_BIT] = "fast-zero",
Max Reitzc4e2aff2019-04-05 21:16:35 +0200213 };
214
Eric Blake68b96f12019-01-17 13:36:56 -0600215 printf(" size: %" PRIu64 "\n", list[i].size);
216 printf(" flags: 0x%x (", list[i].flags);
Max Reitzc4e2aff2019-04-05 21:16:35 +0200217 for (size_t bit = 0; bit < ARRAY_SIZE(flag_names); bit++) {
218 if (flag_names[bit] && (list[i].flags & (1 << bit))) {
219 printf(" %s", flag_names[bit]);
220 }
Eric Blake68b96f12019-01-17 13:36:56 -0600221 }
222 printf(" )\n");
223 }
224 if (list[i].min_block) {
225 printf(" min block: %u\n", list[i].min_block);
226 printf(" opt block: %u\n", list[i].opt_block);
227 printf(" max block: %u\n", list[i].max_block);
228 }
229 if (list[i].n_contexts) {
230 printf(" available meta contexts: %d\n", list[i].n_contexts);
231 for (j = 0; j < list[i].n_contexts; j++) {
232 printf(" %s\n", list[i].contexts[j]);
233 }
234 }
235 }
236 nbd_free_export_list(list, rc);
237
238 ret = EXIT_SUCCESS;
239 out:
240 object_unref(OBJECT(sioc));
241 return ret;
242}
243
244
Eric Blake3c1fa352018-12-15 07:53:08 -0600245#if HAVE_NBD_DEVICE
Paolo Bonzinia517e882011-11-04 15:51:21 +0100246static void *show_parts(void *arg)
thscd831bd2008-07-03 10:23:51 +0000247{
Paolo Bonzinia6ac2312011-12-06 09:07:00 +0100248 char *device = arg;
Paolo Bonzinia517e882011-11-04 15:51:21 +0100249 int nbd;
thscd831bd2008-07-03 10:23:51 +0000250
Paolo Bonzinia517e882011-11-04 15:51:21 +0100251 /* linux just needs an open() to trigger
252 * the partition table update
253 * but remember to load the module with max_part != 0 :
254 * modprobe nbd max_part=63
255 */
256 nbd = open(device, O_RDWR);
Paolo Bonzinifc19f8a2012-03-07 11:05:34 +0100257 if (nbd >= 0) {
Paolo Bonzinia517e882011-11-04 15:51:21 +0100258 close(nbd);
thscd831bd2008-07-03 10:23:51 +0000259 }
Paolo Bonzinia517e882011-11-04 15:51:21 +0100260 return NULL;
261}
262
263static void *nbd_client_thread(void *arg)
264{
Paolo Bonzinia6ac2312011-12-06 09:07:00 +0100265 char *device = arg;
Eric Blake6dc16672019-01-17 13:36:46 -0600266 NBDExportInfo info = { .request_sizes = false, .name = g_strdup("") };
Daniel P. Berranged0d6ff52016-02-10 18:41:02 +0000267 QIOChannelSocket *sioc;
Alex Chenaf74b552020-12-08 13:49:44 +0000268 int fd = -1;
269 int ret = EXIT_FAILURE;
Paolo Bonzinia517e882011-11-04 15:51:21 +0100270 pthread_t show_parts_thread;
Max Reitz1ce52842015-01-26 21:02:59 -0500271 Error *local_error = NULL;
Paolo Bonzinia517e882011-11-04 15:51:21 +0100272
Daniel P. Berranged0d6ff52016-02-10 18:41:02 +0000273 sioc = qio_channel_socket_new();
274 if (qio_channel_socket_connect_sync(sioc,
275 saddr,
276 &local_error) < 0) {
Daniel P. Berrange48bec072015-09-16 14:52:23 +0100277 error_report_err(local_error);
Stefan Hajnoczidc10e8b2012-01-05 13:16:07 +0000278 goto out;
279 }
Paolo Bonzinia517e882011-11-04 15:51:21 +0100280
Alex Chenaf74b552020-12-08 13:49:44 +0000281 if (nbd_receive_negotiate(NULL, QIO_CHANNEL(sioc),
282 NULL, NULL, NULL, &info, &local_error) < 0) {
Max Reitz1ce52842015-01-26 21:02:59 -0500283 if (local_error) {
Markus Armbruster78288672015-12-18 16:35:07 +0100284 error_report_err(local_error);
Max Reitz1ce52842015-01-26 21:02:59 -0500285 }
Alex Chenaf74b552020-12-08 13:49:44 +0000286 goto out;
Paolo Bonzinia517e882011-11-04 15:51:21 +0100287 }
288
Paolo Bonzinia6ac2312011-12-06 09:07:00 +0100289 fd = open(device, O_RDWR);
Paolo Bonzinifc19f8a2012-03-07 11:05:34 +0100290 if (fd < 0) {
Paolo Bonzinia6ac2312011-12-06 09:07:00 +0100291 /* Linux-only, we can use %m in printf. */
Markus Armbrusterb9884682015-12-18 16:35:18 +0100292 error_report("Failed to open %s: %m", device);
Alex Chenaf74b552020-12-08 13:49:44 +0000293 goto out;
Paolo Bonzinia6ac2312011-12-06 09:07:00 +0100294 }
295
Alex Chenaf74b552020-12-08 13:49:44 +0000296 if (nbd_init(fd, sioc, &info, &local_error) < 0) {
Vladimir Sementsov-Ogievskiybe41c102017-05-26 14:09:13 +0300297 error_report_err(local_error);
Alex Chenaf74b552020-12-08 13:49:44 +0000298 goto out;
Paolo Bonzinia517e882011-11-04 15:51:21 +0100299 }
300
301 /* update partition table */
Paolo Bonzinia6ac2312011-12-06 09:07:00 +0100302 pthread_create(&show_parts_thread, NULL, show_parts, device);
Paolo Bonzinia517e882011-11-04 15:51:21 +0100303
Paolo Bonzinic1f8fdc2011-11-04 15:51:22 +0100304 if (verbose) {
305 fprintf(stderr, "NBD device %s is now connected to %s\n",
306 device, srcpath);
307 } else {
308 /* Close stderr so that the qemu-nbd process exits. */
309 dup2(STDOUT_FILENO, STDERR_FILENO);
310 }
Paolo Bonzinia517e882011-11-04 15:51:21 +0100311
Alex Chenaf74b552020-12-08 13:49:44 +0000312 if (nbd_client(fd) < 0) {
313 goto out;
Paolo Bonzinia517e882011-11-04 15:51:21 +0100314 }
Paolo Bonzinia517e882011-11-04 15:51:21 +0100315
Alex Chenaf74b552020-12-08 13:49:44 +0000316 ret = EXIT_SUCCESS;
317
318 out:
319 if (fd >= 0) {
320 close(fd);
321 }
Daniel P. Berranged0d6ff52016-02-10 18:41:02 +0000322 object_unref(OBJECT(sioc));
Eric Blake6dc16672019-01-17 13:36:46 -0600323 g_free(info.name);
Paolo Bonzinia517e882011-11-04 15:51:21 +0100324 kill(getpid(), SIGTERM);
Alex Chenaf74b552020-12-08 13:49:44 +0000325 return (void *) (intptr_t) ret;
thscd831bd2008-07-03 10:23:51 +0000326}
Eric Blake3c1fa352018-12-15 07:53:08 -0600327#endif /* HAVE_NBD_DEVICE */
thscd831bd2008-07-03 10:23:51 +0000328
Fam Zhenge4afbf42015-05-19 10:50:59 +0000329static int nbd_can_accept(void)
Paolo Bonzinia61c6782011-09-12 17:28:11 +0200330{
Eric Blakedf8ad9f2017-05-26 22:04:21 -0500331 return state == RUNNING && nb_fds < shared;
Paolo Bonzinia61c6782011-09-12 17:28:11 +0200332}
333
Daniel P. Berranged0d6ff52016-02-10 18:41:02 +0000334static void nbd_update_server_watch(void);
Fam Zhenge4afbf42015-05-19 10:50:59 +0000335
Eric Blake0c9390d2017-06-08 17:26:17 -0500336static void nbd_client_closed(NBDClient *client, bool negotiated)
Paolo Bonzinia61c6782011-09-12 17:28:11 +0200337{
Paolo Bonzini1743b512011-09-19 14:33:23 +0200338 nb_fds--;
Eric Blake0c9390d2017-06-08 17:26:17 -0500339 if (negotiated && nb_fds == 0 && !persistent && state == RUNNING) {
Paolo Bonzini7860a382012-09-18 13:31:56 +0200340 state = TERMINATE;
341 }
Daniel P. Berranged0d6ff52016-02-10 18:41:02 +0000342 nbd_update_server_watch();
Paolo Bonzini7860a382012-09-18 13:31:56 +0200343 nbd_client_put(client);
Paolo Bonzinia61c6782011-09-12 17:28:11 +0200344}
345
Daniel P. Berrangee4849c12017-12-18 10:16:43 +0000346static void nbd_accept(QIONetListener *listener, QIOChannelSocket *cioc,
347 gpointer opaque)
Paolo Bonzinia61c6782011-09-12 17:28:11 +0200348{
Paolo Bonzini7860a382012-09-18 13:31:56 +0200349 if (state >= TERMINATE) {
Daniel P. Berrangee4849c12017-12-18 10:16:43 +0000350 return;
Paolo Bonzini7860a382012-09-18 13:31:56 +0200351 }
352
Fam Zhengee7d7aa2016-01-14 16:41:01 +0800353 nb_fds++;
Daniel P. Berranged0d6ff52016-02-10 18:41:02 +0000354 nbd_update_server_watch();
Daniel P. Berrangeb25e12d2019-02-27 16:20:33 +0000355 nbd_client_new(cioc, tlscreds, tlsauthz, nbd_client_closed);
Paolo Bonzinia61c6782011-09-12 17:28:11 +0200356}
357
Daniel P. Berranged0d6ff52016-02-10 18:41:02 +0000358static void nbd_update_server_watch(void)
Fam Zhenge4afbf42015-05-19 10:50:59 +0000359{
360 if (nbd_can_accept()) {
Daniel P. Berrangee4849c12017-12-18 10:16:43 +0000361 qio_net_listener_set_client_func(server, nbd_accept, NULL, NULL);
Fam Zhenge4afbf42015-05-19 10:50:59 +0000362 } else {
Daniel P. Berrangee4849c12017-12-18 10:16:43 +0000363 qio_net_listener_set_client_func(server, NULL, NULL, NULL);
Fam Zhenge4afbf42015-05-19 10:50:59 +0000364 }
365}
366
Daniel P. Berrange48bec072015-09-16 14:52:23 +0100367
Markus Armbrusterbd269eb2017-04-26 09:36:41 +0200368static SocketAddress *nbd_build_socket_address(const char *sockpath,
Daniel P. Berrange48bec072015-09-16 14:52:23 +0100369 const char *bindto,
370 const char *port)
371{
Markus Armbrusterbd269eb2017-04-26 09:36:41 +0200372 SocketAddress *saddr;
Daniel P. Berrange48bec072015-09-16 14:52:23 +0100373
Markus Armbrusterbd269eb2017-04-26 09:36:41 +0200374 saddr = g_new0(SocketAddress, 1);
Daniel P. Berrange48bec072015-09-16 14:52:23 +0100375 if (sockpath) {
Markus Armbrusterbd269eb2017-04-26 09:36:41 +0200376 saddr->type = SOCKET_ADDRESS_TYPE_UNIX;
377 saddr->u.q_unix.path = g_strdup(sockpath);
Daniel P. Berrange48bec072015-09-16 14:52:23 +0100378 } else {
Eric Blake03992932016-03-03 09:16:48 -0700379 InetSocketAddress *inet;
Markus Armbrusterbd269eb2017-04-26 09:36:41 +0200380 saddr->type = SOCKET_ADDRESS_TYPE_INET;
381 inet = &saddr->u.inet;
Eric Blake03992932016-03-03 09:16:48 -0700382 inet->host = g_strdup(bindto);
Daniel P. Berrange48bec072015-09-16 14:52:23 +0100383 if (port) {
Eric Blake03992932016-03-03 09:16:48 -0700384 inet->port = g_strdup(port);
Daniel P. Berrange48bec072015-09-16 14:52:23 +0100385 } else {
Eric Blake03992932016-03-03 09:16:48 -0700386 inet->port = g_strdup_printf("%d", NBD_DEFAULT_PORT);
Daniel P. Berrange48bec072015-09-16 14:52:23 +0100387 }
388 }
389
390 return saddr;
391}
392
393
Daniel P. Berrange77c9aae2016-02-17 10:10:19 +0000394static QemuOptsList file_opts = {
395 .name = "file",
396 .implied_opt_name = "file",
397 .head = QTAILQ_HEAD_INITIALIZER(file_opts.head),
398 .desc = {
399 /* no elements => accept any params */
400 { /* end of list */ }
401 },
402};
403
Daniel P. Berrange0ab3b332016-02-10 18:41:00 +0000404static QemuOptsList qemu_object_opts = {
405 .name = "object",
406 .implied_opt_name = "qom-type",
407 .head = QTAILQ_HEAD_INITIALIZER(qemu_object_opts.head),
408 .desc = {
409 { }
410 },
411};
412
Kevin Wolf495bf892019-10-11 21:49:17 +0200413static bool qemu_nbd_object_print_help(const char *type, QemuOpts *opts)
414{
415 if (user_creatable_print_help(type, opts)) {
416 exit(0);
417 }
418 return true;
419}
Daniel P. Berrange0ab3b332016-02-10 18:41:00 +0000420
Daniel P. Berrange145614a2016-02-10 18:41:13 +0000421
Eric Blake68b96f12019-01-17 13:36:56 -0600422static QCryptoTLSCreds *nbd_get_tls_creds(const char *id, bool list,
423 Error **errp)
Daniel P. Berrange145614a2016-02-10 18:41:13 +0000424{
425 Object *obj;
426 QCryptoTLSCreds *creds;
427
428 obj = object_resolve_path_component(
429 object_get_objects_root(), id);
430 if (!obj) {
431 error_setg(errp, "No TLS credentials with id '%s'",
432 id);
433 return NULL;
434 }
435 creds = (QCryptoTLSCreds *)
436 object_dynamic_cast(obj, TYPE_QCRYPTO_TLS_CREDS);
437 if (!creds) {
438 error_setg(errp, "Object with id '%s' is not TLS credentials",
439 id);
440 return NULL;
441 }
442
Eric Blake68b96f12019-01-17 13:36:56 -0600443 if (list) {
444 if (creds->endpoint != QCRYPTO_TLS_CREDS_ENDPOINT_CLIENT) {
445 error_setg(errp,
446 "Expecting TLS credentials with a client endpoint");
447 return NULL;
448 }
449 } else {
450 if (creds->endpoint != QCRYPTO_TLS_CREDS_ENDPOINT_SERVER) {
451 error_setg(errp,
452 "Expecting TLS credentials with a server endpoint");
453 return NULL;
454 }
Daniel P. Berrange145614a2016-02-10 18:41:13 +0000455 }
456 object_ref(obj);
457 return creds;
458}
459
Richard W.M. Jonesa721f532017-02-04 10:03:17 +0000460static void setup_address_and_port(const char **address, const char **port)
461{
462 if (*address == NULL) {
463 *address = "0.0.0.0";
464 }
465
466 if (*port == NULL) {
467 *port = stringify(NBD_DEFAULT_PORT);
468 }
469}
470
Richard W.M. Jonesa721f532017-02-04 10:03:17 +0000471/*
472 * Check socket parameters compatibility when socket activation is used.
473 */
474static const char *socket_activation_validate_opts(const char *device,
475 const char *sockpath,
476 const char *address,
Eric Blake68b96f12019-01-17 13:36:56 -0600477 const char *port,
478 bool list)
Richard W.M. Jonesa721f532017-02-04 10:03:17 +0000479{
480 if (device != NULL) {
481 return "NBD device can't be set when using socket activation";
482 }
483
484 if (sockpath != NULL) {
485 return "Unix socket can't be set when using socket activation";
486 }
487
488 if (address != NULL) {
489 return "The interface can't be set when using socket activation";
490 }
491
492 if (port != NULL) {
493 return "TCP port number can't be set when using socket activation";
494 }
495
Eric Blake68b96f12019-01-17 13:36:56 -0600496 if (list) {
497 return "List mode is incompatible with socket activation";
498 }
499
Richard W.M. Jonesa721f532017-02-04 10:03:17 +0000500 return NULL;
501}
Daniel P. Berrange145614a2016-02-10 18:41:13 +0000502
Kevin Wolfb3b52992018-05-16 13:46:37 +0200503static void qemu_nbd_shutdown(void)
504{
505 job_cancel_sync_all();
Sergio Lopez1895b972021-02-01 13:50:32 +0100506 blk_exp_close_all();
Kevin Wolfb3b52992018-05-16 13:46:37 +0200507 bdrv_close_all();
508}
509
bellard7a5ca862008-05-27 21:13:40 +0000510int main(int argc, char **argv)
511{
Markus Armbruster26f54e92014-10-07 13:59:04 +0200512 BlockBackend *blk;
bellard7a5ca862008-05-27 21:13:40 +0000513 BlockDriverState *bs;
Eric Blake9d26dfc2019-01-17 13:36:43 -0600514 uint64_t dev_offset = 0;
Eric Blakedbb38ca2019-08-23 09:37:22 -0500515 bool readonly = false;
thscd831bd2008-07-03 10:23:51 +0000516 bool disconnect = false;
Richard W.M. Jonesa721f532017-02-04 10:03:17 +0000517 const char *bindto = NULL;
Daniel P. Berrange48bec072015-09-16 14:52:23 +0100518 const char *port = NULL;
519 char *sockpath = NULL;
Paolo Bonzinia6ac2312011-12-06 09:07:00 +0100520 char *device = NULL;
Wenchao Xia8c116b02013-12-04 17:10:55 +0800521 QemuOpts *sn_opts = NULL;
522 const char *sn_id_or_name = NULL;
Eric Blakedbc7b012020-10-27 00:05:55 -0500523 const char *sopt = "hVb:o:p:rsnc:dvk:e:f:tl:x:T:D:AB:L";
bellard7a5ca862008-05-27 21:13:40 +0000524 struct option lopt[] = {
Daniel P. Berrangeaa6e5462016-02-17 10:10:22 +0000525 { "help", no_argument, NULL, 'h' },
526 { "version", no_argument, NULL, 'V' },
527 { "bind", required_argument, NULL, 'b' },
528 { "port", required_argument, NULL, 'p' },
529 { "socket", required_argument, NULL, 'k' },
530 { "offset", required_argument, NULL, 'o' },
531 { "read-only", no_argument, NULL, 'r' },
Eric Blakedbc7b012020-10-27 00:05:55 -0500532 { "allocation-depth", no_argument, NULL, 'A' },
Eric Blake636192c2019-01-11 13:47:20 -0600533 { "bitmap", required_argument, NULL, 'B' },
Daniel P. Berrangeaa6e5462016-02-17 10:10:22 +0000534 { "connect", required_argument, NULL, 'c' },
535 { "disconnect", no_argument, NULL, 'd' },
Eric Blake68b96f12019-01-17 13:36:56 -0600536 { "list", no_argument, NULL, 'L' },
Daniel P. Berrangeaa6e5462016-02-17 10:10:22 +0000537 { "snapshot", no_argument, NULL, 's' },
538 { "load-snapshot", required_argument, NULL, 'l' },
539 { "nocache", no_argument, NULL, 'n' },
540 { "cache", required_argument, NULL, QEMU_NBD_OPT_CACHE },
541 { "aio", required_argument, NULL, QEMU_NBD_OPT_AIO },
542 { "discard", required_argument, NULL, QEMU_NBD_OPT_DISCARD },
543 { "detect-zeroes", required_argument, NULL,
544 QEMU_NBD_OPT_DETECT_ZEROES },
545 { "shared", required_argument, NULL, 'e' },
546 { "format", required_argument, NULL, 'f' },
547 { "persistent", no_argument, NULL, 't' },
548 { "verbose", no_argument, NULL, 'v' },
549 { "object", required_argument, NULL, QEMU_NBD_OPT_OBJECT },
550 { "export-name", required_argument, NULL, 'x' },
Eric Blakeb1a75b32016-10-14 13:33:03 -0500551 { "description", required_argument, NULL, 'D' },
Daniel P. Berrangeaa6e5462016-02-17 10:10:22 +0000552 { "tls-creds", required_argument, NULL, QEMU_NBD_OPT_TLSCREDS },
Daniel P. Berrangeb25e12d2019-02-27 16:20:33 +0000553 { "tls-authz", required_argument, NULL, QEMU_NBD_OPT_TLSAUTHZ },
Daniel P. Berrangeaa6e5462016-02-17 10:10:22 +0000554 { "image-opts", no_argument, NULL, QEMU_NBD_OPT_IMAGE_OPTS },
Denis V. Lunev39ca4632016-06-17 17:44:12 +0300555 { "trace", required_argument, NULL, 'T' },
Max Reitzffb31e12016-09-28 22:46:42 +0200556 { "fork", no_argument, NULL, QEMU_NBD_OPT_FORK },
Max Reitz637bc5a2019-05-08 23:18:16 +0200557 { "pid-file", required_argument, NULL, QEMU_NBD_OPT_PID_FILE },
Blue Swirl660f11b2009-07-31 21:16:51 +0000558 { NULL, 0, NULL, 0 }
bellard7a5ca862008-05-27 21:13:40 +0000559 };
560 int ch;
561 int opt_ind = 0;
Naphtali Spreif5edb012010-01-17 16:48:13 +0200562 int flags = BDRV_O_RDWR;
Max Reitz4fbec262015-02-05 13:58:19 -0500563 int ret = 0;
Paolo Bonzini39a52352012-07-18 14:57:15 +0200564 bool seen_cache = false;
Paolo Bonzinided9d2d2013-02-08 14:06:13 +0100565 bool seen_discard = false;
Paolo Bonzini39a52352012-07-18 14:57:15 +0200566 bool seen_aio = false;
Paolo Bonzinia517e882011-11-04 15:51:21 +0100567 pthread_t client_thread;
Daniel P. Berrangee6b63672013-03-19 11:20:20 +0000568 const char *fmt = NULL;
Max Reitz34b5d2c2013-09-05 14:45:29 +0200569 Error *local_err = NULL;
Peter Lievenb3838a42014-08-13 19:20:18 +0200570 BlockdevDetectZeroesOptions detect_zeroes = BLOCKDEV_DETECT_ZEROES_OPTIONS_OFF;
Max Reitz4fbec262015-02-05 13:58:19 -0500571 QDict *options = NULL;
Eric Blake68b96f12019-01-17 13:36:56 -0600572 const char *export_name = NULL; /* defaults to "" later for server mode */
Eric Blakeb1a75b32016-10-14 13:33:03 -0500573 const char *export_description = NULL;
Eric Blakecbad81c2020-10-27 00:05:49 -0500574 strList *bitmaps = NULL;
Eric Blakedbc7b012020-10-27 00:05:55 -0500575 bool alloc_depth = false;
Daniel P. Berrange145614a2016-02-10 18:41:13 +0000576 const char *tlscredsid = NULL;
Daniel P. Berrange77c9aae2016-02-17 10:10:19 +0000577 bool imageOpts = false;
Kevin Wolf6effd5b2016-03-14 11:43:28 +0100578 bool writethrough = true;
Max Reitzffb31e12016-09-28 22:46:42 +0200579 bool fork_process = false;
Eric Blake68b96f12019-01-17 13:36:56 -0600580 bool list = false;
Max Reitzffb31e12016-09-28 22:46:42 +0200581 int old_stderr = -1;
Richard W.M. Jonesa721f532017-02-04 10:03:17 +0000582 unsigned socket_activation;
Max Reitz637bc5a2019-05-08 23:18:16 +0200583 const char *pid_file_name = NULL;
Kevin Wolf00917172020-09-24 17:26:57 +0200584 BlockExportOptions *export_opts;
bellard7a5ca862008-05-27 21:13:40 +0000585
Eric Blake029a88c2020-09-30 07:11:01 -0500586#ifdef CONFIG_POSIX
Stefan Hajnoczicbc20bf2020-09-29 13:55:15 +0100587 os_setup_early_signal_handling();
588 os_setup_signal_handling();
Max Reitz041e32b2017-06-11 14:37:14 +0200589#endif
590
Daniel P. Berrangé98c5d2e2020-08-25 11:38:48 +0100591 socket_init();
Christophe Fergeauf5852ef2019-01-31 17:46:14 +0100592 error_init(argv[0]);
Daniel P. Berrangefe4db842016-10-04 14:35:52 +0100593 module_call_init(MODULE_INIT_TRACE);
Eduardo Habkoste8f2d272016-05-12 11:10:04 -0300594 qcrypto_init(&error_fatal);
Daniel P. Berrangec2297082016-04-06 12:12:06 +0100595
Daniel P. Berrange0ab3b332016-02-10 18:41:00 +0000596 module_call_init(MODULE_INIT_QOM);
597 qemu_add_opts(&qemu_object_opts);
Denis V. Lunev39ca4632016-06-17 17:44:12 +0300598 qemu_add_opts(&qemu_trace_opts);
Fam Zheng10f5bff2014-02-10 14:48:51 +0800599 qemu_init_exec_dir(argv[0]);
Paolo Bonzinibb345112011-11-04 15:51:19 +0100600
bellard7a5ca862008-05-27 21:13:40 +0000601 while ((ch = getopt_long(argc, argv, sopt, lopt, &opt_ind)) != -1) {
602 switch (ch) {
603 case 's':
ths2f726482008-07-03 11:47:46 +0000604 flags |= BDRV_O_SNAPSHOT;
605 break;
606 case 'n':
Paolo Bonzini39a52352012-07-18 14:57:15 +0200607 optarg = (char *) "none";
608 /* fallthrough */
609 case QEMU_NBD_OPT_CACHE:
610 if (seen_cache) {
Markus Armbruster85b01e02015-12-18 16:35:04 +0100611 error_report("-n and --cache can only be specified once");
612 exit(EXIT_FAILURE);
Paolo Bonzini39a52352012-07-18 14:57:15 +0200613 }
614 seen_cache = true;
Kevin Wolf6effd5b2016-03-14 11:43:28 +0100615 if (bdrv_parse_cache_mode(optarg, &flags, &writethrough) == -1) {
Markus Armbruster85b01e02015-12-18 16:35:04 +0100616 error_report("Invalid cache mode `%s'", optarg);
617 exit(EXIT_FAILURE);
Paolo Bonzini39a52352012-07-18 14:57:15 +0200618 }
bellard7a5ca862008-05-27 21:13:40 +0000619 break;
Paolo Bonzini39a52352012-07-18 14:57:15 +0200620 case QEMU_NBD_OPT_AIO:
621 if (seen_aio) {
Markus Armbruster85b01e02015-12-18 16:35:04 +0100622 error_report("--aio can only be specified once");
623 exit(EXIT_FAILURE);
Paolo Bonzini39a52352012-07-18 14:57:15 +0200624 }
625 seen_aio = true;
Aarushi Mehta76802742020-01-20 14:18:56 +0000626 if (bdrv_parse_aio(optarg, &flags) < 0) {
627 error_report("Invalid aio mode '%s'", optarg);
628 exit(EXIT_FAILURE);
Paolo Bonzini39a52352012-07-18 14:57:15 +0200629 }
630 break;
Paolo Bonzinided9d2d2013-02-08 14:06:13 +0100631 case QEMU_NBD_OPT_DISCARD:
632 if (seen_discard) {
Markus Armbruster85b01e02015-12-18 16:35:04 +0100633 error_report("--discard can only be specified once");
634 exit(EXIT_FAILURE);
Paolo Bonzinided9d2d2013-02-08 14:06:13 +0100635 }
636 seen_discard = true;
637 if (bdrv_parse_discard_flags(optarg, &flags) == -1) {
Markus Armbruster85b01e02015-12-18 16:35:04 +0100638 error_report("Invalid discard mode `%s'", optarg);
639 exit(EXIT_FAILURE);
Paolo Bonzinided9d2d2013-02-08 14:06:13 +0100640 }
641 break;
Peter Lievenb3838a42014-08-13 19:20:18 +0200642 case QEMU_NBD_OPT_DETECT_ZEROES:
643 detect_zeroes =
Marc-André Lureauf7abe0e2017-08-24 10:46:10 +0200644 qapi_enum_parse(&BlockdevDetectZeroesOptions_lookup,
Peter Lievenb3838a42014-08-13 19:20:18 +0200645 optarg,
Peter Lievenb3838a42014-08-13 19:20:18 +0200646 BLOCKDEV_DETECT_ZEROES_OPTIONS_OFF,
647 &local_err);
648 if (local_err) {
Markus Armbrusterc29b77f2015-12-18 16:35:14 +0100649 error_reportf_err(local_err,
650 "Failed to parse detect_zeroes mode: ");
Markus Armbruster85b01e02015-12-18 16:35:04 +0100651 exit(EXIT_FAILURE);
Peter Lievenb3838a42014-08-13 19:20:18 +0200652 }
653 if (detect_zeroes == BLOCKDEV_DETECT_ZEROES_OPTIONS_UNMAP &&
654 !(flags & BDRV_O_UNMAP)) {
Markus Armbruster85b01e02015-12-18 16:35:04 +0100655 error_report("setting detect-zeroes to unmap is not allowed "
656 "without setting discard operation to unmap");
657 exit(EXIT_FAILURE);
Peter Lievenb3838a42014-08-13 19:20:18 +0200658 }
659 break;
bellard7a5ca862008-05-27 21:13:40 +0000660 case 'b':
661 bindto = optarg;
662 break;
663 case 'p':
Daniel P. Berrange48bec072015-09-16 14:52:23 +0100664 port = optarg;
bellard7a5ca862008-05-27 21:13:40 +0000665 break;
666 case 'o':
Eric Blake43b51012019-01-17 13:36:44 -0600667 if (qemu_strtou64(optarg, NULL, 0, &dev_offset) < 0) {
668 error_report("Invalid offset '%s'", optarg);
Markus Armbruster85b01e02015-12-18 16:35:04 +0100669 exit(EXIT_FAILURE);
bellard7a5ca862008-05-27 21:13:40 +0000670 }
bellard7a5ca862008-05-27 21:13:40 +0000671 break;
Wenchao Xia8c116b02013-12-04 17:10:55 +0800672 case 'l':
673 if (strstart(optarg, SNAPSHOT_OPT_BASE, NULL)) {
Markus Armbruster70b94332015-02-13 12:50:26 +0100674 sn_opts = qemu_opts_parse_noisily(&internal_snapshot_opts,
675 optarg, false);
Wenchao Xia8c116b02013-12-04 17:10:55 +0800676 if (!sn_opts) {
Markus Armbruster85b01e02015-12-18 16:35:04 +0100677 error_report("Failed in parsing snapshot param `%s'",
678 optarg);
679 exit(EXIT_FAILURE);
Wenchao Xia8c116b02013-12-04 17:10:55 +0800680 }
681 } else {
682 sn_id_or_name = optarg;
683 }
684 /* fall through */
bellard7a5ca862008-05-27 21:13:40 +0000685 case 'r':
Eric Blakedbb38ca2019-08-23 09:37:22 -0500686 readonly = true;
Naphtali Sprei07108b22010-03-14 15:19:57 +0200687 flags &= ~BDRV_O_RDWR;
bellard7a5ca862008-05-27 21:13:40 +0000688 break;
Eric Blakedbc7b012020-10-27 00:05:55 -0500689 case 'A':
690 alloc_depth = true;
691 break;
Eric Blake636192c2019-01-11 13:47:20 -0600692 case 'B':
Eric Blakecbad81c2020-10-27 00:05:49 -0500693 QAPI_LIST_PREPEND(bitmaps, g_strdup(optarg));
Eric Blake636192c2019-01-11 13:47:20 -0600694 break;
thscd831bd2008-07-03 10:23:51 +0000695 case 'k':
Paolo Bonzinib32f6c22011-11-04 15:51:20 +0100696 sockpath = optarg;
Peter Lieven713cc672014-08-13 19:20:19 +0200697 if (sockpath[0] != '/') {
Markus Armbruster9af9e0f2015-12-18 16:35:19 +0100698 error_report("socket path must be absolute");
Markus Armbruster85b01e02015-12-18 16:35:04 +0100699 exit(EXIT_FAILURE);
Peter Lieven713cc672014-08-13 19:20:19 +0200700 }
thscd831bd2008-07-03 10:23:51 +0000701 break;
702 case 'd':
703 disconnect = true;
704 break;
705 case 'c':
706 device = optarg;
707 break;
ths3b05a8e2008-07-03 12:45:02 +0000708 case 'e':
Eric Blake43b51012019-01-17 13:36:44 -0600709 if (qemu_strtoi(optarg, NULL, 0, &shared) < 0 ||
710 shared < 1) {
Markus Armbruster85b01e02015-12-18 16:35:04 +0100711 error_report("Invalid shared device number '%s'", optarg);
712 exit(EXIT_FAILURE);
ths3b05a8e2008-07-03 12:45:02 +0000713 }
ths3b05a8e2008-07-03 12:45:02 +0000714 break;
Daniel P. Berrangee6b63672013-03-19 11:20:20 +0000715 case 'f':
716 fmt = optarg;
717 break;
Peter Lieven713cc672014-08-13 19:20:19 +0200718 case 't':
719 persistent = 1;
720 break;
Daniel P. Berrange3d4b2f92016-02-10 18:41:08 +0000721 case 'x':
722 export_name = optarg;
Eric Blake93676c82019-11-13 20:46:34 -0600723 if (strlen(export_name) > NBD_MAX_STRING_SIZE) {
724 error_report("export name '%s' too long", export_name);
725 exit(EXIT_FAILURE);
726 }
Daniel P. Berrange3d4b2f92016-02-10 18:41:08 +0000727 break;
Eric Blakeb1a75b32016-10-14 13:33:03 -0500728 case 'D':
729 export_description = optarg;
Eric Blake93676c82019-11-13 20:46:34 -0600730 if (strlen(export_description) > NBD_MAX_STRING_SIZE) {
731 error_report("export description '%s' too long",
732 export_description);
733 exit(EXIT_FAILURE);
734 }
Eric Blakeb1a75b32016-10-14 13:33:03 -0500735 break;
bellard7a5ca862008-05-27 21:13:40 +0000736 case 'v':
737 verbose = 1;
738 break;
739 case 'V':
740 version(argv[0]);
741 exit(0);
742 break;
743 case 'h':
744 usage(argv[0]);
745 exit(0);
746 break;
747 case '?':
Markus Armbruster85b01e02015-12-18 16:35:04 +0100748 error_report("Try `%s --help' for more information.", argv[0]);
749 exit(EXIT_FAILURE);
Daniel P. Berrange0ab3b332016-02-10 18:41:00 +0000750 case QEMU_NBD_OPT_OBJECT: {
751 QemuOpts *opts;
752 opts = qemu_opts_parse_noisily(&qemu_object_opts,
753 optarg, true);
754 if (!opts) {
755 exit(EXIT_FAILURE);
756 }
757 } break;
Daniel P. Berrange145614a2016-02-10 18:41:13 +0000758 case QEMU_NBD_OPT_TLSCREDS:
759 tlscredsid = optarg;
760 break;
Daniel P. Berrange77c9aae2016-02-17 10:10:19 +0000761 case QEMU_NBD_OPT_IMAGE_OPTS:
762 imageOpts = true;
763 break;
Denis V. Lunev39ca4632016-06-17 17:44:12 +0300764 case 'T':
Paolo Bonzini92eecff2020-11-02 06:58:41 -0500765 trace_opt_parse(optarg);
Denis V. Lunev39ca4632016-06-17 17:44:12 +0300766 break;
Daniel P. Berrangeb25e12d2019-02-27 16:20:33 +0000767 case QEMU_NBD_OPT_TLSAUTHZ:
768 tlsauthz = optarg;
769 break;
Max Reitzffb31e12016-09-28 22:46:42 +0200770 case QEMU_NBD_OPT_FORK:
771 fork_process = true;
772 break;
Eric Blake68b96f12019-01-17 13:36:56 -0600773 case 'L':
774 list = true;
775 break;
Max Reitz637bc5a2019-05-08 23:18:16 +0200776 case QEMU_NBD_OPT_PID_FILE:
777 pid_file_name = optarg;
778 break;
bellard7a5ca862008-05-27 21:13:40 +0000779 }
780 }
781
Eric Blake68b96f12019-01-17 13:36:56 -0600782 if (list) {
783 if (argc != optind) {
784 error_report("List mode is incompatible with a file name");
785 exit(EXIT_FAILURE);
786 }
Eric Blake0bc16992020-01-23 10:46:50 -0600787 if (export_name || export_description || dev_offset ||
Eric Blakecbad81c2020-10-27 00:05:49 -0500788 device || disconnect || fmt || sn_id_or_name || bitmaps ||
Eric Blakedbc7b012020-10-27 00:05:55 -0500789 alloc_depth || seen_aio || seen_discard || seen_cache) {
Eric Blake68b96f12019-01-17 13:36:56 -0600790 error_report("List mode is incompatible with per-device settings");
791 exit(EXIT_FAILURE);
792 }
793 if (fork_process) {
794 error_report("List mode is incompatible with forking");
795 exit(EXIT_FAILURE);
796 }
797 } else if ((argc - optind) != 1) {
Markus Armbruster433672b2015-12-18 16:35:24 +0100798 error_report("Invalid number of arguments");
799 error_printf("Try `%s --help' for more information.\n", argv[0]);
Markus Armbruster85b01e02015-12-18 16:35:04 +0100800 exit(EXIT_FAILURE);
Eric Blake68b96f12019-01-17 13:36:56 -0600801 } else if (!export_name) {
802 export_name = "";
bellard7a5ca862008-05-27 21:13:40 +0000803 }
804
Markus Armbruster7e1e0c12018-10-17 10:26:43 +0200805 qemu_opts_foreach(&qemu_object_opts,
806 user_creatable_add_opts_foreach,
Kevin Wolf495bf892019-10-11 21:49:17 +0200807 qemu_nbd_object_print_help, &error_fatal);
Daniel P. Berrange0ab3b332016-02-10 18:41:00 +0000808
Denis V. Lunev39ca4632016-06-17 17:44:12 +0300809 if (!trace_init_backends()) {
810 exit(1);
811 }
Paolo Bonzini92eecff2020-11-02 06:58:41 -0500812 trace_init_file();
Denis V. Lunev39ca4632016-06-17 17:44:12 +0300813 qemu_set_log(LOG_TRACE);
814
Richard W.M. Jonesa721f532017-02-04 10:03:17 +0000815 socket_activation = check_socket_activation();
816 if (socket_activation == 0) {
817 setup_address_and_port(&bindto, &port);
818 } else {
819 /* Using socket activation - check user didn't use -p etc. */
820 const char *err_msg = socket_activation_validate_opts(device, sockpath,
Eric Blake68b96f12019-01-17 13:36:56 -0600821 bindto, port,
822 list);
Richard W.M. Jonesa721f532017-02-04 10:03:17 +0000823 if (err_msg != NULL) {
824 error_report("%s", err_msg);
825 exit(EXIT_FAILURE);
826 }
Paolo Bonzini53fabd42017-03-16 16:29:45 +0100827
828 /* qemu-nbd can only listen on a single socket. */
829 if (socket_activation > 1) {
830 error_report("qemu-nbd does not support socket activation with %s > 1",
831 "LISTEN_FDS");
832 exit(EXIT_FAILURE);
833 }
Richard W.M. Jonesa721f532017-02-04 10:03:17 +0000834 }
835
Daniel P. Berrange145614a2016-02-10 18:41:13 +0000836 if (tlscredsid) {
837 if (sockpath) {
838 error_report("TLS is only supported with IPv4/IPv6");
839 exit(EXIT_FAILURE);
840 }
841 if (device) {
842 error_report("TLS is not supported with a host device");
843 exit(EXIT_FAILURE);
844 }
Daniel P. Berrangeb25e12d2019-02-27 16:20:33 +0000845 if (tlsauthz && list) {
846 error_report("TLS authorization is incompatible with export list");
847 exit(EXIT_FAILURE);
848 }
Eric Blake68b96f12019-01-17 13:36:56 -0600849 tlscreds = nbd_get_tls_creds(tlscredsid, list, &local_err);
Daniel P. Berrange145614a2016-02-10 18:41:13 +0000850 if (local_err) {
Markus Armbruster5217f182020-05-05 12:19:03 +0200851 error_reportf_err(local_err, "Failed to get TLS creds: ");
Daniel P. Berrange145614a2016-02-10 18:41:13 +0000852 exit(EXIT_FAILURE);
853 }
Daniel P. Berrangeb25e12d2019-02-27 16:20:33 +0000854 } else {
855 if (tlsauthz) {
856 error_report("--tls-authz is not permitted without --tls-creds");
857 exit(EXIT_FAILURE);
858 }
Daniel P. Berrange145614a2016-02-10 18:41:13 +0000859 }
860
Eric Blake68b96f12019-01-17 13:36:56 -0600861 if (list) {
862 saddr = nbd_build_socket_address(sockpath, bindto, port);
863 return qemu_nbd_client_list(saddr, tlscreds, bindto);
864 }
865
Eric Blake3c1fa352018-12-15 07:53:08 -0600866#if !HAVE_NBD_DEVICE
867 if (disconnect || device) {
868 error_report("Kernel /dev/nbdN support not available");
869 exit(EXIT_FAILURE);
870 }
871#else /* HAVE_NBD_DEVICE */
thscd831bd2008-07-03 10:23:51 +0000872 if (disconnect) {
Daniel P. Berranged0d6ff52016-02-10 18:41:02 +0000873 int nbdfd = open(argv[optind], O_RDWR);
874 if (nbdfd < 0) {
Markus Armbruster85b01e02015-12-18 16:35:04 +0100875 error_report("Cannot open %s: %s", argv[optind],
876 strerror(errno));
877 exit(EXIT_FAILURE);
Paolo Bonzinifc19f8a2012-03-07 11:05:34 +0100878 }
Daniel P. Berranged0d6ff52016-02-10 18:41:02 +0000879 nbd_disconnect(nbdfd);
thscd831bd2008-07-03 10:23:51 +0000880
Daniel P. Berranged0d6ff52016-02-10 18:41:02 +0000881 close(nbdfd);
thscd831bd2008-07-03 10:23:51 +0000882
883 printf("%s disconnected\n", argv[optind]);
884
Peter Lieven713cc672014-08-13 19:20:19 +0200885 return 0;
thscd831bd2008-07-03 10:23:51 +0000886 }
Eric Blake3c1fa352018-12-15 07:53:08 -0600887#endif
thscd831bd2008-07-03 10:23:51 +0000888
Max Reitzffb31e12016-09-28 22:46:42 +0200889 if ((device && !verbose) || fork_process) {
Daniel P. Berrangéeb705982020-08-25 11:38:50 +0100890#ifndef WIN32
Paolo Bonzinic1f8fdc2011-11-04 15:51:22 +0100891 int stderr_fd[2];
892 pid_t pid;
893 int ret;
894
Paolo Bonzinifc19f8a2012-03-07 11:05:34 +0100895 if (qemu_pipe(stderr_fd) < 0) {
Markus Armbruster85b01e02015-12-18 16:35:04 +0100896 error_report("Error setting up communication pipe: %s",
897 strerror(errno));
898 exit(EXIT_FAILURE);
Paolo Bonzinic1f8fdc2011-11-04 15:51:22 +0100899 }
900
901 /* Now daemonize, but keep a communication channel open to
902 * print errors and exit with the proper status code.
903 */
904 pid = fork();
Max Reitz70d47392015-02-25 13:08:22 -0500905 if (pid < 0) {
Markus Armbruster85b01e02015-12-18 16:35:04 +0100906 error_report("Failed to fork: %s", strerror(errno));
907 exit(EXIT_FAILURE);
Max Reitz70d47392015-02-25 13:08:22 -0500908 } else if (pid == 0) {
Paolo Bonzinic1f8fdc2011-11-04 15:51:22 +0100909 close(stderr_fd[0]);
Max Reitze6df58a2019-05-08 23:18:18 +0200910
Raphael Pour0eaf4532020-05-15 08:36:07 +0200911 /* Remember parent's stderr if we will be restoring it. */
912 if (fork_process) {
913 old_stderr = dup(STDERR_FILENO);
914 }
915
Michael Tokarev9faf31b2012-01-16 18:37:44 +0400916 ret = qemu_daemon(1, 0);
Paolo Bonzinic1f8fdc2011-11-04 15:51:22 +0100917
918 /* Temporarily redirect stderr to the parent's pipe... */
919 dup2(stderr_fd[1], STDERR_FILENO);
Paolo Bonzinifc19f8a2012-03-07 11:05:34 +0100920 if (ret < 0) {
Markus Armbruster85b01e02015-12-18 16:35:04 +0100921 error_report("Failed to daemonize: %s", strerror(errno));
922 exit(EXIT_FAILURE);
Paolo Bonzinic1f8fdc2011-11-04 15:51:22 +0100923 }
924
925 /* ... close the descriptor we inherited and go on. */
926 close(stderr_fd[1]);
927 } else {
928 bool errors = false;
929 char *buf;
930
931 /* In the parent. Print error messages from the child until
932 * it closes the pipe.
933 */
934 close(stderr_fd[1]);
935 buf = g_malloc(1024);
936 while ((ret = read(stderr_fd[0], buf, 1024)) > 0) {
937 errors = true;
938 ret = qemu_write_full(STDERR_FILENO, buf, ret);
Paolo Bonzinifc19f8a2012-03-07 11:05:34 +0100939 if (ret < 0) {
Paolo Bonzinic1f8fdc2011-11-04 15:51:22 +0100940 exit(EXIT_FAILURE);
941 }
942 }
Paolo Bonzinifc19f8a2012-03-07 11:05:34 +0100943 if (ret < 0) {
Markus Armbruster85b01e02015-12-18 16:35:04 +0100944 error_report("Cannot read from daemon: %s",
945 strerror(errno));
946 exit(EXIT_FAILURE);
Paolo Bonzinic1f8fdc2011-11-04 15:51:22 +0100947 }
948
949 /* Usually the daemon should not print any message.
950 * Exit with zero status in that case.
951 */
952 exit(errors);
953 }
Daniel P. Berrangéeb705982020-08-25 11:38:50 +0100954#else /* WIN32 */
955 error_report("Unable to fork into background on Windows hosts");
956 exit(EXIT_FAILURE);
957#endif /* WIN32 */
Paolo Bonzinic1f8fdc2011-11-04 15:51:22 +0100958 }
959
Paolo Bonzinia6ac2312011-12-06 09:07:00 +0100960 if (device != NULL && sockpath == NULL) {
961 sockpath = g_malloc(128);
962 snprintf(sockpath, 128, SOCKET_PATH, basename(device));
thscd831bd2008-07-03 10:23:51 +0000963 }
964
Daniel P. Berrangee4849c12017-12-18 10:16:43 +0000965 server = qio_net_listener_new();
Richard W.M. Jonesa721f532017-02-04 10:03:17 +0000966 if (socket_activation == 0) {
Richard W.M. Jonesa721f532017-02-04 10:03:17 +0000967 saddr = nbd_build_socket_address(sockpath, bindto, port);
Juan Quintelafc8135c2019-08-19 18:08:21 +0200968 if (qio_net_listener_open_sync(server, saddr, 1, &local_err) < 0) {
Daniel P. Berrangee4849c12017-12-18 10:16:43 +0000969 object_unref(OBJECT(server));
Richard W.M. Jonesa721f532017-02-04 10:03:17 +0000970 error_report_err(local_err);
Daniel P. Berrangee4849c12017-12-18 10:16:43 +0000971 exit(EXIT_FAILURE);
Richard W.M. Jonesa721f532017-02-04 10:03:17 +0000972 }
973 } else {
Daniel P. Berrangee4849c12017-12-18 10:16:43 +0000974 size_t i;
Richard W.M. Jonesa721f532017-02-04 10:03:17 +0000975 /* See comment in check_socket_activation above. */
Daniel P. Berrangee4849c12017-12-18 10:16:43 +0000976 for (i = 0; i < socket_activation; i++) {
977 QIOChannelSocket *sioc;
978 sioc = qio_channel_socket_new_fd(FIRST_SOCKET_ACTIVATION_FD + i,
979 &local_err);
980 if (sioc == NULL) {
981 object_unref(OBJECT(server));
Markus Armbruster5217f182020-05-05 12:19:03 +0200982 error_reportf_err(local_err,
983 "Failed to use socket activation: ");
Daniel P. Berrangee4849c12017-12-18 10:16:43 +0000984 exit(EXIT_FAILURE);
985 }
986 qio_net_listener_add(server, sioc);
987 object_unref(OBJECT(sioc));
Richard W.M. Jonesa721f532017-02-04 10:03:17 +0000988 }
989 }
Daniel P. Berrange48bec072015-09-16 14:52:23 +0100990
Chrysostomos Nanakos2f78e492014-09-18 14:30:49 +0300991 if (qemu_init_main_loop(&local_err)) {
Markus Armbruster565f65d2015-02-12 13:55:05 +0100992 error_report_err(local_err);
Chrysostomos Nanakos2f78e492014-09-18 14:30:49 +0300993 exit(EXIT_FAILURE);
994 }
Paolo Bonzini802ddc32011-11-04 15:51:24 +0100995 bdrv_init();
Kevin Wolfb3b52992018-05-16 13:46:37 +0200996 atexit(qemu_nbd_shutdown);
Paolo Bonzini802ddc32011-11-04 15:51:24 +0100997
Daniel P. Berrange77c9aae2016-02-17 10:10:19 +0000998 srcpath = argv[optind];
999 if (imageOpts) {
1000 QemuOpts *opts;
1001 if (fmt) {
1002 error_report("--image-opts and -f are mutually exclusive");
1003 exit(EXIT_FAILURE);
1004 }
1005 opts = qemu_opts_parse_noisily(&file_opts, srcpath, true);
1006 if (!opts) {
1007 qemu_opts_reset(&file_opts);
1008 exit(EXIT_FAILURE);
1009 }
1010 options = qemu_opts_to_qdict(opts, NULL);
1011 qemu_opts_reset(&file_opts);
Max Reitzefaa7c42016-03-16 19:54:38 +01001012 blk = blk_new_open(NULL, NULL, options, flags, &local_err);
Daniel P. Berrange77c9aae2016-02-17 10:10:19 +00001013 } else {
1014 if (fmt) {
1015 options = qdict_new();
Eric Blake46f5ac22017-04-27 16:58:17 -05001016 qdict_put_str(options, "driver", fmt);
Daniel P. Berrange77c9aae2016-02-17 10:10:19 +00001017 }
Max Reitzefaa7c42016-03-16 19:54:38 +01001018 blk = blk_new_open(srcpath, NULL, options, flags, &local_err);
Daniel P. Berrangee6b63672013-03-19 11:20:20 +00001019 }
1020
Max Reitz4fbec262015-02-05 13:58:19 -05001021 if (!blk) {
Markus Armbrusterc29b77f2015-12-18 16:35:14 +01001022 error_reportf_err(local_err, "Failed to blk_new_open '%s': ",
1023 argv[optind]);
Markus Armbruster85b01e02015-12-18 16:35:04 +01001024 exit(EXIT_FAILURE);
Paolo Bonzini802ddc32011-11-04 15:51:24 +01001025 }
Max Reitz4fbec262015-02-05 13:58:19 -05001026 bs = blk_bs(blk);
Paolo Bonzini802ddc32011-11-04 15:51:24 +01001027
Kevin Wolfb57e4de2020-09-24 17:26:52 +02001028 if (dev_offset) {
1029 QDict *raw_opts = qdict_new();
1030 qdict_put_str(raw_opts, "driver", "raw");
1031 qdict_put_str(raw_opts, "file", bs->node_name);
1032 qdict_put_int(raw_opts, "offset", dev_offset);
1033 bs = bdrv_open(NULL, NULL, raw_opts, flags, &error_fatal);
1034 blk_remove_bs(blk);
1035 blk_insert_bs(blk, bs, &error_fatal);
1036 bdrv_unref(bs);
1037 }
1038
Kevin Wolf6effd5b2016-03-14 11:43:28 +01001039 blk_set_enable_write_cache(blk, !writethrough);
1040
Wenchao Xia8c116b02013-12-04 17:10:55 +08001041 if (sn_opts) {
1042 ret = bdrv_snapshot_load_tmp(bs,
1043 qemu_opt_get(sn_opts, SNAPSHOT_OPT_ID),
1044 qemu_opt_get(sn_opts, SNAPSHOT_OPT_NAME),
1045 &local_err);
1046 } else if (sn_id_or_name) {
1047 ret = bdrv_snapshot_load_tmp_by_id_or_name(bs, sn_id_or_name,
1048 &local_err);
1049 }
1050 if (ret < 0) {
Markus Armbrusterc29b77f2015-12-18 16:35:14 +01001051 error_reportf_err(local_err, "Failed to load snapshot: ");
Markus Armbruster85b01e02015-12-18 16:35:04 +01001052 exit(EXIT_FAILURE);
Wenchao Xia8c116b02013-12-04 17:10:55 +08001053 }
1054
Peter Lievenb3838a42014-08-13 19:20:18 +02001055 bs->detect_zeroes = detect_zeroes;
Paolo Bonzini802ddc32011-11-04 15:51:24 +01001056
Kevin Wolf00917172020-09-24 17:26:57 +02001057 nbd_server_is_qemu_nbd(true);
1058
1059 export_opts = g_new(BlockExportOptions, 1);
1060 *export_opts = (BlockExportOptions) {
1061 .type = BLOCK_EXPORT_TYPE_NBD,
Kevin Wolfd53be9c2020-09-24 17:27:04 +02001062 .id = g_strdup("qemu-nbd-export"),
Kevin Wolfb6076af2020-09-24 17:27:01 +02001063 .node_name = g_strdup(bdrv_get_node_name(bs)),
Kevin Wolf00917172020-09-24 17:26:57 +02001064 .has_writethrough = true,
1065 .writethrough = writethrough,
Kevin Wolf30dbc812020-09-24 17:27:11 +02001066 .has_writable = true,
1067 .writable = !readonly,
Kevin Wolf00917172020-09-24 17:26:57 +02001068 .u.nbd = {
Eric Blakecbad81c2020-10-27 00:05:49 -05001069 .has_name = true,
1070 .name = g_strdup(export_name),
1071 .has_description = !!export_description,
1072 .description = g_strdup(export_description),
1073 .has_bitmaps = !!bitmaps,
1074 .bitmaps = bitmaps,
Eric Blakedbc7b012020-10-27 00:05:55 -05001075 .has_allocation_depth = alloc_depth,
1076 .allocation_depth = alloc_depth,
Kevin Wolf00917172020-09-24 17:26:57 +02001077 },
1078 };
1079 blk_exp_add(export_opts, &error_fatal);
1080 qapi_free_BlockExportOptions(export_opts);
ths3b05a8e2008-07-03 12:45:02 +00001081
Paolo Bonzinif1ef5552011-11-04 15:51:23 +01001082 if (device) {
Eric Blake3c1fa352018-12-15 07:53:08 -06001083#if HAVE_NBD_DEVICE
Paolo Bonzinif1ef5552011-11-04 15:51:23 +01001084 int ret;
1085
Paolo Bonzinia6ac2312011-12-06 09:07:00 +01001086 ret = pthread_create(&client_thread, NULL, nbd_client_thread, device);
Paolo Bonzinif1ef5552011-11-04 15:51:23 +01001087 if (ret != 0) {
Markus Armbruster85b01e02015-12-18 16:35:04 +01001088 error_report("Failed to create client thread: %s", strerror(ret));
1089 exit(EXIT_FAILURE);
Paolo Bonzinif1ef5552011-11-04 15:51:23 +01001090 }
Eric Blake3c1fa352018-12-15 07:53:08 -06001091#endif
Paolo Bonzinif1ef5552011-11-04 15:51:23 +01001092 } else {
1093 /* Shut up GCC warnings. */
1094 memset(&client_thread, 0, sizeof(client_thread));
1095 }
1096
Daniel P. Berranged0d6ff52016-02-10 18:41:02 +00001097 nbd_update_server_watch();
bellard7a5ca862008-05-27 21:13:40 +00001098
Max Reitz637bc5a2019-05-08 23:18:16 +02001099 if (pid_file_name) {
1100 qemu_write_pidfile(pid_file_name, &error_fatal);
1101 }
1102
Michael Tokarev9faf31b2012-01-16 18:37:44 +04001103 /* now when the initialization is (almost) complete, chdir("/")
1104 * to free any busy filesystems */
1105 if (chdir("/") < 0) {
Markus Armbruster85b01e02015-12-18 16:35:04 +01001106 error_report("Could not chdir to root directory: %s",
1107 strerror(errno));
1108 exit(EXIT_FAILURE);
Michael Tokarev9faf31b2012-01-16 18:37:44 +04001109 }
1110
Max Reitzffb31e12016-09-28 22:46:42 +02001111 if (fork_process) {
1112 dup2(old_stderr, STDERR_FILENO);
1113 close(old_stderr);
1114 }
1115
Paolo Bonzini7860a382012-09-18 13:31:56 +02001116 state = RUNNING;
ths3b05a8e2008-07-03 12:45:02 +00001117 do {
Paolo Bonzinia61c6782011-09-12 17:28:11 +02001118 main_loop_wait(false);
Paolo Bonzini7860a382012-09-18 13:31:56 +02001119 if (state == TERMINATE) {
Kevin Wolfbc4ee652020-09-24 17:27:03 +02001120 blk_exp_close_all();
Kevin Wolfd794f7f2020-09-24 17:26:56 +02001121 state = TERMINATED;
Paolo Bonzini7860a382012-09-18 13:31:56 +02001122 }
1123 } while (state != TERMINATED);
ths3b05a8e2008-07-03 12:45:02 +00001124
Markus Armbruster26f54e92014-10-07 13:59:04 +02001125 blk_unref(blk);
Paolo Bonzinib32f6c22011-11-04 15:51:20 +01001126 if (sockpath) {
1127 unlink(sockpath);
1128 }
bellard7a5ca862008-05-27 21:13:40 +00001129
Markus Armbrusterfbf28a42014-09-29 16:07:55 +02001130 qemu_opts_del(sn_opts);
Wenchao Xia8c116b02013-12-04 17:10:55 +08001131
Paolo Bonzinia517e882011-11-04 15:51:21 +01001132 if (device) {
1133 void *ret;
1134 pthread_join(client_thread, &ret);
1135 exit(ret != NULL);
1136 } else {
1137 exit(EXIT_SUCCESS);
1138 }
bellard7a5ca862008-05-27 21:13:40 +00001139}