| 1 | package mac |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "crypto/rand" |
| 6 | "encoding/hex" |
| 7 | "errors" |
| 8 | "os" |
| 9 | "slices" |
| 10 | "sync" |
| 11 | "time" |
| 12 | |
| 13 | "amadan.net/oficina/equipo/internal/runner/mac/creds" |
| 14 | "amadan.net/oficina/equipo/internal/runner/mac/proc" |
| 15 | "amadan.net/oficina/equipo/internal/wire" |
| 16 | ) |
| 17 | |
| 18 | // Op is what a turn is for. Every operation that runs repository code, |
| 19 | // runs Xcode or has a model token in a process takes the Mac's one turn |
| 20 | // for its own life and no longer (spec §8.3 item 4); what it starts |
| 21 | // itself (Xcode under Engineering, a simulator under a check) is its. |
| 22 | type Op string |
| 23 | |
| 24 | const ( |
| 25 | OpCheck Op = "check" |
| 26 | OpEngineering Op = "engineering" |
| 27 | OpAsk Op = "ask" |
| 28 | OpProbe Op = "probe" |
| 29 | OpTokenCheck Op = "token_check" |
| 30 | OpSignIn Op = "sign_in" |
| 31 | OpTokenStore Op = "token_store" |
| 32 | OpWarm Op = "warm" |
| 33 | // OpRecover settles what an earlier operation left (Verify). |
| 34 | OpRecover Op = "recover" |
| 35 | ) |
| 36 | |
| 37 | // ErrNoTurn refuses a start outside a turn of the Mac's Turns. |
| 38 | var ErrNoTurn = errors.New("mac: no turn held") |
| 39 | |
| 40 | // ErrUnsettled refuses a turn while an earlier one's processes may |
| 41 | // still be running (its end not proven), or while another live runner |
| 42 | // process left a turn record. |
| 43 | var ErrUnsettled = errors.New("mac: an earlier turn is not settled") |
| 44 | |
| 45 | // ErrPendingCleanup refuses a sandboxed run while another attempt's |
| 46 | // user-temp cleanup is unfinished and not this turn's to finish. |
| 47 | var ErrPendingCleanup = errors.New("mac: another attempt's cleanup is pending") |
| 48 | |
| 49 | // Turns is the Mac's one machine lock, across the runner's processes (a |
| 50 | // channel within a process, an flock on a file in the state dir, which |
| 51 | // no profile lets a sandboxed command reach, between them). Beside the |
| 52 | // lock it keeps two durable records: |
| 53 | // |
| 54 | // - lock+".turn": the turn now (or the last one whose end was not |
| 55 | // proven): its operation, the runner process that took it, the |
| 56 | // runner user's processes when it began (the sweep's "before"), and |
| 57 | // the markers, groups and sessions of what it started. A later turn |
| 58 | // settles it before it begins; a restart does the same. |
| 59 | // - lock+".pending": a sandboxed attempt's user-temp cleanup not yet |
| 60 | // done, owned by the turn that wrote it: only that attempt (its run, |
| 61 | // or its proof later in the same runner) or a restart may finish it. |
| 62 | type Turns struct { |
| 63 | lock string |
| 64 | lk *proc.Lock |
| 65 | // sweep is the table every turn sweeps the runner user with at its |
| 66 | // end (MachineProcs on a Mac); nil: no runner-user sweep. |
| 67 | sweep Procs |
| 68 | // own is the runner's own processes (runnerOwn; tests stand in). |
| 69 | own *Own |
| 70 | // ids reads runner processes' identities (the platform's table; |
| 71 | // tests stand in). |
| 72 | ids Procs |
| 73 | // beforeWrite runs before each turn record write (tests: a slow or |
| 74 | // failing disk). |
| 75 | beforeWrite func() error |
| 76 | beforeDrop func() // before the pending record's removal (tests) |
| 77 | // boot reads this boot (creds.BootID, creds.BootTime; tests stand in). |
| 78 | boot func() (proc.Boot, error) |
| 79 | // Settle bounds settling an earlier turn (30 s). |
| 80 | Settle time.Duration |
| 81 | // mem is the turn record without a lock file (one process). |
| 82 | // records keeps the turn record (lock+".turn"), pends the pending |
| 83 | // cleanup (lock+".pending"). |
| 84 | records, pends *proc.Store |
| 85 | // debts is what unproven turns still owe (lock+".owed"). |
| 86 | debts proc.Debts |
| 87 | } |
| 88 | |
| 89 | // NewTurns makes the runner's one turn; lock is its file in the state |
| 90 | // dir ("" keeps the turn, and its record, within this process). |
| 91 | func NewTurns(lock string) *Turns { |
| 92 | t := &Turns{lock: lock, lk: proc.NewLock(lock), records: &proc.Store{}, pends: &proc.Store{}} |
| 93 | if lock != "" { |
| 94 | t.debts.Path, t.records.Path, t.pends.Path = lock+".owed", lock+".turn", lock+".pending" |
| 95 | } |
| 96 | return t |
| 97 | } |
| 98 | |
| 99 | // Sweep makes every turn sweep the runner user with p at its end: a |
| 100 | // Mac's turns (MachineProcs). A table that may not sweep (Linux outside |
| 101 | // a test's scope) makes every turn fail to begin; nil sweeps nothing. |
| 102 | func (t *Turns) Sweep(p Procs) *Turns { |
| 103 | t.sweep = p |
| 104 | return t |
| 105 | } |
| 106 | |
| 107 | func (t *Turns) owned() *Own { |
| 108 | if t != nil && t.own != nil { |
| 109 | return t.own |
| 110 | } |
| 111 | return runnerOwn |
| 112 | } |
| 113 | |
| 114 | // table is the process table the turns' records are read with. |
| 115 | func (t *Turns) table() Procs { |
| 116 | if t != nil && t.sweep != nil { |
| 117 | return t.sweep |
| 118 | } |
| 119 | return platformProcs |
| 120 | } |
| 121 | |
| 122 | // Turn is one operation's hold on the Mac. |
| 123 | type Turn struct { |
| 124 | t *Turns |
| 125 | ID string |
| 126 | Op Op |
| 127 | |
| 128 | mu sync.Mutex |
| 129 | rec turnRecord |
| 130 | adopted []string |
| 131 | unproven bool |
| 132 | // finished is each launch's stop final note written; lost is set |
| 133 | // once any evidence write of the turn failed (no launch after it). |
| 134 | finished map[string]bool |
| 135 | lost bool |
| 136 | gave sync.Once |
| 137 | given bool |
| 138 | } |
| 139 | |
| 140 | // turnRecord is a turn on disk (lock+".turn"). |
| 141 | type turnRecord struct { |
| 142 | ID string `json:"id"` |
| 143 | Op Op `json:"op"` |
| 144 | Runner ProcID `json:"runner"` |
| 145 | // Sweep: Before is the runner user's table when the turn began. |
| 146 | Sweep bool `json:"sweep"` |
| 147 | Before []ProcID `json:"before,omitempty"` |
| 148 | Markers []string `json:"markers,omitempty"` |
| 149 | Groups span `json:"groups,omitempty"` |
| 150 | Sessions span `json:"sessions,omitempty"` |
| 151 | // Killed is what the turn's stops signalled: each must be gone. |
| 152 | Killed []ProcID `json:"killed,omitempty"` |
| 153 | // Complete: the stop's final note was written, after every other. |
| 154 | Complete bool `json:"complete,omitempty"` |
| 155 | // Began and BootID: when, and in which boot, the turn began. |
| 156 | Began time.Time `json:"began,omitzero"` |
| 157 | BootID string `json:"boot_id,omitempty"` |
| 158 | // FlagsKnown: written at the start, so UnsandboxedRan is meaningful; |
| 159 | // UnsandboxedRan: something ran outside a sandbox in the turn. |
| 160 | FlagsKnown bool `json:"flags_known,omitempty"` |
| 161 | UnsandboxedRan bool `json:"unsandboxed_ran,omitempty"` |
| 162 | } |
| 163 | |
| 164 | // Take waits for the turn and begins op: an earlier turn left unsettled |
| 165 | // is settled first (or the take fails: ErrUnsettled), the runner user's |
| 166 | // table is read for the sweep, and the record is written before Take |
| 167 | // returns. Give it back with Give. |
| 168 | func (t *Turns) Take(ctx context.Context, op Op) (*Turn, error) { |
| 169 | if err := t.lk.Acquire(ctx); err != nil { |
| 170 | return nil, err |
| 171 | } |
| 172 | u, err := t.begin(op, time.Now().Add(t.settleLimit())) |
| 173 | if err != nil { |
| 174 | t.lk.Release() |
| 175 | return nil, err |
| 176 | } |
| 177 | return u, nil |
| 178 | } |
| 179 | |
| 180 | func (t *Turns) settleLimit() time.Duration { |
| 181 | if t.Settle > 0 { |
| 182 | return t.Settle |
| 183 | } |
| 184 | return 30 * time.Second |
| 185 | } |
| 186 | |
| 187 | // TryTake takes the turn for op only if it is free now (in this process |
| 188 | // and the others) and begins; ok is false otherwise. |
| 189 | func (t *Turns) TryTake(op Op) (*Turn, bool) { |
| 190 | return t.tryTakeBy(op, time.Now().Add(t.settleLimit())) |
| 191 | } |
| 192 | |
| 193 | // tryTakeBy is TryTake settling an earlier turn by deadline. |
| 194 | func (t *Turns) tryTakeBy(op Op, deadline time.Time) (*Turn, bool) { |
| 195 | if !t.lk.TryAcquire() { |
| 196 | return nil, false |
| 197 | } |
| 198 | u, err := t.begin(op, deadline) |
| 199 | if err != nil { |
| 200 | t.lk.Release() |
| 201 | return nil, false |
| 202 | } |
| 203 | return u, true |
| 204 | } |
| 205 | |
| 206 | // Hold is op's turn in ctx: the one ctx already holds (what an |
| 207 | // operation starts itself is its own: the probe run's gates), else a |
| 208 | // new one, waited for, given back by give. On a nil Turns it holds |
| 209 | // nothing (the test build's fake worker). |
| 210 | func (t *Turns) Hold(ctx context.Context, op Op) (context.Context, func(), error) { |
| 211 | if t == nil { |
| 212 | return ctx, func() {}, nil |
| 213 | } |
| 214 | if HeldTurn(ctx, t) != nil { |
| 215 | return ctx, func() {}, nil |
| 216 | } |
| 217 | u, err := t.Take(ctx, op) |
| 218 | if err != nil { |
| 219 | return ctx, func() {}, err |
| 220 | } |
| 221 | return u.Context(ctx), u.Give, nil |
| 222 | } |
| 223 | |
| 224 | type turnKey struct{ t *Turns } |
| 225 | |
| 226 | // Context carries the turn to what the operation starts. |
| 227 | func (u *Turn) Context(ctx context.Context) context.Context { |
| 228 | return context.WithValue(ctx, turnKey{u.t}, u) |
| 229 | } |
| 230 | |
| 231 | // HeldTurn is the turn of t that ctx holds, nil when none (or given). |
| 232 | func HeldTurn(ctx context.Context, t *Turns) *Turn { |
| 233 | if t == nil || ctx == nil { |
| 234 | return nil |
| 235 | } |
| 236 | u, _ := ctx.Value(turnKey{t}).(*Turn) |
| 237 | if u == nil { |
| 238 | return nil |
| 239 | } |
| 240 | u.mu.Lock() |
| 241 | defer u.mu.Unlock() |
| 242 | if u.given { |
| 243 | return nil |
| 244 | } |
| 245 | return u |
| 246 | } |
| 247 | |
| 248 | // Give ends the turn. Its record goes only when every launch in it was |
| 249 | // proven ended; otherwise it stays, and the next turn (in any runner |
| 250 | // process) settles it before it begins. |
| 251 | func (u *Turn) Give() { |
| 252 | u.gave.Do(func() { |
| 253 | u.mu.Lock() |
| 254 | u.given = true |
| 255 | keep := u.unproven |
| 256 | u.mu.Unlock() |
| 257 | if !keep { |
| 258 | _ = u.t.dropTurn(u.ID) |
| 259 | } |
| 260 | u.t.lk.Release() |
| 261 | }) |
| 262 | } |
| 263 | |
| 264 | // Before is the runner user's table when the turn began (nil: no sweep). |
| 265 | func (u *Turn) before() []ProcID { |
| 266 | u.mu.Lock() |
| 267 | defer u.mu.Unlock() |
| 268 | if !u.rec.Sweep { |
| 269 | return nil |
| 270 | } |
| 271 | return slices.Clone(u.rec.Before) |
| 272 | } |
| 273 | |
| 274 | // launch puts an attempt's marker on the turn's record before anything |
| 275 | // of it starts. |
| 276 | func (u *Turn) launch(marker string) error { |
| 277 | u.mu.Lock() |
| 278 | defer u.mu.Unlock() |
| 279 | if u.lost { |
| 280 | // An earlier launch's evidence was not written: its record can |
| 281 | // never be completed, so nothing more runs in this turn. |
| 282 | return errors.New("mac: this turn lost a launch's evidence") |
| 283 | } |
| 284 | if !slices.Contains(u.rec.Markers, marker) { |
| 285 | u.rec.Markers = append(u.rec.Markers, marker) |
| 286 | } |
| 287 | if u.finished == nil { |
| 288 | u.finished = map[string]bool{} |
| 289 | } |
| 290 | u.finished[marker] = false |
| 291 | // Incomplete again until this launch's stop writes its final note. |
| 292 | u.rec.Complete = false |
| 293 | if err := u.t.saveTurn(u.rec); err != nil { |
| 294 | u.lost = true |
| 295 | return err |
| 296 | } |
| 297 | return nil |
| 298 | } |
| 299 | |
| 300 | // finish is launch marker's stop's final note: everything it made, |
| 301 | // written whatever grew. The record is complete only once every launch |
| 302 | // of the turn has finished its own writes and none was ever lost. |
| 303 | func (u *Turn) finish(marker string, groups, sessions span, killed []ProcID) error { |
| 304 | if err := u.note(groups, sessions, killed...); err != nil { |
| 305 | return err |
| 306 | } |
| 307 | u.mu.Lock() |
| 308 | defer u.mu.Unlock() |
| 309 | if u.lost { |
| 310 | return errors.New("mac: this turn lost a launch's evidence") |
| 311 | } |
| 312 | u.finished[marker] = true |
| 313 | rec := u.rec |
| 314 | rec.Complete = true |
| 315 | for _, done := range u.finished { |
| 316 | rec.Complete = rec.Complete && done |
| 317 | } |
| 318 | if err := u.t.saveTurn(rec); err != nil { |
| 319 | u.lost = true |
| 320 | return err |
| 321 | } |
| 322 | u.rec = rec |
| 323 | return nil |
| 324 | } |
| 325 | |
| 326 | // note adds groups and sessions an attempt made to the record (best |
| 327 | // effort: the record before the start has the marker and the table). |
| 328 | func (u *Turn) note(groups, sessions span, killed ...ProcID) error { |
| 329 | u.mu.Lock() |
| 330 | defer u.mu.Unlock() |
| 331 | // Merged into a copy and kept only once written: a failed write is |
| 332 | // tried again at the next note, and the caller stops the operation. |
| 333 | rec := u.rec |
| 334 | rec.Groups, rec.Sessions, rec.Killed = rec.Groups.Clone(), rec.Sessions.Clone(), slices.Clone(rec.Killed) |
| 335 | grew := false |
| 336 | for _, m := range []struct { |
| 337 | to *span |
| 338 | from span |
| 339 | }{{&rec.Groups, groups}, {&rec.Sessions, sessions}} { |
| 340 | if *m.to == nil { |
| 341 | *m.to = span{} |
| 342 | } |
| 343 | for id, at := range m.from { |
| 344 | if _, ok := (*m.to)[id]; !ok { |
| 345 | (*m.to)[id] = at |
| 346 | grew = true |
| 347 | } |
| 348 | } |
| 349 | } |
| 350 | for _, id := range killed { |
| 351 | if !slices.Contains(rec.Killed, id) { |
| 352 | rec.Killed = append(rec.Killed, id) |
| 353 | grew = true |
| 354 | } |
| 355 | } |
| 356 | if !grew { |
| 357 | return nil |
| 358 | } |
| 359 | if err := u.t.saveTurn(rec); err != nil { |
| 360 | u.lost = true |
| 361 | return err |
| 362 | } |
| 363 | u.rec = rec |
| 364 | return nil |
| 365 | } |
| 366 | |
| 367 | // unprovenEnd keeps the record past Give: a launch's end is not proven. |
| 368 | func (u *Turn) unprovenEnd() { |
| 369 | u.mu.Lock() |
| 370 | u.unproven = true |
| 371 | u.mu.Unlock() |
| 372 | } |
| 373 | |
| 374 | // owns: the attempt was launched in this turn, or the turn settles it |
| 375 | // (Verify's recovery turn). |
| 376 | func (u *Turn) owns(attempt string) bool { |
| 377 | if u == nil { |
| 378 | return false |
| 379 | } |
| 380 | u.mu.Lock() |
| 381 | defer u.mu.Unlock() |
| 382 | return slices.Contains(u.rec.Markers, attempt) || slices.Contains(u.adopted, attempt) |
| 383 | } |
| 384 | |
| 385 | func (u *Turn) adopt(attempt string) { |
| 386 | u.mu.Lock() |
| 387 | u.adopted = append(u.adopted, attempt) |
| 388 | u.mu.Unlock() |
| 389 | } |
| 390 | |
| 391 | func (t *Turns) begin(op Op, deadline time.Time) (*Turn, error) { |
| 392 | if err := t.settle(deadline); err != nil { |
| 393 | return nil, err |
| 394 | } |
| 395 | b, _ := t.bootNow() |
| 396 | var raw [8]byte |
| 397 | _, _ = rand.Read(raw[:]) |
| 398 | rec := turnRecord{ID: hex.EncodeToString(raw[:]), Op: op, Runner: proc.Self(), Began: time.Now(), BootID: b.ID, FlagsKnown: true} |
| 399 | if t.sweep != nil { |
| 400 | if !t.sweep.Sweeps() { |
| 401 | return nil, ErrNoSweep |
| 402 | } |
| 403 | list, _, err := t.owned().Snapshot(time.Now().Add(30*time.Second), t.sweep, nil) |
| 404 | if err != nil { |
| 405 | return nil, err |
| 406 | } |
| 407 | rec.Sweep = true |
| 408 | rec.Before = make([]ProcID, 0, len(list)) |
| 409 | for _, q := range list { |
| 410 | rec.Before = append(rec.Before, q.ID) |
| 411 | } |
| 412 | } |
| 413 | if err := t.saveTurn(rec); err != nil { |
| 414 | return nil, err |
| 415 | } |
| 416 | return &Turn{t: t, ID: rec.ID, Op: op, rec: rec}, nil |
| 417 | } |
| 418 | |
| 419 | // settle ends what an earlier turn left: every process of its scope |
| 420 | // (its seen groups and sessions, its markers and, if it swept, every |
| 421 | // process of the runner user that did not exist when it began, but for |
| 422 | // this runner's own and launchd's system agents) killed, checked again |
| 423 | // before each signal, until none is left within Settle. A record of |
| 424 | // another live runner process is never settled from here. |
| 425 | func (t *Turns) settle(deadline time.Time) error { |
| 426 | rec, ok, err := t.loadTurn() |
| 427 | if err != nil || !ok { |
| 428 | return err |
| 429 | } |
| 430 | if b, err := t.bootNow(); proc.Rebooted(rec.BootID, rec.Began, b, err) { |
| 431 | // Nothing of a turn from an earlier boot runs now: no sweep (its |
| 432 | // table is that boot's). Kept as a boot-qualified obligation, so |
| 433 | // its attempts' proof skips the old boot's process checks. |
| 434 | if err := t.debts.Owe(proc.Obligation{Markers: rec.Markers, Began: rec.Began, BootID: rec.BootID}); err != nil { |
| 435 | return errors.Join(ErrUnsettled, err) |
| 436 | } |
| 437 | return t.dropTurn(rec.ID) |
| 438 | } |
| 439 | if rec.Runner != proc.Self() { |
| 440 | // Another runner process's record: only once it is surely gone. |
| 441 | if alive, known := proc.Alive(t.identities(), rec.Runner); alive || !known { |
| 442 | return ErrUnsettled |
| 443 | } |
| 444 | } |
| 445 | if rec.Sweep && (t.sweep == nil || !t.sweep.Sweeps()) { |
| 446 | return ErrUnsettled |
| 447 | } |
| 448 | s := proc.Resume(t.table(), rec.Markers, rec.Sweep, rec.Before, rec.Groups, rec.Sessions, rec.Killed) |
| 449 | out := s.Settle(t.owned(), deadline, 0, func() bool { return true }, nil) |
| 450 | owe := func() proc.Obligation { |
| 451 | o := s.Owe() |
| 452 | o.Began, o.BootID, o.Before, o.FlagsKnown, o.UnsandboxedRan = rec.Began, rec.BootID, rec.Before, rec.FlagsKnown, rec.UnsandboxedRan |
| 453 | return o |
| 454 | } |
| 455 | if out != proc.Left && !rec.Complete && len(rec.Markers) > 0 { |
| 456 | // Its final note never came: what it left is not all on record, |
| 457 | // so its attempts are never proven over (the Machines page says |
| 458 | // so). Nothing of the runner user's is left: the Mac goes on. |
| 459 | o := owe() |
| 460 | o.Incomplete = true |
| 461 | if err := t.debts.Owe(o); err != nil { |
| 462 | return errors.Join(ErrUnsettled, err) |
| 463 | } |
| 464 | return t.dropTurn(rec.ID) |
| 465 | } |
| 466 | switch out { |
| 467 | case proc.Proven: |
| 468 | return t.dropTurn(rec.ID) |
| 469 | case proc.Owed: |
| 470 | // Nothing of the runner user's is left to kill, so the Mac may |
| 471 | // take turns again; what another user now holds stays owed by |
| 472 | // the attempt, and its proof waits for it. |
| 473 | if err := t.debts.Owe(owe()); err != nil { |
| 474 | return errors.Join(ErrUnsettled, err) |
| 475 | } |
| 476 | return t.dropTurn(rec.ID) |
| 477 | } |
| 478 | return ErrUnsettled |
| 479 | } |
| 480 | |
| 481 | // unsettledFor: the stored turn record names attempt (its leftovers are |
| 482 | // not settled yet). |
| 483 | func (t *Turns) unsettledFor(attempt string) bool { |
| 484 | rec, ok, err := t.loadTurn() |
| 485 | return err != nil || ok && slices.Contains(rec.Markers, attempt) |
| 486 | } |
| 487 | |
| 488 | func (t *Turns) saveTurn(rec turnRecord) error { |
| 489 | if t.beforeWrite != nil { |
| 490 | if err := t.beforeWrite(); err != nil { |
| 491 | return err |
| 492 | } |
| 493 | } |
| 494 | return t.records.Save(rec) |
| 495 | } |
| 496 | |
| 497 | func (t *Turns) loadTurn() (turnRecord, bool, error) { |
| 498 | var rec turnRecord |
| 499 | ok, err := t.records.Load(&rec) |
| 500 | if err == nil && ok && rec.ID == "" { |
| 501 | err = errors.New("no id") |
| 502 | } |
| 503 | if err != nil { |
| 504 | return turnRecord{}, false, errors.Join(ErrUnsettled, errors.New("mac: unreadable turn record"), err) |
| 505 | } |
| 506 | return rec, ok, nil |
| 507 | } |
| 508 | |
| 509 | // dropTurn removes the stored record if it is still id's. |
| 510 | func (t *Turns) dropTurn(id string) error { |
| 511 | rec, ok, err := t.loadTurn() |
| 512 | if err != nil || !ok || rec.ID != id { |
| 513 | return err |
| 514 | } |
| 515 | return t.records.Drop() |
| 516 | } |
| 517 | |
| 518 | func (t *Turns) identities() Procs { |
| 519 | if t != nil && t.ids != nil { |
| 520 | return t.ids |
| 521 | } |
| 522 | return platformProcs |
| 523 | } |
| 524 | |
| 525 | func (t *Turns) pending() string { |
| 526 | if t == nil || t.lock == "" { |
| 527 | return "" |
| 528 | } |
| 529 | return t.lock + ".pending" |
| 530 | } |
| 531 | |
| 532 | // pendingRecord is a sandboxed attempt whose T cleanup is not done yet: |
| 533 | // written before anything of the attempt starts (and again with its |
| 534 | // group once there is one), removed only once its end is proven and T |
| 535 | // is cleaned. Runner is the runner process whose turn wrote it: only |
| 536 | // that attempt in that process, or a later process once it is gone (a |
| 537 | // restart), may finish it. |
| 538 | type pendingRecord struct { |
| 539 | Attempt string `json:"attempt"` |
| 540 | PGID int `json:"pgid"` |
| 541 | Before tempListing `json:"before"` |
| 542 | Runner ProcID `json:"runner"` |
| 543 | // Began and BootID: the turn's (a later boot proves the attempt over). |
| 544 | Began time.Time `json:"began,omitzero"` |
| 545 | BootID string `json:"boot_id,omitempty"` |
| 546 | } |
| 547 | |
| 548 | // writePending records r durably. Without a lock file (tests) there is |
| 549 | // nowhere to keep it. |
| 550 | func (t *Turns) writePending(r pendingRecord) error { |
| 551 | if t.pending() == "" { |
| 552 | return nil |
| 553 | } |
| 554 | return t.pends.Save(r) |
| 555 | } |
| 556 | |
| 557 | // readPending is the record an earlier attempt left; ok is false when |
| 558 | // there is none. One that cannot be read is an error: nothing runs |
| 559 | // until a person looks. |
| 560 | func (t *Turns) readPending() (r pendingRecord, ok bool, err error) { |
| 561 | if t.pending() == "" { |
| 562 | return r, false, nil |
| 563 | } |
| 564 | if ok, err = t.pends.Load(&r); err == nil && ok && (r.Attempt == "" || r.Before == nil) { |
| 565 | err = errors.New("incomplete") |
| 566 | } |
| 567 | if err != nil { |
| 568 | return pendingRecord{}, false, errors.Join(ErrUserTemp, errors.New("mac: unreadable pending cleanup"), err) |
| 569 | } |
| 570 | return r, ok, nil |
| 571 | } |
| 572 | |
| 573 | // mayFinish: u may finish r. Its owner: the attempt, in the runner |
| 574 | // process that wrote it (the run itself, or its proof later). A restart: |
| 575 | // the process that wrote it is gone (or was older than the owner field). |
| 576 | // Never another turn of a live owner. |
| 577 | func (t *Turns) mayFinish(u *Turn, r pendingRecord) bool { |
| 578 | if r.Runner.PID == 0 { |
| 579 | return true |
| 580 | } |
| 581 | if r.Runner == proc.Self() { |
| 582 | return u.owns(r.Attempt) |
| 583 | } |
| 584 | // A restart only once the writer is surely gone: an identity that |
| 585 | // cannot be read is not a dead one. |
| 586 | alive, known := proc.Alive(t.identities(), r.Runner) |
| 587 | return known && !alive |
| 588 | } |
| 589 | |
| 590 | // dropPending removes the record once its cleanup is done. |
| 591 | func (t *Turns) dropPending() error { |
| 592 | if t.pending() == "" { |
| 593 | return nil |
| 594 | } |
| 595 | if t.beforeDrop != nil { |
| 596 | t.beforeDrop() |
| 597 | } |
| 598 | return t.pends.Drop() |
| 599 | } |
| 600 | |
| 601 | // clearOwed judges what attempt owes (proc.Debts.Assess) before |
| 602 | // deadline. It runs only holding u, a live turn of t: no operation can |
| 603 | // start a process meanwhile. It removes nothing. |
| 604 | func (t *Turns) clearOwed(u *Turn, attempt string, deadline time.Time) (rebooted, met bool, err error) { |
| 605 | if u == nil || u.t != t || HeldTurn(u.Context(context.Background()), t) == nil { |
| 606 | return false, false, ErrNoTurn |
| 607 | } |
| 608 | b, err := t.bootNow() |
| 609 | rebooted, met = t.debts.Assess(attempt, t.table(), t.owned(), b, err, deadline) |
| 610 | return rebooted, met, nil |
| 611 | } |
| 612 | |
| 613 | // markUnsandboxed puts on the turn's record, durably and before it |
| 614 | // starts, that something runs outside a sandbox in it: then only a |
| 615 | // reboot can clear the turn if its end is not proven. |
| 616 | func (u *Turn) markUnsandboxed() error { |
| 617 | u.mu.Lock() |
| 618 | defer u.mu.Unlock() |
| 619 | rec := u.rec |
| 620 | rec.UnsandboxedRan = true |
| 621 | if err := u.t.saveTurn(rec); err != nil { |
| 622 | u.lost = true |
| 623 | return err |
| 624 | } |
| 625 | u.rec = rec |
| 626 | return nil |
| 627 | } |
| 628 | |
| 629 | func (t *Turns) bootNow() (proc.Boot, error) { |
| 630 | if t.boot != nil { |
| 631 | return t.boot() |
| 632 | } |
| 633 | return creds.Boot() |
| 634 | } |
| 635 | |
| 636 | // Unproven is the Machines page's code for an attempt whose end is not |
| 637 | // proven here, and how many unaccounted processes keep it so. |
| 638 | func (t *Turns) Unproven() (string, int) { |
| 639 | switch incomplete, left := t.debts.Unproven(); { |
| 640 | case incomplete && left > 0: |
| 641 | return wire.UnprovenProcessesLeft, left |
| 642 | case incomplete: |
| 643 | return wire.UnprovenRecordIncomplete, 0 |
| 644 | } |
| 645 | return "", 0 |
| 646 | } |
| 647 | |
| 648 | // Incomplete reports whether an attempt's end can never be proven: its |
| 649 | // turn's record was not complete (the Machines page says so). |
| 650 | func (t *Turns) Incomplete() bool { return t.debts.Incomplete() } |
| 651 | |
| 652 | // Pending reports whether an attempt's cleanup is still to be done |
| 653 | // (the warm waits for it: that attempt's end is not proven yet). |
| 654 | func (t *Turns) Pending() bool { |
| 655 | _, err := os.Lstat(t.pending()) |
| 656 | return t.pending() != "" && err == nil |
| 657 | } |
| 658 | |