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