Fix main executable path name resolution on FreeBSD, patch by
[oota-llvm.git] / lib / System / Unix / Program.inc
index c3d5d5ce6e5ec31a66432f0355be50a3c2f1c6c5..2426900578b2696797f2f7923b86e8f6cd801bcb 100644 (file)
@@ -2,8 +2,8 @@
 // 
 //                     The LLVM Compiler Infrastructure
 //
-// This file was developed by Reid Spencer and is distributed under the 
-// University of Illinois Open Source License. See LICENSE.TXT for details.
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
 // 
 //===----------------------------------------------------------------------===//
 //
@@ -22,6 +22,9 @@
 #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
@@ -55,7 +58,7 @@ Program::FindProgramByName(const std::string& progName) {
     return Path();
 
   // Now we have a colon separated list of directories to search; try them.
-  unsigned PathLen = strlen(PathStr);
+  size_t PathLen = strlen(PathStr);
   while (PathLen) {
     // Find the first colon...
     const char *Colon = std::find(PathStr, PathStr+PathLen, ':');
@@ -81,18 +84,32 @@ Program::FindProgramByName(const std::string& progName) {
   return Path();
 }
 
-static void RedirectFD(const std::string &File, int FD) {
-  if (File.empty()) return;  // Noop
+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
+    File = Path->toString();
 
   // Open the file
   int InFD = open(File.c_str(), FD == 0 ? O_RDONLY : O_WRONLY|O_CREAT, 0666);
   if (InFD == -1) {
-    ThrowErrno("Cannot open file '" + File + "' for "
+    MakeErrMsg(ErrMsg, "Cannot open file '" + File + "' for "
               + (FD == 0 ? "input" : "output") + "!\n");
+    return true;
   }
 
-  dup2(InFD, FD);   // Install it as the requested FD
+  // 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;
@@ -100,15 +117,45 @@ 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);
+#ifdef RLIMIT_RSS
+  // Resident set size.
+  getrlimit (RLIMIT_RSS, &r);
+  r.rlim_cur = limit;
+  setrlimit (RLIMIT_RSS, &r);
+#endif
+#ifdef RLIMIT_AS  // e.g. NetBSD doesn't have it.
+  // Virtual memory.
+  getrlimit (RLIMIT_AS, &r);
+  r.rlim_cur = limit;
+  setrlimit (RLIMIT_AS, &r);
+#endif
+#endif
+}
+
 int 
 Program::ExecuteAndWait(const Path& path, 
                         const char** args,
                         const char** envp,
                         const Path** redirects,
-                        unsigned secondsToWait
-) {
-  if (!path.canExecute())
-    return -9999;
+                        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 a child process.
@@ -116,40 +163,41 @@ Program::ExecuteAndWait(const Path& path,
   switch (child) {
     // An error occured:  Return to the caller.
     case -1:
-      ThrowErrno(std::string("Couldn't execute program '") + path.toString() + 
-                 "'");
-      break;
+      MakeErrMsg(ErrMsg, "Couldn't fork");
+      return -1;
 
     // Child process: Execute the program.
     case 0: {
       // Redirect file descriptors...
       if (redirects) {
-        if (redirects[0])
-          if (redirects[0]->isEmpty())
-            RedirectFD("/dev/null",0);
-          else
-            RedirectFD(redirects[0]->toString(), 0);
-        if (redirects[1])
-          if (redirects[1]->isEmpty())
-            RedirectFD("/dev/null",1);
-          else
-            RedirectFD(redirects[1]->toString(), 1);
+        // Redirect stdin
+        if (RedirectIO(redirects[0], 0, ErrMsg)) { return -1; }
+        // Redirect stdout
+        if (RedirectIO(redirects[1], 1, ErrMsg)) { return -1; }
         if (redirects[1] && redirects[2] && 
-            *(redirects[1]) != *(redirects[2])) {
-          if (redirects[2]->isEmpty())
-            RedirectFD("/dev/null",2);
-          else
-            RedirectFD(redirects[2]->toString(), 2);
+            *(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");
+            return -1;
+          }
         } else {
-          dup2(1, 2);
+          // Just redirect stderr
+          if (RedirectIO(redirects[2], 2, ErrMsg)) { return -1; }
         }
       }
 
+      // Set memory limits
+      if (memoryLimit!=0) {
+        SetMemoryLimits(memoryLimit);
+      }
+      
       // Execute!
       if (envp != 0)
-        execve (path.c_str(), (char** const)args, (char**)envp);
+        execve (path.c_str(), (char**)args, (char**)envp);
       else
-        execv (path.c_str(), (char** const)args);
+        execv (path.c_str(), (char**)args);
       // If the execve() failed, we should exit and let the parent pick up
       // our non-zero exit status.
       exit (errno);
@@ -192,11 +240,14 @@ Program::ExecuteAndWait(const Path& path,
 
       // Wait for child to die
       if (wait(&status) != child)
-        ThrowErrno("Child timedout but wouldn't die");
-        
+        MakeErrMsg(ErrMsg, "Child timed out but wouldn't die");
+      else
+        MakeErrMsg(ErrMsg, "Child timed out", 0);
+
       return -1;   // Timeout detected
-    } else {
-      ThrowErrno("Error waiting for child process");
+    } else if (errno != EINTR) {
+      MakeErrMsg(ErrMsg, "Error waiting for child process");
+      return -1;
     }
 
   // We exited normally without timeout, so turn off the timer.
@@ -223,12 +274,14 @@ Program::ExecuteAndWait(const Path& path,
     
 }
 
-void Program::ChangeStdinToBinary(){
+bool Program::ChangeStdinToBinary(){
   // Do nothing, as Unix doesn't differentiate between text and binary.
+  return false;
 }
 
-void Program::ChangeStdoutToBinary(){
+bool Program::ChangeStdoutToBinary(){
   // Do nothing, as Unix doesn't differentiate between text and binary.
+  return false;
 }
 
 }