1 //===- llvm/Support/Unix/Program.cpp -----------------------------*- C++ -*-===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 // This file implements the Unix specific portion of the Program class.
11 //===----------------------------------------------------------------------===//
13 //===----------------------------------------------------------------------===//
14 //=== WARNING: Implementation here must contain only generic UNIX code that
15 //=== is guaranteed to work on *all* UNIX variants.
16 //===----------------------------------------------------------------------===//
18 #include "llvm/Support/Program.h"
21 #include "llvm/ADT/StringExtras.h"
22 #include "llvm/Config/config.h"
23 #include "llvm/Support/Compiler.h"
24 #include "llvm/Support/Errc.h"
25 #include "llvm/Support/FileSystem.h"
26 #include "llvm/Support/Path.h"
27 #include "llvm/Support/StringSaver.h"
28 #include "llvm/Support/raw_ostream.h"
32 #if HAVE_SYS_RESOURCE_H
33 #include <sys/resource.h>
44 #ifdef HAVE_POSIX_SPAWN
47 #if defined(__APPLE__)
48 #include <TargetConditionals.h>
51 #if defined(__APPLE__) && !(defined(TARGET_OS_IPHONE) && TARGET_OS_IPHONE)
52 #define USE_NSGETENVIRON 1
54 #define USE_NSGETENVIRON 0
58 extern char **environ;
60 #include <crt_externs.h> // _NSGetEnviron
67 ProcessInfo::ProcessInfo() : Pid(0), ReturnCode(0) {}
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) return std::string(Name);
76 SmallVector<StringRef, 16> EnvironmentPaths;
78 if (const char *PathEnv = std::getenv("PATH")) {
79 SplitString(PathEnv, EnvironmentPaths, ":");
80 Paths = EnvironmentPaths;
83 for (auto Path : Paths) {
87 // Check to see if this first directory contains the executable...
88 SmallString<128> FilePath(Path);
89 sys::path::append(FilePath, Name);
90 if (sys::fs::can_execute(FilePath.c_str()))
91 return std::string(FilePath.str()); // Found the executable!
93 return errc::no_such_file_or_directory;
96 static bool RedirectIO(Optional<StringRef> Path, int FD, std::string* ErrMsg) {
101 // Redirect empty paths to /dev/null
104 File = std::string(*Path);
107 int InFD = open(File.c_str(), FD == 0 ? O_RDONLY : O_WRONLY|O_CREAT, 0666);
109 MakeErrMsg(ErrMsg, "Cannot open file '" + File + "' for "
110 + (FD == 0 ? "input" : "output"));
114 // Install it as the requested FD
115 if (dup2(InFD, FD) == -1) {
116 MakeErrMsg(ErrMsg, "Cannot dup2");
120 close(InFD); // Close the original FD
124 #ifdef HAVE_POSIX_SPAWN
125 static bool RedirectIO_PS(const std::string *Path, int FD, std::string *ErrMsg,
126 posix_spawn_file_actions_t *FileActions) {
131 // Redirect empty paths to /dev/null
134 File = Path->c_str();
136 if (int Err = posix_spawn_file_actions_addopen(
137 FileActions, FD, File,
138 FD == 0 ? O_RDONLY : O_WRONLY | O_CREAT, 0666))
139 return MakeErrMsg(ErrMsg, "Cannot posix_spawn_file_actions_addopen", Err);
144 static void TimeOutHandler(int Sig) {
147 static void SetMemoryLimits(unsigned size) {
148 #if HAVE_SYS_RESOURCE_H && HAVE_GETRLIMIT && HAVE_SETRLIMIT
150 __typeof__ (r.rlim_cur) limit = (__typeof__ (r.rlim_cur)) (size) * 1048576;
153 getrlimit (RLIMIT_DATA, &r);
155 setrlimit (RLIMIT_DATA, &r);
157 // Resident set size.
158 getrlimit (RLIMIT_RSS, &r);
160 setrlimit (RLIMIT_RSS, &r);
165 static std::vector<const char *>
166 toNullTerminatedCStringArray(ArrayRef<StringRef> Strings, StringSaver &Saver) {
167 std::vector<const char *> Result;
168 for (StringRef S : Strings)
169 Result.push_back(Saver.save(S).data());
170 Result.push_back(nullptr);
174 static bool Execute(ProcessInfo &PI, StringRef Program,
175 ArrayRef<StringRef> Args, Optional<ArrayRef<StringRef>> Env,
176 ArrayRef<Optional<StringRef>> Redirects,
177 unsigned MemoryLimit, std::string *ErrMsg,
178 BitVector *AffinityMask) {
179 if (!llvm::sys::fs::exists(Program)) {
181 *ErrMsg = std::string("Executable \"") + Program.str() +
182 std::string("\" doesn't exist!");
186 assert(!AffinityMask && "Starting a process with an affinity mask is "
187 "currently not supported on Unix!");
189 BumpPtrAllocator Allocator;
190 StringSaver Saver(Allocator);
191 std::vector<const char *> ArgVector, EnvVector;
192 const char **Argv = nullptr;
193 const char **Envp = nullptr;
194 ArgVector = toNullTerminatedCStringArray(Args, Saver);
195 Argv = ArgVector.data();
197 EnvVector = toNullTerminatedCStringArray(*Env, Saver);
198 Envp = EnvVector.data();
201 // If this OS has posix_spawn and there is no memory limit being implied, use
202 // posix_spawn. It is more efficient than fork/exec.
203 #ifdef HAVE_POSIX_SPAWN
204 if (MemoryLimit == 0) {
205 posix_spawn_file_actions_t FileActionsStore;
206 posix_spawn_file_actions_t *FileActions = nullptr;
208 // If we call posix_spawn_file_actions_addopen we have to make sure the
209 // c strings we pass to it stay alive until the call to posix_spawn,
210 // so we copy any StringRefs into this variable.
211 std::string RedirectsStorage[3];
213 if (!Redirects.empty()) {
214 assert(Redirects.size() == 3);
215 std::string *RedirectsStr[3] = {nullptr, nullptr, nullptr};
216 for (int I = 0; I < 3; ++I) {
218 RedirectsStorage[I] = std::string(*Redirects[I]);
219 RedirectsStr[I] = &RedirectsStorage[I];
223 FileActions = &FileActionsStore;
224 posix_spawn_file_actions_init(FileActions);
226 // Redirect stdin/stdout.
227 if (RedirectIO_PS(RedirectsStr[0], 0, ErrMsg, FileActions) ||
228 RedirectIO_PS(RedirectsStr[1], 1, ErrMsg, FileActions))
230 if (!Redirects[1] || !Redirects[2] || *Redirects[1] != *Redirects[2]) {
231 // Just redirect stderr
232 if (RedirectIO_PS(RedirectsStr[2], 2, ErrMsg, FileActions))
235 // If stdout and stderr should go to the same place, redirect stderr
236 // to the FD already open for stdout.
237 if (int Err = posix_spawn_file_actions_adddup2(FileActions, 1, 2))
238 return !MakeErrMsg(ErrMsg, "Can't redirect stderr to stdout", Err);
243 #if !USE_NSGETENVIRON
244 Envp = const_cast<const char **>(environ);
246 // environ is missing in dylibs.
247 Envp = const_cast<const char **>(*_NSGetEnviron());
250 constexpr int maxRetries = 8;
255 PID = 0; // Make Valgrind happy.
256 Err = posix_spawn(&PID, Program.str().c_str(), FileActions,
257 /*attrp*/ nullptr, const_cast<char **>(Argv),
258 const_cast<char **>(Envp));
259 } while (Err == EINTR && ++retries < maxRetries);
262 posix_spawn_file_actions_destroy(FileActions);
265 return !MakeErrMsg(ErrMsg, "posix_spawn failed", Err);
274 // Create a child process.
277 // An error occurred: Return to the caller.
279 MakeErrMsg(ErrMsg, "Couldn't fork");
282 // Child process: Execute the program.
284 // Redirect file descriptors...
285 if (!Redirects.empty()) {
287 if (RedirectIO(Redirects[0], 0, ErrMsg)) { return false; }
289 if (RedirectIO(Redirects[1], 1, ErrMsg)) { return false; }
290 if (Redirects[1] && Redirects[2] && *Redirects[1] == *Redirects[2]) {
291 // If stdout and stderr should go to the same place, redirect stderr
292 // to the FD already open for stdout.
293 if (-1 == dup2(1,2)) {
294 MakeErrMsg(ErrMsg, "Can't redirect stderr to stdout");
298 // Just redirect stderr
299 if (RedirectIO(Redirects[2], 2, ErrMsg)) { return false; }
304 if (MemoryLimit!=0) {
305 SetMemoryLimits(MemoryLimit);
309 std::string PathStr = std::string(Program);
311 execve(PathStr.c_str(), const_cast<char **>(Argv),
312 const_cast<char **>(Envp));
314 execv(PathStr.c_str(), const_cast<char **>(Argv));
315 // If the execve() failed, we should exit. Follow Unix protocol and
316 // return 127 if the executable was not found, and 126 otherwise.
317 // Use _exit rather than exit so that atexit functions and static
318 // object destructors cloned from the parent process aren't
319 // redundantly run, and so that any data buffered in stdio buffers
320 // cloned from the parent aren't redundantly written out.
321 _exit(errno == ENOENT ? 127 : 126);
324 // Parent process: Break out of the switch to do our processing.
341 static pid_t (wait4)(pid_t pid, int *status, int options, struct rusage *usage);
349 extern "C" pid_t (wait4)(pid_t pid, int *status, int options,
350 struct rusage *usage);
352 pid_t (llvm::sys::wait4)(pid_t pid, int *status, int options,
353 struct rusage *usage) {
354 assert(pid > 0 && "Only expecting to handle actual PID values!");
355 assert((options & ~WNOHANG) == 0 && "Expecting WNOHANG at most!");
356 assert(usage && "Expecting usage collection!");
358 // AIX wait4 does not work well with WNOHANG.
359 if (!(options & WNOHANG))
360 return ::wait4(pid, status, options, usage);
362 // For WNOHANG, we use waitid (which supports WNOWAIT) until the child process
364 siginfo_t WaitIdInfo;
365 WaitIdInfo.si_pid = 0;
367 waitid(P_PID, pid, &WaitIdInfo, WNOWAIT | WEXITED | options);
369 if (WaitIdRetVal == -1 || WaitIdInfo.si_pid == 0)
372 assert(WaitIdInfo.si_pid == pid);
374 // The child has already terminated, so a blocking wait on it is okay in the
375 // absence of indiscriminate `wait` calls from the current process (which
376 // would cause the call here to fail with ECHILD).
377 return ::wait4(pid, status, options & ~WNOHANG, usage);
381 ProcessInfo llvm::sys::Wait(const ProcessInfo &PI, unsigned SecondsToWait,
382 bool WaitUntilTerminates, std::string *ErrMsg,
383 Optional<ProcessStatistics> *ProcStat) {
384 struct sigaction Act, Old;
385 assert(PI.Pid && "invalid pid to wait on, process not started?");
387 int WaitPidOptions = 0;
388 pid_t ChildPid = PI.Pid;
389 if (WaitUntilTerminates) {
391 } else if (SecondsToWait) {
392 // Install a timeout handler. The handler itself does nothing, but the
393 // simple fact of having a handler at all causes the wait below to return
394 // with EINTR, unlike if we used SIG_IGN.
395 memset(&Act, 0, sizeof(Act));
396 Act.sa_handler = TimeOutHandler;
397 sigemptyset(&Act.sa_mask);
398 sigaction(SIGALRM, &Act, &Old);
399 // FIXME The alarm signal may be delivered to another thread.
400 alarm(SecondsToWait);
401 } else if (SecondsToWait == 0)
402 WaitPidOptions = WNOHANG;
404 // Parent process: Wait for the child process to terminate.
406 ProcessInfo WaitResult;
412 WaitResult.Pid = sys::wait4(ChildPid, &status, WaitPidOptions, &Info);
413 } while (WaitUntilTerminates && WaitResult.Pid == -1 && errno == EINTR);
415 if (WaitResult.Pid != PI.Pid) {
416 if (WaitResult.Pid == 0) {
417 // Non-blocking wait.
420 if (SecondsToWait && errno == EINTR) {
422 kill(PI.Pid, SIGKILL);
424 // Turn off the alarm and restore the signal handler
426 sigaction(SIGALRM, &Old, nullptr);
428 // Wait for child to die
429 // FIXME This could grab some other child process out from another
430 // waiting thread and then leave a zombie anyway.
431 if (wait(&status) != ChildPid)
432 MakeErrMsg(ErrMsg, "Child timed out but wouldn't die");
434 MakeErrMsg(ErrMsg, "Child timed out", 0);
436 WaitResult.ReturnCode = -2; // Timeout detected
438 } else if (errno != EINTR) {
439 MakeErrMsg(ErrMsg, "Error waiting for child process");
440 WaitResult.ReturnCode = -1;
446 // We exited normally without timeout, so turn off the timer.
447 if (SecondsToWait && !WaitUntilTerminates) {
449 sigaction(SIGALRM, &Old, nullptr);
453 std::chrono::microseconds UserT = toDuration(Info.ru_utime);
454 std::chrono::microseconds KernelT = toDuration(Info.ru_stime);
455 uint64_t PeakMemory = 0;
457 PeakMemory = static_cast<uint64_t>(Info.ru_maxrss);
459 *ProcStat = ProcessStatistics{UserT + KernelT, UserT, PeakMemory};
462 // Return the proper exit status. Detect error conditions
463 // so we can return -1 for them and set ErrMsg informatively.
465 if (WIFEXITED(status)) {
466 result = WEXITSTATUS(status);
467 WaitResult.ReturnCode = result;
471 *ErrMsg = llvm::sys::StrError(ENOENT);
472 WaitResult.ReturnCode = -1;
477 *ErrMsg = "Program could not be executed";
478 WaitResult.ReturnCode = -1;
481 } else if (WIFSIGNALED(status)) {
483 *ErrMsg = strsignal(WTERMSIG(status));
485 if (WCOREDUMP(status))
486 *ErrMsg += " (core dumped)";
489 // Return a special value to indicate that the process received an unhandled
490 // signal during execution as opposed to failing to execute.
491 WaitResult.ReturnCode = -2;
496 std::error_code llvm::sys::ChangeStdinMode(fs::OpenFlags Flags){
497 if (!(Flags & fs::OF_Text))
498 return ChangeStdinToBinary();
499 return std::error_code();
502 std::error_code llvm::sys::ChangeStdoutMode(fs::OpenFlags Flags){
503 if (!(Flags & fs::OF_Text))
504 return ChangeStdoutToBinary();
505 return std::error_code();
508 std::error_code llvm::sys::ChangeStdinToBinary() {
509 // Do nothing, as Unix doesn't differentiate between text and binary.
510 return std::error_code();
513 std::error_code llvm::sys::ChangeStdoutToBinary() {
514 // Do nothing, as Unix doesn't differentiate between text and binary.
515 return std::error_code();
519 llvm::sys::writeFileWithEncoding(StringRef FileName, StringRef Contents,
520 WindowsEncodingMethod Encoding /*unused*/) {
522 llvm::raw_fd_ostream OS(FileName, EC, llvm::sys::fs::OpenFlags::OF_TextWithCRLF);
530 return make_error_code(errc::io_error);
535 bool llvm::sys::commandLineFitsWithinSystemLimits(StringRef Program,
536 ArrayRef<StringRef> Args) {
537 static long ArgMax = sysconf(_SC_ARG_MAX);
538 // POSIX requires that _POSIX_ARG_MAX is 4096, which is the lowest possible
539 // value for ARG_MAX on a POSIX compliant system.
540 static long ArgMin = _POSIX_ARG_MAX;
542 // This the same baseline used by xargs.
543 long EffectiveArgMax = 128 * 1024;
545 if (EffectiveArgMax > ArgMax)
546 EffectiveArgMax = ArgMax;
547 else if (EffectiveArgMax < ArgMin)
548 EffectiveArgMax = ArgMin;
550 // System says no practical limit.
554 // Conservatively account for space required by environment variables.
555 long HalfArgMax = EffectiveArgMax / 2;
557 size_t ArgLength = Program.size() + 1;
558 for (StringRef Arg : Args) {
559 // Ensure that we do not exceed the MAX_ARG_STRLEN constant on Linux, which
560 // does not have a constant unlike what the man pages would have you
561 // believe. Since this limit is pretty high, perform the check
562 // unconditionally rather than trying to be aggressive and limiting it to
564 if (Arg.size() >= (32 * 4096))
567 ArgLength += Arg.size() + 1;
568 if (ArgLength > size_t(HalfArgMax)) {