1use crate::{process::Process, BusServer, Module, RigError};
2use sha2::{Digest, Sha256};
3use std::{
4 path::{Path, PathBuf},
5 process::{Command, Stdio},
6 time::{Duration, Instant},
7};
8use subc_control::{ClientControlRequest, ClientControlResponse};
9use subc_protocol::{Flags, Frame, FrameType, Priority};
10use subc_transport::{authenticate_client, connection_file, read_frame, write_frame};
11use tokio::{io::AsyncWriteExt, net::TcpStream, task::JoinHandle};
12
13pub struct DaemonOptions {
14 pub modules: Vec<Module>,
15 pub machine_id: Option<String>,
18 pub startup_timeout: Duration,
19}
20
21impl Default for DaemonOptions {
22 fn default() -> Self {
23 Self {
24 modules: vec![],
25 machine_id: Some(new_machine_id()),
26 startup_timeout: Duration::from_secs(30),
27 }
28 }
29}
30
31fn with_storage(config: &str, data_home: &Path) -> Result<String, RigError> {
36 let mut value: serde_json::Value =
37 serde_json::from_str(config).map_err(|error| RigError::Invalid(error.to_string()))?;
38 value["storage"] = serde_json::json!({"backend": "sqlite", "data_home": data_home});
39 serde_json::to_string_pretty(&value).map_err(|error| RigError::Invalid(error.to_string()))
40}
41
42pub fn new_machine_id() -> String {
43 let key = nkeys::KeyPair::new_user();
44 crate::signer::hex(&Sha256::digest(key.public_key().as_bytes())[..16])
45}
46
47pub fn write_machine_id(data_home: &Path, machine_id: &str) -> Result<PathBuf, RigError> {
49 if machine_id.len() != 32
50 || !machine_id
51 .bytes()
52 .all(|byte| byte.is_ascii_digit() || (b'a'..=b'f').contains(&byte))
53 {
54 return Err(RigError::Invalid(
55 "machine id must be 32 lowercase hex characters".into(),
56 ));
57 }
58 let path = data_home.join("cortexkit/machine-id");
59 std::fs::create_dir_all(path.parent().unwrap())?;
60 std::fs::write(&path, format!("{machine_id}\n"))?;
61 Ok(path)
62}
63
64pub struct PrivateDaemon {
67 child: Option<Process>,
68 signer: Option<JoinHandle<Result<(), subc_client_rs::SubcModuleError>>>,
69 root: tempfile::TempDir,
70 pub connection_file: PathBuf,
71 pub config_file: PathBuf,
72 pub data_home: PathBuf,
73 pub runtime_dir: PathBuf,
74 pub config_home: PathBuf,
75 log: PathBuf,
76}
77
78impl PrivateDaemon {
79 pub async fn start(bus: &BusServer, options: DaemonOptions) -> Result<Self, RigError> {
80 let subc = crate::ck_subc_binary()?;
82 let ck_bus = crate::binaries::ck_bus_source()?;
83 let root = tempfile::tempdir()?;
84 let data_home = root.path().join("data");
85 let runtime_dir = root.path().join("run");
86 let config_home = root.path().join("config");
87 for dir in [&data_home, &runtime_dir, &config_home.join("cortexkit")] {
88 std::fs::create_dir_all(dir)?;
89 }
90 if let Some(machine_id) = &options.machine_id {
91 write_machine_id(&data_home, machine_id)?;
92 }
93 let config_file = config_home.join("cortexkit/subc.jsonc");
94 std::fs::write(
95 &config_file,
96 with_storage(
97 &crate::subc_config(&ck_bus, &bus.ckbus_env(), &options.modules)?,
98 &data_home,
99 )?,
100 )?;
101 let connection_file = runtime_dir.join("subc-connection.json");
102 let log = root.path().join("daemon.log");
103 let output = std::fs::File::create(&log)?;
104 let child = Process(
107 Command::new(subc)
108 .env("XDG_DATA_HOME", &data_home)
109 .env("XDG_RUNTIME_DIR", &runtime_dir)
110 .env("XDG_CONFIG_HOME", &config_home)
111 .env("SUBC_CGROUP_PLACEMENT", "disabled")
112 .env("SUBC_PORT", "0")
113 .stdin(Stdio::null())
114 .stdout(output.try_clone()?)
115 .stderr(output)
116 .spawn()?,
117 );
118 let mut daemon = Self {
119 child: Some(child),
120 signer: None,
121 root,
122 connection_file,
123 config_file,
124 data_home,
125 runtime_dir,
126 config_home,
127 log,
128 };
129 let deadline = Instant::now() + options.startup_timeout;
130 daemon.wait_connection(deadline).await?;
131 let signer = bus.trust.signer.clone();
132 let connection = daemon.connection_file.clone();
133 daemon.signer = Some(tokio::spawn(async move {
134 subc_client_rs::serve_with(&connection, signer.manifest(), signer).await
135 }));
136 loop {
137 daemon.check_child()?;
138 let response = tokio::time::timeout_at(
139 tokio::time::Instant::from_std(deadline),
140 daemon.control(ClientControlRequest::CatalogList {
141 module_id: Some("ckbus".into()),
142 }),
143 )
144 .await;
145 if let Ok(Ok(ClientControlResponse::CatalogList { modules, .. })) = response {
146 if modules.iter().any(|module| module.module_id == "ckbus") {
147 return Ok(daemon);
148 }
149 }
150 if Instant::now() >= deadline {
151 return Err(daemon.timeout("ckbus catalog registration"));
152 }
153 tokio::time::sleep(Duration::from_millis(50)).await;
154 }
155 }
156
157 async fn wait_connection(&mut self, deadline: Instant) -> Result<(), RigError> {
158 loop {
159 self.check_child()?;
160 if connection_file::read(&self.connection_file).is_ok() {
161 return Ok(());
162 }
163 if Instant::now() >= deadline {
164 return Err(self.timeout("private connection file"));
165 }
166 tokio::time::sleep(Duration::from_millis(50)).await;
167 }
168 }
169 fn check_child(&mut self) -> Result<(), RigError> {
170 if let Some(status) = self.child.as_mut().unwrap().0.try_wait()? {
171 return Err(RigError::Invalid(format!(
172 "ck-subc exited {status}: {}",
173 self.log_text()
174 )));
175 }
176 Ok(())
177 }
178 fn timeout(&self, waiting_for: &str) -> RigError {
179 RigError::Timeout {
180 waiting_for: waiting_for.into(),
181 details: format!(
182 "expected {}; {}",
183 self.connection_file.display(),
184 self.log_text()
185 ),
186 }
187 }
188 pub fn root(&self) -> &Path {
189 self.root.path()
190 }
191 pub fn pid(&self) -> u32 {
192 self.child.as_ref().unwrap().0.id()
193 }
194 pub fn log_text(&self) -> String {
195 let mut text = std::fs::read_to_string(&self.log).unwrap_or_default();
196 let mut directories = vec![self.data_home.join("cortexkit/run/logs")];
197 while let Some(directory) = directories.pop() {
198 let Ok(entries) = std::fs::read_dir(directory) else {
199 continue;
200 };
201 for entry in entries.filter_map(Result::ok) {
202 if entry.file_type().is_ok_and(|kind| kind.is_dir()) {
203 directories.push(entry.path());
204 } else {
205 text.push_str(&format!(
206 "\n{}:\n{}",
207 entry.path().display(),
208 std::fs::read_to_string(entry.path()).unwrap_or_default()
209 ));
210 }
211 }
212 }
213 text
214 }
215
216 pub async fn control(
217 &self,
218 request: ClientControlRequest,
219 ) -> Result<ClientControlResponse, RigError> {
220 tokio::time::timeout(
221 Duration::from_secs(3),
222 control(&self.connection_file, request),
223 )
224 .await
225 .map_err(|_| self.timeout("daemon control response"))?
226 }
227}
228
229impl Drop for PrivateDaemon {
230 fn drop(&mut self) {
231 drop(self.child.take());
232 if let Some(signer) = self.signer.take() {
233 signer.abort();
234 }
235 }
236}
237
238async fn control(
239 path: &Path,
240 request: ClientControlRequest,
241) -> Result<ClientControlResponse, RigError> {
242 let connection =
243 connection_file::read(path).map_err(|error| RigError::Invalid(error.to_string()))?;
244 let endpoint = connection
245 .endpoints
246 .first()
247 .ok_or_else(|| RigError::Invalid("no daemon endpoint".into()))?;
248 let mut stream = TcpStream::connect((endpoint.host.as_str(), endpoint.port)).await?;
249 authenticate_client(&mut stream, &connection, Duration::from_secs(2))
250 .await
251 .map_err(|error| RigError::Invalid(error.to_string()))?;
252 let frame = Frame::build(
253 FrameType::Request,
254 Flags::new(false, Priority::Passive, false),
255 0,
256 0,
257 1,
258 serde_json::to_vec(&request).map_err(|error| RigError::Invalid(error.to_string()))?,
259 )
260 .map_err(|error| RigError::Invalid(error.to_string()))?;
261 write_frame(&mut stream, &frame)
262 .await
263 .map_err(|error| RigError::Invalid(error.to_string()))?;
264 stream.flush().await?;
265 let response = read_frame(&mut stream)
266 .await
267 .map_err(|error| RigError::Invalid(error.to_string()))?
268 .ok_or_else(|| RigError::Invalid("daemon closed control connection".into()))?;
269 if response.header.ty != FrameType::Response {
270 return Err(RigError::Invalid(format!(
271 "control refused: {}",
272 String::from_utf8_lossy(&response.body)
273 )));
274 }
275 serde_json::from_slice(&response.body).map_err(|error| RigError::Invalid(error.to_string()))
276}