From 8dc303d3c86190de9b6bf5aae60b64f1e11ad354 Mon Sep 17 00:00:00 2001 From: Mike Yuan Date: Sun, 14 Jul 2024 18:25:20 +0200 Subject: [PATCH] process-util: modernize pidfd_get_pid() --- src/basic/fd-util.c | 6 ++++-- src/basic/process-util.c | 12 +++++------- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/src/basic/fd-util.c b/src/basic/fd-util.c index d5e733293c2..5d7fc258b2f 100644 --- a/src/basic/fd-util.c +++ b/src/basic/fd-util.c @@ -1123,6 +1123,8 @@ int proc_fd_enoent_errno(void) { if (r == 0) return -ENOSYS; /* /proc/ is not available or not set up properly, we're most likely in some chroot environment. */ - return r > 0 ? -EBADF : -ENOENT; /* If /proc/ is definitely around then this means the fd is - not valid, otherwise let's propagate the original ENOENT. */ + if (r > 0) + return -EBADF; /* If /proc/ is definitely around then this means the fd is not valid. */ + + return -ENOENT; /* Otherwise let's propagate the original ENOENT. */ } diff --git a/src/basic/process-util.c b/src/basic/process-util.c index 15839cb4473..ede5f851350 100644 --- a/src/basic/process-util.c +++ b/src/basic/process-util.c @@ -1840,7 +1840,6 @@ int get_oom_score_adjust(int *ret) { int pidfd_get_pid(int fd, pid_t *ret) { char path[STRLEN("/proc/self/fdinfo/") + DECIMAL_STR_MAX(int)]; _cleanup_free_ char *fdinfo = NULL; - char *p; int r; /* Converts a pidfd into a pid. Well known errors: @@ -1852,22 +1851,21 @@ int pidfd_get_pid(int fd, pid_t *ret) { * -ESRCH → fd valid, but process is already reaped */ - if (fd < 0) - return -EBADF; + assert(fd >= 0); xsprintf(path, "/proc/self/fdinfo/%i", fd); r = read_full_virtual_file(path, &fdinfo, NULL); - if (r == -ENOENT) /* if fdinfo doesn't exist we assume the process does not exist */ - return proc_mounted() > 0 ? -EBADF : -ENOSYS; + if (r == -ENOENT) + return proc_fd_enoent_errno(); if (r < 0) return r; - p = find_line_startswith(fdinfo, "Pid:"); + char *p = find_line_startswith(fdinfo, "Pid:"); if (!p) return -ENOTTY; /* not a pidfd? */ - p += strspn(p, WHITESPACE); + p = skip_leading_chars(p, /* bad = */ NULL); p[strcspn(p, WHITESPACE)] = 0; if (streq(p, "0")) -- 2.47.3