Skip to content

Commit b39b9e9

Browse files
committed
Bug 1406971 - Change nsProcess to use LaunchApp from IPC, instead of NSPR, on Unix. r=froydnj
This also fixes the Unix part of bug 678369, and fixes bug 147659 as a convenient side-effect of using LaunchApp (which has the desired fd-closing behavior already) rather than directly using fork/exec. The main goal is to work around bug 227246, where PR_CreateProcess on Unix interferes with anything else that tries to use child processes. This does not fix that bug -- NSPR will still cause problems if used in that way -- but it's an adequate workaround for that bug in Gecko in almost all cases (the one known exception is nsAuthSambaNTLM, which uses NSPR directly and needs to be fixed separately). Waiting for the child process uses waitpid() directly, sharing the existing code used for OS X. MozReview-Commit-ID: 6zfZ1Zgk2i9 --HG-- extra : rebase_source : e8230503d82943af4563d8d63baa029d8a698683
1 parent 54555dd commit b39b9e9

File tree

2 files changed

+25
-6
lines changed

2 files changed

+25
-6
lines changed

xpcom/threads/nsProcess.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
#include "nsIWeakReferenceUtils.h"
2121
#include "nsIObserver.h"
2222
#include "nsString.h"
23-
#ifndef XP_MACOSX
23+
#ifndef XP_UNIX
2424
#include "prproces.h"
2525
#endif
2626
#if defined(PROCESSMODEL_WINAPI)
@@ -76,7 +76,7 @@ class nsProcess final
7676
int32_t mExitValue;
7777
#if defined(PROCESSMODEL_WINAPI)
7878
HANDLE mProcess;
79-
#elif !defined(XP_MACOSX)
79+
#elif !defined(XP_UNIX)
8080
PRProcess* mProcess;
8181
#endif
8282
};

xpcom/threads/nsProcessCommon.cpp

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,11 @@
3838
#ifdef XP_MACOSX
3939
#include <crt_externs.h>
4040
#include <spawn.h>
41+
#endif
42+
#ifdef XP_UNIX
43+
#ifndef XP_MACOSX
44+
#include "base/process_util.h"
45+
#endif
4146
#include <sys/wait.h>
4247
#include <sys/errno.h>
4348
#endif
@@ -78,7 +83,7 @@ nsProcess::nsProcess()
7883
, mObserver(nullptr)
7984
, mWeakObserver(nullptr)
8085
, mExitValue(-1)
81-
#if !defined(XP_MACOSX)
86+
#if !defined(XP_UNIX)
8287
, mProcess(nullptr)
8388
#endif
8489
{
@@ -272,7 +277,7 @@ nsProcess::Monitor(void* aArg)
272277
}
273278
}
274279
#else
275-
#ifdef XP_MACOSX
280+
#ifdef XP_UNIX
276281
int exitCode = -1;
277282
int status = 0;
278283
pid_t result;
@@ -296,7 +301,7 @@ nsProcess::Monitor(void* aArg)
296301
// Lock in case Kill or GetExitCode are called during this
297302
{
298303
MutexAutoLock lock(process->mLock);
299-
#if !defined(XP_MACOSX)
304+
#if !defined(XP_UNIX)
300305
process->mProcess = nullptr;
301306
#endif
302307
process->mExitValue = exitCode;
@@ -570,6 +575,20 @@ nsProcess::RunProcess(bool aBlocking, char** aMyArgv, nsIObserver* aObserver,
570575
if (result != 0) {
571576
return NS_ERROR_FAILURE;
572577
}
578+
#elif defined(XP_UNIX)
579+
base::file_handle_mapping_vector fdMap;
580+
std::vector<std::string> argvVec;
581+
for (char** arg = aMyArgv; *arg != nullptr; ++arg) {
582+
argvVec.push_back(*arg);
583+
}
584+
pid_t newPid;
585+
if (base::LaunchApp(argvVec, fdMap, false, &newPid)) {
586+
static_assert(sizeof(pid_t) <= sizeof(int32_t),
587+
"mPid is large enough to hold a pid");
588+
mPid = static_cast<int32_t>(newPid);
589+
} else {
590+
return NS_ERROR_FAILURE;
591+
}
573592
#else
574593
mProcess = PR_CreateProcess(aMyArgv[0], aMyArgv, nullptr, nullptr);
575594
if (!mProcess) {
@@ -676,7 +695,7 @@ nsProcess::Kill()
676695
if (TerminateProcess(mProcess, 0) == 0) {
677696
return NS_ERROR_FAILURE;
678697
}
679-
#elif defined(XP_MACOSX)
698+
#elif defined(XP_UNIX)
680699
if (kill(mPid, SIGKILL) != 0) {
681700
return NS_ERROR_FAILURE;
682701
}

0 commit comments

Comments
 (0)