For PR789:
[oota-llvm.git] / lib / System / Unix / Program.inc
index 18fcafeb4eae6c84c050a3db6a59a1d3eb7408f4..77d74a18c67735b123078758a4f57b39e2cecadd 100644 (file)
 
 #include <llvm/Config/config.h>
 #include "Unix.h"
+#include <iostream>
+#if HAVE_SYS_STAT_H
 #include <sys/stat.h>
+#endif
+#if HAVE_SYS_RESOURCE_H
+#include <sys/resource.h>
+#endif
+#if HAVE_SIGNAL_H
+#include <signal.h>
+#endif
+#if HAVE_FCNTL_H
 #include <fcntl.h>
-#ifdef HAVE_SYS_WAIT_H
-#include <sys/wait.h>
 #endif
 
-extern char** environ;
-
 namespace llvm {
 using namespace sys;
 
@@ -37,9 +43,11 @@ Program::FindProgramByName(const std::string& progName) {
   if (progName.length() == 0) // no program
     return Path();
   Path temp;
-  if (!temp.setFile(progName)) // invalid name
+  if (!temp.set(progName)) // invalid name
     return Path();
-  if (temp.executable()) // already executable as is
+  // FIXME: have to check for absolute filename - we cannot assume anything
+  // about "." being in $PATH
+  if (temp.canExecute()) // already executable as is
     return temp;
 
   // At this point, the file name is valid and its not executable
@@ -57,9 +65,9 @@ Program::FindProgramByName(const std::string& progName) {
 
     // Check to see if this first directory contains the executable...
     Path FilePath;
-    if (FilePath.setDirectory(std::string(PathStr,Colon))) {
-      FilePath.appendFile(progName);
-      if (FilePath.executable())
+    if (FilePath.set(std::string(PathStr,Colon))) {
+      FilePath.appendComponent(progName);
+      if (FilePath.canExecute())
         return FilePath;                    // Found the executable!
     }
 
@@ -76,61 +84,199 @@ Program::FindProgramByName(const std::string& progName) {
   return Path();
 }
 
-//
+static bool RedirectFD(const std::string &File, int FD, std::string* ErrMsg) {
+  if (File.empty()) return false;  // Noop
+
+  // Open the file
+  int InFD = open(File.c_str(), FD == 0 ? O_RDONLY : O_WRONLY|O_CREAT, 0666);
+  if (InFD == -1) {
+    MakeErrMsg(ErrMsg, "Cannot open file '" + File + "' for "
+              + (FD == 0 ? "input" : "output") + "!\n");
+    return true;
+  }
+
+  // Install it as the requested FD
+  if (-1 == dup2(InFD, FD)) {
+    MakeErrMsg(ErrMsg, "Cannot dup2");
+    return true;
+  }
+  close(InFD);      // Close the original FD
+  return false;
+}
+
+static bool Timeout = false;
+static void TimeOutHandler(int Sig) {
+  Timeout = true;
+}
+
+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);
+  // Resident set size.
+  getrlimit (RLIMIT_RSS, &r);
+  r.rlim_cur = limit;
+  setrlimit (RLIMIT_RSS, &r);
+  // Virtual memory.
+  getrlimit (RLIMIT_AS, &r);
+  r.rlim_cur = limit;
+  setrlimit (RLIMIT_AS, &r);
+#endif
+}
+
 int 
 Program::ExecuteAndWait(const Path& path, 
-                        const std::vector<std::string>& args) {
-  if (!path.executable())
-    throw path.get() + " is not executable"; 
+                        const char** args,
+                        const char** envp,
+                        const Path** redirects,
+                        unsigned secondsToWait,
+                        unsigned memoryLimit,
+                        std::string* ErrMsg) 
+{
+  if (!path.canExecute()) {
+    if (ErrMsg)
+      *ErrMsg = path.toString() + " is not executable";
+    return -1;
+  }
 
 #ifdef HAVE_SYS_WAIT_H
-  // Create local versions of the parameters that can be passed into execve()
-  // without creating const problems.
-  const char* argv[ args.size() + 2 ];
-  unsigned index = 0;
-  std::string progname(path.getLast());
-  argv[index++] = progname.c_str();
-  for (unsigned i = 0; i < args.size(); i++)
-    argv[index++] = args[i].c_str();
-  argv[index] = 0;
-
   // Create a child process.
-  switch (fork()) {
+  int child = fork();
+  switch (child) {
     // An error occured:  Return to the caller.
     case -1:
-      ThrowErrno(std::string("Couldn't execute program '") + path.get() + "'");
-      break;
+      MakeErrMsg(ErrMsg, "Couldn't fork");
+      return -1;
 
     // Child process: Execute the program.
-    case 0:
-      execve (path.c_str(), (char** const)argv, environ);
+    case 0: {
+      // Redirect file descriptors...
+      if (redirects) {
+        if (redirects[0]) {
+          if (redirects[0]->isEmpty()) {
+            if (RedirectFD("/dev/null",0,ErrMsg)) { return -1; }
+          } else {
+            if (RedirectFD(redirects[0]->toString(), 0,ErrMsg)) { return -1; }
+          }
+        }
+        if (redirects[1]) {
+          if (redirects[1]->isEmpty()) {
+            if (RedirectFD("/dev/null",1,ErrMsg)) { return -1; }
+          } else {
+            if (RedirectFD(redirects[1]->toString(),1,ErrMsg)) { return -1; }
+          }
+        }
+        if (redirects[1] && redirects[2] && 
+            *(redirects[1]) != *(redirects[2])) {
+          if (redirects[2]->isEmpty()) {
+            if (RedirectFD("/dev/null",2,ErrMsg)) { return -1; }
+          } else {
+            if (RedirectFD(redirects[2]->toString(), 2,ErrMsg)) { return -1; }
+          }
+        } else if (-1 == dup2(1,2)) {
+          MakeErrMsg(ErrMsg, "Can't redirect");
+          return -1;
+        }
+      }
+
+      // Set memory limits
+      if (memoryLimit!=0) {
+        SetMemoryLimits(memoryLimit);
+      }
+      
+      // Execute!
+      if (envp != 0)
+        execve (path.c_str(), (char** const)args, (char**)envp);
+      else
+        execv (path.c_str(), (char** const)args);
       // If the execve() failed, we should exit and let the parent pick up
       // our non-zero exit status.
       exit (errno);
+    }
 
     // Parent process: Break out of the switch to do our processing.
     default:
       break;
   }
 
+  // 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);
+  }
+
   // Parent process: Wait for the child process to terminate.
   int status;
-  if ((::wait (&status)) == -1)
-    ThrowErrno(std::string("Failed waiting for program '") + path.get() + "'");
+  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)
+        MakeErrMsg(ErrMsg, "Child timed out but wouldn't die");
+        
+      return -1;   // Timeout detected
+    } else {
+      MakeErrMsg(ErrMsg, "Error waiting for child process");
+      return -1;
+    }
+
+  // We exited normally without timeout, so turn off the timer.
+  if (secondsToWait) {
+    alarm(0);
+    sigaction(SIGALRM, &Old, 0);
+  }
 
-  // If the program exited normally with a zero exit status, return success!
-  if (WIFEXITED (status))
-    return WEXITSTATUS(status);
+  // 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;
+  if (WIFEXITED(status))
+    result = WEXITSTATUS(status);
   else if (WIFSIGNALED(status))
-    throw std::string("Program '") + path.get() + "' received terminating signal.";
-  else
-    return 0;
-    
+    result = 0 - WTERMSIG(status);
+#ifdef WCOREDUMP
+  else if (WCOREDUMP(status))
+    result |= 0x01000000;
+#endif
+  return result;
 #else
-  throw std::string("Program::ExecuteAndWait not implemented on this platform!\n");
+  return -99;
 #endif
+    
+}
+
+bool Program::ChangeStdinToBinary(){
+  // Do nothing, as Unix doesn't differentiate between text and binary.
+  return false;
+}
 
+bool Program::ChangeStdoutToBinary(){
+  // Do nothing, as Unix doesn't differentiate between text and binary.
+  return false;
 }
 
 }
-// vim: sw=2 smartindent smarttab tw=80 autoindent expandtab