Skip to content

Commit

Permalink
fix(ext/node): remove unnecessary and incorrect type priority_t (#20276)
Browse files Browse the repository at this point in the history
`getpriority` and `setpriority` on musl libc accepts `int` / `c_int` /
`i32` as the first argument, not `u32`.

Since the `PRIO_PROCESS` constant is imported from the same crate (libc)
as the `getpriority` and `setpriority` functions, this type cast seems
to be completely unnecessary here.

It was introduced in aa8078b by
@crowlKats.

Relevant sources:

-
https://github.com/rust-lang/libc/blob/835661543db1ec42a6d9a809d69c3c5b5b978b81/src/unix/linux_like/linux/musl/mod.rs#L739-L740
- https://git.musl-libc.org/cgit/musl/tree/src/misc/setpriority.c
- https://git.musl-libc.org/cgit/musl/tree/src/misc/getpriority.c

Co-authored-by: Aapo Alasuutari <aapo.alasuutari@gmail.com>
  • Loading branch information
jirutka and aapoalas authored Sep 1, 2023
1 parent cd0fcc2 commit 3436f65
Showing 1 changed file with 2 additions and 15 deletions.
17 changes: 2 additions & 15 deletions ext/node/ops/os.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,25 +60,14 @@ mod priority {
use libc::id_t;
use libc::PRIO_PROCESS;

#[cfg(any(
target_os = "macos",
target_os = "freebsd",
target_os = "openbsd"
))]
#[allow(non_camel_case_types)]
type priority_t = i32;
#[cfg(target_os = "linux")]
#[allow(non_camel_case_types)]
type priority_t = u32;

const PRIORITY_HIGH: i32 = -14;

// Ref: https://github.com/libuv/libuv/blob/55376b044b74db40772e8a6e24d67a8673998e02/src/unix/core.c#L1533-L1547
pub fn get_priority(pid: u32) -> Result<i32, AnyError> {
set_errno(Errno(0));
match (
// SAFETY: libc::getpriority is unsafe
unsafe { libc::getpriority(PRIO_PROCESS as priority_t, pid as id_t) },
unsafe { libc::getpriority(PRIO_PROCESS, pid as id_t) },
errno(),
) {
(-1, Errno(0)) => Ok(PRIORITY_HIGH),
Expand All @@ -89,9 +78,7 @@ mod priority {

pub fn set_priority(pid: u32, priority: i32) -> Result<(), AnyError> {
// SAFETY: libc::setpriority is unsafe
match unsafe {
libc::setpriority(PRIO_PROCESS as priority_t, pid as id_t, priority)
} {
match unsafe { libc::setpriority(PRIO_PROCESS, pid as id_t, priority) } {
-1 => Err(std::io::Error::last_os_error().into()),
_ => Ok(()),
}
Expand Down

0 comments on commit 3436f65

Please sign in to comment.