]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
process-util: port pidref_get_uid() and pidref_is_my_child() to pidfd helpers
authorMike Yuan <me@yhndnzj.com>
Wed, 20 Nov 2024 14:17:30 +0000 (15:17 +0100)
committerMike Yuan <me@yhndnzj.com>
Sat, 4 Jan 2025 16:48:22 +0000 (17:48 +0100)
src/basic/namespace-util.c
src/basic/pidfd-util.c
src/basic/process-util.c
src/basic/process-util.h
src/test/test-process-util.c

index 74876100697f72fb398e8d914d5b64a6ddbb6f8c..9293999a019fed1fa7c56a78ac567acf9f839ba8 100644 (file)
@@ -349,7 +349,7 @@ int namespace_get_leader(pid_t pid, NamespaceType type, pid_t *ret) {
         for (;;) {
                 pid_t ppid;
 
-                r = get_process_ppid(pid, &ppid);
+                r = pid_get_ppid(pid, &ppid);
                 if (r < 0)
                         return r;
 
index b2a2188e4bab0b7001e4aa7ca81df2b70cb9cd37..204439e4447590cd3fdef0930bd93c8b199491b0 100644 (file)
@@ -188,7 +188,7 @@ int pidfd_get_ppid(int fd, pid_t *ret) {
 
         assert(FLAGS_SET(info.mask, PIDFD_INFO_PID));
 
-        if (info.ppid == 0)
+        if (info.ppid == 0) /* See comments in pid_get_ppid() */
                 return -EADDRNOTAVAIL;
 
         if (ret)
index e0ba9126339343f6836fee72b12b1781e83c323a..f412d202eb8765f4e6e96665b5bf0797a3cd93ee 100644 (file)
@@ -48,6 +48,7 @@
 #include "nulstr-util.h"
 #include "parse-util.h"
 #include "path-util.h"
+#include "pidfd-util.h"
 #include "process-util.h"
 #include "raw-clone.h"
 #include "rlimit-util.h"
@@ -557,7 +558,6 @@ int pid_get_uid(pid_t pid, uid_t *ret) {
 }
 
 int pidref_get_uid(const PidRef *pid, uid_t *ret) {
-        uid_t uid;
         int r;
 
         if (!pidref_is_set(pid))
@@ -566,6 +566,13 @@ int pidref_get_uid(const PidRef *pid, uid_t *ret) {
         if (pidref_is_remote(pid))
                 return -EREMOTE;
 
+        if (pid->fd >= 0) {
+                r = pidfd_get_uid(pid->fd, ret);
+                if (!ERRNO_IS_NEG_NOT_SUPPORTED(r))
+                        return r;
+        }
+
+        uid_t uid;
         r = pid_get_uid(pid->pid, &uid);
         if (r < 0)
                 return r;
@@ -651,7 +658,7 @@ int get_process_environ(pid_t pid, char **ret) {
         return 0;
 }
 
-int get_process_ppid(pid_t pid, pid_t *ret) {
+int pid_get_ppid(pid_t pid, pid_t *ret) {
         _cleanup_free_ char *line = NULL;
         unsigned long ppid;
         const char *p;
@@ -706,6 +713,35 @@ int get_process_ppid(pid_t pid, pid_t *ret) {
         return 0;
 }
 
+int pidref_get_ppid(const PidRef *pidref, pid_t *ret) {
+        int r;
+
+        if (!pidref_is_set(pidref))
+                return -ESRCH;
+
+        if (pidref_is_remote(pidref))
+                return -EREMOTE;
+
+        if (pidref->fd >= 0) {
+                r = pidfd_get_ppid(pidref->fd, ret);
+                if (!ERRNO_IS_NEG_NOT_SUPPORTED(r))
+                        return r;
+        }
+
+        pid_t ppid;
+        r = pid_get_ppid(pidref->pid, ret ? &ppid : NULL);
+        if (r < 0)
+                return r;
+
+        r = pidref_verify(pidref);
+        if (r < 0)
+                return r;
+
+        if (ret)
+                *ret = ppid;
+        return 0;
+}
+
 int pid_get_start_time(pid_t pid, usec_t *ret) {
         _cleanup_free_ char *line = NULL;
         const char *p;
@@ -1046,41 +1082,32 @@ int getenv_for_pid(pid_t pid, const char *field, char **ret) {
         return 0;
 }
 
-int pid_is_my_child(pid_t pid) {
-        pid_t ppid;
+int pidref_is_my_child(const PidRef *pid) {
         int r;
 
-        if (pid < 0)
+        if (!pidref_is_set(pid))
                 return -ESRCH;
 
-        if (pid <= 1)
+        if (pidref_is_remote(pid))
+                return -EREMOTE;
+
+        if (pid->pid == 1 || pidref_is_self(pid))
                 return false;
 
-        r = get_process_ppid(pid, &ppid);
+        pid_t ppid;
+        r = pidref_get_ppid(pid, &ppid);
         if (r < 0)
                 return r;
 
         return ppid == getpid_cached();
 }
 
-int pidref_is_my_child(const PidRef *pid) {
-        int r, result;
-
-        if (!pidref_is_set(pid))
-                return -ESRCH;
-
-        if (pidref_is_remote(pid))
-                return -EREMOTE;
-
-        result = pid_is_my_child(pid->pid);
-        if (result < 0)
-                return result;
+int pid_is_my_child(pid_t pid) {
 
-        r = pidref_verify(pid);
-        if (r < 0)
-                return r;
+        if (pid == 0)
+                return false;
 
-        return result;
+        return pidref_is_my_child(&PIDREF_MAKE_FROM_PID(pid));
 }
 
 int pid_is_unwaited(pid_t pid) {
index 78cf90d370692ab919920efb6843ef39c72f4ecf..b436a7b1b31d940ee35327034cce30227d33f237 100644 (file)
@@ -52,9 +52,10 @@ int get_process_gid(pid_t pid, gid_t *ret);
 int get_process_cwd(pid_t pid, char **ret);
 int get_process_root(pid_t pid, char **ret);
 int get_process_environ(pid_t pid, char **ret);
-int get_process_ppid(pid_t pid, pid_t *ret);
+int pid_get_ppid(pid_t pid, pid_t *ret);
+int pidref_get_ppid(const PidRef *pidref, pid_t *ret);
 int pid_get_start_time(pid_t pid, usec_t *ret);
-int pidref_get_start_time(const PidRefpid, usec_t *ret);
+int pidref_get_start_time(const PidRef *pid, usec_t *ret);
 int get_process_umask(pid_t pid, mode_t *ret);
 
 int container_get_leader(const char *machine, pid_t *pid);
index e6ba6fea707f87f14819378e0537c00a2601f228..5a6bcdffcc0f92c09c1310e2868120a445ecf7ed 100644 (file)
@@ -69,7 +69,7 @@ static void test_pid_get_comm_one(pid_t pid) {
         ASSERT_OK(pid_get_cmdline(pid, 1, 0, &d));
         log_info("PID"PID_FMT" cmdline truncated to 1: '%s'", pid, d);
 
-        r = get_process_ppid(pid, &e);
+        r = pid_get_ppid(pid, &e);
         if (pid == 1)
                 ASSERT_ERROR(r, EADDRNOTAVAIL);
         else
@@ -813,18 +813,18 @@ TEST(setpriority_closest) {
         }
 }
 
-TEST(get_process_ppid) {
+TEST(pid_get_ppid) {
         uint64_t limit;
         int r;
 
-        ASSERT_ERROR(get_process_ppid(1, NULL), EADDRNOTAVAIL);
+        ASSERT_ERROR(pid_get_ppid(1, NULL), EADDRNOTAVAIL);
 
         /* the process with the PID above the global limit definitely doesn't exist. Verify that */
         ASSERT_OK(procfs_get_pid_max(&limit));
         log_debug("kernel.pid_max = %"PRIu64, limit);
 
         if (limit < INT_MAX) {
-                r = get_process_ppid(limit + 1, NULL);
+                r = pid_get_ppid(limit + 1, NULL);
                 log_debug_errno(r, "get_process_limit(%"PRIu64") → %d/%m", limit + 1, r);
                 assert(r == -ESRCH);
         }
@@ -833,7 +833,7 @@ TEST(get_process_ppid) {
                 _cleanup_free_ char *c1 = NULL, *c2 = NULL;
                 pid_t ppid;
 
-                r = get_process_ppid(pid, &ppid);
+                r = pid_get_ppid(pid, &ppid);
                 if (r == -EADDRNOTAVAIL) {
                         log_info("No further parent PID");
                         break;