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
64QT_BEGIN_NAMESPACE
65
66Q_DECLARE_TYPEINFO(pollfd, Q_PRIMITIVE_TYPE);
67
68static constexpr auto OneSecAsNsecs = std::chrono::nanoseconds(std::chrono::seconds{ 1 }).count();
69
70inline timespec durationToTimespec(std::chrono::nanoseconds timeout) noexcept
71{
72 using namespace std::chrono;
73 const seconds secs = duration_cast<seconds>(d: timeout);
74 const nanoseconds frac = timeout - secs;
75 struct timespec ts;
76 ts.tv_sec = secs.count();
77 ts.tv_nsec = frac.count();
78 return ts;
79}
80
81template <typename Duration>
82inline Duration timespecToChrono(timespec ts) noexcept
83{
84 using namespace std::chrono;
85 return duration_cast<Duration>(seconds{ts.tv_sec} + nanoseconds{ts.tv_nsec});
86}
87
88inline std::chrono::milliseconds timespecToChronoMs(timespec ts) noexcept
89{
90 return timespecToChrono<std::chrono::milliseconds>(ts);
91}
92
93// Internal operator functions for timespecs
94constexpr inline timespec &normalizedTimespec(timespec &t)
95{
96 while (t.tv_nsec >= OneSecAsNsecs) {
97 ++t.tv_sec;
98 t.tv_nsec -= OneSecAsNsecs;
99 }
100 while (t.tv_nsec < 0) {
101 --t.tv_sec;
102 t.tv_nsec += OneSecAsNsecs;
103 }
104 return t;
105}
106constexpr inline bool operator<(const timespec &t1, const timespec &t2)
107{ return t1.tv_sec < t2.tv_sec || (t1.tv_sec == t2.tv_sec && t1.tv_nsec < t2.tv_nsec); }
108constexpr inline bool operator==(const timespec &t1, const timespec &t2)
109{ return t1.tv_sec == t2.tv_sec && t1.tv_nsec == t2.tv_nsec; }
110constexpr inline bool operator!=(const timespec &t1, const timespec &t2)
111{ return !(t1 == t2); }
112constexpr inline timespec &operator+=(timespec &t1, const timespec &t2)
113{
114 t1.tv_sec += t2.tv_sec;
115 t1.tv_nsec += t2.tv_nsec;
116 return normalizedTimespec(t&: t1);
117}
118constexpr inline timespec operator+(const timespec &t1, const timespec &t2)
119{
120 timespec tmp = {};
121 tmp.tv_sec = t1.tv_sec + t2.tv_sec;
122 tmp.tv_nsec = t1.tv_nsec + t2.tv_nsec;
123 return normalizedTimespec(t&: tmp);
124}
125constexpr inline timespec operator-(const timespec &t1, const timespec &t2)
126{
127 timespec tmp = {};
128 tmp.tv_sec = t1.tv_sec - (t2.tv_sec - 1);
129 tmp.tv_nsec = t1.tv_nsec - (t2.tv_nsec + OneSecAsNsecs);
130 return normalizedTimespec(t&: tmp);
131}
132constexpr inline timespec operator*(const timespec &t1, int mul)
133{
134 timespec tmp = {};
135 tmp.tv_sec = t1.tv_sec * mul;
136 tmp.tv_nsec = t1.tv_nsec * mul;
137 return normalizedTimespec(t&: tmp);
138}
139inline timeval timespecToTimeval(timespec ts)
140{
141 timeval tv;
142 tv.tv_sec = ts.tv_sec;
143 tv.tv_usec = ts.tv_nsec / 1000;
144 return tv;
145}
146
147inline timespec &operator+=(timespec &t1, std::chrono::milliseconds msecs)
148{
149 t1 += durationToTimespec(timeout: msecs);
150 return t1;
151}
152
153inline timespec &operator+=(timespec &t1, int ms)
154{
155 t1 += std::chrono::milliseconds{ms};
156 return t1;
157}
158
159inline timespec operator+(const timespec &t1, std::chrono::milliseconds msecs)
160{
161 timespec tmp = t1;
162 tmp += msecs;
163 return tmp;
164}
165
166inline timespec operator+(const timespec &t1, int ms)
167{
168 return t1 + std::chrono::milliseconds{ms};
169}
170
171inline timespec qAbsTimespec(timespec ts)
172{
173 if (ts.tv_sec < 0) {
174 ts.tv_sec = -ts.tv_sec - 1;
175 ts.tv_nsec -= OneSecAsNsecs;
176 }
177 if (ts.tv_sec == 0 && ts.tv_nsec < 0) {
178 ts.tv_nsec = -ts.tv_nsec;
179 }
180 return normalizedTimespec(t&: ts);
181}
182
183Q_CORE_EXPORT void qt_ignore_sigpipe() noexcept;
184
185#if defined(Q_PROCESSOR_X86_32) && defined(__GLIBC__)
186# if !__GLIBC_PREREQ(2, 22)
187Q_CORE_EXPORT int qt_open64(const char *pathname, int flags, mode_t);
188# undef QT_OPEN
189# define QT_OPEN qt_open64
190# endif
191#endif
192
193#ifdef AT_FDCWD
194static inline int qt_safe_openat(int dfd, const char *pathname, int flags, mode_t mode = 0777)
195{
196 // everyone already has O_CLOEXEC
197 int fd;
198 QT_EINTR_LOOP(fd, openat(dfd, pathname, flags | O_CLOEXEC, mode));
199 return fd;
200}
201#endif
202
203// don't call QT_OPEN or ::open
204// call qt_safe_open
205static inline int qt_safe_open(const char *pathname, int flags, mode_t mode = 0777)
206{
207#ifdef O_CLOEXEC
208 flags |= O_CLOEXEC;
209#endif
210 int fd;
211 QT_EINTR_LOOP(fd, QT_OPEN(pathname, flags, mode));
212
213#ifndef O_CLOEXEC
214 if (fd != -1)
215 ::fcntl(fd, F_SETFD, FD_CLOEXEC);
216#endif
217
218 return fd;
219}
220#undef QT_OPEN
221#define QT_OPEN qt_safe_open
222
223#ifndef Q_OS_VXWORKS // no POSIX pipes in VxWorks
224// don't call ::pipe
225// call qt_safe_pipe
226static inline int qt_safe_pipe(int pipefd[2], int flags = 0)
227{
228 Q_ASSERT((flags & ~O_NONBLOCK) == 0);
229
230#ifdef QT_THREADSAFE_CLOEXEC
231 // use pipe2
232 flags |= O_CLOEXEC;
233 return ::pipe2(pipedes: pipefd, flags: flags); // pipe2 is documented not to return EINTR
234#else
235 int ret = ::pipe(pipefd);
236 if (ret == -1)
237 return -1;
238
239 ::fcntl(pipefd[0], F_SETFD, FD_CLOEXEC);
240 ::fcntl(pipefd[1], F_SETFD, FD_CLOEXEC);
241
242 // set non-block too?
243 if (flags & O_NONBLOCK) {
244 ::fcntl(pipefd[0], F_SETFL, ::fcntl(pipefd[0], F_GETFL) | O_NONBLOCK);
245 ::fcntl(pipefd[1], F_SETFL, ::fcntl(pipefd[1], F_GETFL) | O_NONBLOCK);
246 }
247
248 return 0;
249#endif
250}
251
252#endif // Q_OS_VXWORKS
253
254// don't call dup or fcntl(F_DUPFD)
255static inline int qt_safe_dup(int oldfd, int atleast = 0, int flags = FD_CLOEXEC)
256{
257 Q_ASSERT(flags == FD_CLOEXEC || flags == 0);
258
259#ifdef F_DUPFD_CLOEXEC
260 int cmd = F_DUPFD;
261 if (flags & FD_CLOEXEC)
262 cmd = F_DUPFD_CLOEXEC;
263 return ::fcntl(fd: oldfd, cmd: cmd, atleast);
264#else
265 // use F_DUPFD
266 int ret = ::fcntl(oldfd, F_DUPFD, atleast);
267
268 if (flags && ret != -1)
269 ::fcntl(ret, F_SETFD, flags);
270 return ret;
271#endif
272}
273
274// don't call dup2
275// call qt_safe_dup2
276static inline int qt_safe_dup2(int oldfd, int newfd, int flags = FD_CLOEXEC)
277{
278 Q_ASSERT(flags == FD_CLOEXEC || flags == 0);
279
280 int ret;
281#ifdef QT_THREADSAFE_CLOEXEC
282 // use dup3
283 QT_EINTR_LOOP(ret, ::dup3(oldfd, newfd, flags ? O_CLOEXEC : 0));
284 return ret;
285#else
286 QT_EINTR_LOOP(ret, ::dup2(oldfd, newfd));
287 if (ret == -1)
288 return -1;
289
290 if (flags)
291 ::fcntl(newfd, F_SETFD, flags);
292 return 0;
293#endif
294}
295
296static inline qint64 qt_safe_read(int fd, void *data, qint64 maxlen)
297{
298 qint64 ret = 0;
299 QT_EINTR_LOOP(ret, QT_READ(fd, data, maxlen));
300 return ret;
301}
302#undef QT_READ
303#define QT_READ qt_safe_read
304
305static inline qint64 qt_safe_write(int fd, const void *data, qint64 len)
306{
307 qint64 ret = 0;
308 QT_EINTR_LOOP(ret, QT_WRITE(fd, data, len));
309 return ret;
310}
311#undef QT_WRITE
312#define QT_WRITE qt_safe_write
313
314static inline qint64 qt_safe_write_nosignal(int fd, const void *data, qint64 len)
315{
316 qt_ignore_sigpipe();
317 return qt_safe_write(fd, data, len);
318}
319
320static inline int qt_safe_close(int fd)
321{
322 int ret;
323 QT_EINTR_LOOP(ret, QT_CLOSE(fd));
324 return ret;
325}
326#undef QT_CLOSE
327#define QT_CLOSE qt_safe_close
328
329// - VxWorks & iOS/tvOS/watchOS don't have processes
330#if QT_CONFIG(process)
331static inline int qt_safe_execve(const char *filename, char *const argv[],
332 char *const envp[])
333{
334 int ret;
335 QT_EINTR_LOOP(ret, ::execve(filename, argv, envp));
336 return ret;
337}
338
339static inline int qt_safe_execv(const char *path, char *const argv[])
340{
341 int ret;
342 QT_EINTR_LOOP(ret, ::execv(path, argv));
343 return ret;
344}
345
346static inline int qt_safe_execvp(const char *file, char *const argv[])
347{
348 int ret;
349 QT_EINTR_LOOP(ret, ::execvp(file, argv));
350 return ret;
351}
352
353static inline pid_t qt_safe_waitpid(pid_t pid, int *status, int options)
354{
355 int ret;
356 QT_EINTR_LOOP(ret, ::waitpid(pid, status, options));
357 return ret;
358}
359#endif // QT_CONFIG(process)
360
361#if !defined(_POSIX_MONOTONIC_CLOCK)
362# define _POSIX_MONOTONIC_CLOCK -1
363#endif
364
365QByteArray qt_readlink(const char *path);
366
367/* non-static */
368inline bool qt_haveLinuxProcfs()
369{
370#ifdef Q_OS_LINUX
371# ifdef QT_LINUX_ALWAYS_HAVE_PROCFS
372 return true;
373# else
374 static const bool present = (access(name: "/proc/version", F_OK) == 0);
375 return present;
376# endif
377#else
378 return false;
379#endif
380}
381
382Q_CORE_EXPORT int qt_safe_poll(struct pollfd *fds, nfds_t nfds, QDeadlineTimer deadline);
383
384static inline struct pollfd qt_make_pollfd(int fd, short events)
385{
386 struct pollfd pfd = { .fd: fd, .events: events, .revents: 0 };
387 return pfd;
388}
389
390// according to X/OPEN we have to define semun ourselves
391// we use prefix as on some systems sem.h will have it
392struct semid_ds;
393union qt_semun {
394 int val; /* value for SETVAL */
395 struct semid_ds *buf; /* buffer for IPC_STAT, IPC_SET */
396 unsigned short *array; /* array for GETALL, SETALL */
397};
398
399QT_END_NAMESPACE
400
401#endif
402

Provided by KDAB

Privacy Policy
Learn Advanced QML with KDAB
Find out more

source code of qtbase/src/corelib/kernel/qcore_unix_p.h