| @@ -12,10 +12,28 @@ import ( |
| 12 | 12 | |
| 13 | 13 | type runningProfile struct { |
| 14 | 14 | PID int `json:"pid"` |
| 15 | +新增: Started string `json:"started,omitempty"` |
| 15 | 16 | Fingerprint string `json:"fingerprint"` |
| 16 | 17 | JobID string `json:"job_id,omitempty"` |
| 17 | 18 | } |
| 18 | 19 | |
| 20 | +新增:// ProcessStartMatches reports false only when pid now belongs to a process |
| 21 | +新增:// started at a different time than the record. After a reboot or crash the |
| 22 | +新增:// recorded PID can be reused by an unrelated process. Records from older |
| 23 | +新增:// versions carry no start time and an unreadable start time cannot prove |
| 24 | +新增:// reuse; both keep the previous PID-only behavior. |
| 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 | +新增: |
| 19 | 37 | func (c Config) Fingerprint() string { |
| 20 | 38 | c.ExitPolicy = c.ExitPolicy.Normalized() |
| 21 | 39 | b, _ := json.Marshal(c) |
| @@ -34,7 +52,8 @@ func (c Config) RecordRunning(state, name string) error { |
| 34 | 52 | // RecordRunningAt records the network owner's identity in the runtime's real |
| 35 | 53 | // instance directory; container environments do not create Lima metadata. |
| 36 | 54 | func (c Config) RecordRunningAt(dir string) error { |
| 37 | | −删除: b, _ := json.Marshal(runningProfile{PID: os.Getpid(), Fingerprint: c.Fingerprint(), JobID: os.Getenv(JobEnv)}) |
| 55 | +新增: pid := os.Getpid() |
| 56 | +新增: b, _ := json.Marshal(runningProfile{PID: pid, Started: processStartToken(pid), Fingerprint: c.Fingerprint(), JobID: os.Getenv(JobEnv)}) |
| 38 | 57 | f, err := os.CreateTemp(dir, ".running-*") |
| 39 | 58 | if err != nil { |
| 40 | 59 | return err |
| @@ -71,11 +90,7 @@ func (c Config) CheckRunning(state string) error { |
| 71 | 90 | if json.Unmarshal(b, &r) != nil { |
| 72 | 91 | return errors.New("invalid running cage profile record") |
| 73 | 92 | } |
| 74 | | −删除: if r.PID < 1 { |
| 75 | | −删除: continue |
| 76 | | −删除: } |
| 77 | | −删除: alive := profileProcessAlive(r.PID) |
| 78 | | −删除: if alive && r.Fingerprint != c.Fingerprint() { |
| 93 | +新增: if r.alive() && r.Fingerprint != c.Fingerprint() { |
| 79 | 94 | return fmt.Errorf("cage configuration changed while a VM is running; run cage stop before restarting") |
| 80 | 95 | } |
| 81 | 96 | } |