Statistics
| Branch: | Revision:

root / qemu-nbd.c @ 537b41f5

History | View | Annotate | Download (20.7 kB)

1
/*
2
 *  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
16
 *  along with this program; if not, see <http://www.gnu.org/licenses/>.
17
 */
18

    
19
#include "qemu-common.h"
20
#include "block/block.h"
21
#include "block/nbd.h"
22
#include "qemu/main-loop.h"
23
#include "qemu/sockets.h"
24
#include "qemu/error-report.h"
25
#include "block/snapshot.h"
26

    
27
#include <stdarg.h>
28
#include <stdio.h>
29
#include <getopt.h>
30
#include <err.h>
31
#include <sys/types.h>
32
#include <sys/socket.h>
33
#include <netinet/in.h>
34
#include <netinet/tcp.h>
35
#include <arpa/inet.h>
36
#include <signal.h>
37
#include <libgen.h>
38
#include <pthread.h>
39

    
40
#define SOCKET_PATH          "/var/lock/qemu-nbd-%s"
41
#define QEMU_NBD_OPT_CACHE   1
42
#define QEMU_NBD_OPT_AIO     2
43
#define QEMU_NBD_OPT_DISCARD 3
44

    
45
static NBDExport *exp;
46
static int verbose;
47
static char *srcpath;
48
static char *sockpath;
49
static int persistent = 0;
50
static enum { RUNNING, TERMINATE, TERMINATING, TERMINATED } state;
51
static int shared = 1;
52
static int nb_fds;
53

    
54
static void usage(const char *name)
55
{
56
    (printf) (
57
"Usage: %s [OPTIONS] FILE\n"
58
"QEMU Disk Network Block Device Server\n"
59
"\n"
60
"  -h, --help           display this help and exit\n"
61
"  -V, --version        output version information and exit\n"
62
"\n"
63
"Connection properties:\n"
64
"  -p, --port=PORT      port to listen on (default `%d')\n"
65
"  -b, --bind=IFACE     interface to bind to (default `0.0.0.0')\n"
66
"  -k, --socket=PATH    path to the unix socket\n"
67
"                       (default '"SOCKET_PATH"')\n"
68
"  -e, --shared=NUM     device can be shared by NUM clients (default '1')\n"
69
"  -t, --persistent     don't exit on the last connection\n"
70
"  -v, --verbose        display extra debugging information\n"
71
"\n"
72
"Exposing part of the image:\n"
73
"  -o, --offset=OFFSET  offset into the image\n"
74
"  -P, --partition=NUM  only expose partition NUM\n"
75
"\n"
76
#ifdef __linux__
77
"Kernel NBD client support:\n"
78
"  -c, --connect=DEV    connect FILE to the local NBD device DEV\n"
79
"  -d, --disconnect     disconnect the specified device\n"
80
"\n"
81
#endif
82
"\n"
83
"Block device options:\n"
84
"  -f, --format=FORMAT  set image format (raw, qcow2, ...)\n"
85
"  -r, --read-only      export read-only\n"
86
"  -s, --snapshot       use FILE as an external snapshot, create a temporary\n"
87
"                       file with backing_file=FILE, redirect the write to\n"
88
"                       the temporary one\n"
89
"  -l, --load-snapshot=SNAPSHOT_PARAM\n"
90
"                       load an internal snapshot inside FILE and export it\n"
91
"                       as an read-only device, SNAPSHOT_PARAM format is\n"
92
"                       'snapshot.id=[ID],snapshot.name=[NAME]', or\n"
93
"                       '[ID_OR_NAME]'\n"
94
"  -n, --nocache        disable host cache\n"
95
"      --cache=MODE     set cache mode (none, writeback, ...)\n"
96
#ifdef CONFIG_LINUX_AIO
97
"      --aio=MODE       set AIO mode (native or threads)\n"
98
#endif
99
"\n"
100
"Report bugs to <qemu-devel@nongnu.org>\n"
101
    , name, NBD_DEFAULT_PORT, "DEVICE");
102
}
103

    
104
static void version(const char *name)
105
{
106
    printf(
107
"%s version 0.0.1\n"
108
"Written by Anthony Liguori.\n"
109
"\n"
110
"Copyright (C) 2006 Anthony Liguori <anthony@codemonkey.ws>.\n"
111
"This is free software; see the source for copying conditions.  There is NO\n"
112
"warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n"
113
    , name);
114
}
115

    
116
struct partition_record
117
{
118
    uint8_t bootable;
119
    uint8_t start_head;
120
    uint32_t start_cylinder;
121
    uint8_t start_sector;
122
    uint8_t system;
123
    uint8_t end_head;
124
    uint8_t end_cylinder;
125
    uint8_t end_sector;
126
    uint32_t start_sector_abs;
127
    uint32_t nb_sectors_abs;
128
};
129

    
130
static void read_partition(uint8_t *p, struct partition_record *r)
131
{
132
    r->bootable = p[0];
133
    r->start_head = p[1];
134
    r->start_cylinder = p[3] | ((p[2] << 2) & 0x0300);
135
    r->start_sector = p[2] & 0x3f;
136
    r->system = p[4];
137
    r->end_head = p[5];
138
    r->end_cylinder = p[7] | ((p[6] << 2) & 0x300);
139
    r->end_sector = p[6] & 0x3f;
140
    r->start_sector_abs = p[8] | p[9] << 8 | p[10] << 16 | p[11] << 24;
141
    r->nb_sectors_abs = p[12] | p[13] << 8 | p[14] << 16 | p[15] << 24;
142
}
143

    
144
static int find_partition(BlockDriverState *bs, int partition,
145
                          off_t *offset, off_t *size)
146
{
147
    struct partition_record mbr[4];
148
    uint8_t data[512];
149
    int i;
150
    int ext_partnum = 4;
151
    int ret;
152

    
153
    if ((ret = bdrv_read(bs, 0, data, 1)) < 0) {
154
        errno = -ret;
155
        err(EXIT_FAILURE, "error while reading");
156
    }
157

    
158
    if (data[510] != 0x55 || data[511] != 0xaa) {
159
        return -EINVAL;
160
    }
161

    
162
    for (i = 0; i < 4; i++) {
163
        read_partition(&data[446 + 16 * i], &mbr[i]);
164

    
165
        if (!mbr[i].nb_sectors_abs)
166
            continue;
167

    
168
        if (mbr[i].system == 0xF || mbr[i].system == 0x5) {
169
            struct partition_record ext[4];
170
            uint8_t data1[512];
171
            int j;
172

    
173
            if ((ret = bdrv_read(bs, mbr[i].start_sector_abs, data1, 1)) < 0) {
174
                errno = -ret;
175
                err(EXIT_FAILURE, "error while reading");
176
            }
177

    
178
            for (j = 0; j < 4; j++) {
179
                read_partition(&data1[446 + 16 * j], &ext[j]);
180
                if (!ext[j].nb_sectors_abs)
181
                    continue;
182

    
183
                if ((ext_partnum + j + 1) == partition) {
184
                    *offset = (uint64_t)ext[j].start_sector_abs << 9;
185
                    *size = (uint64_t)ext[j].nb_sectors_abs << 9;
186
                    return 0;
187
                }
188
            }
189
            ext_partnum += 4;
190
        } else if ((i + 1) == partition) {
191
            *offset = (uint64_t)mbr[i].start_sector_abs << 9;
192
            *size = (uint64_t)mbr[i].nb_sectors_abs << 9;
193
            return 0;
194
        }
195
    }
196

    
197
    return -ENOENT;
198
}
199

    
200
static void termsig_handler(int signum)
201
{
202
    state = TERMINATE;
203
    qemu_notify_event();
204
}
205

    
206
static void combine_addr(char *buf, size_t len, const char* address,
207
                         uint16_t port)
208
{
209
    /* If the address-part contains a colon, it's an IPv6 IP so needs [] */
210
    if (strstr(address, ":")) {
211
        snprintf(buf, len, "[%s]:%u", address, port);
212
    } else {
213
        snprintf(buf, len, "%s:%u", address, port);
214
    }
215
}
216

    
217
static int tcp_socket_incoming(const char *address, uint16_t port)
218
{
219
    char address_and_port[128];
220
    Error *local_err = NULL;
221

    
222
    combine_addr(address_and_port, 128, address, port);
223
    int fd = inet_listen(address_and_port, NULL, 0, SOCK_STREAM, 0, &local_err);
224

    
225
    if (local_err != NULL) {
226
        qerror_report_err(local_err);
227
        error_free(local_err);
228
    }
229
    return fd;
230
}
231

    
232
static int unix_socket_incoming(const char *path)
233
{
234
    Error *local_err = NULL;
235
    int fd = unix_listen(path, NULL, 0, &local_err);
236

    
237
    if (local_err != NULL) {
238
        qerror_report_err(local_err);
239
        error_free(local_err);
240
    }
241
    return fd;
242
}
243

    
244
static int unix_socket_outgoing(const char *path)
245
{
246
    Error *local_err = NULL;
247
    int fd = unix_connect(path, &local_err);
248

    
249
    if (local_err != NULL) {
250
        qerror_report_err(local_err);
251
        error_free(local_err);
252
    }
253
    return fd;
254
}
255

    
256
static void *show_parts(void *arg)
257
{
258
    char *device = arg;
259
    int nbd;
260

    
261
    /* linux just needs an open() to trigger
262
     * the partition table update
263
     * but remember to load the module with max_part != 0 :
264
     *     modprobe nbd max_part=63
265
     */
266
    nbd = open(device, O_RDWR);
267
    if (nbd >= 0) {
268
        close(nbd);
269
    }
270
    return NULL;
271
}
272

    
273
static void *nbd_client_thread(void *arg)
274
{
275
    char *device = arg;
276
    off_t size;
277
    size_t blocksize;
278
    uint32_t nbdflags;
279
    int fd, sock;
280
    int ret;
281
    pthread_t show_parts_thread;
282

    
283
    sock = unix_socket_outgoing(sockpath);
284
    if (sock < 0) {
285
        goto out;
286
    }
287

    
288
    ret = nbd_receive_negotiate(sock, NULL, &nbdflags,
289
                                &size, &blocksize);
290
    if (ret < 0) {
291
        goto out;
292
    }
293

    
294
    fd = open(device, O_RDWR);
295
    if (fd < 0) {
296
        /* Linux-only, we can use %m in printf.  */
297
        fprintf(stderr, "Failed to open %s: %m", device);
298
        goto out;
299
    }
300

    
301
    ret = nbd_init(fd, sock, nbdflags, size, blocksize);
302
    if (ret < 0) {
303
        goto out;
304
    }
305

    
306
    /* update partition table */
307
    pthread_create(&show_parts_thread, NULL, show_parts, device);
308

    
309
    if (verbose) {
310
        fprintf(stderr, "NBD device %s is now connected to %s\n",
311
                device, srcpath);
312
    } else {
313
        /* Close stderr so that the qemu-nbd process exits.  */
314
        dup2(STDOUT_FILENO, STDERR_FILENO);
315
    }
316

    
317
    ret = nbd_client(fd);
318
    if (ret) {
319
        goto out;
320
    }
321
    close(fd);
322
    kill(getpid(), SIGTERM);
323
    return (void *) EXIT_SUCCESS;
324

    
325
out:
326
    kill(getpid(), SIGTERM);
327
    return (void *) EXIT_FAILURE;
328
}
329

    
330
static int nbd_can_accept(void *opaque)
331
{
332
    return nb_fds < shared;
333
}
334

    
335
static void nbd_export_closed(NBDExport *exp)
336
{
337
    assert(state == TERMINATING);
338
    state = TERMINATED;
339
}
340

    
341
static void nbd_client_closed(NBDClient *client)
342
{
343
    nb_fds--;
344
    if (nb_fds == 0 && !persistent && state == RUNNING) {
345
        state = TERMINATE;
346
    }
347
    qemu_notify_event();
348
    nbd_client_put(client);
349
}
350

    
351
static void nbd_accept(void *opaque)
352
{
353
    int server_fd = (uintptr_t) opaque;
354
    struct sockaddr_in addr;
355
    socklen_t addr_len = sizeof(addr);
356

    
357
    int fd = accept(server_fd, (struct sockaddr *)&addr, &addr_len);
358
    if (state >= TERMINATE) {
359
        close(fd);
360
        return;
361
    }
362

    
363
    if (fd >= 0 && nbd_client_new(exp, fd, nbd_client_closed)) {
364
        nb_fds++;
365
    }
366
}
367

    
368
int main(int argc, char **argv)
369
{
370
    BlockDriverState *bs;
371
    BlockDriver *drv;
372
    off_t dev_offset = 0;
373
    uint32_t nbdflags = 0;
374
    bool disconnect = false;
375
    const char *bindto = "0.0.0.0";
376
    char *device = NULL;
377
    int port = NBD_DEFAULT_PORT;
378
    off_t fd_size;
379
    QemuOpts *sn_opts = NULL;
380
    const char *sn_id_or_name = NULL;
381
    const char *sopt = "hVb:o:p:rsnP:c:dvk:e:f:tl:";
382
    struct option lopt[] = {
383
        { "help", 0, NULL, 'h' },
384
        { "version", 0, NULL, 'V' },
385
        { "bind", 1, NULL, 'b' },
386
        { "port", 1, NULL, 'p' },
387
        { "socket", 1, NULL, 'k' },
388
        { "offset", 1, NULL, 'o' },
389
        { "read-only", 0, NULL, 'r' },
390
        { "partition", 1, NULL, 'P' },
391
        { "connect", 1, NULL, 'c' },
392
        { "disconnect", 0, NULL, 'd' },
393
        { "snapshot", 0, NULL, 's' },
394
        { "load-snapshot", 1, NULL, 'l' },
395
        { "nocache", 0, NULL, 'n' },
396
        { "cache", 1, NULL, QEMU_NBD_OPT_CACHE },
397
#ifdef CONFIG_LINUX_AIO
398
        { "aio", 1, NULL, QEMU_NBD_OPT_AIO },
399
#endif
400
        { "discard", 1, NULL, QEMU_NBD_OPT_DISCARD },
401
        { "shared", 1, NULL, 'e' },
402
        { "format", 1, NULL, 'f' },
403
        { "persistent", 0, NULL, 't' },
404
        { "verbose", 0, NULL, 'v' },
405
        { NULL, 0, NULL, 0 }
406
    };
407
    int ch;
408
    int opt_ind = 0;
409
    int li;
410
    char *end;
411
    int flags = BDRV_O_RDWR;
412
    int partition = -1;
413
    int ret;
414
    int fd;
415
    bool seen_cache = false;
416
    bool seen_discard = false;
417
#ifdef CONFIG_LINUX_AIO
418
    bool seen_aio = false;
419
#endif
420
    pthread_t client_thread;
421
    const char *fmt = NULL;
422
    Error *local_err = NULL;
423

    
424
    /* The client thread uses SIGTERM to interrupt the server.  A signal
425
     * handler ensures that "qemu-nbd -v -c" exits with a nice status code.
426
     */
427
    struct sigaction sa_sigterm;
428
    memset(&sa_sigterm, 0, sizeof(sa_sigterm));
429
    sa_sigterm.sa_handler = termsig_handler;
430
    sigaction(SIGTERM, &sa_sigterm, NULL);
431

    
432
    while ((ch = getopt_long(argc, argv, sopt, lopt, &opt_ind)) != -1) {
433
        switch (ch) {
434
        case 's':
435
            flags |= BDRV_O_SNAPSHOT;
436
            break;
437
        case 'n':
438
            optarg = (char *) "none";
439
            /* fallthrough */
440
        case QEMU_NBD_OPT_CACHE:
441
            if (seen_cache) {
442
                errx(EXIT_FAILURE, "-n and --cache can only be specified once");
443
            }
444
            seen_cache = true;
445
            if (bdrv_parse_cache_flags(optarg, &flags) == -1) {
446
                errx(EXIT_FAILURE, "Invalid cache mode `%s'", optarg);
447
            }
448
            break;
449
#ifdef CONFIG_LINUX_AIO
450
        case QEMU_NBD_OPT_AIO:
451
            if (seen_aio) {
452
                errx(EXIT_FAILURE, "--aio can only be specified once");
453
            }
454
            seen_aio = true;
455
            if (!strcmp(optarg, "native")) {
456
                flags |= BDRV_O_NATIVE_AIO;
457
            } else if (!strcmp(optarg, "threads")) {
458
                /* this is the default */
459
            } else {
460
               errx(EXIT_FAILURE, "invalid aio mode `%s'", optarg);
461
            }
462
            break;
463
#endif
464
        case QEMU_NBD_OPT_DISCARD:
465
            if (seen_discard) {
466
                errx(EXIT_FAILURE, "--discard can only be specified once");
467
            }
468
            seen_discard = true;
469
            if (bdrv_parse_discard_flags(optarg, &flags) == -1) {
470
                errx(EXIT_FAILURE, "Invalid discard mode `%s'", optarg);
471
            }
472
            break;
473
        case 'b':
474
            bindto = optarg;
475
            break;
476
        case 'p':
477
            li = strtol(optarg, &end, 0);
478
            if (*end) {
479
                errx(EXIT_FAILURE, "Invalid port `%s'", optarg);
480
            }
481
            if (li < 1 || li > 65535) {
482
                errx(EXIT_FAILURE, "Port out of range `%s'", optarg);
483
            }
484
            port = (uint16_t)li;
485
            break;
486
        case 'o':
487
                dev_offset = strtoll (optarg, &end, 0);
488
            if (*end) {
489
                errx(EXIT_FAILURE, "Invalid offset `%s'", optarg);
490
            }
491
            if (dev_offset < 0) {
492
                errx(EXIT_FAILURE, "Offset must be positive `%s'", optarg);
493
            }
494
            break;
495
        case 'l':
496
            if (strstart(optarg, SNAPSHOT_OPT_BASE, NULL)) {
497
                sn_opts = qemu_opts_parse(&internal_snapshot_opts, optarg, 0);
498
                if (!sn_opts) {
499
                    errx(EXIT_FAILURE, "Failed in parsing snapshot param `%s'",
500
                         optarg);
501
                }
502
            } else {
503
                sn_id_or_name = optarg;
504
            }
505
            /* fall through */
506
        case 'r':
507
            nbdflags |= NBD_FLAG_READ_ONLY;
508
            flags &= ~BDRV_O_RDWR;
509
            break;
510
        case 'P':
511
            partition = strtol(optarg, &end, 0);
512
            if (*end)
513
                errx(EXIT_FAILURE, "Invalid partition `%s'", optarg);
514
            if (partition < 1 || partition > 8)
515
                errx(EXIT_FAILURE, "Invalid partition %d", partition);
516
            break;
517
        case 'k':
518
            sockpath = optarg;
519
            if (sockpath[0] != '/')
520
                errx(EXIT_FAILURE, "socket path must be absolute\n");
521
            break;
522
        case 'd':
523
            disconnect = true;
524
            break;
525
        case 'c':
526
            device = optarg;
527
            break;
528
        case 'e':
529
            shared = strtol(optarg, &end, 0);
530
            if (*end) {
531
                errx(EXIT_FAILURE, "Invalid shared device number '%s'", optarg);
532
            }
533
            if (shared < 1) {
534
                errx(EXIT_FAILURE, "Shared device number must be greater than 0\n");
535
            }
536
            break;
537
        case 'f':
538
            fmt = optarg;
539
            break;
540
        case 't':
541
            persistent = 1;
542
            break;
543
        case 'v':
544
            verbose = 1;
545
            break;
546
        case 'V':
547
            version(argv[0]);
548
            exit(0);
549
            break;
550
        case 'h':
551
            usage(argv[0]);
552
            exit(0);
553
            break;
554
        case '?':
555
            errx(EXIT_FAILURE, "Try `%s --help' for more information.",
556
                 argv[0]);
557
        }
558
    }
559

    
560
    if ((argc - optind) != 1) {
561
        errx(EXIT_FAILURE, "Invalid number of argument.\n"
562
             "Try `%s --help' for more information.",
563
             argv[0]);
564
    }
565

    
566
    if (disconnect) {
567
        fd = open(argv[optind], O_RDWR);
568
        if (fd < 0) {
569
            err(EXIT_FAILURE, "Cannot open %s", argv[optind]);
570
        }
571
        nbd_disconnect(fd);
572

    
573
        close(fd);
574

    
575
        printf("%s disconnected\n", argv[optind]);
576

    
577
        return 0;
578
    }
579

    
580
    if (device && !verbose) {
581
        int stderr_fd[2];
582
        pid_t pid;
583
        int ret;
584

    
585
        if (qemu_pipe(stderr_fd) < 0) {
586
            err(EXIT_FAILURE, "Error setting up communication pipe");
587
        }
588

    
589
        /* Now daemonize, but keep a communication channel open to
590
         * print errors and exit with the proper status code.
591
         */
592
        pid = fork();
593
        if (pid == 0) {
594
            close(stderr_fd[0]);
595
            ret = qemu_daemon(1, 0);
596

    
597
            /* Temporarily redirect stderr to the parent's pipe...  */
598
            dup2(stderr_fd[1], STDERR_FILENO);
599
            if (ret < 0) {
600
                err(EXIT_FAILURE, "Failed to daemonize");
601
            }
602

    
603
            /* ... close the descriptor we inherited and go on.  */
604
            close(stderr_fd[1]);
605
        } else {
606
            bool errors = false;
607
            char *buf;
608

    
609
            /* In the parent.  Print error messages from the child until
610
             * it closes the pipe.
611
             */
612
            close(stderr_fd[1]);
613
            buf = g_malloc(1024);
614
            while ((ret = read(stderr_fd[0], buf, 1024)) > 0) {
615
                errors = true;
616
                ret = qemu_write_full(STDERR_FILENO, buf, ret);
617
                if (ret < 0) {
618
                    exit(EXIT_FAILURE);
619
                }
620
            }
621
            if (ret < 0) {
622
                err(EXIT_FAILURE, "Cannot read from daemon");
623
            }
624

    
625
            /* Usually the daemon should not print any message.
626
             * Exit with zero status in that case.
627
             */
628
            exit(errors);
629
        }
630
    }
631

    
632
    if (device != NULL && sockpath == NULL) {
633
        sockpath = g_malloc(128);
634
        snprintf(sockpath, 128, SOCKET_PATH, basename(device));
635
    }
636

    
637
    qemu_init_main_loop();
638
    bdrv_init();
639
    atexit(bdrv_close_all);
640

    
641
    if (fmt) {
642
        drv = bdrv_find_format(fmt);
643
        if (!drv) {
644
            errx(EXIT_FAILURE, "Unknown file format '%s'", fmt);
645
        }
646
    } else {
647
        drv = NULL;
648
    }
649

    
650
    bs = bdrv_new("hda");
651
    srcpath = argv[optind];
652
    ret = bdrv_open(&bs, srcpath, NULL, NULL, flags, drv, &local_err);
653
    if (ret < 0) {
654
        errno = -ret;
655
        err(EXIT_FAILURE, "Failed to bdrv_open '%s': %s", argv[optind],
656
            error_get_pretty(local_err));
657
    }
658

    
659
    if (sn_opts) {
660
        ret = bdrv_snapshot_load_tmp(bs,
661
                                     qemu_opt_get(sn_opts, SNAPSHOT_OPT_ID),
662
                                     qemu_opt_get(sn_opts, SNAPSHOT_OPT_NAME),
663
                                     &local_err);
664
    } else if (sn_id_or_name) {
665
        ret = bdrv_snapshot_load_tmp_by_id_or_name(bs, sn_id_or_name,
666
                                                   &local_err);
667
    }
668
    if (ret < 0) {
669
        errno = -ret;
670
        err(EXIT_FAILURE,
671
            "Failed to load snapshot: %s",
672
            error_get_pretty(local_err));
673
    }
674

    
675
    fd_size = bdrv_getlength(bs);
676

    
677
    if (partition != -1) {
678
        ret = find_partition(bs, partition, &dev_offset, &fd_size);
679
        if (ret < 0) {
680
            errno = -ret;
681
            err(EXIT_FAILURE, "Could not find partition %d", partition);
682
        }
683
    }
684

    
685
    exp = nbd_export_new(bs, dev_offset, fd_size, nbdflags, nbd_export_closed);
686

    
687
    if (sockpath) {
688
        fd = unix_socket_incoming(sockpath);
689
    } else {
690
        fd = tcp_socket_incoming(bindto, port);
691
    }
692

    
693
    if (fd < 0) {
694
        return 1;
695
    }
696

    
697
    if (device) {
698
        int ret;
699

    
700
        ret = pthread_create(&client_thread, NULL, nbd_client_thread, device);
701
        if (ret != 0) {
702
            errx(EXIT_FAILURE, "Failed to create client thread: %s",
703
                 strerror(ret));
704
        }
705
    } else {
706
        /* Shut up GCC warnings.  */
707
        memset(&client_thread, 0, sizeof(client_thread));
708
    }
709

    
710
    qemu_set_fd_handler2(fd, nbd_can_accept, nbd_accept, NULL,
711
                         (void *)(uintptr_t)fd);
712

    
713
    /* now when the initialization is (almost) complete, chdir("/")
714
     * to free any busy filesystems */
715
    if (chdir("/") < 0) {
716
        err(EXIT_FAILURE, "Could not chdir to root directory");
717
    }
718

    
719
    state = RUNNING;
720
    do {
721
        main_loop_wait(false);
722
        if (state == TERMINATE) {
723
            state = TERMINATING;
724
            nbd_export_close(exp);
725
            nbd_export_put(exp);
726
            exp = NULL;
727
        }
728
    } while (state != TERMINATED);
729

    
730
    bdrv_close(bs);
731
    if (sockpath) {
732
        unlink(sockpath);
733
    }
734

    
735
    if (sn_opts) {
736
        qemu_opts_del(sn_opts);
737
    }
738

    
739
    if (device) {
740
        void *ret;
741
        pthread_join(client_thread, &ret);
742
        exit(ret != NULL);
743
    } else {
744
        exit(EXIT_SUCCESS);
745
    }
746
}