-
Notifications
You must be signed in to change notification settings - Fork 5.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(node/os): implement getPriority, setPriority & userInfo #19370
Merged
Merged
Changes from 13 commits
Commits
Show all changes
23 commits
Select commit
Hold shift + click to select a range
cece9e1
feat(node/os): implement getPriority, setPriority & userInfo
crowlKats f75234a
lint
crowlKats 4660c03
update lock
crowlKats be42bc9
fix & add permissions
crowlKats 380f8bd
fix safety comment
crowlKats bb2530a
Merge branch 'main' into node_os
crowlKats b8ef123
dedupe errno
crowlKats 349fe5b
add priority test
crowlKats cc1fc05
disable test and add error test
crowlKats 1190da5
Merge branch 'main' into node_os
crowlKats d83ba4c
fix test
crowlKats 2e8273f
adjust test
crowlKats 17a240e
throw on no homedir
crowlKats cc3ea28
fmt
crowlKats 60cc450
Merge branch 'main' into node_os
bartlomieju 7ba7145
await status in test
bartlomieju ae2ee0e
add TODO
crowlKats a42147d
Merge branch 'main' into node_os
crowlKats 7e9c2be
address comments
crowlKats 23872d8
Merge branch 'main' into node_os
crowlKats c3fae93
fix build
crowlKats 0136b99
fix build
crowlKats 474a255
Merge branch 'main' into node_os
crowlKats File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,196 @@ | ||
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license. | ||
|
||
use deno_core::error::AnyError; | ||
use deno_core::op; | ||
use deno_core::OpState; | ||
use errno::errno; | ||
use errno::set_errno; | ||
use errno::Errno; | ||
|
||
#[op] | ||
pub fn op_node_os_get_priority<P>( | ||
state: &mut OpState, | ||
pid: u32, | ||
) -> Result<i32, AnyError> | ||
where | ||
P: NodePermissions + 'static, | ||
{ | ||
{ | ||
let permissions = state.borrow_mut::<P>(); | ||
permissions.check_sys("getPriority", "node:os.getPriority()")?; | ||
} | ||
|
||
get_priority(pid) | ||
} | ||
|
||
#[op] | ||
pub fn op_node_os_set_priority<P>( | ||
state: &mut OpState, | ||
pid: u32, | ||
priority: i32, | ||
) -> Result<(), AnyError> | ||
where | ||
P: NodePermissions + 'static, | ||
{ | ||
{ | ||
let permissions = state.borrow_mut::<P>(); | ||
permissions.check_sys("setPriority", "node:os.setPriority()")?; | ||
} | ||
|
||
set_priority(pid, priority) | ||
} | ||
|
||
#[op] | ||
pub fn op_node_os_username<P>(state: &mut OpState) -> Result<String, AnyError> | ||
where | ||
P: NodePermissions + 'static, | ||
{ | ||
{ | ||
let permissions = state.borrow_mut::<P>(); | ||
permissions.check_sys("userInfo", "node:os.userInfo()")?; | ||
} | ||
|
||
Ok(whoami::username()) | ||
} | ||
|
||
use crate::NodePermissions; | ||
#[cfg(unix)] | ||
crowlKats marked this conversation as resolved.
Show resolved
Hide resolved
|
||
use libc::id_t; | ||
#[cfg(unix)] | ||
use libc::PRIO_PROCESS; | ||
#[cfg(windows)] | ||
use winapi::shared::minwindef::DWORD; | ||
#[cfg(windows)] | ||
use winapi::shared::minwindef::FALSE; | ||
#[cfg(windows)] | ||
use winapi::shared::ntdef::NULL; | ||
#[cfg(windows)] | ||
use winapi::um::handleapi::CloseHandle; | ||
#[cfg(windows)] | ||
use winapi::um::processthreadsapi::GetCurrentProcess; | ||
#[cfg(windows)] | ||
use winapi::um::processthreadsapi::GetPriorityClass; | ||
#[cfg(windows)] | ||
use winapi::um::processthreadsapi::OpenProcess; | ||
#[cfg(windows)] | ||
use winapi::um::processthreadsapi::SetPriorityClass; | ||
#[cfg(windows)] | ||
use winapi::um::winbase::ABOVE_NORMAL_PRIORITY_CLASS; | ||
#[cfg(windows)] | ||
use winapi::um::winbase::BELOW_NORMAL_PRIORITY_CLASS; | ||
#[cfg(windows)] | ||
use winapi::um::winbase::HIGH_PRIORITY_CLASS; | ||
#[cfg(windows)] | ||
use winapi::um::winbase::IDLE_PRIORITY_CLASS; | ||
#[cfg(windows)] | ||
use winapi::um::winbase::NORMAL_PRIORITY_CLASS; | ||
#[cfg(windows)] | ||
use winapi::um::winbase::REALTIME_PRIORITY_CLASS; | ||
#[cfg(windows)] | ||
use winapi::um::winnt::PROCESS_QUERY_LIMITED_INFORMATION; | ||
|
||
#[cfg(target_os = "macos")] | ||
#[allow(non_camel_case_types)] | ||
type priority_t = i32; | ||
#[cfg(target_os = "linux")] | ||
#[allow(non_camel_case_types)] | ||
type priority_t = u32; | ||
|
||
#[cfg(windows)] | ||
pub const PRIORITY_LOW: i32 = 19; | ||
crowlKats marked this conversation as resolved.
Show resolved
Hide resolved
|
||
#[cfg(windows)] | ||
pub const PRIORITY_BELOW_NORMAL: i32 = 10; | ||
#[cfg(windows)] | ||
pub const PRIORITY_NORMAL: i32 = 0; | ||
#[cfg(windows)] | ||
pub const PRIORITY_ABOVE_NORMAL: i32 = -7; | ||
pub const PRIORITY_HIGH: i32 = -14; | ||
crowlKats marked this conversation as resolved.
Show resolved
Hide resolved
|
||
#[cfg(windows)] | ||
pub const PRIORITY_HIGHEST: i32 = -20; | ||
|
||
#[cfg(unix)] | ||
pub fn get_priority(pid: u32) -> Result<i32, AnyError> { | ||
set_errno(Errno(0)); | ||
match ( | ||
// SAFETY: libc::getpriority is unsafe | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nitpick: Not much of a safety comment :) |
||
unsafe { libc::getpriority(PRIO_PROCESS as priority_t, pid as id_t) }, | ||
errno(), | ||
) { | ||
(-1, Errno(0)) => Ok(PRIORITY_HIGH), | ||
(-1, _) => Err(std::io::Error::last_os_error().into()), | ||
(priority, _) => Ok(priority), | ||
} | ||
} | ||
|
||
#[cfg(unix)] | ||
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) | ||
} { | ||
-1 => Err(std::io::Error::last_os_error().into()), | ||
_ => Ok(()), | ||
} | ||
} | ||
|
||
#[cfg(windows)] | ||
pub fn get_priority(pid: u32) -> Result<i32, AnyError> { | ||
unsafe { | ||
crowlKats marked this conversation as resolved.
Show resolved
Hide resolved
|
||
let handle = if pid == 0 { | ||
GetCurrentProcess() | ||
} else { | ||
OpenProcess(PROCESS_QUERY_LIMITED_INFORMATION, FALSE, pid as DWORD) | ||
}; | ||
if handle == NULL { | ||
Err(std::io::Error::last_os_error().into()) | ||
} else { | ||
let result = match GetPriorityClass(handle) { | ||
0 => Err(std::io::Error::last_os_error().into()), | ||
REALTIME_PRIORITY_CLASS => Ok(PRIORITY_HIGHEST), | ||
HIGH_PRIORITY_CLASS => Ok(PRIORITY_HIGH), | ||
ABOVE_NORMAL_PRIORITY_CLASS => Ok(PRIORITY_ABOVE_NORMAL), | ||
NORMAL_PRIORITY_CLASS => Ok(PRIORITY_NORMAL), | ||
BELOW_NORMAL_PRIORITY_CLASS => Ok(PRIORITY_BELOW_NORMAL), | ||
IDLE_PRIORITY_CLASS => Ok(PRIORITY_LOW), | ||
_ => Ok(PRIORITY_LOW), | ||
}; | ||
CloseHandle(handle); | ||
result | ||
} | ||
} | ||
} | ||
|
||
#[cfg(windows)] | ||
pub fn set_priority(pid: u32, priority: i32) -> Result<(), AnyError> { | ||
unsafe { | ||
crowlKats marked this conversation as resolved.
Show resolved
Hide resolved
|
||
let handle = if pid == 0 { | ||
GetCurrentProcess() | ||
} else { | ||
OpenProcess(PROCESS_QUERY_LIMITED_INFORMATION, FALSE, pid as DWORD) | ||
}; | ||
if handle == NULL { | ||
Err(std::io::Error::last_os_error().into()) | ||
} else { | ||
let prio_class = match priority { | ||
p if p <= PRIORITY_HIGHEST => REALTIME_PRIORITY_CLASS, | ||
p if PRIORITY_HIGHEST < p && p <= PRIORITY_HIGH => HIGH_PRIORITY_CLASS, | ||
crowlKats marked this conversation as resolved.
Show resolved
Hide resolved
|
||
p if PRIORITY_HIGH < p && p <= PRIORITY_ABOVE_NORMAL => { | ||
ABOVE_NORMAL_PRIORITY_CLASS | ||
} | ||
p if PRIORITY_ABOVE_NORMAL < p && p <= PRIORITY_NORMAL => { | ||
NORMAL_PRIORITY_CLASS | ||
} | ||
p if PRIORITY_NORMAL < p && p <= PRIORITY_BELOW_NORMAL => { | ||
BELOW_NORMAL_PRIORITY_CLASS | ||
} | ||
_ => IDLE_PRIORITY_CLASS, | ||
}; | ||
let result = match SetPriorityClass(handle, prio_class) { | ||
FALSE => Err(std::io::Error::last_os_error().into()), | ||
_ => Ok(()), | ||
}; | ||
CloseHandle(handle); | ||
result | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
suggestion: Just to be extra cool maybe only disable this if user ID is non-zero? ie. When not being run as sudo.