[llvm-commits] CVS: llvm/lib/System/Unix/Program.inc Unix.h

Reid Spencer reid at x10sys.com
Sun Aug 20 23:03:01 PDT 2006



Changes in directory llvm/lib/System/Unix:

Program.inc updated: 1.20 -> 1.21
Unix.h updated: 1.15 -> 1.16
---
Log message:

For PR797: http://llvm.org/PR797 :
Remove all exception code from Program.inc and implement its new interface
with an ErrMsg string argument. 


---
Diffs of the changes:  (+67 -23)

 Program.inc |   59 ++++++++++++++++++++++++++++++++++++-----------------------
 Unix.h      |   31 +++++++++++++++++++++++++++++++
 2 files changed, 67 insertions(+), 23 deletions(-)


Index: llvm/lib/System/Unix/Program.inc
diff -u llvm/lib/System/Unix/Program.inc:1.20 llvm/lib/System/Unix/Program.inc:1.21
--- llvm/lib/System/Unix/Program.inc:1.20	Sun Aug 20 21:04:43 2006
+++ llvm/lib/System/Unix/Program.inc	Mon Aug 21 01:02:44 2006
@@ -81,18 +81,24 @@
   return Path();
 }
 
-static void RedirectFD(const std::string &File, int FD) {
-  if (File.empty()) return;  // Noop
+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) {
-    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;
@@ -105,10 +111,14 @@
                         const char** args,
                         const char** envp,
                         const Path** redirects,
-                        unsigned secondsToWait
-) {
-  if (!path.canExecute())
-    return -9999;
+                        unsigned secondsToWait,
+                        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,9 +126,8 @@
   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: {
@@ -126,22 +135,23 @@
       if (redirects) {
         if (redirects[0])
           if (redirects[0]->isEmpty())
-            RedirectFD("/dev/null",0);
+            if (RedirectFD("/dev/null",0,ErrMsg)) { return -1; }
           else
-            RedirectFD(redirects[0]->toString(), 0);
+            if (RedirectFD(redirects[0]->toString(), 0,ErrMsg)) { return -1; }
         if (redirects[1])
           if (redirects[1]->isEmpty())
-            RedirectFD("/dev/null",1);
+            if (RedirectFD("/dev/null",1,ErrMsg)) { return -1; }
           else
-            RedirectFD(redirects[1]->toString(), 1);
+            if (RedirectFD(redirects[1]->toString(),1,ErrMsg)) { return -1; }
         if (redirects[1] && redirects[2] && 
             *(redirects[1]) != *(redirects[2])) {
           if (redirects[2]->isEmpty())
-            RedirectFD("/dev/null",2);
+            if (RedirectFD("/dev/null",2,ErrMsg)) { return -1; }
           else
-            RedirectFD(redirects[2]->toString(), 2);
-        } else {
-          dup2(1, 2);
+            if (RedirectFD(redirects[2]->toString(), 2,ErrMsg)) { return -1; }
+        } else if (-1 == dup2(1,2)) {
+          MakeErrMsg(ErrMsg, "Can't redirect");
+          return -1;
         }
       }
 
@@ -192,11 +202,12 @@
 
       // 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");
         
       return -1;   // Timeout detected
     } else {
-      ThrowErrno("Error waiting for child process");
+      MakeErrMsg(ErrMsg, "Error waiting for child process");
+      return -1;
     }
 
   // We exited normally without timeout, so turn off the timer.
@@ -223,12 +234,14 @@
     
 }
 
-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;
 }
 
 }


Index: llvm/lib/System/Unix/Unix.h
diff -u llvm/lib/System/Unix/Unix.h:1.15 llvm/lib/System/Unix/Unix.h:1.16
--- llvm/lib/System/Unix/Unix.h:1.15	Fri Jul  7 12:32:37 2006
+++ llvm/lib/System/Unix/Unix.h	Mon Aug 21 01:02:44 2006
@@ -119,4 +119,35 @@
   throw prefix + ": " + buffer;
 }
 
+/// This function builds an error message into \p ErrMsg using the \p prefix
+/// string and the Unix error number given by \p errnum. If errnum is -1, the
+/// default then the value of errno is used.
+/// @brief Make an error message
+inline void MakeErrMsg(
+  std::string* ErrMsg, const std::string& prefix, int errnum = -1) {
+  if (!ErrMsg)
+    return;
+  char buffer[MAXPATHLEN];
+  buffer[0] = 0;
+  if (errnum == -1)
+    errnum = errno;
+#ifdef HAVE_STRERROR_R
+  // strerror_r is thread-safe.
+  if (errnum)
+    strerror_r(errnum,buffer,MAXPATHLEN-1);
+#elif HAVE_STRERROR
+  // Copy the thread un-safe result of strerror into
+  // the buffer as fast as possible to minimize impact
+  // of collision of strerror in multiple threads.
+  if (errnum)
+    strncpy(buffer,strerror(errnum),MAXPATHLEN-1);
+  buffer[MAXPATHLEN-1] = 0;
+#else
+  // Strange that this system doesn't even have strerror
+  // but, oh well, just use a generic message
+  sprintf(buffer, "Error #%d", errnum);
+#endif
+  *ErrMsg = buffer;
+}
+
 #endif






More information about the llvm-commits mailing list