graceful startup errors instead of panics

- bind socket: eprintln instead of expect (exit 0, not 101)
- write_pid, create_dir_all, set_permissions: check errors
- all errors print to stderr and return cleanly
This commit is contained in:
Niko Marmeladkov 2026-07-03 13:14:54 +03:00
parent 38882b70c3
commit 17f52e31ca
Signed by untrusted user who does not match committer: Niko
GPG key ID: E3B955F9442D44E3

View file

@ -38,7 +38,10 @@ pub fn run(config_path: String) {
// Write daemon PID
let pid_path = state::daemon_pid_path();
state::write_pid(&pid_path).ok();
if let Err(e) = state::write_pid(&pid_path) {
eprintln!("error: write PID at {:?}: {}", pid_path, e);
return;
}
// Ensure socket path is clean
let sock_path = state::socket_path();
@ -46,8 +49,16 @@ pub fn run(config_path: String) {
let _ = fs::remove_file(&sock_path);
}
if let Some(parent) = sock_path.parent() {
fs::create_dir_all(parent).ok();
let _ = fs::set_permissions(parent, fs::Permissions::from_mode(0o770));
if let Err(e) = fs::create_dir_all(parent) {
eprintln!("error: create {}: {}", parent.display(), e);
let _ = fs::remove_file(&pid_path);
return;
}
if let Err(e) = fs::set_permissions(parent, fs::Permissions::from_mode(0o770)) {
eprintln!("error: chmod {}: {}", parent.display(), e);
let _ = fs::remove_file(&pid_path);
return;
}
}
// Load config
@ -60,7 +71,14 @@ pub fn run(config_path: String) {
}
};
let listener = UnixListener::bind(&sock_path).expect("bind socket");
let listener = match UnixListener::bind(&sock_path) {
Ok(l) => l,
Err(e) => {
eprintln!("error: bind socket at {:?}: {}", sock_path, e);
let _ = fs::remove_file(&pid_path);
return;
}
};
listener.set_nonblocking(true).ok();
let mut procs: HashMap<String, Proc> = HashMap::new();