dcavalca / rpms / systemd

Forked from rpms/systemd 4 months ago
Clone
9fc0f6
From f04488d2842544de263e245ee1a21e789ec818a8 Mon Sep 17 00:00:00 2001
9fc0f6
From: Yuxuan Shui <yshuiv7@gmail.com>
9fc0f6
Date: Sat, 15 Feb 2014 02:38:50 +0800
9fc0f6
Subject: [PATCH] core: fix detection of dead processes
9fc0f6
9fc0f6
Commit 5ba6985b moves the UNIT_VTABLE(u)->sigchld_event before systemd
9fc0f6
actually reaps the zombie. Which leads to service_load_pid_file accepting
9fc0f6
zombie as a valid pid.
9fc0f6
9fc0f6
This fixes timeouts like:
9fc0f6
[ 2746.602243] systemd[1]: chronyd.service stop-sigterm timed out. Killing.
9fc0f6
[ 2836.852545] systemd[1]: chronyd.service still around after SIGKILL. Ignoring.
9fc0f6
[ 2927.102187] systemd[1]: chronyd.service stop-final-sigterm timed out. Killing.
9fc0f6
[ 3017.352560] systemd[1]: chronyd.service still around after final SIGKILL. Entering failed mode.
9fc0f6
---
9fc0f6
 src/core/service.c |  8 ++++++++
9fc0f6
 src/shared/util.c  | 25 +++++++++++++++++++++++++
9fc0f6
 src/shared/util.h  |  1 +
9fc0f6
 3 files changed, 34 insertions(+)
9fc0f6
9fc0f6
diff --git a/src/core/service.c b/src/core/service.c
9fc0f6
index dff5286..3eacf3b 100644
9fc0f6
--- a/src/core/service.c
9fc0f6
+++ b/src/core/service.c
9fc0f6
@@ -1429,6 +1429,14 @@ static int service_load_pid_file(Service *s, bool may_warn) {
9fc0f6
                 return -ESRCH;
9fc0f6
         }
9fc0f6
 
9fc0f6
+        if (get_process_state(pid) == 'Z') {
9fc0f6
+                if (may_warn)
9fc0f6
+                        log_info_unit(UNIT(s)->id,
9fc0f6
+                                      "PID "PID_FMT" read from file %s is a zombie.",
9fc0f6
+                                      pid, s->pid_file);
9fc0f6
+                return -ESRCH;
9fc0f6
+        }
9fc0f6
+
9fc0f6
         if (s->main_pid_known) {
9fc0f6
                 if (pid == s->main_pid)
9fc0f6
                         return 0;
9fc0f6
diff --git a/src/shared/util.c b/src/shared/util.c
9fc0f6
index 2086847..090a204 100644
9fc0f6
--- a/src/shared/util.c
9fc0f6
+++ b/src/shared/util.c
9fc0f6
@@ -558,6 +558,31 @@ char *truncate_nl(char *s) {
9fc0f6
         return s;
9fc0f6
 }
9fc0f6
 
9fc0f6
+int get_process_state(pid_t pid) {
9fc0f6
+        const char *p;
9fc0f6
+        char state;
9fc0f6
+        int r;
9fc0f6
+        _cleanup_free_ char *line = NULL;
9fc0f6
+
9fc0f6
+        assert(pid >= 0);
9fc0f6
+
9fc0f6
+        p = procfs_file_alloca(pid, "stat");
9fc0f6
+        r = read_one_line_file(p, &line);
9fc0f6
+        if (r < 0)
9fc0f6
+                return r;
9fc0f6
+
9fc0f6
+        p = strrchr(line, ')');
9fc0f6
+        if (!p)
9fc0f6
+                return -EIO;
9fc0f6
+
9fc0f6
+        p++;
9fc0f6
+
9fc0f6
+        if (sscanf(p, " %c", &state) != 1)
9fc0f6
+                return -EIO;
9fc0f6
+
9fc0f6
+        return (unsigned char) state;
9fc0f6
+}
9fc0f6
+
9fc0f6
 int get_process_comm(pid_t pid, char **name) {
9fc0f6
         const char *p;
9fc0f6
 
9fc0f6
diff --git a/src/shared/util.h b/src/shared/util.h
9fc0f6
index 222abe0..1d60687 100644
9fc0f6
--- a/src/shared/util.h
9fc0f6
+++ b/src/shared/util.h
9fc0f6
@@ -215,6 +215,7 @@ char *file_in_same_dir(const char *path, const char *filename);
9fc0f6
 
9fc0f6
 int rmdir_parents(const char *path, const char *stop);
9fc0f6
 
9fc0f6
+char get_process_state(pid_t pid);
9fc0f6
 int get_process_comm(pid_t pid, char **name);
9fc0f6
 int get_process_cmdline(pid_t pid, size_t max_length, bool comm_fallback, char **line);
9fc0f6
 int get_process_exe(pid_t pid, char **name);