ipc/app/MozillaRuntimeMain.cpp

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/ipc/app/MozillaRuntimeMain.cpp	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,153 @@
     1.4 +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
     1.5 + * vim: sw=4 ts=4 et :
     1.6 + * This Source Code Form is subject to the terms of the Mozilla Public
     1.7 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.8 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.9 +
    1.10 +#include "nsXPCOM.h"
    1.11 +#include "nsXULAppAPI.h"
    1.12 +
    1.13 +// FIXME/cjones testing
    1.14 +#if !defined(OS_WIN)
    1.15 +#include <unistd.h>
    1.16 +#endif
    1.17 +
    1.18 +#ifdef XP_WIN
    1.19 +#include <windows.h>
    1.20 +// we want a wmain entry point
    1.21 +// but we don't want its DLL load protection, because we'll handle it here
    1.22 +#define XRE_DONT_PROTECT_DLL_LOAD
    1.23 +#include "nsWindowsWMain.cpp"
    1.24 +#include "nsSetDllDirectory.h"
    1.25 +#endif
    1.26 +
    1.27 +#if defined(XP_WIN) && defined(MOZ_CONTENT_SANDBOX)
    1.28 +#include "sandbox/chromium/base/basictypes.h"
    1.29 +#include "sandbox/win/src/sandbox.h"
    1.30 +#include "sandbox/win/src/sandbox_factory.h"
    1.31 +#include "mozilla/sandboxTarget.h"
    1.32 +#endif
    1.33 +
    1.34 +#ifdef MOZ_WIDGET_GONK
    1.35 +# include <sys/time.h>
    1.36 +# include <sys/resource.h> 
    1.37 +
    1.38 +# include <binder/ProcessState.h>
    1.39 +
    1.40 +# ifdef LOGE_IF
    1.41 +#  undef LOGE_IF
    1.42 +# endif
    1.43 +
    1.44 +# include <android/log.h>
    1.45 +# define LOGE_IF(cond, ...) \
    1.46 +     ( (CONDITION(cond)) \
    1.47 +     ? ((void)__android_log_print(ANDROID_LOG_ERROR, \
    1.48 +       "Gecko:MozillaRntimeMain", __VA_ARGS__)) \
    1.49 +     : (void)0 )
    1.50 +
    1.51 +#endif
    1.52 +
    1.53 +#ifdef MOZ_NUWA_PROCESS
    1.54 +#include <binder/ProcessState.h>
    1.55 +#include "ipc/Nuwa.h"
    1.56 +#endif
    1.57 +
    1.58 +#ifdef MOZ_WIDGET_GONK
    1.59 +static void
    1.60 +InitializeBinder(void *aDummy) {
    1.61 +    // Change thread priority to 0 only during calling ProcessState::self().
    1.62 +    // The priority is registered to binder driver and used for default Binder
    1.63 +    // Thread's priority. 
    1.64 +    // To change the process's priority to small value need's root permission.
    1.65 +    int curPrio = getpriority(PRIO_PROCESS, 0);
    1.66 +    int err = setpriority(PRIO_PROCESS, 0, 0);
    1.67 +    MOZ_ASSERT(!err);
    1.68 +    LOGE_IF(err, "setpriority failed. Current process needs root permission.");
    1.69 +    android::ProcessState::self()->startThreadPool();
    1.70 +    setpriority(PRIO_PROCESS, 0, curPrio);
    1.71 +}
    1.72 +#endif
    1.73 +
    1.74 +#if defined(XP_WIN) && defined(MOZ_CONTENT_SANDBOX)
    1.75 +static bool gIsSandboxEnabled = false;
    1.76 +void StartSandboxCallback()
    1.77 +{
    1.78 +    if (gIsSandboxEnabled) {
    1.79 +        sandbox::TargetServices* target_service =
    1.80 +            sandbox::SandboxFactory::GetTargetServices();
    1.81 +        target_service->LowerToken();
    1.82 +    }
    1.83 +}
    1.84 +#endif
    1.85 +
    1.86 +int
    1.87 +main(int argc, char* argv[])
    1.88 +{
    1.89 +    bool isNuwa = false;
    1.90 +    for (int i = 1; i < argc; i++) {
    1.91 +        isNuwa |= strcmp(argv[i], "-nuwa") == 0;
    1.92 +#if defined(XP_WIN) && defined(MOZ_CONTENT_SANDBOX)
    1.93 +        gIsSandboxEnabled |= strcmp(argv[i], "-sandbox") == 0;
    1.94 +#endif
    1.95 +    }
    1.96 +
    1.97 +#ifdef MOZ_NUWA_PROCESS
    1.98 +    if (isNuwa) {
    1.99 +        PrepareNuwaProcess();
   1.100 +    }
   1.101 +#endif
   1.102 +
   1.103 +#ifdef MOZ_WIDGET_GONK
   1.104 +    // This creates a ThreadPool for binder ipc. A ThreadPool is necessary to
   1.105 +    // receive binder calls, though not necessary to send binder calls.
   1.106 +    // ProcessState::Self() also needs to be called once on the main thread to
   1.107 +    // register the main thread with the binder driver.
   1.108 +
   1.109 +#ifdef MOZ_NUWA_PROCESS
   1.110 +    if (!isNuwa) {
   1.111 +        InitializeBinder(nullptr);
   1.112 +    } else {
   1.113 +        NuwaAddFinalConstructor(&InitializeBinder, nullptr);
   1.114 +    }
   1.115 +#else
   1.116 +    InitializeBinder(nullptr);
   1.117 +#endif
   1.118 +#endif
   1.119 +
   1.120 +    // Check for the absolute minimum number of args we need to move
   1.121 +    // forward here. We expect the last arg to be the child process type.
   1.122 +    if (argc < 1)
   1.123 +      return 3;
   1.124 +    GeckoProcessType proctype = XRE_StringToChildProcessType(argv[--argc]);
   1.125 +
   1.126 +#ifdef XP_WIN
   1.127 +    // For plugins, this is done in PluginProcessChild::Init, as we need to
   1.128 +    // avoid it for unsupported plugins.  See PluginProcessChild::Init for
   1.129 +    // the details.
   1.130 +    if (proctype != GeckoProcessType_Plugin) {
   1.131 +        mozilla::SanitizeEnvironmentVariables();
   1.132 +        SetDllDirectory(L"");
   1.133 +    }
   1.134 +
   1.135 +#ifdef MOZ_CONTENT_SANDBOX
   1.136 +    if (gIsSandboxEnabled) {
   1.137 +        sandbox::TargetServices* target_service =
   1.138 +            sandbox::SandboxFactory::GetTargetServices();
   1.139 +        if (!target_service) {
   1.140 +            return 1;
   1.141 +        }
   1.142 +
   1.143 +        sandbox::ResultCode result = target_service->Init();
   1.144 +        if (result != sandbox::SBOX_ALL_OK) {
   1.145 +           return 2;
   1.146 +        }
   1.147 +        mozilla::SandboxTarget::Instance()->SetStartSandboxCallback(StartSandboxCallback);
   1.148 +    }
   1.149 +#endif
   1.150 +#endif
   1.151 +
   1.152 +    nsresult rv = XRE_InitChildProcess(argc, argv, proctype);
   1.153 +    NS_ENSURE_SUCCESS(rv, 1);
   1.154 +
   1.155 +    return 0;
   1.156 +}

mercurial