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-02-21 13:20:31 -05:00
|
|
|
use crate::metrics::RuntimeMetrics;
|
|
|
|
use crate::ops::UnstableChecker;
|
2020-09-19 19:17:35 -04:00
|
|
|
use crate::permissions::Permissions;
|
2020-09-14 12:48:57 -04:00
|
|
|
use deno_core::error::AnyError;
|
2020-09-21 12:36:37 -04:00
|
|
|
use deno_core::serde_json;
|
|
|
|
use deno_core::serde_json::json;
|
|
|
|
use deno_core::serde_json::Value;
|
2020-06-10 23:00:29 -04:00
|
|
|
use deno_core::ModuleSpecifier;
|
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-01-27 21:12:25 -05:00
|
|
|
|
2020-12-11 12:49:26 -05:00
|
|
|
pub fn init(rt: &mut deno_core::JsRuntime, main_module: ModuleSpecifier) {
|
2020-09-28 06:14:11 -04:00
|
|
|
{
|
|
|
|
let op_state = rt.op_state();
|
|
|
|
let mut state = op_state.borrow_mut();
|
|
|
|
state.put::<ModuleSpecifier>(main_module);
|
|
|
|
}
|
2020-09-10 09:57:45 -04:00
|
|
|
super::reg_json_sync(rt, "op_main_module", op_main_module);
|
|
|
|
super::reg_json_sync(rt, "op_metrics", op_metrics);
|
2020-01-27 21:12:25 -05:00
|
|
|
}
|
|
|
|
|
2020-06-10 23:00:29 -04:00
|
|
|
fn op_main_module(
|
2020-09-10 09:57:45 -04:00
|
|
|
state: &mut OpState,
|
2020-06-10 23:00:29 -04:00
|
|
|
_args: Value,
|
2021-04-02 09:47:57 -04:00
|
|
|
_zero_copy: Option<ZeroCopyBuf>,
|
2020-09-14 12:48:57 -04:00
|
|
|
) -> Result<Value, AnyError> {
|
2020-09-19 19:17:35 -04:00
|
|
|
let main = state.borrow::<ModuleSpecifier>().to_string();
|
2021-02-17 13:47:18 -05:00
|
|
|
let main_url = deno_core::resolve_url_or_path(&main)?;
|
|
|
|
if main_url.scheme() == "file" {
|
2020-06-10 23:00:29 -04:00
|
|
|
let main_path = std::env::current_dir().unwrap().join(main_url.to_string());
|
2020-09-19 19:17:35 -04:00
|
|
|
state
|
|
|
|
.borrow::<Permissions>()
|
2021-03-17 17:45:12 -04:00
|
|
|
.read
|
|
|
|
.check_blind(&main_path, "main_module")?;
|
2020-06-10 23:00:29 -04:00
|
|
|
}
|
2020-08-28 11:08:24 -04:00
|
|
|
Ok(json!(&main))
|
2020-06-10 23:00:29 -04:00
|
|
|
}
|
|
|
|
|
2021-02-12 05:08:36 -05:00
|
|
|
#[allow(clippy::unnecessary_wraps)]
|
2020-02-11 04:04:59 -05:00
|
|
|
fn op_metrics(
|
2020-09-10 09:57:45 -04:00
|
|
|
state: &mut OpState,
|
2020-02-11 04:04:59 -05:00
|
|
|
_args: Value,
|
2021-04-02 09:47:57 -04:00
|
|
|
_zero_copy: Option<ZeroCopyBuf>,
|
2020-09-14 12:48:57 -04:00
|
|
|
) -> Result<Value, AnyError> {
|
2021-02-21 13:20:31 -05:00
|
|
|
let m = state.borrow::<RuntimeMetrics>();
|
|
|
|
let combined = m.combined_metrics();
|
|
|
|
let unstable_checker = state.borrow::<UnstableChecker>();
|
|
|
|
let maybe_ops = if unstable_checker.unstable {
|
|
|
|
Some(&m.ops)
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
};
|
|
|
|
Ok(json!({ "combined": combined, "ops": maybe_ops }))
|
2020-02-11 04:04:59 -05:00
|
|
|
}
|
2020-07-08 10:35:45 -04:00
|
|
|
|
2020-12-11 12:49:26 -05:00
|
|
|
pub fn ppid() -> Value {
|
2020-07-08 10:35:45 -04:00
|
|
|
#[cfg(windows)]
|
|
|
|
{
|
|
|
|
// Adopted from rustup:
|
|
|
|
// https://github.com/rust-lang/rustup/blob/1.21.1/src/cli/self_update.rs#L1036
|
|
|
|
// Copyright Diggory Blake, the Mozilla Corporation, and rustup contributors.
|
|
|
|
// Licensed under either of
|
|
|
|
// - Apache License, Version 2.0
|
|
|
|
// - MIT license
|
|
|
|
use std::mem;
|
|
|
|
use winapi::shared::minwindef::DWORD;
|
|
|
|
use winapi::um::handleapi::{CloseHandle, INVALID_HANDLE_VALUE};
|
|
|
|
use winapi::um::processthreadsapi::GetCurrentProcessId;
|
|
|
|
use winapi::um::tlhelp32::{
|
|
|
|
CreateToolhelp32Snapshot, Process32First, Process32Next, PROCESSENTRY32,
|
|
|
|
TH32CS_SNAPPROCESS,
|
|
|
|
};
|
|
|
|
unsafe {
|
|
|
|
// Take a snapshot of system processes, one of which is ours
|
|
|
|
// and contains our parent's pid
|
|
|
|
let snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
|
|
|
|
if snapshot == INVALID_HANDLE_VALUE {
|
|
|
|
return serde_json::to_value(-1).unwrap();
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut entry: PROCESSENTRY32 = mem::zeroed();
|
|
|
|
entry.dwSize = mem::size_of::<PROCESSENTRY32>() as DWORD;
|
|
|
|
|
|
|
|
// Iterate over system processes looking for ours
|
|
|
|
let success = Process32First(snapshot, &mut entry);
|
|
|
|
if success == 0 {
|
|
|
|
CloseHandle(snapshot);
|
|
|
|
return serde_json::to_value(-1).unwrap();
|
|
|
|
}
|
|
|
|
|
|
|
|
let this_pid = GetCurrentProcessId();
|
|
|
|
while entry.th32ProcessID != this_pid {
|
|
|
|
let success = Process32Next(snapshot, &mut entry);
|
|
|
|
if success == 0 {
|
|
|
|
CloseHandle(snapshot);
|
|
|
|
return serde_json::to_value(-1).unwrap();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
CloseHandle(snapshot);
|
|
|
|
|
|
|
|
// FIXME: Using the process ID exposes a race condition
|
|
|
|
// wherein the parent process already exited and the OS
|
|
|
|
// reassigned its ID.
|
|
|
|
let parent_id = entry.th32ParentProcessID;
|
|
|
|
serde_json::to_value(parent_id).unwrap()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#[cfg(not(windows))]
|
|
|
|
{
|
|
|
|
use std::os::unix::process::parent_id;
|
|
|
|
serde_json::to_value(parent_id()).unwrap()
|
|
|
|
}
|
|
|
|
}
|