Skip to main content

daemon_detect/
markers.rs

1//! The caller-supplied indicators that identify a daemon environment.
2
3use crate::values::{
4    EnvVarName,
5    ServiceAccountName,
6};
7
8/// Environment markers that count as evidence the process is a daemon.
9///
10/// Two kinds of evidence are configurable on top of the built-in
11/// parent-process heuristics (parent PID 1 on Unix; on Windows, the
12/// built-ins alone never fire — supply an environment marker):
13///
14/// - **Service accounts** — validated account names whose effective UID marks
15///   the process as a system daemon on Unix (e.g. `_mydaemon` on macOS,
16///   `mydaemon` on Linux).
17/// - **A Windows service environment variable** — a validated variable name the
18///   service wrapper injects into the daemon's environment.
19#[derive(Debug, Clone, Default)]
20pub struct DaemonMarkers {
21    pub(crate) service_accounts: Vec<ServiceAccountName>,
22    pub(crate) windows_service_env: Option<EnvVarName>,
23}
24
25impl DaemonMarkers {
26    /// Markers with no service accounts and no Windows environment
27    /// variable: only the parent-process heuristics apply.
28    #[must_use]
29    pub fn new() -> Self {
30        Self::default()
31    }
32
33    /// Add a Unix service account whose effective UID marks the
34    /// process as a system daemon.
35    #[must_use]
36    pub fn service_account(mut self, name: ServiceAccountName) -> Self {
37        self.service_accounts.push(name);
38        self
39    }
40
41    /// Set the environment variable that a Windows service wrapper
42    /// injects; its presence marks the process as a service.
43    #[must_use]
44    pub fn windows_service_env(mut self, variable: EnvVarName) -> Self {
45        self.windows_service_env = Some(variable);
46        self
47    }
48
49    /// Whether the current process is likely running as a daemon.
50    ///
51    /// Suitable for early startup decisions (e.g. telemetry routing)
52    /// before a full service identity exists. Does not derive labels.
53    #[must_use]
54    pub fn is_daemon(&self) -> bool {
55        crate::platform::is_daemon_impl(self)
56    }
57}