rustix/
prctl.rs

1//! Helper functions for `prctl` syscalls.
2
3#![allow(unsafe_code)]
4
5use crate::backend::c::{c_int, c_void};
6use crate::backend::prctl::syscalls;
7use crate::io;
8use crate::utils::as_mut_ptr;
9use bitflags::bitflags;
10use core::mem::MaybeUninit;
11use core::ptr::null_mut;
12
13bitflags! {
14    /// `PR_PAC_AP*`.
15    #[repr(transparent)]
16    #[derive(Copy, Clone, Eq, PartialEq, Hash, Debug)]
17    pub struct PointerAuthenticationKeys: u32 {
18        /// `PR_PAC_APIAKEY`—Instruction authentication key `A`.
19        const INSTRUCTION_AUTHENTICATION_KEY_A = linux_raw_sys::prctl::PR_PAC_APIAKEY;
20        /// `PR_PAC_APIBKEY`—Instruction authentication key `B`.
21        const INSTRUCTION_AUTHENTICATION_KEY_B = linux_raw_sys::prctl::PR_PAC_APIBKEY;
22        /// `PR_PAC_APDAKEY`—Data authentication key `A`.
23        const DATA_AUTHENTICATION_KEY_A = linux_raw_sys::prctl::PR_PAC_APDAKEY;
24        /// `PR_PAC_APDBKEY`—Data authentication key `B`.
25        const DATA_AUTHENTICATION_KEY_B = linux_raw_sys::prctl::PR_PAC_APDBKEY;
26        /// `PR_PAC_APGAKEY`—Generic authentication `A` key.
27        const GENERIC_AUTHENTICATION_KEY_A = linux_raw_sys::prctl::PR_PAC_APGAKEY;
28
29        /// <https://docs.rs/bitflags/*/bitflags/#externally-defined-flags>
30        const _ = !0;
31    }
32}
33
34#[inline]
35pub(crate) unsafe fn prctl_1arg(option: c_int) -> io::Result<c_int> {
36    const NULL: *mut c_void = null_mut();
37    syscalls::prctl(option, NULL, NULL, NULL, NULL)
38}
39
40#[inline]
41pub(crate) unsafe fn prctl_2args(option: c_int, arg2: *mut c_void) -> io::Result<c_int> {
42    const NULL: *mut c_void = null_mut();
43    syscalls::prctl(option, arg2, NULL, NULL, NULL)
44}
45
46#[inline]
47pub(crate) unsafe fn prctl_3args(
48    option: c_int,
49    arg2: *mut c_void,
50    arg3: *mut c_void,
51) -> io::Result<c_int> {
52    syscalls::prctl(option, arg2, arg3, null_mut(), null_mut())
53}
54
55#[inline]
56pub(crate) unsafe fn prctl_get_at_arg2_optional<P>(option: i32) -> io::Result<P> {
57    let mut value: MaybeUninit<P> = MaybeUninit::uninit();
58    prctl_2args(option, value.as_mut_ptr().cast())?;
59    Ok(value.assume_init())
60}
61
62#[inline]
63pub(crate) unsafe fn prctl_get_at_arg2<P, T>(option: i32) -> io::Result<T>
64where
65    P: Default,
66    T: TryFrom<P, Error = io::Errno>,
67{
68    let mut value: P = Default::default();
69    prctl_2args(option, as_mut_ptr(&mut value).cast())?;
70    TryFrom::try_from(value)
71}