blob: 20d70bd2fcee81a0378d4ee190dfdfc3197c6015 [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
Pavel Labath757ca882016-10-24 10:59:17 +000021#include "llvm/Config/config.h" // Get autoconf configuration settings
22#include "llvm/Support/Chrono.h"
Michael J. Spencer4d2e1502010-11-29 18:29:55 +000023#include "llvm/Support/Errno.h"
Chris Lattner6ef473f2004-12-08 16:10:52 +000024#include <algorithm>
Duncan P. N. Exon Smith91d3cfe2016-04-05 20:45:04 +000025#include <assert.h>
Chandler Carruth802d7552012-12-04 07:12:27 +000026#include <cerrno>
27#include <cstdio>
28#include <cstdlib>
29#include <cstring>
30#include <string>
Reid Kleckner37f69de2013-07-26 16:54:23 +000031#include <sys/types.h>
Justin Bognerc7e3f3a2015-08-04 06:29:58 +000032#include <sys/wait.h>
Reid Spencer814ba572004-08-25 06:20:07 +000033
Reid Spencere925d8b2004-12-27 06:17:50 +000034#ifdef HAVE_UNISTD_H
35#include <unistd.h>
36#endif
37
Reid Spencere925d8b2004-12-27 06:17:50 +000038#ifdef HAVE_SYS_PARAM_H
39#include <sys/param.h>
Misha Brukman10468d82005-04-21 22:55:34 +000040#endif
Reid Spencere925d8b2004-12-27 06:17:50 +000041
Michael J. Spencer04b795b2012-04-23 19:00:27 +000042#ifdef HAVE_SYS_TIME_H
Reid Spencere925d8b2004-12-27 06:17:50 +000043# include <sys/time.h>
Reid Spencere925d8b2004-12-27 06:17:50 +000044#endif
Duncan P. N. Exon Smith91d3cfe2016-04-05 20:45:04 +000045#include <time.h>
Reid Spencere925d8b2004-12-27 06:17:50 +000046
Rafael Espindola9ab9fe92013-10-08 16:12:58 +000047#ifdef HAVE_DLFCN_H
48# include <dlfcn.h>
49#endif
50
Eugene Leviantea877d42016-08-26 08:14:54 +000051#ifdef HAVE_FCNTL_H
52# include <fcntl.h>
53#endif
54
Reid Spencer42bcf6e2006-08-21 06:02:44 +000055/// This function builds an error message into \p ErrMsg using the \p prefix
56/// string and the Unix error number given by \p errnum. If errnum is -1, the
57/// default then the value of errno is used.
Adrian Prantl4dfcc4a2018-05-01 16:10:38 +000058/// Make an error message
Daniel Dunbar3222b9b2009-04-20 20:50:13 +000059///
60/// If the error number can be converted to a string, it will be
61/// separated from prefix by ": ".
Dan Gohmand78c4002008-05-13 00:00:25 +000062static inline bool MakeErrMsg(
Reid Spencer42bcf6e2006-08-21 06:02:44 +000063 std::string* ErrMsg, const std::string& prefix, int errnum = -1) {
64 if (!ErrMsg)
Reid Spencer879ed5a2006-08-23 07:30:48 +000065 return true;
Reid Spencer42bcf6e2006-08-21 06:02:44 +000066 if (errnum == -1)
67 errnum = errno;
Jeffrey Yasskin1a933302009-07-01 18:11:20 +000068 *ErrMsg = prefix + ": " + llvm::sys::StrError(errnum);
Reid Spencer879ed5a2006-08-23 07:30:48 +000069 return true;
Reid Spencer42bcf6e2006-08-21 06:02:44 +000070}
71
Pavel Labath757ca882016-10-24 10:59:17 +000072namespace llvm {
73namespace sys {
74
75/// Convert a struct timeval to a duration. Note that timeval can be used both
76/// as a time point and a duration. Be sure to check what the input represents.
77inline std::chrono::microseconds toDuration(const struct timeval &TV) {
78 return std::chrono::seconds(TV.tv_sec) +
79 std::chrono::microseconds(TV.tv_usec);
80}
81
82/// Convert a time point to struct timespec.
83inline struct timespec toTimeSpec(TimePoint<> TP) {
84 using namespace std::chrono;
85
86 struct timespec RetVal;
87 RetVal.tv_sec = toTimeT(TP);
88 RetVal.tv_nsec = (TP.time_since_epoch() % seconds(1)).count();
89 return RetVal;
90}
91
92/// Convert a time point to struct timeval.
93inline struct timeval toTimeVal(TimePoint<std::chrono::microseconds> TP) {
94 using namespace std::chrono;
95
96 struct timeval RetVal;
97 RetVal.tv_sec = toTimeT(TP);
98 RetVal.tv_usec = (TP.time_since_epoch() % seconds(1)).count();
99 return RetVal;
100}
101
102} // namespace sys
103} // namespace llvm
104
Duncan P. N. Exon Smith91d3cfe2016-04-05 20:45:04 +0000105#endif