Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qtipccommon_p.h
Go to the documentation of this file.
1// Copyright (C) 2022 Intel Corporation.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3// Qt-Security score:significant reason:default
4
5#ifndef QTIPCCOMMON_P_H
6#define QTIPCCOMMON_P_H
7
8//
9// W A R N I N G
10// -------------
11//
12// This file is not part of the Qt API. It exists purely as an
13// implementation detail. This header file may change from version to
14// version without notice, or even be removed.
15//
16// We mean it.
17//
18
19#include "qtipccommon.h"
20#include <private/qglobal_p.h>
21#include <private/qtcore-config_p.h>
22
23#if QT_CONFIG(sharedmemory) || QT_CONFIG(systemsemaphore)
24
25#if defined(Q_OS_UNIX)
26# include <qfile.h>
27# include <private/qcore_unix_p.h>
28#endif
29
30QT_BEGIN_NAMESPACE
31
32class QNativeIpcKeyPrivate
33{
34public:
35 QString legacyKey_;
36
37 static QString legacyKey(const QNativeIpcKey &key)
38 {
39 if (key.isSlowPath())
40 return key.d->legacyKey_;
41 return QString();
42 }
43 static void setLegacyKey(QNativeIpcKey &key, const QString &legacyKey)
44 {
45 QNativeIpcKeyPrivate::makeExtended(key)->legacyKey_ = legacyKey;
46 }
47 static void setNativeAndLegacyKeys(QNativeIpcKey &key, const QString &nativeKey,
48 const QString &legacyKey)
49 {
50 key.setNativeKey(nativeKey);
51 setLegacyKey(key, legacyKey);
52 }
53
54private:
55 static QNativeIpcKeyPrivate *makeExtended(QNativeIpcKey &key)
56 {
57 if (!key.isSlowPath())
58 key.d = new QNativeIpcKeyPrivate;
59 return key.d;
60 }
61};
62
63namespace QtIpcCommon {
64enum class IpcType {
65 SharedMemory,
66 SystemSemaphore
67};
68
69constexpr bool isIpcSupported(IpcType ipcType, QNativeIpcKey::Type type)
70{
71 switch (type) {
72 case QNativeIpcKey::Type::SystemV:
73 break;
74
75 case QNativeIpcKey::Type::PosixRealtime:
76 if (ipcType == IpcType::SharedMemory)
77 return QT_CONFIG(posix_shm);
78 return QT_CONFIG(posix_sem);
79
80 case QNativeIpcKey::Type::Windows:
81#ifdef Q_OS_WIN
82 return true;
83#else
84 return false;
85#endif
86 }
87
88 if (ipcType == IpcType::SharedMemory)
89 return QT_CONFIG(sysv_shm);
90 return QT_CONFIG(sysv_sem);
91}
92
93template <auto Member1, auto... Members> class IpcStorageVariant
94{
95 template <typename T, typename C> static C extractClass(T C::*);
96 template <typename T, typename C> static T extractObject(T C::*);
97
98 template <auto M>
99 static constexpr bool IsEnabled = decltype(extractObject(M))::Enabled;
100
101 static_assert(std::is_member_object_pointer_v<decltype(Member1)>);
102 using StorageType = decltype(extractClass(Member1));
103 StorageType d;
104
105public:
106 template <typename Lambda> static auto
107 visit_internal(StorageType &storage, QNativeIpcKey::Type keyType, const Lambda &lambda)
108 {
109 if constexpr ((IsEnabled<Member1> || ... || IsEnabled<Members>)) {
110 if constexpr (IsEnabled<Member1>) {
111 using MemberType1 = decltype(extractObject(Member1));
112 if (MemberType1::supports(keyType))
113 return lambda(&(storage.*Member1));
114 }
115 if constexpr ((... || IsEnabled<Members>))
116 return IpcStorageVariant<Members...>::visit_internal(storage, keyType, lambda);
117 Q_UNREACHABLE();
118 } else {
119 // no backends enabled, but we can't return void
120 return false;
121 }
122 }
123
124 template <typename Lambda> auto visit(QNativeIpcKey::Type keyType, const Lambda &lambda)
125 {
126 return visit_internal(d, keyType, lambda);
127 }
128
129 template <typename Lambda> static auto
130 staticVisit(QNativeIpcKey::Type keyType, const Lambda &lambda)
131 {
132 if constexpr ((IsEnabled<Member1> || ... || IsEnabled<Members>)) {
133 if constexpr (IsEnabled<Member1>) {
134 using MemberType1 = decltype(extractObject(Member1));
135 if (MemberType1::supports(keyType))
136 return lambda(static_cast<MemberType1 *>(nullptr));
137 }
138 if constexpr ((... || IsEnabled<Members>))
139 return IpcStorageVariant<Members...>::staticVisit(keyType, lambda);
140 Q_UNREACHABLE();
141 } else {
142 // no backends enabled, but we can't return void
143 return false;
144 }
145 }
146};
147
148QNativeIpcKey legacyPlatformSafeKey(const QString &key, IpcType ipcType, QNativeIpcKey::Type type);
149QNativeIpcKey platformSafeKey(const QString &key, IpcType ipcType, QNativeIpcKey::Type type);
150
151#ifdef Q_OS_UNIX
152// Convenience function to create the file if needed
153inline int createUnixKeyFile(const QByteArray &fileName)
154{
155 int fd = qt_safe_open(fileName.constData(), O_EXCL | O_CREAT | O_RDWR, 0640);
156 if (fd < 0) {
157 if (errno == EEXIST)
158 return 0;
159 return -1;
160 } else {
161 close(fd);
162 }
163 return 1;
164
165}
166#endif // Q_OS_UNIX
167} // namespace QtIpcCommon
168
169QT_END_NAMESPACE
170
171#endif // QT_CONFIG(sharedmemory) || QT_CONFIG(systemsemaphore)
172
173
174#endif // QTIPCCOMMON_P_H
\inmodule QtSql