pidfd_getfd(2)

๐Ÿ’– pidfd_getfd โ€” bestie edition ๐Ÿ’–

okay so this is pidfd_getfd(2) from the System Calls Manual, and it's literally the "hey can I borrow that file descriptor" system call. no cap. ๐Ÿซถ


โœจ NAME โœจ

pidfd_getfd โ€” obtain a duplicate of another process's file descriptor ๐Ÿ‘ฏ

๐Ÿ“š LIBRARY ๐Ÿ“š

Standard C library (libc, -lc)

๐ŸŽ€ SYNOPSIS ๐ŸŽ€

#include <sys/syscall.h>      /* Definition of SYS_* constants */
#include <unistd.h>

int syscall(SYS_pidfd_getfd, int pidfd, int targetfd,
            unsigned int flags);
quick heads up bestie: glibc provides no wrapper for pidfd_getfd(), which means you have to go through syscall(2). it's giving manual labour but make it โœจsyscallsโœจ. ๐Ÿ˜ญ

๐Ÿฆ‹ DESCRIPTION ๐Ÿฆ‹

so pidfd_getfd() allocates a new file descriptor in the calling process. this shiny new fd is a duplicate of an existing file descriptor, targetfd, inside the process referred to by the PID file descriptor pidfd. it's basically cloning your friend's fd but it stays their friend's fd too. ๐Ÿ’…

the duplicate file descriptor refers to the same open file description (peep open(2)) as the original file descriptor in the process referred to by pidfd. so the two file descriptors share file status flags and file offset โ€” like, genuinely share them, not just vibe-share them. and operations on the underlying file object (for example, assigning an address to a socket object using bind(2)) can equally be performed via the duplicate file descriptor. iconic behaviour honestly. โœจ

the close-on-exec flag (FD_CLOEXEC; see fcntl(2)) is set on the file descriptor returned by pidfd_getfd(). it's default-on, no negotiation. ๐Ÿ‘‘

the flags argument is reserved for future use. currently, it must be specified as 0. that's it. that's the whole flag situation. ๐Ÿ’€

permission to duplicate another process's file descriptor is governed by a ptrace access mode PTRACE_MODE_ATTACH_REALCREDS check (see ptrace(2)). so like, you need the credentials, bestie. no sneaking in. ๐Ÿšช

๐ŸŒธ RETURN VALUE ๐ŸŒธ

on success, pidfd_getfd() returns a file descriptor (a nonnegative integer). slay. ๐ŸŽ‰

on error, -1 is returned and errno is set to indicate the error. tragic but informative. ๐Ÿ˜ญ

๐Ÿ’… ERRORS ๐Ÿ’…

  • EBADF โ€” pidfd is not a valid PID file descriptor. that's not a pidfd, that's a fake friend ๐Ÿ’€
  • EBADF โ€” targetfd is not an open file descriptor in the process referred to by pidfd. nothing to copy, nothing to serve. ๐Ÿ™ˆ
  • EINVAL โ€” flags is not 0. you tried to be creative with the reserved field. respect the reservation. โœ‹
  • EMFILE โ€” the per-process limit on the number of open file descriptors has been reached (see the description of RLIMIT_NOFILE in getrlimit(2)). your fd closet is full. ๐Ÿšช
  • ENFILE โ€” the system-wide limit on the total number of open files has been reached. not just you โ€” the WHOLE system is out of room. ๐ŸŒ
  • EPERM โ€” the calling process did not have PTRACE_MODE_ATTACH_REALCREDS permissions (see ptrace(2)) over the process referred to by pidfd. permission check said no. ๐Ÿ˜ค
  • ESRCH โ€” the process referred to by pidfd does not exist (i.e., it has terminated and been waited on). it's gone gone. ๐Ÿชฆ

โญ STANDARDS โญ

Linux. that's the standard. ๐Ÿง

๐Ÿ•ฐ๏ธ HISTORY ๐Ÿ•ฐ๏ธ

Linux 5.6. a relatively recent icon, honestly. โœจ

๐Ÿ“ NOTES ๐Ÿ“

for a description of PID file descriptors, see pidfd_open(2). go read it, it's the prerequisite lore. ๐Ÿ“–

the effect of pidfd_getfd() is similar to the use of SCM_RIGHTS messages described in unix(7), but it differs in the following respects:

  • in order to pass a file descriptor using an SCM_RIGHTS message, the two processes must first establish a UNIX domain socket connection. so much setup. ๐Ÿ˜ฎโ€๐Ÿ’จ
  • the use of SCM_RIGHTS requires cooperation on the part of the process whose file descriptor is being copied. by contrast, no such cooperation is necessary when using pidfd_getfd(). sneaky? no, permitted. ๐Ÿ˜Œ
  • the ability to use pidfd_getfd() is restricted by a PTRACE_MODE_ATTACH_REALCREDS ptrace access mode check. rules are rules. ๐Ÿ”’

๐Ÿ‘€ SEE ALSO ๐Ÿ‘€

clone3(2), dup(2), kcmp(2), pidfd_open(2)


aaaand that's the whole page, bestie. this came from Linux man-pages 6.20, dated 2026-02-08, and it's the pidfd_getfd(2) page from the System Calls Manual. now go duplicate some file descriptors responsibly, mwah ๐Ÿ’–โœจ๐ŸŽ€

๐Ÿ“ฆ where this page comes from
  • arch rolling: man-pages 6.20-1 ยท BSD-2-Clause AND BSD-3-Clause AND BSD-4.3TAHOE AND BSD-4-Clause-UC AND GPL-1.0-or-later AND GPL-2.0-only AND GPL-2.0-or-later AND GPL-3.0-or-later AND LGPL-3.0-linking-exception AND LGPL-3.0-or-later AND LicenseRef-Public-Domain AND LicenseRef-UltraPermissive AND Linux-man-pages-1-para AND Linux-man-pages-copyleft AND Linux-man-pages-copyleft-2-para AND Linux-man-pages-copyleft-var AND MIT AND Spencer-94
  • nixpkgs unstable: man-pages 6.20
  • ๐Ÿ“œ the page's license: Linux-man-pages-copyleft
  • ๐Ÿ’– girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms