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