IPC: rationalize use of O_CLOEXEC

This flag is properly supported everywhere it is defined. There's no need
to retry any more.

Change-Id: I12a088d1ae424825abd3fffd171d6ad10d18247e
Reviewed-by: Fabian Kosmale <fabian.kosmale@qt.io>
This commit is contained in:
Thiago Macieira 2022-10-12 13:01:23 -07:00
parent f7ae2c44a8
commit c9eac98369

View File

@ -19,6 +19,10 @@
#include "private/qcore_unix_p.h"
#ifndef O_CLOEXEC
# define O_CLOEXEC 0
#endif
QT_BEGIN_NAMESPACE
using namespace Qt::StringLiterals;
@ -61,14 +65,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
if (fd == -1) {
const int errorNumber = errno;
const auto function = "QSharedMemory::attach (shm_open)"_L1;
@ -104,14 +101,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
if (hand == -1) {
const int errorNumber = errno;
const auto function = "QSharedMemory::attach (shm_open)"_L1;