naccyde / rpms / systemd

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