Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix waitpid to return the child exit code #53

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

hangj
Copy link

@hangj hangj commented May 16, 2023

  1. The child_ret returned by current waitpid is the status information of the child process, NOT the exit code.
    unsafe fn waitpid(pid: libc::pid_t) -> Result<libc::c_int, ErrorKind> {
    let mut child_ret = 0;
    check_err(libc::waitpid(pid, &mut child_ret, 0), ErrorKind::Wait)?;
    Ok(child_ret)
    }

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
Copy link
Author

hangj commented Jul 17, 2023

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant