2024-01-01 19:58:21 +00:00
|
|
|
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
|
2020-09-06 00:34:02 +00:00
|
|
|
|
2023-09-11 22:10:43 +00:00
|
|
|
use deno_core::op2;
|
2020-06-11 03:00:29 +00:00
|
|
|
use deno_core::ModuleSpecifier;
|
2020-09-10 13:57:45 +00:00
|
|
|
use deno_core::OpState;
|
2020-01-28 02:12:25 +00:00
|
|
|
|
2023-03-17 18:22:15 +00:00
|
|
|
deno_core::extension!(
|
|
|
|
deno_runtime,
|
2024-08-02 18:16:59 +00:00
|
|
|
ops = [op_main_module, op_ppid],
|
2023-03-17 22:15:27 +00:00
|
|
|
options = { main_module: ModuleSpecifier },
|
|
|
|
state = |state, options| {
|
|
|
|
state.put::<ModuleSpecifier>(options.main_module);
|
2023-03-18 22:30:04 +00:00
|
|
|
},
|
2023-03-17 18:22:15 +00:00
|
|
|
);
|
2020-01-28 02:12:25 +00:00
|
|
|
|
2023-09-11 22:10:43 +00:00
|
|
|
#[op2]
|
|
|
|
#[string]
|
2024-10-22 08:41:08 +00:00
|
|
|
fn op_main_module(state: &mut OpState) -> String {
|
2023-02-22 23:22:24 +00:00
|
|
|
let main_url = state.borrow::<ModuleSpecifier>();
|
2024-10-22 08:41:08 +00:00
|
|
|
main_url.to_string()
|
2021-04-05 16:40:24 +00:00
|
|
|
}
|
|
|
|
|
2023-05-01 18:21:27 +00:00
|
|
|
/// This is an op instead of being done at initialization time because
|
2023-06-26 13:10:27 +00:00
|
|
|
/// it's expensive to retrieve the ppid on Windows.
|
2023-10-04 21:43:58 +00:00
|
|
|
#[op2(fast)]
|
2024-01-30 06:34:31 +00:00
|
|
|
#[number]
|
2023-05-01 18:21:27 +00:00
|
|
|
pub fn op_ppid() -> i64 {
|
2020-07-08 14:35:45 +00: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;
|
2023-01-15 04:18:58 +00:00
|
|
|
use winapi::um::handleapi::CloseHandle;
|
|
|
|
use winapi::um::handleapi::INVALID_HANDLE_VALUE;
|
2020-07-08 14:35:45 +00:00
|
|
|
use winapi::um::processthreadsapi::GetCurrentProcessId;
|
2023-01-15 04:18:58 +00:00
|
|
|
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;
|
2022-07-15 16:30:25 +00:00
|
|
|
// SAFETY: winapi calls
|
2020-07-08 14:35:45 +00: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 16:40:24 +00:00
|
|
|
return -1;
|
2020-07-08 14:35:45 +00: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 16:40:24 +00:00
|
|
|
return -1;
|
2020-07-08 14:35:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
let this_pid = GetCurrentProcessId();
|
|
|
|
while entry.th32ProcessID != this_pid {
|
|
|
|
let success = Process32Next(snapshot, &mut entry);
|
|
|
|
if success == 0 {
|
|
|
|
CloseHandle(snapshot);
|
2021-04-05 16:40:24 +00:00
|
|
|
return -1;
|
2020-07-08 14:35:45 +00: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 16:40:24 +00:00
|
|
|
parent_id.into()
|
2020-07-08 14:35:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
#[cfg(not(windows))]
|
|
|
|
{
|
|
|
|
use std::os::unix::process::parent_id;
|
2021-04-05 16:40:24 +00:00
|
|
|
parent_id().into()
|
2020-07-08 14:35:45 +00:00
|
|
|
}
|
|
|
|
}
|