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::{DaemonMarkers, ServiceIdentity, systemd_available};
let markers = DaemonMarkers::new()
.service_account("_mydaemon") // macOS convention
.service_account("mydaemon") // Linux convention
.windows_service_env("MY_DAEMON_SERVICE");
// Quick check for early startup decisions:
let daemonized = markers.is_daemon();
// Full detection with a derived service label:
let identity = ServiceIdentity::new("my-daemon", "com.example.my-daemon", "7476");
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.
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§
- 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.
- Service
Identity - How this process is named as an OS service.
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.