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 QSteadyClockClockId =
70#if !defined(CLOCK_MONOTONIC)
71 // we don't know how to set the monotonic clock
73#elif defined(_LIBCPP_VERSION)
74 && (defined(_LIBCPP_HAS_NO_MONOTONIC_CLOCK)
75 || (defined(_LIBCPP_HAS_MONOTONIC_CLOCK) && !_LIBCPP_HAS_MONOTONIC_CLOCK))
76 // libc++ falling back to system_clock (the macro was inverted in LLVM 20)
78#elif defined(__GLIBCXX__) && !defined(_GLIBCXX_USE_CLOCK_MONOTONIC)
79 // libstdc++ falling back to system_clock
81#elif defined(_LIBCPP_VERSION) && defined(Q_OS_DARWIN)
82 // on Apple systems, libc++ uses CLOCK_MONOTONIC_RAW since LLVM 11
83 // https://github.com/llvm/llvm-project/blob/llvmorg-11.0.0/libcxx/src/chrono.cpp#L117-L129
85#elif defined(__GLIBCXX__) || defined(_LIBCPP_VERSION)
86 // both libstdc++ and libc++ otherwise use CLOCK_MONOTONIC
87 CLOCK_MONOTONIC
88#else
89# warning "Unknown C++ Standard Library implementation - code may be sub-optimal"
90 CLOCK_REALTIME
91#endif
92 ;
93
94static inline constexpr clockid_t QWaitConditionClockId =
95#if !QT_CONFIG(thread)
96 // bootstrap mode, there are no wait conditions
98#elif !QT_CONFIG(pthread_condattr_setclock)
99 // OSes that lack pthread_condattr_setclock() (e.g., Darwin)
101#elif defined(Q_OS_QNX)
102 // unknown why use of the monotonic clock causes failures
104#else
106#endif
107 ;
108
109static constexpr auto OneSecAsNsecs = std::chrono::nanoseconds(std::chrono::seconds{ 1 }).count();
110
111inline timespec durationToTimespec(std::chrono::nanoseconds timeout) noexcept
112{
113 using namespace std::chrono;
114 const seconds secs = duration_cast<seconds>(timeout);
115 const nanoseconds frac = timeout - secs;
116 struct timespec ts;
117 ts.tv_sec = secs.count();
118 ts.tv_nsec = frac.count();
119 return ts;
120}
121
122template <typename Duration>
123inline Duration timespecToChrono(timespec ts) noexcept
124{
125 using namespace std::chrono;
126 return duration_cast<Duration>(seconds{ts.tv_sec} + nanoseconds{ts.tv_nsec});
127}
128
129// Internal operator functions for timespecs
130constexpr inline timespec &normalizedTimespec(timespec &t)
131{
132 while (t.tv_nsec >= OneSecAsNsecs) {
133 ++t.tv_sec;
134 t.tv_nsec -= OneSecAsNsecs;
135 }
136 while (t.tv_nsec < 0) {
137 --t.tv_sec;
138 t.tv_nsec += OneSecAsNsecs;
139 }
140 return t;
141}
142
143inline timeval timespecToTimeval(timespec ts)
144{
145 timeval tv;
146 tv.tv_sec = ts.tv_sec;
147 tv.tv_usec = ts.tv_nsec / 1000;
148 return tv;
149}
150
151template <clockid_t ClockId = QSteadyClockClockId>
152inline timespec deadlineToAbstime(QDeadlineTimer deadline)
153{
154 using namespace std::chrono;
155 using Clock =
156 std::conditional_t<ClockId == CLOCK_REALTIME, system_clock, steady_clock>;
157 auto timePoint = deadline.deadline<Clock>();
158 if (timePoint < typename Clock::time_point{})
159 return {};
160 return durationToTimespec(timePoint.time_since_epoch());
161}
162
163Q_CORE_EXPORT void qt_ignore_sigpipe() noexcept;
164
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);
168# undef QT_OPEN
169# define QT_OPEN qt_open64
170# endif
171#endif
172
173#ifdef AT_FDCWD
174static inline int qt_safe_openat(int dfd, const char *pathname, int flags, mode_t mode = 0777)
175{
176 // everyone already has O_CLOEXEC
177 int fd;
178 QT_EINTR_LOOP(fd, openat(dfd, pathname, flags | O_CLOEXEC, mode));
179 return fd;
180}
181#endif
182
183// don't call QT_OPEN or ::open
184// call qt_safe_open
185static inline int qt_safe_open(const char *pathname, int flags, mode_t mode = 0777)
186{
187#ifdef O_CLOEXEC
188 flags |= O_CLOEXEC;
189#endif
190 int fd;
191 QT_EINTR_LOOP(fd, QT_OPEN(pathname, flags, mode));
192
193#ifndef O_CLOEXEC
194 if (fd != -1)
195 ::fcntl(fd, F_SETFD, FD_CLOEXEC);
196#endif
197
198 return fd;
199}
200#undef QT_OPEN
201#define QT_OPEN qt_safe_open
202
203// don't call ::pipe
204// call qt_safe_pipe
205static inline int qt_safe_pipe(int pipefd[2], int flags = 0)
206{
207 Q_ASSERT((flags & ~O_NONBLOCK) == 0);
208
209#ifdef QT_THREADSAFE_CLOEXEC
210 // use pipe2
211 flags |= O_CLOEXEC;
212 return ::pipe2(pipefd, flags); // pipe2 is documented not to return EINTR
213#else
214 int ret = ::pipe(pipefd);
215 if (ret == -1)
216 return -1;
217
218 ::fcntl(pipefd[0], F_SETFD, FD_CLOEXEC);
219 ::fcntl(pipefd[1], F_SETFD, FD_CLOEXEC);
220
221 // set non-block too?
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);
225 }
226
227 return 0;
228#endif
229}
230
231// don't call dup or fcntl(F_DUPFD)
232static inline int qt_safe_dup(int oldfd, int atleast = 0, int flags = FD_CLOEXEC)
233{
234 Q_ASSERT(flags == FD_CLOEXEC || flags == 0);
235
236#ifdef F_DUPFD_CLOEXEC
237 int cmd = F_DUPFD;
238 if (flags & FD_CLOEXEC)
239 cmd = F_DUPFD_CLOEXEC;
240 return ::fcntl(oldfd, cmd, atleast);
241#else
242 // use F_DUPFD
243 int ret = ::fcntl(oldfd, F_DUPFD, atleast);
244
245 if (flags && ret != -1)
246 ::fcntl(ret, F_SETFD, flags);
247 return ret;
248#endif
249}
250
251// don't call dup2
252// call qt_safe_dup2
253static inline int qt_safe_dup2(int oldfd, int newfd, int flags = FD_CLOEXEC)
254{
255 Q_ASSERT(flags == FD_CLOEXEC || flags == 0);
256
257 int ret;
258#if QT_CONFIG(dup3)
259 // use dup3
260 QT_EINTR_LOOP(ret, ::dup3(oldfd, newfd, flags ? O_CLOEXEC : 0));
261 return ret;
262#else
263 QT_EINTR_LOOP(ret, ::dup2(oldfd, newfd));
264 if (ret == -1)
265 return -1;
266
267 if (flags)
268 ::fcntl(newfd, F_SETFD, flags);
269 return 0;
270#endif
271}
272
273static inline qint64 qt_safe_read(int fd, void *data, qint64 maxlen)
274{
275 qint64 ret = 0;
276 QT_EINTR_LOOP(ret, QT_READ(fd, data, maxlen));
277 return ret;
278}
279#undef QT_READ
280#define QT_READ qt_safe_read
281
282static inline qint64 qt_safe_write(int fd, const void *data, qint64 len)
283{
284 qint64 ret = 0;
285 QT_EINTR_LOOP(ret, QT_WRITE(fd, data, len));
286 return ret;
287}
288#undef QT_WRITE
289#define QT_WRITE qt_safe_write
290
291static inline qint64 qt_safe_write_nosignal(int fd, const void *data, qint64 len)
292{
293 qt_ignore_sigpipe();
294 return qt_safe_write(fd, data, len);
295}
296
297static inline int qt_safe_close(int fd)
298{
299 int ret;
300 QT_EINTR_LOOP(ret, QT_CLOSE(fd));
301 return ret;
302}
303#undef QT_CLOSE
304#define QT_CLOSE qt_safe_close
305
306// - VxWorks & iOS/tvOS/watchOS don't have processes
307#if QT_CONFIG(process)
308static inline int qt_safe_execve(const char *filename, char *const argv[],
309 char *const envp[])
310{
311 int ret;
312 QT_EINTR_LOOP(ret, ::execve(filename, argv, envp));
313 return ret;
314}
315
316static inline int qt_safe_execv(const char *path, char *const argv[])
317{
318 int ret;
319 QT_EINTR_LOOP(ret, ::execv(path, argv));
320 return ret;
321}
322
323static inline int qt_safe_execvp(const char *file, char *const argv[])
324{
325 int ret;
326 QT_EINTR_LOOP(ret, ::execvp(file, argv));
327 return ret;
328}
329
330static inline pid_t qt_safe_waitpid(pid_t pid, int *status, int options)
331{
332 int ret;
333 QT_EINTR_LOOP(ret, ::waitpid(pid, status, options));
334 return ret;
335}
336#endif // QT_CONFIG(process)
337
338#if !defined(_POSIX_MONOTONIC_CLOCK)
339# define _POSIX_MONOTONIC_CLOCK -1
340#endif
341
342QByteArray qt_readlink(const char *path);
343
344/* non-static */
346{
347#ifdef Q_OS_LINUX
348# ifdef QT_LINUX_ALWAYS_HAVE_PROCFS
349 return true;
350# else
351 static const bool present = (access("/proc/version", F_OK) == 0);
352 return present;
353# endif
354#else
355 return false;
356#endif
357}
358
359Q_CORE_EXPORT int qt_safe_poll(struct pollfd *fds, nfds_t nfds, QDeadlineTimer deadline);
360
361static inline struct pollfd qt_make_pollfd(int fd, short events)
362{
363 struct pollfd pfd = { fd, events, 0 };
364 return pfd;
365}
366
367// according to X/OPEN we have to define semun ourselves
368// we use prefix as on some systems sem.h will have it
369struct semid_ds;
370union qt_semun {
371 int val; /* value for SETVAL */
372 struct semid_ds *buf; /* buffer for IPC_STAT, IPC_SET */
373 unsigned short *array; /* array for GETALL, SETALL */
374};
375
376QT_END_NAMESPACE
377
378#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)
#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)
#define PATH_MAX
struct semid_ds * buf
unsigned short * array