84b277
From 72be8870f1ab5d8753d966ee6c81f88734053a95 Mon Sep 17 00:00:00 2001
61b3c9
From: Lennart Poettering <lennart@poettering.net>
61b3c9
Date: Tue, 26 Aug 2014 21:04:21 +0200
61b3c9
Subject: [PATCH] util: reset signals when we fork off agents
61b3c9
61b3c9
If we invoke agents, we should make sure we actually can kill them
61b3c9
again. I mean, it's probably not our job to cleanup the signals if our
61b3c9
tools are invoked in weird contexts, but at least we should make sure,
61b3c9
that the subprocesses we invoke and intend to control work as intended.
61b3c9
61b3c9
Also see:
61b3c9
61b3c9
http://lists.freedesktop.org/archives/systemd-devel/2014-August/022460.html
61b3c9
61b3c9
(cherry picked from commit 8a7c93d858c342744adf481565d8bb03b9713dcf)
61b3c9
61b3c9
Resolves: #1134818
61b3c9
---
61b3c9
 src/shared/util.c | 18 ++++++++++++++++++
61b3c9
 src/shared/util.h |  1 +
61b3c9
 2 files changed, 19 insertions(+)
61b3c9
61b3c9
diff --git a/src/shared/util.c b/src/shared/util.c
84b277
index fc1f765..e4448ff 100644
61b3c9
--- a/src/shared/util.c
61b3c9
+++ b/src/shared/util.c
61b3c9
@@ -935,6 +935,18 @@ int reset_all_signal_handlers(void) {
61b3c9
         return 0;
61b3c9
 }
61b3c9
 
61b3c9
+int reset_signal_mask(void) {
61b3c9
+        sigset_t ss;
61b3c9
+
61b3c9
+        if (sigemptyset(&ss) < 0)
61b3c9
+                return -errno;
61b3c9
+
61b3c9
+        if (sigprocmask(SIG_SETMASK, &ss, NULL) < 0)
61b3c9
+                return -errno;
61b3c9
+
61b3c9
+        return 0;
61b3c9
+}
61b3c9
+
61b3c9
 char *strstrip(char *s) {
61b3c9
         char *e;
61b3c9
 
84b277
@@ -5028,6 +5040,12 @@ int fork_agent(pid_t *pid, const int except[], unsigned n_except, const char *pa
61b3c9
         /* Don't leak fds to the agent */
61b3c9
         close_all_fds(except, n_except);
61b3c9
 
61b3c9
+        /* Make sure we actually can kill the agent, if we need to, in
61b3c9
+         * case somebody invoked us from a shell script that trapped
61b3c9
+         * SIGTERM or so... */
61b3c9
+        reset_all_signal_handlers();
61b3c9
+        reset_signal_mask();
61b3c9
+
61b3c9
         stdout_is_tty = isatty(STDOUT_FILENO);
61b3c9
         stderr_is_tty = isatty(STDERR_FILENO);
61b3c9
 
61b3c9
diff --git a/src/shared/util.h b/src/shared/util.h
84b277
index ec18d2c..5a9bc99 100644
61b3c9
--- a/src/shared/util.h
61b3c9
+++ b/src/shared/util.h
84b277
@@ -207,6 +207,7 @@ int readlink_and_make_absolute(const char *p, char **r);
61b3c9
 int readlink_and_canonicalize(const char *p, char **r);
61b3c9
 
61b3c9
 int reset_all_signal_handlers(void);
61b3c9
+int reset_signal_mask(void);
61b3c9
 
61b3c9
 char *strstrip(char *s);
61b3c9
 char *delete_chars(char *s, const char *bad);