]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
pidref: add new pidref_is_self() helper
authorLennart Poettering <lennart@poettering.net>
Tue, 17 Oct 2023 08:17:49 +0000 (10:17 +0200)
committerLennart Poettering <lennart@poettering.net>
Wed, 18 Oct 2023 12:39:33 +0000 (14:39 +0200)
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)

src/basic/pidref.c
src/basic/pidref.h
src/core/cgroup.c
src/core/service.c
src/core/unit.c

index 3e37cce0ccd8ae891acda84148e6277695667151..aa4ca063babc692535e5e74dc2806979d1f4b961 100644 (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 */
 }
 
+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);
 }
index 61d091a34443dca3bf567c399bff8ebd9d90f0e9..0fdc02a54bb78ca85333a7195a893d28e5b7e6b5 100644 (file)
@@ -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);
index dd46bf92373ed72274362bf4b4859c054b089f70..bb867944360204ffc3c4dd0f627146e7b752efa0 100644 (file)
@@ -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;
index 099b0f10452bbf7ad2c0d1602b8ba5a60f9aa367..bb268eb9397a06a32c236745e4119be1ad82390e 100644 (file)
@@ -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))
index 507d350e7ede761d8483b34949f584e5e7b830cf..4300f18d9bb411c62f108f038d1ae986f18036be 100644 (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.");
 
         /* 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 */