daemon

package
v0.0.7 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 25, 2026 License: MIT Imports: 8 Imported by: 0

Documentation

Overview

Package daemon provides helpers for daemonizing the boid server process. It implements a self-re-exec pattern: the parent spawns a copy of itself with BOID_DAEMON_CHILD=1, waits for the UNIX socket to become ready, and then exits. The child redirects stdin/stdout/stderr to a log file, calls syscall.Setsid to detach from the terminal session, and runs the server.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func IsChild

func IsChild() bool

IsChild reports whether the current process is the daemon child.

func IsSocketAlive

func IsSocketAlive(socketPath string, timeout time.Duration) bool

IsSocketAlive reports whether something is actively listening on socketPath. It returns true if a UNIX domain socket can be dialed within timeout, which distinguishes a running server from a stale socket file (ECONNREFUSED) or missing socket file (ENOENT).

func LogFilePath

func LogFilePath() string

LogFilePath returns the path for the daemon log file. Uses $XDG_STATE_HOME/boid/boid.log, falling back to ~/.local/state/boid/boid.log.

func RedirectToLog

func RedirectToLog(logPath string) error

RedirectToLog opens logPath (O_APPEND|O_CREATE|O_WRONLY, 0o644), creates the parent directory if necessary, and replaces file descriptors 0, 1, and 2:

  • fd 0 (stdin) → /dev/null
  • fd 1 (stdout) → logPath
  • fd 2 (stderr) → logPath

func RedirectToLogRotating

func RedirectToLogRotating(logPath string) error

RedirectToLogRotating is the size-rotating variant of RedirectToLog. It creates an OS pipe, redirects stdin to /dev/null, and redirects stdout and stderr to the pipe write-end. A background goroutine copies from the pipe read-end into a logrotate.Writer so the log is rotated automatically when it grows past MaxSize.

The goroutine exits (and closes the writer) when all write-ends of the pipe are closed, which happens naturally when the process exits.

func Spawn

func Spawn(args []string) (int, error)

Spawn forks a daemon child by re-executing the current binary with the same arguments and with BOID_DAEMON_CHILD=1 added to the environment. It releases the child (no wait) and returns the child PID.

func WaitForSocket

func WaitForSocket(socketPath string, timeout time.Duration) error

WaitForSocket polls socketPath using net.Dial until a connection succeeds or timeout elapses. It returns nil on success, or a descriptive error on timeout.

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL