1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
|
// 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 "qlocalserver.h"
#include "qlocalserver_p.h"
#include "qlocalsocket.h"
#include "qlocalsocket_p.h"
#include "qnet_unix_p.h"
#include "qtemporarydir.h"
#include <stddef.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <qdebug.h>
#include <qdir.h>
#include <qdatetime.h>
#include <optional>
#ifdef Q_OS_VXWORKS
# include <selectLib.h>
#endif
QT_BEGIN_NAMESPACE
using namespace Qt::StringLiterals;
namespace {
QLocalServer::SocketOptions optionsForPlatform(QLocalServer::SocketOptions srcOptions)
{
// For OS that does not support abstract namespace the AbstractNamespaceOption
// means that we go for WorldAccessOption - as it is the closest option in
// regards of access rights. In Linux/Android case we clean-up the access rights.
if (srcOptions.testFlag(QLocalServer::AbstractNamespaceOption)) {
if (PlatformSupportsAbstractNamespace)
return QLocalServer::AbstractNamespaceOption;
else
return QLocalServer::WorldAccessOption;
}
return srcOptions;
}
}
void QLocalServerPrivate::init()
{
}
bool QLocalServerPrivate::removeServer(const QString &name)
{
QString fileName;
if (name.startsWith(u'/')) {
fileName = name;
} else {
fileName = QDir::cleanPath(QDir::tempPath());
fileName += u'/' + name;
}
if (QFile::exists(fileName))
return QFile::remove(fileName);
else
return true;
}
bool QLocalServerPrivate::listen(const QString &requestedServerName)
{
Q_Q(QLocalServer);
// socket options adjusted for current platform
auto options = optionsForPlatform(socketOptions.value());
// determine the full server path
if (options.testFlag(QLocalServer::AbstractNamespaceOption)
|| requestedServerName.startsWith(u'/')) {
fullServerName = requestedServerName;
} else {
fullServerName = QDir::cleanPath(QDir::tempPath());
fullServerName += u'/' + requestedServerName;
}
serverName = requestedServerName;
QByteArray encodedTempPath;
const QByteArray encodedFullServerName = QFile::encodeName(fullServerName);
std::optional<QTemporaryDir> tempDir;
if (options & QLocalServer::WorldAccessOption) {
QFileInfo serverNameFileInfo(fullServerName);
tempDir.emplace(serverNameFileInfo.absolutePath() + u'/');
if (!tempDir->isValid()) {
setError("QLocalServer::listen"_L1);
return false;
}
encodedTempPath = QFile::encodeName(tempDir->path() + "/s"_L1);
}
// create the unix socket
listenSocket = qt_safe_socket(PF_UNIX, SOCK_STREAM, 0);
if (-1 == listenSocket) {
setError("QLocalServer::listen"_L1);
closeServer();
return false;
}
// Construct the unix address
struct ::sockaddr_un addr;
addr.sun_family = PF_UNIX;
::memset(addr.sun_path, 0, sizeof(addr.sun_path));
// for abstract namespace add 2 to length, to take into account trailing AND leading null
constexpr unsigned int extraCharacters = PlatformSupportsAbstractNamespace ? 2 : 1;
if (sizeof(addr.sun_path) < static_cast<size_t>(encodedFullServerName.size() + extraCharacters)) {
setError("QLocalServer::listen"_L1);
closeServer();
return false;
}
QT_SOCKLEN_T addrSize = sizeof(::sockaddr_un);
if (options.testFlag(QLocalServer::AbstractNamespaceOption)) {
// Abstract socket address is distinguished by the fact
// that sun_path[0] is a null byte ('\0')
::memcpy(addr.sun_path + 1, encodedFullServerName.constData(),
encodedFullServerName.size() + 1);
addrSize = offsetof(::sockaddr_un, sun_path) + encodedFullServerName.size() + 1;
} else if (options & QLocalServer::WorldAccessOption) {
if (sizeof(addr.sun_path) < static_cast<size_t>(encodedTempPath.size() + 1)) {
setError("QLocalServer::listen"_L1);
closeServer();
return false;
}
::memcpy(addr.sun_path, encodedTempPath.constData(),
encodedTempPath.size() + 1);
} else {
::memcpy(addr.sun_path, encodedFullServerName.constData(),
encodedFullServerName.size() + 1);
}
// bind
if (-1 == QT_SOCKET_BIND(listenSocket, (sockaddr *)&addr, addrSize)) {
setError("QLocalServer::listen"_L1);
// if address is in use already, just close the socket, but do not delete the file
if (errno == EADDRINUSE)
QT_CLOSE(listenSocket);
// otherwise, close the socket and delete the file
else
closeServer();
listenSocket = -1;
return false;
}
// listen for connections
if (-1 == qt_safe_listen(listenSocket, listenBacklog)) {
setError("QLocalServer::listen"_L1);
closeServer();
return false;
}
if (options & QLocalServer::WorldAccessOption) {
mode_t mode = 000;
if (options & QLocalServer::UserAccessOption)
mode |= S_IRWXU;
if (options & QLocalServer::GroupAccessOption)
mode |= S_IRWXG;
if (options & QLocalServer::OtherAccessOption)
mode |= S_IRWXO;
if (::chmod(encodedTempPath.constData(), mode) == -1) {
setError("QLocalServer::listen"_L1);
closeServer();
return false;
}
if (::rename(encodedTempPath.constData(), encodedFullServerName.constData()) == -1) {
setError("QLocalServer::listen"_L1);
closeServer();
return false;
}
}
Q_ASSERT(!socketNotifier);
socketNotifier = new QSocketNotifier(listenSocket,
QSocketNotifier::Read, q);
q->connect(socketNotifier, SIGNAL(activated(QSocketDescriptor)),
q, SLOT(_q_onNewConnection()));
socketNotifier->setEnabled(maxPendingConnections > 0);
return true;
}
bool QLocalServerPrivate::listen(qintptr socketDescriptor)
{
Q_Q(QLocalServer);
// Attach to the localsocket
listenSocket = socketDescriptor;
::fcntl(listenSocket, F_SETFD, FD_CLOEXEC);
::fcntl(listenSocket, F_SETFL, ::fcntl(listenSocket, F_GETFL) | O_NONBLOCK);
bool abstractAddress = false;
struct ::sockaddr_un addr;
QT_SOCKLEN_T len = sizeof(addr);
memset(&addr, 0, sizeof(addr));
if (::getsockname(socketDescriptor, (sockaddr *)&addr, &len) == 0) {
#if defined(Q_OS_QNX)
if (addr.sun_path[0] == 0 && addr.sun_path[1] == 0)
len = SUN_LEN(&addr);
#endif
if (QLocalSocketPrivate::parseSockaddr(addr, len, fullServerName, serverName,
abstractAddress)) {
QLocalServer::SocketOptions options = socketOptions.value();
socketOptions = options.setFlag(QLocalServer::AbstractNamespaceOption, abstractAddress);
}
}
Q_ASSERT(!socketNotifier);
socketNotifier = new QSocketNotifier(listenSocket,
QSocketNotifier::Read, q);
q->connect(socketNotifier, SIGNAL(activated(QSocketDescriptor)),
q, SLOT(_q_onNewConnection()));
socketNotifier->setEnabled(maxPendingConnections > 0);
return true;
}
/*!
\internal
\sa QLocalServer::closeServer()
*/
void QLocalServerPrivate::closeServer()
{
if (socketNotifier) {
socketNotifier->setEnabled(false); // Otherwise, closed socket is checked before deleter runs
socketNotifier->deleteLater();
socketNotifier = nullptr;
}
if (-1 != listenSocket)
QT_CLOSE(listenSocket);
listenSocket = -1;
if (!fullServerName.isEmpty()
&& !optionsForPlatform(socketOptions).testFlag(QLocalServer::AbstractNamespaceOption)) {
QFile::remove(fullServerName);
}
serverName.clear();
fullServerName.clear();
}
/*!
\internal
We have received a notification that we can read on the listen socket.
Accept the new socket.
*/
void QLocalServerPrivate::_q_onNewConnection()
{
Q_Q(QLocalServer);
if (-1 == listenSocket)
return;
::sockaddr_un addr;
QT_SOCKLEN_T length = sizeof(sockaddr_un);
int connectedSocket = qt_safe_accept(listenSocket, (sockaddr *)&addr, &length);
if (-1 == connectedSocket) {
setError("QLocalSocket::activated"_L1);
closeServer();
} else {
socketNotifier->setEnabled(pendingConnections.size()
<= maxPendingConnections);
q->incomingConnection(connectedSocket);
}
}
void QLocalServerPrivate::waitForNewConnection(int msec, bool *timedOut)
{
pollfd pfd = qt_make_pollfd(listenSocket, POLLIN);
switch (qt_safe_poll(&pfd, 1, QDeadlineTimer(msec))) {
case 0:
if (timedOut)
*timedOut = true;
return;
break;
default:
if ((pfd.revents & POLLNVAL) == 0) {
_q_onNewConnection();
return;
}
errno = EBADF;
Q_FALLTHROUGH();
case -1:
setError("QLocalServer::waitForNewConnection"_L1);
closeServer();
break;
}
}
void QLocalServerPrivate::setError(const QString &function)
{
if (EAGAIN == errno)
return;
switch (errno) {
case EACCES:
errorString = QLocalServer::tr("%1: Permission denied").arg(function);
error = QAbstractSocket::SocketAccessError;
break;
case ELOOP:
case ENOENT:
case ENAMETOOLONG:
case EROFS:
case ENOTDIR:
errorString = QLocalServer::tr("%1: Name error").arg(function);
error = QAbstractSocket::HostNotFoundError;
break;
case EADDRINUSE:
errorString = QLocalServer::tr("%1: Address in use").arg(function);
error = QAbstractSocket::AddressInUseError;
break;
default:
errorString = QLocalServer::tr("%1: Unknown error %2")
.arg(function).arg(errno);
error = QAbstractSocket::UnknownSocketError;
#if defined QLOCALSERVER_DEBUG
qWarning() << errorString << "fullServerName:" << fullServerName;
#endif
}
}
QT_END_NAMESPACE
|