/rust/registry/src/index.crates.io-6f17d22bba15001f/rustix-0.38.42/src/process/id.rs
Line | Count | Source (jump to first uncovered line) |
1 | | //! Unix user, group, and process identifiers. |
2 | | //! |
3 | | //! # Safety |
4 | | //! |
5 | | //! The `Uid`, `Gid`, and `Pid` types can be constructed from raw integers, |
6 | | //! which is marked unsafe because actual OS's assign special meaning to some |
7 | | //! integer values. |
8 | | #![allow(unsafe_code)] |
9 | | |
10 | | use crate::{backend, io}; |
11 | | #[cfg(feature = "alloc")] |
12 | | use alloc::vec::Vec; |
13 | | #[cfg(linux_kernel)] |
14 | | use backend::process::types::RawCpuid; |
15 | | |
16 | | pub use crate::pid::{Pid, RawPid}; |
17 | | pub use crate::ugid::{Gid, RawGid, RawUid, Uid}; |
18 | | |
19 | | /// A Linux CPU ID. |
20 | | #[cfg(linux_kernel)] |
21 | | #[repr(transparent)] |
22 | | #[derive(Copy, Clone, Eq, PartialEq, Debug, Hash)] |
23 | | pub struct Cpuid(RawCpuid); |
24 | | |
25 | | #[cfg(linux_kernel)] |
26 | | impl Cpuid { |
27 | | /// Converts a `RawCpuid` into a `Cpuid`. |
28 | | /// |
29 | | /// # Safety |
30 | | /// |
31 | | /// `raw` must be the value of a valid Linux CPU ID. |
32 | | #[inline] |
33 | 0 | pub const unsafe fn from_raw(raw: RawCpuid) -> Self { |
34 | 0 | Self(raw) |
35 | 0 | } |
36 | | |
37 | | /// Converts a `Cpuid` into a `RawCpuid`. |
38 | | #[inline] |
39 | 0 | pub const fn as_raw(self) -> RawCpuid { |
40 | 0 | self.0 |
41 | 0 | } |
42 | | } |
43 | | |
44 | | /// `getuid()`—Returns the process' real user ID. |
45 | | /// |
46 | | /// # References |
47 | | /// - [POSIX] |
48 | | /// - [Linux] |
49 | | /// |
50 | | /// [POSIX]: https://pubs.opengroup.org/onlinepubs/9799919799/functions/getuid.html |
51 | | /// [Linux]: https://man7.org/linux/man-pages/man2/getuid.2.html |
52 | | #[inline] |
53 | | #[must_use] |
54 | 0 | pub fn getuid() -> Uid { |
55 | 0 | backend::ugid::syscalls::getuid() |
56 | 0 | } |
57 | | |
58 | | /// `geteuid()`—Returns the process' effective user ID. |
59 | | /// |
60 | | /// # References |
61 | | /// - [POSIX] |
62 | | /// - [Linux] |
63 | | /// |
64 | | /// [POSIX]: https://pubs.opengroup.org/onlinepubs/9799919799/functions/geteuid.html |
65 | | /// [Linux]: https://man7.org/linux/man-pages/man2/geteuid.2.html |
66 | | #[inline] |
67 | | #[must_use] |
68 | 0 | pub fn geteuid() -> Uid { |
69 | 0 | backend::ugid::syscalls::geteuid() |
70 | 0 | } |
71 | | |
72 | | /// `getgid()`—Returns the process' real group ID. |
73 | | /// |
74 | | /// # References |
75 | | /// - [POSIX] |
76 | | /// - [Linux] |
77 | | /// |
78 | | /// [POSIX]: https://pubs.opengroup.org/onlinepubs/9799919799/functions/getgid.html |
79 | | /// [Linux]: https://man7.org/linux/man-pages/man2/getgid.2.html |
80 | | #[inline] |
81 | | #[must_use] |
82 | 0 | pub fn getgid() -> Gid { |
83 | 0 | backend::ugid::syscalls::getgid() |
84 | 0 | } |
85 | | |
86 | | /// `getegid()`—Returns the process' effective group ID. |
87 | | /// |
88 | | /// # References |
89 | | /// - [POSIX] |
90 | | /// - [Linux] |
91 | | /// |
92 | | /// [POSIX]: https://pubs.opengroup.org/onlinepubs/9799919799/functions/getegid.html |
93 | | /// [Linux]: https://man7.org/linux/man-pages/man2/getegid.2.html |
94 | | #[inline] |
95 | | #[must_use] |
96 | 0 | pub fn getegid() -> Gid { |
97 | 0 | backend::ugid::syscalls::getegid() |
98 | 0 | } |
99 | | |
100 | | /// `getpid()`—Returns the process' ID. |
101 | | /// |
102 | | /// # References |
103 | | /// - [POSIX] |
104 | | /// - [Linux] |
105 | | /// |
106 | | /// [POSIX]: https://pubs.opengroup.org/onlinepubs/9799919799/functions/getpid.html |
107 | | /// [Linux]: https://man7.org/linux/man-pages/man2/getpid.2.html |
108 | | #[inline] |
109 | | #[must_use] |
110 | 0 | pub fn getpid() -> Pid { |
111 | 0 | backend::pid::syscalls::getpid() |
112 | 0 | } |
113 | | |
114 | | /// `getppid()`—Returns the parent process' ID. |
115 | | /// |
116 | | /// This will return `None` if the current process has no parent (or no parent accessible in the |
117 | | /// current PID namespace), such as if the current process is an init process (PID 1). |
118 | | /// |
119 | | /// # References |
120 | | /// - [POSIX] |
121 | | /// - [Linux] |
122 | | /// |
123 | | /// [POSIX]: https://pubs.opengroup.org/onlinepubs/9799919799/functions/getppid.html |
124 | | /// [Linux]: https://man7.org/linux/man-pages/man2/getppid.2.html |
125 | | #[inline] |
126 | | #[must_use] |
127 | 0 | pub fn getppid() -> Option<Pid> { |
128 | 0 | backend::process::syscalls::getppid() |
129 | 0 | } |
130 | | |
131 | | /// `getpgid(pid)`—Returns the process group ID of the given process. |
132 | | /// |
133 | | /// # References |
134 | | /// - [POSIX] |
135 | | /// - [Linux] |
136 | | /// |
137 | | /// [POSIX]: https://pubs.opengroup.org/onlinepubs/9799919799/functions/getpgid.html |
138 | | /// [Linux]: https://man7.org/linux/man-pages/man2/getpgid.2.html |
139 | | #[inline] |
140 | 0 | pub fn getpgid(pid: Option<Pid>) -> io::Result<Pid> { |
141 | 0 | backend::process::syscalls::getpgid(pid) |
142 | 0 | } |
143 | | |
144 | | /// `setpgid(pid, pgid)`—Sets the process group ID of the given process. |
145 | | /// |
146 | | /// # References |
147 | | /// - [POSIX] |
148 | | /// - [Linux] |
149 | | /// |
150 | | /// [POSIX]: https://pubs.opengroup.org/onlinepubs/9799919799/functions/setpgid.html |
151 | | /// [Linux]: https://man7.org/linux/man-pages/man2/setpgid.2.html |
152 | | #[inline] |
153 | 0 | pub fn setpgid(pid: Option<Pid>, pgid: Option<Pid>) -> io::Result<()> { |
154 | 0 | backend::process::syscalls::setpgid(pid, pgid) |
155 | 0 | } |
156 | | |
157 | | /// `getpgrp()`—Returns the process' group ID. |
158 | | /// |
159 | | /// # References |
160 | | /// - [POSIX] |
161 | | /// - [Linux] |
162 | | /// |
163 | | /// [POSIX]: https://pubs.opengroup.org/onlinepubs/9799919799/functions/getpgrp.html |
164 | | /// [Linux]: https://man7.org/linux/man-pages/man2/getpgrp.2.html |
165 | | #[inline] |
166 | | #[must_use] |
167 | 0 | pub fn getpgrp() -> Pid { |
168 | 0 | backend::process::syscalls::getpgrp() |
169 | 0 | } |
170 | | |
171 | | /// `getsid(pid)`—Get the session ID of the given process. |
172 | | /// |
173 | | /// # References |
174 | | /// - [POSIX] |
175 | | /// - [Linux] |
176 | | /// |
177 | | /// [POSIX]: https://pubs.opengroup.org/onlinepubs/9799919799/functions/getsid.html |
178 | | /// [Linux]: https://man7.org/linux/man-pages/man2/getsid.2.html |
179 | | #[cfg(not(target_os = "redox"))] |
180 | | #[inline] |
181 | 0 | pub fn getsid(pid: Option<Pid>) -> io::Result<Pid> { |
182 | 0 | backend::process::syscalls::getsid(pid) |
183 | 0 | } |
184 | | |
185 | | /// `setsid()`—Create a new session. |
186 | | /// |
187 | | /// # References |
188 | | /// - [POSIX] |
189 | | /// - [Linux] |
190 | | /// |
191 | | /// [POSIX]: https://pubs.opengroup.org/onlinepubs/9799919799/functions/setsid.html |
192 | | /// [Linux]: https://man7.org/linux/man-pages/man2/setsid.2.html |
193 | | #[inline] |
194 | 0 | pub fn setsid() -> io::Result<Pid> { |
195 | 0 | backend::process::syscalls::setsid() |
196 | 0 | } |
197 | | |
198 | | /// `getgroups()`—Return a list of the current user's groups. |
199 | | /// |
200 | | /// # References |
201 | | /// - [POSIX] |
202 | | /// - [Linux] |
203 | | /// |
204 | | /// [POSIX]: https://pubs.opengroup.org/onlinepubs/9799919799/functions/getgroups.html |
205 | | /// [Linux]: https://man7.org/linux/man-pages/man2/getgroups.2.html |
206 | | #[cfg(feature = "alloc")] |
207 | 0 | pub fn getgroups() -> io::Result<Vec<Gid>> { |
208 | 0 | // This code would benefit from having a better way to read into |
209 | 0 | // uninitialized memory, but that requires `unsafe`. |
210 | 0 | let mut buffer = Vec::with_capacity(0); |
211 | 0 | let ngroups = backend::process::syscalls::getgroups(&mut buffer)?; |
212 | 0 | buffer.resize(ngroups, Gid::ROOT); |
213 | 0 | backend::process::syscalls::getgroups(&mut buffer)?; |
214 | 0 | Ok(buffer) |
215 | 0 | } |