Skip to main content

Crate daemon_detect

Crate daemon_detect 

Source
Expand description

§daemon-detect

Crates.io Documentation CI License Downloads

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 single stat of /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:

Structs§

BinaryName
An installed binary’s file name, used in systemd template units ({binary}@{instance}.service) and Windows service names ({binary}-{instance}).
DaemonMarkers
Environment markers that count as evidence the process is a daemon.
DaemonState
Whether — and how — the current process runs as an OS daemon.
EnvVarName
An environment-variable name, as probed with std::env::var_os.
InvalidServiceValue
Why a service value was rejected.
LaunchdLabelPrefix
A reverse-DNS launchd label prefix (e.g. com.example.my-daemon).
ServiceAccountName
A Unix service-account name, as passed to getpwnam(3).
ServiceIdentity
How this process is named as an OS service.
ServiceInstance
A service-instance discriminator (a port, an identity key, …), appended to every label format.

Enums§

DaemonServiceMode
Whether a daemon service is installed system-wide or per-user.

Functions§

systemd_available
Returns true if systemd is the active init system.