| 1 | package cage |
| 2 | |
| 3 | import ( |
| 4 | "crypto/sha256" |
| 5 | "encoding/hex" |
| 6 | "encoding/json" |
| 7 | "errors" |
| 8 | "fmt" |
| 9 | "os" |
| 10 | "path/filepath" |
| 11 | ) |
| 12 | |
| 13 | type runningProfile struct { |
| 14 | PID int `json:"pid"` |
| 15 | Started string `json:"started,omitempty"` |
| 16 | Fingerprint string `json:"fingerprint"` |
| 17 | JobID string `json:"job_id,omitempty"` |
| 18 | } |
| 19 | |
| 20 | |
| 21 | |
| 22 | |
| 23 | |
| 24 | |
| 25 | func ProcessStartMatches(pid int, started string) bool { |
| 26 | if started == "" { |
| 27 | return true |
| 28 | } |
| 29 | current := processStartToken(pid) |
| 30 | return current == "" || current == started |
| 31 | } |
| 32 | |
| 33 | func (r runningProfile) alive() bool { |
| 34 | return r.PID > 0 && profileProcessAlive(r.PID) && ProcessStartMatches(r.PID, r.Started) |
| 35 | } |
| 36 | |
| 37 | func (c Config) Fingerprint() string { |
| 38 | c.ExitPolicy = c.ExitPolicy.Normalized() |
| 39 | b, _ := json.Marshal(c) |
| 40 | h := sha256.Sum256(b) |
| 41 | return hex.EncodeToString(h[:]) |
| 42 | } |
| 43 | |
| 44 | func (c Config) RecordRunning(state, name string) error { |
| 45 | if filepath.Base(name) != name { |
| 46 | return errors.New("invalid cage instance name") |
| 47 | } |
| 48 | dir := filepath.Join(state, "lima", name) |
| 49 | return c.RecordRunningAt(dir) |
| 50 | } |
| 51 | |
| 52 | |
| 53 | |
| 54 | func (c Config) RecordRunningAt(dir string) error { |
| 55 | pid := os.Getpid() |
| 56 | b, _ := json.Marshal(runningProfile{PID: pid, Started: processStartToken(pid), Fingerprint: c.Fingerprint(), JobID: os.Getenv(JobEnv)}) |
| 57 | f, err := os.CreateTemp(dir, ".running-*") |
| 58 | if err != nil { |
| 59 | return err |
| 60 | } |
| 61 | defer os.Remove(f.Name()) |
| 62 | if _, err = f.Write(b); err != nil { |
| 63 | f.Close() |
| 64 | return err |
| 65 | } |
| 66 | if err = f.Close(); err != nil { |
| 67 | return err |
| 68 | } |
| 69 | return os.Rename(f.Name(), filepath.Join(dir, "cage-running.json")) |
| 70 | } |
| 71 | |
| 72 | |
| 73 | |
| 74 | func (c Config) CheckRunning(state string) error { |
| 75 | files, err := filepath.Glob(filepath.Join(state, "lima", "*", "cage-running.json")) |
| 76 | if err != nil { |
| 77 | return err |
| 78 | } |
| 79 | containerRecords, err := filepath.Glob(filepath.Join(state, "container", "*", "cage-running.json")) |
| 80 | if err != nil { |
| 81 | return err |
| 82 | } |
| 83 | files = append(files, containerRecords...) |
| 84 | for _, file := range files { |
| 85 | b, err := os.ReadFile(file) |
| 86 | if err != nil { |
| 87 | return err |
| 88 | } |
| 89 | var r runningProfile |
| 90 | if json.Unmarshal(b, &r) != nil { |
| 91 | return errors.New("invalid running cage profile record") |
| 92 | } |
| 93 | if r.alive() && r.Fingerprint != c.Fingerprint() { |
| 94 | return fmt.Errorf("cage configuration changed while a VM is running; run cage stop before restarting") |
| 95 | } |
| 96 | } |
| 97 | return nil |
| 98 | } |