Skip to main content

daemon_detect/
state.rs

1//! The result of daemon detection.
2
3/// Whether — and how — the current process runs as an OS daemon.
4#[derive(Debug, Clone, PartialEq, Eq)]
5pub struct DaemonState {
6    /// Whether the process is running as a daemon (parent is init /
7    /// launchd / the Windows service manager, or the effective user
8    /// is a configured service account).
9    pub is_daemon: bool,
10    /// The derived OS service label (e.g. a launchd label on macOS,
11    /// a systemd template-unit name on Linux, a service name on
12    /// Windows). `None` when the process is not a daemon.
13    pub service_label: Option<String>,
14    /// Whether the service runs system-wide or per-user. `None` when
15    /// the process is not a daemon.
16    pub service_mode: Option<DaemonServiceMode>,
17}
18
19impl DaemonState {
20    /// The state of a process that is not running as a daemon.
21    #[must_use]
22    pub fn not_a_daemon() -> Self {
23        Self {
24            is_daemon: false,
25            service_label: None,
26            service_mode: None,
27        }
28    }
29}
30
31/// Whether a daemon service is installed system-wide or per-user.
32#[derive(Debug, Clone, Copy, PartialEq, Eq)]
33pub enum DaemonServiceMode {
34    /// System-scoped service (root or a dedicated service account).
35    System,
36    /// User-scoped service (the current user's launchd agent or
37    /// `systemd --user` unit).
38    User,
39}