pidfd_open(2)

๐Ÿ’– pidfd_open(2) ๐Ÿ’–

okay so bestie, pull up a chair, because we're diving into the System Calls Manual and today's main character is pidfd_open(2). she's a Linux syscall, she's got no glibc wrapper (rude, I know), and she's about to hand you a file descriptor that points at a whole entire task. let's get into it. ๐Ÿฆ‹


โœจ NAME โœจ

pidfd_open โ€” obtain a file descriptor that refers to a task

๐Ÿ“š LIBRARY ๐Ÿ“š

Standard C library (libc, -lc)

๐ŸŽ€ SYNOPSIS ๐ŸŽ€

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

int syscall(SYS_pidfd_open, pid_t pid, unsigned int flags);

Note: glibc provides no wrapper for pidfd_open(), necessitating the use of syscall(2). yes bestie, you literally have to go through syscall(2) yourself. it's giving DIY. ๐Ÿ’…

๐ŸŒธ DESCRIPTION ๐ŸŒธ

The pidfd_open() system call creates a file descriptor that refers to the task referenced by pid. The file descriptor is returned as the function result; the close-on-exec flag is set on the file descriptor. (so she's tidy like that โœจ)

The flags argument either has the value 0, or contains the following flags:

  • PIDFD_NONBLOCK (since Linux 5.10) โ€” Return a nonblocking file descriptor. If the task referred to by the file descriptor has not yet terminated, then an attempt to wait on the file descriptor using waitid(2) will immediately return the error EAGAIN rather than blocking. no waiting around, no standing in line, we simply are not doing that today.
  • PIDFD_THREAD (since Linux 6.9) โ€” Create a file descriptor that refers to a specific thread, rather than a process (thread-group leader). If this flag is not set, pid must refer to a process.

๐Ÿ”ฅ RETURN VALUE ๐Ÿ”ฅ

On success, pidfd_open() returns a file descriptor (a nonnegative integer). On error, -1 is returned and errno is set to indicate the error. classic.

๐Ÿ’€ ERRORS ๐Ÿ’€

  • EINVAL โ€” flags is not valid.
  • EINVAL โ€” pid is not valid.
  • EMFILE โ€” The per-process limit on the number of open file descriptors has been reached (see the description of RLIMIT_NOFILE in getrlimit(2)).
  • ENFILE โ€” The system-wide limit on the total number of open files has been reached.
  • ENODEV โ€” The anonymous inode filesystem is not available in this kernel.
  • ENOMEM โ€” Insufficient kernel memory was available.
  • ESRCH โ€” The process specified by pid does not exist.

๐Ÿ‘‘ STANDARDS ๐Ÿ‘‘

Linux. that's it, that's the whole standard. iconic behavior honestly.

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

Linux 5.3. she's a 2019 baby, so she's old enough to be trusted and young enough to be cool. ๐ŸŒˆ

๐Ÿงš NOTES ๐Ÿงš

The following code sequence can be used to obtain a file descriptor for the child of fork(2):

pid = fork();
if (pid > 0) {     /* If parent */
    pidfd = pidfd_open(pid, 0);
    ...
}

Even if the child has already terminated by the time of the pidfd_open() call, its PID will not have been recycled and the returned file descriptor will refer to the resulting zombie process. Note, however, that this is guaranteed only if the following conditions hold true:

  • the disposition of SIGCHLD has not been explicitly set to SIG_IGN (see sigaction(2));
  • the SA_NOCLDWAIT flag was not specified while establishing a handler for SIGCHLD or while setting the disposition of that signal to SIG_DFL (see sigaction(2)); and
  • the zombie process was not reaped elsewhere in the program (e.g., either by an asynchronously executed signal handler or by wait(2) or similar in another thread).

If any of these conditions does not hold, then the child process (along with a PID file descriptor that refers to it) should instead be created using clone(2) with the CLONE_PIDFD flag. no cap, that's the escape hatch. ๐Ÿ™ˆ

๐ŸŒŸ Use cases for PID file descriptors ๐ŸŒŸ

A PID file descriptor returned by pidfd_open() (or by clone(2) with the CLONE_PID flag) can be used for the following purposes:

When the task that it refers to terminates and becomes a zombie, these interfaces indicate the file descriptor as readable (EPOLLIN). When the task is reaped, these interfaces produce a hangup event (EPOLLHUP).

Note, however, that in the current implementation, nothing can be read from the file descriptor (read(2) on the file descriptor fails with the error EINVAL). The polling behavior depends on whether PIDFD_THREAD flag was used when obtaining the file descriptor:

  • With PIDFD_THREAD, the file descriptor becomes readable when the task exits and becomes a zombie, even if the thread-group is not empty.
  • Without PIDFD_THREAD, the file descriptor becomes readable only when the last thread in the thread group exits.
  • If the PID file descriptor refers to a child of the calling process, then it can be waited on using waitid(2).
  • The pidfd_getfd(2) system call can be used to obtain a duplicate of a file descriptor of another process referred to by a PID file descriptor.
  • A PID file descriptor can be used as the argument of setns(2) in order to move into one or more of the same namespaces as the process referred to by the file descriptor.
  • A PID file descriptor can be used as the argument of process_madvise(2) in order to provide advice on the memory usage patterns of the process referred to by the file descriptor.

The pidfd_open() system call is the preferred way of obtaining a PID file descriptor for an already existing process. The alternative is to obtain a file descriptor by opening a /proc/pid directory. However, the latter technique is possible only if the proc(5) filesystem is mounted; furthermore, the file descriptor obtained in this way is not pollable and can't be waited on with waitid(2). so like. bestie. just use the syscall. ๐Ÿ’…

โญ EXAMPLES โญ

The program below opens a PID file descriptor for the process whose PID is specified as its command-line argument. It then uses poll(2) to monitor the file descriptor for process exit, as indicated by an EPOLLIN event. slay.

๐Ÿ“ Program source ๐Ÿ“

#define _GNU_SOURCE
#include <poll.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/syscall.h>
#include <sys/types.h>
#include <unistd.h>

static int
pidfd_open(pid_t pid, unsigned int flags)
{
    return syscall(SYS_pidfd_open, pid, flags);
}

int
main(int argc, char *argv[])
{
    int            pidfd, ready;
    struct pollfd  pollfd;

    if (argc != 2) {
        fprintf(stderr, "Usage: %s <pid>\n", argv[0]);
        exit(EXIT_SUCCESS);
    }

    pidfd = pidfd_open(atoi(argv[1]), 0);
    if (pidfd == -1) {
        perror("pidfd_open");
        exit(EXIT_FAILURE);
    }

    pollfd.fd = pidfd;
    pollfd.events = POLLIN;

    ready = poll(&pollfd, 1, -1);
    if (ready == -1) {
        perror("poll");
        exit(EXIT_FAILURE);
    }

    printf("Events (%#x): POLLIN is %sset\n", pollfd.revents,
           (pollfd.revents & POLLIN) ? "" : "not ");

    close(pidfd);
    exit(EXIT_SUCCESS);
}

๐Ÿซถ SEE ALSO ๐Ÿซถ

clone(2), kill(2), pidfd_getfd(2), pidfd_send_signal(2), poll(2), process_madvise(2), select(2), setns(2), waitid(2), epoll(7)


aaaaand that's the whole page bestie!! this one came from Linux man-pages 6.20, dated 2026-02-08 โ€” go off, man-pages maintainers, we love you, 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