2024-01-01 14:58:21 -05:00
|
|
|
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
|
2023-12-27 22:38:50 -05:00
|
|
|
|
|
|
|
use crate::NodePermissions;
|
|
|
|
use deno_core::op2;
|
|
|
|
use deno_core::OpState;
|
|
|
|
|
|
|
|
mod cpus;
|
2024-10-24 13:45:17 -04:00
|
|
|
pub mod priority;
|
|
|
|
|
|
|
|
#[derive(Debug, thiserror::Error)]
|
|
|
|
pub enum OsError {
|
|
|
|
#[error(transparent)]
|
|
|
|
Priority(priority::PriorityError),
|
|
|
|
#[error(transparent)]
|
|
|
|
Permission(deno_core::error::AnyError),
|
|
|
|
#[error("Failed to get cpu info")]
|
|
|
|
FailedToGetCpuInfo,
|
|
|
|
}
|
2023-12-27 22:38:50 -05:00
|
|
|
|
|
|
|
#[op2(fast)]
|
|
|
|
pub fn op_node_os_get_priority<P>(
|
|
|
|
state: &mut OpState,
|
|
|
|
pid: u32,
|
2024-10-24 13:45:17 -04:00
|
|
|
) -> Result<i32, OsError>
|
2023-12-27 22:38:50 -05:00
|
|
|
where
|
|
|
|
P: NodePermissions + 'static,
|
|
|
|
{
|
|
|
|
{
|
|
|
|
let permissions = state.borrow_mut::<P>();
|
2024-10-24 13:45:17 -04:00
|
|
|
permissions
|
|
|
|
.check_sys("getPriority", "node:os.getPriority()")
|
|
|
|
.map_err(OsError::Permission)?;
|
2023-12-27 22:38:50 -05:00
|
|
|
}
|
|
|
|
|
2024-10-24 13:45:17 -04:00
|
|
|
priority::get_priority(pid).map_err(OsError::Priority)
|
2023-12-27 22:38:50 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[op2(fast)]
|
|
|
|
pub fn op_node_os_set_priority<P>(
|
|
|
|
state: &mut OpState,
|
|
|
|
pid: u32,
|
|
|
|
priority: i32,
|
2024-10-24 13:45:17 -04:00
|
|
|
) -> Result<(), OsError>
|
2023-12-27 22:38:50 -05:00
|
|
|
where
|
|
|
|
P: NodePermissions + 'static,
|
|
|
|
{
|
|
|
|
{
|
|
|
|
let permissions = state.borrow_mut::<P>();
|
2024-10-24 13:45:17 -04:00
|
|
|
permissions
|
|
|
|
.check_sys("setPriority", "node:os.setPriority()")
|
|
|
|
.map_err(OsError::Permission)?;
|
2023-12-27 22:38:50 -05:00
|
|
|
}
|
|
|
|
|
2024-10-24 13:45:17 -04:00
|
|
|
priority::set_priority(pid, priority).map_err(OsError::Priority)
|
2023-12-27 22:38:50 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[op2]
|
|
|
|
#[string]
|
2024-10-24 13:45:17 -04:00
|
|
|
pub fn op_node_os_username<P>(
|
|
|
|
state: &mut OpState,
|
|
|
|
) -> Result<String, deno_core::error::AnyError>
|
2023-12-27 22:38:50 -05:00
|
|
|
where
|
|
|
|
P: NodePermissions + 'static,
|
|
|
|
{
|
|
|
|
{
|
|
|
|
let permissions = state.borrow_mut::<P>();
|
2024-07-10 09:15:43 -04:00
|
|
|
permissions.check_sys("username", "node:os.userInfo()")?;
|
2023-12-27 22:38:50 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
Ok(deno_whoami::username())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[op2(fast)]
|
2024-10-24 13:45:17 -04:00
|
|
|
pub fn op_geteuid<P>(
|
|
|
|
state: &mut OpState,
|
|
|
|
) -> Result<u32, deno_core::error::AnyError>
|
2023-12-27 22:38:50 -05:00
|
|
|
where
|
|
|
|
P: NodePermissions + 'static,
|
|
|
|
{
|
|
|
|
{
|
|
|
|
let permissions = state.borrow_mut::<P>();
|
2024-07-10 09:15:43 -04:00
|
|
|
permissions.check_sys("uid", "node:os.geteuid()")?;
|
2023-12-27 22:38:50 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(windows)]
|
|
|
|
let euid = 0;
|
|
|
|
#[cfg(unix)]
|
|
|
|
// SAFETY: Call to libc geteuid.
|
|
|
|
let euid = unsafe { libc::geteuid() };
|
|
|
|
|
|
|
|
Ok(euid)
|
|
|
|
}
|
|
|
|
|
2024-07-05 14:32:51 -04:00
|
|
|
#[op2(fast)]
|
2024-10-24 13:45:17 -04:00
|
|
|
pub fn op_getegid<P>(
|
|
|
|
state: &mut OpState,
|
|
|
|
) -> Result<u32, deno_core::error::AnyError>
|
2024-07-05 14:32:51 -04:00
|
|
|
where
|
|
|
|
P: NodePermissions + 'static,
|
|
|
|
{
|
|
|
|
{
|
|
|
|
let permissions = state.borrow_mut::<P>();
|
|
|
|
permissions.check_sys("getegid", "node:os.getegid()")?;
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(windows)]
|
|
|
|
let egid = 0;
|
|
|
|
#[cfg(unix)]
|
|
|
|
// SAFETY: Call to libc getegid.
|
|
|
|
let egid = unsafe { libc::getegid() };
|
|
|
|
|
|
|
|
Ok(egid)
|
|
|
|
}
|
|
|
|
|
2023-12-27 22:38:50 -05:00
|
|
|
#[op2]
|
|
|
|
#[serde]
|
2024-10-24 13:45:17 -04:00
|
|
|
pub fn op_cpus<P>(state: &mut OpState) -> Result<Vec<cpus::CpuInfo>, OsError>
|
2023-12-27 22:38:50 -05:00
|
|
|
where
|
|
|
|
P: NodePermissions + 'static,
|
|
|
|
{
|
|
|
|
{
|
|
|
|
let permissions = state.borrow_mut::<P>();
|
2024-10-24 13:45:17 -04:00
|
|
|
permissions
|
|
|
|
.check_sys("cpus", "node:os.cpus()")
|
|
|
|
.map_err(OsError::Permission)?;
|
2023-12-27 22:38:50 -05:00
|
|
|
}
|
|
|
|
|
2024-10-24 13:45:17 -04:00
|
|
|
cpus::cpu_info().ok_or(OsError::FailedToGetCpuInfo)
|
2023-12-27 22:38:50 -05:00
|
|
|
}
|
2024-05-16 01:22:40 -04:00
|
|
|
|
|
|
|
#[op2]
|
|
|
|
#[string]
|
2024-10-24 13:45:17 -04:00
|
|
|
pub fn op_homedir<P>(
|
|
|
|
state: &mut OpState,
|
|
|
|
) -> Result<Option<String>, deno_core::error::AnyError>
|
2024-05-16 01:22:40 -04:00
|
|
|
where
|
|
|
|
P: NodePermissions + 'static,
|
|
|
|
{
|
|
|
|
{
|
|
|
|
let permissions = state.borrow_mut::<P>();
|
|
|
|
permissions.check_sys("homedir", "node:os.homedir()")?;
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(home::home_dir().map(|path| path.to_string_lossy().to_string()))
|
|
|
|
}
|