LLVM API Documentation
00001 //===- llvm/System/Unix/Program.cpp -----------------------------*- C++ -*-===// 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file was developed by Reid Spencer and is distributed under the 00006 // University of Illinois Open Source License. See LICENSE.TXT for details. 00007 // 00008 //===----------------------------------------------------------------------===// 00009 // 00010 // This file implements the Unix specific portion of the Program class. 00011 // 00012 //===----------------------------------------------------------------------===// 00013 00014 //===----------------------------------------------------------------------===// 00015 //=== WARNING: Implementation here must contain only generic UNIX code that 00016 //=== is guaranteed to work on *all* UNIX variants. 00017 //===----------------------------------------------------------------------===// 00018 00019 #include <llvm/Config/config.h> 00020 #include "Unix.h" 00021 #include <sys/stat.h> 00022 #include <fcntl.h> 00023 #ifdef HAVE_SYS_WAIT_H 00024 #include <sys/wait.h> 00025 #endif 00026 00027 extern char** environ; 00028 00029 namespace llvm { 00030 using namespace sys; 00031 00032 // This function just uses the PATH environment variable to find the program. 00033 Path 00034 Program::FindProgramByName(const std::string& progName) { 00035 00036 // Check some degenerate cases 00037 if (progName.length() == 0) // no program 00038 return Path(); 00039 Path temp; 00040 if (!temp.setFile(progName)) // invalid name 00041 return Path(); 00042 if (temp.executable()) // already executable as is 00043 return temp; 00044 00045 // At this point, the file name is valid and its not executable 00046 00047 // Get the path. If its empty, we can't do anything to find it. 00048 const char *PathStr = getenv("PATH"); 00049 if (PathStr == 0) 00050 return Path(); 00051 00052 // Now we have a colon separated list of directories to search; try them. 00053 unsigned PathLen = strlen(PathStr); 00054 while (PathLen) { 00055 // Find the first colon... 00056 const char *Colon = std::find(PathStr, PathStr+PathLen, ':'); 00057 00058 // Check to see if this first directory contains the executable... 00059 Path FilePath; 00060 if (FilePath.setDirectory(std::string(PathStr,Colon))) { 00061 FilePath.appendFile(progName); 00062 if (FilePath.executable()) 00063 return FilePath; // Found the executable! 00064 } 00065 00066 // Nope it wasn't in this directory, check the next path in the list! 00067 PathLen -= Colon-PathStr; 00068 PathStr = Colon; 00069 00070 // Advance past duplicate colons 00071 while (*PathStr == ':') { 00072 PathStr++; 00073 PathLen--; 00074 } 00075 } 00076 return Path(); 00077 } 00078 00079 // 00080 int 00081 Program::ExecuteAndWait(const Path& path, 00082 const std::vector<std::string>& args) { 00083 if (!path.executable()) 00084 throw path.get() + " is not executable"; 00085 00086 #ifdef HAVE_SYS_WAIT_H 00087 // Create local versions of the parameters that can be passed into execve() 00088 // without creating const problems. 00089 const char* argv[ args.size() + 2 ]; 00090 unsigned index = 0; 00091 std::string progname(path.getLast()); 00092 argv[index++] = progname.c_str(); 00093 for (unsigned i = 0; i < args.size(); i++) 00094 argv[index++] = args[i].c_str(); 00095 argv[index] = 0; 00096 00097 // Create a child process. 00098 switch (fork()) { 00099 // An error occured: Return to the caller. 00100 case -1: 00101 ThrowErrno(std::string("Couldn't execute program '") + path.get() + "'"); 00102 break; 00103 00104 // Child process: Execute the program. 00105 case 0: 00106 execve (path.c_str(), (char** const)argv, environ); 00107 // If the execve() failed, we should exit and let the parent pick up 00108 // our non-zero exit status. 00109 exit (errno); 00110 00111 // Parent process: Break out of the switch to do our processing. 00112 default: 00113 break; 00114 } 00115 00116 // Parent process: Wait for the child process to terminate. 00117 int status; 00118 if ((::wait (&status)) == -1) 00119 ThrowErrno(std::string("Failed waiting for program '") + path.get() + "'"); 00120 00121 // If the program exited normally with a zero exit status, return success! 00122 if (WIFEXITED (status)) 00123 return WEXITSTATUS(status); 00124 else if (WIFSIGNALED(status)) 00125 throw std::string("Program '") + path.get() + "' received terminating signal."; 00126 else 00127 return 0; 00128 00129 #else 00130 throw std::string("Program::ExecuteAndWait not implemented on this platform!\n"); 00131 #endif 00132 00133 } 00134 00135 } 00136 // vim: sw=2 smartindent smarttab tw=80 autoindent expandtab