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.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