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 |
| 116 | if (-1 == dup2(InFD, FD)) { |
| 117 | MakeErrMsg(ErrMsg, "Cannot dup2"); |
| 118 | return true; |
| 119 | } |
Reid Spencer | 2a7d9e9 | 2004-12-19 18:00:44 +0000 | [diff] [blame] | 120 | close(InFD); // Close the original FD |
Reid Spencer | 4ce5dc6 | 2006-08-21 06:02:44 +0000 | [diff] [blame] | 121 | return false; |
Reid Spencer | 2a7d9e9 | 2004-12-19 18:00:44 +0000 | [diff] [blame] | 122 | } |
| 123 | |
Anton Korobeynikov | 9ba8a76 | 2007-02-16 19:11:07 +0000 | [diff] [blame] | 124 | static void SetMemoryLimits (unsigned size) |
| 125 | { |
| 126 | #if HAVE_SYS_RESOURCE_H |
| 127 | struct rlimit r; |
| 128 | __typeof__ (r.rlim_cur) limit = (__typeof__ (r.rlim_cur)) (size) * 1048576; |
| 129 | |
| 130 | // Heap size |
| 131 | getrlimit (RLIMIT_DATA, &r); |
| 132 | r.rlim_cur = limit; |
| 133 | setrlimit (RLIMIT_DATA, &r); |
Gabor Greif | aa6b7fd | 2007-07-06 10:31:27 +0000 | [diff] [blame] | 134 | #ifdef RLIMIT_RSS |
Anton Korobeynikov | 9ba8a76 | 2007-02-16 19:11:07 +0000 | [diff] [blame] | 135 | // Resident set size. |
| 136 | getrlimit (RLIMIT_RSS, &r); |
| 137 | r.rlim_cur = limit; |
| 138 | setrlimit (RLIMIT_RSS, &r); |
Reid Spencer | 2e40d03 | 2007-04-23 07:22:51 +0000 | [diff] [blame] | 139 | #endif |
Devang Patel | 69bdf68 | 2007-06-04 15:28:57 +0000 | [diff] [blame] | 140 | #ifdef RLIMIT_AS // e.g. NetBSD doesn't have it. |
Anton Korobeynikov | 9ba8a76 | 2007-02-16 19:11:07 +0000 | [diff] [blame] | 141 | // Virtual memory. |
| 142 | getrlimit (RLIMIT_AS, &r); |
| 143 | r.rlim_cur = limit; |
| 144 | setrlimit (RLIMIT_AS, &r); |
| 145 | #endif |
Devang Patel | 69bdf68 | 2007-06-04 15:28:57 +0000 | [diff] [blame] | 146 | #endif |
Anton Korobeynikov | 9ba8a76 | 2007-02-16 19:11:07 +0000 | [diff] [blame] | 147 | } |
| 148 | |
Mikhail Glushenkov | 3140619 | 2009-07-18 21:43:12 +0000 | [diff] [blame] | 149 | bool |
| 150 | Program::Execute(const Path& path, |
| 151 | const char** args, |
| 152 | const char** envp, |
| 153 | const Path** redirects, |
| 154 | unsigned memoryLimit, |
| 155 | std::string* ErrMsg) |
Reid Spencer | 4ce5dc6 | 2006-08-21 06:02:44 +0000 | [diff] [blame] | 156 | { |
| 157 | if (!path.canExecute()) { |
| 158 | if (ErrMsg) |
Chris Lattner | 74382b7 | 2009-08-23 22:45:37 +0000 | [diff] [blame] | 159 | *ErrMsg = path.str() + " is not executable"; |
Mikhail Glushenkov | 3140619 | 2009-07-18 21:43:12 +0000 | [diff] [blame] | 160 | return false; |
Reid Spencer | 4ce5dc6 | 2006-08-21 06:02:44 +0000 | [diff] [blame] | 161 | } |
Reid Spencer | 52a7efa | 2004-08-29 19:20:41 +0000 | [diff] [blame] | 162 | |
Reid Spencer | 52a7efa | 2004-08-29 19:20:41 +0000 | [diff] [blame] | 163 | // Create a child process. |
Reid Spencer | 2a7d9e9 | 2004-12-19 18:00:44 +0000 | [diff] [blame] | 164 | int child = fork(); |
| 165 | switch (child) { |
Reid Spencer | 52a7efa | 2004-08-29 19:20:41 +0000 | [diff] [blame] | 166 | // An error occured: Return to the caller. |
| 167 | case -1: |
Reid Spencer | 4ce5dc6 | 2006-08-21 06:02:44 +0000 | [diff] [blame] | 168 | MakeErrMsg(ErrMsg, "Couldn't fork"); |
Mikhail Glushenkov | 3140619 | 2009-07-18 21:43:12 +0000 | [diff] [blame] | 169 | return false; |
Reid Spencer | 52a7efa | 2004-08-29 19:20:41 +0000 | [diff] [blame] | 170 | |
| 171 | // Child process: Execute the program. |
Reid Spencer | e2e2411 | 2004-12-14 04:18:51 +0000 | [diff] [blame] | 172 | case 0: { |
Reid Spencer | 2a7d9e9 | 2004-12-19 18:00:44 +0000 | [diff] [blame] | 173 | // Redirect file descriptors... |
| 174 | if (redirects) { |
Matthijs Kooijman | cf45ca0 | 2008-06-12 12:53:35 +0000 | [diff] [blame] | 175 | // Redirect stdin |
Mikhail Glushenkov | 3140619 | 2009-07-18 21:43:12 +0000 | [diff] [blame] | 176 | if (RedirectIO(redirects[0], 0, ErrMsg)) { return false; } |
Matthijs Kooijman | cf45ca0 | 2008-06-12 12:53:35 +0000 | [diff] [blame] | 177 | // Redirect stdout |
Mikhail Glushenkov | 3140619 | 2009-07-18 21:43:12 +0000 | [diff] [blame] | 178 | if (RedirectIO(redirects[1], 1, ErrMsg)) { return false; } |
Mikhail Glushenkov | 234f689 | 2009-07-17 20:38:17 +0000 | [diff] [blame] | 179 | if (redirects[1] && redirects[2] && |
Matthijs Kooijman | cf45ca0 | 2008-06-12 12:53:35 +0000 | [diff] [blame] | 180 | *(redirects[1]) == *(redirects[2])) { |
| 181 | // If stdout and stderr should go to the same place, redirect stderr |
| 182 | // to the FD already open for stdout. |
| 183 | if (-1 == dup2(1,2)) { |
| 184 | MakeErrMsg(ErrMsg, "Can't redirect stderr to stdout"); |
Mikhail Glushenkov | 3140619 | 2009-07-18 21:43:12 +0000 | [diff] [blame] | 185 | return false; |
Matthijs Kooijman | cf45ca0 | 2008-06-12 12:53:35 +0000 | [diff] [blame] | 186 | } |
| 187 | } else { |
| 188 | // Just redirect stderr |
Mikhail Glushenkov | 3140619 | 2009-07-18 21:43:12 +0000 | [diff] [blame] | 189 | if (RedirectIO(redirects[2], 2, ErrMsg)) { return false; } |
Reid Spencer | 2a7d9e9 | 2004-12-19 18:00:44 +0000 | [diff] [blame] | 190 | } |
| 191 | } |
| 192 | |
Anton Korobeynikov | 9ba8a76 | 2007-02-16 19:11:07 +0000 | [diff] [blame] | 193 | // Set memory limits |
| 194 | if (memoryLimit!=0) { |
| 195 | SetMemoryLimits(memoryLimit); |
| 196 | } |
Mikhail Glushenkov | 234f689 | 2009-07-17 20:38:17 +0000 | [diff] [blame] | 197 | |
Reid Spencer | 2a7d9e9 | 2004-12-19 18:00:44 +0000 | [diff] [blame] | 198 | // Execute! |
Evan Cheng | 2fafaf1 | 2006-06-09 20:43:11 +0000 | [diff] [blame] | 199 | if (envp != 0) |
Daniel Dunbar | 6db0a8b | 2009-08-04 20:32:25 +0000 | [diff] [blame] | 200 | execve(path.c_str(), (char**)args, (char**)envp); |
Evan Cheng | 2fafaf1 | 2006-06-09 20:43:11 +0000 | [diff] [blame] | 201 | else |
Daniel Dunbar | 6db0a8b | 2009-08-04 20:32:25 +0000 | [diff] [blame] | 202 | execv(path.c_str(), (char**)args); |
Dan Gohman | 3396852 | 2009-08-05 00:09:12 +0000 | [diff] [blame] | 203 | // If the execve() failed, we should exit. Follow Unix protocol and |
| 204 | // return 127 if the executable was not found, and 126 otherwise. |
| 205 | // Use _exit rather than exit so that atexit functions and static |
| 206 | // object destructors cloned from the parent process aren't |
| 207 | // redundantly run, and so that any data buffered in stdio buffers |
| 208 | // cloned from the parent aren't redundantly written out. |
| 209 | _exit(errno == ENOENT ? 127 : 126); |
Reid Spencer | e2e2411 | 2004-12-14 04:18:51 +0000 | [diff] [blame] | 210 | } |
Reid Spencer | 52a7efa | 2004-08-29 19:20:41 +0000 | [diff] [blame] | 211 | |
| 212 | // Parent process: Break out of the switch to do our processing. |
| 213 | default: |
| 214 | break; |
| 215 | } |
| 216 | |
Daniel Dunbar | 57d6903 | 2009-09-22 04:44:56 +0000 | [diff] [blame^] | 217 | Data_ = reinterpret_cast<void*>(child); |
Mikhail Glushenkov | 3140619 | 2009-07-18 21:43:12 +0000 | [diff] [blame] | 218 | |
| 219 | return true; |
| 220 | } |
| 221 | |
| 222 | int |
| 223 | Program::Wait(unsigned secondsToWait, |
| 224 | std::string* ErrMsg) |
| 225 | { |
| 226 | #ifdef HAVE_SYS_WAIT_H |
Reid Spencer | 2a7d9e9 | 2004-12-19 18:00:44 +0000 | [diff] [blame] | 227 | struct sigaction Act, Old; |
| 228 | |
Daniel Dunbar | 57d6903 | 2009-09-22 04:44:56 +0000 | [diff] [blame^] | 229 | if (Data_ == 0) { |
Mikhail Glushenkov | 3140619 | 2009-07-18 21:43:12 +0000 | [diff] [blame] | 230 | MakeErrMsg(ErrMsg, "Process not started!"); |
| 231 | return -1; |
| 232 | } |
| 233 | |
Reid Spencer | 2a7d9e9 | 2004-12-19 18:00:44 +0000 | [diff] [blame] | 234 | // Install a timeout handler. |
| 235 | if (secondsToWait) { |
Duncan Sands | 3429c5e | 2009-09-07 05:58:25 +0000 | [diff] [blame] | 236 | memset(&Act, 0, sizeof(Act)); |
| 237 | Act.sa_handler = SIG_IGN; |
Reid Spencer | 2a7d9e9 | 2004-12-19 18:00:44 +0000 | [diff] [blame] | 238 | sigemptyset(&Act.sa_mask); |
Reid Spencer | 2a7d9e9 | 2004-12-19 18:00:44 +0000 | [diff] [blame] | 239 | sigaction(SIGALRM, &Act, &Old); |
| 240 | alarm(secondsToWait); |
| 241 | } |
| 242 | |
Reid Spencer | 52a7efa | 2004-08-29 19:20:41 +0000 | [diff] [blame] | 243 | // Parent process: Wait for the child process to terminate. |
| 244 | int status; |
Daniel Dunbar | 57d6903 | 2009-09-22 04:44:56 +0000 | [diff] [blame^] | 245 | uint64_t pid = reinterpret_cast<uint64_t>(Data_); |
| 246 | pid_t child = static_cast<pid_t>(pid); |
Reid Spencer | 2a7d9e9 | 2004-12-19 18:00:44 +0000 | [diff] [blame] | 247 | while (wait(&status) != child) |
| 248 | if (secondsToWait && errno == EINTR) { |
| 249 | // Kill the child. |
| 250 | kill(child, SIGKILL); |
Mikhail Glushenkov | 234f689 | 2009-07-17 20:38:17 +0000 | [diff] [blame] | 251 | |
Reid Spencer | 2a7d9e9 | 2004-12-19 18:00:44 +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) |
Reid Spencer | 4ce5dc6 | 2006-08-21 06:02:44 +0000 | [diff] [blame] | 258 | MakeErrMsg(ErrMsg, "Child timed out but wouldn't die"); |
Devang Patel | a1e4bba | 2008-02-04 20:57:54 +0000 | [diff] [blame] | 259 | else |
| 260 | MakeErrMsg(ErrMsg, "Child timed out", 0); |
| 261 | |
Reid Spencer | 2a7d9e9 | 2004-12-19 18:00:44 +0000 | [diff] [blame] | 262 | return -1; // Timeout detected |
Devang Patel | a1e4bba | 2008-02-04 20:57:54 +0000 | [diff] [blame] | 263 | } else if (errno != EINTR) { |
Reid Spencer | 4ce5dc6 | 2006-08-21 06:02:44 +0000 | [diff] [blame] | 264 | MakeErrMsg(ErrMsg, "Error waiting for child process"); |
| 265 | return -1; |
Reid Spencer | 2a7d9e9 | 2004-12-19 18:00:44 +0000 | [diff] [blame] | 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 | } |
Reid Spencer | 52a7efa | 2004-08-29 19:20:41 +0000 | [diff] [blame] | 273 | |
Reid Spencer | d555f41 | 2005-12-22 20:00:16 +0000 | [diff] [blame] | 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; |
Chris Lattner | 0228c0f | 2006-07-12 22:37:18 +0000 | [diff] [blame] | 277 | if (WIFEXITED(status)) |
Reid Spencer | d555f41 | 2005-12-22 20:00:16 +0000 | [diff] [blame] | 278 | result = WEXITSTATUS(status); |
Reid Spencer | 52a7efa | 2004-08-29 19:20:41 +0000 | [diff] [blame] | 279 | else if (WIFSIGNALED(status)) |
Reid Spencer | d555f41 | 2005-12-22 20:00:16 +0000 | [diff] [blame] | 280 | result = 0 - WTERMSIG(status); |
| 281 | #ifdef WCOREDUMP |
Chris Lattner | 0228c0f | 2006-07-12 22:37:18 +0000 | [diff] [blame] | 282 | else if (WCOREDUMP(status)) |
Reid Spencer | d555f41 | 2005-12-22 20:00:16 +0000 | [diff] [blame] | 283 | result |= 0x01000000; |
Reid Spencer | 52a7efa | 2004-08-29 19:20:41 +0000 | [diff] [blame] | 284 | #endif |
Reid Spencer | d555f41 | 2005-12-22 20:00:16 +0000 | [diff] [blame] | 285 | return result; |
| 286 | #else |
| 287 | return -99; |
| 288 | #endif |
Mikhail Glushenkov | 234f689 | 2009-07-17 20:38:17 +0000 | [diff] [blame] | 289 | |
Reid Spencer | 52a7efa | 2004-08-29 19:20:41 +0000 | [diff] [blame] | 290 | } |
| 291 | |
Mikhail Glushenkov | a607202 | 2009-09-08 19:50:27 +0000 | [diff] [blame] | 292 | bool |
| 293 | Program::Kill(std::string* ErrMsg) { |
Daniel Dunbar | 57d6903 | 2009-09-22 04:44:56 +0000 | [diff] [blame^] | 294 | if (Data_ == 0) { |
Mikhail Glushenkov | a607202 | 2009-09-08 19:50:27 +0000 | [diff] [blame] | 295 | MakeErrMsg(ErrMsg, "Process not started!"); |
| 296 | return true; |
| 297 | } |
| 298 | |
Daniel Dunbar | 57d6903 | 2009-09-22 04:44:56 +0000 | [diff] [blame^] | 299 | uint64_t pid64 = reinterpret_cast<uint64_t>(Data_); |
| 300 | pid_t pid = static_cast<pid_t>(pid64); |
| 301 | |
| 302 | if (kill(pid, SIGKILL) != 0) { |
Mikhail Glushenkov | 8add269 | 2009-09-09 09:51:47 +0000 | [diff] [blame] | 303 | MakeErrMsg(ErrMsg, "The process couldn't be killed!"); |
| 304 | return true; |
| 305 | } |
| 306 | |
| 307 | return false; |
Mikhail Glushenkov | a607202 | 2009-09-08 19:50:27 +0000 | [diff] [blame] | 308 | } |
| 309 | |
Reid Spencer | 4ce5dc6 | 2006-08-21 06:02:44 +0000 | [diff] [blame] | 310 | bool Program::ChangeStdinToBinary(){ |
Reid Spencer | 32f5553 | 2006-06-07 23:18:34 +0000 | [diff] [blame] | 311 | // Do nothing, as Unix doesn't differentiate between text and binary. |
Reid Spencer | 4ce5dc6 | 2006-08-21 06:02:44 +0000 | [diff] [blame] | 312 | return false; |
Reid Spencer | 32f5553 | 2006-06-07 23:18:34 +0000 | [diff] [blame] | 313 | } |
| 314 | |
Reid Spencer | 4ce5dc6 | 2006-08-21 06:02:44 +0000 | [diff] [blame] | 315 | bool Program::ChangeStdoutToBinary(){ |
Reid Spencer | 32f5553 | 2006-06-07 23:18:34 +0000 | [diff] [blame] | 316 | // Do nothing, as Unix doesn't differentiate between text and binary. |
Reid Spencer | 4ce5dc6 | 2006-08-21 06:02:44 +0000 | [diff] [blame] | 317 | return false; |
Reid Spencer | 32f5553 | 2006-06-07 23:18:34 +0000 | [diff] [blame] | 318 | } |
| 319 | |
Reid Spencer | 52a7efa | 2004-08-29 19:20:41 +0000 | [diff] [blame] | 320 | } |