2009-11-19 00:43:53 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*-
|
2009-12-07 09:04:00 +03:00
|
|
|
*/
|
2012-05-21 15:12:37 +04:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2009-06-29 22:38:29 +04:00
|
|
|
|
|
|
|
#include "GeckoChildProcessHost.h"
|
|
|
|
|
2013-10-31 03:58:52 +04:00
|
|
|
#if defined(XP_WIN) && defined(MOZ_CONTENT_SANDBOX)
|
|
|
|
#include "sandboxBroker.h"
|
|
|
|
#endif
|
|
|
|
|
2009-06-29 22:38:29 +04:00
|
|
|
#include "base/command_line.h"
|
|
|
|
#include "base/path_service.h"
|
2009-07-10 23:03:09 +04:00
|
|
|
#include "base/string_util.h"
|
2009-06-29 22:38:29 +04:00
|
|
|
#include "chrome/common/chrome_switches.h"
|
2009-11-19 22:03:30 +03:00
|
|
|
#include "chrome/common/process_watcher.h"
|
2011-10-14 11:54:46 +04:00
|
|
|
#ifdef MOZ_WIDGET_COCOA
|
2010-09-10 21:09:30 +04:00
|
|
|
#include "chrome/common/mach_ipc_mac.h"
|
|
|
|
#include "base/rand_util.h"
|
2010-09-16 10:09:19 +04:00
|
|
|
#include "nsILocalFileMac.h"
|
2010-09-10 21:09:30 +04:00
|
|
|
#endif
|
2009-06-29 22:38:29 +04:00
|
|
|
|
2014-04-04 23:16:16 +04:00
|
|
|
#include "MainThreadUtils.h"
|
2009-10-28 00:52:37 +03:00
|
|
|
#include "prprf.h"
|
2011-03-26 02:40:50 +03:00
|
|
|
#include "prenv.h"
|
2009-10-28 00:52:37 +03:00
|
|
|
|
2010-01-13 00:14:38 +03:00
|
|
|
#include "nsExceptionHandler.h"
|
|
|
|
|
2010-04-10 00:03:08 +04:00
|
|
|
#include "nsDirectoryServiceDefs.h"
|
|
|
|
#include "nsIFile.h"
|
|
|
|
|
2014-04-04 23:16:16 +04:00
|
|
|
#include "mozilla/ClearOnShutdown.h"
|
2010-02-04 01:17:09 +03:00
|
|
|
#include "mozilla/ipc/BrowserProcessSubThread.h"
|
2010-06-26 00:29:43 +04:00
|
|
|
#include "mozilla/Omnijar.h"
|
2010-06-25 22:39:02 +04:00
|
|
|
#include <sys/stat.h>
|
2009-07-20 22:14:41 +04:00
|
|
|
|
2010-05-15 04:25:10 +04:00
|
|
|
#ifdef XP_WIN
|
|
|
|
#include "nsIWinTaskbar.h"
|
|
|
|
#define NS_TASKBAR_CONTRACTID "@mozilla.org/windows-taskbar;1"
|
|
|
|
#endif
|
|
|
|
|
2013-05-31 17:16:54 +04:00
|
|
|
#include "nsTArray.h"
|
|
|
|
#include "nsClassHashtable.h"
|
|
|
|
#include "nsHashKeys.h"
|
|
|
|
|
2011-04-29 23:21:57 +04:00
|
|
|
using mozilla::MonitorAutoLock;
|
2009-06-29 22:38:29 +04:00
|
|
|
using mozilla::ipc::GeckoChildProcessHost;
|
|
|
|
|
2012-07-12 16:51:57 +04:00
|
|
|
#ifdef ANDROID
|
2011-01-20 20:01:31 +03:00
|
|
|
// Like its predecessor in nsExceptionHandler.cpp, this is
|
|
|
|
// the magic number of a file descriptor remapping we must
|
|
|
|
// preserve for the child process.
|
|
|
|
static const int kMagicAndroidSystemPropFd = 5;
|
|
|
|
#endif
|
|
|
|
|
2012-07-27 08:42:56 +04:00
|
|
|
static const bool kLowRightsSubprocesses =
|
|
|
|
// We currently only attempt to drop privileges on gonk, because we
|
|
|
|
// have no plugins or extensions to worry about breaking.
|
|
|
|
#ifdef MOZ_WIDGET_GONK
|
|
|
|
true
|
|
|
|
#else
|
|
|
|
false
|
|
|
|
#endif
|
|
|
|
;
|
|
|
|
|
2014-04-04 23:16:16 +04:00
|
|
|
mozilla::StaticRefPtr<nsIFile> GeckoChildProcessHost::sGreDir;
|
|
|
|
|
2011-06-03 22:33:55 +04:00
|
|
|
static bool
|
|
|
|
ShouldHaveDirectoryService()
|
|
|
|
{
|
|
|
|
return GeckoProcessType_Default == XRE_GetProcessType();
|
|
|
|
}
|
|
|
|
|
2009-07-20 22:14:41 +04:00
|
|
|
template<>
|
|
|
|
struct RunnableMethodTraits<GeckoChildProcessHost>
|
|
|
|
{
|
|
|
|
static void RetainCallee(GeckoChildProcessHost* obj) { }
|
|
|
|
static void ReleaseCallee(GeckoChildProcessHost* obj) { }
|
|
|
|
};
|
|
|
|
|
2013-01-18 00:06:36 +04:00
|
|
|
/*static*/
|
|
|
|
base::ChildPrivileges
|
|
|
|
GeckoChildProcessHost::DefaultChildPrivileges()
|
|
|
|
{
|
|
|
|
return (kLowRightsSubprocesses ?
|
|
|
|
base::PRIVILEGES_UNPRIVILEGED : base::PRIVILEGES_INHERIT);
|
|
|
|
}
|
|
|
|
|
2009-09-03 04:18:27 +04:00
|
|
|
GeckoChildProcessHost::GeckoChildProcessHost(GeckoProcessType aProcessType,
|
2012-08-29 16:24:48 +04:00
|
|
|
ChildPrivileges aPrivileges)
|
2009-07-10 23:03:09 +04:00
|
|
|
: ChildProcessHost(RENDER_PROCESS), // FIXME/cjones: we should own this enum
|
2009-07-20 22:14:41 +04:00
|
|
|
mProcessType(aProcessType),
|
2013-11-02 06:09:45 +04:00
|
|
|
mSandboxEnabled(true),
|
2012-08-29 16:24:48 +04:00
|
|
|
mPrivileges(aPrivileges),
|
2011-04-29 23:21:57 +04:00
|
|
|
mMonitor("mozilla.ipc.GeckChildProcessHost.mMonitor"),
|
2012-08-08 03:29:32 +04:00
|
|
|
mProcessState(CREATING_CHANNEL),
|
2012-08-29 16:24:48 +04:00
|
|
|
mDelegate(nullptr),
|
2009-10-28 00:52:37 +03:00
|
|
|
mChildProcessHandle(0)
|
2011-10-14 11:54:46 +04:00
|
|
|
#if defined(MOZ_WIDGET_COCOA)
|
2010-08-27 17:32:45 +04:00
|
|
|
, mChildTask(MACH_PORT_NULL)
|
|
|
|
#endif
|
2009-07-20 22:14:41 +04:00
|
|
|
{
|
2009-11-10 01:56:55 +03:00
|
|
|
MOZ_COUNT_CTOR(GeckoChildProcessHost);
|
|
|
|
}
|
|
|
|
|
|
|
|
GeckoChildProcessHost::~GeckoChildProcessHost()
|
2009-12-19 03:09:56 +03:00
|
|
|
|
2009-11-10 01:56:55 +03:00
|
|
|
{
|
2009-12-19 03:09:56 +03:00
|
|
|
AssertIOThread();
|
|
|
|
|
|
|
|
MOZ_COUNT_DTOR(GeckoChildProcessHost);
|
2009-11-19 22:03:30 +03:00
|
|
|
|
2009-12-19 03:09:56 +03:00
|
|
|
if (mChildProcessHandle > 0)
|
2010-01-13 23:57:52 +03:00
|
|
|
ProcessWatcher::EnsureProcessTerminated(mChildProcessHandle
|
2010-01-22 18:21:33 +03:00
|
|
|
#if defined(NS_BUILD_REFCNT_LOGGING)
|
2010-01-13 23:57:52 +03:00
|
|
|
, false // don't "force"
|
|
|
|
#endif
|
|
|
|
);
|
2010-08-27 17:32:45 +04:00
|
|
|
|
2011-10-14 11:54:46 +04:00
|
|
|
#if defined(MOZ_WIDGET_COCOA)
|
2010-08-27 17:32:45 +04:00
|
|
|
if (mChildTask != MACH_PORT_NULL)
|
|
|
|
mach_port_deallocate(mach_task_self(), mChildTask);
|
|
|
|
#endif
|
2009-07-20 22:14:41 +04:00
|
|
|
}
|
|
|
|
|
2014-04-04 23:16:16 +04:00
|
|
|
//static
|
|
|
|
void
|
|
|
|
GeckoChildProcessHost::GetPathToBinary(FilePath& exePath)
|
2010-09-16 10:09:19 +04:00
|
|
|
{
|
2011-06-03 22:33:55 +04:00
|
|
|
if (ShouldHaveDirectoryService()) {
|
2014-05-18 03:53:03 +04:00
|
|
|
MOZ_ASSERT(sGreDir);
|
2014-04-04 23:16:16 +04:00
|
|
|
if (sGreDir) {
|
2012-07-11 02:31:04 +04:00
|
|
|
#ifdef OS_WIN
|
2014-04-04 23:16:16 +04:00
|
|
|
nsString path;
|
|
|
|
MOZ_ALWAYS_TRUE(NS_SUCCEEDED(sGreDir->GetPath(path)));
|
2012-07-11 02:31:04 +04:00
|
|
|
#else
|
2014-04-04 23:16:16 +04:00
|
|
|
nsCString path;
|
|
|
|
MOZ_ALWAYS_TRUE(NS_SUCCEEDED(sGreDir->GetNativePath(path)));
|
2012-07-11 02:31:04 +04:00
|
|
|
#endif
|
2014-04-04 23:16:16 +04:00
|
|
|
exePath = FilePath(path.get());
|
2011-10-14 11:54:46 +04:00
|
|
|
#ifdef MOZ_WIDGET_COCOA
|
2014-04-04 23:16:16 +04:00
|
|
|
// We need to use an App Bundle on OS X so that we can hide
|
|
|
|
// the dock icon. See Bug 557225.
|
|
|
|
exePath = exePath.AppendASCII(MOZ_CHILD_PROCESS_BUNDLE);
|
2011-09-07 08:51:38 +04:00
|
|
|
#endif
|
2011-06-03 22:33:55 +04:00
|
|
|
}
|
2010-09-16 10:09:19 +04:00
|
|
|
}
|
2011-09-07 08:51:38 +04:00
|
|
|
|
2011-06-03 22:33:55 +04:00
|
|
|
if (exePath.empty()) {
|
2012-07-11 02:31:04 +04:00
|
|
|
#ifdef OS_WIN
|
|
|
|
exePath = FilePath::FromWStringHack(CommandLine::ForCurrentProcess()->program());
|
|
|
|
#else
|
2010-09-16 10:09:19 +04:00
|
|
|
exePath = FilePath(CommandLine::ForCurrentProcess()->argv()[0]);
|
2012-07-11 02:31:04 +04:00
|
|
|
#endif
|
2010-09-16 10:09:19 +04:00
|
|
|
exePath = exePath.DirName();
|
|
|
|
}
|
|
|
|
|
|
|
|
exePath = exePath.AppendASCII(MOZ_CHILD_PROCESS_NAME);
|
|
|
|
}
|
|
|
|
|
2011-10-14 11:54:46 +04:00
|
|
|
#ifdef MOZ_WIDGET_COCOA
|
2010-09-16 10:09:19 +04:00
|
|
|
class AutoCFTypeObject {
|
|
|
|
public:
|
|
|
|
AutoCFTypeObject(CFTypeRef object)
|
|
|
|
{
|
|
|
|
mObject = object;
|
|
|
|
}
|
|
|
|
~AutoCFTypeObject()
|
|
|
|
{
|
|
|
|
::CFRelease(mObject);
|
|
|
|
}
|
|
|
|
private:
|
|
|
|
CFTypeRef mObject;
|
|
|
|
};
|
|
|
|
#endif
|
|
|
|
|
2012-09-17 12:37:20 +04:00
|
|
|
nsresult GeckoChildProcessHost::GetArchitecturesForBinary(const char *path, uint32_t *result)
|
2010-09-16 10:09:19 +04:00
|
|
|
{
|
|
|
|
*result = 0;
|
|
|
|
|
2011-10-14 11:54:46 +04:00
|
|
|
#ifdef MOZ_WIDGET_COCOA
|
2010-09-16 10:09:19 +04:00
|
|
|
CFURLRef url = ::CFURLCreateFromFileSystemRepresentation(kCFAllocatorDefault,
|
|
|
|
(const UInt8*)path,
|
|
|
|
strlen(path),
|
|
|
|
false);
|
|
|
|
if (!url) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
AutoCFTypeObject autoPluginContainerURL(url);
|
|
|
|
|
|
|
|
CFArrayRef pluginContainerArchs = ::CFBundleCopyExecutableArchitecturesForURL(url);
|
|
|
|
if (!pluginContainerArchs) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
AutoCFTypeObject autoPluginContainerArchs(pluginContainerArchs);
|
|
|
|
|
|
|
|
CFIndex pluginArchCount = ::CFArrayGetCount(pluginContainerArchs);
|
|
|
|
for (CFIndex i = 0; i < pluginArchCount; i++) {
|
|
|
|
CFNumberRef currentArch = static_cast<CFNumberRef>(::CFArrayGetValueAtIndex(pluginContainerArchs, i));
|
|
|
|
int currentArchInt = 0;
|
|
|
|
if (!::CFNumberGetValue(currentArch, kCFNumberIntType, ¤tArchInt)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
switch (currentArchInt) {
|
|
|
|
case kCFBundleExecutableArchitectureI386:
|
|
|
|
*result |= base::PROCESS_ARCH_I386;
|
|
|
|
break;
|
|
|
|
case kCFBundleExecutableArchitectureX86_64:
|
|
|
|
*result |= base::PROCESS_ARCH_X86_64;
|
|
|
|
break;
|
|
|
|
case kCFBundleExecutableArchitecturePPC:
|
|
|
|
*result |= base::PROCESS_ARCH_PPC;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return (*result ? NS_OK : NS_ERROR_FAILURE);
|
|
|
|
#else
|
|
|
|
return NS_ERROR_NOT_IMPLEMENTED;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2012-09-17 12:37:20 +04:00
|
|
|
uint32_t GeckoChildProcessHost::GetSupportedArchitecturesForProcessType(GeckoProcessType type)
|
2010-09-16 10:09:19 +04:00
|
|
|
{
|
2011-10-14 11:54:46 +04:00
|
|
|
#ifdef MOZ_WIDGET_COCOA
|
2010-09-16 10:09:19 +04:00
|
|
|
if (type == GeckoProcessType_Plugin) {
|
2014-04-04 23:16:16 +04:00
|
|
|
|
2010-09-16 10:09:19 +04:00
|
|
|
// Cache this, it shouldn't ever change.
|
2012-09-17 12:37:20 +04:00
|
|
|
static uint32_t pluginContainerArchs = 0;
|
2010-09-16 10:09:19 +04:00
|
|
|
if (pluginContainerArchs == 0) {
|
2014-04-04 23:16:16 +04:00
|
|
|
CacheGreDir();
|
2010-09-16 10:09:19 +04:00
|
|
|
FilePath exePath;
|
|
|
|
GetPathToBinary(exePath);
|
|
|
|
nsresult rv = GetArchitecturesForBinary(exePath.value().c_str(), &pluginContainerArchs);
|
|
|
|
NS_ASSERTION(NS_SUCCEEDED(rv) && pluginContainerArchs != 0, "Getting architecture of plugin container failed!");
|
|
|
|
if (NS_FAILED(rv) || pluginContainerArchs == 0) {
|
|
|
|
pluginContainerArchs = base::GetCurrentProcessArchitecture();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return pluginContainerArchs;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return base::GetCurrentProcessArchitecture();
|
|
|
|
}
|
|
|
|
|
2012-08-08 03:29:32 +04:00
|
|
|
void
|
|
|
|
GeckoChildProcessHost::PrepareLaunch()
|
|
|
|
{
|
|
|
|
#ifdef MOZ_CRASHREPORTER
|
|
|
|
if (CrashReporter::GetEnabled()) {
|
|
|
|
CrashReporter::OOPInit();
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef XP_WIN
|
|
|
|
InitWindowsGroupID();
|
|
|
|
#endif
|
2014-04-04 23:16:16 +04:00
|
|
|
CacheGreDir();
|
|
|
|
}
|
|
|
|
|
|
|
|
//static
|
|
|
|
void
|
|
|
|
GeckoChildProcessHost::CacheGreDir()
|
|
|
|
{
|
2014-05-18 03:53:03 +04:00
|
|
|
if (sGreDir) {
|
|
|
|
return;
|
|
|
|
}
|
2014-04-04 23:16:16 +04:00
|
|
|
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
|
|
|
|
if (ShouldHaveDirectoryService()) {
|
|
|
|
nsCOMPtr<nsIProperties> directoryService(do_GetService(NS_DIRECTORY_SERVICE_CONTRACTID));
|
|
|
|
MOZ_ASSERT(directoryService, "Expected XPCOM to be available");
|
|
|
|
if (directoryService) {
|
|
|
|
// getter_AddRefs doesn't work with StaticRefPtr, so we need to store the
|
|
|
|
// result in an nsCOMPtr and copy it over.
|
|
|
|
nsCOMPtr<nsIFile> greDir;
|
|
|
|
nsresult rv = directoryService->Get(NS_GRE_DIR, NS_GET_IID(nsIFile), getter_AddRefs(greDir));
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
sGreDir = greDir;
|
|
|
|
mozilla::ClearOnShutdown(&sGreDir);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-08-08 03:29:32 +04:00
|
|
|
}
|
|
|
|
|
2010-07-01 07:07:50 +04:00
|
|
|
#ifdef XP_WIN
|
|
|
|
void GeckoChildProcessHost::InitWindowsGroupID()
|
|
|
|
{
|
|
|
|
// On Win7+, pass the application user model to the child, so it can
|
|
|
|
// register with it. This insures windows created by the container
|
|
|
|
// properly group with the parent app on the Win7 taskbar.
|
|
|
|
nsCOMPtr<nsIWinTaskbar> taskbarInfo =
|
|
|
|
do_GetService(NS_TASKBAR_CONTRACTID);
|
|
|
|
if (taskbarInfo) {
|
2011-09-29 10:19:26 +04:00
|
|
|
bool isSupported = false;
|
2010-07-01 07:07:50 +04:00
|
|
|
taskbarInfo->GetAvailable(&isSupported);
|
|
|
|
nsAutoString appId;
|
|
|
|
if (isSupported && NS_SUCCEEDED(taskbarInfo->GetDefaultGroupId(appId))) {
|
|
|
|
mGroupId.Append(appId);
|
|
|
|
} else {
|
|
|
|
mGroupId.AssignLiteral("-");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2009-07-20 22:14:41 +04:00
|
|
|
bool
|
2010-09-16 10:09:19 +04:00
|
|
|
GeckoChildProcessHost::SyncLaunch(std::vector<std::string> aExtraOpts, int aTimeoutMs, base::ProcessArchitecture arch)
|
2009-06-29 22:38:29 +04:00
|
|
|
{
|
2012-08-08 03:29:32 +04:00
|
|
|
PrepareLaunch();
|
2010-07-01 07:07:50 +04:00
|
|
|
|
2010-08-11 01:14:10 +04:00
|
|
|
PRIntervalTime timeoutTicks = (aTimeoutMs > 0) ?
|
|
|
|
PR_MillisecondsToInterval(aTimeoutMs) : PR_INTERVAL_NO_TIMEOUT;
|
2010-04-20 22:43:51 +04:00
|
|
|
MessageLoop* ioLoop = XRE_GetIOMessageLoop();
|
2009-11-06 03:24:27 +03:00
|
|
|
NS_ASSERTION(MessageLoop::current() != ioLoop, "sync launch from the IO thread NYI");
|
2009-07-20 22:14:41 +04:00
|
|
|
|
|
|
|
ioLoop->PostTask(FROM_HERE,
|
|
|
|
NewRunnableMethod(this,
|
2013-05-31 17:16:54 +04:00
|
|
|
&GeckoChildProcessHost::RunPerformAsyncLaunch,
|
2010-09-16 10:09:19 +04:00
|
|
|
aExtraOpts, arch));
|
2009-07-20 22:14:41 +04:00
|
|
|
// NB: this uses a different mechanism than the chromium parent
|
|
|
|
// class.
|
2011-04-29 23:21:57 +04:00
|
|
|
MonitorAutoLock lock(mMonitor);
|
2010-08-11 01:14:10 +04:00
|
|
|
PRIntervalTime waitStart = PR_IntervalNow();
|
|
|
|
PRIntervalTime current;
|
|
|
|
|
|
|
|
// We'll receive several notifications, we need to exit when we
|
|
|
|
// have either successfully launched or have timed out.
|
2014-01-12 07:51:00 +04:00
|
|
|
while (mProcessState != PROCESS_CONNECTED) {
|
|
|
|
// If there was an error then return it, don't wait out the timeout.
|
|
|
|
if (mProcessState == PROCESS_ERROR) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2011-04-29 23:21:57 +04:00
|
|
|
lock.Wait(timeoutTicks);
|
2010-08-11 01:14:10 +04:00
|
|
|
|
|
|
|
if (timeoutTicks != PR_INTERVAL_NO_TIMEOUT) {
|
|
|
|
current = PR_IntervalNow();
|
|
|
|
PRIntervalTime elapsed = current - waitStart;
|
|
|
|
if (elapsed > timeoutTicks) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
timeoutTicks = timeoutTicks - elapsed;
|
|
|
|
waitStart = current;
|
|
|
|
}
|
2009-07-20 22:14:41 +04:00
|
|
|
}
|
|
|
|
|
2012-08-08 03:29:32 +04:00
|
|
|
return mProcessState == PROCESS_CONNECTED;
|
2009-06-29 22:38:29 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2009-11-06 03:24:27 +03:00
|
|
|
GeckoChildProcessHost::AsyncLaunch(std::vector<std::string> aExtraOpts)
|
2009-11-19 00:43:53 +03:00
|
|
|
{
|
2012-08-08 03:29:32 +04:00
|
|
|
PrepareLaunch();
|
2010-07-01 07:07:50 +04:00
|
|
|
|
2010-04-20 22:43:51 +04:00
|
|
|
MessageLoop* ioLoop = XRE_GetIOMessageLoop();
|
2009-11-19 00:43:53 +03:00
|
|
|
ioLoop->PostTask(FROM_HERE,
|
|
|
|
NewRunnableMethod(this,
|
2013-05-31 17:16:54 +04:00
|
|
|
&GeckoChildProcessHost::RunPerformAsyncLaunch,
|
2010-09-16 10:09:19 +04:00
|
|
|
aExtraOpts, base::GetCurrentProcessArchitecture()));
|
2009-11-19 00:43:53 +03:00
|
|
|
|
|
|
|
// This may look like the sync launch wait, but we only delay as
|
|
|
|
// long as it takes to create the channel.
|
2011-04-29 23:21:57 +04:00
|
|
|
MonitorAutoLock lock(mMonitor);
|
2012-08-08 03:29:32 +04:00
|
|
|
while (mProcessState < CHANNEL_INITIALIZED) {
|
2011-04-29 23:21:57 +04:00
|
|
|
lock.Wait();
|
2009-11-19 00:43:53 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-08-08 03:29:32 +04:00
|
|
|
bool
|
|
|
|
GeckoChildProcessHost::LaunchAndWaitForProcessHandle(StringVector aExtraOpts)
|
|
|
|
{
|
|
|
|
PrepareLaunch();
|
|
|
|
|
|
|
|
MessageLoop* ioLoop = XRE_GetIOMessageLoop();
|
|
|
|
ioLoop->PostTask(FROM_HERE,
|
|
|
|
NewRunnableMethod(this,
|
2013-05-31 17:16:54 +04:00
|
|
|
&GeckoChildProcessHost::RunPerformAsyncLaunch,
|
2012-08-08 03:29:32 +04:00
|
|
|
aExtraOpts, base::GetCurrentProcessArchitecture()));
|
|
|
|
|
|
|
|
MonitorAutoLock lock(mMonitor);
|
|
|
|
while (mProcessState < PROCESS_CREATED) {
|
|
|
|
lock.Wait();
|
|
|
|
}
|
|
|
|
MOZ_ASSERT(mProcessState == PROCESS_ERROR || mChildProcessHandle);
|
|
|
|
|
|
|
|
return mProcessState < PROCESS_ERROR;
|
|
|
|
}
|
|
|
|
|
2009-11-19 00:43:53 +03:00
|
|
|
void
|
|
|
|
GeckoChildProcessHost::InitializeChannel()
|
|
|
|
{
|
|
|
|
CreateChannel();
|
|
|
|
|
2011-04-29 23:21:57 +04:00
|
|
|
MonitorAutoLock lock(mMonitor);
|
2012-08-08 03:29:32 +04:00
|
|
|
mProcessState = CHANNEL_INITIALIZED;
|
2011-04-29 23:21:57 +04:00
|
|
|
lock.Notify();
|
2009-11-19 00:43:53 +03:00
|
|
|
}
|
|
|
|
|
2012-12-28 13:45:16 +04:00
|
|
|
void
|
|
|
|
GeckoChildProcessHost::Join()
|
|
|
|
{
|
|
|
|
AssertIOThread();
|
|
|
|
|
|
|
|
if (!mChildProcessHandle) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If this fails, there's nothing we can do.
|
|
|
|
base::KillProcess(mChildProcessHandle, 0, /*wait*/true);
|
2014-03-21 16:50:07 +04:00
|
|
|
SetAlreadyDead();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
GeckoChildProcessHost::SetAlreadyDead()
|
|
|
|
{
|
2012-12-28 13:45:16 +04:00
|
|
|
mChildProcessHandle = 0;
|
|
|
|
}
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t GeckoChildProcessHost::mChildCounter = 0;
|
2011-03-26 02:40:50 +03:00
|
|
|
|
|
|
|
//
|
|
|
|
// Wrapper function for handling GECKO_SEPARATE_NSPR_LOGS
|
|
|
|
//
|
2009-11-19 00:43:53 +03:00
|
|
|
bool
|
2010-09-16 10:09:19 +04:00
|
|
|
GeckoChildProcessHost::PerformAsyncLaunch(std::vector<std::string> aExtraOpts, base::ProcessArchitecture arch)
|
2011-03-26 02:40:50 +03:00
|
|
|
{
|
2014-03-05 17:01:09 +04:00
|
|
|
// If NSPR log files are not requested, we're done.
|
2011-03-26 02:40:50 +03:00
|
|
|
const char* origLogName = PR_GetEnv("NSPR_LOG_FILE");
|
2014-03-05 17:01:09 +04:00
|
|
|
if (!origLogName) {
|
2011-03-26 02:40:50 +03:00
|
|
|
return PerformAsyncLaunchInternal(aExtraOpts, arch);
|
|
|
|
}
|
|
|
|
|
|
|
|
// We currently have no portable way to launch child with environment
|
|
|
|
// different than parent. So temporarily change NSPR_LOG_FILE so child
|
|
|
|
// inherits value we want it to have. (NSPR only looks at NSPR_LOG_FILE at
|
|
|
|
// startup, so it's 'safe' to play with the parent's environment this way.)
|
2012-09-02 06:35:17 +04:00
|
|
|
nsAutoCString setChildLogName("NSPR_LOG_FILE=");
|
2011-03-26 02:40:50 +03:00
|
|
|
setChildLogName.Append(origLogName);
|
|
|
|
|
|
|
|
// remember original value so we can restore it.
|
|
|
|
// - buffer needs to be permanently allocated for PR_SetEnv()
|
|
|
|
// - Note: this code is not called re-entrantly, nor are restoreOrigLogName
|
|
|
|
// or mChildCounter touched by any other thread, so this is safe.
|
|
|
|
static char* restoreOrigLogName = 0;
|
|
|
|
if (!restoreOrigLogName)
|
2011-06-10 00:15:10 +04:00
|
|
|
restoreOrigLogName = strdup(setChildLogName.get());
|
2011-03-26 02:40:50 +03:00
|
|
|
|
|
|
|
// Append child-specific postfix to name
|
|
|
|
setChildLogName.AppendLiteral(".child-");
|
|
|
|
setChildLogName.AppendInt(++mChildCounter);
|
|
|
|
|
|
|
|
// Passing temporary to PR_SetEnv is ok here because env gets copied
|
|
|
|
// by exec, etc., to permanent storage in child when process launched.
|
2011-06-10 00:15:10 +04:00
|
|
|
PR_SetEnv(setChildLogName.get());
|
2011-03-26 02:40:50 +03:00
|
|
|
bool retval = PerformAsyncLaunchInternal(aExtraOpts, arch);
|
|
|
|
|
|
|
|
// Revert to original value
|
|
|
|
PR_SetEnv(restoreOrigLogName);
|
|
|
|
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
2013-05-31 17:16:54 +04:00
|
|
|
bool
|
|
|
|
GeckoChildProcessHost::RunPerformAsyncLaunch(std::vector<std::string> aExtraOpts,
|
|
|
|
base::ProcessArchitecture aArch)
|
|
|
|
{
|
|
|
|
InitializeChannel();
|
|
|
|
return PerformAsyncLaunch(aExtraOpts, aArch);
|
|
|
|
}
|
|
|
|
|
2012-12-18 20:24:42 +04:00
|
|
|
void
|
|
|
|
#if defined(XP_WIN)
|
|
|
|
AddAppDirToCommandLine(CommandLine& aCmdLine)
|
|
|
|
#else
|
|
|
|
AddAppDirToCommandLine(std::vector<std::string>& aCmdLine)
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
// Content processes need access to application resources, so pass
|
|
|
|
// the full application directory path to the child process.
|
|
|
|
if (ShouldHaveDirectoryService()) {
|
|
|
|
nsCOMPtr<nsIProperties> directoryService(do_GetService(NS_DIRECTORY_SERVICE_CONTRACTID));
|
|
|
|
NS_ASSERTION(directoryService, "Expected XPCOM to be available");
|
|
|
|
if (directoryService) {
|
|
|
|
nsCOMPtr<nsIFile> appDir;
|
|
|
|
// NS_XPCOM_CURRENT_PROCESS_DIR really means the app dir, not the
|
|
|
|
// current process dir.
|
|
|
|
nsresult rv = directoryService->Get(NS_XPCOM_CURRENT_PROCESS_DIR,
|
|
|
|
NS_GET_IID(nsIFile),
|
|
|
|
getter_AddRefs(appDir));
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
nsAutoCString path;
|
2014-04-04 23:16:16 +04:00
|
|
|
MOZ_ALWAYS_TRUE(NS_SUCCEEDED(appDir->GetNativePath(path)));
|
2012-12-18 20:24:42 +04:00
|
|
|
#if defined(XP_WIN)
|
|
|
|
aCmdLine.AppendLooseValue(UTF8ToWide("-appdir"));
|
|
|
|
aCmdLine.AppendLooseValue(UTF8ToWide(path.get()));
|
|
|
|
#else
|
|
|
|
aCmdLine.push_back("-appdir");
|
|
|
|
aCmdLine.push_back(path.get());
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-03-26 02:40:50 +03:00
|
|
|
bool
|
|
|
|
GeckoChildProcessHost::PerformAsyncLaunchInternal(std::vector<std::string>& aExtraOpts, base::ProcessArchitecture arch)
|
2009-06-29 22:38:29 +04:00
|
|
|
{
|
2009-11-19 00:43:53 +03:00
|
|
|
// We rely on the fact that InitializeChannel() has already been processed
|
|
|
|
// on the IO thread before this point is reached.
|
|
|
|
if (!GetChannel()) {
|
2009-06-29 22:38:29 +04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-08-09 10:23:24 +04:00
|
|
|
base::ProcessHandle process = 0;
|
2009-11-06 03:24:27 +03:00
|
|
|
|
|
|
|
// send the child the PID so that it can open a ProcessHandle back to us.
|
|
|
|
// probably don't want to do this in the long run
|
|
|
|
char pidstring[32];
|
|
|
|
PR_snprintf(pidstring, sizeof(pidstring) - 1,
|
|
|
|
"%ld", base::Process::Current().pid());
|
|
|
|
|
|
|
|
const char* const childProcessType =
|
|
|
|
XRE_ChildProcessTypeToString(mProcessType);
|
2009-06-29 22:38:29 +04:00
|
|
|
|
2009-11-06 03:24:27 +03:00
|
|
|
//--------------------------------------------------
|
|
|
|
#if defined(OS_POSIX)
|
|
|
|
// For POSIX, we have to be extremely anal about *not* using
|
|
|
|
// std::wstring in code compiled with Mozilla's -fshort-wchar
|
|
|
|
// configuration, because chromium is compiled with -fno-short-wchar
|
|
|
|
// and passing wstrings from one config to the other is unsafe. So
|
|
|
|
// we split the logic here.
|
|
|
|
|
2012-08-29 01:16:29 +04:00
|
|
|
#if defined(OS_LINUX) || defined(OS_MACOSX) || defined(OS_BSD)
|
2010-04-10 00:21:08 +04:00
|
|
|
base::environment_map newEnvVars;
|
2012-08-29 16:24:48 +04:00
|
|
|
ChildPrivileges privs = mPrivileges;
|
|
|
|
if (privs == base::PRIVILEGES_DEFAULT) {
|
2013-01-18 00:06:36 +04:00
|
|
|
privs = DefaultChildPrivileges();
|
2012-08-29 16:24:48 +04:00
|
|
|
}
|
2011-06-03 22:33:55 +04:00
|
|
|
// XPCOM may not be initialized in some subprocesses. We don't want
|
|
|
|
// to initialize XPCOM just for the directory service, especially
|
|
|
|
// since LD_LIBRARY_PATH is already set correctly in subprocesses
|
|
|
|
// (meaning that we don't need to set that up in the environment).
|
|
|
|
if (ShouldHaveDirectoryService()) {
|
2014-05-18 03:53:03 +04:00
|
|
|
MOZ_ASSERT(sGreDir);
|
2014-04-04 23:16:16 +04:00
|
|
|
if (sGreDir) {
|
|
|
|
nsCString path;
|
|
|
|
MOZ_ALWAYS_TRUE(NS_SUCCEEDED(sGreDir->GetNativePath(path)));
|
2012-08-29 01:16:29 +04:00
|
|
|
# if defined(OS_LINUX) || defined(OS_BSD)
|
2013-01-15 01:38:17 +04:00
|
|
|
# if defined(MOZ_WIDGET_ANDROID)
|
2014-04-04 23:16:16 +04:00
|
|
|
path += "/lib";
|
2011-11-11 04:17:46 +04:00
|
|
|
# endif // MOZ_WIDGET_ANDROID
|
2014-04-04 23:16:16 +04:00
|
|
|
const char *ld_library_path = PR_GetEnv("LD_LIBRARY_PATH");
|
|
|
|
nsCString new_ld_lib_path;
|
|
|
|
if (ld_library_path && *ld_library_path) {
|
|
|
|
new_ld_lib_path.Assign(path.get());
|
|
|
|
new_ld_lib_path.AppendLiteral(":");
|
|
|
|
new_ld_lib_path.Append(ld_library_path);
|
|
|
|
newEnvVars["LD_LIBRARY_PATH"] = new_ld_lib_path.get();
|
|
|
|
} else {
|
|
|
|
newEnvVars["LD_LIBRARY_PATH"] = path.get();
|
|
|
|
}
|
2011-06-03 22:33:55 +04:00
|
|
|
# elif OS_MACOSX
|
2014-04-04 23:16:16 +04:00
|
|
|
newEnvVars["DYLD_LIBRARY_PATH"] = path.get();
|
|
|
|
// XXX DYLD_INSERT_LIBRARIES should only be set when launching a plugin
|
|
|
|
// process, and has no effect on other subprocesses (the hooks in
|
|
|
|
// libplugin_child_interpose.dylib become noops). But currently it
|
|
|
|
// gets set when launching any kind of subprocess.
|
|
|
|
//
|
|
|
|
// Trigger "dyld interposing" for the dylib that contains
|
|
|
|
// plugin_child_interpose.mm. This allows us to hook OS calls in the
|
|
|
|
// plugin process (ones that don't work correctly in a background
|
|
|
|
// process). Don't break any other "dyld interposing" that has already
|
|
|
|
// been set up by whatever may have launched the browser.
|
|
|
|
const char* prevInterpose = PR_GetEnv("DYLD_INSERT_LIBRARIES");
|
|
|
|
nsCString interpose;
|
|
|
|
if (prevInterpose) {
|
|
|
|
interpose.Assign(prevInterpose);
|
|
|
|
interpose.AppendLiteral(":");
|
2011-06-03 22:33:55 +04:00
|
|
|
}
|
2014-04-04 23:16:16 +04:00
|
|
|
interpose.Append(path.get());
|
|
|
|
interpose.AppendLiteral("/libplugin_child_interpose.dylib");
|
|
|
|
newEnvVars["DYLD_INSERT_LIBRARIES"] = interpose.get();
|
|
|
|
# endif // OS_LINUX
|
2011-05-24 10:58:28 +04:00
|
|
|
}
|
2010-03-15 20:08:27 +03:00
|
|
|
}
|
2011-06-03 22:33:55 +04:00
|
|
|
#endif // OS_LINUX || OS_MACOSX
|
2010-07-20 02:35:45 +04:00
|
|
|
|
2010-09-16 10:09:19 +04:00
|
|
|
FilePath exePath;
|
|
|
|
GetPathToBinary(exePath);
|
2009-06-29 22:38:29 +04:00
|
|
|
|
2011-11-11 04:17:46 +04:00
|
|
|
#ifdef MOZ_WIDGET_ANDROID
|
2010-06-25 22:39:02 +04:00
|
|
|
// The java wrapper unpacks this for us but can't make it executable
|
|
|
|
chmod(exePath.value().c_str(), 0700);
|
2012-07-12 16:51:57 +04:00
|
|
|
#endif // MOZ_WIDGET_ANDROID
|
2011-01-20 20:01:31 +03:00
|
|
|
|
2012-07-12 16:51:57 +04:00
|
|
|
#ifdef ANDROID
|
2011-01-20 20:01:31 +03:00
|
|
|
// Remap the Android property workspace to a well-known int,
|
|
|
|
// and update the environment to reflect the new value for the
|
|
|
|
// child process.
|
|
|
|
const char *apws = getenv("ANDROID_PROPERTY_WORKSPACE");
|
|
|
|
if (apws) {
|
|
|
|
int fd = atoi(apws);
|
|
|
|
mFileMap.push_back(std::pair<int, int>(fd, kMagicAndroidSystemPropFd));
|
|
|
|
|
|
|
|
char buf[32];
|
|
|
|
char *szptr = strchr(apws, ',');
|
|
|
|
|
|
|
|
snprintf(buf, sizeof(buf), "%d%s", kMagicAndroidSystemPropFd, szptr);
|
|
|
|
newEnvVars["ANDROID_PROPERTY_WORKSPACE"] = buf;
|
|
|
|
}
|
2012-07-12 16:51:57 +04:00
|
|
|
#endif // ANDROID
|
2010-06-25 22:39:02 +04:00
|
|
|
|
2012-08-23 05:50:25 +04:00
|
|
|
#ifdef MOZ_WIDGET_GONK
|
|
|
|
if (const char *ldPreloadPath = getenv("LD_PRELOAD")) {
|
|
|
|
newEnvVars["LD_PRELOAD"] = ldPreloadPath;
|
|
|
|
}
|
|
|
|
#endif // MOZ_WIDGET_GONK
|
|
|
|
|
2009-07-10 23:03:09 +04:00
|
|
|
// remap the IPC socket fd to a well-known int, as the OS does for
|
|
|
|
// STDOUT_FILENO, for example
|
2009-06-29 22:38:29 +04:00
|
|
|
int srcChannelFd, dstChannelFd;
|
|
|
|
channel().GetClientFileDescriptorMapping(&srcChannelFd, &dstChannelFd);
|
|
|
|
mFileMap.push_back(std::pair<int,int>(srcChannelFd, dstChannelFd));
|
2010-01-13 00:14:38 +03:00
|
|
|
|
2009-11-06 03:24:27 +03:00
|
|
|
// no need for kProcessChannelID, the child process inherits the
|
|
|
|
// other end of the socketpair() from us
|
|
|
|
|
|
|
|
std::vector<std::string> childArgv;
|
|
|
|
|
|
|
|
childArgv.push_back(exePath.value());
|
|
|
|
|
|
|
|
childArgv.insert(childArgv.end(), aExtraOpts.begin(), aExtraOpts.end());
|
|
|
|
|
2011-06-03 22:33:55 +04:00
|
|
|
if (Omnijar::IsInitialized()) {
|
|
|
|
// Make sure that child processes can find the omnijar
|
|
|
|
// See XRE_InitCommandLine in nsAppRunner.cpp
|
2012-09-02 06:35:17 +04:00
|
|
|
nsAutoCString path;
|
2011-06-03 22:33:55 +04:00
|
|
|
nsCOMPtr<nsIFile> file = Omnijar::GetPath(Omnijar::GRE);
|
|
|
|
if (file && NS_SUCCEEDED(file->GetNativePath(path))) {
|
|
|
|
childArgv.push_back("-greomni");
|
|
|
|
childArgv.push_back(path.get());
|
|
|
|
}
|
|
|
|
file = Omnijar::GetPath(Omnijar::APP);
|
|
|
|
if (file && NS_SUCCEEDED(file->GetNativePath(path))) {
|
|
|
|
childArgv.push_back("-appomni");
|
|
|
|
childArgv.push_back(path.get());
|
|
|
|
}
|
2010-08-11 02:08:06 +04:00
|
|
|
}
|
|
|
|
|
2012-12-18 20:24:42 +04:00
|
|
|
// Add the application directory path (-appdir path)
|
|
|
|
AddAppDirToCommandLine(childArgv);
|
|
|
|
|
2009-11-06 03:24:27 +03:00
|
|
|
childArgv.push_back(pidstring);
|
|
|
|
|
2010-04-13 00:47:47 +04:00
|
|
|
#if defined(MOZ_CRASHREPORTER)
|
2012-08-29 01:16:29 +04:00
|
|
|
# if defined(OS_LINUX) || defined(OS_BSD)
|
2010-01-13 00:14:38 +03:00
|
|
|
int childCrashFd, childCrashRemapFd;
|
|
|
|
if (!CrashReporter::CreateNotificationPipeForChild(
|
|
|
|
&childCrashFd, &childCrashRemapFd))
|
|
|
|
return false;
|
|
|
|
if (0 <= childCrashFd) {
|
|
|
|
mFileMap.push_back(std::pair<int,int>(childCrashFd, childCrashRemapFd));
|
|
|
|
// "true" == crash reporting enabled
|
|
|
|
childArgv.push_back("true");
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
// "false" == crash reporting disabled
|
|
|
|
childArgv.push_back("false");
|
|
|
|
}
|
2011-10-14 11:54:46 +04:00
|
|
|
# elif defined(MOZ_WIDGET_COCOA)
|
2010-08-16 23:05:09 +04:00
|
|
|
childArgv.push_back(CrashReporter::GetChildNotificationPipe());
|
2010-04-13 00:47:47 +04:00
|
|
|
# endif // OS_LINUX
|
2010-01-13 00:14:38 +03:00
|
|
|
#endif
|
|
|
|
|
2011-10-14 11:54:46 +04:00
|
|
|
#ifdef MOZ_WIDGET_COCOA
|
2010-09-10 21:09:30 +04:00
|
|
|
// Add a mach port to the command line so the child can communicate its
|
|
|
|
// 'task_t' back to the parent.
|
|
|
|
//
|
|
|
|
// Put a random number into the channel name, so that a compromised renderer
|
|
|
|
// can't pretend being the child that's forked off.
|
|
|
|
std::string mach_connection_name = StringPrintf("org.mozilla.machname.%d",
|
|
|
|
base::RandInt(0, std::numeric_limits<int>::max()));
|
|
|
|
childArgv.push_back(mach_connection_name.c_str());
|
|
|
|
#endif
|
|
|
|
|
|
|
|
childArgv.push_back(childProcessType);
|
|
|
|
|
2010-04-10 00:03:08 +04:00
|
|
|
base::LaunchApp(childArgv, mFileMap,
|
2012-08-29 01:16:29 +04:00
|
|
|
#if defined(OS_LINUX) || defined(OS_MACOSX) || defined(OS_BSD)
|
2012-07-27 08:42:56 +04:00
|
|
|
newEnvVars, privs,
|
2010-04-10 00:03:08 +04:00
|
|
|
#endif
|
2010-09-16 10:09:19 +04:00
|
|
|
false, &process, arch);
|
2010-09-10 21:09:30 +04:00
|
|
|
|
2013-07-09 06:37:33 +04:00
|
|
|
// We're in the parent and the child was launched. Close the child FD in the
|
|
|
|
// parent as soon as possible, which will allow the parent to detect when the
|
|
|
|
// child closes its FD (either due to normal exit or due to crash).
|
|
|
|
GetChannel()->CloseClientFileDescriptor();
|
|
|
|
|
2011-10-14 11:54:46 +04:00
|
|
|
#ifdef MOZ_WIDGET_COCOA
|
2010-09-10 21:09:30 +04:00
|
|
|
// Wait for the child process to send us its 'task_t' data.
|
2010-10-14 23:07:38 +04:00
|
|
|
const int kTimeoutMs = 10000;
|
2010-09-10 21:09:30 +04:00
|
|
|
|
|
|
|
MachReceiveMessage child_message;
|
|
|
|
ReceivePort parent_recv_port(mach_connection_name.c_str());
|
|
|
|
kern_return_t err = parent_recv_port.WaitForMessage(&child_message, kTimeoutMs);
|
|
|
|
if (err != KERN_SUCCESS) {
|
|
|
|
std::string errString = StringPrintf("0x%x %s", err, mach_error_string(err));
|
2013-12-17 22:26:45 +04:00
|
|
|
CHROMIUM_LOG(ERROR) << "parent WaitForMessage() failed: " << errString;
|
2010-09-10 21:09:30 +04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
task_t child_task = child_message.GetTranslatedPort(0);
|
|
|
|
if (child_task == MACH_PORT_NULL) {
|
2013-12-17 22:26:45 +04:00
|
|
|
CHROMIUM_LOG(ERROR) << "parent GetTranslatedPort(0) failed.";
|
2010-09-10 21:09:30 +04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (child_message.GetTranslatedPort(1) == MACH_PORT_NULL) {
|
2013-12-17 22:26:45 +04:00
|
|
|
CHROMIUM_LOG(ERROR) << "parent GetTranslatedPort(1) failed.";
|
2010-09-10 21:09:30 +04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
MachPortSender parent_sender(child_message.GetTranslatedPort(1));
|
|
|
|
|
|
|
|
MachSendMessage parent_message(/* id= */0);
|
|
|
|
if (!parent_message.AddDescriptor(bootstrap_port)) {
|
2013-12-17 22:26:45 +04:00
|
|
|
CHROMIUM_LOG(ERROR) << "parent AddDescriptor(" << bootstrap_port << ") failed.";
|
2010-09-10 21:09:30 +04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
err = parent_sender.SendMessage(parent_message, kTimeoutMs);
|
|
|
|
if (err != KERN_SUCCESS) {
|
|
|
|
std::string errString = StringPrintf("0x%x %s", err, mach_error_string(err));
|
2013-12-17 22:26:45 +04:00
|
|
|
CHROMIUM_LOG(ERROR) << "parent SendMessage() failed: " << errString;
|
2010-09-10 21:09:30 +04:00
|
|
|
return false;
|
|
|
|
}
|
2010-08-27 17:32:45 +04:00
|
|
|
#endif
|
2009-11-06 03:24:27 +03:00
|
|
|
|
|
|
|
//--------------------------------------------------
|
|
|
|
#elif defined(OS_WIN)
|
|
|
|
|
2010-09-16 10:09:19 +04:00
|
|
|
FilePath exePath;
|
|
|
|
GetPathToBinary(exePath);
|
2009-06-29 22:38:29 +04:00
|
|
|
|
|
|
|
CommandLine cmdLine(exePath.ToWStringHack());
|
|
|
|
cmdLine.AppendSwitchWithValue(switches::kProcessChannelID, channel_id());
|
|
|
|
|
2009-11-06 03:24:27 +03:00
|
|
|
for (std::vector<std::string>::iterator it = aExtraOpts.begin();
|
2009-07-10 23:03:09 +04:00
|
|
|
it != aExtraOpts.end();
|
|
|
|
++it) {
|
2009-11-06 03:24:27 +03:00
|
|
|
cmdLine.AppendLooseValue(UTF8ToWide(*it));
|
2009-07-10 23:03:09 +04:00
|
|
|
}
|
|
|
|
|
2011-06-03 22:33:55 +04:00
|
|
|
if (Omnijar::IsInitialized()) {
|
|
|
|
// Make sure the child process can find the omnijar
|
|
|
|
// See XRE_InitCommandLine in nsAppRunner.cpp
|
|
|
|
nsAutoString path;
|
|
|
|
nsCOMPtr<nsIFile> file = Omnijar::GetPath(Omnijar::GRE);
|
|
|
|
if (file && NS_SUCCEEDED(file->GetPath(path))) {
|
|
|
|
cmdLine.AppendLooseValue(UTF8ToWide("-greomni"));
|
|
|
|
cmdLine.AppendLooseValue(path.get());
|
|
|
|
}
|
|
|
|
file = Omnijar::GetPath(Omnijar::APP);
|
|
|
|
if (file && NS_SUCCEEDED(file->GetPath(path))) {
|
|
|
|
cmdLine.AppendLooseValue(UTF8ToWide("-appomni"));
|
|
|
|
cmdLine.AppendLooseValue(path.get());
|
|
|
|
}
|
2010-08-11 02:08:06 +04:00
|
|
|
}
|
|
|
|
|
2013-11-02 06:09:45 +04:00
|
|
|
#if defined(XP_WIN) && defined(MOZ_CONTENT_SANDBOX)
|
|
|
|
if (mSandboxEnabled) {
|
|
|
|
// Tell the process that it should lower its rights after initialization.
|
|
|
|
cmdLine.AppendLooseValue(UTF8ToWide("-sandbox"));
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2012-12-18 20:24:42 +04:00
|
|
|
// Add the application directory path (-appdir path)
|
|
|
|
AddAppDirToCommandLine(cmdLine);
|
|
|
|
|
|
|
|
// XXX Command line params past this point are expected to be at
|
|
|
|
// the end of the command line string, and in a specific order.
|
|
|
|
// See XRE_InitChildProcess in nsEmbedFunction.
|
|
|
|
|
|
|
|
// Win app model id
|
2013-12-03 19:09:50 +04:00
|
|
|
cmdLine.AppendLooseValue(mGroupId.get());
|
2012-12-18 20:24:42 +04:00
|
|
|
|
|
|
|
// Process id
|
2009-10-28 00:52:37 +03:00
|
|
|
cmdLine.AppendLooseValue(UTF8ToWide(pidstring));
|
2010-09-10 21:09:30 +04:00
|
|
|
|
2010-01-13 00:14:38 +03:00
|
|
|
#if defined(MOZ_CRASHREPORTER)
|
|
|
|
cmdLine.AppendLooseValue(
|
2010-01-13 18:44:10 +03:00
|
|
|
UTF8ToWide(CrashReporter::GetChildNotificationPipe()));
|
2010-01-13 00:14:38 +03:00
|
|
|
#endif
|
2009-10-28 00:52:37 +03:00
|
|
|
|
2012-12-18 20:24:42 +04:00
|
|
|
// Process type
|
2010-09-10 21:09:30 +04:00
|
|
|
cmdLine.AppendLooseValue(UTF8ToWide(childProcessType));
|
|
|
|
|
2013-10-31 03:58:52 +04:00
|
|
|
#if defined(XP_WIN) && defined(MOZ_CONTENT_SANDBOX)
|
2013-11-02 06:09:45 +04:00
|
|
|
if (mSandboxEnabled) {
|
2013-10-31 03:58:52 +04:00
|
|
|
|
2013-11-02 06:09:45 +04:00
|
|
|
mozilla::SandboxBroker sandboxBroker;
|
|
|
|
sandboxBroker.LaunchApp(cmdLine.program().c_str(),
|
|
|
|
cmdLine.command_line_string().c_str(),
|
|
|
|
&process);
|
|
|
|
} else
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
base::LaunchApp(cmdLine, false, false, &process);
|
|
|
|
}
|
2009-11-06 03:24:27 +03:00
|
|
|
|
2009-06-29 22:38:29 +04:00
|
|
|
#else
|
2009-11-06 03:24:27 +03:00
|
|
|
# error Sorry
|
2009-06-29 22:38:29 +04:00
|
|
|
#endif
|
|
|
|
|
|
|
|
if (!process) {
|
2012-08-08 03:29:32 +04:00
|
|
|
MonitorAutoLock lock(mMonitor);
|
|
|
|
mProcessState = PROCESS_ERROR;
|
|
|
|
lock.Notify();
|
2009-06-29 22:38:29 +04:00
|
|
|
return false;
|
|
|
|
}
|
2012-08-08 03:29:32 +04:00
|
|
|
// NB: on OS X, we block much longer than we need to in order to
|
|
|
|
// reach this call, waiting for the child process's task_t. The
|
|
|
|
// best way to fix that is to refactor this file, hard.
|
2009-06-29 22:38:29 +04:00
|
|
|
SetHandle(process);
|
2011-10-14 11:54:46 +04:00
|
|
|
#if defined(MOZ_WIDGET_COCOA)
|
2010-08-27 17:32:45 +04:00
|
|
|
mChildTask = child_task;
|
|
|
|
#endif
|
2009-06-29 22:38:29 +04:00
|
|
|
|
2012-08-08 03:29:32 +04:00
|
|
|
OpenPrivilegedHandle(base::GetProcId(process));
|
|
|
|
{
|
|
|
|
MonitorAutoLock lock(mMonitor);
|
|
|
|
mProcessState = PROCESS_CREATED;
|
|
|
|
lock.Notify();
|
|
|
|
}
|
|
|
|
|
2009-06-29 22:38:29 +04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2009-07-14 09:12:50 +04:00
|
|
|
void
|
2012-08-08 03:29:32 +04:00
|
|
|
GeckoChildProcessHost::OpenPrivilegedHandle(base::ProcessId aPid)
|
2009-07-14 09:12:50 +04:00
|
|
|
{
|
2012-08-08 03:29:32 +04:00
|
|
|
if (mChildProcessHandle) {
|
|
|
|
MOZ_ASSERT(aPid == base::GetProcId(mChildProcessHandle));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (!base::OpenPrivilegedProcessHandle(aPid, &mChildProcessHandle)) {
|
|
|
|
NS_RUNTIMEABORT("can't open handle to child process");
|
|
|
|
}
|
|
|
|
}
|
2009-10-28 00:52:37 +03:00
|
|
|
|
2012-08-08 03:29:32 +04:00
|
|
|
void
|
2012-09-17 12:37:20 +04:00
|
|
|
GeckoChildProcessHost::OnChannelConnected(int32_t peer_pid)
|
2012-08-08 03:29:32 +04:00
|
|
|
{
|
|
|
|
OpenPrivilegedHandle(peer_pid);
|
|
|
|
{
|
|
|
|
MonitorAutoLock lock(mMonitor);
|
|
|
|
mProcessState = PROCESS_CONNECTED;
|
|
|
|
lock.Notify();
|
|
|
|
}
|
2009-07-14 09:12:50 +04:00
|
|
|
}
|
2009-07-20 22:14:41 +04:00
|
|
|
|
2009-06-29 22:38:29 +04:00
|
|
|
void
|
|
|
|
GeckoChildProcessHost::OnMessageReceived(const IPC::Message& aMsg)
|
|
|
|
{
|
2012-07-15 01:21:32 +04:00
|
|
|
// We never process messages ourself, just save them up for the next
|
|
|
|
// listener.
|
|
|
|
mQueue.push(aMsg);
|
2009-06-29 22:38:29 +04:00
|
|
|
}
|
2012-07-15 01:21:32 +04:00
|
|
|
|
2009-06-29 22:38:29 +04:00
|
|
|
void
|
|
|
|
GeckoChildProcessHost::OnChannelError()
|
|
|
|
{
|
2014-01-12 07:51:00 +04:00
|
|
|
// Update the process state to an error state if we have a channel
|
|
|
|
// error before we're connected. This fixes certain failures,
|
|
|
|
// but does not address the full range of possible issues described
|
|
|
|
// in the FIXME comment below.
|
|
|
|
MonitorAutoLock lock(mMonitor);
|
|
|
|
if (mProcessState < PROCESS_CONNECTED) {
|
|
|
|
mProcessState = PROCESS_ERROR;
|
|
|
|
lock.Notify();
|
|
|
|
}
|
2012-07-15 01:21:32 +04:00
|
|
|
// FIXME/bug 773925: save up this error for the next listener.
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
GeckoChildProcessHost::GetQueuedMessages(std::queue<IPC::Message>& queue)
|
|
|
|
{
|
|
|
|
// If this is called off the IO thread, bad things will happen.
|
|
|
|
DCHECK(MessageLoopForIO::current());
|
|
|
|
swap(queue, mQueue);
|
|
|
|
// We expect the next listener to take over processing of our queue.
|
2009-06-29 22:38:29 +04:00
|
|
|
}
|
2009-09-03 04:18:27 +04:00
|
|
|
|
|
|
|
void
|
|
|
|
GeckoChildProcessHost::OnWaitableEventSignaled(base::WaitableEvent *event)
|
|
|
|
{
|
|
|
|
if (mDelegate) {
|
|
|
|
mDelegate->OnWaitableEventSignaled(event);
|
|
|
|
}
|
|
|
|
ChildProcessHost::OnWaitableEventSignaled(event);
|
|
|
|
}
|
2013-05-31 17:16:54 +04:00
|
|
|
|
|
|
|
#ifdef MOZ_NUWA_PROCESS
|
|
|
|
|
|
|
|
using mozilla::ipc::GeckoExistingProcessHost;
|
|
|
|
using mozilla::ipc::FileDescriptor;
|
|
|
|
|
|
|
|
GeckoExistingProcessHost::
|
|
|
|
GeckoExistingProcessHost(GeckoProcessType aProcessType,
|
|
|
|
base::ProcessHandle aProcess,
|
|
|
|
const FileDescriptor& aFileDescriptor,
|
|
|
|
ChildPrivileges aPrivileges)
|
|
|
|
: GeckoChildProcessHost(aProcessType, aPrivileges)
|
|
|
|
, mExistingProcessHandle(aProcess)
|
|
|
|
, mExistingFileDescriptor(aFileDescriptor)
|
|
|
|
{
|
|
|
|
NS_ASSERTION(aFileDescriptor.IsValid(),
|
|
|
|
"Expected file descriptor to be valid");
|
|
|
|
}
|
|
|
|
|
|
|
|
GeckoExistingProcessHost::~GeckoExistingProcessHost()
|
|
|
|
{
|
2014-03-21 16:50:07 +04:00
|
|
|
// Bug 943174: If we don't do this, ~GeckoChildProcessHost will try
|
|
|
|
// to wait on a process that isn't a direct child, and bad things
|
|
|
|
// will happen.
|
|
|
|
SetAlreadyDead();
|
2013-05-31 17:16:54 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
GeckoExistingProcessHost::PerformAsyncLaunch(StringVector aExtraOpts,
|
|
|
|
base::ProcessArchitecture aArch)
|
|
|
|
{
|
|
|
|
SetHandle(mExistingProcessHandle);
|
|
|
|
|
|
|
|
OpenPrivilegedHandle(base::GetProcId(mExistingProcessHandle));
|
|
|
|
|
|
|
|
MonitorAutoLock lock(mMonitor);
|
|
|
|
mProcessState = PROCESS_CREATED;
|
|
|
|
lock.Notify();
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
GeckoExistingProcessHost::InitializeChannel()
|
|
|
|
{
|
|
|
|
CreateChannel(mExistingFileDescriptor);
|
|
|
|
|
|
|
|
MonitorAutoLock lock(mMonitor);
|
|
|
|
mProcessState = CHANNEL_INITIALIZED;
|
|
|
|
lock.Notify();
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* MOZ_NUWA_PROCESS */
|