Documentation
¶
Overview ¶
Package daemonize gives `oac-daemon connect -b` a no-cgo way to detach from the controlling terminal on macOS, Linux and Windows. Strategy is re-exec-the-binary rather than POSIX double-fork: the parent opens connect.log + connect.pid, then starts a fresh copy of its own argv with stdio redirected to the log file and a sentinel env var set so the child skips the fork branch. Setsid puts the child into its own session so closing the user's shell doesn't kill the daemon.
Re-exec (not raw fork) because Go's runtime is not fork-safe — the goroutine scheduler holds locks the child can't release without exec. exec.Cmd.Start does fork+exec, which is the safe combination.
Index ¶
- Constants
- Variables
- func EnsureLogFile(path string) error
- func IsAlive(pid int) error
- func IsBackgroundChild() bool
- func MustWriteLine(path string, line string) error
- func NotifyContext(parent context.Context) (context.Context, context.CancelFunc)
- func ReadPIDFile(path string) (int, error)
- func RemovePIDFile(path string) error
- func Spawn(argv []string, opts ReExecOptions) (int, error)
- func StopPIDFile(path string, timeout time.Duration) error
- func Tail(path string, opts TailOptions, ctxDone <-chan struct{}, w io.Writer) error
- func WritePIDFile(path string, pid int) error
- type ReExecOptions
- type TailOptions
Constants ¶
const BackgroundSentinelEnv = "OAC_RUNTIME_DAEMON_BACKGROUND_CHILD"
BackgroundSentinelEnv is set by the parent on the child's environment so the child knows it's the post-fork incarnation and must NOT itself try to re-fork. runConnect inspects via IsBackgroundChild.
Variables ¶
var ErrNotRunning = errors.New("daemonize: process not running")
var ErrStaleOrCorrupt = errors.New("daemonize: unsupported, stale or corrupt process record")
Functions ¶
func EnsureLogFile ¶
EnsureLogFile creates the log file (0o600) if missing so a `oac-daemon logs` before the first `connect -b` gets "0 bytes" instead of "no such file".
func IsBackgroundChild ¶
func IsBackgroundChild() bool
IsBackgroundChild reports whether this process was spawned by a `connect -b` re-exec. runConnect skips the fork branch when true, otherwise the child would spawn grandchildren forever.
func MustWriteLine ¶
MustWriteLine appends one line to path with 0o600 mode, adding a trailing newline if missing. Returns errors despite the name — kept short because it's used in startup hot paths.
func NotifyContext ¶
func ReadPIDFile ¶
func RemovePIDFile ¶
func Spawn ¶
func Spawn(argv []string, opts ReExecOptions) (int, error)
Spawn re-execs the current binary in the background. argv is the new process's full argv including argv[0]. Spawn does NOT scrub `-b` — BackgroundSentinelEnv is what tells the child to skip the fork.
On error the partially-opened log file is closed and a best-effort pidfile cleanup runs so a half-spawn doesn't leave stale state.
func StopPIDFile ¶
StopPIDFile never removes ownership before the exact process has exited. A cleanup timeout leaves the record available for observation and a later stop.
func Tail ¶
func Tail(path string, opts TailOptions, ctxDone <-chan struct{}, w io.Writer) error
Tail prints the last opts.LastLines lines of path to w, optionally following the file (poll-on-stat) until ctxDone fires. Designed for human eyes — not a high-throughput log aggregator.
Pass a nil ctxDone when Follow is false.
func WritePIDFile ¶
Types ¶
type ReExecOptions ¶
type ReExecOptions struct {
// LogPath is the absolute log file path. Child stdin is
// /dev/null; stdout+stderr are appended to this file (0o600).
LogPath string
// PIDPath is the absolute process-record path. The parent writes the
// child identity here before returning; existing files are replaced
// atomically.
PIDPath string
// ExtraEnv is appended to the child's environment in addition to
// parent environ + BackgroundSentinelEnv.
ExtraEnv []string
}
ReExecOptions controls how Spawn launches the background child.
type TailOptions ¶
type TailOptions struct {
// LastLines is the number of trailing lines to print before any
// follow logic kicks in. 0 → print nothing historical, jump
// straight to follow.
LastLines int
// Follow keeps the tail open after printing LastLines, polling
// for new bytes appended by a still-running daemon. false →
// return immediately once the historical chunk is flushed.
Follow bool
// PollInterval is the cadence at which Tail re-stats the file
// looking for growth. Zero → 500ms.
PollInterval time.Duration
}
TailOptions configures Tail.