Skip to content
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

NameTypeDirDescription
fromu_int-
tou_int-

Version history

XNU tagmacOS#
xnu-1456.1.26macOS 10.6 Snow Leopard90
xnu-1699.24.8macOS 10.7 Lion90
xnu-2050.18.24macOS 10.8 Mountain Lion90
xnu-2422.115.4macOS 10.9 Mavericks90
xnu-2782.40.9macOS 10.10 Yosemite90
xnu-3247.1.106macOS 10.11 El Capitan90
xnu-3789.1.32macOS 10.12 Sierra90
xnu-4570.1.46macOS 10.13 High Sierra90
xnu-4903.221.2macOS 10.14 Mojave90
xnu-6153.11.26macOS 10.15 Catalina90
xnu-1486.2.1190
xnu-1504.15.390
xnu-1504.3.1290
xnu-1504.7.490
xnu-1504.9.1790
xnu-1504.9.2690
xnu-1504.9.3790
xnu-1699.22.7390
xnu-1699.22.8190
xnu-1699.24.2390
xnu-1699.26.890
xnu-1699.32.790
xnu-2050.22.1390
xnu-2050.24.1590
xnu-2050.48.1190
xnu-2050.7.990
xnu-2050.9.290
xnu-2422.1.7290
xnu-2422.100.1390
xnu-2422.110.1790
xnu-2422.90.2090
xnu-2782.1.9790
xnu-2782.10.7290
xnu-2782.20.4890
xnu-2782.30.590
xnu-3247.10.1190
xnu-3248.20.5590
xnu-3248.30.490
xnu-3248.40.18490
xnu-3248.50.2190
xnu-3248.60.1090
xnu-3789.21.490
xnu-3789.31.290
xnu-3789.41.390
xnu-3789.51.290
xnu-3789.60.2490
xnu-3789.70.1690
xnu-4570.20.6290
xnu-4570.31.390
xnu-4570.41.290
xnu-4570.51.190
xnu-4570.61.190
xnu-4570.71.290
xnu-4903.231.490
xnu-4903.241.190
xnu-4903.270.4790
xnu-6153.101.690
xnu-6153.121.190
xnu-6153.141.190
xnu-6153.41.390
xnu-6153.61.190
xnu-6153.81.590

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