package proc import ( "bytes" "os" "path/filepath" "strconv" "strings" "syscall" "time" ) // procfs is the process table on Linux, where a Mac's runner is only // ever tested (the test build's fake worker, and this package's tests): // /proc//stat for parent, group, session and start, the directory's // owner for the user, environ for the marker, exe for the path. // // Linux machines are shared, so it never drives a runner-user sweep // unless scoped: then it lists only processes whose environment carries // EQUIPO_TEST_SCOPE=, a test's own. type procfs struct { uid int // scope is the environment entries (any one) that tag a test's own // processes, NUL-joined so the table stays comparable; "" lists all. scope string } var Platform Table = procfs{uid: os.Getuid()} // Machine is the table a Mac's turns sweep with; nil off a Mac. func Machine() Table { return nil } // ScopeEnv tags a test's own processes for a scoped table. const ScopeEnv = "EQUIPO_TEST_SCOPE" func (p procfs) read(pid int, markers []string) (Info, bool) { dir := filepath.Join("/proc", strconv.Itoa(pid)) fi, err := os.Stat(dir) if err != nil { return Info{}, false } if st, ok := fi.Sys().(*syscall.Stat_t); !ok || int(st.Uid) != p.uid { return Info{}, false } stat, err := os.ReadFile(filepath.Join(dir, "stat")) if err != nil { return Info{}, false } end := bytes.LastIndexByte(stat, ')') if end < 0 { return Info{}, false } // pid (comm) state ppid pgrp session tty tpgid flags minflt cminflt // majflt cmajflt utime stime cutime cstime priority nice threads // itrealvalue starttime ... f := strings.Fields(string(stat[end+1:])) if len(f) < 20 { return Info{}, false } num := func(s string) int { n, _ := strconv.Atoi(s); return n } start, _ := strconv.ParseInt(f[19], 10, 64) q := Info{ID: ID{PID: pid, Start: start}, PPID: num(f[1]), PGID: num(f[2]), SID: num(f[3]), UID: p.uid} if p.scope == "" && len(markers) == 0 { return q, true } env, err := os.ReadFile(filepath.Join(dir, "environ")) if err != nil { // Unreadable (gone, or not dumpable): unmarked, and outside any // scope. return q, p.scope == "" } vars := bytes.Split(env, []byte{0}) has := func(kv string) bool { for _, v := range vars { if string(v) == kv { return true } } return false } if p.scope != "" { in := false for _, kv := range strings.Split(p.scope, "\x00") { in = in || has(kv) } if !in { return Info{}, false } } for _, m := range markers { if has("EQUIPO_ATTEMPT=" + m) { q.Marker = m break } } return q, true } func (p procfs) List(_ time.Time, markers []string) ([]Info, error) { entries, err := os.ReadDir("/proc") if err != nil { return nil, err } var out []Info for _, e := range entries { pid, err := strconv.Atoi(e.Name()) if err != nil { continue } if q, ok := p.read(pid, markers); ok { out = append(out, q) } } return out, nil } func (p procfs) Lookup(pid int) (Info, bool) { if pid <= 0 { return Info{}, false } return p.read(pid, nil) } func (p procfs) Path(pid int) string { if _, ok := p.Lookup(pid); !ok { return "" } path, err := os.Readlink(filepath.Join("/proc", strconv.Itoa(pid), "exe")) if err != nil { return "" } return path } // SessionGone reads every process's session from /proc (any user's). func (procfs) SessionGone(sid int) bool { entries, err := os.ReadDir("/proc") if err != nil { return false } for _, e := range entries { if _, err := strconv.Atoi(e.Name()); err != nil { continue } stat, err := os.ReadFile(filepath.Join("/proc", e.Name(), "stat")) if err != nil { continue } end := bytes.LastIndexByte(stat, ')') if f := strings.Fields(string(stat[end+1:])); end >= 0 && len(f) > 3 && f[3] == strconv.Itoa(sid) { return false } } return true } func (p procfs) Sweeps() bool { return p.scope != "" } // Now is clock ticks since boot (USER_HZ, 100), as /proc's starttime. func (procfs) Now() int64 { raw, err := os.ReadFile("/proc/uptime") if err != nil { return 0 } f := strings.Fields(string(raw)) if len(f) == 0 { return 0 } up, _ := strconv.ParseFloat(f[0], 64) return int64(up * 100) } // Signal refuses to signal outside the scope: a scoped table's signals // reach only the test's own processes (a group only while a member of // it is the test's). func (p procfs) Signal(pid int, sig syscall.Signal) error { if p.scope != "" && sig != 0 { target := pid if pid < 0 { target = -pid if _, ok := p.Lookup(target); !ok { list, err := p.List(time.Time{}, nil) if err != nil { return err } found := false for _, q := range list { found = found || q.PGID == target } if !found { return syscall.ESRCH } } } else if _, ok := p.Lookup(target); !ok { return syscall.ESRCH } } return syscall.Kill(pid, sig) } // Scoped is a table of only the processes whose environment holds // one of entries (tests: EQUIPO_TEST_SCOPE=, or a test's own // EQUIPO_ATTEMPT=). func Scoped(entries ...string) Table { return procfs{uid: os.Getuid(), scope: strings.Join(entries, "\x00")} }