2009-11-18 13:43:53 -08:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*-
|
2009-12-06 22:04:00 -08:00
|
|
|
*/
|
2009-06-29 11:38:29 -07:00
|
|
|
/* ***** BEGIN LICENSE BLOCK *****
|
|
|
|
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
|
|
|
*
|
|
|
|
* The contents of this file are subject to the Mozilla Public License Version
|
|
|
|
* 1.1 (the "License"); you may not use this file except in compliance with
|
|
|
|
* the License. You may obtain a copy of the License at
|
|
|
|
* http://www.mozilla.org/MPL/
|
|
|
|
*
|
|
|
|
* Software distributed under the License is distributed on an "AS IS" basis,
|
|
|
|
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
|
|
|
* for the specific language governing rights and limitations under the
|
|
|
|
* License.
|
|
|
|
*
|
|
|
|
* The Original Code is Mozilla IPC.
|
|
|
|
*
|
|
|
|
* The Initial Developer of the Original Code is
|
|
|
|
* Ben Turner <bent.mozilla@gmail.com>.
|
|
|
|
* Portions created by the Initial Developer are Copyright (C) 2009
|
|
|
|
* the Initial Developer. All Rights Reserved.
|
|
|
|
*
|
|
|
|
* Contributor(s):
|
|
|
|
*
|
|
|
|
* Alternatively, the contents of this file may be used under the terms of
|
|
|
|
* either the GNU General Public License Version 2 or later (the "GPL"), or
|
|
|
|
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
|
|
|
|
* in which case the provisions of the GPL or the LGPL are applicable instead
|
|
|
|
* of those above. If you wish to allow use of your version of this file only
|
|
|
|
* under the terms of either the GPL or the LGPL, and not to allow others to
|
|
|
|
* use your version of this file under the terms of the MPL, indicate your
|
|
|
|
* decision by deleting the provisions above and replace them with the notice
|
|
|
|
* and other provisions required by the GPL or the LGPL. If you do not delete
|
|
|
|
* the provisions above, a recipient may use your version of this file under
|
|
|
|
* the terms of any one of the MPL, the GPL or the LGPL.
|
|
|
|
*
|
|
|
|
* ***** END LICENSE BLOCK ***** */
|
|
|
|
|
|
|
|
#include "GeckoChildProcessHost.h"
|
|
|
|
|
|
|
|
#include "base/command_line.h"
|
|
|
|
#include "base/path_service.h"
|
2009-07-10 12:03:09 -07:00
|
|
|
#include "base/string_util.h"
|
2009-06-29 11:38:29 -07:00
|
|
|
#include "chrome/common/chrome_switches.h"
|
2009-11-19 11:03:30 -08:00
|
|
|
#include "chrome/common/process_watcher.h"
|
2010-09-10 10:09:30 -07:00
|
|
|
#ifdef XP_MACOSX
|
|
|
|
#include "chrome/common/mach_ipc_mac.h"
|
|
|
|
#include "base/rand_util.h"
|
2010-09-15 23:09:19 -07:00
|
|
|
#include "nsILocalFileMac.h"
|
2010-09-10 10:09:30 -07:00
|
|
|
#endif
|
2009-06-29 11:38:29 -07:00
|
|
|
|
2009-10-27 14:52:37 -07:00
|
|
|
#include "prprf.h"
|
2011-03-25 16:40:50 -07:00
|
|
|
#include "prenv.h"
|
2009-10-27 14:52:37 -07:00
|
|
|
|
2010-01-12 13:14:38 -08:00
|
|
|
#if defined(OS_LINUX)
|
|
|
|
# define XP_LINUX 1
|
|
|
|
#endif
|
|
|
|
#include "nsExceptionHandler.h"
|
|
|
|
|
2010-04-09 13:03:08 -07:00
|
|
|
#include "nsDirectoryServiceDefs.h"
|
|
|
|
#include "nsIFile.h"
|
2010-06-25 13:29:43 -07:00
|
|
|
#include "nsILocalFile.h"
|
2010-04-09 13:03:08 -07:00
|
|
|
|
2010-02-03 14:17:09 -08:00
|
|
|
#include "mozilla/ipc/BrowserProcessSubThread.h"
|
2010-06-25 13:29:43 -07:00
|
|
|
#include "mozilla/Omnijar.h"
|
2010-06-25 11:39:02 -07:00
|
|
|
#include <sys/stat.h>
|
2009-07-20 11:14:41 -07:00
|
|
|
|
2010-05-14 17:25:10 -07:00
|
|
|
#ifdef XP_WIN
|
|
|
|
#include "nsIWinTaskbar.h"
|
|
|
|
#define NS_TASKBAR_CONTRACTID "@mozilla.org/windows-taskbar;1"
|
|
|
|
#endif
|
|
|
|
|
2010-10-29 12:22:28 -07:00
|
|
|
#ifdef ANDROID
|
|
|
|
#include "APKOpen.h"
|
|
|
|
#endif
|
|
|
|
|
2009-07-20 11:14:41 -07:00
|
|
|
using mozilla::MonitorAutoEnter;
|
2009-06-29 11:38:29 -07:00
|
|
|
using mozilla::ipc::GeckoChildProcessHost;
|
|
|
|
|
2011-01-20 09:01:31 -08:00
|
|
|
#ifdef ANDROID
|
|
|
|
// 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
|
|
|
|
|
2009-07-20 11:14:41 -07:00
|
|
|
template<>
|
|
|
|
struct RunnableMethodTraits<GeckoChildProcessHost>
|
|
|
|
{
|
|
|
|
static void RetainCallee(GeckoChildProcessHost* obj) { }
|
|
|
|
static void ReleaseCallee(GeckoChildProcessHost* obj) { }
|
|
|
|
};
|
|
|
|
|
2009-09-02 17:18:27 -07:00
|
|
|
GeckoChildProcessHost::GeckoChildProcessHost(GeckoProcessType aProcessType,
|
|
|
|
base::WaitableEventWatcher::Delegate* aDelegate)
|
2009-07-10 12:03:09 -07:00
|
|
|
: ChildProcessHost(RENDER_PROCESS), // FIXME/cjones: we should own this enum
|
2009-07-20 11:14:41 -07:00
|
|
|
mProcessType(aProcessType),
|
|
|
|
mMonitor("mozilla.ipc.GeckChildProcessHost.mMonitor"),
|
2009-09-02 17:18:27 -07:00
|
|
|
mLaunched(false),
|
2009-11-18 13:43:53 -08:00
|
|
|
mChannelInitialized(false),
|
2009-10-27 14:52:37 -07:00
|
|
|
mDelegate(aDelegate),
|
|
|
|
mChildProcessHandle(0)
|
2010-08-27 06:32:45 -07:00
|
|
|
#if defined(XP_MACOSX)
|
|
|
|
, mChildTask(MACH_PORT_NULL)
|
|
|
|
#endif
|
2009-07-20 11:14:41 -07:00
|
|
|
{
|
2009-11-09 14:56:55 -08:00
|
|
|
MOZ_COUNT_CTOR(GeckoChildProcessHost);
|
2009-11-18 13:43:53 -08:00
|
|
|
|
2010-04-20 11:43:51 -07:00
|
|
|
MessageLoop* ioLoop = XRE_GetIOMessageLoop();
|
2009-11-18 13:43:53 -08:00
|
|
|
ioLoop->PostTask(FROM_HERE,
|
|
|
|
NewRunnableMethod(this,
|
|
|
|
&GeckoChildProcessHost::InitializeChannel));
|
2009-11-09 14:56:55 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
GeckoChildProcessHost::~GeckoChildProcessHost()
|
2009-12-18 16:09:56 -08:00
|
|
|
|
2009-11-09 14:56:55 -08:00
|
|
|
{
|
2009-12-18 16:09:56 -08:00
|
|
|
AssertIOThread();
|
|
|
|
|
|
|
|
MOZ_COUNT_DTOR(GeckoChildProcessHost);
|
2009-11-19 11:03:30 -08:00
|
|
|
|
2009-12-18 16:09:56 -08:00
|
|
|
if (mChildProcessHandle > 0)
|
2010-01-13 12:57:52 -08:00
|
|
|
ProcessWatcher::EnsureProcessTerminated(mChildProcessHandle
|
2010-01-22 07:21:33 -08:00
|
|
|
#if defined(NS_BUILD_REFCNT_LOGGING)
|
2010-01-13 12:57:52 -08:00
|
|
|
, false // don't "force"
|
|
|
|
#endif
|
|
|
|
);
|
2010-08-27 06:32:45 -07:00
|
|
|
|
|
|
|
#if defined(XP_MACOSX)
|
|
|
|
if (mChildTask != MACH_PORT_NULL)
|
|
|
|
mach_port_deallocate(mach_task_self(), mChildTask);
|
|
|
|
#endif
|
2009-07-20 11:14:41 -07:00
|
|
|
}
|
|
|
|
|
2010-09-15 23:09:19 -07:00
|
|
|
void GetPathToBinary(FilePath& exePath)
|
|
|
|
{
|
|
|
|
#if defined(OS_WIN)
|
|
|
|
exePath = FilePath::FromWStringHack(CommandLine::ForCurrentProcess()->program());
|
|
|
|
exePath = exePath.DirName();
|
|
|
|
exePath = exePath.AppendASCII(MOZ_CHILD_PROCESS_NAME);
|
|
|
|
#elif defined(OS_POSIX)
|
|
|
|
nsCOMPtr<nsIProperties> directoryService(do_GetService(NS_DIRECTORY_SERVICE_CONTRACTID));
|
|
|
|
nsCOMPtr<nsIFile> greDir;
|
|
|
|
nsresult rv = directoryService->Get(NS_GRE_DIR, NS_GET_IID(nsIFile), getter_AddRefs(greDir));
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
nsCString path;
|
|
|
|
greDir->GetNativePath(path);
|
|
|
|
exePath = FilePath(path.get());
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
exePath = FilePath(CommandLine::ForCurrentProcess()->argv()[0]);
|
|
|
|
exePath = exePath.DirName();
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef OS_MACOSX
|
|
|
|
// 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);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
exePath = exePath.AppendASCII(MOZ_CHILD_PROCESS_NAME);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef XP_MACOSX
|
|
|
|
class AutoCFTypeObject {
|
|
|
|
public:
|
|
|
|
AutoCFTypeObject(CFTypeRef object)
|
|
|
|
{
|
|
|
|
mObject = object;
|
|
|
|
}
|
|
|
|
~AutoCFTypeObject()
|
|
|
|
{
|
|
|
|
::CFRelease(mObject);
|
|
|
|
}
|
|
|
|
private:
|
|
|
|
CFTypeRef mObject;
|
|
|
|
};
|
|
|
|
#endif
|
|
|
|
|
|
|
|
nsresult GeckoChildProcessHost::GetArchitecturesForBinary(const char *path, uint32 *result)
|
|
|
|
{
|
|
|
|
*result = 0;
|
|
|
|
|
|
|
|
#ifdef XP_MACOSX
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32 GeckoChildProcessHost::GetSupportedArchitecturesForProcessType(GeckoProcessType type)
|
|
|
|
{
|
|
|
|
#ifdef XP_MACOSX
|
|
|
|
if (type == GeckoProcessType_Plugin) {
|
|
|
|
// Cache this, it shouldn't ever change.
|
|
|
|
static uint32 pluginContainerArchs = 0;
|
|
|
|
if (pluginContainerArchs == 0) {
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
2010-06-30 20:07:50 -07: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) {
|
|
|
|
PRBool isSupported = PR_FALSE;
|
|
|
|
taskbarInfo->GetAvailable(&isSupported);
|
|
|
|
nsAutoString appId;
|
|
|
|
if (isSupported && NS_SUCCEEDED(taskbarInfo->GetDefaultGroupId(appId))) {
|
|
|
|
mGroupId.Assign(PRUnichar('\"'));
|
|
|
|
mGroupId.Append(appId);
|
|
|
|
mGroupId.Append(PRUnichar('\"'));
|
|
|
|
} else {
|
|
|
|
mGroupId.AssignLiteral("-");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2009-07-20 11:14:41 -07:00
|
|
|
bool
|
2010-09-15 23:09:19 -07:00
|
|
|
GeckoChildProcessHost::SyncLaunch(std::vector<std::string> aExtraOpts, int aTimeoutMs, base::ProcessArchitecture arch)
|
2009-06-29 11:38:29 -07:00
|
|
|
{
|
2010-06-30 20:07:50 -07:00
|
|
|
#ifdef XP_WIN
|
|
|
|
InitWindowsGroupID();
|
|
|
|
#endif
|
|
|
|
|
2010-08-10 14:14:10 -07:00
|
|
|
PRIntervalTime timeoutTicks = (aTimeoutMs > 0) ?
|
|
|
|
PR_MillisecondsToInterval(aTimeoutMs) : PR_INTERVAL_NO_TIMEOUT;
|
2010-04-20 11:43:51 -07:00
|
|
|
MessageLoop* ioLoop = XRE_GetIOMessageLoop();
|
2009-11-05 16:24:27 -08:00
|
|
|
NS_ASSERTION(MessageLoop::current() != ioLoop, "sync launch from the IO thread NYI");
|
2009-07-20 11:14:41 -07:00
|
|
|
|
|
|
|
ioLoop->PostTask(FROM_HERE,
|
|
|
|
NewRunnableMethod(this,
|
2009-11-18 13:43:53 -08:00
|
|
|
&GeckoChildProcessHost::PerformAsyncLaunch,
|
2010-09-15 23:09:19 -07:00
|
|
|
aExtraOpts, arch));
|
2009-07-20 11:14:41 -07:00
|
|
|
// NB: this uses a different mechanism than the chromium parent
|
|
|
|
// class.
|
|
|
|
MonitorAutoEnter mon(mMonitor);
|
2010-08-10 14:14:10 -07: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.
|
2009-07-20 11:14:41 -07:00
|
|
|
while (!mLaunched) {
|
2010-08-10 14:14:10 -07:00
|
|
|
mon.Wait(timeoutTicks);
|
|
|
|
|
|
|
|
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 11:14:41 -07:00
|
|
|
}
|
|
|
|
|
2010-08-10 14:14:10 -07:00
|
|
|
return mLaunched;
|
2009-06-29 11:38:29 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2009-11-05 16:24:27 -08:00
|
|
|
GeckoChildProcessHost::AsyncLaunch(std::vector<std::string> aExtraOpts)
|
2009-11-18 13:43:53 -08:00
|
|
|
{
|
2010-06-30 20:07:50 -07:00
|
|
|
#ifdef XP_WIN
|
|
|
|
InitWindowsGroupID();
|
|
|
|
#endif
|
|
|
|
|
2010-04-20 11:43:51 -07:00
|
|
|
MessageLoop* ioLoop = XRE_GetIOMessageLoop();
|
2009-11-18 13:43:53 -08:00
|
|
|
ioLoop->PostTask(FROM_HERE,
|
|
|
|
NewRunnableMethod(this,
|
|
|
|
&GeckoChildProcessHost::PerformAsyncLaunch,
|
2010-09-15 23:09:19 -07:00
|
|
|
aExtraOpts, base::GetCurrentProcessArchitecture()));
|
2009-11-18 13:43:53 -08:00
|
|
|
|
|
|
|
// This may look like the sync launch wait, but we only delay as
|
|
|
|
// long as it takes to create the channel.
|
|
|
|
MonitorAutoEnter mon(mMonitor);
|
|
|
|
while (!mChannelInitialized) {
|
|
|
|
mon.Wait();
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
GeckoChildProcessHost::InitializeChannel()
|
|
|
|
{
|
|
|
|
CreateChannel();
|
|
|
|
|
|
|
|
MonitorAutoEnter mon(mMonitor);
|
|
|
|
mChannelInitialized = true;
|
|
|
|
mon.Notify();
|
|
|
|
}
|
|
|
|
|
2011-03-25 16:40:50 -07:00
|
|
|
PRInt32 GeckoChildProcessHost::mChildCounter = 0;
|
|
|
|
|
|
|
|
//
|
|
|
|
// Wrapper function for handling GECKO_SEPARATE_NSPR_LOGS
|
|
|
|
//
|
2009-11-18 13:43:53 -08:00
|
|
|
bool
|
2010-09-15 23:09:19 -07:00
|
|
|
GeckoChildProcessHost::PerformAsyncLaunch(std::vector<std::string> aExtraOpts, base::ProcessArchitecture arch)
|
2011-03-25 16:40:50 -07:00
|
|
|
{
|
|
|
|
// If separate NSPR log files are not requested, we're done.
|
|
|
|
const char* origLogName = PR_GetEnv("NSPR_LOG_FILE");
|
|
|
|
const char* separateLogs = PR_GetEnv("GECKO_SEPARATE_NSPR_LOGS");
|
|
|
|
if (!origLogName || !separateLogs || !*separateLogs ||
|
|
|
|
*separateLogs == '0' || *separateLogs == 'N' || *separateLogs == 'n') {
|
|
|
|
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.)
|
|
|
|
nsCAutoString setChildLogName("NSPR_LOG_FILE=");
|
|
|
|
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)
|
|
|
|
restoreOrigLogName = strdup(PromiseFlatCString(setChildLogName).get());
|
|
|
|
|
|
|
|
// 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.
|
|
|
|
PR_SetEnv(PromiseFlatCString(setChildLogName).get());
|
|
|
|
bool retval = PerformAsyncLaunchInternal(aExtraOpts, arch);
|
|
|
|
|
|
|
|
// Revert to original value
|
|
|
|
PR_SetEnv(restoreOrigLogName);
|
|
|
|
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
GeckoChildProcessHost::PerformAsyncLaunchInternal(std::vector<std::string>& aExtraOpts, base::ProcessArchitecture arch)
|
2009-06-29 11:38:29 -07:00
|
|
|
{
|
2009-07-20 11:14:41 -07:00
|
|
|
// FIXME/cjones: make this work from non-IO threads, too
|
|
|
|
|
2009-11-18 13:43:53 -08: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 11:38:29 -07:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2009-11-05 16:24:27 -08:00
|
|
|
base::ProcessHandle process;
|
|
|
|
|
|
|
|
// 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 11:38:29 -07:00
|
|
|
|
2009-11-05 16:24:27 -08: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.
|
|
|
|
|
2010-07-19 15:35:45 -07:00
|
|
|
#if defined(OS_LINUX) || defined(OS_MACOSX)
|
2010-04-09 13:21:08 -07:00
|
|
|
base::environment_map newEnvVars;
|
2010-03-15 10:08:27 -07:00
|
|
|
nsCOMPtr<nsIProperties> directoryService(do_GetService(NS_DIRECTORY_SERVICE_CONTRACTID));
|
|
|
|
nsCOMPtr<nsIFile> greDir;
|
|
|
|
nsresult rv = directoryService->Get(NS_GRE_DIR, NS_GET_IID(nsIFile), getter_AddRefs(greDir));
|
2010-04-09 13:03:08 -07:00
|
|
|
if (NS_SUCCEEDED(rv)) {
|
2010-03-15 10:08:27 -07:00
|
|
|
nsCString path;
|
|
|
|
greDir->GetNativePath(path);
|
2010-04-09 13:03:08 -07:00
|
|
|
#ifdef OS_LINUX
|
2010-06-25 11:39:02 -07:00
|
|
|
#ifdef ANDROID
|
|
|
|
path += "/lib";
|
|
|
|
#endif
|
2010-04-09 13:03:08 -07:00
|
|
|
newEnvVars["LD_LIBRARY_PATH"] = path.get();
|
2010-07-19 15:35:45 -07:00
|
|
|
#elif OS_MACOSX
|
|
|
|
newEnvVars["DYLD_LIBRARY_PATH"] = path.get();
|
2010-04-09 13:03:08 -07:00
|
|
|
#endif
|
2010-03-15 10:08:27 -07:00
|
|
|
}
|
2010-07-19 15:35:45 -07:00
|
|
|
#endif
|
|
|
|
|
2010-09-15 23:09:19 -07:00
|
|
|
FilePath exePath;
|
|
|
|
GetPathToBinary(exePath);
|
2009-06-29 11:38:29 -07:00
|
|
|
|
2010-06-25 11:39:02 -07:00
|
|
|
#ifdef ANDROID
|
|
|
|
// The java wrapper unpacks this for us but can't make it executable
|
|
|
|
chmod(exePath.value().c_str(), 0700);
|
2010-10-29 12:22:28 -07:00
|
|
|
int cacheCount = 0;
|
|
|
|
const struct lib_cache_info * cache = getLibraryCache();
|
|
|
|
nsCString cacheStr;
|
|
|
|
while (cache &&
|
|
|
|
cacheCount++ < MAX_LIB_CACHE_ENTRIES &&
|
|
|
|
strlen(cache->name)) {
|
|
|
|
mFileMap.push_back(std::pair<int,int>(cache->fd, cache->fd));
|
|
|
|
cacheStr.Append(cache->name);
|
|
|
|
cacheStr.AppendPrintf(":%d;", cache->fd);
|
|
|
|
cache++;
|
|
|
|
}
|
|
|
|
// fill the last arg with something if there's no cache
|
|
|
|
if (cacheStr.IsEmpty())
|
|
|
|
cacheStr.AppendLiteral("-");
|
2011-01-20 09:01:31 -08: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;
|
|
|
|
}
|
|
|
|
#endif // ANDROID
|
2010-06-25 11:39:02 -07:00
|
|
|
|
2009-07-10 12:03:09 -07:00
|
|
|
// remap the IPC socket fd to a well-known int, as the OS does for
|
|
|
|
// STDOUT_FILENO, for example
|
2009-06-29 11:38:29 -07:00
|
|
|
int srcChannelFd, dstChannelFd;
|
|
|
|
channel().GetClientFileDescriptorMapping(&srcChannelFd, &dstChannelFd);
|
|
|
|
mFileMap.push_back(std::pair<int,int>(srcChannelFd, dstChannelFd));
|
2010-01-12 13:14:38 -08:00
|
|
|
|
2009-11-05 16:24:27 -08: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-03-24 21:07:05 -07:00
|
|
|
#ifdef MOZ_OMNIJAR
|
2010-08-10 15:08:06 -07:00
|
|
|
// Make sure the child process can find the omnijar
|
|
|
|
// See XRE_InitCommandLine in nsAppRunner.cpp
|
2011-03-24 21:07:05 -07:00
|
|
|
nsCAutoString omnijarPath;
|
|
|
|
if (mozilla::OmnijarPath()) {
|
|
|
|
mozilla::OmnijarPath()->GetNativePath(omnijarPath);
|
|
|
|
childArgv.push_back("-omnijar");
|
|
|
|
childArgv.push_back(omnijarPath.get());
|
2010-08-10 15:08:06 -07:00
|
|
|
}
|
2011-03-24 21:07:05 -07:00
|
|
|
#endif
|
2010-08-10 15:08:06 -07:00
|
|
|
|
2009-11-05 16:24:27 -08:00
|
|
|
childArgv.push_back(pidstring);
|
|
|
|
|
2010-04-12 13:47:47 -07:00
|
|
|
#if defined(MOZ_CRASHREPORTER)
|
|
|
|
# if defined(OS_LINUX)
|
2010-01-12 13:14:38 -08: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");
|
|
|
|
}
|
2010-04-12 13:47:47 -07:00
|
|
|
# elif defined(XP_MACOSX)
|
2010-08-16 12:05:09 -07:00
|
|
|
childArgv.push_back(CrashReporter::GetChildNotificationPipe());
|
2010-04-12 13:47:47 -07:00
|
|
|
# endif // OS_LINUX
|
2010-01-12 13:14:38 -08:00
|
|
|
#endif
|
|
|
|
|
2010-09-10 10:09:30 -07:00
|
|
|
#ifdef XP_MACOSX
|
|
|
|
// 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-10-29 12:22:28 -07:00
|
|
|
#ifdef ANDROID
|
|
|
|
childArgv.push_back(cacheStr.get());
|
|
|
|
#endif
|
|
|
|
|
2010-04-09 13:03:08 -07:00
|
|
|
base::LaunchApp(childArgv, mFileMap,
|
2010-07-19 15:35:45 -07:00
|
|
|
#if defined(OS_LINUX) || defined(OS_MACOSX)
|
2010-04-09 13:03:08 -07:00
|
|
|
newEnvVars,
|
|
|
|
#endif
|
2010-09-15 23:09:19 -07:00
|
|
|
false, &process, arch);
|
2010-09-10 10:09:30 -07:00
|
|
|
|
|
|
|
#ifdef XP_MACOSX
|
|
|
|
// Wait for the child process to send us its 'task_t' data.
|
2010-10-14 12:07:38 -07:00
|
|
|
const int kTimeoutMs = 10000;
|
2010-09-10 10:09:30 -07: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));
|
|
|
|
LOG(ERROR) << "parent WaitForMessage() failed: " << errString;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
task_t child_task = child_message.GetTranslatedPort(0);
|
|
|
|
if (child_task == MACH_PORT_NULL) {
|
|
|
|
LOG(ERROR) << "parent GetTranslatedPort(0) failed.";
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (child_message.GetTranslatedPort(1) == MACH_PORT_NULL) {
|
|
|
|
LOG(ERROR) << "parent GetTranslatedPort(1) failed.";
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
MachPortSender parent_sender(child_message.GetTranslatedPort(1));
|
|
|
|
|
|
|
|
MachSendMessage parent_message(/* id= */0);
|
|
|
|
if (!parent_message.AddDescriptor(bootstrap_port)) {
|
|
|
|
LOG(ERROR) << "parent AddDescriptor(" << bootstrap_port << ") failed.";
|
|
|
|
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));
|
|
|
|
LOG(ERROR) << "parent SendMessage() failed: " << errString;
|
|
|
|
return false;
|
|
|
|
}
|
2010-08-27 06:32:45 -07:00
|
|
|
#endif
|
2009-11-05 16:24:27 -08:00
|
|
|
|
|
|
|
//--------------------------------------------------
|
|
|
|
#elif defined(OS_WIN)
|
|
|
|
|
2010-09-15 23:09:19 -07:00
|
|
|
FilePath exePath;
|
|
|
|
GetPathToBinary(exePath);
|
2009-06-29 11:38:29 -07:00
|
|
|
|
|
|
|
CommandLine cmdLine(exePath.ToWStringHack());
|
|
|
|
cmdLine.AppendSwitchWithValue(switches::kProcessChannelID, channel_id());
|
|
|
|
|
2009-11-05 16:24:27 -08:00
|
|
|
for (std::vector<std::string>::iterator it = aExtraOpts.begin();
|
2009-07-10 12:03:09 -07:00
|
|
|
it != aExtraOpts.end();
|
|
|
|
++it) {
|
2009-11-05 16:24:27 -08:00
|
|
|
cmdLine.AppendLooseValue(UTF8ToWide(*it));
|
2009-07-10 12:03:09 -07:00
|
|
|
}
|
|
|
|
|
2010-06-30 20:07:50 -07:00
|
|
|
cmdLine.AppendLooseValue(std::wstring(mGroupId.get()));
|
2010-08-10 15:08:06 -07:00
|
|
|
|
2011-03-24 21:07:05 -07:00
|
|
|
#ifdef MOZ_OMNIJAR
|
2010-08-10 15:08:06 -07:00
|
|
|
// Make sure the child process can find the omnijar
|
|
|
|
// See XRE_InitCommandLine in nsAppRunner.cpp
|
2011-03-24 21:07:05 -07:00
|
|
|
nsAutoString omnijarPath;
|
|
|
|
if (mozilla::OmnijarPath()) {
|
|
|
|
mozilla::OmnijarPath()->GetPath(omnijarPath);
|
|
|
|
cmdLine.AppendLooseValue(UTF8ToWide("-omnijar"));
|
|
|
|
cmdLine.AppendLooseValue(omnijarPath.get());
|
2010-08-10 15:08:06 -07:00
|
|
|
}
|
2011-03-24 21:07:05 -07:00
|
|
|
#endif
|
2010-08-10 15:08:06 -07:00
|
|
|
|
2009-10-27 14:52:37 -07:00
|
|
|
cmdLine.AppendLooseValue(UTF8ToWide(pidstring));
|
2010-09-10 10:09:30 -07:00
|
|
|
|
2010-01-12 13:14:38 -08:00
|
|
|
#if defined(MOZ_CRASHREPORTER)
|
|
|
|
cmdLine.AppendLooseValue(
|
2010-01-13 07:44:10 -08:00
|
|
|
UTF8ToWide(CrashReporter::GetChildNotificationPipe()));
|
2010-01-12 13:14:38 -08:00
|
|
|
#endif
|
2009-10-27 14:52:37 -07:00
|
|
|
|
2010-09-10 10:09:30 -07:00
|
|
|
cmdLine.AppendLooseValue(UTF8ToWide(childProcessType));
|
|
|
|
|
2009-06-29 11:38:29 -07:00
|
|
|
base::LaunchApp(cmdLine, false, false, &process);
|
2009-11-05 16:24:27 -08:00
|
|
|
|
2009-06-29 11:38:29 -07:00
|
|
|
#else
|
2009-11-05 16:24:27 -08:00
|
|
|
# error Sorry
|
2009-06-29 11:38:29 -07:00
|
|
|
#endif
|
|
|
|
|
|
|
|
if (!process) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
SetHandle(process);
|
2010-08-27 06:32:45 -07:00
|
|
|
#if defined(XP_MACOSX)
|
|
|
|
mChildTask = child_task;
|
|
|
|
#endif
|
2009-06-29 11:38:29 -07:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2009-07-13 22:12:50 -07:00
|
|
|
void
|
|
|
|
GeckoChildProcessHost::OnChannelConnected(int32 peer_pid)
|
|
|
|
{
|
2009-07-20 11:14:41 -07:00
|
|
|
MonitorAutoEnter mon(mMonitor);
|
|
|
|
mLaunched = true;
|
2009-10-27 14:52:37 -07:00
|
|
|
|
2010-03-24 17:12:31 -07:00
|
|
|
if (!base::OpenPrivilegedProcessHandle(peer_pid, &mChildProcessHandle))
|
2009-10-27 14:52:37 -07:00
|
|
|
NS_RUNTIMEABORT("can't open handle to child process");
|
|
|
|
|
2009-07-20 11:14:41 -07:00
|
|
|
mon.Notify();
|
2009-07-13 22:12:50 -07:00
|
|
|
}
|
2009-07-20 11:14:41 -07:00
|
|
|
|
|
|
|
// XXX/cjones: these next two methods should basically never be called.
|
|
|
|
// after the process is launched, its channel will be used to create
|
|
|
|
// one of our channels, AsyncChannel et al.
|
2009-06-29 11:38:29 -07:00
|
|
|
void
|
|
|
|
GeckoChildProcessHost::OnMessageReceived(const IPC::Message& aMsg)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
void
|
|
|
|
GeckoChildProcessHost::OnChannelError()
|
|
|
|
{
|
|
|
|
// XXXbent Notify that the child process is gone?
|
|
|
|
}
|
2009-09-02 17:18:27 -07:00
|
|
|
|
|
|
|
void
|
|
|
|
GeckoChildProcessHost::OnWaitableEventSignaled(base::WaitableEvent *event)
|
|
|
|
{
|
|
|
|
if (mDelegate) {
|
|
|
|
mDelegate->OnWaitableEventSignaled(event);
|
|
|
|
}
|
|
|
|
ChildProcessHost::OnWaitableEventSignaled(event);
|
|
|
|
}
|