| 1 | package proc |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "errors" |
| 6 | "os" |
| 7 | "os/exec" |
| 8 | "syscall" |
| 9 | "time" |
| 10 | "unsafe" |
| 11 | |
| 12 | "golang.org/x/sys/unix" |
| 13 | ) |
| 14 | |
| 15 | // sysctlProcs is the Mac's process table: kern.proc.ruid (pid, parent, |
| 16 | // group, the kernel's start time to the microsecond, real uid, so a |
| 17 | // setuid program the attempt ran is still listed), getsid for the |
| 18 | // session, proc_pidpath for the executable, and ps -E for the marker |
| 19 | // (which it shows only for non-platform binaries on macOS 27.2). |
| 20 | type sysctlProcs struct{ uid int } |
| 21 | |
| 22 | var Platform Table = sysctlProcs{uid: os.Getuid()} |
| 23 | |
| 24 | // Machine is the table a Mac's turns sweep with; nil off a Mac. |
| 25 | func Machine() Table { return Platform } |
| 26 | |
| 27 | func (p sysctlProcs) table() ([]Info, error) { |
| 28 | kps, err := unix.SysctlKinfoProcSlice("kern.proc.ruid", p.uid) |
| 29 | if err != nil { |
| 30 | return nil, err |
| 31 | } |
| 32 | out := make([]Info, 0, len(kps)) |
| 33 | for i := range kps { |
| 34 | if q, ok := fromKinfo(&kps[i]); ok && q.UID == p.uid { |
| 35 | out = append(out, q) |
| 36 | } |
| 37 | } |
| 38 | return out, nil |
| 39 | } |
| 40 | |
| 41 | func fromKinfo(k *unix.KinfoProc) (Info, bool) { |
| 42 | pid := int(k.Proc.P_pid) |
| 43 | if pid <= 0 { |
| 44 | return Info{}, false |
| 45 | } |
| 46 | start := k.Proc.P_starttime.Sec*1_000_000 + int64(k.Proc.P_starttime.Usec) |
| 47 | sid, err := unix.Getsid(pid) |
| 48 | if err != nil { |
| 49 | sid = -1 |
| 50 | } |
| 51 | return Info{ID: ID{PID: pid, Start: start}, PPID: int(k.Eproc.Ppid), PGID: int(k.Eproc.Pgid), SID: sid, UID: int(k.Eproc.Pcred.P_ruid)}, true |
| 52 | } |
| 53 | |
| 54 | // List reads the table, then ps -E, then the table again: a process is |
| 55 | // marked only when ps named its pid and that pid was the same process |
| 56 | // both times (so a pid used again meanwhile is never taken as marked). |
| 57 | func (p sysctlProcs) List(deadline time.Time, markers []string) ([]Info, error) { |
| 58 | first, err := p.table() |
| 59 | if err != nil || len(markers) == 0 { |
| 60 | return first, err |
| 61 | } |
| 62 | // ps is bounded by the caller's deadline (and never past 10 s). |
| 63 | ctx, cancel := context.WithDeadline(context.Background(), deadline) |
| 64 | defer cancel() |
| 65 | ctx, cancel2 := context.WithTimeout(ctx, 10*time.Second) |
| 66 | defer cancel2() |
| 67 | // Not StartOwn: List runs under Own's write lock, and ps is started |
| 68 | // after the first table and reaped before the second, so neither |
| 69 | // lists it. |
| 70 | raw, err := exec.CommandContext(ctx, "/bin/ps", "-E", "-ww", "-x", "-o", "pid=,pgid=,command=").Output() |
| 71 | if err != nil { |
| 72 | return nil, err |
| 73 | } |
| 74 | second, err := p.table() |
| 75 | if err != nil { |
| 76 | return nil, err |
| 77 | } |
| 78 | was := Index(first) |
| 79 | for _, m := range markers { |
| 80 | for _, hit := range ParseMarked(string(raw), m, os.Getpid()) { |
| 81 | for i := range second { |
| 82 | if second[i].PID == hit.PID && was[hit.PID].ID == second[i].ID { |
| 83 | second[i].Marker = m |
| 84 | } |
| 85 | } |
| 86 | } |
| 87 | } |
| 88 | return second, nil |
| 89 | } |
| 90 | |
| 91 | func (p sysctlProcs) Lookup(pid int) (Info, bool) { |
| 92 | if pid <= 0 { |
| 93 | return Info{}, false |
| 94 | } |
| 95 | k, err := unix.SysctlKinfoProc("kern.proc.pid", pid) |
| 96 | if err != nil { |
| 97 | return Info{}, false |
| 98 | } |
| 99 | q, ok := fromKinfo(k) |
| 100 | if !ok || q.PID != pid || q.UID != p.uid { |
| 101 | return Info{}, false |
| 102 | } |
| 103 | return q, true |
| 104 | } |
| 105 | |
| 106 | // Path is proc_pidpath: __proc_info(PROC_INFO_CALL_PIDINFO, |
| 107 | // pid, PROC_PIDPATHINFO, 0, buf, PROC_PIDPATHINFO_MAXSIZE), the path of |
| 108 | // the vnode the kernel runs, which the process cannot set. |
| 109 | func (sysctlProcs) Path(pid int) string { |
| 110 | const callPIDInfo, pidPathInfo, maxSize = 2, 11, 4096 |
| 111 | buf := make([]byte, maxSize) |
| 112 | n, _, errno := unix.Syscall6(unix.SYS_PROC_INFO, callPIDInfo, uintptr(pid), pidPathInfo, 0, uintptr(unsafe.Pointer(&buf[0])), maxSize) |
| 113 | if errno != 0 || n == 0 || n > maxSize { |
| 114 | return "" |
| 115 | } |
| 116 | for i := 0; i < int(n); i++ { |
| 117 | if buf[i] == 0 { |
| 118 | return string(buf[:i]) |
| 119 | } |
| 120 | } |
| 121 | return string(buf[:n]) |
| 122 | } |
| 123 | |
| 124 | // SessionGone reads every user's processes (kern.proc.all) and asks |
| 125 | // each for its session: gone only when none is in sid and every answer |
| 126 | // was read. |
| 127 | func (sysctlProcs) SessionGone(sid int) bool { |
| 128 | kps, err := unix.SysctlKinfoProcSlice("kern.proc.all") |
| 129 | if err != nil { |
| 130 | return false |
| 131 | } |
| 132 | for i := range kps { |
| 133 | pid := int(kps[i].Proc.P_pid) |
| 134 | if pid <= 0 { |
| 135 | continue |
| 136 | } |
| 137 | got, err := unix.Getsid(pid) |
| 138 | if errors.Is(err, unix.ESRCH) { |
| 139 | continue |
| 140 | } |
| 141 | if err != nil || got == sid { |
| 142 | return false |
| 143 | } |
| 144 | } |
| 145 | return true |
| 146 | } |
| 147 | |
| 148 | func (sysctlProcs) Sweeps() bool { return true } |
| 149 | |
| 150 | // Now is the wall clock in microseconds, as kern.proc's p_starttime. |
| 151 | func (sysctlProcs) Now() int64 { return time.Now().UnixMicro() } |
| 152 | |
| 153 | func (sysctlProcs) Signal(pid int, sig syscall.Signal) error { return syscall.Kill(pid, sig) } |
| 154 | |