| 1 | package exporttest |
| 2 | |
| 3 | import ( |
| 4 | "bytes" |
| 5 | "fmt" |
| 6 | "os" |
| 7 | "os/exec" |
| 8 | "path/filepath" |
| 9 | "strconv" |
| 10 | "strings" |
| 11 | "sync" |
| 12 | "syscall" |
| 13 | "time" |
| 14 | ) |
| 15 | |
| 16 | // sysProcAttr puts a program in a session of its own, and has the kernel |
| 17 | // kill it when the test process dies. |
| 18 | func sysProcAttr() *syscall.SysProcAttr { |
| 19 | return &syscall.SysProcAttr{Setsid: true, Pdeathsig: syscall.SIGKILL} |
| 20 | } |
| 21 | |
| 22 | // killSession kills every process in the session sid, and again, for up |
| 23 | // to a second, until none is left, since a process may start another as |
| 24 | // it is killed. |
| 25 | func killSession(sid int) { |
| 26 | for range 100 { |
| 27 | pids := session(sid) |
| 28 | if len(pids) == 0 { |
| 29 | return |
| 30 | } |
| 31 | for _, pid := range pids { |
| 32 | syscall.Kill(pid, syscall.SIGKILL) |
| 33 | } |
| 34 | time.Sleep(10 * time.Millisecond) |
| 35 | } |
| 36 | } |
| 37 | |
| 38 | // session is the processes in the session sid that have not exited, as |
| 39 | // /proc lists them. |
| 40 | func session(sid int) []int { |
| 41 | stats, _ := filepath.Glob("/proc/[0-9]*/stat") |
| 42 | var pids []int |
| 43 | for _, path := range stats { |
| 44 | data, err := os.ReadFile(path) |
| 45 | if err != nil { |
| 46 | continue // the process has gone |
| 47 | } |
| 48 | // The command's name, in parentheses, may hold any byte. The |
| 49 | // fields after it are the state, the parent, the process group |
| 50 | // and the session. |
| 51 | i := bytes.LastIndexByte(data, ')') |
| 52 | if i < 0 { |
| 53 | continue |
| 54 | } |
| 55 | f := strings.Fields(string(data[i+1:])) |
| 56 | if len(f) < 4 || f[0] == "Z" || f[0] == "X" || f[3] != strconv.Itoa(sid) { |
| 57 | continue |
| 58 | } |
| 59 | if pid, err := strconv.Atoi(filepath.Base(filepath.Dir(path))); err == nil { |
| 60 | pids = append(pids, pid) |
| 61 | } |
| 62 | } |
| 63 | return pids |
| 64 | } |
| 65 | |
| 66 | // watchdog is a process that outlives the test process and kills what it |
| 67 | // left running. The test process tells it, on a pipe it alone holds open, |
| 68 | // of each session it starts, and of each it has emptied. When the test |
| 69 | // process dies, however it dies, the kernel closes the pipe, and the |
| 70 | // watchdog kills every process still in each session it was told of and |
| 71 | // not told was emptied. A session the test emptied is forgotten, so that |
| 72 | // a later session given the same ID is never the watchdog's to kill. |
| 73 | var watchdog struct { |
| 74 | once sync.Once |
| 75 | mu sync.Mutex |
| 76 | // w is the pipe's end the test process writes, or nil when the |
| 77 | // watchdog did not start. |
| 78 | w *os.File |
| 79 | } |
| 80 | |
| 81 | // watchdogScript is the watchdog, a shell that reads "+ SID" for a |
| 82 | // session the test started and "- SID" for one it has emptied. |
| 83 | const watchdogScript = ` |
| 84 | sessions=' ' |
| 85 | while read -r op sid; do |
| 86 | case $op in |
| 87 | +) sessions="$sessions$sid " ;; |
| 88 | -) |
| 89 | left=' ' |
| 90 | for s in $sessions; do [ "$s" = "$sid" ] || left="$left$s "; done |
| 91 | sessions=$left |
| 92 | ;; |
| 93 | esac |
| 94 | done |
| 95 | [ "$sessions" = ' ' ] && exit 0 |
| 96 | for pass in 1 2 3; do |
| 97 | for stat in /proc/[0-9]*/stat; do |
| 98 | read -r line < "$stat" || continue |
| 99 | set -- ${line##*\) } |
| 100 | case $1 in Z|X) continue ;; esac |
| 101 | case $sessions in *" $4 "*) |
| 102 | pid=${stat#/proc/} |
| 103 | kill -9 "${pid%/stat}" |
| 104 | ;; |
| 105 | esac |
| 106 | done 2> /dev/null |
| 107 | done |
| 108 | ` |
| 109 | |
| 110 | // watch tells the watchdog of the session sid, starting the watchdog |
| 111 | // first if this is the test process's first session. |
| 112 | func watch(sid int) { |
| 113 | watchdog.once.Do(startWatchdog) |
| 114 | tell("+", sid) |
| 115 | } |
| 116 | |
| 117 | // unwatch tells the watchdog that the session sid is empty. |
| 118 | func unwatch(sid int) { tell("-", sid) } |
| 119 | |
| 120 | func tell(op string, sid int) { |
| 121 | watchdog.mu.Lock() |
| 122 | defer watchdog.mu.Unlock() |
| 123 | if watchdog.w != nil { |
| 124 | fmt.Fprintf(watchdog.w, "%s %d\n", op, sid) |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | // startWatchdog starts the watchdog in a session of its own, with no |
| 129 | // death signal, so it outlives the test process. Its output is |
| 130 | // /dev/null, since go test waits until no process holds the test's output |
| 131 | // open. It is never waited for: it exits once the test process has. |
| 132 | func startWatchdog() { |
| 133 | r, w, err := os.Pipe() |
| 134 | if err != nil { |
| 135 | return |
| 136 | } |
| 137 | cmd := exec.Command("/bin/sh", "-c", watchdogScript) |
| 138 | cmd.Stdin = r |
| 139 | cmd.SysProcAttr = &syscall.SysProcAttr{Setsid: true} |
| 140 | err = cmd.Start() |
| 141 | r.Close() |
| 142 | if err != nil { |
| 143 | w.Close() |
| 144 | return |
| 145 | } |
| 146 | watchdog.w = w |
| 147 | } |