[Box Backup-dev] COMMIT r839 - box/trunk/bin/bbackupd

boxbackup-dev at fluffy.co.uk boxbackup-dev at fluffy.co.uk
Wed Aug 30 20:51:59 BST 2006


Author: chris
Date: 2006-08-30 20:51:59 +0100 (Wed, 30 Aug 2006)
New Revision: 839

Modified:
   box/trunk/bin/bbackupd/BackupDaemon.cpp
Log:
* bin/bbackupd/BackupDaemon.cpp
- Made the code more readable by defining a reference rSocket to
  mpCommandSocketInfo->mListeningSocket which is used several times.
- Terminate the listening thread if it fails to bind a command socket.
- Log any unrecognised commands received over the command socket.



Modified: box/trunk/bin/bbackupd/BackupDaemon.cpp
===================================================================
--- box/trunk/bin/bbackupd/BackupDaemon.cpp	2006-08-30 19:50:08 UTC (rev 838)
+++ box/trunk/bin/bbackupd/BackupDaemon.cpp	2006-08-30 19:51:59 UTC (rev 839)
@@ -263,16 +263,31 @@
 void BackupDaemon::RunHelperThread(void)
 {
 	mpCommandSocketInfo = new CommandSocketInfo;
-	this->mReceivedCommandConn = false;
+	WinNamedPipeStream& rSocket(mpCommandSocketInfo->mListeningSocket);
 
-	// loop until the parent process exits
-	while (TRUE)
+	// loop until the parent process exits, or we decide
+	// to kill the thread ourselves
+	while (!IsTerminateWanted())
 	{
 		try
 		{
-			mpCommandSocketInfo->mListeningSocket.Accept(
-				BOX_NAMED_PIPE_NAME);
+			rSocket.Accept(BOX_NAMED_PIPE_NAME);
 		}
+		catch (BoxException &e)
+		{
+			::syslog(LOG_ERR, "Failed to open command socket: %s",
+				e.what());
+			SetTerminateWanted();
+			break; // this is fatal to listening thread
+		}
+		catch (...)
+		{
+			::syslog(LOG_ERR, "Failed to open command socket: "
+				"unknown error");
+			SetTerminateWanted();
+			break; // this is fatal to listening thread
+		}
+		}
 		catch(std::exception &e)
 		{
 			::syslog(LOG_ERR, "Failed to open command socket: "
@@ -288,6 +303,11 @@
 			break; // this is fatal to listening thread
 		}
 
+		try
+		{
+			// Errors here do not kill the thread,
+			// only the current connection.
+
 			// This next section comes from Ben's original function
 			// Log
 			::syslog(LOG_INFO, "Connection from command socket");
@@ -304,16 +324,17 @@
 				conf.GetKeyValueInt("MaxUploadWait"),
 				mState);
 
-			mpCommandSocketInfo->mListeningSocket.Write(summary, summarySize);
-			mpCommandSocketInfo->mListeningSocket.Write("ping\n", 5);
+			rSocket.Write(summary, summarySize);
+			rSocket.Write("ping\n", 5);
 
-			IOStreamGetLine readLine(mpCommandSocketInfo->mListeningSocket);
+			IOStreamGetLine readLine(rSocket);
 			std::string command;
 
-			while (mpCommandSocketInfo->mListeningSocket.IsConnected() &&
-			       readLine.GetLine(command) )
+			while (rSocket.IsConnected() && 
+				readLine.GetLine(command) &&
+				!IsTerminateWanted())
 			{
-				TRACE1("Receiving command '%s' over "
+				TRACE1("Received command '%s' over "
 					"command socket\n", command.c_str());
 
 				bool sendOK = false;
@@ -353,12 +374,18 @@
 					SetTerminateWanted();
 					sendOK = true;
 				}
+				else
+				{
+					::syslog(LOG_ERR, "Received unknown command '%s' from client", command.c_str());
+					sendResponse = true;
+					sendOK = false;
+				}
 
 				// Send a response back?
 				if (sendResponse)
 				{
 					const char* response = sendOK ? "ok\n" : "error\n";
-					mpCommandSocketInfo->mListeningSocket.Write(
+					rSocket.Write(
 						response, strlen(response));
 				}
 
@@ -370,7 +397,7 @@
 				this->mReceivedCommandConn = true;
 			}
 
-			mpCommandSocketInfo->mListeningSocket.Close();
+			rSocket.Close();
 		}
 		catch(BoxException &e)
 		{




More information about the Boxbackup-dev mailing list