mirror of
https://git.proxmox.com/git/mirror_lxc
synced 2025-06-07 15:02:20 +00:00
console: exit mainloop on SIGTERM
This allows cleanly exiting a console session without control sequences. Relates to https://github.com/lxc/lxd/pull/4001 . Note that the existence of a signal handler now doesn't guarantee that ts->node is allocated. Instead, ts->node will now only be added to if stdinfd is a tty. New checks need to take that into account. Signed-off-by: Christian Brauner <christian.brauner@ubuntu.com>
This commit is contained in:
parent
0519b5cce7
commit
1349e92e4d
@ -110,21 +110,30 @@ void lxc_console_sigwinch(int sig)
|
|||||||
int lxc_console_cb_signal_fd(int fd, uint32_t events, void *cbdata,
|
int lxc_console_cb_signal_fd(int fd, uint32_t events, void *cbdata,
|
||||||
struct lxc_epoll_descr *descr)
|
struct lxc_epoll_descr *descr)
|
||||||
{
|
{
|
||||||
|
ssize_t ret;
|
||||||
struct signalfd_siginfo siginfo;
|
struct signalfd_siginfo siginfo;
|
||||||
struct lxc_tty_state *ts = cbdata;
|
struct lxc_tty_state *ts = cbdata;
|
||||||
|
|
||||||
ssize_t ret = read(fd, &siginfo, sizeof(siginfo));
|
ret = read(fd, &siginfo, sizeof(siginfo));
|
||||||
if (ret < 0 || (size_t)ret < sizeof(siginfo)) {
|
if (ret < 0 || (size_t)ret < sizeof(siginfo)) {
|
||||||
ERROR("Failed to read signal info");
|
ERROR("Failed to read signal info");
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (siginfo.ssi_signo == SIGTERM) {
|
||||||
|
DEBUG("Received SIGTERM. Detaching from the console");
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (siginfo.ssi_signo == SIGWINCH)
|
||||||
lxc_console_winch(ts);
|
lxc_console_winch(ts);
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
struct lxc_tty_state *lxc_console_signal_init(int srcfd, int dstfd)
|
struct lxc_tty_state *lxc_console_signal_init(int srcfd, int dstfd)
|
||||||
{
|
{
|
||||||
|
int ret;
|
||||||
bool istty;
|
bool istty;
|
||||||
sigset_t mask;
|
sigset_t mask;
|
||||||
struct lxc_tty_state *ts;
|
struct lxc_tty_state *ts;
|
||||||
@ -138,35 +147,45 @@ struct lxc_tty_state *lxc_console_signal_init(int srcfd, int dstfd)
|
|||||||
ts->masterfd = dstfd;
|
ts->masterfd = dstfd;
|
||||||
ts->sigfd = -1;
|
ts->sigfd = -1;
|
||||||
|
|
||||||
|
sigemptyset(&mask);
|
||||||
|
|
||||||
istty = isatty(srcfd) == 1;
|
istty = isatty(srcfd) == 1;
|
||||||
if (!istty) {
|
if (!istty) {
|
||||||
INFO("fd %d does not refer to a tty device", srcfd);
|
INFO("fd %d does not refer to a tty device", srcfd);
|
||||||
return ts;
|
} else {
|
||||||
}
|
/* Add tty to list to be scanned at SIGWINCH time. */
|
||||||
|
|
||||||
/* add tty to list to be scanned at SIGWINCH time */
|
|
||||||
lxc_list_add_elem(&ts->node, ts);
|
lxc_list_add_elem(&ts->node, ts);
|
||||||
lxc_list_add_tail(&lxc_ttys, &ts->node);
|
lxc_list_add_tail(&lxc_ttys, &ts->node);
|
||||||
|
|
||||||
sigemptyset(&mask);
|
|
||||||
sigaddset(&mask, SIGWINCH);
|
sigaddset(&mask, SIGWINCH);
|
||||||
if (sigprocmask(SIG_BLOCK, &mask, &ts->oldmask)) {
|
}
|
||||||
SYSERROR("Failed to block SIGWINCH signal");
|
|
||||||
ts->sigfd = -1;
|
/* Exit the mainloop cleanly on SIGTERM. */
|
||||||
lxc_list_del(&ts->node);
|
sigaddset(&mask, SIGTERM);
|
||||||
return ts;
|
|
||||||
|
ret = sigprocmask(SIG_BLOCK, &mask, &ts->oldmask);
|
||||||
|
if (ret < 0) {
|
||||||
|
WARN("Failed to block signals");
|
||||||
|
goto on_error;
|
||||||
}
|
}
|
||||||
|
|
||||||
ts->sigfd = signalfd(-1, &mask, 0);
|
ts->sigfd = signalfd(-1, &mask, 0);
|
||||||
if (ts->sigfd < 0) {
|
if (ts->sigfd < 0) {
|
||||||
SYSERROR("Failed to create signal fd");
|
WARN("Failed to create signal fd");
|
||||||
sigprocmask(SIG_SETMASK, &ts->oldmask, NULL);
|
sigprocmask(SIG_SETMASK, &ts->oldmask, NULL);
|
||||||
ts->sigfd = -1;
|
goto on_error;
|
||||||
lxc_list_del(&ts->node);
|
|
||||||
return ts;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
DEBUG("Process %d created signal fd %d", getpid(), ts->sigfd);
|
DEBUG("Created signal fd %d", ts->sigfd);
|
||||||
|
return ts;
|
||||||
|
|
||||||
|
on_error:
|
||||||
|
ERROR("Failed to create signal fd");
|
||||||
|
if (ts->sigfd >= 0) {
|
||||||
|
close(ts->sigfd);
|
||||||
|
ts->sigfd = -1;
|
||||||
|
}
|
||||||
|
if (istty)
|
||||||
|
lxc_list_del(&ts->node);
|
||||||
return ts;
|
return ts;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -174,10 +193,14 @@ void lxc_console_signal_fini(struct lxc_tty_state *ts)
|
|||||||
{
|
{
|
||||||
if (ts->sigfd >= 0) {
|
if (ts->sigfd >= 0) {
|
||||||
close(ts->sigfd);
|
close(ts->sigfd);
|
||||||
lxc_list_del(&ts->node);
|
|
||||||
sigprocmask(SIG_SETMASK, &ts->oldmask, NULL);
|
if (sigprocmask(SIG_SETMASK, &ts->oldmask, NULL) < 0)
|
||||||
|
WARN("%s - Failed to restore signal mask", strerror(errno));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (isatty(ts->stdinfd))
|
||||||
|
lxc_list_del(&ts->node);
|
||||||
|
|
||||||
free(ts);
|
free(ts);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user