Wed, 31 Dec 2014 06:55:50 +0100
Added tag UPSTREAM_283F7C6 for changeset ca08bd8f51b2
michael@0 | 1 | /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- |
michael@0 | 2 | */ |
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 "GeckoChildProcessHost.h" |
michael@0 | 8 | |
michael@0 | 9 | #if defined(XP_WIN) && defined(MOZ_CONTENT_SANDBOX) |
michael@0 | 10 | #include "sandboxBroker.h" |
michael@0 | 11 | #endif |
michael@0 | 12 | |
michael@0 | 13 | #include "base/command_line.h" |
michael@0 | 14 | #include "base/path_service.h" |
michael@0 | 15 | #include "base/string_util.h" |
michael@0 | 16 | #include "chrome/common/chrome_switches.h" |
michael@0 | 17 | #include "chrome/common/process_watcher.h" |
michael@0 | 18 | #ifdef MOZ_WIDGET_COCOA |
michael@0 | 19 | #include "chrome/common/mach_ipc_mac.h" |
michael@0 | 20 | #include "base/rand_util.h" |
michael@0 | 21 | #include "nsILocalFileMac.h" |
michael@0 | 22 | #endif |
michael@0 | 23 | |
michael@0 | 24 | #include "MainThreadUtils.h" |
michael@0 | 25 | #include "prprf.h" |
michael@0 | 26 | #include "prenv.h" |
michael@0 | 27 | |
michael@0 | 28 | #include "nsExceptionHandler.h" |
michael@0 | 29 | |
michael@0 | 30 | #include "nsDirectoryServiceDefs.h" |
michael@0 | 31 | #include "nsIFile.h" |
michael@0 | 32 | |
michael@0 | 33 | #include "mozilla/ClearOnShutdown.h" |
michael@0 | 34 | #include "mozilla/ipc/BrowserProcessSubThread.h" |
michael@0 | 35 | #include "mozilla/Omnijar.h" |
michael@0 | 36 | #include <sys/stat.h> |
michael@0 | 37 | |
michael@0 | 38 | #ifdef XP_WIN |
michael@0 | 39 | #include "nsIWinTaskbar.h" |
michael@0 | 40 | #define NS_TASKBAR_CONTRACTID "@mozilla.org/windows-taskbar;1" |
michael@0 | 41 | #endif |
michael@0 | 42 | |
michael@0 | 43 | #include "nsTArray.h" |
michael@0 | 44 | #include "nsClassHashtable.h" |
michael@0 | 45 | #include "nsHashKeys.h" |
michael@0 | 46 | |
michael@0 | 47 | using mozilla::MonitorAutoLock; |
michael@0 | 48 | using mozilla::ipc::GeckoChildProcessHost; |
michael@0 | 49 | |
michael@0 | 50 | #ifdef ANDROID |
michael@0 | 51 | // Like its predecessor in nsExceptionHandler.cpp, this is |
michael@0 | 52 | // the magic number of a file descriptor remapping we must |
michael@0 | 53 | // preserve for the child process. |
michael@0 | 54 | static const int kMagicAndroidSystemPropFd = 5; |
michael@0 | 55 | #endif |
michael@0 | 56 | |
michael@0 | 57 | static const bool kLowRightsSubprocesses = |
michael@0 | 58 | // We currently only attempt to drop privileges on gonk, because we |
michael@0 | 59 | // have no plugins or extensions to worry about breaking. |
michael@0 | 60 | #ifdef MOZ_WIDGET_GONK |
michael@0 | 61 | true |
michael@0 | 62 | #else |
michael@0 | 63 | false |
michael@0 | 64 | #endif |
michael@0 | 65 | ; |
michael@0 | 66 | |
michael@0 | 67 | mozilla::StaticRefPtr<nsIFile> GeckoChildProcessHost::sGreDir; |
michael@0 | 68 | mozilla::DebugOnly<bool> GeckoChildProcessHost::sGreDirCached; |
michael@0 | 69 | |
michael@0 | 70 | static bool |
michael@0 | 71 | ShouldHaveDirectoryService() |
michael@0 | 72 | { |
michael@0 | 73 | return GeckoProcessType_Default == XRE_GetProcessType(); |
michael@0 | 74 | } |
michael@0 | 75 | |
michael@0 | 76 | template<> |
michael@0 | 77 | struct RunnableMethodTraits<GeckoChildProcessHost> |
michael@0 | 78 | { |
michael@0 | 79 | static void RetainCallee(GeckoChildProcessHost* obj) { } |
michael@0 | 80 | static void ReleaseCallee(GeckoChildProcessHost* obj) { } |
michael@0 | 81 | }; |
michael@0 | 82 | |
michael@0 | 83 | /*static*/ |
michael@0 | 84 | base::ChildPrivileges |
michael@0 | 85 | GeckoChildProcessHost::DefaultChildPrivileges() |
michael@0 | 86 | { |
michael@0 | 87 | return (kLowRightsSubprocesses ? |
michael@0 | 88 | base::PRIVILEGES_UNPRIVILEGED : base::PRIVILEGES_INHERIT); |
michael@0 | 89 | } |
michael@0 | 90 | |
michael@0 | 91 | GeckoChildProcessHost::GeckoChildProcessHost(GeckoProcessType aProcessType, |
michael@0 | 92 | ChildPrivileges aPrivileges) |
michael@0 | 93 | : ChildProcessHost(RENDER_PROCESS), // FIXME/cjones: we should own this enum |
michael@0 | 94 | mProcessType(aProcessType), |
michael@0 | 95 | mSandboxEnabled(true), |
michael@0 | 96 | mPrivileges(aPrivileges), |
michael@0 | 97 | mMonitor("mozilla.ipc.GeckChildProcessHost.mMonitor"), |
michael@0 | 98 | mProcessState(CREATING_CHANNEL), |
michael@0 | 99 | mDelegate(nullptr), |
michael@0 | 100 | mChildProcessHandle(0) |
michael@0 | 101 | #if defined(MOZ_WIDGET_COCOA) |
michael@0 | 102 | , mChildTask(MACH_PORT_NULL) |
michael@0 | 103 | #endif |
michael@0 | 104 | { |
michael@0 | 105 | MOZ_COUNT_CTOR(GeckoChildProcessHost); |
michael@0 | 106 | } |
michael@0 | 107 | |
michael@0 | 108 | GeckoChildProcessHost::~GeckoChildProcessHost() |
michael@0 | 109 | |
michael@0 | 110 | { |
michael@0 | 111 | AssertIOThread(); |
michael@0 | 112 | |
michael@0 | 113 | MOZ_COUNT_DTOR(GeckoChildProcessHost); |
michael@0 | 114 | |
michael@0 | 115 | if (mChildProcessHandle > 0) |
michael@0 | 116 | ProcessWatcher::EnsureProcessTerminated(mChildProcessHandle |
michael@0 | 117 | #if defined(NS_BUILD_REFCNT_LOGGING) |
michael@0 | 118 | , false // don't "force" |
michael@0 | 119 | #endif |
michael@0 | 120 | ); |
michael@0 | 121 | |
michael@0 | 122 | #if defined(MOZ_WIDGET_COCOA) |
michael@0 | 123 | if (mChildTask != MACH_PORT_NULL) |
michael@0 | 124 | mach_port_deallocate(mach_task_self(), mChildTask); |
michael@0 | 125 | #endif |
michael@0 | 126 | } |
michael@0 | 127 | |
michael@0 | 128 | //static |
michael@0 | 129 | void |
michael@0 | 130 | GeckoChildProcessHost::GetPathToBinary(FilePath& exePath) |
michael@0 | 131 | { |
michael@0 | 132 | if (ShouldHaveDirectoryService()) { |
michael@0 | 133 | MOZ_ASSERT(sGreDirCached); |
michael@0 | 134 | if (sGreDir) { |
michael@0 | 135 | #ifdef OS_WIN |
michael@0 | 136 | nsString path; |
michael@0 | 137 | MOZ_ALWAYS_TRUE(NS_SUCCEEDED(sGreDir->GetPath(path))); |
michael@0 | 138 | #else |
michael@0 | 139 | nsCString path; |
michael@0 | 140 | MOZ_ALWAYS_TRUE(NS_SUCCEEDED(sGreDir->GetNativePath(path))); |
michael@0 | 141 | #endif |
michael@0 | 142 | exePath = FilePath(path.get()); |
michael@0 | 143 | #ifdef MOZ_WIDGET_COCOA |
michael@0 | 144 | // We need to use an App Bundle on OS X so that we can hide |
michael@0 | 145 | // the dock icon. See Bug 557225. |
michael@0 | 146 | exePath = exePath.AppendASCII(MOZ_CHILD_PROCESS_BUNDLE); |
michael@0 | 147 | #endif |
michael@0 | 148 | } |
michael@0 | 149 | } |
michael@0 | 150 | |
michael@0 | 151 | if (exePath.empty()) { |
michael@0 | 152 | #ifdef OS_WIN |
michael@0 | 153 | exePath = FilePath::FromWStringHack(CommandLine::ForCurrentProcess()->program()); |
michael@0 | 154 | #else |
michael@0 | 155 | exePath = FilePath(CommandLine::ForCurrentProcess()->argv()[0]); |
michael@0 | 156 | #endif |
michael@0 | 157 | exePath = exePath.DirName(); |
michael@0 | 158 | } |
michael@0 | 159 | |
michael@0 | 160 | exePath = exePath.AppendASCII(MOZ_CHILD_PROCESS_NAME); |
michael@0 | 161 | } |
michael@0 | 162 | |
michael@0 | 163 | #ifdef MOZ_WIDGET_COCOA |
michael@0 | 164 | class AutoCFTypeObject { |
michael@0 | 165 | public: |
michael@0 | 166 | AutoCFTypeObject(CFTypeRef object) |
michael@0 | 167 | { |
michael@0 | 168 | mObject = object; |
michael@0 | 169 | } |
michael@0 | 170 | ~AutoCFTypeObject() |
michael@0 | 171 | { |
michael@0 | 172 | ::CFRelease(mObject); |
michael@0 | 173 | } |
michael@0 | 174 | private: |
michael@0 | 175 | CFTypeRef mObject; |
michael@0 | 176 | }; |
michael@0 | 177 | #endif |
michael@0 | 178 | |
michael@0 | 179 | nsresult GeckoChildProcessHost::GetArchitecturesForBinary(const char *path, uint32_t *result) |
michael@0 | 180 | { |
michael@0 | 181 | *result = 0; |
michael@0 | 182 | |
michael@0 | 183 | #ifdef MOZ_WIDGET_COCOA |
michael@0 | 184 | CFURLRef url = ::CFURLCreateFromFileSystemRepresentation(kCFAllocatorDefault, |
michael@0 | 185 | (const UInt8*)path, |
michael@0 | 186 | strlen(path), |
michael@0 | 187 | false); |
michael@0 | 188 | if (!url) { |
michael@0 | 189 | return NS_ERROR_FAILURE; |
michael@0 | 190 | } |
michael@0 | 191 | AutoCFTypeObject autoPluginContainerURL(url); |
michael@0 | 192 | |
michael@0 | 193 | CFArrayRef pluginContainerArchs = ::CFBundleCopyExecutableArchitecturesForURL(url); |
michael@0 | 194 | if (!pluginContainerArchs) { |
michael@0 | 195 | return NS_ERROR_FAILURE; |
michael@0 | 196 | } |
michael@0 | 197 | AutoCFTypeObject autoPluginContainerArchs(pluginContainerArchs); |
michael@0 | 198 | |
michael@0 | 199 | CFIndex pluginArchCount = ::CFArrayGetCount(pluginContainerArchs); |
michael@0 | 200 | for (CFIndex i = 0; i < pluginArchCount; i++) { |
michael@0 | 201 | CFNumberRef currentArch = static_cast<CFNumberRef>(::CFArrayGetValueAtIndex(pluginContainerArchs, i)); |
michael@0 | 202 | int currentArchInt = 0; |
michael@0 | 203 | if (!::CFNumberGetValue(currentArch, kCFNumberIntType, ¤tArchInt)) { |
michael@0 | 204 | continue; |
michael@0 | 205 | } |
michael@0 | 206 | switch (currentArchInt) { |
michael@0 | 207 | case kCFBundleExecutableArchitectureI386: |
michael@0 | 208 | *result |= base::PROCESS_ARCH_I386; |
michael@0 | 209 | break; |
michael@0 | 210 | case kCFBundleExecutableArchitectureX86_64: |
michael@0 | 211 | *result |= base::PROCESS_ARCH_X86_64; |
michael@0 | 212 | break; |
michael@0 | 213 | case kCFBundleExecutableArchitecturePPC: |
michael@0 | 214 | *result |= base::PROCESS_ARCH_PPC; |
michael@0 | 215 | break; |
michael@0 | 216 | default: |
michael@0 | 217 | break; |
michael@0 | 218 | } |
michael@0 | 219 | } |
michael@0 | 220 | |
michael@0 | 221 | return (*result ? NS_OK : NS_ERROR_FAILURE); |
michael@0 | 222 | #else |
michael@0 | 223 | return NS_ERROR_NOT_IMPLEMENTED; |
michael@0 | 224 | #endif |
michael@0 | 225 | } |
michael@0 | 226 | |
michael@0 | 227 | uint32_t GeckoChildProcessHost::GetSupportedArchitecturesForProcessType(GeckoProcessType type) |
michael@0 | 228 | { |
michael@0 | 229 | #ifdef MOZ_WIDGET_COCOA |
michael@0 | 230 | if (type == GeckoProcessType_Plugin) { |
michael@0 | 231 | |
michael@0 | 232 | // Cache this, it shouldn't ever change. |
michael@0 | 233 | static uint32_t pluginContainerArchs = 0; |
michael@0 | 234 | if (pluginContainerArchs == 0) { |
michael@0 | 235 | CacheGreDir(); |
michael@0 | 236 | FilePath exePath; |
michael@0 | 237 | GetPathToBinary(exePath); |
michael@0 | 238 | nsresult rv = GetArchitecturesForBinary(exePath.value().c_str(), &pluginContainerArchs); |
michael@0 | 239 | NS_ASSERTION(NS_SUCCEEDED(rv) && pluginContainerArchs != 0, "Getting architecture of plugin container failed!"); |
michael@0 | 240 | if (NS_FAILED(rv) || pluginContainerArchs == 0) { |
michael@0 | 241 | pluginContainerArchs = base::GetCurrentProcessArchitecture(); |
michael@0 | 242 | } |
michael@0 | 243 | } |
michael@0 | 244 | return pluginContainerArchs; |
michael@0 | 245 | } |
michael@0 | 246 | #endif |
michael@0 | 247 | |
michael@0 | 248 | return base::GetCurrentProcessArchitecture(); |
michael@0 | 249 | } |
michael@0 | 250 | |
michael@0 | 251 | void |
michael@0 | 252 | GeckoChildProcessHost::PrepareLaunch() |
michael@0 | 253 | { |
michael@0 | 254 | #ifdef MOZ_CRASHREPORTER |
michael@0 | 255 | if (CrashReporter::GetEnabled()) { |
michael@0 | 256 | CrashReporter::OOPInit(); |
michael@0 | 257 | } |
michael@0 | 258 | #endif |
michael@0 | 259 | |
michael@0 | 260 | #ifdef XP_WIN |
michael@0 | 261 | InitWindowsGroupID(); |
michael@0 | 262 | #endif |
michael@0 | 263 | CacheGreDir(); |
michael@0 | 264 | } |
michael@0 | 265 | |
michael@0 | 266 | //static |
michael@0 | 267 | void |
michael@0 | 268 | GeckoChildProcessHost::CacheGreDir() |
michael@0 | 269 | { |
michael@0 | 270 | // PerformAysncLaunchInternal/GetPathToBinary may be called on the IO thread, |
michael@0 | 271 | // and they want to use the directory service, which needs to happen on the |
michael@0 | 272 | // main thread (in the event that its implemented in JS). So we grab |
michael@0 | 273 | // NS_GRE_DIR here and stash it. |
michael@0 | 274 | |
michael@0 | 275 | #ifdef MOZ_WIDGET_GONK |
michael@0 | 276 | // Apparently, this ASSERT should be present on all platforms. Currently, |
michael@0 | 277 | // this assert causes mochitest failures on the Mac platform if its left in. |
michael@0 | 278 | |
michael@0 | 279 | // B2G overrides the directory service in JS, so this needs to be called |
michael@0 | 280 | // on the main thread. |
michael@0 | 281 | MOZ_ASSERT(NS_IsMainThread()); |
michael@0 | 282 | #endif |
michael@0 | 283 | |
michael@0 | 284 | if (ShouldHaveDirectoryService()) { |
michael@0 | 285 | nsCOMPtr<nsIProperties> directoryService(do_GetService(NS_DIRECTORY_SERVICE_CONTRACTID)); |
michael@0 | 286 | MOZ_ASSERT(directoryService, "Expected XPCOM to be available"); |
michael@0 | 287 | if (directoryService) { |
michael@0 | 288 | // getter_AddRefs doesn't work with StaticRefPtr, so we need to store the |
michael@0 | 289 | // result in an nsCOMPtr and copy it over. |
michael@0 | 290 | nsCOMPtr<nsIFile> greDir; |
michael@0 | 291 | nsresult rv = directoryService->Get(NS_GRE_DIR, NS_GET_IID(nsIFile), getter_AddRefs(greDir)); |
michael@0 | 292 | if (NS_SUCCEEDED(rv)) { |
michael@0 | 293 | sGreDir = greDir; |
michael@0 | 294 | mozilla::ClearOnShutdown(&sGreDir); |
michael@0 | 295 | } |
michael@0 | 296 | } |
michael@0 | 297 | } |
michael@0 | 298 | sGreDirCached = true; |
michael@0 | 299 | } |
michael@0 | 300 | |
michael@0 | 301 | #ifdef XP_WIN |
michael@0 | 302 | void GeckoChildProcessHost::InitWindowsGroupID() |
michael@0 | 303 | { |
michael@0 | 304 | // On Win7+, pass the application user model to the child, so it can |
michael@0 | 305 | // register with it. This insures windows created by the container |
michael@0 | 306 | // properly group with the parent app on the Win7 taskbar. |
michael@0 | 307 | nsCOMPtr<nsIWinTaskbar> taskbarInfo = |
michael@0 | 308 | do_GetService(NS_TASKBAR_CONTRACTID); |
michael@0 | 309 | if (taskbarInfo) { |
michael@0 | 310 | bool isSupported = false; |
michael@0 | 311 | taskbarInfo->GetAvailable(&isSupported); |
michael@0 | 312 | nsAutoString appId; |
michael@0 | 313 | if (isSupported && NS_SUCCEEDED(taskbarInfo->GetDefaultGroupId(appId))) { |
michael@0 | 314 | mGroupId.Append(appId); |
michael@0 | 315 | } else { |
michael@0 | 316 | mGroupId.AssignLiteral("-"); |
michael@0 | 317 | } |
michael@0 | 318 | } |
michael@0 | 319 | } |
michael@0 | 320 | #endif |
michael@0 | 321 | |
michael@0 | 322 | bool |
michael@0 | 323 | GeckoChildProcessHost::SyncLaunch(std::vector<std::string> aExtraOpts, int aTimeoutMs, base::ProcessArchitecture arch) |
michael@0 | 324 | { |
michael@0 | 325 | PrepareLaunch(); |
michael@0 | 326 | |
michael@0 | 327 | PRIntervalTime timeoutTicks = (aTimeoutMs > 0) ? |
michael@0 | 328 | PR_MillisecondsToInterval(aTimeoutMs) : PR_INTERVAL_NO_TIMEOUT; |
michael@0 | 329 | MessageLoop* ioLoop = XRE_GetIOMessageLoop(); |
michael@0 | 330 | NS_ASSERTION(MessageLoop::current() != ioLoop, "sync launch from the IO thread NYI"); |
michael@0 | 331 | |
michael@0 | 332 | ioLoop->PostTask(FROM_HERE, |
michael@0 | 333 | NewRunnableMethod(this, |
michael@0 | 334 | &GeckoChildProcessHost::RunPerformAsyncLaunch, |
michael@0 | 335 | aExtraOpts, arch)); |
michael@0 | 336 | // NB: this uses a different mechanism than the chromium parent |
michael@0 | 337 | // class. |
michael@0 | 338 | MonitorAutoLock lock(mMonitor); |
michael@0 | 339 | PRIntervalTime waitStart = PR_IntervalNow(); |
michael@0 | 340 | PRIntervalTime current; |
michael@0 | 341 | |
michael@0 | 342 | // We'll receive several notifications, we need to exit when we |
michael@0 | 343 | // have either successfully launched or have timed out. |
michael@0 | 344 | while (mProcessState != PROCESS_CONNECTED) { |
michael@0 | 345 | // If there was an error then return it, don't wait out the timeout. |
michael@0 | 346 | if (mProcessState == PROCESS_ERROR) { |
michael@0 | 347 | break; |
michael@0 | 348 | } |
michael@0 | 349 | |
michael@0 | 350 | lock.Wait(timeoutTicks); |
michael@0 | 351 | |
michael@0 | 352 | if (timeoutTicks != PR_INTERVAL_NO_TIMEOUT) { |
michael@0 | 353 | current = PR_IntervalNow(); |
michael@0 | 354 | PRIntervalTime elapsed = current - waitStart; |
michael@0 | 355 | if (elapsed > timeoutTicks) { |
michael@0 | 356 | break; |
michael@0 | 357 | } |
michael@0 | 358 | timeoutTicks = timeoutTicks - elapsed; |
michael@0 | 359 | waitStart = current; |
michael@0 | 360 | } |
michael@0 | 361 | } |
michael@0 | 362 | |
michael@0 | 363 | return mProcessState == PROCESS_CONNECTED; |
michael@0 | 364 | } |
michael@0 | 365 | |
michael@0 | 366 | bool |
michael@0 | 367 | GeckoChildProcessHost::AsyncLaunch(std::vector<std::string> aExtraOpts) |
michael@0 | 368 | { |
michael@0 | 369 | PrepareLaunch(); |
michael@0 | 370 | |
michael@0 | 371 | MessageLoop* ioLoop = XRE_GetIOMessageLoop(); |
michael@0 | 372 | ioLoop->PostTask(FROM_HERE, |
michael@0 | 373 | NewRunnableMethod(this, |
michael@0 | 374 | &GeckoChildProcessHost::RunPerformAsyncLaunch, |
michael@0 | 375 | aExtraOpts, base::GetCurrentProcessArchitecture())); |
michael@0 | 376 | |
michael@0 | 377 | // This may look like the sync launch wait, but we only delay as |
michael@0 | 378 | // long as it takes to create the channel. |
michael@0 | 379 | MonitorAutoLock lock(mMonitor); |
michael@0 | 380 | while (mProcessState < CHANNEL_INITIALIZED) { |
michael@0 | 381 | lock.Wait(); |
michael@0 | 382 | } |
michael@0 | 383 | |
michael@0 | 384 | return true; |
michael@0 | 385 | } |
michael@0 | 386 | |
michael@0 | 387 | bool |
michael@0 | 388 | GeckoChildProcessHost::LaunchAndWaitForProcessHandle(StringVector aExtraOpts) |
michael@0 | 389 | { |
michael@0 | 390 | PrepareLaunch(); |
michael@0 | 391 | |
michael@0 | 392 | MessageLoop* ioLoop = XRE_GetIOMessageLoop(); |
michael@0 | 393 | ioLoop->PostTask(FROM_HERE, |
michael@0 | 394 | NewRunnableMethod(this, |
michael@0 | 395 | &GeckoChildProcessHost::RunPerformAsyncLaunch, |
michael@0 | 396 | aExtraOpts, base::GetCurrentProcessArchitecture())); |
michael@0 | 397 | |
michael@0 | 398 | MonitorAutoLock lock(mMonitor); |
michael@0 | 399 | while (mProcessState < PROCESS_CREATED) { |
michael@0 | 400 | lock.Wait(); |
michael@0 | 401 | } |
michael@0 | 402 | MOZ_ASSERT(mProcessState == PROCESS_ERROR || mChildProcessHandle); |
michael@0 | 403 | |
michael@0 | 404 | return mProcessState < PROCESS_ERROR; |
michael@0 | 405 | } |
michael@0 | 406 | |
michael@0 | 407 | void |
michael@0 | 408 | GeckoChildProcessHost::InitializeChannel() |
michael@0 | 409 | { |
michael@0 | 410 | CreateChannel(); |
michael@0 | 411 | |
michael@0 | 412 | MonitorAutoLock lock(mMonitor); |
michael@0 | 413 | mProcessState = CHANNEL_INITIALIZED; |
michael@0 | 414 | lock.Notify(); |
michael@0 | 415 | } |
michael@0 | 416 | |
michael@0 | 417 | void |
michael@0 | 418 | GeckoChildProcessHost::Join() |
michael@0 | 419 | { |
michael@0 | 420 | AssertIOThread(); |
michael@0 | 421 | |
michael@0 | 422 | if (!mChildProcessHandle) { |
michael@0 | 423 | return; |
michael@0 | 424 | } |
michael@0 | 425 | |
michael@0 | 426 | // If this fails, there's nothing we can do. |
michael@0 | 427 | base::KillProcess(mChildProcessHandle, 0, /*wait*/true); |
michael@0 | 428 | SetAlreadyDead(); |
michael@0 | 429 | } |
michael@0 | 430 | |
michael@0 | 431 | void |
michael@0 | 432 | GeckoChildProcessHost::SetAlreadyDead() |
michael@0 | 433 | { |
michael@0 | 434 | mChildProcessHandle = 0; |
michael@0 | 435 | } |
michael@0 | 436 | |
michael@0 | 437 | int32_t GeckoChildProcessHost::mChildCounter = 0; |
michael@0 | 438 | |
michael@0 | 439 | // |
michael@0 | 440 | // Wrapper function for handling GECKO_SEPARATE_NSPR_LOGS |
michael@0 | 441 | // |
michael@0 | 442 | bool |
michael@0 | 443 | GeckoChildProcessHost::PerformAsyncLaunch(std::vector<std::string> aExtraOpts, base::ProcessArchitecture arch) |
michael@0 | 444 | { |
michael@0 | 445 | // If NSPR log files are not requested, we're done. |
michael@0 | 446 | const char* origLogName = PR_GetEnv("NSPR_LOG_FILE"); |
michael@0 | 447 | if (!origLogName) { |
michael@0 | 448 | return PerformAsyncLaunchInternal(aExtraOpts, arch); |
michael@0 | 449 | } |
michael@0 | 450 | |
michael@0 | 451 | // We currently have no portable way to launch child with environment |
michael@0 | 452 | // different than parent. So temporarily change NSPR_LOG_FILE so child |
michael@0 | 453 | // inherits value we want it to have. (NSPR only looks at NSPR_LOG_FILE at |
michael@0 | 454 | // startup, so it's 'safe' to play with the parent's environment this way.) |
michael@0 | 455 | nsAutoCString setChildLogName("NSPR_LOG_FILE="); |
michael@0 | 456 | setChildLogName.Append(origLogName); |
michael@0 | 457 | |
michael@0 | 458 | // remember original value so we can restore it. |
michael@0 | 459 | // - buffer needs to be permanently allocated for PR_SetEnv() |
michael@0 | 460 | // - Note: this code is not called re-entrantly, nor are restoreOrigLogName |
michael@0 | 461 | // or mChildCounter touched by any other thread, so this is safe. |
michael@0 | 462 | static char* restoreOrigLogName = 0; |
michael@0 | 463 | if (!restoreOrigLogName) |
michael@0 | 464 | restoreOrigLogName = strdup(setChildLogName.get()); |
michael@0 | 465 | |
michael@0 | 466 | // Append child-specific postfix to name |
michael@0 | 467 | setChildLogName.AppendLiteral(".child-"); |
michael@0 | 468 | setChildLogName.AppendInt(++mChildCounter); |
michael@0 | 469 | |
michael@0 | 470 | // Passing temporary to PR_SetEnv is ok here because env gets copied |
michael@0 | 471 | // by exec, etc., to permanent storage in child when process launched. |
michael@0 | 472 | PR_SetEnv(setChildLogName.get()); |
michael@0 | 473 | bool retval = PerformAsyncLaunchInternal(aExtraOpts, arch); |
michael@0 | 474 | |
michael@0 | 475 | // Revert to original value |
michael@0 | 476 | PR_SetEnv(restoreOrigLogName); |
michael@0 | 477 | |
michael@0 | 478 | return retval; |
michael@0 | 479 | } |
michael@0 | 480 | |
michael@0 | 481 | bool |
michael@0 | 482 | GeckoChildProcessHost::RunPerformAsyncLaunch(std::vector<std::string> aExtraOpts, |
michael@0 | 483 | base::ProcessArchitecture aArch) |
michael@0 | 484 | { |
michael@0 | 485 | InitializeChannel(); |
michael@0 | 486 | return PerformAsyncLaunch(aExtraOpts, aArch); |
michael@0 | 487 | } |
michael@0 | 488 | |
michael@0 | 489 | void |
michael@0 | 490 | #if defined(XP_WIN) |
michael@0 | 491 | AddAppDirToCommandLine(CommandLine& aCmdLine) |
michael@0 | 492 | #else |
michael@0 | 493 | AddAppDirToCommandLine(std::vector<std::string>& aCmdLine) |
michael@0 | 494 | #endif |
michael@0 | 495 | { |
michael@0 | 496 | // Content processes need access to application resources, so pass |
michael@0 | 497 | // the full application directory path to the child process. |
michael@0 | 498 | if (ShouldHaveDirectoryService()) { |
michael@0 | 499 | nsCOMPtr<nsIProperties> directoryService(do_GetService(NS_DIRECTORY_SERVICE_CONTRACTID)); |
michael@0 | 500 | NS_ASSERTION(directoryService, "Expected XPCOM to be available"); |
michael@0 | 501 | if (directoryService) { |
michael@0 | 502 | nsCOMPtr<nsIFile> appDir; |
michael@0 | 503 | // NS_XPCOM_CURRENT_PROCESS_DIR really means the app dir, not the |
michael@0 | 504 | // current process dir. |
michael@0 | 505 | nsresult rv = directoryService->Get(NS_XPCOM_CURRENT_PROCESS_DIR, |
michael@0 | 506 | NS_GET_IID(nsIFile), |
michael@0 | 507 | getter_AddRefs(appDir)); |
michael@0 | 508 | if (NS_SUCCEEDED(rv)) { |
michael@0 | 509 | nsAutoCString path; |
michael@0 | 510 | MOZ_ALWAYS_TRUE(NS_SUCCEEDED(appDir->GetNativePath(path))); |
michael@0 | 511 | #if defined(XP_WIN) |
michael@0 | 512 | aCmdLine.AppendLooseValue(UTF8ToWide("-appdir")); |
michael@0 | 513 | aCmdLine.AppendLooseValue(UTF8ToWide(path.get())); |
michael@0 | 514 | #else |
michael@0 | 515 | aCmdLine.push_back("-appdir"); |
michael@0 | 516 | aCmdLine.push_back(path.get()); |
michael@0 | 517 | #endif |
michael@0 | 518 | } |
michael@0 | 519 | } |
michael@0 | 520 | } |
michael@0 | 521 | } |
michael@0 | 522 | |
michael@0 | 523 | bool |
michael@0 | 524 | GeckoChildProcessHost::PerformAsyncLaunchInternal(std::vector<std::string>& aExtraOpts, base::ProcessArchitecture arch) |
michael@0 | 525 | { |
michael@0 | 526 | // We rely on the fact that InitializeChannel() has already been processed |
michael@0 | 527 | // on the IO thread before this point is reached. |
michael@0 | 528 | if (!GetChannel()) { |
michael@0 | 529 | return false; |
michael@0 | 530 | } |
michael@0 | 531 | |
michael@0 | 532 | base::ProcessHandle process = 0; |
michael@0 | 533 | |
michael@0 | 534 | // send the child the PID so that it can open a ProcessHandle back to us. |
michael@0 | 535 | // probably don't want to do this in the long run |
michael@0 | 536 | char pidstring[32]; |
michael@0 | 537 | PR_snprintf(pidstring, sizeof(pidstring) - 1, |
michael@0 | 538 | "%ld", base::Process::Current().pid()); |
michael@0 | 539 | |
michael@0 | 540 | const char* const childProcessType = |
michael@0 | 541 | XRE_ChildProcessTypeToString(mProcessType); |
michael@0 | 542 | |
michael@0 | 543 | //-------------------------------------------------- |
michael@0 | 544 | #if defined(OS_POSIX) |
michael@0 | 545 | // For POSIX, we have to be extremely anal about *not* using |
michael@0 | 546 | // std::wstring in code compiled with Mozilla's -fshort-wchar |
michael@0 | 547 | // configuration, because chromium is compiled with -fno-short-wchar |
michael@0 | 548 | // and passing wstrings from one config to the other is unsafe. So |
michael@0 | 549 | // we split the logic here. |
michael@0 | 550 | |
michael@0 | 551 | #if defined(OS_LINUX) || defined(OS_MACOSX) || defined(OS_BSD) |
michael@0 | 552 | base::environment_map newEnvVars; |
michael@0 | 553 | ChildPrivileges privs = mPrivileges; |
michael@0 | 554 | if (privs == base::PRIVILEGES_DEFAULT) { |
michael@0 | 555 | privs = DefaultChildPrivileges(); |
michael@0 | 556 | } |
michael@0 | 557 | // XPCOM may not be initialized in some subprocesses. We don't want |
michael@0 | 558 | // to initialize XPCOM just for the directory service, especially |
michael@0 | 559 | // since LD_LIBRARY_PATH is already set correctly in subprocesses |
michael@0 | 560 | // (meaning that we don't need to set that up in the environment). |
michael@0 | 561 | if (ShouldHaveDirectoryService()) { |
michael@0 | 562 | MOZ_ASSERT(sGreDirCached); |
michael@0 | 563 | if (sGreDir) { |
michael@0 | 564 | nsCString path; |
michael@0 | 565 | MOZ_ALWAYS_TRUE(NS_SUCCEEDED(sGreDir->GetNativePath(path))); |
michael@0 | 566 | # if defined(OS_LINUX) || defined(OS_BSD) |
michael@0 | 567 | # if defined(MOZ_WIDGET_ANDROID) |
michael@0 | 568 | path += "/lib"; |
michael@0 | 569 | # endif // MOZ_WIDGET_ANDROID |
michael@0 | 570 | const char *ld_library_path = PR_GetEnv("LD_LIBRARY_PATH"); |
michael@0 | 571 | nsCString new_ld_lib_path; |
michael@0 | 572 | if (ld_library_path && *ld_library_path) { |
michael@0 | 573 | new_ld_lib_path.Assign(path.get()); |
michael@0 | 574 | new_ld_lib_path.AppendLiteral(":"); |
michael@0 | 575 | new_ld_lib_path.Append(ld_library_path); |
michael@0 | 576 | newEnvVars["LD_LIBRARY_PATH"] = new_ld_lib_path.get(); |
michael@0 | 577 | } else { |
michael@0 | 578 | newEnvVars["LD_LIBRARY_PATH"] = path.get(); |
michael@0 | 579 | } |
michael@0 | 580 | # elif OS_MACOSX |
michael@0 | 581 | newEnvVars["DYLD_LIBRARY_PATH"] = path.get(); |
michael@0 | 582 | // XXX DYLD_INSERT_LIBRARIES should only be set when launching a plugin |
michael@0 | 583 | // process, and has no effect on other subprocesses (the hooks in |
michael@0 | 584 | // libplugin_child_interpose.dylib become noops). But currently it |
michael@0 | 585 | // gets set when launching any kind of subprocess. |
michael@0 | 586 | // |
michael@0 | 587 | // Trigger "dyld interposing" for the dylib that contains |
michael@0 | 588 | // plugin_child_interpose.mm. This allows us to hook OS calls in the |
michael@0 | 589 | // plugin process (ones that don't work correctly in a background |
michael@0 | 590 | // process). Don't break any other "dyld interposing" that has already |
michael@0 | 591 | // been set up by whatever may have launched the browser. |
michael@0 | 592 | const char* prevInterpose = PR_GetEnv("DYLD_INSERT_LIBRARIES"); |
michael@0 | 593 | nsCString interpose; |
michael@0 | 594 | if (prevInterpose) { |
michael@0 | 595 | interpose.Assign(prevInterpose); |
michael@0 | 596 | interpose.AppendLiteral(":"); |
michael@0 | 597 | } |
michael@0 | 598 | interpose.Append(path.get()); |
michael@0 | 599 | interpose.AppendLiteral("/libplugin_child_interpose.dylib"); |
michael@0 | 600 | newEnvVars["DYLD_INSERT_LIBRARIES"] = interpose.get(); |
michael@0 | 601 | # endif // OS_LINUX |
michael@0 | 602 | } |
michael@0 | 603 | } |
michael@0 | 604 | #endif // OS_LINUX || OS_MACOSX |
michael@0 | 605 | |
michael@0 | 606 | FilePath exePath; |
michael@0 | 607 | GetPathToBinary(exePath); |
michael@0 | 608 | |
michael@0 | 609 | #ifdef MOZ_WIDGET_ANDROID |
michael@0 | 610 | // The java wrapper unpacks this for us but can't make it executable |
michael@0 | 611 | chmod(exePath.value().c_str(), 0700); |
michael@0 | 612 | #endif // MOZ_WIDGET_ANDROID |
michael@0 | 613 | |
michael@0 | 614 | #ifdef ANDROID |
michael@0 | 615 | // Remap the Android property workspace to a well-known int, |
michael@0 | 616 | // and update the environment to reflect the new value for the |
michael@0 | 617 | // child process. |
michael@0 | 618 | const char *apws = getenv("ANDROID_PROPERTY_WORKSPACE"); |
michael@0 | 619 | if (apws) { |
michael@0 | 620 | int fd = atoi(apws); |
michael@0 | 621 | mFileMap.push_back(std::pair<int, int>(fd, kMagicAndroidSystemPropFd)); |
michael@0 | 622 | |
michael@0 | 623 | char buf[32]; |
michael@0 | 624 | char *szptr = strchr(apws, ','); |
michael@0 | 625 | |
michael@0 | 626 | snprintf(buf, sizeof(buf), "%d%s", kMagicAndroidSystemPropFd, szptr); |
michael@0 | 627 | newEnvVars["ANDROID_PROPERTY_WORKSPACE"] = buf; |
michael@0 | 628 | } |
michael@0 | 629 | #endif // ANDROID |
michael@0 | 630 | |
michael@0 | 631 | #ifdef MOZ_WIDGET_GONK |
michael@0 | 632 | if (const char *ldPreloadPath = getenv("LD_PRELOAD")) { |
michael@0 | 633 | newEnvVars["LD_PRELOAD"] = ldPreloadPath; |
michael@0 | 634 | } |
michael@0 | 635 | #endif // MOZ_WIDGET_GONK |
michael@0 | 636 | |
michael@0 | 637 | // remap the IPC socket fd to a well-known int, as the OS does for |
michael@0 | 638 | // STDOUT_FILENO, for example |
michael@0 | 639 | int srcChannelFd, dstChannelFd; |
michael@0 | 640 | channel().GetClientFileDescriptorMapping(&srcChannelFd, &dstChannelFd); |
michael@0 | 641 | mFileMap.push_back(std::pair<int,int>(srcChannelFd, dstChannelFd)); |
michael@0 | 642 | |
michael@0 | 643 | // no need for kProcessChannelID, the child process inherits the |
michael@0 | 644 | // other end of the socketpair() from us |
michael@0 | 645 | |
michael@0 | 646 | std::vector<std::string> childArgv; |
michael@0 | 647 | |
michael@0 | 648 | childArgv.push_back(exePath.value()); |
michael@0 | 649 | |
michael@0 | 650 | childArgv.insert(childArgv.end(), aExtraOpts.begin(), aExtraOpts.end()); |
michael@0 | 651 | |
michael@0 | 652 | if (Omnijar::IsInitialized()) { |
michael@0 | 653 | // Make sure that child processes can find the omnijar |
michael@0 | 654 | // See XRE_InitCommandLine in nsAppRunner.cpp |
michael@0 | 655 | nsAutoCString path; |
michael@0 | 656 | nsCOMPtr<nsIFile> file = Omnijar::GetPath(Omnijar::GRE); |
michael@0 | 657 | if (file && NS_SUCCEEDED(file->GetNativePath(path))) { |
michael@0 | 658 | childArgv.push_back("-greomni"); |
michael@0 | 659 | childArgv.push_back(path.get()); |
michael@0 | 660 | } |
michael@0 | 661 | file = Omnijar::GetPath(Omnijar::APP); |
michael@0 | 662 | if (file && NS_SUCCEEDED(file->GetNativePath(path))) { |
michael@0 | 663 | childArgv.push_back("-appomni"); |
michael@0 | 664 | childArgv.push_back(path.get()); |
michael@0 | 665 | } |
michael@0 | 666 | } |
michael@0 | 667 | |
michael@0 | 668 | // Add the application directory path (-appdir path) |
michael@0 | 669 | AddAppDirToCommandLine(childArgv); |
michael@0 | 670 | |
michael@0 | 671 | childArgv.push_back(pidstring); |
michael@0 | 672 | |
michael@0 | 673 | #if defined(MOZ_CRASHREPORTER) |
michael@0 | 674 | # if defined(OS_LINUX) || defined(OS_BSD) |
michael@0 | 675 | int childCrashFd, childCrashRemapFd; |
michael@0 | 676 | if (!CrashReporter::CreateNotificationPipeForChild( |
michael@0 | 677 | &childCrashFd, &childCrashRemapFd)) |
michael@0 | 678 | return false; |
michael@0 | 679 | if (0 <= childCrashFd) { |
michael@0 | 680 | mFileMap.push_back(std::pair<int,int>(childCrashFd, childCrashRemapFd)); |
michael@0 | 681 | // "true" == crash reporting enabled |
michael@0 | 682 | childArgv.push_back("true"); |
michael@0 | 683 | } |
michael@0 | 684 | else { |
michael@0 | 685 | // "false" == crash reporting disabled |
michael@0 | 686 | childArgv.push_back("false"); |
michael@0 | 687 | } |
michael@0 | 688 | # elif defined(MOZ_WIDGET_COCOA) |
michael@0 | 689 | childArgv.push_back(CrashReporter::GetChildNotificationPipe()); |
michael@0 | 690 | # endif // OS_LINUX |
michael@0 | 691 | #endif |
michael@0 | 692 | |
michael@0 | 693 | #ifdef MOZ_WIDGET_COCOA |
michael@0 | 694 | // Add a mach port to the command line so the child can communicate its |
michael@0 | 695 | // 'task_t' back to the parent. |
michael@0 | 696 | // |
michael@0 | 697 | // Put a random number into the channel name, so that a compromised renderer |
michael@0 | 698 | // can't pretend being the child that's forked off. |
michael@0 | 699 | std::string mach_connection_name = StringPrintf("org.mozilla.machname.%d", |
michael@0 | 700 | base::RandInt(0, std::numeric_limits<int>::max())); |
michael@0 | 701 | childArgv.push_back(mach_connection_name.c_str()); |
michael@0 | 702 | #endif |
michael@0 | 703 | |
michael@0 | 704 | childArgv.push_back(childProcessType); |
michael@0 | 705 | |
michael@0 | 706 | base::LaunchApp(childArgv, mFileMap, |
michael@0 | 707 | #if defined(OS_LINUX) || defined(OS_MACOSX) || defined(OS_BSD) |
michael@0 | 708 | newEnvVars, privs, |
michael@0 | 709 | #endif |
michael@0 | 710 | false, &process, arch); |
michael@0 | 711 | |
michael@0 | 712 | // We're in the parent and the child was launched. Close the child FD in the |
michael@0 | 713 | // parent as soon as possible, which will allow the parent to detect when the |
michael@0 | 714 | // child closes its FD (either due to normal exit or due to crash). |
michael@0 | 715 | GetChannel()->CloseClientFileDescriptor(); |
michael@0 | 716 | |
michael@0 | 717 | #ifdef MOZ_WIDGET_COCOA |
michael@0 | 718 | // Wait for the child process to send us its 'task_t' data. |
michael@0 | 719 | const int kTimeoutMs = 10000; |
michael@0 | 720 | |
michael@0 | 721 | MachReceiveMessage child_message; |
michael@0 | 722 | ReceivePort parent_recv_port(mach_connection_name.c_str()); |
michael@0 | 723 | kern_return_t err = parent_recv_port.WaitForMessage(&child_message, kTimeoutMs); |
michael@0 | 724 | if (err != KERN_SUCCESS) { |
michael@0 | 725 | std::string errString = StringPrintf("0x%x %s", err, mach_error_string(err)); |
michael@0 | 726 | CHROMIUM_LOG(ERROR) << "parent WaitForMessage() failed: " << errString; |
michael@0 | 727 | return false; |
michael@0 | 728 | } |
michael@0 | 729 | |
michael@0 | 730 | task_t child_task = child_message.GetTranslatedPort(0); |
michael@0 | 731 | if (child_task == MACH_PORT_NULL) { |
michael@0 | 732 | CHROMIUM_LOG(ERROR) << "parent GetTranslatedPort(0) failed."; |
michael@0 | 733 | return false; |
michael@0 | 734 | } |
michael@0 | 735 | |
michael@0 | 736 | if (child_message.GetTranslatedPort(1) == MACH_PORT_NULL) { |
michael@0 | 737 | CHROMIUM_LOG(ERROR) << "parent GetTranslatedPort(1) failed."; |
michael@0 | 738 | return false; |
michael@0 | 739 | } |
michael@0 | 740 | MachPortSender parent_sender(child_message.GetTranslatedPort(1)); |
michael@0 | 741 | |
michael@0 | 742 | MachSendMessage parent_message(/* id= */0); |
michael@0 | 743 | if (!parent_message.AddDescriptor(bootstrap_port)) { |
michael@0 | 744 | CHROMIUM_LOG(ERROR) << "parent AddDescriptor(" << bootstrap_port << ") failed."; |
michael@0 | 745 | return false; |
michael@0 | 746 | } |
michael@0 | 747 | |
michael@0 | 748 | err = parent_sender.SendMessage(parent_message, kTimeoutMs); |
michael@0 | 749 | if (err != KERN_SUCCESS) { |
michael@0 | 750 | std::string errString = StringPrintf("0x%x %s", err, mach_error_string(err)); |
michael@0 | 751 | CHROMIUM_LOG(ERROR) << "parent SendMessage() failed: " << errString; |
michael@0 | 752 | return false; |
michael@0 | 753 | } |
michael@0 | 754 | #endif |
michael@0 | 755 | |
michael@0 | 756 | //-------------------------------------------------- |
michael@0 | 757 | #elif defined(OS_WIN) |
michael@0 | 758 | |
michael@0 | 759 | FilePath exePath; |
michael@0 | 760 | GetPathToBinary(exePath); |
michael@0 | 761 | |
michael@0 | 762 | CommandLine cmdLine(exePath.ToWStringHack()); |
michael@0 | 763 | cmdLine.AppendSwitchWithValue(switches::kProcessChannelID, channel_id()); |
michael@0 | 764 | |
michael@0 | 765 | for (std::vector<std::string>::iterator it = aExtraOpts.begin(); |
michael@0 | 766 | it != aExtraOpts.end(); |
michael@0 | 767 | ++it) { |
michael@0 | 768 | cmdLine.AppendLooseValue(UTF8ToWide(*it)); |
michael@0 | 769 | } |
michael@0 | 770 | |
michael@0 | 771 | if (Omnijar::IsInitialized()) { |
michael@0 | 772 | // Make sure the child process can find the omnijar |
michael@0 | 773 | // See XRE_InitCommandLine in nsAppRunner.cpp |
michael@0 | 774 | nsAutoString path; |
michael@0 | 775 | nsCOMPtr<nsIFile> file = Omnijar::GetPath(Omnijar::GRE); |
michael@0 | 776 | if (file && NS_SUCCEEDED(file->GetPath(path))) { |
michael@0 | 777 | cmdLine.AppendLooseValue(UTF8ToWide("-greomni")); |
michael@0 | 778 | cmdLine.AppendLooseValue(path.get()); |
michael@0 | 779 | } |
michael@0 | 780 | file = Omnijar::GetPath(Omnijar::APP); |
michael@0 | 781 | if (file && NS_SUCCEEDED(file->GetPath(path))) { |
michael@0 | 782 | cmdLine.AppendLooseValue(UTF8ToWide("-appomni")); |
michael@0 | 783 | cmdLine.AppendLooseValue(path.get()); |
michael@0 | 784 | } |
michael@0 | 785 | } |
michael@0 | 786 | |
michael@0 | 787 | #if defined(XP_WIN) && defined(MOZ_CONTENT_SANDBOX) |
michael@0 | 788 | if (mSandboxEnabled) { |
michael@0 | 789 | // Tell the process that it should lower its rights after initialization. |
michael@0 | 790 | cmdLine.AppendLooseValue(UTF8ToWide("-sandbox")); |
michael@0 | 791 | } |
michael@0 | 792 | #endif |
michael@0 | 793 | |
michael@0 | 794 | // Add the application directory path (-appdir path) |
michael@0 | 795 | AddAppDirToCommandLine(cmdLine); |
michael@0 | 796 | |
michael@0 | 797 | // XXX Command line params past this point are expected to be at |
michael@0 | 798 | // the end of the command line string, and in a specific order. |
michael@0 | 799 | // See XRE_InitChildProcess in nsEmbedFunction. |
michael@0 | 800 | |
michael@0 | 801 | // Win app model id |
michael@0 | 802 | cmdLine.AppendLooseValue(mGroupId.get()); |
michael@0 | 803 | |
michael@0 | 804 | // Process id |
michael@0 | 805 | cmdLine.AppendLooseValue(UTF8ToWide(pidstring)); |
michael@0 | 806 | |
michael@0 | 807 | #if defined(MOZ_CRASHREPORTER) |
michael@0 | 808 | cmdLine.AppendLooseValue( |
michael@0 | 809 | UTF8ToWide(CrashReporter::GetChildNotificationPipe())); |
michael@0 | 810 | #endif |
michael@0 | 811 | |
michael@0 | 812 | // Process type |
michael@0 | 813 | cmdLine.AppendLooseValue(UTF8ToWide(childProcessType)); |
michael@0 | 814 | |
michael@0 | 815 | #if defined(XP_WIN) && defined(MOZ_CONTENT_SANDBOX) |
michael@0 | 816 | if (mSandboxEnabled) { |
michael@0 | 817 | |
michael@0 | 818 | mozilla::SandboxBroker sandboxBroker; |
michael@0 | 819 | sandboxBroker.LaunchApp(cmdLine.program().c_str(), |
michael@0 | 820 | cmdLine.command_line_string().c_str(), |
michael@0 | 821 | &process); |
michael@0 | 822 | } else |
michael@0 | 823 | #endif |
michael@0 | 824 | { |
michael@0 | 825 | base::LaunchApp(cmdLine, false, false, &process); |
michael@0 | 826 | } |
michael@0 | 827 | |
michael@0 | 828 | #else |
michael@0 | 829 | # error Sorry |
michael@0 | 830 | #endif |
michael@0 | 831 | |
michael@0 | 832 | if (!process) { |
michael@0 | 833 | MonitorAutoLock lock(mMonitor); |
michael@0 | 834 | mProcessState = PROCESS_ERROR; |
michael@0 | 835 | lock.Notify(); |
michael@0 | 836 | return false; |
michael@0 | 837 | } |
michael@0 | 838 | // NB: on OS X, we block much longer than we need to in order to |
michael@0 | 839 | // reach this call, waiting for the child process's task_t. The |
michael@0 | 840 | // best way to fix that is to refactor this file, hard. |
michael@0 | 841 | SetHandle(process); |
michael@0 | 842 | #if defined(MOZ_WIDGET_COCOA) |
michael@0 | 843 | mChildTask = child_task; |
michael@0 | 844 | #endif |
michael@0 | 845 | |
michael@0 | 846 | OpenPrivilegedHandle(base::GetProcId(process)); |
michael@0 | 847 | { |
michael@0 | 848 | MonitorAutoLock lock(mMonitor); |
michael@0 | 849 | mProcessState = PROCESS_CREATED; |
michael@0 | 850 | lock.Notify(); |
michael@0 | 851 | } |
michael@0 | 852 | |
michael@0 | 853 | return true; |
michael@0 | 854 | } |
michael@0 | 855 | |
michael@0 | 856 | void |
michael@0 | 857 | GeckoChildProcessHost::OpenPrivilegedHandle(base::ProcessId aPid) |
michael@0 | 858 | { |
michael@0 | 859 | if (mChildProcessHandle) { |
michael@0 | 860 | MOZ_ASSERT(aPid == base::GetProcId(mChildProcessHandle)); |
michael@0 | 861 | return; |
michael@0 | 862 | } |
michael@0 | 863 | if (!base::OpenPrivilegedProcessHandle(aPid, &mChildProcessHandle)) { |
michael@0 | 864 | NS_RUNTIMEABORT("can't open handle to child process"); |
michael@0 | 865 | } |
michael@0 | 866 | } |
michael@0 | 867 | |
michael@0 | 868 | void |
michael@0 | 869 | GeckoChildProcessHost::OnChannelConnected(int32_t peer_pid) |
michael@0 | 870 | { |
michael@0 | 871 | OpenPrivilegedHandle(peer_pid); |
michael@0 | 872 | { |
michael@0 | 873 | MonitorAutoLock lock(mMonitor); |
michael@0 | 874 | mProcessState = PROCESS_CONNECTED; |
michael@0 | 875 | lock.Notify(); |
michael@0 | 876 | } |
michael@0 | 877 | } |
michael@0 | 878 | |
michael@0 | 879 | void |
michael@0 | 880 | GeckoChildProcessHost::OnMessageReceived(const IPC::Message& aMsg) |
michael@0 | 881 | { |
michael@0 | 882 | // We never process messages ourself, just save them up for the next |
michael@0 | 883 | // listener. |
michael@0 | 884 | mQueue.push(aMsg); |
michael@0 | 885 | } |
michael@0 | 886 | |
michael@0 | 887 | void |
michael@0 | 888 | GeckoChildProcessHost::OnChannelError() |
michael@0 | 889 | { |
michael@0 | 890 | // Update the process state to an error state if we have a channel |
michael@0 | 891 | // error before we're connected. This fixes certain failures, |
michael@0 | 892 | // but does not address the full range of possible issues described |
michael@0 | 893 | // in the FIXME comment below. |
michael@0 | 894 | MonitorAutoLock lock(mMonitor); |
michael@0 | 895 | if (mProcessState < PROCESS_CONNECTED) { |
michael@0 | 896 | mProcessState = PROCESS_ERROR; |
michael@0 | 897 | lock.Notify(); |
michael@0 | 898 | } |
michael@0 | 899 | // FIXME/bug 773925: save up this error for the next listener. |
michael@0 | 900 | } |
michael@0 | 901 | |
michael@0 | 902 | void |
michael@0 | 903 | GeckoChildProcessHost::GetQueuedMessages(std::queue<IPC::Message>& queue) |
michael@0 | 904 | { |
michael@0 | 905 | // If this is called off the IO thread, bad things will happen. |
michael@0 | 906 | DCHECK(MessageLoopForIO::current()); |
michael@0 | 907 | swap(queue, mQueue); |
michael@0 | 908 | // We expect the next listener to take over processing of our queue. |
michael@0 | 909 | } |
michael@0 | 910 | |
michael@0 | 911 | void |
michael@0 | 912 | GeckoChildProcessHost::OnWaitableEventSignaled(base::WaitableEvent *event) |
michael@0 | 913 | { |
michael@0 | 914 | if (mDelegate) { |
michael@0 | 915 | mDelegate->OnWaitableEventSignaled(event); |
michael@0 | 916 | } |
michael@0 | 917 | ChildProcessHost::OnWaitableEventSignaled(event); |
michael@0 | 918 | } |
michael@0 | 919 | |
michael@0 | 920 | #ifdef MOZ_NUWA_PROCESS |
michael@0 | 921 | |
michael@0 | 922 | using mozilla::ipc::GeckoExistingProcessHost; |
michael@0 | 923 | using mozilla::ipc::FileDescriptor; |
michael@0 | 924 | |
michael@0 | 925 | GeckoExistingProcessHost:: |
michael@0 | 926 | GeckoExistingProcessHost(GeckoProcessType aProcessType, |
michael@0 | 927 | base::ProcessHandle aProcess, |
michael@0 | 928 | const FileDescriptor& aFileDescriptor, |
michael@0 | 929 | ChildPrivileges aPrivileges) |
michael@0 | 930 | : GeckoChildProcessHost(aProcessType, aPrivileges) |
michael@0 | 931 | , mExistingProcessHandle(aProcess) |
michael@0 | 932 | , mExistingFileDescriptor(aFileDescriptor) |
michael@0 | 933 | { |
michael@0 | 934 | NS_ASSERTION(aFileDescriptor.IsValid(), |
michael@0 | 935 | "Expected file descriptor to be valid"); |
michael@0 | 936 | } |
michael@0 | 937 | |
michael@0 | 938 | GeckoExistingProcessHost::~GeckoExistingProcessHost() |
michael@0 | 939 | { |
michael@0 | 940 | // Bug 943174: If we don't do this, ~GeckoChildProcessHost will try |
michael@0 | 941 | // to wait on a process that isn't a direct child, and bad things |
michael@0 | 942 | // will happen. |
michael@0 | 943 | SetAlreadyDead(); |
michael@0 | 944 | } |
michael@0 | 945 | |
michael@0 | 946 | bool |
michael@0 | 947 | GeckoExistingProcessHost::PerformAsyncLaunch(StringVector aExtraOpts, |
michael@0 | 948 | base::ProcessArchitecture aArch) |
michael@0 | 949 | { |
michael@0 | 950 | SetHandle(mExistingProcessHandle); |
michael@0 | 951 | |
michael@0 | 952 | OpenPrivilegedHandle(base::GetProcId(mExistingProcessHandle)); |
michael@0 | 953 | |
michael@0 | 954 | MonitorAutoLock lock(mMonitor); |
michael@0 | 955 | mProcessState = PROCESS_CREATED; |
michael@0 | 956 | lock.Notify(); |
michael@0 | 957 | |
michael@0 | 958 | return true; |
michael@0 | 959 | } |
michael@0 | 960 | |
michael@0 | 961 | void |
michael@0 | 962 | GeckoExistingProcessHost::InitializeChannel() |
michael@0 | 963 | { |
michael@0 | 964 | CreateChannel(mExistingFileDescriptor); |
michael@0 | 965 | |
michael@0 | 966 | MonitorAutoLock lock(mMonitor); |
michael@0 | 967 | mProcessState = CHANNEL_INITIALIZED; |
michael@0 | 968 | lock.Notify(); |
michael@0 | 969 | } |
michael@0 | 970 | |
michael@0 | 971 | #endif /* MOZ_NUWA_PROCESS */ |