From 461463b75dafda4d216e297cba37f6b7388cf12c Mon Sep 17 00:00:00 2001 From: Thiago Macieira Date: Sat, 8 Oct 2022 00:16:58 -0700 Subject: [PATCH] IPC: move createUnixKeyFile to a common, private header So we don't have to mess around with #ifs to support the build. Drive-by replace with unlink() some calls to QFile::remove(), which create a QFSFileEngine, to call QFSFileEngine::remove, which calls QFileSystemEngine::removeFile. Since we've just created the file with qt_safe_open(), we may as well use the low-level Unix function to remove too. Change-Id: Id8d5e3999fe94b03acc1fffd171c06c7efc0b0f0 Reviewed-by: Fabian Kosmale --- src/corelib/ipc/qsharedmemory_android.cpp | 13 ----- src/corelib/ipc/qsharedmemory_p.h | 2 - src/corelib/ipc/qsharedmemory_systemv.cpp | 45 ++++------------- src/corelib/ipc/qsystemsemaphore_p.h | 2 + src/corelib/ipc/qsystemsemaphore_systemv.cpp | 2 +- src/corelib/ipc/qtipccommon_p.h | 53 ++++++++++++++++++++ 6 files changed, 65 insertions(+), 52 deletions(-) create mode 100644 src/corelib/ipc/qtipccommon_p.h diff --git a/src/corelib/ipc/qsharedmemory_android.cpp b/src/corelib/ipc/qsharedmemory_android.cpp index 0cee2ab3af..854b9d2294 100644 --- a/src/corelib/ipc/qsharedmemory_android.cpp +++ b/src/corelib/ipc/qsharedmemory_android.cpp @@ -20,19 +20,6 @@ key_t QSharedMemoryPrivate::handle() return 0; } -#endif // QT_CONFIG(sharedmemory) - -#if QT_CONFIG(sharedmemory) || QT_CONFIG(systemsemaphore) -int QSharedMemoryPrivate::createUnixKeyFile(const QString &fileName) -{ - Q_UNUSED(fileName); - Q_UNIMPLEMENTED(); - return 0; -} -#endif // QT_CONFIG(sharedmemory) || QT_CONFIG(systemsemaphore) - -#if QT_CONFIG(sharedmemory) - bool QSharedMemoryPrivate::cleanHandle() { Q_UNIMPLEMENTED(); diff --git a/src/corelib/ipc/qsharedmemory_p.h b/src/corelib/ipc/qsharedmemory_p.h index 6d7973faf8..3a7a95e4d0 100644 --- a/src/corelib/ipc/qsharedmemory_p.h +++ b/src/corelib/ipc/qsharedmemory_p.h @@ -26,7 +26,6 @@ QT_BEGIN_NAMESPACE namespace QSharedMemoryPrivate { - int createUnixKeyFile(const QString &fileName); QString makePlatformSafeKey(const QString &key, const QString &prefix = QStringLiteral("qipc_sharedmemory_")); } @@ -93,7 +92,6 @@ public: bool lockedByMe = false; #endif - static int createUnixKeyFile(const QString &fileName); static QString makePlatformSafeKey(const QString &key, const QString &prefix = QStringLiteral("qipc_sharedmemory_")); #ifdef Q_OS_WIN diff --git a/src/corelib/ipc/qsharedmemory_systemv.cpp b/src/corelib/ipc/qsharedmemory_systemv.cpp index de6b40746c..dd0e93d703 100644 --- a/src/corelib/ipc/qsharedmemory_systemv.cpp +++ b/src/corelib/ipc/qsharedmemory_systemv.cpp @@ -1,11 +1,11 @@ // Copyright (C) 2016 The Qt Company Ltd. // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only -#include "qplatformdefs.h" - #include "qsharedmemory.h" #include "qsharedmemory_p.h" -#include "qsystemsemaphore.h" + +#include "qtipccommon_p.h" + #include #include @@ -28,6 +28,7 @@ QT_BEGIN_NAMESPACE using namespace Qt::StringLiterals; +using namespace QtIpcCommon; /*! \internal @@ -63,35 +64,6 @@ key_t QSharedMemoryPrivate::handle() return unix_key; } -#endif // QT_CONFIG(sharedmemory) - -#if QT_CONFIG(sharedmemory) || QT_CONFIG(systemsemaphore) -/*! - \internal - Creates the unix file if needed. - returns \c true if the unix file was created. - - -1 error - 0 already existed - 1 created - */ -int QT_PREPEND_NAMESPACE(QSharedMemoryPrivate)::createUnixKeyFile(const QString &fileName) -{ - int fd = qt_safe_open(QFile::encodeName(fileName).constData(), - O_EXCL | O_CREAT | O_RDWR, 0640); - if (-1 == fd) { - if (errno == EEXIST) - return 0; - return -1; - } else { - close(fd); - } - return 1; -} -#endif // QT_CONFIG(sharedmemory) || QT_CONFIG(systemsemaphore) - -#if QT_CONFIG(sharedmemory) - bool QSharedMemoryPrivate::cleanHandle() { unix_key = 0; @@ -102,7 +74,8 @@ bool QSharedMemoryPrivate::create(qsizetype size) { // build file if needed bool createdFile = false; - int built = createUnixKeyFile(nativeKey); + QByteArray nativeKeyFile = QFile::encodeName(nativeKey); + int built = createUnixKeyFile(nativeKeyFile); if (built == -1) { errorString = QSharedMemory::tr("%1: unable to make key").arg("QSharedMemory::handle:"_L1); error = QSharedMemory::KeyError; @@ -115,7 +88,7 @@ bool QSharedMemoryPrivate::create(qsizetype size) // get handle if (!handle()) { if (createdFile) - QFile::remove(nativeKey); + unlink(nativeKeyFile); return false; } @@ -131,7 +104,7 @@ bool QSharedMemoryPrivate::create(qsizetype size) setErrorString(function); } if (createdFile && error != QSharedMemory::AlreadyExists) - QFile::remove(nativeKey); + unlink(nativeKeyFile); return false; } @@ -212,7 +185,7 @@ bool QSharedMemoryPrivate::detach() } // remove file - if (!QFile::remove(nativeKey)) + if (!unlink(QFile::encodeName(nativeKey))) return false; } return true; diff --git a/src/corelib/ipc/qsystemsemaphore_p.h b/src/corelib/ipc/qsystemsemaphore_p.h index 47c9cdfe22..6a4cb73982 100644 --- a/src/corelib/ipc/qsystemsemaphore_p.h +++ b/src/corelib/ipc/qsystemsemaphore_p.h @@ -20,7 +20,9 @@ #if QT_CONFIG(systemsemaphore) #include "qcoreapplication.h" +#include "qtipccommon_p.h" #include "qsharedmemory_p.h" + #include #ifdef QT_POSIX_IPC # include diff --git a/src/corelib/ipc/qsystemsemaphore_systemv.cpp b/src/corelib/ipc/qsystemsemaphore_systemv.cpp index f22c822979..41fe11dc0e 100644 --- a/src/corelib/ipc/qsystemsemaphore_systemv.cpp +++ b/src/corelib/ipc/qsystemsemaphore_systemv.cpp @@ -63,7 +63,7 @@ key_t QSystemSemaphorePrivate::handle(QSystemSemaphore::AccessMode mode) return unix_key; // Create the file needed for ftok - int built = QSharedMemoryPrivate::createUnixKeyFile(fileName); + int built = QtIpcCommon::createUnixKeyFile(QFile::encodeName(fileName)); if (-1 == built) { errorString = QSystemSemaphore::tr("%1: unable to make key") .arg("QSystemSemaphore::handle:"_L1); diff --git a/src/corelib/ipc/qtipccommon_p.h b/src/corelib/ipc/qtipccommon_p.h new file mode 100644 index 0000000000..bb8dacaed1 --- /dev/null +++ b/src/corelib/ipc/qtipccommon_p.h @@ -0,0 +1,53 @@ +// Copyright (C) 2022 Intel Corporation. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only + +#ifndef QTIPCCOMMON_P_H +#define QTIPCCOMMON_P_H + +// +// W A R N I N G +// ------------- +// +// This file is not part of the Qt API. It exists purely as an +// implementation detail. This header file may change from version to +// version without notice, or even be removed. +// +// We mean it. +// + +#include + +#if QT_CONFIG(sharedmemory) || QT_CONFIG(systemsemaphore) + +#if defined(Q_OS_UNIX) +# include +# include +#endif + +QT_BEGIN_NAMESPACE + +namespace QtIpcCommon { +#ifdef Q_OS_UNIX +// Convenience function to create the file if needed +inline int createUnixKeyFile(const QByteArray &fileName) +{ + int fd = qt_safe_open(fileName.constData(), O_EXCL | O_CREAT | O_RDWR, 0640); + if (fd < 0) { + if (errno == EEXIST) + return 0; + return -1; + } else { + close(fd); + } + return 1; + +} +#endif // Q_OS_UNIX +} // namespace QtIpcCommon + +QT_END_NAMESPACE + +#endif // QT_CONFIG(sharedmemory) || QT_CONFIG(systemsemaphore) + + +#endif // QTIPCCOMMON_P_H