From 99ca81a944b12a90ebdd744965b18850bae7baf8 Mon Sep 17 00:00:00 2001 From: Henry Castro Date: Fri, 17 Jul 2015 14:02:25 -0400 Subject: [PATCH] loolwsd: move server socket to parent process --- loolwsd/LOOLSession.cpp | 2 + loolwsd/LOOLWSD.cpp | 83 ++++++++++++++++++++++++++++------------- loolwsd/LOOLWSD.hpp | 2 + 3 files changed, 62 insertions(+), 25 deletions(-) diff --git a/loolwsd/LOOLSession.cpp b/loolwsd/LOOLSession.cpp index 75f1fdcf9..502010a21 100644 --- a/loolwsd/LOOLSession.cpp +++ b/loolwsd/LOOLSession.cpp @@ -492,6 +492,8 @@ void MasterProcessSession::dispatchChild() _availableChildSessions.erase(childSession); std::cout << Util::logPrefix() << "_availableChildSessions size=" << _availableChildSessions.size() << std::endl; + if (_availableChildSessions.size() == 0) + LOOLWSD::_sharedForkChild.begin()[0] = 1; lock.unlock(); // Assume a valid URI diff --git a/loolwsd/LOOLWSD.cpp b/loolwsd/LOOLWSD.cpp index e5be6935b..595f955a4 100644 --- a/loolwsd/LOOLWSD.cpp +++ b/loolwsd/LOOLWSD.cpp @@ -507,6 +507,7 @@ std::string LOOLWSD::jail; std::mutex LOOLWSD::_rngMutex; Random LOOLWSD::_rng; static NamedMutex namedMutexLOOL("loolwsd"); +Poco::SharedMemory LOOLWSD::_sharedForkChild("loolwsd", sizeof(bool), Poco::SharedMemory::AM_WRITE); int LOOLWSD::_numPreSpawnedChildren = 10; #if ENABLE_DEBUG @@ -811,6 +812,7 @@ void LOOLWSD::componentMain() { try { + // initialisation //_childId = Process::id(); @@ -997,30 +999,8 @@ void LOOLWSD::desktopMain() Thread::sleep(std::stoul(std::getenv("SLEEPFORDEBUGGER")) * 1000); } - namedMutexLOOL.lock(); - startupComponent(_numPreSpawnedChildren); - // Start a server listening on the port for clients - ServerSocket svs(portNumber, _numPreSpawnedChildren*10); - ThreadPool threadPool(_numPreSpawnedChildren*2, _numPreSpawnedChildren*5); - HTTPServer srv(new RequestHandlerFactory(), threadPool, svs, new HTTPServerParams); - - srv.start(); - - // And one on the port for child processes - SocketAddress addr2("127.0.0.1", MASTER_PORT_NUMBER); - ServerSocket svs2(addr2, _numPreSpawnedChildren); - ThreadPool threadPool2(_numPreSpawnedChildren*2, _numPreSpawnedChildren*5); - HTTPServer srv2(new RequestHandlerFactory(), threadPool2, svs2, new HTTPServerParams); - - srv2.start(); - - namedMutexLOOL.unlock(); - - /* Pause for a second */ - sleep(1); - while (MasterProcessSession::_childProcesses.size() > 0) { int status; @@ -1055,8 +1035,9 @@ void LOOLWSD::desktopMain() else if (pid < 0) std::cout << Util::logPrefix() << "Child error: " << strerror(errno); - if (MasterProcessSession::getAvailableChildSessions() == 0 && MasterProcessSession::getPendingPreSpawnedChildren() == 0 ) + if ( _sharedForkChild.begin()[0] ) { + _sharedForkChild.begin()[0] = 0; std::cout << Util::logPrefix() << "No availabe child session, fork new one" << std::endl; if (createComponent() < 0 ) break; @@ -1110,6 +1091,8 @@ void LOOLWSD::loolMain() _childId = (((Poco::UInt64)_rng.next()) << 32) | _rng.next() | 1; rngLock.unlock(); + namedMutexLOOL.lock(); + startupDesktop(1); #ifdef __linux @@ -1118,12 +1101,62 @@ void LOOLWSD::loolMain() dropCapability(); #endif + // Start a server listening on the port for clients + ServerSocket svs(portNumber, _numPreSpawnedChildren*10); + ThreadPool threadPool(_numPreSpawnedChildren*2, _numPreSpawnedChildren*5); + HTTPServer srv(new RequestHandlerFactory(), threadPool, svs, new HTTPServerParams); + + srv.start(); + + // And one on the port for child processes + SocketAddress addr2("127.0.0.1", MASTER_PORT_NUMBER); + ServerSocket svs2(addr2, _numPreSpawnedChildren); + ThreadPool threadPool2(_numPreSpawnedChildren*2, _numPreSpawnedChildren*5); + HTTPServer srv2(new RequestHandlerFactory(), threadPool2, svs2, new HTTPServerParams); + + srv2.start(); + + namedMutexLOOL.unlock(); + Thread threadFile; FileTransferHandler svrFile; threadFile.start(svrFile); - int status; - waitpid(-1, &status, 0); + while (MasterProcessSession::_childProcesses.size() > 0) + { + int status; + pid_t pid = waitpid(-1, &status, WUNTRACED | WNOHANG); + if (pid > 0) + { + if ( MasterProcessSession::_childProcesses.find(pid) != MasterProcessSession::_childProcesses.end() ) + { + if ((WIFEXITED(status) || WIFSIGNALED(status) || WTERMSIG(status) ) ) + { + std::cout << Util::logPrefix() << "One of our known child processes died :" << std::to_string(pid) << std::endl; + MasterProcessSession::_childProcesses.erase(pid); + } + + if ( WCOREDUMP(status) ) + std::cout << Util::logPrefix() << "The child produced a core dump." << std::endl; + + if ( WIFSTOPPED(status) ) + std::cout << Util::logPrefix() << "The child process was stopped by delivery of a signal." << std::endl; + + if ( WSTOPSIG(status) ) + std::cout << Util::logPrefix() << "The child process was stopped." << std::endl; + + if ( WIFCONTINUED(status) ) + std::cout << Util::logPrefix() << "The child process was resumed." << std::endl; + } + else + { + std::cout << Util::logPrefix() << "None of our known child processes died :" << std::to_string(pid) << std::endl; + } + } + else if (pid < 0) + std::cout << Util::logPrefix() << "Child error: " << strerror(errno); + + } // Terminate child processes for (auto i : MasterProcessSession::_childProcesses) diff --git a/loolwsd/LOOLWSD.hpp b/loolwsd/LOOLWSD.hpp index bccb98f0a..51e05ad65 100644 --- a/loolwsd/LOOLWSD.hpp +++ b/loolwsd/LOOLWSD.hpp @@ -19,6 +19,7 @@ #include #include #include +#include class LOOLWSD: public Poco::Util::ServerApplication { @@ -35,6 +36,7 @@ public: static std::string childRoot; static std::string loSubPath; static std::string jail; + static Poco::SharedMemory _sharedForkChild; static const int DEFAULT_CLIENT_PORT_NUMBER = 9980; static const int MASTER_PORT_NUMBER = 9981;