Charles Davis | 54c9eb6 | 2010-11-29 19:44:50 +0000 | [diff] [blame] | 1 | //===- llvm/Support/Unix/Program.cpp -----------------------------*- C++ -*-===// |
Mikhail Glushenkov | 28309ac | 2009-07-17 20:38:17 +0000 | [diff] [blame] | 2 | // |
Reid Spencer | 76b83a1 | 2004-08-29 19:20:41 +0000 | [diff] [blame] | 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | f3ebc3f | 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 | 28309ac | 2009-07-17 20:38:17 +0000 | [diff] [blame] | 7 | // |
Reid Spencer | 76b83a1 | 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 | |
| 19 | #include "Unix.h" |
Michael J. Spencer | 65ffd92 | 2014-11-04 01:29:29 +0000 | [diff] [blame] | 20 | #include "llvm/ADT/StringExtras.h" |
Evgeniy Stepanov | 1f5a714 | 2013-02-04 07:03:24 +0000 | [diff] [blame] | 21 | #include "llvm/Support/Compiler.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 22 | #include "llvm/Support/FileSystem.h" |
Rafael Espindola | 9c35966 | 2014-09-03 20:02:00 +0000 | [diff] [blame] | 23 | #include "llvm/Support/raw_ostream.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 24 | #include <llvm/Config/config.h> |
Reid Spencer | d554bbc | 2004-12-27 06:16:52 +0000 | [diff] [blame] | 25 | #if HAVE_SYS_STAT_H |
| 26 | #include <sys/stat.h> |
| 27 | #endif |
Anton Korobeynikov | d01defe | 2007-02-16 19:11:07 +0000 | [diff] [blame] | 28 | #if HAVE_SYS_RESOURCE_H |
| 29 | #include <sys/resource.h> |
| 30 | #endif |
Reid Spencer | d554bbc | 2004-12-27 06:16:52 +0000 | [diff] [blame] | 31 | #if HAVE_SIGNAL_H |
| 32 | #include <signal.h> |
| 33 | #endif |
| 34 | #if HAVE_FCNTL_H |
| 35 | #include <fcntl.h> |
| 36 | #endif |
Rafael Espindola | cd848c0 | 2013-04-11 14:06:34 +0000 | [diff] [blame] | 37 | #if HAVE_UNISTD_H |
| 38 | #include <unistd.h> |
| 39 | #endif |
Chris Lattner | a50738a | 2010-04-18 04:14:37 +0000 | [diff] [blame] | 40 | #ifdef HAVE_POSIX_SPAWN |
Rafael Espindola | 9ab9fe9 | 2013-10-08 16:12:58 +0000 | [diff] [blame] | 41 | #ifdef __sun__ |
| 42 | #define _RESTRICT_KYWD |
| 43 | #endif |
Chris Lattner | a50738a | 2010-04-18 04:14:37 +0000 | [diff] [blame] | 44 | #include <spawn.h> |
Nick Lewycky | 554a398 | 2010-04-18 07:07:48 +0000 | [diff] [blame] | 45 | #if !defined(__APPLE__) |
| 46 | extern char **environ; |
Benjamin Kramer | 1360e9e | 2010-04-18 09:16:04 +0000 | [diff] [blame] | 47 | #else |
| 48 | #include <crt_externs.h> // _NSGetEnviron |
Nick Lewycky | 554a398 | 2010-04-18 07:07:48 +0000 | [diff] [blame] | 49 | #endif |
Chris Lattner | a50738a | 2010-04-18 04:14:37 +0000 | [diff] [blame] | 50 | #endif |
Reid Spencer | 76b83a1 | 2004-08-29 19:20:41 +0000 | [diff] [blame] | 51 | |
Reid Spencer | 76b83a1 | 2004-08-29 19:20:41 +0000 | [diff] [blame] | 52 | namespace llvm { |
Rafael Espindola | 3acea39 | 2014-06-12 21:46:39 +0000 | [diff] [blame] | 53 | |
Reid Spencer | 76b83a1 | 2004-08-29 19:20:41 +0000 | [diff] [blame] | 54 | using namespace sys; |
| 55 | |
Tareq A. Siraj | d88b983 | 2013-10-01 14:28:18 +0000 | [diff] [blame] | 56 | ProcessInfo::ProcessInfo() : Pid(0), ReturnCode(0) {} |
| 57 | |
Michael J. Spencer | 65ffd92 | 2014-11-04 01:29:29 +0000 | [diff] [blame] | 58 | ErrorOr<std::string> sys::findProgramByName(StringRef Name, |
| 59 | ArrayRef<StringRef> Paths) { |
| 60 | assert(!Name.empty() && "Must have a name!"); |
| 61 | // Use the given path verbatim if it contains any slashes; this matches |
| 62 | // the behavior of sh(1) and friends. |
| 63 | if (Name.find('/') != StringRef::npos) |
| 64 | return std::string(Name); |
| 65 | |
Chandler Carruth | ec8406d | 2014-12-02 00:52:01 +0000 | [diff] [blame] | 66 | SmallVector<StringRef, 16> EnvironmentPaths; |
| 67 | if (Paths.empty()) |
| 68 | if (const char *PathEnv = std::getenv("PATH")) { |
| 69 | SplitString(PathEnv, EnvironmentPaths, ":"); |
| 70 | Paths = EnvironmentPaths; |
| 71 | } |
Michael J. Spencer | 65ffd92 | 2014-11-04 01:29:29 +0000 | [diff] [blame] | 72 | |
| 73 | for (auto Path : Paths) { |
| 74 | if (Path.empty()) |
| 75 | continue; |
| 76 | |
| 77 | // Check to see if this first directory contains the executable... |
| 78 | SmallString<128> FilePath(Path); |
| 79 | sys::path::append(FilePath, Name); |
| 80 | if (sys::fs::can_execute(FilePath.c_str())) |
| 81 | return std::string(FilePath.str()); // Found the executable! |
| 82 | } |
| 83 | return std::errc::no_such_file_or_directory; |
| 84 | } |
| 85 | |
Rafael Espindola | b0a5c96 | 2013-06-14 19:38:45 +0000 | [diff] [blame] | 86 | static bool RedirectIO(const StringRef *Path, int FD, std::string* ErrMsg) { |
Craig Topper | e73658d | 2014-04-28 04:05:08 +0000 | [diff] [blame] | 87 | if (!Path) // Noop |
Matthijs Kooijman | 616e484 | 2008-06-12 10:47:18 +0000 | [diff] [blame] | 88 | return false; |
Rafael Espindola | b0a5c96 | 2013-06-14 19:38:45 +0000 | [diff] [blame] | 89 | std::string File; |
| 90 | if (Path->empty()) |
Matthijs Kooijman | 616e484 | 2008-06-12 10:47:18 +0000 | [diff] [blame] | 91 | // Redirect empty paths to /dev/null |
| 92 | File = "/dev/null"; |
| 93 | else |
Rafael Espindola | b0a5c96 | 2013-06-14 19:38:45 +0000 | [diff] [blame] | 94 | File = *Path; |
Reid Spencer | 6cb551b | 2004-12-19 18:00:44 +0000 | [diff] [blame] | 95 | |
| 96 | // Open the file |
Rafael Espindola | b0a5c96 | 2013-06-14 19:38:45 +0000 | [diff] [blame] | 97 | int InFD = open(File.c_str(), FD == 0 ? O_RDONLY : O_WRONLY|O_CREAT, 0666); |
Reid Spencer | 6cb551b | 2004-12-19 18:00:44 +0000 | [diff] [blame] | 98 | if (InFD == -1) { |
Rafael Espindola | b0a5c96 | 2013-06-14 19:38:45 +0000 | [diff] [blame] | 99 | MakeErrMsg(ErrMsg, "Cannot open file '" + File + "' for " |
Daniel Dunbar | 3222b9b | 2009-04-20 20:50:13 +0000 | [diff] [blame] | 100 | + (FD == 0 ? "input" : "output")); |
Reid Spencer | 42bcf6e | 2006-08-21 06:02:44 +0000 | [diff] [blame] | 101 | return true; |
Reid Spencer | 6cb551b | 2004-12-19 18:00:44 +0000 | [diff] [blame] | 102 | } |
| 103 | |
Reid Spencer | 42bcf6e | 2006-08-21 06:02:44 +0000 | [diff] [blame] | 104 | // Install it as the requested FD |
Chris Lattner | 65a437f | 2010-03-14 23:16:45 +0000 | [diff] [blame] | 105 | if (dup2(InFD, FD) == -1) { |
Reid Spencer | 42bcf6e | 2006-08-21 06:02:44 +0000 | [diff] [blame] | 106 | MakeErrMsg(ErrMsg, "Cannot dup2"); |
Chris Lattner | 65a437f | 2010-03-14 23:16:45 +0000 | [diff] [blame] | 107 | close(InFD); |
Reid Spencer | 42bcf6e | 2006-08-21 06:02:44 +0000 | [diff] [blame] | 108 | return true; |
| 109 | } |
Reid Spencer | 6cb551b | 2004-12-19 18:00:44 +0000 | [diff] [blame] | 110 | close(InFD); // Close the original FD |
Reid Spencer | 42bcf6e | 2006-08-21 06:02:44 +0000 | [diff] [blame] | 111 | return false; |
Reid Spencer | 6cb551b | 2004-12-19 18:00:44 +0000 | [diff] [blame] | 112 | } |
| 113 | |
Chris Lattner | a50738a | 2010-04-18 04:14:37 +0000 | [diff] [blame] | 114 | #ifdef HAVE_POSIX_SPAWN |
Rafael Espindola | 70d98f4 | 2013-07-26 16:21:31 +0000 | [diff] [blame] | 115 | static bool RedirectIO_PS(const std::string *Path, int FD, std::string *ErrMsg, |
Benjamin Kramer | dce83c5 | 2011-03-20 15:52:24 +0000 | [diff] [blame] | 116 | posix_spawn_file_actions_t *FileActions) { |
Craig Topper | e73658d | 2014-04-28 04:05:08 +0000 | [diff] [blame] | 117 | if (!Path) // Noop |
Chris Lattner | a50738a | 2010-04-18 04:14:37 +0000 | [diff] [blame] | 118 | return false; |
Rafael Espindola | 70d98f4 | 2013-07-26 16:21:31 +0000 | [diff] [blame] | 119 | const char *File; |
Rafael Espindola | b0a5c96 | 2013-06-14 19:38:45 +0000 | [diff] [blame] | 120 | if (Path->empty()) |
Chris Lattner | a50738a | 2010-04-18 04:14:37 +0000 | [diff] [blame] | 121 | // Redirect empty paths to /dev/null |
| 122 | File = "/dev/null"; |
| 123 | else |
Rafael Espindola | 70d98f4 | 2013-07-26 16:21:31 +0000 | [diff] [blame] | 124 | File = Path->c_str(); |
Benjamin Kramer | 5b4be74 | 2010-04-18 09:19:41 +0000 | [diff] [blame] | 125 | |
Rafael Espindola | b0a5c96 | 2013-06-14 19:38:45 +0000 | [diff] [blame] | 126 | if (int Err = posix_spawn_file_actions_addopen( |
Rafael Espindola | 70d98f4 | 2013-07-26 16:21:31 +0000 | [diff] [blame] | 127 | FileActions, FD, File, |
Rafael Espindola | b0a5c96 | 2013-06-14 19:38:45 +0000 | [diff] [blame] | 128 | FD == 0 ? O_RDONLY : O_WRONLY | O_CREAT, 0666)) |
Chris Lattner | a50738a | 2010-04-18 04:14:37 +0000 | [diff] [blame] | 129 | return MakeErrMsg(ErrMsg, "Cannot dup2", Err); |
| 130 | return false; |
| 131 | } |
| 132 | #endif |
| 133 | |
Duncan Sands | fec62f0 | 2009-11-08 20:55:48 +0000 | [diff] [blame] | 134 | static void TimeOutHandler(int Sig) { |
| 135 | } |
| 136 | |
Anton Korobeynikov | d01defe | 2007-02-16 19:11:07 +0000 | [diff] [blame] | 137 | static void SetMemoryLimits (unsigned size) |
| 138 | { |
Chris Lattner | 62f50da | 2010-02-12 00:37:46 +0000 | [diff] [blame] | 139 | #if HAVE_SYS_RESOURCE_H && HAVE_GETRLIMIT && HAVE_SETRLIMIT |
Anton Korobeynikov | d01defe | 2007-02-16 19:11:07 +0000 | [diff] [blame] | 140 | struct rlimit r; |
| 141 | __typeof__ (r.rlim_cur) limit = (__typeof__ (r.rlim_cur)) (size) * 1048576; |
| 142 | |
| 143 | // Heap size |
| 144 | getrlimit (RLIMIT_DATA, &r); |
| 145 | r.rlim_cur = limit; |
| 146 | setrlimit (RLIMIT_DATA, &r); |
Gabor Greif | 0e21980 | 2007-07-06 10:31:27 +0000 | [diff] [blame] | 147 | #ifdef RLIMIT_RSS |
Anton Korobeynikov | d01defe | 2007-02-16 19:11:07 +0000 | [diff] [blame] | 148 | // Resident set size. |
| 149 | getrlimit (RLIMIT_RSS, &r); |
| 150 | r.rlim_cur = limit; |
| 151 | setrlimit (RLIMIT_RSS, &r); |
Reid Spencer | e2e9b37 | 2007-04-23 07:22:51 +0000 | [diff] [blame] | 152 | #endif |
Devang Patel | d609374a | 2007-06-04 15:28:57 +0000 | [diff] [blame] | 153 | #ifdef RLIMIT_AS // e.g. NetBSD doesn't have it. |
Evgeniy Stepanov | 1f5a714 | 2013-02-04 07:03:24 +0000 | [diff] [blame] | 154 | // Don't set virtual memory limit if built with any Sanitizer. They need 80Tb |
| 155 | // of virtual memory for shadow memory mapping. |
| 156 | #if !LLVM_MEMORY_SANITIZER_BUILD && !LLVM_ADDRESS_SANITIZER_BUILD |
Anton Korobeynikov | d01defe | 2007-02-16 19:11:07 +0000 | [diff] [blame] | 157 | // Virtual memory. |
| 158 | getrlimit (RLIMIT_AS, &r); |
| 159 | r.rlim_cur = limit; |
| 160 | setrlimit (RLIMIT_AS, &r); |
| 161 | #endif |
Devang Patel | d609374a | 2007-06-04 15:28:57 +0000 | [diff] [blame] | 162 | #endif |
Evgeniy Stepanov | 1f5a714 | 2013-02-04 07:03:24 +0000 | [diff] [blame] | 163 | #endif |
Anton Korobeynikov | d01defe | 2007-02-16 19:11:07 +0000 | [diff] [blame] | 164 | } |
| 165 | |
Rafael Espindola | cb2eca0 | 2013-06-12 20:58:35 +0000 | [diff] [blame] | 166 | } |
| 167 | |
Tareq A. Siraj | d88b983 | 2013-10-01 14:28:18 +0000 | [diff] [blame] | 168 | static bool Execute(ProcessInfo &PI, StringRef Program, const char **args, |
Rafael Espindola | b0a5c96 | 2013-06-14 19:38:45 +0000 | [diff] [blame] | 169 | const char **envp, const StringRef **redirects, |
Rafael Espindola | cb2eca0 | 2013-06-12 20:58:35 +0000 | [diff] [blame] | 170 | unsigned memoryLimit, std::string *ErrMsg) { |
Tareq A. Siraj | d88b983 | 2013-10-01 14:28:18 +0000 | [diff] [blame] | 171 | if (!llvm::sys::fs::exists(Program)) { |
| 172 | if (ErrMsg) |
| 173 | *ErrMsg = std::string("Executable \"") + Program.str() + |
| 174 | std::string("\" doesn't exist!"); |
| 175 | return false; |
| 176 | } |
| 177 | |
Chris Lattner | a50738a | 2010-04-18 04:14:37 +0000 | [diff] [blame] | 178 | // If this OS has posix_spawn and there is no memory limit being implied, use |
| 179 | // posix_spawn. It is more efficient than fork/exec. |
| 180 | #ifdef HAVE_POSIX_SPAWN |
| 181 | if (memoryLimit == 0) { |
Benjamin Kramer | dce83c5 | 2011-03-20 15:52:24 +0000 | [diff] [blame] | 182 | posix_spawn_file_actions_t FileActionsStore; |
Craig Topper | e73658d | 2014-04-28 04:05:08 +0000 | [diff] [blame] | 183 | posix_spawn_file_actions_t *FileActions = nullptr; |
Chris Lattner | a50738a | 2010-04-18 04:14:37 +0000 | [diff] [blame] | 184 | |
Rafael Espindola | 70d98f4 | 2013-07-26 16:21:31 +0000 | [diff] [blame] | 185 | // If we call posix_spawn_file_actions_addopen we have to make sure the |
Rafael Espindola | 1cfc5dd | 2013-07-26 20:44:45 +0000 | [diff] [blame] | 186 | // c strings we pass to it stay alive until the call to posix_spawn, |
Rafael Espindola | 70d98f4 | 2013-07-26 16:21:31 +0000 | [diff] [blame] | 187 | // so we copy any StringRefs into this variable. |
| 188 | std::string RedirectsStorage[3]; |
| 189 | |
Chris Lattner | a50738a | 2010-04-18 04:14:37 +0000 | [diff] [blame] | 190 | if (redirects) { |
Craig Topper | e73658d | 2014-04-28 04:05:08 +0000 | [diff] [blame] | 191 | std::string *RedirectsStr[3] = {nullptr, nullptr, nullptr}; |
Rafael Espindola | 70d98f4 | 2013-07-26 16:21:31 +0000 | [diff] [blame] | 192 | for (int I = 0; I < 3; ++I) { |
| 193 | if (redirects[I]) { |
| 194 | RedirectsStorage[I] = *redirects[I]; |
| 195 | RedirectsStr[I] = &RedirectsStorage[I]; |
| 196 | } |
| 197 | } |
| 198 | |
Benjamin Kramer | dce83c5 | 2011-03-20 15:52:24 +0000 | [diff] [blame] | 199 | FileActions = &FileActionsStore; |
| 200 | posix_spawn_file_actions_init(FileActions); |
| 201 | |
Chris Lattner | a50738a | 2010-04-18 04:14:37 +0000 | [diff] [blame] | 202 | // Redirect stdin/stdout. |
Rafael Espindola | 70d98f4 | 2013-07-26 16:21:31 +0000 | [diff] [blame] | 203 | if (RedirectIO_PS(RedirectsStr[0], 0, ErrMsg, FileActions) || |
| 204 | RedirectIO_PS(RedirectsStr[1], 1, ErrMsg, FileActions)) |
Chris Lattner | a50738a | 2010-04-18 04:14:37 +0000 | [diff] [blame] | 205 | return false; |
Craig Topper | e73658d | 2014-04-28 04:05:08 +0000 | [diff] [blame] | 206 | if (redirects[1] == nullptr || redirects[2] == nullptr || |
Chris Lattner | a50738a | 2010-04-18 04:14:37 +0000 | [diff] [blame] | 207 | *redirects[1] != *redirects[2]) { |
| 208 | // Just redirect stderr |
Rafael Espindola | 70d98f4 | 2013-07-26 16:21:31 +0000 | [diff] [blame] | 209 | if (RedirectIO_PS(RedirectsStr[2], 2, ErrMsg, FileActions)) |
| 210 | return false; |
Mikhail Glushenkov | 0e9d9b5 | 2010-10-28 19:32:53 +0000 | [diff] [blame] | 211 | } else { |
Chris Lattner | a50738a | 2010-04-18 04:14:37 +0000 | [diff] [blame] | 212 | // If stdout and stderr should go to the same place, redirect stderr |
| 213 | // to the FD already open for stdout. |
Benjamin Kramer | dce83c5 | 2011-03-20 15:52:24 +0000 | [diff] [blame] | 214 | if (int Err = posix_spawn_file_actions_adddup2(FileActions, 1, 2)) |
Chris Lattner | a50738a | 2010-04-18 04:14:37 +0000 | [diff] [blame] | 215 | return !MakeErrMsg(ErrMsg, "Can't redirect stderr to stdout", Err); |
| 216 | } |
| 217 | } |
| 218 | |
Benjamin Kramer | 1360e9e | 2010-04-18 09:16:04 +0000 | [diff] [blame] | 219 | if (!envp) |
Nick Lewycky | 554a398 | 2010-04-18 07:07:48 +0000 | [diff] [blame] | 220 | #if !defined(__APPLE__) |
Dan Gohman | f656397 | 2010-04-19 15:55:10 +0000 | [diff] [blame] | 221 | envp = const_cast<const char **>(environ); |
Benjamin Kramer | 1360e9e | 2010-04-18 09:16:04 +0000 | [diff] [blame] | 222 | #else |
Dan Gohman | f656397 | 2010-04-19 15:55:10 +0000 | [diff] [blame] | 223 | // environ is missing in dylibs. |
| 224 | envp = const_cast<const char **>(*_NSGetEnviron()); |
Nick Lewycky | 554a398 | 2010-04-18 07:07:48 +0000 | [diff] [blame] | 225 | #endif |
| 226 | |
Daniel Dunbar | 31158b5 | 2010-09-30 23:56:49 +0000 | [diff] [blame] | 227 | // Explicitly initialized to prevent what appears to be a valgrind false |
| 228 | // positive. |
| 229 | pid_t PID = 0; |
Craig Topper | e73658d | 2014-04-28 04:05:08 +0000 | [diff] [blame] | 230 | int Err = posix_spawn(&PID, Program.str().c_str(), FileActions, |
| 231 | /*attrp*/nullptr, const_cast<char **>(args), |
| 232 | const_cast<char **>(envp)); |
Mikhail Glushenkov | 0e9d9b5 | 2010-10-28 19:32:53 +0000 | [diff] [blame] | 233 | |
Benjamin Kramer | dce83c5 | 2011-03-20 15:52:24 +0000 | [diff] [blame] | 234 | if (FileActions) |
| 235 | posix_spawn_file_actions_destroy(FileActions); |
Chris Lattner | a50738a | 2010-04-18 04:14:37 +0000 | [diff] [blame] | 236 | |
| 237 | if (Err) |
| 238 | return !MakeErrMsg(ErrMsg, "posix_spawn failed", Err); |
Mikhail Glushenkov | 0e9d9b5 | 2010-10-28 19:32:53 +0000 | [diff] [blame] | 239 | |
Tareq A. Siraj | d88b983 | 2013-10-01 14:28:18 +0000 | [diff] [blame] | 240 | PI.Pid = PID; |
| 241 | |
Chris Lattner | a50738a | 2010-04-18 04:14:37 +0000 | [diff] [blame] | 242 | return true; |
| 243 | } |
| 244 | #endif |
Mikhail Glushenkov | 0e9d9b5 | 2010-10-28 19:32:53 +0000 | [diff] [blame] | 245 | |
Reid Spencer | 76b83a1 | 2004-08-29 19:20:41 +0000 | [diff] [blame] | 246 | // Create a child process. |
Reid Spencer | 6cb551b | 2004-12-19 18:00:44 +0000 | [diff] [blame] | 247 | int child = fork(); |
| 248 | switch (child) { |
Chris Lattner | 0ab5e2c | 2011-04-15 05:18:47 +0000 | [diff] [blame] | 249 | // An error occurred: Return to the caller. |
Reid Spencer | 76b83a1 | 2004-08-29 19:20:41 +0000 | [diff] [blame] | 250 | case -1: |
Reid Spencer | 42bcf6e | 2006-08-21 06:02:44 +0000 | [diff] [blame] | 251 | MakeErrMsg(ErrMsg, "Couldn't fork"); |
Mikhail Glushenkov | 36cb832 | 2009-07-18 21:43:12 +0000 | [diff] [blame] | 252 | return false; |
Reid Spencer | 76b83a1 | 2004-08-29 19:20:41 +0000 | [diff] [blame] | 253 | |
| 254 | // Child process: Execute the program. |
Reid Spencer | cdefe0a | 2004-12-14 04:18:51 +0000 | [diff] [blame] | 255 | case 0: { |
Reid Spencer | 6cb551b | 2004-12-19 18:00:44 +0000 | [diff] [blame] | 256 | // Redirect file descriptors... |
| 257 | if (redirects) { |
Matthijs Kooijman | 1cc695e | 2008-06-12 12:53:35 +0000 | [diff] [blame] | 258 | // Redirect stdin |
Mikhail Glushenkov | 36cb832 | 2009-07-18 21:43:12 +0000 | [diff] [blame] | 259 | if (RedirectIO(redirects[0], 0, ErrMsg)) { return false; } |
Matthijs Kooijman | 1cc695e | 2008-06-12 12:53:35 +0000 | [diff] [blame] | 260 | // Redirect stdout |
Mikhail Glushenkov | 36cb832 | 2009-07-18 21:43:12 +0000 | [diff] [blame] | 261 | if (RedirectIO(redirects[1], 1, ErrMsg)) { return false; } |
Mikhail Glushenkov | 28309ac | 2009-07-17 20:38:17 +0000 | [diff] [blame] | 262 | if (redirects[1] && redirects[2] && |
Matthijs Kooijman | 1cc695e | 2008-06-12 12:53:35 +0000 | [diff] [blame] | 263 | *(redirects[1]) == *(redirects[2])) { |
| 264 | // If stdout and stderr should go to the same place, redirect stderr |
| 265 | // to the FD already open for stdout. |
| 266 | if (-1 == dup2(1,2)) { |
| 267 | MakeErrMsg(ErrMsg, "Can't redirect stderr to stdout"); |
Mikhail Glushenkov | 36cb832 | 2009-07-18 21:43:12 +0000 | [diff] [blame] | 268 | return false; |
Matthijs Kooijman | 1cc695e | 2008-06-12 12:53:35 +0000 | [diff] [blame] | 269 | } |
| 270 | } else { |
| 271 | // Just redirect stderr |
Mikhail Glushenkov | 36cb832 | 2009-07-18 21:43:12 +0000 | [diff] [blame] | 272 | if (RedirectIO(redirects[2], 2, ErrMsg)) { return false; } |
Reid Spencer | 6cb551b | 2004-12-19 18:00:44 +0000 | [diff] [blame] | 273 | } |
| 274 | } |
| 275 | |
Anton Korobeynikov | d01defe | 2007-02-16 19:11:07 +0000 | [diff] [blame] | 276 | // Set memory limits |
| 277 | if (memoryLimit!=0) { |
| 278 | SetMemoryLimits(memoryLimit); |
| 279 | } |
Mikhail Glushenkov | 28309ac | 2009-07-17 20:38:17 +0000 | [diff] [blame] | 280 | |
Reid Spencer | 6cb551b | 2004-12-19 18:00:44 +0000 | [diff] [blame] | 281 | // Execute! |
Rafael Espindola | b0a5c96 | 2013-06-14 19:38:45 +0000 | [diff] [blame] | 282 | std::string PathStr = Program; |
Craig Topper | e73658d | 2014-04-28 04:05:08 +0000 | [diff] [blame] | 283 | if (envp != nullptr) |
Rafael Espindola | b0a5c96 | 2013-06-14 19:38:45 +0000 | [diff] [blame] | 284 | execve(PathStr.c_str(), |
Dan Gohman | f656397 | 2010-04-19 15:55:10 +0000 | [diff] [blame] | 285 | const_cast<char **>(args), |
| 286 | const_cast<char **>(envp)); |
Evan Cheng | 09cf829 | 2006-06-09 20:43:11 +0000 | [diff] [blame] | 287 | else |
Rafael Espindola | b0a5c96 | 2013-06-14 19:38:45 +0000 | [diff] [blame] | 288 | execv(PathStr.c_str(), |
Dan Gohman | f656397 | 2010-04-19 15:55:10 +0000 | [diff] [blame] | 289 | const_cast<char **>(args)); |
Dan Gohman | 23a419f | 2009-08-05 00:09:12 +0000 | [diff] [blame] | 290 | // If the execve() failed, we should exit. Follow Unix protocol and |
| 291 | // return 127 if the executable was not found, and 126 otherwise. |
| 292 | // Use _exit rather than exit so that atexit functions and static |
| 293 | // object destructors cloned from the parent process aren't |
| 294 | // redundantly run, and so that any data buffered in stdio buffers |
| 295 | // cloned from the parent aren't redundantly written out. |
| 296 | _exit(errno == ENOENT ? 127 : 126); |
Reid Spencer | cdefe0a | 2004-12-14 04:18:51 +0000 | [diff] [blame] | 297 | } |
Reid Spencer | 76b83a1 | 2004-08-29 19:20:41 +0000 | [diff] [blame] | 298 | |
| 299 | // Parent process: Break out of the switch to do our processing. |
| 300 | default: |
| 301 | break; |
| 302 | } |
| 303 | |
Tareq A. Siraj | d88b983 | 2013-10-01 14:28:18 +0000 | [diff] [blame] | 304 | PI.Pid = child; |
Mikhail Glushenkov | 36cb832 | 2009-07-18 21:43:12 +0000 | [diff] [blame] | 305 | |
| 306 | return true; |
| 307 | } |
| 308 | |
Tareq A. Siraj | d88b983 | 2013-10-01 14:28:18 +0000 | [diff] [blame] | 309 | namespace llvm { |
| 310 | |
| 311 | ProcessInfo sys::Wait(const ProcessInfo &PI, unsigned SecondsToWait, |
| 312 | bool WaitUntilTerminates, std::string *ErrMsg) { |
Mikhail Glushenkov | 36cb832 | 2009-07-18 21:43:12 +0000 | [diff] [blame] | 313 | #ifdef HAVE_SYS_WAIT_H |
Reid Spencer | 6cb551b | 2004-12-19 18:00:44 +0000 | [diff] [blame] | 314 | struct sigaction Act, Old; |
Tareq A. Siraj | d88b983 | 2013-10-01 14:28:18 +0000 | [diff] [blame] | 315 | assert(PI.Pid && "invalid pid to wait on, process not started?"); |
Mikhail Glushenkov | 36cb832 | 2009-07-18 21:43:12 +0000 | [diff] [blame] | 316 | |
Tareq A. Siraj | d88b983 | 2013-10-01 14:28:18 +0000 | [diff] [blame] | 317 | int WaitPidOptions = 0; |
| 318 | pid_t ChildPid = PI.Pid; |
| 319 | if (WaitUntilTerminates) { |
| 320 | SecondsToWait = 0; |
Tareq A. Siraj | d88b983 | 2013-10-01 14:28:18 +0000 | [diff] [blame] | 321 | } else if (SecondsToWait) { |
| 322 | // Install a timeout handler. The handler itself does nothing, but the |
| 323 | // simple fact of having a handler at all causes the wait below to return |
| 324 | // with EINTR, unlike if we used SIG_IGN. |
Duncan Sands | 7a68cd0 | 2010-07-14 14:32:33 +0000 | [diff] [blame] | 325 | memset(&Act, 0, sizeof(Act)); |
Duncan Sands | fec62f0 | 2009-11-08 20:55:48 +0000 | [diff] [blame] | 326 | Act.sa_handler = TimeOutHandler; |
Reid Spencer | 6cb551b | 2004-12-19 18:00:44 +0000 | [diff] [blame] | 327 | sigemptyset(&Act.sa_mask); |
Reid Spencer | 6cb551b | 2004-12-19 18:00:44 +0000 | [diff] [blame] | 328 | sigaction(SIGALRM, &Act, &Old); |
Tareq A. Siraj | d88b983 | 2013-10-01 14:28:18 +0000 | [diff] [blame] | 329 | alarm(SecondsToWait); |
| 330 | } else if (SecondsToWait == 0) |
| 331 | WaitPidOptions = WNOHANG; |
Reid Spencer | 6cb551b | 2004-12-19 18:00:44 +0000 | [diff] [blame] | 332 | |
Reid Spencer | 76b83a1 | 2004-08-29 19:20:41 +0000 | [diff] [blame] | 333 | // Parent process: Wait for the child process to terminate. |
| 334 | int status; |
Tareq A. Siraj | d88b983 | 2013-10-01 14:28:18 +0000 | [diff] [blame] | 335 | ProcessInfo WaitResult; |
Julien Lerouge | a67d14f | 2014-06-27 18:02:54 +0000 | [diff] [blame] | 336 | |
| 337 | do { |
| 338 | WaitResult.Pid = waitpid(ChildPid, &status, WaitPidOptions); |
| 339 | } while (WaitUntilTerminates && WaitResult.Pid == -1 && errno == EINTR); |
| 340 | |
Tareq A. Siraj | d88b983 | 2013-10-01 14:28:18 +0000 | [diff] [blame] | 341 | if (WaitResult.Pid != PI.Pid) { |
| 342 | if (WaitResult.Pid == 0) { |
| 343 | // Non-blocking wait. |
| 344 | return WaitResult; |
| 345 | } else { |
| 346 | if (SecondsToWait && errno == EINTR) { |
| 347 | // Kill the child. |
| 348 | kill(PI.Pid, SIGKILL); |
Mikhail Glushenkov | 28309ac | 2009-07-17 20:38:17 +0000 | [diff] [blame] | 349 | |
Tareq A. Siraj | d88b983 | 2013-10-01 14:28:18 +0000 | [diff] [blame] | 350 | // Turn off the alarm and restore the signal handler |
| 351 | alarm(0); |
Craig Topper | e73658d | 2014-04-28 04:05:08 +0000 | [diff] [blame] | 352 | sigaction(SIGALRM, &Old, nullptr); |
Reid Spencer | 6cb551b | 2004-12-19 18:00:44 +0000 | [diff] [blame] | 353 | |
Tareq A. Siraj | d88b983 | 2013-10-01 14:28:18 +0000 | [diff] [blame] | 354 | // Wait for child to die |
| 355 | if (wait(&status) != ChildPid) |
| 356 | MakeErrMsg(ErrMsg, "Child timed out but wouldn't die"); |
| 357 | else |
| 358 | MakeErrMsg(ErrMsg, "Child timed out", 0); |
Devang Patel | 3d4f1b3 | 2008-02-04 20:57:54 +0000 | [diff] [blame] | 359 | |
Tareq A. Siraj | d88b983 | 2013-10-01 14:28:18 +0000 | [diff] [blame] | 360 | WaitResult.ReturnCode = -2; // Timeout detected |
| 361 | return WaitResult; |
| 362 | } else if (errno != EINTR) { |
| 363 | MakeErrMsg(ErrMsg, "Error waiting for child process"); |
| 364 | WaitResult.ReturnCode = -1; |
| 365 | return WaitResult; |
| 366 | } |
Reid Spencer | 6cb551b | 2004-12-19 18:00:44 +0000 | [diff] [blame] | 367 | } |
Tareq A. Siraj | d88b983 | 2013-10-01 14:28:18 +0000 | [diff] [blame] | 368 | } |
Reid Spencer | 6cb551b | 2004-12-19 18:00:44 +0000 | [diff] [blame] | 369 | |
| 370 | // We exited normally without timeout, so turn off the timer. |
Tareq A. Siraj | d88b983 | 2013-10-01 14:28:18 +0000 | [diff] [blame] | 371 | if (SecondsToWait && !WaitUntilTerminates) { |
Reid Spencer | 6cb551b | 2004-12-19 18:00:44 +0000 | [diff] [blame] | 372 | alarm(0); |
Craig Topper | e73658d | 2014-04-28 04:05:08 +0000 | [diff] [blame] | 373 | sigaction(SIGALRM, &Old, nullptr); |
Reid Spencer | 6cb551b | 2004-12-19 18:00:44 +0000 | [diff] [blame] | 374 | } |
Reid Spencer | 76b83a1 | 2004-08-29 19:20:41 +0000 | [diff] [blame] | 375 | |
Dan Gohman | cae3c53 | 2010-10-29 16:39:01 +0000 | [diff] [blame] | 376 | // Return the proper exit status. Detect error conditions |
| 377 | // so we can return -1 for them and set ErrMsg informatively. |
Reid Spencer | ccd5b90 | 2005-12-22 20:00:16 +0000 | [diff] [blame] | 378 | int result = 0; |
Dan Gohman | cae3c53 | 2010-10-29 16:39:01 +0000 | [diff] [blame] | 379 | if (WIFEXITED(status)) { |
Reid Spencer | ccd5b90 | 2005-12-22 20:00:16 +0000 | [diff] [blame] | 380 | result = WEXITSTATUS(status); |
Tareq A. Siraj | d88b983 | 2013-10-01 14:28:18 +0000 | [diff] [blame] | 381 | WaitResult.ReturnCode = result; |
| 382 | |
Dan Gohman | cae3c53 | 2010-10-29 16:39:01 +0000 | [diff] [blame] | 383 | if (result == 127) { |
Dan Gohman | 5bc07d2 | 2010-10-29 17:20:42 +0000 | [diff] [blame] | 384 | if (ErrMsg) |
| 385 | *ErrMsg = llvm::sys::StrError(ENOENT); |
Tareq A. Siraj | d88b983 | 2013-10-01 14:28:18 +0000 | [diff] [blame] | 386 | WaitResult.ReturnCode = -1; |
| 387 | return WaitResult; |
Dan Gohman | cae3c53 | 2010-10-29 16:39:01 +0000 | [diff] [blame] | 388 | } |
| 389 | if (result == 126) { |
Dan Gohman | 5bc07d2 | 2010-10-29 17:20:42 +0000 | [diff] [blame] | 390 | if (ErrMsg) |
| 391 | *ErrMsg = "Program could not be executed"; |
Tareq A. Siraj | d88b983 | 2013-10-01 14:28:18 +0000 | [diff] [blame] | 392 | WaitResult.ReturnCode = -1; |
| 393 | return WaitResult; |
Dan Gohman | cae3c53 | 2010-10-29 16:39:01 +0000 | [diff] [blame] | 394 | } |
| 395 | } else if (WIFSIGNALED(status)) { |
Dan Gohman | 5bc07d2 | 2010-10-29 17:20:42 +0000 | [diff] [blame] | 396 | if (ErrMsg) { |
Andrew Trick | d5d0764 | 2011-05-21 00:56:46 +0000 | [diff] [blame] | 397 | *ErrMsg = strsignal(WTERMSIG(status)); |
Reid Spencer | ccd5b90 | 2005-12-22 20:00:16 +0000 | [diff] [blame] | 398 | #ifdef WCOREDUMP |
Dan Gohman | 5bc07d2 | 2010-10-29 17:20:42 +0000 | [diff] [blame] | 399 | if (WCOREDUMP(status)) |
| 400 | *ErrMsg += " (core dumped)"; |
Reid Spencer | 76b83a1 | 2004-08-29 19:20:41 +0000 | [diff] [blame] | 401 | #endif |
Dan Gohman | 5bc07d2 | 2010-10-29 17:20:42 +0000 | [diff] [blame] | 402 | } |
Andrew Trick | d5d0764 | 2011-05-21 00:56:46 +0000 | [diff] [blame] | 403 | // Return a special value to indicate that the process received an unhandled |
| 404 | // signal during execution as opposed to failing to execute. |
Tareq A. Siraj | d88b983 | 2013-10-01 14:28:18 +0000 | [diff] [blame] | 405 | WaitResult.ReturnCode = -2; |
Dan Gohman | cae3c53 | 2010-10-29 16:39:01 +0000 | [diff] [blame] | 406 | } |
Reid Spencer | ccd5b90 | 2005-12-22 20:00:16 +0000 | [diff] [blame] | 407 | #else |
Dan Gohman | 5bc07d2 | 2010-10-29 17:20:42 +0000 | [diff] [blame] | 408 | if (ErrMsg) |
| 409 | *ErrMsg = "Program::Wait is not implemented on this platform yet!"; |
Mark Seaborn | 8d5b0e2 | 2014-01-27 22:53:07 +0000 | [diff] [blame] | 410 | ProcessInfo WaitResult; |
Tareq A. Siraj | d88b983 | 2013-10-01 14:28:18 +0000 | [diff] [blame] | 411 | WaitResult.ReturnCode = -2; |
Reid Spencer | ccd5b90 | 2005-12-22 20:00:16 +0000 | [diff] [blame] | 412 | #endif |
Tareq A. Siraj | d88b983 | 2013-10-01 14:28:18 +0000 | [diff] [blame] | 413 | return WaitResult; |
Reid Spencer | 76b83a1 | 2004-08-29 19:20:41 +0000 | [diff] [blame] | 414 | } |
| 415 | |
Rafael Espindola | db4ed0b | 2014-06-13 02:24:39 +0000 | [diff] [blame] | 416 | std::error_code sys::ChangeStdinToBinary(){ |
Reid Spencer | ab97f22 | 2006-06-07 23:18:34 +0000 | [diff] [blame] | 417 | // Do nothing, as Unix doesn't differentiate between text and binary. |
Rafael Espindola | db4ed0b | 2014-06-13 02:24:39 +0000 | [diff] [blame] | 418 | return std::error_code(); |
Reid Spencer | ab97f22 | 2006-06-07 23:18:34 +0000 | [diff] [blame] | 419 | } |
| 420 | |
Rafael Espindola | db4ed0b | 2014-06-13 02:24:39 +0000 | [diff] [blame] | 421 | std::error_code sys::ChangeStdoutToBinary(){ |
Reid Spencer | ab97f22 | 2006-06-07 23:18:34 +0000 | [diff] [blame] | 422 | // Do nothing, as Unix doesn't differentiate between text and binary. |
Rafael Espindola | db4ed0b | 2014-06-13 02:24:39 +0000 | [diff] [blame] | 423 | return std::error_code(); |
Reid Spencer | ab97f22 | 2006-06-07 23:18:34 +0000 | [diff] [blame] | 424 | } |
| 425 | |
Rafael Espindola | 9c35966 | 2014-09-03 20:02:00 +0000 | [diff] [blame] | 426 | std::error_code |
| 427 | llvm::sys::writeFileWithEncoding(StringRef FileName, StringRef Contents, |
| 428 | WindowsEncodingMethod Encoding /*unused*/) { |
| 429 | std::error_code EC; |
| 430 | llvm::raw_fd_ostream OS(FileName, EC, llvm::sys::fs::OpenFlags::F_Text); |
| 431 | |
| 432 | if (EC) |
| 433 | return EC; |
| 434 | |
| 435 | OS << Contents; |
| 436 | |
| 437 | if (OS.has_error()) |
| 438 | return std::make_error_code(std::errc::io_error); |
| 439 | |
| 440 | return EC; |
| 441 | } |
| 442 | |
Rafael Espindola | cd848c0 | 2013-04-11 14:06:34 +0000 | [diff] [blame] | 443 | bool llvm::sys::argumentsFitWithinSystemLimits(ArrayRef<const char*> Args) { |
| 444 | static long ArgMax = sysconf(_SC_ARG_MAX); |
| 445 | |
| 446 | // System says no practical limit. |
| 447 | if (ArgMax == -1) |
| 448 | return true; |
| 449 | |
| 450 | // Conservatively account for space required by environment variables. |
Rafael Espindola | 42036ae | 2014-08-25 22:53:21 +0000 | [diff] [blame] | 451 | long HalfArgMax = ArgMax / 2; |
Rafael Espindola | cd848c0 | 2013-04-11 14:06:34 +0000 | [diff] [blame] | 452 | |
| 453 | size_t ArgLength = 0; |
| 454 | for (ArrayRef<const char*>::iterator I = Args.begin(), E = Args.end(); |
| 455 | I != E; ++I) { |
| 456 | ArgLength += strlen(*I) + 1; |
Rafael Espindola | 42036ae | 2014-08-25 22:53:21 +0000 | [diff] [blame] | 457 | if (ArgLength > size_t(HalfArgMax)) { |
Rafael Espindola | cd848c0 | 2013-04-11 14:06:34 +0000 | [diff] [blame] | 458 | return false; |
| 459 | } |
| 460 | } |
| 461 | return true; |
| 462 | } |
Reid Spencer | 76b83a1 | 2004-08-29 19:20:41 +0000 | [diff] [blame] | 463 | } |