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