]> git.lizzy.rs Git - rust.git/commitdiff
std: Add an unstable method Child::id
authorAlex Crichton <alex@alexcrichton.com>
Thu, 16 Apr 2015 16:44:05 +0000 (09:44 -0700)
committerAlex Crichton <alex@alexcrichton.com>
Sat, 16 May 2015 18:13:38 +0000 (11:13 -0700)
This commits adds a method to the `std::process` module to get the process
identifier of the child as a `u32`. On Windows the underlying identifier is
already a `u32`, and on Unix the type is typically defined as `c_int` (`i32` for
almost all our supported platforms), but the actually pid is normally a small
positive number.

Eventually we may add functions to load information about a process based on its
identifier or the ability to terminate a process based on its identifier, but
for now this function should enable this sort of functionality to exist outside
the standard library.

src/libstd/process.rs
src/libstd/sys/unix/process.rs
src/libstd/sys/windows/c.rs
src/libstd/sys/windows/process.rs

index 61398e16ba03613e4063433b155e1fb97d41a081..ae9316ddd622b1d3a6fb90a1623cefd89fc49a35 100644 (file)
@@ -456,6 +456,12 @@ pub fn kill(&mut self) -> io::Result<()> {
         unsafe { self.handle.kill() }
     }
 
+    /// Returns the OS-assigned process identifier associated with this child.
+    #[unstable(feature = "process_id", reason = "api recently added")]
+    pub fn id(&self) -> u32 {
+        self.handle.id()
+    }
+
     /// Waits for the child to exit completely, returning the status that it
     /// exited with. This function will continue to have the same return value
     /// after it has been called at least once.
index 290310f4ad90181738f44866979318ad32ee3bdb..f4bc597304097c85b91ca0d17a807e042da92391 100644 (file)
@@ -315,6 +315,10 @@ fn fail(output: &mut AnonPipe) -> ! {
         fail(&mut output)
     }
 
+    pub fn id(&self) -> u32 {
+        self.pid as u32
+    }
+
     pub fn wait(&self) -> io::Result<ExitStatus> {
         let mut status = 0 as c_int;
         try!(cvt_r(|| unsafe { c::waitpid(self.pid, &mut status, 0) }));
index b07d063de45c992630cd76c61459b1c08ac7823e..e9b850856e1f86c79b4af6d139c833bb6aa2d913 100644 (file)
@@ -482,6 +482,7 @@ pub fn WaitForSingleObject(hHandle: libc::HANDLE,
                                dwMilliseconds: libc::DWORD) -> libc::DWORD;
     pub fn SwitchToThread() -> libc::BOOL;
     pub fn Sleep(dwMilliseconds: libc::DWORD);
+    pub fn GetProcessId(handle: libc::HANDLE) -> libc::DWORD;
 }
 
 #[link(name = "userenv")]
index 032a349b00eff11e211f6a6a6da2e7f3766d3003..bc4762c197e1429eb3fa60133a5ade26bdcd84ba 100644 (file)
@@ -193,6 +193,12 @@ pub unsafe fn kill(&self) -> io::Result<()> {
         Ok(())
     }
 
+    pub fn id(&self) -> u32 {
+        unsafe {
+            c::GetProcessId(self.handle.raw()) as u32
+        }
+    }
+
     pub fn wait(&self) -> io::Result<ExitStatus> {
         use libc::{STILL_ACTIVE, INFINITE, WAIT_OBJECT_0};
         use libc::{GetExitCodeProcess, WaitForSingleObject};