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) && defined(_LIBCPP_HAS_NO_MONOTONIC_CLOCK)
76#elif defined(__GLIBCXX__
) && !defined(_GLIBCXX_USE_CLOCK_MONOTONIC
)
79#elif defined(_LIBCPP_VERSION) && defined(Q_OS_DARWIN)
83#elif defined(__GLIBCXX__
) || defined(_LIBCPP_VERSION)
87# warning "Unknown C++ Standard Library implementation - code may be sub-optimal"
96#elif !QT_CONFIG(pthread_condattr_setclock)
99#elif defined(Q_OS_QNX)
107static constexpr auto OneSecAsNsecs = std::chrono::nanoseconds(std::chrono::seconds{ 1 }).count();
111 using namespace std::chrono;
112 const seconds secs = duration_cast<seconds>(timeout);
113 const nanoseconds frac = timeout - secs;
115 ts.tv_sec = secs.count();
116 ts.tv_nsec = frac.count();
120template <
typename Duration>
123 using namespace std::chrono;
124 return duration_cast<Duration>(seconds{ts.tv_sec} + nanoseconds{ts.tv_nsec});
130 while (t.tv_nsec >= OneSecAsNsecs) {
132 t.tv_nsec -= OneSecAsNsecs;
134 while (t.tv_nsec < 0) {
136 t.tv_nsec += OneSecAsNsecs;
144 tv.tv_sec = ts.tv_sec;
145 tv.tv_usec = ts.tv_nsec / 1000;
152 using namespace std::chrono;
154 std::conditional_t<ClockId == CLOCK_REALTIME, system_clock, steady_clock>;
155 auto timePoint = deadline.deadline<Clock>();
156 if (timePoint <
typename Clock::time_point{})
158 return durationToTimespec(timePoint.time_since_epoch());
163#if defined(Q_PROCESSOR_X86_32) && defined(__GLIBC__
)
164# if !__GLIBC_PREREQ(2
, 22
)
165Q_CORE_EXPORT
int qt_open64(
const char *pathname,
int flags, mode_t);
167# define QT_OPEN qt_open64
172static inline int qt_safe_openat(
int dfd,
const char *pathname,
int flags, mode_t mode = 0777)
176 QT_EINTR_LOOP(fd, openat(dfd, pathname, flags | O_CLOEXEC, mode));
183static inline int qt_safe_open(
const char *pathname,
int flags, mode_t mode = 0777)
193 ::fcntl(fd, F_SETFD, FD_CLOEXEC);
199#define QT_OPEN qt_safe_open
205 Q_ASSERT((flags & ~O_NONBLOCK) == 0);
207#ifdef QT_THREADSAFE_CLOEXEC
210 return ::pipe2(pipefd, flags);
212 int ret = ::pipe(pipefd);
216 ::fcntl(pipefd[0], F_SETFD, FD_CLOEXEC);
217 ::fcntl(pipefd[1], F_SETFD, FD_CLOEXEC);
220 if (flags & O_NONBLOCK) {
221 ::fcntl(pipefd[0], F_SETFL, ::fcntl(pipefd[0], F_GETFL) | O_NONBLOCK);
222 ::fcntl(pipefd[1], F_SETFL, ::fcntl(pipefd[1], F_GETFL) | O_NONBLOCK);
230static inline int qt_safe_dup(
int oldfd,
int atleast = 0,
int flags = FD_CLOEXEC)
232 Q_ASSERT(flags == FD_CLOEXEC || flags == 0);
234#ifdef F_DUPFD_CLOEXEC
236 if (flags & FD_CLOEXEC)
237 cmd = F_DUPFD_CLOEXEC;
238 return ::fcntl(oldfd, cmd, atleast);
241 int ret = ::fcntl(oldfd, F_DUPFD, atleast);
243 if (flags && ret != -1)
244 ::fcntl(ret, F_SETFD, flags);
251static inline int qt_safe_dup2(
int oldfd,
int newfd,
int flags = FD_CLOEXEC)
253 Q_ASSERT(flags == FD_CLOEXEC || flags == 0);
258 QT_EINTR_LOOP(ret, ::dup3(oldfd, newfd, flags ? O_CLOEXEC : 0));
266 ::fcntl(newfd, F_SETFD, flags);
278#define QT_READ qt_safe_read
287#define QT_WRITE qt_safe_write
292 return qt_safe_write(fd, data, len);
302#define QT_CLOSE qt_safe_close
305#if QT_CONFIG(process)
306static inline int qt_safe_execve(
const char *filename,
char *
const argv[],
310 QT_EINTR_LOOP(ret, ::execve(filename, argv, envp));
314static inline int qt_safe_execv(
const char *path,
char *
const argv[])
317 QT_EINTR_LOOP(ret, ::execv(path, argv));
321static inline int qt_safe_execvp(
const char *file,
char *
const argv[])
324 QT_EINTR_LOOP(ret, ::execvp(file, argv));
328static inline pid_t qt_safe_waitpid(pid_t pid,
int *status,
int options)
331 QT_EINTR_LOOP(ret, ::waitpid(pid, status, options));
337# define _POSIX_MONOTONIC_CLOCK -1
346# ifdef QT_LINUX_ALWAYS_HAVE_PROCFS
349 static const bool present = (access(
"/proc/version", F_OK) == 0);
357Q_CORE_EXPORT
int qt_safe_poll(
struct pollfd *fds, nfds_t nfds, QDeadlineTimer deadline);
361 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)