]> Cypherpunks repositories - gostls13.git/commitdiff
syscall: use fcntl F_DUP2FD_CLOEXEC in forkAndExecInChild on dragonfly
authorTobias Klauser <tklauser@distanz.ch>
Tue, 13 Sep 2022 10:15:02 +0000 (12:15 +0200)
committerGopher Robot <gobot@golang.org>
Thu, 15 Sep 2022 21:09:41 +0000 (21:09 +0000)
Use fcntl(oldfd, F_DUP2FD_CLOEXEC, newfd) to duplicate the file
descriptor and mark is as close-on-exec instead of dup2 & fcntl.

DragonFly BSD implements dup3 like this in libc since version 5.4.

Change-Id: I80c765faa288add8ffb236284c9e8c4f8e6c6769
Reviewed-on: https://go-review.googlesource.com/c/go/+/430535
Auto-Submit: Tobias Klauser <tobias.klauser@gmail.com>
Reviewed-by: Jenny Rakoczy <jenny@golang.org>
TryBot-Result: Gopher Robot <gobot@golang.org>
Run-TryBot: Jenny Rakoczy <jenny@golang.org>
Run-TryBot: Tobias Klauser <tobias.klauser@gmail.com>
Auto-Submit: Jenny Rakoczy <jenny@golang.org>
Reviewed-by: Ian Lance Taylor <iant@google.com>
src/syscall/exec_bsd.go
src/syscall/syscall_dragonfly.go
src/syscall/syscall_netbsd.go
src/syscall/syscall_openbsd_mips64.go

index 4762ae751a3430c444a76fd707b8da902ad31e43..3e4c6f9d62f8e2af30a6e87992b91fad31130742 100644 (file)
@@ -184,6 +184,8 @@ func forkAndExecInChild(argv0 *byte, argv, envv []*byte, chroot, dir *byte, attr
        if pipe < nextfd {
                if runtime.GOOS == "netbsd" || (runtime.GOOS == "openbsd" && runtime.GOARCH == "mips64") {
                        _, _, err1 = RawSyscall(_SYS_DUP3, uintptr(pipe), uintptr(nextfd), O_CLOEXEC)
+               } else if runtime.GOOS == "dragonfly" {
+                       _, _, err1 = RawSyscall(SYS_FCNTL, uintptr(pipe), _F_DUP2FD_CLOEXEC, uintptr(nextfd))
                } else {
                        _, _, err1 = RawSyscall(SYS_DUP2, uintptr(pipe), uintptr(nextfd), 0)
                        if err1 != 0 {
@@ -204,6 +206,8 @@ func forkAndExecInChild(argv0 *byte, argv, envv []*byte, chroot, dir *byte, attr
                        }
                        if runtime.GOOS == "netbsd" || (runtime.GOOS == "openbsd" && runtime.GOARCH == "mips64") {
                                _, _, err1 = RawSyscall(_SYS_DUP3, uintptr(fd[i]), uintptr(nextfd), O_CLOEXEC)
+                       } else if runtime.GOOS == "dragonfly" {
+                               _, _, err1 = RawSyscall(SYS_FCNTL, uintptr(fd[i]), _F_DUP2FD_CLOEXEC, uintptr(nextfd))
                        } else {
                                _, _, err1 = RawSyscall(SYS_DUP2, uintptr(fd[i]), uintptr(nextfd), 0)
                                if err1 != 0 {
index 1a3cfe51fa4b4f548a79e140f7d4ea02a92b8c10..1a1f1f69425647377009da563481e3d28a237a42 100644 (file)
@@ -22,7 +22,10 @@ func Syscall6(trap, a1, a2, a3, a4, a5, a6 uintptr) (r1, r2 uintptr, err Errno)
 func RawSyscall(trap, a1, a2, a3 uintptr) (r1, r2 uintptr, err Errno)
 func RawSyscall6(trap, a1, a2, a3, a4, a5, a6 uintptr) (r1, r2 uintptr, err Errno)
 
-const _SYS_DUP3 = 0
+const (
+       _SYS_DUP3         = 0
+       _F_DUP2FD_CLOEXEC = F_DUP2FD_CLOEXEC
+)
 
 // See version list in https://github.com/DragonFlyBSD/DragonFlyBSD/blob/master/sys/sys/param.h
 var (
index d8efb41d20cd7b07106ee6718381eab3fb2dca37..7f7c5b1c6bf4105bacebe3e5bbe454beafd8032a 100644 (file)
@@ -20,7 +20,10 @@ func Syscall9(num, a1, a2, a3, a4, a5, a6, a7, a8, a9 uintptr) (r1, r2 uintptr,
 func RawSyscall(trap, a1, a2, a3 uintptr) (r1, r2 uintptr, err Errno)
 func RawSyscall6(trap, a1, a2, a3, a4, a5, a6 uintptr) (r1, r2 uintptr, err Errno)
 
-const _SYS_DUP3 = SYS_DUP3
+const (
+       _SYS_DUP3         = SYS_DUP3
+       _F_DUP2FD_CLOEXEC = 0
+)
 
 type SockaddrDatalink struct {
        Len    uint8
index 4508ad99b492a576397cfddf0582227d24d02f49..838c684750a10739fb2674698e3d2a0d6f1b5486 100644 (file)
@@ -4,7 +4,10 @@
 
 package syscall
 
-const _SYS_DUP3 = SYS_DUP3
+const (
+       _SYS_DUP3         = SYS_DUP3
+       _F_DUP2FD_CLOEXEC = 0
+)
 
 func setTimespec(sec, nsec int64) Timespec {
        return Timespec{Sec: sec, Nsec: nsec}