---
name: "pidfd_getfd"
section: "2"
platform: "linux"
package: "man-pages"
version: "6.19-1"
release: "Arch Linux 2026-11-01"
platforms: ["linux"]
url: "https://new.carta.tech/pidfd_getfd"
---

# pidfd\_getfd(2)

obtain a duplicate of another process's file descriptor

## 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***);**

*Note*: glibc provides no wrapper for **pidfd\_getfd**(), necessitating the use of [**syscall**(2)](/syscall/2.md).

## DESCRIPTION

The **pidfd\_getfd**() system call allocates a new file descriptor in the calling process. This new file descriptor is a duplicate of an existing file descriptor, *targetfd*, in the process referred to by the PID file descriptor *pidfd*.

The duplicate file descriptor refers to the same open file description (see [**open**(2)](/open/2.md)) as the original file descriptor in the process referred to by *pidfd*. The two file descriptors thus share file status flags and file offset. Furthermore, operations on the underlying file object (for example, assigning an address to a socket object using [**bind**(2)](/bind/2.md)) can equally be performed via the duplicate file descriptor.

The close-on-exec flag (**FD\_CLOEXEC**; see [**fcntl**(2)](/fcntl/2.md)) is set on the file descriptor returned by **pidfd\_getfd**().

The *flags* argument is reserved for future use. Currently, it must be specified as 0.

Permission to duplicate another process's file descriptor is governed by a ptrace access mode **PTRACE\_MODE\_ATTACH\_REALCREDS** check (see [**ptrace**(2)](/ptrace/2.md)).

## RETURN VALUE

On success, **pidfd\_getfd**() returns a file descriptor (a nonnegative integer). On error, -1 is returned and *errno* is set to indicate the error.

## ERRORS

- **EBADF**

  *pidfd* is not a valid PID file descriptor.

- **EBADF**

  *targetfd* is not an open file descriptor in the process referred to by *pidfd*.

- **EINVAL**

  *flags* is not 0.

- **EMFILE**

  The per-process limit on the number of open file descriptors has been reached (see the description of **RLIMIT\_NOFILE** in [**getrlimit**(2)](/getrlimit/2.md)).

- **ENFILE**

  The system-wide limit on the total number of open files has been reached.

- **EPERM**

  The calling process did not have **PTRACE\_MODE\_ATTACH\_REALCREDS** permissions (see [**ptrace**(2)](/ptrace/2.md)) over the process referred to by *pidfd*.

- **ESRCH**

  The process referred to by *pidfd* does not exist (i.e., it has terminated and been waited on).

## STANDARDS

Linux.

## HISTORY

Linux 5.6.

## NOTES

For a description of PID file descriptors, see [**pidfd\_open**(2)](/pidfd_open/2.md).

The effect of **pidfd\_getfd**() is similar to the use of **SCM\_RIGHTS** messages described in [**unix**(7)](/unix/7.md), but 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.

- •

  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**().

- •

  The ability to use **pidfd\_getfd**() is restricted by a **PTRACE\_MODE\_ATTACH\_REALCREDS** ptrace access mode check.

## SEE ALSO

[**clone3**(2)](/clone3/2.md), [**dup**(2)](/dup/2.md), [**kcmp**(2)](/kcmp/2.md), [**pidfd\_open**(2)](/pidfd_open/2.md)
