| 1 | package proc |
| 2 | |
| 3 | import ( |
| 4 | "bytes" |
| 5 | "os" |
| 6 | "path/filepath" |
| 7 | "strconv" |
| 8 | "strings" |
| 9 | "syscall" |
| 10 | "time" |
| 11 | ) |
| 12 | |
| 13 | // procfs is the process table on Linux, where a Mac's runner is only |
| 14 | // ever tested (the test build's fake worker, and this package's tests): |
| 15 | // /proc/<pid>/stat for parent, group, session and start, the directory's |
| 16 | // owner for the user, environ for the marker, exe for the path. |
| 17 | // |
| 18 | // Linux machines are shared, so it never drives a runner-user sweep |
| 19 | // unless scoped: then it lists only processes whose environment carries |
| 20 | // EQUIPO_TEST_SCOPE=<scope>, a test's own. |
| 21 | type procfs struct { |
| 22 | uid int |
| 23 | // scope is the environment entries (any one) that tag a test's own |
| 24 | // processes, NUL-joined so the table stays comparable; "" lists all. |
| 25 | scope string |
| 26 | } |
| 27 | |
| 28 | var Platform Table = procfs{uid: os.Getuid()} |
| 29 | |
| 30 | // Machine is the table a Mac's turns sweep with; nil off a Mac. |
| 31 | func Machine() Table { return nil } |
| 32 | |
| 33 | // ScopeEnv tags a test's own processes for a scoped table. |
| 34 | const ScopeEnv = "EQUIPO_TEST_SCOPE" |
| 35 | |
| 36 | func (p procfs) read(pid int, markers []string) (Info, bool) { |
| 37 | dir := filepath.Join("/proc", strconv.Itoa(pid)) |
| 38 | fi, err := os.Stat(dir) |
| 39 | if err != nil { |
| 40 | return Info{}, false |
| 41 | } |
| 42 | if st, ok := fi.Sys().(*syscall.Stat_t); !ok || int(st.Uid) != p.uid { |
| 43 | return Info{}, false |
| 44 | } |
| 45 | stat, err := os.ReadFile(filepath.Join(dir, "stat")) |
| 46 | if err != nil { |
| 47 | return Info{}, false |
| 48 | } |
| 49 | end := bytes.LastIndexByte(stat, ')') |
| 50 | if end < 0 { |
| 51 | return Info{}, false |
| 52 | } |
| 53 | // pid (comm) state ppid pgrp session tty tpgid flags minflt cminflt |
| 54 | // majflt cmajflt utime stime cutime cstime priority nice threads |
| 55 | // itrealvalue starttime ... |
| 56 | f := strings.Fields(string(stat[end+1:])) |
| 57 | if len(f) < 20 { |
| 58 | return Info{}, false |
| 59 | } |
| 60 | num := func(s string) int { n, _ := strconv.Atoi(s); return n } |
| 61 | start, _ := strconv.ParseInt(f[19], 10, 64) |
| 62 | q := Info{ID: ID{PID: pid, Start: start}, PPID: num(f[1]), PGID: num(f[2]), SID: num(f[3]), UID: p.uid} |
| 63 | if p.scope == "" && len(markers) == 0 { |
| 64 | return q, true |
| 65 | } |
| 66 | env, err := os.ReadFile(filepath.Join(dir, "environ")) |
| 67 | if err != nil { |
| 68 | // Unreadable (gone, or not dumpable): unmarked, and outside any |
| 69 | // scope. |
| 70 | return q, p.scope == "" |
| 71 | } |
| 72 | vars := bytes.Split(env, []byte{0}) |
| 73 | has := func(kv string) bool { |
| 74 | for _, v := range vars { |
| 75 | if string(v) == kv { |
| 76 | return true |
| 77 | } |
| 78 | } |
| 79 | return false |
| 80 | } |
| 81 | if p.scope != "" { |
| 82 | in := false |
| 83 | for _, kv := range strings.Split(p.scope, "\x00") { |
| 84 | in = in || has(kv) |
| 85 | } |
| 86 | if !in { |
| 87 | return Info{}, false |
| 88 | } |
| 89 | } |
| 90 | for _, m := range markers { |
| 91 | if has("EQUIPO_ATTEMPT=" + m) { |
| 92 | q.Marker = m |
| 93 | break |
| 94 | } |
| 95 | } |
| 96 | return q, true |
| 97 | } |
| 98 | |
| 99 | func (p procfs) List(_ time.Time, markers []string) ([]Info, error) { |
| 100 | entries, err := os.ReadDir("/proc") |
| 101 | if err != nil { |
| 102 | return nil, err |
| 103 | } |
| 104 | var out []Info |
| 105 | for _, e := range entries { |
| 106 | pid, err := strconv.Atoi(e.Name()) |
| 107 | if err != nil { |
| 108 | continue |
| 109 | } |
| 110 | if q, ok := p.read(pid, markers); ok { |
| 111 | out = append(out, q) |
| 112 | } |
| 113 | } |
| 114 | return out, nil |
| 115 | } |
| 116 | |
| 117 | func (p procfs) Lookup(pid int) (Info, bool) { |
| 118 | if pid <= 0 { |
| 119 | return Info{}, false |
| 120 | } |
| 121 | return p.read(pid, nil) |
| 122 | } |
| 123 | |
| 124 | func (p procfs) Path(pid int) string { |
| 125 | if _, ok := p.Lookup(pid); !ok { |
| 126 | return "" |
| 127 | } |
| 128 | path, err := os.Readlink(filepath.Join("/proc", strconv.Itoa(pid), "exe")) |
| 129 | if err != nil { |
| 130 | return "" |
| 131 | } |
| 132 | return path |
| 133 | } |
| 134 | |
| 135 | // SessionGone reads every process's session from /proc (any user's). |
| 136 | func (procfs) SessionGone(sid int) bool { |
| 137 | entries, err := os.ReadDir("/proc") |
| 138 | if err != nil { |
| 139 | return false |
| 140 | } |
| 141 | for _, e := range entries { |
| 142 | if _, err := strconv.Atoi(e.Name()); err != nil { |
| 143 | continue |
| 144 | } |
| 145 | stat, err := os.ReadFile(filepath.Join("/proc", e.Name(), "stat")) |
| 146 | if err != nil { |
| 147 | continue |
| 148 | } |
| 149 | end := bytes.LastIndexByte(stat, ')') |
| 150 | if f := strings.Fields(string(stat[end+1:])); end >= 0 && len(f) > 3 && f[3] == strconv.Itoa(sid) { |
| 151 | return false |
| 152 | } |
| 153 | } |
| 154 | return true |
| 155 | } |
| 156 | |
| 157 | func (p procfs) Sweeps() bool { return p.scope != "" } |
| 158 | |
| 159 | // Now is clock ticks since boot (USER_HZ, 100), as /proc's starttime. |
| 160 | func (procfs) Now() int64 { |
| 161 | raw, err := os.ReadFile("/proc/uptime") |
| 162 | if err != nil { |
| 163 | return 0 |
| 164 | } |
| 165 | f := strings.Fields(string(raw)) |
| 166 | if len(f) == 0 { |
| 167 | return 0 |
| 168 | } |
| 169 | up, _ := strconv.ParseFloat(f[0], 64) |
| 170 | return int64(up * 100) |
| 171 | } |
| 172 | |
| 173 | // Signal refuses to signal outside the scope: a scoped table's signals |
| 174 | // reach only the test's own processes (a group only while a member of |
| 175 | // it is the test's). |
| 176 | func (p procfs) Signal(pid int, sig syscall.Signal) error { |
| 177 | if p.scope != "" && sig != 0 { |
| 178 | target := pid |
| 179 | if pid < 0 { |
| 180 | target = -pid |
| 181 | if _, ok := p.Lookup(target); !ok { |
| 182 | list, err := p.List(time.Time{}, nil) |
| 183 | if err != nil { |
| 184 | return err |
| 185 | } |
| 186 | found := false |
| 187 | for _, q := range list { |
| 188 | found = found || q.PGID == target |
| 189 | } |
| 190 | if !found { |
| 191 | return syscall.ESRCH |
| 192 | } |
| 193 | } |
| 194 | } else if _, ok := p.Lookup(target); !ok { |
| 195 | return syscall.ESRCH |
| 196 | } |
| 197 | } |
| 198 | return syscall.Kill(pid, sig) |
| 199 | } |
| 200 | |
| 201 | // Scoped is a table of only the processes whose environment holds |
| 202 | // one of entries (tests: EQUIPO_TEST_SCOPE=<nonce>, or a test's own |
| 203 | // EQUIPO_ATTEMPT=<unique marker>). |
| 204 | func Scoped(entries ...string) Table { |
| 205 | return procfs{uid: os.Getuid(), scope: strings.Join(entries, "\x00")} |
| 206 | } |
| 207 | |