Expand description
§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 daemon_detect::{
BinaryName, DaemonMarkers, EnvVarName, LaunchdLabelPrefix, ServiceAccountName,
ServiceIdentity, systemd_available,
};
let markers = DaemonMarkers::new()
.service_account(ServiceAccountName::try_new("_mydaemon")?) // macOS convention
.service_account(ServiceAccountName::try_new("mydaemon")?) // Linux convention
.windows_service_env(EnvVarName::try_new("MY_DAEMON_SERVICE")?);
// Quick check for early startup decisions:
let daemonized = markers.is_daemon();
// Full detection with a derived service label. Every value is
// validated at construction, so a malformed account name, label
// segment, or instance can never reach the OS surface:
let identity = ServiceIdentity::new(
BinaryName::try_new("my-daemon")?,
LaunchdLabelPrefix::try_new("com.example.my-daemon")?,
7476_u16, // ports convert directly into a ServiceInstance
);
let state = identity.detect(&markers);
if state.is_daemon {
println!(
"running as {} ({:?})",
state.service_label.as_deref().unwrap_or("?"),
state.service_mode,
);
}
// Label formats are also available directly, on every platform:
assert_eq!(identity.launchd_label(), "com.example.my-daemon.7476");
assert_eq!(identity.systemd_unit_name(), "my-daemon@7476.service");
assert_eq!(identity.windows_service_name(), "my-daemon-7476");
let _ = daemonized;
let _ = systemd_available();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).
§Links
Structs§
- Binary
Name - An installed binary’s file name, used in systemd template units
(
{binary}@{instance}.service) and Windows service names ({binary}-{instance}). - Daemon
Markers - Environment markers that count as evidence the process is a daemon.
- Daemon
State - Whether — and how — the current process runs as an OS daemon.
- EnvVar
Name - An environment-variable name, as probed with
std::env::var_os. - Invalid
Service Value - Why a service value was rejected.
- Launchd
Label Prefix - A reverse-DNS launchd label prefix (e.g.
com.example.my-daemon). - Service
Account Name - A Unix service-account name, as passed to
getpwnam(3). - Service
Identity - How this process is named as an OS service.
- Service
Instance - A service-instance discriminator (a port, an identity key, …), appended to every label format.
Enums§
- Daemon
Service Mode - Whether a daemon service is installed system-wide or per-user.
Functions§
- systemd_
available - Returns
trueif systemd is the active init system.