For PR351:
authorReid Spencer <rspencer@reidspencer.com>
Sun, 19 Dec 2004 18:00:09 +0000 (18:00 +0000)
committerReid Spencer <rspencer@reidspencer.com>
Sun, 19 Dec 2004 18:00:09 +0000 (18:00 +0000)
* Remove unneeded header files.
* Move RedirectFD static function to lib/System/Unix/Program.cpp
* Delete RunProgramWithTimeout, now implemented by
  sys::Program::ExecuteAndWait. RunProgramWithTimeout is now a convenience func.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@19040 91177308-0d34-0410-b5e6-96231b3b80d8

lib/Support/SystemUtils.cpp

index 72e4e917efb8e27c01e67201780302f26623dc49..4213b84b8bde374dfe5c703c632804b3cc2ee6b8 100644 (file)
 
 #include "llvm/Support/SystemUtils.h"
 #include "llvm/System/Program.h"
-#include "llvm/Config/fcntl.h"
-#include "llvm/Config/sys/wait.h"
-#include <algorithm>
-#include <cerrno>
-#include <cstdlib>
-#include <fstream>
-#include <iostream>
-#include <signal.h>
 
 using namespace llvm;
 
@@ -41,122 +33,20 @@ bool llvm::isStandardOutAConsole() {
 /// into the same directory, but that directory is neither the current
 /// directory, nor in the PATH.  If the executable cannot be found, return an
 /// empty string.
-/// 
+///
 #undef FindExecutable   // needed on windows :(
 sys::Path llvm::FindExecutable(const std::string &ExeName,
-                                 const std::string &ProgramPath) {
-  // First check the directory that the calling program is in.  We can do this 
+                               const std::string &ProgramPath) {
+  // First check the directory that the calling program is in.  We can do this  
   // if ProgramPath contains at least one / character, indicating that it is a
   // relative path to bugpoint itself.
-  //
   sys::Path Result ( ProgramPath );
   Result.elideFile();
-
   if (!Result.isEmpty()) {
     Result.appendFile(ExeName);
-    if (Result.executable()) return Result;
+    if (Result.executable()) 
+      return Result;
   }
 
   return sys::Program::FindProgramByName(ExeName);
 }
-
-static void RedirectFD(const std::string &File, int FD) {
-  if (File.empty()) return;  // Noop
-
-  // Open the file
-  int InFD = open(File.c_str(), FD == 0 ? O_RDONLY : O_WRONLY|O_CREAT, 0666);
-  if (InFD == -1) {
-    std::cerr << "Error opening file '" << File << "' for "
-              << (FD == 0 ? "input" : "output") << "!\n";
-    exit(1);
-  }
-
-  dup2(InFD, FD);   // Install it as the requested FD
-  close(InFD);      // Close the original FD
-}
-
-static bool Timeout = false;
-static void TimeOutHandler(int Sig) {
-  Timeout = true;
-}
-
-/// RunProgramWithTimeout - This function executes the specified program, with
-/// the specified null-terminated argument array, with the stdin/out/err fd's
-/// redirected, with a timeout specified by the last argument.  This terminates
-/// the calling program if there is an error executing the specified program.
-/// It returns the return value of the program, or -1 if a timeout is detected.
-///
-int llvm::RunProgramWithTimeout(const std::string &ProgramPath,
-                                const char **Args,
-                                const std::string &StdInFile,
-                                const std::string &StdOutFile,
-                                const std::string &StdErrFile,
-                                unsigned NumSeconds) {
-#ifdef HAVE_SYS_WAIT_H
-  int Child = fork();
-  switch (Child) {
-  case -1:
-    std::cerr << "ERROR forking!\n";
-    exit(1);
-  case 0:               // Child
-    RedirectFD(StdInFile, 0);      // Redirect file descriptors...
-    RedirectFD(StdOutFile, 1);
-    if (StdOutFile != StdErrFile)
-      RedirectFD(StdErrFile, 2);
-    else
-      dup2(1, 2);
-
-    execv(ProgramPath.c_str(), (char *const *)Args);
-    std::cerr << "Error executing program: '" << ProgramPath;
-    for (; *Args; ++Args)
-      std::cerr << " " << *Args;
-    std::cerr << "'\n";
-    exit(1);
-
-  default: break;
-  }
-
-  // Make sure all output has been written while waiting
-  std::cout << std::flush;
-
-  // Install a timeout handler.
-  Timeout = false;
-  struct sigaction Act, Old;
-  Act.sa_sigaction = 0;
-  Act.sa_handler = TimeOutHandler;
-  sigemptyset(&Act.sa_mask);
-  Act.sa_flags = 0;
-  sigaction(SIGALRM, &Act, &Old);
-
-  // Set the timeout if one is set.
-  if (NumSeconds)
-    alarm(NumSeconds);
-
-  int Status;
-  while (wait(&Status) != Child)
-    if (errno == EINTR) {
-      if (Timeout) {
-        // Kill the child.
-        kill(Child, SIGKILL);
-        
-        if (wait(&Status) != Child)
-          std::cerr << "Something funny happened waiting for the child!\n";
-        
-        alarm(0);
-        sigaction(SIGALRM, &Old, 0);
-        return -1;   // Timeout detected
-      } else {
-        std::cerr << "Error waiting for child process!\n";
-        exit(1);
-      }
-    }
-
-  alarm(0);
-  sigaction(SIGALRM, &Old, 0);
-  return Status;
-
-#else
-  std::cerr << "RunProgramWithTimeout not implemented on this platform!\n";
-  return -1;
-#endif
-}