tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

MozillaRuntimeMain.cpp (3246B)


      1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
      2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
      3 /* This Source Code Form is subject to the terms of the Mozilla Public
      4 * License, v. 2.0. If a copy of the MPL was not distributed with this
      5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
      6 
      7 #include "nsXULAppAPI.h"
      8 #include "XREChildData.h"
      9 #include "mozilla/Bootstrap.h"
     10 #include "mozilla/ProcessType.h"
     11 #include "mozilla/RuntimeExceptionModule.h"
     12 #include "mozilla/ScopeExit.h"
     13 #if defined(XP_WIN)
     14 #  include "mozilla/WindowsDllBlocklist.h"
     15 #  include "mozilla/GeckoArgs.h"
     16 
     17 #  include "nsWindowsWMain.cpp"
     18 
     19 #  ifdef MOZ_SANDBOX
     20 #    include "mozilla/sandboxing/SandboxInitialization.h"
     21 #    include "mozilla/sandboxing/sandboxLogging.h"
     22 #  endif
     23 #endif  // defined(XP_WIN)
     24 
     25 using namespace mozilla;
     26 
     27 int main(int argc, char* argv[]) {
     28  // Set the process type and gecko child id.
     29  if (argc < 2) {
     30    return 3;
     31  }
     32  SetGeckoProcessType(argv[--argc]);
     33  SetGeckoChildID(argv[--argc]);
     34 
     35  auto bootstrapResult = GetBootstrap();
     36  if (bootstrapResult.isErr()) {
     37    return 2;
     38  }
     39 
     40  Bootstrap::UniquePtr bootstrap = bootstrapResult.unwrap();
     41 
     42 #if defined(MOZ_ENABLE_FORKSERVER)
     43  if (GetGeckoProcessType() == GeckoProcessType_ForkServer) {
     44    bootstrap->NS_LogInit();
     45 
     46    // Run a fork server in this process, single thread. When it returns, it
     47    // means the fork server have been stopped or a new child process is
     48    // created.
     49    //
     50    // For the latter case, XRE_ForkServer() will return false, running in a
     51    // child process just forked from the fork server process. argc & argv will
     52    // be updated with the values passing from the chrome process, as will
     53    // GeckoProcessType and GeckoChildID. With the new values, this function
     54    // continues the reset of the code acting as a child process.
     55    if (bootstrap->XRE_ForkServer(&argc, &argv)) {
     56      // Return from the fork server in the fork server process.
     57      // Stop the fork server.
     58      bootstrap->NS_LogTerm();
     59      return 0;
     60    }
     61  }
     62 #endif
     63 
     64  // Register an external module to report on otherwise uncatchable
     65  // exceptions. Note that in child processes this must be called after Gecko
     66  // process type has been set.
     67  CrashReporter::RegisterRuntimeExceptionModule();
     68 
     69  // Make sure we unregister the runtime exception module before returning.
     70  auto unregisterRuntimeExceptionModule =
     71      MakeScopeExit([] { CrashReporter::UnregisterRuntimeExceptionModule(); });
     72 
     73 #ifdef HAS_DLL_BLOCKLIST
     74  uint32_t initFlags = eDllBlocklistInitFlagIsChildProcess;
     75  SetDllBlocklistProcessTypeFlags(initFlags, GetGeckoProcessType());
     76  DllBlocklist_Initialize(initFlags);
     77 #endif
     78 
     79  XREChildData childData;
     80 
     81 #if defined(XP_WIN) && defined(MOZ_SANDBOX)
     82  if (IsSandboxedProcess()) {
     83    childData.sandboxTargetServices =
     84        mozilla::sandboxing::GetInitializedTargetServices();
     85    if (!childData.sandboxTargetServices) {
     86      return 1;
     87    }
     88 
     89    childData.ProvideLogFunction = mozilla::sandboxing::ProvideLogFunction;
     90  }
     91 #endif
     92 
     93  nsresult rv = bootstrap->XRE_InitChildProcess(argc, argv, &childData);
     94 
     95 #if defined(DEBUG) && defined(HAS_DLL_BLOCKLIST)
     96  DllBlocklist_Shutdown();
     97 #endif
     98 
     99  return NS_FAILED(rv) ? 1 : 0;
    100 }