Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qcore_unix_p.h
Go to the documentation of this file.
1// Copyright (C) 2016 The Qt Company Ltd.
2// Copyright (C) 2016 Intel Corporation.
3// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
4
5#ifndef QCORE_UNIX_P_H
6#define QCORE_UNIX_P_H
7
8//
9// W A R N I N G
10// -------------
11//
12// This file is not part of the Qt API. It exists for the convenience
13// of Qt code on Unix. This header file may change from version to
14// version without notice, or even be removed.
15//
16// We mean it.
17//
18
19#include "qplatformdefs.h"
20#include <QtCore/private/qglobal_p.h>
21#include "qbytearray.h"
22#include "qdeadlinetimer.h"
23
24#ifndef Q_OS_UNIX
25# error "qcore_unix_p.h included on a non-Unix system"
26#endif
27
28#include <string.h>
29#include <sys/types.h>
30#include <sys/stat.h>
31#include <unistd.h>
32
33#if !defined (Q_OS_VXWORKS)
34# if !defined(Q_OS_HPUX) || defined(__ia64)
35# include <sys/select.h>
36# endif
37# include <sys/time.h>
38#else
39# include <selectLib.h>
40#endif
41
42#include <chrono>
43#include <sys/wait.h>
44#include <errno.h>
45#include <fcntl.h>
46
47#if defined(Q_OS_VXWORKS)
48# include <ioLib.h>
49#endif
50
51#ifdef QT_NO_NATIVE_POLL
52# include "qpoll_p.h"
53#else
54# include <poll.h>
55#endif
56
57struct sockaddr;
58
59#define QT_EINTR_LOOP(var, cmd)
60 do {
61 var = cmd;
62 } while (var == -1 && errno == EINTR)
63
65
67
68static inline constexpr clockid_t SteadyClockClockId =
69#if !defined(CLOCK_MONOTONIC)
70 // we don't know how to set the monotonic clock
72#elif defined(_LIBCPP_VERSION) && defined(_LIBCPP_HAS_NO_MONOTONIC_CLOCK)
73 // libc++ falling back to system_clock
75#elif defined(__GLIBCXX__) && !defined(_GLIBCXX_USE_CLOCK_MONOTONIC)
76 // libstdc++ falling back to system_clock
78#elif defined(_LIBCPP_VERSION) && defined(Q_OS_DARWIN)
79 // on Apple systems, libc++ uses CLOCK_MONOTONIC_RAW since LLVM 11
80 // https://github.com/llvm/llvm-project/blob/llvmorg-11.0.0/libcxx/src/chrono.cpp#L117-L129
82#elif defined(__GLIBCXX__) || defined(_LIBCPP_VERSION)
83 // both libstdc++ and libc++ otherwise use CLOCK_MONOTONIC
84 CLOCK_MONOTONIC
85#else
86# warning "Unknown C++ Standard Library implementation - code may be sub-optimal"
87 CLOCK_REALTIME
88#endif
89 ;
90
91static inline constexpr clockid_t QWaitConditionClockId =
92#if !QT_CONFIG(thread)
93 // bootstrap mode, there are no wait conditions
95#elif !QT_CONFIG(pthread_condattr_setclock)
96 // OSes that lack pthread_condattr_setclock() (e.g., Darwin)
98#elif defined(Q_OS_QNX)
99 // unknown why use of the monotonic clock causes failures
101#else
103#endif
104 ;
105
106static constexpr auto OneSecAsNsecs = std::chrono::nanoseconds(std::chrono::seconds{ 1 }).count();
107
108inline timespec durationToTimespec(std::chrono::nanoseconds timeout) noexcept
109{
110 using namespace std::chrono;
111 const seconds secs = duration_cast<seconds>(timeout);
112 const nanoseconds frac = timeout - secs;
113 struct timespec ts;
114 ts.tv_sec = secs.count();
115 ts.tv_nsec = frac.count();
116 return ts;
117}
118
119template <typename Duration>
120inline Duration timespecToChrono(timespec ts) noexcept
121{
122 using namespace std::chrono;
123 return duration_cast<Duration>(seconds{ts.tv_sec} + nanoseconds{ts.tv_nsec});
124}
125
126inline std::chrono::milliseconds timespecToChronoMs(timespec ts) noexcept
127{
128 return timespecToChrono<std::chrono::milliseconds>(ts);
129}
130
131// Internal operator functions for timespecs
132constexpr inline timespec &normalizedTimespec(timespec &t)
133{
134 while (t.tv_nsec >= OneSecAsNsecs) {
135 ++t.tv_sec;
136 t.tv_nsec -= OneSecAsNsecs;
137 }
138 while (t.tv_nsec < 0) {
139 --t.tv_sec;
140 t.tv_nsec += OneSecAsNsecs;
141 }
142 return t;
143}
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)
151{
152 t1.tv_sec += t2.tv_sec;
153 t1.tv_nsec += t2.tv_nsec;
154 return normalizedTimespec(t1);
155}
156constexpr inline timespec operator+(const timespec &t1, const timespec &t2)
157{
158 timespec tmp = {};
159 tmp.tv_sec = t1.tv_sec + t2.tv_sec;
160 tmp.tv_nsec = t1.tv_nsec + t2.tv_nsec;
161 return normalizedTimespec(tmp);
162}
163constexpr inline timespec operator-(const timespec &t1, const timespec &t2)
164{
165 timespec tmp = {};
166 tmp.tv_sec = t1.tv_sec - (t2.tv_sec - 1);
167 tmp.tv_nsec = t1.tv_nsec - (t2.tv_nsec + OneSecAsNsecs);
168 return normalizedTimespec(tmp);
169}
170constexpr inline timespec operator*(const timespec &t1, int mul)
171{
172 timespec tmp = {};
173 tmp.tv_sec = t1.tv_sec * mul;
174 tmp.tv_nsec = t1.tv_nsec * mul;
175 return normalizedTimespec(tmp);
176}
177inline timeval timespecToTimeval(timespec ts)
178{
179 timeval tv;
180 tv.tv_sec = ts.tv_sec;
181 tv.tv_usec = ts.tv_nsec / 1000;
182 return tv;
183}
184
185inline timespec &operator+=(timespec &t1, std::chrono::milliseconds msecs)
186{
187 t1 += durationToTimespec(msecs);
188 return t1;
189}
190
191inline timespec &operator+=(timespec &t1, int ms)
192{
193 t1 += std::chrono::milliseconds{ms};
194 return t1;
195}
196
197inline timespec operator+(const timespec &t1, std::chrono::milliseconds msecs)
198{
199 timespec tmp = t1;
200 tmp += msecs;
201 return tmp;
202}
203
204inline timespec operator+(const timespec &t1, int ms)
205{
206 return t1 + std::chrono::milliseconds{ms};
207}
208
209inline timespec qAbsTimespec(timespec ts)
210{
211 if (ts.tv_sec < 0) {
212 ts.tv_sec = -ts.tv_sec - 1;
213 ts.tv_nsec -= OneSecAsNsecs;
214 }
215 if (ts.tv_sec == 0 && ts.tv_nsec < 0) {
216 ts.tv_nsec = -ts.tv_nsec;
217 }
218 return normalizedTimespec(ts);
219}
220
221template <clockid_t ClockId = SteadyClockClockId>
222inline timespec deadlineToAbstime(QDeadlineTimer deadline)
223{
224 using namespace std::chrono;
225 using Clock =
226 std::conditional_t<ClockId == CLOCK_REALTIME, system_clock, steady_clock>;
227 auto timePoint = deadline.deadline<Clock>();
228 if (timePoint < typename Clock::time_point{})
229 return {};
230 return durationToTimespec(timePoint.time_since_epoch());
231}
232
233Q_CORE_EXPORT void qt_ignore_sigpipe() noexcept;
234
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);
238# undef QT_OPEN
239# define QT_OPEN qt_open64
240# endif
241#endif
242
243#ifdef AT_FDCWD
244static inline int qt_safe_openat(int dfd, const char *pathname, int flags, mode_t mode = 0777)
245{
246 // everyone already has O_CLOEXEC
247 int fd;
248 QT_EINTR_LOOP(fd, openat(dfd, pathname, flags | O_CLOEXEC, mode));
249 return fd;
250}
251#endif
252
253// don't call QT_OPEN or ::open
254// call qt_safe_open
255static inline int qt_safe_open(const char *pathname, int flags, mode_t mode = 0777)
256{
257#ifdef O_CLOEXEC
258 flags |= O_CLOEXEC;
259#endif
260 int fd;
261 QT_EINTR_LOOP(fd, QT_OPEN(pathname, flags, mode));
262
263#ifndef O_CLOEXEC
264 if (fd != -1)
265 ::fcntl(fd, F_SETFD, FD_CLOEXEC);
266#endif
267
268 return fd;
269}
270#undef QT_OPEN
271#define QT_OPEN qt_safe_open
272
273// don't call ::pipe
274// call qt_safe_pipe
275static inline int qt_safe_pipe(int pipefd[2], int flags = 0)
276{
277 Q_ASSERT((flags & ~O_NONBLOCK) == 0);
278
279#ifdef QT_THREADSAFE_CLOEXEC
280 // use pipe2
281 flags |= O_CLOEXEC;
282 return ::pipe2(pipefd, flags); // pipe2 is documented not to return EINTR
283#else
284 int ret = ::pipe(pipefd);
285 if (ret == -1)
286 return -1;
287
288 ::fcntl(pipefd[0], F_SETFD, FD_CLOEXEC);
289 ::fcntl(pipefd[1], F_SETFD, FD_CLOEXEC);
290
291 // set non-block too?
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);
295 }
296
297 return 0;
298#endif
299}
300
301// don't call dup or fcntl(F_DUPFD)
302static inline int qt_safe_dup(int oldfd, int atleast = 0, int flags = FD_CLOEXEC)
303{
304 Q_ASSERT(flags == FD_CLOEXEC || flags == 0);
305
306#ifdef F_DUPFD_CLOEXEC
307 int cmd = F_DUPFD;
308 if (flags & FD_CLOEXEC)
309 cmd = F_DUPFD_CLOEXEC;
310 return ::fcntl(oldfd, cmd, atleast);
311#else
312 // use F_DUPFD
313 int ret = ::fcntl(oldfd, F_DUPFD, atleast);
314
315 if (flags && ret != -1)
316 ::fcntl(ret, F_SETFD, flags);
317 return ret;
318#endif
319}
320
321// don't call dup2
322// call qt_safe_dup2
323static inline int qt_safe_dup2(int oldfd, int newfd, int flags = FD_CLOEXEC)
324{
325 Q_ASSERT(flags == FD_CLOEXEC || flags == 0);
326
327 int ret;
328#if QT_CONFIG(dup3)
329 // use dup3
330 QT_EINTR_LOOP(ret, ::dup3(oldfd, newfd, flags ? O_CLOEXEC : 0));
331 return ret;
332#else
333 QT_EINTR_LOOP(ret, ::dup2(oldfd, newfd));
334 if (ret == -1)
335 return -1;
336
337 if (flags)
338 ::fcntl(newfd, F_SETFD, flags);
339 return 0;
340#endif
341}
342
343static inline qint64 qt_safe_read(int fd, void *data, qint64 maxlen)
344{
345 qint64 ret = 0;
346 QT_EINTR_LOOP(ret, QT_READ(fd, data, maxlen));
347 return ret;
348}
349#undef QT_READ
350#define QT_READ qt_safe_read
351
352static inline qint64 qt_safe_write(int fd, const void *data, qint64 len)
353{
354 qint64 ret = 0;
355 QT_EINTR_LOOP(ret, QT_WRITE(fd, data, len));
356 return ret;
357}
358#undef QT_WRITE
359#define QT_WRITE qt_safe_write
360
361static inline qint64 qt_safe_write_nosignal(int fd, const void *data, qint64 len)
362{
363 qt_ignore_sigpipe();
364 return qt_safe_write(fd, data, len);
365}
366
367static inline int qt_safe_close(int fd)
368{
369 int ret;
370 QT_EINTR_LOOP(ret, QT_CLOSE(fd));
371 return ret;
372}
373#undef QT_CLOSE
374#define QT_CLOSE qt_safe_close
375
376// - VxWorks & iOS/tvOS/watchOS don't have processes
377#if QT_CONFIG(process)
378static inline int qt_safe_execve(const char *filename, char *const argv[],
379 char *const envp[])
380{
381 int ret;
382 QT_EINTR_LOOP(ret, ::execve(filename, argv, envp));
383 return ret;
384}
385
386static inline int qt_safe_execv(const char *path, char *const argv[])
387{
388 int ret;
389 QT_EINTR_LOOP(ret, ::execv(path, argv));
390 return ret;
391}
392
393static inline int qt_safe_execvp(const char *file, char *const argv[])
394{
395 int ret;
396 QT_EINTR_LOOP(ret, ::execvp(file, argv));
397 return ret;
398}
399
400static inline pid_t qt_safe_waitpid(pid_t pid, int *status, int options)
401{
402 int ret;
403 QT_EINTR_LOOP(ret, ::waitpid(pid, status, options));
404 return ret;
405}
406#endif // QT_CONFIG(process)
407
408#if !defined(_POSIX_MONOTONIC_CLOCK)
409# define _POSIX_MONOTONIC_CLOCK -1
410#endif
411
412QByteArray qt_readlink(const char *path);
413
414/* non-static */
416{
417#ifdef Q_OS_LINUX
418# ifdef QT_LINUX_ALWAYS_HAVE_PROCFS
419 return true;
420# else
421 static const bool present = (access("/proc/version", F_OK) == 0);
422 return present;
423# endif
424#else
425 return false;
426#endif
427}
428
429Q_CORE_EXPORT int qt_safe_poll(struct pollfd *fds, nfds_t nfds, QDeadlineTimer deadline);
430
431static inline struct pollfd qt_make_pollfd(int fd, short events)
432{
433 struct pollfd pfd = { fd, events, 0 };
434 return pfd;
435}
436
437// according to X/OPEN we have to define semun ourselves
438// we use prefix as on some systems sem.h will have it
439struct semid_ds;
440union qt_semun {
441 int val; /* value for SETVAL */
442 struct semid_ds *buf; /* buffer for IPC_STAT, IPC_SET */
443 unsigned short *array; /* array for GETALL, SETALL */
444};
445
446QT_END_NAMESPACE
447
448#endif
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)
Definition qpoll.cpp:136
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
#define PATH_MAX
struct semid_ds * buf
unsigned short * array