20#include "qplatformdefs.h"
21#include <QtCore/private/qglobal_p.h>
26# error "qcore_unix_p.h included on a non-Unix system"
34#if !defined (Q_OS_VXWORKS)
35# if !defined(Q_OS_HPUX) || defined(__ia64)
36# include <sys/select.h>
40# include <selectLib.h>
48#if defined(Q_OS_VXWORKS)
52#ifdef QT_NO_NATIVE_POLL
60#define QT_EINTR_LOOP(var, cmd)
63 } while (var == -1
&& errno
== EINTR
)
70#if !defined(CLOCK_MONOTONIC
)
73#elif defined(_LIBCPP_VERSION)
74 && (defined(_LIBCPP_HAS_NO_MONOTONIC_CLOCK)
75 || (defined(_LIBCPP_HAS_MONOTONIC_CLOCK) && !_LIBCPP_HAS_MONOTONIC_CLOCK))
78#elif defined(__GLIBCXX__
) && !defined(_GLIBCXX_USE_CLOCK_MONOTONIC
)
81#elif defined(_LIBCPP_VERSION) && defined(Q_OS_DARWIN)
85#elif defined(__GLIBCXX__
) || defined(_LIBCPP_VERSION)
89# warning "Unknown C++ Standard Library implementation - code may be sub-optimal"
98#elif !QT_CONFIG(pthread_condattr_setclock)
101#elif defined(Q_OS_QNX)
109static constexpr auto OneSecAsNsecs = std::chrono::nanoseconds(std::chrono::seconds{ 1 }).count();
113 using namespace std::chrono;
114 const seconds secs = duration_cast<seconds>(timeout);
115 const nanoseconds frac = timeout - secs;
117 ts.tv_sec = secs.count();
118 ts.tv_nsec = frac.count();
122template <
typename Duration>
125 using namespace std::chrono;
126 return duration_cast<Duration>(seconds{ts.tv_sec} + nanoseconds{ts.tv_nsec});
132 while (t.tv_nsec >= OneSecAsNsecs) {
134 t.tv_nsec -= OneSecAsNsecs;
136 while (t.tv_nsec < 0) {
138 t.tv_nsec += OneSecAsNsecs;
146 tv.tv_sec = ts.tv_sec;
147 tv.tv_usec = ts.tv_nsec / 1000;
154 using namespace std::chrono;
156 std::conditional_t<ClockId == CLOCK_REALTIME, system_clock, steady_clock>;
157 auto timePoint = deadline.deadline<Clock>();
158 if (timePoint <
typename Clock::time_point{})
160 return durationToTimespec(timePoint.time_since_epoch());
165#if defined(Q_PROCESSOR_X86_32) && defined(__GLIBC__
)
166# if !__GLIBC_PREREQ(2
, 22
)
167Q_CORE_EXPORT
int qt_open64(
const char *pathname,
int flags, mode_t);
169# define QT_OPEN qt_open64
174static inline int qt_safe_openat(
int dfd,
const char *pathname,
int flags, mode_t mode = 0777)
178 QT_EINTR_LOOP(fd, openat(dfd, pathname, flags | O_CLOEXEC, mode));
185static inline int qt_safe_open(
const char *pathname,
int flags, mode_t mode = 0777)
195 ::fcntl(fd, F_SETFD, FD_CLOEXEC);
201#define QT_OPEN qt_safe_open
207 Q_ASSERT((flags & ~O_NONBLOCK) == 0);
209#ifdef QT_THREADSAFE_CLOEXEC
212 return ::pipe2(pipefd, flags);
214 int ret = ::pipe(pipefd);
218 ::fcntl(pipefd[0], F_SETFD, FD_CLOEXEC);
219 ::fcntl(pipefd[1], F_SETFD, FD_CLOEXEC);
222 if (flags & O_NONBLOCK) {
223 ::fcntl(pipefd[0], F_SETFL, ::fcntl(pipefd[0], F_GETFL) | O_NONBLOCK);
224 ::fcntl(pipefd[1], F_SETFL, ::fcntl(pipefd[1], F_GETFL) | O_NONBLOCK);
232static inline int qt_safe_dup(
int oldfd,
int atleast = 0,
int flags = FD_CLOEXEC)
234 Q_ASSERT(flags == FD_CLOEXEC || flags == 0);
236#ifdef F_DUPFD_CLOEXEC
238 if (flags & FD_CLOEXEC)
239 cmd = F_DUPFD_CLOEXEC;
240 return ::fcntl(oldfd, cmd, atleast);
243 int ret = ::fcntl(oldfd, F_DUPFD, atleast);
245 if (flags && ret != -1)
246 ::fcntl(ret, F_SETFD, flags);
253static inline int qt_safe_dup2(
int oldfd,
int newfd,
int flags = FD_CLOEXEC)
255 Q_ASSERT(flags == FD_CLOEXEC || flags == 0);
260 QT_EINTR_LOOP(ret, ::dup3(oldfd, newfd, flags ? O_CLOEXEC : 0));
268 ::fcntl(newfd, F_SETFD, flags);
280#define QT_READ qt_safe_read
289#define QT_WRITE qt_safe_write
294 return qt_safe_write(fd, data, len);
304#define QT_CLOSE qt_safe_close
307#if QT_CONFIG(process)
308static inline int qt_safe_execve(
const char *filename,
char *
const argv[],
312 QT_EINTR_LOOP(ret, ::execve(filename, argv, envp));
316static inline int qt_safe_execv(
const char *path,
char *
const argv[])
319 QT_EINTR_LOOP(ret, ::execv(path, argv));
323static inline int qt_safe_execvp(
const char *file,
char *
const argv[])
326 QT_EINTR_LOOP(ret, ::execvp(file, argv));
330static inline pid_t qt_safe_waitpid(pid_t pid,
int *status,
int options)
333 QT_EINTR_LOOP(ret, ::waitpid(pid, status, options));
339# define _POSIX_MONOTONIC_CLOCK -1
348# ifdef QT_LINUX_ALWAYS_HAVE_PROCFS
351 static const bool present = (access(
"/proc/version", F_OK) == 0);
359Q_CORE_EXPORT
int qt_safe_poll(
struct pollfd *fds, nfds_t nfds, QDeadlineTimer deadline);
363 struct pollfd pfd = { fd, events, 0 };
Combined button and popup list for selecting options.
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)
#define QT_EINTR_LOOP(var, cmd)
static qint64 qt_safe_read(int fd, void *data, qint64 maxlen)
QT_BEGIN_NAMESPACE Q_DECLARE_TYPEINFO(pollfd, Q_PRIMITIVE_TYPE)
static constexpr clockid_t QSteadyClockClockId
static constexpr auto OneSecAsNsecs
bool qt_haveLinuxProcfs()
#define _POSIX_MONOTONIC_CLOCK
Duration timespecToChrono(timespec ts) noexcept
timeval timespecToTimeval(timespec ts)
static struct pollfd qt_make_pollfd(int fd, short events)
static int qt_safe_dup2(int oldfd, int newfd, int flags=FD_CLOEXEC)
static constexpr clockid_t QWaitConditionClockId
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)
static qint64 qt_safe_write_nosignal(int fd, const void *data, qint64 len)
static int qt_safe_pipe(int pipefd[2], int flags=0)