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