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