ths | cd831bd | 2008-07-03 10:23:51 +0000 | [diff] [blame] | 1 | /* |
bellard | 7a5ca86 | 2008-05-27 21:13:40 +0000 | [diff] [blame] | 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 |
Blue Swirl | 8167ee8 | 2009-07-16 20:47:01 +0000 | [diff] [blame] | 16 | * along with this program; if not, see <http://www.gnu.org/licenses/>. |
bellard | 7a5ca86 | 2008-05-27 21:13:40 +0000 | [diff] [blame] | 17 | */ |
| 18 | |
Stefan Weil | 5a61cb6 | 2011-09-08 17:55:32 +0200 | [diff] [blame] | 19 | #include "qemu-common.h" |
Paolo Bonzini | 737e150 | 2012-12-17 18:19:44 +0100 | [diff] [blame] | 20 | #include "block/block.h" |
| 21 | #include "block/nbd.h" |
Alex Bligh | 6a1751b | 2013-08-21 16:02:47 +0100 | [diff] [blame] | 22 | #include "qemu/main-loop.h" |
bellard | 7a5ca86 | 2008-05-27 21:13:40 +0000 | [diff] [blame] | 23 | |
bellard | 7a5ca86 | 2008-05-27 21:13:40 +0000 | [diff] [blame] | 24 | #include <stdarg.h> |
| 25 | #include <stdio.h> |
| 26 | #include <getopt.h> |
| 27 | #include <err.h> |
ths | cd831bd | 2008-07-03 10:23:51 +0000 | [diff] [blame] | 28 | #include <sys/types.h> |
bellard | 7a5ca86 | 2008-05-27 21:13:40 +0000 | [diff] [blame] | 29 | #include <sys/socket.h> |
| 30 | #include <netinet/in.h> |
| 31 | #include <netinet/tcp.h> |
| 32 | #include <arpa/inet.h> |
ths | cd831bd | 2008-07-03 10:23:51 +0000 | [diff] [blame] | 33 | #include <signal.h> |
Blue Swirl | 2bff4b6 | 2009-12-23 15:34:04 +0000 | [diff] [blame] | 34 | #include <libgen.h> |
Paolo Bonzini | a517e88 | 2011-11-04 15:51:21 +0100 | [diff] [blame] | 35 | #include <pthread.h> |
ths | cd831bd | 2008-07-03 10:23:51 +0000 | [diff] [blame] | 36 | |
Paolo Bonzini | ded9d2d | 2013-02-08 14:06:13 +0100 | [diff] [blame] | 37 | #define SOCKET_PATH "/var/lock/qemu-nbd-%s" |
| 38 | #define QEMU_NBD_OPT_CACHE 1 |
| 39 | #define QEMU_NBD_OPT_AIO 2 |
| 40 | #define QEMU_NBD_OPT_DISCARD 3 |
bellard | 7a5ca86 | 2008-05-27 21:13:40 +0000 | [diff] [blame] | 41 | |
Paolo Bonzini | af49bbb | 2011-09-19 14:03:37 +0200 | [diff] [blame] | 42 | static NBDExport *exp; |
blueswir1 | b1d8e52 | 2008-10-26 13:43:07 +0000 | [diff] [blame] | 43 | static int verbose; |
Paolo Bonzini | a517e88 | 2011-11-04 15:51:21 +0100 | [diff] [blame] | 44 | static char *srcpath; |
| 45 | static char *sockpath; |
Paolo Bonzini | 7860a38 | 2012-09-18 13:31:56 +0200 | [diff] [blame] | 46 | static int persistent = 0; |
| 47 | static enum { RUNNING, TERMINATE, TERMINATING, TERMINATED } state; |
Paolo Bonzini | a61c678 | 2011-09-12 17:28:11 +0200 | [diff] [blame] | 48 | static int shared = 1; |
| 49 | static int nb_fds; |
bellard | 7a5ca86 | 2008-05-27 21:13:40 +0000 | [diff] [blame] | 50 | |
| 51 | static void usage(const char *name) |
| 52 | { |
Paolo Bonzini | b033cd8 | 2012-07-18 14:50:52 +0200 | [diff] [blame] | 53 | (printf) ( |
bellard | 7a5ca86 | 2008-05-27 21:13:40 +0000 | [diff] [blame] | 54 | "Usage: %s [OPTIONS] FILE\n" |
| 55 | "QEMU Disk Network Block Device Server\n" |
| 56 | "\n" |
bellard | 7a5ca86 | 2008-05-27 21:13:40 +0000 | [diff] [blame] | 57 | " -h, --help display this help and exit\n" |
| 58 | " -V, --version output version information and exit\n" |
| 59 | "\n" |
Paolo Bonzini | b033cd8 | 2012-07-18 14:50:52 +0200 | [diff] [blame] | 60 | "Connection properties:\n" |
| 61 | " -p, --port=PORT port to listen on (default `%d')\n" |
| 62 | " -b, --bind=IFACE interface to bind to (default `0.0.0.0')\n" |
| 63 | " -k, --socket=PATH path to the unix socket\n" |
| 64 | " (default '"SOCKET_PATH"')\n" |
| 65 | " -e, --shared=NUM device can be shared by NUM clients (default '1')\n" |
| 66 | " -t, --persistent don't exit on the last connection\n" |
| 67 | " -v, --verbose display extra debugging information\n" |
| 68 | "\n" |
| 69 | "Exposing part of the image:\n" |
| 70 | " -o, --offset=OFFSET offset into the image\n" |
| 71 | " -P, --partition=NUM only expose partition NUM\n" |
| 72 | "\n" |
| 73 | #ifdef __linux__ |
| 74 | "Kernel NBD client support:\n" |
| 75 | " -c, --connect=DEV connect FILE to the local NBD device DEV\n" |
| 76 | " -d, --disconnect disconnect the specified device\n" |
| 77 | "\n" |
| 78 | #endif |
| 79 | "\n" |
| 80 | "Block device options:\n" |
| 81 | " -r, --read-only export read-only\n" |
| 82 | " -s, --snapshot use snapshot file\n" |
| 83 | " -n, --nocache disable host cache\n" |
Paolo Bonzini | 39a5235 | 2012-07-18 14:57:15 +0200 | [diff] [blame] | 84 | " --cache=MODE set cache mode (none, writeback, ...)\n" |
| 85 | #ifdef CONFIG_LINUX_AIO |
| 86 | " --aio=MODE set AIO mode (native or threads)\n" |
| 87 | #endif |
Paolo Bonzini | b033cd8 | 2012-07-18 14:50:52 +0200 | [diff] [blame] | 88 | "\n" |
| 89 | "Report bugs to <qemu-devel@nongnu.org>\n" |
Laurent Vivier | c2e2872 | 2010-09-17 20:37:25 +0200 | [diff] [blame] | 90 | , name, NBD_DEFAULT_PORT, "DEVICE"); |
bellard | 7a5ca86 | 2008-05-27 21:13:40 +0000 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | static void version(const char *name) |
| 94 | { |
| 95 | printf( |
ths | 315bc7a | 2008-07-18 18:06:23 +0000 | [diff] [blame] | 96 | "%s version 0.0.1\n" |
bellard | 7a5ca86 | 2008-05-27 21:13:40 +0000 | [diff] [blame] | 97 | "Written by Anthony Liguori.\n" |
| 98 | "\n" |
| 99 | "Copyright (C) 2006 Anthony Liguori <anthony@codemonkey.ws>.\n" |
| 100 | "This is free software; see the source for copying conditions. There is NO\n" |
| 101 | "warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n" |
ths | 315bc7a | 2008-07-18 18:06:23 +0000 | [diff] [blame] | 102 | , name); |
bellard | 7a5ca86 | 2008-05-27 21:13:40 +0000 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | struct partition_record |
| 106 | { |
| 107 | uint8_t bootable; |
| 108 | uint8_t start_head; |
| 109 | uint32_t start_cylinder; |
| 110 | uint8_t start_sector; |
| 111 | uint8_t system; |
| 112 | uint8_t end_head; |
| 113 | uint8_t end_cylinder; |
| 114 | uint8_t end_sector; |
| 115 | uint32_t start_sector_abs; |
| 116 | uint32_t nb_sectors_abs; |
| 117 | }; |
| 118 | |
| 119 | static void read_partition(uint8_t *p, struct partition_record *r) |
| 120 | { |
| 121 | r->bootable = p[0]; |
| 122 | r->start_head = p[1]; |
| 123 | r->start_cylinder = p[3] | ((p[2] << 2) & 0x0300); |
| 124 | r->start_sector = p[2] & 0x3f; |
| 125 | r->system = p[4]; |
| 126 | r->end_head = p[5]; |
| 127 | r->end_cylinder = p[7] | ((p[6] << 2) & 0x300); |
| 128 | r->end_sector = p[6] & 0x3f; |
| 129 | r->start_sector_abs = p[8] | p[9] << 8 | p[10] << 16 | p[11] << 24; |
| 130 | r->nb_sectors_abs = p[12] | p[13] << 8 | p[14] << 16 | p[15] << 24; |
| 131 | } |
| 132 | |
| 133 | static int find_partition(BlockDriverState *bs, int partition, |
| 134 | off_t *offset, off_t *size) |
| 135 | { |
| 136 | struct partition_record mbr[4]; |
| 137 | uint8_t data[512]; |
| 138 | int i; |
| 139 | int ext_partnum = 4; |
Ryota Ozaki | cb7cf0e | 2010-05-03 06:50:25 +0900 | [diff] [blame] | 140 | int ret; |
bellard | 7a5ca86 | 2008-05-27 21:13:40 +0000 | [diff] [blame] | 141 | |
Ryota Ozaki | cb7cf0e | 2010-05-03 06:50:25 +0900 | [diff] [blame] | 142 | if ((ret = bdrv_read(bs, 0, data, 1)) < 0) { |
| 143 | errno = -ret; |
| 144 | err(EXIT_FAILURE, "error while reading"); |
| 145 | } |
bellard | 7a5ca86 | 2008-05-27 21:13:40 +0000 | [diff] [blame] | 146 | |
| 147 | if (data[510] != 0x55 || data[511] != 0xaa) { |
Paolo Bonzini | 185b433 | 2012-03-05 08:56:10 +0100 | [diff] [blame] | 148 | return -EINVAL; |
bellard | 7a5ca86 | 2008-05-27 21:13:40 +0000 | [diff] [blame] | 149 | } |
| 150 | |
| 151 | for (i = 0; i < 4; i++) { |
| 152 | read_partition(&data[446 + 16 * i], &mbr[i]); |
| 153 | |
| 154 | if (!mbr[i].nb_sectors_abs) |
| 155 | continue; |
| 156 | |
| 157 | if (mbr[i].system == 0xF || mbr[i].system == 0x5) { |
| 158 | struct partition_record ext[4]; |
| 159 | uint8_t data1[512]; |
| 160 | int j; |
| 161 | |
Ryota Ozaki | cb7cf0e | 2010-05-03 06:50:25 +0900 | [diff] [blame] | 162 | if ((ret = bdrv_read(bs, mbr[i].start_sector_abs, data1, 1)) < 0) { |
| 163 | errno = -ret; |
| 164 | err(EXIT_FAILURE, "error while reading"); |
| 165 | } |
bellard | 7a5ca86 | 2008-05-27 21:13:40 +0000 | [diff] [blame] | 166 | |
| 167 | for (j = 0; j < 4; j++) { |
| 168 | read_partition(&data1[446 + 16 * j], &ext[j]); |
| 169 | if (!ext[j].nb_sectors_abs) |
| 170 | continue; |
| 171 | |
| 172 | if ((ext_partnum + j + 1) == partition) { |
| 173 | *offset = (uint64_t)ext[j].start_sector_abs << 9; |
| 174 | *size = (uint64_t)ext[j].nb_sectors_abs << 9; |
| 175 | return 0; |
| 176 | } |
| 177 | } |
| 178 | ext_partnum += 4; |
| 179 | } else if ((i + 1) == partition) { |
| 180 | *offset = (uint64_t)mbr[i].start_sector_abs << 9; |
| 181 | *size = (uint64_t)mbr[i].nb_sectors_abs << 9; |
| 182 | return 0; |
| 183 | } |
| 184 | } |
| 185 | |
Paolo Bonzini | 185b433 | 2012-03-05 08:56:10 +0100 | [diff] [blame] | 186 | return -ENOENT; |
bellard | 7a5ca86 | 2008-05-27 21:13:40 +0000 | [diff] [blame] | 187 | } |
| 188 | |
Paolo Bonzini | bb34511 | 2011-11-04 15:51:19 +0100 | [diff] [blame] | 189 | static void termsig_handler(int signum) |
| 190 | { |
Paolo Bonzini | 7860a38 | 2012-09-18 13:31:56 +0200 | [diff] [blame] | 191 | state = TERMINATE; |
Paolo Bonzini | a61c678 | 2011-09-12 17:28:11 +0200 | [diff] [blame] | 192 | qemu_notify_event(); |
Paolo Bonzini | bb34511 | 2011-11-04 15:51:19 +0100 | [diff] [blame] | 193 | } |
| 194 | |
Paolo Bonzini | a517e88 | 2011-11-04 15:51:21 +0100 | [diff] [blame] | 195 | static void *show_parts(void *arg) |
ths | cd831bd | 2008-07-03 10:23:51 +0000 | [diff] [blame] | 196 | { |
Paolo Bonzini | a6ac231 | 2011-12-06 09:07:00 +0100 | [diff] [blame] | 197 | char *device = arg; |
Paolo Bonzini | a517e88 | 2011-11-04 15:51:21 +0100 | [diff] [blame] | 198 | int nbd; |
ths | cd831bd | 2008-07-03 10:23:51 +0000 | [diff] [blame] | 199 | |
Paolo Bonzini | a517e88 | 2011-11-04 15:51:21 +0100 | [diff] [blame] | 200 | /* linux just needs an open() to trigger |
| 201 | * the partition table update |
| 202 | * but remember to load the module with max_part != 0 : |
| 203 | * modprobe nbd max_part=63 |
| 204 | */ |
| 205 | nbd = open(device, O_RDWR); |
Paolo Bonzini | fc19f8a | 2012-03-07 11:05:34 +0100 | [diff] [blame] | 206 | if (nbd >= 0) { |
Paolo Bonzini | a517e88 | 2011-11-04 15:51:21 +0100 | [diff] [blame] | 207 | close(nbd); |
ths | cd831bd | 2008-07-03 10:23:51 +0000 | [diff] [blame] | 208 | } |
Paolo Bonzini | a517e88 | 2011-11-04 15:51:21 +0100 | [diff] [blame] | 209 | return NULL; |
| 210 | } |
| 211 | |
| 212 | static void *nbd_client_thread(void *arg) |
| 213 | { |
Paolo Bonzini | a6ac231 | 2011-12-06 09:07:00 +0100 | [diff] [blame] | 214 | char *device = arg; |
Paolo Bonzini | a517e88 | 2011-11-04 15:51:21 +0100 | [diff] [blame] | 215 | off_t size; |
| 216 | size_t blocksize; |
| 217 | uint32_t nbdflags; |
Paolo Bonzini | a6ac231 | 2011-12-06 09:07:00 +0100 | [diff] [blame] | 218 | int fd, sock; |
Paolo Bonzini | a517e88 | 2011-11-04 15:51:21 +0100 | [diff] [blame] | 219 | int ret; |
| 220 | pthread_t show_parts_thread; |
| 221 | |
Stefan Hajnoczi | dc10e8b | 2012-01-05 13:16:07 +0000 | [diff] [blame] | 222 | sock = unix_socket_outgoing(sockpath); |
Paolo Bonzini | fc19f8a | 2012-03-07 11:05:34 +0100 | [diff] [blame] | 223 | if (sock < 0) { |
Stefan Hajnoczi | dc10e8b | 2012-01-05 13:16:07 +0000 | [diff] [blame] | 224 | goto out; |
| 225 | } |
Paolo Bonzini | a517e88 | 2011-11-04 15:51:21 +0100 | [diff] [blame] | 226 | |
| 227 | ret = nbd_receive_negotiate(sock, NULL, &nbdflags, |
| 228 | &size, &blocksize); |
Paolo Bonzini | fc19f8a | 2012-03-07 11:05:34 +0100 | [diff] [blame] | 229 | if (ret < 0) { |
Paolo Bonzini | a517e88 | 2011-11-04 15:51:21 +0100 | [diff] [blame] | 230 | goto out; |
| 231 | } |
| 232 | |
Paolo Bonzini | a6ac231 | 2011-12-06 09:07:00 +0100 | [diff] [blame] | 233 | fd = open(device, O_RDWR); |
Paolo Bonzini | fc19f8a | 2012-03-07 11:05:34 +0100 | [diff] [blame] | 234 | if (fd < 0) { |
Paolo Bonzini | a6ac231 | 2011-12-06 09:07:00 +0100 | [diff] [blame] | 235 | /* Linux-only, we can use %m in printf. */ |
| 236 | fprintf(stderr, "Failed to open %s: %m", device); |
| 237 | goto out; |
| 238 | } |
| 239 | |
Paolo Bonzini | a517e88 | 2011-11-04 15:51:21 +0100 | [diff] [blame] | 240 | ret = nbd_init(fd, sock, nbdflags, size, blocksize); |
Paolo Bonzini | fc19f8a | 2012-03-07 11:05:34 +0100 | [diff] [blame] | 241 | if (ret < 0) { |
Paolo Bonzini | a517e88 | 2011-11-04 15:51:21 +0100 | [diff] [blame] | 242 | goto out; |
| 243 | } |
| 244 | |
| 245 | /* update partition table */ |
Paolo Bonzini | a6ac231 | 2011-12-06 09:07:00 +0100 | [diff] [blame] | 246 | pthread_create(&show_parts_thread, NULL, show_parts, device); |
Paolo Bonzini | a517e88 | 2011-11-04 15:51:21 +0100 | [diff] [blame] | 247 | |
Paolo Bonzini | c1f8fdc | 2011-11-04 15:51:22 +0100 | [diff] [blame] | 248 | if (verbose) { |
| 249 | fprintf(stderr, "NBD device %s is now connected to %s\n", |
| 250 | device, srcpath); |
| 251 | } else { |
| 252 | /* Close stderr so that the qemu-nbd process exits. */ |
| 253 | dup2(STDOUT_FILENO, STDERR_FILENO); |
| 254 | } |
Paolo Bonzini | a517e88 | 2011-11-04 15:51:21 +0100 | [diff] [blame] | 255 | |
| 256 | ret = nbd_client(fd); |
| 257 | if (ret) { |
| 258 | goto out; |
| 259 | } |
| 260 | close(fd); |
| 261 | kill(getpid(), SIGTERM); |
| 262 | return (void *) EXIT_SUCCESS; |
| 263 | |
| 264 | out: |
| 265 | kill(getpid(), SIGTERM); |
| 266 | return (void *) EXIT_FAILURE; |
ths | cd831bd | 2008-07-03 10:23:51 +0000 | [diff] [blame] | 267 | } |
| 268 | |
Paolo Bonzini | a61c678 | 2011-09-12 17:28:11 +0200 | [diff] [blame] | 269 | static int nbd_can_accept(void *opaque) |
| 270 | { |
| 271 | return nb_fds < shared; |
| 272 | } |
| 273 | |
Paolo Bonzini | 7860a38 | 2012-09-18 13:31:56 +0200 | [diff] [blame] | 274 | static void nbd_export_closed(NBDExport *exp) |
| 275 | { |
| 276 | assert(state == TERMINATING); |
| 277 | state = TERMINATED; |
| 278 | } |
| 279 | |
Paolo Bonzini | 1743b51 | 2011-09-19 14:33:23 +0200 | [diff] [blame] | 280 | static void nbd_client_closed(NBDClient *client) |
Paolo Bonzini | a61c678 | 2011-09-12 17:28:11 +0200 | [diff] [blame] | 281 | { |
Paolo Bonzini | 1743b51 | 2011-09-19 14:33:23 +0200 | [diff] [blame] | 282 | nb_fds--; |
Paolo Bonzini | 7860a38 | 2012-09-18 13:31:56 +0200 | [diff] [blame] | 283 | if (nb_fds == 0 && !persistent && state == RUNNING) { |
| 284 | state = TERMINATE; |
| 285 | } |
Paolo Bonzini | 1743b51 | 2011-09-19 14:33:23 +0200 | [diff] [blame] | 286 | qemu_notify_event(); |
Paolo Bonzini | 7860a38 | 2012-09-18 13:31:56 +0200 | [diff] [blame] | 287 | nbd_client_put(client); |
Paolo Bonzini | a61c678 | 2011-09-12 17:28:11 +0200 | [diff] [blame] | 288 | } |
| 289 | |
| 290 | static void nbd_accept(void *opaque) |
| 291 | { |
| 292 | int server_fd = (uintptr_t) opaque; |
| 293 | struct sockaddr_in addr; |
| 294 | socklen_t addr_len = sizeof(addr); |
| 295 | |
| 296 | int fd = accept(server_fd, (struct sockaddr *)&addr, &addr_len); |
Paolo Bonzini | 7860a38 | 2012-09-18 13:31:56 +0200 | [diff] [blame] | 297 | if (state >= TERMINATE) { |
| 298 | close(fd); |
| 299 | return; |
| 300 | } |
| 301 | |
Paolo Bonzini | fc19f8a | 2012-03-07 11:05:34 +0100 | [diff] [blame] | 302 | if (fd >= 0 && nbd_client_new(exp, fd, nbd_client_closed)) { |
Paolo Bonzini | a61c678 | 2011-09-12 17:28:11 +0200 | [diff] [blame] | 303 | nb_fds++; |
| 304 | } |
| 305 | } |
| 306 | |
bellard | 7a5ca86 | 2008-05-27 21:13:40 +0000 | [diff] [blame] | 307 | int main(int argc, char **argv) |
| 308 | { |
| 309 | BlockDriverState *bs; |
Daniel P. Berrange | e6b6367 | 2013-03-19 11:20:20 +0000 | [diff] [blame] | 310 | BlockDriver *drv; |
bellard | 7a5ca86 | 2008-05-27 21:13:40 +0000 | [diff] [blame] | 311 | off_t dev_offset = 0; |
Paolo Bonzini | b90fb4b | 2011-09-08 17:24:54 +0200 | [diff] [blame] | 312 | uint32_t nbdflags = 0; |
ths | cd831bd | 2008-07-03 10:23:51 +0000 | [diff] [blame] | 313 | bool disconnect = false; |
bellard | 7a5ca86 | 2008-05-27 21:13:40 +0000 | [diff] [blame] | 314 | const char *bindto = "0.0.0.0"; |
Paolo Bonzini | a6ac231 | 2011-12-06 09:07:00 +0100 | [diff] [blame] | 315 | char *device = NULL; |
Laurent Vivier | c2e2872 | 2010-09-17 20:37:25 +0200 | [diff] [blame] | 316 | int port = NBD_DEFAULT_PORT; |
bellard | 7a5ca86 | 2008-05-27 21:13:40 +0000 | [diff] [blame] | 317 | off_t fd_size; |
Daniel P. Berrange | e6b6367 | 2013-03-19 11:20:20 +0000 | [diff] [blame] | 318 | const char *sopt = "hVb:o:p:rsnP:c:dvk:e:f:t"; |
bellard | 7a5ca86 | 2008-05-27 21:13:40 +0000 | [diff] [blame] | 319 | struct option lopt[] = { |
Blue Swirl | 660f11b | 2009-07-31 21:16:51 +0000 | [diff] [blame] | 320 | { "help", 0, NULL, 'h' }, |
| 321 | { "version", 0, NULL, 'V' }, |
| 322 | { "bind", 1, NULL, 'b' }, |
| 323 | { "port", 1, NULL, 'p' }, |
| 324 | { "socket", 1, NULL, 'k' }, |
| 325 | { "offset", 1, NULL, 'o' }, |
| 326 | { "read-only", 0, NULL, 'r' }, |
| 327 | { "partition", 1, NULL, 'P' }, |
| 328 | { "connect", 1, NULL, 'c' }, |
| 329 | { "disconnect", 0, NULL, 'd' }, |
| 330 | { "snapshot", 0, NULL, 's' }, |
| 331 | { "nocache", 0, NULL, 'n' }, |
Paolo Bonzini | 39a5235 | 2012-07-18 14:57:15 +0200 | [diff] [blame] | 332 | { "cache", 1, NULL, QEMU_NBD_OPT_CACHE }, |
| 333 | #ifdef CONFIG_LINUX_AIO |
| 334 | { "aio", 1, NULL, QEMU_NBD_OPT_AIO }, |
| 335 | #endif |
Paolo Bonzini | ded9d2d | 2013-02-08 14:06:13 +0100 | [diff] [blame] | 336 | { "discard", 1, NULL, QEMU_NBD_OPT_DISCARD }, |
Blue Swirl | 660f11b | 2009-07-31 21:16:51 +0000 | [diff] [blame] | 337 | { "shared", 1, NULL, 'e' }, |
Daniel P. Berrange | e6b6367 | 2013-03-19 11:20:20 +0000 | [diff] [blame] | 338 | { "format", 1, NULL, 'f' }, |
Blue Swirl | 660f11b | 2009-07-31 21:16:51 +0000 | [diff] [blame] | 339 | { "persistent", 0, NULL, 't' }, |
| 340 | { "verbose", 0, NULL, 'v' }, |
| 341 | { NULL, 0, NULL, 0 } |
bellard | 7a5ca86 | 2008-05-27 21:13:40 +0000 | [diff] [blame] | 342 | }; |
| 343 | int ch; |
| 344 | int opt_ind = 0; |
| 345 | int li; |
| 346 | char *end; |
Naphtali Sprei | f5edb01 | 2010-01-17 16:48:13 +0200 | [diff] [blame] | 347 | int flags = BDRV_O_RDWR; |
bellard | 7a5ca86 | 2008-05-27 21:13:40 +0000 | [diff] [blame] | 348 | int partition = -1; |
ths | cd831bd | 2008-07-03 10:23:51 +0000 | [diff] [blame] | 349 | int ret; |
ths | 3b05a8e | 2008-07-03 12:45:02 +0000 | [diff] [blame] | 350 | int fd; |
Paolo Bonzini | 39a5235 | 2012-07-18 14:57:15 +0200 | [diff] [blame] | 351 | bool seen_cache = false; |
Paolo Bonzini | ded9d2d | 2013-02-08 14:06:13 +0100 | [diff] [blame] | 352 | bool seen_discard = false; |
Paolo Bonzini | 39a5235 | 2012-07-18 14:57:15 +0200 | [diff] [blame] | 353 | #ifdef CONFIG_LINUX_AIO |
| 354 | bool seen_aio = false; |
| 355 | #endif |
Paolo Bonzini | a517e88 | 2011-11-04 15:51:21 +0100 | [diff] [blame] | 356 | pthread_t client_thread; |
Daniel P. Berrange | e6b6367 | 2013-03-19 11:20:20 +0000 | [diff] [blame] | 357 | const char *fmt = NULL; |
Max Reitz | 34b5d2c | 2013-09-05 14:45:29 +0200 | [diff] [blame] | 358 | Error *local_err = NULL; |
bellard | 7a5ca86 | 2008-05-27 21:13:40 +0000 | [diff] [blame] | 359 | |
Paolo Bonzini | a517e88 | 2011-11-04 15:51:21 +0100 | [diff] [blame] | 360 | /* The client thread uses SIGTERM to interrupt the server. A signal |
| 361 | * handler ensures that "qemu-nbd -v -c" exits with a nice status code. |
| 362 | */ |
Paolo Bonzini | bb34511 | 2011-11-04 15:51:19 +0100 | [diff] [blame] | 363 | struct sigaction sa_sigterm; |
Paolo Bonzini | bb34511 | 2011-11-04 15:51:19 +0100 | [diff] [blame] | 364 | memset(&sa_sigterm, 0, sizeof(sa_sigterm)); |
| 365 | sa_sigterm.sa_handler = termsig_handler; |
| 366 | sigaction(SIGTERM, &sa_sigterm, NULL); |
| 367 | |
bellard | 7a5ca86 | 2008-05-27 21:13:40 +0000 | [diff] [blame] | 368 | while ((ch = getopt_long(argc, argv, sopt, lopt, &opt_ind)) != -1) { |
| 369 | switch (ch) { |
| 370 | case 's': |
ths | 2f72648 | 2008-07-03 11:47:46 +0000 | [diff] [blame] | 371 | flags |= BDRV_O_SNAPSHOT; |
| 372 | break; |
| 373 | case 'n': |
Paolo Bonzini | 39a5235 | 2012-07-18 14:57:15 +0200 | [diff] [blame] | 374 | optarg = (char *) "none"; |
| 375 | /* fallthrough */ |
| 376 | case QEMU_NBD_OPT_CACHE: |
| 377 | if (seen_cache) { |
| 378 | errx(EXIT_FAILURE, "-n and --cache can only be specified once"); |
| 379 | } |
| 380 | seen_cache = true; |
| 381 | if (bdrv_parse_cache_flags(optarg, &flags) == -1) { |
| 382 | errx(EXIT_FAILURE, "Invalid cache mode `%s'", optarg); |
| 383 | } |
bellard | 7a5ca86 | 2008-05-27 21:13:40 +0000 | [diff] [blame] | 384 | break; |
Paolo Bonzini | 39a5235 | 2012-07-18 14:57:15 +0200 | [diff] [blame] | 385 | #ifdef CONFIG_LINUX_AIO |
| 386 | case QEMU_NBD_OPT_AIO: |
| 387 | if (seen_aio) { |
| 388 | errx(EXIT_FAILURE, "--aio can only be specified once"); |
| 389 | } |
| 390 | seen_aio = true; |
| 391 | if (!strcmp(optarg, "native")) { |
| 392 | flags |= BDRV_O_NATIVE_AIO; |
| 393 | } else if (!strcmp(optarg, "threads")) { |
| 394 | /* this is the default */ |
| 395 | } else { |
| 396 | errx(EXIT_FAILURE, "invalid aio mode `%s'", optarg); |
| 397 | } |
| 398 | break; |
| 399 | #endif |
Paolo Bonzini | ded9d2d | 2013-02-08 14:06:13 +0100 | [diff] [blame] | 400 | case QEMU_NBD_OPT_DISCARD: |
| 401 | if (seen_discard) { |
| 402 | errx(EXIT_FAILURE, "--discard can only be specified once"); |
| 403 | } |
| 404 | seen_discard = true; |
| 405 | if (bdrv_parse_discard_flags(optarg, &flags) == -1) { |
| 406 | errx(EXIT_FAILURE, "Invalid discard mode `%s'", optarg); |
| 407 | } |
| 408 | break; |
bellard | 7a5ca86 | 2008-05-27 21:13:40 +0000 | [diff] [blame] | 409 | case 'b': |
| 410 | bindto = optarg; |
| 411 | break; |
| 412 | case 'p': |
| 413 | li = strtol(optarg, &end, 0); |
| 414 | if (*end) { |
Ryota Ozaki | b6353be | 2010-03-20 15:23:23 +0900 | [diff] [blame] | 415 | errx(EXIT_FAILURE, "Invalid port `%s'", optarg); |
bellard | 7a5ca86 | 2008-05-27 21:13:40 +0000 | [diff] [blame] | 416 | } |
| 417 | if (li < 1 || li > 65535) { |
Ryota Ozaki | b6353be | 2010-03-20 15:23:23 +0900 | [diff] [blame] | 418 | errx(EXIT_FAILURE, "Port out of range `%s'", optarg); |
bellard | 7a5ca86 | 2008-05-27 21:13:40 +0000 | [diff] [blame] | 419 | } |
| 420 | port = (uint16_t)li; |
| 421 | break; |
| 422 | case 'o': |
| 423 | dev_offset = strtoll (optarg, &end, 0); |
| 424 | if (*end) { |
Ryota Ozaki | b6353be | 2010-03-20 15:23:23 +0900 | [diff] [blame] | 425 | errx(EXIT_FAILURE, "Invalid offset `%s'", optarg); |
bellard | 7a5ca86 | 2008-05-27 21:13:40 +0000 | [diff] [blame] | 426 | } |
| 427 | if (dev_offset < 0) { |
Ryota Ozaki | b6353be | 2010-03-20 15:23:23 +0900 | [diff] [blame] | 428 | errx(EXIT_FAILURE, "Offset must be positive `%s'", optarg); |
bellard | 7a5ca86 | 2008-05-27 21:13:40 +0000 | [diff] [blame] | 429 | } |
| 430 | break; |
| 431 | case 'r': |
Paolo Bonzini | b90fb4b | 2011-09-08 17:24:54 +0200 | [diff] [blame] | 432 | nbdflags |= NBD_FLAG_READ_ONLY; |
Naphtali Sprei | 07108b2 | 2010-03-14 15:19:57 +0200 | [diff] [blame] | 433 | flags &= ~BDRV_O_RDWR; |
bellard | 7a5ca86 | 2008-05-27 21:13:40 +0000 | [diff] [blame] | 434 | break; |
| 435 | case 'P': |
| 436 | partition = strtol(optarg, &end, 0); |
| 437 | if (*end) |
Ryota Ozaki | b6353be | 2010-03-20 15:23:23 +0900 | [diff] [blame] | 438 | errx(EXIT_FAILURE, "Invalid partition `%s'", optarg); |
bellard | 7a5ca86 | 2008-05-27 21:13:40 +0000 | [diff] [blame] | 439 | if (partition < 1 || partition > 8) |
Ryota Ozaki | b6353be | 2010-03-20 15:23:23 +0900 | [diff] [blame] | 440 | errx(EXIT_FAILURE, "Invalid partition %d", partition); |
bellard | 7a5ca86 | 2008-05-27 21:13:40 +0000 | [diff] [blame] | 441 | break; |
ths | cd831bd | 2008-07-03 10:23:51 +0000 | [diff] [blame] | 442 | case 'k': |
Paolo Bonzini | b32f6c2 | 2011-11-04 15:51:20 +0100 | [diff] [blame] | 443 | sockpath = optarg; |
| 444 | if (sockpath[0] != '/') |
Ryota Ozaki | b6353be | 2010-03-20 15:23:23 +0900 | [diff] [blame] | 445 | errx(EXIT_FAILURE, "socket path must be absolute\n"); |
ths | cd831bd | 2008-07-03 10:23:51 +0000 | [diff] [blame] | 446 | break; |
| 447 | case 'd': |
| 448 | disconnect = true; |
| 449 | break; |
| 450 | case 'c': |
| 451 | device = optarg; |
| 452 | break; |
ths | 3b05a8e | 2008-07-03 12:45:02 +0000 | [diff] [blame] | 453 | case 'e': |
| 454 | shared = strtol(optarg, &end, 0); |
| 455 | if (*end) { |
Ryota Ozaki | b6353be | 2010-03-20 15:23:23 +0900 | [diff] [blame] | 456 | errx(EXIT_FAILURE, "Invalid shared device number '%s'", optarg); |
ths | 3b05a8e | 2008-07-03 12:45:02 +0000 | [diff] [blame] | 457 | } |
| 458 | if (shared < 1) { |
Ryota Ozaki | b6353be | 2010-03-20 15:23:23 +0900 | [diff] [blame] | 459 | errx(EXIT_FAILURE, "Shared device number must be greater than 0\n"); |
ths | 3b05a8e | 2008-07-03 12:45:02 +0000 | [diff] [blame] | 460 | } |
| 461 | break; |
Daniel P. Berrange | e6b6367 | 2013-03-19 11:20:20 +0000 | [diff] [blame] | 462 | case 'f': |
| 463 | fmt = optarg; |
| 464 | break; |
ths | 7581825 | 2008-07-03 13:41:03 +0000 | [diff] [blame] | 465 | case 't': |
| 466 | persistent = 1; |
| 467 | break; |
bellard | 7a5ca86 | 2008-05-27 21:13:40 +0000 | [diff] [blame] | 468 | case 'v': |
| 469 | verbose = 1; |
| 470 | break; |
| 471 | case 'V': |
| 472 | version(argv[0]); |
| 473 | exit(0); |
| 474 | break; |
| 475 | case 'h': |
| 476 | usage(argv[0]); |
| 477 | exit(0); |
| 478 | break; |
| 479 | case '?': |
Ryota Ozaki | b6353be | 2010-03-20 15:23:23 +0900 | [diff] [blame] | 480 | errx(EXIT_FAILURE, "Try `%s --help' for more information.", |
bellard | 7a5ca86 | 2008-05-27 21:13:40 +0000 | [diff] [blame] | 481 | argv[0]); |
| 482 | } |
| 483 | } |
| 484 | |
| 485 | if ((argc - optind) != 1) { |
Ryota Ozaki | b6353be | 2010-03-20 15:23:23 +0900 | [diff] [blame] | 486 | errx(EXIT_FAILURE, "Invalid number of argument.\n" |
bellard | 7a5ca86 | 2008-05-27 21:13:40 +0000 | [diff] [blame] | 487 | "Try `%s --help' for more information.", |
| 488 | argv[0]); |
| 489 | } |
| 490 | |
ths | cd831bd | 2008-07-03 10:23:51 +0000 | [diff] [blame] | 491 | if (disconnect) { |
| 492 | fd = open(argv[optind], O_RDWR); |
Paolo Bonzini | fc19f8a | 2012-03-07 11:05:34 +0100 | [diff] [blame] | 493 | if (fd < 0) { |
Ryota Ozaki | cb7cf0e | 2010-05-03 06:50:25 +0900 | [diff] [blame] | 494 | err(EXIT_FAILURE, "Cannot open %s", argv[optind]); |
Paolo Bonzini | fc19f8a | 2012-03-07 11:05:34 +0100 | [diff] [blame] | 495 | } |
ths | cd831bd | 2008-07-03 10:23:51 +0000 | [diff] [blame] | 496 | nbd_disconnect(fd); |
| 497 | |
| 498 | close(fd); |
| 499 | |
| 500 | printf("%s disconnected\n", argv[optind]); |
| 501 | |
| 502 | return 0; |
| 503 | } |
| 504 | |
Paolo Bonzini | c1f8fdc | 2011-11-04 15:51:22 +0100 | [diff] [blame] | 505 | if (device && !verbose) { |
| 506 | int stderr_fd[2]; |
| 507 | pid_t pid; |
| 508 | int ret; |
| 509 | |
Paolo Bonzini | fc19f8a | 2012-03-07 11:05:34 +0100 | [diff] [blame] | 510 | if (qemu_pipe(stderr_fd) < 0) { |
Paolo Bonzini | c1f8fdc | 2011-11-04 15:51:22 +0100 | [diff] [blame] | 511 | err(EXIT_FAILURE, "Error setting up communication pipe"); |
| 512 | } |
| 513 | |
| 514 | /* Now daemonize, but keep a communication channel open to |
| 515 | * print errors and exit with the proper status code. |
| 516 | */ |
| 517 | pid = fork(); |
| 518 | if (pid == 0) { |
| 519 | close(stderr_fd[0]); |
Michael Tokarev | 9faf31b | 2012-01-16 18:37:44 +0400 | [diff] [blame] | 520 | ret = qemu_daemon(1, 0); |
Paolo Bonzini | c1f8fdc | 2011-11-04 15:51:22 +0100 | [diff] [blame] | 521 | |
| 522 | /* Temporarily redirect stderr to the parent's pipe... */ |
| 523 | dup2(stderr_fd[1], STDERR_FILENO); |
Paolo Bonzini | fc19f8a | 2012-03-07 11:05:34 +0100 | [diff] [blame] | 524 | if (ret < 0) { |
Paolo Bonzini | c1f8fdc | 2011-11-04 15:51:22 +0100 | [diff] [blame] | 525 | err(EXIT_FAILURE, "Failed to daemonize"); |
| 526 | } |
| 527 | |
| 528 | /* ... close the descriptor we inherited and go on. */ |
| 529 | close(stderr_fd[1]); |
| 530 | } else { |
| 531 | bool errors = false; |
| 532 | char *buf; |
| 533 | |
| 534 | /* In the parent. Print error messages from the child until |
| 535 | * it closes the pipe. |
| 536 | */ |
| 537 | close(stderr_fd[1]); |
| 538 | buf = g_malloc(1024); |
| 539 | while ((ret = read(stderr_fd[0], buf, 1024)) > 0) { |
| 540 | errors = true; |
| 541 | ret = qemu_write_full(STDERR_FILENO, buf, ret); |
Paolo Bonzini | fc19f8a | 2012-03-07 11:05:34 +0100 | [diff] [blame] | 542 | if (ret < 0) { |
Paolo Bonzini | c1f8fdc | 2011-11-04 15:51:22 +0100 | [diff] [blame] | 543 | exit(EXIT_FAILURE); |
| 544 | } |
| 545 | } |
Paolo Bonzini | fc19f8a | 2012-03-07 11:05:34 +0100 | [diff] [blame] | 546 | if (ret < 0) { |
Paolo Bonzini | c1f8fdc | 2011-11-04 15:51:22 +0100 | [diff] [blame] | 547 | err(EXIT_FAILURE, "Cannot read from daemon"); |
| 548 | } |
| 549 | |
| 550 | /* Usually the daemon should not print any message. |
| 551 | * Exit with zero status in that case. |
| 552 | */ |
| 553 | exit(errors); |
| 554 | } |
| 555 | } |
| 556 | |
Paolo Bonzini | a6ac231 | 2011-12-06 09:07:00 +0100 | [diff] [blame] | 557 | if (device != NULL && sockpath == NULL) { |
| 558 | sockpath = g_malloc(128); |
| 559 | snprintf(sockpath, 128, SOCKET_PATH, basename(device)); |
ths | cd831bd | 2008-07-03 10:23:51 +0000 | [diff] [blame] | 560 | } |
| 561 | |
Paolo Bonzini | 7e7f4a0 | 2012-11-03 18:06:26 +0100 | [diff] [blame] | 562 | qemu_init_main_loop(); |
Paolo Bonzini | 802ddc3 | 2011-11-04 15:51:24 +0100 | [diff] [blame] | 563 | bdrv_init(); |
| 564 | atexit(bdrv_close_all); |
| 565 | |
Daniel P. Berrange | e6b6367 | 2013-03-19 11:20:20 +0000 | [diff] [blame] | 566 | if (fmt) { |
| 567 | drv = bdrv_find_format(fmt); |
| 568 | if (!drv) { |
| 569 | errx(EXIT_FAILURE, "Unknown file format '%s'", fmt); |
| 570 | } |
| 571 | } else { |
| 572 | drv = NULL; |
| 573 | } |
| 574 | |
Paolo Bonzini | 802ddc3 | 2011-11-04 15:51:24 +0100 | [diff] [blame] | 575 | bs = bdrv_new("hda"); |
| 576 | srcpath = argv[optind]; |
Max Reitz | 34b5d2c | 2013-09-05 14:45:29 +0200 | [diff] [blame] | 577 | ret = bdrv_open(bs, srcpath, NULL, flags, drv, &local_err); |
Daniel P. Berrange | e6b6367 | 2013-03-19 11:20:20 +0000 | [diff] [blame] | 578 | if (ret < 0) { |
Paolo Bonzini | 802ddc3 | 2011-11-04 15:51:24 +0100 | [diff] [blame] | 579 | errno = -ret; |
Max Reitz | 34b5d2c | 2013-09-05 14:45:29 +0200 | [diff] [blame] | 580 | err(EXIT_FAILURE, "Failed to bdrv_open '%s': %s", argv[optind], |
| 581 | error_get_pretty(local_err)); |
Paolo Bonzini | 802ddc3 | 2011-11-04 15:51:24 +0100 | [diff] [blame] | 582 | } |
| 583 | |
Paolo Bonzini | 38ceff0 | 2012-03-12 16:17:27 +0100 | [diff] [blame] | 584 | fd_size = bdrv_getlength(bs); |
Paolo Bonzini | 802ddc3 | 2011-11-04 15:51:24 +0100 | [diff] [blame] | 585 | |
Paolo Bonzini | 185b433 | 2012-03-05 08:56:10 +0100 | [diff] [blame] | 586 | if (partition != -1) { |
| 587 | ret = find_partition(bs, partition, &dev_offset, &fd_size); |
| 588 | if (ret < 0) { |
| 589 | errno = -ret; |
| 590 | err(EXIT_FAILURE, "Could not find partition %d", partition); |
| 591 | } |
Paolo Bonzini | 802ddc3 | 2011-11-04 15:51:24 +0100 | [diff] [blame] | 592 | } |
| 593 | |
Paolo Bonzini | 7860a38 | 2012-09-18 13:31:56 +0200 | [diff] [blame] | 594 | exp = nbd_export_new(bs, dev_offset, fd_size, nbdflags, nbd_export_closed); |
ths | 3b05a8e | 2008-07-03 12:45:02 +0000 | [diff] [blame] | 595 | |
Paolo Bonzini | b32f6c2 | 2011-11-04 15:51:20 +0100 | [diff] [blame] | 596 | if (sockpath) { |
Paolo Bonzini | a61c678 | 2011-09-12 17:28:11 +0200 | [diff] [blame] | 597 | fd = unix_socket_incoming(sockpath); |
ths | cd831bd | 2008-07-03 10:23:51 +0000 | [diff] [blame] | 598 | } else { |
Paolo Bonzini | a61c678 | 2011-09-12 17:28:11 +0200 | [diff] [blame] | 599 | fd = tcp_socket_incoming(bindto, port); |
ths | cd831bd | 2008-07-03 10:23:51 +0000 | [diff] [blame] | 600 | } |
| 601 | |
Paolo Bonzini | fc19f8a | 2012-03-07 11:05:34 +0100 | [diff] [blame] | 602 | if (fd < 0) { |
bellard | 7a5ca86 | 2008-05-27 21:13:40 +0000 | [diff] [blame] | 603 | return 1; |
Paolo Bonzini | a61c678 | 2011-09-12 17:28:11 +0200 | [diff] [blame] | 604 | } |
Paolo Bonzini | f1ef555 | 2011-11-04 15:51:23 +0100 | [diff] [blame] | 605 | |
| 606 | if (device) { |
| 607 | int ret; |
| 608 | |
Paolo Bonzini | a6ac231 | 2011-12-06 09:07:00 +0100 | [diff] [blame] | 609 | ret = pthread_create(&client_thread, NULL, nbd_client_thread, device); |
Paolo Bonzini | f1ef555 | 2011-11-04 15:51:23 +0100 | [diff] [blame] | 610 | if (ret != 0) { |
| 611 | errx(EXIT_FAILURE, "Failed to create client thread: %s", |
| 612 | strerror(ret)); |
| 613 | } |
| 614 | } else { |
| 615 | /* Shut up GCC warnings. */ |
| 616 | memset(&client_thread, 0, sizeof(client_thread)); |
| 617 | } |
| 618 | |
Paolo Bonzini | a61c678 | 2011-09-12 17:28:11 +0200 | [diff] [blame] | 619 | qemu_set_fd_handler2(fd, nbd_can_accept, nbd_accept, NULL, |
| 620 | (void *)(uintptr_t)fd); |
bellard | 7a5ca86 | 2008-05-27 21:13:40 +0000 | [diff] [blame] | 621 | |
Michael Tokarev | 9faf31b | 2012-01-16 18:37:44 +0400 | [diff] [blame] | 622 | /* now when the initialization is (almost) complete, chdir("/") |
| 623 | * to free any busy filesystems */ |
| 624 | if (chdir("/") < 0) { |
| 625 | err(EXIT_FAILURE, "Could not chdir to root directory"); |
| 626 | } |
| 627 | |
Paolo Bonzini | 7860a38 | 2012-09-18 13:31:56 +0200 | [diff] [blame] | 628 | state = RUNNING; |
ths | 3b05a8e | 2008-07-03 12:45:02 +0000 | [diff] [blame] | 629 | do { |
Paolo Bonzini | a61c678 | 2011-09-12 17:28:11 +0200 | [diff] [blame] | 630 | main_loop_wait(false); |
Paolo Bonzini | 7860a38 | 2012-09-18 13:31:56 +0200 | [diff] [blame] | 631 | if (state == TERMINATE) { |
| 632 | state = TERMINATING; |
| 633 | nbd_export_close(exp); |
| 634 | nbd_export_put(exp); |
| 635 | exp = NULL; |
| 636 | } |
| 637 | } while (state != TERMINATED); |
ths | 3b05a8e | 2008-07-03 12:45:02 +0000 | [diff] [blame] | 638 | |
Paolo Bonzini | a4aab7b | 2012-08-22 18:50:30 +0200 | [diff] [blame] | 639 | bdrv_close(bs); |
Paolo Bonzini | b32f6c2 | 2011-11-04 15:51:20 +0100 | [diff] [blame] | 640 | if (sockpath) { |
| 641 | unlink(sockpath); |
| 642 | } |
bellard | 7a5ca86 | 2008-05-27 21:13:40 +0000 | [diff] [blame] | 643 | |
Paolo Bonzini | a517e88 | 2011-11-04 15:51:21 +0100 | [diff] [blame] | 644 | if (device) { |
| 645 | void *ret; |
| 646 | pthread_join(client_thread, &ret); |
| 647 | exit(ret != NULL); |
| 648 | } else { |
| 649 | exit(EXIT_SUCCESS); |
| 650 | } |
bellard | 7a5ca86 | 2008-05-27 21:13:40 +0000 | [diff] [blame] | 651 | } |