Program.inc 17 KB

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