blob: d997bb06cabdab66f1137318fd1a3030a8019ba6 [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>
thscd831bd2008-07-03 10:23:51 +000034
35#define SOCKET_PATH "/var/lock/qemu-nbd-%s"
bellard7a5ca862008-05-27 21:13:40 +000036
ths2f726482008-07-03 11:47:46 +000037#define NBD_BUFFER_SIZE (1024*1024)
38
Paolo Bonzinibb345112011-11-04 15:51:19 +010039static int sigterm_wfd;
blueswir1b1d8e522008-10-26 13:43:07 +000040static int verbose;
bellard7a5ca862008-05-27 21:13:40 +000041
42static void usage(const char *name)
43{
44 printf(
45"Usage: %s [OPTIONS] FILE\n"
46"QEMU Disk Network Block Device Server\n"
47"\n"
Laurent Vivierc2e28722010-09-17 20:37:25 +020048" -p, --port=PORT port to listen on (default `%d')\n"
bellard7a5ca862008-05-27 21:13:40 +000049" -o, --offset=OFFSET offset into the image\n"
50" -b, --bind=IFACE interface to bind to (default `0.0.0.0')\n"
thscd831bd2008-07-03 10:23:51 +000051" -k, --socket=PATH path to the unix socket\n"
52" (default '"SOCKET_PATH"')\n"
bellard7a5ca862008-05-27 21:13:40 +000053" -r, --read-only export read-only\n"
54" -P, --partition=NUM only expose partition NUM\n"
ths2f726482008-07-03 11:47:46 +000055" -s, --snapshot use snapshot file\n"
56" -n, --nocache disable host cache\n"
thscd831bd2008-07-03 10:23:51 +000057" -c, --connect=DEV connect FILE to the local NBD device DEV\n"
58" -d, --disconnect disconnect the specified device\n"
ths3b05a8e2008-07-03 12:45:02 +000059" -e, --shared=NUM device can be shared by NUM clients (default '1')\n"
ths75818252008-07-03 13:41:03 +000060" -t, --persistent don't exit on the last connection\n"
bellard7a5ca862008-05-27 21:13:40 +000061" -v, --verbose display extra debugging information\n"
62" -h, --help display this help and exit\n"
63" -V, --version output version information and exit\n"
64"\n"
65"Report bugs to <anthony@codemonkey.ws>\n"
Laurent Vivierc2e28722010-09-17 20:37:25 +020066 , name, NBD_DEFAULT_PORT, "DEVICE");
bellard7a5ca862008-05-27 21:13:40 +000067}
68
69static void version(const char *name)
70{
71 printf(
ths315bc7a2008-07-18 18:06:23 +000072"%s version 0.0.1\n"
bellard7a5ca862008-05-27 21:13:40 +000073"Written by Anthony Liguori.\n"
74"\n"
75"Copyright (C) 2006 Anthony Liguori <anthony@codemonkey.ws>.\n"
76"This is free software; see the source for copying conditions. There is NO\n"
77"warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n"
ths315bc7a2008-07-18 18:06:23 +000078 , name);
bellard7a5ca862008-05-27 21:13:40 +000079}
80
81struct partition_record
82{
83 uint8_t bootable;
84 uint8_t start_head;
85 uint32_t start_cylinder;
86 uint8_t start_sector;
87 uint8_t system;
88 uint8_t end_head;
89 uint8_t end_cylinder;
90 uint8_t end_sector;
91 uint32_t start_sector_abs;
92 uint32_t nb_sectors_abs;
93};
94
95static void read_partition(uint8_t *p, struct partition_record *r)
96{
97 r->bootable = p[0];
98 r->start_head = p[1];
99 r->start_cylinder = p[3] | ((p[2] << 2) & 0x0300);
100 r->start_sector = p[2] & 0x3f;
101 r->system = p[4];
102 r->end_head = p[5];
103 r->end_cylinder = p[7] | ((p[6] << 2) & 0x300);
104 r->end_sector = p[6] & 0x3f;
105 r->start_sector_abs = p[8] | p[9] << 8 | p[10] << 16 | p[11] << 24;
106 r->nb_sectors_abs = p[12] | p[13] << 8 | p[14] << 16 | p[15] << 24;
107}
108
109static int find_partition(BlockDriverState *bs, int partition,
110 off_t *offset, off_t *size)
111{
112 struct partition_record mbr[4];
113 uint8_t data[512];
114 int i;
115 int ext_partnum = 4;
Ryota Ozakicb7cf0e2010-05-03 06:50:25 +0900116 int ret;
bellard7a5ca862008-05-27 21:13:40 +0000117
Ryota Ozakicb7cf0e2010-05-03 06:50:25 +0900118 if ((ret = bdrv_read(bs, 0, data, 1)) < 0) {
119 errno = -ret;
120 err(EXIT_FAILURE, "error while reading");
121 }
bellard7a5ca862008-05-27 21:13:40 +0000122
123 if (data[510] != 0x55 || data[511] != 0xaa) {
124 errno = -EINVAL;
125 return -1;
126 }
127
128 for (i = 0; i < 4; i++) {
129 read_partition(&data[446 + 16 * i], &mbr[i]);
130
131 if (!mbr[i].nb_sectors_abs)
132 continue;
133
134 if (mbr[i].system == 0xF || mbr[i].system == 0x5) {
135 struct partition_record ext[4];
136 uint8_t data1[512];
137 int j;
138
Ryota Ozakicb7cf0e2010-05-03 06:50:25 +0900139 if ((ret = bdrv_read(bs, mbr[i].start_sector_abs, data1, 1)) < 0) {
140 errno = -ret;
141 err(EXIT_FAILURE, "error while reading");
142 }
bellard7a5ca862008-05-27 21:13:40 +0000143
144 for (j = 0; j < 4; j++) {
145 read_partition(&data1[446 + 16 * j], &ext[j]);
146 if (!ext[j].nb_sectors_abs)
147 continue;
148
149 if ((ext_partnum + j + 1) == partition) {
150 *offset = (uint64_t)ext[j].start_sector_abs << 9;
151 *size = (uint64_t)ext[j].nb_sectors_abs << 9;
152 return 0;
153 }
154 }
155 ext_partnum += 4;
156 } else if ((i + 1) == partition) {
157 *offset = (uint64_t)mbr[i].start_sector_abs << 9;
158 *size = (uint64_t)mbr[i].nb_sectors_abs << 9;
159 return 0;
160 }
161 }
162
163 errno = -ENOENT;
164 return -1;
165}
166
Paolo Bonzinibb345112011-11-04 15:51:19 +0100167static void termsig_handler(int signum)
168{
169 static int sigterm_reported;
170 if (!sigterm_reported) {
171 sigterm_reported = (write(sigterm_wfd, "", 1) == 1);
172 }
173}
174
thscd831bd2008-07-03 10:23:51 +0000175static void show_parts(const char *device)
176{
177 if (fork() == 0) {
178 int nbd;
179
180 /* linux just needs an open() to trigger
181 * the partition table update
182 * but remember to load the module with max_part != 0 :
183 * modprobe nbd max_part=63
184 */
185 nbd = open(device, O_RDWR);
186 if (nbd != -1)
187 close(nbd);
188 exit(0);
189 }
190}
191
bellard7a5ca862008-05-27 21:13:40 +0000192int main(int argc, char **argv)
193{
194 BlockDriverState *bs;
195 off_t dev_offset = 0;
196 off_t offset = 0;
Paolo Bonzinib90fb4b2011-09-08 17:24:54 +0200197 uint32_t nbdflags = 0;
thscd831bd2008-07-03 10:23:51 +0000198 bool disconnect = false;
bellard7a5ca862008-05-27 21:13:40 +0000199 const char *bindto = "0.0.0.0";
Laurent Vivierc2e28722010-09-17 20:37:25 +0200200 int port = NBD_DEFAULT_PORT;
bellard7a5ca862008-05-27 21:13:40 +0000201 struct sockaddr_in addr;
202 socklen_t addr_len = sizeof(addr);
203 off_t fd_size;
thscd831bd2008-07-03 10:23:51 +0000204 char *device = NULL;
205 char *socket = NULL;
206 char sockpath[128];
aliguorid6aa6712009-01-08 19:34:35 +0000207 const char *sopt = "hVb:o:p:rsnP:c:dvk:e:t";
bellard7a5ca862008-05-27 21:13:40 +0000208 struct option lopt[] = {
Blue Swirl660f11b2009-07-31 21:16:51 +0000209 { "help", 0, NULL, 'h' },
210 { "version", 0, NULL, 'V' },
211 { "bind", 1, NULL, 'b' },
212 { "port", 1, NULL, 'p' },
213 { "socket", 1, NULL, 'k' },
214 { "offset", 1, NULL, 'o' },
215 { "read-only", 0, NULL, 'r' },
216 { "partition", 1, NULL, 'P' },
217 { "connect", 1, NULL, 'c' },
218 { "disconnect", 0, NULL, 'd' },
219 { "snapshot", 0, NULL, 's' },
220 { "nocache", 0, NULL, 'n' },
221 { "shared", 1, NULL, 'e' },
222 { "persistent", 0, NULL, 't' },
223 { "verbose", 0, NULL, 'v' },
224 { NULL, 0, NULL, 0 }
bellard7a5ca862008-05-27 21:13:40 +0000225 };
226 int ch;
227 int opt_ind = 0;
228 int li;
229 char *end;
Naphtali Spreif5edb012010-01-17 16:48:13 +0200230 int flags = BDRV_O_RDWR;
bellard7a5ca862008-05-27 21:13:40 +0000231 int partition = -1;
thscd831bd2008-07-03 10:23:51 +0000232 int ret;
ths3b05a8e2008-07-03 12:45:02 +0000233 int shared = 1;
ths2f726482008-07-03 11:47:46 +0000234 uint8_t *data;
ths3b05a8e2008-07-03 12:45:02 +0000235 fd_set fds;
236 int *sharing_fds;
237 int fd;
238 int i;
239 int nb_fds = 0;
240 int max_fd;
ths75818252008-07-03 13:41:03 +0000241 int persistent = 0;
bellard7a5ca862008-05-27 21:13:40 +0000242
Paolo Bonzinibb345112011-11-04 15:51:19 +0100243 /* Set up a SIGTERM handler so that we exit with a nice status code. */
244 struct sigaction sa_sigterm;
245 int sigterm_fd[2];
246 if (qemu_pipe(sigterm_fd) == -1) {
247 err(EXIT_FAILURE, "Error setting up communication pipe");
248 }
249
250 sigterm_wfd = sigterm_fd[1];
251 memset(&sa_sigterm, 0, sizeof(sa_sigterm));
252 sa_sigterm.sa_handler = termsig_handler;
253 sigaction(SIGTERM, &sa_sigterm, NULL);
254
bellard7a5ca862008-05-27 21:13:40 +0000255 while ((ch = getopt_long(argc, argv, sopt, lopt, &opt_ind)) != -1) {
256 switch (ch) {
257 case 's':
ths2f726482008-07-03 11:47:46 +0000258 flags |= BDRV_O_SNAPSHOT;
259 break;
260 case 'n':
Christoph Hellwiga6599792011-05-17 18:04:06 +0200261 flags |= BDRV_O_NOCACHE | BDRV_O_CACHE_WB;
bellard7a5ca862008-05-27 21:13:40 +0000262 break;
263 case 'b':
264 bindto = optarg;
265 break;
266 case 'p':
267 li = strtol(optarg, &end, 0);
268 if (*end) {
Ryota Ozakib6353be2010-03-20 15:23:23 +0900269 errx(EXIT_FAILURE, "Invalid port `%s'", optarg);
bellard7a5ca862008-05-27 21:13:40 +0000270 }
271 if (li < 1 || li > 65535) {
Ryota Ozakib6353be2010-03-20 15:23:23 +0900272 errx(EXIT_FAILURE, "Port out of range `%s'", optarg);
bellard7a5ca862008-05-27 21:13:40 +0000273 }
274 port = (uint16_t)li;
275 break;
276 case 'o':
277 dev_offset = strtoll (optarg, &end, 0);
278 if (*end) {
Ryota Ozakib6353be2010-03-20 15:23:23 +0900279 errx(EXIT_FAILURE, "Invalid offset `%s'", optarg);
bellard7a5ca862008-05-27 21:13:40 +0000280 }
281 if (dev_offset < 0) {
Ryota Ozakib6353be2010-03-20 15:23:23 +0900282 errx(EXIT_FAILURE, "Offset must be positive `%s'", optarg);
bellard7a5ca862008-05-27 21:13:40 +0000283 }
284 break;
285 case 'r':
Paolo Bonzinib90fb4b2011-09-08 17:24:54 +0200286 nbdflags |= NBD_FLAG_READ_ONLY;
Naphtali Sprei07108b22010-03-14 15:19:57 +0200287 flags &= ~BDRV_O_RDWR;
bellard7a5ca862008-05-27 21:13:40 +0000288 break;
289 case 'P':
290 partition = strtol(optarg, &end, 0);
291 if (*end)
Ryota Ozakib6353be2010-03-20 15:23:23 +0900292 errx(EXIT_FAILURE, "Invalid partition `%s'", optarg);
bellard7a5ca862008-05-27 21:13:40 +0000293 if (partition < 1 || partition > 8)
Ryota Ozakib6353be2010-03-20 15:23:23 +0900294 errx(EXIT_FAILURE, "Invalid partition %d", partition);
bellard7a5ca862008-05-27 21:13:40 +0000295 break;
thscd831bd2008-07-03 10:23:51 +0000296 case 'k':
297 socket = optarg;
298 if (socket[0] != '/')
Ryota Ozakib6353be2010-03-20 15:23:23 +0900299 errx(EXIT_FAILURE, "socket path must be absolute\n");
thscd831bd2008-07-03 10:23:51 +0000300 break;
301 case 'd':
302 disconnect = true;
303 break;
304 case 'c':
305 device = optarg;
306 break;
ths3b05a8e2008-07-03 12:45:02 +0000307 case 'e':
308 shared = strtol(optarg, &end, 0);
309 if (*end) {
Ryota Ozakib6353be2010-03-20 15:23:23 +0900310 errx(EXIT_FAILURE, "Invalid shared device number '%s'", optarg);
ths3b05a8e2008-07-03 12:45:02 +0000311 }
312 if (shared < 1) {
Ryota Ozakib6353be2010-03-20 15:23:23 +0900313 errx(EXIT_FAILURE, "Shared device number must be greater than 0\n");
ths3b05a8e2008-07-03 12:45:02 +0000314 }
315 break;
ths75818252008-07-03 13:41:03 +0000316 case 't':
317 persistent = 1;
318 break;
bellard7a5ca862008-05-27 21:13:40 +0000319 case 'v':
320 verbose = 1;
321 break;
322 case 'V':
323 version(argv[0]);
324 exit(0);
325 break;
326 case 'h':
327 usage(argv[0]);
328 exit(0);
329 break;
330 case '?':
Ryota Ozakib6353be2010-03-20 15:23:23 +0900331 errx(EXIT_FAILURE, "Try `%s --help' for more information.",
bellard7a5ca862008-05-27 21:13:40 +0000332 argv[0]);
333 }
334 }
335
336 if ((argc - optind) != 1) {
Ryota Ozakib6353be2010-03-20 15:23:23 +0900337 errx(EXIT_FAILURE, "Invalid number of argument.\n"
bellard7a5ca862008-05-27 21:13:40 +0000338 "Try `%s --help' for more information.",
339 argv[0]);
340 }
341
thscd831bd2008-07-03 10:23:51 +0000342 if (disconnect) {
343 fd = open(argv[optind], O_RDWR);
344 if (fd == -1)
Ryota Ozakicb7cf0e2010-05-03 06:50:25 +0900345 err(EXIT_FAILURE, "Cannot open %s", argv[optind]);
thscd831bd2008-07-03 10:23:51 +0000346
347 nbd_disconnect(fd);
348
349 close(fd);
350
351 printf("%s disconnected\n", argv[optind]);
352
353 return 0;
354 }
355
bellard7a5ca862008-05-27 21:13:40 +0000356 bdrv_init();
357
358 bs = bdrv_new("hda");
bellard7a5ca862008-05-27 21:13:40 +0000359
Ryota Ozakicb7cf0e2010-05-03 06:50:25 +0900360 if ((ret = bdrv_open(bs, argv[optind], flags, NULL)) < 0) {
361 errno = -ret;
362 err(EXIT_FAILURE, "Failed to bdrv_open '%s'", argv[optind]);
363 }
bellard7a5ca862008-05-27 21:13:40 +0000364
365 fd_size = bs->total_sectors * 512;
366
367 if (partition != -1 &&
368 find_partition(bs, partition, &dev_offset, &fd_size))
Ryota Ozakicb7cf0e2010-05-03 06:50:25 +0900369 err(EXIT_FAILURE, "Could not find partition %d", partition);
bellard7a5ca862008-05-27 21:13:40 +0000370
thscd831bd2008-07-03 10:23:51 +0000371 if (device) {
ths3b05a8e2008-07-03 12:45:02 +0000372 pid_t pid;
373 int sock;
374
Ryota Ozakicb7cf0e2010-05-03 06:50:25 +0900375 /* want to fail before daemonizing */
376 if (access(device, R_OK|W_OK) == -1) {
377 err(EXIT_FAILURE, "Could not access '%s'", device);
378 }
379
Anthony Liguorif5de1412009-06-15 12:49:59 -0500380 if (!verbose) {
381 /* detach client and server */
Alexandre Raymondf97742d2011-06-06 23:34:10 -0400382 if (qemu_daemon(0, 0) == -1) {
Ryota Ozakicb7cf0e2010-05-03 06:50:25 +0900383 err(EXIT_FAILURE, "Failed to daemonize");
Anthony Liguorif5de1412009-06-15 12:49:59 -0500384 }
385 }
thscd831bd2008-07-03 10:23:51 +0000386
387 if (socket == NULL) {
Blue Swirl22ff51e2009-12-23 15:45:30 +0000388 snprintf(sockpath, sizeof(sockpath), SOCKET_PATH,
389 basename(device));
thscd831bd2008-07-03 10:23:51 +0000390 socket = sockpath;
391 }
392
393 pid = fork();
394 if (pid < 0)
395 return 1;
396 if (pid != 0) {
397 off_t size;
398 size_t blocksize;
399
400 ret = 0;
401 bdrv_close(bs);
402
403 do {
404 sock = unix_socket_outgoing(socket);
405 if (sock == -1) {
Ryota Ozakicb7cf0e2010-05-03 06:50:25 +0900406 if (errno != ENOENT && errno != ECONNREFUSED) {
407 ret = 1;
thscd831bd2008-07-03 10:23:51 +0000408 goto out;
Ryota Ozakicb7cf0e2010-05-03 06:50:25 +0900409 }
thscd831bd2008-07-03 10:23:51 +0000410 sleep(1); /* wait children */
411 }
412 } while (sock == -1);
413
414 fd = open(device, O_RDWR);
415 if (fd == -1) {
416 ret = 1;
417 goto out;
418 }
419
Laurent Vivier1d45f8b2010-08-25 22:48:33 +0200420 ret = nbd_receive_negotiate(sock, NULL, &nbdflags,
Paolo Bonzinib90fb4b2011-09-08 17:24:54 +0200421 &size, &blocksize);
thscd831bd2008-07-03 10:23:51 +0000422 if (ret == -1) {
423 ret = 1;
424 goto out;
425 }
426
Paolo Bonzinib90fb4b2011-09-08 17:24:54 +0200427 ret = nbd_init(fd, sock, nbdflags, size, blocksize);
thscd831bd2008-07-03 10:23:51 +0000428 if (ret == -1) {
429 ret = 1;
430 goto out;
431 }
432
433 printf("NBD device %s is now connected to file %s\n",
434 device, argv[optind]);
435
436 /* update partition table */
437
438 show_parts(device);
439
Jes Sorensene301b132010-08-31 09:30:34 +0200440 ret = nbd_client(fd);
441 if (ret) {
442 ret = 1;
443 }
thscd831bd2008-07-03 10:23:51 +0000444 close(fd);
445 out:
446 kill(pid, SIGTERM);
thscd831bd2008-07-03 10:23:51 +0000447
448 return ret;
449 }
450 /* children */
451 }
452
Anthony Liguori7267c092011-08-20 22:09:37 -0500453 sharing_fds = g_malloc((shared + 1) * sizeof(int));
ths3b05a8e2008-07-03 12:45:02 +0000454
thscd831bd2008-07-03 10:23:51 +0000455 if (socket) {
ths3b05a8e2008-07-03 12:45:02 +0000456 sharing_fds[0] = unix_socket_incoming(socket);
thscd831bd2008-07-03 10:23:51 +0000457 } else {
ths3b05a8e2008-07-03 12:45:02 +0000458 sharing_fds[0] = tcp_socket_incoming(bindto, port);
thscd831bd2008-07-03 10:23:51 +0000459 }
460
ths3b05a8e2008-07-03 12:45:02 +0000461 if (sharing_fds[0] == -1)
bellard7a5ca862008-05-27 21:13:40 +0000462 return 1;
ths3b05a8e2008-07-03 12:45:02 +0000463 max_fd = sharing_fds[0];
464 nb_fds++;
bellard7a5ca862008-05-27 21:13:40 +0000465
Christoph Hellwig72aef732010-09-12 23:42:56 +0200466 data = qemu_blockalign(bs, NBD_BUFFER_SIZE);
Paolo Bonzinibb345112011-11-04 15:51:19 +0100467 if (data == NULL) {
Ryota Ozakib6353be2010-03-20 15:23:23 +0900468 errx(EXIT_FAILURE, "Cannot allocate data buffer");
Paolo Bonzinibb345112011-11-04 15:51:19 +0100469 }
ths3b05a8e2008-07-03 12:45:02 +0000470
471 do {
ths3b05a8e2008-07-03 12:45:02 +0000472 FD_ZERO(&fds);
Paolo Bonzinibb345112011-11-04 15:51:19 +0100473 FD_SET(sigterm_fd[0], &fds);
ths3b05a8e2008-07-03 12:45:02 +0000474 for (i = 0; i < nb_fds; i++)
475 FD_SET(sharing_fds[i], &fds);
476
Paolo Bonzinibb345112011-11-04 15:51:19 +0100477 do {
478 ret = select(max_fd + 1, &fds, NULL, NULL, NULL);
479 } while (ret == -1 && errno == EINTR);
480 if (ret == -1 || FD_ISSET(sigterm_fd[0], &fds)) {
ths3b05a8e2008-07-03 12:45:02 +0000481 break;
Paolo Bonzinibb345112011-11-04 15:51:19 +0100482 }
ths3b05a8e2008-07-03 12:45:02 +0000483
484 if (FD_ISSET(sharing_fds[0], &fds))
485 ret--;
486 for (i = 1; i < nb_fds && ret; i++) {
487 if (FD_ISSET(sharing_fds[i], &fds)) {
488 if (nbd_trip(bs, sharing_fds[i], fd_size, dev_offset,
Paolo Bonzinib90fb4b2011-09-08 17:24:54 +0200489 &offset, nbdflags, data, NBD_BUFFER_SIZE) != 0) {
ths3b05a8e2008-07-03 12:45:02 +0000490 close(sharing_fds[i]);
491 nb_fds--;
492 sharing_fds[i] = sharing_fds[nb_fds];
493 i--;
494 }
495 ret--;
496 }
497 }
498 /* new connection ? */
499 if (FD_ISSET(sharing_fds[0], &fds)) {
500 if (nb_fds < shared + 1) {
501 sharing_fds[nb_fds] = accept(sharing_fds[0],
502 (struct sockaddr *)&addr,
503 &addr_len);
504 if (sharing_fds[nb_fds] != -1 &&
Paolo Bonzinib90fb4b2011-09-08 17:24:54 +0200505 nbd_negotiate(sharing_fds[nb_fds], fd_size, nbdflags) != -1) {
ths3b05a8e2008-07-03 12:45:02 +0000506 if (sharing_fds[nb_fds] > max_fd)
507 max_fd = sharing_fds[nb_fds];
508 nb_fds++;
509 }
510 }
511 }
ths75818252008-07-03 13:41:03 +0000512 } while (persistent || nb_fds > 1);
Herve Poussineauf8a83242010-01-24 21:23:56 +0000513 qemu_vfree(data);
bellard7a5ca862008-05-27 21:13:40 +0000514
ths3b05a8e2008-07-03 12:45:02 +0000515 close(sharing_fds[0]);
bellard7a5ca862008-05-27 21:13:40 +0000516 bdrv_close(bs);
Anthony Liguori7267c092011-08-20 22:09:37 -0500517 g_free(sharing_fds);
thscd831bd2008-07-03 10:23:51 +0000518 if (socket)
519 unlink(socket);
bellard7a5ca862008-05-27 21:13:40 +0000520
521 return 0;
522}