maurycy
(maurycy)
May 5, 2026, 10:01pm
1
Any particular reason why we don’t support `pidfd_getfd()` syscall?
`os.pidfd_open` and `signal.pidfd_send_signal` exist, so it looks like an simple omission.
There was a thread 4 years ago that got two upvotes but didn’t get anywhere:
People just hijack it with `ctypes` right now. Quick findings from Github:
if self._pid_fd is None:
self._pid_fd = os.pidfd_open(self._picam2_pid)
fd = self._syscall(438, c_int(self._pid_fd), c_int(picam2_fd), c_int(0)) # 438 is pidfd_getfd
if fd == -1:
errno = get_errno()
raise OSError(errno, os.strerror(errno))
return fd
syscall = libc.syscall
SYS_pidfd_open = 434
SYS_pidfd_getfd = 438
for i, pid in enumerate(all_pids):
pidfd = syscall(SYS_pidfd_open, pid, 0)
if pidfd < 0:
err = ctypes.get_errno()
raise RuntimeError(
f"pidfd_open({pid}) failed with errno {err}: {os.strerror(err)}"
)
pidfds.append(pidfd)
for i, (pidfd, fd) in enumerate(zip(pidfds, all_handles_data)):
remote_fd = syscall(SYS_pidfd_getfd, pidfd, fd, 0)
if remote_fd < 0:
err = ctypes.get_errno()
error_msg = f"pidfd_getfd(pidfd={pidfd}, fd={fd}) failed with errno {err}: {os.strerror(err)}."
if err == 1: # EPERM
error_msg += (
" Permission denied. If running in a container, try adding --cap-add=SYS_PTRACE "
This file has been truncated. show original
# Constants for syscall
SYS_pidfd_open = 434 # From syscall.h
SYS_pidfd_getfd = 438 # From syscall.h
GETFD_FLAGS = 0
# C bindings to syscall (Linux only)
if sys.platform.startswith("linux"):
libc = ctypes.CDLL("libc.so.6", use_errno=True)
else:
print("DMA only works on EdgeFirst Platforms")
sys.exit(0)
def pidfd_open(pid: int, flags: int = 0) -> int:
return libc.syscall(SYS_pidfd_open, pid, flags)
def pidfd_getfd(pidfd: int, target_fd: int, flags: int = GETFD_FLAGS) -> int:
return libc.syscall(SYS_pidfd_getfd, pidfd, target_fd, flags)
This file has been truncated. show original
I can do a PR immediately if there’s interest.
vstinner
(Victor Stinner)
May 6, 2026, 2:48pm
2
Adding os.pidfd_getfd(pidfd, targetfd, flags=0) sounds like a reasonable addition. FD_CLOEXEC should be used by default to respect PEP 446 . The tricky part is to write good documentation and write tests.
Well, so far, nobody opened an issue to propose adding this function to Python stdlib.
1 Like