Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
Loading...
Searching...
No Matches
qcore_unix.cpp
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
5#include <QtCore/private/qglobal_p.h>
6#include <QtCore/qbasicatomic.h>
7#include "qcore_unix_p.h"
8
9#include <stdlib.h>
10
11#ifdef __GLIBC__
12# include <sys/syscall.h>
13# include <pthread.h>
14# include <unistd.h>
15#endif
16
17#ifdef Q_OS_DARWIN
18#include <mach/mach_time.h>
19#endif
20
22
23void qt_ignore_sigpipe() noexcept // noexcept: sigaction(2) is not a Posix Cancellation Point
24{
25 // Set to ignore SIGPIPE once only.
26 Q_CONSTINIT static QBasicAtomicInt atom = Q_BASIC_ATOMIC_INITIALIZER(0);
27 if (!atom.loadRelaxed()) {
28 // More than one thread could turn off SIGPIPE at the same time
29 // But that's acceptable because they all would be doing the same
30 // action
31 struct sigaction noaction = {};
32 noaction.sa_handler = SIG_IGN;
33 ::sigaction(SIGPIPE, &noaction, nullptr);
34 atom.storeRelaxed(1);
35 }
36}
37
39{
40#ifndef PATH_MAX
41 // suitably large value that won't consume too much memory
42# define PATH_MAX 1024*1024
43#endif
44
46
47 ssize_t len = ::readlink(path, buf.data(), buf.size());
48 while (len == buf.size()) {
49 // readlink(2) will fill our buffer and not necessarily terminate with NUL;
50 if (buf.size() >= PATH_MAX) {
51 errno = ENAMETOOLONG;
52 return QByteArray();
53 }
54
55 // double the size and try again
56 buf.resize(buf.size() * 2);
57 len = ::readlink(path, buf.data(), buf.size());
58 }
59
60 if (len == -1)
61 return QByteArray();
62
63 buf.resize(len);
64 return buf;
65}
66
67#if defined(Q_PROCESSOR_X86_32) && defined(__GLIBC__)
68# if !__GLIBC_PREREQ(2, 22)
69// glibc prior to release 2.22 had a bug that suppresses the third argument to
70// open() / open64() / openat(), causing file creation with O_TMPFILE to have
71// the wrong permissions. So we bypass the glibc implementation and go straight
72// for the syscall. See
73// https://sourceware.org/git/?p=glibc.git;a=commit;h=65f6f938cd562a614a68e15d0581a34b177ec29d
74int qt_open64(const char *pathname, int flags, mode_t mode)
75{
76 return syscall(SYS_open, pathname, flags | O_LARGEFILE, mode);
77}
78# endif
79#endif
80
81#ifndef QT_BOOTSTRAPPED
82
83#if QT_CONFIG(poll_pollts)
84# define ppoll pollts
85#endif
86
87[[maybe_unused]]
88static inline int timespecToMillisecs(const struct timespec *ts)
89{
90 using namespace std::chrono;
91 if (!ts)
92 return -1;
93 auto ms = ceil<milliseconds>(timespecToChrono<nanoseconds>(*ts));
94 return int(ms.count());
95}
96
97// defined in qpoll.cpp
98int qt_poll(struct pollfd *fds, nfds_t nfds, const struct timespec *timeout_ts);
99
100static inline int qt_ppoll(struct pollfd *fds, nfds_t nfds, const struct timespec *timeout_ts)
101{
102#if QT_CONFIG(poll_ppoll) || QT_CONFIG(poll_pollts)
103 return ::ppoll(fds, nfds, timeout_ts, nullptr);
104#elif QT_CONFIG(poll_poll)
105 return ::poll(fds, nfds, timespecToMillisecs(timeout_ts));
106#elif QT_CONFIG(poll_select)
107 return qt_poll(fds, nfds, timeout_ts);
108#else
109 // configure.json reports an error when everything is not available
110#endif
111}
112
113
121int qt_safe_poll(struct pollfd *fds, nfds_t nfds, QDeadlineTimer deadline)
122{
123 if (deadline.isForever()) {
124 // no timeout -> block forever
125 int ret;
126 QT_EINTR_LOOP(ret, qt_ppoll(fds, nfds, nullptr));
127 return ret;
128 }
129
130 using namespace std::chrono;
131 nanoseconds remaining = deadline.remainingTimeAsDuration();
132 // loop and recalculate the timeout as needed
133 do {
134 timespec ts = durationToTimespec(remaining);
135 const int ret = qt_ppoll(fds, nfds, &ts);
136 if (ret != -1 || errno != EINTR)
137 return ret;
138 remaining = deadline.remainingTimeAsDuration();
139 } while (remaining > 0ns);
140
141 return 0;
142}
143
144#endif // QT_BOOTSTRAPPED
145
\inmodule QtCore
Definition qbytearray.h:57
\inmodule QtCore
constexpr bool isForever() const noexcept
Returns true if this QDeadlineTimer object never expires, false otherwise.
std::chrono::nanoseconds remainingTimeAsDuration() const noexcept
Returns the time remaining before the deadline.
EGLint EGLint EGLint EGLint int int int int * fds
Combined button and popup list for selecting options.
constexpr Initialization Uninitialized
#define Q_BASIC_ATOMIC_INITIALIZER(a)
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:136
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)
#define PATH_MAX
QByteArray qt_readlink(const char *path)
#define QT_EINTR_LOOP(var, cmd)
timespec durationToTimespec(std::chrono::nanoseconds timeout) noexcept
typedef QByteArray(EGLAPIENTRYP PFNQGSGETDISPLAYSPROC)()
return ret
GLenum mode
GLenum GLuint GLenum GLsizei const GLchar * buf
GLbitfield flags
GLsizei const GLchar *const * path
GLenum GLsizei len
QDeadlineTimer deadline(30s)