//! Bindings for the FreeBSD `procctl` system call.
//!
//! There are similarities (but also differences) with Linux's `prctl` system call, whose interface
//! is located in the `prctl.rs` file.
#![allow(unsafe_code)]
use core::mem::MaybeUninit;
use crate::backend::c::{c_int, c_uint, c_void};
use crate::backend::process::syscalls;
use crate::backend::process::types::{RawId, Signal};
use crate::io;
use crate::process::{Pid, RawPid};
//
// Helper functions.
//
/// Subset of `idtype_t` C enum, with only the values allowed by `procctl`.
#[repr(i32)]
pub enum IdType {
/// Process id.
Pid = 0,
/// Process group id.
Pgid = 2,
}
/// A process selector for use with the `procctl` interface.
///
/// `None` represents the current process. `Some((IdType::Pid, pid))` represents the process
/// with pid `pid`. `Some((IdType::Pgid, pgid))` represents the control processes belonging to
/// the process group with id `pgid`.
pub type ProcSelector = Option<(IdType, Pid)>;
fn proc_selector_to_raw(selector: ProcSelector) -> (IdType, RawPid) {
match selector {
Some((idtype, id)) => (idtype, id.as_raw_nonzero().get()),
None => (IdType::Pid, 0),
}
}
#[inline]
pub(crate) unsafe fn procctl(
option: c_int,
process: ProcSelector,
data: *mut c_void,
) -> io::Result<()> {
let (idtype, id) = proc_selector_to_raw(process);
syscalls::procctl(idtype as c_uint, id as RawId, option, data)
}
#[inline]
pub(crate) unsafe fn procctl_set
(
option: c_int,
process: ProcSelector,
data: &P,
) -> io::Result<()> {
procctl(option, process, (data as *const P as *mut P).cast())
}
#[inline]
pub(crate) unsafe fn procctl_get_optional