ipc/app/MozillaRuntimeMain.cpp

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
michael@0 2 * vim: sw=4 ts=4 et :
michael@0 3 * This Source Code Form is subject to the terms of the Mozilla Public
michael@0 4 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 6
michael@0 7 #include "nsXPCOM.h"
michael@0 8 #include "nsXULAppAPI.h"
michael@0 9
michael@0 10 // FIXME/cjones testing
michael@0 11 #if !defined(OS_WIN)
michael@0 12 #include <unistd.h>
michael@0 13 #endif
michael@0 14
michael@0 15 #ifdef XP_WIN
michael@0 16 #include <windows.h>
michael@0 17 // we want a wmain entry point
michael@0 18 // but we don't want its DLL load protection, because we'll handle it here
michael@0 19 #define XRE_DONT_PROTECT_DLL_LOAD
michael@0 20 #include "nsWindowsWMain.cpp"
michael@0 21 #include "nsSetDllDirectory.h"
michael@0 22 #endif
michael@0 23
michael@0 24 #if defined(XP_WIN) && defined(MOZ_CONTENT_SANDBOX)
michael@0 25 #include "sandbox/chromium/base/basictypes.h"
michael@0 26 #include "sandbox/win/src/sandbox.h"
michael@0 27 #include "sandbox/win/src/sandbox_factory.h"
michael@0 28 #include "mozilla/sandboxTarget.h"
michael@0 29 #endif
michael@0 30
michael@0 31 #ifdef MOZ_WIDGET_GONK
michael@0 32 # include <sys/time.h>
michael@0 33 # include <sys/resource.h>
michael@0 34
michael@0 35 # include <binder/ProcessState.h>
michael@0 36
michael@0 37 # ifdef LOGE_IF
michael@0 38 # undef LOGE_IF
michael@0 39 # endif
michael@0 40
michael@0 41 # include <android/log.h>
michael@0 42 # define LOGE_IF(cond, ...) \
michael@0 43 ( (CONDITION(cond)) \
michael@0 44 ? ((void)__android_log_print(ANDROID_LOG_ERROR, \
michael@0 45 "Gecko:MozillaRntimeMain", __VA_ARGS__)) \
michael@0 46 : (void)0 )
michael@0 47
michael@0 48 #endif
michael@0 49
michael@0 50 #ifdef MOZ_NUWA_PROCESS
michael@0 51 #include <binder/ProcessState.h>
michael@0 52 #include "ipc/Nuwa.h"
michael@0 53 #endif
michael@0 54
michael@0 55 #ifdef MOZ_WIDGET_GONK
michael@0 56 static void
michael@0 57 InitializeBinder(void *aDummy) {
michael@0 58 // Change thread priority to 0 only during calling ProcessState::self().
michael@0 59 // The priority is registered to binder driver and used for default Binder
michael@0 60 // Thread's priority.
michael@0 61 // To change the process's priority to small value need's root permission.
michael@0 62 int curPrio = getpriority(PRIO_PROCESS, 0);
michael@0 63 int err = setpriority(PRIO_PROCESS, 0, 0);
michael@0 64 MOZ_ASSERT(!err);
michael@0 65 LOGE_IF(err, "setpriority failed. Current process needs root permission.");
michael@0 66 android::ProcessState::self()->startThreadPool();
michael@0 67 setpriority(PRIO_PROCESS, 0, curPrio);
michael@0 68 }
michael@0 69 #endif
michael@0 70
michael@0 71 #if defined(XP_WIN) && defined(MOZ_CONTENT_SANDBOX)
michael@0 72 static bool gIsSandboxEnabled = false;
michael@0 73 void StartSandboxCallback()
michael@0 74 {
michael@0 75 if (gIsSandboxEnabled) {
michael@0 76 sandbox::TargetServices* target_service =
michael@0 77 sandbox::SandboxFactory::GetTargetServices();
michael@0 78 target_service->LowerToken();
michael@0 79 }
michael@0 80 }
michael@0 81 #endif
michael@0 82
michael@0 83 int
michael@0 84 main(int argc, char* argv[])
michael@0 85 {
michael@0 86 bool isNuwa = false;
michael@0 87 for (int i = 1; i < argc; i++) {
michael@0 88 isNuwa |= strcmp(argv[i], "-nuwa") == 0;
michael@0 89 #if defined(XP_WIN) && defined(MOZ_CONTENT_SANDBOX)
michael@0 90 gIsSandboxEnabled |= strcmp(argv[i], "-sandbox") == 0;
michael@0 91 #endif
michael@0 92 }
michael@0 93
michael@0 94 #ifdef MOZ_NUWA_PROCESS
michael@0 95 if (isNuwa) {
michael@0 96 PrepareNuwaProcess();
michael@0 97 }
michael@0 98 #endif
michael@0 99
michael@0 100 #ifdef MOZ_WIDGET_GONK
michael@0 101 // This creates a ThreadPool for binder ipc. A ThreadPool is necessary to
michael@0 102 // receive binder calls, though not necessary to send binder calls.
michael@0 103 // ProcessState::Self() also needs to be called once on the main thread to
michael@0 104 // register the main thread with the binder driver.
michael@0 105
michael@0 106 #ifdef MOZ_NUWA_PROCESS
michael@0 107 if (!isNuwa) {
michael@0 108 InitializeBinder(nullptr);
michael@0 109 } else {
michael@0 110 NuwaAddFinalConstructor(&InitializeBinder, nullptr);
michael@0 111 }
michael@0 112 #else
michael@0 113 InitializeBinder(nullptr);
michael@0 114 #endif
michael@0 115 #endif
michael@0 116
michael@0 117 // Check for the absolute minimum number of args we need to move
michael@0 118 // forward here. We expect the last arg to be the child process type.
michael@0 119 if (argc < 1)
michael@0 120 return 3;
michael@0 121 GeckoProcessType proctype = XRE_StringToChildProcessType(argv[--argc]);
michael@0 122
michael@0 123 #ifdef XP_WIN
michael@0 124 // For plugins, this is done in PluginProcessChild::Init, as we need to
michael@0 125 // avoid it for unsupported plugins. See PluginProcessChild::Init for
michael@0 126 // the details.
michael@0 127 if (proctype != GeckoProcessType_Plugin) {
michael@0 128 mozilla::SanitizeEnvironmentVariables();
michael@0 129 SetDllDirectory(L"");
michael@0 130 }
michael@0 131
michael@0 132 #ifdef MOZ_CONTENT_SANDBOX
michael@0 133 if (gIsSandboxEnabled) {
michael@0 134 sandbox::TargetServices* target_service =
michael@0 135 sandbox::SandboxFactory::GetTargetServices();
michael@0 136 if (!target_service) {
michael@0 137 return 1;
michael@0 138 }
michael@0 139
michael@0 140 sandbox::ResultCode result = target_service->Init();
michael@0 141 if (result != sandbox::SBOX_ALL_OK) {
michael@0 142 return 2;
michael@0 143 }
michael@0 144 mozilla::SandboxTarget::Instance()->SetStartSandboxCallback(StartSandboxCallback);
michael@0 145 }
michael@0 146 #endif
michael@0 147 #endif
michael@0 148
michael@0 149 nsresult rv = XRE_InitChildProcess(argc, argv, proctype);
michael@0 150 NS_ENSURE_SUCCESS(rv, 1);
michael@0 151
michael@0 152 return 0;
michael@0 153 }

mercurial