[Libreoffice-commits] online.git: 3 commits - wsd/LOOLWSD.cpp

Tor Lillqvist tml at collabora.com
Fri Jan 13 11:40:10 UTC 2017


 wsd/LOOLWSD.cpp |   52 ++++++++++++++++++++++++++++++++++++++--------------
 1 file changed, 38 insertions(+), 14 deletions(-)

New commits:
commit 40e5a667018c6d801901424882612f400bbf0c07
Author: Tor Lillqvist <tml at collabora.com>
Date:   Fri Jan 13 13:39:10 2017 +0200

    No double colon in English text;)
    
    Change-Id: I3e25a5e1be8672a7796a71a28c2979a30ab326a3

diff --git a/wsd/LOOLWSD.cpp b/wsd/LOOLWSD.cpp
index f7c2399..65452fa 100644
--- a/wsd/LOOLWSD.cpp
+++ b/wsd/LOOLWSD.cpp
@@ -1838,7 +1838,7 @@ void LOOLWSD::initialize(Application& self)
 
     std::string adminURI = getAdminURI(config());
     if (!adminURI.empty())
-        std::cerr << "\nOr for the Admin Console see:: \n\n"
+        std::cerr << "\nOr for the Admin Console:\n\n"
                   << adminURI << '\n' << std::endl;
 #endif
 }
commit 591fc0238474f100d6eab3de6d9a7c2dddb2d1f7
Author: Tor Lillqvist <tml at collabora.com>
Date:   Fri Jan 13 13:36:12 2017 +0200

    No Hungarian notation please
    
    Change-Id: I0701dfbecb0b6056c0453d83bac9e85a98eff366

diff --git a/wsd/LOOLWSD.cpp b/wsd/LOOLWSD.cpp
index eff8aa0..f7c2399 100644
--- a/wsd/LOOLWSD.cpp
+++ b/wsd/LOOLWSD.cpp
@@ -1475,7 +1475,7 @@ public:
 namespace
 {
 
-inline ServerSocket* getServerSocket(int nPortNumber, bool reuseDetails)
+inline ServerSocket* getServerSocket(int portNumber, bool reuseDetails)
 {
     try
     {
@@ -1491,8 +1491,8 @@ inline ServerSocket* getServerSocket(int nPortNumber, bool reuseDetails)
         {
             try
             {
-                LOG_INF("Trying first to connect to an existing loolwsd at the same port " << nPortNumber);
-                StreamSocket s(SocketAddress(("127.0.0.1:" + std::to_string(nPortNumber)).c_str()));
+                LOG_INF("Trying first to connect to an existing loolwsd at the same port " << portNumber);
+                StreamSocket s(SocketAddress(("127.0.0.1:" + std::to_string(portNumber)).c_str()));
                 LOG_FTL("Connection succeeded, so we can't continue");
                 return nullptr;
             }
@@ -1504,7 +1504,7 @@ inline ServerSocket* getServerSocket(int nPortNumber, bool reuseDetails)
 
         ServerSocket* socket = LOOLWSD::isSSLEnabled() ? new SecureServerSocket() : new ServerSocket();
         Poco::Net::IPAddress wildcardAddr;
-        SocketAddress address(wildcardAddr, nPortNumber);
+        SocketAddress address(wildcardAddr, portNumber);
         socket->bind(address, reuseDetails);
         // 64 is the default value for the backlog parameter in Poco
         // when creating a ServerSocket, so use it here, too.
@@ -1518,26 +1518,26 @@ inline ServerSocket* getServerSocket(int nPortNumber, bool reuseDetails)
     }
 }
 
-inline ServerSocket* findFreeServerPort(int& nClientPortNumber)
+inline ServerSocket* findFreeServerPort(int& portNumber)
 {
     ServerSocket* socket = nullptr;
     while (!socket)
     {
-        socket = getServerSocket(nClientPortNumber, false);
+        socket = getServerSocket(portNumber, false);
         if (!socket)
         {
-            nClientPortNumber++;
-            LOG_INF("client port busy - trying " << nClientPortNumber);
+            portNumber++;
+            LOG_INF("client port busy - trying " << portNumber);
         }
     }
     return socket;
 }
 
-inline ServerSocket* getMasterSocket(int nMasterPortNumber)
+inline ServerSocket* getMasterSocket(int portNumber)
 {
     try
     {
-        SocketAddress addr2("127.0.0.1", nMasterPortNumber);
+        SocketAddress addr2("127.0.0.1", portNumber);
         return new ServerSocket(addr2);
     }
     catch (const Exception& exc)
@@ -1547,16 +1547,16 @@ inline ServerSocket* getMasterSocket(int nMasterPortNumber)
     }
 }
 
-inline ServerSocket* findFreeMasterPort(int &nMasterPortNumber)
+inline ServerSocket* findFreeMasterPort(int &portNumber)
 {
     ServerSocket* socket = nullptr;
     while (!socket)
     {
-        socket = getServerSocket(nMasterPortNumber, false);
+        socket = getServerSocket(portNumber, false);
         if (!socket)
         {
-            nMasterPortNumber++;
-            LOG_INF("master port busy - trying " << nMasterPortNumber);
+            portNumber++;
+            LOG_INF("master port busy - trying " << portNumber);
         }
     }
     return socket;
commit ba733ffad77e95bdac771785feb1366578110221
Author: Tor Lillqvist <tml at collabora.com>
Date:   Fri Jan 13 13:26:36 2017 +0200

    Prevent accidentally having duplicate loolwsds on same port, new approach
    
    Before setting up the socket where we listen for client requests, try
    connecting to the same port. If that succeeds, another loolwsd process
    is already running and listening on that port. That is obviously
    undesirable.
    
    Yes, there is a race condition if multiple loolwsd processes are
    started simultaneously and do this check before any of them have
    actually created the socket. Live with it. Multiple loolwsd processes
    is a problem that happens accidentally for developers only anyway. In
    a production environment systemd takes care of having just one, I
    hope.
    
    Thanks to Kendy for the idea.
    
    Change-Id: Ifdde83472f9a56e592ec5dc7649dd7706efc2f7c

diff --git a/wsd/LOOLWSD.cpp b/wsd/LOOLWSD.cpp
index 0c60114..eff8aa0 100644
--- a/wsd/LOOLWSD.cpp
+++ b/wsd/LOOLWSD.cpp
@@ -67,8 +67,8 @@
 #include <Poco/Net/HTTPServerParams.h>
 #include <Poco/Net/HTTPServerRequest.h>
 #include <Poco/Net/HTTPServerResponse.h>
-#include <Poco/Net/InvalidCertificateHandler.h>
 #include <Poco/Net/IPAddress.h>
+#include <Poco/Net/InvalidCertificateHandler.h>
 #include <Poco/Net/KeyConsoleHandler.h>
 #include <Poco/Net/MessageHeader.h>
 #include <Poco/Net/NameValueCollection.h>
@@ -140,6 +140,7 @@ using Poco::Net::PartHandler;
 using Poco::Net::SecureServerSocket;
 using Poco::Net::ServerSocket;
 using Poco::Net::SocketAddress;
+using Poco::Net::StreamSocket;
 using Poco::Net::WebSocket;
 using Poco::Path;
 using Poco::Pipe;
@@ -1478,6 +1479,29 @@ inline ServerSocket* getServerSocket(int nPortNumber, bool reuseDetails)
 {
     try
     {
+        // reuseDetails being true means we are not doing UnitWSD::isUnitTesting. In that case make
+        // sure there isn't another loolwsd already listening on the same port, as the way we create
+        // and listen on the socket doesn't prevent it otherwise. Try connecting to the port first,
+        // not expecting to suceed. (Yes, there is a race condition here if multiple loolwsd
+        // processes come here simultaneously. Live with it. Multiple loolwsd processes is a problem
+        // that happens accidentally for developers only anyway, in production systemd takes care of
+        // having just one, I hope.)
+
+        if (reuseDetails)
+        {
+            try
+            {
+                LOG_INF("Trying first to connect to an existing loolwsd at the same port " << nPortNumber);
+                StreamSocket s(SocketAddress(("127.0.0.1:" + std::to_string(nPortNumber)).c_str()));
+                LOG_FTL("Connection succeeded, so we can't continue");
+                return nullptr;
+            }
+            catch (const Exception&)
+            {
+                LOG_INF("Conection failed, so hopefully we are the only loolwsd on this port");
+            }
+        }
+
         ServerSocket* socket = LOOLWSD::isSSLEnabled() ? new SecureServerSocket() : new ServerSocket();
         Poco::Net::IPAddress wildcardAddr;
         SocketAddress address(wildcardAddr, nPortNumber);


More information about the Libreoffice-commits mailing list