diff options
author | Tobias Koenig <tobias.koenig@kdab.com> | 2015-01-03 11:59:50 +0100 |
---|---|---|
committer | Tobias Koenig <tobias.koenig@kdab.com> | 2015-01-09 10:59:00 +0100 |
commit | ddc884c841b770974cefbf0029254560b607d57f (patch) | |
tree | ebd0a05ec7cafe69d44f570a53b437cc62b1945b /src/corelib/kernel/qsharedmemory_posix.cpp | |
parent | 96995db4af6e1f5e9fe313e4c71a41fd939fedf8 (diff) |
Use O_CLOEXEC flag POSIX shared memory handle
Change-Id: Ie554c5963112b88c058082085dbc9eed42a41861
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
Diffstat (limited to 'src/corelib/kernel/qsharedmemory_posix.cpp')
-rw-r--r-- | src/corelib/kernel/qsharedmemory_posix.cpp | 14 |
1 files changed, 14 insertions, 0 deletions
diff --git a/src/corelib/kernel/qsharedmemory_posix.cpp b/src/corelib/kernel/qsharedmemory_posix.cpp index de45172295..74b810bec6 100644 --- a/src/corelib/kernel/qsharedmemory_posix.cpp +++ b/src/corelib/kernel/qsharedmemory_posix.cpp @@ -85,7 +85,14 @@ bool QSharedMemoryPrivate::create(int size) const QByteArray shmName = QFile::encodeName(makePlatformSafeKey(key)); 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 QString function = QLatin1String("QSharedMemory::create"); @@ -122,7 +129,14 @@ bool QSharedMemoryPrivate::attach(QSharedMemory::AccessMode mode) 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 QString function = QLatin1String("QSharedMemory::attach (shm_open)"); |