blob: 347c776ab99b2dfe93bdc9df6c3524bd57952001 [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
Stefan Weil5a61cb62011-09-08 17:55:32 +020019#include "qemu-common.h"
bellard7a5ca862008-05-27 21:13:40 +000020#include "block_int.h"
21#include "nbd.h"
22
bellard7a5ca862008-05-27 21:13:40 +000023#include <stdarg.h>
24#include <stdio.h>
25#include <getopt.h>
26#include <err.h>
thscd831bd2008-07-03 10:23:51 +000027#include <sys/types.h>
bellard7a5ca862008-05-27 21:13:40 +000028#include <sys/socket.h>
29#include <netinet/in.h>
30#include <netinet/tcp.h>
31#include <arpa/inet.h>
thscd831bd2008-07-03 10:23:51 +000032#include <signal.h>
Blue Swirl2bff4b62009-12-23 15:34:04 +000033#include <libgen.h>
Paolo Bonzinia517e882011-11-04 15:51:21 +010034#include <pthread.h>
thscd831bd2008-07-03 10:23:51 +000035
36#define SOCKET_PATH "/var/lock/qemu-nbd-%s"
bellard7a5ca862008-05-27 21:13:40 +000037
Paolo Bonziniaf49bbb2011-09-19 14:03:37 +020038static NBDExport *exp;
blueswir1b1d8e522008-10-26 13:43:07 +000039static int verbose;
Paolo Bonzinia517e882011-11-04 15:51:21 +010040static char *device;
41static char *srcpath;
42static char *sockpath;
Paolo Bonzinia61c6782011-09-12 17:28:11 +020043static bool sigterm_reported;
44static bool nbd_started;
45static int shared = 1;
46static int nb_fds;
bellard7a5ca862008-05-27 21:13:40 +000047
48static void usage(const char *name)
49{
50 printf(
51"Usage: %s [OPTIONS] FILE\n"
52"QEMU Disk Network Block Device Server\n"
53"\n"
Laurent Vivierc2e28722010-09-17 20:37:25 +020054" -p, --port=PORT port to listen on (default `%d')\n"
bellard7a5ca862008-05-27 21:13:40 +000055" -o, --offset=OFFSET offset into the image\n"
56" -b, --bind=IFACE interface to bind to (default `0.0.0.0')\n"
thscd831bd2008-07-03 10:23:51 +000057" -k, --socket=PATH path to the unix socket\n"
58" (default '"SOCKET_PATH"')\n"
bellard7a5ca862008-05-27 21:13:40 +000059" -r, --read-only export read-only\n"
60" -P, --partition=NUM only expose partition NUM\n"
ths2f726482008-07-03 11:47:46 +000061" -s, --snapshot use snapshot file\n"
62" -n, --nocache disable host cache\n"
thscd831bd2008-07-03 10:23:51 +000063" -c, --connect=DEV connect FILE to the local NBD device DEV\n"
64" -d, --disconnect disconnect the specified device\n"
ths3b05a8e2008-07-03 12:45:02 +000065" -e, --shared=NUM device can be shared by NUM clients (default '1')\n"
ths75818252008-07-03 13:41:03 +000066" -t, --persistent don't exit on the last connection\n"
bellard7a5ca862008-05-27 21:13:40 +000067" -v, --verbose display extra debugging information\n"
68" -h, --help display this help and exit\n"
69" -V, --version output version information and exit\n"
70"\n"
71"Report bugs to <anthony@codemonkey.ws>\n"
Laurent Vivierc2e28722010-09-17 20:37:25 +020072 , name, NBD_DEFAULT_PORT, "DEVICE");
bellard7a5ca862008-05-27 21:13:40 +000073}
74
75static void version(const char *name)
76{
77 printf(
ths315bc7a2008-07-18 18:06:23 +000078"%s version 0.0.1\n"
bellard7a5ca862008-05-27 21:13:40 +000079"Written by Anthony Liguori.\n"
80"\n"
81"Copyright (C) 2006 Anthony Liguori <anthony@codemonkey.ws>.\n"
82"This is free software; see the source for copying conditions. There is NO\n"
83"warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n"
ths315bc7a2008-07-18 18:06:23 +000084 , name);
bellard7a5ca862008-05-27 21:13:40 +000085}
86
87struct partition_record
88{
89 uint8_t bootable;
90 uint8_t start_head;
91 uint32_t start_cylinder;
92 uint8_t start_sector;
93 uint8_t system;
94 uint8_t end_head;
95 uint8_t end_cylinder;
96 uint8_t end_sector;
97 uint32_t start_sector_abs;
98 uint32_t nb_sectors_abs;
99};
100
101static void read_partition(uint8_t *p, struct partition_record *r)
102{
103 r->bootable = p[0];
104 r->start_head = p[1];
105 r->start_cylinder = p[3] | ((p[2] << 2) & 0x0300);
106 r->start_sector = p[2] & 0x3f;
107 r->system = p[4];
108 r->end_head = p[5];
109 r->end_cylinder = p[7] | ((p[6] << 2) & 0x300);
110 r->end_sector = p[6] & 0x3f;
111 r->start_sector_abs = p[8] | p[9] << 8 | p[10] << 16 | p[11] << 24;
112 r->nb_sectors_abs = p[12] | p[13] << 8 | p[14] << 16 | p[15] << 24;
113}
114
115static int find_partition(BlockDriverState *bs, int partition,
116 off_t *offset, off_t *size)
117{
118 struct partition_record mbr[4];
119 uint8_t data[512];
120 int i;
121 int ext_partnum = 4;
Ryota Ozakicb7cf0e2010-05-03 06:50:25 +0900122 int ret;
bellard7a5ca862008-05-27 21:13:40 +0000123
Ryota Ozakicb7cf0e2010-05-03 06:50:25 +0900124 if ((ret = bdrv_read(bs, 0, data, 1)) < 0) {
125 errno = -ret;
126 err(EXIT_FAILURE, "error while reading");
127 }
bellard7a5ca862008-05-27 21:13:40 +0000128
129 if (data[510] != 0x55 || data[511] != 0xaa) {
130 errno = -EINVAL;
131 return -1;
132 }
133
134 for (i = 0; i < 4; i++) {
135 read_partition(&data[446 + 16 * i], &mbr[i]);
136
137 if (!mbr[i].nb_sectors_abs)
138 continue;
139
140 if (mbr[i].system == 0xF || mbr[i].system == 0x5) {
141 struct partition_record ext[4];
142 uint8_t data1[512];
143 int j;
144
Ryota Ozakicb7cf0e2010-05-03 06:50:25 +0900145 if ((ret = bdrv_read(bs, mbr[i].start_sector_abs, data1, 1)) < 0) {
146 errno = -ret;
147 err(EXIT_FAILURE, "error while reading");
148 }
bellard7a5ca862008-05-27 21:13:40 +0000149
150 for (j = 0; j < 4; j++) {
151 read_partition(&data1[446 + 16 * j], &ext[j]);
152 if (!ext[j].nb_sectors_abs)
153 continue;
154
155 if ((ext_partnum + j + 1) == partition) {
156 *offset = (uint64_t)ext[j].start_sector_abs << 9;
157 *size = (uint64_t)ext[j].nb_sectors_abs << 9;
158 return 0;
159 }
160 }
161 ext_partnum += 4;
162 } else if ((i + 1) == partition) {
163 *offset = (uint64_t)mbr[i].start_sector_abs << 9;
164 *size = (uint64_t)mbr[i].nb_sectors_abs << 9;
165 return 0;
166 }
167 }
168
169 errno = -ENOENT;
170 return -1;
171}
172
Paolo Bonzinibb345112011-11-04 15:51:19 +0100173static void termsig_handler(int signum)
174{
Paolo Bonzinia61c6782011-09-12 17:28:11 +0200175 sigterm_reported = true;
176 qemu_notify_event();
Paolo Bonzinibb345112011-11-04 15:51:19 +0100177}
178
Paolo Bonzinia517e882011-11-04 15:51:21 +0100179static void *show_parts(void *arg)
thscd831bd2008-07-03 10:23:51 +0000180{
Paolo Bonzinia517e882011-11-04 15:51:21 +0100181 int nbd;
thscd831bd2008-07-03 10:23:51 +0000182
Paolo Bonzinia517e882011-11-04 15:51:21 +0100183 /* linux just needs an open() to trigger
184 * the partition table update
185 * but remember to load the module with max_part != 0 :
186 * modprobe nbd max_part=63
187 */
188 nbd = open(device, O_RDWR);
189 if (nbd != -1) {
190 close(nbd);
thscd831bd2008-07-03 10:23:51 +0000191 }
Paolo Bonzinia517e882011-11-04 15:51:21 +0100192 return NULL;
193}
194
195static void *nbd_client_thread(void *arg)
196{
197 int fd = *(int *)arg;
198 off_t size;
199 size_t blocksize;
200 uint32_t nbdflags;
201 int sock;
202 int ret;
203 pthread_t show_parts_thread;
204
205 do {
206 sock = unix_socket_outgoing(sockpath);
207 if (sock == -1) {
Paolo Bonzinif1ef5552011-11-04 15:51:23 +0100208 goto out;
Paolo Bonzinia517e882011-11-04 15:51:21 +0100209 }
210 } while (sock == -1);
211
212 ret = nbd_receive_negotiate(sock, NULL, &nbdflags,
213 &size, &blocksize);
214 if (ret == -1) {
215 goto out;
216 }
217
218 ret = nbd_init(fd, sock, nbdflags, size, blocksize);
219 if (ret == -1) {
220 goto out;
221 }
222
223 /* update partition table */
224 pthread_create(&show_parts_thread, NULL, show_parts, NULL);
225
Paolo Bonzinic1f8fdc2011-11-04 15:51:22 +0100226 if (verbose) {
227 fprintf(stderr, "NBD device %s is now connected to %s\n",
228 device, srcpath);
229 } else {
230 /* Close stderr so that the qemu-nbd process exits. */
231 dup2(STDOUT_FILENO, STDERR_FILENO);
232 }
Paolo Bonzinia517e882011-11-04 15:51:21 +0100233
234 ret = nbd_client(fd);
235 if (ret) {
236 goto out;
237 }
238 close(fd);
239 kill(getpid(), SIGTERM);
240 return (void *) EXIT_SUCCESS;
241
242out:
243 kill(getpid(), SIGTERM);
244 return (void *) EXIT_FAILURE;
thscd831bd2008-07-03 10:23:51 +0000245}
246
Paolo Bonzinia61c6782011-09-12 17:28:11 +0200247static int nbd_can_accept(void *opaque)
248{
249 return nb_fds < shared;
250}
251
252static void nbd_read(void *opaque)
253{
254 int fd = (uintptr_t) opaque;
255
256 if (nbd_trip(exp, fd) != 0) {
257 qemu_set_fd_handler2(fd, NULL, NULL, NULL, NULL);
258 close(fd);
259 nb_fds--;
260 }
261}
262
263static void nbd_accept(void *opaque)
264{
265 int server_fd = (uintptr_t) opaque;
266 struct sockaddr_in addr;
267 socklen_t addr_len = sizeof(addr);
268
269 int fd = accept(server_fd, (struct sockaddr *)&addr, &addr_len);
270 nbd_started = true;
271 if (fd != -1 && nbd_negotiate(exp, fd) != -1) {
272 qemu_set_fd_handler2(fd, NULL, nbd_read, NULL, (void *) (intptr_t) fd);
273 nb_fds++;
274 }
275}
276
bellard7a5ca862008-05-27 21:13:40 +0000277int main(int argc, char **argv)
278{
279 BlockDriverState *bs;
280 off_t dev_offset = 0;
Paolo Bonzinib90fb4b2011-09-08 17:24:54 +0200281 uint32_t nbdflags = 0;
thscd831bd2008-07-03 10:23:51 +0000282 bool disconnect = false;
bellard7a5ca862008-05-27 21:13:40 +0000283 const char *bindto = "0.0.0.0";
Laurent Vivierc2e28722010-09-17 20:37:25 +0200284 int port = NBD_DEFAULT_PORT;
bellard7a5ca862008-05-27 21:13:40 +0000285 off_t fd_size;
aliguorid6aa6712009-01-08 19:34:35 +0000286 const char *sopt = "hVb:o:p:rsnP:c:dvk:e:t";
bellard7a5ca862008-05-27 21:13:40 +0000287 struct option lopt[] = {
Blue Swirl660f11b2009-07-31 21:16:51 +0000288 { "help", 0, NULL, 'h' },
289 { "version", 0, NULL, 'V' },
290 { "bind", 1, NULL, 'b' },
291 { "port", 1, NULL, 'p' },
292 { "socket", 1, NULL, 'k' },
293 { "offset", 1, NULL, 'o' },
294 { "read-only", 0, NULL, 'r' },
295 { "partition", 1, NULL, 'P' },
296 { "connect", 1, NULL, 'c' },
297 { "disconnect", 0, NULL, 'd' },
298 { "snapshot", 0, NULL, 's' },
299 { "nocache", 0, NULL, 'n' },
300 { "shared", 1, NULL, 'e' },
301 { "persistent", 0, NULL, 't' },
302 { "verbose", 0, NULL, 'v' },
303 { NULL, 0, NULL, 0 }
bellard7a5ca862008-05-27 21:13:40 +0000304 };
305 int ch;
306 int opt_ind = 0;
307 int li;
308 char *end;
Naphtali Spreif5edb012010-01-17 16:48:13 +0200309 int flags = BDRV_O_RDWR;
bellard7a5ca862008-05-27 21:13:40 +0000310 int partition = -1;
thscd831bd2008-07-03 10:23:51 +0000311 int ret;
ths3b05a8e2008-07-03 12:45:02 +0000312 int fd;
ths75818252008-07-03 13:41:03 +0000313 int persistent = 0;
Paolo Bonzinia517e882011-11-04 15:51:21 +0100314 pthread_t client_thread;
bellard7a5ca862008-05-27 21:13:40 +0000315
Paolo Bonzinia517e882011-11-04 15:51:21 +0100316 /* The client thread uses SIGTERM to interrupt the server. A signal
317 * handler ensures that "qemu-nbd -v -c" exits with a nice status code.
318 */
Paolo Bonzinibb345112011-11-04 15:51:19 +0100319 struct sigaction sa_sigterm;
Paolo Bonzinibb345112011-11-04 15:51:19 +0100320 memset(&sa_sigterm, 0, sizeof(sa_sigterm));
321 sa_sigterm.sa_handler = termsig_handler;
322 sigaction(SIGTERM, &sa_sigterm, NULL);
323
bellard7a5ca862008-05-27 21:13:40 +0000324 while ((ch = getopt_long(argc, argv, sopt, lopt, &opt_ind)) != -1) {
325 switch (ch) {
326 case 's':
ths2f726482008-07-03 11:47:46 +0000327 flags |= BDRV_O_SNAPSHOT;
328 break;
329 case 'n':
Christoph Hellwiga6599792011-05-17 18:04:06 +0200330 flags |= BDRV_O_NOCACHE | BDRV_O_CACHE_WB;
bellard7a5ca862008-05-27 21:13:40 +0000331 break;
332 case 'b':
333 bindto = optarg;
334 break;
335 case 'p':
336 li = strtol(optarg, &end, 0);
337 if (*end) {
Ryota Ozakib6353be2010-03-20 15:23:23 +0900338 errx(EXIT_FAILURE, "Invalid port `%s'", optarg);
bellard7a5ca862008-05-27 21:13:40 +0000339 }
340 if (li < 1 || li > 65535) {
Ryota Ozakib6353be2010-03-20 15:23:23 +0900341 errx(EXIT_FAILURE, "Port out of range `%s'", optarg);
bellard7a5ca862008-05-27 21:13:40 +0000342 }
343 port = (uint16_t)li;
344 break;
345 case 'o':
346 dev_offset = strtoll (optarg, &end, 0);
347 if (*end) {
Ryota Ozakib6353be2010-03-20 15:23:23 +0900348 errx(EXIT_FAILURE, "Invalid offset `%s'", optarg);
bellard7a5ca862008-05-27 21:13:40 +0000349 }
350 if (dev_offset < 0) {
Ryota Ozakib6353be2010-03-20 15:23:23 +0900351 errx(EXIT_FAILURE, "Offset must be positive `%s'", optarg);
bellard7a5ca862008-05-27 21:13:40 +0000352 }
353 break;
354 case 'r':
Paolo Bonzinib90fb4b2011-09-08 17:24:54 +0200355 nbdflags |= NBD_FLAG_READ_ONLY;
Naphtali Sprei07108b22010-03-14 15:19:57 +0200356 flags &= ~BDRV_O_RDWR;
bellard7a5ca862008-05-27 21:13:40 +0000357 break;
358 case 'P':
359 partition = strtol(optarg, &end, 0);
360 if (*end)
Ryota Ozakib6353be2010-03-20 15:23:23 +0900361 errx(EXIT_FAILURE, "Invalid partition `%s'", optarg);
bellard7a5ca862008-05-27 21:13:40 +0000362 if (partition < 1 || partition > 8)
Ryota Ozakib6353be2010-03-20 15:23:23 +0900363 errx(EXIT_FAILURE, "Invalid partition %d", partition);
bellard7a5ca862008-05-27 21:13:40 +0000364 break;
thscd831bd2008-07-03 10:23:51 +0000365 case 'k':
Paolo Bonzinib32f6c22011-11-04 15:51:20 +0100366 sockpath = optarg;
367 if (sockpath[0] != '/')
Ryota Ozakib6353be2010-03-20 15:23:23 +0900368 errx(EXIT_FAILURE, "socket path must be absolute\n");
thscd831bd2008-07-03 10:23:51 +0000369 break;
370 case 'd':
371 disconnect = true;
372 break;
373 case 'c':
374 device = optarg;
375 break;
ths3b05a8e2008-07-03 12:45:02 +0000376 case 'e':
377 shared = strtol(optarg, &end, 0);
378 if (*end) {
Ryota Ozakib6353be2010-03-20 15:23:23 +0900379 errx(EXIT_FAILURE, "Invalid shared device number '%s'", optarg);
ths3b05a8e2008-07-03 12:45:02 +0000380 }
381 if (shared < 1) {
Ryota Ozakib6353be2010-03-20 15:23:23 +0900382 errx(EXIT_FAILURE, "Shared device number must be greater than 0\n");
ths3b05a8e2008-07-03 12:45:02 +0000383 }
384 break;
ths75818252008-07-03 13:41:03 +0000385 case 't':
386 persistent = 1;
387 break;
bellard7a5ca862008-05-27 21:13:40 +0000388 case 'v':
389 verbose = 1;
390 break;
391 case 'V':
392 version(argv[0]);
393 exit(0);
394 break;
395 case 'h':
396 usage(argv[0]);
397 exit(0);
398 break;
399 case '?':
Ryota Ozakib6353be2010-03-20 15:23:23 +0900400 errx(EXIT_FAILURE, "Try `%s --help' for more information.",
bellard7a5ca862008-05-27 21:13:40 +0000401 argv[0]);
402 }
403 }
404
405 if ((argc - optind) != 1) {
Ryota Ozakib6353be2010-03-20 15:23:23 +0900406 errx(EXIT_FAILURE, "Invalid number of argument.\n"
bellard7a5ca862008-05-27 21:13:40 +0000407 "Try `%s --help' for more information.",
408 argv[0]);
409 }
410
thscd831bd2008-07-03 10:23:51 +0000411 if (disconnect) {
412 fd = open(argv[optind], O_RDWR);
413 if (fd == -1)
Ryota Ozakicb7cf0e2010-05-03 06:50:25 +0900414 err(EXIT_FAILURE, "Cannot open %s", argv[optind]);
thscd831bd2008-07-03 10:23:51 +0000415
416 nbd_disconnect(fd);
417
418 close(fd);
419
420 printf("%s disconnected\n", argv[optind]);
421
422 return 0;
423 }
424
Paolo Bonzinic1f8fdc2011-11-04 15:51:22 +0100425 if (device && !verbose) {
426 int stderr_fd[2];
427 pid_t pid;
428 int ret;
429
430 if (qemu_pipe(stderr_fd) == -1) {
431 err(EXIT_FAILURE, "Error setting up communication pipe");
432 }
433
434 /* Now daemonize, but keep a communication channel open to
435 * print errors and exit with the proper status code.
436 */
437 pid = fork();
438 if (pid == 0) {
439 close(stderr_fd[0]);
440 ret = qemu_daemon(0, 0);
441
442 /* Temporarily redirect stderr to the parent's pipe... */
443 dup2(stderr_fd[1], STDERR_FILENO);
444 if (ret == -1) {
445 err(EXIT_FAILURE, "Failed to daemonize");
446 }
447
448 /* ... close the descriptor we inherited and go on. */
449 close(stderr_fd[1]);
450 } else {
451 bool errors = false;
452 char *buf;
453
454 /* In the parent. Print error messages from the child until
455 * it closes the pipe.
456 */
457 close(stderr_fd[1]);
458 buf = g_malloc(1024);
459 while ((ret = read(stderr_fd[0], buf, 1024)) > 0) {
460 errors = true;
461 ret = qemu_write_full(STDERR_FILENO, buf, ret);
462 if (ret == -1) {
463 exit(EXIT_FAILURE);
464 }
465 }
466 if (ret == -1) {
467 err(EXIT_FAILURE, "Cannot read from daemon");
468 }
469
470 /* Usually the daemon should not print any message.
471 * Exit with zero status in that case.
472 */
473 exit(errors);
474 }
475 }
476
thscd831bd2008-07-03 10:23:51 +0000477 if (device) {
Paolo Bonzinia517e882011-11-04 15:51:21 +0100478 /* Open before spawning new threads. In the future, we may
479 * drop privileges after opening.
480 */
481 fd = open(device, O_RDWR);
482 if (fd == -1) {
483 err(EXIT_FAILURE, "Failed to open %s", device);
Ryota Ozakicb7cf0e2010-05-03 06:50:25 +0900484 }
485
Paolo Bonzinib32f6c22011-11-04 15:51:20 +0100486 if (sockpath == NULL) {
487 sockpath = g_malloc(128);
488 snprintf(sockpath, 128, SOCKET_PATH, basename(device));
thscd831bd2008-07-03 10:23:51 +0000489 }
thscd831bd2008-07-03 10:23:51 +0000490 }
491
Paolo Bonzini802ddc32011-11-04 15:51:24 +0100492 bdrv_init();
493 atexit(bdrv_close_all);
494
495 bs = bdrv_new("hda");
496 srcpath = argv[optind];
497 if ((ret = bdrv_open(bs, srcpath, flags, NULL)) < 0) {
498 errno = -ret;
499 err(EXIT_FAILURE, "Failed to bdrv_open '%s'", argv[optind]);
500 }
501
502 fd_size = bs->total_sectors * 512;
503
504 if (partition != -1 &&
505 find_partition(bs, partition, &dev_offset, &fd_size)) {
506 err(EXIT_FAILURE, "Could not find partition %d", partition);
507 }
508
Paolo Bonziniaf49bbb2011-09-19 14:03:37 +0200509 exp = nbd_export_new(bs, dev_offset, fd_size, nbdflags);
ths3b05a8e2008-07-03 12:45:02 +0000510
Paolo Bonzinib32f6c22011-11-04 15:51:20 +0100511 if (sockpath) {
Paolo Bonzinia61c6782011-09-12 17:28:11 +0200512 fd = unix_socket_incoming(sockpath);
thscd831bd2008-07-03 10:23:51 +0000513 } else {
Paolo Bonzinia61c6782011-09-12 17:28:11 +0200514 fd = tcp_socket_incoming(bindto, port);
thscd831bd2008-07-03 10:23:51 +0000515 }
516
Paolo Bonzinia61c6782011-09-12 17:28:11 +0200517 if (fd == -1) {
bellard7a5ca862008-05-27 21:13:40 +0000518 return 1;
Paolo Bonzinia61c6782011-09-12 17:28:11 +0200519 }
Paolo Bonzinif1ef5552011-11-04 15:51:23 +0100520
521 if (device) {
522 int ret;
523
524 ret = pthread_create(&client_thread, NULL, nbd_client_thread, &fd);
525 if (ret != 0) {
526 errx(EXIT_FAILURE, "Failed to create client thread: %s",
527 strerror(ret));
528 }
529 } else {
530 /* Shut up GCC warnings. */
531 memset(&client_thread, 0, sizeof(client_thread));
532 }
533
Paolo Bonzinia61c6782011-09-12 17:28:11 +0200534 qemu_init_main_loop();
535 qemu_set_fd_handler2(fd, nbd_can_accept, nbd_accept, NULL,
536 (void *)(uintptr_t)fd);
bellard7a5ca862008-05-27 21:13:40 +0000537
ths3b05a8e2008-07-03 12:45:02 +0000538 do {
Paolo Bonzinia61c6782011-09-12 17:28:11 +0200539 main_loop_wait(false);
540 } while (!sigterm_reported && (persistent || !nbd_started || nb_fds > 0));
ths3b05a8e2008-07-03 12:45:02 +0000541
Paolo Bonziniaf49bbb2011-09-19 14:03:37 +0200542 nbd_export_close(exp);
Paolo Bonzinib32f6c22011-11-04 15:51:20 +0100543 if (sockpath) {
544 unlink(sockpath);
545 }
bellard7a5ca862008-05-27 21:13:40 +0000546
Paolo Bonzinia517e882011-11-04 15:51:21 +0100547 if (device) {
548 void *ret;
549 pthread_join(client_thread, &ret);
550 exit(ret != NULL);
551 } else {
552 exit(EXIT_SUCCESS);
553 }
bellard7a5ca862008-05-27 21:13:40 +0000554}