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 "slices"
7 "syscall"
8 "time"
9)
10
11// Rebooted reports whether now is a later boot than the one a turn began
12// in (boot id then, at began): another boot id, and a boot time after
13// the turn's start. A process does not outlive a boot, so nothing of
14// that turn can be running. Anything unknown or unreadable: no.
15func Rebooted(then string, began time.Time, now Boot, err error) bool {
16 return err == nil && then != "" && now.ID != "" && now.ID != then && !began.IsZero() && now.Time.After(began)
17}
18
19// IdleClear is the clearance of an incomplete turn while no turn runs
20// (the caller holds the Mac's turn): two whole reads of the runner
21// user's processes, each in time, must find only the runner, its own
22// helpers and processes in baseline (the table of the earliest
23// unresolved turn, by identity), and then everything o names must be
24// gone for every user. left is how many
25// processes the reads found that nothing accounts for (-1: a read
26// failed).
27func IdleClear(t Table, own *Own, o Obligation, baseline []ID, deadline time.Time) (left int, ok bool) {
28 if !t.Sweeps() {
29 // The runner user is not the runner's alone (Linux outside a
30 // test's scope): its other processes prove nothing.
31 return -1, false
32 }
33 before := map[ID]bool{}
34 for _, id := range baseline {
35 before[id] = true
36 }
37 uid, self := os.Getuid(), os.Getpid()
38 // Two reads: an identity the first accepted that is another process
39 // by the second (a pid used again) is unaccounted then.
40 for range 2 {
41 list, mine, err := own.Snapshot(deadline, t, nil)
42 if err != nil || !time.Now().Before(deadline) {
43 return -1, false
44 }
45 left = 0
46 for _, q := range list {
47 if q.UID == uid && q.PID != self && !mine[q.ID] && !before[q.ID] {
48 left++
49 }
50 }
51 if left > 0 {
52 return left, false
53 }
54 }
55 return 0, o.gone(t) && time.Now().Before(deadline)
56}
57
58// Assess judges, holding the Mac's turn and before deadline, whether
59// every obligation naming attempt is met: by a reboot since its turn
60// began (rebooted: a boot-qualified clearance, the old boot's process
61// checks no longer apply), or, for an incomplete turn whose record shows
62// nothing ran outside a sandbox, by an idle read, or by what it names
63// being gone. The idle read exempts only what existed before the
64// earliest unresolved turn began. Nothing is removed (Discharge does
65// that once the whole proof holds); an idle read's count is kept.
66func (d *Debts) Assess(attempt string, t Table, own *Own, boot Boot, bootErr error, deadline time.Time) (rebooted, met bool) {
67 met = true
68 err := d.lockedBy(deadline, func() error {
69 all, err := d.load()
70 if err != nil {
71 return err
72 }
73 resolved := func(o Obligation) bool { return o.ProvenIn != "" || Rebooted(o.BootID, o.Began, boot, bootErr) }
74 // The earliest unresolved turn's table, by start: one with no
75 // start counts as earliest; one a reboot resolved does not count.
76 var earliest *Obligation
77 for i := range all {
78 o := &all[i]
79 if resolved(*o) {
80 continue
81 }
82 if earliest == nil || o.Began.IsZero() || !earliest.Began.IsZero() && o.Began.Before(earliest.Began) {
83 earliest = o
84 if o.Began.IsZero() {
85 break
86 }
87 }
88 }
89 var baseline []ID
90 if earliest != nil {
91 baseline = earliest.Before
92 }
93 changed := false
94 for i, o := range all {
95 if !slices.Contains(o.Markers, attempt) {
96 continue
97 }
98 switch {
99 case resolved(o):
100 rebooted = true
101 case !o.Incomplete:
102 met = met && o.Met(t)
103 case !o.FlagsKnown || o.UnsandboxedRan || !othersClear(all, i, t, resolved):
104 // Something may have left the runner user: a reboot only.
105 met = false
106 default:
107 left, ok := IdleClear(t, own, o, baseline, deadline)
108 met = met && ok
109 if n := max(left, 0); !ok && n != o.Left {
110 all[i].Left, changed = n, true
111 }
112 }
113 }
114 if changed {
115 _ = d.save(all)
116 }
117 return nil
118 })
119 return rebooted, err == nil && met && time.Now().Before(deadline)
120}
121
122// othersClear: every other unresolved turn shows nothing ran outside a
123// sandbox, and has everything it names gone for every user. Else one of
124// its processes may have left the runner user and could serve this
125// turn's work unseen.
126func othersClear(all []Obligation, me int, t Table, resolved func(Obligation) bool) bool {
127 for i, o := range all {
128 if i == me || resolved(o) {
129 continue
130 }
131 if !o.FlagsKnown || o.UnsandboxedRan || !o.gone(t) {
132 return false
133 }
134 }
135 return true
136}
137
138// Discharge removes every obligation naming attempt (its end proven),
139// before deadline (zero: none), or not at all. A save that ends late is
140// undone: the debt stays for a later proof.
141func (d *Debts) Discharge(attempt string, deadline time.Time) error {
142 return d.lockedBy(deadline, func() error {
143 all, err := d.load()
144 if err != nil {
145 return err
146 }
147 if d.between != nil {
148 d.between()
149 }
150 keep := slices.DeleteFunc(slices.Clone(all), func(o Obligation) bool { return slices.Contains(o.Markers, attempt) })
151 if late(deadline) {
152 return errLate
153 }
154 if err := d.save(keep); err != nil {
155 return err
156 }
157 if late(deadline) {
158 return errors.Join(errLate, d.save(all))
159 }
160 return nil
161 })
162}
163
164// late: deadline (zero: none) has passed.
165func late(deadline time.Time) bool { return !deadline.IsZero() && !time.Now().Before(deadline) }
166
167// completionLife bounds how long a reboot's completion waits for its
168// consumers (a record that only one of them ever acknowledges).
169const completionLife = 30 * 24 * time.Hour
170
171// Complete records, before deadline, that attempt was proven over by a
172// reboot into boot (a boot-qualified completion). attempt leaves every
173// obligation that names it (one that names only it goes); another
174// marker's evidence stays. Every later proof of the record, in any
175// runner process, is that completion: no process check, no signal. It
176// goes once every consumer has acknowledged it (Acknowledge), or after
177// completionLife. Saved late, it fails but stays: a later proof
178// finishes from it.
179func (d *Debts) Complete(attempt, boot string, deadline time.Time) error {
180 return d.lockedBy(deadline, func() error {
181 all, err := d.load()
182 if err != nil {
183 return err
184 }
185 done := Obligation{Markers: []string{attempt}, ProvenIn: boot, ProvenAt: time.Now()}
186 var keep []Obligation
187 for _, o := range all {
188 switch {
189 case o.ProvenIn != "" && time.Since(o.ProvenAt) > completionLife:
190 case !slices.Contains(o.Markers, attempt):
191 keep = append(keep, o)
192 case o.ProvenIn != "":
193 done = o
194 case len(o.Markers) > 1:
195 o.Markers = slices.DeleteFunc(slices.Clone(o.Markers), func(m string) bool { return m == attempt })
196 keep = append(keep, o)
197 }
198 }
199 if late(deadline) {
200 return errLate
201 }
202 if err := d.save(append(keep, done)); err != nil {
203 return err
204 }
205 if late(deadline) {
206 return errLate
207 }
208 return nil
209 })
210}
211
212// Acknowledge records that consumer has durably kept what a proof of
213// attempt found (its own recovery committed): the completion goes once
214// every one of consumers has. Nothing else is touched.
215func (d *Debts) Acknowledge(attempt, consumer string, consumers []string) error {
216 return d.locked(func() error {
217 all, err := d.load()
218 if err != nil {
219 return err
220 }
221 i := slices.IndexFunc(all, func(o Obligation) bool { return o.ProvenIn != "" && slices.Contains(o.Markers, attempt) })
222 if i < 0 {
223 return nil
224 }
225 if !slices.Contains(all[i].Recorded, consumer) {
226 all[i].Recorded = append(all[i].Recorded, consumer)
227 }
228 if !slices.ContainsFunc(consumers, func(c string) bool { return !slices.Contains(all[i].Recorded, c) }) {
229 all = slices.Delete(all, i, i+1)
230 }
231 return d.save(all)
232 })
233}
234
235// Owes reports whether attempt owes anything (an unreadable file: yes).
236func (d *Debts) Owes(attempt string) bool {
237 owes := true
238 _ = d.locked(func() error {
239 all, err := d.load()
240 owes = err != nil || slices.ContainsFunc(all, func(o Obligation) bool { return slices.Contains(o.Markers, attempt) })
241 return nil
242 })
243 return owes
244}
245
246// Unproven reports whether an obligation can never be met as it stands
247// (an incomplete turn; an unreadable file says yes), and the largest
248// count of unaccounted processes an idle read found for one.
249func (d *Debts) Unproven() (incomplete bool, left int) {
250 incomplete = true
251 _ = d.locked(func() error {
252 all, err := d.load()
253 if err != nil {
254 return nil
255 }
256 incomplete = false
257 for _, o := range all {
258 if o.Incomplete {
259 incomplete, left = true, max(left, o.Left)
260 }
261 }
262 return nil
263 })
264 return incomplete, left
265}
266
267// SettleMarked kills what carries the marker (and the recorded group
268// while a marked process, read just now, is still in it) and reports
269// whether, before one deadline (Settle), the group is gone and nothing
270// marked is left. pgid 0: the marker alone settles it.
271func SettleMarked(procs Table, own *Own, pgid int, attempt string, deadline time.Time) bool {
272 me := os.Getpid()
273 s := NewScope(procs, nil, nil)
274 for {
275 list, mine, err := own.Snapshot(deadline, procs, []string{attempt})
276 if err != nil || !time.Now().Before(deadline) {
277 return false
278 }
279 var marked []Info
280 for _, q := range list {
281 if q.Marker == attempt && q.PID != me && q.PID > 1 && !mine[q.ID] {
282 marked = append(marked, q)
283 }
284 }
285 for _, q := range marked {
286 if pgid > 1 && s.InGroup(q, pgid) {
287 _ = procs.Signal(-pgid, syscall.SIGKILL)
288 break
289 }
290 }
291 for _, q := range marked {
292 if s.Still(q) {
293 _ = procs.Signal(q.PID, syscall.SIGKILL)
294 }
295 }
296 gone := pgid == 0 || errors.Is(procs.Signal(-pgid, 0), syscall.ESRCH)
297 if gone && len(marked) == 0 && time.Now().Before(deadline) {
298 return true
299 }
300 if !time.Now().Before(deadline) {
301 return false
302 }
303 time.Sleep(min(100*time.Millisecond, time.Until(deadline)))
304 }
305}
306