Unix.h 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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. #ifndef LLVM_LIB_SUPPORT_UNIX_UNIX_H
  13. #define LLVM_LIB_SUPPORT_UNIX_UNIX_H
  14. //===----------------------------------------------------------------------===//
  15. //=== WARNING: Implementation here must contain only generic UNIX code that
  16. //=== is guaranteed to work on all UNIX variants.
  17. //===----------------------------------------------------------------------===//
  18. #include "llvm/Config/config.h"
  19. #include "llvm/Support/Chrono.h"
  20. #include "llvm/Support/Errno.h"
  21. #include "llvm/Support/ErrorHandling.h"
  22. #include <algorithm>
  23. #include <assert.h>
  24. #include <cerrno>
  25. #include <cstdio>
  26. #include <cstdlib>
  27. #include <cstring>
  28. #include <string>
  29. #include <sys/types.h>
  30. #include <sys/wait.h>
  31. #ifdef HAVE_UNISTD_H
  32. #include <unistd.h>
  33. #endif
  34. #ifdef HAVE_SYS_TIME_H
  35. # include <sys/time.h>
  36. #endif
  37. #include <time.h>
  38. #ifdef HAVE_DLFCN_H
  39. # include <dlfcn.h>
  40. #endif
  41. #ifdef HAVE_FCNTL_H
  42. # include <fcntl.h>
  43. #endif
  44. /// This function builds an error message into \p ErrMsg using the \p prefix
  45. /// string and the Unix error number given by \p errnum. If errnum is -1, the
  46. /// default then the value of errno is used.
  47. /// Make an error message
  48. ///
  49. /// If the error number can be converted to a string, it will be
  50. /// separated from prefix by ": ".
  51. static inline bool MakeErrMsg(
  52. std::string* ErrMsg, const std::string& prefix, int errnum = -1) {
  53. if (!ErrMsg)
  54. return true;
  55. if (errnum == -1)
  56. errnum = errno;
  57. *ErrMsg = prefix + ": " + llvm::sys::StrError(errnum);
  58. return true;
  59. }
  60. // Include StrError(errnum) in a fatal error message.
  61. [[noreturn]] static inline void ReportErrnumFatal(const char *Msg, int errnum) {
  62. std::string ErrMsg;
  63. MakeErrMsg(&ErrMsg, Msg, errnum);
  64. llvm::report_fatal_error(llvm::Twine(ErrMsg));
  65. }
  66. namespace llvm {
  67. namespace sys {
  68. /// Convert a struct timeval to a duration. Note that timeval can be used both
  69. /// as a time point and a duration. Be sure to check what the input represents.
  70. inline std::chrono::microseconds toDuration(const struct timeval &TV) {
  71. return std::chrono::seconds(TV.tv_sec) +
  72. std::chrono::microseconds(TV.tv_usec);
  73. }
  74. /// Convert a time point to struct timespec.
  75. inline struct timespec toTimeSpec(TimePoint<> TP) {
  76. using namespace std::chrono;
  77. struct timespec RetVal;
  78. RetVal.tv_sec = toTimeT(TP);
  79. RetVal.tv_nsec = (TP.time_since_epoch() % seconds(1)).count();
  80. return RetVal;
  81. }
  82. /// Convert a time point to struct timeval.
  83. inline struct timeval toTimeVal(TimePoint<std::chrono::microseconds> TP) {
  84. using namespace std::chrono;
  85. struct timeval RetVal;
  86. RetVal.tv_sec = toTimeT(TP);
  87. RetVal.tv_usec = (TP.time_since_epoch() % seconds(1)).count();
  88. return RetVal;
  89. }
  90. } // namespace sys
  91. } // namespace llvm
  92. #endif