BSD syscall#90
svc · unix #90
dup2
Duplicates a file descriptor onto a specific target fd, atomically closing the target first if necessary.
Prototype
int dup2(u_int from, u_int to);Returns: int
Arguments
| Name | Type | Dir | Description |
|---|---|---|---|
| from | u_int | - | |
| to | u_int | - |
Version history
| XNU tag | macOS | # |
|---|---|---|
| xnu-1456.1.26 | macOS 10.6 Snow Leopard | 90 |
| xnu-1699.24.8 | macOS 10.7 Lion | 90 |
| xnu-2050.18.24 | macOS 10.8 Mountain Lion | 90 |
| xnu-2422.115.4 | macOS 10.9 Mavericks | 90 |
| xnu-2782.40.9 | macOS 10.10 Yosemite | 90 |
| xnu-3247.1.106 | macOS 10.11 El Capitan | 90 |
| xnu-3789.1.32 | macOS 10.12 Sierra | 90 |
| xnu-4570.1.46 | macOS 10.13 High Sierra | 90 |
| xnu-4903.221.2 | macOS 10.14 Mojave | 90 |
| xnu-6153.11.26 | macOS 10.15 Catalina | 90 |
| xnu-1486.2.11 | — | 90 |
| xnu-1504.15.3 | — | 90 |
| xnu-1504.3.12 | — | 90 |
| xnu-1504.7.4 | — | 90 |
| xnu-1504.9.17 | — | 90 |
| xnu-1504.9.26 | — | 90 |
| xnu-1504.9.37 | — | 90 |
| xnu-1699.22.73 | — | 90 |
| xnu-1699.22.81 | — | 90 |
| xnu-1699.24.23 | — | 90 |
| xnu-1699.26.8 | — | 90 |
| xnu-1699.32.7 | — | 90 |
| xnu-2050.22.13 | — | 90 |
| xnu-2050.24.15 | — | 90 |
| xnu-2050.48.11 | — | 90 |
| xnu-2050.7.9 | — | 90 |
| xnu-2050.9.2 | — | 90 |
| xnu-2422.1.72 | — | 90 |
| xnu-2422.100.13 | — | 90 |
| xnu-2422.110.17 | — | 90 |
| xnu-2422.90.20 | — | 90 |
| xnu-2782.1.97 | — | 90 |
| xnu-2782.10.72 | — | 90 |
| xnu-2782.20.48 | — | 90 |
| xnu-2782.30.5 | — | 90 |
| xnu-3247.10.11 | — | 90 |
| xnu-3248.20.55 | — | 90 |
| xnu-3248.30.4 | — | 90 |
| xnu-3248.40.184 | — | 90 |
| xnu-3248.50.21 | — | 90 |
| xnu-3248.60.10 | — | 90 |
| xnu-3789.21.4 | — | 90 |
| xnu-3789.31.2 | — | 90 |
| xnu-3789.41.3 | — | 90 |
| xnu-3789.51.2 | — | 90 |
| xnu-3789.60.24 | — | 90 |
| xnu-3789.70.16 | — | 90 |
| xnu-4570.20.62 | — | 90 |
| xnu-4570.31.3 | — | 90 |
| xnu-4570.41.2 | — | 90 |
| xnu-4570.51.1 | — | 90 |
| xnu-4570.61.1 | — | 90 |
| xnu-4570.71.2 | — | 90 |
| xnu-4903.231.4 | — | 90 |
| xnu-4903.241.1 | — | 90 |
| xnu-4903.270.47 | — | 90 |
| xnu-6153.101.6 | — | 90 |
| xnu-6153.121.1 | — | 90 |
| xnu-6153.141.1 | — | 90 |
| xnu-6153.41.3 | — | 90 |
| xnu-6153.61.1 | — | 90 |
| xnu-6153.81.5 | — | 90 |
Examples
C — redirect stdout to a pipe
dup2(pipefd[1], STDOUT_FILENO);
close(pipefd[1]);Notes
Used heavily by shells and exec wrappers to wire up stdio redirection before execve. If oldfd equals newfd dup2 returns newfd without closing anything. Unlike dup3 on Linux it cannot set FD_CLOEXEC atomically; use fcntl(F_DUPFD_CLOEXEC) when that matters.
Detection
syscall::dup2:entry in DTrace exposes both fds. Endpoint Security has no dedicated event but a subsequent NOTIFY_CLOSE may fire for the implicitly closed target.
Related APIs
dupclosefcntlpipeopen