xref: /freebsd/contrib/llvm-project/llvm/lib/Support/Unix/Program.inc (revision fe815331bb40604ba31312acf7e4619674631777)
1//===- llvm/Support/Unix/Program.cpp -----------------------------*- C++ -*-===//
2//
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
6//
7//===----------------------------------------------------------------------===//
8//
9// This file implements the Unix specific portion of the Program class.
10//
11//===----------------------------------------------------------------------===//
12
13//===----------------------------------------------------------------------===//
14//=== WARNING: Implementation here must contain only generic UNIX code that
15//===          is guaranteed to work on *all* UNIX variants.
16//===----------------------------------------------------------------------===//
17
18#include "llvm/Support/Program.h"
19
20#include "Unix.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"
29#if HAVE_SYS_STAT_H
30#include <sys/stat.h>
31#endif
32#if HAVE_SYS_RESOURCE_H
33#include <sys/resource.h>
34#endif
35#if HAVE_SIGNAL_H
36#include <signal.h>
37#endif
38#if HAVE_FCNTL_H
39#include <fcntl.h>
40#endif
41#if HAVE_UNISTD_H
42#include <unistd.h>
43#endif
44#ifdef HAVE_POSIX_SPAWN
45#include <spawn.h>
46
47#if defined(__APPLE__)
48#include <TargetConditionals.h>
49#endif
50
51#if defined(__APPLE__) && !(defined(TARGET_OS_IPHONE) && TARGET_OS_IPHONE)
52#define USE_NSGETENVIRON 1
53#else
54#define USE_NSGETENVIRON 0
55#endif
56
57#if !USE_NSGETENVIRON
58  extern char **environ;
59#else
60#include <crt_externs.h> // _NSGetEnviron
61#endif
62#endif
63
64using namespace llvm;
65using namespace sys;
66
67ProcessInfo::ProcessInfo() : Pid(0), ReturnCode(0) {}
68
69ErrorOr<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);
75
76  SmallVector<StringRef, 16> EnvironmentPaths;
77  if (Paths.empty())
78    if (const char *PathEnv = std::getenv("PATH")) {
79      SplitString(PathEnv, EnvironmentPaths, ":");
80      Paths = EnvironmentPaths;
81    }
82
83  for (auto Path : Paths) {
84    if (Path.empty())
85      continue;
86
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!
92  }
93  return errc::no_such_file_or_directory;
94}
95
96static bool RedirectIO(Optional<StringRef> Path, int FD, std::string* ErrMsg) {
97  if (!Path) // Noop
98    return false;
99  std::string File;
100  if (Path->empty())
101    // Redirect empty paths to /dev/null
102    File = "/dev/null";
103  else
104    File = std::string(*Path);
105
106  // Open the file
107  int InFD = open(File.c_str(), FD == 0 ? O_RDONLY : O_WRONLY|O_CREAT, 0666);
108  if (InFD == -1) {
109    MakeErrMsg(ErrMsg, "Cannot open file '" + File + "' for "
110              + (FD == 0 ? "input" : "output"));
111    return true;
112  }
113
114  // Install it as the requested FD
115  if (dup2(InFD, FD) == -1) {
116    MakeErrMsg(ErrMsg, "Cannot dup2");
117    close(InFD);
118    return true;
119  }
120  close(InFD);      // Close the original FD
121  return false;
122}
123
124#ifdef HAVE_POSIX_SPAWN
125static bool RedirectIO_PS(const std::string *Path, int FD, std::string *ErrMsg,
126                          posix_spawn_file_actions_t *FileActions) {
127  if (!Path) // Noop
128    return false;
129  const char *File;
130  if (Path->empty())
131    // Redirect empty paths to /dev/null
132    File = "/dev/null";
133  else
134    File = Path->c_str();
135
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);
140  return false;
141}
142#endif
143
144static void TimeOutHandler(int Sig) {
145}
146
147static void SetMemoryLimits(unsigned size) {
148#if HAVE_SYS_RESOURCE_H && HAVE_GETRLIMIT && HAVE_SETRLIMIT
149  struct rlimit r;
150  __typeof__ (r.rlim_cur) limit = (__typeof__ (r.rlim_cur)) (size) * 1048576;
151
152  // Heap size
153  getrlimit (RLIMIT_DATA, &r);
154  r.rlim_cur = limit;
155  setrlimit (RLIMIT_DATA, &r);
156#ifdef RLIMIT_RSS
157  // Resident set size.
158  getrlimit (RLIMIT_RSS, &r);
159  r.rlim_cur = limit;
160  setrlimit (RLIMIT_RSS, &r);
161#endif
162#endif
163}
164
165static std::vector<const char *>
166toNullTerminatedCStringArray(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);
171  return Result;
172}
173
174static 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  if (!llvm::sys::fs::exists(Program)) {
179    if (ErrMsg)
180      *ErrMsg = std::string("Executable \"") + Program.str() +
181                std::string("\" doesn't exist!");
182    return false;
183  }
184
185  BumpPtrAllocator Allocator;
186  StringSaver Saver(Allocator);
187  std::vector<const char *> ArgVector, EnvVector;
188  const char **Argv = nullptr;
189  const char **Envp = nullptr;
190  ArgVector = toNullTerminatedCStringArray(Args, Saver);
191  Argv = ArgVector.data();
192  if (Env) {
193    EnvVector = toNullTerminatedCStringArray(*Env, Saver);
194    Envp = EnvVector.data();
195  }
196
197  // If this OS has posix_spawn and there is no memory limit being implied, use
198  // posix_spawn.  It is more efficient than fork/exec.
199#ifdef HAVE_POSIX_SPAWN
200  if (MemoryLimit == 0) {
201    posix_spawn_file_actions_t FileActionsStore;
202    posix_spawn_file_actions_t *FileActions = nullptr;
203
204    // If we call posix_spawn_file_actions_addopen we have to make sure the
205    // c strings we pass to it stay alive until the call to posix_spawn,
206    // so we copy any StringRefs into this variable.
207    std::string RedirectsStorage[3];
208
209    if (!Redirects.empty()) {
210      assert(Redirects.size() == 3);
211      std::string *RedirectsStr[3] = {nullptr, nullptr, nullptr};
212      for (int I = 0; I < 3; ++I) {
213        if (Redirects[I]) {
214          RedirectsStorage[I] = std::string(*Redirects[I]);
215          RedirectsStr[I] = &RedirectsStorage[I];
216        }
217      }
218
219      FileActions = &FileActionsStore;
220      posix_spawn_file_actions_init(FileActions);
221
222      // Redirect stdin/stdout.
223      if (RedirectIO_PS(RedirectsStr[0], 0, ErrMsg, FileActions) ||
224          RedirectIO_PS(RedirectsStr[1], 1, ErrMsg, FileActions))
225        return false;
226      if (!Redirects[1] || !Redirects[2] || *Redirects[1] != *Redirects[2]) {
227        // Just redirect stderr
228        if (RedirectIO_PS(RedirectsStr[2], 2, ErrMsg, FileActions))
229          return false;
230      } else {
231        // If stdout and stderr should go to the same place, redirect stderr
232        // to the FD already open for stdout.
233        if (int Err = posix_spawn_file_actions_adddup2(FileActions, 1, 2))
234          return !MakeErrMsg(ErrMsg, "Can't redirect stderr to stdout", Err);
235      }
236    }
237
238    if (!Envp)
239#if !USE_NSGETENVIRON
240      Envp = const_cast<const char **>(environ);
241#else
242      // environ is missing in dylibs.
243      Envp = const_cast<const char **>(*_NSGetEnviron());
244#endif
245
246    constexpr int maxRetries = 8;
247    int retries = 0;
248    pid_t PID;
249    int Err;
250    do {
251      PID = 0; // Make Valgrind happy.
252      Err = posix_spawn(&PID, Program.str().c_str(), FileActions,
253                        /*attrp*/ nullptr, const_cast<char **>(Argv),
254                        const_cast<char **>(Envp));
255    } while (Err == EINTR && ++retries < maxRetries);
256
257    if (FileActions)
258      posix_spawn_file_actions_destroy(FileActions);
259
260    if (Err)
261     return !MakeErrMsg(ErrMsg, "posix_spawn failed", Err);
262
263    PI.Pid = PID;
264    PI.Process = PID;
265
266    return true;
267  }
268#endif
269
270  // Create a child process.
271  int child = fork();
272  switch (child) {
273    // An error occurred:  Return to the caller.
274    case -1:
275      MakeErrMsg(ErrMsg, "Couldn't fork");
276      return false;
277
278    // Child process: Execute the program.
279    case 0: {
280      // Redirect file descriptors...
281      if (!Redirects.empty()) {
282        // Redirect stdin
283        if (RedirectIO(Redirects[0], 0, ErrMsg)) { return false; }
284        // Redirect stdout
285        if (RedirectIO(Redirects[1], 1, ErrMsg)) { return false; }
286        if (Redirects[1] && Redirects[2] && *Redirects[1] == *Redirects[2]) {
287          // If stdout and stderr should go to the same place, redirect stderr
288          // to the FD already open for stdout.
289          if (-1 == dup2(1,2)) {
290            MakeErrMsg(ErrMsg, "Can't redirect stderr to stdout");
291            return false;
292          }
293        } else {
294          // Just redirect stderr
295          if (RedirectIO(Redirects[2], 2, ErrMsg)) { return false; }
296        }
297      }
298
299      // Set memory limits
300      if (MemoryLimit!=0) {
301        SetMemoryLimits(MemoryLimit);
302      }
303
304      // Execute!
305      std::string PathStr = std::string(Program);
306      if (Envp != nullptr)
307        execve(PathStr.c_str(), const_cast<char **>(Argv),
308               const_cast<char **>(Envp));
309      else
310        execv(PathStr.c_str(), const_cast<char **>(Argv));
311      // If the execve() failed, we should exit. Follow Unix protocol and
312      // return 127 if the executable was not found, and 126 otherwise.
313      // Use _exit rather than exit so that atexit functions and static
314      // object destructors cloned from the parent process aren't
315      // redundantly run, and so that any data buffered in stdio buffers
316      // cloned from the parent aren't redundantly written out.
317      _exit(errno == ENOENT ? 127 : 126);
318    }
319
320    // Parent process: Break out of the switch to do our processing.
321    default:
322      break;
323  }
324
325  PI.Pid = child;
326  PI.Process = child;
327
328  return true;
329}
330
331namespace llvm {
332namespace sys {
333
334#ifndef _AIX
335using ::wait4;
336#else
337static pid_t (wait4)(pid_t pid, int *status, int options, struct rusage *usage);
338#endif
339
340} // namespace sys
341} // namespace llvm
342
343#ifdef _AIX
344#ifndef _ALL_SOURCE
345extern "C" pid_t (wait4)(pid_t pid, int *status, int options,
346                         struct rusage *usage);
347#endif
348pid_t (llvm::sys::wait4)(pid_t pid, int *status, int options,
349                         struct rusage *usage) {
350  assert(pid > 0 && "Only expecting to handle actual PID values!");
351  assert((options & ~WNOHANG) == 0 && "Expecting WNOHANG at most!");
352  assert(usage && "Expecting usage collection!");
353
354  // AIX wait4 does not work well with WNOHANG.
355  if (!(options & WNOHANG))
356    return ::wait4(pid, status, options, usage);
357
358  // For WNOHANG, we use waitid (which supports WNOWAIT) until the child process
359  // has terminated.
360  siginfo_t WaitIdInfo;
361  WaitIdInfo.si_pid = 0;
362  int WaitIdRetVal =
363      waitid(P_PID, pid, &WaitIdInfo, WNOWAIT | WEXITED | options);
364
365  if (WaitIdRetVal == -1 || WaitIdInfo.si_pid == 0)
366    return WaitIdRetVal;
367
368  assert(WaitIdInfo.si_pid == pid);
369
370  // The child has already terminated, so a blocking wait on it is okay in the
371  // absence of indiscriminate `wait` calls from the current process (which
372  // would cause the call here to fail with ECHILD).
373  return ::wait4(pid, status, options & ~WNOHANG, usage);
374}
375#endif
376
377ProcessInfo llvm::sys::Wait(const ProcessInfo &PI, unsigned SecondsToWait,
378                            bool WaitUntilTerminates, std::string *ErrMsg,
379                            Optional<ProcessStatistics> *ProcStat) {
380  struct sigaction Act, Old;
381  assert(PI.Pid && "invalid pid to wait on, process not started?");
382
383  int WaitPidOptions = 0;
384  pid_t ChildPid = PI.Pid;
385  if (WaitUntilTerminates) {
386    SecondsToWait = 0;
387  } else if (SecondsToWait) {
388    // Install a timeout handler.  The handler itself does nothing, but the
389    // simple fact of having a handler at all causes the wait below to return
390    // with EINTR, unlike if we used SIG_IGN.
391    memset(&Act, 0, sizeof(Act));
392    Act.sa_handler = TimeOutHandler;
393    sigemptyset(&Act.sa_mask);
394    sigaction(SIGALRM, &Act, &Old);
395    // FIXME The alarm signal may be delivered to another thread.
396    alarm(SecondsToWait);
397  } else if (SecondsToWait == 0)
398    WaitPidOptions = WNOHANG;
399
400  // Parent process: Wait for the child process to terminate.
401  int status;
402  ProcessInfo WaitResult;
403  rusage Info;
404  if (ProcStat)
405    ProcStat->reset();
406
407  do {
408    WaitResult.Pid = sys::wait4(ChildPid, &status, WaitPidOptions, &Info);
409  } while (WaitUntilTerminates && WaitResult.Pid == -1 && errno == EINTR);
410
411  if (WaitResult.Pid != PI.Pid) {
412    if (WaitResult.Pid == 0) {
413      // Non-blocking wait.
414      return WaitResult;
415    } else {
416      if (SecondsToWait && errno == EINTR) {
417        // Kill the child.
418        kill(PI.Pid, SIGKILL);
419
420        // Turn off the alarm and restore the signal handler
421        alarm(0);
422        sigaction(SIGALRM, &Old, nullptr);
423
424        // Wait for child to die
425        // FIXME This could grab some other child process out from another
426        // waiting thread and then leave a zombie anyway.
427        if (wait(&status) != ChildPid)
428          MakeErrMsg(ErrMsg, "Child timed out but wouldn't die");
429        else
430          MakeErrMsg(ErrMsg, "Child timed out", 0);
431
432        WaitResult.ReturnCode = -2; // Timeout detected
433        return WaitResult;
434      } else if (errno != EINTR) {
435        MakeErrMsg(ErrMsg, "Error waiting for child process");
436        WaitResult.ReturnCode = -1;
437        return WaitResult;
438      }
439    }
440  }
441
442  // We exited normally without timeout, so turn off the timer.
443  if (SecondsToWait && !WaitUntilTerminates) {
444    alarm(0);
445    sigaction(SIGALRM, &Old, nullptr);
446  }
447
448  if (ProcStat) {
449    std::chrono::microseconds UserT = toDuration(Info.ru_utime);
450    std::chrono::microseconds KernelT = toDuration(Info.ru_stime);
451    uint64_t PeakMemory = static_cast<uint64_t>(Info.ru_maxrss);
452    *ProcStat = ProcessStatistics{UserT + KernelT, UserT, PeakMemory};
453  }
454
455  // Return the proper exit status. Detect error conditions
456  // so we can return -1 for them and set ErrMsg informatively.
457  int result = 0;
458  if (WIFEXITED(status)) {
459    result = WEXITSTATUS(status);
460    WaitResult.ReturnCode = result;
461
462    if (result == 127) {
463      if (ErrMsg)
464        *ErrMsg = llvm::sys::StrError(ENOENT);
465      WaitResult.ReturnCode = -1;
466      return WaitResult;
467    }
468    if (result == 126) {
469      if (ErrMsg)
470        *ErrMsg = "Program could not be executed";
471      WaitResult.ReturnCode = -1;
472      return WaitResult;
473    }
474  } else if (WIFSIGNALED(status)) {
475    if (ErrMsg) {
476      *ErrMsg = strsignal(WTERMSIG(status));
477#ifdef WCOREDUMP
478      if (WCOREDUMP(status))
479        *ErrMsg += " (core dumped)";
480#endif
481    }
482    // Return a special value to indicate that the process received an unhandled
483    // signal during execution as opposed to failing to execute.
484    WaitResult.ReturnCode = -2;
485  }
486  return WaitResult;
487}
488
489std::error_code llvm::sys::ChangeStdinToBinary() {
490  // Do nothing, as Unix doesn't differentiate between text and binary.
491  return std::error_code();
492}
493
494std::error_code llvm::sys::ChangeStdoutToBinary() {
495  // Do nothing, as Unix doesn't differentiate between text and binary.
496  return std::error_code();
497}
498
499std::error_code
500llvm::sys::writeFileWithEncoding(StringRef FileName, StringRef Contents,
501                                 WindowsEncodingMethod Encoding /*unused*/) {
502  std::error_code EC;
503  llvm::raw_fd_ostream OS(FileName, EC, llvm::sys::fs::OpenFlags::OF_Text);
504
505  if (EC)
506    return EC;
507
508  OS << Contents;
509
510  if (OS.has_error())
511    return make_error_code(errc::io_error);
512
513  return EC;
514}
515
516bool llvm::sys::commandLineFitsWithinSystemLimits(StringRef Program,
517                                                  ArrayRef<StringRef> Args) {
518  static long ArgMax = sysconf(_SC_ARG_MAX);
519  // POSIX requires that _POSIX_ARG_MAX is 4096, which is the lowest possible
520  // value for ARG_MAX on a POSIX compliant system.
521  static long ArgMin = _POSIX_ARG_MAX;
522
523  // This the same baseline used by xargs.
524  long EffectiveArgMax = 128 * 1024;
525
526  if (EffectiveArgMax > ArgMax)
527    EffectiveArgMax = ArgMax;
528  else if (EffectiveArgMax < ArgMin)
529    EffectiveArgMax = ArgMin;
530
531  // System says no practical limit.
532  if (ArgMax == -1)
533    return true;
534
535  // Conservatively account for space required by environment variables.
536  long HalfArgMax = EffectiveArgMax / 2;
537
538  size_t ArgLength = Program.size() + 1;
539  for (StringRef Arg : Args) {
540    // Ensure that we do not exceed the MAX_ARG_STRLEN constant on Linux, which
541    // does not have a constant unlike what the man pages would have you
542    // believe. Since this limit is pretty high, perform the check
543    // unconditionally rather than trying to be aggressive and limiting it to
544    // Linux only.
545    if (Arg.size() >= (32 * 4096))
546      return false;
547
548    ArgLength += Arg.size() + 1;
549    if (ArgLength > size_t(HalfArgMax)) {
550      return false;
551    }
552  }
553
554  return true;
555}
556