aboutsummaryrefslogtreecommitdiff
path: root/src/os/exec/read3.go
diff options
context:
space:
mode:
authorIan Lance Taylor <iant@golang.org>2020-04-16 18:19:59 -0700
committerIan Lance Taylor <iant@golang.org>2020-04-18 19:58:12 +0000
commit98c6b9844b3c3a14eaa46515a7f63fed2deb57ce (patch)
treead8e07b520294e60fe29f34b80f21b2bb1add4a8 /src/os/exec/read3.go
parent2a20f5c47456dbe648100d37831ca439cc79a9ff (diff)
downloadgo-98c6b9844b3c3a14eaa46515a7f63fed2deb57ce.tar.gz
go-98c6b9844b3c3a14eaa46515a7f63fed2deb57ce.zip
os/exec: build TestExtraFiles subprocess without cgo
Fixes #25628 Change-Id: I8b69e59f9c0123c4f65b5931d7c6d7ecc1c720e8 Reviewed-on: https://go-review.googlesource.com/c/go/+/228639 Run-TryBot: Ian Lance Taylor <iant@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Bryan C. Mills <bcmills@google.com>
Diffstat (limited to 'src/os/exec/read3.go')
-rw-r--r--src/os/exec/read3.go99
1 files changed, 99 insertions, 0 deletions
diff --git a/src/os/exec/read3.go b/src/os/exec/read3.go
new file mode 100644
index 0000000000..25d732a991
--- /dev/null
+++ b/src/os/exec/read3.go
@@ -0,0 +1,99 @@
+// Copyright 2020 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+// +build ignore
+
+// This is a test program that verifies that it can read from
+// descriptor 3 and that no other descriptors are open.
+// This is not done via TestHelperProcess and GO_WANT_HELPER_PROCESS
+// because we want to ensure that this program does not use cgo,
+// because C libraries can open file descriptors behind our backs
+// and confuse the test. See issue 25628.
+package main
+
+import (
+ "fmt"
+ "internal/poll"
+ "io/ioutil"
+ "os"
+ "os/exec"
+ "runtime"
+ "strings"
+)
+
+func main() {
+ fd3 := os.NewFile(3, "fd3")
+ bs, err := ioutil.ReadAll(fd3)
+ if err != nil {
+ fmt.Printf("ReadAll from fd 3: %v\n", err)
+ os.Exit(1)
+ }
+
+ // Now verify that there are no other open fds.
+ // stdin == 0
+ // stdout == 1
+ // stderr == 2
+ // descriptor from parent == 3
+ // All descriptors 4 and up should be available,
+ // except for any used by the network poller.
+ var files []*os.File
+ for wantfd := uintptr(4); wantfd <= 100; wantfd++ {
+ if poll.IsPollDescriptor(wantfd) {
+ continue
+ }
+ f, err := os.Open(os.Args[0])
+ if err != nil {
+ fmt.Printf("error opening file with expected fd %d: %v", wantfd, err)
+ os.Exit(1)
+ }
+ if got := f.Fd(); got != wantfd {
+ fmt.Printf("leaked parent file. fd = %d; want %d\n", got, wantfd)
+ fdfile := fmt.Sprintf("/proc/self/fd/%d", wantfd)
+ link, err := os.Readlink(fdfile)
+ fmt.Printf("readlink(%q) = %q, %v\n", fdfile, link, err)
+ var args []string
+ switch runtime.GOOS {
+ case "plan9":
+ args = []string{fmt.Sprintf("/proc/%d/fd", os.Getpid())}
+ case "aix":
+ args = []string{fmt.Sprint(os.Getpid())}
+ default:
+ args = []string{"-p", fmt.Sprint(os.Getpid())}
+ }
+
+ // Determine which command to use to display open files.
+ ofcmd := "lsof"
+ switch runtime.GOOS {
+ case "dragonfly", "freebsd", "netbsd", "openbsd":
+ ofcmd = "fstat"
+ case "plan9":
+ ofcmd = "/bin/cat"
+ case "aix":
+ ofcmd = "procfiles"
+ }
+
+ cmd := exec.Command(ofcmd, args...)
+ out, err := cmd.CombinedOutput()
+ if err != nil {
+ fmt.Fprintf(os.Stderr, "%s failed: %v\n", strings.Join(cmd.Args, " "), err)
+ }
+ fmt.Printf("%s", out)
+ os.Exit(1)
+ }
+ files = append(files, f)
+ }
+
+ for _, f := range files {
+ f.Close()
+ }
+
+ // Referring to fd3 here ensures that it is not
+ // garbage collected, and therefore closed, while
+ // executing the wantfd loop above. It doesn't matter
+ // what we do with fd3 as long as we refer to it;
+ // closing it is the easy choice.
+ fd3.Close()
+
+ os.Stdout.Write(bs)
+}