Reid Spencer | 33b9d77 | 2004-09-11 04:56:56 +0000 | [diff] [blame] | 1 | //===- Unix/Process.cpp - Unix Process Implementation --------- -*- C++ -*-===// |
Michael J. Spencer | 447762d | 2010-11-29 18:16:10 +0000 | [diff] [blame] | 2 | // |
Reid Spencer | 33b9d77 | 2004-09-11 04:56:56 +0000 | [diff] [blame] | 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | f3ebc3f | 2007-12-29 20:36:04 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Michael J. Spencer | 447762d | 2010-11-29 18:16:10 +0000 | [diff] [blame] | 7 | // |
Reid Spencer | 33b9d77 | 2004-09-11 04:56:56 +0000 | [diff] [blame] | 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file provides the generic Unix implementation of the Process class. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
Reid Spencer | ac38f3a | 2004-12-20 00:59:28 +0000 | [diff] [blame] | 14 | #include "Unix.h" |
Daniel Dunbar | 5f1c956 | 2012-05-08 20:38:00 +0000 | [diff] [blame] | 15 | #include "llvm/ADT/Hashing.h" |
Rui Ueyama | 471d0c5 | 2013-09-10 19:45:51 +0000 | [diff] [blame] | 16 | #include "llvm/ADT/StringRef.h" |
Nico Weber | 432a388 | 2018-04-30 14:59:11 +0000 | [diff] [blame] | 17 | #include "llvm/Config/config.h" |
Chris Bieneman | fa35e11 | 2014-09-22 22:39:20 +0000 | [diff] [blame] | 18 | #include "llvm/Support/ManagedStatic.h" |
Chandler Carruth | cad7e5e | 2013-08-07 08:47:36 +0000 | [diff] [blame] | 19 | #include "llvm/Support/Mutex.h" |
| 20 | #include "llvm/Support/MutexGuard.h" |
David Majnemer | 121a174 | 2014-10-06 23:16:18 +0000 | [diff] [blame] | 21 | #if HAVE_FCNTL_H |
| 22 | #include <fcntl.h> |
| 23 | #endif |
Reid Spencer | ac38f3a | 2004-12-20 00:59:28 +0000 | [diff] [blame] | 24 | #ifdef HAVE_SYS_TIME_H |
| 25 | #include <sys/time.h> |
| 26 | #endif |
| 27 | #ifdef HAVE_SYS_RESOURCE_H |
| 28 | #include <sys/resource.h> |
| 29 | #endif |
Benjamin Kramer | 2416521 | 2014-10-12 22:49:26 +0000 | [diff] [blame] | 30 | #ifdef HAVE_SYS_STAT_H |
| 31 | #include <sys/stat.h> |
| 32 | #endif |
Duncan P. N. Exon Smith | 91d3cfe | 2016-04-05 20:45:04 +0000 | [diff] [blame] | 33 | #if HAVE_SIGNAL_H |
| 34 | #include <signal.h> |
| 35 | #endif |
Erich Keane | d8f61f8 | 2017-07-21 22:48:47 +0000 | [diff] [blame] | 36 | // DragonFlyBSD, and OpenBSD have deprecated <malloc.h> for |
Eric Christopher | 22738d0 | 2012-08-06 20:52:18 +0000 | [diff] [blame] | 37 | // <stdlib.h> instead. Unix.h includes this for us already. |
| 38 | #if defined(HAVE_MALLOC_H) && !defined(__DragonFly__) && \ |
Erich Keane | d8f61f8 | 2017-07-21 22:48:47 +0000 | [diff] [blame] | 39 | !defined(__OpenBSD__) |
Reid Spencer | ac38f3a | 2004-12-20 00:59:28 +0000 | [diff] [blame] | 40 | #include <malloc.h> |
| 41 | #endif |
Davide Italiano | faafae3 | 2015-02-19 07:27:14 +0000 | [diff] [blame] | 42 | #if defined(HAVE_MALLCTL) |
| 43 | #include <malloc_np.h> |
| 44 | #endif |
Chris Lattner | 698fa76 | 2005-11-14 07:00:29 +0000 | [diff] [blame] | 45 | #ifdef HAVE_MALLOC_MALLOC_H |
| 46 | #include <malloc/malloc.h> |
| 47 | #endif |
Douglas Gregor | 1543661 | 2009-05-11 18:05:52 +0000 | [diff] [blame] | 48 | #ifdef HAVE_SYS_IOCTL_H |
| 49 | # include <sys/ioctl.h> |
| 50 | #endif |
Douglas Gregor | b81294d | 2009-05-18 17:21:34 +0000 | [diff] [blame] | 51 | #ifdef HAVE_TERMIOS_H |
| 52 | # include <termios.h> |
| 53 | #endif |
Reid Spencer | 33b9d77 | 2004-09-11 04:56:56 +0000 | [diff] [blame] | 54 | |
| 55 | //===----------------------------------------------------------------------===// |
| 56 | //=== WARNING: Implementation here must contain only generic UNIX code that |
| 57 | //=== is guaranteed to work on *all* UNIX variants. |
| 58 | //===----------------------------------------------------------------------===// |
| 59 | |
Chris Lattner | 2f107cf | 2006-09-14 06:01:41 +0000 | [diff] [blame] | 60 | using namespace llvm; |
Reid Spencer | 33b9d77 | 2004-09-11 04:56:56 +0000 | [diff] [blame] | 61 | using namespace sys; |
Chandler Carruth | 97683aa | 2012-12-31 11:17:50 +0000 | [diff] [blame] | 62 | |
Pavel Labath | 757ca88 | 2016-10-24 10:59:17 +0000 | [diff] [blame] | 63 | static std::pair<std::chrono::microseconds, std::chrono::microseconds> getRUsageTimes() { |
Chandler Carruth | ef7f968 | 2013-01-04 23:19:55 +0000 | [diff] [blame] | 64 | #if defined(HAVE_GETRUSAGE) |
| 65 | struct rusage RU; |
| 66 | ::getrusage(RUSAGE_SELF, &RU); |
Pavel Labath | 757ca88 | 2016-10-24 10:59:17 +0000 | [diff] [blame] | 67 | return { toDuration(RU.ru_utime), toDuration(RU.ru_stime) }; |
Chandler Carruth | ef7f968 | 2013-01-04 23:19:55 +0000 | [diff] [blame] | 68 | #else |
| 69 | #warning Cannot get usage times on this platform |
Pavel Labath | 775bbc3 | 2016-11-09 11:43:57 +0000 | [diff] [blame] | 70 | return { std::chrono::microseconds::zero(), std::chrono::microseconds::zero() }; |
Chandler Carruth | ef7f968 | 2013-01-04 23:19:55 +0000 | [diff] [blame] | 71 | #endif |
| 72 | } |
| 73 | |
NAKAMURA Takumi | 7a04234 | 2013-09-04 14:12:26 +0000 | [diff] [blame] | 74 | // On Cygwin, getpagesize() returns 64k(AllocationGranularity) and |
| 75 | // offset in mmap(3) should be aligned to the AllocationGranularity. |
Rafael Espindola | c0610bf | 2014-12-04 16:59:36 +0000 | [diff] [blame] | 76 | unsigned Process::getPageSize() { |
NAKAMURA Takumi | 3bbbe2e | 2013-08-21 13:47:12 +0000 | [diff] [blame] | 77 | #if defined(HAVE_GETPAGESIZE) |
Rafael Espindola | c0610bf | 2014-12-04 16:59:36 +0000 | [diff] [blame] | 78 | static const int page_size = ::getpagesize(); |
Reid Spencer | ac38f3a | 2004-12-20 00:59:28 +0000 | [diff] [blame] | 79 | #elif defined(HAVE_SYSCONF) |
Rafael Espindola | c0610bf | 2014-12-04 16:59:36 +0000 | [diff] [blame] | 80 | static long page_size = ::sysconf(_SC_PAGE_SIZE); |
Reid Spencer | ac38f3a | 2004-12-20 00:59:28 +0000 | [diff] [blame] | 81 | #else |
Fangrui Song | 69d6418 | 2018-06-10 04:53:14 +0000 | [diff] [blame] | 82 | #error Cannot get the page size on this machine |
Reid Spencer | ac38f3a | 2004-12-20 00:59:28 +0000 | [diff] [blame] | 83 | #endif |
Reid Spencer | 33b9d77 | 2004-09-11 04:56:56 +0000 | [diff] [blame] | 84 | return static_cast<unsigned>(page_size); |
| 85 | } |
| 86 | |
Chris Lattner | 698fa76 | 2005-11-14 07:00:29 +0000 | [diff] [blame] | 87 | size_t Process::GetMallocUsage() { |
Reid Spencer | 1cf74ce | 2004-12-20 16:06:44 +0000 | [diff] [blame] | 88 | #if defined(HAVE_MALLINFO) |
Reid Spencer | ac38f3a | 2004-12-20 00:59:28 +0000 | [diff] [blame] | 89 | struct mallinfo mi; |
| 90 | mi = ::mallinfo(); |
| 91 | return mi.uordblks; |
Chris Lattner | 16cbc6a | 2005-11-14 07:27:56 +0000 | [diff] [blame] | 92 | #elif defined(HAVE_MALLOC_ZONE_STATISTICS) && defined(HAVE_MALLOC_MALLOC_H) |
| 93 | malloc_statistics_t Stats; |
| 94 | malloc_zone_statistics(malloc_default_zone(), &Stats); |
| 95 | return Stats.size_in_use; // darwin |
Davide Italiano | faafae3 | 2015-02-19 07:27:14 +0000 | [diff] [blame] | 96 | #elif defined(HAVE_MALLCTL) |
| 97 | size_t alloc, sz; |
| 98 | sz = sizeof(size_t); |
| 99 | if (mallctl("stats.allocated", &alloc, &sz, NULL, 0) == 0) |
| 100 | return alloc; |
Davide Italiano | 8d98196 | 2015-02-21 02:36:54 +0000 | [diff] [blame] | 101 | return 0; |
Reid Spencer | 1cf74ce | 2004-12-20 16:06:44 +0000 | [diff] [blame] | 102 | #elif defined(HAVE_SBRK) |
Reid Spencer | ac38f3a | 2004-12-20 00:59:28 +0000 | [diff] [blame] | 103 | // Note this is only an approximation and more closely resembles |
| 104 | // the value returned by mallinfo in the arena field. |
Chris Lattner | 698fa76 | 2005-11-14 07:00:29 +0000 | [diff] [blame] | 105 | static char *StartOfMemory = reinterpret_cast<char*>(::sbrk(0)); |
| 106 | char *EndOfMemory = (char*)sbrk(0); |
| 107 | if (EndOfMemory != ((char*)-1) && StartOfMemory != ((char*)-1)) |
| 108 | return EndOfMemory - StartOfMemory; |
Davide Italiano | 8d98196 | 2015-02-21 02:36:54 +0000 | [diff] [blame] | 109 | return 0; |
Reid Spencer | ac38f3a | 2004-12-20 00:59:28 +0000 | [diff] [blame] | 110 | #else |
| 111 | #warning Cannot get malloc info on this platform |
| 112 | return 0; |
| 113 | #endif |
| 114 | } |
| 115 | |
Pavel Labath | 757ca88 | 2016-10-24 10:59:17 +0000 | [diff] [blame] | 116 | void Process::GetTimeUsage(TimePoint<> &elapsed, std::chrono::nanoseconds &user_time, |
| 117 | std::chrono::nanoseconds &sys_time) { |
| 118 | elapsed = std::chrono::system_clock::now(); |
Benjamin Kramer | d6f1f84 | 2014-03-02 13:30:33 +0000 | [diff] [blame] | 119 | std::tie(user_time, sys_time) = getRUsageTimes(); |
Reid Spencer | ac38f3a | 2004-12-20 00:59:28 +0000 | [diff] [blame] | 120 | } |
| 121 | |
Sylvestre Ledru | 14ada94 | 2012-04-11 15:35:36 +0000 | [diff] [blame] | 122 | #if defined(HAVE_MACH_MACH_H) && !defined(__GNU__) |
Nate Begeman | 4840515 | 2008-04-12 00:47:46 +0000 | [diff] [blame] | 123 | #include <mach/mach.h> |
| 124 | #endif |
| 125 | |
Reid Spencer | cf15b87 | 2004-12-27 06:17:27 +0000 | [diff] [blame] | 126 | // Some LLVM programs such as bugpoint produce core files as a normal part of |
| 127 | // their operation. To prevent the disk from filling up, this function |
| 128 | // does what's necessary to prevent their generation. |
| 129 | void Process::PreventCoreFiles() { |
| 130 | #if HAVE_SETRLIMIT |
| 131 | struct rlimit rlim; |
| 132 | rlim.rlim_cur = rlim.rlim_max = 0; |
Chris Lattner | f64397b | 2006-05-14 18:53:09 +0000 | [diff] [blame] | 133 | setrlimit(RLIMIT_CORE, &rlim); |
Reid Spencer | cf15b87 | 2004-12-27 06:17:27 +0000 | [diff] [blame] | 134 | #endif |
Chris Lattner | 2f107cf | 2006-09-14 06:01:41 +0000 | [diff] [blame] | 135 | |
Sylvestre Ledru | 14ada94 | 2012-04-11 15:35:36 +0000 | [diff] [blame] | 136 | #if defined(HAVE_MACH_MACH_H) && !defined(__GNU__) |
Nate Begeman | 4840515 | 2008-04-12 00:47:46 +0000 | [diff] [blame] | 137 | // Disable crash reporting on Mac OS X 10.0-10.4 |
| 138 | |
| 139 | // get information about the original set of exception ports for the task |
| 140 | mach_msg_type_number_t Count = 0; |
| 141 | exception_mask_t OriginalMasks[EXC_TYPES_COUNT]; |
| 142 | exception_port_t OriginalPorts[EXC_TYPES_COUNT]; |
| 143 | exception_behavior_t OriginalBehaviors[EXC_TYPES_COUNT]; |
| 144 | thread_state_flavor_t OriginalFlavors[EXC_TYPES_COUNT]; |
Michael J. Spencer | 447762d | 2010-11-29 18:16:10 +0000 | [diff] [blame] | 145 | kern_return_t err = |
Nate Begeman | 4840515 | 2008-04-12 00:47:46 +0000 | [diff] [blame] | 146 | task_get_exception_ports(mach_task_self(), EXC_MASK_ALL, OriginalMasks, |
| 147 | &Count, OriginalPorts, OriginalBehaviors, |
| 148 | OriginalFlavors); |
| 149 | if (err == KERN_SUCCESS) { |
| 150 | // replace each with MACH_PORT_NULL. |
| 151 | for (unsigned i = 0; i != Count; ++i) |
Michael J. Spencer | 447762d | 2010-11-29 18:16:10 +0000 | [diff] [blame] | 152 | task_set_exception_ports(mach_task_self(), OriginalMasks[i], |
Nate Begeman | 4840515 | 2008-04-12 00:47:46 +0000 | [diff] [blame] | 153 | MACH_PORT_NULL, OriginalBehaviors[i], |
| 154 | OriginalFlavors[i]); |
| 155 | } |
| 156 | |
| 157 | // Disable crash reporting on Mac OS X 10.5 |
Nate Begeman | f8be383 | 2008-03-31 22:19:25 +0000 | [diff] [blame] | 158 | signal(SIGABRT, _exit); |
| 159 | signal(SIGILL, _exit); |
| 160 | signal(SIGFPE, _exit); |
| 161 | signal(SIGSEGV, _exit); |
| 162 | signal(SIGBUS, _exit); |
Chris Lattner | 2f107cf | 2006-09-14 06:01:41 +0000 | [diff] [blame] | 163 | #endif |
Leny Kholodov | 1b73e66 | 2016-05-04 16:56:51 +0000 | [diff] [blame] | 164 | |
| 165 | coreFilesPrevented = true; |
Reid Spencer | cf15b87 | 2004-12-27 06:17:27 +0000 | [diff] [blame] | 166 | } |
Reid Spencer | ac38f3a | 2004-12-20 00:59:28 +0000 | [diff] [blame] | 167 | |
Rui Ueyama | 471d0c5 | 2013-09-10 19:45:51 +0000 | [diff] [blame] | 168 | Optional<std::string> Process::GetEnv(StringRef Name) { |
| 169 | std::string NameStr = Name.str(); |
| 170 | const char *Val = ::getenv(NameStr.c_str()); |
| 171 | if (!Val) |
| 172 | return None; |
| 173 | return std::string(Val); |
| 174 | } |
| 175 | |
David Majnemer | 121a174 | 2014-10-06 23:16:18 +0000 | [diff] [blame] | 176 | namespace { |
| 177 | class FDCloser { |
| 178 | public: |
| 179 | FDCloser(int &FD) : FD(FD), KeepOpen(false) {} |
| 180 | void keepOpen() { KeepOpen = true; } |
| 181 | ~FDCloser() { |
| 182 | if (!KeepOpen && FD >= 0) |
| 183 | ::close(FD); |
| 184 | } |
| 185 | |
| 186 | private: |
Aaron Ballman | f9a1897 | 2015-02-15 22:54:22 +0000 | [diff] [blame] | 187 | FDCloser(const FDCloser &) = delete; |
| 188 | void operator=(const FDCloser &) = delete; |
David Majnemer | 121a174 | 2014-10-06 23:16:18 +0000 | [diff] [blame] | 189 | |
| 190 | int &FD; |
| 191 | bool KeepOpen; |
| 192 | }; |
Duncan P. N. Exon Smith | 91d3cfe | 2016-04-05 20:45:04 +0000 | [diff] [blame] | 193 | } |
David Majnemer | 121a174 | 2014-10-06 23:16:18 +0000 | [diff] [blame] | 194 | |
| 195 | std::error_code Process::FixupStandardFileDescriptors() { |
| 196 | int NullFD = -1; |
| 197 | FDCloser FDC(NullFD); |
| 198 | const int StandardFDs[] = {STDIN_FILENO, STDOUT_FILENO, STDERR_FILENO}; |
| 199 | for (int StandardFD : StandardFDs) { |
| 200 | struct stat st; |
| 201 | errno = 0; |
Pavel Labath | b2c73c4 | 2018-06-11 13:30:47 +0000 | [diff] [blame] | 202 | if (RetryAfterSignal(-1, ::fstat, StandardFD, &st) < 0) { |
David Majnemer | 121a174 | 2014-10-06 23:16:18 +0000 | [diff] [blame] | 203 | assert(errno && "expected errno to be set if fstat failed!"); |
| 204 | // fstat should return EBADF if the file descriptor is closed. |
Pavel Labath | fe09f50 | 2017-06-29 13:15:31 +0000 | [diff] [blame] | 205 | if (errno != EBADF) |
David Majnemer | 121a174 | 2014-10-06 23:16:18 +0000 | [diff] [blame] | 206 | return std::error_code(errno, std::generic_category()); |
| 207 | } |
| 208 | // if fstat succeeds, move on to the next FD. |
| 209 | if (!errno) |
| 210 | continue; |
| 211 | assert(errno == EBADF && "expected errno to have EBADF at this point!"); |
| 212 | |
| 213 | if (NullFD < 0) { |
Hans Wennborg | 0a0e411 | 2018-08-27 15:55:39 +0000 | [diff] [blame] | 214 | // Call ::open in a lambda to avoid overload resolution in |
| 215 | // RetryAfterSignal when open is overloaded, such as in Bionic. |
| 216 | auto Open = [&]() { return ::open("/dev/null", O_RDWR); }; |
| 217 | if ((NullFD = RetryAfterSignal(-1, Open)) < 0) |
David Majnemer | 121a174 | 2014-10-06 23:16:18 +0000 | [diff] [blame] | 218 | return std::error_code(errno, std::generic_category()); |
David Majnemer | 121a174 | 2014-10-06 23:16:18 +0000 | [diff] [blame] | 219 | } |
| 220 | |
| 221 | if (NullFD == StandardFD) |
| 222 | FDC.keepOpen(); |
| 223 | else if (dup2(NullFD, StandardFD) < 0) |
| 224 | return std::error_code(errno, std::generic_category()); |
| 225 | } |
| 226 | return std::error_code(); |
| 227 | } |
| 228 | |
David Majnemer | 51c2afc | 2014-10-07 05:48:40 +0000 | [diff] [blame] | 229 | std::error_code Process::SafelyCloseFileDescriptor(int FD) { |
| 230 | // Create a signal set filled with *all* signals. |
| 231 | sigset_t FullSet; |
| 232 | if (sigfillset(&FullSet) < 0) |
| 233 | return std::error_code(errno, std::generic_category()); |
| 234 | // Atomically swap our current signal mask with a full mask. |
| 235 | sigset_t SavedSet; |
David Majnemer | ecc1777 | 2014-10-08 08:48:43 +0000 | [diff] [blame] | 236 | #if LLVM_ENABLE_THREADS |
David Majnemer | 51c2afc | 2014-10-07 05:48:40 +0000 | [diff] [blame] | 237 | if (int EC = pthread_sigmask(SIG_SETMASK, &FullSet, &SavedSet)) |
| 238 | return std::error_code(EC, std::generic_category()); |
David Majnemer | ecc1777 | 2014-10-08 08:48:43 +0000 | [diff] [blame] | 239 | #else |
| 240 | if (sigprocmask(SIG_SETMASK, &FullSet, &SavedSet) < 0) |
| 241 | return std::error_code(errno, std::generic_category()); |
| 242 | #endif |
David Majnemer | 51c2afc | 2014-10-07 05:48:40 +0000 | [diff] [blame] | 243 | // Attempt to close the file descriptor. |
| 244 | // We need to save the error, if one occurs, because our subsequent call to |
| 245 | // pthread_sigmask might tamper with errno. |
| 246 | int ErrnoFromClose = 0; |
| 247 | if (::close(FD) < 0) |
| 248 | ErrnoFromClose = errno; |
| 249 | // Restore the signal mask back to what we saved earlier. |
David Majnemer | ecc1777 | 2014-10-08 08:48:43 +0000 | [diff] [blame] | 250 | int EC = 0; |
| 251 | #if LLVM_ENABLE_THREADS |
| 252 | EC = pthread_sigmask(SIG_SETMASK, &SavedSet, nullptr); |
| 253 | #else |
| 254 | if (sigprocmask(SIG_SETMASK, &SavedSet, nullptr) < 0) |
| 255 | EC = errno; |
| 256 | #endif |
David Majnemer | 51c2afc | 2014-10-07 05:48:40 +0000 | [diff] [blame] | 257 | // The error code from close takes precedence over the one from |
| 258 | // pthread_sigmask. |
| 259 | if (ErrnoFromClose) |
| 260 | return std::error_code(ErrnoFromClose, std::generic_category()); |
| 261 | return std::error_code(EC, std::generic_category()); |
| 262 | } |
| 263 | |
Reid Spencer | 6f802ba | 2005-01-01 22:29:26 +0000 | [diff] [blame] | 264 | bool Process::StandardInIsUserInput() { |
Dan Gohman | e592923 | 2009-09-11 20:46:33 +0000 | [diff] [blame] | 265 | return FileDescriptorIsDisplayed(STDIN_FILENO); |
Reid Spencer | 6f802ba | 2005-01-01 22:29:26 +0000 | [diff] [blame] | 266 | } |
| 267 | |
| 268 | bool Process::StandardOutIsDisplayed() { |
Dan Gohman | e592923 | 2009-09-11 20:46:33 +0000 | [diff] [blame] | 269 | return FileDescriptorIsDisplayed(STDOUT_FILENO); |
Reid Spencer | 6f802ba | 2005-01-01 22:29:26 +0000 | [diff] [blame] | 270 | } |
| 271 | |
| 272 | bool Process::StandardErrIsDisplayed() { |
Dan Gohman | e592923 | 2009-09-11 20:46:33 +0000 | [diff] [blame] | 273 | return FileDescriptorIsDisplayed(STDERR_FILENO); |
| 274 | } |
| 275 | |
| 276 | bool Process::FileDescriptorIsDisplayed(int fd) { |
Reid Spencer | 6f802ba | 2005-01-01 22:29:26 +0000 | [diff] [blame] | 277 | #if HAVE_ISATTY |
Dan Gohman | e592923 | 2009-09-11 20:46:33 +0000 | [diff] [blame] | 278 | return isatty(fd); |
Duncan Sands | 44d423a | 2009-09-06 10:53:22 +0000 | [diff] [blame] | 279 | #else |
Reid Spencer | 6f802ba | 2005-01-01 22:29:26 +0000 | [diff] [blame] | 280 | // If we don't have isatty, just return false. |
| 281 | return false; |
Duncan Sands | 44d423a | 2009-09-06 10:53:22 +0000 | [diff] [blame] | 282 | #endif |
Reid Spencer | 6f802ba | 2005-01-01 22:29:26 +0000 | [diff] [blame] | 283 | } |
Douglas Gregor | 1543661 | 2009-05-11 18:05:52 +0000 | [diff] [blame] | 284 | |
Duncan P. N. Exon Smith | 91d3cfe | 2016-04-05 20:45:04 +0000 | [diff] [blame] | 285 | static unsigned getColumns(int FileID) { |
Douglas Gregor | 1543661 | 2009-05-11 18:05:52 +0000 | [diff] [blame] | 286 | // If COLUMNS is defined in the environment, wrap to that many columns. |
| 287 | if (const char *ColumnsStr = std::getenv("COLUMNS")) { |
| 288 | int Columns = std::atoi(ColumnsStr); |
| 289 | if (Columns > 0) |
| 290 | return Columns; |
| 291 | } |
| 292 | |
| 293 | unsigned Columns = 0; |
| 294 | |
Douglas Gregor | b81294d | 2009-05-18 17:21:34 +0000 | [diff] [blame] | 295 | #if defined(HAVE_SYS_IOCTL_H) && defined(HAVE_TERMIOS_H) |
Douglas Gregor | 1543661 | 2009-05-11 18:05:52 +0000 | [diff] [blame] | 296 | // Try to determine the width of the terminal. |
| 297 | struct winsize ws; |
| 298 | if (ioctl(FileID, TIOCGWINSZ, &ws) == 0) |
| 299 | Columns = ws.ws_col; |
| 300 | #endif |
| 301 | |
| 302 | return Columns; |
| 303 | } |
| 304 | |
| 305 | unsigned Process::StandardOutColumns() { |
| 306 | if (!StandardOutIsDisplayed()) |
| 307 | return 0; |
| 308 | |
| 309 | return getColumns(1); |
| 310 | } |
| 311 | |
| 312 | unsigned Process::StandardErrColumns() { |
| 313 | if (!StandardErrIsDisplayed()) |
| 314 | return 0; |
| 315 | |
| 316 | return getColumns(2); |
| 317 | } |
Torok Edwin | 9b5a47f | 2009-06-04 07:09:50 +0000 | [diff] [blame] | 318 | |
Chandler Carruth | 9121985 | 2013-08-12 10:40:11 +0000 | [diff] [blame] | 319 | #ifdef HAVE_TERMINFO |
Chandler Carruth | 67ff8b7 | 2013-08-18 01:20:32 +0000 | [diff] [blame] | 320 | // We manually declare these extern functions because finding the correct |
Chandler Carruth | 9121985 | 2013-08-12 10:40:11 +0000 | [diff] [blame] | 321 | // headers from various terminfo, curses, or other sources is harder than |
| 322 | // writing their specs down. |
| 323 | extern "C" int setupterm(char *term, int filedes, int *errret); |
Chandler Carruth | 67ff8b7 | 2013-08-18 01:20:32 +0000 | [diff] [blame] | 324 | extern "C" struct term *set_curterm(struct term *termp); |
| 325 | extern "C" int del_curterm(struct term *termp); |
Chandler Carruth | 9121985 | 2013-08-12 10:40:11 +0000 | [diff] [blame] | 326 | extern "C" int tigetnum(char *capname); |
| 327 | #endif |
| 328 | |
Chris Bieneman | 7827217 | 2014-09-24 18:35:58 +0000 | [diff] [blame] | 329 | #ifdef HAVE_TERMINFO |
Duncan P. N. Exon Smith | 91d3cfe | 2016-04-05 20:45:04 +0000 | [diff] [blame] | 330 | static ManagedStatic<sys::Mutex> TermColorMutex; |
Chris Bieneman | 7827217 | 2014-09-24 18:35:58 +0000 | [diff] [blame] | 331 | #endif |
Chris Bieneman | fa35e11 | 2014-09-22 22:39:20 +0000 | [diff] [blame] | 332 | |
Duncan P. N. Exon Smith | 91d3cfe | 2016-04-05 20:45:04 +0000 | [diff] [blame] | 333 | static bool terminalHasColors(int fd) { |
Chandler Carruth | f11f1e4 | 2013-08-12 09:49:17 +0000 | [diff] [blame] | 334 | #ifdef HAVE_TERMINFO |
| 335 | // First, acquire a global lock because these C routines are thread hostile. |
Chris Bieneman | fa35e11 | 2014-09-22 22:39:20 +0000 | [diff] [blame] | 336 | MutexGuard G(*TermColorMutex); |
Chandler Carruth | cad7e5e | 2013-08-07 08:47:36 +0000 | [diff] [blame] | 337 | |
| 338 | int errret = 0; |
Serge Guelton | f4dc59b | 2017-05-11 08:53:00 +0000 | [diff] [blame] | 339 | if (setupterm(nullptr, fd, &errret) != 0) |
Chandler Carruth | cad7e5e | 2013-08-07 08:47:36 +0000 | [diff] [blame] | 340 | // Regardless of why, if we can't get terminfo, we shouldn't try to print |
| 341 | // colors. |
| 342 | return false; |
| 343 | |
Chandler Carruth | f11f1e4 | 2013-08-12 09:49:17 +0000 | [diff] [blame] | 344 | // Test whether the terminal as set up supports color output. How to do this |
| 345 | // isn't entirely obvious. We can use the curses routine 'has_colors' but it |
| 346 | // would be nice to avoid a dependency on curses proper when we can make do |
| 347 | // with a minimal terminfo parsing library. Also, we don't really care whether |
| 348 | // the terminal supports the curses-specific color changing routines, merely |
| 349 | // if it will interpret ANSI color escape codes in a reasonable way. Thus, the |
| 350 | // strategy here is just to query the baseline colors capability and if it |
| 351 | // supports colors at all to assume it will translate the escape codes into |
| 352 | // whatever range of colors it does support. We can add more detailed tests |
| 353 | // here if users report them as necessary. |
| 354 | // |
| 355 | // The 'tigetnum' routine returns -2 or -1 on errors, and might return 0 if |
| 356 | // the terminfo says that no colors are supported. |
Chandler Carruth | 67ff8b7 | 2013-08-18 01:20:32 +0000 | [diff] [blame] | 357 | bool HasColors = tigetnum(const_cast<char *>("colors")) > 0; |
| 358 | |
| 359 | // Now extract the structure allocated by setupterm and free its memory |
| 360 | // through a really silly dance. |
Serge Guelton | f4dc59b | 2017-05-11 08:53:00 +0000 | [diff] [blame] | 361 | struct term *termp = set_curterm(nullptr); |
Chandler Carruth | 67ff8b7 | 2013-08-18 01:20:32 +0000 | [diff] [blame] | 362 | (void)del_curterm(termp); // Drop any errors here. |
| 363 | |
| 364 | // Return true if we found a color capabilities for the current terminal. |
| 365 | if (HasColors) |
Chandler Carruth | cad7e5e | 2013-08-07 08:47:36 +0000 | [diff] [blame] | 366 | return true; |
Petr Hosek | cc7a8f1 | 2018-01-19 17:10:55 +0000 | [diff] [blame] | 367 | #else |
| 368 | // When the terminfo database is not available, check if the current terminal |
| 369 | // is one of terminals that are known to support ANSI color escape codes. |
| 370 | if (const char *TermStr = std::getenv("TERM")) { |
| 371 | return StringSwitch<bool>(TermStr) |
| 372 | .Case("ansi", true) |
| 373 | .Case("cygwin", true) |
| 374 | .Case("linux", true) |
| 375 | .StartsWith("screen", true) |
| 376 | .StartsWith("xterm", true) |
| 377 | .StartsWith("vt100", true) |
| 378 | .StartsWith("rxvt", true) |
| 379 | .EndsWith("color", true) |
| 380 | .Default(false); |
| 381 | } |
Chandler Carruth | cad7e5e | 2013-08-07 08:47:36 +0000 | [diff] [blame] | 382 | #endif |
| 383 | |
| 384 | // Otherwise, be conservative. |
Torok Edwin | 9b5a47f | 2009-06-04 07:09:50 +0000 | [diff] [blame] | 385 | return false; |
| 386 | } |
| 387 | |
Daniel Dunbar | 712de82 | 2012-07-20 18:29:38 +0000 | [diff] [blame] | 388 | bool Process::FileDescriptorHasColors(int fd) { |
| 389 | // A file descriptor has colors if it is displayed and the terminal has |
| 390 | // colors. |
Chandler Carruth | cad7e5e | 2013-08-07 08:47:36 +0000 | [diff] [blame] | 391 | return FileDescriptorIsDisplayed(fd) && terminalHasColors(fd); |
Daniel Dunbar | 712de82 | 2012-07-20 18:29:38 +0000 | [diff] [blame] | 392 | } |
| 393 | |
Torok Edwin | 9b5a47f | 2009-06-04 07:09:50 +0000 | [diff] [blame] | 394 | bool Process::StandardOutHasColors() { |
Daniel Dunbar | 712de82 | 2012-07-20 18:29:38 +0000 | [diff] [blame] | 395 | return FileDescriptorHasColors(STDOUT_FILENO); |
Torok Edwin | 9b5a47f | 2009-06-04 07:09:50 +0000 | [diff] [blame] | 396 | } |
| 397 | |
| 398 | bool Process::StandardErrHasColors() { |
Daniel Dunbar | 712de82 | 2012-07-20 18:29:38 +0000 | [diff] [blame] | 399 | return FileDescriptorHasColors(STDERR_FILENO); |
Torok Edwin | 9b5a47f | 2009-06-04 07:09:50 +0000 | [diff] [blame] | 400 | } |
| 401 | |
Nico Rieck | 92d649a | 2013-09-11 00:36:48 +0000 | [diff] [blame] | 402 | void Process::UseANSIEscapeCodes(bool /*enable*/) { |
| 403 | // No effect. |
| 404 | } |
| 405 | |
Torok Edwin | 9b5a47f | 2009-06-04 07:09:50 +0000 | [diff] [blame] | 406 | bool Process::ColorNeedsFlush() { |
| 407 | // No, we use ANSI escape sequences. |
| 408 | return false; |
| 409 | } |
| 410 | |
Torok Edwin | 9b5a47f | 2009-06-04 07:09:50 +0000 | [diff] [blame] | 411 | const char *Process::OutputColor(char code, bool bold, bool bg) { |
| 412 | return colorcodes[bg?1:0][bold?1:0][code&7]; |
| 413 | } |
| 414 | |
| 415 | const char *Process::OutputBold(bool bg) { |
| 416 | return "\033[1m"; |
| 417 | } |
| 418 | |
Benjamin Kramer | 13d16f3 | 2012-04-16 08:56:50 +0000 | [diff] [blame] | 419 | const char *Process::OutputReverse() { |
| 420 | return "\033[7m"; |
| 421 | } |
| 422 | |
Torok Edwin | 9b5a47f | 2009-06-04 07:09:50 +0000 | [diff] [blame] | 423 | const char *Process::ResetColor() { |
| 424 | return "\033[0m"; |
| 425 | } |
NAKAMURA Takumi | 54acb28 | 2012-05-06 08:24:18 +0000 | [diff] [blame] | 426 | |
Joerg Sonnenberger | 8472f84 | 2016-09-29 21:10:38 +0000 | [diff] [blame] | 427 | #if !HAVE_DECL_ARC4RANDOM |
Duncan P. N. Exon Smith | 91d3cfe | 2016-04-05 20:45:04 +0000 | [diff] [blame] | 428 | static unsigned GetRandomNumberSeed() { |
Daniel Dunbar | 5f1c956 | 2012-05-08 20:38:00 +0000 | [diff] [blame] | 429 | // Attempt to get the initial seed from /dev/urandom, if possible. |
Rafael Espindola | 515f8df | 2015-12-11 22:52:32 +0000 | [diff] [blame] | 430 | int urandomFD = open("/dev/urandom", O_RDONLY); |
| 431 | |
| 432 | if (urandomFD != -1) { |
Daniel Dunbar | 5f1c956 | 2012-05-08 20:38:00 +0000 | [diff] [blame] | 433 | unsigned seed; |
Rafael Espindola | 515f8df | 2015-12-11 22:52:32 +0000 | [diff] [blame] | 434 | // Don't use a buffered read to avoid reading more data |
| 435 | // from /dev/urandom than we need. |
| 436 | int count = read(urandomFD, (void *)&seed, sizeof(seed)); |
| 437 | |
| 438 | close(urandomFD); |
Daniel Dunbar | 5f1c956 | 2012-05-08 20:38:00 +0000 | [diff] [blame] | 439 | |
| 440 | // Return the seed if the read was successful. |
Rafael Espindola | 515f8df | 2015-12-11 22:52:32 +0000 | [diff] [blame] | 441 | if (count == sizeof(seed)) |
Daniel Dunbar | 5f1c956 | 2012-05-08 20:38:00 +0000 | [diff] [blame] | 442 | return seed; |
NAKAMURA Takumi | 7bec741 | 2012-05-06 08:24:24 +0000 | [diff] [blame] | 443 | } |
Daniel Dunbar | 5f1c956 | 2012-05-08 20:38:00 +0000 | [diff] [blame] | 444 | |
| 445 | // Otherwise, swizzle the current time and the process ID to form a reasonable |
| 446 | // seed. |
Pavel Labath | 757ca88 | 2016-10-24 10:59:17 +0000 | [diff] [blame] | 447 | const auto Now = std::chrono::high_resolution_clock::now(); |
| 448 | return hash_combine(Now.time_since_epoch().count(), ::getpid()); |
NAKAMURA Takumi | 7bec741 | 2012-05-06 08:24:24 +0000 | [diff] [blame] | 449 | } |
| 450 | #endif |
| 451 | |
NAKAMURA Takumi | 54acb28 | 2012-05-06 08:24:18 +0000 | [diff] [blame] | 452 | unsigned llvm::sys::Process::GetRandomNumber() { |
Joerg Sonnenberger | 8472f84 | 2016-09-29 21:10:38 +0000 | [diff] [blame] | 453 | #if HAVE_DECL_ARC4RANDOM |
NAKAMURA Takumi | 54acb28 | 2012-05-06 08:24:18 +0000 | [diff] [blame] | 454 | return arc4random(); |
| 455 | #else |
Richard Trieu | 7a08381 | 2016-02-18 22:09:30 +0000 | [diff] [blame] | 456 | static int x = (static_cast<void>(::srand(GetRandomNumberSeed())), 0); |
NAKAMURA Takumi | 7bec741 | 2012-05-06 08:24:24 +0000 | [diff] [blame] | 457 | (void)x; |
NAKAMURA Takumi | 54acb28 | 2012-05-06 08:24:18 +0000 | [diff] [blame] | 458 | return ::rand(); |
| 459 | #endif |
| 460 | } |