Rollup merge of #153130 - joboet:move_getpid_sys_process, r=Mark-Simulacrum

std: move `getpid` to `sys::process`

Part of rust-lang/rust#117276.

Availability of process IDs is highly correlated with availability of processes, so moving the `getpid` implementations to `sys::process` makes a lot of sense (and removes quite some code duplication). The only notable change here is on Hermit, which doesn't have processes but does have `getpid`. But that [always returns 0](https://github.com/hermit-os/kernel/blob/ef27b798856b50d562a42c4ffd2edcb7493c5b5f/src/syscalls/tasks.rs#L21), so I doubt it is useful. If the change to a panic is problematic we could always copy the stub implementation and return zero ourselves (this also applies to the other single-process platforms).

CC @stlankes @mkroening
This commit is contained in:
Jonathan Brouwer
2026-03-01 17:43:40 +01:00
committed by GitHub
21 changed files with 32 additions and 57 deletions
+1 -1
View File
@@ -593,5 +593,5 @@ fn from(fd: OwnedFd) -> process::ChildStderr {
#[must_use]
#[stable(feature = "unix_ppid", since = "1.27.0")]
pub fn parent_id() -> u32 {
crate::sys::os::getppid()
crate::sys::process::getppid()
}
+1 -1
View File
@@ -2545,7 +2545,7 @@ pub fn abort() -> ! {
#[must_use]
#[stable(feature = "getpid", since = "1.26.0")]
pub fn id() -> u32 {
crate::sys::os::getpid()
imp::getpid()
}
/// A trait for implementing arbitrary return types in the `main` function.
-5
View File
@@ -1,4 +1,3 @@
use super::hermit_abi;
use crate::ffi::{OsStr, OsString};
use crate::marker::PhantomData;
use crate::path::{self, PathBuf};
@@ -56,7 +55,3 @@ pub fn temp_dir() -> PathBuf {
pub fn home_dir() -> Option<PathBuf> {
None
}
pub fn getpid() -> u32 {
unsafe { hermit_abi::getpid() as u32 }
}
-4
View File
@@ -62,7 +62,3 @@ pub fn temp_dir() -> PathBuf {
pub fn home_dir() -> Option<PathBuf> {
None
}
pub fn getpid() -> u32 {
panic!("Pids on Motor OS are u64.")
}
-4
View File
@@ -55,7 +55,3 @@ pub fn temp_dir() -> PathBuf {
pub fn home_dir() -> Option<PathBuf> {
None
}
pub fn getpid() -> u32 {
panic!("no pids in SGX")
}
-4
View File
@@ -62,7 +62,3 @@ pub fn temp_dir() -> PathBuf {
pub fn home_dir() -> Option<PathBuf> {
None
}
pub fn getpid() -> u32 {
panic!("no pids on this platform")
}
-4
View File
@@ -66,7 +66,3 @@ pub fn temp_dir() -> PathBuf {
pub fn home_dir() -> Option<PathBuf> {
None
}
pub fn getpid() -> u32 {
panic!("no pids on this platform")
}
-4
View File
@@ -121,7 +121,3 @@ pub fn temp_dir() -> PathBuf {
pub fn home_dir() -> Option<PathBuf> {
None
}
pub fn getpid() -> u32 {
panic!("no pids on this platform")
}
-8
View File
@@ -533,14 +533,6 @@ unsafe fn fallback() -> Option<OsString> {
}
}
pub fn getpid() -> u32 {
unsafe { libc::getpid() as u32 }
}
pub fn getppid() -> u32 {
unsafe { libc::getppid() as u32 }
}
#[cfg(all(target_os = "linux", target_env = "gnu"))]
pub fn glibc_version() -> Option<(usize, usize)> {
unsafe extern "C" {
@@ -55,7 +55,3 @@ pub fn temp_dir() -> PathBuf {
pub fn home_dir() -> Option<PathBuf> {
None
}
pub fn getpid() -> u32 {
panic!("no pids on this platform")
}
-4
View File
@@ -102,10 +102,6 @@ pub fn home_dir() -> Option<PathBuf> {
None
}
pub fn getpid() -> u32 {
panic!("unsupported");
}
#[doc(hidden)]
pub trait IsMinusOne {
fn is_minus_one(&self) -> bool;
-4
View File
@@ -188,7 +188,3 @@ pub fn home_dir() -> Option<PathBuf> {
.map(PathBuf::from)
.or_else(home_dir_crt)
}
pub fn getpid() -> u32 {
unsafe { c::GetCurrentProcessId() }
}
-4
View File
@@ -118,7 +118,3 @@ pub fn temp_dir() -> PathBuf {
pub fn home_dir() -> Option<PathBuf> {
None
}
pub fn getpid() -> u32 {
panic!("no pids on this platform")
}
-4
View File
@@ -55,7 +55,3 @@ pub fn temp_dir() -> PathBuf {
pub fn home_dir() -> Option<PathBuf> {
None
}
pub fn getpid() -> u32 {
panic!("no pids on this platform")
}
+3 -1
View File
@@ -27,9 +27,11 @@
mod env;
pub use env::CommandEnvs;
#[cfg(target_family = "unix")]
pub use imp::getppid;
pub use imp::{
ChildPipe, Command, CommandArgs, EnvKey, ExitCode, ExitStatus, ExitStatusError, Process, Stdio,
read_output,
getpid, read_output,
};
#[cfg(any(
+4
View File
@@ -327,3 +327,7 @@ pub fn read_output(
) -> io::Result<()> {
Err(io::Error::from_raw_os_error(moto_rt::E_NOT_IMPLEMENTED.into()))
}
pub fn getpid() -> u32 {
panic!("Pids on Motor OS are u64.")
}
+4
View File
@@ -900,3 +900,7 @@ fn env_changes(env: &CommandEnv) -> Option<BTreeMap<EnvKey, (Option<OsString>, O
Some(result)
}
pub fn getpid() -> u32 {
panic!("no pids on this platform")
}
@@ -679,3 +679,11 @@ fn read(fd: &FileDesc, dst: &mut Vec<u8>) -> Result<bool, io::Error> {
}
}
}
pub fn getpid() -> u32 {
unsafe { libc::getpid() as u32 }
}
pub fn getppid() -> u32 {
unsafe { libc::getppid() as u32 }
}
+3 -1
View File
@@ -23,5 +23,7 @@
pub use imp::{ExitStatus, ExitStatusError, Process};
pub use self::common::{ChildPipe, Command, CommandArgs, ExitCode, Stdio, read_output};
pub use self::common::{
ChildPipe, Command, CommandArgs, ExitCode, Stdio, getpid, getppid, read_output,
};
pub use crate::ffi::OsString as EnvKey;
@@ -327,3 +327,7 @@ pub fn read_output(
) -> io::Result<()> {
match out.diverge() {}
}
pub fn getpid() -> u32 {
panic!("no pids on this platform")
}
+4
View File
@@ -976,3 +976,7 @@ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_list().entries(self.iter.clone()).finish()
}
}
pub fn getpid() -> u32 {
unsafe { c::GetCurrentProcessId() }
}