mirror of
https://github.com/devvmhq/devvm.git
synced 2026-12-01 06:23:17 -10:00
internal/daemon (standard library only, so devvm _proxy uses it too) owns devvm's background processes: Detach and Spawn (own session, no input, reaped once they end, DEVVM_DAEMON_STATUS naming the process's own status file), Handle (signals through the process started), Proc (a pid file checked against the process's arguments, Stop: SIGTERM, a wait, SIGKILL, the pid file removed once stopped), readiness by status file (Ready, WriteStatus, WaitReady: the pid and data, written in one line-ended write, never another start's), Alive and Signal (no pid below 1), and one lifeline meaning (FollowLifeline, Lifeline.Holds and Wait: content equality, a bounded wait past what the file held first). It replaces netpolicy's proxy pid, stop and start wait, vmssh.Forwards' pid, stop and start wait, the build proxy's process and log-read port, links' link-session pid, stop and start, the proxy's watchLifeline and vmssh's lifeline reading, platform.StartDetached/StartDetachedProcess, and the alive copies (netpolicy alive/signal, lima alive, vmssh.ProcessRuns, smolvm runsOn's). The three log-scraping waits go: the proxy and the forwarder say they're ready in a status file, and the build proxy's port comes from its own. forbidigo bans os.FindProcess, syscall.Kill and unix.Kill outside internal/daemon and internal/platform, with its line in testdata/lint/forbidigo.go; the structure test lists DEVVM_DAEMON_STATUS and puts internal/daemon among the leaves; depguard holds it to the standard library. Tests: daemon's own (detach, handle stop, a reused pid, a stale pid file, groups refused, readiness, lifeline), characterisation tests first for the proxy's, the forwarders', the link sessions' and the build proxy's pid handling; coverage: internal/daemon 97.7%, lima 87.6 to 87.7. Co-Authored-By: Claude Opus 5.5 <[email protected]>
62 lines
1.9 KiB
Go
62 lines
1.9 KiB
Go
package daemon
|
|
|
|
import (
|
|
"os"
|
|
"strconv"
|
|
"strings"
|
|
"time"
|
|
)
|
|
|
|
// StatusEnv names a background process's status file (Cmd.Status) in its
|
|
// environment: what devvm gives the processes it starts.
|
|
const StatusEnv = "DEVVM_DAEMON_STATUS"
|
|
|
|
// Ready says the process is ready, with data (a proxy's address), in the
|
|
// status file its start named, and takes that name out of its environment,
|
|
// so the programs it runs never write there. Without one it does nothing.
|
|
func Ready(data string) error {
|
|
path := os.Getenv(StatusEnv)
|
|
if path == "" {
|
|
return nil
|
|
}
|
|
_ = os.Unsetenv(StatusEnv)
|
|
return WriteStatus(path, os.Getpid(), data)
|
|
}
|
|
|
|
// WriteStatus writes a status file: the pid of the process that's ready,
|
|
// then its data, on one line each, in a single write.
|
|
func WriteStatus(path string, pid int, data string) error {
|
|
return writeLine(path, strconv.Itoa(pid)+"\n"+strings.ReplaceAll(data, "\n", " "))
|
|
}
|
|
|
|
// readStatus returns what the process pid said in its status file, once
|
|
// it has said it whole: never another process's, such as an earlier
|
|
// start's.
|
|
func readStatus(path string, pid int) (string, bool) {
|
|
b, err := os.ReadFile(path) //nolint:gosec // devvm's own file for the process
|
|
if err != nil {
|
|
return "", false
|
|
}
|
|
first, rest, _ := strings.Cut(string(b), "\n")
|
|
data, end := strings.CutSuffix(rest, "\n")
|
|
return data, end && first == strconv.Itoa(pid)
|
|
}
|
|
|
|
// WaitReady waits for the process pid to say it's ready in its status file
|
|
// (Ready), looking tries times, tick apart, and returns what it said. ok
|
|
// is false once runs says it no longer runs, or when it never said so:
|
|
// one that said so and then ended isn't ready.
|
|
func WaitReady(status string, pid int, runs func() bool, tick time.Duration, tries int) (data string, ok bool) {
|
|
for range tries {
|
|
data, said := readStatus(status, pid)
|
|
if !runs() {
|
|
return "", false
|
|
}
|
|
if said {
|
|
return data, true
|
|
}
|
|
time.Sleep(tick)
|
|
}
|
|
return "", false
|
|
}
|