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" |
Daniel Dunbar | 5f1c956 | 2012-05-08 20:38:00 +0000 | [diff] [blame] | 20 | #include "llvm/Support/TimeValue.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 |
David Majnemer | 7348322 | 2014-10-07 05:56:45 +0000 | [diff] [blame] | 33 | #if HAVE_SIGNAL_H |
| 34 | #include <signal.h> |
| 35 | #endif |
Eric Christopher | 22738d0 | 2012-08-06 20:52:18 +0000 | [diff] [blame] | 36 | // DragonFlyBSD, OpenBSD, and Bitrig have deprecated <malloc.h> for |
| 37 | // <stdlib.h> instead. Unix.h includes this for us already. |
| 38 | #if defined(HAVE_MALLOC_H) && !defined(__DragonFly__) && \ |
| 39 | !defined(__OpenBSD__) && !defined(__Bitrig__) |
Reid Spencer | ac38f3a | 2004-12-20 00:59:28 +0000 | [diff] [blame] | 40 | #include <malloc.h> |
| 41 | #endif |
Chris Lattner | 698fa76 | 2005-11-14 07:00:29 +0000 | [diff] [blame] | 42 | #ifdef HAVE_MALLOC_MALLOC_H |
| 43 | #include <malloc/malloc.h> |
| 44 | #endif |
Douglas Gregor | 1543661 | 2009-05-11 18:05:52 +0000 | [diff] [blame] | 45 | #ifdef HAVE_SYS_IOCTL_H |
| 46 | # include <sys/ioctl.h> |
| 47 | #endif |
Douglas Gregor | b81294d | 2009-05-18 17:21:34 +0000 | [diff] [blame] | 48 | #ifdef HAVE_TERMIOS_H |
| 49 | # include <termios.h> |
| 50 | #endif |
Reid Spencer | 33b9d77 | 2004-09-11 04:56:56 +0000 | [diff] [blame] | 51 | |
| 52 | //===----------------------------------------------------------------------===// |
| 53 | //=== WARNING: Implementation here must contain only generic UNIX code that |
| 54 | //=== is guaranteed to work on *all* UNIX variants. |
| 55 | //===----------------------------------------------------------------------===// |
| 56 | |
Chris Lattner | 2f107cf | 2006-09-14 06:01:41 +0000 | [diff] [blame] | 57 | using namespace llvm; |
Reid Spencer | 33b9d77 | 2004-09-11 04:56:56 +0000 | [diff] [blame] | 58 | using namespace sys; |
Chandler Carruth | 97683aa | 2012-12-31 11:17:50 +0000 | [diff] [blame] | 59 | |
Chandler Carruth | ef7f968 | 2013-01-04 23:19:55 +0000 | [diff] [blame] | 60 | static std::pair<TimeValue, TimeValue> getRUsageTimes() { |
| 61 | #if defined(HAVE_GETRUSAGE) |
| 62 | struct rusage RU; |
| 63 | ::getrusage(RUSAGE_SELF, &RU); |
| 64 | return std::make_pair( |
| 65 | TimeValue( |
| 66 | static_cast<TimeValue::SecondsType>(RU.ru_utime.tv_sec), |
| 67 | static_cast<TimeValue::NanoSecondsType>( |
| 68 | RU.ru_utime.tv_usec * TimeValue::NANOSECONDS_PER_MICROSECOND)), |
| 69 | TimeValue( |
| 70 | static_cast<TimeValue::SecondsType>(RU.ru_stime.tv_sec), |
| 71 | static_cast<TimeValue::NanoSecondsType>( |
| 72 | RU.ru_stime.tv_usec * TimeValue::NANOSECONDS_PER_MICROSECOND))); |
| 73 | #else |
| 74 | #warning Cannot get usage times on this platform |
| 75 | return std::make_pair(TimeValue(), TimeValue()); |
| 76 | #endif |
| 77 | } |
| 78 | |
NAKAMURA Takumi | 7a04234 | 2013-09-04 14:12:26 +0000 | [diff] [blame] | 79 | // On Cygwin, getpagesize() returns 64k(AllocationGranularity) and |
| 80 | // offset in mmap(3) should be aligned to the AllocationGranularity. |
Rafael Espindola | c0610bf | 2014-12-04 16:59:36 +0000 | [diff] [blame] | 81 | unsigned Process::getPageSize() { |
NAKAMURA Takumi | 3bbbe2e | 2013-08-21 13:47:12 +0000 | [diff] [blame] | 82 | #if defined(HAVE_GETPAGESIZE) |
Rafael Espindola | c0610bf | 2014-12-04 16:59:36 +0000 | [diff] [blame] | 83 | static const int page_size = ::getpagesize(); |
Reid Spencer | ac38f3a | 2004-12-20 00:59:28 +0000 | [diff] [blame] | 84 | #elif defined(HAVE_SYSCONF) |
Rafael Espindola | c0610bf | 2014-12-04 16:59:36 +0000 | [diff] [blame] | 85 | static long page_size = ::sysconf(_SC_PAGE_SIZE); |
Reid Spencer | ac38f3a | 2004-12-20 00:59:28 +0000 | [diff] [blame] | 86 | #else |
| 87 | #warning Cannot get the page size on this machine |
| 88 | #endif |
Reid Spencer | 33b9d77 | 2004-09-11 04:56:56 +0000 | [diff] [blame] | 89 | return static_cast<unsigned>(page_size); |
| 90 | } |
| 91 | |
Chris Lattner | 698fa76 | 2005-11-14 07:00:29 +0000 | [diff] [blame] | 92 | size_t Process::GetMallocUsage() { |
Reid Spencer | 1cf74ce | 2004-12-20 16:06:44 +0000 | [diff] [blame] | 93 | #if defined(HAVE_MALLINFO) |
Reid Spencer | ac38f3a | 2004-12-20 00:59:28 +0000 | [diff] [blame] | 94 | struct mallinfo mi; |
| 95 | mi = ::mallinfo(); |
| 96 | return mi.uordblks; |
Chris Lattner | 16cbc6a | 2005-11-14 07:27:56 +0000 | [diff] [blame] | 97 | #elif defined(HAVE_MALLOC_ZONE_STATISTICS) && defined(HAVE_MALLOC_MALLOC_H) |
| 98 | malloc_statistics_t Stats; |
| 99 | malloc_zone_statistics(malloc_default_zone(), &Stats); |
| 100 | return Stats.size_in_use; // darwin |
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; |
Reid Spencer | ac38f3a | 2004-12-20 00:59:28 +0000 | [diff] [blame] | 108 | else |
| 109 | return 0; |
| 110 | #else |
| 111 | #warning Cannot get malloc info on this platform |
| 112 | return 0; |
| 113 | #endif |
| 114 | } |
| 115 | |
Chandler Carruth | ef7f968 | 2013-01-04 23:19:55 +0000 | [diff] [blame] | 116 | void Process::GetTimeUsage(TimeValue &elapsed, TimeValue &user_time, |
| 117 | TimeValue &sys_time) { |
Reid Spencer | ac38f3a | 2004-12-20 00:59:28 +0000 | [diff] [blame] | 118 | elapsed = TimeValue::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 |
Reid Spencer | cf15b87 | 2004-12-27 06:17:27 +0000 | [diff] [blame] | 164 | } |
Reid Spencer | ac38f3a | 2004-12-20 00:59:28 +0000 | [diff] [blame] | 165 | |
Rui Ueyama | 471d0c5 | 2013-09-10 19:45:51 +0000 | [diff] [blame] | 166 | Optional<std::string> Process::GetEnv(StringRef Name) { |
| 167 | std::string NameStr = Name.str(); |
| 168 | const char *Val = ::getenv(NameStr.c_str()); |
| 169 | if (!Val) |
| 170 | return None; |
| 171 | return std::string(Val); |
| 172 | } |
| 173 | |
Rafael Espindola | db4ed0b | 2014-06-13 02:24:39 +0000 | [diff] [blame] | 174 | std::error_code |
| 175 | Process::GetArgumentVector(SmallVectorImpl<const char *> &ArgsOut, |
| 176 | ArrayRef<const char *> ArgsIn, |
| 177 | SpecificBumpPtrAllocator<char> &) { |
David Majnemer | 61eae2e | 2013-10-07 01:00:07 +0000 | [diff] [blame] | 178 | ArgsOut.append(ArgsIn.begin(), ArgsIn.end()); |
| 179 | |
Rafael Espindola | db4ed0b | 2014-06-13 02:24:39 +0000 | [diff] [blame] | 180 | return std::error_code(); |
David Majnemer | 61eae2e | 2013-10-07 01:00:07 +0000 | [diff] [blame] | 181 | } |
| 182 | |
David Majnemer | 121a174 | 2014-10-06 23:16:18 +0000 | [diff] [blame] | 183 | namespace { |
| 184 | class FDCloser { |
| 185 | public: |
| 186 | FDCloser(int &FD) : FD(FD), KeepOpen(false) {} |
| 187 | void keepOpen() { KeepOpen = true; } |
| 188 | ~FDCloser() { |
| 189 | if (!KeepOpen && FD >= 0) |
| 190 | ::close(FD); |
| 191 | } |
| 192 | |
| 193 | private: |
| 194 | FDCloser(const FDCloser &) LLVM_DELETED_FUNCTION; |
| 195 | void operator=(const FDCloser &) LLVM_DELETED_FUNCTION; |
| 196 | |
| 197 | int &FD; |
| 198 | bool KeepOpen; |
| 199 | }; |
| 200 | } |
| 201 | |
| 202 | std::error_code Process::FixupStandardFileDescriptors() { |
| 203 | int NullFD = -1; |
| 204 | FDCloser FDC(NullFD); |
| 205 | const int StandardFDs[] = {STDIN_FILENO, STDOUT_FILENO, STDERR_FILENO}; |
| 206 | for (int StandardFD : StandardFDs) { |
| 207 | struct stat st; |
| 208 | errno = 0; |
| 209 | while (fstat(StandardFD, &st) < 0) { |
| 210 | assert(errno && "expected errno to be set if fstat failed!"); |
| 211 | // fstat should return EBADF if the file descriptor is closed. |
| 212 | if (errno == EBADF) |
| 213 | break; |
| 214 | // retry fstat if we got EINTR, otherwise bubble up the failure. |
| 215 | if (errno != EINTR) |
| 216 | return std::error_code(errno, std::generic_category()); |
| 217 | } |
| 218 | // if fstat succeeds, move on to the next FD. |
| 219 | if (!errno) |
| 220 | continue; |
| 221 | assert(errno == EBADF && "expected errno to have EBADF at this point!"); |
| 222 | |
| 223 | if (NullFD < 0) { |
| 224 | while ((NullFD = open("/dev/null", O_RDWR)) < 0) { |
| 225 | if (errno == EINTR) |
| 226 | continue; |
| 227 | return std::error_code(errno, std::generic_category()); |
| 228 | } |
| 229 | } |
| 230 | |
| 231 | if (NullFD == StandardFD) |
| 232 | FDC.keepOpen(); |
| 233 | else if (dup2(NullFD, StandardFD) < 0) |
| 234 | return std::error_code(errno, std::generic_category()); |
| 235 | } |
| 236 | return std::error_code(); |
| 237 | } |
| 238 | |
David Majnemer | 51c2afc | 2014-10-07 05:48:40 +0000 | [diff] [blame] | 239 | std::error_code Process::SafelyCloseFileDescriptor(int FD) { |
| 240 | // Create a signal set filled with *all* signals. |
| 241 | sigset_t FullSet; |
| 242 | if (sigfillset(&FullSet) < 0) |
| 243 | return std::error_code(errno, std::generic_category()); |
| 244 | // Atomically swap our current signal mask with a full mask. |
| 245 | sigset_t SavedSet; |
David Majnemer | ecc1777 | 2014-10-08 08:48:43 +0000 | [diff] [blame] | 246 | #if LLVM_ENABLE_THREADS |
David Majnemer | 51c2afc | 2014-10-07 05:48:40 +0000 | [diff] [blame] | 247 | if (int EC = pthread_sigmask(SIG_SETMASK, &FullSet, &SavedSet)) |
| 248 | return std::error_code(EC, std::generic_category()); |
David Majnemer | ecc1777 | 2014-10-08 08:48:43 +0000 | [diff] [blame] | 249 | #else |
| 250 | if (sigprocmask(SIG_SETMASK, &FullSet, &SavedSet) < 0) |
| 251 | return std::error_code(errno, std::generic_category()); |
| 252 | #endif |
David Majnemer | 51c2afc | 2014-10-07 05:48:40 +0000 | [diff] [blame] | 253 | // Attempt to close the file descriptor. |
| 254 | // We need to save the error, if one occurs, because our subsequent call to |
| 255 | // pthread_sigmask might tamper with errno. |
| 256 | int ErrnoFromClose = 0; |
| 257 | if (::close(FD) < 0) |
| 258 | ErrnoFromClose = errno; |
| 259 | // Restore the signal mask back to what we saved earlier. |
David Majnemer | ecc1777 | 2014-10-08 08:48:43 +0000 | [diff] [blame] | 260 | int EC = 0; |
| 261 | #if LLVM_ENABLE_THREADS |
| 262 | EC = pthread_sigmask(SIG_SETMASK, &SavedSet, nullptr); |
| 263 | #else |
| 264 | if (sigprocmask(SIG_SETMASK, &SavedSet, nullptr) < 0) |
| 265 | EC = errno; |
| 266 | #endif |
David Majnemer | 51c2afc | 2014-10-07 05:48:40 +0000 | [diff] [blame] | 267 | // The error code from close takes precedence over the one from |
| 268 | // pthread_sigmask. |
| 269 | if (ErrnoFromClose) |
| 270 | return std::error_code(ErrnoFromClose, std::generic_category()); |
| 271 | return std::error_code(EC, std::generic_category()); |
| 272 | } |
| 273 | |
Reid Spencer | 6f802ba | 2005-01-01 22:29:26 +0000 | [diff] [blame] | 274 | bool Process::StandardInIsUserInput() { |
Dan Gohman | e592923 | 2009-09-11 20:46:33 +0000 | [diff] [blame] | 275 | return FileDescriptorIsDisplayed(STDIN_FILENO); |
Reid Spencer | 6f802ba | 2005-01-01 22:29:26 +0000 | [diff] [blame] | 276 | } |
| 277 | |
| 278 | bool Process::StandardOutIsDisplayed() { |
Dan Gohman | e592923 | 2009-09-11 20:46:33 +0000 | [diff] [blame] | 279 | return FileDescriptorIsDisplayed(STDOUT_FILENO); |
Reid Spencer | 6f802ba | 2005-01-01 22:29:26 +0000 | [diff] [blame] | 280 | } |
| 281 | |
| 282 | bool Process::StandardErrIsDisplayed() { |
Dan Gohman | e592923 | 2009-09-11 20:46:33 +0000 | [diff] [blame] | 283 | return FileDescriptorIsDisplayed(STDERR_FILENO); |
| 284 | } |
| 285 | |
| 286 | bool Process::FileDescriptorIsDisplayed(int fd) { |
Reid Spencer | 6f802ba | 2005-01-01 22:29:26 +0000 | [diff] [blame] | 287 | #if HAVE_ISATTY |
Dan Gohman | e592923 | 2009-09-11 20:46:33 +0000 | [diff] [blame] | 288 | return isatty(fd); |
Duncan Sands | 44d423a | 2009-09-06 10:53:22 +0000 | [diff] [blame] | 289 | #else |
Reid Spencer | 6f802ba | 2005-01-01 22:29:26 +0000 | [diff] [blame] | 290 | // If we don't have isatty, just return false. |
| 291 | return false; |
Duncan Sands | 44d423a | 2009-09-06 10:53:22 +0000 | [diff] [blame] | 292 | #endif |
Reid Spencer | 6f802ba | 2005-01-01 22:29:26 +0000 | [diff] [blame] | 293 | } |
Douglas Gregor | 1543661 | 2009-05-11 18:05:52 +0000 | [diff] [blame] | 294 | |
| 295 | static unsigned getColumns(int FileID) { |
| 296 | // If COLUMNS is defined in the environment, wrap to that many columns. |
| 297 | if (const char *ColumnsStr = std::getenv("COLUMNS")) { |
| 298 | int Columns = std::atoi(ColumnsStr); |
| 299 | if (Columns > 0) |
| 300 | return Columns; |
| 301 | } |
| 302 | |
| 303 | unsigned Columns = 0; |
| 304 | |
Douglas Gregor | b81294d | 2009-05-18 17:21:34 +0000 | [diff] [blame] | 305 | #if defined(HAVE_SYS_IOCTL_H) && defined(HAVE_TERMIOS_H) |
Douglas Gregor | 1543661 | 2009-05-11 18:05:52 +0000 | [diff] [blame] | 306 | // Try to determine the width of the terminal. |
| 307 | struct winsize ws; |
| 308 | if (ioctl(FileID, TIOCGWINSZ, &ws) == 0) |
| 309 | Columns = ws.ws_col; |
| 310 | #endif |
| 311 | |
| 312 | return Columns; |
| 313 | } |
| 314 | |
| 315 | unsigned Process::StandardOutColumns() { |
| 316 | if (!StandardOutIsDisplayed()) |
| 317 | return 0; |
| 318 | |
| 319 | return getColumns(1); |
| 320 | } |
| 321 | |
| 322 | unsigned Process::StandardErrColumns() { |
| 323 | if (!StandardErrIsDisplayed()) |
| 324 | return 0; |
| 325 | |
| 326 | return getColumns(2); |
| 327 | } |
Torok Edwin | 9b5a47f | 2009-06-04 07:09:50 +0000 | [diff] [blame] | 328 | |
Chandler Carruth | 9121985 | 2013-08-12 10:40:11 +0000 | [diff] [blame] | 329 | #ifdef HAVE_TERMINFO |
Chandler Carruth | 67ff8b7 | 2013-08-18 01:20:32 +0000 | [diff] [blame] | 330 | // We manually declare these extern functions because finding the correct |
Chandler Carruth | 9121985 | 2013-08-12 10:40:11 +0000 | [diff] [blame] | 331 | // headers from various terminfo, curses, or other sources is harder than |
| 332 | // writing their specs down. |
| 333 | extern "C" int setupterm(char *term, int filedes, int *errret); |
Chandler Carruth | 67ff8b7 | 2013-08-18 01:20:32 +0000 | [diff] [blame] | 334 | extern "C" struct term *set_curterm(struct term *termp); |
| 335 | extern "C" int del_curterm(struct term *termp); |
Chandler Carruth | 9121985 | 2013-08-12 10:40:11 +0000 | [diff] [blame] | 336 | extern "C" int tigetnum(char *capname); |
| 337 | #endif |
| 338 | |
Chris Bieneman | 7827217 | 2014-09-24 18:35:58 +0000 | [diff] [blame] | 339 | #ifdef HAVE_TERMINFO |
Chris Bieneman | fa35e11 | 2014-09-22 22:39:20 +0000 | [diff] [blame] | 340 | static ManagedStatic<sys::Mutex> TermColorMutex; |
Chris Bieneman | 7827217 | 2014-09-24 18:35:58 +0000 | [diff] [blame] | 341 | #endif |
Chris Bieneman | fa35e11 | 2014-09-22 22:39:20 +0000 | [diff] [blame] | 342 | |
Chandler Carruth | cad7e5e | 2013-08-07 08:47:36 +0000 | [diff] [blame] | 343 | static bool terminalHasColors(int fd) { |
Chandler Carruth | f11f1e4 | 2013-08-12 09:49:17 +0000 | [diff] [blame] | 344 | #ifdef HAVE_TERMINFO |
| 345 | // First, acquire a global lock because these C routines are thread hostile. |
Chris Bieneman | fa35e11 | 2014-09-22 22:39:20 +0000 | [diff] [blame] | 346 | MutexGuard G(*TermColorMutex); |
Chandler Carruth | cad7e5e | 2013-08-07 08:47:36 +0000 | [diff] [blame] | 347 | |
| 348 | int errret = 0; |
Craig Topper | e73658d | 2014-04-28 04:05:08 +0000 | [diff] [blame] | 349 | if (setupterm((char *)nullptr, fd, &errret) != 0) |
Chandler Carruth | cad7e5e | 2013-08-07 08:47:36 +0000 | [diff] [blame] | 350 | // Regardless of why, if we can't get terminfo, we shouldn't try to print |
| 351 | // colors. |
| 352 | return false; |
| 353 | |
Chandler Carruth | f11f1e4 | 2013-08-12 09:49:17 +0000 | [diff] [blame] | 354 | // Test whether the terminal as set up supports color output. How to do this |
| 355 | // isn't entirely obvious. We can use the curses routine 'has_colors' but it |
| 356 | // would be nice to avoid a dependency on curses proper when we can make do |
| 357 | // with a minimal terminfo parsing library. Also, we don't really care whether |
| 358 | // the terminal supports the curses-specific color changing routines, merely |
| 359 | // if it will interpret ANSI color escape codes in a reasonable way. Thus, the |
| 360 | // strategy here is just to query the baseline colors capability and if it |
| 361 | // supports colors at all to assume it will translate the escape codes into |
| 362 | // whatever range of colors it does support. We can add more detailed tests |
| 363 | // here if users report them as necessary. |
| 364 | // |
| 365 | // The 'tigetnum' routine returns -2 or -1 on errors, and might return 0 if |
| 366 | // the terminfo says that no colors are supported. |
Chandler Carruth | 67ff8b7 | 2013-08-18 01:20:32 +0000 | [diff] [blame] | 367 | bool HasColors = tigetnum(const_cast<char *>("colors")) > 0; |
| 368 | |
| 369 | // Now extract the structure allocated by setupterm and free its memory |
| 370 | // through a really silly dance. |
Craig Topper | e73658d | 2014-04-28 04:05:08 +0000 | [diff] [blame] | 371 | struct term *termp = set_curterm((struct term *)nullptr); |
Chandler Carruth | 67ff8b7 | 2013-08-18 01:20:32 +0000 | [diff] [blame] | 372 | (void)del_curterm(termp); // Drop any errors here. |
| 373 | |
| 374 | // Return true if we found a color capabilities for the current terminal. |
| 375 | if (HasColors) |
Chandler Carruth | cad7e5e | 2013-08-07 08:47:36 +0000 | [diff] [blame] | 376 | return true; |
| 377 | #endif |
| 378 | |
| 379 | // Otherwise, be conservative. |
Torok Edwin | 9b5a47f | 2009-06-04 07:09:50 +0000 | [diff] [blame] | 380 | return false; |
| 381 | } |
| 382 | |
Daniel Dunbar | 712de82 | 2012-07-20 18:29:38 +0000 | [diff] [blame] | 383 | bool Process::FileDescriptorHasColors(int fd) { |
| 384 | // A file descriptor has colors if it is displayed and the terminal has |
| 385 | // colors. |
Chandler Carruth | cad7e5e | 2013-08-07 08:47:36 +0000 | [diff] [blame] | 386 | return FileDescriptorIsDisplayed(fd) && terminalHasColors(fd); |
Daniel Dunbar | 712de82 | 2012-07-20 18:29:38 +0000 | [diff] [blame] | 387 | } |
| 388 | |
Torok Edwin | 9b5a47f | 2009-06-04 07:09:50 +0000 | [diff] [blame] | 389 | bool Process::StandardOutHasColors() { |
Daniel Dunbar | 712de82 | 2012-07-20 18:29:38 +0000 | [diff] [blame] | 390 | return FileDescriptorHasColors(STDOUT_FILENO); |
Torok Edwin | 9b5a47f | 2009-06-04 07:09:50 +0000 | [diff] [blame] | 391 | } |
| 392 | |
| 393 | bool Process::StandardErrHasColors() { |
Daniel Dunbar | 712de82 | 2012-07-20 18:29:38 +0000 | [diff] [blame] | 394 | return FileDescriptorHasColors(STDERR_FILENO); |
Torok Edwin | 9b5a47f | 2009-06-04 07:09:50 +0000 | [diff] [blame] | 395 | } |
| 396 | |
Nico Rieck | 92d649a | 2013-09-11 00:36:48 +0000 | [diff] [blame] | 397 | void Process::UseANSIEscapeCodes(bool /*enable*/) { |
| 398 | // No effect. |
| 399 | } |
| 400 | |
Torok Edwin | 9b5a47f | 2009-06-04 07:09:50 +0000 | [diff] [blame] | 401 | bool Process::ColorNeedsFlush() { |
| 402 | // No, we use ANSI escape sequences. |
| 403 | return false; |
| 404 | } |
| 405 | |
Torok Edwin | 9b5a47f | 2009-06-04 07:09:50 +0000 | [diff] [blame] | 406 | const char *Process::OutputColor(char code, bool bold, bool bg) { |
| 407 | return colorcodes[bg?1:0][bold?1:0][code&7]; |
| 408 | } |
| 409 | |
| 410 | const char *Process::OutputBold(bool bg) { |
| 411 | return "\033[1m"; |
| 412 | } |
| 413 | |
Benjamin Kramer | 13d16f3 | 2012-04-16 08:56:50 +0000 | [diff] [blame] | 414 | const char *Process::OutputReverse() { |
| 415 | return "\033[7m"; |
| 416 | } |
| 417 | |
Torok Edwin | 9b5a47f | 2009-06-04 07:09:50 +0000 | [diff] [blame] | 418 | const char *Process::ResetColor() { |
| 419 | return "\033[0m"; |
| 420 | } |
NAKAMURA Takumi | 54acb28 | 2012-05-06 08:24:18 +0000 | [diff] [blame] | 421 | |
Todd Fiala | 4ccfe39 | 2014-02-05 05:04:36 +0000 | [diff] [blame] | 422 | #if !defined(HAVE_DECL_ARC4RANDOM) || !HAVE_DECL_ARC4RANDOM |
NAKAMURA Takumi | 7bec741 | 2012-05-06 08:24:24 +0000 | [diff] [blame] | 423 | static unsigned GetRandomNumberSeed() { |
Daniel Dunbar | 5f1c956 | 2012-05-08 20:38:00 +0000 | [diff] [blame] | 424 | // Attempt to get the initial seed from /dev/urandom, if possible. |
| 425 | if (FILE *RandomSource = ::fopen("/dev/urandom", "r")) { |
| 426 | unsigned seed; |
| 427 | int count = ::fread((void *)&seed, sizeof(seed), 1, RandomSource); |
NAKAMURA Takumi | 7bec741 | 2012-05-06 08:24:24 +0000 | [diff] [blame] | 428 | ::fclose(RandomSource); |
Daniel Dunbar | 5f1c956 | 2012-05-08 20:38:00 +0000 | [diff] [blame] | 429 | |
| 430 | // Return the seed if the read was successful. |
| 431 | if (count == 1) |
| 432 | return seed; |
NAKAMURA Takumi | 7bec741 | 2012-05-06 08:24:24 +0000 | [diff] [blame] | 433 | } |
Daniel Dunbar | 5f1c956 | 2012-05-08 20:38:00 +0000 | [diff] [blame] | 434 | |
| 435 | // Otherwise, swizzle the current time and the process ID to form a reasonable |
| 436 | // seed. |
Chandler Carruth | 5473dfb | 2012-12-31 11:45:20 +0000 | [diff] [blame] | 437 | TimeValue Now = TimeValue::now(); |
Daniel Dunbar | 5f1c956 | 2012-05-08 20:38:00 +0000 | [diff] [blame] | 438 | return hash_combine(Now.seconds(), Now.nanoseconds(), ::getpid()); |
NAKAMURA Takumi | 7bec741 | 2012-05-06 08:24:24 +0000 | [diff] [blame] | 439 | } |
| 440 | #endif |
| 441 | |
NAKAMURA Takumi | 54acb28 | 2012-05-06 08:24:18 +0000 | [diff] [blame] | 442 | unsigned llvm::sys::Process::GetRandomNumber() { |
Todd Fiala | 4ccfe39 | 2014-02-05 05:04:36 +0000 | [diff] [blame] | 443 | #if defined(HAVE_DECL_ARC4RANDOM) && HAVE_DECL_ARC4RANDOM |
NAKAMURA Takumi | 54acb28 | 2012-05-06 08:24:18 +0000 | [diff] [blame] | 444 | return arc4random(); |
| 445 | #else |
NAKAMURA Takumi | 7bec741 | 2012-05-06 08:24:24 +0000 | [diff] [blame] | 446 | static int x = (::srand(GetRandomNumberSeed()), 0); |
| 447 | (void)x; |
NAKAMURA Takumi | 54acb28 | 2012-05-06 08:24:18 +0000 | [diff] [blame] | 448 | return ::rand(); |
| 449 | #endif |
| 450 | } |