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#ifdef Q_OS_OHOS
74 return false;
75#else
76 break;
77#endif
78
79 case QNativeIpcKey::Type::PosixRealtime:
80 if (ipcType == IpcType::SharedMemory)
81 return QT_CONFIG(posix_shm);
82 return QT_CONFIG(posix_sem);
83
84 case QNativeIpcKey::Type::Windows:
85#ifdef Q_OS_WIN
86 return true;
87#else
88 return false;
89#endif
90 }
91
92 if (ipcType == IpcType::SharedMemory)
93 return QT_CONFIG(sysv_shm);
94 return QT_CONFIG(sysv_sem);
95}
96
97template <auto Member1, auto... Members> class IpcStorageVariant
98{
99 template <typename T, typename C> static C extractClass(T C::*);
100 template <typename T, typename C> static T extractObject(T C::*);
101
102 template <auto M>
103 static constexpr bool IsEnabled = decltype(extractObject(M))::Enabled;
104
105 static_assert(std::is_member_object_pointer_v<decltype(Member1)>);
106 using StorageType = decltype(extractClass(Member1));
107 StorageType d;
108
109public:
110 template <typename Lambda> static auto
111 visit_internal(StorageType &storage, QNativeIpcKey::Type keyType, const Lambda &lambda)
112 {
113 if constexpr ((IsEnabled<Member1> || ... || IsEnabled<Members>)) {
114 if constexpr (IsEnabled<Member1>) {
115 using MemberType1 = decltype(extractObject(Member1));
116 if (MemberType1::supports(keyType))
117 return lambda(&(storage.*Member1));
118 }
119 if constexpr ((... || IsEnabled<Members>))
120 return IpcStorageVariant<Members...>::visit_internal(storage, keyType, lambda);
121 Q_UNREACHABLE();
122 } else {
123 // no backends enabled, but we can't return void
124 return false;
125 }
126 }
127
128 template <typename Lambda> auto visit(QNativeIpcKey::Type keyType, const Lambda &lambda)
129 {
130 return visit_internal(d, keyType, lambda);
131 }
132
133 template <typename Lambda> static auto
134 staticVisit(QNativeIpcKey::Type keyType, const Lambda &lambda)
135 {
136 if constexpr ((IsEnabled<Member1> || ... || IsEnabled<Members>)) {
137 if constexpr (IsEnabled<Member1>) {
138 using MemberType1 = decltype(extractObject(Member1));
139 if (MemberType1::supports(keyType))
140 return lambda(static_cast<MemberType1 *>(nullptr));
141 }
142 if constexpr ((... || IsEnabled<Members>))
143 return IpcStorageVariant<Members...>::staticVisit(keyType, lambda);
144 Q_UNREACHABLE();
145 } else {
146 // no backends enabled, but we can't return void
147 return false;
148 }
149 }
150};
151
152QNativeIpcKey legacyPlatformSafeKey(const QString &key, IpcType ipcType, QNativeIpcKey::Type type);
153QNativeIpcKey platformSafeKey(const QString &key, IpcType ipcType, QNativeIpcKey::Type type);
154
155#ifdef Q_OS_UNIX
156// Convenience function to create the file if needed
157inline int createUnixKeyFile(const QByteArray &fileName)
158{
159 int fd = qt_safe_open(fileName.constData(), O_EXCL | O_CREAT | O_RDWR, 0640);
160 if (fd < 0) {
161 if (errno == EEXIST)
162 return 0;
163 return -1;
164 } else {
165 close(fd);
166 }
167 return 1;
168
169}
170#endif // Q_OS_UNIX
171} // namespace QtIpcCommon
172
173QT_END_NAMESPACE
174
175#endif // QT_CONFIG(sharedmemory) || QT_CONFIG(systemsemaphore)
176
177
178#endif // QTIPCCOMMON_P_H
\inmodule QtSql
Combined button and popup list for selecting options.