diff options
| author | Tobias Klauser <tklauser@distanz.ch> | 2021-10-25 13:37:29 +0200 |
|---|---|---|
| committer | Tobias Klauser <tobias.klauser@gmail.com> | 2021-10-26 05:02:53 +0000 |
| commit | ec6c00418c83aa4f68a5afc9803831f43f8d794f (patch) | |
| tree | d3261a139ab78281420ba2ff0450a48050580a4b /src/syscall/exec_libc.go | |
| parent | adfb85b3150d5ebe36440f82d83be88403951319 (diff) | |
| download | go-ec6c00418c83aa4f68a5afc9803831f43f8d794f.tar.xz | |
syscall: use fcntl F_DUP2FD_CLOEXEC in forkAndExecInChild on illumos
Use fcntl(oldfd, F_DUP2FD_CLOEXEC, newfd) to duplicate the file
descriptor and mark is as close-on-exec instead of dup2 & fcntl.
Illumos implements dup3 like this in libc.
Change-Id: I9782bce553ffb832e9b1a12bbf3c0a40c821f56e
Reviewed-on: https://go-review.googlesource.com/c/go/+/358374
Trust: Tobias Klauser <tobias.klauser@gmail.com>
Run-TryBot: Tobias Klauser <tobias.klauser@gmail.com>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Ian Lance Taylor <iant@golang.org>
Diffstat (limited to 'src/syscall/exec_libc.go')
| -rw-r--r-- | src/syscall/exec_libc.go | 26 |
1 files changed, 20 insertions, 6 deletions
diff --git a/src/syscall/exec_libc.go b/src/syscall/exec_libc.go index 8a84954051..b14abd4e63 100644 --- a/src/syscall/exec_libc.go +++ b/src/syscall/exec_libc.go @@ -10,6 +10,7 @@ package syscall import ( + "runtime" "unsafe" ) @@ -197,11 +198,19 @@ 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 = dup2child(uintptr(pipe), uintptr(nextfd)) + switch runtime.GOOS { + case "illumos": + _, err1 = fcntl1(uintptr(pipe), _F_DUP2FD_CLOEXEC, uintptr(nextfd)) + default: + _, err1 = dup2child(uintptr(pipe), uintptr(nextfd)) + if err1 != 0 { + goto childerror + } + _, err1 = fcntl1(uintptr(nextfd), F_SETFD, FD_CLOEXEC) + } if err1 != 0 { goto childerror } - fcntl1(uintptr(nextfd), F_SETFD, FD_CLOEXEC) pipe = nextfd nextfd++ } @@ -210,11 +219,16 @@ func forkAndExecInChild(argv0 *byte, argv, envv []*byte, chroot, dir *byte, attr if nextfd == pipe { // don't stomp on pipe nextfd++ } - _, err1 = dup2child(uintptr(fd[i]), uintptr(nextfd)) - if err1 != 0 { - goto childerror + switch runtime.GOOS { + case "illumos": + _, err1 = fcntl1(uintptr(fd[i]), _F_DUP2FD_CLOEXEC, uintptr(nextfd)) + default: + _, err1 = dup2child(uintptr(fd[i]), uintptr(nextfd)) + if err1 != 0 { + goto childerror + } + _, err1 = fcntl1(uintptr(nextfd), F_SETFD, FD_CLOEXEC) } - _, err1 = fcntl1(uintptr(nextfd), F_SETFD, FD_CLOEXEC) if err1 != 0 { goto childerror } |
