1 /****************************************************************************
2 **
3 ** Copyright (C) 2015 Konstantin Ritt <ritt.ks@gmail.com>
4 ** Copyright (C) 2016 The Qt Company Ltd.
5 ** Copyright (C) 2015 Klarälvdalens Datakonsult AB, a KDAB Group company, info@kdab.com, author Tobias Koenig <tobias.koenig@kdab.com>
6 ** Contact: https://www.qt.io/licensing/
7 **
8 ** This file is part of the QtCore module of the Qt Toolkit.
9 **
10 ** $QT_BEGIN_LICENSE:LGPL$
11 ** Commercial License Usage
12 ** Licensees holding valid commercial Qt licenses may use this file in
13 ** accordance with the commercial license agreement provided with the
14 ** Software or, alternatively, in accordance with the terms contained in
15 ** a written agreement between you and The Qt Company. For licensing terms
16 ** and conditions see https://www.qt.io/terms-conditions. For further
17 ** information use the contact form at https://www.qt.io/contact-us.
18 **
19 ** GNU Lesser General Public License Usage
20 ** Alternatively, this file may be used under the terms of the GNU Lesser
21 ** General Public License version 3 as published by the Free Software
22 ** Foundation and appearing in the file LICENSE.LGPL3 included in the
23 ** packaging of this file. Please review the following information to
24 ** ensure the GNU Lesser General Public License version 3 requirements
25 ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
26 **
27 ** GNU General Public License Usage
28 ** Alternatively, this file may be used under the terms of the GNU
29 ** General Public License version 2.0 or (at your option) the GNU General
30 ** Public license version 3 or any later version approved by the KDE Free
31 ** Qt Foundation. The licenses are as published by the Free Software
32 ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
33 ** included in the packaging of this file. Please review the following
34 ** information to ensure the GNU General Public License requirements will
35 ** be met: https://www.gnu.org/licenses/gpl-2.0.html and
36 ** https://www.gnu.org/licenses/gpl-3.0.html.
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41 
42 #include "qplatformdefs.h"
43 
44 #include "qsharedmemory.h"
45 #include "qsharedmemory_p.h"
46 #include "qsystemsemaphore.h"
47 #include <qfile.h>
48 
49 #include <errno.h>
50 
51 #ifdef QT_POSIX_IPC
52 
53 #ifndef QT_NO_SHAREDMEMORY
54 #include <sys/types.h>
55 #include <sys/mman.h>
56 #include <sys/stat.h>
57 #include <fcntl.h>
58 #include <unistd.h>
59 
60 #include "private/qcore_unix_p.h"
61 
62 QT_BEGIN_NAMESPACE
63 
handle()64 int QSharedMemoryPrivate::handle()
65 {
66     // don't allow making handles on empty keys
67     const QString safeKey = makePlatformSafeKey(key);
68     if (safeKey.isEmpty()) {
69         errorString = QSharedMemory::tr("%1: key is empty").arg(QLatin1String("QSharedMemory::handle"));
70         error = QSharedMemory::KeyError;
71         return 0;
72     }
73 
74     return 1;
75 }
76 
cleanHandle()77 bool QSharedMemoryPrivate::cleanHandle()
78 {
79     qt_safe_close(hand);
80     hand = -1;
81 
82     return true;
83 }
84 
create(int size)85 bool QSharedMemoryPrivate::create(int size)
86 {
87     if (!handle())
88         return false;
89 
90     const QByteArray shmName = QFile::encodeName(makePlatformSafeKey(key));
91 
92     int fd;
93 #ifdef O_CLOEXEC
94     // First try with O_CLOEXEC flag, if that fails, fall back to normal flags
95     EINTR_LOOP(fd, ::shm_open(shmName.constData(), O_RDWR | O_CREAT | O_EXCL | O_CLOEXEC, 0600));
96     if (fd == -1)
97         EINTR_LOOP(fd, ::shm_open(shmName.constData(), O_RDWR | O_CREAT | O_EXCL, 0600));
98 #else
99     EINTR_LOOP(fd, ::shm_open(shmName.constData(), O_RDWR | O_CREAT | O_EXCL, 0600));
100 #endif
101     if (fd == -1) {
102         const int errorNumber = errno;
103         const QLatin1String function("QSharedMemory::attach (shm_open)");
104         switch (errorNumber) {
105         case ENAMETOOLONG:
106         case EINVAL:
107             errorString = QSharedMemory::tr("%1: bad name").arg(function);
108             error = QSharedMemory::KeyError;
109             break;
110         default:
111             setErrorString(function);
112         }
113         return false;
114     }
115 
116     // the size may only be set once
117     int ret;
118     EINTR_LOOP(ret, QT_FTRUNCATE(fd, size));
119     if (ret == -1) {
120         setErrorString(QLatin1String("QSharedMemory::create (ftruncate)"));
121         qt_safe_close(fd);
122         return false;
123     }
124 
125     qt_safe_close(fd);
126 
127     return true;
128 }
129 
attach(QSharedMemory::AccessMode mode)130 bool QSharedMemoryPrivate::attach(QSharedMemory::AccessMode mode)
131 {
132     const QByteArray shmName = QFile::encodeName(makePlatformSafeKey(key));
133 
134     const int oflag = (mode == QSharedMemory::ReadOnly ? O_RDONLY : O_RDWR);
135     const mode_t omode = (mode == QSharedMemory::ReadOnly ? 0400 : 0600);
136 
137 #ifdef O_CLOEXEC
138     // First try with O_CLOEXEC flag, if that fails, fall back to normal flags
139     EINTR_LOOP(hand, ::shm_open(shmName.constData(), oflag | O_CLOEXEC, omode));
140     if (hand == -1)
141         EINTR_LOOP(hand, ::shm_open(shmName.constData(), oflag, omode));
142 #else
143     EINTR_LOOP(hand, ::shm_open(shmName.constData(), oflag, omode));
144 #endif
145     if (hand == -1) {
146         const int errorNumber = errno;
147         const QLatin1String function("QSharedMemory::attach (shm_open)");
148         switch (errorNumber) {
149         case ENAMETOOLONG:
150         case EINVAL:
151             errorString = QSharedMemory::tr("%1: bad name").arg(function);
152             error = QSharedMemory::KeyError;
153             break;
154         default:
155             setErrorString(function);
156         }
157         hand = -1;
158         return false;
159     }
160 
161     // grab the size
162     QT_STATBUF st;
163     if (QT_FSTAT(hand, &st) == -1) {
164         setErrorString(QLatin1String("QSharedMemory::attach (fstat)"));
165         cleanHandle();
166         return false;
167     }
168     size = st.st_size;
169 
170     // grab the memory
171     const int mprot = (mode == QSharedMemory::ReadOnly ? PROT_READ : PROT_READ | PROT_WRITE);
172     memory = QT_MMAP(0, size, mprot, MAP_SHARED, hand, 0);
173     if (memory == MAP_FAILED || !memory) {
174         setErrorString(QLatin1String("QSharedMemory::attach (mmap)"));
175         cleanHandle();
176         memory = 0;
177         size = 0;
178         return false;
179     }
180 
181 #ifdef F_ADD_SEALS
182     // Make sure the shared memory region will not shrink
183     // otherwise someone could cause SIGBUS on us.
184     // (see http://lwn.net/Articles/594919/)
185     fcntl(hand, F_ADD_SEALS, F_SEAL_SHRINK);
186 #endif
187 
188     return true;
189 }
190 
detach()191 bool QSharedMemoryPrivate::detach()
192 {
193     // detach from the memory segment
194     if (::munmap(memory, size) == -1) {
195         setErrorString(QLatin1String("QSharedMemory::detach (munmap)"));
196         return false;
197     }
198     memory = 0;
199     size = 0;
200 
201 #ifdef Q_OS_QNX
202     // On QNX the st_nlink field of struct stat contains the number of
203     // active shm_open() connections to the shared memory file, so we
204     // can use it to automatically clean up the file once the last
205     // user has detached from it.
206 
207     // get the number of current attachments
208     int shm_nattch = 0;
209     QT_STATBUF st;
210     if (QT_FSTAT(hand, &st) == 0) {
211         // subtract 2 from linkcount: one for our own open and one for the dir entry
212         shm_nattch = st.st_nlink - 2;
213     }
214 
215     cleanHandle();
216 
217     // if there are no attachments then unlink the shared memory
218     if (shm_nattch == 0) {
219         const QByteArray shmName = QFile::encodeName(makePlatformSafeKey(key));
220         if (::shm_unlink(shmName.constData()) == -1 && errno != ENOENT)
221             setErrorString(QLatin1String("QSharedMemory::detach (shm_unlink)"));
222     }
223 #else
224     // On non-QNX systems (tested Linux and Haiku), the st_nlink field is always 1,
225     // so we'll simply leak the shared memory files.
226     cleanHandle();
227 #endif
228 
229     return true;
230 }
231 
232 QT_END_NAMESPACE
233 
234 #endif // QT_NO_SHAREDMEMORY
235 
236 #endif // QT_POSIX_IPC
237