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