summaryrefslogtreecommitdiffstats
path: root/src/corelib/ipc/qsharedmemory_posix.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/corelib/ipc/qsharedmemory_posix.cpp')
-rw-r--r--src/corelib/ipc/qsharedmemory_posix.cpp29
1 files changed, 11 insertions, 18 deletions
diff --git a/src/corelib/ipc/qsharedmemory_posix.cpp b/src/corelib/ipc/qsharedmemory_posix.cpp
index 313b34a594..582c6628e1 100644
--- a/src/corelib/ipc/qsharedmemory_posix.cpp
+++ b/src/corelib/ipc/qsharedmemory_posix.cpp
@@ -10,6 +10,7 @@
#include <errno.h>
+#if QT_CONFIG(sharedmemory)
#if QT_CONFIG(posix_shm)
#include <sys/types.h>
#include <sys/mman.h>
@@ -19,6 +20,10 @@
#include "private/qcore_unix_p.h"
+#ifndef O_CLOEXEC
+# define O_CLOEXEC 0
+#endif
+
QT_BEGIN_NAMESPACE
using namespace Qt::StringLiterals;
@@ -47,7 +52,8 @@ bool QSharedMemoryPosix::handle(QSharedMemoryPrivate *self)
bool QSharedMemoryPosix::cleanHandle(QSharedMemoryPrivate *)
{
- qt_safe_close(hand);
+ if (hand != -1)
+ qt_safe_close(hand);
hand = -1;
return true;
@@ -61,14 +67,7 @@ bool QSharedMemoryPosix::create(QSharedMemoryPrivate *self, qsizetype size)
const QByteArray shmName = QFile::encodeName(self->nativeKey.nativeKey());
int fd;
-#ifdef O_CLOEXEC
- // First try with O_CLOEXEC flag, if that fails, fall back to normal flags
- EINTR_LOOP(fd, ::shm_open(shmName.constData(), O_RDWR | O_CREAT | O_EXCL | O_CLOEXEC, 0600));
- if (fd == -1)
- EINTR_LOOP(fd, ::shm_open(shmName.constData(), O_RDWR | O_CREAT | O_EXCL, 0600));
-#else
- EINTR_LOOP(fd, ::shm_open(shmName.constData(), O_RDWR | O_CREAT | O_EXCL, 0600));
-#endif
+ QT_EINTR_LOOP(fd, ::shm_open(shmName.constData(), O_RDWR | O_CREAT | O_EXCL | O_CLOEXEC, 0600));
if (fd == -1) {
const int errorNumber = errno;
const auto function = "QSharedMemory::attach (shm_open)"_L1;
@@ -85,7 +84,7 @@ bool QSharedMemoryPosix::create(QSharedMemoryPrivate *self, qsizetype size)
// the size may only be set once
int ret;
- EINTR_LOOP(ret, QT_FTRUNCATE(fd, size));
+ QT_EINTR_LOOP(ret, QT_FTRUNCATE(fd, size));
if (ret == -1) {
self->setUnixErrorString("QSharedMemory::create (ftruncate)"_L1);
qt_safe_close(fd);
@@ -104,14 +103,7 @@ bool QSharedMemoryPosix::attach(QSharedMemoryPrivate *self, QSharedMemory::Acces
const int oflag = (mode == QSharedMemory::ReadOnly ? O_RDONLY : O_RDWR);
const mode_t omode = (mode == QSharedMemory::ReadOnly ? 0400 : 0600);
-#ifdef O_CLOEXEC
- // First try with O_CLOEXEC flag, if that fails, fall back to normal flags
- EINTR_LOOP(hand, ::shm_open(shmName.constData(), oflag | O_CLOEXEC, omode));
- if (hand == -1)
- EINTR_LOOP(hand, ::shm_open(shmName.constData(), oflag, omode));
-#else
- EINTR_LOOP(hand, ::shm_open(shmName.constData(), oflag, omode));
-#endif
+ QT_EINTR_LOOP(hand, ::shm_open(shmName.constData(), oflag | O_CLOEXEC, omode));
if (hand == -1) {
const int errorNumber = errno;
const auto function = "QSharedMemory::attach (shm_open)"_L1;
@@ -201,3 +193,4 @@ bool QSharedMemoryPosix::detach(QSharedMemoryPrivate *self)
QT_END_NAMESPACE
#endif // QT_CONFIG(posix_shm)
+#endif // QT_CONFIG(sharedmemory)