2024-01-01 14:58:21 -05:00
|
|
|
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
|
2020-09-05 20:34:02 -04:00
|
|
|
|
2021-04-06 04:33:43 -04:00
|
|
|
use super::utils::into_string;
|
2022-06-08 11:45:38 -04:00
|
|
|
use crate::worker::ExitCode;
|
2023-01-14 23:18:58 -05:00
|
|
|
use deno_core::error::type_error;
|
|
|
|
use deno_core::error::AnyError;
|
2023-09-11 18:10:43 -04:00
|
|
|
use deno_core::op2;
|
2020-09-16 14:28:07 -04:00
|
|
|
use deno_core::url::Url;
|
2022-12-17 17:25:51 -05:00
|
|
|
use deno_core::v8;
|
2020-09-10 09:57:45 -04:00
|
|
|
use deno_core::OpState;
|
2022-09-14 11:59:20 -04:00
|
|
|
use deno_node::NODE_ENV_VAR_ALLOWLIST;
|
2024-06-06 23:37:53 -04:00
|
|
|
use deno_permissions::PermissionsContainer;
|
2022-12-17 17:25:51 -05:00
|
|
|
use serde::Serialize;
|
2019-08-23 01:30:14 -04:00
|
|
|
use std::collections::HashMap;
|
|
|
|
use std::env;
|
2019-08-14 11:03:02 -04:00
|
|
|
|
2022-11-02 03:17:00 -04:00
|
|
|
mod sys_info;
|
|
|
|
|
2024-01-23 16:56:56 -05:00
|
|
|
deno_core::extension!(
|
|
|
|
deno_os,
|
|
|
|
ops = [
|
2023-03-17 14:22:15 -04:00
|
|
|
op_env,
|
|
|
|
op_exec_path,
|
|
|
|
op_exit,
|
|
|
|
op_delete_env,
|
|
|
|
op_get_env,
|
|
|
|
op_gid,
|
|
|
|
op_hostname,
|
|
|
|
op_loadavg,
|
|
|
|
op_network_interfaces,
|
|
|
|
op_os_release,
|
|
|
|
op_os_uptime,
|
|
|
|
op_set_env,
|
|
|
|
op_set_exit_code,
|
2024-05-29 19:16:27 -04:00
|
|
|
op_get_exit_code,
|
2023-03-17 14:22:15 -04:00
|
|
|
op_system_memory_info,
|
|
|
|
op_uid,
|
|
|
|
op_runtime_memory_usage,
|
2024-01-23 16:56:56 -05:00
|
|
|
],
|
2023-03-17 18:15:27 -04:00
|
|
|
options = {
|
2023-03-17 14:22:15 -04:00
|
|
|
exit_code: ExitCode,
|
|
|
|
},
|
2023-03-17 18:15:27 -04:00
|
|
|
state = |state, options| {
|
|
|
|
state.put::<ExitCode>(options.exit_code);
|
2023-03-17 14:22:15 -04:00
|
|
|
},
|
|
|
|
);
|
2019-08-14 11:03:02 -04:00
|
|
|
|
2023-03-17 14:22:15 -04:00
|
|
|
deno_core::extension!(
|
|
|
|
deno_os_worker,
|
2024-01-23 16:56:56 -05:00
|
|
|
ops = [
|
|
|
|
op_env,
|
|
|
|
op_exec_path,
|
|
|
|
op_exit,
|
|
|
|
op_delete_env,
|
|
|
|
op_get_env,
|
|
|
|
op_gid,
|
|
|
|
op_hostname,
|
|
|
|
op_loadavg,
|
|
|
|
op_network_interfaces,
|
|
|
|
op_os_release,
|
|
|
|
op_os_uptime,
|
|
|
|
op_set_env,
|
|
|
|
op_set_exit_code,
|
2024-05-29 19:16:27 -04:00
|
|
|
op_get_exit_code,
|
2024-01-23 16:56:56 -05:00
|
|
|
op_system_memory_info,
|
|
|
|
op_uid,
|
|
|
|
op_runtime_memory_usage,
|
|
|
|
],
|
2023-03-17 14:22:15 -04:00
|
|
|
middleware = |op| match op.name {
|
2024-05-29 19:16:27 -04:00
|
|
|
"op_exit" | "op_set_exit_code" | "op_get_exit_code" =>
|
2024-04-12 15:45:38 -04:00
|
|
|
op.with_implementation_from(&deno_core::op_void_sync()),
|
2023-03-17 14:22:15 -04:00
|
|
|
_ => op,
|
|
|
|
},
|
|
|
|
);
|
2022-06-13 17:53:04 -04:00
|
|
|
|
2023-09-11 18:10:43 -04:00
|
|
|
#[op2]
|
|
|
|
#[string]
|
2022-03-14 18:38:53 -04:00
|
|
|
fn op_exec_path(state: &mut OpState) -> Result<String, AnyError> {
|
2019-08-23 01:30:14 -04:00
|
|
|
let current_exe = env::current_exe().unwrap();
|
2023-01-07 11:25:34 -05:00
|
|
|
state
|
|
|
|
.borrow_mut::<PermissionsContainer>()
|
|
|
|
.check_read_blind(¤t_exe, "exec_path", "Deno.execPath()")?;
|
2019-08-23 01:30:14 -04:00
|
|
|
// Now apply URL parser to current exe to get fully resolved path, otherwise
|
|
|
|
// we might get `./` and `../` bits in `exec_path`
|
2019-08-14 11:03:02 -04:00
|
|
|
let exe_url = Url::from_file_path(current_exe).unwrap();
|
2019-08-23 01:30:14 -04:00
|
|
|
let path = exe_url.to_file_path().unwrap();
|
2021-04-05 12:40:24 -04:00
|
|
|
|
|
|
|
into_string(path.into_os_string())
|
|
|
|
}
|
|
|
|
|
2023-09-11 18:10:43 -04:00
|
|
|
#[op2(fast)]
|
2019-10-11 14:41:54 -04:00
|
|
|
fn op_set_env(
|
2020-09-10 09:57:45 -04:00
|
|
|
state: &mut OpState,
|
2023-09-11 18:10:43 -04:00
|
|
|
#[string] key: &str,
|
|
|
|
#[string] value: &str,
|
2021-04-05 12:40:24 -04:00
|
|
|
) -> Result<(), AnyError> {
|
2023-03-03 08:34:10 -05:00
|
|
|
state.borrow_mut::<PermissionsContainer>().check_env(key)?;
|
2022-09-21 02:18:58 -04:00
|
|
|
if key.is_empty() {
|
|
|
|
return Err(type_error("Key is an empty string."));
|
|
|
|
}
|
|
|
|
if key.contains(&['=', '\0'] as &[char]) {
|
|
|
|
return Err(type_error(format!(
|
2023-01-27 10:43:16 -05:00
|
|
|
"Key contains invalid characters: {key:?}"
|
2022-09-21 02:18:58 -04:00
|
|
|
)));
|
|
|
|
}
|
|
|
|
if value.contains('\0') {
|
|
|
|
return Err(type_error(format!(
|
2023-01-27 10:43:16 -05:00
|
|
|
"Value contains invalid characters: {value:?}"
|
2022-09-21 02:18:58 -04:00
|
|
|
)));
|
2021-02-23 04:24:59 -05:00
|
|
|
}
|
2021-10-05 16:38:27 -04:00
|
|
|
env::set_var(key, value);
|
2021-04-05 12:40:24 -04:00
|
|
|
Ok(())
|
2019-08-14 11:03:02 -04:00
|
|
|
}
|
|
|
|
|
2023-09-11 18:10:43 -04:00
|
|
|
#[op2]
|
|
|
|
#[serde]
|
2022-03-14 18:38:53 -04:00
|
|
|
fn op_env(state: &mut OpState) -> Result<HashMap<String, String>, AnyError> {
|
2023-01-07 11:25:34 -05:00
|
|
|
state.borrow_mut::<PermissionsContainer>().check_env_all()?;
|
2021-04-05 12:40:24 -04:00
|
|
|
Ok(env::vars().collect())
|
2019-10-02 11:55:28 -04:00
|
|
|
}
|
|
|
|
|
2023-09-11 18:10:43 -04:00
|
|
|
#[op2]
|
|
|
|
#[string]
|
2019-10-11 14:41:54 -04:00
|
|
|
fn op_get_env(
|
2020-09-10 09:57:45 -04:00
|
|
|
state: &mut OpState,
|
2023-09-11 18:10:43 -04:00
|
|
|
#[string] key: String,
|
2021-04-05 12:40:24 -04:00
|
|
|
) -> Result<Option<String>, AnyError> {
|
2022-11-11 11:04:57 -05:00
|
|
|
let skip_permission_check = NODE_ENV_VAR_ALLOWLIST.contains(&key);
|
2022-09-14 11:59:20 -04:00
|
|
|
|
|
|
|
if !skip_permission_check {
|
2023-01-07 11:25:34 -05:00
|
|
|
state.borrow_mut::<PermissionsContainer>().check_env(&key)?;
|
2022-09-14 11:59:20 -04:00
|
|
|
}
|
|
|
|
|
2022-09-21 02:18:58 -04:00
|
|
|
if key.is_empty() {
|
|
|
|
return Err(type_error("Key is an empty string."));
|
2021-02-23 04:24:59 -05:00
|
|
|
}
|
2022-09-21 02:18:58 -04:00
|
|
|
|
|
|
|
if key.contains(&['=', '\0'] as &[char]) {
|
|
|
|
return Err(type_error(format!(
|
2023-01-27 10:43:16 -05:00
|
|
|
"Key contains invalid characters: {key:?}"
|
2022-09-21 02:18:58 -04:00
|
|
|
)));
|
|
|
|
}
|
|
|
|
|
2021-04-05 12:40:24 -04:00
|
|
|
let r = match env::var(key) {
|
|
|
|
Err(env::VarError::NotPresent) => None,
|
|
|
|
v => Some(v?),
|
2019-10-02 11:55:28 -04:00
|
|
|
};
|
2020-08-28 11:08:24 -04:00
|
|
|
Ok(r)
|
2019-10-02 11:55:28 -04:00
|
|
|
}
|
2021-04-13 07:25:21 -04:00
|
|
|
|
2023-09-11 18:10:43 -04:00
|
|
|
#[op2(fast)]
|
|
|
|
fn op_delete_env(
|
|
|
|
state: &mut OpState,
|
|
|
|
#[string] key: String,
|
|
|
|
) -> Result<(), AnyError> {
|
2023-01-07 11:25:34 -05:00
|
|
|
state.borrow_mut::<PermissionsContainer>().check_env(&key)?;
|
2021-04-05 12:40:24 -04:00
|
|
|
if key.is_empty() || key.contains(&['=', '\0'] as &[char]) {
|
2021-02-23 04:24:59 -05:00
|
|
|
return Err(type_error("Key contains invalid characters."));
|
|
|
|
}
|
2021-04-05 12:40:24 -04:00
|
|
|
env::remove_var(key);
|
|
|
|
Ok(())
|
2019-08-14 11:03:02 -04:00
|
|
|
}
|
|
|
|
|
2023-09-11 18:10:43 -04:00
|
|
|
#[op2(fast)]
|
|
|
|
fn op_set_exit_code(state: &mut OpState, #[smi] code: i32) {
|
2022-06-08 11:45:38 -04:00
|
|
|
state.borrow_mut::<ExitCode>().set(code);
|
2021-11-27 18:45:38 -05:00
|
|
|
}
|
|
|
|
|
2024-05-29 19:16:27 -04:00
|
|
|
#[op2(fast)]
|
|
|
|
#[smi]
|
|
|
|
fn op_get_exit_code(state: &mut OpState) -> i32 {
|
|
|
|
state.borrow_mut::<ExitCode>().get()
|
|
|
|
}
|
|
|
|
|
2023-09-11 18:10:43 -04:00
|
|
|
#[op2(fast)]
|
2022-05-13 04:36:31 -04:00
|
|
|
fn op_exit(state: &mut OpState) {
|
2022-06-08 11:45:38 -04:00
|
|
|
let code = state.borrow::<ExitCode>().get();
|
2021-04-05 12:40:24 -04:00
|
|
|
std::process::exit(code)
|
2019-08-14 11:03:02 -04:00
|
|
|
}
|
|
|
|
|
2023-09-11 18:10:43 -04:00
|
|
|
#[op2]
|
|
|
|
#[serde]
|
2022-03-14 18:38:53 -04:00
|
|
|
fn op_loadavg(state: &mut OpState) -> Result<(f64, f64, f64), AnyError> {
|
2022-09-28 08:46:50 -04:00
|
|
|
state
|
2023-01-07 11:25:34 -05:00
|
|
|
.borrow_mut::<PermissionsContainer>()
|
|
|
|
.check_sys("loadavg", "Deno.loadavg()")?;
|
2022-11-02 03:17:00 -04:00
|
|
|
Ok(sys_info::loadavg())
|
2020-02-22 18:46:52 -05:00
|
|
|
}
|
|
|
|
|
2023-09-11 18:10:43 -04:00
|
|
|
#[op2]
|
|
|
|
#[string]
|
2022-03-14 18:38:53 -04:00
|
|
|
fn op_hostname(state: &mut OpState) -> Result<String, AnyError> {
|
2022-09-28 08:46:50 -04:00
|
|
|
state
|
2023-01-07 11:25:34 -05:00
|
|
|
.borrow_mut::<PermissionsContainer>()
|
|
|
|
.check_sys("hostname", "Deno.hostname()")?;
|
2022-11-02 03:17:00 -04:00
|
|
|
Ok(sys_info::hostname())
|
2019-09-27 19:09:42 -04:00
|
|
|
}
|
2020-02-24 08:35:45 -05:00
|
|
|
|
2023-09-11 18:10:43 -04:00
|
|
|
#[op2]
|
|
|
|
#[string]
|
2022-03-14 18:38:53 -04:00
|
|
|
fn op_os_release(state: &mut OpState) -> Result<String, AnyError> {
|
2022-09-28 08:46:50 -04:00
|
|
|
state
|
2023-01-07 11:25:34 -05:00
|
|
|
.borrow_mut::<PermissionsContainer>()
|
|
|
|
.check_sys("osRelease", "Deno.osRelease()")?;
|
2022-11-02 03:17:00 -04:00
|
|
|
Ok(sys_info::os_release())
|
2021-04-05 12:40:24 -04:00
|
|
|
}
|
|
|
|
|
2023-09-11 18:10:43 -04:00
|
|
|
#[op2]
|
|
|
|
#[serde]
|
2021-12-22 18:00:43 -05:00
|
|
|
fn op_network_interfaces(
|
|
|
|
state: &mut OpState,
|
|
|
|
) -> Result<Vec<NetworkInterface>, AnyError> {
|
2022-09-28 08:46:50 -04:00
|
|
|
state
|
2023-01-07 11:25:34 -05:00
|
|
|
.borrow_mut::<PermissionsContainer>()
|
|
|
|
.check_sys("networkInterfaces", "Deno.networkInterfaces()")?;
|
2021-12-22 18:00:43 -05:00
|
|
|
Ok(netif::up()?.map(NetworkInterface::from).collect())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(serde::Serialize)]
|
|
|
|
struct NetworkInterface {
|
|
|
|
family: &'static str,
|
|
|
|
name: String,
|
|
|
|
address: String,
|
|
|
|
netmask: String,
|
|
|
|
scopeid: Option<u32>,
|
|
|
|
cidr: String,
|
|
|
|
mac: String,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<netif::Interface> for NetworkInterface {
|
|
|
|
fn from(ifa: netif::Interface) -> Self {
|
|
|
|
let family = match ifa.address() {
|
|
|
|
std::net::IpAddr::V4(_) => "IPv4",
|
|
|
|
std::net::IpAddr::V6(_) => "IPv6",
|
|
|
|
};
|
|
|
|
|
|
|
|
let (address, range) = ifa.cidr();
|
2023-01-27 10:43:16 -05:00
|
|
|
let cidr = format!("{address:?}/{range}");
|
2021-12-22 18:00:43 -05:00
|
|
|
|
|
|
|
let name = ifa.name().to_owned();
|
|
|
|
let address = format!("{:?}", ifa.address());
|
|
|
|
let netmask = format!("{:?}", ifa.netmask());
|
|
|
|
let scopeid = ifa.scope_id();
|
|
|
|
|
|
|
|
let [b0, b1, b2, b3, b4, b5] = ifa.mac();
|
2023-01-27 10:43:16 -05:00
|
|
|
let mac = format!("{b0:02x}:{b1:02x}:{b2:02x}:{b3:02x}:{b4:02x}:{b5:02x}");
|
2021-12-22 18:00:43 -05:00
|
|
|
|
|
|
|
Self {
|
|
|
|
family,
|
|
|
|
name,
|
|
|
|
address,
|
|
|
|
netmask,
|
|
|
|
scopeid,
|
|
|
|
cidr,
|
|
|
|
mac,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-11 18:10:43 -04:00
|
|
|
#[op2]
|
|
|
|
#[serde]
|
2020-09-10 04:38:17 -04:00
|
|
|
fn op_system_memory_info(
|
2020-09-10 09:57:45 -04:00
|
|
|
state: &mut OpState,
|
2022-11-02 03:17:00 -04:00
|
|
|
) -> Result<Option<sys_info::MemInfo>, AnyError> {
|
2022-09-28 08:46:50 -04:00
|
|
|
state
|
2023-01-07 11:25:34 -05:00
|
|
|
.borrow_mut::<PermissionsContainer>()
|
|
|
|
.check_sys("systemMemoryInfo", "Deno.systemMemoryInfo()")?;
|
2022-11-02 03:17:00 -04:00
|
|
|
Ok(sys_info::mem_info())
|
2020-09-10 04:38:17 -04:00
|
|
|
}
|
2022-01-31 00:44:19 -05:00
|
|
|
|
2022-05-31 04:42:44 -04:00
|
|
|
#[cfg(not(windows))]
|
2023-09-21 10:08:23 -04:00
|
|
|
#[op2]
|
|
|
|
#[smi]
|
2022-10-26 16:37:48 -04:00
|
|
|
fn op_gid(state: &mut OpState) -> Result<Option<u32>, AnyError> {
|
2022-09-28 08:46:50 -04:00
|
|
|
state
|
2023-01-07 11:25:34 -05:00
|
|
|
.borrow_mut::<PermissionsContainer>()
|
|
|
|
.check_sys("gid", "Deno.gid()")?;
|
2022-06-25 18:13:24 -04:00
|
|
|
// TODO(bartlomieju):
|
|
|
|
#[allow(clippy::undocumented_unsafe_blocks)]
|
|
|
|
unsafe {
|
|
|
|
Ok(Some(libc::getgid()))
|
|
|
|
}
|
2022-05-31 04:42:44 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(windows)]
|
2023-09-21 10:08:23 -04:00
|
|
|
#[op2]
|
|
|
|
#[smi]
|
2022-10-26 16:37:48 -04:00
|
|
|
fn op_gid(state: &mut OpState) -> Result<Option<u32>, AnyError> {
|
2022-09-28 08:46:50 -04:00
|
|
|
state
|
2023-01-07 11:25:34 -05:00
|
|
|
.borrow_mut::<PermissionsContainer>()
|
|
|
|
.check_sys("gid", "Deno.gid()")?;
|
2022-05-31 04:42:44 -04:00
|
|
|
Ok(None)
|
|
|
|
}
|
|
|
|
|
2022-01-31 00:44:19 -05:00
|
|
|
#[cfg(not(windows))]
|
2023-09-21 10:08:23 -04:00
|
|
|
#[op2]
|
|
|
|
#[smi]
|
2022-10-26 16:37:48 -04:00
|
|
|
fn op_uid(state: &mut OpState) -> Result<Option<u32>, AnyError> {
|
2022-09-28 08:46:50 -04:00
|
|
|
state
|
2023-01-07 11:25:34 -05:00
|
|
|
.borrow_mut::<PermissionsContainer>()
|
|
|
|
.check_sys("uid", "Deno.uid()")?;
|
2022-06-25 18:13:24 -04:00
|
|
|
// TODO(bartlomieju):
|
|
|
|
#[allow(clippy::undocumented_unsafe_blocks)]
|
|
|
|
unsafe {
|
|
|
|
Ok(Some(libc::getuid()))
|
|
|
|
}
|
2022-01-31 00:44:19 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(windows)]
|
2023-09-21 10:08:23 -04:00
|
|
|
#[op2]
|
|
|
|
#[smi]
|
2022-10-26 16:37:48 -04:00
|
|
|
fn op_uid(state: &mut OpState) -> Result<Option<u32>, AnyError> {
|
2022-09-28 08:46:50 -04:00
|
|
|
state
|
2023-01-07 11:25:34 -05:00
|
|
|
.borrow_mut::<PermissionsContainer>()
|
|
|
|
.check_sys("uid", "Deno.uid()")?;
|
2022-01-31 00:44:19 -05:00
|
|
|
Ok(None)
|
|
|
|
}
|
2022-12-17 17:25:51 -05:00
|
|
|
|
|
|
|
// HeapStats stores values from a isolate.get_heap_statistics() call
|
|
|
|
#[derive(Serialize)]
|
|
|
|
#[serde(rename_all = "camelCase")]
|
|
|
|
struct MemoryUsage {
|
|
|
|
rss: usize,
|
|
|
|
heap_total: usize,
|
|
|
|
heap_used: usize,
|
|
|
|
external: usize,
|
|
|
|
}
|
|
|
|
|
2023-09-11 18:10:43 -04:00
|
|
|
#[op2]
|
|
|
|
#[serde]
|
2022-12-17 17:25:51 -05:00
|
|
|
fn op_runtime_memory_usage(scope: &mut v8::HandleScope) -> MemoryUsage {
|
|
|
|
let mut s = v8::HeapStatistics::default();
|
|
|
|
scope.get_heap_statistics(&mut s);
|
|
|
|
MemoryUsage {
|
|
|
|
rss: rss(),
|
|
|
|
heap_total: s.total_heap_size(),
|
|
|
|
heap_used: s.used_heap_size(),
|
|
|
|
external: s.external_memory(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-10 08:04:14 -05:00
|
|
|
#[cfg(any(target_os = "android", target_os = "linux"))]
|
2022-12-17 17:25:51 -05:00
|
|
|
fn rss() -> usize {
|
|
|
|
// Inspired by https://github.com/Arc-blroth/memory-stats/blob/5364d0d09143de2a470d33161b2330914228fde9/src/linux.rs
|
|
|
|
|
|
|
|
// Extracts a positive integer from a string that
|
|
|
|
// may contain leading spaces and trailing chars.
|
|
|
|
// Returns the extracted number and the index of
|
|
|
|
// the next character in the string.
|
|
|
|
fn scan_int(string: &str) -> (usize, usize) {
|
|
|
|
let mut out = 0;
|
|
|
|
let mut idx = 0;
|
|
|
|
let mut chars = string.chars().peekable();
|
|
|
|
while let Some(' ') = chars.next_if_eq(&' ') {
|
|
|
|
idx += 1;
|
|
|
|
}
|
|
|
|
for n in chars {
|
|
|
|
idx += 1;
|
2023-03-09 14:18:00 -05:00
|
|
|
if n.is_ascii_digit() {
|
2022-12-17 17:25:51 -05:00
|
|
|
out *= 10;
|
|
|
|
out += n as usize - '0' as usize;
|
|
|
|
} else {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
(out, idx)
|
|
|
|
}
|
|
|
|
|
2023-05-10 20:06:59 -04:00
|
|
|
#[allow(clippy::disallowed_methods)]
|
2022-12-17 17:25:51 -05:00
|
|
|
let statm_content = if let Ok(c) = std::fs::read_to_string("/proc/self/statm")
|
|
|
|
{
|
|
|
|
c
|
|
|
|
} else {
|
|
|
|
return 0;
|
|
|
|
};
|
|
|
|
|
|
|
|
// statm returns the virtual size and rss, in
|
|
|
|
// multiples of the page size, as the first
|
|
|
|
// two columns of output.
|
|
|
|
// SAFETY: libc call
|
|
|
|
let page_size = unsafe { libc::sysconf(libc::_SC_PAGESIZE) };
|
|
|
|
|
|
|
|
if page_size < 0 {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
let (_total_size_pages, idx) = scan_int(&statm_content);
|
|
|
|
let (total_rss_pages, _) = scan_int(&statm_content[idx..]);
|
|
|
|
|
|
|
|
total_rss_pages * page_size as usize
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(target_os = "macos")]
|
|
|
|
fn rss() -> usize {
|
|
|
|
// Inspired by https://github.com/Arc-blroth/memory-stats/blob/5364d0d09143de2a470d33161b2330914228fde9/src/darwin.rs
|
|
|
|
|
|
|
|
let mut task_info =
|
|
|
|
std::mem::MaybeUninit::<libc::mach_task_basic_info_data_t>::uninit();
|
|
|
|
let mut count = libc::MACH_TASK_BASIC_INFO_COUNT;
|
|
|
|
// SAFETY: libc calls
|
|
|
|
let r = unsafe {
|
|
|
|
libc::task_info(
|
|
|
|
libc::mach_task_self(),
|
|
|
|
libc::MACH_TASK_BASIC_INFO,
|
|
|
|
task_info.as_mut_ptr() as libc::task_info_t,
|
|
|
|
&mut count as *mut libc::mach_msg_type_number_t,
|
|
|
|
)
|
|
|
|
};
|
|
|
|
// According to libuv this should never fail
|
|
|
|
assert_eq!(r, libc::KERN_SUCCESS);
|
|
|
|
// SAFETY: we just asserted that it was success
|
|
|
|
let task_info = unsafe { task_info.assume_init() };
|
|
|
|
task_info.resident_size as usize
|
|
|
|
}
|
|
|
|
|
2023-06-05 08:24:19 -04:00
|
|
|
#[cfg(target_os = "openbsd")]
|
|
|
|
fn rss() -> usize {
|
|
|
|
// Uses OpenBSD's KERN_PROC_PID sysctl(2)
|
|
|
|
// to retrieve information about the current
|
|
|
|
// process, part of which is the RSS (p_vm_rssize)
|
|
|
|
|
|
|
|
// SAFETY: libc call (get PID of own process)
|
|
|
|
let pid = unsafe { libc::getpid() };
|
|
|
|
// SAFETY: libc call (get system page size)
|
|
|
|
let pagesize = unsafe { libc::sysconf(libc::_SC_PAGESIZE) } as usize;
|
|
|
|
// KERN_PROC_PID returns a struct libc::kinfo_proc
|
|
|
|
let mut kinfoproc = std::mem::MaybeUninit::<libc::kinfo_proc>::uninit();
|
|
|
|
let mut size = std::mem::size_of_val(&kinfoproc) as libc::size_t;
|
|
|
|
let mut mib = [
|
|
|
|
libc::CTL_KERN,
|
|
|
|
libc::KERN_PROC,
|
|
|
|
libc::KERN_PROC_PID,
|
|
|
|
pid,
|
|
|
|
// mib is an array of integers, size is of type size_t
|
|
|
|
// conversion is safe, because the size of a libc::kinfo_proc
|
|
|
|
// structure will not exceed i32::MAX
|
|
|
|
size.try_into().unwrap(),
|
|
|
|
1,
|
|
|
|
];
|
|
|
|
// SAFETY: libc call, mib has been statically initialized,
|
|
|
|
// kinfoproc is a valid pointer to a libc::kinfo_proc struct
|
|
|
|
let res = unsafe {
|
|
|
|
libc::sysctl(
|
|
|
|
mib.as_mut_ptr(),
|
|
|
|
mib.len() as _,
|
|
|
|
kinfoproc.as_mut_ptr() as *mut libc::c_void,
|
|
|
|
&mut size,
|
|
|
|
std::ptr::null_mut(),
|
|
|
|
0,
|
|
|
|
)
|
|
|
|
};
|
|
|
|
|
|
|
|
if res == 0 {
|
|
|
|
// SAFETY: sysctl returns 0 on success and kinfoproc is initialized
|
|
|
|
// p_vm_rssize contains size in pages -> multiply with pagesize to
|
|
|
|
// get size in bytes.
|
|
|
|
pagesize * unsafe { (*kinfoproc.as_mut_ptr()).p_vm_rssize as usize }
|
|
|
|
} else {
|
|
|
|
0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-17 17:25:51 -05:00
|
|
|
#[cfg(windows)]
|
|
|
|
fn rss() -> usize {
|
|
|
|
use winapi::shared::minwindef::DWORD;
|
|
|
|
use winapi::shared::minwindef::FALSE;
|
|
|
|
use winapi::um::processthreadsapi::GetCurrentProcess;
|
|
|
|
use winapi::um::psapi::GetProcessMemoryInfo;
|
|
|
|
use winapi::um::psapi::PROCESS_MEMORY_COUNTERS;
|
|
|
|
|
|
|
|
// SAFETY: winapi calls
|
|
|
|
unsafe {
|
|
|
|
// this handle is a constant—no need to close it
|
|
|
|
let current_process = GetCurrentProcess();
|
|
|
|
let mut pmc: PROCESS_MEMORY_COUNTERS = std::mem::zeroed();
|
|
|
|
|
|
|
|
if GetProcessMemoryInfo(
|
|
|
|
current_process,
|
|
|
|
&mut pmc,
|
|
|
|
std::mem::size_of::<PROCESS_MEMORY_COUNTERS>() as DWORD,
|
|
|
|
) != FALSE
|
|
|
|
{
|
|
|
|
pmc.WorkingSetSize
|
|
|
|
} else {
|
|
|
|
0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-12-26 18:16:12 -05:00
|
|
|
|
2022-12-28 08:56:05 -05:00
|
|
|
fn os_uptime(state: &mut OpState) -> Result<u64, AnyError> {
|
2022-12-26 18:16:12 -05:00
|
|
|
state
|
2023-01-07 11:25:34 -05:00
|
|
|
.borrow_mut::<PermissionsContainer>()
|
|
|
|
.check_sys("osUptime", "Deno.osUptime()")?;
|
2022-12-26 18:16:12 -05:00
|
|
|
Ok(sys_info::os_uptime())
|
|
|
|
}
|
2022-12-28 08:56:05 -05:00
|
|
|
|
2023-10-07 11:34:03 -04:00
|
|
|
#[op2(fast)]
|
|
|
|
#[number]
|
2022-12-28 08:56:05 -05:00
|
|
|
fn op_os_uptime(state: &mut OpState) -> Result<u64, AnyError> {
|
|
|
|
os_uptime(state)
|
|
|
|
}
|