process-util: port pidref_get_uid() and pidref_is_my_child() to pidfd helpers
This commit is contained in:
@@ -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;
|
||||
|
||||
|
||||
@@ -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)
|
||||
|
||||
+52
-25
@@ -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,25 +1082,8 @@ 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 r;
|
||||
|
||||
if (pid < 0)
|
||||
return -ESRCH;
|
||||
|
||||
if (pid <= 1)
|
||||
return false;
|
||||
|
||||
r = get_process_ppid(pid, &ppid);
|
||||
if (r < 0)
|
||||
return r;
|
||||
|
||||
return ppid == getpid_cached();
|
||||
}
|
||||
|
||||
int pidref_is_my_child(const PidRef *pid) {
|
||||
int r, result;
|
||||
int r;
|
||||
|
||||
if (!pidref_is_set(pid))
|
||||
return -ESRCH;
|
||||
@@ -1072,15 +1091,23 @@ int pidref_is_my_child(const PidRef *pid) {
|
||||
if (pidref_is_remote(pid))
|
||||
return -EREMOTE;
|
||||
|
||||
result = pid_is_my_child(pid->pid);
|
||||
if (result < 0)
|
||||
return result;
|
||||
if (pid->pid == 1 || pidref_is_self(pid))
|
||||
return false;
|
||||
|
||||
r = pidref_verify(pid);
|
||||
pid_t ppid;
|
||||
r = pidref_get_ppid(pid, &ppid);
|
||||
if (r < 0)
|
||||
return r;
|
||||
|
||||
return result;
|
||||
return ppid == getpid_cached();
|
||||
}
|
||||
|
||||
int pid_is_my_child(pid_t pid) {
|
||||
|
||||
if (pid == 0)
|
||||
return false;
|
||||
|
||||
return pidref_is_my_child(&PIDREF_MAKE_FROM_PID(pid));
|
||||
}
|
||||
|
||||
int pid_is_unwaited(pid_t pid) {
|
||||
|
||||
@@ -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 PidRef* pid, 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);
|
||||
|
||||
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user