Skip to content

Commit

Permalink
Remove unnecessary PrctlOption enum
Browse files Browse the repository at this point in the history
  • Loading branch information
DOhlsson committed Apr 16, 2022
1 parent 589fa67 commit 09a654a
Showing 1 changed file with 16 additions and 123 deletions.
139 changes: 16 additions & 123 deletions src/sys/prctl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,113 +13,6 @@ use libc::{c_char, c_int, c_ulong};
use std::convert::TryFrom;
use std::ffi::{CStr, CString};

pub use self::PrctlOption::*;

libc_enum! {
/// Types of prctl options see [prctl(2)](https://man7.org/linux/man-pages/man2/prctl.2.html) for more information.

#[repr(i32)]
#[non_exhaustive]
#[allow(non_camel_case_types)]
pub enum PrctlOption {
/// Set the parent-death signal of the calling process to arg2
PR_SET_PDEATHSIG,
/// Read the current parent-death signal into the location pointed to by arg2
PR_GET_PDEATHSIG,
/// Return from the function the calling process's dumpable attribute
PR_GET_DUMPABLE,
/// Set the process's dumpable attribute to arg2
PR_SET_DUMPABLE,
/// Read the current access control bits into the location pointed to by arg2. PowerPC
/// only.
PR_GET_UNALIGN,
/// Set unaligned access control bits to arg2. PowerPC only.
PR_SET_UNALIGN,
/// Set the calling thread's "keep capabilities" flag. arg2 must be either 0 (clear the
/// flag) or 1 (set the flag)
PR_GET_KEEPCAPS,
/// Return from the function the calling thread's "keep capabilities" flag
PR_SET_KEEPCAPS,
/// Read floating-point exception mode into the location pointed to by arg2. PowerPC only.
PR_GET_FPEXC,
/// Set floating-point exception mode to arg2. PowerPC only.
PR_SET_FPEXC,
/// Return from the function which process timing method is currently in use
PR_GET_TIMING,
/// Set process timing method to arg2
PR_SET_TIMING,
/// Set the name of the calling thread (max 16 bytes) to the value in the location pointed
/// to by (char *) arg2
PR_SET_NAME,
/// Read the name of the calling thread into the buffer pointed to by (char *) arg2
PR_GET_NAME,
/// Read the endian-ness of the calling process into the location pointed to by arg2.
/// PowerPC only.
PR_GET_ENDIAN,
/// Set the endian-ness of the calling process to arg2. PowerPC only.
PR_SET_ENDIAN,
/// Return from the function the secure computing mode of the calling thread
PR_GET_SECCOMP,
/// Set the "securebits" flags of the calling thread to the value supplied in arg2. See
/// capabilities(7)
PR_SET_SECCOMP,
/// Return 1 from the function if the capability specified in arg2 is in the calling
/// thread's capability bounding set, or 0 if it is not
PR_CAPBSET_READ,
/// Drop the capability specified in arg2 from the calling thread's capability bounding set
PR_CAPBSET_DROP,
/// Read the state of the timestamp counter flag into the location pointed to by arg2
PR_GET_TSC,
/// Set the state of the timestamp counter flag to arg2
PR_SET_TSC,
/// Return from the function the "securebits" flags of the calling thread. See
/// capabilities(7)
PR_GET_SECUREBITS,
/// Set the "securebits" flags of the calling thread to arg2
PR_SET_SECUREBITS,
/// Set the timer slack value for the calling thread to arg2
PR_SET_TIMERSLACK,
/// Return from the function the current timer slack value of the calling thread
PR_GET_TIMERSLACK,
/// Disable all performance counters attached to the calling process
PR_TASK_PERF_EVENTS_DISABLE,
/// Enable performance counters attached to the calling process
PR_TASK_PERF_EVENTS_ENABLE,
/// Set the machine check memory corruption kill policy for the calling thread to arg2
PR_MCE_KILL,
/// Return from the function the current machine check kill policy
PR_MCE_KILL_GET,
/// Modify certain kernel memory map descriptor fields of the calling process
PR_SET_MM,
/// Set the process ID that is allowed to ptrace this process. Only relevant when Yama LSM
/// is enabled.
PR_SET_PTRACER,
/// Set the "child subreaper" attribute of the calling process to arg2
PR_SET_CHILD_SUBREAPER,
/// Read the "child subreaper" attribute of the calling process into the location pointed
/// to by arg2
PR_GET_CHILD_SUBREAPER,
/// Set the calling thread's "no new privs" attribute to the value in arg2
PR_SET_NO_NEW_PRIVS,
/// Return from the function the value of the "no new privs" attribute for the calling
/// thread
PR_GET_NO_NEW_PRIVS,
/// Read the "clear child tid" address into the location pointed to by (int **) arg2
PR_GET_TID_ADDRESS,
/// Set the state of the "THP disable" flag for the calling thread to arg2
PR_SET_THP_DISABLE,
/// return from the function the "THP disable" flag for the calling thread
PR_GET_THP_DISABLE,
/// Set the MIPS "floating-point mode" for the process to arg2. MIPS only.
PR_SET_FP_MODE,
/// Return from the function the "floatin-point mode" for the process. MIPS only.
PR_GET_FP_MODE,
/// Reads or changes the ambient capability set of the calling thread, according to the
/// value of arg2
PR_CAP_AMBIENT,
}
}

libc_enum! {
/// The type of hardware memory corruption kill policy for the thread.

Expand All @@ -137,54 +30,54 @@ libc_enum! {
impl TryFrom<i32>
}

fn prctl_set_bool(option: PrctlOption, status: bool) -> Result<()> {
let res = unsafe { libc::prctl(option as c_int, status as c_ulong, 0, 0, 0) };
fn prctl_set_bool(option: c_int, status: bool) -> Result<()> {
let res = unsafe { libc::prctl(option, status as c_ulong, 0, 0, 0) };
Errno::result(res).map(drop)
}

/// Set the "child subreaper" attribute for this process
pub fn set_child_subreaper(attribute: bool) -> Result<()> {
prctl_set_bool(PR_SET_CHILD_SUBREAPER, attribute)
prctl_set_bool(libc::PR_SET_CHILD_SUBREAPER, attribute)
}

/// Get the "child subreaper" attribute for this process
pub fn get_child_subreaper() -> Result<bool> {
// prctl writes into this var
let mut subreaper: c_int = 0;

let res = unsafe { libc::prctl(PR_GET_CHILD_SUBREAPER as c_int, &mut subreaper, 0, 0, 0) };
let res = unsafe { libc::prctl(libc::PR_GET_CHILD_SUBREAPER, &mut subreaper, 0, 0, 0) };

Errno::result(res).map(|_| subreaper != 0)
}

/// Set the dumpable attribute which determines if core dumps are created for this process.
pub fn set_dumpable(attribute: bool) -> Result<()> {
prctl_set_bool(PR_SET_DUMPABLE, attribute)
prctl_set_bool(libc::PR_SET_DUMPABLE, attribute)
}

/// Get the dumpable attribute for this process.
pub fn get_dumpable() -> Result<bool> {
let res = unsafe { libc::prctl(PR_GET_DUMPABLE as c_int, 0, 0, 0, 0) };
let res = unsafe { libc::prctl(libc::PR_GET_DUMPABLE, 0, 0, 0, 0) };

Errno::result(res).map(|res| res != 0)
}

/// Set the "keep capabilities" attribute for this process. This causes the thread to retain
/// capabilities even if it switches its UID to a nonzero value.
pub fn set_keepcaps(attribute: bool) -> Result<()> {
prctl_set_bool(PR_SET_KEEPCAPS, attribute)
prctl_set_bool(libc::PR_SET_KEEPCAPS, attribute)
}

/// Get the "keep capabilities" attribute for this process
pub fn get_keepcaps() -> Result<bool> {
let res = unsafe { libc::prctl(PR_GET_KEEPCAPS as c_int, 0, 0, 0, 0) };
let res = unsafe { libc::prctl(libc::PR_GET_KEEPCAPS, 0, 0, 0, 0) };

Errno::result(res).map(|res| res != 0)
}

/// Clear the thread memory corruption kill policy and use the system-wide default
pub fn clear_mce_kill() -> Result<()> {
let res = unsafe { libc::prctl(PR_MCE_KILL as c_int, libc::PR_MCE_KILL_CLEAR, 0, 0, 0) };
let res = unsafe { libc::prctl(libc::PR_MCE_KILL, libc::PR_MCE_KILL_CLEAR, 0, 0, 0) };

Errno::result(res).map(drop)
}
Expand All @@ -193,7 +86,7 @@ pub fn clear_mce_kill() -> Result<()> {
pub fn set_mce_kill(policy: PrctlMCEKillPolicy) -> Result<()> {
let res = unsafe {
libc::prctl(
PR_MCE_KILL as c_int,
libc::PR_MCE_KILL,
libc::PR_MCE_KILL_SET,
policy as c_ulong,
0,
Expand All @@ -206,7 +99,7 @@ pub fn set_mce_kill(policy: PrctlMCEKillPolicy) -> Result<()> {

/// Get the thread memory corruption kill policy
pub fn get_mce_kill() -> Result<PrctlMCEKillPolicy> {
let res = unsafe { libc::prctl(PR_MCE_KILL_GET as c_int, 0, 0, 0, 0) };
let res = unsafe { libc::prctl(libc::PR_MCE_KILL_GET, 0, 0, 0, 0) };

match Errno::result(res) {
Ok(val) => Ok(PrctlMCEKillPolicy::try_from(val)?),
Expand All @@ -222,7 +115,7 @@ pub fn set_pdeathsig<T: Into<Option<Signal>>>(signal: T) -> Result<()> {
None => 0,
};

let res = unsafe { libc::prctl(PR_SET_PDEATHSIG as c_int, sig, 0, 0, 0) };
let res = unsafe { libc::prctl(libc::PR_SET_PDEATHSIG, sig, 0, 0, 0) };

Errno::result(res).map(drop)
}
Expand All @@ -232,7 +125,7 @@ pub fn get_pdeathsig() -> Result<Option<Signal>> {
// prctl writes into this var
let mut sig: c_int = 0;

let res = unsafe { libc::prctl(PR_GET_PDEATHSIG as c_int, &mut sig, 0, 0, 0) };
let res = unsafe { libc::prctl(libc::PR_GET_PDEATHSIG, &mut sig, 0, 0, 0) };

match Errno::result(res) {
Ok(_) => Ok(match sig {
Expand All @@ -244,10 +137,10 @@ pub fn get_pdeathsig() -> Result<Option<Signal>> {
}

/// Set the name of the calling thread (max 15 bytes)
pub fn set_name(name: &String) -> Result<()> {
pub fn set_name(name: &str) -> Result<()> {
let name = CString::new(name.as_bytes()).unwrap();

let res = unsafe { libc::prctl(PR_SET_NAME as c_int, name.as_ptr(), 0, 0, 0) };
let res = unsafe { libc::prctl(libc::PR_SET_NAME, name.as_ptr(), 0, 0, 0) };

Errno::result(res).map(drop)
}
Expand All @@ -256,7 +149,7 @@ pub fn set_name(name: &String) -> Result<()> {
pub fn get_name() -> Result<String> {
let buf = [32u8; 16];

let res = unsafe { libc::prctl(PR_GET_NAME as c_int, &buf, 0, 0, 0) };
let res = unsafe { libc::prctl(libc::PR_GET_NAME, &buf, 0, 0, 0) };

let ptr = buf.as_ptr() as *const c_char;
let cstr = unsafe { CStr::from_ptr(ptr) };
Expand Down

0 comments on commit 09a654a

Please sign in to comment.