Reid Spencer | 52a7efa | 2004-08-29 19:20:41 +0000 | [diff] [blame] | 1 | //===- llvm/System/Unix/Program.cpp -----------------------------*- C++ -*-===// |
Mikhail Glushenkov | 234f689 | 2009-07-17 20:38:17 +0000 | [diff] [blame] | 2 | // |
Reid Spencer | 52a7efa | 2004-08-29 19:20:41 +0000 | [diff] [blame] | 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. |
Mikhail Glushenkov | 234f689 | 2009-07-17 20:38:17 +0000 | [diff] [blame] | 7 | // |
Reid Spencer | 52a7efa | 2004-08-29 19:20:41 +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 | |
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 | cdf54d0 | 2004-12-27 06:16:52 +0000 | [diff] [blame] | 21 | #if HAVE_SYS_STAT_H |
| 22 | #include <sys/stat.h> |
| 23 | #endif |
Anton Korobeynikov | 9ba8a76 | 2007-02-16 19:11:07 +0000 | [diff] [blame] | 24 | #if HAVE_SYS_RESOURCE_H |
| 25 | #include <sys/resource.h> |
| 26 | #endif |
Reid Spencer | cdf54d0 | 2004-12-27 06:16:52 +0000 | [diff] [blame] | 27 | #if HAVE_SIGNAL_H |
| 28 | #include <signal.h> |
| 29 | #endif |
| 30 | #if HAVE_FCNTL_H |
| 31 | #include <fcntl.h> |
| 32 | #endif |
Reid Spencer | 52a7efa | 2004-08-29 19:20:41 +0000 | [diff] [blame] | 33 | |
Reid Spencer | 52a7efa | 2004-08-29 19:20:41 +0000 | [diff] [blame] | 34 | namespace llvm { |
| 35 | using namespace sys; |
| 36 | |
Daniel Dunbar | 57d6903 | 2009-09-22 04:44:56 +0000 | [diff] [blame] | 37 | Program::Program() : Data_(0) {} |
| 38 | |
| 39 | Program::~Program() {} |
| 40 | |
| 41 | unsigned Program::GetPid() const { |
| 42 | uint64_t pid = reinterpret_cast<uint64_t>(Data_); |
| 43 | return static_cast<unsigned>(pid); |
| 44 | } |
| 45 | |
Reid Spencer | 52a7efa | 2004-08-29 19:20:41 +0000 | [diff] [blame] | 46 | // This function just uses the PATH environment variable to find the program. |
Reid Spencer | 2565943 | 2004-09-13 21:48:44 +0000 | [diff] [blame] | 47 | Path |
Reid Spencer | 52a7efa | 2004-08-29 19:20:41 +0000 | [diff] [blame] | 48 | Program::FindProgramByName(const std::string& progName) { |
| 49 | |
| 50 | // Check some degenerate cases |
| 51 | if (progName.length() == 0) // no program |
Reid Spencer | 2565943 | 2004-09-13 21:48:44 +0000 | [diff] [blame] | 52 | return Path(); |
| 53 | Path temp; |
Reid Spencer | dd04df0 | 2005-07-07 23:21:43 +0000 | [diff] [blame] | 54 | if (!temp.set(progName)) // invalid name |
Reid Spencer | 2565943 | 2004-09-13 21:48:44 +0000 | [diff] [blame] | 55 | return Path(); |
Benjamin Kramer | b0facb1 | 2009-07-28 22:08:15 +0000 | [diff] [blame] | 56 | // Use the given path verbatim if it contains any slashes; this matches |
| 57 | // the behavior of sh(1) and friends. |
Dan Gohman | a87861e | 2009-07-28 23:25:18 +0000 | [diff] [blame] | 58 | if (progName.find('/') != std::string::npos) |
Reid Spencer | 52a7efa | 2004-08-29 19:20:41 +0000 | [diff] [blame] | 59 | return temp; |
| 60 | |
Dan Gohman | 88abb89 | 2009-08-05 17:32:39 +0000 | [diff] [blame] | 61 | // At this point, the file name does not contain slashes. Search for it |
| 62 | // through the directories specified in the PATH environment variable. |
Mikhail Glushenkov | 234f689 | 2009-07-17 20:38:17 +0000 | [diff] [blame] | 63 | |
Reid Spencer | 52a7efa | 2004-08-29 19:20:41 +0000 | [diff] [blame] | 64 | // Get the path. If its empty, we can't do anything to find it. |
| 65 | const char *PathStr = getenv("PATH"); |
Mikhail Glushenkov | 234f689 | 2009-07-17 20:38:17 +0000 | [diff] [blame] | 66 | if (PathStr == 0) |
Reid Spencer | 2565943 | 2004-09-13 21:48:44 +0000 | [diff] [blame] | 67 | return Path(); |
Reid Spencer | 52a7efa | 2004-08-29 19:20:41 +0000 | [diff] [blame] | 68 | |
| 69 | // 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] | 70 | size_t PathLen = strlen(PathStr); |
Reid Spencer | 52a7efa | 2004-08-29 19:20:41 +0000 | [diff] [blame] | 71 | while (PathLen) { |
| 72 | // Find the first colon... |
| 73 | const char *Colon = std::find(PathStr, PathStr+PathLen, ':'); |
| 74 | |
| 75 | // Check to see if this first directory contains the executable... |
Reid Spencer | 2565943 | 2004-09-13 21:48:44 +0000 | [diff] [blame] | 76 | Path FilePath; |
Reid Spencer | dd04df0 | 2005-07-07 23:21:43 +0000 | [diff] [blame] | 77 | if (FilePath.set(std::string(PathStr,Colon))) { |
| 78 | FilePath.appendComponent(progName); |
Reid Spencer | c7f0832 | 2005-07-07 18:21:42 +0000 | [diff] [blame] | 79 | if (FilePath.canExecute()) |
Reid Spencer | 52a7efa | 2004-08-29 19:20:41 +0000 | [diff] [blame] | 80 | return FilePath; // Found the executable! |
| 81 | } |
| 82 | |
| 83 | // Nope it wasn't in this directory, check the next path in the list! |
| 84 | PathLen -= Colon-PathStr; |
| 85 | PathStr = Colon; |
| 86 | |
| 87 | // Advance past duplicate colons |
| 88 | while (*PathStr == ':') { |
| 89 | PathStr++; |
| 90 | PathLen--; |
| 91 | } |
| 92 | } |
Reid Spencer | 2565943 | 2004-09-13 21:48:44 +0000 | [diff] [blame] | 93 | return Path(); |
Reid Spencer | 52a7efa | 2004-08-29 19:20:41 +0000 | [diff] [blame] | 94 | } |
| 95 | |
Matthijs Kooijman | 905261e | 2008-06-12 10:47:18 +0000 | [diff] [blame] | 96 | static bool RedirectIO(const Path *Path, int FD, std::string* ErrMsg) { |
| 97 | if (Path == 0) |
| 98 | // Noop |
| 99 | return false; |
| 100 | std::string File; |
| 101 | if (Path->isEmpty()) |
| 102 | // Redirect empty paths to /dev/null |
| 103 | File = "/dev/null"; |
| 104 | else |
Chris Lattner | 74382b7 | 2009-08-23 22:45:37 +0000 | [diff] [blame] | 105 | File = Path->str(); |
Reid Spencer | 2a7d9e9 | 2004-12-19 18:00:44 +0000 | [diff] [blame] | 106 | |
| 107 | // Open the file |
| 108 | int InFD = open(File.c_str(), FD == 0 ? O_RDONLY : O_WRONLY|O_CREAT, 0666); |
| 109 | if (InFD == -1) { |
Reid Spencer | 4ce5dc6 | 2006-08-21 06:02:44 +0000 | [diff] [blame] | 110 | MakeErrMsg(ErrMsg, "Cannot open file '" + File + "' for " |
Daniel Dunbar | a7f2a9e | 2009-04-20 20:50:13 +0000 | [diff] [blame] | 111 | + (FD == 0 ? "input" : "output")); |
Reid Spencer | 4ce5dc6 | 2006-08-21 06:02:44 +0000 | [diff] [blame] | 112 | return true; |
Reid Spencer | 2a7d9e9 | 2004-12-19 18:00:44 +0000 | [diff] [blame] | 113 | } |
| 114 | |
Reid Spencer | 4ce5dc6 | 2006-08-21 06:02:44 +0000 | [diff] [blame] | 115 | // Install it as the requested FD |
Chris Lattner | cdff5f7 | 2010-03-14 23:16:45 +0000 | [diff] [blame^] | 116 | if (dup2(InFD, FD) == -1) { |
Reid Spencer | 4ce5dc6 | 2006-08-21 06:02:44 +0000 | [diff] [blame] | 117 | MakeErrMsg(ErrMsg, "Cannot dup2"); |
Chris Lattner | cdff5f7 | 2010-03-14 23:16:45 +0000 | [diff] [blame^] | 118 | close(InFD); |
Reid Spencer | 4ce5dc6 | 2006-08-21 06:02:44 +0000 | [diff] [blame] | 119 | return true; |
| 120 | } |
Reid Spencer | 2a7d9e9 | 2004-12-19 18:00:44 +0000 | [diff] [blame] | 121 | close(InFD); // Close the original FD |
Reid Spencer | 4ce5dc6 | 2006-08-21 06:02:44 +0000 | [diff] [blame] | 122 | return false; |
Reid Spencer | 2a7d9e9 | 2004-12-19 18:00:44 +0000 | [diff] [blame] | 123 | } |
| 124 | |
Duncan Sands | 68d29d0 | 2009-11-08 20:55:48 +0000 | [diff] [blame] | 125 | static void TimeOutHandler(int Sig) { |
| 126 | } |
| 127 | |
Anton Korobeynikov | 9ba8a76 | 2007-02-16 19:11:07 +0000 | [diff] [blame] | 128 | static void SetMemoryLimits (unsigned size) |
| 129 | { |
Chris Lattner | 6ae7bbb | 2010-02-12 00:37:46 +0000 | [diff] [blame] | 130 | #if HAVE_SYS_RESOURCE_H && HAVE_GETRLIMIT && HAVE_SETRLIMIT |
Anton Korobeynikov | 9ba8a76 | 2007-02-16 19:11:07 +0000 | [diff] [blame] | 131 | struct rlimit r; |
| 132 | __typeof__ (r.rlim_cur) limit = (__typeof__ (r.rlim_cur)) (size) * 1048576; |
| 133 | |
| 134 | // Heap size |
| 135 | getrlimit (RLIMIT_DATA, &r); |
| 136 | r.rlim_cur = limit; |
| 137 | setrlimit (RLIMIT_DATA, &r); |
Gabor Greif | aa6b7fd | 2007-07-06 10:31:27 +0000 | [diff] [blame] | 138 | #ifdef RLIMIT_RSS |
Anton Korobeynikov | 9ba8a76 | 2007-02-16 19:11:07 +0000 | [diff] [blame] | 139 | // Resident set size. |
| 140 | getrlimit (RLIMIT_RSS, &r); |
| 141 | r.rlim_cur = limit; |
| 142 | setrlimit (RLIMIT_RSS, &r); |
Reid Spencer | 2e40d03 | 2007-04-23 07:22:51 +0000 | [diff] [blame] | 143 | #endif |
Devang Patel | 69bdf68 | 2007-06-04 15:28:57 +0000 | [diff] [blame] | 144 | #ifdef RLIMIT_AS // e.g. NetBSD doesn't have it. |
Anton Korobeynikov | 9ba8a76 | 2007-02-16 19:11:07 +0000 | [diff] [blame] | 145 | // Virtual memory. |
| 146 | getrlimit (RLIMIT_AS, &r); |
| 147 | r.rlim_cur = limit; |
| 148 | setrlimit (RLIMIT_AS, &r); |
| 149 | #endif |
Devang Patel | 69bdf68 | 2007-06-04 15:28:57 +0000 | [diff] [blame] | 150 | #endif |
Anton Korobeynikov | 9ba8a76 | 2007-02-16 19:11:07 +0000 | [diff] [blame] | 151 | } |
| 152 | |
Mikhail Glushenkov | 3140619 | 2009-07-18 21:43:12 +0000 | [diff] [blame] | 153 | bool |
| 154 | Program::Execute(const Path& path, |
| 155 | const char** args, |
| 156 | const char** envp, |
| 157 | const Path** redirects, |
| 158 | unsigned memoryLimit, |
| 159 | std::string* ErrMsg) |
Reid Spencer | 4ce5dc6 | 2006-08-21 06:02:44 +0000 | [diff] [blame] | 160 | { |
| 161 | if (!path.canExecute()) { |
| 162 | if (ErrMsg) |
Chris Lattner | 74382b7 | 2009-08-23 22:45:37 +0000 | [diff] [blame] | 163 | *ErrMsg = path.str() + " is not executable"; |
Mikhail Glushenkov | 3140619 | 2009-07-18 21:43:12 +0000 | [diff] [blame] | 164 | return false; |
Reid Spencer | 4ce5dc6 | 2006-08-21 06:02:44 +0000 | [diff] [blame] | 165 | } |
Reid Spencer | 52a7efa | 2004-08-29 19:20:41 +0000 | [diff] [blame] | 166 | |
Reid Spencer | 52a7efa | 2004-08-29 19:20:41 +0000 | [diff] [blame] | 167 | // Create a child process. |
Reid Spencer | 2a7d9e9 | 2004-12-19 18:00:44 +0000 | [diff] [blame] | 168 | int child = fork(); |
| 169 | switch (child) { |
Reid Spencer | 52a7efa | 2004-08-29 19:20:41 +0000 | [diff] [blame] | 170 | // An error occured: Return to the caller. |
| 171 | case -1: |
Reid Spencer | 4ce5dc6 | 2006-08-21 06:02:44 +0000 | [diff] [blame] | 172 | MakeErrMsg(ErrMsg, "Couldn't fork"); |
Mikhail Glushenkov | 3140619 | 2009-07-18 21:43:12 +0000 | [diff] [blame] | 173 | return false; |
Reid Spencer | 52a7efa | 2004-08-29 19:20:41 +0000 | [diff] [blame] | 174 | |
| 175 | // Child process: Execute the program. |
Reid Spencer | e2e2411 | 2004-12-14 04:18:51 +0000 | [diff] [blame] | 176 | case 0: { |
Reid Spencer | 2a7d9e9 | 2004-12-19 18:00:44 +0000 | [diff] [blame] | 177 | // Redirect file descriptors... |
| 178 | if (redirects) { |
Matthijs Kooijman | cf45ca0 | 2008-06-12 12:53:35 +0000 | [diff] [blame] | 179 | // Redirect stdin |
Mikhail Glushenkov | 3140619 | 2009-07-18 21:43:12 +0000 | [diff] [blame] | 180 | if (RedirectIO(redirects[0], 0, ErrMsg)) { return false; } |
Matthijs Kooijman | cf45ca0 | 2008-06-12 12:53:35 +0000 | [diff] [blame] | 181 | // Redirect stdout |
Mikhail Glushenkov | 3140619 | 2009-07-18 21:43:12 +0000 | [diff] [blame] | 182 | if (RedirectIO(redirects[1], 1, ErrMsg)) { return false; } |
Mikhail Glushenkov | 234f689 | 2009-07-17 20:38:17 +0000 | [diff] [blame] | 183 | if (redirects[1] && redirects[2] && |
Matthijs Kooijman | cf45ca0 | 2008-06-12 12:53:35 +0000 | [diff] [blame] | 184 | *(redirects[1]) == *(redirects[2])) { |
| 185 | // If stdout and stderr should go to the same place, redirect stderr |
| 186 | // to the FD already open for stdout. |
| 187 | if (-1 == dup2(1,2)) { |
| 188 | MakeErrMsg(ErrMsg, "Can't redirect stderr to stdout"); |
Mikhail Glushenkov | 3140619 | 2009-07-18 21:43:12 +0000 | [diff] [blame] | 189 | return false; |
Matthijs Kooijman | cf45ca0 | 2008-06-12 12:53:35 +0000 | [diff] [blame] | 190 | } |
| 191 | } else { |
| 192 | // Just redirect stderr |
Mikhail Glushenkov | 3140619 | 2009-07-18 21:43:12 +0000 | [diff] [blame] | 193 | if (RedirectIO(redirects[2], 2, ErrMsg)) { return false; } |
Reid Spencer | 2a7d9e9 | 2004-12-19 18:00:44 +0000 | [diff] [blame] | 194 | } |
| 195 | } |
| 196 | |
Anton Korobeynikov | 9ba8a76 | 2007-02-16 19:11:07 +0000 | [diff] [blame] | 197 | // Set memory limits |
| 198 | if (memoryLimit!=0) { |
| 199 | SetMemoryLimits(memoryLimit); |
| 200 | } |
Mikhail Glushenkov | 234f689 | 2009-07-17 20:38:17 +0000 | [diff] [blame] | 201 | |
Reid Spencer | 2a7d9e9 | 2004-12-19 18:00:44 +0000 | [diff] [blame] | 202 | // Execute! |
Evan Cheng | 2fafaf1 | 2006-06-09 20:43:11 +0000 | [diff] [blame] | 203 | if (envp != 0) |
Daniel Dunbar | 6db0a8b | 2009-08-04 20:32:25 +0000 | [diff] [blame] | 204 | execve(path.c_str(), (char**)args, (char**)envp); |
Evan Cheng | 2fafaf1 | 2006-06-09 20:43:11 +0000 | [diff] [blame] | 205 | else |
Daniel Dunbar | 6db0a8b | 2009-08-04 20:32:25 +0000 | [diff] [blame] | 206 | execv(path.c_str(), (char**)args); |
Dan Gohman | 3396852 | 2009-08-05 00:09:12 +0000 | [diff] [blame] | 207 | // If the execve() failed, we should exit. Follow Unix protocol and |
| 208 | // return 127 if the executable was not found, and 126 otherwise. |
| 209 | // Use _exit rather than exit so that atexit functions and static |
| 210 | // object destructors cloned from the parent process aren't |
| 211 | // redundantly run, and so that any data buffered in stdio buffers |
| 212 | // cloned from the parent aren't redundantly written out. |
| 213 | _exit(errno == ENOENT ? 127 : 126); |
Reid Spencer | e2e2411 | 2004-12-14 04:18:51 +0000 | [diff] [blame] | 214 | } |
Reid Spencer | 52a7efa | 2004-08-29 19:20:41 +0000 | [diff] [blame] | 215 | |
| 216 | // Parent process: Break out of the switch to do our processing. |
| 217 | default: |
| 218 | break; |
| 219 | } |
| 220 | |
Daniel Dunbar | 57d6903 | 2009-09-22 04:44:56 +0000 | [diff] [blame] | 221 | Data_ = reinterpret_cast<void*>(child); |
Mikhail Glushenkov | 3140619 | 2009-07-18 21:43:12 +0000 | [diff] [blame] | 222 | |
| 223 | return true; |
| 224 | } |
| 225 | |
| 226 | int |
| 227 | Program::Wait(unsigned secondsToWait, |
| 228 | std::string* ErrMsg) |
| 229 | { |
| 230 | #ifdef HAVE_SYS_WAIT_H |
Reid Spencer | 2a7d9e9 | 2004-12-19 18:00:44 +0000 | [diff] [blame] | 231 | struct sigaction Act, Old; |
| 232 | |
Daniel Dunbar | 57d6903 | 2009-09-22 04:44:56 +0000 | [diff] [blame] | 233 | if (Data_ == 0) { |
Mikhail Glushenkov | 3140619 | 2009-07-18 21:43:12 +0000 | [diff] [blame] | 234 | MakeErrMsg(ErrMsg, "Process not started!"); |
| 235 | return -1; |
| 236 | } |
| 237 | |
Duncan Sands | 68d29d0 | 2009-11-08 20:55:48 +0000 | [diff] [blame] | 238 | // Install a timeout handler. The handler itself does nothing, but the simple |
| 239 | // fact of having a handler at all causes the wait below to return with EINTR, |
| 240 | // unlike if we used SIG_IGN. |
Reid Spencer | 2a7d9e9 | 2004-12-19 18:00:44 +0000 | [diff] [blame] | 241 | if (secondsToWait) { |
Duncan Sands | 68d29d0 | 2009-11-08 20:55:48 +0000 | [diff] [blame] | 242 | Act.sa_sigaction = 0; |
| 243 | Act.sa_handler = TimeOutHandler; |
Reid Spencer | 2a7d9e9 | 2004-12-19 18:00:44 +0000 | [diff] [blame] | 244 | sigemptyset(&Act.sa_mask); |
Duncan Sands | 68d29d0 | 2009-11-08 20:55:48 +0000 | [diff] [blame] | 245 | Act.sa_flags = 0; |
Reid Spencer | 2a7d9e9 | 2004-12-19 18:00:44 +0000 | [diff] [blame] | 246 | sigaction(SIGALRM, &Act, &Old); |
| 247 | alarm(secondsToWait); |
| 248 | } |
| 249 | |
Reid Spencer | 52a7efa | 2004-08-29 19:20:41 +0000 | [diff] [blame] | 250 | // Parent process: Wait for the child process to terminate. |
| 251 | int status; |
Daniel Dunbar | 57d6903 | 2009-09-22 04:44:56 +0000 | [diff] [blame] | 252 | uint64_t pid = reinterpret_cast<uint64_t>(Data_); |
| 253 | pid_t child = static_cast<pid_t>(pid); |
Ted Kremenek | 8201ebd | 2009-10-22 22:16:17 +0000 | [diff] [blame] | 254 | while (waitpid(pid, &status, 0) != child) |
Reid Spencer | 2a7d9e9 | 2004-12-19 18:00:44 +0000 | [diff] [blame] | 255 | if (secondsToWait && errno == EINTR) { |
| 256 | // Kill the child. |
| 257 | kill(child, SIGKILL); |
Mikhail Glushenkov | 234f689 | 2009-07-17 20:38:17 +0000 | [diff] [blame] | 258 | |
Reid Spencer | 2a7d9e9 | 2004-12-19 18:00:44 +0000 | [diff] [blame] | 259 | // Turn off the alarm and restore the signal handler |
| 260 | alarm(0); |
| 261 | sigaction(SIGALRM, &Old, 0); |
| 262 | |
| 263 | // Wait for child to die |
| 264 | if (wait(&status) != child) |
Reid Spencer | 4ce5dc6 | 2006-08-21 06:02:44 +0000 | [diff] [blame] | 265 | MakeErrMsg(ErrMsg, "Child timed out but wouldn't die"); |
Devang Patel | a1e4bba | 2008-02-04 20:57:54 +0000 | [diff] [blame] | 266 | else |
| 267 | MakeErrMsg(ErrMsg, "Child timed out", 0); |
| 268 | |
Reid Spencer | 2a7d9e9 | 2004-12-19 18:00:44 +0000 | [diff] [blame] | 269 | return -1; // Timeout detected |
Devang Patel | a1e4bba | 2008-02-04 20:57:54 +0000 | [diff] [blame] | 270 | } else if (errno != EINTR) { |
Reid Spencer | 4ce5dc6 | 2006-08-21 06:02:44 +0000 | [diff] [blame] | 271 | MakeErrMsg(ErrMsg, "Error waiting for child process"); |
| 272 | return -1; |
Reid Spencer | 2a7d9e9 | 2004-12-19 18:00:44 +0000 | [diff] [blame] | 273 | } |
| 274 | |
| 275 | // We exited normally without timeout, so turn off the timer. |
| 276 | if (secondsToWait) { |
| 277 | alarm(0); |
| 278 | sigaction(SIGALRM, &Old, 0); |
| 279 | } |
Reid Spencer | 52a7efa | 2004-08-29 19:20:41 +0000 | [diff] [blame] | 280 | |
Reid Spencer | d555f41 | 2005-12-22 20:00:16 +0000 | [diff] [blame] | 281 | // Return the proper exit status. 0=success, >0 is programs' exit status, |
| 282 | // <0 means a signal was returned, -9999999 means the program dumped core. |
| 283 | int result = 0; |
Chris Lattner | 0228c0f | 2006-07-12 22:37:18 +0000 | [diff] [blame] | 284 | if (WIFEXITED(status)) |
Reid Spencer | d555f41 | 2005-12-22 20:00:16 +0000 | [diff] [blame] | 285 | result = WEXITSTATUS(status); |
Reid Spencer | 52a7efa | 2004-08-29 19:20:41 +0000 | [diff] [blame] | 286 | else if (WIFSIGNALED(status)) |
Reid Spencer | d555f41 | 2005-12-22 20:00:16 +0000 | [diff] [blame] | 287 | result = 0 - WTERMSIG(status); |
| 288 | #ifdef WCOREDUMP |
Chris Lattner | 0228c0f | 2006-07-12 22:37:18 +0000 | [diff] [blame] | 289 | else if (WCOREDUMP(status)) |
Reid Spencer | d555f41 | 2005-12-22 20:00:16 +0000 | [diff] [blame] | 290 | result |= 0x01000000; |
Reid Spencer | 52a7efa | 2004-08-29 19:20:41 +0000 | [diff] [blame] | 291 | #endif |
Reid Spencer | d555f41 | 2005-12-22 20:00:16 +0000 | [diff] [blame] | 292 | return result; |
| 293 | #else |
| 294 | return -99; |
| 295 | #endif |
Mikhail Glushenkov | 234f689 | 2009-07-17 20:38:17 +0000 | [diff] [blame] | 296 | |
Reid Spencer | 52a7efa | 2004-08-29 19:20:41 +0000 | [diff] [blame] | 297 | } |
| 298 | |
Mikhail Glushenkov | a607202 | 2009-09-08 19:50:27 +0000 | [diff] [blame] | 299 | bool |
| 300 | Program::Kill(std::string* ErrMsg) { |
Daniel Dunbar | 57d6903 | 2009-09-22 04:44:56 +0000 | [diff] [blame] | 301 | if (Data_ == 0) { |
Mikhail Glushenkov | a607202 | 2009-09-08 19:50:27 +0000 | [diff] [blame] | 302 | MakeErrMsg(ErrMsg, "Process not started!"); |
| 303 | return true; |
| 304 | } |
| 305 | |
Daniel Dunbar | 57d6903 | 2009-09-22 04:44:56 +0000 | [diff] [blame] | 306 | uint64_t pid64 = reinterpret_cast<uint64_t>(Data_); |
| 307 | pid_t pid = static_cast<pid_t>(pid64); |
| 308 | |
| 309 | if (kill(pid, SIGKILL) != 0) { |
Mikhail Glushenkov | 8add269 | 2009-09-09 09:51:47 +0000 | [diff] [blame] | 310 | MakeErrMsg(ErrMsg, "The process couldn't be killed!"); |
| 311 | return true; |
| 312 | } |
| 313 | |
| 314 | return false; |
Mikhail Glushenkov | a607202 | 2009-09-08 19:50:27 +0000 | [diff] [blame] | 315 | } |
| 316 | |
Reid Spencer | 4ce5dc6 | 2006-08-21 06:02:44 +0000 | [diff] [blame] | 317 | bool Program::ChangeStdinToBinary(){ |
Reid Spencer | 32f5553 | 2006-06-07 23:18:34 +0000 | [diff] [blame] | 318 | // Do nothing, as Unix doesn't differentiate between text and binary. |
Reid Spencer | 4ce5dc6 | 2006-08-21 06:02:44 +0000 | [diff] [blame] | 319 | return false; |
Reid Spencer | 32f5553 | 2006-06-07 23:18:34 +0000 | [diff] [blame] | 320 | } |
| 321 | |
Reid Spencer | 4ce5dc6 | 2006-08-21 06:02:44 +0000 | [diff] [blame] | 322 | bool Program::ChangeStdoutToBinary(){ |
Reid Spencer | 32f5553 | 2006-06-07 23:18:34 +0000 | [diff] [blame] | 323 | // Do nothing, as Unix doesn't differentiate between text and binary. |
Reid Spencer | 4ce5dc6 | 2006-08-21 06:02:44 +0000 | [diff] [blame] | 324 | return false; |
Reid Spencer | 32f5553 | 2006-06-07 23:18:34 +0000 | [diff] [blame] | 325 | } |
| 326 | |
Douglas Gregor | 21569cd | 2010-01-28 06:42:08 +0000 | [diff] [blame] | 327 | bool Program::ChangeStderrToBinary(){ |
| 328 | // Do nothing, as Unix doesn't differentiate between text and binary. |
| 329 | return false; |
| 330 | } |
| 331 | |
Reid Spencer | 52a7efa | 2004-08-29 19:20:41 +0000 | [diff] [blame] | 332 | } |