process-util: add pidref_is_my_child()

This commit is contained in:
Lennart Poettering
2023-10-18 14:49:40 +02:00
parent becdfcb9f1
commit 6774be4206
4 changed files with 29 additions and 3 deletions
+20
View File
@@ -1015,6 +1015,9 @@ int pid_is_my_child(pid_t pid) {
pid_t ppid;
int r;
if (pid < 0)
return -ESRCH;
if (pid <= 1)
return false;
@@ -1025,6 +1028,23 @@ int pid_is_my_child(pid_t pid) {
return ppid == getpid_cached();
}
int pidref_is_my_child(const PidRef *pid) {
int r, result;
if (!pidref_is_set(pid))
return -ESRCH;
result = pid_is_my_child(pid->pid);
if (result < 0)
return result;
r = pidref_verify(pid);
if (r < 0)
return r;
return result;
}
bool pid_is_unwaited(pid_t pid) {
/* Checks whether a PID is still valid at all, including a zombie */
+1
View File
@@ -90,6 +90,7 @@ int pid_is_alive(pid_t pid);
int pidref_is_alive(const PidRef *pidref);
bool pid_is_unwaited(pid_t pid);
int pid_is_my_child(pid_t pid);
int pidref_is_my_child(const PidRef *pidref);
int pid_from_same_root_fs(pid_t pid);
bool is_main_thread(void);
+1 -1
View File
@@ -3175,7 +3175,7 @@ int unit_search_main_pid(Unit *u, PidRef *ret) {
if (pidref_equal(&pidref, &npidref)) /* seen already, cgroupfs reports duplicates! */
continue;
if (pid_is_my_child(npidref.pid) == 0) /* ignore processes further down the tree */
if (pidref_is_my_child(&npidref) <= 0) /* ignore processes further down the tree */
continue;
if (pidref_is_set(&pidref) != 0)
+7 -2
View File
@@ -180,6 +180,8 @@ static void service_unwatch_pid_file(Service *s) {
}
static int service_set_main_pidref(Service *s, PidRef *pidref) {
int r;
assert(s);
/* Takes ownership of the specified pidref on success, but not on failure. */
@@ -205,11 +207,14 @@ static int service_set_main_pidref(Service *s, PidRef *pidref) {
s->main_pid = TAKE_PIDREF(*pidref);
s->main_pid_known = true;
s->main_pid_alien = pid_is_my_child(s->main_pid.pid) == 0;
if (s->main_pid_alien)
r = pidref_is_my_child(&s->main_pid);
if (r < 0)
log_unit_warning_errno(UNIT(s), r, "Can't determine if process "PID_FMT" is our child, assuming it is not: %m", s->main_pid.pid);
else if (r == 0)
log_unit_warning(UNIT(s), "Supervising process "PID_FMT" which is not our child. We'll most likely not notice when it exits.", s->main_pid.pid);
s->main_pid_alien = r <= 0;
return 0;
}