Skip to content

Commit f34ff7a

Browse files
committed
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.
2 parents aca207a + 1ec7a69 commit f34ff7a

File tree

4 files changed

+17
-0
lines changed

4 files changed

+17
-0
lines changed

src/libstd/process.rs

+6
Original file line numberDiff line numberDiff line change
@@ -456,6 +456,12 @@ impl Child {
456456
unsafe { self.handle.kill() }
457457
}
458458

459+
/// Returns the OS-assigned process identifier associated with this child.
460+
#[unstable(feature = "process_id", reason = "api recently added")]
461+
pub fn id(&self) -> u32 {
462+
self.handle.id()
463+
}
464+
459465
/// Waits for the child to exit completely, returning the status that it
460466
/// exited with. This function will continue to have the same return value
461467
/// after it has been called at least once.

src/libstd/sys/unix/process.rs

+4
Original file line numberDiff line numberDiff line change
@@ -315,6 +315,10 @@ impl Process {
315315
fail(&mut output)
316316
}
317317

318+
pub fn id(&self) -> u32 {
319+
self.pid as u32
320+
}
321+
318322
pub fn wait(&self) -> io::Result<ExitStatus> {
319323
let mut status = 0 as c_int;
320324
try!(cvt_r(|| unsafe { c::waitpid(self.pid, &mut status, 0) }));

src/libstd/sys/windows/c.rs

+1
Original file line numberDiff line numberDiff line change
@@ -482,6 +482,7 @@ extern "system" {
482482
dwMilliseconds: libc::DWORD) -> libc::DWORD;
483483
pub fn SwitchToThread() -> libc::BOOL;
484484
pub fn Sleep(dwMilliseconds: libc::DWORD);
485+
pub fn GetProcessId(handle: libc::HANDLE) -> libc::DWORD;
485486
}
486487

487488
#[link(name = "userenv")]

src/libstd/sys/windows/process.rs

+6
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,12 @@ impl Process {
193193
Ok(())
194194
}
195195

196+
pub fn id(&self) -> u32 {
197+
unsafe {
198+
c::GetProcessId(self.handle.raw()) as u32
199+
}
200+
}
201+
196202
pub fn wait(&self) -> io::Result<ExitStatus> {
197203
use libc::{STILL_ACTIVE, INFINITE, WAIT_OBJECT_0};
198204
use libc::{GetExitCodeProcess, WaitForSingleObject};

0 commit comments

Comments
 (0)