lxccontainer: switch to pidfds whenever possible

parent 39293f22
...@@ -688,8 +688,17 @@ static int lxc_cmd_stop_callback(int fd, struct lxc_cmd_req *req, ...@@ -688,8 +688,17 @@ static int lxc_cmd_stop_callback(int fd, struct lxc_cmd_req *req,
if (handler->conf->stopsignal) if (handler->conf->stopsignal)
stopsignal = handler->conf->stopsignal; stopsignal = handler->conf->stopsignal;
memset(&rsp, 0, sizeof(rsp)); memset(&rsp, 0, sizeof(rsp));
rsp.ret = kill(handler->pid, stopsignal);
if (handler-> pidfd >= 0)
rsp.ret = lxc_raw_pidfd_send_signal(handler->pidfd, stopsignal, NULL, 0);
else
rsp.ret = kill(handler->pid, stopsignal);
if (!rsp.ret) { if (!rsp.ret) {
if (handler->pidfd >= 0)
TRACE("Sent signal %d to pidfd %d", stopsignal, handler->pidfd);
else
TRACE("Sent signal %d to pidfd %d", stopsignal, handler->pid);
rsp.ret = cgroup_ops->unfreeze(cgroup_ops, -1); rsp.ret = cgroup_ops->unfreeze(cgroup_ops, -1);
if (!rsp.ret) if (!rsp.ret)
return 0; return 0;
......
...@@ -1976,8 +1976,9 @@ static bool lxcapi_create(struct lxc_container *c, const char *t, ...@@ -1976,8 +1976,9 @@ static bool lxcapi_create(struct lxc_container *c, const char *t,
static bool do_lxcapi_reboot(struct lxc_container *c) static bool do_lxcapi_reboot(struct lxc_container *c)
{ {
__do_close_prot_errno int pidfd = -EBADF;
pid_t pid = -1;
int ret; int ret;
pid_t pid;
int rebootsignal = SIGINT; int rebootsignal = SIGINT;
if (!c) if (!c)
...@@ -1986,18 +1987,23 @@ static bool do_lxcapi_reboot(struct lxc_container *c) ...@@ -1986,18 +1987,23 @@ static bool do_lxcapi_reboot(struct lxc_container *c)
if (!do_lxcapi_is_running(c)) if (!do_lxcapi_is_running(c))
return false; return false;
pid = do_lxcapi_init_pid(c); pidfd = do_lxcapi_init_pidfd(c);
if (pid <= 0) if (pidfd < 0) {
return false; pid = do_lxcapi_init_pid(c);
if (pid <= 0)
return false;
}
if (c->lxc_conf && c->lxc_conf->rebootsignal) if (c->lxc_conf && c->lxc_conf->rebootsignal)
rebootsignal = c->lxc_conf->rebootsignal; rebootsignal = c->lxc_conf->rebootsignal;
ret = kill(pid, rebootsignal); if (pidfd >= 0)
if (ret < 0) { ret = lxc_raw_pidfd_send_signal(pidfd, rebootsignal, NULL, 0);
WARN("Failed to send signal %d to pid %d", rebootsignal, pid); else
return false; ret = kill(pid, rebootsignal);
} if (ret < 0)
return log_warn(false, "Failed to send signal %d to pid %d",
rebootsignal, pid);
return true; return true;
} }
...@@ -2006,10 +2012,11 @@ WRAP_API(bool, lxcapi_reboot) ...@@ -2006,10 +2012,11 @@ WRAP_API(bool, lxcapi_reboot)
static bool do_lxcapi_reboot2(struct lxc_container *c, int timeout) static bool do_lxcapi_reboot2(struct lxc_container *c, int timeout)
{ {
int killret, ret; __do_close_prot_errno int pidfd = -EBADF, state_client_fd = -EBADF;
pid_t pid; int rebootsignal = SIGINT;
int rebootsignal = SIGINT, state_client_fd = -1; pid_t pid = -1;
lxc_state_t states[MAX_STATE] = {0}; lxc_state_t states[MAX_STATE] = {0};
int killret, ret;
if (!c) if (!c)
return false; return false;
...@@ -2017,9 +2024,12 @@ static bool do_lxcapi_reboot2(struct lxc_container *c, int timeout) ...@@ -2017,9 +2024,12 @@ static bool do_lxcapi_reboot2(struct lxc_container *c, int timeout)
if (!do_lxcapi_is_running(c)) if (!do_lxcapi_is_running(c))
return true; return true;
pid = do_lxcapi_init_pid(c); pidfd = do_lxcapi_init_pidfd(c);
if (pid <= 0) if (pidfd < 0) {
return true; pid = do_lxcapi_init_pid(c);
if (pid <= 0)
return true;
}
if (c->lxc_conf && c->lxc_conf->rebootsignal) if (c->lxc_conf && c->lxc_conf->rebootsignal)
rebootsignal = c->lxc_conf->rebootsignal; rebootsignal = c->lxc_conf->rebootsignal;
...@@ -2045,21 +2055,18 @@ static bool do_lxcapi_reboot2(struct lxc_container *c, int timeout) ...@@ -2045,21 +2055,18 @@ static bool do_lxcapi_reboot2(struct lxc_container *c, int timeout)
} }
/* Send reboot signal to container. */ /* Send reboot signal to container. */
killret = kill(pid, rebootsignal); if (pidfd >= 0)
if (killret < 0) { killret = lxc_raw_pidfd_send_signal(pidfd, rebootsignal, NULL, 0);
if (state_client_fd >= 0) else
close(state_client_fd); killret = kill(pid, rebootsignal);
if (killret < 0)
WARN("Failed to send signal %d to pid %d", rebootsignal, pid); return log_warn(false, "Failed to send signal %d to pid %d", rebootsignal, pid);
return false;
}
TRACE("Sent signal %d to pid %d", rebootsignal, pid); TRACE("Sent signal %d to pid %d", rebootsignal, pid);
if (timeout == 0) if (timeout == 0)
return true; return true;
ret = lxc_cmd_sock_rcv_state(state_client_fd, timeout); ret = lxc_cmd_sock_rcv_state(state_client_fd, timeout);
close(state_client_fd);
if (ret < 0) if (ret < 0)
return false; return false;
...@@ -2074,11 +2081,11 @@ WRAP_API_1(bool, lxcapi_reboot2, int) ...@@ -2074,11 +2081,11 @@ WRAP_API_1(bool, lxcapi_reboot2, int)
static bool do_lxcapi_shutdown(struct lxc_container *c, int timeout) static bool do_lxcapi_shutdown(struct lxc_container *c, int timeout)
{ {
__do_close_prot_errno int state_client_fd = -EBADF; __do_close_prot_errno int pidfd = -EBADF, state_client_fd = -EBADF;
int haltsignal = SIGPWR; int haltsignal = SIGPWR;
pid_t pid = -1;
lxc_state_t states[MAX_STATE] = {0}; lxc_state_t states[MAX_STATE] = {0};
int killret, ret; int killret, ret;
pid_t pid;
if (!c) if (!c)
return false; return false;
...@@ -2086,9 +2093,12 @@ static bool do_lxcapi_shutdown(struct lxc_container *c, int timeout) ...@@ -2086,9 +2093,12 @@ static bool do_lxcapi_shutdown(struct lxc_container *c, int timeout)
if (!do_lxcapi_is_running(c)) if (!do_lxcapi_is_running(c))
return true; return true;
pid = do_lxcapi_init_pid(c); pidfd = do_lxcapi_init_pidfd(c);
if (pid <= 0) if (pidfd < 0) {
return true; pid = do_lxcapi_init_pid(c);
if (pid <= 0)
return true;
}
/* Detect whether we should send SIGRTMIN + 3 (e.g. systemd). */ /* Detect whether we should send SIGRTMIN + 3 (e.g. systemd). */
if (c->lxc_conf && c->lxc_conf->haltsignal) if (c->lxc_conf && c->lxc_conf->haltsignal)
...@@ -2117,11 +2127,21 @@ static bool do_lxcapi_shutdown(struct lxc_container *c, int timeout) ...@@ -2117,11 +2127,21 @@ static bool do_lxcapi_shutdown(struct lxc_container *c, int timeout)
} }
/* Send shutdown signal to container. */ /* Send shutdown signal to container. */
killret = kill(pid, haltsignal); if (pidfd >= 0) {
if (killret < 0) killret = lxc_raw_pidfd_send_signal(pidfd, haltsignal, NULL, 0);
return log_warn(false, "Failed to send signal %d to pid %d", haltsignal, pid); if (killret < 0)
return log_warn(false, "Failed to send signal %d to pidfd %d",
haltsignal, pidfd);
TRACE("Sent signal %d to pid %d", haltsignal, pid); TRACE("Sent signal %d to pidfd %d", haltsignal, pidfd);
} else {
killret = kill(pid, haltsignal);
if (killret < 0)
return log_warn(false, "Failed to send signal %d to pid %d",
haltsignal, pid);
TRACE("Sent signal %d to pid %d", haltsignal, pid);
}
if (timeout == 0) if (timeout == 0)
return true; return true;
......
...@@ -1869,10 +1869,6 @@ bool lxc_can_use_pidfd(int pidfd) ...@@ -1869,10 +1869,6 @@ bool lxc_can_use_pidfd(int pidfd)
if (pidfd < 0) if (pidfd < 0)
return log_error(false, "Kernel does not support pidfds"); return log_error(false, "Kernel does not support pidfds");
ret = lxc_raw_pidfd_send_signal(pidfd, 0, NULL, 0);
if (ret)
return log_error_errno(false, errno, "Kernel does not support sending signals through pidfds");
/* /*
* We don't care whether or not children were in a waitable state. We * We don't care whether or not children were in a waitable state. We
* just care whether waitid() recognizes P_PIDFD. * just care whether waitid() recognizes P_PIDFD.
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment