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

PR: Add get_pid method to PTY struct #6

Merged
merged 1 commit into from
Jan 11, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/pty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -279,4 +279,9 @@ impl PTY {
pub fn is_alive(&mut self) -> Result<bool, OsString> {
self.pty.is_alive()
}

/// Retrieve the process ID (PID) of the spawned program.
pub fn get_pid(&self) -> u32 {
self.pty.get_pid()
}
}
8 changes: 8 additions & 0 deletions src/pty/base.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,9 @@ pub trait PTYImpl: Sync + Send {

/// Determine if the process is still alive.
fn is_alive(&mut self) -> Result<bool, OsString>;

/// Retrieve the Process ID associated to the current process.
fn get_pid(&self) -> u32;
}


Expand Down Expand Up @@ -392,6 +395,11 @@ impl PTYProcess {
}
}

/// Retrieve the Process ID associated to the current process.
pub fn get_pid(&self) -> u32 {
self.pid
}

}

impl Drop for PTYProcess {
Expand Down
4 changes: 4 additions & 0 deletions src/pty/conpty/default_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,8 @@ impl PTYImpl for ConPTY {
fn is_alive(&mut self) -> Result<bool, OsString> {
Err(OsString::from("pty_rs was compiled without ConPTY enabled"))
}

fn get_pid(&self) -> u32 {
0
}
}
4 changes: 4 additions & 0 deletions src/pty/conpty/pty_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -356,6 +356,10 @@ impl PTYImpl for ConPTY {
fn is_alive(&mut self) -> Result<bool, OsString> {
self.process.is_alive()
}

fn get_pid(&self) -> u32 {
self.process.get_pid()
}
}

impl Drop for ConPTY {
Expand Down
4 changes: 4 additions & 0 deletions src/pty/winpty/default_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,8 @@ impl PTYImpl for WinPTY {
fn is_alive(&mut self) -> Result<bool, OsString> {
Err(OsString::from("winpty_rs was compiled without WinPTY enabled"))
}

fn get_pid(&self) -> u32 {
0
}
}
4 changes: 4 additions & 0 deletions src/pty/winpty/pty_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -250,4 +250,8 @@ impl PTYImpl for WinPTY {
fn is_alive(&mut self) -> Result<bool, OsString> {
self.process.is_alive()
}

fn get_pid(&self) -> u32 {
self.process.get_pid()
}
}
3 changes: 2 additions & 1 deletion tests/conpty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,8 @@ fn read_write_conpty() {
}

println!("!!!!!!!!!!!!!!!!!");
assert!(out_regex.is_match(output_str))
assert!(out_regex.is_match(output_str));
assert_ne!(pty.get_pid(), 0)
}

#[test]
Expand Down
1 change: 1 addition & 0 deletions tests/winpty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ fn read_write_winpty() {
}

assert!(out_regex.is_match(output_str));
assert_ne!(pty.get_pid(), 0)
}

#[test]
Expand Down