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};
|
2021-05-02 19:22:57 -04:00
|
|
|
use deno_core::op_sync;
|
2020-09-16 14:28:07 -04:00
|
|
|
use deno_core::url::Url;
|
2021-05-02 19:22:57 -04:00
|
|
|
use deno_core::Extension;
|
2020-09-10 09:57:45 -04:00
|
|
|
use deno_core::OpState;
|
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;
|
2021-12-11 09:56:45 -05:00
|
|
|
use std::sync::atomic::AtomicI32;
|
2021-11-27 18:45:38 -05:00
|
|
|
use std::sync::atomic::Ordering::Relaxed;
|
2021-12-11 09:56:45 -05:00
|
|
|
use std::sync::Arc;
|
2019-08-14 11:03:02 -04:00
|
|
|
|
2021-12-11 09:56:45 -05:00
|
|
|
pub fn init(maybe_exit_code: Option<Arc<AtomicI32>>) -> Extension {
|
2021-05-02 19:22:57 -04:00
|
|
|
Extension::builder()
|
|
|
|
.ops(vec![
|
|
|
|
("op_exit", op_sync(op_exit)),
|
|
|
|
("op_env", op_sync(op_env)),
|
|
|
|
("op_exec_path", op_sync(op_exec_path)),
|
|
|
|
("op_set_env", op_sync(op_set_env)),
|
|
|
|
("op_get_env", op_sync(op_get_env)),
|
|
|
|
("op_delete_env", op_sync(op_delete_env)),
|
|
|
|
("op_hostname", op_sync(op_hostname)),
|
|
|
|
("op_loadavg", op_sync(op_loadavg)),
|
|
|
|
("op_os_release", op_sync(op_os_release)),
|
2021-11-27 18:45:38 -05:00
|
|
|
("op_set_exit_code", op_sync(op_set_exit_code)),
|
2021-05-02 19:22:57 -04:00
|
|
|
("op_system_memory_info", op_sync(op_system_memory_info)),
|
|
|
|
])
|
2021-12-11 09:56:45 -05:00
|
|
|
.state(move |state| {
|
|
|
|
let exit_code = maybe_exit_code.clone().unwrap_or_default();
|
|
|
|
state.put::<Arc<AtomicI32>>(exit_code);
|
|
|
|
Ok(())
|
|
|
|
})
|
2021-05-02 19:22:57 -04:00
|
|
|
.build()
|
2019-08-14 11:03:02 -04:00
|
|
|
}
|
|
|
|
|
2021-10-05 16:38:27 -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();
|
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-10-11 14:41:54 -04:00
|
|
|
fn op_set_env(
|
2020-09-10 09:57:45 -04:00
|
|
|
state: &mut OpState,
|
2021-10-05 16:38:27 -04:00
|
|
|
key: String,
|
|
|
|
value: String,
|
2021-04-05 12:40:24 -04:00
|
|
|
) -> Result<(), AnyError> {
|
2021-10-05 16:38:27 -04:00
|
|
|
state.borrow_mut::<Permissions>().env.check(&key)?;
|
|
|
|
let invalid_key = key.is_empty() || key.contains(&['=', '\0'] as &[char]);
|
|
|
|
let invalid_value = value.contains('\0');
|
2021-02-23 04:24:59 -05:00
|
|
|
if invalid_key || invalid_value {
|
|
|
|
return Err(type_error("Key or value contains invalid characters."));
|
|
|
|
}
|
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
|
|
|
}
|
|
|
|
|
2019-10-11 14:41:54 -04:00
|
|
|
fn op_env(
|
2020-09-10 09:57:45 -04:00
|
|
|
state: &mut OpState,
|
2021-10-05 16:38:27 -04:00
|
|
|
_: (),
|
2021-05-08 08:37:42 -04:00
|
|
|
_: (),
|
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-05-08 08:37:42 -04:00
|
|
|
_: (),
|
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-05-08 08:37:42 -04:00
|
|
|
_: (),
|
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
|
|
|
}
|
|
|
|
|
2021-12-11 09:56:45 -05:00
|
|
|
fn op_set_exit_code(
|
|
|
|
state: &mut OpState,
|
|
|
|
code: i32,
|
|
|
|
_: (),
|
|
|
|
) -> Result<(), AnyError> {
|
|
|
|
state.borrow_mut::<Arc<AtomicI32>>().store(code, Relaxed);
|
2021-11-27 18:45:38 -05:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2021-12-11 09:56:45 -05:00
|
|
|
fn op_exit(state: &mut OpState, _: (), _: ()) -> Result<(), AnyError> {
|
|
|
|
let code = state.borrow::<Arc<AtomicI32>>().load(Relaxed);
|
2021-04-05 12:40:24 -04:00
|
|
|
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-10-05 16:38:27 -04:00
|
|
|
_: (),
|
2021-05-08 08:37:42 -04:00
|
|
|
_: (),
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-05 16:38:27 -04:00
|
|
|
fn op_hostname(state: &mut OpState, _: (), _: ()) -> 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-10-05 16:38:27 -04:00
|
|
|
_: (),
|
2021-05-08 08:37:42 -04:00
|
|
|
_: (),
|
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-10-05 16:38:27 -04:00
|
|
|
_: (),
|
2021-05-08 08:37:42 -04:00
|
|
|
_: (),
|
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
|
|
|
}
|
|
|
|
}
|