Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1 | //===- llvm/System/Unix/Program.cpp -----------------------------*- C++ -*-===// |
Mikhail Glushenkov | 63b88c7 | 2009-07-17 20:38:17 +0000 | [diff] [blame] | 2 | // |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 081ce94 | 2007-12-29 20:36:04 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Mikhail Glushenkov | 63b88c7 | 2009-07-17 20:38:17 +0000 | [diff] [blame] | 7 | // |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file implements the Unix specific portion of the Program class. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | //===----------------------------------------------------------------------===// |
| 15 | //=== WARNING: Implementation here must contain only generic UNIX code that |
| 16 | //=== is guaranteed to work on *all* UNIX variants. |
| 17 | //===----------------------------------------------------------------------===// |
| 18 | |
| 19 | #include <llvm/Config/config.h> |
| 20 | #include "Unix.h" |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 21 | #if HAVE_SYS_STAT_H |
| 22 | #include <sys/stat.h> |
| 23 | #endif |
| 24 | #if HAVE_SYS_RESOURCE_H |
| 25 | #include <sys/resource.h> |
| 26 | #endif |
| 27 | #if HAVE_SIGNAL_H |
| 28 | #include <signal.h> |
| 29 | #endif |
| 30 | #if HAVE_FCNTL_H |
| 31 | #include <fcntl.h> |
| 32 | #endif |
| 33 | |
| 34 | namespace llvm { |
| 35 | using namespace sys; |
| 36 | |
Daniel Dunbar | 1b39906 | 2009-08-03 05:02:46 +0000 | [diff] [blame] | 37 | Program::Program() : Pid_(0) {} |
| 38 | |
| 39 | Program::~Program() {} |
| 40 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 41 | // This function just uses the PATH environment variable to find the program. |
| 42 | Path |
| 43 | Program::FindProgramByName(const std::string& progName) { |
| 44 | |
| 45 | // Check some degenerate cases |
| 46 | if (progName.length() == 0) // no program |
| 47 | return Path(); |
| 48 | Path temp; |
| 49 | if (!temp.set(progName)) // invalid name |
| 50 | return Path(); |
Benjamin Kramer | 9cd3dfe | 2009-07-28 22:08:15 +0000 | [diff] [blame] | 51 | // Use the given path verbatim if it contains any slashes; this matches |
| 52 | // the behavior of sh(1) and friends. |
Dan Gohman | 33bff13 | 2009-07-28 23:25:18 +0000 | [diff] [blame] | 53 | if (progName.find('/') != std::string::npos) |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 54 | return temp; |
| 55 | |
| 56 | // At this point, the file name is valid and its not executable |
Mikhail Glushenkov | 63b88c7 | 2009-07-17 20:38:17 +0000 | [diff] [blame] | 57 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 58 | // Get the path. If its empty, we can't do anything to find it. |
| 59 | const char *PathStr = getenv("PATH"); |
Mikhail Glushenkov | 63b88c7 | 2009-07-17 20:38:17 +0000 | [diff] [blame] | 60 | if (PathStr == 0) |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 61 | return Path(); |
| 62 | |
| 63 | // Now we have a colon separated list of directories to search; try them. |
Evan Cheng | 591bfc8 | 2008-05-05 18:30:58 +0000 | [diff] [blame] | 64 | size_t PathLen = strlen(PathStr); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 65 | while (PathLen) { |
| 66 | // Find the first colon... |
| 67 | const char *Colon = std::find(PathStr, PathStr+PathLen, ':'); |
| 68 | |
| 69 | // Check to see if this first directory contains the executable... |
| 70 | Path FilePath; |
| 71 | if (FilePath.set(std::string(PathStr,Colon))) { |
| 72 | FilePath.appendComponent(progName); |
| 73 | if (FilePath.canExecute()) |
| 74 | return FilePath; // Found the executable! |
| 75 | } |
| 76 | |
| 77 | // Nope it wasn't in this directory, check the next path in the list! |
| 78 | PathLen -= Colon-PathStr; |
| 79 | PathStr = Colon; |
| 80 | |
| 81 | // Advance past duplicate colons |
| 82 | while (*PathStr == ':') { |
| 83 | PathStr++; |
| 84 | PathLen--; |
| 85 | } |
| 86 | } |
| 87 | return Path(); |
| 88 | } |
| 89 | |
Matthijs Kooijman | 641150d | 2008-06-12 10:47:18 +0000 | [diff] [blame] | 90 | static bool RedirectIO(const Path *Path, int FD, std::string* ErrMsg) { |
| 91 | if (Path == 0) |
| 92 | // Noop |
| 93 | return false; |
| 94 | std::string File; |
| 95 | if (Path->isEmpty()) |
| 96 | // Redirect empty paths to /dev/null |
| 97 | File = "/dev/null"; |
| 98 | else |
| 99 | File = Path->toString(); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 100 | |
| 101 | // Open the file |
| 102 | int InFD = open(File.c_str(), FD == 0 ? O_RDONLY : O_WRONLY|O_CREAT, 0666); |
| 103 | if (InFD == -1) { |
| 104 | MakeErrMsg(ErrMsg, "Cannot open file '" + File + "' for " |
Daniel Dunbar | f7ea85d | 2009-04-20 20:50:13 +0000 | [diff] [blame] | 105 | + (FD == 0 ? "input" : "output")); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 106 | return true; |
| 107 | } |
| 108 | |
| 109 | // Install it as the requested FD |
| 110 | if (-1 == dup2(InFD, FD)) { |
| 111 | MakeErrMsg(ErrMsg, "Cannot dup2"); |
| 112 | return true; |
| 113 | } |
| 114 | close(InFD); // Close the original FD |
| 115 | return false; |
| 116 | } |
| 117 | |
| 118 | static bool Timeout = false; |
| 119 | static void TimeOutHandler(int Sig) { |
| 120 | Timeout = true; |
| 121 | } |
| 122 | |
| 123 | static void SetMemoryLimits (unsigned size) |
| 124 | { |
| 125 | #if HAVE_SYS_RESOURCE_H |
| 126 | struct rlimit r; |
| 127 | __typeof__ (r.rlim_cur) limit = (__typeof__ (r.rlim_cur)) (size) * 1048576; |
| 128 | |
| 129 | // Heap size |
| 130 | getrlimit (RLIMIT_DATA, &r); |
| 131 | r.rlim_cur = limit; |
| 132 | setrlimit (RLIMIT_DATA, &r); |
| 133 | #ifdef RLIMIT_RSS |
| 134 | // Resident set size. |
| 135 | getrlimit (RLIMIT_RSS, &r); |
| 136 | r.rlim_cur = limit; |
| 137 | setrlimit (RLIMIT_RSS, &r); |
| 138 | #endif |
| 139 | #ifdef RLIMIT_AS // e.g. NetBSD doesn't have it. |
| 140 | // Virtual memory. |
| 141 | getrlimit (RLIMIT_AS, &r); |
| 142 | r.rlim_cur = limit; |
| 143 | setrlimit (RLIMIT_AS, &r); |
| 144 | #endif |
| 145 | #endif |
| 146 | } |
| 147 | |
Mikhail Glushenkov | b23120b | 2009-07-18 21:43:12 +0000 | [diff] [blame] | 148 | bool |
| 149 | Program::Execute(const Path& path, |
| 150 | const char** args, |
| 151 | const char** envp, |
| 152 | const Path** redirects, |
| 153 | unsigned memoryLimit, |
| 154 | std::string* ErrMsg) |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 155 | { |
| 156 | if (!path.canExecute()) { |
| 157 | if (ErrMsg) |
| 158 | *ErrMsg = path.toString() + " is not executable"; |
Mikhail Glushenkov | b23120b | 2009-07-18 21:43:12 +0000 | [diff] [blame] | 159 | return false; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 160 | } |
| 161 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 162 | // Create a child process. |
| 163 | int child = fork(); |
| 164 | switch (child) { |
| 165 | // An error occured: Return to the caller. |
| 166 | case -1: |
| 167 | MakeErrMsg(ErrMsg, "Couldn't fork"); |
Mikhail Glushenkov | b23120b | 2009-07-18 21:43:12 +0000 | [diff] [blame] | 168 | return false; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 169 | |
| 170 | // Child process: Execute the program. |
| 171 | case 0: { |
| 172 | // Redirect file descriptors... |
| 173 | if (redirects) { |
Matthijs Kooijman | 0e7dc8a | 2008-06-12 12:53:35 +0000 | [diff] [blame] | 174 | // Redirect stdin |
Mikhail Glushenkov | b23120b | 2009-07-18 21:43:12 +0000 | [diff] [blame] | 175 | if (RedirectIO(redirects[0], 0, ErrMsg)) { return false; } |
Matthijs Kooijman | 0e7dc8a | 2008-06-12 12:53:35 +0000 | [diff] [blame] | 176 | // Redirect stdout |
Mikhail Glushenkov | b23120b | 2009-07-18 21:43:12 +0000 | [diff] [blame] | 177 | if (RedirectIO(redirects[1], 1, ErrMsg)) { return false; } |
Mikhail Glushenkov | 63b88c7 | 2009-07-17 20:38:17 +0000 | [diff] [blame] | 178 | if (redirects[1] && redirects[2] && |
Matthijs Kooijman | 0e7dc8a | 2008-06-12 12:53:35 +0000 | [diff] [blame] | 179 | *(redirects[1]) == *(redirects[2])) { |
| 180 | // If stdout and stderr should go to the same place, redirect stderr |
| 181 | // to the FD already open for stdout. |
| 182 | if (-1 == dup2(1,2)) { |
| 183 | MakeErrMsg(ErrMsg, "Can't redirect stderr to stdout"); |
Mikhail Glushenkov | b23120b | 2009-07-18 21:43:12 +0000 | [diff] [blame] | 184 | return false; |
Matthijs Kooijman | 0e7dc8a | 2008-06-12 12:53:35 +0000 | [diff] [blame] | 185 | } |
| 186 | } else { |
| 187 | // Just redirect stderr |
Mikhail Glushenkov | b23120b | 2009-07-18 21:43:12 +0000 | [diff] [blame] | 188 | if (RedirectIO(redirects[2], 2, ErrMsg)) { return false; } |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 189 | } |
| 190 | } |
| 191 | |
| 192 | // Set memory limits |
| 193 | if (memoryLimit!=0) { |
| 194 | SetMemoryLimits(memoryLimit); |
| 195 | } |
Mikhail Glushenkov | 63b88c7 | 2009-07-17 20:38:17 +0000 | [diff] [blame] | 196 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 197 | // Execute! |
| 198 | if (envp != 0) |
Daniel Dunbar | 1df47e8 | 2009-08-04 20:32:25 +0000 | [diff] [blame] | 199 | execve(path.c_str(), (char**)args, (char**)envp); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 200 | else |
Daniel Dunbar | 1df47e8 | 2009-08-04 20:32:25 +0000 | [diff] [blame] | 201 | execv(path.c_str(), (char**)args); |
Dan Gohman | a629b69 | 2009-08-05 00:09:12 +0000 | [diff] [blame] | 202 | // If the execve() failed, we should exit. Follow Unix protocol and |
| 203 | // return 127 if the executable was not found, and 126 otherwise. |
| 204 | // Use _exit rather than exit so that atexit functions and static |
| 205 | // object destructors cloned from the parent process aren't |
| 206 | // redundantly run, and so that any data buffered in stdio buffers |
| 207 | // cloned from the parent aren't redundantly written out. |
| 208 | _exit(errno == ENOENT ? 127 : 126); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 209 | } |
| 210 | |
| 211 | // Parent process: Break out of the switch to do our processing. |
| 212 | default: |
| 213 | break; |
| 214 | } |
| 215 | |
Mikhail Glushenkov | b23120b | 2009-07-18 21:43:12 +0000 | [diff] [blame] | 216 | Pid_ = child; |
| 217 | |
| 218 | return true; |
| 219 | } |
| 220 | |
| 221 | int |
| 222 | Program::Wait(unsigned secondsToWait, |
| 223 | std::string* ErrMsg) |
| 224 | { |
| 225 | #ifdef HAVE_SYS_WAIT_H |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 226 | struct sigaction Act, Old; |
| 227 | |
Mikhail Glushenkov | b23120b | 2009-07-18 21:43:12 +0000 | [diff] [blame] | 228 | if (Pid_ == 0) { |
| 229 | MakeErrMsg(ErrMsg, "Process not started!"); |
| 230 | return -1; |
| 231 | } |
| 232 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 233 | // Install a timeout handler. |
| 234 | if (secondsToWait) { |
| 235 | Timeout = false; |
| 236 | Act.sa_sigaction = 0; |
| 237 | Act.sa_handler = TimeOutHandler; |
| 238 | sigemptyset(&Act.sa_mask); |
| 239 | Act.sa_flags = 0; |
| 240 | sigaction(SIGALRM, &Act, &Old); |
| 241 | alarm(secondsToWait); |
| 242 | } |
| 243 | |
| 244 | // Parent process: Wait for the child process to terminate. |
| 245 | int status; |
Mikhail Glushenkov | b23120b | 2009-07-18 21:43:12 +0000 | [diff] [blame] | 246 | int child = this->Pid_; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 247 | while (wait(&status) != child) |
| 248 | if (secondsToWait && errno == EINTR) { |
| 249 | // Kill the child. |
| 250 | kill(child, SIGKILL); |
Mikhail Glushenkov | 63b88c7 | 2009-07-17 20:38:17 +0000 | [diff] [blame] | 251 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 252 | // Turn off the alarm and restore the signal handler |
| 253 | alarm(0); |
| 254 | sigaction(SIGALRM, &Old, 0); |
| 255 | |
| 256 | // Wait for child to die |
| 257 | if (wait(&status) != child) |
| 258 | MakeErrMsg(ErrMsg, "Child timed out but wouldn't die"); |
Devang Patel | 930a5ad | 2008-02-04 20:57:54 +0000 | [diff] [blame] | 259 | else |
| 260 | MakeErrMsg(ErrMsg, "Child timed out", 0); |
| 261 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 262 | return -1; // Timeout detected |
Devang Patel | 930a5ad | 2008-02-04 20:57:54 +0000 | [diff] [blame] | 263 | } else if (errno != EINTR) { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 264 | MakeErrMsg(ErrMsg, "Error waiting for child process"); |
| 265 | return -1; |
| 266 | } |
| 267 | |
| 268 | // We exited normally without timeout, so turn off the timer. |
| 269 | if (secondsToWait) { |
| 270 | alarm(0); |
| 271 | sigaction(SIGALRM, &Old, 0); |
| 272 | } |
| 273 | |
| 274 | // Return the proper exit status. 0=success, >0 is programs' exit status, |
| 275 | // <0 means a signal was returned, -9999999 means the program dumped core. |
| 276 | int result = 0; |
| 277 | if (WIFEXITED(status)) |
| 278 | result = WEXITSTATUS(status); |
| 279 | else if (WIFSIGNALED(status)) |
| 280 | result = 0 - WTERMSIG(status); |
| 281 | #ifdef WCOREDUMP |
| 282 | else if (WCOREDUMP(status)) |
| 283 | result |= 0x01000000; |
| 284 | #endif |
| 285 | return result; |
| 286 | #else |
| 287 | return -99; |
| 288 | #endif |
Mikhail Glushenkov | 63b88c7 | 2009-07-17 20:38:17 +0000 | [diff] [blame] | 289 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 290 | } |
| 291 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 292 | bool Program::ChangeStdinToBinary(){ |
| 293 | // Do nothing, as Unix doesn't differentiate between text and binary. |
| 294 | return false; |
| 295 | } |
| 296 | |
| 297 | bool Program::ChangeStdoutToBinary(){ |
| 298 | // Do nothing, as Unix doesn't differentiate between text and binary. |
| 299 | return false; |
| 300 | } |
| 301 | |
| 302 | } |