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
)
73#define QT_WEAK_SYMBOL(fn)
76 QT_WARNING_DISABLE_CLANG("-Wunguarded-availability")
86#if !defined(CLOCK_MONOTONIC
)
89#elif defined(_LIBCPP_VERSION)
90 && (defined(_LIBCPP_HAS_NO_MONOTONIC_CLOCK)
91 || (defined(_LIBCPP_HAS_MONOTONIC_CLOCK) && !_LIBCPP_HAS_MONOTONIC_CLOCK))
94#elif defined(__GLIBCXX__
) && !defined(_GLIBCXX_USE_CLOCK_MONOTONIC
)
97#elif defined(_LIBCPP_VERSION) && defined(Q_OS_DARWIN)
101#elif defined(__GLIBCXX__
) || defined(_LIBCPP_VERSION)
105# warning "Unknown C++ Standard Library implementation - code may be sub-optimal"
111#if !QT_CONFIG(thread)
114#elif !QT_CONFIG(pthread_condattr_setclock)
117#elif defined(Q_OS_QNX)
125static constexpr auto OneSecAsNsecs = std::chrono::nanoseconds(std::chrono::seconds{ 1 }).count();
129 using namespace std::chrono;
130 const seconds secs = duration_cast<seconds>(timeout);
131 const nanoseconds frac = timeout - secs;
133 ts.tv_sec = secs.count();
134 ts.tv_nsec = frac.count();
138template <
typename Duration>
141 using namespace std::chrono;
142 return duration_cast<Duration>(seconds{ts.tv_sec} + nanoseconds{ts.tv_nsec});
148 while (t.tv_nsec >= OneSecAsNsecs) {
150 t.tv_nsec -= OneSecAsNsecs;
152 while (t.tv_nsec < 0) {
154 t.tv_nsec += OneSecAsNsecs;
162 tv.tv_sec = ts.tv_sec;
163 tv.tv_usec = ts.tv_nsec / 1000;
170 using namespace std::chrono;
172 std::conditional_t<ClockId == CLOCK_REALTIME, system_clock, steady_clock>;
173 auto timePoint = deadline.deadline<Clock>();
174 if (timePoint <
typename Clock::time_point{})
176 return durationToTimespec(timePoint.time_since_epoch());
181#if defined(Q_PROCESSOR_X86_32) && defined(__GLIBC__
)
182# if !__GLIBC_PREREQ(2
, 22
)
183Q_CORE_EXPORT
int qt_open64(
const char *pathname,
int flags, mode_t);
185# define QT_OPEN qt_open64
190static inline int qt_safe_openat(
int dfd,
const char *pathname,
int flags, mode_t mode = 0777)
194 QT_EINTR_LOOP(fd, openat(dfd, pathname, flags | O_CLOEXEC, mode));
201static inline int qt_safe_open(
const char *pathname,
int flags, mode_t mode = 0777)
211 ::fcntl(fd, F_SETFD, FD_CLOEXEC);
217#define QT_OPEN qt_safe_open
223 Q_ASSERT((flags & ~O_NONBLOCK) == 0);
225#ifdef QT_THREADSAFE_CLOEXEC
226 if (
auto pipe2 = QT_WEAK_SYMBOL(::pipe2)) {
229 return pipe2(pipefd, flags);
233 int ret = ::pipe(pipefd);
237 ::fcntl(pipefd[0], F_SETFD, FD_CLOEXEC);
238 ::fcntl(pipefd[1], F_SETFD, FD_CLOEXEC);
241 if (flags & O_NONBLOCK) {
242 ::fcntl(pipefd[0], F_SETFL, ::fcntl(pipefd[0], F_GETFL) | O_NONBLOCK);
243 ::fcntl(pipefd[1], F_SETFL, ::fcntl(pipefd[1], F_GETFL) | O_NONBLOCK);
250static inline int qt_safe_dup(
int oldfd,
int atleast = 0,
int flags = FD_CLOEXEC)
252 Q_ASSERT(flags == FD_CLOEXEC || flags == 0);
254#ifdef F_DUPFD_CLOEXEC
256 if (flags & FD_CLOEXEC)
257 cmd = F_DUPFD_CLOEXEC;
258 return ::fcntl(oldfd, cmd, atleast);
261 int ret = ::fcntl(oldfd, F_DUPFD, atleast);
263 if (flags && ret != -1)
264 ::fcntl(ret, F_SETFD, flags);
271static inline int qt_safe_dup2(
int oldfd,
int newfd,
int flags = FD_CLOEXEC)
273 Q_ASSERT(flags == FD_CLOEXEC || flags == 0);
278 if (
auto dup3 = QT_WEAK_SYMBOL(::dup3)) {
279 QT_EINTR_LOOP(ret, dup3(oldfd, newfd, flags ? O_CLOEXEC : 0));
289 ::fcntl(newfd, F_SETFD, flags);
300#define QT_READ qt_safe_read
309#define QT_WRITE qt_safe_write
314 return qt_safe_write(fd, data, len);
324#define QT_CLOSE qt_safe_close
327#if QT_CONFIG(process)
328static inline int qt_safe_execve(
const char *filename,
char *
const argv[],
332 QT_EINTR_LOOP(ret, ::execve(filename, argv, envp));
336static inline int qt_safe_execv(
const char *path,
char *
const argv[])
339 QT_EINTR_LOOP(ret, ::execv(path, argv));
343static inline int qt_safe_execvp(
const char *file,
char *
const argv[])
346 QT_EINTR_LOOP(ret, ::execvp(file, argv));
350static inline pid_t qt_safe_waitpid(pid_t pid,
int *status,
int options)
353 QT_EINTR_LOOP(ret, ::waitpid(pid, status, options));
359# define _POSIX_MONOTONIC_CLOCK -1
368# ifdef QT_LINUX_ALWAYS_HAVE_PROCFS
371 static const bool present = (access(
"/proc/version", F_OK) == 0);
379Q_CORE_EXPORT
int qt_safe_poll(
struct pollfd *fds, nfds_t nfds, QDeadlineTimer deadline);
383 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)