1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-12-11 10:07:54 -05:00
denoland-deno/runtime/ops/runtime.rs

93 lines
2.8 KiB
Rust
Raw Normal View History

// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.
2020-09-05 20:34:02 -04:00
use crate::permissions::PermissionsContainer;
use deno_core::error::AnyError;
use deno_core::op;
use deno_core::Extension;
2020-06-10 23:00:29 -04:00
use deno_core::ModuleSpecifier;
use deno_core::OpState;
pub fn init(main_module: ModuleSpecifier) -> Extension {
Extension::builder("deno_runtime")
.ops(vec![op_main_module::decl()])
.state(move |state| {
state.put::<ModuleSpecifier>(main_module.clone());
})
.build()
}
#[op]
fn op_main_module(state: &mut OpState) -> Result<String, AnyError> {
let main_url = state.borrow::<ModuleSpecifier>();
let main_path = main_url.to_string();
if main_url.scheme() == "file" {
let main_path = main_url.to_file_path().unwrap();
state
.borrow_mut::<PermissionsContainer>()
.check_read_blind(&main_path, "main_module", "Deno.mainModule")?;
2020-06-10 23:00:29 -04:00
}
Ok(main_path)
}
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;
use winapi::um::handleapi::INVALID_HANDLE_VALUE;
2020-07-08 10:35:45 -04:00
use winapi::um::processthreadsapi::GetCurrentProcessId;
use winapi::um::tlhelp32::CreateToolhelp32Snapshot;
use winapi::um::tlhelp32::Process32First;
use winapi::um::tlhelp32::Process32Next;
use winapi::um::tlhelp32::PROCESSENTRY32;
use winapi::um::tlhelp32::TH32CS_SNAPPROCESS;
// 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 {
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);
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);
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;
parent_id.into()
2020-07-08 10:35:45 -04:00
}
}
#[cfg(not(windows))]
{
use std::os::unix::process::parent_id;
parent_id().into()
2020-07-08 10:35:45 -04:00
}
}