2010-11-29 19:44:50 +00:00
|
|
|
//===- llvm/Support/Unix/Program.cpp -----------------------------*- C++ -*-===//
|
2009-07-17 20:38:17 +00:00
|
|
|
//
|
2004-08-29 19:20:41 +00:00
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
2007-12-29 20:36:04 +00:00
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
2009-07-17 20:38:17 +00:00
|
|
|
//
|
2004-08-29 19:20:41 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file implements the Unix specific portion of the Program class.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//=== WARNING: Implementation here must contain only generic UNIX code that
|
|
|
|
//=== is guaranteed to work on *all* UNIX variants.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "Unix.h"
|
2013-02-04 07:03:24 +00:00
|
|
|
#include "llvm/Support/Compiler.h"
|
2012-12-03 16:50:05 +00:00
|
|
|
#include "llvm/Support/FileSystem.h"
|
|
|
|
#include <llvm/Config/config.h>
|
2004-12-27 06:16:52 +00:00
|
|
|
#if HAVE_SYS_STAT_H
|
2004-08-29 19:20:41 +00:00
|
|
|
#include <sys/stat.h>
|
2004-12-27 06:16:52 +00:00
|
|
|
#endif
|
2007-02-16 19:11:07 +00:00
|
|
|
#if HAVE_SYS_RESOURCE_H
|
|
|
|
#include <sys/resource.h>
|
|
|
|
#endif
|
2004-12-27 06:16:52 +00:00
|
|
|
#if HAVE_SIGNAL_H
|
2004-12-19 18:58:11 +00:00
|
|
|
#include <signal.h>
|
2004-12-27 06:16:52 +00:00
|
|
|
#endif
|
|
|
|
#if HAVE_FCNTL_H
|
2004-08-29 19:20:41 +00:00
|
|
|
#include <fcntl.h>
|
|
|
|
#endif
|
2013-04-11 14:06:34 +00:00
|
|
|
#if HAVE_UNISTD_H
|
|
|
|
#include <unistd.h>
|
|
|
|
#endif
|
2010-04-18 04:14:37 +00:00
|
|
|
#ifdef HAVE_POSIX_SPAWN
|
2013-10-08 16:12:58 +00:00
|
|
|
#ifdef __sun__
|
|
|
|
#define _RESTRICT_KYWD
|
|
|
|
#endif
|
2010-04-18 04:14:37 +00:00
|
|
|
#include <spawn.h>
|
2010-04-18 07:07:48 +00:00
|
|
|
#if !defined(__APPLE__)
|
|
|
|
extern char **environ;
|
2010-04-18 09:16:04 +00:00
|
|
|
#else
|
|
|
|
#include <crt_externs.h> // _NSGetEnviron
|
2010-04-18 07:07:48 +00:00
|
|
|
#endif
|
2010-04-18 04:14:37 +00:00
|
|
|
#endif
|
2004-08-29 19:20:41 +00:00
|
|
|
|
|
|
|
namespace llvm {
|
2014-06-12 21:46:39 +00:00
|
|
|
|
2004-08-29 19:20:41 +00:00
|
|
|
using namespace sys;
|
|
|
|
|
2013-10-01 14:28:18 +00:00
|
|
|
ProcessInfo::ProcessInfo() : Pid(0), ReturnCode(0) {}
|
|
|
|
|
2004-08-29 19:20:41 +00:00
|
|
|
// This function just uses the PATH environment variable to find the program.
|
2013-06-13 19:25:37 +00:00
|
|
|
std::string
|
2013-06-12 20:58:35 +00:00
|
|
|
sys::FindProgramByName(const std::string& progName) {
|
2004-08-29 19:20:41 +00:00
|
|
|
|
|
|
|
// Check some degenerate cases
|
|
|
|
if (progName.length() == 0) // no program
|
2013-06-13 19:25:37 +00:00
|
|
|
return "";
|
2013-06-14 19:38:45 +00:00
|
|
|
std::string temp = progName;
|
2009-07-28 22:08:15 +00:00
|
|
|
// Use the given path verbatim if it contains any slashes; this matches
|
|
|
|
// the behavior of sh(1) and friends.
|
2010-10-28 20:34:33 +00:00
|
|
|
if (progName.find('/') != std::string::npos)
|
2013-06-14 19:38:45 +00:00
|
|
|
return temp;
|
2004-08-29 19:20:41 +00:00
|
|
|
|
2010-11-02 20:32:39 +00:00
|
|
|
// At this point, the file name is valid and does not contain slashes. Search
|
|
|
|
// for it through the directories specified in the PATH environment variable.
|
2009-07-17 20:38:17 +00:00
|
|
|
|
2004-08-29 19:20:41 +00:00
|
|
|
// Get the path. If its empty, we can't do anything to find it.
|
|
|
|
const char *PathStr = getenv("PATH");
|
2014-04-28 04:05:08 +00:00
|
|
|
if (!PathStr)
|
2013-06-13 19:25:37 +00:00
|
|
|
return "";
|
2004-08-29 19:20:41 +00:00
|
|
|
|
|
|
|
// Now we have a colon separated list of directories to search; try them.
|
2008-05-05 18:30:58 +00:00
|
|
|
size_t PathLen = strlen(PathStr);
|
2004-08-29 19:20:41 +00:00
|
|
|
while (PathLen) {
|
|
|
|
// Find the first colon...
|
|
|
|
const char *Colon = std::find(PathStr, PathStr+PathLen, ':');
|
|
|
|
|
|
|
|
// Check to see if this first directory contains the executable...
|
2013-06-14 19:38:45 +00:00
|
|
|
SmallString<128> FilePath(PathStr,Colon);
|
|
|
|
sys::path::append(FilePath, progName);
|
|
|
|
if (sys::fs::can_execute(Twine(FilePath)))
|
|
|
|
return FilePath.str(); // Found the executable!
|
2004-08-29 19:20:41 +00:00
|
|
|
|
|
|
|
// Nope it wasn't in this directory, check the next path in the list!
|
|
|
|
PathLen -= Colon-PathStr;
|
|
|
|
PathStr = Colon;
|
|
|
|
|
|
|
|
// Advance past duplicate colons
|
|
|
|
while (*PathStr == ':') {
|
|
|
|
PathStr++;
|
|
|
|
PathLen--;
|
|
|
|
}
|
|
|
|
}
|
2013-06-13 19:25:37 +00:00
|
|
|
return "";
|
2004-08-29 19:20:41 +00:00
|
|
|
}
|
|
|
|
|
2013-06-14 19:38:45 +00:00
|
|
|
static bool RedirectIO(const StringRef *Path, int FD, std::string* ErrMsg) {
|
2014-04-28 04:05:08 +00:00
|
|
|
if (!Path) // Noop
|
2008-06-12 10:47:18 +00:00
|
|
|
return false;
|
2013-06-14 19:38:45 +00:00
|
|
|
std::string File;
|
|
|
|
if (Path->empty())
|
2008-06-12 10:47:18 +00:00
|
|
|
// Redirect empty paths to /dev/null
|
|
|
|
File = "/dev/null";
|
|
|
|
else
|
2013-06-14 19:38:45 +00:00
|
|
|
File = *Path;
|
2004-12-19 18:00:44 +00:00
|
|
|
|
|
|
|
// Open the file
|
2013-06-14 19:38:45 +00:00
|
|
|
int InFD = open(File.c_str(), FD == 0 ? O_RDONLY : O_WRONLY|O_CREAT, 0666);
|
2004-12-19 18:00:44 +00:00
|
|
|
if (InFD == -1) {
|
2013-06-14 19:38:45 +00:00
|
|
|
MakeErrMsg(ErrMsg, "Cannot open file '" + File + "' for "
|
2009-04-20 20:50:13 +00:00
|
|
|
+ (FD == 0 ? "input" : "output"));
|
2006-08-21 06:02:44 +00:00
|
|
|
return true;
|
2004-12-19 18:00:44 +00:00
|
|
|
}
|
|
|
|
|
2006-08-21 06:02:44 +00:00
|
|
|
// Install it as the requested FD
|
2010-03-14 23:16:45 +00:00
|
|
|
if (dup2(InFD, FD) == -1) {
|
2006-08-21 06:02:44 +00:00
|
|
|
MakeErrMsg(ErrMsg, "Cannot dup2");
|
2010-03-14 23:16:45 +00:00
|
|
|
close(InFD);
|
2006-08-21 06:02:44 +00:00
|
|
|
return true;
|
|
|
|
}
|
2004-12-19 18:00:44 +00:00
|
|
|
close(InFD); // Close the original FD
|
2006-08-21 06:02:44 +00:00
|
|
|
return false;
|
2004-12-19 18:00:44 +00:00
|
|
|
}
|
|
|
|
|
2010-04-18 04:14:37 +00:00
|
|
|
#ifdef HAVE_POSIX_SPAWN
|
2013-07-26 16:21:31 +00:00
|
|
|
static bool RedirectIO_PS(const std::string *Path, int FD, std::string *ErrMsg,
|
2011-03-20 15:52:24 +00:00
|
|
|
posix_spawn_file_actions_t *FileActions) {
|
2014-04-28 04:05:08 +00:00
|
|
|
if (!Path) // Noop
|
2010-04-18 04:14:37 +00:00
|
|
|
return false;
|
2013-07-26 16:21:31 +00:00
|
|
|
const char *File;
|
2013-06-14 19:38:45 +00:00
|
|
|
if (Path->empty())
|
2010-04-18 04:14:37 +00:00
|
|
|
// Redirect empty paths to /dev/null
|
|
|
|
File = "/dev/null";
|
|
|
|
else
|
2013-07-26 16:21:31 +00:00
|
|
|
File = Path->c_str();
|
2010-04-18 09:19:41 +00:00
|
|
|
|
2013-06-14 19:38:45 +00:00
|
|
|
if (int Err = posix_spawn_file_actions_addopen(
|
2013-07-26 16:21:31 +00:00
|
|
|
FileActions, FD, File,
|
2013-06-14 19:38:45 +00:00
|
|
|
FD == 0 ? O_RDONLY : O_WRONLY | O_CREAT, 0666))
|
2010-04-18 04:14:37 +00:00
|
|
|
return MakeErrMsg(ErrMsg, "Cannot dup2", Err);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2009-11-08 20:55:48 +00:00
|
|
|
static void TimeOutHandler(int Sig) {
|
|
|
|
}
|
|
|
|
|
2007-02-16 19:11:07 +00:00
|
|
|
static void SetMemoryLimits (unsigned size)
|
|
|
|
{
|
2010-02-12 00:37:46 +00:00
|
|
|
#if HAVE_SYS_RESOURCE_H && HAVE_GETRLIMIT && HAVE_SETRLIMIT
|
2007-02-16 19:11:07 +00:00
|
|
|
struct rlimit r;
|
|
|
|
__typeof__ (r.rlim_cur) limit = (__typeof__ (r.rlim_cur)) (size) * 1048576;
|
|
|
|
|
|
|
|
// Heap size
|
|
|
|
getrlimit (RLIMIT_DATA, &r);
|
|
|
|
r.rlim_cur = limit;
|
|
|
|
setrlimit (RLIMIT_DATA, &r);
|
2007-07-06 10:31:27 +00:00
|
|
|
#ifdef RLIMIT_RSS
|
2007-02-16 19:11:07 +00:00
|
|
|
// Resident set size.
|
|
|
|
getrlimit (RLIMIT_RSS, &r);
|
|
|
|
r.rlim_cur = limit;
|
|
|
|
setrlimit (RLIMIT_RSS, &r);
|
2007-04-23 07:22:51 +00:00
|
|
|
#endif
|
2007-06-04 15:28:57 +00:00
|
|
|
#ifdef RLIMIT_AS // e.g. NetBSD doesn't have it.
|
2013-02-04 07:03:24 +00:00
|
|
|
// Don't set virtual memory limit if built with any Sanitizer. They need 80Tb
|
|
|
|
// of virtual memory for shadow memory mapping.
|
|
|
|
#if !LLVM_MEMORY_SANITIZER_BUILD && !LLVM_ADDRESS_SANITIZER_BUILD
|
2007-02-16 19:11:07 +00:00
|
|
|
// Virtual memory.
|
|
|
|
getrlimit (RLIMIT_AS, &r);
|
|
|
|
r.rlim_cur = limit;
|
|
|
|
setrlimit (RLIMIT_AS, &r);
|
|
|
|
#endif
|
2007-06-04 15:28:57 +00:00
|
|
|
#endif
|
2013-02-04 07:03:24 +00:00
|
|
|
#endif
|
2007-02-16 19:11:07 +00:00
|
|
|
}
|
|
|
|
|
2013-06-12 20:58:35 +00:00
|
|
|
}
|
|
|
|
|
2013-10-01 14:28:18 +00:00
|
|
|
static bool Execute(ProcessInfo &PI, StringRef Program, const char **args,
|
2013-06-14 19:38:45 +00:00
|
|
|
const char **envp, const StringRef **redirects,
|
2013-06-12 20:58:35 +00:00
|
|
|
unsigned memoryLimit, std::string *ErrMsg) {
|
2013-10-01 14:28:18 +00:00
|
|
|
if (!llvm::sys::fs::exists(Program)) {
|
|
|
|
if (ErrMsg)
|
|
|
|
*ErrMsg = std::string("Executable \"") + Program.str() +
|
|
|
|
std::string("\" doesn't exist!");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2010-04-18 04:14:37 +00:00
|
|
|
// If this OS has posix_spawn and there is no memory limit being implied, use
|
|
|
|
// posix_spawn. It is more efficient than fork/exec.
|
|
|
|
#ifdef HAVE_POSIX_SPAWN
|
|
|
|
if (memoryLimit == 0) {
|
2011-03-20 15:52:24 +00:00
|
|
|
posix_spawn_file_actions_t FileActionsStore;
|
2014-04-28 04:05:08 +00:00
|
|
|
posix_spawn_file_actions_t *FileActions = nullptr;
|
2010-04-18 04:14:37 +00:00
|
|
|
|
2013-07-26 16:21:31 +00:00
|
|
|
// If we call posix_spawn_file_actions_addopen we have to make sure the
|
2013-07-26 20:44:45 +00:00
|
|
|
// c strings we pass to it stay alive until the call to posix_spawn,
|
2013-07-26 16:21:31 +00:00
|
|
|
// so we copy any StringRefs into this variable.
|
|
|
|
std::string RedirectsStorage[3];
|
|
|
|
|
2010-04-18 04:14:37 +00:00
|
|
|
if (redirects) {
|
2014-04-28 04:05:08 +00:00
|
|
|
std::string *RedirectsStr[3] = {nullptr, nullptr, nullptr};
|
2013-07-26 16:21:31 +00:00
|
|
|
for (int I = 0; I < 3; ++I) {
|
|
|
|
if (redirects[I]) {
|
|
|
|
RedirectsStorage[I] = *redirects[I];
|
|
|
|
RedirectsStr[I] = &RedirectsStorage[I];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-03-20 15:52:24 +00:00
|
|
|
FileActions = &FileActionsStore;
|
|
|
|
posix_spawn_file_actions_init(FileActions);
|
|
|
|
|
2010-04-18 04:14:37 +00:00
|
|
|
// Redirect stdin/stdout.
|
2013-07-26 16:21:31 +00:00
|
|
|
if (RedirectIO_PS(RedirectsStr[0], 0, ErrMsg, FileActions) ||
|
|
|
|
RedirectIO_PS(RedirectsStr[1], 1, ErrMsg, FileActions))
|
2010-04-18 04:14:37 +00:00
|
|
|
return false;
|
2014-04-28 04:05:08 +00:00
|
|
|
if (redirects[1] == nullptr || redirects[2] == nullptr ||
|
2010-04-18 04:14:37 +00:00
|
|
|
*redirects[1] != *redirects[2]) {
|
|
|
|
// Just redirect stderr
|
2013-07-26 16:21:31 +00:00
|
|
|
if (RedirectIO_PS(RedirectsStr[2], 2, ErrMsg, FileActions))
|
|
|
|
return false;
|
2010-10-28 19:32:53 +00:00
|
|
|
} else {
|
2010-04-18 04:14:37 +00:00
|
|
|
// If stdout and stderr should go to the same place, redirect stderr
|
|
|
|
// to the FD already open for stdout.
|
2011-03-20 15:52:24 +00:00
|
|
|
if (int Err = posix_spawn_file_actions_adddup2(FileActions, 1, 2))
|
2010-04-18 04:14:37 +00:00
|
|
|
return !MakeErrMsg(ErrMsg, "Can't redirect stderr to stdout", Err);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-04-18 09:16:04 +00:00
|
|
|
if (!envp)
|
2010-04-18 07:07:48 +00:00
|
|
|
#if !defined(__APPLE__)
|
2010-04-19 15:55:10 +00:00
|
|
|
envp = const_cast<const char **>(environ);
|
2010-04-18 09:16:04 +00:00
|
|
|
#else
|
2010-04-19 15:55:10 +00:00
|
|
|
// environ is missing in dylibs.
|
|
|
|
envp = const_cast<const char **>(*_NSGetEnviron());
|
2010-04-18 07:07:48 +00:00
|
|
|
#endif
|
|
|
|
|
2010-09-30 23:56:49 +00:00
|
|
|
// Explicitly initialized to prevent what appears to be a valgrind false
|
|
|
|
// positive.
|
|
|
|
pid_t PID = 0;
|
2014-04-28 04:05:08 +00:00
|
|
|
int Err = posix_spawn(&PID, Program.str().c_str(), FileActions,
|
|
|
|
/*attrp*/nullptr, const_cast<char **>(args),
|
|
|
|
const_cast<char **>(envp));
|
2010-10-28 19:32:53 +00:00
|
|
|
|
2011-03-20 15:52:24 +00:00
|
|
|
if (FileActions)
|
|
|
|
posix_spawn_file_actions_destroy(FileActions);
|
2010-04-18 04:14:37 +00:00
|
|
|
|
|
|
|
if (Err)
|
|
|
|
return !MakeErrMsg(ErrMsg, "posix_spawn failed", Err);
|
2010-10-28 19:32:53 +00:00
|
|
|
|
2013-10-01 14:28:18 +00:00
|
|
|
PI.Pid = PID;
|
|
|
|
|
2010-04-18 04:14:37 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
#endif
|
2010-10-28 19:32:53 +00:00
|
|
|
|
2004-08-29 19:20:41 +00:00
|
|
|
// Create a child process.
|
2004-12-19 18:00:44 +00:00
|
|
|
int child = fork();
|
|
|
|
switch (child) {
|
2011-04-15 05:18:47 +00:00
|
|
|
// An error occurred: Return to the caller.
|
2004-08-29 19:20:41 +00:00
|
|
|
case -1:
|
2006-08-21 06:02:44 +00:00
|
|
|
MakeErrMsg(ErrMsg, "Couldn't fork");
|
2009-07-18 21:43:12 +00:00
|
|
|
return false;
|
2004-08-29 19:20:41 +00:00
|
|
|
|
|
|
|
// Child process: Execute the program.
|
2004-12-14 04:18:51 +00:00
|
|
|
case 0: {
|
2004-12-19 18:00:44 +00:00
|
|
|
// Redirect file descriptors...
|
|
|
|
if (redirects) {
|
2008-06-12 12:53:35 +00:00
|
|
|
// Redirect stdin
|
2009-07-18 21:43:12 +00:00
|
|
|
if (RedirectIO(redirects[0], 0, ErrMsg)) { return false; }
|
2008-06-12 12:53:35 +00:00
|
|
|
// Redirect stdout
|
2009-07-18 21:43:12 +00:00
|
|
|
if (RedirectIO(redirects[1], 1, ErrMsg)) { return false; }
|
2009-07-17 20:38:17 +00:00
|
|
|
if (redirects[1] && redirects[2] &&
|
2008-06-12 12:53:35 +00:00
|
|
|
*(redirects[1]) == *(redirects[2])) {
|
|
|
|
// If stdout and stderr should go to the same place, redirect stderr
|
|
|
|
// to the FD already open for stdout.
|
|
|
|
if (-1 == dup2(1,2)) {
|
|
|
|
MakeErrMsg(ErrMsg, "Can't redirect stderr to stdout");
|
2009-07-18 21:43:12 +00:00
|
|
|
return false;
|
2008-06-12 12:53:35 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Just redirect stderr
|
2009-07-18 21:43:12 +00:00
|
|
|
if (RedirectIO(redirects[2], 2, ErrMsg)) { return false; }
|
2004-12-19 18:00:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-02-16 19:11:07 +00:00
|
|
|
// Set memory limits
|
|
|
|
if (memoryLimit!=0) {
|
|
|
|
SetMemoryLimits(memoryLimit);
|
|
|
|
}
|
2009-07-17 20:38:17 +00:00
|
|
|
|
2004-12-19 18:00:44 +00:00
|
|
|
// Execute!
|
2013-06-14 19:38:45 +00:00
|
|
|
std::string PathStr = Program;
|
2014-04-28 04:05:08 +00:00
|
|
|
if (envp != nullptr)
|
2013-06-14 19:38:45 +00:00
|
|
|
execve(PathStr.c_str(),
|
2010-04-19 15:55:10 +00:00
|
|
|
const_cast<char **>(args),
|
|
|
|
const_cast<char **>(envp));
|
2006-06-09 20:43:11 +00:00
|
|
|
else
|
2013-06-14 19:38:45 +00:00
|
|
|
execv(PathStr.c_str(),
|
2010-04-19 15:55:10 +00:00
|
|
|
const_cast<char **>(args));
|
2009-08-05 00:09:12 +00:00
|
|
|
// If the execve() failed, we should exit. Follow Unix protocol and
|
|
|
|
// return 127 if the executable was not found, and 126 otherwise.
|
|
|
|
// Use _exit rather than exit so that atexit functions and static
|
|
|
|
// object destructors cloned from the parent process aren't
|
|
|
|
// redundantly run, and so that any data buffered in stdio buffers
|
|
|
|
// cloned from the parent aren't redundantly written out.
|
|
|
|
_exit(errno == ENOENT ? 127 : 126);
|
2004-12-14 04:18:51 +00:00
|
|
|
}
|
2004-08-29 19:20:41 +00:00
|
|
|
|
|
|
|
// Parent process: Break out of the switch to do our processing.
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2013-10-01 14:28:18 +00:00
|
|
|
PI.Pid = child;
|
2009-07-18 21:43:12 +00:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-10-01 14:28:18 +00:00
|
|
|
namespace llvm {
|
|
|
|
|
|
|
|
ProcessInfo sys::Wait(const ProcessInfo &PI, unsigned SecondsToWait,
|
|
|
|
bool WaitUntilTerminates, std::string *ErrMsg) {
|
2009-07-18 21:43:12 +00:00
|
|
|
#ifdef HAVE_SYS_WAIT_H
|
2004-12-19 18:00:44 +00:00
|
|
|
struct sigaction Act, Old;
|
2013-10-01 14:28:18 +00:00
|
|
|
assert(PI.Pid && "invalid pid to wait on, process not started?");
|
|
|
|
|
|
|
|
int WaitPidOptions = 0;
|
|
|
|
pid_t ChildPid = PI.Pid;
|
|
|
|
if (WaitUntilTerminates) {
|
|
|
|
SecondsToWait = 0;
|
|
|
|
ChildPid = -1; // mimic a wait() using waitpid()
|
|
|
|
} else if (SecondsToWait) {
|
|
|
|
// Install a timeout handler. The handler itself does nothing, but the
|
|
|
|
// simple fact of having a handler at all causes the wait below to return
|
|
|
|
// with EINTR, unlike if we used SIG_IGN.
|
2010-07-14 14:32:33 +00:00
|
|
|
memset(&Act, 0, sizeof(Act));
|
2009-11-08 20:55:48 +00:00
|
|
|
Act.sa_handler = TimeOutHandler;
|
2004-12-19 18:00:44 +00:00
|
|
|
sigemptyset(&Act.sa_mask);
|
|
|
|
sigaction(SIGALRM, &Act, &Old);
|
2013-10-01 14:28:18 +00:00
|
|
|
alarm(SecondsToWait);
|
|
|
|
} else if (SecondsToWait == 0)
|
|
|
|
WaitPidOptions = WNOHANG;
|
2004-12-19 18:00:44 +00:00
|
|
|
|
2004-08-29 19:20:41 +00:00
|
|
|
// Parent process: Wait for the child process to terminate.
|
|
|
|
int status;
|
2013-10-01 14:28:18 +00:00
|
|
|
ProcessInfo WaitResult;
|
2014-06-27 18:02:54 +00:00
|
|
|
|
|
|
|
do {
|
|
|
|
WaitResult.Pid = waitpid(ChildPid, &status, WaitPidOptions);
|
|
|
|
} while (WaitUntilTerminates && WaitResult.Pid == -1 && errno == EINTR);
|
|
|
|
|
2013-10-01 14:28:18 +00:00
|
|
|
if (WaitResult.Pid != PI.Pid) {
|
|
|
|
if (WaitResult.Pid == 0) {
|
|
|
|
// Non-blocking wait.
|
|
|
|
return WaitResult;
|
|
|
|
} else {
|
|
|
|
if (SecondsToWait && errno == EINTR) {
|
|
|
|
// Kill the child.
|
|
|
|
kill(PI.Pid, SIGKILL);
|
|
|
|
|
|
|
|
// Turn off the alarm and restore the signal handler
|
|
|
|
alarm(0);
|
2014-04-28 04:05:08 +00:00
|
|
|
sigaction(SIGALRM, &Old, nullptr);
|
2013-10-01 14:28:18 +00:00
|
|
|
|
|
|
|
// Wait for child to die
|
|
|
|
if (wait(&status) != ChildPid)
|
|
|
|
MakeErrMsg(ErrMsg, "Child timed out but wouldn't die");
|
|
|
|
else
|
|
|
|
MakeErrMsg(ErrMsg, "Child timed out", 0);
|
|
|
|
|
|
|
|
WaitResult.ReturnCode = -2; // Timeout detected
|
|
|
|
return WaitResult;
|
|
|
|
} else if (errno != EINTR) {
|
|
|
|
MakeErrMsg(ErrMsg, "Error waiting for child process");
|
|
|
|
WaitResult.ReturnCode = -1;
|
|
|
|
return WaitResult;
|
|
|
|
}
|
2004-12-19 18:00:44 +00:00
|
|
|
}
|
2013-10-01 14:28:18 +00:00
|
|
|
}
|
2004-12-19 18:00:44 +00:00
|
|
|
|
|
|
|
// We exited normally without timeout, so turn off the timer.
|
2013-10-01 14:28:18 +00:00
|
|
|
if (SecondsToWait && !WaitUntilTerminates) {
|
2004-12-19 18:00:44 +00:00
|
|
|
alarm(0);
|
2014-04-28 04:05:08 +00:00
|
|
|
sigaction(SIGALRM, &Old, nullptr);
|
2004-12-19 18:00:44 +00:00
|
|
|
}
|
2004-08-29 19:20:41 +00:00
|
|
|
|
2010-10-29 16:39:01 +00:00
|
|
|
// Return the proper exit status. Detect error conditions
|
|
|
|
// so we can return -1 for them and set ErrMsg informatively.
|
2005-12-22 20:00:16 +00:00
|
|
|
int result = 0;
|
2010-10-29 16:39:01 +00:00
|
|
|
if (WIFEXITED(status)) {
|
2005-12-22 20:00:16 +00:00
|
|
|
result = WEXITSTATUS(status);
|
2013-10-01 14:28:18 +00:00
|
|
|
WaitResult.ReturnCode = result;
|
|
|
|
|
2010-10-29 16:39:01 +00:00
|
|
|
if (result == 127) {
|
2010-10-29 17:20:42 +00:00
|
|
|
if (ErrMsg)
|
|
|
|
*ErrMsg = llvm::sys::StrError(ENOENT);
|
2013-10-01 14:28:18 +00:00
|
|
|
WaitResult.ReturnCode = -1;
|
|
|
|
return WaitResult;
|
2010-10-29 16:39:01 +00:00
|
|
|
}
|
|
|
|
if (result == 126) {
|
2010-10-29 17:20:42 +00:00
|
|
|
if (ErrMsg)
|
|
|
|
*ErrMsg = "Program could not be executed";
|
2013-10-01 14:28:18 +00:00
|
|
|
WaitResult.ReturnCode = -1;
|
|
|
|
return WaitResult;
|
2010-10-29 16:39:01 +00:00
|
|
|
}
|
|
|
|
} else if (WIFSIGNALED(status)) {
|
2010-10-29 17:20:42 +00:00
|
|
|
if (ErrMsg) {
|
2011-05-21 00:56:46 +00:00
|
|
|
*ErrMsg = strsignal(WTERMSIG(status));
|
2005-12-22 20:00:16 +00:00
|
|
|
#ifdef WCOREDUMP
|
2010-10-29 17:20:42 +00:00
|
|
|
if (WCOREDUMP(status))
|
|
|
|
*ErrMsg += " (core dumped)";
|
2005-12-22 20:00:16 +00:00
|
|
|
#endif
|
2010-10-29 17:20:42 +00:00
|
|
|
}
|
2011-05-21 00:56:46 +00:00
|
|
|
// Return a special value to indicate that the process received an unhandled
|
|
|
|
// signal during execution as opposed to failing to execute.
|
2013-10-01 14:28:18 +00:00
|
|
|
WaitResult.ReturnCode = -2;
|
2010-10-29 16:39:01 +00:00
|
|
|
}
|
2004-08-29 19:20:41 +00:00
|
|
|
#else
|
2010-10-29 17:20:42 +00:00
|
|
|
if (ErrMsg)
|
|
|
|
*ErrMsg = "Program::Wait is not implemented on this platform yet!";
|
2014-01-27 22:53:07 +00:00
|
|
|
ProcessInfo WaitResult;
|
2013-10-01 14:28:18 +00:00
|
|
|
WaitResult.ReturnCode = -2;
|
2004-08-29 19:20:41 +00:00
|
|
|
#endif
|
2013-10-01 14:28:18 +00:00
|
|
|
return WaitResult;
|
2004-08-29 19:20:41 +00:00
|
|
|
}
|
|
|
|
|
2014-06-13 02:24:39 +00:00
|
|
|
std::error_code sys::ChangeStdinToBinary(){
|
2006-06-07 23:18:34 +00:00
|
|
|
// Do nothing, as Unix doesn't differentiate between text and binary.
|
2014-06-13 02:24:39 +00:00
|
|
|
return std::error_code();
|
2006-06-07 23:18:34 +00:00
|
|
|
}
|
|
|
|
|
2014-06-13 02:24:39 +00:00
|
|
|
std::error_code sys::ChangeStdoutToBinary(){
|
2006-06-07 23:18:34 +00:00
|
|
|
// Do nothing, as Unix doesn't differentiate between text and binary.
|
2014-06-13 02:24:39 +00:00
|
|
|
return std::error_code();
|
2006-06-07 23:18:34 +00:00
|
|
|
}
|
|
|
|
|
2013-04-11 14:06:34 +00:00
|
|
|
bool llvm::sys::argumentsFitWithinSystemLimits(ArrayRef<const char*> Args) {
|
|
|
|
static long ArgMax = sysconf(_SC_ARG_MAX);
|
|
|
|
|
|
|
|
// System says no practical limit.
|
|
|
|
if (ArgMax == -1)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
// Conservatively account for space required by environment variables.
|
2014-08-25 22:53:21 +00:00
|
|
|
long HalfArgMax = ArgMax / 2;
|
2013-04-11 14:06:34 +00:00
|
|
|
|
|
|
|
size_t ArgLength = 0;
|
|
|
|
for (ArrayRef<const char*>::iterator I = Args.begin(), E = Args.end();
|
|
|
|
I != E; ++I) {
|
|
|
|
ArgLength += strlen(*I) + 1;
|
2014-08-25 22:53:21 +00:00
|
|
|
if (ArgLength > size_t(HalfArgMax)) {
|
2013-04-11 14:06:34 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
2004-08-29 19:20:41 +00:00
|
|
|
}
|