]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
process-util: modernize pidfd_get_pid()
authorMike Yuan <me@yhndnzj.com>
Sun, 14 Jul 2024 16:25:20 +0000 (18:25 +0200)
committerMike Yuan <me@yhndnzj.com>
Sun, 21 Jul 2024 20:48:53 +0000 (22:48 +0200)
src/basic/fd-util.c
src/basic/process-util.c

index d5e733293c24cc47e127fc4186b6116afe0775b5..5d7fc258b2f906b5613bb5b2567d4ad507d4c305 100644 (file)
@@ -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. */
 }
index 15839cb447343cee058f13ce5a429ffd59a820f9..ede5f8513508a2b58fc061032752223665a7122f 100644 (file)
@@ -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"))