package mac import ( "os" "slices" "strconv" "strings" "sync" "syscall" "testing" "time" ) // phantomPID is a process the table shows carrying a marker that no // signal ends: an end that is never proven. const phantomPID = 1 << 30 // phantom is the platform's table plus that process, for the markers in // only (any marker when only is empty). type phantom struct { Procs only []string } func withPhantom(only ...string) *phantom { return &phantom{Procs: platformProcs, only: only} } func (p *phantom) ghost(m string) ProcInfo { return ProcInfo{ID: ProcID{PID: phantomPID, Start: 1}, PPID: 1, PGID: phantomPID, SID: phantomPID, UID: os.Getuid(), Marker: m} } func (p *phantom) List(deadline time.Time, markers []string) ([]ProcInfo, error) { list, err := p.Procs.List(deadline, markers) if err != nil { return nil, err } for _, m := range markers { if len(p.only) == 0 || slices.Contains(p.only, m) { return append(list, p.ghost(m)), nil } } return list, nil } func (p *phantom) Lookup(pid int) (ProcInfo, bool) { if pid == phantomPID { return p.ghost(""), true } return p.Procs.Lookup(pid) } func (p *phantom) Signal(pid int, sig syscall.Signal) error { if pid == phantomPID || pid == -phantomPID { return nil } return p.Procs.Signal(pid, sig) } // table is a synthetic process table. Signals are recorded; a process // signalled with SIGKILL (or SIGTERM, unless stubborn) leaves it. type table struct { mu sync.Mutex procs map[int]ProcInfo paths map[int]string stubborn map[int]bool sent []sent // lookup, when set, runs before each Lookup (a race between the // listing and the re-check). lookup func(t *table, pid int) // unkillable rows survive every signal; after runs (lock held) once a // signal has been delivered to pid. unkillable map[int]bool after func(tb *table, pid int, sig syscall.Signal) // hidden rows are left out of List (a read that missed them). hidden map[int]bool // clock is Now (10000 when 0); afterList runs (lock held) after each // List, with how many lists there have been. clock int64 lists int afterList func(tb *table, n int, markers []string) } func (tb *table) Now() int64 { tb.mu.Lock() defer tb.mu.Unlock() if tb.clock == 0 { return 10000 } return tb.clock } type sent struct { PID int Sig syscall.Signal } // parseTable reads rows shaped like // `ps -axo pid,ppid,pgid,sess,lstart,uid,comm` as the darwin table // reports them (lstart as the kernel's microseconds), one per line, and // a trailing marker= where the environment shows one. "me" in the // uid column is this test's uid, "self" in the pid column this process. func parseTable(t *testing.T, rows string) *table { t.Helper() tb := &table{procs: map[int]ProcInfo{}, paths: map[int]string{}, stubborn: map[int]bool{}, unkillable: map[int]bool{}} for _, line := range strings.Split(rows, "\n") { f := strings.Fields(line) if len(f) == 0 || strings.HasPrefix(f[0], "#") { continue } if len(f) < 7 { t.Fatalf("row %q", line) } n := func(s string) int { switch s { case "me": return os.Getuid() case "self": return os.Getpid() } v, err := strconv.Atoi(s) if err != nil { t.Fatalf("row %q: %v", line, err) } return v } 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])} if len(f) > 7 { q.Marker = strings.TrimPrefix(f[7], "marker=") } if q.PID == os.Getpid() && f[0] != "self" { t.Skip("a synthetic pid is this test's own") } tb.procs[q.PID] = q tb.paths[q.PID] = f[6] } return tb } func (tb *table) List(_ time.Time, markers []string) ([]ProcInfo, error) { tb.mu.Lock() defer tb.mu.Unlock() var out []ProcInfo for _, q := range tb.procs { if tb.hidden[q.PID] { continue } if !slices.Contains(markers, q.Marker) { q.Marker = "" } out = append(out, q) } slices.SortFunc(out, func(a, b ProcInfo) int { return a.PID - b.PID }) tb.lists++ if tb.afterList != nil { tb.afterList(tb, tb.lists, markers) } return out, nil } func (tb *table) Lookup(pid int) (ProcInfo, bool) { if tb.lookup != nil { tb.lookup(tb, pid) } tb.mu.Lock() defer tb.mu.Unlock() // Every user's: the scope checks the user itself, whatever the // table does. q, ok := tb.procs[pid] if !ok { return ProcInfo{}, false } q.Marker = "" return q, true } func (tb *table) Path(pid int) string { tb.mu.Lock() defer tb.mu.Unlock() return tb.paths[pid] } func (tb *table) Sweeps() bool { return true } func (tb *table) Signal(pid int, sig syscall.Signal) error { tb.mu.Lock() defer tb.mu.Unlock() var hit []int for p, q := range tb.procs { if p == pid || pid < 0 && q.PGID == -pid { hit = append(hit, p) } } if sig != 0 { // Every attempt, even at nothing: a group signal sent on stale // membership is the bug, whatever it reached. tb.sent = append(tb.sent, sent{pid, sig}) } if len(hit) == 0 { return syscall.ESRCH } if sig == 0 { // The kernel says EPERM for one pid we may not signal. if q := tb.procs[pid]; pid > 0 && q.UID != os.Getuid() { return syscall.EPERM } return nil } for _, p := range hit { if tb.unkillable[p] { continue } if sig == syscall.SIGKILL || !tb.stubborn[p] { delete(tb.procs, p) delete(tb.paths, p) } } if tb.after != nil { tb.after(tb, pid, sig) } return nil } // SessionGone: no row of any user is in session sid. func (tb *table) SessionGone(sid int) bool { tb.mu.Lock() defer tb.mu.Unlock() for _, q := range tb.procs { if q.SID == sid { return false } } return true } func (tb *table) remove(pid int) { tb.mu.Lock() defer tb.mu.Unlock() delete(tb.procs, pid) delete(tb.paths, pid) } // reparent gives pid a new parent (its old one exited). func (tb *table) reparent(pid, ppid int) { tb.mu.Lock() defer tb.mu.Unlock() q := tb.procs[pid] q.PPID = ppid tb.procs[pid] = q } func (tb *table) mark(set *map[int]bool, pid int) { tb.mu.Lock() defer tb.mu.Unlock() if *set == nil { *set = map[int]bool{} } (*set)[pid] = true } func (tb *table) unmark(set *map[int]bool, pid int) { tb.mu.Lock() defer tb.mu.Unlock() delete(*set, pid) } // signalled is every pid (negative: a group) the table was signalled at. func (tb *table) signalled() []int { tb.mu.Lock() defer tb.mu.Unlock() var out []int for _, s := range tb.sent { if !slices.Contains(out, s.PID) { out = append(out, s.PID) } } return out } func (tb *table) add(q ProcInfo, path string) { tb.mu.Lock() defer tb.mu.Unlock() tb.procs[q.PID] = q tb.paths[q.PID] = path } func (tb *table) has(pid int) bool { tb.mu.Lock() defer tb.mu.Unlock() _, ok := tb.procs[pid] return ok }