Statistics
| Branch: | Revision:

root / qemu-nbd.c @ a74cdab4

History | View | Annotate | Download (13.7 kB)

1 cd831bd7 ths
/*
2 7a5ca864 bellard
 *  Copyright (C) 2005  Anthony Liguori <anthony@codemonkey.ws>
3 7a5ca864 bellard
 *
4 7a5ca864 bellard
 *  Network Block Device
5 7a5ca864 bellard
 *
6 7a5ca864 bellard
 *  This program is free software; you can redistribute it and/or modify
7 7a5ca864 bellard
 *  it under the terms of the GNU General Public License as published by
8 7a5ca864 bellard
 *  the Free Software Foundation; under version 2 of the License.
9 7a5ca864 bellard
 *
10 7a5ca864 bellard
 *  This program is distributed in the hope that it will be useful,
11 7a5ca864 bellard
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12 7a5ca864 bellard
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 7a5ca864 bellard
 *  GNU General Public License for more details.
14 7a5ca864 bellard
 *
15 7a5ca864 bellard
 *  You should have received a copy of the GNU General Public License
16 8167ee88 Blue Swirl
 *  along with this program; if not, see <http://www.gnu.org/licenses/>.
17 7a5ca864 bellard
 */
18 7a5ca864 bellard
19 7a5ca864 bellard
#include <qemu-common.h>
20 7a5ca864 bellard
#include "block_int.h"
21 7a5ca864 bellard
#include "nbd.h"
22 7a5ca864 bellard
23 7a5ca864 bellard
#include <stdarg.h>
24 7a5ca864 bellard
#include <stdio.h>
25 7a5ca864 bellard
#include <getopt.h>
26 7a5ca864 bellard
#include <err.h>
27 cd831bd7 ths
#include <sys/types.h>
28 7a5ca864 bellard
#include <sys/socket.h>
29 7a5ca864 bellard
#include <netinet/in.h>
30 7a5ca864 bellard
#include <netinet/tcp.h>
31 7a5ca864 bellard
#include <arpa/inet.h>
32 cd831bd7 ths
#include <signal.h>
33 2bff4b6f Blue Swirl
#include <libgen.h>
34 cd831bd7 ths
35 cd831bd7 ths
#define SOCKET_PATH    "/var/lock/qemu-nbd-%s"
36 7a5ca864 bellard
37 2f726488 ths
#define NBD_BUFFER_SIZE (1024*1024)
38 2f726488 ths
39 b1d8e52e blueswir1
static int verbose;
40 7a5ca864 bellard
41 7a5ca864 bellard
static void usage(const char *name)
42 7a5ca864 bellard
{
43 7a5ca864 bellard
    printf(
44 7a5ca864 bellard
"Usage: %s [OPTIONS] FILE\n"
45 7a5ca864 bellard
"QEMU Disk Network Block Device Server\n"
46 7a5ca864 bellard
"\n"
47 c2e2872b Laurent Vivier
"  -p, --port=PORT      port to listen on (default `%d')\n"
48 7a5ca864 bellard
"  -o, --offset=OFFSET  offset into the image\n"
49 7a5ca864 bellard
"  -b, --bind=IFACE     interface to bind to (default `0.0.0.0')\n"
50 cd831bd7 ths
"  -k, --socket=PATH    path to the unix socket\n"
51 cd831bd7 ths
"                       (default '"SOCKET_PATH"')\n"
52 7a5ca864 bellard
"  -r, --read-only      export read-only\n"
53 7a5ca864 bellard
"  -P, --partition=NUM  only expose partition NUM\n"
54 2f726488 ths
"  -s, --snapshot       use snapshot file\n"
55 2f726488 ths
"  -n, --nocache        disable host cache\n"
56 cd831bd7 ths
"  -c, --connect=DEV    connect FILE to the local NBD device DEV\n"
57 cd831bd7 ths
"  -d, --disconnect     disconnect the specified device\n"
58 3b05a8e9 ths
"  -e, --shared=NUM     device can be shared by NUM clients (default '1')\n"
59 75818250 ths
"  -t, --persistent     don't exit on the last connection\n"
60 7a5ca864 bellard
"  -v, --verbose        display extra debugging information\n"
61 7a5ca864 bellard
"  -h, --help           display this help and exit\n"
62 7a5ca864 bellard
"  -V, --version        output version information and exit\n"
63 7a5ca864 bellard
"\n"
64 7a5ca864 bellard
"Report bugs to <anthony@codemonkey.ws>\n"
65 c2e2872b Laurent Vivier
    , name, NBD_DEFAULT_PORT, "DEVICE");
66 7a5ca864 bellard
}
67 7a5ca864 bellard
68 7a5ca864 bellard
static void version(const char *name)
69 7a5ca864 bellard
{
70 7a5ca864 bellard
    printf(
71 315bc7aa ths
"%s version 0.0.1\n"
72 7a5ca864 bellard
"Written by Anthony Liguori.\n"
73 7a5ca864 bellard
"\n"
74 7a5ca864 bellard
"Copyright (C) 2006 Anthony Liguori <anthony@codemonkey.ws>.\n"
75 7a5ca864 bellard
"This is free software; see the source for copying conditions.  There is NO\n"
76 7a5ca864 bellard
"warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n"
77 315bc7aa ths
    , name);
78 7a5ca864 bellard
}
79 7a5ca864 bellard
80 7a5ca864 bellard
struct partition_record
81 7a5ca864 bellard
{
82 7a5ca864 bellard
    uint8_t bootable;
83 7a5ca864 bellard
    uint8_t start_head;
84 7a5ca864 bellard
    uint32_t start_cylinder;
85 7a5ca864 bellard
    uint8_t start_sector;
86 7a5ca864 bellard
    uint8_t system;
87 7a5ca864 bellard
    uint8_t end_head;
88 7a5ca864 bellard
    uint8_t end_cylinder;
89 7a5ca864 bellard
    uint8_t end_sector;
90 7a5ca864 bellard
    uint32_t start_sector_abs;
91 7a5ca864 bellard
    uint32_t nb_sectors_abs;
92 7a5ca864 bellard
};
93 7a5ca864 bellard
94 7a5ca864 bellard
static void read_partition(uint8_t *p, struct partition_record *r)
95 7a5ca864 bellard
{
96 7a5ca864 bellard
    r->bootable = p[0];
97 7a5ca864 bellard
    r->start_head = p[1];
98 7a5ca864 bellard
    r->start_cylinder = p[3] | ((p[2] << 2) & 0x0300);
99 7a5ca864 bellard
    r->start_sector = p[2] & 0x3f;
100 7a5ca864 bellard
    r->system = p[4];
101 7a5ca864 bellard
    r->end_head = p[5];
102 7a5ca864 bellard
    r->end_cylinder = p[7] | ((p[6] << 2) & 0x300);
103 7a5ca864 bellard
    r->end_sector = p[6] & 0x3f;
104 7a5ca864 bellard
    r->start_sector_abs = p[8] | p[9] << 8 | p[10] << 16 | p[11] << 24;
105 7a5ca864 bellard
    r->nb_sectors_abs = p[12] | p[13] << 8 | p[14] << 16 | p[15] << 24;
106 7a5ca864 bellard
}
107 7a5ca864 bellard
108 7a5ca864 bellard
static int find_partition(BlockDriverState *bs, int partition,
109 7a5ca864 bellard
                          off_t *offset, off_t *size)
110 7a5ca864 bellard
{
111 7a5ca864 bellard
    struct partition_record mbr[4];
112 7a5ca864 bellard
    uint8_t data[512];
113 7a5ca864 bellard
    int i;
114 7a5ca864 bellard
    int ext_partnum = 4;
115 cb7cf0e3 Ryota Ozaki
    int ret;
116 7a5ca864 bellard
117 cb7cf0e3 Ryota Ozaki
    if ((ret = bdrv_read(bs, 0, data, 1)) < 0) {
118 cb7cf0e3 Ryota Ozaki
        errno = -ret;
119 cb7cf0e3 Ryota Ozaki
        err(EXIT_FAILURE, "error while reading");
120 cb7cf0e3 Ryota Ozaki
    }
121 7a5ca864 bellard
122 7a5ca864 bellard
    if (data[510] != 0x55 || data[511] != 0xaa) {
123 7a5ca864 bellard
        errno = -EINVAL;
124 7a5ca864 bellard
        return -1;
125 7a5ca864 bellard
    }
126 7a5ca864 bellard
127 7a5ca864 bellard
    for (i = 0; i < 4; i++) {
128 7a5ca864 bellard
        read_partition(&data[446 + 16 * i], &mbr[i]);
129 7a5ca864 bellard
130 7a5ca864 bellard
        if (!mbr[i].nb_sectors_abs)
131 7a5ca864 bellard
            continue;
132 7a5ca864 bellard
133 7a5ca864 bellard
        if (mbr[i].system == 0xF || mbr[i].system == 0x5) {
134 7a5ca864 bellard
            struct partition_record ext[4];
135 7a5ca864 bellard
            uint8_t data1[512];
136 7a5ca864 bellard
            int j;
137 7a5ca864 bellard
138 cb7cf0e3 Ryota Ozaki
            if ((ret = bdrv_read(bs, mbr[i].start_sector_abs, data1, 1)) < 0) {
139 cb7cf0e3 Ryota Ozaki
                errno = -ret;
140 cb7cf0e3 Ryota Ozaki
                err(EXIT_FAILURE, "error while reading");
141 cb7cf0e3 Ryota Ozaki
            }
142 7a5ca864 bellard
143 7a5ca864 bellard
            for (j = 0; j < 4; j++) {
144 7a5ca864 bellard
                read_partition(&data1[446 + 16 * j], &ext[j]);
145 7a5ca864 bellard
                if (!ext[j].nb_sectors_abs)
146 7a5ca864 bellard
                    continue;
147 7a5ca864 bellard
148 7a5ca864 bellard
                if ((ext_partnum + j + 1) == partition) {
149 7a5ca864 bellard
                    *offset = (uint64_t)ext[j].start_sector_abs << 9;
150 7a5ca864 bellard
                    *size = (uint64_t)ext[j].nb_sectors_abs << 9;
151 7a5ca864 bellard
                    return 0;
152 7a5ca864 bellard
                }
153 7a5ca864 bellard
            }
154 7a5ca864 bellard
            ext_partnum += 4;
155 7a5ca864 bellard
        } else if ((i + 1) == partition) {
156 7a5ca864 bellard
            *offset = (uint64_t)mbr[i].start_sector_abs << 9;
157 7a5ca864 bellard
            *size = (uint64_t)mbr[i].nb_sectors_abs << 9;
158 7a5ca864 bellard
            return 0;
159 7a5ca864 bellard
        }
160 7a5ca864 bellard
    }
161 7a5ca864 bellard
162 7a5ca864 bellard
    errno = -ENOENT;
163 7a5ca864 bellard
    return -1;
164 7a5ca864 bellard
}
165 7a5ca864 bellard
166 cd831bd7 ths
static void show_parts(const char *device)
167 cd831bd7 ths
{
168 cd831bd7 ths
    if (fork() == 0) {
169 cd831bd7 ths
        int nbd;
170 cd831bd7 ths
171 cd831bd7 ths
        /* linux just needs an open() to trigger
172 cd831bd7 ths
         * the partition table update
173 cd831bd7 ths
         * but remember to load the module with max_part != 0 :
174 cd831bd7 ths
         *     modprobe nbd max_part=63
175 cd831bd7 ths
         */
176 cd831bd7 ths
        nbd = open(device, O_RDWR);
177 cd831bd7 ths
        if (nbd != -1)
178 cd831bd7 ths
              close(nbd);
179 cd831bd7 ths
        exit(0);
180 cd831bd7 ths
    }
181 cd831bd7 ths
}
182 cd831bd7 ths
183 7a5ca864 bellard
int main(int argc, char **argv)
184 7a5ca864 bellard
{
185 7a5ca864 bellard
    BlockDriverState *bs;
186 7a5ca864 bellard
    off_t dev_offset = 0;
187 7a5ca864 bellard
    off_t offset = 0;
188 7a5ca864 bellard
    bool readonly = false;
189 cd831bd7 ths
    bool disconnect = false;
190 7a5ca864 bellard
    const char *bindto = "0.0.0.0";
191 c2e2872b Laurent Vivier
    int port = NBD_DEFAULT_PORT;
192 7a5ca864 bellard
    struct sockaddr_in addr;
193 7a5ca864 bellard
    socklen_t addr_len = sizeof(addr);
194 7a5ca864 bellard
    off_t fd_size;
195 cd831bd7 ths
    char *device = NULL;
196 cd831bd7 ths
    char *socket = NULL;
197 cd831bd7 ths
    char sockpath[128];
198 d6aa671f aliguori
    const char *sopt = "hVb:o:p:rsnP:c:dvk:e:t";
199 7a5ca864 bellard
    struct option lopt[] = {
200 660f11be Blue Swirl
        { "help", 0, NULL, 'h' },
201 660f11be Blue Swirl
        { "version", 0, NULL, 'V' },
202 660f11be Blue Swirl
        { "bind", 1, NULL, 'b' },
203 660f11be Blue Swirl
        { "port", 1, NULL, 'p' },
204 660f11be Blue Swirl
        { "socket", 1, NULL, 'k' },
205 660f11be Blue Swirl
        { "offset", 1, NULL, 'o' },
206 660f11be Blue Swirl
        { "read-only", 0, NULL, 'r' },
207 660f11be Blue Swirl
        { "partition", 1, NULL, 'P' },
208 660f11be Blue Swirl
        { "connect", 1, NULL, 'c' },
209 660f11be Blue Swirl
        { "disconnect", 0, NULL, 'd' },
210 660f11be Blue Swirl
        { "snapshot", 0, NULL, 's' },
211 660f11be Blue Swirl
        { "nocache", 0, NULL, 'n' },
212 660f11be Blue Swirl
        { "shared", 1, NULL, 'e' },
213 660f11be Blue Swirl
        { "persistent", 0, NULL, 't' },
214 660f11be Blue Swirl
        { "verbose", 0, NULL, 'v' },
215 660f11be Blue Swirl
        { NULL, 0, NULL, 0 }
216 7a5ca864 bellard
    };
217 7a5ca864 bellard
    int ch;
218 7a5ca864 bellard
    int opt_ind = 0;
219 7a5ca864 bellard
    int li;
220 7a5ca864 bellard
    char *end;
221 f5edb014 Naphtali Sprei
    int flags = BDRV_O_RDWR;
222 7a5ca864 bellard
    int partition = -1;
223 cd831bd7 ths
    int ret;
224 3b05a8e9 ths
    int shared = 1;
225 2f726488 ths
    uint8_t *data;
226 3b05a8e9 ths
    fd_set fds;
227 3b05a8e9 ths
    int *sharing_fds;
228 3b05a8e9 ths
    int fd;
229 3b05a8e9 ths
    int i;
230 3b05a8e9 ths
    int nb_fds = 0;
231 3b05a8e9 ths
    int max_fd;
232 75818250 ths
    int persistent = 0;
233 1d45f8b5 Laurent Vivier
    uint32_t nbdflags;
234 7a5ca864 bellard
235 7a5ca864 bellard
    while ((ch = getopt_long(argc, argv, sopt, lopt, &opt_ind)) != -1) {
236 7a5ca864 bellard
        switch (ch) {
237 7a5ca864 bellard
        case 's':
238 2f726488 ths
            flags |= BDRV_O_SNAPSHOT;
239 2f726488 ths
            break;
240 2f726488 ths
        case 'n':
241 9f7965c7 aliguori
            flags |= BDRV_O_NOCACHE;
242 7a5ca864 bellard
            break;
243 7a5ca864 bellard
        case 'b':
244 7a5ca864 bellard
            bindto = optarg;
245 7a5ca864 bellard
            break;
246 7a5ca864 bellard
        case 'p':
247 7a5ca864 bellard
            li = strtol(optarg, &end, 0);
248 7a5ca864 bellard
            if (*end) {
249 b6353bea Ryota Ozaki
                errx(EXIT_FAILURE, "Invalid port `%s'", optarg);
250 7a5ca864 bellard
            }
251 7a5ca864 bellard
            if (li < 1 || li > 65535) {
252 b6353bea Ryota Ozaki
                errx(EXIT_FAILURE, "Port out of range `%s'", optarg);
253 7a5ca864 bellard
            }
254 7a5ca864 bellard
            port = (uint16_t)li;
255 7a5ca864 bellard
            break;
256 7a5ca864 bellard
        case 'o':
257 7a5ca864 bellard
                dev_offset = strtoll (optarg, &end, 0);
258 7a5ca864 bellard
            if (*end) {
259 b6353bea Ryota Ozaki
                errx(EXIT_FAILURE, "Invalid offset `%s'", optarg);
260 7a5ca864 bellard
            }
261 7a5ca864 bellard
            if (dev_offset < 0) {
262 b6353bea Ryota Ozaki
                errx(EXIT_FAILURE, "Offset must be positive `%s'", optarg);
263 7a5ca864 bellard
            }
264 7a5ca864 bellard
            break;
265 7a5ca864 bellard
        case 'r':
266 7a5ca864 bellard
            readonly = true;
267 07108b29 Naphtali Sprei
            flags &= ~BDRV_O_RDWR;
268 7a5ca864 bellard
            break;
269 7a5ca864 bellard
        case 'P':
270 7a5ca864 bellard
            partition = strtol(optarg, &end, 0);
271 7a5ca864 bellard
            if (*end)
272 b6353bea Ryota Ozaki
                errx(EXIT_FAILURE, "Invalid partition `%s'", optarg);
273 7a5ca864 bellard
            if (partition < 1 || partition > 8)
274 b6353bea Ryota Ozaki
                errx(EXIT_FAILURE, "Invalid partition %d", partition);
275 7a5ca864 bellard
            break;
276 cd831bd7 ths
        case 'k':
277 cd831bd7 ths
            socket = optarg;
278 cd831bd7 ths
            if (socket[0] != '/')
279 b6353bea Ryota Ozaki
                errx(EXIT_FAILURE, "socket path must be absolute\n");
280 cd831bd7 ths
            break;
281 cd831bd7 ths
        case 'd':
282 cd831bd7 ths
            disconnect = true;
283 cd831bd7 ths
            break;
284 cd831bd7 ths
        case 'c':
285 cd831bd7 ths
            device = optarg;
286 cd831bd7 ths
            break;
287 3b05a8e9 ths
        case 'e':
288 3b05a8e9 ths
            shared = strtol(optarg, &end, 0);
289 3b05a8e9 ths
            if (*end) {
290 b6353bea Ryota Ozaki
                errx(EXIT_FAILURE, "Invalid shared device number '%s'", optarg);
291 3b05a8e9 ths
            }
292 3b05a8e9 ths
            if (shared < 1) {
293 b6353bea Ryota Ozaki
                errx(EXIT_FAILURE, "Shared device number must be greater than 0\n");
294 3b05a8e9 ths
            }
295 3b05a8e9 ths
            break;
296 75818250 ths
        case 't':
297 75818250 ths
            persistent = 1;
298 75818250 ths
            break;
299 7a5ca864 bellard
        case 'v':
300 7a5ca864 bellard
            verbose = 1;
301 7a5ca864 bellard
            break;
302 7a5ca864 bellard
        case 'V':
303 7a5ca864 bellard
            version(argv[0]);
304 7a5ca864 bellard
            exit(0);
305 7a5ca864 bellard
            break;
306 7a5ca864 bellard
        case 'h':
307 7a5ca864 bellard
            usage(argv[0]);
308 7a5ca864 bellard
            exit(0);
309 7a5ca864 bellard
            break;
310 7a5ca864 bellard
        case '?':
311 b6353bea Ryota Ozaki
            errx(EXIT_FAILURE, "Try `%s --help' for more information.",
312 7a5ca864 bellard
                 argv[0]);
313 7a5ca864 bellard
        }
314 7a5ca864 bellard
    }
315 7a5ca864 bellard
316 7a5ca864 bellard
    if ((argc - optind) != 1) {
317 b6353bea Ryota Ozaki
        errx(EXIT_FAILURE, "Invalid number of argument.\n"
318 7a5ca864 bellard
             "Try `%s --help' for more information.",
319 7a5ca864 bellard
             argv[0]);
320 7a5ca864 bellard
    }
321 7a5ca864 bellard
322 cd831bd7 ths
    if (disconnect) {
323 cd831bd7 ths
        fd = open(argv[optind], O_RDWR);
324 cd831bd7 ths
        if (fd == -1)
325 cb7cf0e3 Ryota Ozaki
            err(EXIT_FAILURE, "Cannot open %s", argv[optind]);
326 cd831bd7 ths
327 cd831bd7 ths
        nbd_disconnect(fd);
328 cd831bd7 ths
329 cd831bd7 ths
        close(fd);
330 cd831bd7 ths
331 cd831bd7 ths
        printf("%s disconnected\n", argv[optind]);
332 cd831bd7 ths
333 cd831bd7 ths
        return 0;
334 cd831bd7 ths
    }
335 cd831bd7 ths
336 7a5ca864 bellard
    bdrv_init();
337 7a5ca864 bellard
338 7a5ca864 bellard
    bs = bdrv_new("hda");
339 7a5ca864 bellard
340 cb7cf0e3 Ryota Ozaki
    if ((ret = bdrv_open(bs, argv[optind], flags, NULL)) < 0) {
341 cb7cf0e3 Ryota Ozaki
        errno = -ret;
342 cb7cf0e3 Ryota Ozaki
        err(EXIT_FAILURE, "Failed to bdrv_open '%s'", argv[optind]);
343 cb7cf0e3 Ryota Ozaki
    }
344 7a5ca864 bellard
345 7a5ca864 bellard
    fd_size = bs->total_sectors * 512;
346 7a5ca864 bellard
347 7a5ca864 bellard
    if (partition != -1 &&
348 7a5ca864 bellard
        find_partition(bs, partition, &dev_offset, &fd_size))
349 cb7cf0e3 Ryota Ozaki
        err(EXIT_FAILURE, "Could not find partition %d", partition);
350 7a5ca864 bellard
351 cd831bd7 ths
    if (device) {
352 3b05a8e9 ths
        pid_t pid;
353 3b05a8e9 ths
        int sock;
354 3b05a8e9 ths
355 cb7cf0e3 Ryota Ozaki
        /* want to fail before daemonizing */
356 cb7cf0e3 Ryota Ozaki
        if (access(device, R_OK|W_OK) == -1) {
357 cb7cf0e3 Ryota Ozaki
            err(EXIT_FAILURE, "Could not access '%s'", device);
358 cb7cf0e3 Ryota Ozaki
        }
359 cb7cf0e3 Ryota Ozaki
360 f5de141b Anthony Liguori
        if (!verbose) {
361 f5de141b Anthony Liguori
            /* detach client and server */
362 f5de141b Anthony Liguori
            if (daemon(0, 0) == -1) {
363 cb7cf0e3 Ryota Ozaki
                err(EXIT_FAILURE, "Failed to daemonize");
364 f5de141b Anthony Liguori
            }
365 f5de141b Anthony Liguori
        }
366 cd831bd7 ths
367 cd831bd7 ths
        if (socket == NULL) {
368 22ff51ee Blue Swirl
            snprintf(sockpath, sizeof(sockpath), SOCKET_PATH,
369 22ff51ee Blue Swirl
                     basename(device));
370 cd831bd7 ths
            socket = sockpath;
371 cd831bd7 ths
        }
372 cd831bd7 ths
373 cd831bd7 ths
        pid = fork();
374 cd831bd7 ths
        if (pid < 0)
375 cd831bd7 ths
            return 1;
376 cd831bd7 ths
        if (pid != 0) {
377 cd831bd7 ths
            off_t size;
378 cd831bd7 ths
            size_t blocksize;
379 cd831bd7 ths
380 cd831bd7 ths
            ret = 0;
381 cd831bd7 ths
            bdrv_close(bs);
382 cd831bd7 ths
383 cd831bd7 ths
            do {
384 cd831bd7 ths
                sock = unix_socket_outgoing(socket);
385 cd831bd7 ths
                if (sock == -1) {
386 cb7cf0e3 Ryota Ozaki
                    if (errno != ENOENT && errno != ECONNREFUSED) {
387 cb7cf0e3 Ryota Ozaki
                        ret = 1;
388 cd831bd7 ths
                        goto out;
389 cb7cf0e3 Ryota Ozaki
                    }
390 cd831bd7 ths
                    sleep(1);        /* wait children */
391 cd831bd7 ths
                }
392 cd831bd7 ths
            } while (sock == -1);
393 cd831bd7 ths
394 cd831bd7 ths
            fd = open(device, O_RDWR);
395 cd831bd7 ths
            if (fd == -1) {
396 cd831bd7 ths
                ret = 1;
397 cd831bd7 ths
                goto out;
398 cd831bd7 ths
            }
399 cd831bd7 ths
400 1d45f8b5 Laurent Vivier
            ret = nbd_receive_negotiate(sock, NULL, &nbdflags,
401 1d45f8b5 Laurent Vivier
                                        &size, &blocksize);
402 cd831bd7 ths
            if (ret == -1) {
403 cd831bd7 ths
                ret = 1;
404 cd831bd7 ths
                goto out;
405 cd831bd7 ths
            }
406 cd831bd7 ths
407 cd831bd7 ths
            ret = nbd_init(fd, sock, size, blocksize);
408 cd831bd7 ths
            if (ret == -1) {
409 cd831bd7 ths
                ret = 1;
410 cd831bd7 ths
                goto out;
411 cd831bd7 ths
            }
412 cd831bd7 ths
413 cd831bd7 ths
            printf("NBD device %s is now connected to file %s\n",
414 cd831bd7 ths
                    device, argv[optind]);
415 cd831bd7 ths
416 cd831bd7 ths
            /* update partition table */
417 cd831bd7 ths
418 cd831bd7 ths
            show_parts(device);
419 cd831bd7 ths
420 e301b13d Jes Sorensen
            ret = nbd_client(fd);
421 e301b13d Jes Sorensen
            if (ret) {
422 e301b13d Jes Sorensen
                ret = 1;
423 e301b13d Jes Sorensen
            }
424 cd831bd7 ths
            close(fd);
425 cd831bd7 ths
 out:
426 cd831bd7 ths
            kill(pid, SIGTERM);
427 cd831bd7 ths
            unlink(socket);
428 cd831bd7 ths
429 cd831bd7 ths
            return ret;
430 cd831bd7 ths
        }
431 cd831bd7 ths
        /* children */
432 cd831bd7 ths
    }
433 cd831bd7 ths
434 3b05a8e9 ths
    sharing_fds = qemu_malloc((shared + 1) * sizeof(int));
435 3b05a8e9 ths
436 cd831bd7 ths
    if (socket) {
437 3b05a8e9 ths
        sharing_fds[0] = unix_socket_incoming(socket);
438 cd831bd7 ths
    } else {
439 3b05a8e9 ths
        sharing_fds[0] = tcp_socket_incoming(bindto, port);
440 cd831bd7 ths
    }
441 cd831bd7 ths
442 3b05a8e9 ths
    if (sharing_fds[0] == -1)
443 7a5ca864 bellard
        return 1;
444 3b05a8e9 ths
    max_fd = sharing_fds[0];
445 3b05a8e9 ths
    nb_fds++;
446 7a5ca864 bellard
447 72aef731 Christoph Hellwig
    data = qemu_blockalign(bs, NBD_BUFFER_SIZE);
448 3b05a8e9 ths
    if (data == NULL)
449 b6353bea Ryota Ozaki
        errx(EXIT_FAILURE, "Cannot allocate data buffer");
450 7a5ca864 bellard
451 3b05a8e9 ths
    do {
452 7a5ca864 bellard
453 3b05a8e9 ths
        FD_ZERO(&fds);
454 3b05a8e9 ths
        for (i = 0; i < nb_fds; i++)
455 3b05a8e9 ths
            FD_SET(sharing_fds[i], &fds);
456 3b05a8e9 ths
457 3b05a8e9 ths
        ret = select(max_fd + 1, &fds, NULL, NULL, NULL);
458 3b05a8e9 ths
        if (ret == -1)
459 3b05a8e9 ths
            break;
460 3b05a8e9 ths
461 3b05a8e9 ths
        if (FD_ISSET(sharing_fds[0], &fds))
462 3b05a8e9 ths
            ret--;
463 3b05a8e9 ths
        for (i = 1; i < nb_fds && ret; i++) {
464 3b05a8e9 ths
            if (FD_ISSET(sharing_fds[i], &fds)) {
465 3b05a8e9 ths
                if (nbd_trip(bs, sharing_fds[i], fd_size, dev_offset,
466 3b05a8e9 ths
                    &offset, readonly, data, NBD_BUFFER_SIZE) != 0) {
467 3b05a8e9 ths
                    close(sharing_fds[i]);
468 3b05a8e9 ths
                    nb_fds--;
469 3b05a8e9 ths
                    sharing_fds[i] = sharing_fds[nb_fds];
470 3b05a8e9 ths
                    i--;
471 3b05a8e9 ths
                }
472 3b05a8e9 ths
                ret--;
473 3b05a8e9 ths
            }
474 3b05a8e9 ths
        }
475 3b05a8e9 ths
        /* new connection ? */
476 3b05a8e9 ths
        if (FD_ISSET(sharing_fds[0], &fds)) {
477 3b05a8e9 ths
            if (nb_fds < shared + 1) {
478 3b05a8e9 ths
                sharing_fds[nb_fds] = accept(sharing_fds[0],
479 3b05a8e9 ths
                                             (struct sockaddr *)&addr,
480 3b05a8e9 ths
                                             &addr_len);
481 3b05a8e9 ths
                if (sharing_fds[nb_fds] != -1 &&
482 27982661 aliguori
                    nbd_negotiate(sharing_fds[nb_fds], fd_size) != -1) {
483 3b05a8e9 ths
                        if (sharing_fds[nb_fds] > max_fd)
484 3b05a8e9 ths
                            max_fd = sharing_fds[nb_fds];
485 3b05a8e9 ths
                        nb_fds++;
486 3b05a8e9 ths
                }
487 3b05a8e9 ths
            }
488 3b05a8e9 ths
        }
489 75818250 ths
    } while (persistent || nb_fds > 1);
490 f8a83245 Herve Poussineau
    qemu_vfree(data);
491 7a5ca864 bellard
492 3b05a8e9 ths
    close(sharing_fds[0]);
493 7a5ca864 bellard
    bdrv_close(bs);
494 3b05a8e9 ths
    qemu_free(sharing_fds);
495 cd831bd7 ths
    if (socket)
496 cd831bd7 ths
        unlink(socket);
497 7a5ca864 bellard
498 7a5ca864 bellard
    return 0;
499 7a5ca864 bellard
}