Skip to content

Commit

Permalink
renice: handle macos/non-macos priority arg types
Browse files Browse the repository at this point in the history
  • Loading branch information
jgarzik committed Mar 3, 2024
1 parent 28e969e commit 195290e
Showing 1 changed file with 16 additions and 8 deletions.
24 changes: 16 additions & 8 deletions process/src/renice.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,12 @@ fn lookup_uid(username: &str) -> Result<u32, &'static str> {
Ok(passwd.pw_uid)
}

fn parse_id(which: i32, input: &str) -> Result<u32, &'static str> {
fn parse_id(which: u32, input: &str) -> Result<u32, &'static str> {
match input.parse::<u32>() {
Ok(0) => Err("Invalid ID"),
Ok(n) => Ok(n),
Err(e) => {
if which != libc::PRIO_USER {
if which != libc::PRIO_USER as u32 {
eprintln!("{}", e);
Err("Invalid ID")
} else {
Expand All @@ -79,11 +79,15 @@ fn parse_id(which: i32, input: &str) -> Result<u32, &'static str> {
}
}

fn xgetpriority(which: i32, id: u32) -> io::Result<i32> {
fn xgetpriority(which: u32, id: u32) -> io::Result<i32> {
set_errno(errno::Errno(0));

#[cfg(not(target_os = "macos"))]
let res = unsafe { libc::getpriority(which, id) };

#[cfg(target_os = "macos")]
let res = unsafe { libc::getpriority(which as i32, id) };

let errno_res = errno().0;
if errno_res == 0 {
Ok(res)
Expand All @@ -94,9 +98,13 @@ fn xgetpriority(which: i32, id: u32) -> io::Result<i32> {
}
}

fn xsetpriority(which: i32, id: u32, prio: i32) -> io::Result<()> {
fn xsetpriority(which: u32, id: u32, prio: i32) -> io::Result<()> {
#[cfg(not(target_os = "macos"))]
let res = unsafe { libc::setpriority(which, id, prio) };

#[cfg(target_os = "macos")]
let res = unsafe { libc::setpriority(which as i32, id, prio) };

if res < 0 {
let e = io::Error::from_raw_os_error(res);
eprintln!("setpriority: {}", e);
Expand All @@ -114,13 +122,13 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
bind_textdomain_codeset(PROJECT_NAME, "UTF-8")?;

// which class of priority to modify
let which = {
let which: u32 = {
if args.pgrp {
libc::PRIO_PGRP
libc::PRIO_PGRP as u32
} else if args.user {
libc::PRIO_USER
libc::PRIO_USER as u32
} else {
libc::PRIO_PROCESS
libc::PRIO_PROCESS as u32
}
};

Expand Down

0 comments on commit 195290e

Please sign in to comment.