pty-rs icon indicating copy to clipboard operation
pty-rs copied to clipboard

Get exit status from command running on child

Open joecorcoran opened this issue 7 years ago • 1 comments

Hi there! Thanks for this crate – it's really cool.

I was wondering if there's a way of getting the exit status of whichever command runs on the forked process.

For example, here (from the README):

fn main() {
  let fork = Fork::from_ptmx().unwrap();

  if let Some(mut master) = fork.is_parent().ok() {
    let mut output = String::new();

    match master.read_to_string(&mut output) {
      Ok(_nread) => println!("child tty is: {}", output.trim()),
      Err(e)     => panic!("read error: {}", e),
    }
  }
  else {
    // Child process just exec `tty`
    let cmd  = CString::new("tty").unwrap().as_ptr();
    let args = [cmd, ptr::null()].as_mut_ptr();

    unsafe { libc::execvp(cmd, args) };
  }
}

I'd like the exit status of the tty command that runs on the child. I see that waiting for the fork to exit returns the PID, but not any exit status. Do you think this is possible?

joecorcoran avatar May 05 '17 16:05 joecorcoran

Thinking about this more, I guess it's harder than in Ruby because there is no way to raise/rescue exceptions in Rust. I had thought about doing something with waitpid and WIFEXITED but it's beyond my systems knowledge really. If anyone wants help investigating that, let me know and I'll do my best. :)

joecorcoran avatar May 08 '17 11:05 joecorcoran