| 1 | package mac |
| 2 | |
| 3 | import ( |
| 4 | "os" |
| 5 | "slices" |
| 6 | "strconv" |
| 7 | "strings" |
| 8 | "sync" |
| 9 | "syscall" |
| 10 | "testing" |
| 11 | "time" |
| 12 | ) |
| 13 | |
| 14 | // phantomPID is a process the table shows carrying a marker that no |
| 15 | // signal ends: an end that is never proven. |
| 16 | const phantomPID = 1 << 30 |
| 17 | |
| 18 | // phantom is the platform's table plus that process, for the markers in |
| 19 | // only (any marker when only is empty). |
| 20 | type phantom struct { |
| 21 | Procs |
| 22 | only []string |
| 23 | } |
| 24 | |
| 25 | func withPhantom(only ...string) *phantom { return &phantom{Procs: platformProcs, only: only} } |
| 26 | |
| 27 | func (p *phantom) ghost(m string) ProcInfo { |
| 28 | return ProcInfo{ID: ProcID{PID: phantomPID, Start: 1}, PPID: 1, PGID: phantomPID, SID: phantomPID, UID: os.Getuid(), Marker: m} |
| 29 | } |
| 30 | |
| 31 | func (p *phantom) List(deadline time.Time, markers []string) ([]ProcInfo, error) { |
| 32 | list, err := p.Procs.List(deadline, markers) |
| 33 | if err != nil { |
| 34 | return nil, err |
| 35 | } |
| 36 | for _, m := range markers { |
| 37 | if len(p.only) == 0 || slices.Contains(p.only, m) { |
| 38 | return append(list, p.ghost(m)), nil |
| 39 | } |
| 40 | } |
| 41 | return list, nil |
| 42 | } |
| 43 | |
| 44 | func (p *phantom) Lookup(pid int) (ProcInfo, bool) { |
| 45 | if pid == phantomPID { |
| 46 | return p.ghost(""), true |
| 47 | } |
| 48 | return p.Procs.Lookup(pid) |
| 49 | } |
| 50 | |
| 51 | func (p *phantom) Signal(pid int, sig syscall.Signal) error { |
| 52 | if pid == phantomPID || pid == -phantomPID { |
| 53 | return nil |
| 54 | } |
| 55 | return p.Procs.Signal(pid, sig) |
| 56 | } |
| 57 | |
| 58 | // table is a synthetic process table. Signals are recorded; a process |
| 59 | // signalled with SIGKILL (or SIGTERM, unless stubborn) leaves it. |
| 60 | type table struct { |
| 61 | mu sync.Mutex |
| 62 | procs map[int]ProcInfo |
| 63 | paths map[int]string |
| 64 | stubborn map[int]bool |
| 65 | sent []sent |
| 66 | // lookup, when set, runs before each Lookup (a race between the |
| 67 | // listing and the re-check). |
| 68 | lookup func(t *table, pid int) |
| 69 | // unkillable rows survive every signal; after runs (lock held) once a |
| 70 | // signal has been delivered to pid. |
| 71 | unkillable map[int]bool |
| 72 | after func(tb *table, pid int, sig syscall.Signal) |
| 73 | // hidden rows are left out of List (a read that missed them). |
| 74 | hidden map[int]bool |
| 75 | // clock is Now (10000 when 0); afterList runs (lock held) after each |
| 76 | // List, with how many lists there have been. |
| 77 | clock int64 |
| 78 | lists int |
| 79 | afterList func(tb *table, n int, markers []string) |
| 80 | } |
| 81 | |
| 82 | func (tb *table) Now() int64 { |
| 83 | tb.mu.Lock() |
| 84 | defer tb.mu.Unlock() |
| 85 | if tb.clock == 0 { |
| 86 | return 10000 |
| 87 | } |
| 88 | return tb.clock |
| 89 | } |
| 90 | |
| 91 | type sent struct { |
| 92 | PID int |
| 93 | Sig syscall.Signal |
| 94 | } |
| 95 | |
| 96 | // parseTable reads rows shaped like |
| 97 | // `ps -axo pid,ppid,pgid,sess,lstart,uid,comm` as the darwin table |
| 98 | // reports them (lstart as the kernel's microseconds), one per line, and |
| 99 | // a trailing marker=<m> where the environment shows one. "me" in the |
| 100 | // uid column is this test's uid, "self" in the pid column this process. |
| 101 | func parseTable(t *testing.T, rows string) *table { |
| 102 | t.Helper() |
| 103 | tb := &table{procs: map[int]ProcInfo{}, paths: map[int]string{}, stubborn: map[int]bool{}, unkillable: map[int]bool{}} |
| 104 | for _, line := range strings.Split(rows, "\n") { |
| 105 | f := strings.Fields(line) |
| 106 | if len(f) == 0 || strings.HasPrefix(f[0], "#") { |
| 107 | continue |
| 108 | } |
| 109 | if len(f) < 7 { |
| 110 | t.Fatalf("row %q", line) |
| 111 | } |
| 112 | n := func(s string) int { |
| 113 | switch s { |
| 114 | case "me": |
| 115 | return os.Getuid() |
| 116 | case "self": |
| 117 | return os.Getpid() |
| 118 | } |
| 119 | v, err := strconv.Atoi(s) |
| 120 | if err != nil { |
| 121 | t.Fatalf("row %q: %v", line, err) |
| 122 | } |
| 123 | return v |
| 124 | } |
| 125 | q := ProcInfo{ID: ProcID{PID: n(f[0]), Start: int64(n(f[4]))}, PPID: n(f[1]), PGID: n(f[2]), SID: n(f[3]), UID: n(f[5])} |
| 126 | if len(f) > 7 { |
| 127 | q.Marker = strings.TrimPrefix(f[7], "marker=") |
| 128 | } |
| 129 | if q.PID == os.Getpid() && f[0] != "self" { |
| 130 | t.Skip("a synthetic pid is this test's own") |
| 131 | } |
| 132 | tb.procs[q.PID] = q |
| 133 | tb.paths[q.PID] = f[6] |
| 134 | } |
| 135 | return tb |
| 136 | } |
| 137 | |
| 138 | func (tb *table) List(_ time.Time, markers []string) ([]ProcInfo, error) { |
| 139 | tb.mu.Lock() |
| 140 | defer tb.mu.Unlock() |
| 141 | var out []ProcInfo |
| 142 | for _, q := range tb.procs { |
| 143 | if tb.hidden[q.PID] { |
| 144 | continue |
| 145 | } |
| 146 | if !slices.Contains(markers, q.Marker) { |
| 147 | q.Marker = "" |
| 148 | } |
| 149 | out = append(out, q) |
| 150 | } |
| 151 | slices.SortFunc(out, func(a, b ProcInfo) int { return a.PID - b.PID }) |
| 152 | tb.lists++ |
| 153 | if tb.afterList != nil { |
| 154 | tb.afterList(tb, tb.lists, markers) |
| 155 | } |
| 156 | return out, nil |
| 157 | } |
| 158 | |
| 159 | func (tb *table) Lookup(pid int) (ProcInfo, bool) { |
| 160 | if tb.lookup != nil { |
| 161 | tb.lookup(tb, pid) |
| 162 | } |
| 163 | tb.mu.Lock() |
| 164 | defer tb.mu.Unlock() |
| 165 | // Every user's: the scope checks the user itself, whatever the |
| 166 | // table does. |
| 167 | q, ok := tb.procs[pid] |
| 168 | if !ok { |
| 169 | return ProcInfo{}, false |
| 170 | } |
| 171 | q.Marker = "" |
| 172 | return q, true |
| 173 | } |
| 174 | |
| 175 | func (tb *table) Path(pid int) string { |
| 176 | tb.mu.Lock() |
| 177 | defer tb.mu.Unlock() |
| 178 | return tb.paths[pid] |
| 179 | } |
| 180 | |
| 181 | func (tb *table) Sweeps() bool { return true } |
| 182 | |
| 183 | func (tb *table) Signal(pid int, sig syscall.Signal) error { |
| 184 | tb.mu.Lock() |
| 185 | defer tb.mu.Unlock() |
| 186 | var hit []int |
| 187 | for p, q := range tb.procs { |
| 188 | if p == pid || pid < 0 && q.PGID == -pid { |
| 189 | hit = append(hit, p) |
| 190 | } |
| 191 | } |
| 192 | if sig != 0 { |
| 193 | // Every attempt, even at nothing: a group signal sent on stale |
| 194 | // membership is the bug, whatever it reached. |
| 195 | tb.sent = append(tb.sent, sent{pid, sig}) |
| 196 | } |
| 197 | if len(hit) == 0 { |
| 198 | return syscall.ESRCH |
| 199 | } |
| 200 | if sig == 0 { |
| 201 | // The kernel says EPERM for one pid we may not signal. |
| 202 | if q := tb.procs[pid]; pid > 0 && q.UID != os.Getuid() { |
| 203 | return syscall.EPERM |
| 204 | } |
| 205 | return nil |
| 206 | } |
| 207 | for _, p := range hit { |
| 208 | if tb.unkillable[p] { |
| 209 | continue |
| 210 | } |
| 211 | if sig == syscall.SIGKILL || !tb.stubborn[p] { |
| 212 | delete(tb.procs, p) |
| 213 | delete(tb.paths, p) |
| 214 | } |
| 215 | } |
| 216 | if tb.after != nil { |
| 217 | tb.after(tb, pid, sig) |
| 218 | } |
| 219 | return nil |
| 220 | } |
| 221 | |
| 222 | // SessionGone: no row of any user is in session sid. |
| 223 | func (tb *table) SessionGone(sid int) bool { |
| 224 | tb.mu.Lock() |
| 225 | defer tb.mu.Unlock() |
| 226 | for _, q := range tb.procs { |
| 227 | if q.SID == sid { |
| 228 | return false |
| 229 | } |
| 230 | } |
| 231 | return true |
| 232 | } |
| 233 | |
| 234 | func (tb *table) remove(pid int) { |
| 235 | tb.mu.Lock() |
| 236 | defer tb.mu.Unlock() |
| 237 | delete(tb.procs, pid) |
| 238 | delete(tb.paths, pid) |
| 239 | } |
| 240 | |
| 241 | // reparent gives pid a new parent (its old one exited). |
| 242 | func (tb *table) reparent(pid, ppid int) { |
| 243 | tb.mu.Lock() |
| 244 | defer tb.mu.Unlock() |
| 245 | q := tb.procs[pid] |
| 246 | q.PPID = ppid |
| 247 | tb.procs[pid] = q |
| 248 | } |
| 249 | |
| 250 | func (tb *table) mark(set *map[int]bool, pid int) { |
| 251 | tb.mu.Lock() |
| 252 | defer tb.mu.Unlock() |
| 253 | if *set == nil { |
| 254 | *set = map[int]bool{} |
| 255 | } |
| 256 | (*set)[pid] = true |
| 257 | } |
| 258 | |
| 259 | func (tb *table) unmark(set *map[int]bool, pid int) { |
| 260 | tb.mu.Lock() |
| 261 | defer tb.mu.Unlock() |
| 262 | delete(*set, pid) |
| 263 | } |
| 264 | |
| 265 | // signalled is every pid (negative: a group) the table was signalled at. |
| 266 | func (tb *table) signalled() []int { |
| 267 | tb.mu.Lock() |
| 268 | defer tb.mu.Unlock() |
| 269 | var out []int |
| 270 | for _, s := range tb.sent { |
| 271 | if !slices.Contains(out, s.PID) { |
| 272 | out = append(out, s.PID) |
| 273 | } |
| 274 | } |
| 275 | return out |
| 276 | } |
| 277 | |
| 278 | func (tb *table) add(q ProcInfo, path string) { |
| 279 | tb.mu.Lock() |
| 280 | defer tb.mu.Unlock() |
| 281 | tb.procs[q.PID] = q |
| 282 | tb.paths[q.PID] = path |
| 283 | } |
| 284 | |
| 285 | func (tb *table) has(pid int) bool { |
| 286 | tb.mu.Lock() |
| 287 | defer tb.mu.Unlock() |
| 288 | _, ok := tb.procs[pid] |
| 289 | return ok |
| 290 | } |
| 291 | |