1use std::sync::Arc;
12
13mod boot_guard;
14mod connection;
15mod dispatch_trait;
16mod locks;
17mod process_files;
18mod run_daemon;
19
20pub use dispatch_trait::*;
21
22#[cfg(all(unix, feature = "fault-injection"))]
23pub use connection::handle_conn_for_test;
24#[cfg(unix)]
25use connection::*;
26
27#[cfg(unix)]
28pub use run_daemon::*;
29
30#[cfg(unix)]
31pub use boot_guard::*;
32
33#[cfg(all(test, unix))]
34use locks::open_lock_file;
35#[cfg(unix)]
36pub use locks::{
37 acquire_daemon_boot_guard, acquire_recovery_lock, open_mutating_daemon_lock,
38 try_acquire_daemon_boot_guard_until, try_acquire_recoverer_lock_until, DaemonBootGuard,
39};
40#[cfg(unix)]
41pub(crate) use locks::{peer_uid, uid_is_permitted};
42#[cfg(unix)]
43use locks::{socket_identity, SocketIdentity};
44pub use process_files::*;
45
46mod store_guard;
47mod store_identity;
48#[cfg(unix)]
49use store_guard::ensure_claimed_parent_identity;
50#[cfg(unix)]
51pub use store_guard::{acquire_daemon_store_guards, bind_daemon_store_files, claim_stores};
52pub use store_guard::{assert_daemon_store_identities, DaemonStoreGuard};
53#[cfg(unix)]
54pub use store_identity::claimed_daemon_store_identity;
55mod supervisor_marker;
56#[cfg(unix)]
57pub use supervisor_marker::supervisor_marker_path;
58
59#[cfg(unix)]
60use std::io::Write as _;
61#[cfg(unix)]
62use std::os::unix::fs::{MetadataExt, PermissionsExt};
63#[cfg(unix)]
64use std::os::unix::io::AsRawFd;
65#[cfg(all(test, unix))]
66use std::path::PathBuf;
67
68#[cfg(unix)]
69use async_trait::async_trait;
70#[cfg(unix)]
71use libc;
72#[cfg(unix)]
73use serde::Deserialize;
74#[cfg(unix)]
75use tokio::io::AsyncReadExt;
76#[cfg(all(test, unix))]
77use tokio::io::AsyncWriteExt;
78#[cfg(unix)]
79use tokio::net::{UnixListener, UnixStream};
80
81#[cfg(unix)]
82use crate::pack::RequestIdentity;
83#[cfg(unix)]
84use khive_db::{run_checkpoint_task, CheckpointConfig, CheckpointLifecycleOwner, ConnectionPool};
85
86mod exit_record;
89mod lifecycle;
90#[cfg(unix)]
91use lifecycle::khive_dir;
92#[cfg(test)]
93use lifecycle::khive_root_from;
94#[cfg(unix)]
95use lifecycle::{
96 current_supervisor_claim, default_socket_path, ensure_rendezvous_overrides_paired,
97 read_supervisor_marker_claim, DaemonLifecycle, PID_PATH_ENV, SOCKET_PATH_ENV,
98};
99pub use lifecycle::{
100 lock_path, volume_lock_dir, DaemonLifecyclePhase, DaemonLifecycleSnapshot, DaemonLifetime,
101 DaemonOptions, DaemonShutdownReason, DaemonStartupReport, DEFAULT_DEMAND_IDLE_SECS,
102};
103#[cfg(unix)]
104pub use lifecycle::{
105 pid_path, recoverer_lock_path, rendezvous_split, socket_path, RendezvousSplit,
106 SUPERVISOR_CLAIM_ENV,
107};
108
109mod load_limits;
110#[cfg(unix)]
111use load_limits::{admit_or_refuse_busy, ConnectionAdmission};
112pub use load_limits::{
113 recall_ledger_snapshot, track_recall_ledger_task, ConnectionCapSnapshot, RecallLedgerSnapshot,
114};
115
116pub const MAX_FRAME_BYTES: usize = 8 * 1024 * 1024;
118
119pub const PROTOCOL_VERSION: u32 = 8;
128
129#[doc(hidden)]
133pub const DAEMON_LEXICAL_TIMEOUT_MARKER: &str = "__khive_daemon_lexical_timeout";
134
135const DEFAULT_DRAIN_TIMEOUT_SECS: u64 = 10;
136#[cfg(unix)]
140const INITIAL_FRAME_READ_TIMEOUT: std::time::Duration = std::time::Duration::from_secs(30);
141
142#[cfg(unix)]
143fn next_accept_error_backoff(previous: Option<std::time::Duration>) -> std::time::Duration {
144 previous
145 .map(|delay| delay.saturating_mul(2))
146 .unwrap_or_else(|| std::time::Duration::from_millis(10))
147 .min(std::time::Duration::from_secs(1))
148}
149
150mod config_id;
153#[cfg(test)]
154use config_id::parse_config_id;
155pub use config_id::{
156 config_id_extra_embedder_exclusions, config_ids_compatible, first_config_mismatch_field,
157};
158
159mod wire_frames;
160pub use wire_frames::{
161 drop_error_detail_iteratively, error_detail_value_within_limit, CheckpointStoreMetrics,
162 DaemonDispatchError, DaemonRequestFrame, DaemonResponseFrame, MetricsSnapshot,
163 ERROR_DETAIL_NESTING_DEPTH_LIMIT,
164};
165#[cfg(unix)]
166pub use wire_frames::{read_frame, write_frame};
167#[cfg(unix)]
168use wire_frames::{read_initial_frame, take_daemon_lexical_timeout_marker};
169
170#[cfg(unix)]
172pub fn env_truthy(key: &str) -> bool {
173 std::env::var(key)
174 .map(|v| {
175 let v = v.trim();
176 !v.is_empty() && v != "0" && !v.eq_ignore_ascii_case("false")
177 })
178 .unwrap_or(false)
179}
180
181include!("daemon_khive_root_tests.rs");
182
183#[cfg(all(unix, any(test, feature = "test-internals")))]
189#[doc(hidden)]
190pub async fn serve_connection_for_test<D: DaemonDispatch>(stream: UnixStream, dispatcher: D) {
191 handle_conn_with_shutdown(
192 stream,
193 dispatcher,
194 None,
195 tokio::time::Instant::now() + INITIAL_FRAME_READ_TIMEOUT,
196 )
197 .await;
198}
199
200#[cfg(all(test, unix))]
201#[path = "daemon_tests.rs"]
202mod tests;