From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: Samuel Attard <samuel.r.attard@gmail.com>
Date: Mon, 7 Mar 2022 16:36:28 -0800
Subject: feat: add kNoStartDebugSignalHandler to Environment to prevent
 SIGUSR1 handling

This patch should be upstreamed, it allows embedders to prevent the call to StartDebugSignalHandler which handles SIGUSR1 and starts the inspector agent.  Apps that have --inspect disabled also don't want SIGUSR1 to have this affect.

diff --git a/src/env-inl.h b/src/env-inl.h
index bea695bd8c92d44cb9526f347a9464549155ed85..0cbd1b25c107b5deba5fdd14551cc641904f7f21 100644
--- a/src/env-inl.h
+++ b/src/env-inl.h
@@ -669,6 +669,10 @@ inline bool Environment::no_global_search_paths() const {
          !options_->global_search_paths;
 }
 
+inline bool Environment::should_start_debug_signal_handler() const {
+  return (flags_ & EnvironmentFlags::kNoStartDebugSignalHandler) == 0;
+}
+
 inline bool Environment::no_browser_globals() const {
   // configure --no-browser-globals
 #ifdef NODE_NO_BROWSER_GLOBALS
diff --git a/src/env.h b/src/env.h
index 82f2dd1689114ec9c89fa32130985f0e9e78cc5f..4cb02112598459ea78a4b7b683ddd57e21ecb4c4 100644
--- a/src/env.h
+++ b/src/env.h
@@ -767,6 +767,7 @@ class Environment : public MemoryRetainer {
   inline bool tracks_unmanaged_fds() const;
   inline bool hide_console_windows() const;
   inline bool no_global_search_paths() const;
+  inline bool should_start_debug_signal_handler() const;
   inline bool no_browser_globals() const;
   inline uint64_t thread_id() const;
   inline worker::Worker* worker_context() const;
diff --git a/src/inspector_agent.cc b/src/inspector_agent.cc
index 214f992c9d015593ccdecd6eff0af4d960eccfbb..e5307b529c88d0e4db7fd7c6bd1b032d219b8c31 100644
--- a/src/inspector_agent.cc
+++ b/src/inspector_agent.cc
@@ -693,8 +693,10 @@ bool Agent::Start(const std::string& path,
                               StartIoThreadAsyncCallback));
     uv_unref(reinterpret_cast<uv_handle_t*>(&start_io_thread_async));
     start_io_thread_async.data = this;
-    // Ignore failure, SIGUSR1 won't work, but that should not block node start.
-    StartDebugSignalHandler();
+    if (parent_env_->should_start_debug_signal_handler()) {
+      // Ignore failure, SIGUSR1 won't work, but that should not block node start.
+      StartDebugSignalHandler();
+    }
 
     parent_env_->AddCleanupHook([](void* data) {
       Environment* env = static_cast<Environment*>(data);
diff --git a/src/node.h b/src/node.h
index 8314edbc82971148310c5c99e5a4b18dffa14e51..4b9b416e77b07407eae7b9587705b74ad3e6f211 100644
--- a/src/node.h
+++ b/src/node.h
@@ -559,7 +559,11 @@ enum Flags : uint64_t {
   // This control is needed by embedders who may not want to initialize the V8
   // inspector in situations where one has already been created,
   // e.g. Blink's in Chromium.
-  kNoCreateInspector = 1 << 9
+  kNoCreateInspector = 1 << 9,
+  // Controls where or not the InspectorAgent for this Environment should
+  // call StartDebugSignalHandler.  This control is needed by embedders who may
+  // not want to allow other processes to start the V8 inspector.
+  kNoStartDebugSignalHandler = 1 << 10
 };
 }  // namespace EnvironmentFlags
 
