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