| 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 | |
| 21 | QT_BEGIN_NAMESPACE |
| 22 | |
| 23 | void 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, act: &noaction, oact: nullptr); |
| 34 | atom.storeRelaxed(newValue: 1); |
| 35 | } |
| 36 | } |
| 37 | |
| 38 | QByteArray qt_readlink(const char *path) |
| 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 | |
| 45 | QByteArray buf(256, Qt::Uninitialized); |
| 46 | |
| 47 | ssize_t len = ::readlink(path: path, buf: buf.data(), len: 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(size: buf.size() * 2); |
| 57 | len = ::readlink(path: path, buf: buf.data(), len: buf.size()); |
| 58 | } |
| 59 | |
| 60 | if (len == -1) |
| 61 | return QByteArray(); |
| 62 | |
| 63 | buf.resize(size: 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 |
| 74 | int 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]] |
| 88 | static inline int timespecToMillisecs(const struct timespec *ts) |
| 89 | { |
| 90 | using namespace std::chrono; |
| 91 | if (!ts) |
| 92 | return -1; |
| 93 | auto ms = ceil<milliseconds>(d: timespecToChrono<nanoseconds>(ts: *ts)); |
| 94 | return int(ms.count()); |
| 95 | } |
| 96 | |
| 97 | // defined in qpoll.cpp |
| 98 | int qt_poll(struct pollfd *fds, nfds_t nfds, const struct timespec *timeout_ts); |
| 99 | |
| 100 | static 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: fds, nfds: nfds, timeout: timeout_ts, ss: 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 | |
| 114 | /*! |
| 115 | \internal |
| 116 | |
| 117 | Behaves as close to POSIX poll(2) as practical but may be implemented |
| 118 | using select(2) where necessary. In that case, returns -1 and sets errno |
| 119 | to EINVAL if passed any descriptor greater than or equal to FD_SETSIZE. |
| 120 | */ |
| 121 | int 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(timeout: remaining); |
| 135 | const int ret = qt_ppoll(fds, nfds, timeout_ts: &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 | |
| 146 | QT_END_NAMESPACE |
| 147 | |