blob: 1fc9a414f74941739cce4e34ef9677f816de0985 [file] [log] [blame]
Duncan P. N. Exon Smith91d3cfe2016-04-05 20:45:04 +00001//===- llvm/Support/Unix/Unix.h - Common Unix Include File -------*- C++ -*-===//
Misha Brukman10468d82005-04-21 22:55:34 +00002//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// 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
Misha Brukman10468d82005-04-21 22:55:34 +00006//
Reid Spencer814ba572004-08-25 06:20:07 +00007//===----------------------------------------------------------------------===//
8//
9// This file defines things specific to Unix implementations.
10//
11//===----------------------------------------------------------------------===//
12
Benjamin Kramera7c40ef2014-08-13 16:26:38 +000013#ifndef LLVM_LIB_SUPPORT_UNIX_UNIX_H
14#define LLVM_LIB_SUPPORT_UNIX_UNIX_H
Reid Spencere925d8b2004-12-27 06:17:50 +000015
Reid Spencer814ba572004-08-25 06:20:07 +000016//===----------------------------------------------------------------------===//
17//=== WARNING: Implementation here must contain only generic UNIX code that
18//=== is guaranteed to work on all UNIX variants.
19//===----------------------------------------------------------------------===//
20
Sven van Haastregt1bc2ccc2019-07-03 09:57:59 +000021#include "llvm/Config/config.h"
Pavel Labath757ca882016-10-24 10:59:17 +000022#include "llvm/Support/Chrono.h"
Michael J. Spencer4d2e1502010-11-29 18:29:55 +000023#include "llvm/Support/Errno.h"
Sam McCalla9c3c172019-10-23 15:34:48 +020024#include "llvm/Support/ErrorHandling.h"
Chris Lattner6ef473f2004-12-08 16:10:52 +000025#include <algorithm>
Duncan P. N. Exon Smith91d3cfe2016-04-05 20:45:04 +000026#include <assert.h>
Chandler Carruth802d7552012-12-04 07:12:27 +000027#include <cerrno>
28#include <cstdio>
29#include <cstdlib>
30#include <cstring>
31#include <string>
Reid Kleckner37f69de2013-07-26 16:54:23 +000032#include <sys/types.h>
Justin Bognerc7e3f3a2015-08-04 06:29:58 +000033#include <sys/wait.h>
Reid Spencer814ba572004-08-25 06:20:07 +000034
Reid Spencere925d8b2004-12-27 06:17:50 +000035#ifdef HAVE_UNISTD_H
36#include <unistd.h>
37#endif
38
Reid Spencere925d8b2004-12-27 06:17:50 +000039#ifdef HAVE_SYS_PARAM_H
40#include <sys/param.h>
Misha Brukman10468d82005-04-21 22:55:34 +000041#endif
Reid Spencere925d8b2004-12-27 06:17:50 +000042
Michael J. Spencer04b795b2012-04-23 19:00:27 +000043#ifdef HAVE_SYS_TIME_H
Reid Spencere925d8b2004-12-27 06:17:50 +000044# include <sys/time.h>
Reid Spencere925d8b2004-12-27 06:17:50 +000045#endif
Duncan P. N. Exon Smith91d3cfe2016-04-05 20:45:04 +000046#include <time.h>
Reid Spencere925d8b2004-12-27 06:17:50 +000047
Rafael Espindola9ab9fe92013-10-08 16:12:58 +000048#ifdef HAVE_DLFCN_H
49# include <dlfcn.h>
50#endif
51
Eugene Leviantea877d42016-08-26 08:14:54 +000052#ifdef HAVE_FCNTL_H
53# include <fcntl.h>
54#endif
55
Reid Spencer42bcf6e2006-08-21 06:02:44 +000056/// This function builds an error message into \p ErrMsg using the \p prefix
57/// string and the Unix error number given by \p errnum. If errnum is -1, the
58/// default then the value of errno is used.
Adrian Prantl4dfcc4a2018-05-01 16:10:38 +000059/// Make an error message
Daniel Dunbar3222b9b2009-04-20 20:50:13 +000060///
61/// If the error number can be converted to a string, it will be
62/// separated from prefix by ": ".
Dan Gohmand78c4002008-05-13 00:00:25 +000063static inline bool MakeErrMsg(
Reid Spencer42bcf6e2006-08-21 06:02:44 +000064 std::string* ErrMsg, const std::string& prefix, int errnum = -1) {
65 if (!ErrMsg)
Reid Spencer879ed5a2006-08-23 07:30:48 +000066 return true;
Reid Spencer42bcf6e2006-08-21 06:02:44 +000067 if (errnum == -1)
68 errnum = errno;
Jeffrey Yasskin1a933302009-07-01 18:11:20 +000069 *ErrMsg = prefix + ": " + llvm::sys::StrError(errnum);
Reid Spencer879ed5a2006-08-23 07:30:48 +000070 return true;
Reid Spencer42bcf6e2006-08-21 06:02:44 +000071}
72
Sam McCalla9c3c172019-10-23 15:34:48 +020073// Include StrError(errnum) in a fatal error message.
74LLVM_ATTRIBUTE_NORETURN static inline void ReportErrnumFatal(const char *Msg,
75 int errnum) {
76 std::string ErrMsg;
77 MakeErrMsg(&ErrMsg, Msg, errnum);
78 llvm::report_fatal_error(ErrMsg);
79}
80
Pavel Labath757ca882016-10-24 10:59:17 +000081namespace llvm {
82namespace sys {
83
84/// Convert a struct timeval to a duration. Note that timeval can be used both
85/// as a time point and a duration. Be sure to check what the input represents.
86inline std::chrono::microseconds toDuration(const struct timeval &TV) {
87 return std::chrono::seconds(TV.tv_sec) +
88 std::chrono::microseconds(TV.tv_usec);
89}
90
91/// Convert a time point to struct timespec.
92inline struct timespec toTimeSpec(TimePoint<> TP) {
93 using namespace std::chrono;
94
95 struct timespec RetVal;
96 RetVal.tv_sec = toTimeT(TP);
97 RetVal.tv_nsec = (TP.time_since_epoch() % seconds(1)).count();
98 return RetVal;
99}
100
101/// Convert a time point to struct timeval.
102inline struct timeval toTimeVal(TimePoint<std::chrono::microseconds> TP) {
103 using namespace std::chrono;
104
105 struct timeval RetVal;
106 RetVal.tv_sec = toTimeT(TP);
107 RetVal.tv_usec = (TP.time_since_epoch() % seconds(1)).count();
108 return RetVal;
109}
110
111} // namespace sys
112} // namespace llvm
113
Duncan P. N. Exon Smith91d3cfe2016-04-05 20:45:04 +0000114#endif