Chris Lattner | 4a10645 | 2002-12-23 23:50:16 +0000 | [diff] [blame] | 1 | //===- SystemUtils.h - Utilities to do low-level system stuff --*- C++ -*--===// |
John Criswell | b576c94 | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file was developed by the LLVM research group and is distributed under |
| 6 | // the University of Illinois Open Source License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
Chris Lattner | 4a10645 | 2002-12-23 23:50:16 +0000 | [diff] [blame] | 9 | // |
| 10 | // This file contains functions used to do a variety of low-level, often |
| 11 | // system-specific, tasks. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
Misha Brukman | 3d1b0c7 | 2003-08-07 21:28:50 +0000 | [diff] [blame] | 15 | #include "Support/SystemUtils.h" |
John Criswell | 7a73b80 | 2003-06-30 21:59:07 +0000 | [diff] [blame] | 16 | #include "Config/sys/types.h" |
| 17 | #include "Config/sys/stat.h" |
| 18 | #include "Config/fcntl.h" |
| 19 | #include "Config/sys/wait.h" |
| 20 | #include "Config/unistd.h" |
Chris Lattner | 74b1f45 | 2004-01-10 19:15:14 +0000 | [diff] [blame] | 21 | #include <algorithm> |
| 22 | #include <fstream> |
| 23 | #include <iostream> |
| 24 | #include <cstdlib> |
| 25 | #include <cerrno> |
Chris Lattner | 2cdd21c | 2003-12-14 21:35:53 +0000 | [diff] [blame] | 26 | using namespace llvm; |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 27 | |
Chris Lattner | 4a10645 | 2002-12-23 23:50:16 +0000 | [diff] [blame] | 28 | /// isExecutableFile - This function returns true if the filename specified |
| 29 | /// exists and is executable. |
| 30 | /// |
Chris Lattner | 2cdd21c | 2003-12-14 21:35:53 +0000 | [diff] [blame] | 31 | bool llvm::isExecutableFile(const std::string &ExeFileName) { |
Chris Lattner | 4a10645 | 2002-12-23 23:50:16 +0000 | [diff] [blame] | 32 | struct stat Buf; |
| 33 | if (stat(ExeFileName.c_str(), &Buf)) |
| 34 | return false; // Must not be executable! |
| 35 | |
| 36 | if (!(Buf.st_mode & S_IFREG)) |
| 37 | return false; // Not a regular file? |
| 38 | |
| 39 | if (Buf.st_uid == getuid()) // Owner of file? |
| 40 | return Buf.st_mode & S_IXUSR; |
| 41 | else if (Buf.st_gid == getgid()) // In group of file? |
| 42 | return Buf.st_mode & S_IXGRP; |
| 43 | else // Unrelated to file? |
| 44 | return Buf.st_mode & S_IXOTH; |
| 45 | } |
| 46 | |
Chris Lattner | b234d46 | 2004-04-02 05:04:03 +0000 | [diff] [blame] | 47 | /// isStandardOutAConsole - Return true if we can tell that the standard output |
| 48 | /// stream goes to a terminal window or console. |
| 49 | bool llvm::isStandardOutAConsole() { |
| 50 | // FIXME: if we don't have isatty, just return false. |
| 51 | return isatty(1); |
| 52 | } |
| 53 | |
| 54 | |
Misha Brukman | f7066c7 | 2003-08-07 21:34:25 +0000 | [diff] [blame] | 55 | /// FindExecutable - Find a named executable, giving the argv[0] of program |
Misha Brukman | 44f8a34 | 2003-09-29 22:40:07 +0000 | [diff] [blame] | 56 | /// being executed. This allows us to find another LLVM tool if it is built |
| 57 | /// into the same directory, but that directory is neither the current |
| 58 | /// directory, nor in the PATH. If the executable cannot be found, return an |
| 59 | /// empty string. |
Misha Brukman | f7066c7 | 2003-08-07 21:34:25 +0000 | [diff] [blame] | 60 | /// |
Chris Lattner | 2cdd21c | 2003-12-14 21:35:53 +0000 | [diff] [blame] | 61 | std::string llvm::FindExecutable(const std::string &ExeName, |
| 62 | const std::string &ProgramPath) { |
Chris Lattner | 4a10645 | 2002-12-23 23:50:16 +0000 | [diff] [blame] | 63 | // First check the directory that bugpoint is in. We can do this if |
| 64 | // BugPointPath contains at least one / character, indicating that it is a |
| 65 | // relative path to bugpoint itself. |
| 66 | // |
Misha Brukman | 35d402f | 2003-08-07 21:33:33 +0000 | [diff] [blame] | 67 | std::string Result = ProgramPath; |
Chris Lattner | 4a10645 | 2002-12-23 23:50:16 +0000 | [diff] [blame] | 68 | while (!Result.empty() && Result[Result.size()-1] != '/') |
| 69 | Result.erase(Result.size()-1, 1); |
| 70 | |
| 71 | if (!Result.empty()) { |
| 72 | Result += ExeName; |
| 73 | if (isExecutableFile(Result)) return Result; // Found it? |
| 74 | } |
| 75 | |
Misha Brukman | 35d402f | 2003-08-07 21:33:33 +0000 | [diff] [blame] | 76 | // Okay, if the path to the program didn't tell us anything, try using the |
| 77 | // PATH environment variable. |
Chris Lattner | 4a10645 | 2002-12-23 23:50:16 +0000 | [diff] [blame] | 78 | const char *PathStr = getenv("PATH"); |
| 79 | if (PathStr == 0) return ""; |
| 80 | |
Misha Brukman | bc0e998 | 2003-07-14 17:20:40 +0000 | [diff] [blame] | 81 | // Now we have a colon separated list of directories to search... try them... |
Chris Lattner | 4a10645 | 2002-12-23 23:50:16 +0000 | [diff] [blame] | 82 | unsigned PathLen = strlen(PathStr); |
| 83 | while (PathLen) { |
| 84 | // Find the first colon... |
| 85 | const char *Colon = std::find(PathStr, PathStr+PathLen, ':'); |
| 86 | |
| 87 | // Check to see if this first directory contains the executable... |
| 88 | std::string FilePath = std::string(PathStr, Colon) + '/' + ExeName; |
| 89 | if (isExecutableFile(FilePath)) |
| 90 | return FilePath; // Found the executable! |
| 91 | |
| 92 | // Nope it wasn't in this directory, check the next range! |
| 93 | PathLen -= Colon-PathStr; |
| 94 | PathStr = Colon; |
| 95 | while (*PathStr == ':') { // Advance past colons |
| 96 | PathStr++; |
| 97 | PathLen--; |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | // If we fell out, we ran out of directories in PATH to search, return failure |
| 102 | return ""; |
| 103 | } |
| 104 | |
| 105 | static void RedirectFD(const std::string &File, int FD) { |
| 106 | if (File.empty()) return; // Noop |
| 107 | |
| 108 | // Open the file |
| 109 | int InFD = open(File.c_str(), FD == 0 ? O_RDONLY : O_WRONLY|O_CREAT, 0666); |
| 110 | if (InFD == -1) { |
| 111 | std::cerr << "Error opening file '" << File << "' for " |
Misha Brukman | 44f8a34 | 2003-09-29 22:40:07 +0000 | [diff] [blame] | 112 | << (FD == 0 ? "input" : "output") << "!\n"; |
Chris Lattner | 4a10645 | 2002-12-23 23:50:16 +0000 | [diff] [blame] | 113 | exit(1); |
| 114 | } |
| 115 | |
| 116 | dup2(InFD, FD); // Install it as the requested FD |
| 117 | close(InFD); // Close the original FD |
| 118 | } |
| 119 | |
| 120 | /// RunProgramWithTimeout - This function executes the specified program, with |
| 121 | /// the specified null-terminated argument array, with the stdin/out/err fd's |
Misha Brukman | 950971d | 2003-09-16 15:31:46 +0000 | [diff] [blame] | 122 | /// redirected, with a timeout specified on the command line. This terminates |
Chris Lattner | 4a10645 | 2002-12-23 23:50:16 +0000 | [diff] [blame] | 123 | /// the calling program if there is an error executing the specified program. |
| 124 | /// It returns the return value of the program, or -1 if a timeout is detected. |
| 125 | /// |
Chris Lattner | 2cdd21c | 2003-12-14 21:35:53 +0000 | [diff] [blame] | 126 | int llvm::RunProgramWithTimeout(const std::string &ProgramPath, |
| 127 | const char **Args, |
| 128 | const std::string &StdInFile, |
| 129 | const std::string &StdOutFile, |
| 130 | const std::string &StdErrFile) { |
Chris Lattner | 4a10645 | 2002-12-23 23:50:16 +0000 | [diff] [blame] | 131 | // FIXME: install sigalarm handler here for timeout... |
| 132 | |
| 133 | int Child = fork(); |
| 134 | switch (Child) { |
| 135 | case -1: |
| 136 | std::cerr << "ERROR forking!\n"; |
| 137 | exit(1); |
| 138 | case 0: // Child |
| 139 | RedirectFD(StdInFile, 0); // Redirect file descriptors... |
| 140 | RedirectFD(StdOutFile, 1); |
| 141 | RedirectFD(StdErrFile, 2); |
| 142 | |
| 143 | execv(ProgramPath.c_str(), (char *const *)Args); |
Brian Gaeke | 53e557d | 2003-10-15 20:46:58 +0000 | [diff] [blame] | 144 | std::cerr << "Error executing program: '" << ProgramPath; |
Chris Lattner | 4a10645 | 2002-12-23 23:50:16 +0000 | [diff] [blame] | 145 | for (; *Args; ++Args) |
| 146 | std::cerr << " " << *Args; |
Brian Gaeke | 53e557d | 2003-10-15 20:46:58 +0000 | [diff] [blame] | 147 | std::cerr << "'\n"; |
Chris Lattner | 4a10645 | 2002-12-23 23:50:16 +0000 | [diff] [blame] | 148 | exit(1); |
| 149 | |
| 150 | default: break; |
| 151 | } |
| 152 | |
| 153 | // Make sure all output has been written while waiting |
| 154 | std::cout << std::flush; |
| 155 | |
| 156 | int Status; |
| 157 | if (wait(&Status) != Child) { |
| 158 | if (errno == EINTR) { |
| 159 | static bool FirstTimeout = true; |
| 160 | if (FirstTimeout) { |
Misha Brukman | 44f8a34 | 2003-09-29 22:40:07 +0000 | [diff] [blame] | 161 | std::cout << |
Chris Lattner | 4a10645 | 2002-12-23 23:50:16 +0000 | [diff] [blame] | 162 | "*** Program execution timed out! This mechanism is designed to handle\n" |
| 163 | " programs stuck in infinite loops gracefully. The -timeout option\n" |
| 164 | " can be used to change the timeout threshold or disable it completely\n" |
| 165 | " (with -timeout=0). This message is only displayed once.\n"; |
Misha Brukman | 44f8a34 | 2003-09-29 22:40:07 +0000 | [diff] [blame] | 166 | FirstTimeout = false; |
Chris Lattner | 4a10645 | 2002-12-23 23:50:16 +0000 | [diff] [blame] | 167 | } |
| 168 | return -1; // Timeout detected |
| 169 | } |
| 170 | |
| 171 | std::cerr << "Error waiting for child process!\n"; |
| 172 | exit(1); |
| 173 | } |
| 174 | return Status; |
| 175 | } |
John Criswell | 5afb5f6 | 2003-09-17 15:13:59 +0000 | [diff] [blame] | 176 | |
| 177 | |
| 178 | // |
| 179 | // Function: ExecWait () |
| 180 | // |
| 181 | // Description: |
| 182 | // This function executes a program with the specified arguments and |
| 183 | // environment. It then waits for the progarm to termiante and then returns |
| 184 | // to the caller. |
| 185 | // |
| 186 | // Inputs: |
| 187 | // argv - The arguments to the program as an array of C strings. The first |
| 188 | // argument should be the name of the program to execute, and the |
| 189 | // last argument should be a pointer to NULL. |
| 190 | // |
| 191 | // envp - The environment passes to the program as an array of C strings in |
| 192 | // the form of "name=value" pairs. The last element should be a |
| 193 | // pointer to NULL. |
| 194 | // |
| 195 | // Outputs: |
| 196 | // None. |
| 197 | // |
| 198 | // Return value: |
| 199 | // 0 - No errors. |
| 200 | // 1 - The program could not be executed. |
| 201 | // 1 - The program returned a non-zero exit status. |
| 202 | // 1 - The program terminated abnormally. |
| 203 | // |
| 204 | // Notes: |
| 205 | // The program will inherit the stdin, stdout, and stderr file descriptors |
| 206 | // as well as other various configuration settings (umask). |
| 207 | // |
| 208 | // This function should not print anything to stdout/stderr on its own. It is |
| 209 | // a generic library function. The caller or executed program should report |
| 210 | // errors in the way it sees fit. |
| 211 | // |
John Criswell | e5b3e15 | 2003-09-17 19:02:49 +0000 | [diff] [blame] | 212 | // This function does not use $PATH to find programs. |
| 213 | // |
Chris Lattner | 2cdd21c | 2003-12-14 21:35:53 +0000 | [diff] [blame] | 214 | int llvm::ExecWait(const char * const old_argv[], |
| 215 | const char * const old_envp[]) { |
John Criswell | 5afb5f6 | 2003-09-17 15:13:59 +0000 | [diff] [blame] | 216 | // Child process ID |
| 217 | register int child; |
| 218 | |
| 219 | // Status from child process when it exits |
| 220 | int status; |
| 221 | |
| 222 | // |
John Criswell | e5b3e15 | 2003-09-17 19:02:49 +0000 | [diff] [blame] | 223 | // Create local versions of the parameters that can be passed into execve() |
| 224 | // without creating const problems. |
John Criswell | 5afb5f6 | 2003-09-17 15:13:59 +0000 | [diff] [blame] | 225 | // |
John Criswell | e5b3e15 | 2003-09-17 19:02:49 +0000 | [diff] [blame] | 226 | char ** const argv = (char ** const) old_argv; |
| 227 | char ** const envp = (char ** const) old_envp; |
John Criswell | 5afb5f6 | 2003-09-17 15:13:59 +0000 | [diff] [blame] | 228 | |
| 229 | // |
| 230 | // Create a child process. |
| 231 | // |
| 232 | switch (child=fork()) |
| 233 | { |
John Criswell | e5b3e15 | 2003-09-17 19:02:49 +0000 | [diff] [blame] | 234 | // |
| 235 | // An error occured: Return to the caller. |
| 236 | // |
John Criswell | 5afb5f6 | 2003-09-17 15:13:59 +0000 | [diff] [blame] | 237 | case -1: |
| 238 | return 1; |
| 239 | break; |
| 240 | |
John Criswell | e5b3e15 | 2003-09-17 19:02:49 +0000 | [diff] [blame] | 241 | // |
| 242 | // Child process: Execute the program. |
| 243 | // |
John Criswell | 5afb5f6 | 2003-09-17 15:13:59 +0000 | [diff] [blame] | 244 | case 0: |
John Criswell | e5b3e15 | 2003-09-17 19:02:49 +0000 | [diff] [blame] | 245 | execve (argv[0], argv, envp); |
| 246 | |
| 247 | // |
| 248 | // If the execve() failed, we should exit and let the parent pick up |
| 249 | // our non-zero exit status. |
| 250 | // |
| 251 | exit (1); |
John Criswell | 5afb5f6 | 2003-09-17 15:13:59 +0000 | [diff] [blame] | 252 | break; |
| 253 | |
John Criswell | e5b3e15 | 2003-09-17 19:02:49 +0000 | [diff] [blame] | 254 | // |
| 255 | // Parent process: Break out of the switch to do our processing. |
| 256 | // |
John Criswell | 5afb5f6 | 2003-09-17 15:13:59 +0000 | [diff] [blame] | 257 | default: |
| 258 | break; |
| 259 | } |
| 260 | |
| 261 | // |
| 262 | // Parent process: Wait for the child process to termiante. |
| 263 | // |
| 264 | if ((wait (&status)) == -1) |
| 265 | { |
| 266 | return 1; |
| 267 | } |
| 268 | |
| 269 | // |
| 270 | // If the program exited normally with a zero exit status, return success! |
| 271 | // |
| 272 | if (WIFEXITED (status) && (WEXITSTATUS(status) == 0)) |
| 273 | { |
| 274 | return 0; |
| 275 | } |
| 276 | |
| 277 | // |
| 278 | // Otherwise, return failure. |
| 279 | // |
| 280 | return 1; |
| 281 | } |