From 9f5650aecb22a1d864b2c0b749910556795f1490 Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Mon, 17 Feb 2014 18:28:53 +0100 Subject: util: generalize code that checks whether PIDs are alive or unwaited for --- src/core/service.c | 25 +++++-------------------- src/core/unit.c | 2 +- 2 files changed, 6 insertions(+), 21 deletions(-) (limited to 'src/core') diff --git a/src/core/service.c b/src/core/service.c index 8e07bd0170..c5ff0dec34 100644 --- a/src/core/service.c +++ b/src/core/service.c @@ -1368,25 +1368,10 @@ static int service_load_pid_file(Service *s, bool may_warn) { return r; } - if (kill(pid, 0) < 0 && errno != EPERM) { + if (!pid_is_alive(pid)) { if (may_warn) - log_info_unit(UNIT(s)->id, - "PID "PID_FMT" read from file %s does not exist.", - pid, s->pid_file); - return -ESRCH; - } + log_info_unit(UNIT(s)->id, "PID "PID_FMT" read from file %s does not exist or is a zombie.", pid, s->pid_file); - r = get_process_state(pid); - if (r < 0) { - if (may_warn) - log_info_unit(UNIT(s)->id, "Failed to read /proc/%d/stat: %s", - pid, strerror(-r)); - return r; - } else if (r == 'Z') { - if (may_warn) - log_info_unit(UNIT(s)->id, - "PID "PID_FMT" read from file %s is a zombie.", - pid, s->pid_file); return -ESRCH; } @@ -1579,7 +1564,7 @@ static int service_coldplug(Unit *u) { return r; } - if (pid_valid(s->main_pid) && + if (pid_is_unwaited(s->main_pid) && ((s->deserialized_state == SERVICE_START && IN_SET(s->type, SERVICE_FORKING, SERVICE_DBUS, SERVICE_ONESHOT, SERVICE_NOTIFY)) || IN_SET(s->deserialized_state, SERVICE_START, SERVICE_START_POST, @@ -1592,7 +1577,7 @@ static int service_coldplug(Unit *u) { return r; } - if (pid_valid(s->control_pid) && + if (pid_is_unwaited(s->control_pid) && IN_SET(s->deserialized_state, SERVICE_START_PRE, SERVICE_START, SERVICE_START_POST, SERVICE_RELOAD, @@ -1824,7 +1809,7 @@ static int main_pid_good(Service *s) { /* If it's an alien child let's check if it is still * alive ... */ if (s->main_pid_alien && s->main_pid > 0) - return kill(s->main_pid, 0) >= 0 || errno != ESRCH; + return pid_is_alive(s->main_pid); /* .. otherwise assume we'll get a SIGCHLD for it, * which we really should wait for to collect exit diff --git a/src/core/unit.c b/src/core/unit.c index d529638f86..21ad434f9d 100644 --- a/src/core/unit.c +++ b/src/core/unit.c @@ -1833,7 +1833,7 @@ void unit_tidy_watch_pids(Unit *u, pid_t except1, pid_t except2) { if (pid == except1 || pid == except2) continue; - if (kill(pid, 0) < 0 && errno == ESRCH) + if (!pid_is_unwaited(pid)) set_remove(u->pids, e); } } -- cgit v1.2.3-54-g00ecf