[Box Backup-commit] COMMIT r1831 - box/chris/general/bin/bbackupd

boxbackup-dev at fluffy.co.uk boxbackup-dev at fluffy.co.uk
Fri Sep 14 22:23:23 BST 2007


Author: chris
Date: 2007-09-14 22:23:23 +0100 (Fri, 14 Sep 2007)
New Revision: 1831

Modified:
   box/chris/general/bin/bbackupd/BackupDaemon.cpp
Log:
Don't initialise the command socket thread (on Win32) until the
configuration is known.

Create the command socket (on Win32) with the specified pipe name
in the configuration file.

Commonise some code between Windows and Unix daemon startup.


Modified: box/chris/general/bin/bbackupd/BackupDaemon.cpp
===================================================================
--- box/chris/general/bin/bbackupd/BackupDaemon.cpp	2007-09-14 18:43:06 UTC (rev 1830)
+++ box/chris/general/bin/bbackupd/BackupDaemon.cpp	2007-09-14 21:23:23 UTC (rev 1831)
@@ -126,42 +126,33 @@
 		mNotificationsSent[l] = false;
 	}
 
-#ifdef WIN32
-	// Create the event object to signal from main thread to worker
-	// when new messages are queued to be sent to the command socket.
-	mhMessageToSendEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
-	if(mhMessageToSendEvent == INVALID_HANDLE_VALUE)
-	{
-		BOX_ERROR("Failed to create event object: error " <<
-			GetLastError());
-		exit(1);
-	}
+	#ifdef WIN32
+		// Create the event object to signal from main thread to
+		// worker when new messages are queued to be sent to the
+		// command socket.
 
-	// Create the event object to signal from worker to main thread
-	// when a command has been received on the command socket.
-	mhCommandReceivedEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
-	if(mhCommandReceivedEvent == INVALID_HANDLE_VALUE)
-	{
-		BOX_ERROR("Failed to create event object: error " <<
-			GetLastError());
-		exit(1);
-	}
+		mhMessageToSendEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
+		if(mhMessageToSendEvent == INVALID_HANDLE_VALUE)
+		{
+			BOX_ERROR("Failed to create event object: error " <<
+				GetLastError());
+			exit(1);
+		}
 
-	// Create the critical section to protect the message queue
-	InitializeCriticalSection(&mMessageQueueLock);
+		// Create the event object to signal from worker to main thread
+		// when a command has been received on the command socket.
 
-	// Create a thread to handle the named pipe
-	HANDLE hThread;
-	unsigned int dwThreadId;
+		mhCommandReceivedEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
+		if(mhCommandReceivedEvent == INVALID_HANDLE_VALUE)
+		{
+			BOX_ERROR("Failed to create event object: error " <<
+				GetLastError());
+			exit(1);
+		}
 
-	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
+		// Create the critical section to protect the message queue
+		InitializeCriticalSection(&mMessageQueueLock);
+	#endif
 }
 
 // --------------------------------------------------------------------------
@@ -287,6 +278,7 @@
 #ifdef WIN32
 void BackupDaemon::RunHelperThread(void)
 {
+	const Configuration &conf(GetConfiguration());
 	mpCommandSocketInfo = new CommandSocketInfo;
 	WinNamedPipeStream& rSocket(mpCommandSocketInfo->mListeningSocket);
 
@@ -296,7 +288,8 @@
 	{
 		try
 		{
-			rSocket.Accept(BOX_NAMED_PIPE_NAME);
+			std::string socket = conf.GetKeyValue("CommandSocket");
+			rSocket.Accept(socket);
 		}
 		catch (BoxException &e)
 		{
@@ -331,7 +324,6 @@
 
 			// Send a header line summarising the configuration 
 			// and current state
-			const Configuration &conf(GetConfiguration());
 			char summary[256];
 			size_t summarySize = sprintf(summary, 
 				"bbackupd: %d %d %d %d\nstate %d\n",
@@ -512,31 +504,37 @@
 	// initialise global timer mechanism
 	Timers::Init();
 	
-#ifdef WIN32
-	try
-	{
-		Run2();
-	}
-	catch(...)
-	{
-		Timers::Cleanup();
-		throw;
-	}
-#else // ! WIN32
-	// Ignore SIGPIPE (so that if a command connection is broken, the daemon doesn't terminate)
-	::signal(SIGPIPE, SIG_IGN);
+	#ifdef WIN32
+		// Create a thread to handle the named pipe
+		HANDLE hThread;
+		unsigned int dwThreadId;
 
-	// Create a command socket?
-	const Configuration &conf(GetConfiguration());
-	if(conf.KeyExists("CommandSocket"))
-	{
-		// Yes, create a local UNIX socket
-		mpCommandSocketInfo = new CommandSocketInfo;
-		const char *socketName = conf.GetKeyValue("CommandSocket").c_str();
-		::unlink(socketName);
-		mpCommandSocketInfo->mListeningSocket.Listen(Socket::TypeUNIX, socketName);
-	}
+		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 
+	#else
+		// Ignore SIGPIPE so that if a command connection is broken,
+		// the daemon doesn't terminate.
+		::signal(SIGPIPE, SIG_IGN);
 
+		// Create a command socket?
+		const Configuration &conf(GetConfiguration());
+		if(conf.KeyExists("CommandSocket"))
+		{
+			// Yes, create a local UNIX socket
+			mpCommandSocketInfo = new CommandSocketInfo;
+			const char *socketName =
+				conf.GetKeyValue("CommandSocket").c_str();
+			::unlink(socketName);
+			mpCommandSocketInfo->mListeningSocket.Listen(
+				Socket::TypeUNIX, socketName);
+		}
+	#endif // !WIN32
+
 	// Handle things nicely on exceptions
 	try
 	{
@@ -544,6 +542,11 @@
 	}
 	catch(...)
 	{
+		#ifdef WIN32
+			// Don't delete the socket, as the helper thread
+			// is probably still using it. Let Windows clean
+			// up after us.
+		#else
 		if(mpCommandSocketInfo != 0)
 		{
 			try 
@@ -563,19 +566,21 @@
 			}
 			mpCommandSocketInfo = 0;
 		}
+		#endif // WIN32
 
 		Timers::Cleanup();
 		
 		throw;
 	}
 
-	// Clean up
-	if(mpCommandSocketInfo != 0)
-	{
-		delete mpCommandSocketInfo;
-		mpCommandSocketInfo = 0;
-	}
-#endif
+	#ifndef WIN32
+		// Clean up
+		if(mpCommandSocketInfo != 0)
+		{
+			delete mpCommandSocketInfo;
+			mpCommandSocketInfo = 0;
+		}
+	#endif
 	
 	Timers::Cleanup();
 }




More information about the Boxbackup-commit mailing list