daemonize icon indicating copy to clipboard operation
daemonize copied to clipboard

Fix waitpid to return the child exit code

Open hangj opened this issue 1 year ago • 1 comments

  1. The child_ret returned by current waitpid is the status information of the child process, NOT the exit code.
    https://github.com/knsd/daemonize/blob/8a87c0b9e5568d2d57284d68e18ea5f24dcad5fb/daemonize/src/lib.rs#L443-L447

We can get the exit code with libc::WEXITSTATUS.

  1. I try to lock the pid_file before perform_fork in execute_child, or the child will always exit with 0, and the parent also exit with 0.

hangj avatar May 16 '23 13:05 hangj

Add a note here:

extern crate daemonize;

fn main() {
    daemonize::Daemonize::new()
        .working_directory(".")
        .pid_file("pid")
        .start()
        .expect("daemonize failed");

    loop {
        std::thread::sleep(std::time::Duration::from_secs(1));
    }
}

And cargo run twice

$ cargo run
$ echo $?
0
$ cargo run
thread 'main' panicked at 'daemonize failed: Error { kind: LockPidfile(35) }', my-test/src/main.rs:8:10
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
$ echo $?
0 # 0 again, should be non-zero

hangj avatar Jul 17 '23 08:07 hangj