[Box Backup-dev] COMMIT r420 - box/chris/win32/vc2005-compile-fixes/bin/bbackupd

boxbackup-dev at fluffy.co.uk boxbackup-dev at fluffy.co.uk
Sat Feb 11 13:12:58 GMT 2006


Author: chris
Date: 2006-02-11 13:12:57 +0000 (Sat, 11 Feb 2006)
New Revision: 420

Modified:
   box/chris/win32/vc2005-compile-fixes/bin/bbackupd/BackupDaemon.cpp
Log:
* BackupDaemon.cpp
- Start the helper thread when BackupDaemon is created, and don't delete
  it or close the socket from the main thread (race condition)
- Simplified BackupDaemon::Run()
- Don't print the "parameter" when starting the helper thread
- Allow the 100-second delay after catching an exception to be interrupted
  (after 10 seconds) by changing it to a normal inter-run sleep


Modified: box/chris/win32/vc2005-compile-fixes/bin/bbackupd/BackupDaemon.cpp
===================================================================
--- box/chris/win32/vc2005-compile-fixes/bin/bbackupd/BackupDaemon.cpp	2006-02-11 13:06:49 UTC (rev 419)
+++ box/chris/win32/vc2005-compile-fixes/bin/bbackupd/BackupDaemon.cpp	2006-02-11 13:12:57 UTC (rev 420)
@@ -83,9 +83,29 @@
 // This prevents repetative cycles of load on the server
 #define		SYNC_PERIOD_RANDOM_EXTRA_TIME_SHIFT_BY	6
 
+#ifdef WIN32
 // --------------------------------------------------------------------------
 //
 // Function
+//		Name:    HelperThread()
+//		Purpose: Background thread function, called by Windows,
+//			calls the BackupDaemon's RunHelperThread method
+//			to listen for and act on control communications
+//		Created: 18/2/04
+//
+// --------------------------------------------------------------------------
+unsigned int WINAPI HelperThread( LPVOID lpParam ) 
+{ 
+	// printf( "Parameter = %lu.\n", *(DWORD*)lpParam ); 
+	((BackupDaemon *)lpParam)->RunHelperThread();
+
+	return 0;
+}
+#endif
+
+// --------------------------------------------------------------------------
+//
+// Function
 //		Name:    BackupDaemon::BackupDaemon()
 //		Purpose: constructor
 //		Created: 2003/10/08
@@ -104,6 +124,20 @@
 	{
 		mNotificationsSent[l] = false;
 	}
+
+#ifdef WIN32
+	// Create a thread to handle the named pipe
+	HANDLE hThread;
+	unsigned int dwThreadId;
+
+	hThread = (HANDLE) _beginthreadex( 
+        	NULL,                        // default security attributes 
+        	0,                           // use default stack size  
+        	HelperThread,                // thread function 
+        	this,                        // argument to thread function 
+        	0,                           // use default creation flags 
+        	&dwThreadId);                // returns the thread identifier 
+#endif
 }
 
 // --------------------------------------------------------------------------
@@ -227,30 +261,13 @@
 }
 
 #ifdef WIN32
-// --------------------------------------------------------------------------
-//
-// Function
-//		Name:    HelperThread()
-//		Purpose: Background thread function, called by Windows,
-//			calls the BackupDaemon's RunHelperThread method
-//			to listen for and act on control communications
-//		Created: 18/2/04
-//
-// --------------------------------------------------------------------------
-unsigned int WINAPI HelperThread( LPVOID lpParam ) 
-{ 
-	printf( "Parameter = %lu.\n", *(DWORD*)lpParam ); 
-	((BackupDaemon *)lpParam)->RunHelperThread();
-
-	return 0;
-}
-
 void BackupDaemon::RunHelperThread(void)
 {
 	mpCommandSocketInfo = new CommandSocketInfo;
 	this->mReceivedCommandConn = false;
 
-	while ( !IsTerminateWanted() )
+	// loop until the parent process exits
+	while (TRUE)
 	{
 		try
 		{
@@ -290,7 +307,7 @@
 				bool disconnect = false;
 
 				// Command to process!
-				if(command == "quit" || command == "")
+				if (command == "quit" || command == "")
 				{
 					// Close the socket.
 					disconnect = true;
@@ -303,20 +320,20 @@
 					this->mSyncIsForcedOut = false;
 					sendOK = true;
 				}
-				else if(command == "force-sync")
+				else if (command == "force-sync")
 				{
 					// Sync now (forced -- overrides any SyncAllowScript)
 					this->mDoSyncFlagOut = true;
 					this->mSyncIsForcedOut = true;
 					sendOK = true;
 				}
-				else if(command == "reload")
+				else if (command == "reload")
 				{
 					// Reload the configuration
 					SetReloadConfigWanted();
 					sendOK = true;
 				}
-				else if(command == "terminate")
+				else if (command == "terminate")
 				{
 					// Terminate the daemon cleanly
 					SetTerminateWanted();
@@ -365,24 +382,21 @@
 void BackupDaemon::Run()
 {
 #ifdef WIN32
-
-	// Create a thread to handle the named pipe
-	HANDLE hThread;
-	unsigned int dwThreadId;
-
-	hThread = (HANDLE) _beginthreadex( 
-        	NULL,                        // default security attributes 
-        	0,                           // use default stack size  
-        	HelperThread,                // thread function 
-        	this,                        // argument to thread function 
-        	0,                           // use default creation flags 
-        	&dwThreadId);                // returns the thread identifier 
-
 	// init our own timer for file diff timeouts
 	InitTimer();
 
+	try
+	{
+		Run2();
+	}
+	catch (...)
+	{
+		FiniTimer();
+		throw;
+	}
+
+	FiniTimer();
 #else // ! WIN32
-
 	// Ignore SIGPIPE (so that if a command connection is broken, the daemon doesn't terminate)
 	::signal(SIGPIPE, SIG_IGN);
 
@@ -396,8 +410,6 @@
 		::unlink(socketName);
 		mpCommandSocketInfo->mListeningSocket.Listen(Socket::TypeUNIX, socketName);
 	}
-	
-#endif // WIN32
 
 	// Handle things nicely on exceptions
 	try
@@ -423,17 +435,13 @@
 
 		throw;
 	}
-	
+
 	// Clean up
 	if(mpCommandSocketInfo != 0)
 	{
 		delete mpCommandSocketInfo;
 		mpCommandSocketInfo = 0;
 	}
-
-#ifdef WIN32
-	// clean up windows specific stuff.
-	FiniTimer();
 #endif
 }
 
@@ -766,7 +774,12 @@
 						"to retry...", 
 						errorString, errorCode, 
 						errorSubCode);
-					::sleep(100);
+					::sleep(10);
+					nextSyncTime = currentSyncStartTime + 
+						SecondsToBoxTime(90) +
+						Random::RandomInt(
+							updateStoreInterval >> 
+							SYNC_PERIOD_RANDOM_EXTRA_TIME_SHIFT_BY);
 				}
 			}
 
@@ -1080,25 +1093,23 @@
 // --------------------------------------------------------------------------
 void BackupDaemon::CloseCommandConnection()
 {
+#ifndef WIN32
 	try
 	{
 		TRACE0("Closing command connection\n");
 		
-#ifdef WIN32
-		mpCommandSocketInfo->mListeningSocket.Close();
-#else
 		if(mpCommandSocketInfo->mpGetLine)
 		{
 			delete mpCommandSocketInfo->mpGetLine;
 			mpCommandSocketInfo->mpGetLine = 0;
 		}
 		mpCommandSocketInfo->mpConnectedSocket.reset();
-#endif
 	}
 	catch(...)
 	{
 		// Ignore any errors
 	}
+#endif
 }
 
 
@@ -1115,7 +1126,11 @@
 {
 	// The bbackupctl program can't rely on a state change, because it may never
 	// change if the server doesn't need to be contacted.
-	
+
+#ifdef __MINGW32__
+#warning race condition: what happens if socket is closed?
+#endif
+
 	if (mpCommandSocketInfo != NULL &&
 #ifdef WIN32
 	    mpCommandSocketInfo->mListeningSocket.IsConnected()
@@ -2119,17 +2134,6 @@
 // --------------------------------------------------------------------------
 BackupDaemon::CommandSocketInfo::~CommandSocketInfo()
 {
-#ifdef WIN32
-	try
-	{
-		mListeningSocket.Close();
-	}
-	catch(ServerException &e)
-	{
-		// ignore errors as we're closing down anyway
-	}
-#endif
-
 	if(mpGetLine)
 	{
 		delete mpGetLine;




More information about the Boxbackup-dev mailing list