From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail.ozlabs.org (mail.ozlabs.org [IPv6:2404:9400:2221:ea00::3]) by passt.top (Postfix) with ESMTPS id F16905A004F for ; Mon, 05 Aug 2024 14:37:08 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gibson.dropbear.id.au; s=202312; t=1722861425; bh=WJW6bbOpyQ2TMIBGE4MyyH8Maq9FOsuuK2a4n9FkmDI=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=AehDD2Jeh0HIjVVL/zT7LK8wMEPf+ZOctIkKwIOm57EMX4pKK/BZ3hUNGttDv/E5c eLzdtkqwwYWkl/AA+v6sz6lDK3YHLlcipKnwS//zVRu6jj+rb5mPxEeNG5Kut3nEID XygA3J5eXrUU4kOU0k87hu8i1AWbb/AXoydlk1IUKYZT9S1z8xu0sBSpehwj7dD7at 6k4bSXn/MwyyoD5AhkaZ3V+342TJpIlV0MRZ8QkhXatNgv7aqYY4fVMWk0/tsnymjP xfz+dpwY0Ww1gL6icZWcg8lAh2FUKtUzaaX52k0Tlo2K47mrWUvkNOmw7ffsoSrZ/m pg1C/3BIFmbWQ== Received: by gandalf.ozlabs.org (Postfix, from userid 1007) id 4WcwtT1dCwz4x42; Mon, 5 Aug 2024 22:37:05 +1000 (AEST) From: David Gibson To: Stefano Brivio , passt-dev@passt.top Subject: [PATCH v2 02/22] nstool: Propagate SIGTERM to processes executed in the namespace Date: Mon, 5 Aug 2024 22:36:41 +1000 Message-ID: <20240805123701.1720730-3-david@gibson.dropbear.id.au> X-Mailer: git-send-email 2.45.2 In-Reply-To: <20240805123701.1720730-1-david@gibson.dropbear.id.au> References: <20240805123701.1720730-1-david@gibson.dropbear.id.au> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Message-ID-Hash: PCWDDLJO55CWUBPGS67DQYTKAGU274KH X-Message-ID-Hash: PCWDDLJO55CWUBPGS67DQYTKAGU274KH X-MailFrom: dgibson@gandalf.ozlabs.org X-Mailman-Rule-Misses: dmarc-mitigation; no-senders; approved; emergency; loop; banned-address; member-moderation; nonmember-moderation; administrivia; implicit-dest; max-recipients; max-size; news-moderation; no-subject; digests; suspicious-header CC: Cleber Rosa , David Gibson X-Mailman-Version: 3.3.8 Precedence: list List-Id: Development discussion and patches for passt Archived-At: Archived-At: List-Archive: List-Archive: List-Help: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: 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 --- test/nstool.c | 26 ++++++++++++++++++++++++-- 1 file changed, 24 insertions(+), 2 deletions(-) diff --git a/test/nstool.c b/test/nstool.c index a6aca981..fc357d8a 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)) { -- 2.45.2