2004-08-30 03:20:41 +08:00
|
|
|
//===- llvm/System/Unix/Program.cpp -----------------------------*- C++ -*-===//
|
2009-07-18 04:38:17 +08:00
|
|
|
//
|
2004-08-30 03:20:41 +08:00
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
2007-12-30 04:36:04 +08:00
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
2009-07-18 04:38:17 +08:00
|
|
|
//
|
2004-08-30 03:20:41 +08: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.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2004-09-02 06:55:40 +08:00
|
|
|
#include <llvm/Config/config.h>
|
2004-08-30 03:20:41 +08:00
|
|
|
#include "Unix.h"
|
2004-12-27 14:16:52 +08:00
|
|
|
#if HAVE_SYS_STAT_H
|
2004-08-30 03:20:41 +08:00
|
|
|
#include <sys/stat.h>
|
2004-12-27 14:16:52 +08:00
|
|
|
#endif
|
2007-02-17 03:11:07 +08:00
|
|
|
#if HAVE_SYS_RESOURCE_H
|
|
|
|
#include <sys/resource.h>
|
|
|
|
#endif
|
2004-12-27 14:16:52 +08:00
|
|
|
#if HAVE_SIGNAL_H
|
2004-12-20 02:58:11 +08:00
|
|
|
#include <signal.h>
|
2004-12-27 14:16:52 +08:00
|
|
|
#endif
|
|
|
|
#if HAVE_FCNTL_H
|
2004-08-30 03:20:41 +08:00
|
|
|
#include <fcntl.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
namespace llvm {
|
|
|
|
using namespace sys;
|
|
|
|
|
2009-09-22 12:44:56 +08:00
|
|
|
Program::Program() : Data_(0) {}
|
|
|
|
|
|
|
|
Program::~Program() {}
|
|
|
|
|
|
|
|
unsigned Program::GetPid() const {
|
|
|
|
uint64_t pid = reinterpret_cast<uint64_t>(Data_);
|
|
|
|
return static_cast<unsigned>(pid);
|
|
|
|
}
|
|
|
|
|
2004-08-30 03:20:41 +08:00
|
|
|
// This function just uses the PATH environment variable to find the program.
|
2004-09-14 05:48:44 +08:00
|
|
|
Path
|
2004-08-30 03:20:41 +08:00
|
|
|
Program::FindProgramByName(const std::string& progName) {
|
|
|
|
|
|
|
|
// Check some degenerate cases
|
|
|
|
if (progName.length() == 0) // no program
|
2004-09-14 05:48:44 +08:00
|
|
|
return Path();
|
|
|
|
Path temp;
|
2005-07-08 07:21:43 +08:00
|
|
|
if (!temp.set(progName)) // invalid name
|
2004-09-14 05:48:44 +08:00
|
|
|
return Path();
|
2009-07-29 06:08:15 +08:00
|
|
|
// Use the given path verbatim if it contains any slashes; this matches
|
|
|
|
// the behavior of sh(1) and friends.
|
2009-07-29 07:25:18 +08:00
|
|
|
if (progName.find('/') != std::string::npos)
|
2004-08-30 03:20:41 +08:00
|
|
|
return temp;
|
|
|
|
|
2009-08-06 01:32:39 +08:00
|
|
|
// At this point, the file name does not contain slashes. Search for it
|
|
|
|
// through the directories specified in the PATH environment variable.
|
2009-07-18 04:38:17 +08:00
|
|
|
|
2004-08-30 03:20:41 +08:00
|
|
|
// Get the path. If its empty, we can't do anything to find it.
|
|
|
|
const char *PathStr = getenv("PATH");
|
2009-07-18 04:38:17 +08:00
|
|
|
if (PathStr == 0)
|
2004-09-14 05:48:44 +08:00
|
|
|
return Path();
|
2004-08-30 03:20:41 +08:00
|
|
|
|
|
|
|
// Now we have a colon separated list of directories to search; try them.
|
2008-05-06 02:30:58 +08:00
|
|
|
size_t PathLen = strlen(PathStr);
|
2004-08-30 03:20:41 +08: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...
|
2004-09-14 05:48:44 +08:00
|
|
|
Path FilePath;
|
2005-07-08 07:21:43 +08:00
|
|
|
if (FilePath.set(std::string(PathStr,Colon))) {
|
|
|
|
FilePath.appendComponent(progName);
|
2005-07-08 02:21:42 +08:00
|
|
|
if (FilePath.canExecute())
|
2004-08-30 03:20:41 +08:00
|
|
|
return FilePath; // Found the executable!
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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--;
|
|
|
|
}
|
|
|
|
}
|
2004-09-14 05:48:44 +08:00
|
|
|
return Path();
|
2004-08-30 03:20:41 +08:00
|
|
|
}
|
|
|
|
|
2008-06-12 18:47:18 +08:00
|
|
|
static bool RedirectIO(const Path *Path, int FD, std::string* ErrMsg) {
|
|
|
|
if (Path == 0)
|
|
|
|
// Noop
|
|
|
|
return false;
|
|
|
|
std::string File;
|
|
|
|
if (Path->isEmpty())
|
|
|
|
// Redirect empty paths to /dev/null
|
|
|
|
File = "/dev/null";
|
|
|
|
else
|
2009-08-24 06:45:37 +08:00
|
|
|
File = Path->str();
|
2004-12-20 02:00:44 +08:00
|
|
|
|
|
|
|
// Open the file
|
|
|
|
int InFD = open(File.c_str(), FD == 0 ? O_RDONLY : O_WRONLY|O_CREAT, 0666);
|
|
|
|
if (InFD == -1) {
|
2006-08-21 14:02:44 +08:00
|
|
|
MakeErrMsg(ErrMsg, "Cannot open file '" + File + "' for "
|
2009-04-21 04:50:13 +08:00
|
|
|
+ (FD == 0 ? "input" : "output"));
|
2006-08-21 14:02:44 +08:00
|
|
|
return true;
|
2004-12-20 02:00:44 +08:00
|
|
|
}
|
|
|
|
|
2006-08-21 14:02:44 +08:00
|
|
|
// Install it as the requested FD
|
|
|
|
if (-1 == dup2(InFD, FD)) {
|
|
|
|
MakeErrMsg(ErrMsg, "Cannot dup2");
|
|
|
|
return true;
|
|
|
|
}
|
2004-12-20 02:00:44 +08:00
|
|
|
close(InFD); // Close the original FD
|
2006-08-21 14:02:44 +08:00
|
|
|
return false;
|
2004-12-20 02:00:44 +08:00
|
|
|
}
|
|
|
|
|
2009-11-09 04:55:48 +08:00
|
|
|
static void TimeOutHandler(int Sig) {
|
|
|
|
}
|
|
|
|
|
2007-02-17 03:11:07 +08:00
|
|
|
static void SetMemoryLimits (unsigned size)
|
|
|
|
{
|
2010-02-12 08:37:46 +08:00
|
|
|
#if HAVE_SYS_RESOURCE_H && HAVE_GETRLIMIT && HAVE_SETRLIMIT
|
2007-02-17 03:11:07 +08: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 18:31:27 +08:00
|
|
|
#ifdef RLIMIT_RSS
|
2007-02-17 03:11:07 +08:00
|
|
|
// Resident set size.
|
|
|
|
getrlimit (RLIMIT_RSS, &r);
|
|
|
|
r.rlim_cur = limit;
|
|
|
|
setrlimit (RLIMIT_RSS, &r);
|
2007-04-23 15:22:51 +08:00
|
|
|
#endif
|
2007-06-04 23:28:57 +08:00
|
|
|
#ifdef RLIMIT_AS // e.g. NetBSD doesn't have it.
|
2007-02-17 03:11:07 +08:00
|
|
|
// Virtual memory.
|
|
|
|
getrlimit (RLIMIT_AS, &r);
|
|
|
|
r.rlim_cur = limit;
|
|
|
|
setrlimit (RLIMIT_AS, &r);
|
|
|
|
#endif
|
2007-06-04 23:28:57 +08:00
|
|
|
#endif
|
2007-02-17 03:11:07 +08:00
|
|
|
}
|
|
|
|
|
2009-07-19 05:43:12 +08:00
|
|
|
bool
|
|
|
|
Program::Execute(const Path& path,
|
|
|
|
const char** args,
|
|
|
|
const char** envp,
|
|
|
|
const Path** redirects,
|
|
|
|
unsigned memoryLimit,
|
|
|
|
std::string* ErrMsg)
|
2006-08-21 14:02:44 +08:00
|
|
|
{
|
|
|
|
if (!path.canExecute()) {
|
|
|
|
if (ErrMsg)
|
2009-08-24 06:45:37 +08:00
|
|
|
*ErrMsg = path.str() + " is not executable";
|
2009-07-19 05:43:12 +08:00
|
|
|
return false;
|
2006-08-21 14:02:44 +08:00
|
|
|
}
|
2004-08-30 03:20:41 +08:00
|
|
|
|
|
|
|
// Create a child process.
|
2004-12-20 02:00:44 +08:00
|
|
|
int child = fork();
|
|
|
|
switch (child) {
|
2004-08-30 03:20:41 +08:00
|
|
|
// An error occured: Return to the caller.
|
|
|
|
case -1:
|
2006-08-21 14:02:44 +08:00
|
|
|
MakeErrMsg(ErrMsg, "Couldn't fork");
|
2009-07-19 05:43:12 +08:00
|
|
|
return false;
|
2004-08-30 03:20:41 +08:00
|
|
|
|
|
|
|
// Child process: Execute the program.
|
2004-12-14 12:18:51 +08:00
|
|
|
case 0: {
|
2004-12-20 02:00:44 +08:00
|
|
|
// Redirect file descriptors...
|
|
|
|
if (redirects) {
|
2008-06-12 20:53:35 +08:00
|
|
|
// Redirect stdin
|
2009-07-19 05:43:12 +08:00
|
|
|
if (RedirectIO(redirects[0], 0, ErrMsg)) { return false; }
|
2008-06-12 20:53:35 +08:00
|
|
|
// Redirect stdout
|
2009-07-19 05:43:12 +08:00
|
|
|
if (RedirectIO(redirects[1], 1, ErrMsg)) { return false; }
|
2009-07-18 04:38:17 +08:00
|
|
|
if (redirects[1] && redirects[2] &&
|
2008-06-12 20:53:35 +08: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-19 05:43:12 +08:00
|
|
|
return false;
|
2008-06-12 20:53:35 +08:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Just redirect stderr
|
2009-07-19 05:43:12 +08:00
|
|
|
if (RedirectIO(redirects[2], 2, ErrMsg)) { return false; }
|
2004-12-20 02:00:44 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-02-17 03:11:07 +08:00
|
|
|
// Set memory limits
|
|
|
|
if (memoryLimit!=0) {
|
|
|
|
SetMemoryLimits(memoryLimit);
|
|
|
|
}
|
2009-07-18 04:38:17 +08:00
|
|
|
|
2004-12-20 02:00:44 +08:00
|
|
|
// Execute!
|
2006-06-10 04:43:11 +08:00
|
|
|
if (envp != 0)
|
2009-08-05 04:32:25 +08:00
|
|
|
execve(path.c_str(), (char**)args, (char**)envp);
|
2006-06-10 04:43:11 +08:00
|
|
|
else
|
2009-08-05 04:32:25 +08:00
|
|
|
execv(path.c_str(), (char**)args);
|
2009-08-05 08:09:12 +08: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 12:18:51 +08:00
|
|
|
}
|
2004-08-30 03:20:41 +08:00
|
|
|
|
|
|
|
// Parent process: Break out of the switch to do our processing.
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2009-09-22 12:44:56 +08:00
|
|
|
Data_ = reinterpret_cast<void*>(child);
|
2009-07-19 05:43:12 +08:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
Program::Wait(unsigned secondsToWait,
|
|
|
|
std::string* ErrMsg)
|
|
|
|
{
|
|
|
|
#ifdef HAVE_SYS_WAIT_H
|
2004-12-20 02:00:44 +08:00
|
|
|
struct sigaction Act, Old;
|
|
|
|
|
2009-09-22 12:44:56 +08:00
|
|
|
if (Data_ == 0) {
|
2009-07-19 05:43:12 +08:00
|
|
|
MakeErrMsg(ErrMsg, "Process not started!");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2009-11-09 04:55:48 +08:00
|
|
|
// 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.
|
2004-12-20 02:00:44 +08:00
|
|
|
if (secondsToWait) {
|
2009-11-09 04:55:48 +08:00
|
|
|
Act.sa_sigaction = 0;
|
|
|
|
Act.sa_handler = TimeOutHandler;
|
2004-12-20 02:00:44 +08:00
|
|
|
sigemptyset(&Act.sa_mask);
|
2009-11-09 04:55:48 +08:00
|
|
|
Act.sa_flags = 0;
|
2004-12-20 02:00:44 +08:00
|
|
|
sigaction(SIGALRM, &Act, &Old);
|
|
|
|
alarm(secondsToWait);
|
|
|
|
}
|
|
|
|
|
2004-08-30 03:20:41 +08:00
|
|
|
// Parent process: Wait for the child process to terminate.
|
|
|
|
int status;
|
2009-09-22 12:44:56 +08:00
|
|
|
uint64_t pid = reinterpret_cast<uint64_t>(Data_);
|
|
|
|
pid_t child = static_cast<pid_t>(pid);
|
2009-10-23 06:16:17 +08:00
|
|
|
while (waitpid(pid, &status, 0) != child)
|
2004-12-20 02:00:44 +08:00
|
|
|
if (secondsToWait && errno == EINTR) {
|
|
|
|
// Kill the child.
|
|
|
|
kill(child, SIGKILL);
|
2009-07-18 04:38:17 +08:00
|
|
|
|
2004-12-20 02:00:44 +08:00
|
|
|
// Turn off the alarm and restore the signal handler
|
|
|
|
alarm(0);
|
|
|
|
sigaction(SIGALRM, &Old, 0);
|
|
|
|
|
|
|
|
// Wait for child to die
|
|
|
|
if (wait(&status) != child)
|
2006-08-21 14:02:44 +08:00
|
|
|
MakeErrMsg(ErrMsg, "Child timed out but wouldn't die");
|
2008-02-05 04:57:54 +08:00
|
|
|
else
|
|
|
|
MakeErrMsg(ErrMsg, "Child timed out", 0);
|
|
|
|
|
2004-12-20 02:00:44 +08:00
|
|
|
return -1; // Timeout detected
|
2008-02-05 04:57:54 +08:00
|
|
|
} else if (errno != EINTR) {
|
2006-08-21 14:02:44 +08:00
|
|
|
MakeErrMsg(ErrMsg, "Error waiting for child process");
|
|
|
|
return -1;
|
2004-12-20 02:00:44 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// We exited normally without timeout, so turn off the timer.
|
|
|
|
if (secondsToWait) {
|
|
|
|
alarm(0);
|
|
|
|
sigaction(SIGALRM, &Old, 0);
|
|
|
|
}
|
2004-08-30 03:20:41 +08:00
|
|
|
|
2005-12-23 04:00:16 +08:00
|
|
|
// Return the proper exit status. 0=success, >0 is programs' exit status,
|
|
|
|
// <0 means a signal was returned, -9999999 means the program dumped core.
|
|
|
|
int result = 0;
|
2006-07-13 06:37:18 +08:00
|
|
|
if (WIFEXITED(status))
|
2005-12-23 04:00:16 +08:00
|
|
|
result = WEXITSTATUS(status);
|
2004-08-30 03:20:41 +08:00
|
|
|
else if (WIFSIGNALED(status))
|
2005-12-23 04:00:16 +08:00
|
|
|
result = 0 - WTERMSIG(status);
|
|
|
|
#ifdef WCOREDUMP
|
2006-07-13 06:37:18 +08:00
|
|
|
else if (WCOREDUMP(status))
|
2005-12-23 04:00:16 +08:00
|
|
|
result |= 0x01000000;
|
|
|
|
#endif
|
|
|
|
return result;
|
2004-08-30 03:20:41 +08:00
|
|
|
#else
|
2005-12-23 04:00:16 +08:00
|
|
|
return -99;
|
2004-08-30 03:20:41 +08:00
|
|
|
#endif
|
2009-07-18 04:38:17 +08:00
|
|
|
|
2004-08-30 03:20:41 +08:00
|
|
|
}
|
|
|
|
|
2009-09-09 03:50:27 +08:00
|
|
|
bool
|
|
|
|
Program::Kill(std::string* ErrMsg) {
|
2009-09-22 12:44:56 +08:00
|
|
|
if (Data_ == 0) {
|
2009-09-09 03:50:27 +08:00
|
|
|
MakeErrMsg(ErrMsg, "Process not started!");
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2009-09-22 12:44:56 +08:00
|
|
|
uint64_t pid64 = reinterpret_cast<uint64_t>(Data_);
|
|
|
|
pid_t pid = static_cast<pid_t>(pid64);
|
|
|
|
|
|
|
|
if (kill(pid, SIGKILL) != 0) {
|
2009-09-09 17:51:47 +08:00
|
|
|
MakeErrMsg(ErrMsg, "The process couldn't be killed!");
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
2009-09-09 03:50:27 +08:00
|
|
|
}
|
|
|
|
|
2006-08-21 14:02:44 +08:00
|
|
|
bool Program::ChangeStdinToBinary(){
|
2006-06-08 07:18:34 +08:00
|
|
|
// Do nothing, as Unix doesn't differentiate between text and binary.
|
2006-08-21 14:02:44 +08:00
|
|
|
return false;
|
2006-06-08 07:18:34 +08:00
|
|
|
}
|
|
|
|
|
2006-08-21 14:02:44 +08:00
|
|
|
bool Program::ChangeStdoutToBinary(){
|
2006-06-08 07:18:34 +08:00
|
|
|
// Do nothing, as Unix doesn't differentiate between text and binary.
|
2006-08-21 14:02:44 +08:00
|
|
|
return false;
|
2006-06-08 07:18:34 +08:00
|
|
|
}
|
|
|
|
|
2010-01-28 14:42:08 +08:00
|
|
|
bool Program::ChangeStderrToBinary(){
|
|
|
|
// Do nothing, as Unix doesn't differentiate between text and binary.
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2004-08-30 03:20:41 +08:00
|
|
|
}
|