public inbox for util-linux@vger.kernel.org
 help / color / mirror / Atom feed
From: Kiran Rangoon <kiranrangoon0@gmail.com>
To: util-linux@vger.kernel.org
Cc: kzak@redhat.com, Kiran Rangoon <kiranrangoon0@gmail.com>
Subject: [Patch V3 2/4] unshare: implement signal forwarding when --forward-signals is used
Date: Fri, 16 Jan 2026 12:06:46 -0500	[thread overview]
Message-ID: <20260116170648.26439-3-kiranrangoon0@gmail.com> (raw)
In-Reply-To: <20260116170648.26439-1-kiranrangoon0@gmail.com>

When --forward-signals is specified, install signal handlers for
SIGTERM and SIGINT that forward these signals to the child process.
This allows child processes with cleanup handlers to execute gracefully during shutdown scenarios like
system reboot.

Also fix waitpid() to handle EINTR properly when signals are being
forwarded, ensuring the parent waits for the child to complete
signal handling before exiting.

Signed-off-by: Kiran Rangoon <kiranrangoon0@gmail.com>
---
 sys-utils/unshare.c | 55 +++++++++++++++++++++++++++++++++++++++++----
 1 file changed, 51 insertions(+), 4 deletions(-)

diff --git a/sys-utils/unshare.c b/sys-utils/unshare.c
index 8a7a26df2..a4d28892d 100644
--- a/sys-utils/unshare.c
+++ b/sys-utils/unshare.c
@@ -74,6 +74,9 @@ static struct namespace_file {
 
 static int npersists;	/* number of persistent namespaces */
 
+/* Global PID of child process for signal forwarding */
+static volatile pid_t child_pid = 0;
+
 enum {
 	SETGROUPS_NONE = -1,
 	SETGROUPS_DENY = 0,
@@ -135,6 +138,19 @@ static void map_id(const char *file, uint32_t from, uint32_t to)
 	close(fd);
 }
 
+/**
+ * forward_signal() - Forward signal to child process
+ * @sig: Signal number to forward
+ *
+ * Signal handler that forwards SIGTERM/SIGINT from parent to child.
+ * Only used when --forward-signals flag is enabled.
+ */
+static void forward_signal(int sig)
+{
+	if (child_pid > 0)
+		kill(child_pid, sig);
+}
+
 static unsigned long parse_propagation(const char *str)
 {
 	size_t i;
@@ -1107,11 +1123,32 @@ int main(int argc, char *argv[])
 		settime(monotonic, CLOCK_MONOTONIC);
 
 	if (forkit) {
+		if (forward_signals) {
+			/* Install signal handlers to forward signals to child */
+			struct sigaction sa;
+
+			memset(&sa, 0, sizeof(sa));
+			sa.sa_handler = forward_signal;
+			sigemptyset(&sa.sa_mask);
+			sa.sa_flags = SA_RESTART;
+
+			if (sigaction(SIGTERM, &sa, NULL) == -1)
+				err(EXIT_FAILURE, _("sigaction SIGTERM failed"));
+			if (sigaction(SIGINT, &sa, NULL) == -1)
+				err(EXIT_FAILURE, _("sigaction SIGINT failed"));
+
+		/* Save old mask for child to restore */
+		if (sigemptyset(&sigset) != 0 ||
+		    sigprocmask(SIG_SETMASK, NULL, &oldsigset) != 0)
+			err(EXIT_FAILURE, _("sigprocmask failed"));
+	} else {
+		/* Block signals to prevent "impatient parent" problem */
 		if (sigemptyset(&sigset) != 0 ||
-			sigaddset(&sigset, SIGINT) != 0 ||
-			sigaddset(&sigset, SIGTERM) != 0 ||
-			sigprocmask(SIG_BLOCK, &sigset, &oldsigset) != 0)
+		    sigaddset(&sigset, SIGINT) != 0 ||
+		    sigaddset(&sigset, SIGTERM) != 0 ||
+		    sigprocmask(SIG_BLOCK, &sigset, &oldsigset) != 0)
 			err(EXIT_FAILURE, _("sigprocmask block failed"));
+	}
 #ifdef HAVE_PIDFD_OPEN
 		if (kill_child_signo != 0) {
 			/* make a connection to the original process (parent) */
@@ -1128,6 +1165,7 @@ int main(int argc, char *argv[])
 		case -1:
 			err(EXIT_FAILURE, _("fork failed"));
 		case 0:	/* child */
+			child_pid = 0; /* Clear in child process */
 			if (sigprocmask(SIG_SETMASK, &oldsigset, NULL))
 				err(EXIT_FAILURE,
 					_("sigprocmask restore failed"));
@@ -1135,6 +1173,8 @@ int main(int argc, char *argv[])
 				close(fd_bind);
 			break;
 		default: /* parent */
+			if (forward_signals)
+				child_pid = pid; /* Store for signal handler */
 			break;
 		}
 	}
@@ -1149,7 +1189,14 @@ int main(int argc, char *argv[])
 	}
 
 	if (pid) {
-		if (waitpid(pid, &status, 0) == -1)
+		int rc;
+
+		/* Wait for child, handling EINTR from signal forwarding */
+		do {
+			rc = waitpid(pid, &status, 0);
+		} while (rc == -1 && errno == EINTR);
+
+		if (rc == -1)
 			err(EXIT_FAILURE, _("waitpid failed"));
 
 		if (WIFEXITED(status))
-- 
2.47.3


  parent reply	other threads:[~2026-01-16 17:07 UTC|newest]

Thread overview: 18+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-01-08 18:31 [PATCH 0/5] unshare: fix signal forwarding to child processes Kiran Rangoon
2026-01-08 18:31 ` [PATCH 1/5] unshare: add global child_pid variable for signal forwarding Kiran Rangoon
2026-01-08 18:31 ` [PATCH 2/5] unshare: add signal forwarding handler Kiran Rangoon
2026-01-08 18:31 ` [PATCH 3/5] unshare: replace signal blocking with signal handlers Kiran Rangoon
2026-01-08 18:31 ` [PATCH 4/5] unshare: store child PID in global variable Kiran Rangoon
2026-01-08 18:31 ` [PATCH 5/5] unshare: handle EINTR in waitpid loop Kiran Rangoon
2026-01-12 14:05 ` [PATCH 0/5] unshare: fix signal forwarding to child processes Karel Zak
2026-01-13 17:29   ` [Patch V2 0/4] " Kiran Rangoon
2026-01-13 17:29     ` [V2 1/4] unshare: add --forward-signals option to argument parser Kiran Rangoon
2026-01-13 17:29     ` [V2 2/4] unshare: implement signal forwarding when --forward-signals is used Kiran Rangoon
2026-01-13 17:29     ` [V2 3/4] unshare: document --forward-signals in man page Kiran Rangoon
2026-01-13 17:29     ` [V2 4/4] tests: add tests for unshare --forward-signals Kiran Rangoon
2026-01-16 17:06       ` [PATCH V3 0/4] unshare: add --forward-signals option Kiran Rangoon
2026-01-16 17:06         ` [Patch V3 1/4] unshare: add --forward-signals option to argument parser Kiran Rangoon
2026-01-16 17:06         ` Kiran Rangoon [this message]
2026-01-16 17:06         ` [Patch V3 3/4] unshare: document --forward-signals in man page Kiran Rangoon
2026-01-16 17:06         ` [Patch V3 4/4] tests: add tests for unshare --forward-signals Kiran Rangoon
2026-01-21  8:37         ` [PATCH V3 0/4] unshare: add --forward-signals option Karel Zak

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20260116170648.26439-3-kiranrangoon0@gmail.com \
    --to=kiranrangoon0@gmail.com \
    --cc=kzak@redhat.com \
    --cc=util-linux@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox