2021-01-10 21:59:07 -05:00
|
|
|
// Copyright 2018-2021 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;
|
2020-09-19 19:17:35 -04:00
|
|
|
use crate::permissions::Permissions;
|
2021-04-06 04:33:43 -04:00
|
|
|
use deno_core::error::{type_error, AnyError};
|
2020-09-16 14:28:07 -04:00
|
|
|
use deno_core::url::Url;
|
2020-09-10 09:57:45 -04:00
|
|
|
use deno_core::OpState;
|
2020-04-23 05:51:07 -04:00
|
|
|
use deno_core::ZeroCopyBuf;
|
2020-09-16 12:43:08 -04:00
|
|
|
use serde::Deserialize;
|
2021-04-05 12:40:24 -04: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
|
|
|
|
2020-09-10 09:57:45 -04:00
|
|
|
pub fn init(rt: &mut deno_core::JsRuntime) {
|
2021-04-12 15:55:05 -04:00
|
|
|
super::reg_sync(rt, "op_exit", op_exit);
|
|
|
|
super::reg_sync(rt, "op_env", op_env);
|
|
|
|
super::reg_sync(rt, "op_exec_path", op_exec_path);
|
|
|
|
super::reg_sync(rt, "op_set_env", op_set_env);
|
|
|
|
super::reg_sync(rt, "op_get_env", op_get_env);
|
|
|
|
super::reg_sync(rt, "op_delete_env", op_delete_env);
|
|
|
|
super::reg_sync(rt, "op_hostname", op_hostname);
|
|
|
|
super::reg_sync(rt, "op_loadavg", op_loadavg);
|
|
|
|
super::reg_sync(rt, "op_os_release", op_os_release);
|
|
|
|
super::reg_sync(rt, "op_system_memory_info", op_system_memory_info);
|
|
|
|
super::reg_sync(rt, "op_system_cpu_info", op_system_cpu_info);
|
2019-08-14 11:03:02 -04:00
|
|
|
}
|
|
|
|
|
2019-10-11 14:41:54 -04:00
|
|
|
fn op_exec_path(
|
2020-09-10 09:57:45 -04:00
|
|
|
state: &mut OpState,
|
2021-04-05 12:40:24 -04:00
|
|
|
_args: (),
|
2021-04-02 09:47:57 -04:00
|
|
|
_zero_copy: Option<ZeroCopyBuf>,
|
2021-04-05 12:40:24 -04:00
|
|
|
) -> Result<String, AnyError> {
|
2019-08-23 01:30:14 -04:00
|
|
|
let current_exe = env::current_exe().unwrap();
|
2020-09-19 19:17:35 -04:00
|
|
|
state
|
2021-04-11 22:15:43 -04:00
|
|
|
.borrow_mut::<Permissions>()
|
2021-03-17 17:45:12 -04:00
|
|
|
.read
|
|
|
|
.check_blind(¤t_exe, "exec_path")?;
|
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())
|
|
|
|
}
|
|
|
|
|
2019-08-26 08:50:21 -04:00
|
|
|
#[derive(Deserialize)]
|
2021-03-18 14:42:01 -04:00
|
|
|
pub struct SetEnv {
|
2019-08-26 08:50:21 -04:00
|
|
|
key: String,
|
|
|
|
value: String,
|
|
|
|
}
|
|
|
|
|
2019-10-11 14:41:54 -04:00
|
|
|
fn op_set_env(
|
2020-09-10 09:57:45 -04:00
|
|
|
state: &mut OpState,
|
2021-03-18 14:42:01 -04:00
|
|
|
args: SetEnv,
|
2021-04-02 09:47:57 -04:00
|
|
|
_zero_copy: Option<ZeroCopyBuf>,
|
2021-04-05 12:40:24 -04:00
|
|
|
) -> Result<(), AnyError> {
|
2021-04-13 07:25:21 -04:00
|
|
|
state.borrow_mut::<Permissions>().env.check(&args.key)?;
|
2021-02-23 04:24:59 -05:00
|
|
|
let invalid_key =
|
|
|
|
args.key.is_empty() || args.key.contains(&['=', '\0'] as &[char]);
|
|
|
|
let invalid_value = args.value.contains('\0');
|
|
|
|
if invalid_key || invalid_value {
|
|
|
|
return Err(type_error("Key or value contains invalid characters."));
|
|
|
|
}
|
2019-08-26 08:50:21 -04:00
|
|
|
env::set_var(args.key, args.value);
|
2021-04-05 12:40:24 -04:00
|
|
|
Ok(())
|
2019-08-14 11:03:02 -04:00
|
|
|
}
|
|
|
|
|
2019-10-11 14:41:54 -04:00
|
|
|
fn op_env(
|
2020-09-10 09:57:45 -04:00
|
|
|
state: &mut OpState,
|
2021-04-05 12:40:24 -04:00
|
|
|
_args: (),
|
2021-04-02 09:47:57 -04:00
|
|
|
_zero_copy: Option<ZeroCopyBuf>,
|
2021-04-05 12:40:24 -04:00
|
|
|
) -> Result<HashMap<String, String>, AnyError> {
|
2021-04-13 07:25:21 -04:00
|
|
|
state.borrow_mut::<Permissions>().env.check_all()?;
|
2021-04-05 12:40:24 -04:00
|
|
|
Ok(env::vars().collect())
|
2019-10-02 11:55:28 -04:00
|
|
|
}
|
|
|
|
|
2019-10-11 14:41:54 -04:00
|
|
|
fn op_get_env(
|
2020-09-10 09:57:45 -04:00
|
|
|
state: &mut OpState,
|
2021-04-05 12:40:24 -04:00
|
|
|
key: String,
|
2021-04-02 09:47:57 -04:00
|
|
|
_zero_copy: Option<ZeroCopyBuf>,
|
2021-04-05 12:40:24 -04:00
|
|
|
) -> Result<Option<String>, AnyError> {
|
2021-04-13 07:25:21 -04:00
|
|
|
state.borrow_mut::<Permissions>().env.check(&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
|
|
|
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
|
|
|
|
2020-06-09 08:58:30 -04:00
|
|
|
fn op_delete_env(
|
2020-09-10 09:57:45 -04:00
|
|
|
state: &mut OpState,
|
2021-04-05 12:40:24 -04:00
|
|
|
key: String,
|
2021-04-02 09:47:57 -04:00
|
|
|
_zero_copy: Option<ZeroCopyBuf>,
|
2021-04-05 12:40:24 -04:00
|
|
|
) -> Result<(), AnyError> {
|
2021-04-13 07:25:21 -04:00
|
|
|
state.borrow_mut::<Permissions>().env.check(&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
|
|
|
}
|
|
|
|
|
2019-10-11 14:41:54 -04:00
|
|
|
fn op_exit(
|
2020-09-10 09:57:45 -04:00
|
|
|
_state: &mut OpState,
|
2021-04-05 12:40:24 -04:00
|
|
|
code: i32,
|
2021-04-02 09:47:57 -04:00
|
|
|
_zero_copy: Option<ZeroCopyBuf>,
|
2021-04-05 12:40:24 -04:00
|
|
|
) -> Result<(), AnyError> {
|
|
|
|
std::process::exit(code)
|
2019-08-14 11:03:02 -04:00
|
|
|
}
|
|
|
|
|
2020-02-22 18:46:52 -05:00
|
|
|
fn op_loadavg(
|
2020-09-10 09:57:45 -04:00
|
|
|
state: &mut OpState,
|
2021-04-05 12:40:24 -04:00
|
|
|
_args: (),
|
2021-04-02 09:47:57 -04:00
|
|
|
_zero_copy: Option<ZeroCopyBuf>,
|
2021-04-05 12:40:24 -04:00
|
|
|
) -> Result<(f64, f64, f64), AnyError> {
|
2020-09-26 14:26:51 -04:00
|
|
|
super::check_unstable(state, "Deno.loadavg");
|
2021-04-13 07:25:21 -04:00
|
|
|
state.borrow_mut::<Permissions>().env.check_all()?;
|
2020-02-22 18:46:52 -05:00
|
|
|
match sys_info::loadavg() {
|
2021-04-05 12:40:24 -04:00
|
|
|
Ok(loadavg) => Ok((loadavg.one, loadavg.five, loadavg.fifteen)),
|
|
|
|
Err(_) => Ok((0.0, 0.0, 0.0)),
|
2020-02-22 18:46:52 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-11 14:41:54 -04:00
|
|
|
fn op_hostname(
|
2020-09-10 09:57:45 -04:00
|
|
|
state: &mut OpState,
|
2021-04-05 12:40:24 -04:00
|
|
|
_args: (),
|
2021-04-02 09:47:57 -04:00
|
|
|
_zero_copy: Option<ZeroCopyBuf>,
|
2021-04-05 12:40:24 -04:00
|
|
|
) -> Result<String, AnyError> {
|
2020-09-26 14:26:51 -04:00
|
|
|
super::check_unstable(state, "Deno.hostname");
|
2021-04-13 07:25:21 -04:00
|
|
|
state.borrow_mut::<Permissions>().env.check_all()?;
|
2020-02-24 08:35:45 -05:00
|
|
|
let hostname = sys_info::hostname().unwrap_or_else(|_| "".to_string());
|
2021-04-05 12:40:24 -04:00
|
|
|
Ok(hostname)
|
2019-09-27 19:09:42 -04:00
|
|
|
}
|
2020-02-24 08:35:45 -05:00
|
|
|
|
|
|
|
fn op_os_release(
|
2020-09-10 09:57:45 -04:00
|
|
|
state: &mut OpState,
|
2021-04-05 12:40:24 -04:00
|
|
|
_args: (),
|
2021-04-02 09:47:57 -04:00
|
|
|
_zero_copy: Option<ZeroCopyBuf>,
|
2021-04-05 12:40:24 -04:00
|
|
|
) -> Result<String, AnyError> {
|
2020-09-26 14:26:51 -04:00
|
|
|
super::check_unstable(state, "Deno.osRelease");
|
2021-04-13 07:25:21 -04:00
|
|
|
state.borrow_mut::<Permissions>().env.check_all()?;
|
2020-02-24 08:35:45 -05:00
|
|
|
let release = sys_info::os_release().unwrap_or_else(|_| "".to_string());
|
2021-04-05 12:40:24 -04:00
|
|
|
Ok(release)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Copied from sys-info/lib.rs (then tweaked)
|
|
|
|
#[derive(Serialize)]
|
|
|
|
#[serde(rename_all = "camelCase")]
|
|
|
|
struct MemInfo {
|
|
|
|
pub total: u64,
|
|
|
|
pub free: u64,
|
|
|
|
pub available: u64,
|
|
|
|
pub buffers: u64,
|
|
|
|
pub cached: u64,
|
|
|
|
pub swap_total: u64,
|
|
|
|
pub swap_free: u64,
|
2020-02-24 08:35:45 -05:00
|
|
|
}
|
2020-09-10 04:38:17 -04:00
|
|
|
|
|
|
|
fn op_system_memory_info(
|
2020-09-10 09:57:45 -04:00
|
|
|
state: &mut OpState,
|
2021-04-05 12:40:24 -04:00
|
|
|
_args: (),
|
2021-04-02 09:47:57 -04:00
|
|
|
_zero_copy: Option<ZeroCopyBuf>,
|
2021-04-05 12:40:24 -04:00
|
|
|
) -> Result<Option<MemInfo>, AnyError> {
|
2020-09-26 14:26:51 -04:00
|
|
|
super::check_unstable(state, "Deno.systemMemoryInfo");
|
2021-04-13 07:25:21 -04:00
|
|
|
state.borrow_mut::<Permissions>().env.check_all()?;
|
2020-09-10 04:38:17 -04:00
|
|
|
match sys_info::mem_info() {
|
2021-04-05 12:40:24 -04:00
|
|
|
Ok(info) => Ok(Some(MemInfo {
|
|
|
|
total: info.total,
|
|
|
|
free: info.free,
|
|
|
|
available: info.avail,
|
|
|
|
buffers: info.buffers,
|
|
|
|
cached: info.cached,
|
|
|
|
swap_total: info.swap_total,
|
|
|
|
swap_free: info.swap_free,
|
2020-09-10 04:38:17 -04:00
|
|
|
})),
|
2021-04-05 12:40:24 -04:00
|
|
|
Err(_) => Ok(None),
|
2020-09-10 04:38:17 -04:00
|
|
|
}
|
|
|
|
}
|
2020-10-26 10:54:27 -04:00
|
|
|
|
2021-04-05 12:40:24 -04:00
|
|
|
#[derive(Serialize)]
|
|
|
|
struct CpuInfo {
|
|
|
|
cores: Option<u32>,
|
|
|
|
speed: Option<u64>,
|
|
|
|
}
|
|
|
|
|
2020-10-26 10:54:27 -04:00
|
|
|
fn op_system_cpu_info(
|
|
|
|
state: &mut OpState,
|
2021-04-05 12:40:24 -04:00
|
|
|
_args: (),
|
2021-04-02 09:47:57 -04:00
|
|
|
_zero_copy: Option<ZeroCopyBuf>,
|
2021-04-05 12:40:24 -04:00
|
|
|
) -> Result<CpuInfo, AnyError> {
|
2020-10-26 10:54:27 -04:00
|
|
|
super::check_unstable(state, "Deno.systemCpuInfo");
|
2021-04-13 07:25:21 -04:00
|
|
|
state.borrow_mut::<Permissions>().env.check_all()?;
|
2020-10-26 10:54:27 -04:00
|
|
|
|
|
|
|
let cores = sys_info::cpu_num().ok();
|
|
|
|
let speed = sys_info::cpu_speed().ok();
|
|
|
|
|
2021-04-05 12:40:24 -04:00
|
|
|
Ok(CpuInfo { cores, speed })
|
2020-10-26 10:54:27 -04:00
|
|
|
}
|