Skip to main content

daemon_detect/
markers.rs

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