2004-08-29 19:20:41 +00:00
|
|
|
//===- llvm/System/Unix/Program.cpp -----------------------------*- C++ -*-===//
|
|
|
|
//
|
|
|
|
// 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.
|
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.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2004-09-01 22:55:40 +00:00
|
|
|
#include <llvm/Config/config.h>
|
2004-08-29 19:20:41 +00:00
|
|
|
#include "Unix.h"
|
2004-12-27 06:16:52 +00:00
|
|
|
#include <iostream>
|
|
|
|
#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
|
|
|
|
|
|
|
|
namespace llvm {
|
|
|
|
using namespace sys;
|
|
|
|
|
|
|
|
// This function just uses the PATH environment variable to find the program.
|
2004-09-13 21:48:44 +00:00
|
|
|
Path
|
2004-08-29 19:20:41 +00:00
|
|
|
Program::FindProgramByName(const std::string& progName) {
|
|
|
|
|
|
|
|
// Check some degenerate cases
|
|
|
|
if (progName.length() == 0) // no program
|
2004-09-13 21:48:44 +00:00
|
|
|
return Path();
|
|
|
|
Path temp;
|
2005-07-07 23:21:43 +00:00
|
|
|
if (!temp.set(progName)) // invalid name
|
2004-09-13 21:48:44 +00:00
|
|
|
return Path();
|
2005-04-20 15:42:11 +00:00
|
|
|
// FIXME: have to check for absolute filename - we cannot assume anything
|
|
|
|
// about "." being in $PATH
|
2005-07-07 18:21:42 +00:00
|
|
|
if (temp.canExecute()) // already executable as is
|
2004-08-29 19:20:41 +00:00
|
|
|
return temp;
|
|
|
|
|
|
|
|
// At this point, the file name is valid and its not executable
|
|
|
|
|
|
|
|
// Get the path. If its empty, we can't do anything to find it.
|
|
|
|
const char *PathStr = getenv("PATH");
|
|
|
|
if (PathStr == 0)
|
2004-09-13 21:48:44 +00:00
|
|
|
return Path();
|
2004-08-29 19:20:41 +00:00
|
|
|
|
|
|
|
// Now we have a colon separated list of directories to search; try them.
|
|
|
|
unsigned PathLen = strlen(PathStr);
|
|
|
|
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-13 21:48:44 +00:00
|
|
|
Path FilePath;
|
2005-07-07 23:21:43 +00:00
|
|
|
if (FilePath.set(std::string(PathStr,Colon))) {
|
|
|
|
FilePath.appendComponent(progName);
|
2005-07-07 18:21:42 +00:00
|
|
|
if (FilePath.canExecute())
|
2004-08-29 19:20:41 +00: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-13 21:48:44 +00:00
|
|
|
return Path();
|
2004-08-29 19:20:41 +00:00
|
|
|
}
|
|
|
|
|
2006-08-21 06:02:44 +00:00
|
|
|
static bool RedirectFD(const std::string &File, int FD, std::string* ErrMsg) {
|
|
|
|
if (File.empty()) return false; // Noop
|
2004-12-19 18:00:44 +00: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 06:02:44 +00:00
|
|
|
MakeErrMsg(ErrMsg, "Cannot open file '" + File + "' for "
|
2004-12-19 18:00:44 +00:00
|
|
|
+ (FD == 0 ? "input" : "output") + "!\n");
|
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
|
|
|
|
if (-1 == dup2(InFD, FD)) {
|
|
|
|
MakeErrMsg(ErrMsg, "Cannot dup2");
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
static bool Timeout = false;
|
|
|
|
static void TimeOutHandler(int Sig) {
|
|
|
|
Timeout = true;
|
|
|
|
}
|
|
|
|
|
2007-02-16 19:11:07 +00:00
|
|
|
static void SetMemoryLimits (unsigned size)
|
|
|
|
{
|
|
|
|
#if HAVE_SYS_RESOURCE_H
|
|
|
|
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.
|
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
|
2007-02-16 19:11:07 +00:00
|
|
|
}
|
|
|
|
|
2004-08-29 19:20:41 +00:00
|
|
|
int
|
2004-09-13 21:48:44 +00:00
|
|
|
Program::ExecuteAndWait(const Path& path,
|
2004-12-19 18:00:44 +00:00
|
|
|
const char** args,
|
|
|
|
const char** envp,
|
|
|
|
const Path** redirects,
|
2006-08-21 06:02:44 +00:00
|
|
|
unsigned secondsToWait,
|
2007-02-16 19:11:07 +00:00
|
|
|
unsigned memoryLimit,
|
2006-08-21 06:02:44 +00:00
|
|
|
std::string* ErrMsg)
|
|
|
|
{
|
|
|
|
if (!path.canExecute()) {
|
|
|
|
if (ErrMsg)
|
|
|
|
*ErrMsg = path.toString() + " is not executable";
|
|
|
|
return -1;
|
|
|
|
}
|
2004-08-29 19:20:41 +00:00
|
|
|
|
|
|
|
#ifdef HAVE_SYS_WAIT_H
|
|
|
|
// Create a child process.
|
2004-12-19 18:00:44 +00:00
|
|
|
int child = fork();
|
|
|
|
switch (child) {
|
2004-08-29 19:20:41 +00:00
|
|
|
// An error occured: Return to the caller.
|
|
|
|
case -1:
|
2006-08-21 06:02:44 +00:00
|
|
|
MakeErrMsg(ErrMsg, "Couldn't fork");
|
|
|
|
return -1;
|
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) {
|
2006-08-22 15:56:52 +00:00
|
|
|
if (redirects[0]) {
|
|
|
|
if (redirects[0]->isEmpty()) {
|
2006-08-21 06:02:44 +00:00
|
|
|
if (RedirectFD("/dev/null",0,ErrMsg)) { return -1; }
|
2006-08-22 15:56:52 +00:00
|
|
|
} else {
|
2006-08-21 06:02:44 +00:00
|
|
|
if (RedirectFD(redirects[0]->toString(), 0,ErrMsg)) { return -1; }
|
2006-08-22 15:56:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (redirects[1]) {
|
|
|
|
if (redirects[1]->isEmpty()) {
|
2006-08-21 06:02:44 +00:00
|
|
|
if (RedirectFD("/dev/null",1,ErrMsg)) { return -1; }
|
2006-08-22 15:56:52 +00:00
|
|
|
} else {
|
2006-08-21 06:02:44 +00:00
|
|
|
if (RedirectFD(redirects[1]->toString(),1,ErrMsg)) { return -1; }
|
2006-08-22 15:56:52 +00:00
|
|
|
}
|
|
|
|
}
|
2004-12-19 18:00:44 +00:00
|
|
|
if (redirects[1] && redirects[2] &&
|
|
|
|
*(redirects[1]) != *(redirects[2])) {
|
2006-08-22 15:56:52 +00:00
|
|
|
if (redirects[2]->isEmpty()) {
|
2006-08-21 06:02:44 +00:00
|
|
|
if (RedirectFD("/dev/null",2,ErrMsg)) { return -1; }
|
2006-08-22 15:56:52 +00:00
|
|
|
} else {
|
2006-08-21 06:02:44 +00:00
|
|
|
if (RedirectFD(redirects[2]->toString(), 2,ErrMsg)) { return -1; }
|
2006-08-22 15:56:52 +00:00
|
|
|
}
|
2006-08-21 06:02:44 +00:00
|
|
|
} else if (-1 == dup2(1,2)) {
|
|
|
|
MakeErrMsg(ErrMsg, "Can't redirect");
|
|
|
|
return -1;
|
2004-12-19 18:00:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-02-16 19:11:07 +00:00
|
|
|
// Set memory limits
|
|
|
|
if (memoryLimit!=0) {
|
|
|
|
SetMemoryLimits(memoryLimit);
|
|
|
|
}
|
|
|
|
|
2004-12-19 18:00:44 +00:00
|
|
|
// Execute!
|
2006-06-09 20:43:11 +00:00
|
|
|
if (envp != 0)
|
2007-09-14 20:08:19 +00:00
|
|
|
execve (path.c_str(), (char**)args, (char**)envp);
|
2006-06-09 20:43:11 +00:00
|
|
|
else
|
2007-09-14 20:08:19 +00:00
|
|
|
execv (path.c_str(), (char**)args);
|
2004-08-29 19:20:41 +00:00
|
|
|
// If the execve() failed, we should exit and let the parent pick up
|
|
|
|
// our non-zero exit status.
|
|
|
|
exit (errno);
|
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;
|
|
|
|
}
|
|
|
|
|
2004-12-19 18:00:44 +00:00
|
|
|
// Make sure stderr and stdout have been flushed
|
|
|
|
std::cerr << std::flush;
|
|
|
|
std::cout << std::flush;
|
|
|
|
fsync(1);
|
|
|
|
fsync(2);
|
|
|
|
|
|
|
|
struct sigaction Act, Old;
|
|
|
|
|
|
|
|
// Install a timeout handler.
|
|
|
|
if (secondsToWait) {
|
|
|
|
Timeout = false;
|
|
|
|
Act.sa_sigaction = 0;
|
|
|
|
Act.sa_handler = TimeOutHandler;
|
|
|
|
sigemptyset(&Act.sa_mask);
|
|
|
|
Act.sa_flags = 0;
|
|
|
|
sigaction(SIGALRM, &Act, &Old);
|
|
|
|
alarm(secondsToWait);
|
|
|
|
}
|
|
|
|
|
2004-08-29 19:20:41 +00:00
|
|
|
// Parent process: Wait for the child process to terminate.
|
|
|
|
int status;
|
2004-12-19 18:00:44 +00:00
|
|
|
while (wait(&status) != child)
|
|
|
|
if (secondsToWait && errno == EINTR) {
|
|
|
|
// Kill the child.
|
|
|
|
kill(child, SIGKILL);
|
|
|
|
|
|
|
|
// 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 06:02:44 +00:00
|
|
|
MakeErrMsg(ErrMsg, "Child timed out but wouldn't die");
|
2008-02-04 20:57:54 +00:00
|
|
|
else
|
|
|
|
MakeErrMsg(ErrMsg, "Child timed out", 0);
|
|
|
|
|
2004-12-19 18:00:44 +00:00
|
|
|
return -1; // Timeout detected
|
2008-02-04 20:57:54 +00:00
|
|
|
} else if (errno != EINTR) {
|
2006-08-21 06:02:44 +00:00
|
|
|
MakeErrMsg(ErrMsg, "Error waiting for child process");
|
|
|
|
return -1;
|
2004-12-19 18:00:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// We exited normally without timeout, so turn off the timer.
|
|
|
|
if (secondsToWait) {
|
|
|
|
alarm(0);
|
|
|
|
sigaction(SIGALRM, &Old, 0);
|
|
|
|
}
|
2004-08-29 19:20:41 +00:00
|
|
|
|
2005-12-22 20:00:16 +00: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-12 22:37:18 +00:00
|
|
|
if (WIFEXITED(status))
|
2005-12-22 20:00:16 +00:00
|
|
|
result = WEXITSTATUS(status);
|
2004-08-29 19:20:41 +00:00
|
|
|
else if (WIFSIGNALED(status))
|
2005-12-22 20:00:16 +00:00
|
|
|
result = 0 - WTERMSIG(status);
|
|
|
|
#ifdef WCOREDUMP
|
2006-07-12 22:37:18 +00:00
|
|
|
else if (WCOREDUMP(status))
|
2005-12-22 20:00:16 +00:00
|
|
|
result |= 0x01000000;
|
|
|
|
#endif
|
|
|
|
return result;
|
2004-08-29 19:20:41 +00:00
|
|
|
#else
|
2005-12-22 20:00:16 +00:00
|
|
|
return -99;
|
2004-08-29 19:20:41 +00:00
|
|
|
#endif
|
2005-12-22 20:00:16 +00:00
|
|
|
|
2004-08-29 19:20:41 +00:00
|
|
|
}
|
|
|
|
|
2006-08-21 06:02:44 +00:00
|
|
|
bool Program::ChangeStdinToBinary(){
|
2006-06-07 23:18:34 +00:00
|
|
|
// Do nothing, as Unix doesn't differentiate between text and binary.
|
2006-08-21 06:02:44 +00:00
|
|
|
return false;
|
2006-06-07 23:18:34 +00:00
|
|
|
}
|
|
|
|
|
2006-08-21 06:02:44 +00:00
|
|
|
bool Program::ChangeStdoutToBinary(){
|
2006-06-07 23:18:34 +00:00
|
|
|
// Do nothing, as Unix doesn't differentiate between text and binary.
|
2006-08-21 06:02:44 +00:00
|
|
|
return false;
|
2006-06-07 23:18:34 +00:00
|
|
|
}
|
|
|
|
|
2004-08-29 19:20:41 +00:00
|
|
|
}
|