From: Lennart Poettering Date: Tue, 17 Oct 2023 08:17:49 +0000 (+0200) Subject: pidref: add new pidref_is_self() helper X-Git-Tag: v255-rc1~209^2~10 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=a7a877697f07d764b12694316917aa1dc6ff64fb;p=thirdparty%2Fsystemd.git pidref: add new pidref_is_self() helper This simply checks if the specified PidRef refers to the process we are running in. (In case you wonder why this is not a static inline: to avoid cyclic header inclusion problems between pidref.h + process-util.h) --- diff --git a/src/basic/pidref.c b/src/basic/pidref.c index 3e37cce0ccd..aa4ca063bab 100644 --- a/src/basic/pidref.c +++ b/src/basic/pidref.c @@ -263,6 +263,13 @@ int pidref_verify(const PidRef *pidref) { return 1; /* We have a pidfd and it still points to the PID we have, hence all is *really* OK → return 1 */ } +bool pidref_is_self(const PidRef *pidref) { + if (!pidref) + return false; + + return pidref->pid == getpid_cached(); +} + static void pidref_hash_func(const PidRef *pidref, struct siphash *state) { siphash24_compress(&pidref->pid, sizeof(pidref->pid), state); } diff --git a/src/basic/pidref.h b/src/basic/pidref.h index 61d091a3444..0fdc02a54bb 100644 --- a/src/basic/pidref.h +++ b/src/basic/pidref.h @@ -43,6 +43,8 @@ static inline int pidref_set_self(PidRef *pidref) { return pidref_set_pid(pidref, 0); } +bool pidref_is_self(const PidRef *pidref); + void pidref_done(PidRef *pidref); PidRef *pidref_free(PidRef *pidref); DEFINE_TRIVIAL_CLEANUP_FUNC(PidRef*, pidref_free); diff --git a/src/core/cgroup.c b/src/core/cgroup.c index dd46bf92373..bb867944360 100644 --- a/src/core/cgroup.c +++ b/src/core/cgroup.c @@ -3913,7 +3913,7 @@ Unit *manager_get_unit_by_pidref(Manager *m, PidRef *pid) { if (!pidref_is_set(pid)) return NULL; - if (pid->pid == getpid_cached()) + if (pidref_is_self(pid)) return hashmap_get(m->units, SPECIAL_INIT_SCOPE); if (pid->pid == 1) return NULL; diff --git a/src/core/service.c b/src/core/service.c index 099b0f10452..bb268eb9397 100644 --- a/src/core/service.c +++ b/src/core/service.c @@ -190,7 +190,7 @@ static int service_set_main_pidref(Service *s, PidRef *pidref) { if (pidref->pid <= 1) return -EINVAL; - if (pidref->pid == getpid_cached()) + if (pidref_is_self(pidref)) return -EINVAL; if (pidref_equal(&s->main_pid, pidref) && s->main_pid_known) { @@ -1061,7 +1061,7 @@ static int service_is_suitable_main_pid(Service *s, PidRef *pid, int prio) { * PID is questionnable but should be accepted if the source of configuration is trusted. > 0 if the PID is * good */ - if (pid->pid == getpid_cached() || pid->pid == 1) + if (pidref_is_self(pid) || pid->pid == 1) return log_unit_full_errno(UNIT(s), prio, SYNTHETIC_ERRNO(EPERM), "New main PID "PID_FMT" is the manager, refusing.", pid->pid); if (pidref_equal(pid, &s->control_pid)) diff --git a/src/core/unit.c b/src/core/unit.c index 507d350e7ed..4300f18d9bb 100644 --- a/src/core/unit.c +++ b/src/core/unit.c @@ -5949,7 +5949,7 @@ int unit_pid_attachable(Unit *u, PidRef *pid, sd_bus_error *error) { return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Process identifier is not valid."); /* Some extra safety check */ - if (pid->pid == 1 || pid->pid == getpid_cached()) + if (pid->pid == 1 || pidref_is_self(pid)) return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Process " PID_FMT " is a manager process, refusing.", pid->pid); /* Don't even begin to bother with kernel threads */