1cd831bd7Sths /* 27a5ca864Sbellard * Copyright (C) 2005 Anthony Liguori <anthony@codemonkey.ws> 37a5ca864Sbellard * 47a5ca864Sbellard * Network Block Device 57a5ca864Sbellard * 67a5ca864Sbellard * This program is free software; you can redistribute it and/or modify 77a5ca864Sbellard * it under the terms of the GNU General Public License as published by 87a5ca864Sbellard * the Free Software Foundation; under version 2 of the License. 97a5ca864Sbellard * 107a5ca864Sbellard * This program is distributed in the hope that it will be useful, 117a5ca864Sbellard * but WITHOUT ANY WARRANTY; without even the implied warranty of 127a5ca864Sbellard * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 137a5ca864Sbellard * GNU General Public License for more details. 147a5ca864Sbellard * 157a5ca864Sbellard * You should have received a copy of the GNU General Public License 168167ee88SBlue Swirl * along with this program; if not, see <http://www.gnu.org/licenses/>. 177a5ca864Sbellard */ 187a5ca864Sbellard 195a61cb60SStefan Weil #include "qemu-common.h" 207a5ca864Sbellard #include "block_int.h" 217a5ca864Sbellard #include "nbd.h" 227a5ca864Sbellard 237a5ca864Sbellard #include <stdarg.h> 247a5ca864Sbellard #include <stdio.h> 257a5ca864Sbellard #include <getopt.h> 267a5ca864Sbellard #include <err.h> 27cd831bd7Sths #include <sys/types.h> 287a5ca864Sbellard #include <sys/socket.h> 297a5ca864Sbellard #include <netinet/in.h> 307a5ca864Sbellard #include <netinet/tcp.h> 317a5ca864Sbellard #include <arpa/inet.h> 32cd831bd7Sths #include <signal.h> 332bff4b6fSBlue Swirl #include <libgen.h> 34cd831bd7Sths 35cd831bd7Sths #define SOCKET_PATH "/var/lock/qemu-nbd-%s" 367a5ca864Sbellard 372f726488Sths #define NBD_BUFFER_SIZE (1024*1024) 382f726488Sths 39*bb345110SPaolo Bonzini static int sigterm_wfd; 40b1d8e52eSblueswir1 static int verbose; 417a5ca864Sbellard 427a5ca864Sbellard static void usage(const char *name) 437a5ca864Sbellard { 447a5ca864Sbellard printf( 457a5ca864Sbellard "Usage: %s [OPTIONS] FILE\n" 467a5ca864Sbellard "QEMU Disk Network Block Device Server\n" 477a5ca864Sbellard "\n" 48c2e2872bSLaurent Vivier " -p, --port=PORT port to listen on (default `%d')\n" 497a5ca864Sbellard " -o, --offset=OFFSET offset into the image\n" 507a5ca864Sbellard " -b, --bind=IFACE interface to bind to (default `0.0.0.0')\n" 51cd831bd7Sths " -k, --socket=PATH path to the unix socket\n" 52cd831bd7Sths " (default '"SOCKET_PATH"')\n" 537a5ca864Sbellard " -r, --read-only export read-only\n" 547a5ca864Sbellard " -P, --partition=NUM only expose partition NUM\n" 552f726488Sths " -s, --snapshot use snapshot file\n" 562f726488Sths " -n, --nocache disable host cache\n" 57cd831bd7Sths " -c, --connect=DEV connect FILE to the local NBD device DEV\n" 58cd831bd7Sths " -d, --disconnect disconnect the specified device\n" 593b05a8e9Sths " -e, --shared=NUM device can be shared by NUM clients (default '1')\n" 6075818250Sths " -t, --persistent don't exit on the last connection\n" 617a5ca864Sbellard " -v, --verbose display extra debugging information\n" 627a5ca864Sbellard " -h, --help display this help and exit\n" 637a5ca864Sbellard " -V, --version output version information and exit\n" 647a5ca864Sbellard "\n" 657a5ca864Sbellard "Report bugs to <anthony@codemonkey.ws>\n" 66c2e2872bSLaurent Vivier , name, NBD_DEFAULT_PORT, "DEVICE"); 677a5ca864Sbellard } 687a5ca864Sbellard 697a5ca864Sbellard static void version(const char *name) 707a5ca864Sbellard { 717a5ca864Sbellard printf( 72315bc7aaSths "%s version 0.0.1\n" 737a5ca864Sbellard "Written by Anthony Liguori.\n" 747a5ca864Sbellard "\n" 757a5ca864Sbellard "Copyright (C) 2006 Anthony Liguori <anthony@codemonkey.ws>.\n" 767a5ca864Sbellard "This is free software; see the source for copying conditions. There is NO\n" 777a5ca864Sbellard "warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n" 78315bc7aaSths , name); 797a5ca864Sbellard } 807a5ca864Sbellard 817a5ca864Sbellard struct partition_record 827a5ca864Sbellard { 837a5ca864Sbellard uint8_t bootable; 847a5ca864Sbellard uint8_t start_head; 857a5ca864Sbellard uint32_t start_cylinder; 867a5ca864Sbellard uint8_t start_sector; 877a5ca864Sbellard uint8_t system; 887a5ca864Sbellard uint8_t end_head; 897a5ca864Sbellard uint8_t end_cylinder; 907a5ca864Sbellard uint8_t end_sector; 917a5ca864Sbellard uint32_t start_sector_abs; 927a5ca864Sbellard uint32_t nb_sectors_abs; 937a5ca864Sbellard }; 947a5ca864Sbellard 957a5ca864Sbellard static void read_partition(uint8_t *p, struct partition_record *r) 967a5ca864Sbellard { 977a5ca864Sbellard r->bootable = p[0]; 987a5ca864Sbellard r->start_head = p[1]; 997a5ca864Sbellard r->start_cylinder = p[3] | ((p[2] << 2) & 0x0300); 1007a5ca864Sbellard r->start_sector = p[2] & 0x3f; 1017a5ca864Sbellard r->system = p[4]; 1027a5ca864Sbellard r->end_head = p[5]; 1037a5ca864Sbellard r->end_cylinder = p[7] | ((p[6] << 2) & 0x300); 1047a5ca864Sbellard r->end_sector = p[6] & 0x3f; 1057a5ca864Sbellard r->start_sector_abs = p[8] | p[9] << 8 | p[10] << 16 | p[11] << 24; 1067a5ca864Sbellard r->nb_sectors_abs = p[12] | p[13] << 8 | p[14] << 16 | p[15] << 24; 1077a5ca864Sbellard } 1087a5ca864Sbellard 1097a5ca864Sbellard static int find_partition(BlockDriverState *bs, int partition, 1107a5ca864Sbellard off_t *offset, off_t *size) 1117a5ca864Sbellard { 1127a5ca864Sbellard struct partition_record mbr[4]; 1137a5ca864Sbellard uint8_t data[512]; 1147a5ca864Sbellard int i; 1157a5ca864Sbellard int ext_partnum = 4; 116cb7cf0e3SRyota Ozaki int ret; 1177a5ca864Sbellard 118cb7cf0e3SRyota Ozaki if ((ret = bdrv_read(bs, 0, data, 1)) < 0) { 119cb7cf0e3SRyota Ozaki errno = -ret; 120cb7cf0e3SRyota Ozaki err(EXIT_FAILURE, "error while reading"); 121cb7cf0e3SRyota Ozaki } 1227a5ca864Sbellard 1237a5ca864Sbellard if (data[510] != 0x55 || data[511] != 0xaa) { 1247a5ca864Sbellard errno = -EINVAL; 1257a5ca864Sbellard return -1; 1267a5ca864Sbellard } 1277a5ca864Sbellard 1287a5ca864Sbellard for (i = 0; i < 4; i++) { 1297a5ca864Sbellard read_partition(&data[446 + 16 * i], &mbr[i]); 1307a5ca864Sbellard 1317a5ca864Sbellard if (!mbr[i].nb_sectors_abs) 1327a5ca864Sbellard continue; 1337a5ca864Sbellard 1347a5ca864Sbellard if (mbr[i].system == 0xF || mbr[i].system == 0x5) { 1357a5ca864Sbellard struct partition_record ext[4]; 1367a5ca864Sbellard uint8_t data1[512]; 1377a5ca864Sbellard int j; 1387a5ca864Sbellard 139cb7cf0e3SRyota Ozaki if ((ret = bdrv_read(bs, mbr[i].start_sector_abs, data1, 1)) < 0) { 140cb7cf0e3SRyota Ozaki errno = -ret; 141cb7cf0e3SRyota Ozaki err(EXIT_FAILURE, "error while reading"); 142cb7cf0e3SRyota Ozaki } 1437a5ca864Sbellard 1447a5ca864Sbellard for (j = 0; j < 4; j++) { 1457a5ca864Sbellard read_partition(&data1[446 + 16 * j], &ext[j]); 1467a5ca864Sbellard if (!ext[j].nb_sectors_abs) 1477a5ca864Sbellard continue; 1487a5ca864Sbellard 1497a5ca864Sbellard if ((ext_partnum + j + 1) == partition) { 1507a5ca864Sbellard *offset = (uint64_t)ext[j].start_sector_abs << 9; 1517a5ca864Sbellard *size = (uint64_t)ext[j].nb_sectors_abs << 9; 1527a5ca864Sbellard return 0; 1537a5ca864Sbellard } 1547a5ca864Sbellard } 1557a5ca864Sbellard ext_partnum += 4; 1567a5ca864Sbellard } else if ((i + 1) == partition) { 1577a5ca864Sbellard *offset = (uint64_t)mbr[i].start_sector_abs << 9; 1587a5ca864Sbellard *size = (uint64_t)mbr[i].nb_sectors_abs << 9; 1597a5ca864Sbellard return 0; 1607a5ca864Sbellard } 1617a5ca864Sbellard } 1627a5ca864Sbellard 1637a5ca864Sbellard errno = -ENOENT; 1647a5ca864Sbellard return -1; 1657a5ca864Sbellard } 1667a5ca864Sbellard 167*bb345110SPaolo Bonzini static void termsig_handler(int signum) 168*bb345110SPaolo Bonzini { 169*bb345110SPaolo Bonzini static int sigterm_reported; 170*bb345110SPaolo Bonzini if (!sigterm_reported) { 171*bb345110SPaolo Bonzini sigterm_reported = (write(sigterm_wfd, "", 1) == 1); 172*bb345110SPaolo Bonzini } 173*bb345110SPaolo Bonzini } 174*bb345110SPaolo Bonzini 175cd831bd7Sths static void show_parts(const char *device) 176cd831bd7Sths { 177cd831bd7Sths if (fork() == 0) { 178cd831bd7Sths int nbd; 179cd831bd7Sths 180cd831bd7Sths /* linux just needs an open() to trigger 181cd831bd7Sths * the partition table update 182cd831bd7Sths * but remember to load the module with max_part != 0 : 183cd831bd7Sths * modprobe nbd max_part=63 184cd831bd7Sths */ 185cd831bd7Sths nbd = open(device, O_RDWR); 186cd831bd7Sths if (nbd != -1) 187cd831bd7Sths close(nbd); 188cd831bd7Sths exit(0); 189cd831bd7Sths } 190cd831bd7Sths } 191cd831bd7Sths 1927a5ca864Sbellard int main(int argc, char **argv) 1937a5ca864Sbellard { 1947a5ca864Sbellard BlockDriverState *bs; 1957a5ca864Sbellard off_t dev_offset = 0; 1967a5ca864Sbellard off_t offset = 0; 197b90fb4b8SPaolo Bonzini uint32_t nbdflags = 0; 198cd831bd7Sths bool disconnect = false; 1997a5ca864Sbellard const char *bindto = "0.0.0.0"; 200c2e2872bSLaurent Vivier int port = NBD_DEFAULT_PORT; 2017a5ca864Sbellard struct sockaddr_in addr; 2027a5ca864Sbellard socklen_t addr_len = sizeof(addr); 2037a5ca864Sbellard off_t fd_size; 204cd831bd7Sths char *device = NULL; 205cd831bd7Sths char *socket = NULL; 206cd831bd7Sths char sockpath[128]; 207d6aa671fSaliguori const char *sopt = "hVb:o:p:rsnP:c:dvk:e:t"; 2087a5ca864Sbellard struct option lopt[] = { 209660f11beSBlue Swirl { "help", 0, NULL, 'h' }, 210660f11beSBlue Swirl { "version", 0, NULL, 'V' }, 211660f11beSBlue Swirl { "bind", 1, NULL, 'b' }, 212660f11beSBlue Swirl { "port", 1, NULL, 'p' }, 213660f11beSBlue Swirl { "socket", 1, NULL, 'k' }, 214660f11beSBlue Swirl { "offset", 1, NULL, 'o' }, 215660f11beSBlue Swirl { "read-only", 0, NULL, 'r' }, 216660f11beSBlue Swirl { "partition", 1, NULL, 'P' }, 217660f11beSBlue Swirl { "connect", 1, NULL, 'c' }, 218660f11beSBlue Swirl { "disconnect", 0, NULL, 'd' }, 219660f11beSBlue Swirl { "snapshot", 0, NULL, 's' }, 220660f11beSBlue Swirl { "nocache", 0, NULL, 'n' }, 221660f11beSBlue Swirl { "shared", 1, NULL, 'e' }, 222660f11beSBlue Swirl { "persistent", 0, NULL, 't' }, 223660f11beSBlue Swirl { "verbose", 0, NULL, 'v' }, 224660f11beSBlue Swirl { NULL, 0, NULL, 0 } 2257a5ca864Sbellard }; 2267a5ca864Sbellard int ch; 2277a5ca864Sbellard int opt_ind = 0; 2287a5ca864Sbellard int li; 2297a5ca864Sbellard char *end; 230f5edb014SNaphtali Sprei int flags = BDRV_O_RDWR; 2317a5ca864Sbellard int partition = -1; 232cd831bd7Sths int ret; 2333b05a8e9Sths int shared = 1; 2342f726488Sths uint8_t *data; 2353b05a8e9Sths fd_set fds; 2363b05a8e9Sths int *sharing_fds; 2373b05a8e9Sths int fd; 2383b05a8e9Sths int i; 2393b05a8e9Sths int nb_fds = 0; 2403b05a8e9Sths int max_fd; 24175818250Sths int persistent = 0; 2427a5ca864Sbellard 243*bb345110SPaolo Bonzini /* Set up a SIGTERM handler so that we exit with a nice status code. */ 244*bb345110SPaolo Bonzini struct sigaction sa_sigterm; 245*bb345110SPaolo Bonzini int sigterm_fd[2]; 246*bb345110SPaolo Bonzini if (qemu_pipe(sigterm_fd) == -1) { 247*bb345110SPaolo Bonzini err(EXIT_FAILURE, "Error setting up communication pipe"); 248*bb345110SPaolo Bonzini } 249*bb345110SPaolo Bonzini 250*bb345110SPaolo Bonzini sigterm_wfd = sigterm_fd[1]; 251*bb345110SPaolo Bonzini memset(&sa_sigterm, 0, sizeof(sa_sigterm)); 252*bb345110SPaolo Bonzini sa_sigterm.sa_handler = termsig_handler; 253*bb345110SPaolo Bonzini sigaction(SIGTERM, &sa_sigterm, NULL); 254*bb345110SPaolo Bonzini 2557a5ca864Sbellard while ((ch = getopt_long(argc, argv, sopt, lopt, &opt_ind)) != -1) { 2567a5ca864Sbellard switch (ch) { 2577a5ca864Sbellard case 's': 2582f726488Sths flags |= BDRV_O_SNAPSHOT; 2592f726488Sths break; 2602f726488Sths case 'n': 261a6599793SChristoph Hellwig flags |= BDRV_O_NOCACHE | BDRV_O_CACHE_WB; 2627a5ca864Sbellard break; 2637a5ca864Sbellard case 'b': 2647a5ca864Sbellard bindto = optarg; 2657a5ca864Sbellard break; 2667a5ca864Sbellard case 'p': 2677a5ca864Sbellard li = strtol(optarg, &end, 0); 2687a5ca864Sbellard if (*end) { 269b6353beaSRyota Ozaki errx(EXIT_FAILURE, "Invalid port `%s'", optarg); 2707a5ca864Sbellard } 2717a5ca864Sbellard if (li < 1 || li > 65535) { 272b6353beaSRyota Ozaki errx(EXIT_FAILURE, "Port out of range `%s'", optarg); 2737a5ca864Sbellard } 2747a5ca864Sbellard port = (uint16_t)li; 2757a5ca864Sbellard break; 2767a5ca864Sbellard case 'o': 2777a5ca864Sbellard dev_offset = strtoll (optarg, &end, 0); 2787a5ca864Sbellard if (*end) { 279b6353beaSRyota Ozaki errx(EXIT_FAILURE, "Invalid offset `%s'", optarg); 2807a5ca864Sbellard } 2817a5ca864Sbellard if (dev_offset < 0) { 282b6353beaSRyota Ozaki errx(EXIT_FAILURE, "Offset must be positive `%s'", optarg); 2837a5ca864Sbellard } 2847a5ca864Sbellard break; 2857a5ca864Sbellard case 'r': 286b90fb4b8SPaolo Bonzini nbdflags |= NBD_FLAG_READ_ONLY; 28707108b29SNaphtali Sprei flags &= ~BDRV_O_RDWR; 2887a5ca864Sbellard break; 2897a5ca864Sbellard case 'P': 2907a5ca864Sbellard partition = strtol(optarg, &end, 0); 2917a5ca864Sbellard if (*end) 292b6353beaSRyota Ozaki errx(EXIT_FAILURE, "Invalid partition `%s'", optarg); 2937a5ca864Sbellard if (partition < 1 || partition > 8) 294b6353beaSRyota Ozaki errx(EXIT_FAILURE, "Invalid partition %d", partition); 2957a5ca864Sbellard break; 296cd831bd7Sths case 'k': 297cd831bd7Sths socket = optarg; 298cd831bd7Sths if (socket[0] != '/') 299b6353beaSRyota Ozaki errx(EXIT_FAILURE, "socket path must be absolute\n"); 300cd831bd7Sths break; 301cd831bd7Sths case 'd': 302cd831bd7Sths disconnect = true; 303cd831bd7Sths break; 304cd831bd7Sths case 'c': 305cd831bd7Sths device = optarg; 306cd831bd7Sths break; 3073b05a8e9Sths case 'e': 3083b05a8e9Sths shared = strtol(optarg, &end, 0); 3093b05a8e9Sths if (*end) { 310b6353beaSRyota Ozaki errx(EXIT_FAILURE, "Invalid shared device number '%s'", optarg); 3113b05a8e9Sths } 3123b05a8e9Sths if (shared < 1) { 313b6353beaSRyota Ozaki errx(EXIT_FAILURE, "Shared device number must be greater than 0\n"); 3143b05a8e9Sths } 3153b05a8e9Sths break; 31675818250Sths case 't': 31775818250Sths persistent = 1; 31875818250Sths break; 3197a5ca864Sbellard case 'v': 3207a5ca864Sbellard verbose = 1; 3217a5ca864Sbellard break; 3227a5ca864Sbellard case 'V': 3237a5ca864Sbellard version(argv[0]); 3247a5ca864Sbellard exit(0); 3257a5ca864Sbellard break; 3267a5ca864Sbellard case 'h': 3277a5ca864Sbellard usage(argv[0]); 3287a5ca864Sbellard exit(0); 3297a5ca864Sbellard break; 3307a5ca864Sbellard case '?': 331b6353beaSRyota Ozaki errx(EXIT_FAILURE, "Try `%s --help' for more information.", 3327a5ca864Sbellard argv[0]); 3337a5ca864Sbellard } 3347a5ca864Sbellard } 3357a5ca864Sbellard 3367a5ca864Sbellard if ((argc - optind) != 1) { 337b6353beaSRyota Ozaki errx(EXIT_FAILURE, "Invalid number of argument.\n" 3387a5ca864Sbellard "Try `%s --help' for more information.", 3397a5ca864Sbellard argv[0]); 3407a5ca864Sbellard } 3417a5ca864Sbellard 342cd831bd7Sths if (disconnect) { 343cd831bd7Sths fd = open(argv[optind], O_RDWR); 344cd831bd7Sths if (fd == -1) 345cb7cf0e3SRyota Ozaki err(EXIT_FAILURE, "Cannot open %s", argv[optind]); 346cd831bd7Sths 347cd831bd7Sths nbd_disconnect(fd); 348cd831bd7Sths 349cd831bd7Sths close(fd); 350cd831bd7Sths 351cd831bd7Sths printf("%s disconnected\n", argv[optind]); 352cd831bd7Sths 353cd831bd7Sths return 0; 354cd831bd7Sths } 355cd831bd7Sths 3567a5ca864Sbellard bdrv_init(); 3577a5ca864Sbellard 3587a5ca864Sbellard bs = bdrv_new("hda"); 3597a5ca864Sbellard 360cb7cf0e3SRyota Ozaki if ((ret = bdrv_open(bs, argv[optind], flags, NULL)) < 0) { 361cb7cf0e3SRyota Ozaki errno = -ret; 362cb7cf0e3SRyota Ozaki err(EXIT_FAILURE, "Failed to bdrv_open '%s'", argv[optind]); 363cb7cf0e3SRyota Ozaki } 3647a5ca864Sbellard 3657a5ca864Sbellard fd_size = bs->total_sectors * 512; 3667a5ca864Sbellard 3677a5ca864Sbellard if (partition != -1 && 3687a5ca864Sbellard find_partition(bs, partition, &dev_offset, &fd_size)) 369cb7cf0e3SRyota Ozaki err(EXIT_FAILURE, "Could not find partition %d", partition); 3707a5ca864Sbellard 371cd831bd7Sths if (device) { 372cd831bd7Sths pid_t pid; 3733b05a8e9Sths int sock; 3743b05a8e9Sths 375cb7cf0e3SRyota Ozaki /* want to fail before daemonizing */ 376cb7cf0e3SRyota Ozaki if (access(device, R_OK|W_OK) == -1) { 377cb7cf0e3SRyota Ozaki err(EXIT_FAILURE, "Could not access '%s'", device); 378cb7cf0e3SRyota Ozaki } 379cb7cf0e3SRyota Ozaki 380f5de141bSAnthony Liguori if (!verbose) { 381f5de141bSAnthony Liguori /* detach client and server */ 382f97742d0SAlexandre Raymond if (qemu_daemon(0, 0) == -1) { 383cb7cf0e3SRyota Ozaki err(EXIT_FAILURE, "Failed to daemonize"); 384f5de141bSAnthony Liguori } 385f5de141bSAnthony Liguori } 386cd831bd7Sths 387cd831bd7Sths if (socket == NULL) { 38822ff51eeSBlue Swirl snprintf(sockpath, sizeof(sockpath), SOCKET_PATH, 38922ff51eeSBlue Swirl basename(device)); 390cd831bd7Sths socket = sockpath; 391cd831bd7Sths } 392cd831bd7Sths 393cd831bd7Sths pid = fork(); 394cd831bd7Sths if (pid < 0) 395cd831bd7Sths return 1; 396cd831bd7Sths if (pid != 0) { 397cd831bd7Sths off_t size; 398cd831bd7Sths size_t blocksize; 399cd831bd7Sths 400cd831bd7Sths ret = 0; 401cd831bd7Sths bdrv_close(bs); 402cd831bd7Sths 403cd831bd7Sths do { 404cd831bd7Sths sock = unix_socket_outgoing(socket); 405cd831bd7Sths if (sock == -1) { 406cb7cf0e3SRyota Ozaki if (errno != ENOENT && errno != ECONNREFUSED) { 407cb7cf0e3SRyota Ozaki ret = 1; 408cd831bd7Sths goto out; 409cb7cf0e3SRyota Ozaki } 410cd831bd7Sths sleep(1); /* wait children */ 411cd831bd7Sths } 412cd831bd7Sths } while (sock == -1); 413cd831bd7Sths 414cd831bd7Sths fd = open(device, O_RDWR); 415cd831bd7Sths if (fd == -1) { 416cd831bd7Sths ret = 1; 417cd831bd7Sths goto out; 418cd831bd7Sths } 419cd831bd7Sths 4201d45f8b5SLaurent Vivier ret = nbd_receive_negotiate(sock, NULL, &nbdflags, 4211d45f8b5SLaurent Vivier &size, &blocksize); 422cd831bd7Sths if (ret == -1) { 423cd831bd7Sths ret = 1; 424cd831bd7Sths goto out; 425cd831bd7Sths } 426cd831bd7Sths 427b90fb4b8SPaolo Bonzini ret = nbd_init(fd, sock, nbdflags, size, blocksize); 428cd831bd7Sths if (ret == -1) { 429cd831bd7Sths ret = 1; 430cd831bd7Sths goto out; 431cd831bd7Sths } 432cd831bd7Sths 433cd831bd7Sths printf("NBD device %s is now connected to file %s\n", 434cd831bd7Sths device, argv[optind]); 435cd831bd7Sths 436cd831bd7Sths /* update partition table */ 437cd831bd7Sths 438cd831bd7Sths show_parts(device); 439cd831bd7Sths 440e301b13dSJes Sorensen ret = nbd_client(fd); 441e301b13dSJes Sorensen if (ret) { 442e301b13dSJes Sorensen ret = 1; 443e301b13dSJes Sorensen } 444cd831bd7Sths close(fd); 445cd831bd7Sths out: 446cd831bd7Sths kill(pid, SIGTERM); 447cd831bd7Sths 448cd831bd7Sths return ret; 449cd831bd7Sths } 450cd831bd7Sths /* children */ 451cd831bd7Sths } 452cd831bd7Sths 4537267c094SAnthony Liguori sharing_fds = g_malloc((shared + 1) * sizeof(int)); 4543b05a8e9Sths 455cd831bd7Sths if (socket) { 4563b05a8e9Sths sharing_fds[0] = unix_socket_incoming(socket); 457cd831bd7Sths } else { 4583b05a8e9Sths sharing_fds[0] = tcp_socket_incoming(bindto, port); 459cd831bd7Sths } 460cd831bd7Sths 4613b05a8e9Sths if (sharing_fds[0] == -1) 4627a5ca864Sbellard return 1; 4633b05a8e9Sths max_fd = sharing_fds[0]; 4643b05a8e9Sths nb_fds++; 4657a5ca864Sbellard 46672aef731SChristoph Hellwig data = qemu_blockalign(bs, NBD_BUFFER_SIZE); 467*bb345110SPaolo Bonzini if (data == NULL) { 468b6353beaSRyota Ozaki errx(EXIT_FAILURE, "Cannot allocate data buffer"); 469*bb345110SPaolo Bonzini } 4703b05a8e9Sths 4713b05a8e9Sths do { 4723b05a8e9Sths FD_ZERO(&fds); 473*bb345110SPaolo Bonzini FD_SET(sigterm_fd[0], &fds); 4743b05a8e9Sths for (i = 0; i < nb_fds; i++) 4753b05a8e9Sths FD_SET(sharing_fds[i], &fds); 4763b05a8e9Sths 477*bb345110SPaolo Bonzini do { 4783b05a8e9Sths ret = select(max_fd + 1, &fds, NULL, NULL, NULL); 479*bb345110SPaolo Bonzini } while (ret == -1 && errno == EINTR); 480*bb345110SPaolo Bonzini if (ret == -1 || FD_ISSET(sigterm_fd[0], &fds)) { 4813b05a8e9Sths break; 482*bb345110SPaolo Bonzini } 4833b05a8e9Sths 4843b05a8e9Sths if (FD_ISSET(sharing_fds[0], &fds)) 4853b05a8e9Sths ret--; 4863b05a8e9Sths for (i = 1; i < nb_fds && ret; i++) { 4873b05a8e9Sths if (FD_ISSET(sharing_fds[i], &fds)) { 4883b05a8e9Sths if (nbd_trip(bs, sharing_fds[i], fd_size, dev_offset, 489b90fb4b8SPaolo Bonzini &offset, nbdflags, data, NBD_BUFFER_SIZE) != 0) { 4903b05a8e9Sths close(sharing_fds[i]); 4913b05a8e9Sths nb_fds--; 4923b05a8e9Sths sharing_fds[i] = sharing_fds[nb_fds]; 4933b05a8e9Sths i--; 4943b05a8e9Sths } 4953b05a8e9Sths ret--; 4963b05a8e9Sths } 4973b05a8e9Sths } 4983b05a8e9Sths /* new connection ? */ 4993b05a8e9Sths if (FD_ISSET(sharing_fds[0], &fds)) { 5003b05a8e9Sths if (nb_fds < shared + 1) { 5013b05a8e9Sths sharing_fds[nb_fds] = accept(sharing_fds[0], 5023b05a8e9Sths (struct sockaddr *)&addr, 5033b05a8e9Sths &addr_len); 5043b05a8e9Sths if (sharing_fds[nb_fds] != -1 && 505b90fb4b8SPaolo Bonzini nbd_negotiate(sharing_fds[nb_fds], fd_size, nbdflags) != -1) { 5063b05a8e9Sths if (sharing_fds[nb_fds] > max_fd) 5073b05a8e9Sths max_fd = sharing_fds[nb_fds]; 5083b05a8e9Sths nb_fds++; 5093b05a8e9Sths } 5103b05a8e9Sths } 5113b05a8e9Sths } 51275818250Sths } while (persistent || nb_fds > 1); 513f8a83245SHerve Poussineau qemu_vfree(data); 5147a5ca864Sbellard 5153b05a8e9Sths close(sharing_fds[0]); 5167a5ca864Sbellard bdrv_close(bs); 5177267c094SAnthony Liguori g_free(sharing_fds); 518cd831bd7Sths if (socket) 519cd831bd7Sths unlink(socket); 5207a5ca864Sbellard 5217a5ca864Sbellard return 0; 5227a5ca864Sbellard } 523