2023-01-02 16:00:42 -05:00
|
|
|
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.
|
2020-09-05 20:34:02 -04:00
|
|
|
|
2023-01-07 11:25:34 -05:00
|
|
|
use crate::permissions::PermissionsContainer;
|
2021-11-16 09:02:28 -05:00
|
|
|
use deno_core::anyhow::Context;
|
2020-09-14 12:48:57 -04:00
|
|
|
use deno_core::error::AnyError;
|
2022-03-14 13:44:15 -04:00
|
|
|
use deno_core::op;
|
2021-05-02 19:22:57 -04:00
|
|
|
use deno_core::Extension;
|
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-01-27 21:12:25 -05:00
|
|
|
|
2021-05-02 19:22:57 -04:00
|
|
|
pub fn init(main_module: ModuleSpecifier) -> Extension {
|
2023-01-08 17:48:46 -05:00
|
|
|
Extension::builder("deno_runtime")
|
2022-03-14 13:44:15 -04:00
|
|
|
.ops(vec![op_main_module::decl()])
|
2021-05-02 19:22:57 -04:00
|
|
|
.state(move |state| {
|
|
|
|
state.put::<ModuleSpecifier>(main_module.clone());
|
|
|
|
Ok(())
|
|
|
|
})
|
|
|
|
.build()
|
2020-01-27 21:12:25 -05:00
|
|
|
}
|
|
|
|
|
2022-03-14 13:44:15 -04:00
|
|
|
#[op]
|
2022-03-14 18:38:53 -04:00
|
|
|
fn op_main_module(state: &mut OpState) -> Result<String, 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" {
|
2021-04-21 11:52:00 -04:00
|
|
|
let main_path = std::env::current_dir()
|
|
|
|
.context("Failed to get current working directory")?
|
|
|
|
.join(main_url.to_string());
|
2023-01-07 11:25:34 -05:00
|
|
|
state
|
|
|
|
.borrow_mut::<PermissionsContainer>()
|
|
|
|
.check_read_blind(&main_path, "main_module", "Deno.mainModule")?;
|
2020-06-10 23:00:29 -04:00
|
|
|
}
|
2021-04-05 12:40:24 -04:00
|
|
|
Ok(main)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn ppid() -> i64 {
|
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,
|
|
|
|
};
|
2022-07-15 12:30:25 -04:00
|
|
|
// SAFETY: winapi calls
|
2020-07-08 10:35:45 -04:00
|
|
|
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 {
|
2021-04-05 12:40:24 -04:00
|
|
|
return -1;
|
2020-07-08 10:35:45 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
2021-04-05 12:40:24 -04:00
|
|
|
return -1;
|
2020-07-08 10:35:45 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
let this_pid = GetCurrentProcessId();
|
|
|
|
while entry.th32ProcessID != this_pid {
|
|
|
|
let success = Process32Next(snapshot, &mut entry);
|
|
|
|
if success == 0 {
|
|
|
|
CloseHandle(snapshot);
|
2021-04-05 12:40:24 -04:00
|
|
|
return -1;
|
2020-07-08 10:35:45 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
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;
|
2021-04-05 12:40:24 -04:00
|
|
|
parent_id.into()
|
2020-07-08 10:35:45 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
#[cfg(not(windows))]
|
|
|
|
{
|
|
|
|
use std::os::unix::process::parent_id;
|
2021-04-05 12:40:24 -04:00
|
|
|
parent_id().into()
|
2020-07-08 10:35:45 -04:00
|
|
|
}
|
|
|
|
}
|