package proc import ( "errors" "os" "strconv" "strings" "time" ) // Proc is a process carrying an attempt's marker. type Proc struct{ PID, PGID int } // ParseMarked reads `ps -E -ww -x -o pid=,pgid=,command=` output: each // line is the pid, the group, then the command and, separated by spaces, // the environment. A process is marked when one of those words is // exactly EQUIPO_ATTEMPT=. self is never listed. func ParseMarked(out, attempt string, self int) []Proc { want := "EQUIPO_ATTEMPT=" + attempt var procs []Proc for _, line := range strings.Split(out, "\n") { f := strings.Fields(line) if len(f) < 3 { continue } pid, err1 := strconv.Atoi(f[0]) pgid, err2 := strconv.Atoi(f[1]) if err1 != nil || err2 != nil || pid == self { continue } for _, w := range f[2:] { if w == want { procs = append(procs, Proc{PID: pid, PGID: pgid}) break } } } return procs } // Marked lists this user's processes whose environment carries // EQUIPO_ATTEMPT= where the kernel shows it (/proc on Linux; // ps -E on a Mac, which cannot see platform binaries'). Never this // process. func Marked(attempt string) ([]Proc, error) { if attempt == "" { return nil, errors.New("mac: no attempt marker") } list, _, err := Runner.Snapshot(time.Now().Add(30*time.Second), Platform, []string{attempt}) if err != nil { return nil, err } var out []Proc for _, q := range list { if q.Marker == attempt && q.PID != os.Getpid() { out = append(out, Proc{PID: q.PID, PGID: q.PGID}) } } return out, nil }