Qt 6.x
The Qt SDK
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
4#ifndef QTIPCCOMMON_P_H
5#define QTIPCCOMMON_P_H
6
7//
8// W A R N I N G
9// -------------
10//
11// This file is not part of the Qt API. It exists purely as an
12// implementation detail. This header file may change from version to
13// version without notice, or even be removed.
14//
15// We mean it.
16//
17
18#include "qtipccommon.h"
19#include <private/qglobal_p.h>
20#include <private/qtcore-config_p.h>
21
22#if QT_CONFIG(sharedmemory) || QT_CONFIG(systemsemaphore)
23
24#if defined(Q_OS_UNIX)
25# include <qfile.h>
26# include <private/qcore_unix_p.h>
27#endif
28
30
31class QNativeIpcKeyPrivate
32{
33public:
34 QNativeIpcKey::Type type = {};
35
36 friend bool operator==(const QNativeIpcKeyPrivate &lhs, const QNativeIpcKeyPrivate &rhs)
37 {
38 return lhs.type == rhs.type;
39 }
40};
41
42inline QNativeIpcKeyPrivate *QNativeIpcKey::d_func()
43{
44 Q_ASSERT(d & 1); // Q_ASSERT(isSlowPath) but without the unlikely
45 return reinterpret_cast<QNativeIpcKeyPrivate *>(d & ~1);
46}
47
48inline const QNativeIpcKeyPrivate *QNativeIpcKey::d_func() const
49{
50 Q_ASSERT(d & 1); // Q_ASSERT(isSlowPath) but without the unlikely
51 return reinterpret_cast<QNativeIpcKeyPrivate *>(d & ~1);
52}
53
54namespace QtIpcCommon {
55enum class IpcType {
56 SharedMemory,
57 SystemSemaphore
58};
59
60static constexpr bool isIpcSupported(IpcType ipcType, QNativeIpcKey::Type type)
61{
62 switch (type) {
63 case QNativeIpcKey::Type::SystemV:
64 break;
65
66 case QNativeIpcKey::Type::PosixRealtime:
67 if (ipcType == IpcType::SharedMemory)
68 return QT_CONFIG(posix_shm);
69 return QT_CONFIG(posix_sem);
70
71 case QNativeIpcKey::Type::Windows:
72#ifdef Q_OS_WIN
73 return true;
74#else
75 return false;
76#endif
77 }
78
79 if (ipcType == IpcType::SharedMemory)
80 return QT_CONFIG(sysv_shm);
81 return QT_CONFIG(sysv_sem);
82}
83
84template <auto Member1, auto... Members> class IpcStorageVariant
85{
86 template <typename T, typename C> static C extractClass(T C::*);
87 template <typename T, typename C> static T extractObject(T C::*);
88
89 template <auto M>
90 static constexpr bool IsEnabled = decltype(extractObject(M))::Enabled;
91
92 static_assert(std::is_member_object_pointer_v<decltype(Member1)>);
93 using StorageType = decltype(extractClass(Member1));
94 StorageType d;
95
96public:
97 template <typename Lambda> static auto
98 visit_internal(StorageType &storage, QNativeIpcKey::Type keyType, const Lambda &lambda)
99 {
100 if constexpr ((IsEnabled<Member1> || ... || IsEnabled<Members>)) {
101 if constexpr (IsEnabled<Member1>) {
102 using MemberType1 = decltype(extractObject(Member1));
103 if (MemberType1::supports(keyType))
104 return lambda(&(storage.*Member1));
105 }
106 if constexpr ((... || IsEnabled<Members>))
107 return IpcStorageVariant<Members...>::visit_internal(storage, keyType, lambda);
108 Q_UNREACHABLE();
109 } else {
110 // no backends enabled, but we can't return void
111 return false;
112 }
113 }
114
115 template <typename Lambda> auto visit(QNativeIpcKey::Type keyType, const Lambda &lambda)
116 {
117 return visit_internal(d, keyType, lambda);
118 }
119
120 template <typename Lambda> static auto
121 staticVisit(QNativeIpcKey::Type keyType, const Lambda &lambda)
122 {
123 if constexpr ((IsEnabled<Member1> || ... || IsEnabled<Members>)) {
124 if constexpr (IsEnabled<Member1>) {
125 using MemberType1 = decltype(extractObject(Member1));
126 if (MemberType1::supports(keyType))
127 return lambda(static_cast<MemberType1 *>(nullptr));
128 }
129 if constexpr ((... || IsEnabled<Members>))
130 return IpcStorageVariant<Members...>::staticVisit(keyType, lambda);
131 Q_UNREACHABLE();
132 } else {
133 // no backends enabled, but we can't return void
134 return false;
135 }
136 }
137};
138
140legacyPlatformSafeKey(const QString &key, IpcType ipcType,
141 QNativeIpcKey::Type type = QNativeIpcKey::legacyDefaultTypeForOs());
142Q_AUTOTEST_EXPORT QString platformSafeKey(const QString &key, IpcType ipcType, QNativeIpcKey::Type type);
143
144#ifdef Q_OS_UNIX
145// Convenience function to create the file if needed
146inline int createUnixKeyFile(const QByteArray &fileName)
147{
148 int fd = qt_safe_open(fileName.constData(), O_EXCL | O_CREAT | O_RDWR, 0640);
149 if (fd < 0) {
150 if (errno == EEXIST)
151 return 0;
152 return -1;
153 } else {
154 close(fd);
155 }
156 return 1;
157
158}
159#endif // Q_OS_UNIX
160} // namespace QtIpcCommon
161
163
164#endif // QT_CONFIG(sharedmemory) || QT_CONFIG(systemsemaphore)
165
166
167#endif // QTIPCCOMMON_P_H
\inmodule QtCore
Definition qbytearray.h:57
\macro QT_RESTRICTED_CAST_FROM_ASCII
Definition qstring.h:127
Combined button and popup list for selecting options.
std::conditional_t< sizeof(Layout1)<=sizeof(Layout2), Layout1, Layout2 > Members
static int qt_safe_open(const char *pathname, int flags, mode_t mode=0777)
GLuint64 key
GLenum type
GLuint64 GLenum GLint fd
bool operator==(const QRandomGenerator &rng1, const QRandomGenerator &rng2)
Definition qrandom.cpp:1219
#define Q_ASSERT(cond)
Definition qrandom.cpp:47
#define M(_x, _y)
#define Q_AUTOTEST_EXPORT
#define QT_CONFIG(feature)
QStorageInfo storage
[1]