1
0
mirror of https://github.com/systemd/systemd.git synced 2025-03-11 20:58:27 +03:00

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)
This commit is contained in:
Lennart Poettering 2023-10-17 10:17:49 +02:00
parent d7d748548b
commit a7a877697f
5 changed files with 13 additions and 4 deletions

View File

@ -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 */ 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) { static void pidref_hash_func(const PidRef *pidref, struct siphash *state) {
siphash24_compress(&pidref->pid, sizeof(pidref->pid), state); siphash24_compress(&pidref->pid, sizeof(pidref->pid), state);
} }

View File

@ -43,6 +43,8 @@ static inline int pidref_set_self(PidRef *pidref) {
return pidref_set_pid(pidref, 0); return pidref_set_pid(pidref, 0);
} }
bool pidref_is_self(const PidRef *pidref);
void pidref_done(PidRef *pidref); void pidref_done(PidRef *pidref);
PidRef *pidref_free(PidRef *pidref); PidRef *pidref_free(PidRef *pidref);
DEFINE_TRIVIAL_CLEANUP_FUNC(PidRef*, pidref_free); DEFINE_TRIVIAL_CLEANUP_FUNC(PidRef*, pidref_free);

View File

@ -3913,7 +3913,7 @@ Unit *manager_get_unit_by_pidref(Manager *m, PidRef *pid) {
if (!pidref_is_set(pid)) if (!pidref_is_set(pid))
return NULL; return NULL;
if (pid->pid == getpid_cached()) if (pidref_is_self(pid))
return hashmap_get(m->units, SPECIAL_INIT_SCOPE); return hashmap_get(m->units, SPECIAL_INIT_SCOPE);
if (pid->pid == 1) if (pid->pid == 1)
return NULL; return NULL;

View File

@ -190,7 +190,7 @@ static int service_set_main_pidref(Service *s, PidRef *pidref) {
if (pidref->pid <= 1) if (pidref->pid <= 1)
return -EINVAL; return -EINVAL;
if (pidref->pid == getpid_cached()) if (pidref_is_self(pidref))
return -EINVAL; return -EINVAL;
if (pidref_equal(&s->main_pid, pidref) && s->main_pid_known) { 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 * PID is questionnable but should be accepted if the source of configuration is trusted. > 0 if the PID is
* good */ * 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); 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)) if (pidref_equal(pid, &s->control_pid))

View File

@ -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."); return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Process identifier is not valid.");
/* Some extra safety check */ /* 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); 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 */ /* Don't even begin to bother with kernel threads */