summaryrefslogtreecommitdiffstats
path: root/vendor/rustix/src/pid.rs
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-05-30 03:57:19 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-05-30 03:57:19 +0000
commita0b8f38ab54ac451646aa00cd5e91b6c76f22a84 (patch)
treefc451898ccaf445814e26b46664d78702178101d /vendor/rustix/src/pid.rs
parentAdding debian version 1.71.1+dfsg1-2. (diff)
downloadrustc-a0b8f38ab54ac451646aa00cd5e91b6c76f22a84.tar.xz
rustc-a0b8f38ab54ac451646aa00cd5e91b6c76f22a84.zip
Merging upstream version 1.72.1+dfsg1.
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'vendor/rustix/src/pid.rs')
-rw-r--r--vendor/rustix/src/pid.rs93
1 files changed, 93 insertions, 0 deletions
diff --git a/vendor/rustix/src/pid.rs b/vendor/rustix/src/pid.rs
new file mode 100644
index 000000000..bebc1f00c
--- /dev/null
+++ b/vendor/rustix/src/pid.rs
@@ -0,0 +1,93 @@
+#![allow(unsafe_code)]
+
+use crate::backend::c;
+use core::num::NonZeroI32;
+
+/// A process identifier as a raw integer.
+pub type RawPid = c::pid_t;
+
+/// `pid_t`—A non-zero Unix process ID.
+///
+/// This is a pid, and not a pidfd. It is not a file descriptor, and the
+/// process it refers to could disappear at any time and be replaced by
+/// another, unrelated, process.
+#[repr(transparent)]
+#[derive(Copy, Clone, Eq, PartialEq, Debug, Hash)]
+pub struct Pid(NonZeroI32);
+
+impl Pid {
+ /// A `Pid` corresponding to the init process (pid 1).
+ pub const INIT: Self = Self(
+ // SAFETY: One is non-zero.
+ unsafe { NonZeroI32::new_unchecked(1) },
+ );
+
+ /// Converts a `RawPid` into a `Pid`.
+ ///
+ /// Returns `Some` for strictly positive `RawPid`s. Otherwise, returns
+ /// `None`.
+ ///
+ /// This is always safe because a `Pid` is a number without any guarantees
+ /// for the kernel. Non-child `Pid`s are always racy for any syscalls,
+ /// but can only cause logic errors. If you want race-free access or
+ /// control to non-child processes, please consider other mechanisms
+ /// like [pidfd] on Linux.
+ ///
+ /// [pidfd]: https://man7.org/linux/man-pages/man2/pidfd_open.2.html
+ #[inline]
+ pub const fn from_raw(raw: RawPid) -> Option<Self> {
+ if raw > 0 {
+ // SAFETY: raw > 0.
+ unsafe { Some(Self::from_raw_unchecked(raw)) }
+ } else {
+ None
+ }
+ }
+
+ /// Converts a known strictly positive `RawPid` into a `Pid`.
+ ///
+ /// # Safety
+ ///
+ /// The caller must guarantee `raw` is strictly positive.
+ #[inline]
+ pub const unsafe fn from_raw_unchecked(raw: RawPid) -> Self {
+ debug_assert!(raw > 0);
+ Self(NonZeroI32::new_unchecked(raw))
+ }
+
+ /// Creates a `Pid` holding the ID of the given child process.
+ #[cfg(feature = "std")]
+ #[inline]
+ pub fn from_child(child: &std::process::Child) -> Self {
+ let id = child.id();
+ // SAFETY: We know the returned ID is valid because it came directly
+ // from an OS API.
+ unsafe { Self::from_raw_unchecked(id as i32) }
+ }
+
+ /// Converts a `Pid` into a `NonZeroI32`.
+ #[inline]
+ pub const fn as_raw_nonzero(self) -> NonZeroI32 {
+ self.0
+ }
+
+ /// Converts an `Option<Pid>` into a `RawPid`.
+ #[inline]
+ pub fn as_raw(pid: Option<Self>) -> RawPid {
+ pid.map_or(0, |pid| pid.0.get())
+ }
+
+ /// Test whether this pid represents the init process (pid 1).
+ #[inline]
+ pub const fn is_init(self) -> bool {
+ self.0.get() == Self::INIT.0.get()
+ }
+}
+
+#[test]
+fn test_sizes() {
+ use core::mem::size_of;
+
+ assert_eq!(size_of::<RawPid>(), size_of::<NonZeroI32>());
+ assert_eq!(size_of::<RawPid>(), size_of::<Pid>());
+}