From 6ff6b935608f7c5b6b53517f3fc5bafec911a79d Mon Sep 17 00:00:00 2001 From: Thayne McCombs Date: Thu, 5 Oct 2017 23:49:36 -0600 Subject: [PATCH 1/3] Add current_pid function Fixes #44971 --- src/libstd/process.rs | 18 ++++++++++++++++++ src/libstd/sys/redox/os.rs | 4 ++++ src/libstd/sys/unix/os.rs | 4 ++++ src/libstd/sys/windows/os.rs | 4 ++++ 4 files changed, 30 insertions(+) diff --git a/src/libstd/process.rs b/src/libstd/process.rs index 1869ad3ed707a..38035e1fa6bee 100644 --- a/src/libstd/process.rs +++ b/src/libstd/process.rs @@ -1167,6 +1167,24 @@ pub fn abort() -> ! { unsafe { ::sys::abort_internal() }; } +/// Returns the OS-assigned process identifier associated with this process. +/// +/// # Examples +/// +/// Basic usage: +/// +/// ```no_run +/// use std::process:current_pid; +/// +/// println!("My pid is {}", current_pid()); +/// ``` +/// +/// +#[unstable(feature = "getpid", issue = "44971", reason = "recently added")] +pub fn current_pid() -> u32 { + ::sys::os::getpid() +} + #[cfg(all(test, not(target_os = "emscripten")))] mod tests { use io::prelude::*; diff --git a/src/libstd/sys/redox/os.rs b/src/libstd/sys/redox/os.rs index efddd5f029484..c27e2ee172c6b 100644 --- a/src/libstd/sys/redox/os.rs +++ b/src/libstd/sys/redox/os.rs @@ -209,3 +209,7 @@ pub fn exit(code: i32) -> ! { let _ = syscall::exit(code as usize); unreachable!(); } + +pub fn getpid() -> u32 { + syscall::getpid().unwrap() as u32 +} diff --git a/src/libstd/sys/unix/os.rs b/src/libstd/sys/unix/os.rs index 5ef98d247105e..132f59b999d4f 100644 --- a/src/libstd/sys/unix/os.rs +++ b/src/libstd/sys/unix/os.rs @@ -513,3 +513,7 @@ pub fn home_dir() -> Option { pub fn exit(code: i32) -> ! { unsafe { libc::exit(code as c_int) } } + +pub fn getpid() -> u32 { + unsafe { libc::getpid() as u32 } +} diff --git a/src/libstd/sys/windows/os.rs b/src/libstd/sys/windows/os.rs index a51b458451e86..b94482435597e 100644 --- a/src/libstd/sys/windows/os.rs +++ b/src/libstd/sys/windows/os.rs @@ -318,6 +318,10 @@ pub fn exit(code: i32) -> ! { unsafe { c::ExitProcess(code as c::UINT) } } +pub fn getpid() -> u32 { + unsafe { c::GetCurrentProcessId() as u32 } +} + #[cfg(test)] mod tests { use io::Error; From ba7575886e6a294b8185f51a1701574f72abe5a8 Mon Sep 17 00:00:00 2001 From: Thayne McCombs Date: Sat, 7 Oct 2017 00:55:58 -0600 Subject: [PATCH 2/3] Add missing colon. --- src/libstd/process.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/libstd/process.rs b/src/libstd/process.rs index 38035e1fa6bee..711d8df1b605d 100644 --- a/src/libstd/process.rs +++ b/src/libstd/process.rs @@ -1174,7 +1174,7 @@ pub fn abort() -> ! { /// Basic usage: /// /// ```no_run -/// use std::process:current_pid; +/// use std::process::current_pid; /// /// println!("My pid is {}", current_pid()); /// ``` From 29b319b6b20bb8e18b7e3c14528f9933de2f5e92 Mon Sep 17 00:00:00 2001 From: Thayne McCombs Date: Sat, 7 Oct 2017 20:59:58 -0600 Subject: [PATCH 3/3] Change current_pid to just `id`. --- src/libstd/process.rs | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/libstd/process.rs b/src/libstd/process.rs index 711d8df1b605d..dee01df651d11 100644 --- a/src/libstd/process.rs +++ b/src/libstd/process.rs @@ -1174,14 +1174,15 @@ pub fn abort() -> ! { /// Basic usage: /// /// ```no_run -/// use std::process::current_pid; +/// #![feature(getpid)] +/// use std::process; /// -/// println!("My pid is {}", current_pid()); +/// println!("My pid is {}", process::id()); /// ``` /// /// #[unstable(feature = "getpid", issue = "44971", reason = "recently added")] -pub fn current_pid() -> u32 { +pub fn id() -> u32 { ::sys::os::getpid() }