1 | /**************************************************************************** |
2 | ** |
3 | ** Copyright (C) 2016 The Qt Company Ltd. |
4 | ** Copyright (C) 2016 Intel Corporation. |
5 | ** Contact: https://www.qt.io/licensing/ |
6 | ** |
7 | ** This file is part of the QtCore module of the Qt Toolkit. |
8 | ** |
9 | ** $QT_BEGIN_LICENSE:LGPL$ |
10 | ** Commercial License Usage |
11 | ** Licensees holding valid commercial Qt licenses may use this file in |
12 | ** accordance with the commercial license agreement provided with the |
13 | ** Software or, alternatively, in accordance with the terms contained in |
14 | ** a written agreement between you and The Qt Company. For licensing terms |
15 | ** and conditions see https://www.qt.io/terms-conditions. For further |
16 | ** information use the contact form at https://www.qt.io/contact-us. |
17 | ** |
18 | ** GNU Lesser General Public License Usage |
19 | ** Alternatively, this file may be used under the terms of the GNU Lesser |
20 | ** General Public License version 3 as published by the Free Software |
21 | ** Foundation and appearing in the file LICENSE.LGPL3 included in the |
22 | ** packaging of this file. Please review the following information to |
23 | ** ensure the GNU Lesser General Public License version 3 requirements |
24 | ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. |
25 | ** |
26 | ** GNU General Public License Usage |
27 | ** Alternatively, this file may be used under the terms of the GNU |
28 | ** General Public License version 2.0 or (at your option) the GNU General |
29 | ** Public license version 3 or any later version approved by the KDE Free |
30 | ** Qt Foundation. The licenses are as published by the Free Software |
31 | ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 |
32 | ** included in the packaging of this file. Please review the following |
33 | ** information to ensure the GNU General Public License requirements will |
34 | ** be met: https://www.gnu.org/licenses/gpl-2.0.html and |
35 | ** https://www.gnu.org/licenses/gpl-3.0.html. |
36 | ** |
37 | ** $QT_END_LICENSE$ |
38 | ** |
39 | ****************************************************************************/ |
40 | |
41 | #ifndef QCORE_UNIX_P_H |
42 | #define QCORE_UNIX_P_H |
43 | |
44 | // |
45 | // W A R N I N G |
46 | // ------------- |
47 | // |
48 | // This file is not part of the Qt API. It exists for the convenience |
49 | // of Qt code on Unix. This header file may change from version to |
50 | // version without notice, or even be removed. |
51 | // |
52 | // We mean it. |
53 | // |
54 | |
55 | #include "qplatformdefs.h" |
56 | #include <QtCore/private/qglobal_p.h> |
57 | #include "qatomic.h" |
58 | #include "qbytearray.h" |
59 | |
60 | #ifndef Q_OS_UNIX |
61 | # error "qcore_unix_p.h included on a non-Unix system" |
62 | #endif |
63 | |
64 | #include <string.h> |
65 | #include <sys/types.h> |
66 | #include <sys/stat.h> |
67 | #include <unistd.h> |
68 | |
69 | #ifdef Q_OS_NACL |
70 | #elif !defined (Q_OS_VXWORKS) |
71 | # if !defined(Q_OS_HPUX) || defined(__ia64) |
72 | # include <sys/select.h> |
73 | # endif |
74 | # include <sys/time.h> |
75 | #else |
76 | # include <selectLib.h> |
77 | #endif |
78 | |
79 | #include <sys/wait.h> |
80 | #include <errno.h> |
81 | #include <fcntl.h> |
82 | |
83 | #if !defined(QT_POSIX_IPC) && !defined(QT_NO_SHAREDMEMORY) && !defined(Q_OS_ANDROID) |
84 | # include <sys/ipc.h> |
85 | #endif |
86 | |
87 | #if defined(Q_OS_VXWORKS) |
88 | # include <ioLib.h> |
89 | #endif |
90 | |
91 | #ifdef QT_NO_NATIVE_POLL |
92 | # include "qpoll_p.h" |
93 | #else |
94 | # include <poll.h> |
95 | #endif |
96 | |
97 | struct sockaddr; |
98 | |
99 | #define EINTR_LOOP(var, cmd) \ |
100 | do { \ |
101 | var = cmd; \ |
102 | } while (var == -1 && errno == EINTR) |
103 | |
104 | QT_BEGIN_NAMESPACE |
105 | |
106 | Q_DECLARE_TYPEINFO(pollfd, Q_PRIMITIVE_TYPE); |
107 | |
108 | // Internal operator functions for timespecs |
109 | inline timespec &normalizedTimespec(timespec &t) |
110 | { |
111 | while (t.tv_nsec >= 1000000000) { |
112 | ++t.tv_sec; |
113 | t.tv_nsec -= 1000000000; |
114 | } |
115 | while (t.tv_nsec < 0) { |
116 | --t.tv_sec; |
117 | t.tv_nsec += 1000000000; |
118 | } |
119 | return t; |
120 | } |
121 | inline bool operator<(const timespec &t1, const timespec &t2) |
122 | { return t1.tv_sec < t2.tv_sec || (t1.tv_sec == t2.tv_sec && t1.tv_nsec < t2.tv_nsec); } |
123 | inline bool operator==(const timespec &t1, const timespec &t2) |
124 | { return t1.tv_sec == t2.tv_sec && t1.tv_nsec == t2.tv_nsec; } |
125 | inline bool operator!=(const timespec &t1, const timespec &t2) |
126 | { return !(t1 == t2); } |
127 | inline timespec &operator+=(timespec &t1, const timespec &t2) |
128 | { |
129 | t1.tv_sec += t2.tv_sec; |
130 | t1.tv_nsec += t2.tv_nsec; |
131 | return normalizedTimespec(t&: t1); |
132 | } |
133 | inline timespec operator+(const timespec &t1, const timespec &t2) |
134 | { |
135 | timespec tmp; |
136 | tmp.tv_sec = t1.tv_sec + t2.tv_sec; |
137 | tmp.tv_nsec = t1.tv_nsec + t2.tv_nsec; |
138 | return normalizedTimespec(t&: tmp); |
139 | } |
140 | inline timespec operator-(const timespec &t1, const timespec &t2) |
141 | { |
142 | timespec tmp; |
143 | tmp.tv_sec = t1.tv_sec - (t2.tv_sec - 1); |
144 | tmp.tv_nsec = t1.tv_nsec - (t2.tv_nsec + 1000000000); |
145 | return normalizedTimespec(t&: tmp); |
146 | } |
147 | inline timespec operator*(const timespec &t1, int mul) |
148 | { |
149 | timespec tmp; |
150 | tmp.tv_sec = t1.tv_sec * mul; |
151 | tmp.tv_nsec = t1.tv_nsec * mul; |
152 | return normalizedTimespec(t&: tmp); |
153 | } |
154 | inline timeval timespecToTimeval(const timespec &ts) |
155 | { |
156 | timeval tv; |
157 | tv.tv_sec = ts.tv_sec; |
158 | tv.tv_usec = ts.tv_nsec / 1000; |
159 | return tv; |
160 | } |
161 | |
162 | |
163 | inline void qt_ignore_sigpipe() |
164 | { |
165 | // Set to ignore SIGPIPE once only. |
166 | static QBasicAtomicInt atom = Q_BASIC_ATOMIC_INITIALIZER(0); |
167 | if (!atom.loadRelaxed()) { |
168 | // More than one thread could turn off SIGPIPE at the same time |
169 | // But that's acceptable because they all would be doing the same |
170 | // action |
171 | struct sigaction noaction; |
172 | memset(s: &noaction, c: 0, n: sizeof(noaction)); |
173 | noaction.sa_handler = SIG_IGN; |
174 | ::sigaction(SIGPIPE, act: &noaction, oact: nullptr); |
175 | atom.storeRelaxed(newValue: 1); |
176 | } |
177 | } |
178 | |
179 | #if defined(Q_PROCESSOR_X86_32) && defined(__GLIBC__) |
180 | # if !__GLIBC_PREREQ(2, 22) |
181 | Q_CORE_EXPORT int qt_open64(const char *pathname, int flags, mode_t); |
182 | # undef QT_OPEN |
183 | # define QT_OPEN qt_open64 |
184 | # endif |
185 | #endif |
186 | |
187 | // don't call QT_OPEN or ::open |
188 | // call qt_safe_open |
189 | static inline int qt_safe_open(const char *pathname, int flags, mode_t mode = 0777) |
190 | { |
191 | #ifdef O_CLOEXEC |
192 | flags |= O_CLOEXEC; |
193 | #endif |
194 | int fd; |
195 | EINTR_LOOP(fd, QT_OPEN(pathname, flags, mode)); |
196 | |
197 | #ifndef O_CLOEXEC |
198 | if (fd != -1) |
199 | ::fcntl(fd, F_SETFD, FD_CLOEXEC); |
200 | #endif |
201 | |
202 | return fd; |
203 | } |
204 | #undef QT_OPEN |
205 | #define QT_OPEN qt_safe_open |
206 | |
207 | #ifndef Q_OS_VXWORKS // no POSIX pipes in VxWorks |
208 | // don't call ::pipe |
209 | // call qt_safe_pipe |
210 | static inline int qt_safe_pipe(int pipefd[2], int flags = 0) |
211 | { |
212 | Q_ASSERT((flags & ~O_NONBLOCK) == 0); |
213 | |
214 | #ifdef QT_THREADSAFE_CLOEXEC |
215 | // use pipe2 |
216 | flags |= O_CLOEXEC; |
217 | return ::pipe2(pipedes: pipefd, flags: flags); // pipe2 is documented not to return EINTR |
218 | #else |
219 | int ret = ::pipe(pipefd); |
220 | if (ret == -1) |
221 | return -1; |
222 | |
223 | ::fcntl(pipefd[0], F_SETFD, FD_CLOEXEC); |
224 | ::fcntl(pipefd[1], F_SETFD, FD_CLOEXEC); |
225 | |
226 | // set non-block too? |
227 | if (flags & O_NONBLOCK) { |
228 | ::fcntl(pipefd[0], F_SETFL, ::fcntl(pipefd[0], F_GETFL) | O_NONBLOCK); |
229 | ::fcntl(pipefd[1], F_SETFL, ::fcntl(pipefd[1], F_GETFL) | O_NONBLOCK); |
230 | } |
231 | |
232 | return 0; |
233 | #endif |
234 | } |
235 | |
236 | #endif // Q_OS_VXWORKS |
237 | |
238 | // don't call dup or fcntl(F_DUPFD) |
239 | static inline int qt_safe_dup(int oldfd, int atleast = 0, int flags = FD_CLOEXEC) |
240 | { |
241 | Q_ASSERT(flags == FD_CLOEXEC || flags == 0); |
242 | |
243 | #ifdef F_DUPFD_CLOEXEC |
244 | int cmd = F_DUPFD; |
245 | if (flags & FD_CLOEXEC) |
246 | cmd = F_DUPFD_CLOEXEC; |
247 | return ::fcntl(fd: oldfd, cmd: cmd, atleast); |
248 | #else |
249 | // use F_DUPFD |
250 | int ret = ::fcntl(oldfd, F_DUPFD, atleast); |
251 | |
252 | if (flags && ret != -1) |
253 | ::fcntl(ret, F_SETFD, flags); |
254 | return ret; |
255 | #endif |
256 | } |
257 | |
258 | // don't call dup2 |
259 | // call qt_safe_dup2 |
260 | static inline int qt_safe_dup2(int oldfd, int newfd, int flags = FD_CLOEXEC) |
261 | { |
262 | Q_ASSERT(flags == FD_CLOEXEC || flags == 0); |
263 | |
264 | int ret; |
265 | #ifdef QT_THREADSAFE_CLOEXEC |
266 | // use dup3 |
267 | EINTR_LOOP(ret, ::dup3(oldfd, newfd, flags ? O_CLOEXEC : 0)); |
268 | return ret; |
269 | #else |
270 | EINTR_LOOP(ret, ::dup2(oldfd, newfd)); |
271 | if (ret == -1) |
272 | return -1; |
273 | |
274 | if (flags) |
275 | ::fcntl(newfd, F_SETFD, flags); |
276 | return 0; |
277 | #endif |
278 | } |
279 | |
280 | static inline qint64 qt_safe_read(int fd, void *data, qint64 maxlen) |
281 | { |
282 | qint64 ret = 0; |
283 | EINTR_LOOP(ret, QT_READ(fd, data, maxlen)); |
284 | return ret; |
285 | } |
286 | #undef QT_READ |
287 | #define QT_READ qt_safe_read |
288 | |
289 | static inline qint64 qt_safe_write(int fd, const void *data, qint64 len) |
290 | { |
291 | qint64 ret = 0; |
292 | EINTR_LOOP(ret, QT_WRITE(fd, data, len)); |
293 | return ret; |
294 | } |
295 | #undef QT_WRITE |
296 | #define QT_WRITE qt_safe_write |
297 | |
298 | static inline qint64 qt_safe_write_nosignal(int fd, const void *data, qint64 len) |
299 | { |
300 | qt_ignore_sigpipe(); |
301 | return qt_safe_write(fd, data, len); |
302 | } |
303 | |
304 | static inline int qt_safe_close(int fd) |
305 | { |
306 | int ret; |
307 | EINTR_LOOP(ret, QT_CLOSE(fd)); |
308 | return ret; |
309 | } |
310 | #undef QT_CLOSE |
311 | #define QT_CLOSE qt_safe_close |
312 | |
313 | // - VxWorks & iOS/tvOS/watchOS don't have processes |
314 | #if QT_CONFIG(process) |
315 | static inline int qt_safe_execve(const char *filename, char *const argv[], |
316 | char *const envp[]) |
317 | { |
318 | int ret; |
319 | EINTR_LOOP(ret, ::execve(filename, argv, envp)); |
320 | return ret; |
321 | } |
322 | |
323 | static inline int qt_safe_execv(const char *path, char *const argv[]) |
324 | { |
325 | int ret; |
326 | EINTR_LOOP(ret, ::execv(path, argv)); |
327 | return ret; |
328 | } |
329 | |
330 | static inline int qt_safe_execvp(const char *file, char *const argv[]) |
331 | { |
332 | int ret; |
333 | EINTR_LOOP(ret, ::execvp(file, argv)); |
334 | return ret; |
335 | } |
336 | |
337 | static inline pid_t qt_safe_waitpid(pid_t pid, int *status, int options) |
338 | { |
339 | int ret; |
340 | EINTR_LOOP(ret, ::waitpid(pid, status, options)); |
341 | return ret; |
342 | } |
343 | #endif // QT_CONFIG(process) |
344 | |
345 | #if !defined(_POSIX_MONOTONIC_CLOCK) |
346 | # define _POSIX_MONOTONIC_CLOCK -1 |
347 | #endif |
348 | |
349 | // in qelapsedtimer_mac.cpp or qtimestamp_unix.cpp |
350 | timespec qt_gettime() noexcept; |
351 | void qt_nanosleep(timespec amount); |
352 | QByteArray qt_readlink(const char *path); |
353 | |
354 | /* non-static */ |
355 | inline bool qt_haveLinuxProcfs() |
356 | { |
357 | #ifdef Q_OS_LINUX |
358 | # ifdef QT_LINUX_ALWAYS_HAVE_PROCFS |
359 | return true; |
360 | # else |
361 | static const bool present = (access(name: "/proc/version" , F_OK) == 0); |
362 | return present; |
363 | # endif |
364 | #else |
365 | return false; |
366 | #endif |
367 | } |
368 | |
369 | Q_CORE_EXPORT int qt_safe_poll(struct pollfd *fds, nfds_t nfds, const struct timespec *timeout_ts); |
370 | |
371 | static inline int qt_poll_msecs(struct pollfd *fds, nfds_t nfds, int timeout) |
372 | { |
373 | timespec ts, *pts = nullptr; |
374 | |
375 | if (timeout >= 0) { |
376 | ts.tv_sec = timeout / 1000; |
377 | ts.tv_nsec = (timeout % 1000) * 1000 * 1000; |
378 | pts = &ts; |
379 | } |
380 | |
381 | return qt_safe_poll(fds, nfds, timeout_ts: pts); |
382 | } |
383 | |
384 | static 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 |
392 | struct semid_ds; |
393 | union 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 | |
399 | QT_END_NAMESPACE |
400 | |
401 | #endif |
402 | |