use std::path::Path;
use std::time::{Duration, Instant};
use crate::launch::Launch;
use crate::persist::{lock, Endpoint};
const STARTUP_TIMEOUT: Duration = Duration::from_secs(20);
const POLL: Duration = Duration::from_millis(25);
#[derive(Debug, Clone, thiserror::Error)]
pub enum DaemonError {
#[error("the MemFork daemon did not start within {}s: {reason}", STARTUP_TIMEOUT.as_secs())]
DidNotStart {
reason: String,
},
#[error(
"a MemFork daemon from version {theirs} already owns {dir}, and this is \
version {ours}.\n\
Two versions must not share one store: the formats they write need not \
match, and guessing is how data gets corrupted.\n\
Run `memfork stop` to shut the running one down, then try again."
)]
VersionMismatch {
theirs: String,
ours: String,
dir: String,
},
#[error("cannot start the MemFork daemon: {0}")]
Spawn(String),
}
pub fn usable(dir: &Path) -> Result<Option<Endpoint>, DaemonError> {
let Some(endpoint) = lock::owner(dir) else {
return Ok(None);
};
if endpoint.port.is_none() {
return Ok(Some(endpoint));
}
check_version(dir, &endpoint)?;
Ok(Some(endpoint))
}
fn check_version(dir: &Path, endpoint: &Endpoint) -> Result<(), DaemonError> {
let theirs = endpoint.memfork_version.as_deref().unwrap_or("unknown");
if theirs != crate::VERSION {
return Err(DaemonError::VersionMismatch {
theirs: theirs.to_owned(),
ours: crate::VERSION.to_owned(),
dir: dir.display().to_string(),
});
}
Ok(())
}
pub fn ensure(dir: &Path, launch: &Launch, idle_seconds: u64) -> Result<Endpoint, DaemonError> {
if let Some(endpoint) = usable(dir)? {
if endpoint.port.is_some() {
return Ok(endpoint);
}
}
spawn(dir, launch, idle_seconds)?;
let deadline = Instant::now() + STARTUP_TIMEOUT;
let mut last = String::from("no endpoint file appeared");
while Instant::now() < deadline {
match usable(dir) {
Ok(Some(endpoint)) if endpoint.port.is_some() => return Ok(endpoint),
Ok(Some(_)) => last = "something owns the directory but is not serving".to_owned(),
Ok(None) => {}
Err(e @ DaemonError::VersionMismatch { .. }) => return Err(e),
Err(e) => last = e.to_string(),
}
std::thread::sleep(POLL);
}
Err(DaemonError::DidNotStart { reason: last })
}
fn spawn(dir: &Path, launch: &Launch, idle_seconds: u64) -> Result<(), DaemonError> {
#[cfg(windows)]
windows_handles::stop_inheriting_std_handles();
let argv = launch.argv(&[
"serve",
"--data-dir",
&dir.display().to_string(),
"--port",
"0",
"--idle-timeout",
&idle_seconds.to_string(),
]);
if let Some(helper) = spawn_helper() {
return spawn_through(&helper, &argv);
}
let (program, args) = argv.split_first().ok_or_else(|| {
DaemonError::Spawn("there is no command to start a daemon with".to_owned())
})?;
let mut command = std::process::Command::new(program);
command
.args(args)
.stdin(std::process::Stdio::null())
.stdout(std::process::Stdio::null())
.stderr(std::process::Stdio::null());
#[cfg(unix)]
{
use std::os::unix::process::CommandExt;
command.process_group(0);
}
#[cfg(windows)]
{
use std::os::windows::process::CommandExt;
const CREATE_NEW_PROCESS_GROUP: u32 = 0x0000_0200;
const DETACHED_PROCESS: u32 = 0x0000_0008;
command.creation_flags(CREATE_NEW_PROCESS_GROUP | DETACHED_PROCESS);
}
command
.spawn()
.map(|_| ())
.map_err(|e| DaemonError::Spawn(e.to_string()))
}
pub const SPAWN_VIA_ENV: &str = "MEMFORK_SPAWN_VIA";
fn spawn_helper() -> Option<Vec<String>> {
let raw = std::env::var(SPAWN_VIA_ENV).ok()?;
let parts: Vec<String> = serde_json::from_str(&raw).ok()?;
(!parts.is_empty()).then_some(parts)
}
fn spawn_through(helper: &[String], argv: &[String]) -> Result<(), DaemonError> {
let command = serde_json::to_string(argv)
.map_err(|e| DaemonError::Spawn(format!("cannot encode the daemon command: {e}")))?;
let (program, args) = helper
.split_first()
.ok_or_else(|| DaemonError::Spawn("the spawn helper is empty".to_owned()))?;
let output = std::process::Command::new(program)
.args(args)
.arg(command)
.stdin(std::process::Stdio::null())
.stdout(std::process::Stdio::null())
.stderr(std::process::Stdio::piped())
.output()
.map_err(|e| DaemonError::Spawn(format!("cannot run the spawn helper: {e}")))?;
if output.status.success() {
return Ok(());
}
Err(DaemonError::Spawn(format!(
"the spawn helper failed: {}",
String::from_utf8_lossy(&output.stderr).trim()
)))
}
#[cfg(windows)]
mod windows_handles {
use windows_sys::Win32::Foundation::{SetHandleInformation, HANDLE_FLAG_INHERIT};
use windows_sys::Win32::System::Console::{
GetStdHandle, STD_ERROR_HANDLE, STD_INPUT_HANDLE, STD_OUTPUT_HANDLE,
};
#[allow(unsafe_code)]
pub(super) fn stop_inheriting_std_handles() {
for which in [STD_INPUT_HANDLE, STD_OUTPUT_HANDLE, STD_ERROR_HANDLE] {
unsafe {
let handle = GetStdHandle(which);
if !handle.is_null() {
SetHandleInformation(handle, HANDLE_FLAG_INHERIT, 0);
}
}
}
}
}
fn post_shutdown(port: u16, token: &str) -> Result<(), String> {
use std::io::{Read, Write};
let address = format!("127.0.0.1:{port}");
let mut stream = std::net::TcpStream::connect(&address)
.map_err(|e| format!("cannot reach the daemon on {address}: {e}"))?;
stream
.set_read_timeout(Some(Duration::from_secs(10)))
.map_err(|e| format!("cannot set a timeout on the connection: {e}"))?;
let request = format!(
"POST {path} HTTP/1.1\r\n\
Host: 127.0.0.1:{port}\r\n\
Authorization: Bearer {token}\r\n\
Content-Length: 0\r\n\
Connection: close\r\n\
\r\n",
path = crate::serve::SHUTDOWN_PATH,
);
stream
.write_all(request.as_bytes())
.map_err(|e| format!("cannot send the shutdown request: {e}"))?;
let mut answer = String::new();
let _ = stream.read_to_string(&mut answer);
if answer.starts_with("HTTP/1.1 401") {
return Err("the daemon rejected the token in its own endpoint file".to_owned());
}
Ok(())
}
pub fn stop(dir: &Path) -> Result<String, String> {
let Some(endpoint) = lock::owner(dir) else {
return Ok(format!("nothing is running on {}", dir.display()));
};
let (Some(port), Some(token)) = (endpoint.port, endpoint.token.clone()) else {
return Err(format!(
"process {} owns {} but is not a daemon, so there is nothing to stop.\n\
It will release the directory when it exits.",
endpoint.pid,
dir.display()
));
};
post_shutdown(port, &token)?;
let deadline = Instant::now() + Duration::from_secs(10);
while Instant::now() < deadline {
if lock::owner(dir).is_none() {
return Ok(format!(
"stopped the daemon (process {}) on {}",
endpoint.pid,
dir.display()
));
}
std::thread::sleep(POLL);
}
Err(format!(
"asked process {} to stop, and it still holds {} ten seconds later",
endpoint.pid,
dir.display()
))
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn nothing_running_is_not_an_error() {
let dir = tempfile::tempdir().expect("tempdir");
assert!(usable(dir.path()).expect("checked").is_none());
assert!(stop(dir.path())
.expect("stop")
.contains("nothing is running"));
}
#[test]
fn a_daemon_from_another_version_is_refused_by_name() {
let dir = tempfile::tempdir().expect("tempdir");
let endpoint = Endpoint {
memfork_version: Some("0.0.1-from-the-past".to_owned()),
port: Some(1234),
token: Some("t".to_owned()),
..Endpoint::for_this_process()
};
match check_version(dir.path(), &endpoint) {
Err(DaemonError::VersionMismatch { theirs, ours, .. }) => {
assert_eq!(theirs, "0.0.1-from-the-past");
assert_eq!(ours, crate::VERSION);
}
other => panic!("a foreign version was accepted: {other:?}"),
}
}
#[test]
fn a_daemon_that_declares_no_version_is_also_refused() {
let dir = tempfile::tempdir().expect("tempdir");
let endpoint = Endpoint {
memfork_version: None,
port: Some(1234),
token: Some("t".to_owned()),
..Endpoint::for_this_process()
};
assert!(check_version(dir.path(), &endpoint).is_err());
}
#[test]
fn our_own_version_is_accepted() {
let dir = tempfile::tempdir().expect("tempdir");
let endpoint = Endpoint {
memfork_version: Some(crate::VERSION.to_owned()),
port: Some(1234),
token: Some("t".to_owned()),
..Endpoint::for_this_process()
};
assert!(check_version(dir.path(), &endpoint).is_ok());
}
#[test]
fn the_mismatch_message_says_what_to_do_about_it() {
let err = DaemonError::VersionMismatch {
theirs: "0.0.1".to_owned(),
ours: "0.2.0".to_owned(),
dir: "/tmp/x".to_owned(),
};
let text = err.to_string();
assert!(text.contains("memfork stop"), "{text}");
assert!(text.contains("0.0.1") && text.contains("0.2.0"), "{text}");
}
}