Skip to contentSkip to conversation

unshare: fix signal forwarding to child processes

PATCH Superseded Kiran Rangoon 2 participants 18 messages Jan 8 – Jan 21 Util-Linux package

lore
Share & tools
lore
#0 lore raw

cover unshare: fix signal forwarding to child processes

This series fixes a bug where unshare with --fork was not properly forwarding SIGTERM and SIGINT to child processes, causing premature termination during system shutdown.

The problem occurred because the parent process was blocking signals instead of handling and forwarding them. When systemd sent SIGTERM during shutdown, the parent would receive it but the child would continue running until the kernel forcibly killed it. This resulted in scripts being unable to handle SIGTERM traps properly and perform cleanup operations.

The fix replaces signal blocking with signal handlers that forward SIGTERM/SIGINT to the child process, allowing proper signal handling and graceful shutdown. The waitpid loop is also updated to handle EINTR interruptions from the signal forwarding mechanism.

Kiran Rangoon (5):
unshare: add global child_pid variable for signal forwarding
unshare: add signal forwarding handler
unshare: replace signal blocking with signal handlers
unshare: store child PID in global variable
unshare: handle EINTR in waitpid loop

 sys-utils/unshare.c | 47 ++++++++++++++++++++++++++++++++++++++++-----
 1 file changed, 42 insertions(+), 5 deletions(-)
Signature · 1 line
2.47.3
#1 lore raw

PATCH 1/5 unshare: add global child_pid variable for signal forwarding

Add a global variable to store the child process PID, which will be used by the signal handler to forward SIGTERM/SIGINT to the child.

---
 sys-utils/unshare.c | 3 +++
 1 file changed, 3 insertions(+)
sys-utils/unshare.c +3 -0
index 5370ab981..6df53666a 100644
@@ -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,
Signature · 1 line
2.47.3
#2 lore raw

PATCH 2/5 unshare: add signal forwarding handler

In reply to Kiran Rangoon

Add forward_signal() function that forwards SIGTERM/SIGINT from the parent process to the child process. This will be installed as a signal handler in the next step.

---
 sys-utils/unshare.c | 13 +++++++++++++
 1 file changed, 13 insertions(+)
sys-utils/unshare.c +13 -0
index 6df53666a..3850e5f4a 100644
@@ -122,6 +122,19 @@ static void setgroups_control(int action)
 	close(fd);
 }
 
+/**
+ * forward_signal() - Forward signal to child process
+ * @sig: Signal number to forward
+ *
+ * Signal handler that forwards SIGTERM/SIGINT from parent to child.
+ * This allows the child to handle signals properly when using --fork.
+ */
+static void forward_signal(int sig)
+{
+	if (child_pid > 0)
+		kill(child_pid, sig);
+}
+
 static void map_id(const char *file, uint32_t from, uint32_t to)
 {
 	char *buf;
Signature · 1 line
2.47.3
#3 lore raw

PATCH 3/5 unshare: replace signal blocking with signal handlers

In reply to Kiran Rangoon

Replace sigprocmask(SIG_BLOCK) with sigaction() to install signal handlers for SIGTERM and SIGINT. This allows the parent to catch signals and forward them to the child, instead of just blocking them.

The forward_signal() handler installed in the previous commit will now be called when SIGTERM/SIGINT is received.

---
 sys-utils/unshare.c | 20 ++++++++++++++++----
 1 file changed, 16 insertions(+), 4 deletions(-)
sys-utils/unshare.c +16 -4
index 3850e5f4a..9255ff4f8 100644
@@ -1116,11 +1116,23 @@ int main(int argc, char *argv[])
 		settime(monotonic, CLOCK_MONOTONIC);
 
 	if (forkit) {
+		struct sigaction sa;
+
+		/* Set up signal handler to forward signals to child */
+		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 ||
-			sigaddset(&sigset, SIGINT) != 0 ||
-			sigaddset(&sigset, SIGTERM) != 0 ||
-			sigprocmask(SIG_BLOCK, &sigset, &oldsigset) != 0)
-			err(EXIT_FAILURE, _("sigprocmask block failed"));
+			sigprocmask(SIG_SETMASK, NULL, &oldsigset) != 0)
+			err(EXIT_FAILURE, _("sigprocmask failed"));
 #ifdef HAVE_PIDFD_OPEN
 		if (kill_child_signo != 0) {
 			/* make a connection to the original process (parent) */
Signature · 1 line
2.47.3
#4 lore raw

PATCH 4/5 unshare: store child PID in global variable

In reply to Kiran Rangoon

After fork(), store the child PID in the global child_pid variable so the signal handler can forward signals to it. Clear child_pid in the child process to prevent recursion.

---
 sys-utils/unshare.c | 2 ++
 1 file changed, 2 insertions(+)
sys-utils/unshare.c +2 -0
index 9255ff4f8..8bc805e05 100644
@@ -1149,6 +1149,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"));
@@ -1156,6 +1157,7 @@ int main(int argc, char *argv[])
 				close(fd_bind);
 			break;
 		default: /* parent */
+			child_pid = pid;  /* Store child PID for signal handler */
 			break;
 		}
 	}
Signature · 1 line
2.47.3
#5 lore raw

PATCH 5/5 unshare: handle EINTR in waitpid loop

In reply to Kiran Rangoon

Modify waitpid() to retry on EINTR. When the signal handler forwards a signal to the child, waitpid() can be interrupted. We need to retry in this case instead of failing.

---
 sys-utils/unshare.c | 9 ++++++++-
 1 file changed, 8 insertions(+), 1 deletion(-)
sys-utils/unshare.c +8 -1
index 8bc805e05..2ced8b7a8 100644
@@ -1172,7 +1172,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))
Signature · 1 line
2.47.3
#7 lore raw

v2 cover unshare: fix signal forwarding to child processes

Changes in V2 (responding to feedback from karelzak):
- Made signal forwarding opt-in via new --forward-signals flag
- Preserves default behavior (signals blocked) for backward compatibility
- Addresses concerns from PR #1087 about "impatient parent" problem

This adds --forward-signals to enable graceful shutdown of child processes during system reboot. Since util-linux 2.36, unshare blocks SIGTERM/SIGINT to prevent premature parent exit, but this prevents children from receiving shutdown signals for cleanup.

The new flag installs signal handlers instead of blocking, forwards signals to the child, and properly handles EINTR in waitpid(). Includes tests for basic forwarding and --kill-child compatibility.

Kiran Rangoon (4):
unshare: add --forward-signals option to argument parser
unshare: implement signal forwarding when --forward-signals is used
unshare: document --forward-signals in man page
tests: add tests for unshare --forward-signals

 sys-utils/unshare.1.adoc                      | 13 ++++
 sys-utils/unshare.c                           | 67 +++++++++++++++++--
 tests/expected/unshare/forward-signals        |  1 +
 .../unshare/forward-signals-kill-child        |  1 +
 tests/ts/unshare/forward-signals              | 55 +++++++++++++++
 tests/ts/unshare/forward-signals-kill-child   | 56 ++++++++++++++++
 6 files changed, 186 insertions(+), 7 deletions(-)
 create mode 100644 tests/expected/unshare/forward-signals
 create mode 100644 tests/expected/unshare/forward-signals-kill-child
 create mode 100755 tests/ts/unshare/forward-signals
 create mode 100755 tests/ts/unshare/forward-signals-kill-child
Signature · 1 line
2.47.3
#8 lore raw

PATCH v2 1/4 unshare: add --forward-signals option to argument parser

Add a new --forward-signals command-line option that will allow unshare to forward SIGTERM and SIGINT signals from the parent process to the forked child process.

This commit adds the option parsing infrastructure but does not implement the signal forwarding logic yet. The flag defaults to 0 (disabled) to maintain backward compatibility.

---
 sys-utils/unshare.c | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)
sys-utils/unshare.c +7 -1
index 5370ab981..7e94b9148 100644
@@ -801,6 +801,7 @@ int main(int argc, char *argv[])
 		OPT_MAPAUTO,
 		OPT_MAPSUBIDS,
 		OPT_OWNER,
+		OPT_FORWARD_SIGNALS,
 	};
 	static const struct option longopts[] = {
 		{ "help",          no_argument,       NULL, 'h'             },
@@ -817,6 +818,7 @@ int main(int argc, char *argv[])
 
 		{ "fork",          no_argument,       NULL, 'f'             },
 		{ "kill-child",    optional_argument, NULL, OPT_KILLCHILD   },
+		{ "forward-signals", no_argument,     NULL, OPT_FORWARD_SIGNALS },
 		{ "mount-proc",    optional_argument, NULL, OPT_MOUNTPROC   },
 		{ "mount-binfmt",  optional_argument, NULL, OPT_MOUNTBINFMT },
 		{ "map-user",      required_argument, NULL, OPT_MAPUSER     },
@@ -843,7 +845,7 @@ int main(int argc, char *argv[])
 
 	int setgrpcmd = SETGROUPS_NONE;
 	int unshare_flags = 0;
-	int c, forkit = 0;
+	int c, forkit = 0, forward_signals = 0;
 	uid_t mapuser = -1, owneruser = -1;
 	gid_t mapgroup = -1, ownergroup = -1;
 	struct map_range *usermap = NULL;
@@ -1015,6 +1017,10 @@ int main(int argc, char *argv[])
 			keepcaps = 1;
 			cap_last_cap(); /* Force last cap to be cached before we fork. */
 			break;
+		case OPT_FORWARD_SIGNALS:
+			forkit = 1;
+			forward_signals = 1;
+			break;
 		case 'S':
 			uid = strtoul_or_err(optarg, _("failed to parse uid"));
 			force_uid = 1;
Signature · 1 line
2.47.3
#9 lore raw

PATCH v2 2/4 unshare: implement signal forwarding when --forward-signals is used

In reply to Kiran Rangoon

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.

---
 sys-utils/unshare.c | 59 ++++++++++++++++++++++++++++++++++++++++-----
 1 file changed, 53 insertions(+), 6 deletions(-)
sys-utils/unshare.c +53 -6
index 7e94b9148..efcb57cba 100644
@@ -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;
@@ -1106,11 +1122,32 @@ int main(int argc, char *argv[])
 		settime(monotonic, CLOCK_MONOTONIC);
 
 	if (forkit) {
-		if (sigemptyset(&sigset) != 0 ||
-			sigaddset(&sigset, SIGINT) != 0 ||
-			sigaddset(&sigset, SIGTERM) != 0 ||
-			sigprocmask(SIG_BLOCK, &sigset, &oldsigset) != 0)
-			err(EXIT_FAILURE, _("sigprocmask block failed"));
+		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)
+				err(EXIT_FAILURE, _("sigprocmask block failed"));
+		}
 #ifdef HAVE_PIDFD_OPEN
 		if (kill_child_signo != 0) {
 			/* make a connection to the original process (parent) */
@@ -1127,6 +1164,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"));
@@ -1134,6 +1172,8 @@ int main(int argc, char *argv[])
 				close(fd_bind);
 			break;
 		default: /* parent */
+			if (forward_signals)
+				child_pid = pid; /* Store for signal handler */
 			break;
 		}
 	}
@@ -1148,7 +1188,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))
Signature · 1 line
2.47.3
#10 lore raw

PATCH v2 3/4 unshare: document --forward-signals in man page

In reply to Kiran Rangoon

Add documentation for the new --forward-signals option, explaining its use case (graceful shutdown during reboot/system management) and interaction with the --fork option.

---
 sys-utils/unshare.1.adoc | 13 +++++++++++++
 1 file changed, 13 insertions(+)
sys-utils/unshare.1.adoc +13 -0
index 85d00af3c..fa0b61df1 100644
@@ -81,6 +81,19 @@ Create a new time namespace. If _file_ is specified, then the namespace is made
 *-f*, *--fork*::
 Fork the specified _program_ as a child process of *unshare* rather than running it directly. This is useful when creating a new PID namespace. Note that when *unshare* is waiting for the child process, then it ignores *SIGINT* and *SIGTERM* and does not forward any signals to the child. It is necessary to send signals to the child process.
 
+*--forward-signals*::
+Forward *SIGTERM* and *SIGINT* signals received by the parent *unshare* process to the child process.
+When this option is not specified, *unshare* ignores these signals while waiting for the child process
+to exit (the default behavior since util-linux 2.36). This allows the parent to remain alive while
+the child process handles the signals.
++
+This option is useful when the parent *unshare* process will receive *SIGTERM* or *SIGINT* signals
+(for example, during system reboot or from a process manager), and you want the child process to
+be notified of graceful shutdown requests so it can perform cleanup operations. If your child
+process has signal handlers (such as shell trap handlers), enabling this option allows them to execute.
++
+This option implies *--fork*.
+
 *--keep-caps*::
 When the *--user* option is given, ensure that capabilities granted in the user namespace are preserved in the child process.
 
Signature · 1 line
2.47.3
#11 lore raw

PATCH v2 4/4 tests: add tests for unshare --forward-signals

In reply to Kiran Rangoon

Add two test cases for the new --forward-signals option:
- forward-signals: verifies SIGTERM is forwarded to child
- forward-signals-kill-child: verifies compatibility with --kill-child

Both tests use test_sigreceive which exits with the signal number received, confirming proper signal forwarding.

---
 tests/expected/unshare/forward-signals        |  1 +
 .../unshare/forward-signals-kill-child        |  1 +
 tests/ts/unshare/forward-signals              | 55 ++++++++++++++++++
 tests/ts/unshare/forward-signals-kill-child   | 56 +++++++++++++++++++
 4 files changed, 113 insertions(+)
 create mode 100644 tests/expected/unshare/forward-signals
 create mode 100644 tests/expected/unshare/forward-signals-kill-child
 create mode 100755 tests/ts/unshare/forward-signals
 create mode 100755 tests/ts/unshare/forward-signals-kill-child
tests/expected/unshare/forward-signals new file +1 -0
new file mode 100644
index 000000000..868dab6e1
@@ -0,0 +1 @@
+SIGTERM forwarded successfully
tests/expected/unshare/forward-signals-kill-child new file +1 -0
new file mode 100644
index 000000000..d4e1224da
@@ -0,0 +1 @@
+SIGTERM forwarded successfully with kill-child
tests/ts/unshare/forward-signals new file +55 -0

55 lines not on this page · raw message

tests/ts/unshare/forward-signals-kill-child new file +56 -0

56 lines not on this page · raw message

Signature · 1 line
2.47.3
#12 lore raw

v3 cover unshare: add --forward-signals option

This series adds a new --forward-signals option to unshare that forwards SIGTERM and SIGINT from the parent process to the child. This allows child processes to handle graceful shutdown during system reboot or service manager termination.

Changes in V3:
- Add bash completion for --forward-signals
- Add usage text in help output
- Fix code indentation to match project style
- Improve test synchronization (replace fixed sleep with adaptive polling)
- Add namespace capability checks in tests to prevent CI failures

Kiran Rangoon (4):
unshare: add --forward-signals option to argument parser
unshare: implement signal forwarding when --forward-signals is used
unshare: document --forward-signals in man page
tests: add tests for unshare --forward-signals

 bash-completion/unshare                       |  1 +
 sys-utils/unshare.1.adoc                      | 13 ++++
 sys-utils/unshare.c                           | 64 ++++++++++++++--
 tests/expected/unshare/forward-signals        |  1 +
 .../unshare/forward-signals-kill-child        |  1 +
 tests/ts/unshare/forward-signals              | 73 ++++++++++++++++++
 tests/ts/unshare/forward-signals-kill-child   | 74 +++++++++++++++++++
 7 files changed, 222 insertions(+), 5 deletions(-)
 create mode 100644 tests/expected/unshare/forward-signals
 create mode 100644 tests/expected/unshare/forward-signals-kill-child
 create mode 100755 tests/ts/unshare/forward-signals
 create mode 100755 tests/ts/unshare/forward-signals-kill-child
Signature · 1 line
2.47.3
#13 lore raw

PATCH v3 1/4 unshare: add --forward-signals option to argument parser

Add a new --forward-signals command-line option that will allow unshare to forward SIGTERM and SIGINT signals from the parent process to the forked child process.

This commit adds the option parsing infrastructure but does not implement the signal forwarding logic yet. The flag defaults to 0 (disabled) to maintain backward compatibility.

---
 bash-completion/unshare | 1 +
 sys-utils/unshare.c     | 9 ++++++++-
 2 files changed, 9 insertions(+), 1 deletion(-)
bash-completion/unshare +1 -0
index 19eeb8f08..fdd40e5cb 100644
@@ -28,6 +28,7 @@ _unshare_module()
 				--cgroup
 				--time
 				--fork
+				--forward-signals
 				--kill-child
 				--keep-caps
 				--mount-proc
sys-utils/unshare.c +8 -1
index 5370ab981..8a7a26df2 100644
@@ -769,6 +769,7 @@ static void __attribute__((__noreturn__)) usage(void)
 	fputs(_(" -f, --fork                fork before launching <program>\n"), out);
 	fputs(_(" --kill-child[=<signame>]  when dying, kill the forked child (implies --fork)\n"
 		"                             defaults to SIGKILL\n"), out);
+	fputs(_(" --forward-signals         forward SIGTERM and SIGINT to child (implies --fork)\n"), out);
 	fputs(USAGE_SEPARATOR, out);
 	fputs(_(" --setgroups allow|deny    control the setgroups syscall in user namespaces\n"), out);
 	fputs(_(" --keep-caps               retain capabilities granted in user namespaces\n"), out);
@@ -801,6 +802,7 @@ int main(int argc, char *argv[])
 		OPT_MAPAUTO,
 		OPT_MAPSUBIDS,
 		OPT_OWNER,
+		OPT_FORWARD_SIGNALS,
 	};
 	static const struct option longopts[] = {
 		{ "help",          no_argument,       NULL, 'h'             },
@@ -817,6 +819,7 @@ int main(int argc, char *argv[])
 
 		{ "fork",          no_argument,       NULL, 'f'             },
 		{ "kill-child",    optional_argument, NULL, OPT_KILLCHILD   },
+		{ "forward-signals", no_argument,     NULL, OPT_FORWARD_SIGNALS },
 		{ "mount-proc",    optional_argument, NULL, OPT_MOUNTPROC   },
 		{ "mount-binfmt",  optional_argument, NULL, OPT_MOUNTBINFMT },
 		{ "map-user",      required_argument, NULL, OPT_MAPUSER     },
@@ -843,7 +846,7 @@ int main(int argc, char *argv[])
 
 	int setgrpcmd = SETGROUPS_NONE;
 	int unshare_flags = 0;
-	int c, forkit = 0;
+	int c, forkit = 0, forward_signals = 0;
 	uid_t mapuser = -1, owneruser = -1;
 	gid_t mapgroup = -1, ownergroup = -1;
 	struct map_range *usermap = NULL;
@@ -1015,6 +1018,10 @@ int main(int argc, char *argv[])
 			keepcaps = 1;
 			cap_last_cap(); /* Force last cap to be cached before we fork. */
 			break;
+		case OPT_FORWARD_SIGNALS:
+			forkit = 1;
+			forward_signals = 1;
+			break;
 		case 'S':
 			uid = strtoul_or_err(optarg, _("failed to parse uid"));
 			force_uid = 1;
Signature · 1 line
2.47.3
#14 lore raw

PATCH v3 2/4 unshare: implement signal forwarding when --forward-signals is used

In reply to Kiran Rangoon

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.

---
 sys-utils/unshare.c | 55 +++++++++++++++++++++++++++++++++++++++++----
 1 file changed, 51 insertions(+), 4 deletions(-)
sys-utils/unshare.c +51 -4
index 8a7a26df2..a4d28892d 100644
@@ -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))
Signature · 1 line
2.47.3
#15 lore raw

PATCH v3 3/4 unshare: document --forward-signals in man page

In reply to Kiran Rangoon

Add documentation for the new --forward-signals option, explaining its use case (graceful shutdown during reboot/system management) and interaction with the --fork option.

---
 sys-utils/unshare.1.adoc | 13 +++++++++++++
 1 file changed, 13 insertions(+)
sys-utils/unshare.1.adoc +13 -0
index 1d1fd68f8..888928147 100644
@@ -81,6 +81,19 @@ Create a new time namespace. If _file_ is specified, then the namespace is made
 *-f*, *--fork*::
 Fork the specified _program_ as a child process of *unshare* rather than running it directly. This is useful when creating a new PID namespace. Note that when *unshare* is waiting for the child process, then it ignores *SIGINT* and *SIGTERM* and does not forward any signals to the child. It is necessary to send signals to the child process.
 
+*--forward-signals*::
+Forward *SIGTERM* and *SIGINT* signals received by the parent *unshare* process to the child process.
+When this option is not specified, *unshare* ignores these signals while waiting for the child process
+to exit (the default behavior since util-linux 2.36). This allows the parent to remain alive while
+the child process handles the signals.
++
+This option is useful when the parent *unshare* process will receive *SIGTERM* or *SIGINT* signals
+(for example, during system reboot or from a process manager), and you want the child process to
+be notified of graceful shutdown requests so it can perform cleanup operations. If your child
+process has signal handlers (such as shell trap handlers), enabling this option allows them to execute.
++
+This option implies *--fork*.
+
 *--keep-caps*::
 When the *--user* option is given, ensure that capabilities granted in the user namespace are preserved in the child process.
 
Signature · 1 line
2.47.3
#16 lore raw

PATCH v3 4/4 tests: add tests for unshare --forward-signals

In reply to Kiran Rangoon

Add two test cases for the new --forward-signals option:
- forward-signals: verifies SIGTERM is forwarded to child
- forward-signals-kill-child: verifies compatibility with --kill-child

Both tests use test_sigreceive which exits with the signal number received, confirming proper signal forwarding.

---
 tests/expected/unshare/forward-signals        |  1 +
 .../unshare/forward-signals-kill-child        |  1 +
 tests/ts/unshare/forward-signals              | 73 ++++++++++++++++++
 tests/ts/unshare/forward-signals-kill-child   | 74 +++++++++++++++++++
 4 files changed, 149 insertions(+)
 create mode 100644 tests/expected/unshare/forward-signals
 create mode 100644 tests/expected/unshare/forward-signals-kill-child
 create mode 100755 tests/ts/unshare/forward-signals
 create mode 100755 tests/ts/unshare/forward-signals-kill-child
tests/expected/unshare/forward-signals new file +1 -0
new file mode 100644
index 000000000..868dab6e1
@@ -0,0 +1 @@
+SIGTERM forwarded successfully
tests/expected/unshare/forward-signals-kill-child new file +1 -0
new file mode 100644
index 000000000..d4e1224da
@@ -0,0 +1 @@
+SIGTERM forwarded successfully with kill-child
tests/ts/unshare/forward-signals new file +73 -0

73 lines not on this page · raw message

tests/ts/unshare/forward-signals-kill-child new file +74 -0

74 lines not on this page · raw message

Signature · 1 line
2.47.3
maintainer Applied
#17 lore raw

Re: [PATCH V3 0/4] unshare: add --forward-signals option

In reply to Kiran Rangoon
Quoted 4 lines · Kiran Rangoon
 bash-completion/unshare                       |  1 +
 sys-utils/unshare.1.adoc                      | 13 ++++
 sys-utils/unshare.c                           | 64 ++++++++++++++--
 tests/expected/unshare/forward-signals        |  1 +
Quoted 8 lines
 .../unshare/forward-signals-kill-child        |  1 +
 tests/ts/unshare/forward-signals              | 73 ++++++++++++++++++
 tests/ts/unshare/forward-signals-kill-child   | 74 +++++++++++++++++++
 7 files changed, 222 insertions(+), 5 deletions(-)
 create mode 100644 tests/expected/unshare/forward-signals
 create mode 100644 tests/expected/unshare/forward-signals-kill-child
 create mode 100755 tests/ts/unshare/forward-signals
 create mode 100755 tests/ts/unshare/forward-signals-kill-child

Applied, thanks.

Karel

Signature · 2 lines
 Karel Zak  <[email protected]>
 http://karelzak.blogspot.com