]> Cypherpunks repositories - gostls13.git/commitdiff
syscall: use fcntl with F_DUP2FD_CLOEXEC in forkAndExecInChild on FreeBSD
authorTobias Klauser <tklauser@distanz.ch>
Wed, 13 Oct 2021 14:30:16 +0000 (16:30 +0200)
committerTobias Klauser <tobias.klauser@gmail.com>
Thu, 14 Oct 2021 13:09:28 +0000 (13: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.

FreeBSD implements dup3 like this in libc.

Change-Id: I36e37bc61c2e31561adb49001f287764125a74de
Reviewed-on: https://go-review.googlesource.com/c/go/+/355571
Trust: Tobias Klauser <tobias.klauser@gmail.com>
Reviewed-by: Ian Lance Taylor <iant@golang.org>
src/syscall/exec_freebsd.go

index a7410db4b6ba9d1287ea69b2f99cb5890b281c92..90793fe83fa7606692a8315bb639128f3120c46c 100644 (file)
@@ -214,11 +214,10 @@ 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 {
-               _, _, err1 = RawSyscall(SYS_DUP2, uintptr(pipe), uintptr(nextfd), 0)
+               _, _, err1 = RawSyscall(SYS_FCNTL, uintptr(pipe), F_DUP2FD_CLOEXEC, uintptr(nextfd))
                if err1 != 0 {
                        goto childerror
                }
-               RawSyscall(SYS_FCNTL, uintptr(nextfd), F_SETFD, FD_CLOEXEC)
                pipe = nextfd
                nextfd++
        }
@@ -227,11 +226,10 @@ func forkAndExecInChild(argv0 *byte, argv, envv []*byte, chroot, dir *byte, attr
                        if nextfd == pipe { // don't stomp on pipe
                                nextfd++
                        }
-                       _, _, err1 = RawSyscall(SYS_DUP2, uintptr(fd[i]), uintptr(nextfd), 0)
+                       _, _, err1 = RawSyscall(SYS_FCNTL, uintptr(fd[i]), F_DUP2FD_CLOEXEC, uintptr(nextfd))
                        if err1 != 0 {
                                goto childerror
                        }
-                       RawSyscall(SYS_FCNTL, uintptr(nextfd), F_SETFD, FD_CLOEXEC)
                        fd[i] = nextfd
                        nextfd++
                }