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:
parent
38882b70c3
commit
17f52e31ca
1 changed files with 22 additions and 4 deletions
|
|
@ -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();
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue