blob: 0f45df1a0da0e20fd9bd12dfbeed03c0a979f070 [file] [log] [blame]
Charles Davis54c9eb62010-11-29 19:44:50 +00001//===- llvm/Support/Unix/Program.cpp -----------------------------*- C++ -*-===//
Mikhail Glushenkov28309ac2009-07-17 20:38:17 +00002//
Reid Spencer76b83a12004-08-29 19:20:41 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattnerf3ebc3f2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Mikhail Glushenkov28309ac2009-07-17 20:38:17 +00007//
Reid Spencer76b83a12004-08-29 19:20:41 +00008//===----------------------------------------------------------------------===//
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. Spencer65ffd922014-11-04 01:29:29 +000020#include "llvm/ADT/StringExtras.h"
Evgeniy Stepanov1f5a7142013-02-04 07:03:24 +000021#include "llvm/Support/Compiler.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000022#include "llvm/Support/FileSystem.h"
Rafael Espindola9c359662014-09-03 20:02:00 +000023#include "llvm/Support/raw_ostream.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000024#include <llvm/Config/config.h>
Reid Spencerd554bbc2004-12-27 06:16:52 +000025#if HAVE_SYS_STAT_H
26#include <sys/stat.h>
27#endif
Anton Korobeynikovd01defe2007-02-16 19:11:07 +000028#if HAVE_SYS_RESOURCE_H
29#include <sys/resource.h>
30#endif
Reid Spencerd554bbc2004-12-27 06:16:52 +000031#if HAVE_SIGNAL_H
32#include <signal.h>
33#endif
34#if HAVE_FCNTL_H
35#include <fcntl.h>
36#endif
Rafael Espindolacd848c02013-04-11 14:06:34 +000037#if HAVE_UNISTD_H
38#include <unistd.h>
39#endif
Chris Lattnera50738a2010-04-18 04:14:37 +000040#ifdef HAVE_POSIX_SPAWN
Rafael Espindola9ab9fe92013-10-08 16:12:58 +000041#ifdef __sun__
42#define _RESTRICT_KYWD
43#endif
Chris Lattnera50738a2010-04-18 04:14:37 +000044#include <spawn.h>
Nick Lewycky554a3982010-04-18 07:07:48 +000045#if !defined(__APPLE__)
46 extern char **environ;
Benjamin Kramer1360e9e2010-04-18 09:16:04 +000047#else
48#include <crt_externs.h> // _NSGetEnviron
Nick Lewycky554a3982010-04-18 07:07:48 +000049#endif
Chris Lattnera50738a2010-04-18 04:14:37 +000050#endif
Reid Spencer76b83a12004-08-29 19:20:41 +000051
Reid Spencer76b83a12004-08-29 19:20:41 +000052namespace llvm {
Rafael Espindola3acea392014-06-12 21:46:39 +000053
Reid Spencer76b83a12004-08-29 19:20:41 +000054using namespace sys;
55
Tareq A. Sirajd88b9832013-10-01 14:28:18 +000056ProcessInfo::ProcessInfo() : Pid(0), ReturnCode(0) {}
57
Michael J. Spencer65ffd922014-11-04 01:29:29 +000058ErrorOr<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 Carruthec8406d2014-12-02 00:52:01 +000066 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. Spencer65ffd922014-11-04 01:29:29 +000072
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 Espindolab0a5c962013-06-14 19:38:45 +000086static bool RedirectIO(const StringRef *Path, int FD, std::string* ErrMsg) {
Craig Toppere73658d2014-04-28 04:05:08 +000087 if (!Path) // Noop
Matthijs Kooijman616e4842008-06-12 10:47:18 +000088 return false;
Rafael Espindolab0a5c962013-06-14 19:38:45 +000089 std::string File;
90 if (Path->empty())
Matthijs Kooijman616e4842008-06-12 10:47:18 +000091 // Redirect empty paths to /dev/null
92 File = "/dev/null";
93 else
Rafael Espindolab0a5c962013-06-14 19:38:45 +000094 File = *Path;
Reid Spencer6cb551b2004-12-19 18:00:44 +000095
96 // Open the file
Rafael Espindolab0a5c962013-06-14 19:38:45 +000097 int InFD = open(File.c_str(), FD == 0 ? O_RDONLY : O_WRONLY|O_CREAT, 0666);
Reid Spencer6cb551b2004-12-19 18:00:44 +000098 if (InFD == -1) {
Rafael Espindolab0a5c962013-06-14 19:38:45 +000099 MakeErrMsg(ErrMsg, "Cannot open file '" + File + "' for "
Daniel Dunbar3222b9b2009-04-20 20:50:13 +0000100 + (FD == 0 ? "input" : "output"));
Reid Spencer42bcf6e2006-08-21 06:02:44 +0000101 return true;
Reid Spencer6cb551b2004-12-19 18:00:44 +0000102 }
103
Reid Spencer42bcf6e2006-08-21 06:02:44 +0000104 // Install it as the requested FD
Chris Lattner65a437f2010-03-14 23:16:45 +0000105 if (dup2(InFD, FD) == -1) {
Reid Spencer42bcf6e2006-08-21 06:02:44 +0000106 MakeErrMsg(ErrMsg, "Cannot dup2");
Chris Lattner65a437f2010-03-14 23:16:45 +0000107 close(InFD);
Reid Spencer42bcf6e2006-08-21 06:02:44 +0000108 return true;
109 }
Reid Spencer6cb551b2004-12-19 18:00:44 +0000110 close(InFD); // Close the original FD
Reid Spencer42bcf6e2006-08-21 06:02:44 +0000111 return false;
Reid Spencer6cb551b2004-12-19 18:00:44 +0000112}
113
Chris Lattnera50738a2010-04-18 04:14:37 +0000114#ifdef HAVE_POSIX_SPAWN
Rafael Espindola70d98f42013-07-26 16:21:31 +0000115static bool RedirectIO_PS(const std::string *Path, int FD, std::string *ErrMsg,
Benjamin Kramerdce83c52011-03-20 15:52:24 +0000116 posix_spawn_file_actions_t *FileActions) {
Craig Toppere73658d2014-04-28 04:05:08 +0000117 if (!Path) // Noop
Chris Lattnera50738a2010-04-18 04:14:37 +0000118 return false;
Rafael Espindola70d98f42013-07-26 16:21:31 +0000119 const char *File;
Rafael Espindolab0a5c962013-06-14 19:38:45 +0000120 if (Path->empty())
Chris Lattnera50738a2010-04-18 04:14:37 +0000121 // Redirect empty paths to /dev/null
122 File = "/dev/null";
123 else
Rafael Espindola70d98f42013-07-26 16:21:31 +0000124 File = Path->c_str();
Benjamin Kramer5b4be742010-04-18 09:19:41 +0000125
Rafael Espindolab0a5c962013-06-14 19:38:45 +0000126 if (int Err = posix_spawn_file_actions_addopen(
Rafael Espindola70d98f42013-07-26 16:21:31 +0000127 FileActions, FD, File,
Rafael Espindolab0a5c962013-06-14 19:38:45 +0000128 FD == 0 ? O_RDONLY : O_WRONLY | O_CREAT, 0666))
Chris Lattnera50738a2010-04-18 04:14:37 +0000129 return MakeErrMsg(ErrMsg, "Cannot dup2", Err);
130 return false;
131}
132#endif
133
Duncan Sandsfec62f02009-11-08 20:55:48 +0000134static void TimeOutHandler(int Sig) {
135}
136
Anton Korobeynikovd01defe2007-02-16 19:11:07 +0000137static void SetMemoryLimits (unsigned size)
138{
Chris Lattner62f50da2010-02-12 00:37:46 +0000139#if HAVE_SYS_RESOURCE_H && HAVE_GETRLIMIT && HAVE_SETRLIMIT
Anton Korobeynikovd01defe2007-02-16 19:11:07 +0000140 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 Greif0e219802007-07-06 10:31:27 +0000147#ifdef RLIMIT_RSS
Anton Korobeynikovd01defe2007-02-16 19:11:07 +0000148 // Resident set size.
149 getrlimit (RLIMIT_RSS, &r);
150 r.rlim_cur = limit;
151 setrlimit (RLIMIT_RSS, &r);
Reid Spencere2e9b372007-04-23 07:22:51 +0000152#endif
Devang Pateld609374a2007-06-04 15:28:57 +0000153#ifdef RLIMIT_AS // e.g. NetBSD doesn't have it.
Evgeniy Stepanov1f5a7142013-02-04 07:03:24 +0000154 // 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 Korobeynikovd01defe2007-02-16 19:11:07 +0000157 // Virtual memory.
158 getrlimit (RLIMIT_AS, &r);
159 r.rlim_cur = limit;
160 setrlimit (RLIMIT_AS, &r);
161#endif
Devang Pateld609374a2007-06-04 15:28:57 +0000162#endif
Evgeniy Stepanov1f5a7142013-02-04 07:03:24 +0000163#endif
Anton Korobeynikovd01defe2007-02-16 19:11:07 +0000164}
165
Rafael Espindolacb2eca02013-06-12 20:58:35 +0000166}
167
Tareq A. Sirajd88b9832013-10-01 14:28:18 +0000168static bool Execute(ProcessInfo &PI, StringRef Program, const char **args,
Rafael Espindolab0a5c962013-06-14 19:38:45 +0000169 const char **envp, const StringRef **redirects,
Rafael Espindolacb2eca02013-06-12 20:58:35 +0000170 unsigned memoryLimit, std::string *ErrMsg) {
Tareq A. Sirajd88b9832013-10-01 14:28:18 +0000171 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 Lattnera50738a2010-04-18 04:14:37 +0000178 // 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 Kramerdce83c52011-03-20 15:52:24 +0000182 posix_spawn_file_actions_t FileActionsStore;
Craig Toppere73658d2014-04-28 04:05:08 +0000183 posix_spawn_file_actions_t *FileActions = nullptr;
Chris Lattnera50738a2010-04-18 04:14:37 +0000184
Rafael Espindola70d98f42013-07-26 16:21:31 +0000185 // If we call posix_spawn_file_actions_addopen we have to make sure the
Rafael Espindola1cfc5dd2013-07-26 20:44:45 +0000186 // c strings we pass to it stay alive until the call to posix_spawn,
Rafael Espindola70d98f42013-07-26 16:21:31 +0000187 // so we copy any StringRefs into this variable.
188 std::string RedirectsStorage[3];
189
Chris Lattnera50738a2010-04-18 04:14:37 +0000190 if (redirects) {
Craig Toppere73658d2014-04-28 04:05:08 +0000191 std::string *RedirectsStr[3] = {nullptr, nullptr, nullptr};
Rafael Espindola70d98f42013-07-26 16:21:31 +0000192 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 Kramerdce83c52011-03-20 15:52:24 +0000199 FileActions = &FileActionsStore;
200 posix_spawn_file_actions_init(FileActions);
201
Chris Lattnera50738a2010-04-18 04:14:37 +0000202 // Redirect stdin/stdout.
Rafael Espindola70d98f42013-07-26 16:21:31 +0000203 if (RedirectIO_PS(RedirectsStr[0], 0, ErrMsg, FileActions) ||
204 RedirectIO_PS(RedirectsStr[1], 1, ErrMsg, FileActions))
Chris Lattnera50738a2010-04-18 04:14:37 +0000205 return false;
Craig Toppere73658d2014-04-28 04:05:08 +0000206 if (redirects[1] == nullptr || redirects[2] == nullptr ||
Chris Lattnera50738a2010-04-18 04:14:37 +0000207 *redirects[1] != *redirects[2]) {
208 // Just redirect stderr
Rafael Espindola70d98f42013-07-26 16:21:31 +0000209 if (RedirectIO_PS(RedirectsStr[2], 2, ErrMsg, FileActions))
210 return false;
Mikhail Glushenkov0e9d9b52010-10-28 19:32:53 +0000211 } else {
Chris Lattnera50738a2010-04-18 04:14:37 +0000212 // If stdout and stderr should go to the same place, redirect stderr
213 // to the FD already open for stdout.
Benjamin Kramerdce83c52011-03-20 15:52:24 +0000214 if (int Err = posix_spawn_file_actions_adddup2(FileActions, 1, 2))
Chris Lattnera50738a2010-04-18 04:14:37 +0000215 return !MakeErrMsg(ErrMsg, "Can't redirect stderr to stdout", Err);
216 }
217 }
218
Benjamin Kramer1360e9e2010-04-18 09:16:04 +0000219 if (!envp)
Nick Lewycky554a3982010-04-18 07:07:48 +0000220#if !defined(__APPLE__)
Dan Gohmanf6563972010-04-19 15:55:10 +0000221 envp = const_cast<const char **>(environ);
Benjamin Kramer1360e9e2010-04-18 09:16:04 +0000222#else
Dan Gohmanf6563972010-04-19 15:55:10 +0000223 // environ is missing in dylibs.
224 envp = const_cast<const char **>(*_NSGetEnviron());
Nick Lewycky554a3982010-04-18 07:07:48 +0000225#endif
226
Daniel Dunbar31158b52010-09-30 23:56:49 +0000227 // Explicitly initialized to prevent what appears to be a valgrind false
228 // positive.
229 pid_t PID = 0;
Craig Toppere73658d2014-04-28 04:05:08 +0000230 int Err = posix_spawn(&PID, Program.str().c_str(), FileActions,
231 /*attrp*/nullptr, const_cast<char **>(args),
232 const_cast<char **>(envp));
Mikhail Glushenkov0e9d9b52010-10-28 19:32:53 +0000233
Benjamin Kramerdce83c52011-03-20 15:52:24 +0000234 if (FileActions)
235 posix_spawn_file_actions_destroy(FileActions);
Chris Lattnera50738a2010-04-18 04:14:37 +0000236
237 if (Err)
238 return !MakeErrMsg(ErrMsg, "posix_spawn failed", Err);
Mikhail Glushenkov0e9d9b52010-10-28 19:32:53 +0000239
Tareq A. Sirajd88b9832013-10-01 14:28:18 +0000240 PI.Pid = PID;
241
Chris Lattnera50738a2010-04-18 04:14:37 +0000242 return true;
243 }
244#endif
Mikhail Glushenkov0e9d9b52010-10-28 19:32:53 +0000245
Reid Spencer76b83a12004-08-29 19:20:41 +0000246 // Create a child process.
Reid Spencer6cb551b2004-12-19 18:00:44 +0000247 int child = fork();
248 switch (child) {
Chris Lattner0ab5e2c2011-04-15 05:18:47 +0000249 // An error occurred: Return to the caller.
Reid Spencer76b83a12004-08-29 19:20:41 +0000250 case -1:
Reid Spencer42bcf6e2006-08-21 06:02:44 +0000251 MakeErrMsg(ErrMsg, "Couldn't fork");
Mikhail Glushenkov36cb8322009-07-18 21:43:12 +0000252 return false;
Reid Spencer76b83a12004-08-29 19:20:41 +0000253
254 // Child process: Execute the program.
Reid Spencercdefe0a2004-12-14 04:18:51 +0000255 case 0: {
Reid Spencer6cb551b2004-12-19 18:00:44 +0000256 // Redirect file descriptors...
257 if (redirects) {
Matthijs Kooijman1cc695e2008-06-12 12:53:35 +0000258 // Redirect stdin
Mikhail Glushenkov36cb8322009-07-18 21:43:12 +0000259 if (RedirectIO(redirects[0], 0, ErrMsg)) { return false; }
Matthijs Kooijman1cc695e2008-06-12 12:53:35 +0000260 // Redirect stdout
Mikhail Glushenkov36cb8322009-07-18 21:43:12 +0000261 if (RedirectIO(redirects[1], 1, ErrMsg)) { return false; }
Mikhail Glushenkov28309ac2009-07-17 20:38:17 +0000262 if (redirects[1] && redirects[2] &&
Matthijs Kooijman1cc695e2008-06-12 12:53:35 +0000263 *(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 Glushenkov36cb8322009-07-18 21:43:12 +0000268 return false;
Matthijs Kooijman1cc695e2008-06-12 12:53:35 +0000269 }
270 } else {
271 // Just redirect stderr
Mikhail Glushenkov36cb8322009-07-18 21:43:12 +0000272 if (RedirectIO(redirects[2], 2, ErrMsg)) { return false; }
Reid Spencer6cb551b2004-12-19 18:00:44 +0000273 }
274 }
275
Anton Korobeynikovd01defe2007-02-16 19:11:07 +0000276 // Set memory limits
277 if (memoryLimit!=0) {
278 SetMemoryLimits(memoryLimit);
279 }
Mikhail Glushenkov28309ac2009-07-17 20:38:17 +0000280
Reid Spencer6cb551b2004-12-19 18:00:44 +0000281 // Execute!
Rafael Espindolab0a5c962013-06-14 19:38:45 +0000282 std::string PathStr = Program;
Craig Toppere73658d2014-04-28 04:05:08 +0000283 if (envp != nullptr)
Rafael Espindolab0a5c962013-06-14 19:38:45 +0000284 execve(PathStr.c_str(),
Dan Gohmanf6563972010-04-19 15:55:10 +0000285 const_cast<char **>(args),
286 const_cast<char **>(envp));
Evan Cheng09cf8292006-06-09 20:43:11 +0000287 else
Rafael Espindolab0a5c962013-06-14 19:38:45 +0000288 execv(PathStr.c_str(),
Dan Gohmanf6563972010-04-19 15:55:10 +0000289 const_cast<char **>(args));
Dan Gohman23a419f2009-08-05 00:09:12 +0000290 // 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 Spencercdefe0a2004-12-14 04:18:51 +0000297 }
Reid Spencer76b83a12004-08-29 19:20:41 +0000298
299 // Parent process: Break out of the switch to do our processing.
300 default:
301 break;
302 }
303
Tareq A. Sirajd88b9832013-10-01 14:28:18 +0000304 PI.Pid = child;
Mikhail Glushenkov36cb8322009-07-18 21:43:12 +0000305
306 return true;
307}
308
Tareq A. Sirajd88b9832013-10-01 14:28:18 +0000309namespace llvm {
310
311ProcessInfo sys::Wait(const ProcessInfo &PI, unsigned SecondsToWait,
312 bool WaitUntilTerminates, std::string *ErrMsg) {
Mikhail Glushenkov36cb8322009-07-18 21:43:12 +0000313#ifdef HAVE_SYS_WAIT_H
Reid Spencer6cb551b2004-12-19 18:00:44 +0000314 struct sigaction Act, Old;
Tareq A. Sirajd88b9832013-10-01 14:28:18 +0000315 assert(PI.Pid && "invalid pid to wait on, process not started?");
Mikhail Glushenkov36cb8322009-07-18 21:43:12 +0000316
Tareq A. Sirajd88b9832013-10-01 14:28:18 +0000317 int WaitPidOptions = 0;
318 pid_t ChildPid = PI.Pid;
319 if (WaitUntilTerminates) {
320 SecondsToWait = 0;
Tareq A. Sirajd88b9832013-10-01 14:28:18 +0000321 } 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 Sands7a68cd02010-07-14 14:32:33 +0000325 memset(&Act, 0, sizeof(Act));
Duncan Sandsfec62f02009-11-08 20:55:48 +0000326 Act.sa_handler = TimeOutHandler;
Reid Spencer6cb551b2004-12-19 18:00:44 +0000327 sigemptyset(&Act.sa_mask);
Reid Spencer6cb551b2004-12-19 18:00:44 +0000328 sigaction(SIGALRM, &Act, &Old);
Tareq A. Sirajd88b9832013-10-01 14:28:18 +0000329 alarm(SecondsToWait);
330 } else if (SecondsToWait == 0)
331 WaitPidOptions = WNOHANG;
Reid Spencer6cb551b2004-12-19 18:00:44 +0000332
Reid Spencer76b83a12004-08-29 19:20:41 +0000333 // Parent process: Wait for the child process to terminate.
334 int status;
Tareq A. Sirajd88b9832013-10-01 14:28:18 +0000335 ProcessInfo WaitResult;
Julien Lerougea67d14f2014-06-27 18:02:54 +0000336
337 do {
338 WaitResult.Pid = waitpid(ChildPid, &status, WaitPidOptions);
339 } while (WaitUntilTerminates && WaitResult.Pid == -1 && errno == EINTR);
340
Tareq A. Sirajd88b9832013-10-01 14:28:18 +0000341 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 Glushenkov28309ac2009-07-17 20:38:17 +0000349
Tareq A. Sirajd88b9832013-10-01 14:28:18 +0000350 // Turn off the alarm and restore the signal handler
351 alarm(0);
Craig Toppere73658d2014-04-28 04:05:08 +0000352 sigaction(SIGALRM, &Old, nullptr);
Reid Spencer6cb551b2004-12-19 18:00:44 +0000353
Tareq A. Sirajd88b9832013-10-01 14:28:18 +0000354 // 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 Patel3d4f1b32008-02-04 20:57:54 +0000359
Tareq A. Sirajd88b9832013-10-01 14:28:18 +0000360 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 Spencer6cb551b2004-12-19 18:00:44 +0000367 }
Tareq A. Sirajd88b9832013-10-01 14:28:18 +0000368 }
Reid Spencer6cb551b2004-12-19 18:00:44 +0000369
370 // We exited normally without timeout, so turn off the timer.
Tareq A. Sirajd88b9832013-10-01 14:28:18 +0000371 if (SecondsToWait && !WaitUntilTerminates) {
Reid Spencer6cb551b2004-12-19 18:00:44 +0000372 alarm(0);
Craig Toppere73658d2014-04-28 04:05:08 +0000373 sigaction(SIGALRM, &Old, nullptr);
Reid Spencer6cb551b2004-12-19 18:00:44 +0000374 }
Reid Spencer76b83a12004-08-29 19:20:41 +0000375
Dan Gohmancae3c532010-10-29 16:39:01 +0000376 // Return the proper exit status. Detect error conditions
377 // so we can return -1 for them and set ErrMsg informatively.
Reid Spencerccd5b902005-12-22 20:00:16 +0000378 int result = 0;
Dan Gohmancae3c532010-10-29 16:39:01 +0000379 if (WIFEXITED(status)) {
Reid Spencerccd5b902005-12-22 20:00:16 +0000380 result = WEXITSTATUS(status);
Tareq A. Sirajd88b9832013-10-01 14:28:18 +0000381 WaitResult.ReturnCode = result;
382
Dan Gohmancae3c532010-10-29 16:39:01 +0000383 if (result == 127) {
Dan Gohman5bc07d22010-10-29 17:20:42 +0000384 if (ErrMsg)
385 *ErrMsg = llvm::sys::StrError(ENOENT);
Tareq A. Sirajd88b9832013-10-01 14:28:18 +0000386 WaitResult.ReturnCode = -1;
387 return WaitResult;
Dan Gohmancae3c532010-10-29 16:39:01 +0000388 }
389 if (result == 126) {
Dan Gohman5bc07d22010-10-29 17:20:42 +0000390 if (ErrMsg)
391 *ErrMsg = "Program could not be executed";
Tareq A. Sirajd88b9832013-10-01 14:28:18 +0000392 WaitResult.ReturnCode = -1;
393 return WaitResult;
Dan Gohmancae3c532010-10-29 16:39:01 +0000394 }
395 } else if (WIFSIGNALED(status)) {
Dan Gohman5bc07d22010-10-29 17:20:42 +0000396 if (ErrMsg) {
Andrew Trickd5d07642011-05-21 00:56:46 +0000397 *ErrMsg = strsignal(WTERMSIG(status));
Reid Spencerccd5b902005-12-22 20:00:16 +0000398#ifdef WCOREDUMP
Dan Gohman5bc07d22010-10-29 17:20:42 +0000399 if (WCOREDUMP(status))
400 *ErrMsg += " (core dumped)";
Reid Spencer76b83a12004-08-29 19:20:41 +0000401#endif
Dan Gohman5bc07d22010-10-29 17:20:42 +0000402 }
Andrew Trickd5d07642011-05-21 00:56:46 +0000403 // Return a special value to indicate that the process received an unhandled
404 // signal during execution as opposed to failing to execute.
Tareq A. Sirajd88b9832013-10-01 14:28:18 +0000405 WaitResult.ReturnCode = -2;
Dan Gohmancae3c532010-10-29 16:39:01 +0000406 }
Reid Spencerccd5b902005-12-22 20:00:16 +0000407#else
Dan Gohman5bc07d22010-10-29 17:20:42 +0000408 if (ErrMsg)
409 *ErrMsg = "Program::Wait is not implemented on this platform yet!";
Mark Seaborn8d5b0e22014-01-27 22:53:07 +0000410 ProcessInfo WaitResult;
Tareq A. Sirajd88b9832013-10-01 14:28:18 +0000411 WaitResult.ReturnCode = -2;
Reid Spencerccd5b902005-12-22 20:00:16 +0000412#endif
Tareq A. Sirajd88b9832013-10-01 14:28:18 +0000413 return WaitResult;
Reid Spencer76b83a12004-08-29 19:20:41 +0000414}
415
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000416 std::error_code sys::ChangeStdinToBinary(){
Reid Spencerab97f222006-06-07 23:18:34 +0000417 // Do nothing, as Unix doesn't differentiate between text and binary.
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000418 return std::error_code();
Reid Spencerab97f222006-06-07 23:18:34 +0000419}
420
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000421 std::error_code sys::ChangeStdoutToBinary(){
Reid Spencerab97f222006-06-07 23:18:34 +0000422 // Do nothing, as Unix doesn't differentiate between text and binary.
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000423 return std::error_code();
Reid Spencerab97f222006-06-07 23:18:34 +0000424}
425
Rafael Espindola9c359662014-09-03 20:02:00 +0000426std::error_code
427llvm::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 Espindolacd848c02013-04-11 14:06:34 +0000443bool 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 Espindola42036ae2014-08-25 22:53:21 +0000451 long HalfArgMax = ArgMax / 2;
Rafael Espindolacd848c02013-04-11 14:06:34 +0000452
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 Espindola42036ae2014-08-25 22:53:21 +0000457 if (ArgLength > size_t(HalfArgMax)) {
Rafael Espindolacd848c02013-04-11 14:06:34 +0000458 return false;
459 }
460 }
461 return true;
462}
Reid Spencer76b83a12004-08-29 19:20:41 +0000463}