Source file
src/os/exec/read3.go
1
2
3
4
5
6
7
8
9
10
11
12
13 package main
14
15 import (
16 "fmt"
17 "internal/poll"
18 "io"
19 "os"
20 "os/exec"
21 "os/exec/internal/fdtest"
22 "runtime"
23 "strings"
24 )
25
26 func main() {
27 fd3 := os.NewFile(3, "fd3")
28 defer fd3.Close()
29
30 bs, err := io.ReadAll(fd3)
31 if err != nil {
32 fmt.Printf("ReadAll from fd 3: %v\n", err)
33 os.Exit(1)
34 }
35
36
37
38
39
40
41
42
43 for fd := uintptr(4); fd <= 100; fd++ {
44 if poll.IsPollDescriptor(fd) {
45 continue
46 }
47
48 if !fdtest.Exists(fd) {
49 continue
50 }
51
52 fmt.Printf("leaked parent file. fdtest.Exists(%d) got true want false\n", fd)
53
54 fdfile := fmt.Sprintf("/proc/self/fd/%d", fd)
55 link, err := os.Readlink(fdfile)
56 fmt.Printf("readlink(%q) = %q, %v\n", fdfile, link, err)
57
58 var args []string
59 switch runtime.GOOS {
60 case "plan9":
61 args = []string{fmt.Sprintf("/proc/%d/fd", os.Getpid())}
62 case "aix", "solaris", "illumos":
63 args = []string{fmt.Sprint(os.Getpid())}
64 default:
65 args = []string{"-p", fmt.Sprint(os.Getpid())}
66 }
67
68
69 ofcmd := "lsof"
70 switch runtime.GOOS {
71 case "dragonfly", "freebsd", "netbsd", "openbsd":
72 ofcmd = "fstat"
73 case "plan9":
74 ofcmd = "/bin/cat"
75 case "aix":
76 ofcmd = "procfiles"
77 case "solaris", "illumos":
78 ofcmd = "pfiles"
79 }
80
81 cmd := exec.Command(ofcmd, args...)
82 out, err := cmd.CombinedOutput()
83 if err != nil {
84 fmt.Fprintf(os.Stderr, "%s failed: %v\n", strings.Join(cmd.Args, " "), err)
85 }
86 fmt.Printf("%s", out)
87 os.Exit(1)
88 }
89
90 os.Stdout.Write(bs)
91 }
92
View as plain text