From ca6aee9d7790294501cff7711fe6204499aa698b Mon Sep 17 00:00:00 2001 From: Yury Luneff Date: Thu, 19 Nov 2026 21:27:12 +0300 Subject: [PATCH] Report netlink-map failures in its exit status netlink-map and netlink-revive forked before configuring the device, and the parent exited 0 at once. So a failed configuration (a busy device, no permission, no nbd netlink family) still returned 0, and the command could return before the device was set up. Fork once before setting up the device in both map modes, the way vitastor-ublk does: the parent waits for the daemon to send the device number, prints it and exits 0, or exits 1 if the daemon dies first. The ioctl setup child no longer forks on its own, and failed tries are reaped. map now prints the device also when it is given explicitly. --- src/client/nbd_proxy.cpp | 61 +++++++++++++++++++-------------- tests/test_img_online_resize.sh | 3 ++ 2 files changed, 38 insertions(+), 26 deletions(-) diff --git a/src/client/nbd_proxy.cpp b/src/client/nbd_proxy.cpp index 386cb341..a5293d4c 100644 --- a/src/client/nbd_proxy.cpp +++ b/src/client/nbd_proxy.cpp @@ -21,6 +21,7 @@ #include #include #include +#include #include #include "cluster_client.h" @@ -607,6 +608,17 @@ help: { logfile = cfg["logfile"].string_value(); } + int notifyfd[2] = { -1, -1 }; + if (bg) + { + if (socketpair(AF_UNIX, SOCK_STREAM, 0, notifyfd) < 0) + { + perror("socketpair"); + exit(1); + } + daemonize_fork(notifyfd); + close(notifyfd[0]); + } if (netlink) { @@ -627,10 +639,6 @@ help: if (!cfg["nbd_disconnect_on_close"].is_null()) cflags |= NBD_CFLAG_DISCONNECT_ON_CLOSE; #endif - if (bg) - { - daemonize_fork(); - } int err = netlink_configure(sockfd + 1, 1, dev_num, device_size, 4096, flags, cflags, nbd_timeout, nbd_conn_timeout, NULL, revive); if (err < 0) { @@ -640,11 +648,6 @@ help: } dev_num = err; close(sockfd[1]); - printf("/dev/nbd%d\n", dev_num); - if (bg) - { - daemonize_reopen_stdio(); - } #else fprintf(stderr, "netlink support is disabled in this build\n"); exit(1); @@ -680,7 +683,6 @@ help: int r = run_nbd(sockfd, i, device_size, NBD_FLAG_SEND_FLUSH, nbd_timeout, bg); if (r == 0) { - printf("/dev/nbd%d\n", i); dev_num = i; break; } @@ -700,11 +702,16 @@ help: } } } - if (bg) - { - daemonize(); - } } + if (bg) + { + daemonize_reopen_stdio(); + // MSG_NOSIGNAL: a killed parent must not take the daemon down with SIGPIPE + (void)send(notifyfd[1], &dev_num, sizeof(dev_num), MSG_NOSIGNAL); + close(notifyfd[1]); + } + else + printf("/dev/nbd%d\n", dev_num); // Initialize read state read_state = CL_READ_HDR; recv_buf = malloc_or_die(receive_buffer_size); @@ -816,10 +823,19 @@ help: } } - void daemonize_fork() + void daemonize_fork(int *notifyfd) { if (fork()) + { + // Parent - wait for the device number + close(notifyfd[1]); + int child_dev_num = -1; + (void)read(notifyfd[0], &child_dev_num, sizeof(child_dev_num)); + if (child_dev_num < 0) + exit(1); + printf("/dev/nbd%d\n", child_dev_num); exit(0); + } setsid(); if (fork()) exit(0); @@ -837,12 +853,6 @@ help: fprintf(stderr, "Warning: Failed to chdir into /\n"); } - void daemonize() - { - daemonize_fork(); - daemonize_reopen_stdio(); - } - json11::Json::object list_mapped() { const char *self_filename = exe_name; @@ -965,7 +975,8 @@ protected: { return -errno; } - if (!fork()) + pid_t child = fork(); + if (!child) { // Do all NBD configuration in the child process, after the last fork. // Why? It's needed because there is a race condition in the Linux kernel nbd driver @@ -980,10 +991,6 @@ protected: // works correctly, and we don't accidentally break previous NBD devices while setting // up a new device. Forking to check every device is of course rather slow, so we also // do an additional check by calling list_mapped() before searching for a free NBD device. - if (bg) - { - daemonize_fork(); - } close(notifyfd[0]); snprintf(path, sizeof(path), "/dev/nbd%d", dev_num); int r, nbd = open(path, O_RDWR), qd_fd; @@ -1067,6 +1074,8 @@ protected: close(sockfd[1]); return 0; } + // The map daemon serves for long, don't leave zombies of failed tries + waitpid(child, NULL, 0); return -child_errno; } diff --git a/tests/test_img_online_resize.sh b/tests/test_img_online_resize.sh index 15f77dc8..e096d673 100755 --- a/tests/test_img_online_resize.sh +++ b/tests/test_img_online_resize.sh @@ -11,6 +11,9 @@ fi NBD_DEV=$(sudo build/src/client/vitastor-nbd --config_path $VITASTOR_CFG map --image testimg) trap "sudo build/src/client/vitastor-nbd unmap $NBD_DEV"' || true; kill -9 $(jobs -p)' EXIT +if sudo build/src/client/vitastor-nbd --config_path $VITASTOR_CFG netlink-map $NBD_DEV --image testimg; then + format_error "netlink-map onto busy $NBD_DEV exited 0" +fi UBLK_DEV=$(sudo build/src/client/vitastor-ublk --config_path $VITASTOR_CFG map --image testimg) trap "sudo build/src/client/vitastor-ublk unmap $UBLK_DEV || true; sudo build/src/client/vitastor-nbd unmap $NBD_DEV || true; "'kill -9 $(jobs -p)' EXIT -- 2.47.3