Linux : Child process wait for parent process termination

OS : Ubuntu

Child process:

#[cfg(target_os = "linux")]
fn watch_parent_process(_ : i32){
    use libc::{prctl, PR_SET_PDEATHSIG};
    use nix::sys::signal::{sigaction, SaFlags, SigAction, SigHandler, SigSet, SIGUSR1};

    unsafe {
        prctl(PR_SET_PDEATHSIG, SIGUSR1);
    }


    let sig_action = SigAction::new(
        SigHandler::Handler(handle_sigusr1),
        SaFlags::empty(),
        SigSet::empty(),
    );

    if let Err(err) = unsafe { sigaction(SIGUSR1, &sig_action) } {
        error!("sigaction(SIGUSR1): {}", err);
        process::exit(2);
    };
    
}

#[cfg(target_os = "linux")]
extern "C" fn handle_sigusr1(_: libc::c_int) {
    info!("Parent process exited");
    process::exit(1);
}

Parent process

unsafe {
        prctl(PR_SET_CHILD_SUBREAPER, 1, 0, 0, 0);
}
let mut cmd = Command::new("/home/Dev/x-x-x/target/x86_64-unknown-linux-gnu/release/x-x-x");

match cmd.spawn() {
    Ok(child) => println!("process id is {}", child.id()),
    Err(e) => {
        println!("process didn't start: {}", e);
    },
}

When parent process exits, child process does not receive the signal.

Appreciated for any help.

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.