mirror of
https://github.com/systemd/systemd.git
synced 2025-03-10 16:58:28 +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:
parent
d7d748548b
commit
a7a877697f
@ -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);
|
||||
}
|
||||
|
@ -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);
|
||||
|
@ -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;
|
||||
|
@ -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))
|
||||
|
@ -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 */
|
||||
|
Loading…
x
Reference in New Issue
Block a user