Program.inc 15 KB

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