Documentation
¶
Index ¶
- Constants
- func AdoptPTY(fd uintptr, name string) *os.File
- func CPUPercent(pid int) (float64, error)
- func DetectPort(pid int) (int, error)
- func DetectPorts(pids []int) map[int]int
- func MemBytes(pid int) (int64, error)
- func ParseLsofListenPorts(out string) map[int][]int
- func ParseProcNetTCPListen(content string) map[string]int
- func TerminateGroup(pid int, grace time.Duration) (killed bool, err error)
- type Process
Constants ¶
const DefaultStopGrace = 5 * time.Second
DefaultStopGrace is how long a process is given to exit after SIGTERM before it is force-killed with SIGKILL.
Variables ¶
This section is empty.
Functions ¶
func AdoptPTY ¶
AdoptPTY wraps a PTY-master fd inherited from another process (a daemon re-exec passes them as fds 3, 4, …) as a pollable *os.File, matching how Start prepares its own PTY. Returns nil if the fd is not usable.
func CPUPercent ¶
CPUPercent returns the CPU usage percentage for the given PID. Returns 0 if the process is not found or measurement fails.
func DetectPort ¶
DetectPort returns the lowest TCP port a service is listening on, or 0 when it is not listening yet. See DetectPorts, which it wraps.
func DetectPorts ¶ added in v0.4.0
DetectPorts resolves the listening port of several services at once, keyed by the pid devrun tracks. Services with no listener are absent from the result.
Two things make this harder than asking the OS about one pid:
A service's own process usually holds no socket. Commands are spawned as `sh -c` (see hupShield), and a `yarn dev` or `npm start` leaves the listener in a grandchild, so the whole process tree has to be considered.
And both platforms have a trap that answers about the wrong process rather than failing: lsof ORs its selectors unless given -a, and /proc/<pid>/net/tcp is per network namespace, listing every socket on the host. Either one returns a plausible port belonging to something else entirely.
A service listening on several ports — an app port plus a debugger — has no single right answer; the lowest is returned so the value at least stays stable between polls. Declare the port in config when that guess is wrong.
func ParseLsofListenPorts ¶ added in v0.4.0
ParseLsofListenPorts reads `lsof -FpnT` field output and returns the ports each pid is listening on. Records are one field per line, tagged by their first byte: p a pid, f a new file, n its name, T a TCP state such as TST=LISTEN.
func ParseProcNetTCPListen ¶ added in v0.4.0
ParseProcNetTCPListen returns the listening port of every row in a /proc/net/tcp or tcp6 table, keyed by socket inode. Columns are sl, local_address, rem_address, st, ... with inode tenth; local_address is hex ADDRESS:PORT and state 0A is TCP_LISTEN.
func TerminateGroup ¶
TerminateGroup gracefully stops the process group led by pid: it sends SIGTERM to the group, polls for the leader to exit for up to grace, and only then sends SIGKILL to the group. It never reaps the process — a caller that owns Wait must do that. It reports whether SIGKILL had to be sent.
pid must be a process-group leader (pgid == pid), which holds for every process started by Start. Signals target the negative pid so the leader and all of its descendants receive them; if the group is already gone the bare pid is used as a fallback.
The leader-exit poll uses kill(pid, 0), so if pid is a child of the current process it needs a concurrent reaper (the daemon's watchExit, or an eventual Wait) — an exited-but-unreaped zombie keeps the poll alive until the grace period elapses. Re-adopted processes are reaped by init, so this is a non-issue there.
Types ¶
type Process ¶
Process represents a running child process with a PTY.
Cmd is nil for a process adopted across a daemon re-exec (it is not a child of the current process); Pid is always set and is what Stop signals.
func Start ¶
Start forks the command with a PTY. CWD defaults to current dir if empty. The caller (daemon supervisor) is responsible for reading PTY output.
func (*Process) Stop ¶
Stop terminates the process gracefully: SIGTERM first, then SIGKILL only if it is still alive after DefaultStopGrace. Signals go to the whole process group — Start runs each command in its own session via the PTY, so the child is a process-group leader — which also reaches services that spawn their own children (e.g. a dev server behind a shell wrapper).
Stop does NOT call Process.Wait — watchExit in the daemon supervisor is the sole owner of Wait, preventing a double-waitpid deadlock.