blob: 350b145c28caf01f4a9f72aeed5848f27c877216 [file] [log] [blame]
Reid Spencer33b9d772004-09-11 04:56:56 +00001//===- Unix/Process.cpp - Unix Process Implementation --------- -*- C++ -*-===//
Michael J. Spencer447762d2010-11-29 18:16:10 +00002//
Reid Spencer33b9d772004-09-11 04:56:56 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattnerf3ebc3f2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Michael J. Spencer447762d2010-11-29 18:16:10 +00007//
Reid Spencer33b9d772004-09-11 04:56:56 +00008//===----------------------------------------------------------------------===//
9//
10// This file provides the generic Unix implementation of the Process class.
11//
12//===----------------------------------------------------------------------===//
13
Reid Spencerac38f3a2004-12-20 00:59:28 +000014#include "Unix.h"
Daniel Dunbar5f1c9562012-05-08 20:38:00 +000015#include "llvm/ADT/Hashing.h"
Rui Ueyama471d0c52013-09-10 19:45:51 +000016#include "llvm/ADT/StringRef.h"
Chris Bienemanfa35e112014-09-22 22:39:20 +000017#include "llvm/Support/ManagedStatic.h"
Chandler Carruthcad7e5e2013-08-07 08:47:36 +000018#include "llvm/Support/Mutex.h"
19#include "llvm/Support/MutexGuard.h"
Daniel Dunbar5f1c9562012-05-08 20:38:00 +000020#include "llvm/Support/TimeValue.h"
David Majnemer121a1742014-10-06 23:16:18 +000021#if HAVE_FCNTL_H
22#include <fcntl.h>
23#endif
Reid Spencerac38f3a2004-12-20 00:59:28 +000024#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 Kramer24165212014-10-12 22:49:26 +000030#ifdef HAVE_SYS_STAT_H
31#include <sys/stat.h>
32#endif
Eugene Zelenko1760dc22016-04-05 20:19:49 +000033#include <csignal>
Eric Christopher22738d02012-08-06 20:52:18 +000034// DragonFlyBSD, OpenBSD, and Bitrig have deprecated <malloc.h> for
35// <stdlib.h> instead. Unix.h includes this for us already.
36#if defined(HAVE_MALLOC_H) && !defined(__DragonFly__) && \
37 !defined(__OpenBSD__) && !defined(__Bitrig__)
Reid Spencerac38f3a2004-12-20 00:59:28 +000038#include <malloc.h>
39#endif
Davide Italianofaafae32015-02-19 07:27:14 +000040#if defined(HAVE_MALLCTL)
41#include <malloc_np.h>
42#endif
Chris Lattner698fa762005-11-14 07:00:29 +000043#ifdef HAVE_MALLOC_MALLOC_H
44#include <malloc/malloc.h>
45#endif
Douglas Gregor15436612009-05-11 18:05:52 +000046#ifdef HAVE_SYS_IOCTL_H
47# include <sys/ioctl.h>
48#endif
Douglas Gregorb81294d2009-05-18 17:21:34 +000049#ifdef HAVE_TERMIOS_H
50# include <termios.h>
51#endif
Reid Spencer33b9d772004-09-11 04:56:56 +000052
53//===----------------------------------------------------------------------===//
54//=== WARNING: Implementation here must contain only generic UNIX code that
55//=== is guaranteed to work on *all* UNIX variants.
56//===----------------------------------------------------------------------===//
57
Chris Lattner2f107cf2006-09-14 06:01:41 +000058using namespace llvm;
Reid Spencer33b9d772004-09-11 04:56:56 +000059using namespace sys;
Chandler Carruth97683aa2012-12-31 11:17:50 +000060
Eugene Zelenko1760dc22016-04-05 20:19:49 +000061namespace {
62
63std::pair<TimeValue, TimeValue> getRUsageTimes() {
Chandler Carruthef7f9682013-01-04 23:19:55 +000064#if defined(HAVE_GETRUSAGE)
65 struct rusage RU;
66 ::getrusage(RUSAGE_SELF, &RU);
67 return std::make_pair(
68 TimeValue(
69 static_cast<TimeValue::SecondsType>(RU.ru_utime.tv_sec),
70 static_cast<TimeValue::NanoSecondsType>(
71 RU.ru_utime.tv_usec * TimeValue::NANOSECONDS_PER_MICROSECOND)),
72 TimeValue(
73 static_cast<TimeValue::SecondsType>(RU.ru_stime.tv_sec),
74 static_cast<TimeValue::NanoSecondsType>(
75 RU.ru_stime.tv_usec * TimeValue::NANOSECONDS_PER_MICROSECOND)));
76#else
77#warning Cannot get usage times on this platform
78 return std::make_pair(TimeValue(), TimeValue());
79#endif
80}
81
Eugene Zelenko1760dc22016-04-05 20:19:49 +000082} // end anonymous namespace
83
NAKAMURA Takumi7a042342013-09-04 14:12:26 +000084// On Cygwin, getpagesize() returns 64k(AllocationGranularity) and
85// offset in mmap(3) should be aligned to the AllocationGranularity.
Rafael Espindolac0610bf2014-12-04 16:59:36 +000086unsigned Process::getPageSize() {
NAKAMURA Takumi3bbbe2e2013-08-21 13:47:12 +000087#if defined(HAVE_GETPAGESIZE)
Rafael Espindolac0610bf2014-12-04 16:59:36 +000088 static const int page_size = ::getpagesize();
Reid Spencerac38f3a2004-12-20 00:59:28 +000089#elif defined(HAVE_SYSCONF)
Rafael Espindolac0610bf2014-12-04 16:59:36 +000090 static long page_size = ::sysconf(_SC_PAGE_SIZE);
Reid Spencerac38f3a2004-12-20 00:59:28 +000091#else
92#warning Cannot get the page size on this machine
93#endif
Reid Spencer33b9d772004-09-11 04:56:56 +000094 return static_cast<unsigned>(page_size);
95}
96
Chris Lattner698fa762005-11-14 07:00:29 +000097size_t Process::GetMallocUsage() {
Reid Spencer1cf74ce2004-12-20 16:06:44 +000098#if defined(HAVE_MALLINFO)
Reid Spencerac38f3a2004-12-20 00:59:28 +000099 struct mallinfo mi;
100 mi = ::mallinfo();
101 return mi.uordblks;
Chris Lattner16cbc6a2005-11-14 07:27:56 +0000102#elif defined(HAVE_MALLOC_ZONE_STATISTICS) && defined(HAVE_MALLOC_MALLOC_H)
103 malloc_statistics_t Stats;
104 malloc_zone_statistics(malloc_default_zone(), &Stats);
105 return Stats.size_in_use; // darwin
Davide Italianofaafae32015-02-19 07:27:14 +0000106#elif defined(HAVE_MALLCTL)
107 size_t alloc, sz;
108 sz = sizeof(size_t);
109 if (mallctl("stats.allocated", &alloc, &sz, NULL, 0) == 0)
110 return alloc;
Davide Italiano8d981962015-02-21 02:36:54 +0000111 return 0;
Reid Spencer1cf74ce2004-12-20 16:06:44 +0000112#elif defined(HAVE_SBRK)
Reid Spencerac38f3a2004-12-20 00:59:28 +0000113 // Note this is only an approximation and more closely resembles
114 // the value returned by mallinfo in the arena field.
Chris Lattner698fa762005-11-14 07:00:29 +0000115 static char *StartOfMemory = reinterpret_cast<char*>(::sbrk(0));
116 char *EndOfMemory = (char*)sbrk(0);
117 if (EndOfMemory != ((char*)-1) && StartOfMemory != ((char*)-1))
118 return EndOfMemory - StartOfMemory;
Davide Italiano8d981962015-02-21 02:36:54 +0000119 return 0;
Reid Spencerac38f3a2004-12-20 00:59:28 +0000120#else
121#warning Cannot get malloc info on this platform
122 return 0;
123#endif
124}
125
Chandler Carruthef7f9682013-01-04 23:19:55 +0000126void Process::GetTimeUsage(TimeValue &elapsed, TimeValue &user_time,
127 TimeValue &sys_time) {
Reid Spencerac38f3a2004-12-20 00:59:28 +0000128 elapsed = TimeValue::now();
Benjamin Kramerd6f1f842014-03-02 13:30:33 +0000129 std::tie(user_time, sys_time) = getRUsageTimes();
Reid Spencerac38f3a2004-12-20 00:59:28 +0000130}
131
Sylvestre Ledru14ada942012-04-11 15:35:36 +0000132#if defined(HAVE_MACH_MACH_H) && !defined(__GNU__)
Nate Begeman48405152008-04-12 00:47:46 +0000133#include <mach/mach.h>
134#endif
135
Reid Spencercf15b872004-12-27 06:17:27 +0000136// Some LLVM programs such as bugpoint produce core files as a normal part of
137// their operation. To prevent the disk from filling up, this function
138// does what's necessary to prevent their generation.
139void Process::PreventCoreFiles() {
140#if HAVE_SETRLIMIT
141 struct rlimit rlim;
142 rlim.rlim_cur = rlim.rlim_max = 0;
Chris Lattnerf64397b2006-05-14 18:53:09 +0000143 setrlimit(RLIMIT_CORE, &rlim);
Reid Spencercf15b872004-12-27 06:17:27 +0000144#endif
Chris Lattner2f107cf2006-09-14 06:01:41 +0000145
Sylvestre Ledru14ada942012-04-11 15:35:36 +0000146#if defined(HAVE_MACH_MACH_H) && !defined(__GNU__)
Nate Begeman48405152008-04-12 00:47:46 +0000147 // Disable crash reporting on Mac OS X 10.0-10.4
148
149 // get information about the original set of exception ports for the task
150 mach_msg_type_number_t Count = 0;
151 exception_mask_t OriginalMasks[EXC_TYPES_COUNT];
152 exception_port_t OriginalPorts[EXC_TYPES_COUNT];
153 exception_behavior_t OriginalBehaviors[EXC_TYPES_COUNT];
154 thread_state_flavor_t OriginalFlavors[EXC_TYPES_COUNT];
Michael J. Spencer447762d2010-11-29 18:16:10 +0000155 kern_return_t err =
Nate Begeman48405152008-04-12 00:47:46 +0000156 task_get_exception_ports(mach_task_self(), EXC_MASK_ALL, OriginalMasks,
157 &Count, OriginalPorts, OriginalBehaviors,
158 OriginalFlavors);
159 if (err == KERN_SUCCESS) {
160 // replace each with MACH_PORT_NULL.
161 for (unsigned i = 0; i != Count; ++i)
Michael J. Spencer447762d2010-11-29 18:16:10 +0000162 task_set_exception_ports(mach_task_self(), OriginalMasks[i],
Nate Begeman48405152008-04-12 00:47:46 +0000163 MACH_PORT_NULL, OriginalBehaviors[i],
164 OriginalFlavors[i]);
165 }
166
167 // Disable crash reporting on Mac OS X 10.5
Nate Begemanf8be3832008-03-31 22:19:25 +0000168 signal(SIGABRT, _exit);
169 signal(SIGILL, _exit);
170 signal(SIGFPE, _exit);
171 signal(SIGSEGV, _exit);
172 signal(SIGBUS, _exit);
Chris Lattner2f107cf2006-09-14 06:01:41 +0000173#endif
Reid Spencercf15b872004-12-27 06:17:27 +0000174}
Reid Spencerac38f3a2004-12-20 00:59:28 +0000175
Rui Ueyama471d0c52013-09-10 19:45:51 +0000176Optional<std::string> Process::GetEnv(StringRef Name) {
177 std::string NameStr = Name.str();
178 const char *Val = ::getenv(NameStr.c_str());
179 if (!Val)
180 return None;
181 return std::string(Val);
182}
183
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000184std::error_code
185Process::GetArgumentVector(SmallVectorImpl<const char *> &ArgsOut,
186 ArrayRef<const char *> ArgsIn,
187 SpecificBumpPtrAllocator<char> &) {
David Majnemer61eae2e2013-10-07 01:00:07 +0000188 ArgsOut.append(ArgsIn.begin(), ArgsIn.end());
189
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000190 return std::error_code();
David Majnemer61eae2e2013-10-07 01:00:07 +0000191}
192
David Majnemer121a1742014-10-06 23:16:18 +0000193namespace {
Eugene Zelenko1760dc22016-04-05 20:19:49 +0000194
David Majnemer121a1742014-10-06 23:16:18 +0000195class FDCloser {
196public:
197 FDCloser(int &FD) : FD(FD), KeepOpen(false) {}
198 void keepOpen() { KeepOpen = true; }
199 ~FDCloser() {
200 if (!KeepOpen && FD >= 0)
201 ::close(FD);
202 }
203
204private:
Aaron Ballmanf9a18972015-02-15 22:54:22 +0000205 FDCloser(const FDCloser &) = delete;
206 void operator=(const FDCloser &) = delete;
David Majnemer121a1742014-10-06 23:16:18 +0000207
208 int &FD;
209 bool KeepOpen;
210};
Eugene Zelenko1760dc22016-04-05 20:19:49 +0000211
212} // end anonymous namespace
David Majnemer121a1742014-10-06 23:16:18 +0000213
214std::error_code Process::FixupStandardFileDescriptors() {
215 int NullFD = -1;
216 FDCloser FDC(NullFD);
217 const int StandardFDs[] = {STDIN_FILENO, STDOUT_FILENO, STDERR_FILENO};
218 for (int StandardFD : StandardFDs) {
219 struct stat st;
220 errno = 0;
221 while (fstat(StandardFD, &st) < 0) {
222 assert(errno && "expected errno to be set if fstat failed!");
223 // fstat should return EBADF if the file descriptor is closed.
224 if (errno == EBADF)
225 break;
226 // retry fstat if we got EINTR, otherwise bubble up the failure.
227 if (errno != EINTR)
228 return std::error_code(errno, std::generic_category());
229 }
230 // if fstat succeeds, move on to the next FD.
231 if (!errno)
232 continue;
233 assert(errno == EBADF && "expected errno to have EBADF at this point!");
234
235 if (NullFD < 0) {
236 while ((NullFD = open("/dev/null", O_RDWR)) < 0) {
237 if (errno == EINTR)
238 continue;
239 return std::error_code(errno, std::generic_category());
240 }
241 }
242
243 if (NullFD == StandardFD)
244 FDC.keepOpen();
245 else if (dup2(NullFD, StandardFD) < 0)
246 return std::error_code(errno, std::generic_category());
247 }
248 return std::error_code();
249}
250
David Majnemer51c2afc2014-10-07 05:48:40 +0000251std::error_code Process::SafelyCloseFileDescriptor(int FD) {
252 // Create a signal set filled with *all* signals.
253 sigset_t FullSet;
254 if (sigfillset(&FullSet) < 0)
255 return std::error_code(errno, std::generic_category());
256 // Atomically swap our current signal mask with a full mask.
257 sigset_t SavedSet;
David Majnemerecc17772014-10-08 08:48:43 +0000258#if LLVM_ENABLE_THREADS
David Majnemer51c2afc2014-10-07 05:48:40 +0000259 if (int EC = pthread_sigmask(SIG_SETMASK, &FullSet, &SavedSet))
260 return std::error_code(EC, std::generic_category());
David Majnemerecc17772014-10-08 08:48:43 +0000261#else
262 if (sigprocmask(SIG_SETMASK, &FullSet, &SavedSet) < 0)
263 return std::error_code(errno, std::generic_category());
264#endif
David Majnemer51c2afc2014-10-07 05:48:40 +0000265 // Attempt to close the file descriptor.
266 // We need to save the error, if one occurs, because our subsequent call to
267 // pthread_sigmask might tamper with errno.
268 int ErrnoFromClose = 0;
269 if (::close(FD) < 0)
270 ErrnoFromClose = errno;
271 // Restore the signal mask back to what we saved earlier.
David Majnemerecc17772014-10-08 08:48:43 +0000272 int EC = 0;
273#if LLVM_ENABLE_THREADS
274 EC = pthread_sigmask(SIG_SETMASK, &SavedSet, nullptr);
275#else
276 if (sigprocmask(SIG_SETMASK, &SavedSet, nullptr) < 0)
277 EC = errno;
278#endif
David Majnemer51c2afc2014-10-07 05:48:40 +0000279 // The error code from close takes precedence over the one from
280 // pthread_sigmask.
281 if (ErrnoFromClose)
282 return std::error_code(ErrnoFromClose, std::generic_category());
283 return std::error_code(EC, std::generic_category());
284}
285
Reid Spencer6f802ba2005-01-01 22:29:26 +0000286bool Process::StandardInIsUserInput() {
Dan Gohmane5929232009-09-11 20:46:33 +0000287 return FileDescriptorIsDisplayed(STDIN_FILENO);
Reid Spencer6f802ba2005-01-01 22:29:26 +0000288}
289
290bool Process::StandardOutIsDisplayed() {
Dan Gohmane5929232009-09-11 20:46:33 +0000291 return FileDescriptorIsDisplayed(STDOUT_FILENO);
Reid Spencer6f802ba2005-01-01 22:29:26 +0000292}
293
294bool Process::StandardErrIsDisplayed() {
Dan Gohmane5929232009-09-11 20:46:33 +0000295 return FileDescriptorIsDisplayed(STDERR_FILENO);
296}
297
298bool Process::FileDescriptorIsDisplayed(int fd) {
Reid Spencer6f802ba2005-01-01 22:29:26 +0000299#if HAVE_ISATTY
Dan Gohmane5929232009-09-11 20:46:33 +0000300 return isatty(fd);
Duncan Sands44d423a2009-09-06 10:53:22 +0000301#else
Reid Spencer6f802ba2005-01-01 22:29:26 +0000302 // If we don't have isatty, just return false.
303 return false;
Duncan Sands44d423a2009-09-06 10:53:22 +0000304#endif
Reid Spencer6f802ba2005-01-01 22:29:26 +0000305}
Douglas Gregor15436612009-05-11 18:05:52 +0000306
Eugene Zelenko1760dc22016-04-05 20:19:49 +0000307namespace {
308
309unsigned getColumns(int FileID) {
Douglas Gregor15436612009-05-11 18:05:52 +0000310 // If COLUMNS is defined in the environment, wrap to that many columns.
311 if (const char *ColumnsStr = std::getenv("COLUMNS")) {
312 int Columns = std::atoi(ColumnsStr);
313 if (Columns > 0)
314 return Columns;
315 }
316
317 unsigned Columns = 0;
318
Douglas Gregorb81294d2009-05-18 17:21:34 +0000319#if defined(HAVE_SYS_IOCTL_H) && defined(HAVE_TERMIOS_H)
Douglas Gregor15436612009-05-11 18:05:52 +0000320 // Try to determine the width of the terminal.
321 struct winsize ws;
322 if (ioctl(FileID, TIOCGWINSZ, &ws) == 0)
323 Columns = ws.ws_col;
324#endif
325
326 return Columns;
327}
328
Eugene Zelenko1760dc22016-04-05 20:19:49 +0000329} // end anonymous namespace
330
Douglas Gregor15436612009-05-11 18:05:52 +0000331unsigned Process::StandardOutColumns() {
332 if (!StandardOutIsDisplayed())
333 return 0;
334
335 return getColumns(1);
336}
337
338unsigned Process::StandardErrColumns() {
339 if (!StandardErrIsDisplayed())
340 return 0;
341
342 return getColumns(2);
343}
Torok Edwin9b5a47f2009-06-04 07:09:50 +0000344
Chandler Carruth91219852013-08-12 10:40:11 +0000345#ifdef HAVE_TERMINFO
Chandler Carruth67ff8b72013-08-18 01:20:32 +0000346// We manually declare these extern functions because finding the correct
Chandler Carruth91219852013-08-12 10:40:11 +0000347// headers from various terminfo, curses, or other sources is harder than
348// writing their specs down.
349extern "C" int setupterm(char *term, int filedes, int *errret);
Chandler Carruth67ff8b72013-08-18 01:20:32 +0000350extern "C" struct term *set_curterm(struct term *termp);
351extern "C" int del_curterm(struct term *termp);
Chandler Carruth91219852013-08-12 10:40:11 +0000352extern "C" int tigetnum(char *capname);
353#endif
354
Eugene Zelenko1760dc22016-04-05 20:19:49 +0000355namespace {
356
Chris Bieneman78272172014-09-24 18:35:58 +0000357#ifdef HAVE_TERMINFO
Eugene Zelenko1760dc22016-04-05 20:19:49 +0000358ManagedStatic<sys::Mutex> TermColorMutex;
Chris Bieneman78272172014-09-24 18:35:58 +0000359#endif
Chris Bienemanfa35e112014-09-22 22:39:20 +0000360
Eugene Zelenko1760dc22016-04-05 20:19:49 +0000361bool terminalHasColors(int fd) {
Chandler Carruthf11f1e42013-08-12 09:49:17 +0000362#ifdef HAVE_TERMINFO
363 // First, acquire a global lock because these C routines are thread hostile.
Chris Bienemanfa35e112014-09-22 22:39:20 +0000364 MutexGuard G(*TermColorMutex);
Chandler Carruthcad7e5e2013-08-07 08:47:36 +0000365
366 int errret = 0;
Craig Toppere73658d2014-04-28 04:05:08 +0000367 if (setupterm((char *)nullptr, fd, &errret) != 0)
Chandler Carruthcad7e5e2013-08-07 08:47:36 +0000368 // Regardless of why, if we can't get terminfo, we shouldn't try to print
369 // colors.
370 return false;
371
Chandler Carruthf11f1e42013-08-12 09:49:17 +0000372 // Test whether the terminal as set up supports color output. How to do this
373 // isn't entirely obvious. We can use the curses routine 'has_colors' but it
374 // would be nice to avoid a dependency on curses proper when we can make do
375 // with a minimal terminfo parsing library. Also, we don't really care whether
376 // the terminal supports the curses-specific color changing routines, merely
377 // if it will interpret ANSI color escape codes in a reasonable way. Thus, the
378 // strategy here is just to query the baseline colors capability and if it
379 // supports colors at all to assume it will translate the escape codes into
380 // whatever range of colors it does support. We can add more detailed tests
381 // here if users report them as necessary.
382 //
383 // The 'tigetnum' routine returns -2 or -1 on errors, and might return 0 if
384 // the terminfo says that no colors are supported.
Chandler Carruth67ff8b72013-08-18 01:20:32 +0000385 bool HasColors = tigetnum(const_cast<char *>("colors")) > 0;
386
387 // Now extract the structure allocated by setupterm and free its memory
388 // through a really silly dance.
Craig Toppere73658d2014-04-28 04:05:08 +0000389 struct term *termp = set_curterm((struct term *)nullptr);
Chandler Carruth67ff8b72013-08-18 01:20:32 +0000390 (void)del_curterm(termp); // Drop any errors here.
391
392 // Return true if we found a color capabilities for the current terminal.
393 if (HasColors)
Chandler Carruthcad7e5e2013-08-07 08:47:36 +0000394 return true;
395#endif
396
397 // Otherwise, be conservative.
Torok Edwin9b5a47f2009-06-04 07:09:50 +0000398 return false;
399}
400
Eugene Zelenko1760dc22016-04-05 20:19:49 +0000401} // end anonymous namespace
402
Daniel Dunbar712de822012-07-20 18:29:38 +0000403bool Process::FileDescriptorHasColors(int fd) {
404 // A file descriptor has colors if it is displayed and the terminal has
405 // colors.
Chandler Carruthcad7e5e2013-08-07 08:47:36 +0000406 return FileDescriptorIsDisplayed(fd) && terminalHasColors(fd);
Daniel Dunbar712de822012-07-20 18:29:38 +0000407}
408
Torok Edwin9b5a47f2009-06-04 07:09:50 +0000409bool Process::StandardOutHasColors() {
Daniel Dunbar712de822012-07-20 18:29:38 +0000410 return FileDescriptorHasColors(STDOUT_FILENO);
Torok Edwin9b5a47f2009-06-04 07:09:50 +0000411}
412
413bool Process::StandardErrHasColors() {
Daniel Dunbar712de822012-07-20 18:29:38 +0000414 return FileDescriptorHasColors(STDERR_FILENO);
Torok Edwin9b5a47f2009-06-04 07:09:50 +0000415}
416
Nico Rieck92d649a2013-09-11 00:36:48 +0000417void Process::UseANSIEscapeCodes(bool /*enable*/) {
418 // No effect.
419}
420
Torok Edwin9b5a47f2009-06-04 07:09:50 +0000421bool Process::ColorNeedsFlush() {
422 // No, we use ANSI escape sequences.
423 return false;
424}
425
Torok Edwin9b5a47f2009-06-04 07:09:50 +0000426const char *Process::OutputColor(char code, bool bold, bool bg) {
427 return colorcodes[bg?1:0][bold?1:0][code&7];
428}
429
430const char *Process::OutputBold(bool bg) {
431 return "\033[1m";
432}
433
Benjamin Kramer13d16f32012-04-16 08:56:50 +0000434const char *Process::OutputReverse() {
435 return "\033[7m";
436}
437
Torok Edwin9b5a47f2009-06-04 07:09:50 +0000438const char *Process::ResetColor() {
439 return "\033[0m";
440}
NAKAMURA Takumi54acb282012-05-06 08:24:18 +0000441
Todd Fiala4ccfe392014-02-05 05:04:36 +0000442#if !defined(HAVE_DECL_ARC4RANDOM) || !HAVE_DECL_ARC4RANDOM
Eugene Zelenko1760dc22016-04-05 20:19:49 +0000443
444namespace {
445
446unsigned GetRandomNumberSeed() {
Daniel Dunbar5f1c9562012-05-08 20:38:00 +0000447 // Attempt to get the initial seed from /dev/urandom, if possible.
Rafael Espindola515f8df2015-12-11 22:52:32 +0000448 int urandomFD = open("/dev/urandom", O_RDONLY);
449
450 if (urandomFD != -1) {
Daniel Dunbar5f1c9562012-05-08 20:38:00 +0000451 unsigned seed;
Rafael Espindola515f8df2015-12-11 22:52:32 +0000452 // Don't use a buffered read to avoid reading more data
453 // from /dev/urandom than we need.
454 int count = read(urandomFD, (void *)&seed, sizeof(seed));
455
456 close(urandomFD);
Daniel Dunbar5f1c9562012-05-08 20:38:00 +0000457
458 // Return the seed if the read was successful.
Rafael Espindola515f8df2015-12-11 22:52:32 +0000459 if (count == sizeof(seed))
Daniel Dunbar5f1c9562012-05-08 20:38:00 +0000460 return seed;
NAKAMURA Takumi7bec7412012-05-06 08:24:24 +0000461 }
Daniel Dunbar5f1c9562012-05-08 20:38:00 +0000462
463 // Otherwise, swizzle the current time and the process ID to form a reasonable
464 // seed.
Chandler Carruth5473dfb2012-12-31 11:45:20 +0000465 TimeValue Now = TimeValue::now();
Daniel Dunbar5f1c9562012-05-08 20:38:00 +0000466 return hash_combine(Now.seconds(), Now.nanoseconds(), ::getpid());
NAKAMURA Takumi7bec7412012-05-06 08:24:24 +0000467}
Eugene Zelenko1760dc22016-04-05 20:19:49 +0000468
469} // end anonymous namespace
470
NAKAMURA Takumi7bec7412012-05-06 08:24:24 +0000471#endif
472
NAKAMURA Takumi54acb282012-05-06 08:24:18 +0000473unsigned llvm::sys::Process::GetRandomNumber() {
Todd Fiala4ccfe392014-02-05 05:04:36 +0000474#if defined(HAVE_DECL_ARC4RANDOM) && HAVE_DECL_ARC4RANDOM
NAKAMURA Takumi54acb282012-05-06 08:24:18 +0000475 return arc4random();
476#else
Richard Trieu7a083812016-02-18 22:09:30 +0000477 static int x = (static_cast<void>(::srand(GetRandomNumberSeed())), 0);
NAKAMURA Takumi7bec7412012-05-06 08:24:24 +0000478 (void)x;
NAKAMURA Takumi54acb282012-05-06 08:24:18 +0000479 return ::rand();
480#endif
481}