deno_runtime/ops/
runtime.rs

1// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
2
3use deno_core::op2;
4use deno_core::ModuleSpecifier;
5use deno_core::OpState;
6
7deno_core::extension!(
8  deno_runtime,
9  ops = [op_main_module, op_ppid],
10  options = { main_module: ModuleSpecifier },
11  state = |state, options| {
12    state.put::<ModuleSpecifier>(options.main_module);
13  },
14);
15
16#[op2]
17#[string]
18fn op_main_module(state: &mut OpState) -> String {
19  let main_url = state.borrow::<ModuleSpecifier>();
20  main_url.to_string()
21}
22
23/// This is an op instead of being done at initialization time because
24/// it's expensive to retrieve the ppid on Windows.
25#[op2(fast)]
26#[number]
27pub fn op_ppid() -> i64 {
28  #[cfg(windows)]
29  {
30    // Adopted from rustup:
31    // https://github.com/rust-lang/rustup/blob/1.21.1/src/cli/self_update.rs#L1036
32    // Copyright Diggory Blake, the Mozilla Corporation, and rustup contributors.
33    // Licensed under either of
34    // - Apache License, Version 2.0
35    // - MIT license
36    use std::mem;
37    use winapi::shared::minwindef::DWORD;
38    use winapi::um::handleapi::CloseHandle;
39    use winapi::um::handleapi::INVALID_HANDLE_VALUE;
40    use winapi::um::processthreadsapi::GetCurrentProcessId;
41    use winapi::um::tlhelp32::CreateToolhelp32Snapshot;
42    use winapi::um::tlhelp32::Process32First;
43    use winapi::um::tlhelp32::Process32Next;
44    use winapi::um::tlhelp32::PROCESSENTRY32;
45    use winapi::um::tlhelp32::TH32CS_SNAPPROCESS;
46    // SAFETY: winapi calls
47    unsafe {
48      // Take a snapshot of system processes, one of which is ours
49      // and contains our parent's pid
50      let snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
51      if snapshot == INVALID_HANDLE_VALUE {
52        return -1;
53      }
54
55      let mut entry: PROCESSENTRY32 = mem::zeroed();
56      entry.dwSize = mem::size_of::<PROCESSENTRY32>() as DWORD;
57
58      // Iterate over system processes looking for ours
59      let success = Process32First(snapshot, &mut entry);
60      if success == 0 {
61        CloseHandle(snapshot);
62        return -1;
63      }
64
65      let this_pid = GetCurrentProcessId();
66      while entry.th32ProcessID != this_pid {
67        let success = Process32Next(snapshot, &mut entry);
68        if success == 0 {
69          CloseHandle(snapshot);
70          return -1;
71        }
72      }
73      CloseHandle(snapshot);
74
75      // FIXME: Using the process ID exposes a race condition
76      // wherein the parent process already exited and the OS
77      // reassigned its ID.
78      let parent_id = entry.th32ParentProcessID;
79      parent_id.into()
80    }
81  }
82  #[cfg(not(windows))]
83  {
84    use std::os::unix::process::parent_id;
85    parent_id().into()
86  }
87}