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