package proc import ( "context" "errors" "os" "os/exec" "syscall" "time" "unsafe" "golang.org/x/sys/unix" ) // sysctlProcs is the Mac's process table: kern.proc.ruid (pid, parent, // group, the kernel's start time to the microsecond, real uid, so a // setuid program the attempt ran is still listed), getsid for the // session, proc_pidpath for the executable, and ps -E for the marker // (which it shows only for non-platform binaries on macOS 27.2). type sysctlProcs struct{ uid int } var Platform Table = sysctlProcs{uid: os.Getuid()} // Machine is the table a Mac's turns sweep with; nil off a Mac. func Machine() Table { return Platform } func (p sysctlProcs) table() ([]Info, error) { kps, err := unix.SysctlKinfoProcSlice("kern.proc.ruid", p.uid) if err != nil { return nil, err } out := make([]Info, 0, len(kps)) for i := range kps { if q, ok := fromKinfo(&kps[i]); ok && q.UID == p.uid { out = append(out, q) } } return out, nil } func fromKinfo(k *unix.KinfoProc) (Info, bool) { pid := int(k.Proc.P_pid) if pid <= 0 { return Info{}, false } start := k.Proc.P_starttime.Sec*1_000_000 + int64(k.Proc.P_starttime.Usec) sid, err := unix.Getsid(pid) if err != nil { sid = -1 } 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 } // List reads the table, then ps -E, then the table again: a process is // marked only when ps named its pid and that pid was the same process // both times (so a pid used again meanwhile is never taken as marked). func (p sysctlProcs) List(deadline time.Time, markers []string) ([]Info, error) { first, err := p.table() if err != nil || len(markers) == 0 { return first, err } // ps is bounded by the caller's deadline (and never past 10 s). ctx, cancel := context.WithDeadline(context.Background(), deadline) defer cancel() ctx, cancel2 := context.WithTimeout(ctx, 10*time.Second) defer cancel2() // Not StartOwn: List runs under Own's write lock, and ps is started // after the first table and reaped before the second, so neither // lists it. raw, err := exec.CommandContext(ctx, "/bin/ps", "-E", "-ww", "-x", "-o", "pid=,pgid=,command=").Output() if err != nil { return nil, err } second, err := p.table() if err != nil { return nil, err } was := Index(first) for _, m := range markers { for _, hit := range ParseMarked(string(raw), m, os.Getpid()) { for i := range second { if second[i].PID == hit.PID && was[hit.PID].ID == second[i].ID { second[i].Marker = m } } } } return second, nil } func (p sysctlProcs) Lookup(pid int) (Info, bool) { if pid <= 0 { return Info{}, false } k, err := unix.SysctlKinfoProc("kern.proc.pid", pid) if err != nil { return Info{}, false } q, ok := fromKinfo(k) if !ok || q.PID != pid || q.UID != p.uid { return Info{}, false } return q, true } // Path is proc_pidpath: __proc_info(PROC_INFO_CALL_PIDINFO, // pid, PROC_PIDPATHINFO, 0, buf, PROC_PIDPATHINFO_MAXSIZE), the path of // the vnode the kernel runs, which the process cannot set. func (sysctlProcs) Path(pid int) string { const callPIDInfo, pidPathInfo, maxSize = 2, 11, 4096 buf := make([]byte, maxSize) n, _, errno := unix.Syscall6(unix.SYS_PROC_INFO, callPIDInfo, uintptr(pid), pidPathInfo, 0, uintptr(unsafe.Pointer(&buf[0])), maxSize) if errno != 0 || n == 0 || n > maxSize { return "" } for i := 0; i < int(n); i++ { if buf[i] == 0 { return string(buf[:i]) } } return string(buf[:n]) } // SessionGone reads every user's processes (kern.proc.all) and asks // each for its session: gone only when none is in sid and every answer // was read. func (sysctlProcs) SessionGone(sid int) bool { kps, err := unix.SysctlKinfoProcSlice("kern.proc.all") if err != nil { return false } for i := range kps { pid := int(kps[i].Proc.P_pid) if pid <= 0 { continue } got, err := unix.Getsid(pid) if errors.Is(err, unix.ESRCH) { continue } if err != nil || got == sid { return false } } return true } func (sysctlProcs) Sweeps() bool { return true } // Now is the wall clock in microseconds, as kern.proc's p_starttime. func (sysctlProcs) Now() int64 { return time.Now().UnixMicro() } func (sysctlProcs) Signal(pid int, sig syscall.Signal) error { return syscall.Kill(pid, sig) }