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
qsharedmemory_posix.cpp
Go to the documentation of this file.
1// Copyright (C) 2015 Konstantin Ritt <ritt.ks@gmail.com>
2// Copyright (C) 2016 The Qt Company Ltd.
3// Copyright (C) 2015 Klarälvdalens Datakonsult AB, a KDAB Group company, info@kdab.com, author Tobias Koenig <tobias.koenig@kdab.com>
4// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
5// Qt-Security score:significant reason:default
6
10#include <qfile.h>
11
12#include <errno.h>
13
14#if QT_CONFIG(sharedmemory)
15#if QT_CONFIG(posix_shm)
16#include <sys/types.h>
17#include <sys/mman.h>
18#include <sys/stat.h>
19#include <fcntl.h>
20#include <unistd.h>
21
22#include "private/qcore_unix_p.h"
23
24QT_BEGIN_NAMESPACE
25
26using namespace Qt::StringLiterals;
27using namespace QtIpcCommon;
28
29bool QSharedMemoryPosix::runtimeSupportCheck()
30{
31 static const bool result = []() {
32 (void)shm_open("", 0, 0); // this WILL fail
33 return errno != ENOSYS;
34 }();
35 return result;
36}
37
38bool QSharedMemoryPosix::handle(QSharedMemoryPrivate *self)
39{
40 // don't allow making handles on empty keys
41 if (self->nativeKey.isEmpty()) {
42 self->setError(QSharedMemory::KeyError,
43 QSharedMemory::tr("%1: key is empty").arg("QSharedMemory::handle"_L1));
44 return false;
45 }
46
47 return true;
48}
49
50bool QSharedMemoryPosix::cleanHandle(QSharedMemoryPrivate *)
51{
52 if (hand != -1)
53 qt_safe_close(hand);
54 hand = -1;
55
56 return true;
57}
58
59bool QSharedMemoryPosix::create(QSharedMemoryPrivate *self, qsizetype size)
60{
61 if (!handle(self))
62 return false;
63
64 const QByteArray shmName = QFile::encodeName(self->nativeKey.nativeKey());
65
66 int fd;
67 QT_EINTR_LOOP(fd, ::shm_open(shmName.constData(), O_RDWR | O_CREAT | O_EXCL, 0600));
68 if (fd == -1) {
69 const int errorNumber = errno;
70 const auto function = "QSharedMemory::attach (shm_open)"_L1;
71 switch (errorNumber) {
72 case EINVAL:
73 self->setError(QSharedMemory::KeyError,
74 QSharedMemory::tr("%1: bad name").arg(function));
75 break;
76 default:
77 self->setUnixErrorString(function);
78 }
79 return false;
80 }
81
82 // the size may only be set once
83 int ret;
84 QT_EINTR_LOOP(ret, QT_FTRUNCATE(fd, size));
85 if (ret == -1) {
86 self->setUnixErrorString("QSharedMemory::create (ftruncate)"_L1);
87 qt_safe_close(fd);
88 return false;
89 }
90
91 qt_safe_close(fd);
92
93 return true;
94}
95
96bool QSharedMemoryPosix::attach(QSharedMemoryPrivate *self, QSharedMemory::AccessMode mode)
97{
98 const QByteArray shmName = QFile::encodeName(self->nativeKey.nativeKey());
99
100 const int oflag = (mode == QSharedMemory::ReadOnly ? O_RDONLY : O_RDWR);
101 const mode_t omode = (mode == QSharedMemory::ReadOnly ? 0400 : 0600);
102
103 QT_EINTR_LOOP(hand, ::shm_open(shmName.constData(), oflag, omode));
104 if (hand == -1) {
105 const int errorNumber = errno;
106 const auto function = "QSharedMemory::attach (shm_open)"_L1;
107 switch (errorNumber) {
108 case EINVAL:
109 self->setError(QSharedMemory::KeyError,
110 QSharedMemory::tr("%1: bad name").arg(function));
111 break;
112 default:
113 self->setUnixErrorString(function);
114 }
115 hand = -1;
116 return false;
117 }
118
119 // grab the size
120 QT_STATBUF st;
121 if (QT_FSTAT(hand, &st) == -1) {
122 self->setUnixErrorString("QSharedMemory::attach (fstat)"_L1);
123 cleanHandle(self);
124 return false;
125 }
126 self->size = qsizetype(st.st_size);
127
128 // grab the memory
129 const int mprot = (mode == QSharedMemory::ReadOnly ? PROT_READ : PROT_READ | PROT_WRITE);
130 self->memory = QT_MMAP(0, size_t(self->size), mprot, MAP_SHARED, hand, 0);
131 if (self->memory == MAP_FAILED || !self->memory) {
132 self->setUnixErrorString("QSharedMemory::attach (mmap)"_L1);
133 cleanHandle(self);
134 self->memory = 0;
135 self->size = 0;
136 return false;
137 }
138
139#ifdef F_ADD_SEALS
140 // Make sure the shared memory region will not shrink
141 // otherwise someone could cause SIGBUS on us.
142 // (see http://lwn.net/Articles/594919/)
143 fcntl(hand, F_ADD_SEALS, F_SEAL_SHRINK);
144#endif
145
146 return true;
147}
148
149bool QSharedMemoryPosix::detach(QSharedMemoryPrivate *self)
150{
151 // detach from the memory segment
152 if (::munmap(self->memory, size_t(self->size)) == -1) {
153 self->setUnixErrorString("QSharedMemory::detach (munmap)"_L1);
154 return false;
155 }
156 self->memory = 0;
157 self->size = 0;
158
159#ifdef Q_OS_QNX
160 // On QNX the st_nlink field of struct stat contains the number of
161 // active shm_open() connections to the shared memory file, so we
162 // can use it to automatically clean up the file once the last
163 // user has detached from it.
164
165 // get the number of current attachments
166 int shm_nattch = 0;
167 QT_STATBUF st;
168 if (QT_FSTAT(hand, &st) == 0) {
169 // subtract 2 from linkcount: one for our own open and one for the dir entry
170 shm_nattch = st.st_nlink - 2;
171 }
172
173 cleanHandle(self);
174
175 // if there are no attachments then unlink the shared memory
176 if (shm_nattch == 0) {
177 const QByteArray shmName = QFile::encodeName(self->nativeKey.nativeKey());
178 if (::shm_unlink(shmName.constData()) == -1 && errno != ENOENT)
179 self->setUnixErrorString("QSharedMemory::detach (shm_unlink)"_L1);
180 }
181#else
182 // On non-QNX systems (tested Linux and Haiku), the st_nlink field is always 1,
183 // so we'll simply leak the shared memory files.
184 cleanHandle(self);
185#endif
186
187 return true;
188}
189
190QT_END_NAMESPACE
191
192#endif // QT_CONFIG(posix_shm)
193#endif // QT_CONFIG(sharedmemory)