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