1//===- Unix/Process.cpp - Unix Process Implementation --------- -*- 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 provides the generic Unix implementation of the Process class. 10// 11//===----------------------------------------------------------------------===// 12 13#include "Unix.h" 14#include "llvm/ADT/Hashing.h" 15#include "llvm/ADT/StringRef.h" 16#include "llvm/Config/config.h" 17#include "llvm/Config/llvm-config.h" // for LLVM_ENABLE_THREADS 18#include <mutex> 19#include <optional> 20#include <fcntl.h> 21#include <sys/time.h> 22#include <sys/resource.h> 23#include <sys/stat.h> 24#include <signal.h> 25#if defined(HAVE_MALLINFO) || defined(HAVE_MALLINFO2) 26#include <malloc.h> 27#endif 28#if defined(HAVE_MALLCTL) 29#include <malloc_np.h> 30#endif 31#ifdef HAVE_MALLOC_MALLOC_H 32#include <malloc/malloc.h> 33#endif 34#ifdef HAVE_GETAUXVAL 35#include <sys/auxv.h> 36#endif 37#ifdef HAVE_SYS_IOCTL_H 38#include <sys/ioctl.h> 39#endif 40 41//===----------------------------------------------------------------------===// 42//=== WARNING: Implementation here must contain only generic UNIX code that 43//=== is guaranteed to work on *all* UNIX variants. 44//===----------------------------------------------------------------------===// 45 46using namespace llvm; 47using namespace sys; 48 49static std::pair<std::chrono::microseconds, std::chrono::microseconds> 50getRUsageTimes() { 51#if defined(HAVE_GETRUSAGE) 52 struct rusage RU; 53 ::getrusage(RUSAGE_SELF, &RU); 54 return {toDuration(RU.ru_utime), toDuration(RU.ru_stime)}; 55#else 56#ifndef __MVS__ // Exclude for MVS in case -pedantic is used 57#warning Cannot get usage times on this platform 58#endif 59 return {std::chrono::microseconds::zero(), std::chrono::microseconds::zero()}; 60#endif 61} 62 63Process::Pid Process::getProcessId() { 64 static_assert(sizeof(Pid) >= sizeof(pid_t), 65 "Process::Pid should be big enough to store pid_t"); 66 return Pid(::getpid()); 67} 68 69// On Cygwin, getpagesize() returns 64k(AllocationGranularity) and 70// offset in mmap(3) should be aligned to the AllocationGranularity. 71Expected<unsigned> Process::getPageSize() { 72#if defined(HAVE_GETAUXVAL) 73 static const int page_size = ::getauxval(AT_PAGESZ); 74#elif defined(HAVE_GETPAGESIZE) 75 static const int page_size = ::getpagesize(); 76#elif defined(HAVE_SYSCONF) 77 static long page_size = ::sysconf(_SC_PAGE_SIZE); 78#else 79#error Cannot get the page size on this machine 80#endif 81 if (page_size == -1) 82 return errorCodeToError(errnoAsErrorCode()); 83 84 assert(page_size > 0 && "Page size cannot be 0"); 85 assert((page_size % 1024) == 0 && "Page size must be aligned by 1024"); 86 return static_cast<unsigned>(page_size); 87} 88 89size_t Process::GetMallocUsage() { 90#if defined(HAVE_MALLINFO2) 91 struct mallinfo2 mi; 92 mi = ::mallinfo2(); 93 return mi.uordblks; 94#elif defined(HAVE_MALLINFO) 95 struct mallinfo mi; 96 mi = ::mallinfo(); 97 return mi.uordblks; 98#elif defined(HAVE_MALLOC_ZONE_STATISTICS) && defined(HAVE_MALLOC_MALLOC_H) 99 malloc_statistics_t Stats; 100 malloc_zone_statistics(malloc_default_zone(), &Stats); 101 return Stats.size_in_use; // darwin 102#elif defined(HAVE_MALLCTL) 103 size_t alloc, sz; 104 sz = sizeof(size_t); 105 if (mallctl("stats.allocated", &alloc, &sz, NULL, 0) == 0) 106 return alloc; 107 return 0; 108#elif defined(HAVE_SBRK) 109 // Note this is only an approximation and more closely resembles 110 // the value returned by mallinfo in the arena field. 111 static char *StartOfMemory = reinterpret_cast<char *>(::sbrk(0)); 112 char *EndOfMemory = (char *)sbrk(0); 113 if (EndOfMemory != ((char *)-1) && StartOfMemory != ((char *)-1)) 114 return EndOfMemory - StartOfMemory; 115 return 0; 116#else 117#ifndef __MVS__ // Exclude for MVS in case -pedantic is used 118#warning Cannot get malloc info on this platform 119#endif 120 return 0; 121#endif 122} 123 124void Process::GetTimeUsage(TimePoint<> &elapsed, 125 std::chrono::nanoseconds &user_time, 126 std::chrono::nanoseconds &sys_time) { 127 elapsed = std::chrono::system_clock::now(); 128 std::tie(user_time, sys_time) = getRUsageTimes(); 129} 130 131#if defined(HAVE_MACH_MACH_H) && !defined(__GNU__) 132#include <mach/mach.h> 133#endif 134 135// Some LLVM programs such as bugpoint produce core files as a normal part of 136// their operation. To prevent the disk from filling up, this function 137// does what's necessary to prevent their generation. 138void Process::PreventCoreFiles() { 139 struct rlimit rlim; 140 getrlimit(RLIMIT_CORE, &rlim); 141#ifdef __linux__ 142 // On Linux, if the kernel.core_pattern sysctl starts with a '|' (i.e. it 143 // is being piped to a coredump handler such as systemd-coredumpd), the 144 // kernel ignores RLIMIT_CORE (since we aren't creating a file in the file 145 // system) except for the magic value of 1, which disables coredumps when 146 // piping. 1 byte is too small for any kind of valid core dump, so it 147 // also disables coredumps if kernel.core_pattern creates files directly. 148 // While most piped coredump handlers do respect the crashing processes' 149 // RLIMIT_CORE, this is notable not the case for Debian's systemd-coredump 150 // due to a local patch that changes sysctl.d/50-coredump.conf to ignore 151 // the specified limit and instead use RLIM_INFINITY. 152 // 153 // The alternative to using RLIMIT_CORE=1 would be to use prctl() with the 154 // PR_SET_DUMPABLE flag, however that also prevents ptrace(), so makes it 155 // impossible to attach a debugger. 156 rlim.rlim_cur = std::min<rlim_t>(1, rlim.rlim_max); 157#else 158 rlim.rlim_cur = 0; 159#endif 160 setrlimit(RLIMIT_CORE, &rlim); 161 162#if defined(HAVE_MACH_MACH_H) && !defined(__GNU__) 163 // Disable crash reporting on Mac OS X 10.0-10.4 164 165 // get information about the original set of exception ports for the task 166 mach_msg_type_number_t Count = 0; 167 exception_mask_t OriginalMasks[EXC_TYPES_COUNT]; 168 exception_port_t OriginalPorts[EXC_TYPES_COUNT]; 169 exception_behavior_t OriginalBehaviors[EXC_TYPES_COUNT]; 170 thread_state_flavor_t OriginalFlavors[EXC_TYPES_COUNT]; 171 kern_return_t err = task_get_exception_ports( 172 mach_task_self(), EXC_MASK_ALL, OriginalMasks, &Count, OriginalPorts, 173 OriginalBehaviors, OriginalFlavors); 174 if (err == KERN_SUCCESS) { 175 // replace each with MACH_PORT_NULL. 176 for (unsigned i = 0; i != Count; ++i) 177 task_set_exception_ports(mach_task_self(), OriginalMasks[i], 178 MACH_PORT_NULL, OriginalBehaviors[i], 179 OriginalFlavors[i]); 180 } 181 182 // Disable crash reporting on Mac OS X 10.5 183 signal(SIGABRT, _exit); 184 signal(SIGILL, _exit); 185 signal(SIGFPE, _exit); 186 signal(SIGSEGV, _exit); 187 signal(SIGBUS, _exit); 188#endif 189 190 coreFilesPrevented = true; 191} 192 193std::optional<std::string> Process::GetEnv(StringRef Name) { 194 std::string NameStr = Name.str(); 195 const char *Val = ::getenv(NameStr.c_str()); 196 if (!Val) 197 return std::nullopt; 198 return std::string(Val); 199} 200 201namespace { 202class FDCloser { 203public: 204 FDCloser(int &FD) : FD(FD), KeepOpen(false) {} 205 void keepOpen() { KeepOpen = true; } 206 ~FDCloser() { 207 if (!KeepOpen && FD >= 0) 208 ::close(FD); 209 } 210 211private: 212 FDCloser(const FDCloser &) = delete; 213 void operator=(const FDCloser &) = delete; 214 215 int &FD; 216 bool KeepOpen; 217}; 218} // namespace 219 220std::error_code Process::FixupStandardFileDescriptors() { 221 int NullFD = -1; 222 FDCloser FDC(NullFD); 223 const int StandardFDs[] = {STDIN_FILENO, STDOUT_FILENO, STDERR_FILENO}; 224 for (int StandardFD : StandardFDs) { 225 struct stat st; 226 errno = 0; 227 if (RetryAfterSignal(-1, ::fstat, StandardFD, &st) < 0) { 228 assert(errno && "expected errno to be set if fstat failed!"); 229 // fstat should return EBADF if the file descriptor is closed. 230 if (errno != EBADF) 231 return errnoAsErrorCode(); 232 } 233 // if fstat succeeds, move on to the next FD. 234 if (!errno) 235 continue; 236 assert(errno == EBADF && "expected errno to have EBADF at this point!"); 237 238 if (NullFD < 0) { 239 // Call ::open in a lambda to avoid overload resolution in 240 // RetryAfterSignal when open is overloaded, such as in Bionic. 241 auto Open = [&]() { return ::open("/dev/null", O_RDWR); }; 242 if ((NullFD = RetryAfterSignal(-1, Open)) < 0) 243 return errnoAsErrorCode(); 244 } 245 246 if (NullFD == StandardFD) 247 FDC.keepOpen(); 248 else if (dup2(NullFD, StandardFD) < 0) 249 return errnoAsErrorCode(); 250 } 251 return std::error_code(); 252} 253 254// Close a file descriptor while being mindful of EINTR. 255// 256// On Unix systems closing a file descriptor usually starts with removing it 257// from the fd table (or an equivalent structure). This means any error 258// generated past that point will still result in the entry being cleared. 259// 260// Make sure to not bubble up EINTR as there is nothing to do in that case. 261// XXX what about other errors? 262#if defined(__linux__) || defined(__DragonFly__) || defined(__FreeBSD__) || \ 263 defined(__NetBSD__) || defined(__OpenBSD__) 264std::error_code Process::SafelyCloseFileDescriptor(int FD) { 265 int EC = 0; 266 if (::close(FD) < 0) { 267 if (errno != EINTR) 268 EC = errno; 269 } 270 return std::error_code(EC, std::generic_category()); 271} 272#else 273std::error_code Process::SafelyCloseFileDescriptor(int FD) { 274 // Create a signal set filled with *all* signals. 275 sigset_t FullSet, SavedSet; 276 if (sigfillset(&FullSet) < 0 || sigfillset(&SavedSet) < 0) 277 return errnoAsErrorCode(); 278 279 // Atomically swap our current signal mask with a full mask. 280#if LLVM_ENABLE_THREADS 281 if (int EC = pthread_sigmask(SIG_SETMASK, &FullSet, &SavedSet)) 282 return std::error_code(EC, std::generic_category()); 283#else 284 if (sigprocmask(SIG_SETMASK, &FullSet, &SavedSet) < 0) 285 return errnoAsErrorCode(); 286#endif 287 // Attempt to close the file descriptor. 288 // We need to save the error, if one occurs, because our subsequent call to 289 // pthread_sigmask might tamper with errno. 290 int ErrnoFromClose = 0; 291 if (::close(FD) < 0) 292 ErrnoFromClose = errno; 293 // Restore the signal mask back to what we saved earlier. 294 int EC = 0; 295#if LLVM_ENABLE_THREADS 296 EC = pthread_sigmask(SIG_SETMASK, &SavedSet, nullptr); 297#else 298 if (sigprocmask(SIG_SETMASK, &SavedSet, nullptr) < 0) 299 EC = errno; 300#endif 301 // The error code from close takes precedence over the one from 302 // pthread_sigmask. 303 if (ErrnoFromClose) 304 return std::error_code(ErrnoFromClose, std::generic_category()); 305 return std::error_code(EC, std::generic_category()); 306} 307#endif 308 309bool Process::StandardInIsUserInput() { 310 return FileDescriptorIsDisplayed(STDIN_FILENO); 311} 312 313bool Process::StandardOutIsDisplayed() { 314 return FileDescriptorIsDisplayed(STDOUT_FILENO); 315} 316 317bool Process::StandardErrIsDisplayed() { 318 return FileDescriptorIsDisplayed(STDERR_FILENO); 319} 320 321bool Process::FileDescriptorIsDisplayed(int fd) { 322#if HAVE_ISATTY 323 return isatty(fd); 324#else 325 // If we don't have isatty, just return false. 326 return false; 327#endif 328} 329 330static unsigned getColumns(int FileID) { 331 // If COLUMNS is defined in the environment, wrap to that many columns. 332 // This matches GCC. 333 if (const char *ColumnsStr = std::getenv("COLUMNS")) { 334 int Columns = std::atoi(ColumnsStr); 335 if (Columns > 0) 336 return Columns; 337 } 338 339 // Some shells do not export COLUMNS; query the column count via ioctl() 340 // instead if it isn't available. 341 unsigned Columns = 0; 342 343#if defined(HAVE_SYS_IOCTL_H) && !defined(__sun__) 344 struct winsize ws; 345 if (ioctl(FileID, TIOCGWINSZ, &ws) == 0) 346 Columns = ws.ws_col; 347#endif 348 349 return Columns; 350} 351 352unsigned Process::StandardOutColumns() { 353 if (!StandardOutIsDisplayed()) 354 return 0; 355 356 return getColumns(0); 357} 358 359unsigned Process::StandardErrColumns() { 360 if (!StandardErrIsDisplayed()) 361 return 0; 362 363 return getColumns(1); 364} 365 366static bool terminalHasColors() { 367 // Check if the current terminal is one of terminals that are known to support 368 // ANSI color escape codes. 369 if (const char *TermStr = std::getenv("TERM")) { 370 return StringSwitch<bool>(TermStr) 371 .Case("ansi", true) 372 .Case("cygwin", true) 373 .Case("linux", true) 374 .StartsWith("screen", true) 375 .StartsWith("xterm", true) 376 .StartsWith("vt100", true) 377 .StartsWith("rxvt", true) 378 .EndsWith("color", true) 379 .Default(false); 380 } 381 382 return false; 383} 384 385bool Process::FileDescriptorHasColors(int fd) { 386 // A file descriptor has colors if it is displayed and the terminal has 387 // colors. 388 return FileDescriptorIsDisplayed(fd) && terminalHasColors(); 389} 390 391bool Process::StandardOutHasColors() { 392 return FileDescriptorHasColors(STDOUT_FILENO); 393} 394 395bool Process::StandardErrHasColors() { 396 return FileDescriptorHasColors(STDERR_FILENO); 397} 398 399void Process::UseANSIEscapeCodes(bool /*enable*/) { 400 // No effect. 401} 402 403bool Process::ColorNeedsFlush() { 404 // No, we use ANSI escape sequences. 405 return false; 406} 407 408const char *Process::OutputColor(char code, bool bold, bool bg) { 409 return colorcodes[bg ? 1 : 0][bold ? 1 : 0][code & 15]; 410} 411 412const char *Process::OutputBold(bool bg) { return "\033[1m"; } 413 414const char *Process::OutputReverse() { return "\033[7m"; } 415 416const char *Process::ResetColor() { return "\033[0m"; } 417 418#if !HAVE_DECL_ARC4RANDOM 419static unsigned GetRandomNumberSeed() { 420 // Attempt to get the initial seed from /dev/urandom, if possible. 421 int urandomFD = open("/dev/urandom", O_RDONLY); 422 423 if (urandomFD != -1) { 424 unsigned seed; 425 // Don't use a buffered read to avoid reading more data 426 // from /dev/urandom than we need. 427 int count = read(urandomFD, (void *)&seed, sizeof(seed)); 428 429 close(urandomFD); 430 431 // Return the seed if the read was successful. 432 if (count == sizeof(seed)) 433 return seed; 434 } 435 436 // Otherwise, swizzle the current time and the process ID to form a reasonable 437 // seed. 438 const auto Now = std::chrono::high_resolution_clock::now(); 439 return hash_combine(Now.time_since_epoch().count(), ::getpid()); 440} 441#endif 442 443unsigned llvm::sys::Process::GetRandomNumber() { 444#if HAVE_DECL_ARC4RANDOM 445 return arc4random(); 446#else 447 static int x = (static_cast<void>(::srand(GetRandomNumberSeed())), 0); 448 (void)x; 449 return ::rand(); 450#endif 451} 452 453[[noreturn]] void Process::ExitNoCleanup(int RetCode) { _Exit(RetCode); } 454