Report netlink-map failures in its exit status #121

Merged
vitalif merged 1 commits from koolkhel/vitastor:nbd-netlink-map-status into master 2026-11-20 19:04:14 +03:00
2 changed files with 38 additions and 26 deletions

View File

@@ -21,6 +21,7 @@
#include <sys/epoll.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <sys/wait.h>
#include <unistd.h>
#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;

Если хочется такое сделать, нужно скопипастить с ublk_server, там по идее такая же штука реализована уже, не нужно чтобы в двух файлах было 2 разных реализации примерно одного и того же, и также не надо чтобы было аж 4 функции - daemonize_fork, daemonize_reopen_stdio, daemonize_begin, daemonize_end

Если хочется такое сделать, нужно скопипастить с ublk_server, там по идее такая же штука реализована уже, не нужно чтобы в двух файлах было 2 разных реализации примерно одного и того же, и также не надо чтобы было аж 4 функции - daemonize_fork, daemonize_reopen_stdio, daemonize_begin, daemonize_end
(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;
}

View File

@@ -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