From 22521bfa207ea3f65b9c8f28b5fe44d9b735375b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Edgar=20Andr=C3=A9s=20Margffoy=20Tuay?= Date: Tue, 11 Jan 2022 15:32:20 -0500 Subject: [PATCH] Add get_pid method to PTY struct --- src/pty.rs | 5 +++++ src/pty/base.rs | 8 ++++++++ src/pty/conpty/default_impl.rs | 4 ++++ src/pty/conpty/pty_impl.rs | 4 ++++ src/pty/winpty/default_impl.rs | 4 ++++ src/pty/winpty/pty_impl.rs | 4 ++++ tests/conpty.rs | 3 ++- tests/winpty.rs | 1 + 8 files changed, 32 insertions(+), 1 deletion(-) diff --git a/src/pty.rs b/src/pty.rs index 7103c81..3b42255 100644 --- a/src/pty.rs +++ b/src/pty.rs @@ -279,4 +279,9 @@ impl PTY { pub fn is_alive(&mut self) -> Result { self.pty.is_alive() } + + /// Retrieve the process ID (PID) of the spawned program. + pub fn get_pid(&self) -> u32 { + self.pty.get_pid() + } } diff --git a/src/pty/base.rs b/src/pty/base.rs index 62cf6ba..59d0d45 100644 --- a/src/pty/base.rs +++ b/src/pty/base.rs @@ -90,6 +90,9 @@ pub trait PTYImpl: Sync + Send { /// Determine if the process is still alive. fn is_alive(&mut self) -> Result; + + /// Retrieve the Process ID associated to the current process. + fn get_pid(&self) -> u32; } @@ -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 { diff --git a/src/pty/conpty/default_impl.rs b/src/pty/conpty/default_impl.rs index 2b2d59a..4fa29b0 100644 --- a/src/pty/conpty/default_impl.rs +++ b/src/pty/conpty/default_impl.rs @@ -38,4 +38,8 @@ impl PTYImpl for ConPTY { fn is_alive(&mut self) -> Result { Err(OsString::from("pty_rs was compiled without ConPTY enabled")) } + + fn get_pid(&self) -> u32 { + 0 + } } diff --git a/src/pty/conpty/pty_impl.rs b/src/pty/conpty/pty_impl.rs index f91c6f2..e7700be 100644 --- a/src/pty/conpty/pty_impl.rs +++ b/src/pty/conpty/pty_impl.rs @@ -356,6 +356,10 @@ impl PTYImpl for ConPTY { fn is_alive(&mut self) -> Result { self.process.is_alive() } + + fn get_pid(&self) -> u32 { + self.process.get_pid() + } } impl Drop for ConPTY { diff --git a/src/pty/winpty/default_impl.rs b/src/pty/winpty/default_impl.rs index 180d4b3..d37dd6a 100644 --- a/src/pty/winpty/default_impl.rs +++ b/src/pty/winpty/default_impl.rs @@ -36,4 +36,8 @@ impl PTYImpl for WinPTY { fn is_alive(&mut self) -> Result { Err(OsString::from("winpty_rs was compiled without WinPTY enabled")) } + + fn get_pid(&self) -> u32 { + 0 + } } diff --git a/src/pty/winpty/pty_impl.rs b/src/pty/winpty/pty_impl.rs index 2a64893..1fff992 100644 --- a/src/pty/winpty/pty_impl.rs +++ b/src/pty/winpty/pty_impl.rs @@ -250,4 +250,8 @@ impl PTYImpl for WinPTY { fn is_alive(&mut self) -> Result { self.process.is_alive() } + + fn get_pid(&self) -> u32 { + self.process.get_pid() + } } diff --git a/tests/conpty.rs b/tests/conpty.rs index caaa609..c7dbabb 100644 --- a/tests/conpty.rs +++ b/tests/conpty.rs @@ -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] diff --git a/tests/winpty.rs b/tests/winpty.rs index 63b2364..05b49b7 100644 --- a/tests/winpty.rs +++ b/tests/winpty.rs @@ -66,6 +66,7 @@ fn read_write_winpty() { } assert!(out_regex.is_match(output_str)); + assert_ne!(pty.get_pid(), 0) } #[test]