oficina / equipo Public

Clone
git clone https://amadan.net/oficina/equipo

Plain git — no account needed to clone.

Download

Download this file

1package proc
2
3import (
4 "errors"
5 "os"
6 "strconv"
7 "strings"
8 "time"
9)
10
11// Proc is a process carrying an attempt's marker.
12type Proc struct{ PID, PGID int }
13
14// ParseMarked reads `ps -E -ww -x -o pid=,pgid=,command=` output: each
15// line is the pid, the group, then the command and, separated by spaces,
16// the environment. A process is marked when one of those words is
17// exactly EQUIPO_ATTEMPT=<attempt>. self is never listed.
18func ParseMarked(out, attempt string, self int) []Proc {
19 want := "EQUIPO_ATTEMPT=" + attempt
20 var procs []Proc
21 for _, line := range strings.Split(out, "\n") {
22 f := strings.Fields(line)
23 if len(f) < 3 {
24 continue
25 }
26 pid, err1 := strconv.Atoi(f[0])
27 pgid, err2 := strconv.Atoi(f[1])
28 if err1 != nil || err2 != nil || pid == self {
29 continue
30 }
31 for _, w := range f[2:] {
32 if w == want {
33 procs = append(procs, Proc{PID: pid, PGID: pgid})
34 break
35 }
36 }
37 }
38 return procs
39}
40
41// Marked lists this user's processes whose environment carries
42// EQUIPO_ATTEMPT=<attempt> where the kernel shows it (/proc on Linux;
43// ps -E on a Mac, which cannot see platform binaries'). Never this
44// process.
45func Marked(attempt string) ([]Proc, error) {
46 if attempt == "" {
47 return nil, errors.New("mac: no attempt marker")
48 }
49 list, _, err := Runner.Snapshot(time.Now().Add(30*time.Second), Platform, []string{attempt})
50 if err != nil {
51 return nil, err
52 }
53 var out []Proc
54 for _, q := range list {
55 if q.Marker == attempt && q.PID != os.Getpid() {
56 out = append(out, Proc{PID: q.PID, PGID: q.PGID})
57 }
58 }
59 return out, nil
60}
61