19#include "qplatformdefs.h"
20#include <QtCore/private/qglobal_p.h>
25# error "qcore_unix_p.h included on a non-Unix system"
33#if !defined (Q_OS_VXWORKS)
34# if !defined(Q_OS_HPUX) || defined(__ia64)
35# include <sys/select.h>
39# include <selectLib.h>
47#if defined(Q_OS_VXWORKS)
51#ifdef QT_NO_NATIVE_POLL
59#define QT_EINTR_LOOP(var, cmd)
62 } while (var == -1
&& errno
== EINTR
)
69#if !defined(CLOCK_MONOTONIC
)
72#elif defined(_LIBCPP_VERSION) && defined(_LIBCPP_HAS_NO_MONOTONIC_CLOCK)
75#elif defined(__GLIBCXX__
) && !defined(_GLIBCXX_USE_CLOCK_MONOTONIC
)
78#elif defined(_LIBCPP_VERSION) && defined(Q_OS_DARWIN)
82#elif defined(__GLIBCXX__
) || defined(_LIBCPP_VERSION)
86# warning "Unknown C++ Standard Library implementation - code may be sub-optimal"
95#elif !QT_CONFIG(pthread_condattr_setclock)
98#elif defined(Q_OS_QNX)
106static constexpr auto OneSecAsNsecs = std::chrono::nanoseconds(std::chrono::seconds{ 1 }).count();
110 using namespace std::chrono;
111 const seconds secs = duration_cast<seconds>(timeout);
112 const nanoseconds frac = timeout - secs;
114 ts.tv_sec = secs.count();
115 ts.tv_nsec = frac.count();
119template <
typename Duration>
122 using namespace std::chrono;
123 return duration_cast<Duration>(seconds{ts.tv_sec} + nanoseconds{ts.tv_nsec});
128 return timespecToChrono<
std::chrono::milliseconds>(ts);
134 while (t.tv_nsec >= OneSecAsNsecs) {
136 t.tv_nsec -= OneSecAsNsecs;
138 while (t.tv_nsec < 0) {
140 t.tv_nsec += OneSecAsNsecs;
144constexpr inline bool operator<(
const timespec &t1,
const timespec &t2)
145{
return t1.tv_sec < t2.tv_sec || (t1.tv_sec == t2.tv_sec && t1.tv_nsec < t2.tv_nsec); }
146constexpr inline bool operator==(
const timespec &t1,
const timespec &t2)
147{
return t1.tv_sec == t2.tv_sec && t1.tv_nsec == t2.tv_nsec; }
148constexpr inline bool operator!=(
const timespec &t1,
const timespec &t2)
149{
return !(t1
== t2); }
150constexpr inline timespec &
operator+=(timespec &t1,
const timespec &t2)
152 t1.tv_sec += t2.tv_sec;
153 t1.tv_nsec += t2.tv_nsec;
156constexpr inline timespec
operator+(
const timespec &t1,
const timespec &t2)
159 tmp.tv_sec = t1.tv_sec + t2.tv_sec;
160 tmp.tv_nsec = t1.tv_nsec + t2.tv_nsec;
163constexpr inline timespec
operator-(
const timespec &t1,
const timespec &t2)
166 tmp.tv_sec = t1.tv_sec - (t2.tv_sec - 1);
167 tmp.tv_nsec = t1.tv_nsec - (t2.tv_nsec + OneSecAsNsecs);
170constexpr inline timespec
operator*(
const timespec &t1,
int mul)
173 tmp.tv_sec = t1.tv_sec * mul;
174 tmp.tv_nsec = t1.tv_nsec * mul;
180 tv.tv_sec = ts.tv_sec;
181 tv.tv_usec = ts.tv_nsec / 1000;
185inline timespec &
operator+=(timespec &t1,
std::chrono::milliseconds msecs)
187 t1 += durationToTimespec(msecs);
193 t1 += std::chrono::milliseconds{ms};
197inline timespec
operator+(
const timespec &t1,
std::chrono::milliseconds msecs)
204inline timespec
operator+(
const timespec &t1,
int ms)
206 return t1 + std::chrono::milliseconds{ms};
212 ts.tv_sec = -ts.tv_sec - 1;
213 ts.tv_nsec -= OneSecAsNsecs;
215 if (ts.tv_sec == 0 && ts.tv_nsec < 0) {
216 ts.tv_nsec = -ts.tv_nsec;
224 using namespace std::chrono;
226 std::conditional_t<ClockId == CLOCK_REALTIME, system_clock, steady_clock>;
227 auto timePoint = deadline.deadline<Clock>();
228 if (timePoint <
typename Clock::time_point{})
230 return durationToTimespec(timePoint.time_since_epoch());
235#if defined(Q_PROCESSOR_X86_32) && defined(__GLIBC__
)
236# if !__GLIBC_PREREQ(2
, 22
)
237Q_CORE_EXPORT
int qt_open64(
const char *pathname,
int flags, mode_t);
239# define QT_OPEN qt_open64
244static inline int qt_safe_openat(
int dfd,
const char *pathname,
int flags, mode_t mode = 0777)
248 QT_EINTR_LOOP(fd, openat(dfd, pathname, flags | O_CLOEXEC, mode));
255static inline int qt_safe_open(
const char *pathname,
int flags, mode_t mode = 0777)
265 ::fcntl(fd, F_SETFD, FD_CLOEXEC);
271#define QT_OPEN qt_safe_open
277 Q_ASSERT((flags & ~O_NONBLOCK) == 0);
279#ifdef QT_THREADSAFE_CLOEXEC
282 return ::pipe2(pipefd, flags);
284 int ret = ::pipe(pipefd);
288 ::fcntl(pipefd[0], F_SETFD, FD_CLOEXEC);
289 ::fcntl(pipefd[1], F_SETFD, FD_CLOEXEC);
292 if (flags & O_NONBLOCK) {
293 ::fcntl(pipefd[0], F_SETFL, ::fcntl(pipefd[0], F_GETFL) | O_NONBLOCK);
294 ::fcntl(pipefd[1], F_SETFL, ::fcntl(pipefd[1], F_GETFL) | O_NONBLOCK);
302static inline int qt_safe_dup(
int oldfd,
int atleast = 0,
int flags = FD_CLOEXEC)
304 Q_ASSERT(flags == FD_CLOEXEC || flags == 0);
306#ifdef F_DUPFD_CLOEXEC
308 if (flags & FD_CLOEXEC)
309 cmd = F_DUPFD_CLOEXEC;
310 return ::fcntl(oldfd, cmd, atleast);
313 int ret = ::fcntl(oldfd, F_DUPFD, atleast);
315 if (flags && ret != -1)
316 ::fcntl(ret, F_SETFD, flags);
323static inline int qt_safe_dup2(
int oldfd,
int newfd,
int flags = FD_CLOEXEC)
325 Q_ASSERT(flags == FD_CLOEXEC || flags == 0);
330 QT_EINTR_LOOP(ret, ::dup3(oldfd, newfd, flags ? O_CLOEXEC : 0));
338 ::fcntl(newfd, F_SETFD, flags);
350#define QT_READ qt_safe_read
359#define QT_WRITE qt_safe_write
364 return qt_safe_write(fd, data, len);
374#define QT_CLOSE qt_safe_close
377#if QT_CONFIG(process)
378static inline int qt_safe_execve(
const char *filename,
char *
const argv[],
382 QT_EINTR_LOOP(ret, ::execve(filename, argv, envp));
386static inline int qt_safe_execv(
const char *path,
char *
const argv[])
389 QT_EINTR_LOOP(ret, ::execv(path, argv));
393static inline int qt_safe_execvp(
const char *file,
char *
const argv[])
396 QT_EINTR_LOOP(ret, ::execvp(file, argv));
400static inline pid_t qt_safe_waitpid(pid_t pid,
int *status,
int options)
403 QT_EINTR_LOOP(ret, ::waitpid(pid, status, options));
409# define _POSIX_MONOTONIC_CLOCK -1
418# ifdef QT_LINUX_ALWAYS_HAVE_PROCFS
421 static const bool present = (access(
"/proc/version", F_OK) == 0);
429Q_CORE_EXPORT
int qt_safe_poll(
struct pollfd *fds, nfds_t nfds, QDeadlineTimer deadline);
433 struct pollfd pfd = { fd, events, 0 };
int qt_safe_poll(struct pollfd *fds, nfds_t nfds, QDeadlineTimer deadline)
int qt_poll(struct pollfd *fds, nfds_t nfds, const struct timespec *timeout_ts)
static int timespecToMillisecs(const struct timespec *ts)
QT_BEGIN_NAMESPACE void qt_ignore_sigpipe() noexcept
static int qt_ppoll(struct pollfd *fds, nfds_t nfds, const struct timespec *timeout_ts)
QByteArray qt_readlink(const char *path)
static qint64 qt_safe_write(int fd, const void *data, qint64 len)
static int qt_safe_open(const char *pathname, int flags, mode_t mode=0777)
constexpr bool operator==(const timespec &t1, const timespec &t2)
#define QT_EINTR_LOOP(var, cmd)
static qint64 qt_safe_read(int fd, void *data, qint64 maxlen)
timespec & operator+=(timespec &t1, std::chrono::milliseconds msecs)
QT_BEGIN_NAMESPACE Q_DECLARE_TYPEINFO(pollfd, Q_PRIMITIVE_TYPE)
constexpr bool operator!=(const timespec &t1, const timespec &t2)
static constexpr auto OneSecAsNsecs
timespec qAbsTimespec(timespec ts)
bool qt_haveLinuxProcfs()
#define _POSIX_MONOTONIC_CLOCK
constexpr timespec operator+(const timespec &t1, const timespec &t2)
Duration timespecToChrono(timespec ts) noexcept
timeval timespecToTimeval(timespec ts)
static struct pollfd qt_make_pollfd(int fd, short events)
std::chrono::milliseconds timespecToChronoMs(timespec ts) noexcept
constexpr timespec operator*(const timespec &t1, int mul)
static int qt_safe_dup2(int oldfd, int newfd, int flags=FD_CLOEXEC)
static constexpr clockid_t QWaitConditionClockId
timespec operator+(const timespec &t1, int ms)
timespec durationToTimespec(std::chrono::nanoseconds timeout) noexcept
constexpr timespec & normalizedTimespec(timespec &t)
timespec deadlineToAbstime(QDeadlineTimer deadline)
static int qt_safe_dup(int oldfd, int atleast=0, int flags=FD_CLOEXEC)
static int qt_safe_close(int fd)
timespec & operator+=(timespec &t1, int ms)
static qint64 qt_safe_write_nosignal(int fd, const void *data, qint64 len)
timespec operator+(const timespec &t1, std::chrono::milliseconds msecs)
static int qt_safe_pipe(int pipefd[2], int flags=0)
constexpr timespec & operator+=(timespec &t1, const timespec &t2)
constexpr timespec operator-(const timespec &t1, const timespec &t2)
constexpr bool operator<(const timespec &t1, const timespec &t2)
static constexpr clockid_t SteadyClockClockId