Reid Spencer | 52a7efa | 2004-08-29 19:20:41 +0000 | [diff] [blame] | 1 | //===- llvm/System/Unix/Program.cpp -----------------------------*- C++ -*-===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 4ee451d | 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. |
Reid Spencer | 52a7efa | 2004-08-29 19:20:41 +0000 | [diff] [blame] | 7 | // |
| 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 | |
Reid Spencer | 551ccae | 2004-09-01 22:55:40 +0000 | [diff] [blame] | 19 | #include <llvm/Config/config.h> |
Bill Wendling | eecfa36 | 2008-05-29 21:46:33 +0000 | [diff] [blame] | 20 | #include <llvm/Support/Streams.h> |
Reid Spencer | 52a7efa | 2004-08-29 19:20:41 +0000 | [diff] [blame] | 21 | #include "Unix.h" |
Reid Spencer | cdf54d0 | 2004-12-27 06:16:52 +0000 | [diff] [blame] | 22 | #if HAVE_SYS_STAT_H |
| 23 | #include <sys/stat.h> |
| 24 | #endif |
Anton Korobeynikov | 9ba8a76 | 2007-02-16 19:11:07 +0000 | [diff] [blame] | 25 | #if HAVE_SYS_RESOURCE_H |
| 26 | #include <sys/resource.h> |
| 27 | #endif |
Reid Spencer | cdf54d0 | 2004-12-27 06:16:52 +0000 | [diff] [blame] | 28 | #if HAVE_SIGNAL_H |
| 29 | #include <signal.h> |
| 30 | #endif |
| 31 | #if HAVE_FCNTL_H |
| 32 | #include <fcntl.h> |
| 33 | #endif |
Reid Spencer | 52a7efa | 2004-08-29 19:20:41 +0000 | [diff] [blame] | 34 | |
Reid Spencer | 52a7efa | 2004-08-29 19:20:41 +0000 | [diff] [blame] | 35 | namespace llvm { |
| 36 | using namespace sys; |
| 37 | |
| 38 | // This function just uses the PATH environment variable to find the program. |
Reid Spencer | 2565943 | 2004-09-13 21:48:44 +0000 | [diff] [blame] | 39 | Path |
Reid Spencer | 52a7efa | 2004-08-29 19:20:41 +0000 | [diff] [blame] | 40 | Program::FindProgramByName(const std::string& progName) { |
| 41 | |
| 42 | // Check some degenerate cases |
| 43 | if (progName.length() == 0) // no program |
Reid Spencer | 2565943 | 2004-09-13 21:48:44 +0000 | [diff] [blame] | 44 | return Path(); |
| 45 | Path temp; |
Reid Spencer | dd04df0 | 2005-07-07 23:21:43 +0000 | [diff] [blame] | 46 | if (!temp.set(progName)) // invalid name |
Reid Spencer | 2565943 | 2004-09-13 21:48:44 +0000 | [diff] [blame] | 47 | return Path(); |
Misha Brukman | 22c46da | 2005-04-20 15:42:11 +0000 | [diff] [blame] | 48 | // FIXME: have to check for absolute filename - we cannot assume anything |
| 49 | // about "." being in $PATH |
Reid Spencer | c7f0832 | 2005-07-07 18:21:42 +0000 | [diff] [blame] | 50 | if (temp.canExecute()) // already executable as is |
Reid Spencer | 52a7efa | 2004-08-29 19:20:41 +0000 | [diff] [blame] | 51 | return temp; |
| 52 | |
| 53 | // At this point, the file name is valid and its not executable |
| 54 | |
| 55 | // Get the path. If its empty, we can't do anything to find it. |
| 56 | const char *PathStr = getenv("PATH"); |
| 57 | if (PathStr == 0) |
Reid Spencer | 2565943 | 2004-09-13 21:48:44 +0000 | [diff] [blame] | 58 | return Path(); |
Reid Spencer | 52a7efa | 2004-08-29 19:20:41 +0000 | [diff] [blame] | 59 | |
| 60 | // Now we have a colon separated list of directories to search; try them. |
Evan Cheng | 34cd4a4 | 2008-05-05 18:30:58 +0000 | [diff] [blame] | 61 | size_t PathLen = strlen(PathStr); |
Reid Spencer | 52a7efa | 2004-08-29 19:20:41 +0000 | [diff] [blame] | 62 | while (PathLen) { |
| 63 | // Find the first colon... |
| 64 | const char *Colon = std::find(PathStr, PathStr+PathLen, ':'); |
| 65 | |
| 66 | // Check to see if this first directory contains the executable... |
Reid Spencer | 2565943 | 2004-09-13 21:48:44 +0000 | [diff] [blame] | 67 | Path FilePath; |
Reid Spencer | dd04df0 | 2005-07-07 23:21:43 +0000 | [diff] [blame] | 68 | if (FilePath.set(std::string(PathStr,Colon))) { |
| 69 | FilePath.appendComponent(progName); |
Reid Spencer | c7f0832 | 2005-07-07 18:21:42 +0000 | [diff] [blame] | 70 | if (FilePath.canExecute()) |
Reid Spencer | 52a7efa | 2004-08-29 19:20:41 +0000 | [diff] [blame] | 71 | return FilePath; // Found the executable! |
| 72 | } |
| 73 | |
| 74 | // Nope it wasn't in this directory, check the next path in the list! |
| 75 | PathLen -= Colon-PathStr; |
| 76 | PathStr = Colon; |
| 77 | |
| 78 | // Advance past duplicate colons |
| 79 | while (*PathStr == ':') { |
| 80 | PathStr++; |
| 81 | PathLen--; |
| 82 | } |
| 83 | } |
Reid Spencer | 2565943 | 2004-09-13 21:48:44 +0000 | [diff] [blame] | 84 | return Path(); |
Reid Spencer | 52a7efa | 2004-08-29 19:20:41 +0000 | [diff] [blame] | 85 | } |
| 86 | |
Reid Spencer | 4ce5dc6 | 2006-08-21 06:02:44 +0000 | [diff] [blame] | 87 | static bool RedirectFD(const std::string &File, int FD, std::string* ErrMsg) { |
| 88 | if (File.empty()) return false; // Noop |
Reid Spencer | 2a7d9e9 | 2004-12-19 18:00:44 +0000 | [diff] [blame] | 89 | |
| 90 | // Open the file |
| 91 | int InFD = open(File.c_str(), FD == 0 ? O_RDONLY : O_WRONLY|O_CREAT, 0666); |
| 92 | if (InFD == -1) { |
Reid Spencer | 4ce5dc6 | 2006-08-21 06:02:44 +0000 | [diff] [blame] | 93 | MakeErrMsg(ErrMsg, "Cannot open file '" + File + "' for " |
Reid Spencer | 2a7d9e9 | 2004-12-19 18:00:44 +0000 | [diff] [blame] | 94 | + (FD == 0 ? "input" : "output") + "!\n"); |
Reid Spencer | 4ce5dc6 | 2006-08-21 06:02:44 +0000 | [diff] [blame] | 95 | return true; |
Reid Spencer | 2a7d9e9 | 2004-12-19 18:00:44 +0000 | [diff] [blame] | 96 | } |
| 97 | |
Reid Spencer | 4ce5dc6 | 2006-08-21 06:02:44 +0000 | [diff] [blame] | 98 | // Install it as the requested FD |
| 99 | if (-1 == dup2(InFD, FD)) { |
| 100 | MakeErrMsg(ErrMsg, "Cannot dup2"); |
| 101 | return true; |
| 102 | } |
Reid Spencer | 2a7d9e9 | 2004-12-19 18:00:44 +0000 | [diff] [blame] | 103 | close(InFD); // Close the original FD |
Reid Spencer | 4ce5dc6 | 2006-08-21 06:02:44 +0000 | [diff] [blame] | 104 | return false; |
Reid Spencer | 2a7d9e9 | 2004-12-19 18:00:44 +0000 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | static bool Timeout = false; |
| 108 | static void TimeOutHandler(int Sig) { |
| 109 | Timeout = true; |
| 110 | } |
| 111 | |
Anton Korobeynikov | 9ba8a76 | 2007-02-16 19:11:07 +0000 | [diff] [blame] | 112 | static void SetMemoryLimits (unsigned size) |
| 113 | { |
| 114 | #if HAVE_SYS_RESOURCE_H |
| 115 | struct rlimit r; |
| 116 | __typeof__ (r.rlim_cur) limit = (__typeof__ (r.rlim_cur)) (size) * 1048576; |
| 117 | |
| 118 | // Heap size |
| 119 | getrlimit (RLIMIT_DATA, &r); |
| 120 | r.rlim_cur = limit; |
| 121 | setrlimit (RLIMIT_DATA, &r); |
Gabor Greif | aa6b7fd | 2007-07-06 10:31:27 +0000 | [diff] [blame] | 122 | #ifdef RLIMIT_RSS |
Anton Korobeynikov | 9ba8a76 | 2007-02-16 19:11:07 +0000 | [diff] [blame] | 123 | // Resident set size. |
| 124 | getrlimit (RLIMIT_RSS, &r); |
| 125 | r.rlim_cur = limit; |
| 126 | setrlimit (RLIMIT_RSS, &r); |
Reid Spencer | 2e40d03 | 2007-04-23 07:22:51 +0000 | [diff] [blame] | 127 | #endif |
Devang Patel | 69bdf68 | 2007-06-04 15:28:57 +0000 | [diff] [blame] | 128 | #ifdef RLIMIT_AS // e.g. NetBSD doesn't have it. |
Anton Korobeynikov | 9ba8a76 | 2007-02-16 19:11:07 +0000 | [diff] [blame] | 129 | // Virtual memory. |
| 130 | getrlimit (RLIMIT_AS, &r); |
| 131 | r.rlim_cur = limit; |
| 132 | setrlimit (RLIMIT_AS, &r); |
| 133 | #endif |
Devang Patel | 69bdf68 | 2007-06-04 15:28:57 +0000 | [diff] [blame] | 134 | #endif |
Anton Korobeynikov | 9ba8a76 | 2007-02-16 19:11:07 +0000 | [diff] [blame] | 135 | } |
| 136 | |
Reid Spencer | 52a7efa | 2004-08-29 19:20:41 +0000 | [diff] [blame] | 137 | int |
Reid Spencer | 2565943 | 2004-09-13 21:48:44 +0000 | [diff] [blame] | 138 | Program::ExecuteAndWait(const Path& path, |
Reid Spencer | 2a7d9e9 | 2004-12-19 18:00:44 +0000 | [diff] [blame] | 139 | const char** args, |
| 140 | const char** envp, |
| 141 | const Path** redirects, |
Reid Spencer | 4ce5dc6 | 2006-08-21 06:02:44 +0000 | [diff] [blame] | 142 | unsigned secondsToWait, |
Anton Korobeynikov | 9ba8a76 | 2007-02-16 19:11:07 +0000 | [diff] [blame] | 143 | unsigned memoryLimit, |
Reid Spencer | 4ce5dc6 | 2006-08-21 06:02:44 +0000 | [diff] [blame] | 144 | std::string* ErrMsg) |
| 145 | { |
| 146 | if (!path.canExecute()) { |
| 147 | if (ErrMsg) |
| 148 | *ErrMsg = path.toString() + " is not executable"; |
| 149 | return -1; |
| 150 | } |
Reid Spencer | 52a7efa | 2004-08-29 19:20:41 +0000 | [diff] [blame] | 151 | |
| 152 | #ifdef HAVE_SYS_WAIT_H |
Reid Spencer | 52a7efa | 2004-08-29 19:20:41 +0000 | [diff] [blame] | 153 | // Create a child process. |
Reid Spencer | 2a7d9e9 | 2004-12-19 18:00:44 +0000 | [diff] [blame] | 154 | int child = fork(); |
| 155 | switch (child) { |
Reid Spencer | 52a7efa | 2004-08-29 19:20:41 +0000 | [diff] [blame] | 156 | // An error occured: Return to the caller. |
| 157 | case -1: |
Reid Spencer | 4ce5dc6 | 2006-08-21 06:02:44 +0000 | [diff] [blame] | 158 | MakeErrMsg(ErrMsg, "Couldn't fork"); |
| 159 | return -1; |
Reid Spencer | 52a7efa | 2004-08-29 19:20:41 +0000 | [diff] [blame] | 160 | |
| 161 | // Child process: Execute the program. |
Reid Spencer | e2e2411 | 2004-12-14 04:18:51 +0000 | [diff] [blame] | 162 | case 0: { |
Reid Spencer | 2a7d9e9 | 2004-12-19 18:00:44 +0000 | [diff] [blame] | 163 | // Redirect file descriptors... |
| 164 | if (redirects) { |
Reid Spencer | ad42ea7 | 2006-08-22 15:56:52 +0000 | [diff] [blame] | 165 | if (redirects[0]) { |
| 166 | if (redirects[0]->isEmpty()) { |
Reid Spencer | 4ce5dc6 | 2006-08-21 06:02:44 +0000 | [diff] [blame] | 167 | if (RedirectFD("/dev/null",0,ErrMsg)) { return -1; } |
Reid Spencer | ad42ea7 | 2006-08-22 15:56:52 +0000 | [diff] [blame] | 168 | } else { |
Reid Spencer | 4ce5dc6 | 2006-08-21 06:02:44 +0000 | [diff] [blame] | 169 | if (RedirectFD(redirects[0]->toString(), 0,ErrMsg)) { return -1; } |
Reid Spencer | ad42ea7 | 2006-08-22 15:56:52 +0000 | [diff] [blame] | 170 | } |
| 171 | } |
| 172 | if (redirects[1]) { |
| 173 | if (redirects[1]->isEmpty()) { |
Reid Spencer | 4ce5dc6 | 2006-08-21 06:02:44 +0000 | [diff] [blame] | 174 | if (RedirectFD("/dev/null",1,ErrMsg)) { return -1; } |
Reid Spencer | ad42ea7 | 2006-08-22 15:56:52 +0000 | [diff] [blame] | 175 | } else { |
Reid Spencer | 4ce5dc6 | 2006-08-21 06:02:44 +0000 | [diff] [blame] | 176 | if (RedirectFD(redirects[1]->toString(),1,ErrMsg)) { return -1; } |
Reid Spencer | ad42ea7 | 2006-08-22 15:56:52 +0000 | [diff] [blame] | 177 | } |
| 178 | } |
Reid Spencer | 2a7d9e9 | 2004-12-19 18:00:44 +0000 | [diff] [blame] | 179 | if (redirects[1] && redirects[2] && |
| 180 | *(redirects[1]) != *(redirects[2])) { |
Reid Spencer | ad42ea7 | 2006-08-22 15:56:52 +0000 | [diff] [blame] | 181 | if (redirects[2]->isEmpty()) { |
Reid Spencer | 4ce5dc6 | 2006-08-21 06:02:44 +0000 | [diff] [blame] | 182 | if (RedirectFD("/dev/null",2,ErrMsg)) { return -1; } |
Reid Spencer | ad42ea7 | 2006-08-22 15:56:52 +0000 | [diff] [blame] | 183 | } else { |
Reid Spencer | 4ce5dc6 | 2006-08-21 06:02:44 +0000 | [diff] [blame] | 184 | if (RedirectFD(redirects[2]->toString(), 2,ErrMsg)) { return -1; } |
Reid Spencer | ad42ea7 | 2006-08-22 15:56:52 +0000 | [diff] [blame] | 185 | } |
Reid Spencer | 4ce5dc6 | 2006-08-21 06:02:44 +0000 | [diff] [blame] | 186 | } else if (-1 == dup2(1,2)) { |
| 187 | MakeErrMsg(ErrMsg, "Can't redirect"); |
| 188 | return -1; |
Reid Spencer | 2a7d9e9 | 2004-12-19 18:00:44 +0000 | [diff] [blame] | 189 | } |
| 190 | } |
| 191 | |
Anton Korobeynikov | 9ba8a76 | 2007-02-16 19:11:07 +0000 | [diff] [blame] | 192 | // Set memory limits |
| 193 | if (memoryLimit!=0) { |
| 194 | SetMemoryLimits(memoryLimit); |
| 195 | } |
| 196 | |
Reid Spencer | 2a7d9e9 | 2004-12-19 18:00:44 +0000 | [diff] [blame] | 197 | // Execute! |
Evan Cheng | 2fafaf1 | 2006-06-09 20:43:11 +0000 | [diff] [blame] | 198 | if (envp != 0) |
Dan Gohman | cb648f9 | 2007-09-14 20:08:19 +0000 | [diff] [blame] | 199 | execve (path.c_str(), (char**)args, (char**)envp); |
Evan Cheng | 2fafaf1 | 2006-06-09 20:43:11 +0000 | [diff] [blame] | 200 | else |
Dan Gohman | cb648f9 | 2007-09-14 20:08:19 +0000 | [diff] [blame] | 201 | execv (path.c_str(), (char**)args); |
Reid Spencer | 52a7efa | 2004-08-29 19:20:41 +0000 | [diff] [blame] | 202 | // If the execve() failed, we should exit and let the parent pick up |
| 203 | // our non-zero exit status. |
| 204 | exit (errno); |
Reid Spencer | e2e2411 | 2004-12-14 04:18:51 +0000 | [diff] [blame] | 205 | } |
Reid Spencer | 52a7efa | 2004-08-29 19:20:41 +0000 | [diff] [blame] | 206 | |
| 207 | // Parent process: Break out of the switch to do our processing. |
| 208 | default: |
| 209 | break; |
| 210 | } |
| 211 | |
Reid Spencer | 2a7d9e9 | 2004-12-19 18:00:44 +0000 | [diff] [blame] | 212 | // Make sure stderr and stdout have been flushed |
Bill Wendling | eecfa36 | 2008-05-29 21:46:33 +0000 | [diff] [blame] | 213 | cerr.flush(); |
| 214 | cout.flush(); |
Reid Spencer | 2a7d9e9 | 2004-12-19 18:00:44 +0000 | [diff] [blame] | 215 | fsync(1); |
| 216 | fsync(2); |
| 217 | |
| 218 | struct sigaction Act, Old; |
| 219 | |
| 220 | // Install a timeout handler. |
| 221 | if (secondsToWait) { |
| 222 | Timeout = false; |
| 223 | Act.sa_sigaction = 0; |
| 224 | Act.sa_handler = TimeOutHandler; |
| 225 | sigemptyset(&Act.sa_mask); |
| 226 | Act.sa_flags = 0; |
| 227 | sigaction(SIGALRM, &Act, &Old); |
| 228 | alarm(secondsToWait); |
| 229 | } |
| 230 | |
Reid Spencer | 52a7efa | 2004-08-29 19:20:41 +0000 | [diff] [blame] | 231 | // Parent process: Wait for the child process to terminate. |
| 232 | int status; |
Reid Spencer | 2a7d9e9 | 2004-12-19 18:00:44 +0000 | [diff] [blame] | 233 | while (wait(&status) != child) |
| 234 | if (secondsToWait && errno == EINTR) { |
| 235 | // Kill the child. |
| 236 | kill(child, SIGKILL); |
| 237 | |
| 238 | // Turn off the alarm and restore the signal handler |
| 239 | alarm(0); |
| 240 | sigaction(SIGALRM, &Old, 0); |
| 241 | |
| 242 | // Wait for child to die |
| 243 | if (wait(&status) != child) |
Reid Spencer | 4ce5dc6 | 2006-08-21 06:02:44 +0000 | [diff] [blame] | 244 | MakeErrMsg(ErrMsg, "Child timed out but wouldn't die"); |
Devang Patel | a1e4bba | 2008-02-04 20:57:54 +0000 | [diff] [blame] | 245 | else |
| 246 | MakeErrMsg(ErrMsg, "Child timed out", 0); |
| 247 | |
Reid Spencer | 2a7d9e9 | 2004-12-19 18:00:44 +0000 | [diff] [blame] | 248 | return -1; // Timeout detected |
Devang Patel | a1e4bba | 2008-02-04 20:57:54 +0000 | [diff] [blame] | 249 | } else if (errno != EINTR) { |
Reid Spencer | 4ce5dc6 | 2006-08-21 06:02:44 +0000 | [diff] [blame] | 250 | MakeErrMsg(ErrMsg, "Error waiting for child process"); |
| 251 | return -1; |
Reid Spencer | 2a7d9e9 | 2004-12-19 18:00:44 +0000 | [diff] [blame] | 252 | } |
| 253 | |
| 254 | // We exited normally without timeout, so turn off the timer. |
| 255 | if (secondsToWait) { |
| 256 | alarm(0); |
| 257 | sigaction(SIGALRM, &Old, 0); |
| 258 | } |
Reid Spencer | 52a7efa | 2004-08-29 19:20:41 +0000 | [diff] [blame] | 259 | |
Reid Spencer | d555f41 | 2005-12-22 20:00:16 +0000 | [diff] [blame] | 260 | // Return the proper exit status. 0=success, >0 is programs' exit status, |
| 261 | // <0 means a signal was returned, -9999999 means the program dumped core. |
| 262 | int result = 0; |
Chris Lattner | 0228c0f | 2006-07-12 22:37:18 +0000 | [diff] [blame] | 263 | if (WIFEXITED(status)) |
Reid Spencer | d555f41 | 2005-12-22 20:00:16 +0000 | [diff] [blame] | 264 | result = WEXITSTATUS(status); |
Reid Spencer | 52a7efa | 2004-08-29 19:20:41 +0000 | [diff] [blame] | 265 | else if (WIFSIGNALED(status)) |
Reid Spencer | d555f41 | 2005-12-22 20:00:16 +0000 | [diff] [blame] | 266 | result = 0 - WTERMSIG(status); |
| 267 | #ifdef WCOREDUMP |
Chris Lattner | 0228c0f | 2006-07-12 22:37:18 +0000 | [diff] [blame] | 268 | else if (WCOREDUMP(status)) |
Reid Spencer | d555f41 | 2005-12-22 20:00:16 +0000 | [diff] [blame] | 269 | result |= 0x01000000; |
Reid Spencer | 52a7efa | 2004-08-29 19:20:41 +0000 | [diff] [blame] | 270 | #endif |
Reid Spencer | d555f41 | 2005-12-22 20:00:16 +0000 | [diff] [blame] | 271 | return result; |
| 272 | #else |
| 273 | return -99; |
| 274 | #endif |
| 275 | |
Reid Spencer | 52a7efa | 2004-08-29 19:20:41 +0000 | [diff] [blame] | 276 | } |
| 277 | |
Reid Spencer | 4ce5dc6 | 2006-08-21 06:02:44 +0000 | [diff] [blame] | 278 | bool Program::ChangeStdinToBinary(){ |
Reid Spencer | 32f5553 | 2006-06-07 23:18:34 +0000 | [diff] [blame] | 279 | // Do nothing, as Unix doesn't differentiate between text and binary. |
Reid Spencer | 4ce5dc6 | 2006-08-21 06:02:44 +0000 | [diff] [blame] | 280 | return false; |
Reid Spencer | 32f5553 | 2006-06-07 23:18:34 +0000 | [diff] [blame] | 281 | } |
| 282 | |
Reid Spencer | 4ce5dc6 | 2006-08-21 06:02:44 +0000 | [diff] [blame] | 283 | bool Program::ChangeStdoutToBinary(){ |
Reid Spencer | 32f5553 | 2006-06-07 23:18:34 +0000 | [diff] [blame] | 284 | // Do nothing, as Unix doesn't differentiate between text and binary. |
Reid Spencer | 4ce5dc6 | 2006-08-21 06:02:44 +0000 | [diff] [blame] | 285 | return false; |
Reid Spencer | 32f5553 | 2006-06-07 23:18:34 +0000 | [diff] [blame] | 286 | } |
| 287 | |
Reid Spencer | 52a7efa | 2004-08-29 19:20:41 +0000 | [diff] [blame] | 288 | } |