]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
pidfd-util: use get_proc_field() for pidfd_get_pid_fdinfo()
authorMike Yuan <me@yhndnzj.com>
Thu, 13 Mar 2025 13:54:04 +0000 (14:54 +0100)
committerYu Watanabe <watanabe.yu+github@gmail.com>
Thu, 1 May 2025 04:10:26 +0000 (13:10 +0900)
src/basic/pidfd-util.c

index edd2848d07c9a8ed84980e48a8e1e774abff0030..52a371ba1aca7ed96f03f7df737fa031d110deab 100644 (file)
@@ -89,26 +89,21 @@ static int pidfd_get_info(int fd, struct pidfd_info *info) {
 
 static int pidfd_get_pid_fdinfo(int fd, pid_t *ret) {
         char path[STRLEN("/proc/self/fdinfo/") + DECIMAL_STR_MAX(int)];
-        _cleanup_free_ char *fdinfo = NULL;
+        _cleanup_free_ char *p = NULL;
         int r;
 
         assert(fd >= 0);
 
         xsprintf(path, "/proc/self/fdinfo/%i", fd);
 
-        r = read_full_virtual_file(path, &fdinfo, NULL);
+        r = get_proc_field(path, "Pid", &p);
         if (r == -ENOENT)
-                return proc_fd_enoent_errno();
+                return -EBADF;
+        if (r == -ENODATA) /* not a pidfd? */
+                return -ENOTTY;
         if (r < 0)
                 return r;
 
-        char *p = find_line_startswith(fdinfo, "Pid:");
-        if (!p)
-                return -ENOTTY; /* not a pidfd? */
-
-        p = skip_leading_chars(p, /* bad = */ NULL);
-        p[strcspn(p, WHITESPACE)] = 0;
-
         if (streq(p, "0"))
                 return -EREMOTE; /* PID is in foreign PID namespace? */
         if (streq(p, "-1"))