c-scape 0.22.3

A libc bottom-half implementation in Rust
Documentation
use crate::convert_res;
use errno::{set_errno, Errno};
use libc::{c_int, id_t, pid_t};
use rustix::process::{Pid, Uid};

#[cfg(not(target_env = "musl"))]
use libc::__priority_which_t;
#[cfg(target_env = "musl")]
#[allow(non_camel_case_types)]
type __priority_which_t = c_int;

#[no_mangle]
unsafe extern "C" fn getpriority(which: __priority_which_t, who: id_t) -> c_int {
    libc!(libc::getpriority(which, who));

    let result = match which {
        libc::PRIO_PROCESS => rustix::process::getpriority_process(Pid::from_raw(who as pid_t)),
        libc::PRIO_PGRP => rustix::process::getpriority_pgrp(Pid::from_raw(who as pid_t)),
        libc::PRIO_USER => rustix::process::getpriority_user(Uid::from_raw(who)),
        _ => {
            set_errno(Errno(libc::EINVAL));
            return -1;
        }
    };

    match convert_res(result) {
        Some(prio) => prio,
        None => -1,
    }
}

#[no_mangle]
unsafe extern "C" fn setpriority(which: __priority_which_t, who: id_t, prio: c_int) -> c_int {
    libc!(libc::setpriority(which, who, prio));

    let result = match which {
        libc::PRIO_PROCESS => {
            rustix::process::setpriority_process(Pid::from_raw(who as pid_t), prio)
        }
        libc::PRIO_PGRP => rustix::process::setpriority_pgrp(Pid::from_raw(who as pid_t), prio),
        libc::PRIO_USER => rustix::process::setpriority_user(Uid::from_raw(who), prio),
        _ => {
            set_errno(Errno(libc::EINVAL));
            return -1;
        }
    };

    match convert_res(result) {
        Some(()) => 0,
        None => -1,
    }
}

#[no_mangle]
unsafe extern "C" fn nice(inc: c_int) -> c_int {
    libc!(libc::nice(inc));

    match convert_res(rustix::process::nice(inc)) {
        Some(new) => new,
        None => -1,
    }
}