diff options
Diffstat (limited to 'lib/server')
-rw-r--r-- | lib/server/Daemon.cpp | 16 | ||||
-rw-r--r-- | lib/server/Daemon.h | 5 | ||||
-rw-r--r-- | lib/server/ProtocolUncertainStream.cpp | 3 | ||||
-rw-r--r-- | lib/server/ServerControl.h | 3 | ||||
-rw-r--r-- | lib/server/ServerStream.h | 6 | ||||
-rw-r--r-- | lib/server/ServerTLS.h | 9 | ||||
-rw-r--r-- | lib/server/SocketListen.h | 83 | ||||
-rw-r--r-- | lib/server/SocketStream.cpp | 56 |
8 files changed, 66 insertions, 115 deletions
diff --git a/lib/server/Daemon.cpp b/lib/server/Daemon.cpp index 292a628c..95f5c338 100644 --- a/lib/server/Daemon.cpp +++ b/lib/server/Daemon.cpp @@ -465,7 +465,8 @@ int Daemon::Main(const std::string &rConfigFileName) // Set new session if(::setsid() == -1) { - BOX_LOG_SYS_ERROR("Failed to setsid()"); + BOX_ERROR("Failed to setsid(): " << + strerror(errno)); THROW_EXCEPTION(ServerException, DaemoniseFailed) } @@ -474,7 +475,6 @@ int Daemon::Main(const std::string &rConfigFileName) { case -1: // error - BOX_LOG_SYS_ERROR("Failed to fork() a child"); THROW_EXCEPTION(ServerException, DaemoniseFailed) break; @@ -497,11 +497,9 @@ int Daemon::Main(const std::string &rConfigFileName) struct sigaction sa; sa.sa_handler = SignalHandler; sa.sa_flags = 0; - sigemptyset(&sa.sa_mask); // macro - if(::sigaction(SIGHUP, &sa, NULL) != 0 || - ::sigaction(SIGTERM, &sa, NULL) != 0) + sigemptyset(&sa.sa_mask); // macro + if(::sigaction(SIGHUP, &sa, NULL) != 0 || ::sigaction(SIGTERM, &sa, NULL) != 0) { - BOX_LOG_SYS_ERROR("Failed to set signal handlers"); THROW_EXCEPTION(ServerException, DaemoniseFailed) } #endif // !WIN32 @@ -517,8 +515,7 @@ int Daemon::Main(const std::string &rConfigFileName) if(::write(pidFile, pid, pidsize) != pidsize) { - BOX_LOG_SYS_FATAL("Failed to write PID file: " << - pidFileName); + BOX_FATAL("can't write pid file"); THROW_EXCEPTION(ServerException, DaemoniseFailed) } @@ -547,7 +544,6 @@ int Daemon::Main(const std::string &rConfigFileName) int devnull = ::open(PLATFORM_DEV_NULL, O_RDWR, 0); if(devnull == -1) { - BOX_LOG_SYS_ERROR("Failed to open /dev/null"); THROW_EXCEPTION(CommonException, OSFileError); } // Then duplicate them to all three handles @@ -894,8 +890,6 @@ box_time_t Daemon::GetConfigFileModifiedTime() const { return 0; } - BOX_LOG_SYS_ERROR("Failed to stat configuration file: " << - GetConfigFileName()); THROW_EXCEPTION(CommonException, OSFileError) } diff --git a/lib/server/Daemon.h b/lib/server/Daemon.h index ef5728ec..60444ab9 100644 --- a/lib/server/Daemon.h +++ b/lib/server/Daemon.h @@ -92,9 +92,8 @@ private: std::string mAppName; }; -#define DAEMON_VERIFY_SERVER_KEYS \ - ConfigurationVerifyKey("PidFile", ConfigTest_Exists), \ - ConfigurationVerifyKey("User", ConfigTest_LastEntry) +#define DAEMON_VERIFY_SERVER_KEYS {"PidFile", 0, ConfigTest_Exists, 0}, \ + {"User", 0, ConfigTest_LastEntry, 0} #endif // DAEMON__H diff --git a/lib/server/ProtocolUncertainStream.cpp b/lib/server/ProtocolUncertainStream.cpp index 9c15455d..60c1fa1d 100644 --- a/lib/server/ProtocolUncertainStream.cpp +++ b/lib/server/ProtocolUncertainStream.cpp @@ -41,8 +41,7 @@ ProtocolUncertainStream::~ProtocolUncertainStream() { if(!mFinished) { - BOX_WARNING("ProtocolUncertainStream destroyed before " - "stream finished"); + TRACE0("ProtocolUncertainStream::~ProtocolUncertainStream() destroyed when stream not complete\n"); } } diff --git a/lib/server/ServerControl.h b/lib/server/ServerControl.h index 771f88fb..ce5620c2 100644 --- a/lib/server/ServerControl.h +++ b/lib/server/ServerControl.h @@ -142,7 +142,8 @@ inline bool KillServerInternal(int pid) bool killed = (::kill(pid, SIGTERM) == 0); if (!killed) { - BOX_LOG_SYS_ERROR("Failed to kill process " << pid); + BOX_ERROR("Failed to kill process " << pid << ": " << + strerror(errno)); } TEST_THAT(killed); return killed; diff --git a/lib/server/ServerStream.h b/lib/server/ServerStream.h index 41899e78..32a57bac 100644 --- a/lib/server/ServerStream.h +++ b/lib/server/ServerStream.h @@ -273,7 +273,7 @@ public: } // Log it - BOX_NOTICE("Message from child process " << pid << ": " << logMessage); + BOX_WARNING("Message from child process " << pid << ": " << logMessage); } else { @@ -365,8 +365,8 @@ private: }; #define SERVERSTREAM_VERIFY_SERVER_KEYS(DEFAULT_ADDRESSES) \ - ConfigurationVerifyKey("ListenAddresses", 0, DEFAULT_ADDRESSES), \ - DAEMON_VERIFY_SERVER_KEYS + {"ListenAddresses", DEFAULT_ADDRESSES, 0, 0}, \ + DAEMON_VERIFY_SERVER_KEYS #include "MemLeakFindOff.h" diff --git a/lib/server/ServerTLS.h b/lib/server/ServerTLS.h index 1bfcb547..71d35380 100644 --- a/lib/server/ServerTLS.h +++ b/lib/server/ServerTLS.h @@ -70,10 +70,11 @@ private: }; #define SERVERTLS_VERIFY_SERVER_KEYS(DEFAULT_ADDRESSES) \ - ConfigurationVerifyKey("CertificateFile", ConfigTest_Exists), \ - ConfigurationVerifyKey("PrivateKeyFile", ConfigTest_Exists), \ - ConfigurationVerifyKey("TrustedCAsFile", ConfigTest_Exists), \ - SERVERSTREAM_VERIFY_SERVER_KEYS(DEFAULT_ADDRESSES) + {"CertificateFile", 0, ConfigTest_Exists, 0}, \ + {"PrivateKeyFile", 0, ConfigTest_Exists, 0}, \ + {"TrustedCAsFile", 0, ConfigTest_Exists, 0}, \ + SERVERSTREAM_VERIFY_SERVER_KEYS(DEFAULT_ADDRESSES) + #endif // SERVERTLS__H diff --git a/lib/server/SocketListen.h b/lib/server/SocketListen.h index 1cfce648..ff08fb8f 100644 --- a/lib/server/SocketListen.h +++ b/lib/server/SocketListen.h @@ -108,57 +108,45 @@ public: if(::close(mSocketHandle) == -1) #endif { - BOX_LOG_SYS_ERROR("Failed to close network " - "socket"); - THROW_EXCEPTION(ServerException, - SocketCloseError) + THROW_EXCEPTION(ServerException, SocketCloseError) } } mSocketHandle = -1; } - // ------------------------------------------------------------------ + // -------------------------------------------------------------------------- // // Function // Name: SocketListen::Listen(int, char*, int) // Purpose: Initialises, starts the socket listening. // Created: 2003/07/31 // - // ------------------------------------------------------------------ + // -------------------------------------------------------------------------- void Listen(int Type, const char *Name, int Port = 0) { - if(mSocketHandle != -1) - { - THROW_EXCEPTION(ServerException, SocketAlreadyOpen); - } + if(mSocketHandle != -1) {THROW_EXCEPTION(ServerException, SocketAlreadyOpen)} // Setup parameters based on type, looking up names if required int sockDomain = 0; SocketAllAddr addr; int addrLen = 0; - Socket::NameLookupToSockAddr(addr, sockDomain, Type, Name, - Port, addrLen); + Socket::NameLookupToSockAddr(addr, sockDomain, Type, Name, Port, addrLen); // Create the socket - mSocketHandle = ::socket(sockDomain, SOCK_STREAM, - 0 /* let OS choose protocol */); + mSocketHandle = ::socket(sockDomain, SOCK_STREAM, 0 /* let OS choose protocol */); if(mSocketHandle == -1) { - BOX_LOG_SYS_ERROR("Failed to create a network socket"); THROW_EXCEPTION(ServerException, SocketOpenError) } // Set an option to allow reuse (useful for -HUP situations!) #ifdef WIN32 - if(::setsockopt(mSocketHandle, SOL_SOCKET, SO_REUSEADDR, "", - 0) == -1) + if(::setsockopt(mSocketHandle, SOL_SOCKET, SO_REUSEADDR, "", 0) == -1) #else int option = true; - if(::setsockopt(mSocketHandle, SOL_SOCKET, SO_REUSEADDR, - &option, sizeof(option)) == -1) + if(::setsockopt(mSocketHandle, SOL_SOCKET, SO_REUSEADDR, &option, sizeof(option)) == -1) #endif { - BOX_LOG_SYS_ERROR("Failed to set socket options"); THROW_EXCEPTION(ServerException, SocketOpenError) } @@ -173,25 +161,19 @@ public: } } - // ------------------------------------------------------------------ + // -------------------------------------------------------------------------- // // Function // Name: SocketListen::Accept(int) - // Purpose: Accepts a connection, returning a pointer to - // a class of the specified type. May return a - // null pointer if a signal happens, or there's - // a timeout. Timeout specified in - // milliseconds, defaults to infinite time. + // Purpose: Accepts a connection, returning a pointer to a class of + // the specified type. May return a null pointer if a signal happens, + // or there's a timeout. Timeout specified in milliseconds, defaults to infinite time. // Created: 2003/07/31 // - // ------------------------------------------------------------------ - std::auto_ptr<SocketType> Accept(int Timeout = INFTIM, - std::string *pLogMsg = 0) + // -------------------------------------------------------------------------- + std::auto_ptr<SocketType> Accept(int Timeout = INFTIM, std::string *pLogMsg = 0) { - if(mSocketHandle == -1) - { - THROW_EXCEPTION(ServerException, BadSocketHandle); - } + if(mSocketHandle == -1) {THROW_EXCEPTION(ServerException, BadSocketHandle)} // Do the accept, using the supplied locking type int sock; @@ -203,10 +185,8 @@ public: if(!socklock.HaveLock()) { - // Didn't get the lock for some reason. - // Wait a while, then return nothing. - BOX_ERROR("Failed to get a lock on incoming " - "connection"); + // Didn't get the lock for some reason. Wait a while, then + // return nothing. ::sleep(1); return std::auto_ptr<SocketType>(); } @@ -222,18 +202,12 @@ public: // signal? if(errno == EINTR) { - BOX_ERROR("Failed to accept " - "connection: interrupted by " - "signal"); // return nothing return std::auto_ptr<SocketType>(); } else { - BOX_LOG_SYS_ERROR("Failed to poll " - "connection"); - THROW_EXCEPTION(ServerException, - SocketPollError) + THROW_EXCEPTION(ServerException, SocketPollError) } break; case 0: // timed out @@ -246,19 +220,16 @@ public: sock = ::accept(mSocketHandle, &addr, &addrlen); } - - // Got socket (or error), unlock (implicit in destruction) + // Got socket (or error), unlock (implcit in destruction) if(sock == -1) { - BOX_LOG_SYS_ERROR("Failed to accept connection"); THROW_EXCEPTION(ServerException, SocketAcceptError) } // Log it if(pLogMsg) { - *pLogMsg = Socket::IncomingConnectionLogMessage(&addr, - addrlen); + *pLogMsg = Socket::IncomingConnectionLogMessage(&addr, addrlen); } else { @@ -272,29 +243,27 @@ public: // Functions to allow adding to WaitForEvent class, for efficient waiting // on multiple sockets. #ifdef HAVE_KQUEUE - // ------------------------------------------------------------------ + // -------------------------------------------------------------------------- // // Function // Name: SocketListen::FillInKEevent // Purpose: Fills in a kevent structure for this socket // Created: 9/3/04 // - // ------------------------------------------------------------------ + // -------------------------------------------------------------------------- void FillInKEvent(struct kevent &rEvent, int Flags = 0) const { - EV_SET(&rEvent, mSocketHandle, EVFILT_READ, 0, 0, 0, - (void*)this); + EV_SET(&rEvent, mSocketHandle, EVFILT_READ, 0, 0, 0, (void*)this); } #else - // ------------------------------------------------------------------ + // -------------------------------------------------------------------------- // // Function // Name: SocketListen::FillInPoll - // Purpose: Fills in the data necessary for a poll - // operation + // Purpose: Fills in the data necessary for a poll operation // Created: 9/3/04 // - // ------------------------------------------------------------------ + // -------------------------------------------------------------------------- void FillInPoll(int &fd, short &events, int Flags = 0) const { fd = mSocketHandle; diff --git a/lib/server/SocketStream.cpp b/lib/server/SocketStream.cpp index 7faff8c3..5cb252bd 100644 --- a/lib/server/SocketStream.cpp +++ b/lib/server/SocketStream.cpp @@ -150,11 +150,9 @@ void SocketStream::Open(int Type, const char *Name, int Port) Socket::NameLookupToSockAddr(addr, sockDomain, Type, Name, Port, addrLen); // Create the socket - mSocketHandle = ::socket(sockDomain, SOCK_STREAM, - 0 /* let OS choose protocol */); + mSocketHandle = ::socket(sockDomain, SOCK_STREAM, 0 /* let OS choose protocol */); if(mSocketHandle == INVALID_SOCKET_VALUE) { - BOX_LOG_SYS_ERROR("Failed to create a network socket"); THROW_EXCEPTION(ServerException, SocketOpenError) } @@ -165,15 +163,22 @@ void SocketStream::Open(int Type, const char *Name, int Port) #ifdef WIN32 DWORD err = WSAGetLastError(); ::closesocket(mSocketHandle); - BOX_LOG_WIN_ERROR_NUMBER("Failed to connect to socket " - "(type " << Type << ", name " << Name << - ", port " << Port << ")", err); -#else // !WIN32 - BOX_LOG_SYS_ERROR("Failed to connect to socket (type " << - Type << ", name " << Name << ", port " << Port << - ")"); +#else + int err = errno; ::close(mSocketHandle); -#endif // WIN32 +#endif + +#ifdef WIN32 + BOX_ERROR("Failed to connect to socket (type " << Type << + ", name " << Name << ", port " << Port << "): " << + GetErrorMessage(err) + ); +#else + BOX_ERROR("Failed to connect to socket (type " << Type << + ", name " << Name << ", port " << Port << "): " << + strerror(err) << " (" << err << ")" + ); +#endif mSocketHandle = INVALID_SOCKET_VALUE; THROW_EXCEPTION(ConnectionException, Conn_SocketConnectError) @@ -215,9 +220,7 @@ int SocketStream::Read(void *pBuffer, int NBytes, int Timeout) else { // Bad! - BOX_LOG_SYS_ERROR("Failed to poll socket"); - THROW_EXCEPTION(ServerException, - SocketPollError) + THROW_EXCEPTION(ServerException, SocketPollError) } break; @@ -247,12 +250,9 @@ int SocketStream::Read(void *pBuffer, int NBytes, int Timeout) else { // Other error - BOX_LOG_SYS_ERROR("Failed to read from socket"); - THROW_EXCEPTION(ConnectionException, - Conn_SocketReadError); + THROW_EXCEPTION(ConnectionException, Conn_SocketReadError) } } - // Closed for reading? if(r == 0) { @@ -297,9 +297,7 @@ void SocketStream::Write(const void *pBuffer, int NBytes) { // Error. mWriteClosed = true; // assume can't write again - BOX_LOG_SYS_ERROR("Failed to write to socket"); - THROW_EXCEPTION(ConnectionException, - Conn_SocketWriteError); + THROW_EXCEPTION(ConnectionException, Conn_SocketWriteError) } // Knock off bytes sent @@ -312,9 +310,7 @@ void SocketStream::Write(const void *pBuffer, int NBytes) // Need to wait until it can send again? if(bytesLeft > 0) { - BOX_TRACE("Waiting to send data on socket " << - mSocketHandle << " (" << bytesLeft << - " of " << NBytes << " bytes left)"); + TRACE3("Waiting to send data on socket %d, (%d to send of %d)\n", mSocketHandle, bytesLeft, NBytes); // Wait for data to send. struct pollfd p; @@ -327,10 +323,7 @@ void SocketStream::Write(const void *pBuffer, int NBytes) // Don't exception if it's just a signal if(errno != EINTR) { - BOX_LOG_SYS_ERROR("Failed to poll " - "socket"); - THROW_EXCEPTION(ServerException, - SocketPollError) + THROW_EXCEPTION(ServerException, SocketPollError) } } } @@ -357,7 +350,6 @@ void SocketStream::Close() if(::close(mSocketHandle) == -1) #endif { - BOX_LOG_SYS_ERROR("Failed to close socket"); THROW_EXCEPTION(ServerException, SocketCloseError) } mSocketHandle = INVALID_SOCKET_VALUE; @@ -388,7 +380,6 @@ void SocketStream::Shutdown(bool Read, bool Write) // Shut it down! if(::shutdown(mSocketHandle, how) == -1) { - BOX_LOG_SYS_ERROR("Failed to shutdown socket"); THROW_EXCEPTION(ConnectionException, Conn_SocketShutdownError) } } @@ -467,15 +458,12 @@ bool SocketStream::GetPeerCredentials(uid_t &rUidOut, gid_t &rGidOut) struct ucred cred; socklen_t credLen = sizeof(cred); - if(::getsockopt(mSocketHandle, SOL_SOCKET, SO_PEERCRED, &cred, - &credLen) == 0) + if(::getsockopt(mSocketHandle, SOL_SOCKET, SO_PEERCRED, &cred, &credLen) == 0) { rUidOut = cred.uid; rGidOut = cred.gid; return true; } - - BOX_LOG_SYS_ERROR("Failed to get peer credentials on socket"); #endif // Not available |