1 | /****************************************************************************
|
---|
2 | **
|
---|
3 | ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
|
---|
4 | ** Contact: Qt Software Information ([email protected])
|
---|
5 | **
|
---|
6 | ** This file is part of the QtNetwork module of the Qt Toolkit.
|
---|
7 | **
|
---|
8 | ** $QT_BEGIN_LICENSE:LGPL$
|
---|
9 | ** Commercial Usage
|
---|
10 | ** Licensees holding valid Qt Commercial licenses may use this file in
|
---|
11 | ** accordance with the Qt Commercial License Agreement provided with the
|
---|
12 | ** Software or, alternatively, in accordance with the terms contained in
|
---|
13 | ** a written agreement between you and Nokia.
|
---|
14 | **
|
---|
15 | ** GNU Lesser General Public License Usage
|
---|
16 | ** Alternatively, this file may be used under the terms of the GNU Lesser
|
---|
17 | ** General Public License version 2.1 as published by the Free Software
|
---|
18 | ** Foundation and appearing in the file LICENSE.LGPL included in the
|
---|
19 | ** packaging of this file. Please review the following information to
|
---|
20 | ** ensure the GNU Lesser General Public License version 2.1 requirements
|
---|
21 | ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
|
---|
22 | **
|
---|
23 | ** In addition, as a special exception, Nokia gives you certain
|
---|
24 | ** additional rights. These rights are described in the Nokia Qt LGPL
|
---|
25 | ** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this
|
---|
26 | ** package.
|
---|
27 | **
|
---|
28 | ** GNU General Public License Usage
|
---|
29 | ** Alternatively, this file may be used under the terms of the GNU
|
---|
30 | ** General Public License version 3.0 as published by the Free Software
|
---|
31 | ** Foundation and appearing in the file LICENSE.GPL included in the
|
---|
32 | ** packaging of this file. Please review the following information to
|
---|
33 | ** ensure the GNU General Public License version 3.0 requirements will be
|
---|
34 | ** met: http://www.gnu.org/copyleft/gpl.html.
|
---|
35 | **
|
---|
36 | ** If you are unsure which license is appropriate for your use, please
|
---|
37 | ** contact the sales department at [email protected].
|
---|
38 | ** $QT_END_LICENSE$
|
---|
39 | **
|
---|
40 | ****************************************************************************/
|
---|
41 |
|
---|
42 | #include "qlocalserver.h"
|
---|
43 | #include "qlocalserver_p.h"
|
---|
44 | #include "qlocalsocket.h"
|
---|
45 | #include "qlocalsocket_p.h"
|
---|
46 |
|
---|
47 | #ifndef QT_NO_LOCALSERVER
|
---|
48 |
|
---|
49 | #include <sys/socket.h>
|
---|
50 | #include <sys/un.h>
|
---|
51 |
|
---|
52 | #include <qdebug.h>
|
---|
53 | #include <qdir.h>
|
---|
54 | #include <qdatetime.h>
|
---|
55 |
|
---|
56 | QT_BEGIN_NAMESPACE
|
---|
57 |
|
---|
58 | void QLocalServerPrivate::init()
|
---|
59 | {
|
---|
60 | }
|
---|
61 |
|
---|
62 | bool QLocalServerPrivate::removeServer(const QString &name)
|
---|
63 | {
|
---|
64 | QString fileName;
|
---|
65 | if (name.startsWith(QLatin1Char('/'))) {
|
---|
66 | fileName = name;
|
---|
67 | } else {
|
---|
68 | fileName = QDir::cleanPath(QDir::tempPath());
|
---|
69 | fileName += QLatin1Char('/') + name;
|
---|
70 | }
|
---|
71 | if (QFile::exists(fileName))
|
---|
72 | return QFile::remove(fileName);
|
---|
73 | else
|
---|
74 | return true;
|
---|
75 | }
|
---|
76 |
|
---|
77 | bool QLocalServerPrivate::listen(const QString &requestedServerName)
|
---|
78 | {
|
---|
79 | Q_Q(QLocalServer);
|
---|
80 |
|
---|
81 | // determine the full server path
|
---|
82 | if (requestedServerName.startsWith(QLatin1Char('/'))) {
|
---|
83 | fullServerName = requestedServerName;
|
---|
84 | } else {
|
---|
85 | fullServerName = QDir::cleanPath(QDir::tempPath());
|
---|
86 | fullServerName += QLatin1Char('/') + requestedServerName;
|
---|
87 | }
|
---|
88 | serverName = requestedServerName;
|
---|
89 |
|
---|
90 | // create the unix socket
|
---|
91 | listenSocket = qSocket(PF_UNIX, SOCK_STREAM, 0);
|
---|
92 | if (-1 == listenSocket) {
|
---|
93 | setError(QLatin1String("QLocalServer::listen"));
|
---|
94 | closeServer();
|
---|
95 | return false;
|
---|
96 | }
|
---|
97 |
|
---|
98 | // Construct the unix address
|
---|
99 | struct ::sockaddr_un addr;
|
---|
100 | addr.sun_family = PF_UNIX;
|
---|
101 | if (sizeof(addr.sun_path) < (uint)fullServerName.toLatin1().size() + 1) {
|
---|
102 | setError(QLatin1String("QLocalServer::listen"));
|
---|
103 | closeServer();
|
---|
104 | return false;
|
---|
105 | }
|
---|
106 | ::memcpy(addr.sun_path, fullServerName.toLatin1().data(),
|
---|
107 | fullServerName.toLatin1().size() + 1);
|
---|
108 |
|
---|
109 | // bind
|
---|
110 | if(-1 == qBind(listenSocket, (sockaddr *)&addr, sizeof(sockaddr_un))) {
|
---|
111 | setError(QLatin1String("QLocalServer::listen"));
|
---|
112 | // if address is in use already, just close the socket, but do not delete the file
|
---|
113 | if(errno == EADDRINUSE)
|
---|
114 | QT_CLOSE(listenSocket);
|
---|
115 | // otherwise, close the socket and delete the file
|
---|
116 | else
|
---|
117 | closeServer();
|
---|
118 | listenSocket = -1;
|
---|
119 | return false;
|
---|
120 | }
|
---|
121 |
|
---|
122 | // listen for connections
|
---|
123 | if (-1 == qListen(listenSocket, 50)) {
|
---|
124 | setError(QLatin1String("QLocalServer::listen"));
|
---|
125 | closeServer();
|
---|
126 | listenSocket = -1;
|
---|
127 | if (error != QAbstractSocket::AddressInUseError)
|
---|
128 | QFile::remove(fullServerName);
|
---|
129 | return false;
|
---|
130 | }
|
---|
131 | Q_ASSERT(!socketNotifier);
|
---|
132 | socketNotifier = new QSocketNotifier(listenSocket,
|
---|
133 | QSocketNotifier::Read, q);
|
---|
134 | q->connect(socketNotifier, SIGNAL(activated(int)),
|
---|
135 | q, SLOT(_q_socketActivated()));
|
---|
136 | socketNotifier->setEnabled(maxPendingConnections > 0);
|
---|
137 | return true;
|
---|
138 | }
|
---|
139 |
|
---|
140 | /*!
|
---|
141 | \internal
|
---|
142 |
|
---|
143 | \sa QLocalServer::closeServer()
|
---|
144 | */
|
---|
145 | void QLocalServerPrivate::closeServer()
|
---|
146 | {
|
---|
147 | if (-1 != listenSocket)
|
---|
148 | QT_CLOSE(listenSocket);
|
---|
149 | listenSocket = -1;
|
---|
150 |
|
---|
151 | if (socketNotifier)
|
---|
152 | socketNotifier->deleteLater();
|
---|
153 | socketNotifier = 0;
|
---|
154 |
|
---|
155 | if (!fullServerName.isEmpty())
|
---|
156 | QFile::remove(fullServerName);
|
---|
157 | }
|
---|
158 |
|
---|
159 | /*!
|
---|
160 | \internal
|
---|
161 |
|
---|
162 | We have received a notification that we can read on the listen socket.
|
---|
163 | Accept the new socket.
|
---|
164 | */
|
---|
165 | void QLocalServerPrivate::_q_socketActivated()
|
---|
166 | {
|
---|
167 | Q_Q(QLocalServer);
|
---|
168 | if (-1 == listenSocket)
|
---|
169 | return;
|
---|
170 |
|
---|
171 | ::sockaddr_un addr;
|
---|
172 | QT_SOCKLEN_T length = sizeof(sockaddr_un);
|
---|
173 | int connectedSocket = qAccept(listenSocket, (sockaddr *)&addr, &length);
|
---|
174 | if(-1 == connectedSocket) {
|
---|
175 | setError(QLatin1String("QLocalSocket::activated"));
|
---|
176 | closeServer();
|
---|
177 | } else {
|
---|
178 | socketNotifier->setEnabled(pendingConnections.size()
|
---|
179 | <= maxPendingConnections);
|
---|
180 | q->incomingConnection(connectedSocket);
|
---|
181 | }
|
---|
182 | }
|
---|
183 |
|
---|
184 | void QLocalServerPrivate::waitForNewConnection(int msec, bool *timedOut)
|
---|
185 | {
|
---|
186 | fd_set readfds;
|
---|
187 | FD_ZERO(&readfds);
|
---|
188 | FD_SET(listenSocket, &readfds);
|
---|
189 |
|
---|
190 | timeval timeout;
|
---|
191 | timeout.tv_sec = msec / 1000;
|
---|
192 | timeout.tv_usec = (msec % 1000) * 1000;
|
---|
193 |
|
---|
194 | // timeout can not be 0 or else select will return an error.
|
---|
195 | if (0 == msec)
|
---|
196 | timeout.tv_usec = 1000;
|
---|
197 |
|
---|
198 | int result = -1;
|
---|
199 | // on Linux timeout will be updated by select, but _not_ on other systems.
|
---|
200 | QTime timer;
|
---|
201 | timer.start();
|
---|
202 | while (pendingConnections.isEmpty() && (-1 == msec || timer.elapsed() < msec)) {
|
---|
203 | result = ::select(listenSocket + 1, &readfds, 0, 0, &timeout);
|
---|
204 | if (-1 == result && errno != EINTR) {
|
---|
205 | setError(QLatin1String("QLocalServer::waitForNewConnection"));
|
---|
206 | closeServer();
|
---|
207 | break;
|
---|
208 | }
|
---|
209 | if (result > 0)
|
---|
210 | _q_socketActivated();
|
---|
211 | }
|
---|
212 | if (timedOut)
|
---|
213 | *timedOut = (result == 0);
|
---|
214 | }
|
---|
215 |
|
---|
216 | void QLocalServerPrivate::setError(const QString &function)
|
---|
217 | {
|
---|
218 | if (EAGAIN == errno)
|
---|
219 | return;
|
---|
220 |
|
---|
221 | switch (errno) {
|
---|
222 | case EACCES:
|
---|
223 | errorString = QLocalServer::tr("%1: Permission denied").arg(function);
|
---|
224 | error = QAbstractSocket::SocketAccessError;
|
---|
225 | break;
|
---|
226 | case ELOOP:
|
---|
227 | case ENOENT:
|
---|
228 | case ENAMETOOLONG:
|
---|
229 | case EROFS:
|
---|
230 | case ENOTDIR:
|
---|
231 | errorString = QLocalServer::tr("%1: Name error").arg(function);
|
---|
232 | error = QAbstractSocket::HostNotFoundError;
|
---|
233 | break;
|
---|
234 | case EADDRINUSE:
|
---|
235 | errorString = QLocalServer::tr("%1: Address in use").arg(function);
|
---|
236 | error = QAbstractSocket::AddressInUseError;
|
---|
237 | break;
|
---|
238 |
|
---|
239 | default:
|
---|
240 | errorString = QLocalServer::tr("%1: Unknown error %2")
|
---|
241 | .arg(function).arg(errno);
|
---|
242 | error = QAbstractSocket::UnknownSocketError;
|
---|
243 | #if defined QLOCALSERVER_DEBUG
|
---|
244 | qWarning() << errorString << "fullServerName:" << fullServerName;
|
---|
245 | #endif
|
---|
246 | }
|
---|
247 | }
|
---|
248 |
|
---|
249 | QT_END_NAMESPACE
|
---|
250 |
|
---|
251 | #endif // QT_NO_LOCALSERVER
|
---|