Skip to content

Commit 24e798e

Browse files
committed
syscall: use fcntl with F_DUP2FD_CLOEXEC in forkAndExecInChild on FreeBSD
Use fcntl(oldfd, F_DUP2FD_CLOEXEC, newfd) to duplicate the file descriptor and mark is as close-on-exec instead of dup2 & fcntl. FreeBSD implements dup3 like this in libc. Change-Id: I36e37bc61c2e31561adb49001f287764125a74de Reviewed-on: https://go-review.googlesource.com/c/go/+/355571 Trust: Tobias Klauser <[email protected]> Reviewed-by: Ian Lance Taylor <[email protected]>
1 parent 2feb2cc commit 24e798e

File tree

1 file changed

+2
-4
lines changed

1 file changed

+2
-4
lines changed

src/syscall/exec_freebsd.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -214,11 +214,10 @@ func forkAndExecInChild(argv0 *byte, argv, envv []*byte, chroot, dir *byte, attr
214214
// Pass 1: look for fd[i] < i and move those up above len(fd)
215215
// so that pass 2 won't stomp on an fd it needs later.
216216
if pipe < nextfd {
217-
_, _, err1 = RawSyscall(SYS_DUP2, uintptr(pipe), uintptr(nextfd), 0)
217+
_, _, err1 = RawSyscall(SYS_FCNTL, uintptr(pipe), F_DUP2FD_CLOEXEC, uintptr(nextfd))
218218
if err1 != 0 {
219219
goto childerror
220220
}
221-
RawSyscall(SYS_FCNTL, uintptr(nextfd), F_SETFD, FD_CLOEXEC)
222221
pipe = nextfd
223222
nextfd++
224223
}
@@ -227,11 +226,10 @@ func forkAndExecInChild(argv0 *byte, argv, envv []*byte, chroot, dir *byte, attr
227226
if nextfd == pipe { // don't stomp on pipe
228227
nextfd++
229228
}
230-
_, _, err1 = RawSyscall(SYS_DUP2, uintptr(fd[i]), uintptr(nextfd), 0)
229+
_, _, err1 = RawSyscall(SYS_FCNTL, uintptr(fd[i]), F_DUP2FD_CLOEXEC, uintptr(nextfd))
231230
if err1 != 0 {
232231
goto childerror
233232
}
234-
RawSyscall(SYS_FCNTL, uintptr(nextfd), F_SETFD, FD_CLOEXEC)
235233
fd[i] = nextfd
236234
nextfd++
237235
}

0 commit comments

Comments
 (0)