1 //===- llvm/Support/Unix/Unix.h - Common Unix Include File -------*- C++ -*-===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 // 9 // This file defines things specific to Unix implementations. 10 // 11 //===----------------------------------------------------------------------===// 12 13 #ifndef LLVM_LIB_SUPPORT_UNIX_UNIX_H 14 #define LLVM_LIB_SUPPORT_UNIX_UNIX_H 15 16 //===----------------------------------------------------------------------===// 17 //=== WARNING: Implementation here must contain only generic UNIX code that 18 //=== is guaranteed to work on all UNIX variants. 19 //===----------------------------------------------------------------------===// 20 21 #include "llvm/Config/config.h" 22 #include "llvm/Support/Chrono.h" 23 #include "llvm/Support/Errno.h" 24 #include "llvm/Support/ErrorHandling.h" 25 #include <algorithm> 26 #include <assert.h> 27 #include <cerrno> 28 #include <cstdio> 29 #include <cstdlib> 30 #include <cstring> 31 #include <string> 32 #include <sys/types.h> 33 #include <sys/wait.h> 34 35 #ifdef HAVE_UNISTD_H 36 #include <unistd.h> 37 #endif 38 39 #include <sys/time.h> 40 #include <time.h> 41 42 #include <dlfcn.h> 43 44 # include <fcntl.h> 45 46 /// This function builds an error message into \p ErrMsg using the \p prefix 47 /// string and the Unix error number given by \p errnum. If errnum is -1, the 48 /// default then the value of errno is used. 49 /// Make an error message 50 /// 51 /// If the error number can be converted to a string, it will be 52 /// separated from prefix by ": ". 53 static inline bool MakeErrMsg( 54 std::string* ErrMsg, const std::string& prefix, int errnum = -1) { 55 if (!ErrMsg) 56 return true; 57 if (errnum == -1) 58 errnum = errno; 59 *ErrMsg = prefix + ": " + llvm::sys::StrError(errnum); 60 return true; 61 } 62 63 // Include StrError(errnum) in a fatal error message. 64 [[noreturn]] static inline void ReportErrnumFatal(const char *Msg, int errnum) { 65 std::string ErrMsg; 66 MakeErrMsg(&ErrMsg, Msg, errnum); 67 llvm::report_fatal_error(llvm::Twine(ErrMsg)); 68 } 69 70 namespace llvm { 71 namespace sys { 72 73 /// Convert a struct timeval to a duration. Note that timeval can be used both 74 /// as a time point and a duration. Be sure to check what the input represents. 75 inline std::chrono::microseconds toDuration(const struct timeval &TV) { 76 return std::chrono::seconds(TV.tv_sec) + 77 std::chrono::microseconds(TV.tv_usec); 78 } 79 80 /// Convert a time point to struct timespec. 81 inline struct timespec toTimeSpec(TimePoint<> TP) { 82 using namespace std::chrono; 83 84 struct timespec RetVal; 85 RetVal.tv_sec = toTimeT(TP); 86 RetVal.tv_nsec = (TP.time_since_epoch() % seconds(1)).count(); 87 return RetVal; 88 } 89 90 /// Convert a time point to struct timeval. 91 inline struct timeval toTimeVal(TimePoint<std::chrono::microseconds> TP) { 92 using namespace std::chrono; 93 94 struct timeval RetVal; 95 RetVal.tv_sec = toTimeT(TP); 96 RetVal.tv_usec = (TP.time_since_epoch() % seconds(1)).count(); 97 return RetVal; 98 } 99 100 } // namespace sys 101 } // namespace llvm 102 103 #endif 104