/rust/registry/src/index.crates.io-1949cf8c6b5b557f/rustix-1.1.2/src/pid.rs
Line | Count | Source |
1 | | //! The `Pid` type. |
2 | | |
3 | | #![allow(unsafe_code)] |
4 | | |
5 | | use core::{fmt, num::NonZeroI32}; |
6 | | |
7 | | /// A process identifier as a raw integer. |
8 | | pub type RawPid = i32; |
9 | | |
10 | | /// `pid_t`—A non-zero Unix process ID. |
11 | | /// |
12 | | /// This is a pid, and not a pidfd. It is not a file descriptor, and the |
13 | | /// process it refers to could disappear at any time and be replaced by |
14 | | /// another, unrelated, process. |
15 | | /// |
16 | | /// On Linux, `Pid` values are also used to identify threads. |
17 | | #[repr(transparent)] |
18 | | #[derive(Copy, Clone, Eq, PartialEq, Debug, Hash)] |
19 | | pub struct Pid(NonZeroI32); |
20 | | |
21 | | impl Pid { |
22 | | /// A `Pid` corresponding to the init process (pid 1). |
23 | | pub const INIT: Self = Self(match NonZeroI32::new(1) { |
24 | | Some(n) => n, |
25 | | None => panic!("unreachable"), |
26 | | }); |
27 | | |
28 | | /// Converts a `RawPid` into a `Pid`. |
29 | | /// |
30 | | /// Returns `Some` for positive values, and `None` for zero values. |
31 | | /// |
32 | | /// This is safe because a `Pid` is a number without any guarantees for the |
33 | | /// kernel. Non-child `Pid`s are always racy for any syscalls, but can only |
34 | | /// cause logic errors. If you want race-free access to or control of |
35 | | /// non-child processes, please consider other mechanisms like [pidfd] on |
36 | | /// Linux. |
37 | | /// |
38 | | /// Passing a negative number doesn't invoke undefined behavior, but it |
39 | | /// may cause unexpected behavior. |
40 | | /// |
41 | | /// [pidfd]: https://man7.org/linux/man-pages/man2/pidfd_open.2.html |
42 | | #[inline] |
43 | 0 | pub const fn from_raw(raw: RawPid) -> Option<Self> { |
44 | 0 | debug_assert!(raw >= 0); |
45 | 0 | match NonZeroI32::new(raw) { |
46 | 0 | Some(non_zero) => Some(Self(non_zero)), |
47 | 0 | None => None, |
48 | | } |
49 | 0 | } |
50 | | |
51 | | /// Converts a known positive `RawPid` into a `Pid`. |
52 | | /// |
53 | | /// Passing a negative number doesn't invoke undefined behavior, but it |
54 | | /// may cause unexpected behavior. |
55 | | /// |
56 | | /// # Safety |
57 | | /// |
58 | | /// The caller must guarantee `raw` is non-zero. |
59 | | #[inline] |
60 | 0 | pub const unsafe fn from_raw_unchecked(raw: RawPid) -> Self { |
61 | 0 | debug_assert!(raw > 0); |
62 | 0 | Self(NonZeroI32::new_unchecked(raw)) |
63 | 0 | } |
64 | | |
65 | | /// Creates a `Pid` holding the ID of the given child process. |
66 | | #[cfg(feature = "std")] |
67 | | #[inline] |
68 | 0 | pub fn from_child(child: &std::process::Child) -> Self { |
69 | 0 | let id = child.id(); |
70 | | // SAFETY: We know the returned ID is valid because it came directly |
71 | | // from an OS API. |
72 | 0 | unsafe { Self::from_raw_unchecked(id as i32) } |
73 | 0 | } |
74 | | |
75 | | /// Converts a `Pid` into a `NonZeroI32`. |
76 | | #[inline] |
77 | 0 | pub const fn as_raw_nonzero(self) -> NonZeroI32 { |
78 | 0 | self.0 |
79 | 0 | } |
80 | | |
81 | | /// Converts a `Pid` into a `RawPid`. |
82 | | /// |
83 | | /// This is the same as `self.as_raw_nonzero().get()`. |
84 | | #[inline] |
85 | 0 | pub const fn as_raw_pid(self) -> RawPid { |
86 | 0 | self.0.get() |
87 | 0 | } |
88 | | |
89 | | /// Converts an `Option<Pid>` into a `RawPid`. |
90 | | #[inline] |
91 | 0 | pub const fn as_raw(pid: Option<Self>) -> RawPid { |
92 | 0 | match pid { |
93 | 0 | Some(pid) => pid.0.get(), |
94 | 0 | None => 0, |
95 | | } |
96 | 0 | } |
97 | | |
98 | | /// Test whether this pid represents the init process ([`Pid::INIT`]). |
99 | | #[inline] |
100 | 0 | pub const fn is_init(self) -> bool { |
101 | 0 | self.0.get() == Self::INIT.0.get() |
102 | 0 | } |
103 | | } |
104 | | |
105 | | impl fmt::Display for Pid { |
106 | 0 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
107 | 0 | self.0.fmt(f) |
108 | 0 | } |
109 | | } |
110 | | impl fmt::Binary for Pid { |
111 | 0 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
112 | 0 | self.0.fmt(f) |
113 | 0 | } |
114 | | } |
115 | | impl fmt::Octal for Pid { |
116 | 0 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
117 | 0 | self.0.fmt(f) |
118 | 0 | } |
119 | | } |
120 | | impl fmt::LowerHex for Pid { |
121 | 0 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
122 | 0 | self.0.fmt(f) |
123 | 0 | } |
124 | | } |
125 | | impl fmt::UpperHex for Pid { |
126 | 0 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
127 | 0 | self.0.fmt(f) |
128 | 0 | } |
129 | | } |
130 | | #[cfg(lower_upper_exp_for_non_zero)] |
131 | | impl fmt::LowerExp for Pid { |
132 | 0 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
133 | 0 | self.0.fmt(f) |
134 | 0 | } |
135 | | } |
136 | | #[cfg(lower_upper_exp_for_non_zero)] |
137 | | impl fmt::UpperExp for Pid { |
138 | 0 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
139 | 0 | self.0.fmt(f) |
140 | 0 | } |
141 | | } |
142 | | |
143 | | #[cfg(test)] |
144 | | mod tests { |
145 | | use super::*; |
146 | | |
147 | | #[test] |
148 | | fn test_sizes() { |
149 | | use core::mem::transmute; |
150 | | |
151 | | assert_eq_size!(RawPid, NonZeroI32); |
152 | | assert_eq_size!(RawPid, Pid); |
153 | | assert_eq_size!(RawPid, Option<Pid>); |
154 | | |
155 | | // Rustix doesn't depend on `Option<Pid>` matching the ABI of a raw integer |
156 | | // for correctness, but it should work nonetheless. |
157 | | const_assert_eq!(0 as RawPid, unsafe { |
158 | | transmute::<Option<Pid>, RawPid>(None) |
159 | | }); |
160 | | const_assert_eq!(4567 as RawPid, unsafe { |
161 | | transmute::<Option<Pid>, RawPid>(Some(Pid::from_raw_unchecked(4567))) |
162 | | }); |
163 | | } |
164 | | |
165 | | #[test] |
166 | | fn test_ctors() { |
167 | | use std::num::NonZeroI32; |
168 | | assert!(Pid::from_raw(0).is_none()); |
169 | | assert_eq!( |
170 | | Pid::from_raw(77).unwrap().as_raw_nonzero(), |
171 | | NonZeroI32::new(77).unwrap() |
172 | | ); |
173 | | assert_eq!(Pid::from_raw(77).unwrap().as_raw_pid(), 77); |
174 | | assert_eq!(Pid::as_raw(Pid::from_raw(77)), 77); |
175 | | } |
176 | | |
177 | | #[test] |
178 | | fn test_specials() { |
179 | | assert!(Pid::from_raw(1).unwrap().is_init()); |
180 | | assert_eq!(Pid::from_raw(1).unwrap(), Pid::INIT); |
181 | | } |
182 | | } |