diff options
Diffstat (limited to 'lib/server/ServerStream.h')
-rw-r--r-- | lib/server/ServerStream.h | 87 |
1 files changed, 60 insertions, 27 deletions
diff --git a/lib/server/ServerStream.h b/lib/server/ServerStream.h index e1136c72..0e81d05e 100644 --- a/lib/server/ServerStream.h +++ b/lib/server/ServerStream.h @@ -1,4 +1,4 @@ -// distribution boxbackup-0.10 (svn version: 494) +// distribution boxbackup-0.11rc1 (svn version: 2023_2024) // // Copyright (c) 2003 - 2006 // Ben Summers and contributors. All rights reserved. @@ -52,7 +52,6 @@ #include <errno.h> #ifndef WIN32 - #include <syslog.h> #include <sys/wait.h> #endif @@ -94,9 +93,11 @@ public: return "generic-stream-server"; } + virtual void OnIdle() { } + virtual void Run() { - // Set process title as appropraite + // Set process title as appropriate SetProcessTitle(ForkToHandleRequests?"server":"idle"); // Handle exceptions and child task quitting gracefully. @@ -109,7 +110,10 @@ public: { if(childExit) { - ::syslog(LOG_ERR, "in server child, exception %s (%d/%d) -- terminating child", e.what(), e.GetType(), e.GetSubType()); + BOX_ERROR("Error in child process, " + "terminating connection: exception " << + e.what() << "(" << e.GetType() << + "/" << e.GetSubType() << ")"); _exit(1); } else throw; @@ -118,7 +122,9 @@ public: { if(childExit) { - ::syslog(LOG_ERR, "in server child, exception %s -- terminating child", e.what()); + BOX_ERROR("Error in child process, " + "terminating connection: exception " << + e.what()); _exit(1); } else throw; @@ -127,7 +133,9 @@ public: { if(childExit) { - ::syslog(LOG_ERR, "in server child, unknown exception -- terminating child"); + BOX_ERROR("Error in child process, " + "terminating connection: " + "unknown exception"); _exit(1); } else throw; @@ -206,16 +214,22 @@ public: } else if(c[0] == "unix") { - // Check arguments size - if(c.size() != 2) - { - THROW_EXCEPTION(ServerException, ServerStreamBadListenAddrs) - } + #ifdef WIN32 + BOX_WARNING("Ignoring request to listen on a Unix socket on Windows: " << addrlist[a]); + delete psocket; + psocket = NULL; + #else + // Check arguments size + if(c.size() != 2) + { + THROW_EXCEPTION(ServerException, ServerStreamBadListenAddrs) + } - // unlink anything there - ::unlink(c[1].c_str()); - - psocket->Listen(Socket::TypeUNIX, c[1].c_str()); + // unlink anything there + ::unlink(c[1].c_str()); + + psocket->Listen(Socket::TypeUNIX, c[1].c_str()); + #endif // WIN32 } else { @@ -223,8 +237,11 @@ public: THROW_EXCEPTION(ServerException, ServerStreamBadListenAddrs) } - // Add to list of sockets - mSockets.push_back(psocket); + if (psocket != NULL) + { + // Add to list of sockets + mSockets.push_back(psocket); + } } catch(...) { @@ -232,8 +249,11 @@ public: throw; } - // Add to the list of things to wait on - connectionWait.Add(psocket); + if (psocket != NULL) + { + // Add to the list of things to wait on + connectionWait.Add(psocket); + } } } @@ -245,7 +265,8 @@ public: if(psocket) { - // Get the incomming connection (with zero wait time) + // Get the incoming connection + // (with zero wait time) std::string logMessage; std::auto_ptr<StreamType> connection(psocket->Accept(0, &logMessage)); @@ -253,7 +274,8 @@ public: if(connection.get()) { // Since this is a template parameter, the if() will be optimised out by the compiler - if(ForkToHandleRequests) + #ifndef WIN32 // no fork on Win32 + if(ForkToHandleRequests && !IsSingleProcess()) { pid_t pid = ::fork(); switch(pid) @@ -289,20 +311,26 @@ public: } // Log it - ::syslog(LOG_INFO, "%s (handling in child %d)", logMessage.c_str(), pid); + BOX_WARNING("Message from child process " << pid << ": " << logMessage); } else { - // Just handle in this connection + #endif // !WIN32 + // Just handle in this process SetProcessTitle("handling"); HandleConnection(*connection); SetProcessTitle("idle"); + #ifndef WIN32 } + #endif // !WIN32 } } - + + OnIdle(); + + #ifndef WIN32 // Clean up child processes (if forking daemon) - if(ForkToHandleRequests) + if(ForkToHandleRequests && !IsSingleProcess()) { int status = 0; int p = 0; @@ -315,6 +343,7 @@ public: } } while(p > 0); } + #endif // !WIN32 } } catch(...) @@ -335,11 +364,15 @@ public: virtual void Connection(StreamType &rStream) = 0; protected: - // For checking code in dervied classes -- use if you have an algorithm which + // For checking code in derived classes -- use if you have an algorithm which // depends on the forking model in case someone changes it later. bool WillForkToHandleRequests() { - return ForkToHandleRequests; + #ifdef WIN32 + return false; + #else + return ForkToHandleRequests && !IsSingleProcess(); + #endif // WIN32 } private: |