]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
process-util: add pidref_get_uid() and rename get_process_uid() → pidref_get_uid()
authorLennart Poettering <lennart@poettering.net>
Tue, 17 Oct 2023 09:27:06 +0000 (11:27 +0200)
committerLennart Poettering <lennart@poettering.net>
Wed, 18 Oct 2023 12:39:33 +0000 (14:39 +0200)
src/basic/process-util.c
src/basic/process-util.h
src/core/dbus-unit.c
src/journal/journald-context.c
src/shared/killall.c
src/test/test-process-util.c

index 2374387850831723c60f583d72f13b674fa997c8..f381b6369cd0472176d79ac18fd9c49201a35b23 100644 (file)
@@ -572,7 +572,8 @@ static int get_process_id(pid_t pid, const char *field, uid_t *ret) {
         return -EIO;
 }
 
-int get_process_uid(pid_t pid, uid_t *ret) {
+int pid_get_uid(pid_t pid, uid_t *ret) {
+        assert(ret);
 
         if (pid == 0 || pid == getpid_cached()) {
                 *ret = getuid();
@@ -582,6 +583,26 @@ int get_process_uid(pid_t pid, uid_t *ret) {
         return get_process_id(pid, "Uid:", ret);
 }
 
+int pidref_get_uid(const PidRef *pid, uid_t *ret) {
+        uid_t uid;
+        int r;
+
+        if (!pidref_is_set(pid))
+                return -ESRCH;
+
+        r = pid_get_uid(pid->pid, &uid);
+        if (r < 0)
+                return r;
+
+        r = pidref_verify(pid);
+        if (r < 0)
+                return r;
+
+        if (ret)
+                *ret = uid;
+        return 0;
+}
+
 int get_process_gid(pid_t pid, gid_t *ret) {
 
         if (pid == 0 || pid == getpid_cached()) {
index f49e487bd1be4df99496095b19b5d72eac720f18..53dce1ee71f22a61e12f1ae6285c2cedfd1de49e 100644 (file)
@@ -46,7 +46,8 @@ int pidref_get_cmdline(const PidRef *pid, size_t max_columns, ProcessCmdlineFlag
 int pid_get_cmdline_strv(pid_t pid, ProcessCmdlineFlags flags, char ***ret);
 int pidref_get_cmdline_strv(const PidRef *pid, ProcessCmdlineFlags flags, char ***ret);
 int get_process_exe(pid_t pid, char **ret);
-int get_process_uid(pid_t pid, uid_t *ret);
+int pid_get_uid(pid_t pid, uid_t *ret);
+int pidref_get_uid(const PidRef *pid, uid_t *ret);
 int get_process_gid(pid_t pid, gid_t *ret);
 int get_process_capeff(pid_t pid, char **ret);
 int get_process_cwd(pid_t pid, char **ret);
index 91660cf3aafeb425a49ac4cf53ec8abfdc245dcc..8c9ad0ef9f0e8fefbf477cc70ec449b7c1c13b32 100644 (file)
@@ -1530,7 +1530,7 @@ int bus_unit_method_attach_processes(sd_bus_message *message, void *userdata, sd
                 /* Let's validate security: if the sender is root, then all is OK. If the sender is any other unit,
                  * then the process' UID and the target unit's UID have to match the sender's UID */
                 if (sender_uid != 0 && sender_uid != getuid()) {
-                        r = get_process_uid(pidref->pid, &process_uid);
+                        r = pidref_get_uid(pidref, &process_uid);
                         if (r < 0)
                                 return sd_bus_error_set_errnof(error, r, "Failed to retrieve process UID: %m");
 
index 3cf337a0897902f1f16727a88e37fceabf2e9d1a..1f653801ad74c032fbb014d079733fc3d1f771a9 100644 (file)
@@ -210,7 +210,7 @@ static void client_context_read_uid_gid(ClientContext *c, const struct ucred *uc
         if (ucred && uid_is_valid(ucred->uid))
                 c->uid = ucred->uid;
         else
-                (void) get_process_uid(c->pid, &c->uid);
+                (void) pid_get_uid(c->pid, &c->uid);
 
         if (ucred && gid_is_valid(ucred->gid))
                 c->gid = ucred->gid;
index ac1b69e6bb94fe4f067ea48553b97b0943c8f1bc..df0f450a880f3801759d17d31e549f6ca93cdf82 100644 (file)
@@ -85,7 +85,7 @@ static bool ignore_proc(pid_t pid, bool warn_rootfs) {
         if (is_survivor_cgroup(pid))
                 return true;
 
-        r = get_process_uid(pid, &uid);
+        r = pid_get_uid(pid, &uid);
         if (r < 0)
                 return true; /* not really, but better safe than sorry */
 
index ab508867ff67f87ba8c35f02f848d894f5e00633..6f548ecae9c1605c1c033de539821595005699b8 100644 (file)
@@ -82,7 +82,7 @@ static void test_pid_get_comm_one(pid_t pid) {
         assert_se(r >= 0 || r == -EACCES);
         log_info("PID"PID_FMT" exe: '%s'", pid, strna(f));
 
-        assert_se(get_process_uid(pid, &u) == 0);
+        assert_se(pid_get_uid(pid, &u) == 0);
         log_info("PID"PID_FMT" UID: "UID_FMT, pid, u);
 
         assert_se(get_process_gid(pid, &g) == 0);