4f94566
From 7bd1311e54a418b1532d3931b9af70f8aabf4ece Mon Sep 17 00:00:00 2001
4f94566
From: Lennart Poettering <lennart@poettering.net>
4f94566
Date: Thu, 6 Mar 2014 02:19:06 +0100
4f94566
Subject: [PATCH] util: add timeout to generator execution
4f94566
4f94566
(cherry picked from commit aa62a8936f5983770e90b791083d55107659f7a1)
4f94566
(cherry picked from commit d66226bce64620543d21675ae610ecfeb9395e2a
4f94566
                and commit 5e2f14e62b43f713bade20dd9f55db525ac43859)
4f94566
4f94566
Conflicts:
4f94566
	src/core/manager.c
4f94566
---
4f94566
 src/core/manager.c   |   5 +-
4f94566
 src/core/shutdown.c  |   2 +-
4f94566
 src/shared/hashmap.h |   8 +++
4f94566
 src/shared/util.c    | 162 +++++++++++++++++++++++++++------------------------
4f94566
 src/shared/util.h    |   2 +-
4f94566
 src/sleep/sleep.c    |  14 +++--
4f94566
 6 files changed, 108 insertions(+), 85 deletions(-)
4f94566
4f94566
diff --git a/src/core/manager.c b/src/core/manager.c
4f94566
index 58c17ab..3a38fab 100644
4f94566
--- a/src/core/manager.c
4f94566
+++ b/src/core/manager.c
4f94566
@@ -2661,9 +2661,8 @@ void manager_run_generators(Manager *m) {
4f94566
         argv[3] = m->generator_unit_path_late;
4f94566
         argv[4] = NULL;
4f94566
 
4f94566
-        RUN_WITH_UMASK(0022) {
4f94566
-                execute_directory(generator_path, d, (char**) argv);
4f94566
-        }
4f94566
+        RUN_WITH_UMASK(0022)
4f94566
+                execute_directory(generator_path, d, DEFAULT_TIMEOUT_USEC, (char**) argv);
4f94566
 
4f94566
         trim_generator_dir(m, &m->generator_unit_path);
4f94566
         trim_generator_dir(m, &m->generator_unit_path_early);
4f94566
diff --git a/src/core/shutdown.c b/src/core/shutdown.c
4f94566
index 5e0f3ce..68960b1 100644
4f94566
--- a/src/core/shutdown.c
4f94566
+++ b/src/core/shutdown.c
4f94566
@@ -294,7 +294,7 @@ int main(int argc, char *argv[]) {
4f94566
         arguments[0] = NULL;
4f94566
         arguments[1] = argv[1];
4f94566
         arguments[2] = NULL;
4f94566
-        execute_directory(SYSTEM_SHUTDOWN_PATH, NULL, arguments);
4f94566
+        execute_directory(SYSTEM_SHUTDOWN_PATH, NULL, DEFAULT_TIMEOUT_USEC, arguments);
4f94566
 
4f94566
         if (!in_container && !in_initrd() &&
4f94566
             access("/run/initramfs/shutdown", X_OK) == 0) {
4f94566
diff --git a/src/shared/hashmap.h b/src/shared/hashmap.h
4f94566
index 3d4f672..b912af8 100644
4f94566
--- a/src/shared/hashmap.h
4f94566
+++ b/src/shared/hashmap.h
4f94566
@@ -24,6 +24,7 @@
4f94566
 #include <stdbool.h>
4f94566
 
4f94566
 #include "macro.h"
4f94566
+#include "util.h"
4f94566
 
4f94566
 /* Pretty straightforward hash table implementation. As a minor
4f94566
  * optimization a NULL hashmap object will be treated as empty hashmap
4f94566
@@ -104,3 +105,10 @@ char **hashmap_get_strv(Hashmap *h);
4f94566
 
4f94566
 #define HASHMAP_FOREACH_BACKWARDS(e, h, i) \
4f94566
         for ((i) = ITERATOR_LAST, (e) = hashmap_iterate_backwards((h), &(i), NULL); (e); (e) = hashmap_iterate_backwards((h), &(i), NULL))
4f94566
+
4f94566
+DEFINE_TRIVIAL_CLEANUP_FUNC(Hashmap*, hashmap_free);
4f94566
+DEFINE_TRIVIAL_CLEANUP_FUNC(Hashmap*, hashmap_free_free);
4f94566
+DEFINE_TRIVIAL_CLEANUP_FUNC(Hashmap*, hashmap_free_free_free);
4f94566
+#define _cleanup_hashmap_free_ _cleanup_(hashmap_freep)
4f94566
+#define _cleanup_hashmap_free_free_ _cleanup_(hashmap_free_freep)
4f94566
+#define _cleanup_hashmap_free_free_free_ _cleanup_(hashmap_free_free_freep)
4f94566
diff --git a/src/shared/util.c b/src/shared/util.c
4f94566
index 1329854..bec339b 100644
4f94566
--- a/src/shared/util.c
4f94566
+++ b/src/shared/util.c
4f94566
@@ -3740,111 +3740,123 @@ bool dirent_is_file_with_suffix(const struct dirent *de, const char *suffix) {
4f94566
         return endswith(de->d_name, suffix);
4f94566
 }
4f94566
 
4f94566
-void execute_directory(const char *directory, DIR *d, char *argv[]) {
4f94566
-        DIR *_d = NULL;
4f94566
-        struct dirent *de;
4f94566
-        Hashmap *pids = NULL;
4f94566
+void execute_directory(const char *directory, DIR *d, usec_t timeout, char *argv[]) {
4f94566
+        pid_t executor_pid;
4f94566
+        int r;
4f94566
 
4f94566
         assert(directory);
4f94566
 
4f94566
-        /* Executes all binaries in a directory in parallel and
4f94566
-         * waits for them to finish. */
4f94566
+        /* Executes all binaries in a directory in parallel and waits
4f94566
+         * for them to finish. Optionally a timeout is applied. */
4f94566
 
4f94566
-        if (!d) {
4f94566
-                if (!(_d = opendir(directory))) {
4f94566
+        executor_pid = fork();
4f94566
+        if (executor_pid < 0) {
4f94566
+                log_error("Failed to fork: %m");
4f94566
+                return;
4f94566
 
4f94566
-                        if (errno == ENOENT)
4f94566
-                                return;
4f94566
+        } else if (executor_pid == 0) {
4f94566
+                _cleanup_hashmap_free_free_ Hashmap *pids = NULL;
4f94566
+                _cleanup_closedir_ DIR *_d = NULL;
4f94566
+                struct dirent *de;
4f94566
+                sigset_t ss;
4f94566
 
4f94566
-                        log_error("Failed to enumerate directory %s: %m", directory);
4f94566
-                        return;
4f94566
-                }
4f94566
+                /* We fork this all off from a child process so that
4f94566
+                 * we can somewhat cleanly make use of SIGALRM to set
4f94566
+                 * a time limit */
4f94566
 
4f94566
-                d = _d;
4f94566
-        }
4f94566
+                reset_all_signal_handlers();
4f94566
 
4f94566
-        if (!(pids = hashmap_new(trivial_hash_func, trivial_compare_func))) {
4f94566
-                log_error("Failed to allocate set.");
4f94566
-                goto finish;
4f94566
-        }
4f94566
+                assert_se(sigemptyset(&ss) == 0);
4f94566
+                assert_se(sigprocmask(SIG_SETMASK, &ss, NULL) == 0);
4f94566
 
4f94566
-        while ((de = readdir(d))) {
4f94566
-                char *path;
4f94566
-                pid_t pid;
4f94566
-                int k;
4f94566
+                assert_se(prctl(PR_SET_PDEATHSIG, SIGTERM) == 0);
4f94566
 
4f94566
-                if (!dirent_is_file(de))
4f94566
-                        continue;
4f94566
+                if (!d) {
4f94566
+                        d = _d = opendir(directory);
4f94566
+                        if (!d) {
4f94566
+                                if (errno == ENOENT)
4f94566
+                                        _exit(EXIT_SUCCESS);
4f94566
 
4f94566
-                if (asprintf(&path, "%s/%s", directory, de->d_name) < 0) {
4f94566
-                        log_oom();
4f94566
-                        continue;
4f94566
+                                log_error("Failed to enumerate directory %s: %m", directory);
4f94566
+                                _exit(EXIT_FAILURE);
4f94566
+                        }
4f94566
                 }
4f94566
 
4f94566
-                if ((pid = fork()) < 0) {
4f94566
-                        log_error("Failed to fork: %m");
4f94566
-                        free(path);
4f94566
-                        continue;
4f94566
+                pids = hashmap_new(NULL, NULL);
4f94566
+                if (!pids) {
4f94566
+                        log_oom();
4f94566
+                        _exit(EXIT_FAILURE);
4f94566
                 }
4f94566
 
4f94566
-                if (pid == 0) {
4f94566
-                        char *_argv[2];
4f94566
-                        /* Child */
4f94566
+                FOREACH_DIRENT(de, d, break) {
4f94566
+                        _cleanup_free_ char *path = NULL;
4f94566
+                        pid_t pid;
4f94566
 
4f94566
-                        if (!argv) {
4f94566
-                                _argv[0] = path;
4f94566
-                                _argv[1] = NULL;
4f94566
-                                argv = _argv;
4f94566
-                        } else
4f94566
-                                argv[0] = path;
4f94566
+                        if (!dirent_is_file(de))
4f94566
+                                continue;
4f94566
 
4f94566
-                        execv(path, argv);
4f94566
+                        if (asprintf(&path, "%s/%s", directory, de->d_name) < 0) {
4f94566
+                                log_oom();
4f94566
+                                _exit(EXIT_FAILURE);
4f94566
+                        }
4f94566
 
4f94566
-                        log_error("Failed to execute %s: %m", path);
4f94566
-                        _exit(EXIT_FAILURE);
4f94566
-                }
4f94566
+                        pid = fork();
4f94566
+                        if (pid < 0) {
4f94566
+                                log_error("Failed to fork: %m");
4f94566
+                                continue;
4f94566
+                        } else if (pid == 0) {
4f94566
+                                char *_argv[2];
4f94566
 
4f94566
-                log_debug("Spawned %s as %lu", path, (unsigned long) pid);
4f94566
+                                assert_se(prctl(PR_SET_PDEATHSIG, SIGTERM) == 0);
4f94566
 
4f94566
-                if ((k = hashmap_put(pids, UINT_TO_PTR(pid), path)) < 0) {
4f94566
-                        log_error("Failed to add PID to set: %s", strerror(-k));
4f94566
-                        free(path);
4f94566
-                }
4f94566
-        }
4f94566
+                                if (!argv) {
4f94566
+                                        _argv[0] = path;
4f94566
+                                        _argv[1] = NULL;
4f94566
+                                        argv = _argv;
4f94566
+                                } else
4f94566
+                                        argv[0] = path;
4f94566
 
4f94566
-        while (!hashmap_isempty(pids)) {
4f94566
-                pid_t pid = PTR_TO_UINT(hashmap_first_key(pids));
4f94566
-                siginfo_t si = {};
4f94566
-                char *path;
4f94566
+                                execv(path, argv);
4f94566
+                                log_error("Failed to execute %s: %m", path);
4f94566
+                                _exit(EXIT_FAILURE);
4f94566
+                        }
4f94566
 
4f94566
-                if (waitid(P_PID, pid, &si, WEXITED) < 0) {
4f94566
 
4f94566
-                        if (errno == EINTR)
4f94566
-                                continue;
4f94566
+                        log_debug("Spawned %s as %lu.", path, (long unsigned) pid);
4f94566
 
4f94566
-                        log_error("waitid() failed: %m");
4f94566
-                        goto finish;
4f94566
+                        r = hashmap_put(pids, UINT_TO_PTR(pid), path);
4f94566
+                        if (r < 0) {
4f94566
+                                log_oom();
4f94566
+                                _exit(EXIT_FAILURE);
4f94566
+                        }
4f94566
+
4f94566
+                        path = NULL;
4f94566
                 }
4f94566
 
4f94566
-                if ((path = hashmap_remove(pids, UINT_TO_PTR(si.si_pid)))) {
4f94566
-                        if (!is_clean_exit(si.si_code, si.si_status, NULL)) {
4f94566
-                                if (si.si_code == CLD_EXITED)
4f94566
-                                        log_error("%s exited with exit status %i.", path, si.si_status);
4f94566
-                                else
4f94566
-                                        log_error("%s terminated by signal %s.", path, signal_to_string(si.si_status));
4f94566
-                        } else
4f94566
-                                log_debug("%s exited successfully.", path);
4f94566
+                /* Abort execution of this process after the
4f94566
+                 * timout. We simply rely on SIGALRM as default action
4f94566
+                 * terminating the process, and turn on alarm(). */
4f94566
+
4f94566
+                if (timeout != (usec_t) -1)
4f94566
+                        alarm((timeout + USEC_PER_SEC - 1) / USEC_PER_SEC);
4f94566
+
4f94566
+                while (!hashmap_isempty(pids)) {
4f94566
+                        _cleanup_free_ char *path = NULL;
4f94566
+                        pid_t pid;
4f94566
+
4f94566
+                        pid = PTR_TO_UINT(hashmap_first_key(pids));
4f94566
+                        assert(pid > 0);
4f94566
 
4f94566
-                        free(path);
4f94566
+                        path = hashmap_remove(pids, UINT_TO_PTR(pid));
4f94566
+                        assert(path);
4f94566
+
4f94566
+                        wait_for_terminate_and_warn(path, pid);
4f94566
                 }
4f94566
-        }
4f94566
 
4f94566
-finish:
4f94566
-        if (_d)
4f94566
-                closedir(_d);
4f94566
+                _exit(EXIT_SUCCESS);
4f94566
+        }
4f94566
 
4f94566
-        if (pids)
4f94566
-                hashmap_free_free(pids);
4f94566
+        wait_for_terminate_and_warn(directory, executor_pid);
4f94566
 }
4f94566
 
4f94566
 int kill_and_sigcont(pid_t pid, int sig) {
4f94566
diff --git a/src/shared/util.h b/src/shared/util.h
4f94566
index 02621a7..47a3dc9 100644
4f94566
--- a/src/shared/util.h
4f94566
+++ b/src/shared/util.h
4f94566
@@ -432,7 +432,7 @@ bool tty_is_console(const char *tty) _pure_;
4f94566
 int vtnr_from_tty(const char *tty);
4f94566
 const char *default_term_for_tty(const char *tty);
4f94566
 
4f94566
-void execute_directory(const char *directory, DIR *_d, char *argv[]);
4f94566
+void execute_directory(const char *directory, DIR *_d, usec_t timeout, char *argv[]);
4f94566
 
4f94566
 int kill_and_sigcont(pid_t pid, int sig);
4f94566
 
4f94566
diff --git a/src/sleep/sleep.c b/src/sleep/sleep.c
4f94566
index c228cb6..8e847db 100644
4f94566
--- a/src/sleep/sleep.c
4f94566
+++ b/src/sleep/sleep.c
4f94566
@@ -25,14 +25,15 @@
4f94566
 #include <string.h>
4f94566
 #include <getopt.h>
4f94566
 
4f94566
-#include "systemd/sd-id128.h"
4f94566
-#include "systemd/sd-messages.h"
4f94566
+#include "sd-id128.h"
4f94566
+#include "sd-messages.h"
4f94566
 #include "log.h"
4f94566
 #include "util.h"
4f94566
 #include "strv.h"
4f94566
 #include "fileio.h"
4f94566
 #include "build.h"
4f94566
 #include "sleep-config.h"
4f94566
+#include "def.h"
4f94566
 
4f94566
 static char* arg_verb = NULL;
4f94566
 
4f94566
@@ -41,9 +42,12 @@ static int write_mode(char **modes) {
4f94566
         char **mode;
4f94566
 
4f94566
         STRV_FOREACH(mode, modes) {
4f94566
-                int k = write_string_file("/sys/power/disk", *mode);
4f94566
+                int k;
4f94566
+
4f94566
+                k = write_string_file("/sys/power/disk", *mode);
4f94566
                 if (k == 0)
4f94566
                         return 0;
4f94566
+
4f94566
                 log_debug("Failed to write '%s' to /sys/power/disk: %s",
4f94566
                           *mode, strerror(-k));
4f94566
                 if (r == 0)
4f94566
@@ -106,7 +110,7 @@ static int execute(char **modes, char **states) {
4f94566
         arguments[1] = (char*) "pre";
4f94566
         arguments[2] = arg_verb;
4f94566
         arguments[3] = NULL;
4f94566
-        execute_directory(SYSTEM_SLEEP_PATH, NULL, arguments);
4f94566
+        execute_directory(SYSTEM_SLEEP_PATH, NULL, DEFAULT_TIMEOUT_USEC, arguments);
4f94566
 
4f94566
         log_struct(LOG_INFO,
4f94566
                    MESSAGE_ID(SD_MESSAGE_SLEEP_START),
4f94566
@@ -125,7 +129,7 @@ static int execute(char **modes, char **states) {
4f94566
                    NULL);
4f94566
 
4f94566
         arguments[1] = (char*) "post";
4f94566
-        execute_directory(SYSTEM_SLEEP_PATH, NULL, arguments);
4f94566
+        execute_directory(SYSTEM_SLEEP_PATH, NULL, DEFAULT_TIMEOUT_USEC, arguments);
4f94566
 
4f94566
         return r;
4f94566
 }