chiark
/
gitweb
/
~ianmdlvl
/
elogind.git
/ commitdiff
commit
grep
author
committer
pickaxe
?
search:
re
summary
|
shortlog
|
log
|
commit
| commitdiff |
tree
raw
|
patch
|
inline
| side by side (from parent 1:
ed7e5fe
)
util: generalize code that checks whether PIDs are alive or unwaited for
author
Lennart Poettering
<lennart@poettering.net>
Mon, 17 Feb 2014 17:28:53 +0000
(18:28 +0100)
committer
Lennart Poettering
<lennart@poettering.net>
Tue, 18 Feb 2014 01:51:47 +0000
(
02:51
+0100)
src/core/service.c
patch
|
blob
|
history
src/core/unit.c
patch
|
blob
|
history
src/libsystemd/sd-bus/bus-creds.c
patch
|
blob
|
history
src/shared/util.c
patch
|
blob
|
history
src/shared/util.h
patch
|
blob
|
history
src/tty-ask-password-agent/tty-ask-password-agent.c
patch
|
blob
|
history
diff --git
a/src/core/service.c
b/src/core/service.c
index 8e07bd0170c187513812185b1e21dac8fa7f41ba..c5ff0dec34f73ea07bce542d063dde15a3741c51 100644
(file)
--- 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;
}
return r;
}
- if (
kill(pid, 0) < 0 && errno != EPERM
) {
+ if (
!pid_is_alive(pid)
) {
if (may_warn)
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;
}
return -ESRCH;
}
@@
-1579,7
+1564,7
@@
static int service_coldplug(Unit *u) {
return r;
}
return r;
}
- if (pid_
vali
d(s->main_pid) &&
+ if (pid_
is_unwaite
d(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,
((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;
}
return r;
}
- if (pid_
vali
d(s->control_pid) &&
+ if (pid_
is_unwaite
d(s->control_pid) &&
IN_SET(s->deserialized_state,
SERVICE_START_PRE, SERVICE_START, SERVICE_START_POST,
SERVICE_RELOAD,
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)
/* 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
/* .. 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 d529638f86a300b75a7f10a552efb6b9a24dfcea..21ad434f9d7d88af50bd0a13d58e6693df721f72 100644
(file)
--- 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 (pid == except1 || pid == except2)
continue;
- if (
kill(pid, 0) < 0 && errno == ESRCH
)
+ if (
!pid_is_unwaited(pid)
)
set_remove(u->pids, e);
}
}
set_remove(u->pids, e);
}
}
diff --git
a/src/libsystemd/sd-bus/bus-creds.c
b/src/libsystemd/sd-bus/bus-creds.c
index 677e94b3ba88c8a93cd9d057c6913096863c2846..6143254a60761ad44e7cfd52a1fb1b91d4137af0 100644
(file)
--- a/
src/libsystemd/sd-bus/bus-creds.c
+++ b/
src/libsystemd/sd-bus/bus-creds.c
@@
-148,7
+148,7
@@
_public_ int sd_bus_creds_new_from_pid(pid_t pid, uint64_t mask, sd_bus_creds **
/* Check if the process existed at all, in case we haven't
* figured that out already */
/* Check if the process existed at all, in case we haven't
* figured that out already */
- if (
kill(pid, 0) < 0 && errno == ESRCH
) {
+ if (
!pid_is_alive(pid)
) {
sd_bus_creds_unref(c);
return -ESRCH;
}
sd_bus_creds_unref(c);
return -ESRCH;
}
diff --git
a/src/shared/util.c
b/src/shared/util.c
index 4f3145b2bc493ec1efff7d00afe29e9355e83cec..72b1e2f3c89da92c302b10c6f80e3c5940d42f28 100644
(file)
--- a/
src/shared/util.c
+++ b/
src/shared/util.c
@@
-6045,7
+6045,9
@@
int namespace_enter(int pidns_fd, int mntns_fd, int root_fd) {
return 0;
}
return 0;
}
-bool pid_valid(pid_t pid) {
+bool pid_is_unwaited(pid_t pid) {
+ /* Checks whether a PID is still valid at all, including a zombie */
+
if (pid <= 0)
return false;
if (pid <= 0)
return false;
@@
-6055,6
+6057,21
@@
bool pid_valid(pid_t pid) {
return errno != ESRCH;
}
return errno != ESRCH;
}
+bool pid_is_alive(pid_t pid) {
+ int r;
+
+ /* Checks whether a PID is still valid and not a zombie */
+
+ if (pid <= 0)
+ return false;
+
+ r = get_process_state(pid);
+ if (r == -ENOENT || r == 'Z')
+ return false;
+
+ return true;
+}
+
int getpeercred(int fd, struct ucred *ucred) {
socklen_t n = sizeof(struct ucred);
struct ucred u;
int getpeercred(int fd, struct ucred *ucred) {
socklen_t n = sizeof(struct ucred);
struct ucred u;
diff --git
a/src/shared/util.h
b/src/shared/util.h
index 7c88dad631d16fe0c705c759754a86a583cb0ea2..a41348e32ead839f63c583278aa512913656b80e 100644
(file)
--- a/
src/shared/util.h
+++ b/
src/shared/util.h
@@
-858,7
+858,8
@@
int container_get_leader(const char *machine, pid_t *pid);
int namespace_open(pid_t pid, int *pidns_fd, int *mntns_fd, int *root_fd);
int namespace_enter(int pidns_fd, int mntns_fd, int root_fd);
int namespace_open(pid_t pid, int *pidns_fd, int *mntns_fd, int *root_fd);
int namespace_enter(int pidns_fd, int mntns_fd, int root_fd);
-bool pid_valid(pid_t pid);
+bool pid_is_alive(pid_t pid);
+bool pid_is_unwaited(pid_t pid);
int getpeercred(int fd, struct ucred *ucred);
int getpeersec(int fd, char **ret);
int getpeercred(int fd, struct ucred *ucred);
int getpeersec(int fd, char **ret);
diff --git
a/src/tty-ask-password-agent/tty-ask-password-agent.c
b/src/tty-ask-password-agent/tty-ask-password-agent.c
index c0451c0fd0fabf3daf8faf73a4e7acd1b6b9303d..08177c6d04f080bb02e76cfe966183fe118b55ea 100644
(file)
--- a/
src/tty-ask-password-agent/tty-ask-password-agent.c
+++ b/
src/tty-ask-password-agent/tty-ask-password-agent.c
@@
-294,9
+294,7
@@
static int parse_password(const char *filename, char **wall) {
}
}
}
}
- if (pid > 0 &&
- kill(pid, 0) < 0 &&
- errno == ESRCH) {
+ if (pid > 0 && !pid_is_alive(pid)) {
r = 0;
goto finish;
}
r = 0;
goto finish;
}