package proc import ( "errors" "os" "slices" "syscall" "time" ) // Rebooted reports whether now is a later boot than the one a turn began // in (boot id then, at began): another boot id, and a boot time after // the turn's start. A process does not outlive a boot, so nothing of // that turn can be running. Anything unknown or unreadable: no. func Rebooted(then string, began time.Time, now Boot, err error) bool { return err == nil && then != "" && now.ID != "" && now.ID != then && !began.IsZero() && now.Time.After(began) } // IdleClear is the clearance of an incomplete turn while no turn runs // (the caller holds the Mac's turn): two whole reads of the runner // user's processes, each in time, must find only the runner, its own // helpers and processes in baseline (the table of the earliest // unresolved turn, by identity), and then everything o names must be // gone for every user. left is how many // processes the reads found that nothing accounts for (-1: a read // failed). func IdleClear(t Table, own *Own, o Obligation, baseline []ID, deadline time.Time) (left int, ok bool) { if !t.Sweeps() { // The runner user is not the runner's alone (Linux outside a // test's scope): its other processes prove nothing. return -1, false } before := map[ID]bool{} for _, id := range baseline { before[id] = true } uid, self := os.Getuid(), os.Getpid() // Two reads: an identity the first accepted that is another process // by the second (a pid used again) is unaccounted then. for range 2 { list, mine, err := own.Snapshot(deadline, t, nil) if err != nil || !time.Now().Before(deadline) { return -1, false } left = 0 for _, q := range list { if q.UID == uid && q.PID != self && !mine[q.ID] && !before[q.ID] { left++ } } if left > 0 { return left, false } } return 0, o.gone(t) && time.Now().Before(deadline) } // Assess judges, holding the Mac's turn and before deadline, whether // every obligation naming attempt is met: by a reboot since its turn // began (rebooted: a boot-qualified clearance, the old boot's process // checks no longer apply), or, for an incomplete turn whose record shows // nothing ran outside a sandbox, by an idle read, or by what it names // being gone. The idle read exempts only what existed before the // earliest unresolved turn began. Nothing is removed (Discharge does // that once the whole proof holds); an idle read's count is kept. func (d *Debts) Assess(attempt string, t Table, own *Own, boot Boot, bootErr error, deadline time.Time) (rebooted, met bool) { met = true err := d.lockedBy(deadline, func() error { all, err := d.load() if err != nil { return err } resolved := func(o Obligation) bool { return o.ProvenIn != "" || Rebooted(o.BootID, o.Began, boot, bootErr) } // The earliest unresolved turn's table, by start: one with no // start counts as earliest; one a reboot resolved does not count. var earliest *Obligation for i := range all { o := &all[i] if resolved(*o) { continue } if earliest == nil || o.Began.IsZero() || !earliest.Began.IsZero() && o.Began.Before(earliest.Began) { earliest = o if o.Began.IsZero() { break } } } var baseline []ID if earliest != nil { baseline = earliest.Before } changed := false for i, o := range all { if !slices.Contains(o.Markers, attempt) { continue } switch { case resolved(o): rebooted = true case !o.Incomplete: met = met && o.Met(t) case !o.FlagsKnown || o.UnsandboxedRan || !othersClear(all, i, t, resolved): // Something may have left the runner user: a reboot only. met = false default: left, ok := IdleClear(t, own, o, baseline, deadline) met = met && ok if n := max(left, 0); !ok && n != o.Left { all[i].Left, changed = n, true } } } if changed { _ = d.save(all) } return nil }) return rebooted, err == nil && met && time.Now().Before(deadline) } // othersClear: every other unresolved turn shows nothing ran outside a // sandbox, and has everything it names gone for every user. Else one of // its processes may have left the runner user and could serve this // turn's work unseen. func othersClear(all []Obligation, me int, t Table, resolved func(Obligation) bool) bool { for i, o := range all { if i == me || resolved(o) { continue } if !o.FlagsKnown || o.UnsandboxedRan || !o.gone(t) { return false } } return true } // Discharge removes every obligation naming attempt (its end proven), // before deadline (zero: none), or not at all. A save that ends late is // undone: the debt stays for a later proof. func (d *Debts) Discharge(attempt string, deadline time.Time) error { return d.lockedBy(deadline, func() error { all, err := d.load() if err != nil { return err } if d.between != nil { d.between() } keep := slices.DeleteFunc(slices.Clone(all), func(o Obligation) bool { return slices.Contains(o.Markers, attempt) }) if late(deadline) { return errLate } if err := d.save(keep); err != nil { return err } if late(deadline) { return errors.Join(errLate, d.save(all)) } return nil }) } // late: deadline (zero: none) has passed. func late(deadline time.Time) bool { return !deadline.IsZero() && !time.Now().Before(deadline) } // completionLife bounds how long a reboot's completion waits for its // consumers (a record that only one of them ever acknowledges). const completionLife = 30 * 24 * time.Hour // Complete records, before deadline, that attempt was proven over by a // reboot into boot (a boot-qualified completion). attempt leaves every // obligation that names it (one that names only it goes); another // marker's evidence stays. Every later proof of the record, in any // runner process, is that completion: no process check, no signal. It // goes once every consumer has acknowledged it (Acknowledge), or after // completionLife. Saved late, it fails but stays: a later proof // finishes from it. func (d *Debts) Complete(attempt, boot string, deadline time.Time) error { return d.lockedBy(deadline, func() error { all, err := d.load() if err != nil { return err } done := Obligation{Markers: []string{attempt}, ProvenIn: boot, ProvenAt: time.Now()} var keep []Obligation for _, o := range all { switch { case o.ProvenIn != "" && time.Since(o.ProvenAt) > completionLife: case !slices.Contains(o.Markers, attempt): keep = append(keep, o) case o.ProvenIn != "": done = o case len(o.Markers) > 1: o.Markers = slices.DeleteFunc(slices.Clone(o.Markers), func(m string) bool { return m == attempt }) keep = append(keep, o) } } if late(deadline) { return errLate } if err := d.save(append(keep, done)); err != nil { return err } if late(deadline) { return errLate } return nil }) } // Acknowledge records that consumer has durably kept what a proof of // attempt found (its own recovery committed): the completion goes once // every one of consumers has. Nothing else is touched. func (d *Debts) Acknowledge(attempt, consumer string, consumers []string) error { return d.locked(func() error { all, err := d.load() if err != nil { return err } i := slices.IndexFunc(all, func(o Obligation) bool { return o.ProvenIn != "" && slices.Contains(o.Markers, attempt) }) if i < 0 { return nil } if !slices.Contains(all[i].Recorded, consumer) { all[i].Recorded = append(all[i].Recorded, consumer) } if !slices.ContainsFunc(consumers, func(c string) bool { return !slices.Contains(all[i].Recorded, c) }) { all = slices.Delete(all, i, i+1) } return d.save(all) }) } // Owes reports whether attempt owes anything (an unreadable file: yes). func (d *Debts) Owes(attempt string) bool { owes := true _ = d.locked(func() error { all, err := d.load() owes = err != nil || slices.ContainsFunc(all, func(o Obligation) bool { return slices.Contains(o.Markers, attempt) }) return nil }) return owes } // Unproven reports whether an obligation can never be met as it stands // (an incomplete turn; an unreadable file says yes), and the largest // count of unaccounted processes an idle read found for one. func (d *Debts) Unproven() (incomplete bool, left int) { incomplete = true _ = d.locked(func() error { all, err := d.load() if err != nil { return nil } incomplete = false for _, o := range all { if o.Incomplete { incomplete, left = true, max(left, o.Left) } } return nil }) return incomplete, left } // SettleMarked kills what carries the marker (and the recorded group // while a marked process, read just now, is still in it) and reports // whether, before one deadline (Settle), the group is gone and nothing // marked is left. pgid 0: the marker alone settles it. func SettleMarked(procs Table, own *Own, pgid int, attempt string, deadline time.Time) bool { me := os.Getpid() s := NewScope(procs, nil, nil) for { list, mine, err := own.Snapshot(deadline, procs, []string{attempt}) if err != nil || !time.Now().Before(deadline) { return false } var marked []Info for _, q := range list { if q.Marker == attempt && q.PID != me && q.PID > 1 && !mine[q.ID] { marked = append(marked, q) } } for _, q := range marked { if pgid > 1 && s.InGroup(q, pgid) { _ = procs.Signal(-pgid, syscall.SIGKILL) break } } for _, q := range marked { if s.Still(q) { _ = procs.Signal(q.PID, syscall.SIGKILL) } } gone := pgid == 0 || errors.Is(procs.Signal(-pgid, 0), syscall.ESRCH) if gone && len(marked) == 0 && time.Now().Before(deadline) { return true } if !time.Now().Before(deadline) { return false } time.Sleep(min(100*time.Millisecond, time.Until(deadline))) } }