daemon-detect
Detect whether the current process runs as an OS daemon — launchd, systemd, or a Windows service — and derive its service label and scope.
A binary that can run both interactively and as a managed service often
needs to know which one it is right now: to route telemetry, pick config
paths, decide whether to daemonize helpers, or report its own service unit
for systemctl / launchctl tooling. daemon-detect answers that from
process metadata with no process spawning:
- Unix — parent PID 1 (launchd / systemd / init), or the effective UID matching a service account you configure.
- Windows — an environment variable your service wrapper injects.
- systemd presence — the canonical
sd_booted(3)check (a singlestatof/run/systemd/system).
Usage
use ;
#
ServiceIdentity carries the binary name (systemd and Windows labels), a
reverse-DNS launchd prefix, and an instance discriminator (a port, an
identity key, …) so one binary can run several service instances. Each is
a validated type — BinaryName, LaunchdLabelPrefix, ServiceInstance,
ServiceAccountName, EnvVarName — whose try_new enforces the limits
of the OS surface it is handed to (length bounds, ASCII-graphic content,
and the separator characters of passwd entries, environment blocks,
systemd template units, and launchd labels).
DaemonState::service_mode distinguishes system-scoped services (root or
a configured service account) from per-user ones (launchctl agents,
systemd --user).
The crate performs no I/O beyond a stat and passwd lookups, spawns
nothing, and has no dependencies outside libc on Unix.
License
Licensed under either of:
- Apache License, Version 2.0 (
LICENSE-APACHE); - MIT License (
LICENSE-MIT).