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) && 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
127// Internal operator functions for timespecs
128constexpr inline timespec &normalizedTimespec(timespec &t)
129{
130 while (t.tv_nsec >= OneSecAsNsecs) {
131 ++t.tv_sec;
132 t.tv_nsec -= OneSecAsNsecs;
133 }
134 while (t.tv_nsec < 0) {
135 --t.tv_sec;
136 t.tv_nsec += OneSecAsNsecs;
137 }
138 return t;
139}
140
141inline timeval timespecToTimeval(timespec ts)
142{
143 timeval tv;
144 tv.tv_sec = ts.tv_sec;
145 tv.tv_usec = ts.tv_nsec / 1000;
146 return tv;
147}
148
149template <clockid_t ClockId = QSteadyClockClockId>
150inline timespec deadlineToAbstime(QDeadlineTimer deadline)
151{
152 using namespace std::chrono;
153 using Clock =
154 std::conditional_t<ClockId == CLOCK_REALTIME, system_clock, steady_clock>;
155 auto timePoint = deadline.deadline<Clock>();
156 if (timePoint < typename Clock::time_point{})
157 return {};
158 return durationToTimespec(timePoint.time_since_epoch());
159}
160
161Q_CORE_EXPORT void qt_ignore_sigpipe() noexcept;
162
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);
166# undef QT_OPEN
167# define QT_OPEN qt_open64
168# endif
169#endif
170
171#ifdef AT_FDCWD
172static inline int qt_safe_openat(int dfd, const char *pathname, int flags, mode_t mode = 0777)
173{
174 // everyone already has O_CLOEXEC
175 int fd;
176 QT_EINTR_LOOP(fd, openat(dfd, pathname, flags | O_CLOEXEC, mode));
177 return fd;
178}
179#endif
180
181// don't call QT_OPEN or ::open
182// call qt_safe_open
183static inline int qt_safe_open(const char *pathname, int flags, mode_t mode = 0777)
184{
185#ifdef O_CLOEXEC
186 flags |= O_CLOEXEC;
187#endif
188 int fd;
189 QT_EINTR_LOOP(fd, QT_OPEN(pathname, flags, mode));
190
191#ifndef O_CLOEXEC
192 if (fd != -1)
193 ::fcntl(fd, F_SETFD, FD_CLOEXEC);
194#endif
195
196 return fd;
197}
198#undef QT_OPEN
199#define QT_OPEN qt_safe_open
200
201// don't call ::pipe
202// call qt_safe_pipe
203static inline int qt_safe_pipe(int pipefd[2], int flags = 0)
204{
205 Q_ASSERT((flags & ~O_NONBLOCK) == 0);
206
207#ifdef QT_THREADSAFE_CLOEXEC
208 // use pipe2
209 flags |= O_CLOEXEC;
210 return ::pipe2(pipefd, flags); // pipe2 is documented not to return EINTR
211#else
212 int ret = ::pipe(pipefd);
213 if (ret == -1)
214 return -1;
215
216 ::fcntl(pipefd[0], F_SETFD, FD_CLOEXEC);
217 ::fcntl(pipefd[1], F_SETFD, FD_CLOEXEC);
218
219 // set non-block too?
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);
223 }
224
225 return 0;
226#endif
227}
228
229// don't call dup or fcntl(F_DUPFD)
230static inline int qt_safe_dup(int oldfd, int atleast = 0, int flags = FD_CLOEXEC)
231{
232 Q_ASSERT(flags == FD_CLOEXEC || flags == 0);
233
234#ifdef F_DUPFD_CLOEXEC
235 int cmd = F_DUPFD;
236 if (flags & FD_CLOEXEC)
237 cmd = F_DUPFD_CLOEXEC;
238 return ::fcntl(oldfd, cmd, atleast);
239#else
240 // use F_DUPFD
241 int ret = ::fcntl(oldfd, F_DUPFD, atleast);
242
243 if (flags && ret != -1)
244 ::fcntl(ret, F_SETFD, flags);
245 return ret;
246#endif
247}
248
249// don't call dup2
250// call qt_safe_dup2
251static inline int qt_safe_dup2(int oldfd, int newfd, int flags = FD_CLOEXEC)
252{
253 Q_ASSERT(flags == FD_CLOEXEC || flags == 0);
254
255 int ret;
256#if QT_CONFIG(dup3)
257 // use dup3
258 QT_EINTR_LOOP(ret, ::dup3(oldfd, newfd, flags ? O_CLOEXEC : 0));
259 return ret;
260#else
261 QT_EINTR_LOOP(ret, ::dup2(oldfd, newfd));
262 if (ret == -1)
263 return -1;
264
265 if (flags)
266 ::fcntl(newfd, F_SETFD, flags);
267 return 0;
268#endif
269}
270
271static inline qint64 qt_safe_read(int fd, void *data, qint64 maxlen)
272{
273 qint64 ret = 0;
274 QT_EINTR_LOOP(ret, QT_READ(fd, data, maxlen));
275 return ret;
276}
277#undef QT_READ
278#define QT_READ qt_safe_read
279
280static inline qint64 qt_safe_write(int fd, const void *data, qint64 len)
281{
282 qint64 ret = 0;
283 QT_EINTR_LOOP(ret, QT_WRITE(fd, data, len));
284 return ret;
285}
286#undef QT_WRITE
287#define QT_WRITE qt_safe_write
288
289static inline qint64 qt_safe_write_nosignal(int fd, const void *data, qint64 len)
290{
291 qt_ignore_sigpipe();
292 return qt_safe_write(fd, data, len);
293}
294
295static inline int qt_safe_close(int fd)
296{
297 int ret;
298 QT_EINTR_LOOP(ret, QT_CLOSE(fd));
299 return ret;
300}
301#undef QT_CLOSE
302#define QT_CLOSE qt_safe_close
303
304// - VxWorks & iOS/tvOS/watchOS don't have processes
305#if QT_CONFIG(process)
306static inline int qt_safe_execve(const char *filename, char *const argv[],
307 char *const envp[])
308{
309 int ret;
310 QT_EINTR_LOOP(ret, ::execve(filename, argv, envp));
311 return ret;
312}
313
314static inline int qt_safe_execv(const char *path, char *const argv[])
315{
316 int ret;
317 QT_EINTR_LOOP(ret, ::execv(path, argv));
318 return ret;
319}
320
321static inline int qt_safe_execvp(const char *file, char *const argv[])
322{
323 int ret;
324 QT_EINTR_LOOP(ret, ::execvp(file, argv));
325 return ret;
326}
327
328static inline pid_t qt_safe_waitpid(pid_t pid, int *status, int options)
329{
330 int ret;
331 QT_EINTR_LOOP(ret, ::waitpid(pid, status, options));
332 return ret;
333}
334#endif // QT_CONFIG(process)
335
336#if !defined(_POSIX_MONOTONIC_CLOCK)
337# define _POSIX_MONOTONIC_CLOCK -1
338#endif
339
340QByteArray qt_readlink(const char *path);
341
342/* non-static */
344{
345#ifdef Q_OS_LINUX
346# ifdef QT_LINUX_ALWAYS_HAVE_PROCFS
347 return true;
348# else
349 static const bool present = (access("/proc/version", F_OK) == 0);
350 return present;
351# endif
352#else
353 return false;
354#endif
355}
356
357Q_CORE_EXPORT int qt_safe_poll(struct pollfd *fds, nfds_t nfds, QDeadlineTimer deadline);
358
359static inline struct pollfd qt_make_pollfd(int fd, short events)
360{
361 struct pollfd pfd = { fd, events, 0 };
362 return pfd;
363}
364
365// according to X/OPEN we have to define semun ourselves
366// we use prefix as on some systems sem.h will have it
367struct semid_ds;
368union qt_semun {
369 int val; /* value for SETVAL */
370 struct semid_ds *buf; /* buffer for IPC_STAT, IPC_SET */
371 unsigned short *array; /* array for GETALL, SETALL */
372};
373
374QT_END_NAMESPACE
375
376#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