daemonOwnershipForPID classifies whether pid is this repo's watch daemon. It takes the PID explicitly (rather than re-reading watch.pid) so callers can validate the exact process they are about to act on, avoiding a TOCTOU race with a concurrent start/stop rewriting the pid file.
(root string, pid int)
| 85 | // validate the exact process they are about to act on, avoiding a TOCTOU race |
| 86 | // with a concurrent start/stop rewriting the pid file. |
| 87 | func daemonOwnershipForPID(root string, pid int) daemonOwnership { |
| 88 | if pid <= 0 { |
| 89 | return ownershipForeign |
| 90 | } |
| 91 | cmdline, err := processCommandLine(pid) |
| 92 | if err != nil { |
| 93 | return ownershipUnknown |
| 94 | } |
| 95 | cmdline = strings.TrimSpace(cmdline) |
| 96 | if cmdline == "" { |
| 97 | return ownershipUnknown |
| 98 | } |
| 99 | |
| 100 | absRoot, err := filepath.Abs(root) |
| 101 | if err != nil { |
| 102 | absRoot = root |
| 103 | } |
| 104 | if absRoot != "" && |
| 105 | strings.Contains(cmdline, "watch") && |
| 106 | strings.Contains(cmdline, "daemon") && |
| 107 | strings.Contains(cmdline, absRoot) { |
| 108 | return ownershipOwned |
| 109 | } |
| 110 | return ownershipForeign |
| 111 | } |
| 112 | |
| 113 | // IsOwnedDaemon reports whether the PID file points to a codemap watch daemon |
| 114 | // for this repository root. It is true only when ownership is positively |
no test coverage detected