diff options
| author | bors <bors@rust-lang.org> | 2015-05-19 19:20:20 +0000 |
|---|---|---|
| committer | bors <bors@rust-lang.org> | 2015-05-19 19:20:20 +0000 |
| commit | f34ff7af7362053e8aee7a35365d6320ed6e88b8 (patch) | |
| tree | 743382e396ca1b5e133b11f313febc9421ed4780 /src/libstd/sys | |
| parent | aca207a65c82d7581076772846ef424f1a64449b (diff) | |
| parent | 1ec7a697328fb10e7135b87557ff0a5ea702dd8d (diff) | |
| download | rust-f34ff7af7362053e8aee7a35365d6320ed6e88b8.tar.gz rust-f34ff7af7362053e8aee7a35365d6320ed6e88b8.zip | |
Auto merge of #25495 - alexcrichton:process-pid, r=aturon
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.
Diffstat (limited to 'src/libstd/sys')
| -rw-r--r-- | src/libstd/sys/unix/process.rs | 4 | ||||
| -rw-r--r-- | src/libstd/sys/windows/c.rs | 1 | ||||
| -rw-r--r-- | src/libstd/sys/windows/process.rs | 6 |
3 files changed, 11 insertions, 0 deletions
diff --git a/src/libstd/sys/unix/process.rs b/src/libstd/sys/unix/process.rs index 290310f4ad9..f4bc5973040 100644 --- a/src/libstd/sys/unix/process.rs +++ b/src/libstd/sys/unix/process.rs @@ -315,6 +315,10 @@ impl Process { 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) })); diff --git a/src/libstd/sys/windows/c.rs b/src/libstd/sys/windows/c.rs index b07d063de45..e9b850856e1 100644 --- a/src/libstd/sys/windows/c.rs +++ b/src/libstd/sys/windows/c.rs @@ -482,6 +482,7 @@ extern "system" { 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")] diff --git a/src/libstd/sys/windows/process.rs b/src/libstd/sys/windows/process.rs index 032a349b00e..bc4762c197e 100644 --- a/src/libstd/sys/windows/process.rs +++ b/src/libstd/sys/windows/process.rs @@ -193,6 +193,12 @@ impl Process { 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}; |
