rustix/thread/
id.rs

1//! CPU and thread identifiers.
2//!
3//! # Safety
4//!
5//! The `Cpuid`, type can be constructed from raw integers, which is marked
6//! unsafe because actual OS's assign special meaning to some integer values.
7
8#![allow(unsafe_code)]
9use crate::{backend, io};
10#[cfg(linux_kernel)]
11use backend::thread::types::RawCpuid;
12
13pub use crate::pid::{Pid, RawPid};
14pub use crate::ugid::{Gid, RawGid, RawUid, Uid};
15
16/// A Linux CPU ID.
17#[cfg(linux_kernel)]
18#[repr(transparent)]
19#[derive(Copy, Clone, Eq, PartialEq, Debug, Hash)]
20pub struct Cpuid(RawCpuid);
21
22#[cfg(linux_kernel)]
23impl Cpuid {
24    /// Converts a `RawCpuid` into a `Cpuid`.
25    ///
26    /// # Safety
27    ///
28    /// `raw` must be the value of a valid Linux CPU ID.
29    #[inline]
30    pub const unsafe fn from_raw(raw: RawCpuid) -> Self {
31        Self(raw)
32    }
33
34    /// Converts a `Cpuid` into a `RawCpuid`.
35    #[inline]
36    pub const fn as_raw(self) -> RawCpuid {
37        self.0
38    }
39}
40
41/// `gettid()`—Returns the thread ID.
42///
43/// This returns the OS thread ID, which is not necessarily the same as the
44/// Rust's `std::thread::Thread::id` or the pthread ID.
45///
46/// This function always does a system call. To avoid this overhead, ask the
47/// thread runtime for the ID instead, for example using [`libc::gettid`] or
48/// [`origin::thread::current_id`].
49///
50/// [`libc::gettid`]: https://docs.rs/libc/*/libc/fn.gettid.html
51/// [`origin::thread::current_id`]: https://docs.rs/origin/*/origin/thread/fn.current_id.html
52///
53/// # References
54///  - [Linux]
55///
56/// [Linux]: https://man7.org/linux/man-pages/man2/gettid.2.html
57#[inline]
58#[must_use]
59pub fn gettid() -> Pid {
60    backend::thread::syscalls::gettid()
61}
62
63/// `setuid(uid)`—Sets the effective user ID of the calling thread.
64///
65/// # Warning
66///
67/// This is not the `setuid` you are looking for… POSIX requires uids to be
68/// process granular, but on Linux they are per-thread. Thus, this call only
69/// changes the uid for the current *thread*, not the entire process even
70/// though that is in violation of the POSIX standard.
71///
72/// For details on this distinction, see the C library vs. kernel differences
73/// in the [manual page][linux_notes]. This call implements the kernel
74/// behavior.
75///
76/// # References
77///  - [POSIX]
78///  - [Linux]
79///
80/// [POSIX]: https://pubs.opengroup.org/onlinepubs/9799919799/functions/setuid.html
81/// [Linux]: https://man7.org/linux/man-pages/man2/setuid.2.html
82/// [linux_notes]: https://man7.org/linux/man-pages/man2/setuid.2.html#NOTES
83#[inline]
84pub fn set_thread_uid(uid: Uid) -> io::Result<()> {
85    backend::thread::syscalls::setuid_thread(uid)
86}
87
88/// `setresuid(ruid, euid, suid)`—Sets the real, effective, and saved user ID
89/// of the calling thread.
90///
91/// # Warning
92///
93/// This is not the `setresuid` you are looking for… POSIX requires uids to be
94/// process granular, but on Linux they are per-thread. Thus, this call only
95/// changes the uid for the current *thread*, not the entire process even
96/// though that is in violation of the POSIX standard.
97///
98/// For details on this distinction, see the C library vs. kernel differences
99/// in the [manual page][linux_notes] and the notes in [`set_thread_uid`]. This
100/// call implements the kernel behavior.
101///
102/// # References
103///  - [Linux]
104///
105/// [Linux]: https://man7.org/linux/man-pages/man2/setresuid.2.html
106/// [linux_notes]: https://man7.org/linux/man-pages/man2/setresuid.2.html#NOTES
107#[inline]
108pub fn set_thread_res_uid(ruid: Uid, euid: Uid, suid: Uid) -> io::Result<()> {
109    backend::thread::syscalls::setresuid_thread(ruid, euid, suid)
110}
111
112/// `setgid(gid)`—Sets the effective group ID of the current thread.
113///
114/// # Warning
115///
116/// This is not the `setgid` you are looking for… POSIX requires gids to be
117/// process granular, but on Linux they are per-thread. Thus, this call only
118/// changes the gid for the current *thread*, not the entire process even
119/// though that is in violation of the POSIX standard.
120///
121/// For details on this distinction, see the C library vs. kernel differences
122/// in the [manual page][linux_notes]. This call implements the kernel
123/// behavior.
124///
125/// # References
126///  - [POSIX]
127///  - [Linux]
128///
129/// [POSIX]: https://pubs.opengroup.org/onlinepubs/9799919799/functions/setgid.html
130/// [Linux]: https://man7.org/linux/man-pages/man2/setgid.2.html
131/// [linux_notes]: https://man7.org/linux/man-pages/man2/setgid.2.html#NOTES
132#[inline]
133pub fn set_thread_gid(gid: Gid) -> io::Result<()> {
134    backend::thread::syscalls::setgid_thread(gid)
135}
136
137/// `setresgid(rgid, egid, sgid)`—Sets the real, effective, and saved group
138/// ID of the current thread.
139///
140/// # Warning
141///
142/// This is not the `setresgid` you are looking for… POSIX requires gids to be
143/// process granular, but on Linux they are per-thread. Thus, this call only
144/// changes the gid for the current *thread*, not the entire process even
145/// though that is in violation of the POSIX standard.
146///
147/// For details on this distinction, see the C library vs. kernel differences
148/// in the [manual page][linux_notes] and the notes in [`set_thread_gid`]. This
149/// call implements the kernel behavior.
150///
151/// # References
152///  - [Linux]
153///
154/// [Linux]: https://man7.org/linux/man-pages/man2/setresgid.2.html
155/// [linux_notes]: https://man7.org/linux/man-pages/man2/setresgid.2.html#NOTES
156#[inline]
157pub fn set_thread_res_gid(rgid: Gid, egid: Gid, sgid: Gid) -> io::Result<()> {
158    backend::thread::syscalls::setresgid_thread(rgid, egid, sgid)
159}
160
161/// `setgroups(groups)`—Sets the supplementary group IDs for the calling
162/// thread.
163///
164/// # Warning
165///
166/// This is not the `setgroups` you are looking for… POSIX requires gids to be
167/// process granular, but on Linux they are per-thread. Thus, this call only
168/// changes the gids for the current *thread*, not the entire process even
169/// though that is in violation of the POSIX standard.
170///
171/// For details on this distinction, see the C library vs. kernel differences
172/// in the [manual page][linux_notes]. This call implements the kernel
173/// behavior.
174///
175/// # References
176///  - [Linux]
177///
178/// [Linux]: https://man7.org/linux/man-pages/man2/setgroups.2.html
179/// [linux_notes]: https://man7.org/linux/man-pages/man2/setgroups.2.html#NOTES
180#[cfg(linux_kernel)]
181#[inline]
182pub fn set_thread_groups(groups: &[Gid]) -> io::Result<()> {
183    backend::thread::syscalls::setgroups_thread(groups)
184}