writeArmStateFile writes (or idempotently overwrites) this session's arm-state file, mode 0600, with the given disarmed value, and returns the path. It refuses only when there is no key to write under at all — no messaging socket AND no working directory — because a file with neither could never be
(disarmed bool)
| 213 | // no messaging socket AND no working directory — because a file with |
| 214 | // neither could never be matched to a session or a repo by a reader. |
| 215 | func writeArmStateFile(disarmed bool) (path string, err error) { |
| 216 | socket, cwd := currentSessionArmKey() |
| 217 | if socket == "" && cwd == "" { |
| 218 | return "", fmt.Errorf("cannot record arm state: no messaging socket and no working directory to key the session on") |
| 219 | } |
| 220 | p, err := armStatePath(socket, cwd) |
| 221 | if err != nil { |
| 222 | return "", err |
| 223 | } |
| 224 | // Carry the first-connect flag forward across arm/disarm rewrites so |
| 225 | // toggling consent within a session doesn't offer the session-start |
| 226 | // playbook again. Only a LIVE prior file counts — a dead-owner one is a |
| 227 | // stale session and its first-connect state is irrelevant. |
| 228 | booted := false |
| 229 | if prev, _, rerr := readArmState(); rerr == nil && prev != nil && armStateOwnerAlive(prev) { |
| 230 | booted = prev.Booted |
| 231 | } |
| 232 | st := ArmState{ |
| 233 | Armed: !disarmed, |
| 234 | Disarmed: disarmed, |
| 235 | Booted: booted, |
| 236 | PID: os.Getpid(), |
| 237 | Socket: socket, |
| 238 | Cwd: cwd, |
| 239 | StartedAt: time.Now().UTC().Format(time.RFC3339), |
| 240 | } |
| 241 | if !disarmed { |
| 242 | var generation [32]byte |
| 243 | if _, err := rand.Read(generation[:]); err != nil { |
| 244 | return "", fmt.Errorf("generate arm generation: %w", err) |
| 245 | } |
| 246 | st.Generation = hex.EncodeToString(generation[:]) |
| 247 | } |
| 248 | if socket != "" { |
| 249 | // Bind the file to this specific socket instance — its mtime plus, |
| 250 | // where available, its inode+device — not just the path, so a |
| 251 | // reused path (or a lingering stale node) can't revive it. |
| 252 | if info, statErr := os.Stat(socket); statErr == nil { |
| 253 | st.SocketMtimeUnixNano = info.ModTime().UnixNano() |
| 254 | if ino, dev, ok := statIdentity(info); ok { |
| 255 | st.SocketIno, st.SocketDev = ino, dev |
| 256 | st.MountNamespace = namespaceStamp(mountNamespaceFn) |
| 257 | } |
| 258 | } |
| 259 | } else { |
| 260 | // Headless: the owner is the session process, not this command |
| 261 | // (BUG-3227). os.Getpid() here is the exiting `pad session arm`, |
| 262 | // which read as dead on the very next read, so every headless arm |
| 263 | // and disarm was reaped on arrival. CaptureSessionOwner resolves |
| 264 | // the same owner the session registry records, with its start |
| 265 | // token for the pid-reuse defence. |
| 266 | owner, oerr := CaptureSessionOwner() |
| 267 | if oerr != nil { |
| 268 | return "", fmt.Errorf("cannot record arm state: %w", oerr) |
| 269 | } |
| 270 | st.PID, st.ProcStart, st.PIDSource, st.PIDNamespace = owner.PID, owner.ProcStart, owner.PIDSource, owner.PIDNamespace |
| 271 | } |
| 272 | data, err := json.MarshalIndent(st, "", " ") |
no test coverage detected