shadowsocks_rust/daemonize/
unix.rs

1use std::{env::current_dir, path::Path};
2
3use daemonize::Daemonize;
4use log::error;
5
6/// Daemonize a server process in a *nix standard way
7///
8/// This function will redirect `stdout`, `stderr` to `/dev/null`,
9/// and follow the exact behavior in shadowsocks-libev
10pub fn daemonize<F: AsRef<Path>>(pid_path: Option<F>) {
11    let pwd = current_dir()
12        .unwrap_or_else(|err| panic!("cannot get current working directory, {err:?}"))
13        .canonicalize()
14        .unwrap_or_else(|err| panic!("cannot get absolute path to working directory, {err:?}"));
15    let mut d = Daemonize::new().umask(0).working_directory(pwd);
16
17    if let Some(p) = pid_path {
18        d = d.pid_file(p);
19    }
20
21    if let Err(err) = d.start() {
22        error!("failed to daemonize, {:?} ({})", err, err);
23    }
24}