Debian Codegraph @

third_party/rust/rustix/src/process/id.rs

raw
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 pub const unsafe fn from_raw(raw: RawCpuid) -> Self {
34 Self(raw)
35 }
36
37 /// Converts a `Cpuid` into a `RawCpuid`.
38 #[inline]
39 pub const fn as_raw(self) -> RawCpuid {
40 self.0
41 }
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 pub fn getuid() -> Uid {
55 backend::ugid::syscalls::getuid()
56 }
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 pub fn geteuid() -> Uid {
69 backend::ugid::syscalls::geteuid()
70 }
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 pub fn getgid() -> Gid {
83 backend::ugid::syscalls::getgid()
84 }
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 pub fn getegid() -> Gid {
97 backend::ugid::syscalls::getegid()
98 }
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 pub fn getpid() -> Pid {
111 backend::pid::syscalls::getpid()
112 }
113
114 /// `getppid()`—Returns the parent process' ID.
115 ///
116 /// # References
117 /// - [POSIX]
118 /// - [Linux]
119 ///
120 /// [POSIX]: https://pubs.opengroup.org/onlinepubs/9799919799/functions/getppid.html
121 /// [Linux]: https://man7.org/linux/man-pages/man2/getppid.2.html
122 #[inline]
123 #[must_use]
124 pub fn getppid() -> Option<Pid> {
125 backend::process::syscalls::getppid()
126 }
127
128 /// `getpgid(pid)`—Returns the process group ID of the given process.
129 ///
130 /// # References
131 /// - [POSIX]
132 /// - [Linux]
133 ///
134 /// [POSIX]: https://pubs.opengroup.org/onlinepubs/9799919799/functions/getpgid.html
135 /// [Linux]: https://man7.org/linux/man-pages/man2/getpgid.2.html
136 #[inline]
137 pub fn getpgid(pid: Option<Pid>) -> io::Result<Pid> {
138 backend::process::syscalls::getpgid(pid)
139 }
140
141 /// `setpgid(pid, pgid)`—Sets the process group ID of the given process.
142 ///
143 /// # References
144 /// - [POSIX]
145 /// - [Linux]
146 ///
147 /// [POSIX]: https://pubs.opengroup.org/onlinepubs/9799919799/functions/setpgid.html
148 /// [Linux]: https://man7.org/linux/man-pages/man2/setpgid.2.html
149 #[inline]
150 pub fn setpgid(pid: Option<Pid>, pgid: Option<Pid>) -> io::Result<()> {
151 backend::process::syscalls::setpgid(pid, pgid)
152 }
153
154 /// `getpgrp()`—Returns the process' group ID.
155 ///
156 /// # References
157 /// - [POSIX]
158 /// - [Linux]
159 ///
160 /// [POSIX]: https://pubs.opengroup.org/onlinepubs/9799919799/functions/getpgrp.html
161 /// [Linux]: https://man7.org/linux/man-pages/man2/getpgrp.2.html
162 #[inline]
163 #[must_use]
164 pub fn getpgrp() -> Pid {
165 backend::process::syscalls::getpgrp()
166 }
167
168 /// `getsid(pid)`—Get the session ID of the given process.
169 ///
170 /// # References
171 /// - [POSIX]
172 /// - [Linux]
173 ///
174 /// [POSIX]: https://pubs.opengroup.org/onlinepubs/9799919799/functions/getsid.html
175 /// [Linux]: https://man7.org/linux/man-pages/man2/getsid.2.html
176 #[cfg(not(target_os = "redox"))]
177 #[inline]
178 pub fn getsid(pid: Option<Pid>) -> io::Result<Pid> {
179 backend::process::syscalls::getsid(pid)
180 }
181
182 /// `setsid()`—Create a new session.
183 ///
184 /// # References
185 /// - [POSIX]
186 /// - [Linux]
187 ///
188 /// [POSIX]: https://pubs.opengroup.org/onlinepubs/9799919799/functions/setsid.html
189 /// [Linux]: https://man7.org/linux/man-pages/man2/setsid.2.html
190 #[inline]
191 pub fn setsid() -> io::Result<Pid> {
192 backend::process::syscalls::setsid()
193 }
194
195 /// `getgroups()`—Return a list of the current user's groups.
196 ///
197 /// # References
198 /// - [POSIX]
199 /// - [Linux]
200 ///
201 /// [POSIX]: https://pubs.opengroup.org/onlinepubs/9799919799/functions/getgroups.html
202 /// [Linux]: https://man7.org/linux/man-pages/man2/getgroups.2.html
203 #[cfg(feature = "alloc")]
204 pub fn getgroups() -> io::Result<Vec<Gid>> {
205 // This code would benefit from having a better way to read into
206 // uninitialized memory, but that requires `unsafe`.
207 let mut buffer = Vec::with_capacity(0);
208 let ngroups = backend::process::syscalls::getgroups(&mut buffer)?;
209 buffer.resize(ngroups, Gid::ROOT);
210 backend::process::syscalls::getgroups(&mut buffer)?;
211 Ok(buffer)
212 }