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