aboutsummaryrefslogtreecommitdiff
path: root/src/syscall/exec_bsd.go
diff options
context:
space:
mode:
authorTobias Klauser <tklauser@distanz.ch>2022-03-04 09:49:32 +0100
committerTobias Klauser <tobias.klauser@gmail.com>2022-03-05 08:36:13 +0000
commit55a60cadc3f5d01f76ac9435da2ed941e194a29b (patch)
tree9867ff3e90013113d6ed05b0c58cf1e92be365e4 /src/syscall/exec_bsd.go
parentbf97c99b62fe7d6652cc8c807dbc91998d488a01 (diff)
downloadgo-55a60cadc3f5d01f76ac9435da2ed941e194a29b.tar.xz
syscall: use dup3 in forkAndExecInChild on OpenBSD
Use dup3(oldfd, newfd, O_CLOEXEC) to atomically duplicate the file descriptor and mark is as close-on-exec instead of dup2 & fcntl. The dup3 system call first appeared in OpenBSD 5.7. Change-Id: Ic06c2c7089dcdbd931ee24e5e8c316879d81474e Reviewed-on: https://go-review.googlesource.com/c/go/+/389974 Trust: Tobias Klauser <tobias.klauser@gmail.com> Run-TryBot: Tobias Klauser <tobias.klauser@gmail.com> Reviewed-by: Ian Lance Taylor <iant@golang.org>
Diffstat (limited to 'src/syscall/exec_bsd.go')
-rw-r--r--src/syscall/exec_bsd.go26
1 files changed, 12 insertions, 14 deletions
diff --git a/src/syscall/exec_bsd.go b/src/syscall/exec_bsd.go
index 148f5a91aa..530b48cb70 100644
--- a/src/syscall/exec_bsd.go
+++ b/src/syscall/exec_bsd.go
@@ -181,18 +181,17 @@ func forkAndExecInChild(argv0 *byte, argv, envv []*byte, chroot, dir *byte, attr
// Pass 1: look for fd[i] < i and move those up above len(fd)
// so that pass 2 won't stomp on an fd it needs later.
if pipe < nextfd {
- switch runtime.GOOS {
- case "netbsd":
+ if runtime.GOOS == "netbsd" || (runtime.GOOS == "openbsd" && runtime.GOARCH == "mips64") {
_, _, err1 = RawSyscall(_SYS_DUP3, uintptr(pipe), uintptr(nextfd), O_CLOEXEC)
- if err1 != 0 {
- goto childerror
- }
- default:
+ } else {
_, _, err1 = RawSyscall(SYS_DUP2, uintptr(pipe), uintptr(nextfd), 0)
if err1 != 0 {
goto childerror
}
- RawSyscall(SYS_FCNTL, uintptr(nextfd), F_SETFD, FD_CLOEXEC)
+ _, _, err1 = RawSyscall(SYS_FCNTL, uintptr(nextfd), F_SETFD, FD_CLOEXEC)
+ }
+ if err1 != 0 {
+ goto childerror
}
pipe = nextfd
nextfd++
@@ -202,18 +201,17 @@ func forkAndExecInChild(argv0 *byte, argv, envv []*byte, chroot, dir *byte, attr
if nextfd == pipe { // don't stomp on pipe
nextfd++
}
- switch runtime.GOOS {
- case "netbsd":
+ if runtime.GOOS == "netbsd" || (runtime.GOOS == "openbsd" && runtime.GOARCH == "mips64") {
_, _, err1 = RawSyscall(_SYS_DUP3, uintptr(fd[i]), uintptr(nextfd), O_CLOEXEC)
- if err1 != 0 {
- goto childerror
- }
- default:
+ } else {
_, _, err1 = RawSyscall(SYS_DUP2, uintptr(fd[i]), uintptr(nextfd), 0)
if err1 != 0 {
goto childerror
}
- RawSyscall(SYS_FCNTL, uintptr(nextfd), F_SETFD, FD_CLOEXEC)
+ _, _, err1 = RawSyscall(SYS_FCNTL, uintptr(nextfd), F_SETFD, FD_CLOEXEC)
+ }
+ if err1 != 0 {
+ goto childerror
}
fd[i] = nextfd
nextfd++