summaryrefslogtreecommitdiffstats
path: root/src/network/socket/qlocalserver_unix.cpp
diff options
context:
space:
mode:
authorAndrew Stanley-Jones <andrew.stanley-jones@nokia.com>2012-02-09 15:51:22 +0100
committerQt by Nokia <qt-info@nokia.com>2012-02-15 02:36:02 +0100
commit980947122307797e1d8da03f768d8f14a360d20b (patch)
tree309b9a526c1563def2018d649dc536c82b926cdb /src/network/socket/qlocalserver_unix.cpp
parentfc8f92106d6743d4165de7d8a440b7e5dbd14391 (diff)
Allow the QLocalServer to listen to a native descriptor
QLocalServer could only listen to sockets it created. Thi is not always possible as sockets may be passed by socketpair() or have to be created locally by other means. This adds a similar feature to QLocalSocket where a native descriptor maybe used. Change-Id: I43b0af179b3b868dd164d4e1fd312ff4546cf9ff Reviewed-by: Michalina Ziemba <michalina.ziemba@nokia.com> Reviewed-by: Tapani Mikola <tapani.mikola@nokia.com> Reviewed-by: Harald Fernengel <harald.fernengel@nokia.com>
Diffstat (limited to 'src/network/socket/qlocalserver_unix.cpp')
-rw-r--r--src/network/socket/qlocalserver_unix.cpp42
1 files changed, 42 insertions, 0 deletions
diff --git a/src/network/socket/qlocalserver_unix.cpp b/src/network/socket/qlocalserver_unix.cpp
index ce0c283f0b..0c2edef578 100644
--- a/src/network/socket/qlocalserver_unix.cpp
+++ b/src/network/socket/qlocalserver_unix.cpp
@@ -203,6 +203,48 @@ bool QLocalServerPrivate::listen(const QString &requestedServerName)
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);
+
+#ifdef Q_OS_LINUX
+ struct ::sockaddr_un addr;
+ socklen_t len;
+ memset(&addr, 0, sizeof(addr));
+ if (0 == ::getsockname(listenSocket, (sockaddr *)&addr, &len)) {
+ // check for absract sockets
+ if (addr.sun_family == PF_UNIX && addr.sun_path[0] == 0) {
+ addr.sun_path[0] = '@';
+ }
+ QString name = QString::fromLatin1(addr.sun_path);
+ if (!name.isEmpty()) {
+ fullServerName = name;
+ serverName = fullServerName.mid(fullServerName.lastIndexOf(QLatin1String("/"))+1);
+ if (serverName.isEmpty()) {
+ serverName = fullServerName;
+ }
+ }
+ }
+#else
+ serverName.clear();
+ fullServerName.clear();
+#endif
+
+ Q_ASSERT(!socketNotifier);
+ socketNotifier = new QSocketNotifier(listenSocket,
+ QSocketNotifier::Read, q);
+ q->connect(socketNotifier, SIGNAL(activated(int)),
+ q, SLOT(_q_onNewConnection()));
+ socketNotifier->setEnabled(maxPendingConnections > 0);
+ return true;
+}
+
/*!
\internal