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 forpidfd_getfd(), which means you have to go throughsyscall(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โpidfdis not a valid PID file descriptor. that's not a pidfd, that's a fake friend ๐EBADFโtargetfdis not an open file descriptor in the process referred to bypidfd. nothing to copy, nothing to serve. ๐EINVALโflagsis not0. 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 ofRLIMIT_NOFILEingetrlimit(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 havePTRACE_MODE_ATTACH_REALCREDSpermissions (seeptrace(2)) over the process referred to bypidfd. permission check said no. ๐คESRCHโ the process referred to bypidfddoes 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_RIGHTSmessage, the two processes must first establish a UNIX domain socket connection. so much setup. ๐ฎโ๐จ - the use of
SCM_RIGHTSrequires cooperation on the part of the process whose file descriptor is being copied. by contrast, no such cooperation is necessary when usingpidfd_getfd(). sneaky? no, permitted. ๐ - the ability to use
pidfd_getfd()is restricted by aPTRACE_MODE_ATTACH_REALCREDSptrace 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
- Copyright 2020, Michael Kerrisk <mtk.manpages@gmail.com>
- ๐ girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms