+static void *show_parts(void *arg)
+{
+ char *device = arg;
+ int nbd;
+
+ /* linux just needs an open() to trigger
+ * the partition table update
+ * but remember to load the module with max_part != 0 :
+ * modprobe nbd max_part=63
+ */
+ nbd = open(device, O_RDWR);
+ if (nbd >= 0) {
+ close(nbd);
+ }
+ return NULL;
+}
+
+static void *nbd_client_thread(void *arg)
+{
+ char *device = arg;
+ off_t size;
+ size_t blocksize;
+ uint32_t nbdflags;
+ int fd, sock;
+ int ret;
+ pthread_t show_parts_thread;
+
+ sock = unix_socket_outgoing(sockpath);
+ if (sock < 0) {
+ goto out;
+ }
+
+ ret = nbd_receive_negotiate(sock, NULL, &nbdflags,
+ &size, &blocksize);
+ if (ret < 0) {
+ goto out;
+ }
+
+ fd = open(device, O_RDWR);
+ if (fd < 0) {
+ /* Linux-only, we can use %m in printf. */
+ fprintf(stderr, "Failed to open %s: %m", device);
+ goto out;
+ }
+
+ ret = nbd_init(fd, sock, nbdflags, size, blocksize);
+ if (ret < 0) {
+ goto out;
+ }
+
+ /* update partition table */
+ pthread_create(&show_parts_thread, NULL, show_parts, device);
+
+ if (verbose) {
+ fprintf(stderr, "NBD device %s is now connected to %s\n",
+ device, srcpath);
+ } else {
+ /* Close stderr so that the qemu-nbd process exits. */
+ dup2(STDOUT_FILENO, STDERR_FILENO);
+ }
+
+ ret = nbd_client(fd);
+ if (ret) {
+ goto out;
+ }
+ close(fd);
+ kill(getpid(), SIGTERM);
+ return (void *) EXIT_SUCCESS;
+
+out:
+ kill(getpid(), SIGTERM);
+ return (void *) EXIT_FAILURE;
+}
+
+static int nbd_can_accept(void *opaque)
+{
+ return nb_fds < shared;
+}
+
+static void nbd_export_closed(NBDExport *exp)
+{
+ assert(state == TERMINATING);
+ state = TERMINATED;
+}
+
+static void nbd_client_closed(NBDClient *client)
+{
+ nb_fds--;
+ if (nb_fds == 0 && !persistent && state == RUNNING) {
+ state = TERMINATE;
+ }
+ qemu_notify_event();
+ nbd_client_put(client);
+}
+
+static void nbd_accept(void *opaque)
+{
+ int server_fd = (uintptr_t) opaque;
+ struct sockaddr_in addr;
+ socklen_t addr_len = sizeof(addr);
+
+ int fd = accept(server_fd, (struct sockaddr *)&addr, &addr_len);
+ if (state >= TERMINATE) {
+ close(fd);
+ return;
+ }
+
+ if (fd >= 0 && nbd_client_new(exp, fd, nbd_client_closed)) {
+ nb_fds++;