mirror of
https://github.com/devvmhq/devvm.git
synced 2026-12-01 06:23:17 -10:00
run/proxy.confined said how the child was confined, but not which broker said it. After a downgrade, an older devvm starts the VM's proxy as one process and never writes the file, so the newer devvm's file stayed and devvm doctor took the older devvm's unconfined proxy for a confined one. The broker now writes a second line naming itself, its pid and its start as the system tells it (proxy.BrokerID, through IO.Started), and ReadConfined takes the file only when it names the running broker, which doctor finds by the pid file and platform.ProcessStart (kern.proc.pid's p_starttime on macOS, /proc's starttime on Linux). A file that names another broker, or none, is one that hasn't said, so another binary's proxy gets proxy_old, and when doctor can't read the running broker's start it says it can't tell (proxy_confinement_unknown). The e2e broker check reads the file's first line and checks the second names run/proxy.pid's broker. Tests: TestUnconfinedStaleMarker, TestReadConfined, TestProcessStart. Co-Authored-By: Claude Opus 5.5 <[email protected]>
34 lines
961 B
Go
34 lines
961 B
Go
package platform
|
|
|
|
import (
|
|
"errors"
|
|
"os"
|
|
"strconv"
|
|
"strings"
|
|
)
|
|
|
|
// ProcessStart returns when a running process started, as the kernel keeps it: /proc's
|
|
// starttime, in clock ticks since boot, as one word of digits, the same at every read for as long
|
|
// as the process runs.
|
|
func ProcessStart(pid int) (string, error) {
|
|
b, err := os.ReadFile("/proc/" + strconv.Itoa(pid) + "/stat")
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
// The command's name, in parentheses, can hold spaces and parentheses itself, so the fields
|
|
// are counted from the last ")": the state is the third field, and starttime the 22nd.
|
|
i := strings.LastIndexByte(string(b), ')')
|
|
if i < 0 {
|
|
return "", errors.New("bad /proc stat")
|
|
}
|
|
f := strings.Fields(string(b[i+1:]))
|
|
if len(f) < 20 {
|
|
return "", errors.New("short /proc stat")
|
|
}
|
|
start := f[22-3]
|
|
if _, err := strconv.ParseUint(start, 10, 64); err != nil {
|
|
return "", errors.New("bad /proc starttime")
|
|
}
|
|
return start, nil
|
|
}
|