mirror of
https://github.com/mozilla/gecko-dev.git
synced 2024-11-24 05:11:16 +00:00
Bug 1231123 - Simplify LaunchApp on BSDs by dropping fork/exec version. r=jld
--HG-- extra : transplant_source : 2%08%3De%92%DE%D7%A9%AF%F7%A1%13yU%A2Y%3DiAn
This commit is contained in:
parent
b9ba40cc6b
commit
fa002c319a
@ -2,70 +2,22 @@
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
// derived from process_util_linux.cc and process_util_mac.cc
|
||||
// derived from process_util_mac.cc
|
||||
|
||||
#include "base/process_util.h"
|
||||
|
||||
#include <sys/wait.h>
|
||||
|
||||
#include <fcntl.h>
|
||||
#include <unistd.h>
|
||||
#include <spawn.h>
|
||||
#include <sys/wait.h>
|
||||
|
||||
#include <string>
|
||||
|
||||
#include "base/eintr_wrapper.h"
|
||||
#include "base/file_util.h"
|
||||
#include "base/logging.h"
|
||||
#include "base/string_tokenizer.h"
|
||||
#include "base/string_util.h"
|
||||
#include "mozilla/UniquePtr.h"
|
||||
|
||||
#if defined(_POSIX_SPAWN) && _POSIX_SPAWN > 0
|
||||
#define HAVE_POSIX_SPAWN 1
|
||||
#endif
|
||||
|
||||
/*
|
||||
* On platforms that are not gonk based, we fall back to an arbitrary
|
||||
* UID. This is generally the UID for user `nobody', albeit it is not
|
||||
* always the case.
|
||||
*/
|
||||
|
||||
#if defined(OS_NETBSD) || defined(OS_OPENBSD)
|
||||
# define CHILD_UNPRIVILEGED_UID 32767
|
||||
# define CHILD_UNPRIVILEGED_GID 32767
|
||||
#else
|
||||
# define CHILD_UNPRIVILEGED_UID 65534
|
||||
# define CHILD_UNPRIVILEGED_GID 65534
|
||||
#endif
|
||||
|
||||
#ifndef __dso_public
|
||||
# ifdef __exported
|
||||
# define __dso_public __exported
|
||||
# else
|
||||
# define __dso_public __attribute__((__visibility__("default")))
|
||||
# endif
|
||||
#endif
|
||||
|
||||
#ifdef HAVE_POSIX_SPAWN
|
||||
#include <spawn.h>
|
||||
extern "C" char **environ __dso_public;
|
||||
#endif
|
||||
|
||||
namespace {
|
||||
|
||||
enum ParsingState {
|
||||
KEY_NAME,
|
||||
KEY_VALUE
|
||||
};
|
||||
|
||||
static mozilla::EnvironmentLog gProcessLog("MOZ_PROCESS_LOG");
|
||||
|
||||
} // namespace
|
||||
extern "C" char **environ __attribute__((__visibility__("default")));
|
||||
|
||||
namespace base {
|
||||
|
||||
#ifdef HAVE_POSIX_SPAWN
|
||||
|
||||
void FreeEnvVarsArray(char* array[], int length)
|
||||
{
|
||||
for (int i = 0; i < length; i++) {
|
||||
@ -201,107 +153,4 @@ void SetCurrentProcessPrivileges(ChildPrivileges privs) {
|
||||
|
||||
}
|
||||
|
||||
#else // no posix_spawn, use fork/exec
|
||||
|
||||
bool LaunchApp(const std::vector<std::string>& argv,
|
||||
const file_handle_mapping_vector& fds_to_remap,
|
||||
bool wait, ProcessHandle* process_handle) {
|
||||
return LaunchApp(argv, fds_to_remap, environment_map(),
|
||||
wait, process_handle);
|
||||
}
|
||||
|
||||
bool LaunchApp(const std::vector<std::string>& argv,
|
||||
const file_handle_mapping_vector& fds_to_remap,
|
||||
const environment_map& env_vars_to_set,
|
||||
bool wait, ProcessHandle* process_handle,
|
||||
ProcessArchitecture arch) {
|
||||
return LaunchApp(argv, fds_to_remap, env_vars_to_set,
|
||||
PRIVILEGES_INHERIT,
|
||||
wait, process_handle);
|
||||
}
|
||||
|
||||
bool LaunchApp(const std::vector<std::string>& argv,
|
||||
const file_handle_mapping_vector& fds_to_remap,
|
||||
const environment_map& env_vars_to_set,
|
||||
ChildPrivileges privs,
|
||||
bool wait, ProcessHandle* process_handle,
|
||||
ProcessArchitecture arch) {
|
||||
mozilla::UniquePtr<char*[]> argv_cstr(new char*[argv.size() + 1]);
|
||||
// Illegal to allocate memory after fork and before execvp
|
||||
InjectiveMultimap fd_shuffle1, fd_shuffle2;
|
||||
fd_shuffle1.reserve(fds_to_remap.size());
|
||||
fd_shuffle2.reserve(fds_to_remap.size());
|
||||
|
||||
pid_t pid = fork();
|
||||
if (pid < 0)
|
||||
return false;
|
||||
|
||||
if (pid == 0) {
|
||||
for (file_handle_mapping_vector::const_iterator
|
||||
it = fds_to_remap.begin(); it != fds_to_remap.end(); ++it) {
|
||||
fd_shuffle1.push_back(InjectionArc(it->first, it->second, false));
|
||||
fd_shuffle2.push_back(InjectionArc(it->first, it->second, false));
|
||||
}
|
||||
|
||||
if (!ShuffleFileDescriptors(&fd_shuffle1))
|
||||
_exit(127);
|
||||
|
||||
CloseSuperfluousFds(fd_shuffle2);
|
||||
|
||||
for (size_t i = 0; i < argv.size(); i++)
|
||||
argv_cstr[i] = const_cast<char*>(argv[i].c_str());
|
||||
argv_cstr[argv.size()] = NULL;
|
||||
|
||||
SetCurrentProcessPrivileges(privs);
|
||||
|
||||
for (environment_map::const_iterator it = env_vars_to_set.begin();
|
||||
it != env_vars_to_set.end(); ++it) {
|
||||
if (setenv(it->first.c_str(), it->second.c_str(), 1/*overwrite*/))
|
||||
_exit(127);
|
||||
}
|
||||
execv(argv_cstr[0], argv_cstr.get());
|
||||
// if we get here, we're in serious trouble and should complain loudly
|
||||
DLOG(ERROR) << "FAILED TO exec() CHILD PROCESS, path: " << argv_cstr[0];
|
||||
_exit(127);
|
||||
} else {
|
||||
gProcessLog.print("==> process %d launched child process %d\n",
|
||||
GetCurrentProcId(), pid);
|
||||
if (wait)
|
||||
HANDLE_EINTR(waitpid(pid, 0, 0));
|
||||
|
||||
if (process_handle)
|
||||
*process_handle = pid;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool LaunchApp(const CommandLine& cl,
|
||||
bool wait, bool start_hidden,
|
||||
ProcessHandle* process_handle) {
|
||||
file_handle_mapping_vector no_files;
|
||||
return LaunchApp(cl.argv(), no_files, wait, process_handle);
|
||||
}
|
||||
|
||||
void SetCurrentProcessPrivileges(ChildPrivileges privs) {
|
||||
if (privs == PRIVILEGES_INHERIT) {
|
||||
return;
|
||||
}
|
||||
|
||||
gid_t gid = CHILD_UNPRIVILEGED_GID;
|
||||
uid_t uid = CHILD_UNPRIVILEGED_UID;
|
||||
if (setgid(gid) != 0) {
|
||||
DLOG(ERROR) << "FAILED TO setgid() CHILD PROCESS";
|
||||
_exit(127);
|
||||
}
|
||||
if (setuid(uid) != 0) {
|
||||
DLOG(ERROR) << "FAILED TO setuid() CHILD PROCESS";
|
||||
_exit(127);
|
||||
}
|
||||
if (chdir("/") != 0)
|
||||
gProcessLog.print("==> could not chdir()\n");
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
} // namespace base
|
||||
|
Loading…
Reference in New Issue
Block a user