xref: /qemu/qemu-nbd.c (revision 3b05a8e91bf15e1858f3f3618069dbc78e55b88e)
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
167a5ca864Sbellard  *  along with this program; if not, write to the Free Software
177a5ca864Sbellard  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
187a5ca864Sbellard  */
197a5ca864Sbellard 
207a5ca864Sbellard #include <qemu-common.h>
217a5ca864Sbellard #include "block_int.h"
227a5ca864Sbellard #include "nbd.h"
237a5ca864Sbellard 
247a5ca864Sbellard #include <stdarg.h>
257a5ca864Sbellard #include <stdio.h>
267a5ca864Sbellard #include <getopt.h>
277a5ca864Sbellard #include <err.h>
28cd831bd7Sths #include <sys/types.h>
297a5ca864Sbellard #include <sys/socket.h>
307a5ca864Sbellard #include <netinet/in.h>
317a5ca864Sbellard #include <netinet/tcp.h>
327a5ca864Sbellard #include <arpa/inet.h>
33cd831bd7Sths #include <signal.h>
34cd831bd7Sths 
35cd831bd7Sths #define SOCKET_PATH    "/var/lock/qemu-nbd-%s"
367a5ca864Sbellard 
372f726488Sths #define NBD_BUFFER_SIZE (1024*1024)
382f726488Sths 
397a5ca864Sbellard int verbose;
407a5ca864Sbellard 
417a5ca864Sbellard static void usage(const char *name)
427a5ca864Sbellard {
437a5ca864Sbellard     printf(
447a5ca864Sbellard "Usage: %s [OPTIONS] FILE\n"
457a5ca864Sbellard "QEMU Disk Network Block Device Server\n"
467a5ca864Sbellard "\n"
477a5ca864Sbellard "  -p, --port=PORT      port to listen on (default `1024')\n"
487a5ca864Sbellard "  -o, --offset=OFFSET  offset into the image\n"
497a5ca864Sbellard "  -b, --bind=IFACE     interface to bind to (default `0.0.0.0')\n"
50cd831bd7Sths "  -k, --socket=PATH    path to the unix socket\n"
51cd831bd7Sths "                       (default '"SOCKET_PATH"')\n"
527a5ca864Sbellard "  -r, --read-only      export read-only\n"
537a5ca864Sbellard "  -P, --partition=NUM  only expose partition NUM\n"
542f726488Sths "  -s, --snapshot       use snapshot file\n"
552f726488Sths "  -n, --nocache        disable host cache\n"
56cd831bd7Sths "  -c, --connect=DEV    connect FILE to the local NBD device DEV\n"
57cd831bd7Sths "  -d, --disconnect     disconnect the specified device\n"
58*3b05a8e9Sths "  -e, --shared=NUM     device can be shared by NUM clients (default '1')\n"
597a5ca864Sbellard "  -v, --verbose        display extra debugging information\n"
607a5ca864Sbellard "  -h, --help           display this help and exit\n"
617a5ca864Sbellard "  -V, --version        output version information and exit\n"
627a5ca864Sbellard "\n"
637a5ca864Sbellard "Report bugs to <anthony@codemonkey.ws>\n"
64cd831bd7Sths     , name, "DEVICE");
657a5ca864Sbellard }
667a5ca864Sbellard 
677a5ca864Sbellard static void version(const char *name)
687a5ca864Sbellard {
697a5ca864Sbellard     printf(
707a5ca864Sbellard "qemu-nbd version 0.0.1\n"
717a5ca864Sbellard "Written by Anthony Liguori.\n"
727a5ca864Sbellard "\n"
737a5ca864Sbellard "Copyright (C) 2006 Anthony Liguori <anthony@codemonkey.ws>.\n"
747a5ca864Sbellard "This is free software; see the source for copying conditions.  There is NO\n"
757a5ca864Sbellard "warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n"
767a5ca864Sbellard     );
777a5ca864Sbellard }
787a5ca864Sbellard 
797a5ca864Sbellard struct partition_record
807a5ca864Sbellard {
817a5ca864Sbellard     uint8_t bootable;
827a5ca864Sbellard     uint8_t start_head;
837a5ca864Sbellard     uint32_t start_cylinder;
847a5ca864Sbellard     uint8_t start_sector;
857a5ca864Sbellard     uint8_t system;
867a5ca864Sbellard     uint8_t end_head;
877a5ca864Sbellard     uint8_t end_cylinder;
887a5ca864Sbellard     uint8_t end_sector;
897a5ca864Sbellard     uint32_t start_sector_abs;
907a5ca864Sbellard     uint32_t nb_sectors_abs;
917a5ca864Sbellard };
927a5ca864Sbellard 
937a5ca864Sbellard static void read_partition(uint8_t *p, struct partition_record *r)
947a5ca864Sbellard {
957a5ca864Sbellard     r->bootable = p[0];
967a5ca864Sbellard     r->start_head = p[1];
977a5ca864Sbellard     r->start_cylinder = p[3] | ((p[2] << 2) & 0x0300);
987a5ca864Sbellard     r->start_sector = p[2] & 0x3f;
997a5ca864Sbellard     r->system = p[4];
1007a5ca864Sbellard     r->end_head = p[5];
1017a5ca864Sbellard     r->end_cylinder = p[7] | ((p[6] << 2) & 0x300);
1027a5ca864Sbellard     r->end_sector = p[6] & 0x3f;
1037a5ca864Sbellard     r->start_sector_abs = p[8] | p[9] << 8 | p[10] << 16 | p[11] << 24;
1047a5ca864Sbellard     r->nb_sectors_abs = p[12] | p[13] << 8 | p[14] << 16 | p[15] << 24;
1057a5ca864Sbellard }
1067a5ca864Sbellard 
1077a5ca864Sbellard static int find_partition(BlockDriverState *bs, int partition,
1087a5ca864Sbellard                           off_t *offset, off_t *size)
1097a5ca864Sbellard {
1107a5ca864Sbellard     struct partition_record mbr[4];
1117a5ca864Sbellard     uint8_t data[512];
1127a5ca864Sbellard     int i;
1137a5ca864Sbellard     int ext_partnum = 4;
1147a5ca864Sbellard 
1157a5ca864Sbellard     if (bdrv_read(bs, 0, data, 1))
1167a5ca864Sbellard         errx(EINVAL, "error while reading");
1177a5ca864Sbellard 
1187a5ca864Sbellard     if (data[510] != 0x55 || data[511] != 0xaa) {
1197a5ca864Sbellard         errno = -EINVAL;
1207a5ca864Sbellard         return -1;
1217a5ca864Sbellard     }
1227a5ca864Sbellard 
1237a5ca864Sbellard     for (i = 0; i < 4; i++) {
1247a5ca864Sbellard         read_partition(&data[446 + 16 * i], &mbr[i]);
1257a5ca864Sbellard 
1267a5ca864Sbellard         if (!mbr[i].nb_sectors_abs)
1277a5ca864Sbellard             continue;
1287a5ca864Sbellard 
1297a5ca864Sbellard         if (mbr[i].system == 0xF || mbr[i].system == 0x5) {
1307a5ca864Sbellard             struct partition_record ext[4];
1317a5ca864Sbellard             uint8_t data1[512];
1327a5ca864Sbellard             int j;
1337a5ca864Sbellard 
1347a5ca864Sbellard             if (bdrv_read(bs, mbr[i].start_sector_abs, data1, 1))
1357a5ca864Sbellard                 errx(EINVAL, "error while reading");
1367a5ca864Sbellard 
1377a5ca864Sbellard             for (j = 0; j < 4; j++) {
1387a5ca864Sbellard                 read_partition(&data1[446 + 16 * j], &ext[j]);
1397a5ca864Sbellard                 if (!ext[j].nb_sectors_abs)
1407a5ca864Sbellard                     continue;
1417a5ca864Sbellard 
1427a5ca864Sbellard                 if ((ext_partnum + j + 1) == partition) {
1437a5ca864Sbellard                     *offset = (uint64_t)ext[j].start_sector_abs << 9;
1447a5ca864Sbellard                     *size = (uint64_t)ext[j].nb_sectors_abs << 9;
1457a5ca864Sbellard                     return 0;
1467a5ca864Sbellard                 }
1477a5ca864Sbellard             }
1487a5ca864Sbellard             ext_partnum += 4;
1497a5ca864Sbellard         } else if ((i + 1) == partition) {
1507a5ca864Sbellard             *offset = (uint64_t)mbr[i].start_sector_abs << 9;
1517a5ca864Sbellard             *size = (uint64_t)mbr[i].nb_sectors_abs << 9;
1527a5ca864Sbellard             return 0;
1537a5ca864Sbellard         }
1547a5ca864Sbellard     }
1557a5ca864Sbellard 
1567a5ca864Sbellard     errno = -ENOENT;
1577a5ca864Sbellard     return -1;
1587a5ca864Sbellard }
1597a5ca864Sbellard 
160cd831bd7Sths static void show_parts(const char *device)
161cd831bd7Sths {
162cd831bd7Sths     if (fork() == 0) {
163cd831bd7Sths         int nbd;
164cd831bd7Sths 
165cd831bd7Sths         /* linux just needs an open() to trigger
166cd831bd7Sths          * the partition table update
167cd831bd7Sths          * but remember to load the module with max_part != 0 :
168cd831bd7Sths          *     modprobe nbd max_part=63
169cd831bd7Sths          */
170cd831bd7Sths         nbd = open(device, O_RDWR);
171cd831bd7Sths         if (nbd != -1)
172cd831bd7Sths               close(nbd);
173cd831bd7Sths         exit(0);
174cd831bd7Sths     }
175cd831bd7Sths }
176cd831bd7Sths 
1777a5ca864Sbellard int main(int argc, char **argv)
1787a5ca864Sbellard {
1797a5ca864Sbellard     BlockDriverState *bs;
1807a5ca864Sbellard     off_t dev_offset = 0;
1817a5ca864Sbellard     off_t offset = 0;
1827a5ca864Sbellard     bool readonly = false;
183cd831bd7Sths     bool disconnect = false;
1847a5ca864Sbellard     const char *bindto = "0.0.0.0";
1857a5ca864Sbellard     int port = 1024;
1867a5ca864Sbellard     struct sockaddr_in addr;
1877a5ca864Sbellard     socklen_t addr_len = sizeof(addr);
1887a5ca864Sbellard     off_t fd_size;
189cd831bd7Sths     char *device = NULL;
190cd831bd7Sths     char *socket = NULL;
191cd831bd7Sths     char sockpath[128];
192*3b05a8e9Sths     const char *sopt = "hVbo:p:rsnP:c:dvk:e:";
1937a5ca864Sbellard     struct option lopt[] = {
1947a5ca864Sbellard         { "help", 0, 0, 'h' },
1957a5ca864Sbellard         { "version", 0, 0, 'V' },
1967a5ca864Sbellard         { "bind", 1, 0, 'b' },
1977a5ca864Sbellard         { "port", 1, 0, 'p' },
198cd831bd7Sths         { "socket", 1, 0, 'k' },
1997a5ca864Sbellard         { "offset", 1, 0, 'o' },
2007a5ca864Sbellard         { "read-only", 0, 0, 'r' },
2017a5ca864Sbellard         { "partition", 1, 0, 'P' },
202cd831bd7Sths         { "connect", 1, 0, 'c' },
203cd831bd7Sths         { "disconnect", 0, 0, 'd' },
2047a5ca864Sbellard         { "snapshot", 0, 0, 's' },
2052f726488Sths         { "nocache", 0, 0, 'n' },
206*3b05a8e9Sths         { "shared", 1, 0, 'e' },
2077a5ca864Sbellard         { "verbose", 0, 0, 'v' },
208975b092bSths         { NULL, 0, 0, 0 }
2097a5ca864Sbellard     };
2107a5ca864Sbellard     int ch;
2117a5ca864Sbellard     int opt_ind = 0;
2127a5ca864Sbellard     int li;
2137a5ca864Sbellard     char *end;
2142f726488Sths     int flags = 0;
2157a5ca864Sbellard     int partition = -1;
216cd831bd7Sths     int ret;
217*3b05a8e9Sths     int shared = 1;
2182f726488Sths     uint8_t *data;
219*3b05a8e9Sths     fd_set fds;
220*3b05a8e9Sths     int *sharing_fds;
221*3b05a8e9Sths     int fd;
222*3b05a8e9Sths     int i;
223*3b05a8e9Sths     int nb_fds = 0;
224*3b05a8e9Sths     int max_fd;
2257a5ca864Sbellard 
2267a5ca864Sbellard     while ((ch = getopt_long(argc, argv, sopt, lopt, &opt_ind)) != -1) {
2277a5ca864Sbellard         switch (ch) {
2287a5ca864Sbellard         case 's':
2292f726488Sths             flags |= BDRV_O_SNAPSHOT;
2302f726488Sths             break;
2312f726488Sths         case 'n':
2322f726488Sths             flags |= BDRV_O_DIRECT;
2337a5ca864Sbellard             break;
2347a5ca864Sbellard         case 'b':
2357a5ca864Sbellard             bindto = optarg;
2367a5ca864Sbellard             break;
2377a5ca864Sbellard         case 'p':
2387a5ca864Sbellard             li = strtol(optarg, &end, 0);
2397a5ca864Sbellard             if (*end) {
2407a5ca864Sbellard                 errx(EINVAL, "Invalid port `%s'", optarg);
2417a5ca864Sbellard             }
2427a5ca864Sbellard             if (li < 1 || li > 65535) {
2437a5ca864Sbellard                 errx(EINVAL, "Port out of range `%s'", optarg);
2447a5ca864Sbellard             }
2457a5ca864Sbellard             port = (uint16_t)li;
2467a5ca864Sbellard             break;
2477a5ca864Sbellard         case 'o':
2487a5ca864Sbellard                 dev_offset = strtoll (optarg, &end, 0);
2497a5ca864Sbellard             if (*end) {
2507a5ca864Sbellard                 errx(EINVAL, "Invalid offset `%s'", optarg);
2517a5ca864Sbellard             }
2527a5ca864Sbellard             if (dev_offset < 0) {
2537a5ca864Sbellard                 errx(EINVAL, "Offset must be positive `%s'", optarg);
2547a5ca864Sbellard             }
2557a5ca864Sbellard             break;
2567a5ca864Sbellard         case 'r':
2577a5ca864Sbellard             readonly = true;
2587a5ca864Sbellard             break;
2597a5ca864Sbellard         case 'P':
2607a5ca864Sbellard             partition = strtol(optarg, &end, 0);
2617a5ca864Sbellard             if (*end)
2627a5ca864Sbellard                 errx(EINVAL, "Invalid partition `%s'", optarg);
2637a5ca864Sbellard             if (partition < 1 || partition > 8)
2647a5ca864Sbellard                 errx(EINVAL, "Invalid partition %d", partition);
2657a5ca864Sbellard             break;
266cd831bd7Sths         case 'k':
267cd831bd7Sths             socket = optarg;
268cd831bd7Sths             if (socket[0] != '/')
269cd831bd7Sths                 errx(EINVAL, "socket path must be absolute\n");
270cd831bd7Sths             break;
271cd831bd7Sths         case 'd':
272cd831bd7Sths             disconnect = true;
273cd831bd7Sths             break;
274cd831bd7Sths         case 'c':
275cd831bd7Sths             device = optarg;
276cd831bd7Sths             break;
277*3b05a8e9Sths         case 'e':
278*3b05a8e9Sths             shared = strtol(optarg, &end, 0);
279*3b05a8e9Sths             if (*end) {
280*3b05a8e9Sths                 errx(EINVAL, "Invalid shared device number '%s'", optarg);
281*3b05a8e9Sths             }
282*3b05a8e9Sths             if (shared < 1) {
283*3b05a8e9Sths                 errx(EINVAL, "Shared device number must be greater than 0\n");
284*3b05a8e9Sths             }
285*3b05a8e9Sths             break;
2867a5ca864Sbellard         case 'v':
2877a5ca864Sbellard             verbose = 1;
2887a5ca864Sbellard             break;
2897a5ca864Sbellard         case 'V':
2907a5ca864Sbellard             version(argv[0]);
2917a5ca864Sbellard             exit(0);
2927a5ca864Sbellard             break;
2937a5ca864Sbellard         case 'h':
2947a5ca864Sbellard             usage(argv[0]);
2957a5ca864Sbellard             exit(0);
2967a5ca864Sbellard             break;
2977a5ca864Sbellard         case '?':
2987a5ca864Sbellard             errx(EINVAL, "Try `%s --help' for more information.",
2997a5ca864Sbellard                  argv[0]);
3007a5ca864Sbellard         }
3017a5ca864Sbellard     }
3027a5ca864Sbellard 
3037a5ca864Sbellard     if ((argc - optind) != 1) {
3047a5ca864Sbellard         errx(EINVAL, "Invalid number of argument.\n"
3057a5ca864Sbellard              "Try `%s --help' for more information.",
3067a5ca864Sbellard              argv[0]);
3077a5ca864Sbellard     }
3087a5ca864Sbellard 
309cd831bd7Sths     if (disconnect) {
310cd831bd7Sths         fd = open(argv[optind], O_RDWR);
311cd831bd7Sths         if (fd == -1)
312cd831bd7Sths             errx(errno, "Cannot open %s", argv[optind]);
313cd831bd7Sths 
314cd831bd7Sths         nbd_disconnect(fd);
315cd831bd7Sths 
316cd831bd7Sths         close(fd);
317cd831bd7Sths 
318cd831bd7Sths         printf("%s disconnected\n", argv[optind]);
319cd831bd7Sths 
320cd831bd7Sths 	return 0;
321cd831bd7Sths     }
322cd831bd7Sths 
3237a5ca864Sbellard     bdrv_init();
3247a5ca864Sbellard 
3257a5ca864Sbellard     bs = bdrv_new("hda");
3267a5ca864Sbellard     if (bs == NULL)
3277a5ca864Sbellard         return 1;
3287a5ca864Sbellard 
3292f726488Sths     if (bdrv_open(bs, argv[optind], flags) == -1)
3307a5ca864Sbellard         return 1;
3317a5ca864Sbellard 
3327a5ca864Sbellard     fd_size = bs->total_sectors * 512;
3337a5ca864Sbellard 
3347a5ca864Sbellard     if (partition != -1 &&
3357a5ca864Sbellard         find_partition(bs, partition, &dev_offset, &fd_size))
3367a5ca864Sbellard         errx(errno, "Could not find partition %d", partition);
3377a5ca864Sbellard 
338cd831bd7Sths     if (device) {
339cd831bd7Sths         pid_t pid;
340*3b05a8e9Sths         int sock;
341*3b05a8e9Sths 
342cd831bd7Sths         if (!verbose)
343cd831bd7Sths             daemon(0, 0);	/* detach client and server */
344cd831bd7Sths 
345cd831bd7Sths         if (socket == NULL) {
346cd831bd7Sths             sprintf(sockpath, SOCKET_PATH, basename(device));
347cd831bd7Sths             socket = sockpath;
348cd831bd7Sths         }
349cd831bd7Sths 
350cd831bd7Sths         pid = fork();
351cd831bd7Sths         if (pid < 0)
352cd831bd7Sths             return 1;
353cd831bd7Sths         if (pid != 0) {
354cd831bd7Sths             off_t size;
355cd831bd7Sths             size_t blocksize;
356cd831bd7Sths 
357cd831bd7Sths             ret = 0;
358cd831bd7Sths             bdrv_close(bs);
359cd831bd7Sths 
360cd831bd7Sths             do {
361cd831bd7Sths                 sock = unix_socket_outgoing(socket);
362cd831bd7Sths                 if (sock == -1) {
363cd831bd7Sths                     if (errno != ENOENT && errno != ECONNREFUSED)
364cd831bd7Sths                         goto out;
365cd831bd7Sths                     sleep(1);	/* wait children */
366cd831bd7Sths                 }
367cd831bd7Sths             } while (sock == -1);
368cd831bd7Sths 
369cd831bd7Sths             fd = open(device, O_RDWR);
370cd831bd7Sths             if (fd == -1) {
371cd831bd7Sths                 ret = 1;
372cd831bd7Sths                 goto out;
373cd831bd7Sths             }
374cd831bd7Sths 
375cd831bd7Sths             ret = nbd_receive_negotiate(sock, &size, &blocksize);
376cd831bd7Sths             if (ret == -1) {
377cd831bd7Sths                 ret = 1;
378cd831bd7Sths                 goto out;
379cd831bd7Sths             }
380cd831bd7Sths 
381cd831bd7Sths             ret = nbd_init(fd, sock, size, blocksize);
382cd831bd7Sths             if (ret == -1) {
383cd831bd7Sths                 ret = 1;
384cd831bd7Sths                 goto out;
385cd831bd7Sths             }
386cd831bd7Sths 
387cd831bd7Sths             printf("NBD device %s is now connected to file %s\n",
388cd831bd7Sths                     device, argv[optind]);
389cd831bd7Sths 
390cd831bd7Sths 	    /* update partition table */
391cd831bd7Sths 
392cd831bd7Sths             show_parts(device);
393cd831bd7Sths 
394cd831bd7Sths             nbd_client(fd, sock);
395cd831bd7Sths             close(fd);
396cd831bd7Sths  out:
397cd831bd7Sths             kill(pid, SIGTERM);
398cd831bd7Sths             unlink(socket);
399cd831bd7Sths 
400cd831bd7Sths             return ret;
401cd831bd7Sths         }
402cd831bd7Sths         /* children */
403cd831bd7Sths     }
404cd831bd7Sths 
405*3b05a8e9Sths     sharing_fds = qemu_malloc((shared + 1) * sizeof(int));
406*3b05a8e9Sths     if (sharing_fds == NULL)
407*3b05a8e9Sths         errx(ENOMEM, "Cannot allocate sharing fds");
408*3b05a8e9Sths 
409cd831bd7Sths     if (socket) {
410*3b05a8e9Sths         sharing_fds[0] = unix_socket_incoming(socket);
411cd831bd7Sths     } else {
412*3b05a8e9Sths         sharing_fds[0] = tcp_socket_incoming(bindto, port);
413cd831bd7Sths     }
414cd831bd7Sths 
415*3b05a8e9Sths     if (sharing_fds[0] == -1)
4167a5ca864Sbellard         return 1;
417*3b05a8e9Sths     max_fd = sharing_fds[0];
418*3b05a8e9Sths     nb_fds++;
4197a5ca864Sbellard 
4202f726488Sths     data = qemu_memalign(512, NBD_BUFFER_SIZE);
421*3b05a8e9Sths     if (data == NULL)
422*3b05a8e9Sths         errx(ENOMEM, "Cannot allocate data buffer");
423*3b05a8e9Sths 
424*3b05a8e9Sths     do {
425*3b05a8e9Sths 
426*3b05a8e9Sths         FD_ZERO(&fds);
427*3b05a8e9Sths         for (i = 0; i < nb_fds; i++)
428*3b05a8e9Sths             FD_SET(sharing_fds[i], &fds);
429*3b05a8e9Sths 
430*3b05a8e9Sths         ret = select(max_fd + 1, &fds, NULL, NULL, NULL);
431*3b05a8e9Sths         if (ret == -1)
432*3b05a8e9Sths             break;
433*3b05a8e9Sths 
434*3b05a8e9Sths         if (FD_ISSET(sharing_fds[0], &fds))
435*3b05a8e9Sths             ret--;
436*3b05a8e9Sths         for (i = 1; i < nb_fds && ret; i++) {
437*3b05a8e9Sths             if (FD_ISSET(sharing_fds[i], &fds)) {
438*3b05a8e9Sths                 if (nbd_trip(bs, sharing_fds[i], fd_size, dev_offset,
439*3b05a8e9Sths                     &offset, readonly, data, NBD_BUFFER_SIZE) != 0) {
440*3b05a8e9Sths                     close(sharing_fds[i]);
441*3b05a8e9Sths                     nb_fds--;
442*3b05a8e9Sths                     sharing_fds[i] = sharing_fds[nb_fds];
443*3b05a8e9Sths                     i--;
444*3b05a8e9Sths                 }
445*3b05a8e9Sths                 ret--;
446*3b05a8e9Sths             }
447*3b05a8e9Sths         }
448*3b05a8e9Sths         /* new connection ? */
449*3b05a8e9Sths         if (FD_ISSET(sharing_fds[0], &fds)) {
450*3b05a8e9Sths             if (nb_fds < shared + 1) {
451*3b05a8e9Sths                 sharing_fds[nb_fds] = accept(sharing_fds[0],
452*3b05a8e9Sths                                              (struct sockaddr *)&addr,
453*3b05a8e9Sths                                              &addr_len);
454*3b05a8e9Sths                 if (sharing_fds[nb_fds] != -1 &&
455*3b05a8e9Sths                     nbd_negotiate(bs, sharing_fds[nb_fds], fd_size) != -1) {
456*3b05a8e9Sths                         if (sharing_fds[nb_fds] > max_fd)
457*3b05a8e9Sths                             max_fd = sharing_fds[nb_fds];
458*3b05a8e9Sths                         nb_fds++;
459*3b05a8e9Sths                 }
460*3b05a8e9Sths             }
461*3b05a8e9Sths         }
462*3b05a8e9Sths     } while (nb_fds > 1);
4632f726488Sths     qemu_free(data);
4647a5ca864Sbellard 
465*3b05a8e9Sths     close(sharing_fds[0]);
4667a5ca864Sbellard     bdrv_close(bs);
467*3b05a8e9Sths     qemu_free(sharing_fds);
468cd831bd7Sths     if (socket)
469cd831bd7Sths         unlink(socket);
4707a5ca864Sbellard 
4717a5ca864Sbellard     return 0;
4727a5ca864Sbellard }
473