Map netlink after forking to show correct PID in vitastor-nbd ls
This commit is contained in:
+27
-11
@@ -64,7 +64,7 @@ static void netlink_sock_alloc(struct netlink_ctx *ctx)
|
|||||||
if (nl_driver_id < 0)
|
if (nl_driver_id < 0)
|
||||||
{
|
{
|
||||||
nl_socket_free(sk);
|
nl_socket_free(sk);
|
||||||
fail("Couldn't resolve the nbd netlink family\n");
|
fail("Couldn't resolve the nbd netlink family: %s (code %d)\n", nl_geterror(nl_driver_id), nl_driver_id);
|
||||||
}
|
}
|
||||||
|
|
||||||
ctx->driver_id = nl_driver_id;
|
ctx->driver_id = nl_driver_id;
|
||||||
@@ -555,7 +555,12 @@ help:
|
|||||||
fcntl(sockfd[0], F_SETFL, fcntl(sockfd[0], F_GETFL, 0) | O_NONBLOCK);
|
fcntl(sockfd[0], F_SETFL, fcntl(sockfd[0], F_GETFL, 0) | O_NONBLOCK);
|
||||||
nbd_fd = sockfd[0];
|
nbd_fd = sockfd[0];
|
||||||
load_module();
|
load_module();
|
||||||
|
|
||||||
bool bg = cfg["foreground"].is_null();
|
bool bg = cfg["foreground"].is_null();
|
||||||
|
if (cfg["logfile"].string_value() != "")
|
||||||
|
{
|
||||||
|
logfile = cfg["logfile"].string_value();
|
||||||
|
}
|
||||||
|
|
||||||
if (netlink)
|
if (netlink)
|
||||||
{
|
{
|
||||||
@@ -588,6 +593,10 @@ help:
|
|||||||
}
|
}
|
||||||
close(sockfd[1]);
|
close(sockfd[1]);
|
||||||
printf("/dev/nbd%d\n", err);
|
printf("/dev/nbd%d\n", err);
|
||||||
|
if (bg)
|
||||||
|
{
|
||||||
|
daemonize_reopen_stdio();
|
||||||
|
}
|
||||||
#else
|
#else
|
||||||
fprintf(stderr, "netlink support is disabled in this build\n");
|
fprintf(stderr, "netlink support is disabled in this build\n");
|
||||||
exit(1);
|
exit(1);
|
||||||
@@ -631,14 +640,10 @@ help:
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
if (bg)
|
||||||
if (cfg["logfile"].string_value() != "")
|
{
|
||||||
{
|
daemonize();
|
||||||
logfile = cfg["logfile"].string_value();
|
}
|
||||||
}
|
|
||||||
if (bg)
|
|
||||||
{
|
|
||||||
daemonize();
|
|
||||||
}
|
}
|
||||||
// Initialize read state
|
// Initialize read state
|
||||||
read_state = CL_READ_HDR;
|
read_state = CL_READ_HDR;
|
||||||
@@ -716,13 +721,17 @@ help:
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void daemonize()
|
void daemonize_fork()
|
||||||
{
|
{
|
||||||
if (fork())
|
if (fork())
|
||||||
exit(0);
|
exit(0);
|
||||||
setsid();
|
setsid();
|
||||||
if (fork())
|
if (fork())
|
||||||
exit(0);
|
exit(0);
|
||||||
|
}
|
||||||
|
|
||||||
|
void daemonize_reopen_stdio()
|
||||||
|
{
|
||||||
close(0);
|
close(0);
|
||||||
close(1);
|
close(1);
|
||||||
close(2);
|
close(2);
|
||||||
@@ -733,6 +742,12 @@ help:
|
|||||||
fprintf(stderr, "Warning: Failed to chdir into /\n");
|
fprintf(stderr, "Warning: Failed to chdir into /\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void daemonize()
|
||||||
|
{
|
||||||
|
daemonize_fork();
|
||||||
|
daemonize_reopen_stdio();
|
||||||
|
}
|
||||||
|
|
||||||
json11::Json::object list_mapped()
|
json11::Json::object list_mapped()
|
||||||
{
|
{
|
||||||
const char *self_filename = exe_name;
|
const char *self_filename = exe_name;
|
||||||
@@ -783,8 +798,9 @@ help:
|
|||||||
if (!strcmp(pid_filename, self_filename))
|
if (!strcmp(pid_filename, self_filename))
|
||||||
{
|
{
|
||||||
json11::Json::object cfg = nbd_proxy::parse_args(argv.size(), argv.data());
|
json11::Json::object cfg = nbd_proxy::parse_args(argv.size(), argv.data());
|
||||||
if (cfg["command"] == "map")
|
if (cfg["command"] == "map" || cfg["command"] == "netlink-map")
|
||||||
{
|
{
|
||||||
|
cfg["interface"] = (cfg["command"] == "netlink-map") ? "netlink" : "nbd";
|
||||||
cfg.erase("command");
|
cfg.erase("command");
|
||||||
cfg["pid"] = pid;
|
cfg["pid"] = pid;
|
||||||
mapped["/dev/nbd"+std::to_string(dev_num)] = cfg;
|
mapped["/dev/nbd"+std::to_string(dev_num)] = cfg;
|
||||||
|
|||||||
Reference in New Issue
Block a user