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