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