From 755f9fd91125c65361c81d8aa9e8af2cfd7adc6d Mon Sep 17 00:00:00 2001 From: David Gibson Date: Mon, 5 Aug 2024 22:36:41 +1000 Subject: [PATCH] nstool: Propagate SIGTERM to processes executed in the namespace Particularly in shell it's sometimes natural to save the pid from a process run and later kill it. If doing this with nstool exec, however, it will kill nstool itself, not the program it is running, which isn't usually what you want or expect. Address this by having nstool propagate SIGTERM to its child process. It may make sense to propagate some other signals, but some introduce extra complications, so we'll worry about them when and if it seems useful. Signed-off-by: David Gibson Signed-off-by: Stefano Brivio --- test/nstool.c | 26 ++++++++++++++++++++++++-- 1 file changed, 24 insertions(+), 2 deletions(-) diff --git a/test/nstool.c b/test/nstool.c index a6aca98..fc357d8 100644 --- a/test/nstool.c +++ b/test/nstool.c @@ -345,17 +345,39 @@ static int openns(const char *fmt, ...) return fd; } +static pid_t sig_pid; +static void sig_handler(int signum) +{ + int err; + + err = kill(sig_pid, signum); + if (err) + die("Propagating %s: %s\n", strsignal(signum), strerror(errno)); +} + static void wait_for_child(pid_t pid) { - int status; + struct sigaction sa = { + .sa_handler = sig_handler, + .sa_flags = SA_RESETHAND, + }; + int status, err; + + sig_pid = pid; + err = sigaction(SIGTERM, &sa, NULL); + if (err) + die("sigaction(SIGTERM): %s\n", strerror(errno)); /* Match the child's exit status, if possible */ for (;;) { pid_t rc; rc = waitpid(pid, &status, WUNTRACED); - if (rc < 0) + if (rc < 0) { + if (errno == EINTR) + continue; die("waitpid() on %d: %s\n", pid, strerror(errno)); + } if (rc != pid) die("waitpid() on %d returned %d", pid, rc); if (WIFSTOPPED(status)) {