#include <dirent.h>
#include <errno.h>
#include <fcntl.h>
#include <signal.h>
#include <stdlib.h>
#include <sys/resource.h>
#include <sys/time.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include <iterator>
#include <limits>
#include <set>
#include "base/allocator/type_profiler_control.h"
#include "base/command_line.h"
#include "base/compiler_specific.h"
#include "base/debug/debugger.h"
#include "base/debug/stack_trace.h"
#include "base/eintr_wrapper.h"
#include "base/file_util.h"
#include "base/files/dir_reader_posix.h"
#include "base/logging.h"
#include "base/memory/scoped_ptr.h"
#include "base/process_util.h"
#include "base/stringprintf.h"
#include "base/synchronization/waitable_event.h"
#include "base/third_party/dynamic_annotations/dynamic_annotations.h"
#include "base/threading/platform_thread.h"
#include "base/threading/thread_restrictions.h"
#if defined(OS_CHROMEOS)
#include <sys/ioctl.h>
#endif
#if defined(OS_FREEBSD)
#include <sys/event.h>
#include <sys/ucontext.h>
#endif
#if defined(OS_MACOSX)
#include <crt_externs.h>
#include <sys/event.h>
#else
extern char** environ;
#endif
namespace base {
namespace {
char** GetEnvironment() {
#if defined(OS_MACOSX)
return *_NSGetEnviron();
#else
return environ;
#endif
}
void SetEnvironment(char** env) {
#if defined(OS_MACOSX)
*_NSGetEnviron() = env;
#else
environ = env;
#endif
}
int WaitpidWithTimeout(ProcessHandle handle, int64 wait_milliseconds,
bool* success) {
int status = -1;
pid_t ret_pid = HANDLE_EINTR(waitpid(handle, &status, WNOHANG));
static const int64 kMaxSleepInMicroseconds = 1 << 18;
int64 max_sleep_time_usecs = 1 << 10;
int64 double_sleep_time = 0;
Time wakeup_time = Time::Now() +
TimeDelta::FromMilliseconds(wait_milliseconds);
while (ret_pid == 0) {
Time now = Time::Now();
if (now > wakeup_time)
break;
int64 sleep_time_usecs = (wakeup_time - now).InMicroseconds();
if (sleep_time_usecs > max_sleep_time_usecs)
sleep_time_usecs = max_sleep_time_usecs;
usleep(sleep_time_usecs);
ret_pid = HANDLE_EINTR(waitpid(handle, &status, WNOHANG));
if ((max_sleep_time_usecs < kMaxSleepInMicroseconds) &&
(double_sleep_time++ % 4 == 0)) {
max_sleep_time_usecs *= 2;
}
}
if (success)
*success = (ret_pid != -1);
return status;
}
#if !defined(OS_ANDROID)
void StackDumpSignalHandler(int signal, siginfo_t* info, ucontext_t* context) {
if (debug::BeingDebugged())
debug::BreakDebugger();
DLOG(ERROR) << "Received signal " << signal;
debug::StackTrace().PrintBacktrace();
#if defined(OS_MACOSX)
#if ARCH_CPU_32_BITS
char buf[1024];
size_t len;
len = static_cast<size_t>(
snprintf(buf, sizeof(buf),
"ax: %x, bx: %x, cx: %x, dx: %x\n",
context->uc_mcontext->__ss.__eax,
context->uc_mcontext->__ss.__ebx,
context->uc_mcontext->__ss.__ecx,
context->uc_mcontext->__ss.__edx));
write(STDERR_FILENO, buf, std::min(len, sizeof(buf) - 1));
len = static_cast<size_t>(
snprintf(buf, sizeof(buf),
"di: %x, si: %x, bp: %x, sp: %x, ss: %x, flags: %x\n",
context->uc_mcontext->__ss.__edi,
context->uc_mcontext->__ss.__esi,
context->uc_mcontext->__ss.__ebp,
context->uc_mcontext->__ss.__esp,
context->uc_mcontext->__ss.__ss,
context->uc_mcontext->__ss.__eflags));
write(STDERR_FILENO, buf, std::min(len, sizeof(buf) - 1));
len = static_cast<size_t>(
snprintf(buf, sizeof(buf),
"ip: %x, cs: %x, ds: %x, es: %x, fs: %x, gs: %x\n",
context->uc_mcontext->__ss.__eip,
context->uc_mcontext->__ss.__cs,
context->uc_mcontext->__ss.__ds,
context->uc_mcontext->__ss.__es,
context->uc_mcontext->__ss.__fs,
context->uc_mcontext->__ss.__gs));
write(STDERR_FILENO, buf, std::min(len, sizeof(buf) - 1));
#endif
#endif
_exit(1);
}
#endif
void ResetChildSignalHandlersToDefaults() {
signal(SIGHUP, SIG_DFL);
signal(SIGINT, SIG_DFL);
signal(SIGILL, SIG_DFL);
signal(SIGABRT, SIG_DFL);
signal(SIGFPE, SIG_DFL);
signal(SIGBUS, SIG_DFL);
signal(SIGSEGV, SIG_DFL);
signal(SIGSYS, SIG_DFL);
signal(SIGTERM, SIG_DFL);
}
}
ProcessId GetCurrentProcId() {
return getpid();
}
ProcessHandle GetCurrentProcessHandle() {
return GetCurrentProcId();
}
bool OpenProcessHandle(ProcessId pid, ProcessHandle* handle) {
*handle = pid;
return true;
}
bool OpenPrivilegedProcessHandle(ProcessId pid, ProcessHandle* handle) {
return OpenProcessHandle(pid, handle);
}
bool OpenProcessHandleWithAccess(ProcessId pid,
uint32 access_flags,
ProcessHandle* handle) {
return OpenProcessHandle(pid, handle);
}
void CloseProcessHandle(ProcessHandle process) {
return;
}
ProcessId GetProcId(ProcessHandle process) {
return process;
}
bool KillProcess(ProcessHandle process_id, int exit_code, bool wait) {
DCHECK_GT(process_id, 1) << " tried to kill invalid process_id";
if (process_id <= 1)
return false;
bool result = kill(process_id, SIGTERM) == 0;
if (result && wait) {
int tries = 60;
if (RunningOnValgrind()) {
tries *= 2;
}
unsigned sleep_ms = 4;
bool exited = false;
while (tries-- > 0) {
pid_t pid = HANDLE_EINTR(waitpid(process_id, NULL, WNOHANG));
if (pid == process_id) {
exited = true;
break;
}
if (pid == -1) {
if (errno == ECHILD) {
exited = true;
break;
}
DPLOG(ERROR) << "Error waiting for process " << process_id;
}
usleep(sleep_ms * 1000);
const unsigned kMaxSleepMs = 1000;
if (sleep_ms < kMaxSleepMs)
sleep_ms *= 2;
}
if (!exited)
result = kill(process_id, SIGKILL) == 0;
}
if (!result)
DPLOG(ERROR) << "Unable to terminate process " << process_id;
return result;
}
bool KillProcessGroup(ProcessHandle process_group_id) {
bool result = kill(-1 * process_group_id, SIGKILL) == 0;
if (!result)
DPLOG(ERROR) << "Unable to terminate process group " << process_group_id;
return result;
}
class ScopedDIRClose {
public:
inline void operator()(DIR* x) const {
if (x) {
closedir(x);
}
}
};
typedef scoped_ptr_malloc<DIR, ScopedDIRClose> ScopedDIR;
#if defined(OS_LINUX)
static const rlim_t kSystemDefaultMaxFds = 8192;
static const char kFDDir[] = "/proc/self/fd";
#elif defined(OS_MACOSX)
static const rlim_t kSystemDefaultMaxFds = 256;
static const char kFDDir[] = "/dev/fd";
#elif defined(OS_SOLARIS)
static const rlim_t kSystemDefaultMaxFds = 8192;
static const char kFDDir[] = "/dev/fd";
#elif defined(OS_FREEBSD)
static const rlim_t kSystemDefaultMaxFds = 8192;
static const char kFDDir[] = "/dev/fd";
#elif defined(OS_OPENBSD)
static const rlim_t kSystemDefaultMaxFds = 256;
static const char kFDDir[] = "/dev/fd";
#elif defined(OS_ANDROID)
static const rlim_t kSystemDefaultMaxFds = 1024;
static const char kFDDir[] = "/proc/self/fd";
#endif
void CloseSuperfluousFds(const base::InjectiveMultimap& saved_mapping) {
struct rlimit nofile;
rlim_t max_fds;
if (getrlimit(RLIMIT_NOFILE, &nofile)) {
max_fds = kSystemDefaultMaxFds;
RAW_LOG(ERROR, "getrlimit(RLIMIT_NOFILE) failed");
} else {
max_fds = nofile.rlim_cur;
}
if (max_fds > INT_MAX)
max_fds = INT_MAX;
DirReaderPosix fd_dir(kFDDir);
if (!fd_dir.IsValid()) {
for (rlim_t i = 0; i < max_fds; ++i) {
const int fd = static_cast<int>(i);
if (fd == STDIN_FILENO || fd == STDOUT_FILENO || fd == STDERR_FILENO)
continue;
InjectiveMultimap::const_iterator j;
for (j = saved_mapping.begin(); j != saved_mapping.end(); j++) {
if (fd == j->dest)
break;
}
if (j != saved_mapping.end())
continue;
ignore_result(HANDLE_EINTR(close(fd)));
}
return;
}
const int dir_fd = fd_dir.fd();
for ( ; fd_dir.Next(); ) {
if (fd_dir.name()[0] == '.')
continue;
char *endptr;
errno = 0;
const long int fd = strtol(fd_dir.name(), &endptr, 10);
if (fd_dir.name()[0] == 0 || *endptr || fd < 0 || errno)
continue;
if (fd == STDIN_FILENO || fd == STDOUT_FILENO || fd == STDERR_FILENO)
continue;
InjectiveMultimap::const_iterator i;
for (i = saved_mapping.begin(); i != saved_mapping.end(); i++) {
if (fd == i->dest)
break;
}
if (i != saved_mapping.end())
continue;
if (fd == dir_fd)
continue;
if (fd < static_cast<int>(max_fds)) {
int ret = HANDLE_EINTR(close(fd));
DPCHECK(ret == 0);
}
}
}
char** AlterEnvironment(const EnvironmentVector& changes,
const char* const* const env) {
unsigned count = 0;
unsigned size = 0;
for (unsigned i = 0; env[i]; i++) {
const char *const pair = env[i];
count++;
size += strlen(pair) + 1 ;
}
for (EnvironmentVector::const_iterator j = changes.begin();
j != changes.end();
++j) {
bool found = false;
const char *pair;
for (unsigned i = 0; env[i]; i++) {
pair = env[i];
const char *const equals = strchr(pair, '=');
if (!equals)
continue;
const unsigned keylen = equals - pair;
if (keylen == j->first.size() &&
memcmp(pair, j->first.data(), keylen) == 0) {
found = true;
break;
}
}
if (found) {
count--;
size -= strlen(pair) + 1;
if (j->second.size())
found = false;
}
if (!found && !j->second.empty()) {
count++;
size += j->first.size() + 1 + j->second.size() + 1 ;
}
}
count++;
uint8_t *buffer = new uint8_t[sizeof(char*) * count + size];
char **const ret = reinterpret_cast<char**>(buffer);
unsigned k = 0;
char *scratch = reinterpret_cast<char*>(buffer + sizeof(char*) * count);
for (unsigned i = 0; env[i]; i++) {
const char *const pair = env[i];
const char *const equals = strchr(pair, '=');
if (!equals) {
const unsigned len = strlen(pair);
ret[k++] = scratch;
memcpy(scratch, pair, len + 1);
scratch += len + 1;
continue;
}
const unsigned keylen = equals - pair;
bool handled = false;
for (EnvironmentVector::const_iterator
j = changes.begin(); j != changes.end(); j++) {
if (j->first.size() == keylen &&
memcmp(j->first.data(), pair, keylen) == 0) {
if (!j->second.empty()) {
ret[k++] = scratch;
memcpy(scratch, pair, keylen + 1);
scratch += keylen + 1;
memcpy(scratch, j->second.c_str(), j->second.size() + 1);
scratch += j->second.size() + 1;
}
handled = true;
break;
}
}
if (!handled) {
const unsigned len = strlen(pair);
ret[k++] = scratch;
memcpy(scratch, pair, len + 1);
scratch += len + 1;
}
}
for (EnvironmentVector::const_iterator
j = changes.begin(); j != changes.end(); j++) {
if (j->second.empty())
continue;
bool found = false;
for (unsigned i = 0; env[i]; i++) {
const char *const pair = env[i];
const char *const equals = strchr(pair, '=');
if (!equals)
continue;
const unsigned keylen = equals - pair;
if (keylen == j->first.size() &&
memcmp(pair, j->first.data(), keylen) == 0) {
found = true;
break;
}
}
if (!found) {
ret[k++] = scratch;
memcpy(scratch, j->first.data(), j->first.size());
scratch += j->first.size();
*scratch++ = '=';
memcpy(scratch, j->second.c_str(), j->second.size() + 1);
scratch += j->second.size() + 1;
}
}
ret[k] = NULL;
return ret;
}
bool LaunchProcess(const std::vector<std::string>& argv,
const LaunchOptions& options,
ProcessHandle* process_handle) {
size_t fd_shuffle_size = 0;
if (options.fds_to_remap) {
fd_shuffle_size = options.fds_to_remap->size();
}
#if defined(OS_MACOSX)
if (options.synchronize) {
++fd_shuffle_size;
}
#endif
InjectiveMultimap fd_shuffle1;
InjectiveMultimap fd_shuffle2;
fd_shuffle1.reserve(fd_shuffle_size);
fd_shuffle2.reserve(fd_shuffle_size);
scoped_array<char*> argv_cstr(new char*[argv.size() + 1]);
scoped_array<char*> new_environ;
if (options.environ)
new_environ.reset(AlterEnvironment(*options.environ, GetEnvironment()));
#if defined(OS_MACOSX)
int synchronization_pipe_fds[2];
file_util::ScopedFD synchronization_read_fd;
file_util::ScopedFD synchronization_write_fd;
if (options.synchronize) {
DCHECK(!options.wait);
if (HANDLE_EINTR(pipe(synchronization_pipe_fds)) != 0) {
DPLOG(ERROR) << "pipe";
return false;
}
synchronization_read_fd.reset(&synchronization_pipe_fds[0]);
synchronization_write_fd.reset(&synchronization_pipe_fds[1]);
}
#endif
pid_t pid;
#if defined(OS_LINUX)
if (options.clone_flags) {
pid = syscall(__NR_clone, options.clone_flags, 0, 0, 0);
} else
#endif
{
pid = fork();
}
if (pid < 0) {
DPLOG(ERROR) << "fork";
return false;
} else if (pid == 0) {
int null_fd = HANDLE_EINTR(open("/dev/null", O_RDONLY));
if (null_fd < 0) {
RAW_LOG(ERROR, "Failed to open /dev/null");
_exit(127);
}
file_util::ScopedFD null_fd_closer(&null_fd);
int new_fd = HANDLE_EINTR(dup2(null_fd, STDIN_FILENO));
if (new_fd != STDIN_FILENO) {
RAW_LOG(ERROR, "Failed to dup /dev/null for stdin");
_exit(127);
}
if (options.new_process_group) {
if (setpgid(0, 0) < 0) {
RAW_LOG(ERROR, "setpgid failed");
_exit(127);
}
}
base::type_profiler::Controller::Stop();
if (options.maximize_rlimits) {
std::set<int>::const_iterator resource;
for (resource = options.maximize_rlimits->begin();
resource != options.maximize_rlimits->end();
++resource) {
struct rlimit limit;
if (getrlimit(*resource, &limit) < 0) {
RAW_LOG(WARNING, "getrlimit failed");
} else if (limit.rlim_cur < limit.rlim_max) {
limit.rlim_cur = limit.rlim_max;
if (setrlimit(*resource, &limit) < 0) {
RAW_LOG(WARNING, "setrlimit failed");
}
}
}
}
#if defined(OS_MACOSX)
RestoreDefaultExceptionHandler();
#endif
ResetChildSignalHandlersToDefaults();
#if defined(OS_MACOSX)
if (options.synchronize) {
synchronization_write_fd.reset();
}
#endif
#if 0
void *malloc_thunk =
reinterpret_cast<void*>(reinterpret_cast<intptr_t>(malloc) & ~4095);
mprotect(malloc_thunk, 4096, PROT_READ | PROT_WRITE | PROT_EXEC);
memset(reinterpret_cast<void*>(malloc), 0xff, 8);
#endif
#if defined(OS_CHROMEOS)
if (options.ctrl_terminal_fd >= 0) {
if (HANDLE_EINTR(setsid()) != -1) {
if (HANDLE_EINTR(
ioctl(options.ctrl_terminal_fd, TIOCSCTTY, NULL)) == -1) {
RAW_LOG(WARNING, "ioctl(TIOCSCTTY), ctrl terminal not set");
}
} else {
RAW_LOG(WARNING, "setsid failed, ctrl terminal not set");
}
}
#endif
if (options.fds_to_remap) {
for (FileHandleMappingVector::const_iterator
it = options.fds_to_remap->begin();
it != options.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 defined(OS_MACOSX)
if (options.synchronize) {
int keep_fd = *synchronization_read_fd.get();
fd_shuffle1.push_back(InjectionArc(keep_fd, keep_fd, false));
fd_shuffle2.push_back(InjectionArc(keep_fd, keep_fd, false));
}
#endif
if (options.environ)
SetEnvironment(new_environ.get());
if (!ShuffleFileDescriptors(&fd_shuffle1))
_exit(127);
CloseSuperfluousFds(fd_shuffle2);
#if defined(OS_MACOSX)
if (options.synchronize) {
char read_char;
int read_result =
HANDLE_EINTR(read(*synchronization_read_fd.get(), &read_char, 1));
if (read_result != 1) {
RAW_LOG(ERROR, "LaunchProcess: synchronization read: error");
_exit(127);
}
synchronization_read_fd.reset();
}
#endif
for (size_t i = 0; i < argv.size(); i++)
argv_cstr[i] = const_cast<char*>(argv[i].c_str());
argv_cstr[argv.size()] = NULL;
execvp(argv_cstr[0], argv_cstr.get());
RAW_LOG(ERROR, "LaunchProcess: failed to execvp:");
RAW_LOG(ERROR, argv_cstr[0]);
_exit(127);
} else {
if (options.wait) {
base::ThreadRestrictions::AssertIOAllowed();
pid_t ret = HANDLE_EINTR(waitpid(pid, 0, 0));
DPCHECK(ret > 0);
}
if (process_handle)
*process_handle = pid;
#if defined(OS_MACOSX)
if (options.synchronize) {
synchronization_read_fd.reset();
*options.synchronize = new int(*synchronization_write_fd.release());
}
#endif
}
return true;
}
bool LaunchProcess(const CommandLine& cmdline,
const LaunchOptions& options,
ProcessHandle* process_handle) {
return LaunchProcess(cmdline.argv(), options, process_handle);
}
#if defined(OS_MACOSX)
void LaunchSynchronize(LaunchSynchronizationHandle handle) {
int synchronization_fd = *handle;
file_util::ScopedFD synchronization_fd_closer(&synchronization_fd);
delete handle;
if (HANDLE_EINTR(write(synchronization_fd, "", 1)) != 1) {
DPLOG(ERROR) << "write";
}
}
#endif
ProcessMetrics::~ProcessMetrics() { }
bool EnableInProcessStackDumping() {
struct sigaction action;
memset(&action, 0, sizeof(action));
action.sa_handler = SIG_IGN;
sigemptyset(&action.sa_mask);
bool success = (sigaction(SIGPIPE, &action, NULL) == 0);
#if !defined(OS_ANDROID)
sig_t handler = reinterpret_cast<sig_t>(&StackDumpSignalHandler);
success &= (signal(SIGILL, handler) != SIG_ERR);
success &= (signal(SIGABRT, handler) != SIG_ERR);
success &= (signal(SIGFPE, handler) != SIG_ERR);
success &= (signal(SIGBUS, handler) != SIG_ERR);
success &= (signal(SIGSEGV, handler) != SIG_ERR);
success &= (signal(SIGSYS, handler) != SIG_ERR);
#endif
return success;
}
void RaiseProcessToHighPriority() {
}
TerminationStatus GetTerminationStatus(ProcessHandle handle, int* exit_code) {
int status = 0;
const pid_t result = HANDLE_EINTR(waitpid(handle, &status, WNOHANG));
if (result == -1) {
DPLOG(ERROR) << "waitpid(" << handle << ")";
if (exit_code)
*exit_code = 0;
return TERMINATION_STATUS_NORMAL_TERMINATION;
} else if (result == 0) {
if (exit_code)
*exit_code = 0;
return TERMINATION_STATUS_STILL_RUNNING;
}
if (exit_code)
*exit_code = status;
if (WIFSIGNALED(status)) {
switch (WTERMSIG(status)) {
case SIGABRT:
case SIGBUS:
case SIGFPE:
case SIGILL:
case SIGSEGV:
return TERMINATION_STATUS_PROCESS_CRASHED;
case SIGINT:
case SIGKILL:
case SIGTERM:
return TERMINATION_STATUS_PROCESS_WAS_KILLED;
default:
break;
}
}
if (WIFEXITED(status) && WEXITSTATUS(status) != 0)
return TERMINATION_STATUS_ABNORMAL_TERMINATION;
return TERMINATION_STATUS_NORMAL_TERMINATION;
}
bool WaitForExitCode(ProcessHandle handle, int* exit_code) {
int status;
if (HANDLE_EINTR(waitpid(handle, &status, 0)) == -1) {
NOTREACHED();
return false;
}
if (WIFEXITED(status)) {
*exit_code = WEXITSTATUS(status);
return true;
}
DCHECK(WIFSIGNALED(status));
return false;
}
bool WaitForExitCodeWithTimeout(ProcessHandle handle, int* exit_code,
base::TimeDelta timeout) {
bool waitpid_success = false;
int status = WaitpidWithTimeout(handle, timeout.InMilliseconds(),
&waitpid_success);
if (status == -1)
return false;
if (!waitpid_success)
return false;
if (WIFSIGNALED(status)) {
*exit_code = -1;
return true;
}
if (WIFEXITED(status)) {
*exit_code = WEXITSTATUS(status);
return true;
}
return false;
}
#if defined(OS_MACOSX)
static bool WaitForSingleNonChildProcess(ProcessHandle handle,
base::TimeDelta wait) {
DCHECK_GT(handle, 0);
DCHECK(wait.InMilliseconds() == base::kNoTimeout || wait > base::TimeDelta());
int kq = kqueue();
if (kq == -1) {
DPLOG(ERROR) << "kqueue";
return false;
}
file_util::ScopedFD kq_closer(&kq);
struct kevent change = {0};
EV_SET(&change, handle, EVFILT_PROC, EV_ADD, NOTE_EXIT, 0, NULL);
int result = HANDLE_EINTR(kevent(kq, &change, 1, NULL, 0, NULL));
if (result == -1) {
if (errno == ESRCH) {
return true;
}
DPLOG(ERROR) << "kevent (setup " << handle << ")";
return false;
}
bool wait_forever = wait.InMilliseconds() == base::kNoTimeout;
base::TimeDelta remaining_delta;
base::Time deadline;
if (!wait_forever) {
remaining_delta = wait;
deadline = base::Time::Now() + remaining_delta;
}
result = -1;
struct kevent event = {0};
while (wait_forever || remaining_delta > base::TimeDelta()) {
struct timespec remaining_timespec;
struct timespec* remaining_timespec_ptr;
if (wait_forever) {
remaining_timespec_ptr = NULL;
} else {
remaining_timespec = remaining_delta.ToTimeSpec();
remaining_timespec_ptr = &remaining_timespec;
}
result = kevent(kq, NULL, 0, &event, 1, remaining_timespec_ptr);
if (result == -1 && errno == EINTR) {
if (!wait_forever) {
remaining_delta = deadline - base::Time::Now();
}
result = 0;
} else {
break;
}
}
if (result < 0) {
DPLOG(ERROR) << "kevent (wait " << handle << ")";
return false;
} else if (result > 1) {
DLOG(ERROR) << "kevent (wait " << handle << "): unexpected result "
<< result;
return false;
} else if (result == 0) {
return false;
}
DCHECK_EQ(result, 1);
if (event.filter != EVFILT_PROC ||
(event.fflags & NOTE_EXIT) == 0 ||
event.ident != static_cast<uintptr_t>(handle)) {
DLOG(ERROR) << "kevent (wait " << handle
<< "): unexpected event: filter=" << event.filter
<< ", fflags=" << event.fflags
<< ", ident=" << event.ident;
return false;
}
return true;
}
#endif
bool WaitForSingleProcess(ProcessHandle handle, base::TimeDelta wait) {
ProcessHandle parent_pid = GetParentProcessId(handle);
ProcessHandle our_pid = Process::Current().handle();
if (parent_pid != our_pid) {
#if defined(OS_MACOSX)
return WaitForSingleNonChildProcess(handle, wait);
#else
NOTIMPLEMENTED();
#endif
}
bool waitpid_success;
int status = -1;
if (wait.InMilliseconds() == base::kNoTimeout) {
waitpid_success = (HANDLE_EINTR(waitpid(handle, &status, 0)) != -1);
} else {
status = WaitpidWithTimeout(
handle, wait.InMilliseconds(), &waitpid_success);
}
if (status != -1) {
DCHECK(waitpid_success);
return WIFEXITED(status);
} else {
return false;
}
}
int64 TimeValToMicroseconds(const struct timeval& tv) {
static const int kMicrosecondsPerSecond = 1000000;
int64 ret = tv.tv_sec;
ret *= kMicrosecondsPerSecond;
ret += tv.tv_usec;
return ret;
}
enum GetAppOutputInternalResult {
EXECUTE_FAILURE,
EXECUTE_SUCCESS,
GOT_MAX_OUTPUT,
};
static GetAppOutputInternalResult GetAppOutputInternal(
const std::vector<std::string>& argv,
char* const envp[],
std::string* output,
size_t max_output,
bool do_search_path,
int* exit_code) {
base::ThreadRestrictions::AssertIOAllowed();
DCHECK(exit_code);
*exit_code = EXIT_FAILURE;
int pipe_fd[2];
pid_t pid;
InjectiveMultimap fd_shuffle1, fd_shuffle2;
scoped_array<char*> argv_cstr(new char*[argv.size() + 1]);
fd_shuffle1.reserve(3);
fd_shuffle2.reserve(3);
DCHECK(!do_search_path ^ !envp);
if (pipe(pipe_fd) < 0)
return EXECUTE_FAILURE;
switch (pid = fork()) {
case -1:
close(pipe_fd[0]);
close(pipe_fd[1]);
return EXECUTE_FAILURE;
case 0:
{
#if defined(OS_MACOSX)
RestoreDefaultExceptionHandler();
#endif
int dev_null = open("/dev/null", O_WRONLY);
if (dev_null < 0)
_exit(127);
base::type_profiler::Controller::Stop();
fd_shuffle1.push_back(InjectionArc(pipe_fd[1], STDOUT_FILENO, true));
fd_shuffle1.push_back(InjectionArc(dev_null, STDERR_FILENO, true));
fd_shuffle1.push_back(InjectionArc(dev_null, STDIN_FILENO, true));
std::copy(fd_shuffle1.begin(), fd_shuffle1.end(),
std::back_inserter(fd_shuffle2));
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;
if (do_search_path)
execvp(argv_cstr[0], argv_cstr.get());
else
execve(argv_cstr[0], argv_cstr.get(), envp);
_exit(127);
}
default:
{
close(pipe_fd[1]);
output->clear();
char buffer[256];
size_t output_buf_left = max_output;
ssize_t bytes_read = 1;
while (output_buf_left > 0) {
bytes_read = HANDLE_EINTR(read(pipe_fd[0], buffer,
std::min(output_buf_left, sizeof(buffer))));
if (bytes_read <= 0)
break;
output->append(buffer, bytes_read);
output_buf_left -= static_cast<size_t>(bytes_read);
}
close(pipe_fd[0]);
bool success = WaitForExitCode(pid, exit_code);
if (!output_buf_left && bytes_read > 0)
return GOT_MAX_OUTPUT;
else if (success)
return EXECUTE_SUCCESS;
return EXECUTE_FAILURE;
}
}
}
bool GetAppOutput(const CommandLine& cl, std::string* output) {
return GetAppOutput(cl.argv(), output);
}
bool GetAppOutput(const std::vector<std::string>& argv, std::string* output) {
int exit_code;
GetAppOutputInternalResult result = GetAppOutputInternal(
argv, NULL, output, std::numeric_limits<std::size_t>::max(), true,
&exit_code);
return result == EXECUTE_SUCCESS && exit_code == EXIT_SUCCESS;
}
bool GetAppOutputRestricted(const CommandLine& cl,
std::string* output, size_t max_output) {
char* const empty_environ = NULL;
int exit_code;
GetAppOutputInternalResult result = GetAppOutputInternal(
cl.argv(), &empty_environ, output, max_output, false, &exit_code);
return result == GOT_MAX_OUTPUT || (result == EXECUTE_SUCCESS &&
exit_code == EXIT_SUCCESS);
}
bool GetAppOutputWithExitCode(const CommandLine& cl,
std::string* output,
int* exit_code) {
GetAppOutputInternalResult result = GetAppOutputInternal(
cl.argv(), NULL, output, std::numeric_limits<std::size_t>::max(), true,
exit_code);
return result == EXECUTE_SUCCESS;
}
bool WaitForProcessesToExit(const FilePath::StringType& executable_name,
base::TimeDelta wait,
const ProcessFilter* filter) {
bool result = false;
base::Time end_time = base::Time::Now() + wait;
do {
NamedProcessIterator iter(executable_name, filter);
if (!iter.NextProcessEntry()) {
result = true;
break;
}
base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds(100));
} while ((end_time - base::Time::Now()) > base::TimeDelta());
return result;
}
bool CleanupProcesses(const FilePath::StringType& executable_name,
base::TimeDelta wait,
int exit_code,
const ProcessFilter* filter) {
bool exited_cleanly = WaitForProcessesToExit(executable_name, wait, filter);
if (!exited_cleanly)
KillProcesses(executable_name, exit_code, filter);
return exited_cleanly;
}
#if !defined(OS_MACOSX)
namespace {
static bool IsChildDead(pid_t child) {
const pid_t result = HANDLE_EINTR(waitpid(child, NULL, WNOHANG));
if (result == -1) {
DPLOG(ERROR) << "waitpid(" << child << ")";
NOTREACHED();
} else if (result > 0) {
return true;
}
return false;
}
class BackgroundReaper : public PlatformThread::Delegate {
public:
BackgroundReaper(pid_t child, unsigned timeout)
: child_(child),
timeout_(timeout) {
}
virtual void ThreadMain() OVERRIDE {
WaitForChildToDie();
delete this;
}
void WaitForChildToDie() {
if (timeout_ == 0) {
pid_t r = HANDLE_EINTR(waitpid(child_, NULL, 0));
if (r != child_) {
DPLOG(ERROR) << "While waiting for " << child_
<< " to terminate, we got the following result: " << r;
}
return;
}
for (unsigned i = 0; i < 2 * timeout_; ++i) {
PlatformThread::Sleep(TimeDelta::FromMilliseconds(500));
if (IsChildDead(child_))
return;
}
if (kill(child_, SIGKILL) == 0) {
if (HANDLE_EINTR(waitpid(child_, NULL, 0)) < 0)
DPLOG(WARNING) << "waitpid";
} else {
DLOG(ERROR) << "While waiting for " << child_ << " to terminate we"
<< " failed to deliver a SIGKILL signal (" << errno << ").";
}
}
private:
const pid_t child_;
const unsigned timeout_;
DISALLOW_COPY_AND_ASSIGN(BackgroundReaper);
};
}
void EnsureProcessTerminated(ProcessHandle process) {
if (IsChildDead(process))
return;
const unsigned timeout = 2;
BackgroundReaper* reaper = new BackgroundReaper(process, timeout);
PlatformThread::CreateNonJoinable(0, reaper);
}
void EnsureProcessGetsReaped(ProcessHandle process) {
if (IsChildDead(process))
return;
BackgroundReaper* reaper = new BackgroundReaper(process, 0);
PlatformThread::CreateNonJoinable(0, reaper);
}
#endif
}