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

boxbackup-dev@fluffy.co.uk boxbackup-dev@fluffy.co.uk
Sat, 22 Sep 2007 00:05:26 +0100


Author: chris
Date: 2007-09-22 00:05:25 +0100 (Sat, 22 Sep 2007)
New Revision: 1854

Modified:
   box/chris/general/bin/bbackupd/BackupDaemon.cpp
   box/chris/general/bin/bbackupd/BackupDaemon.h
   box/chris/general/bin/bbackupd/bbackupd.cpp
Log:
Use Daemon's delegated option processing instead of our own hacks.

Move Windows service startup, installation and removal to BackupDaemon.


Modified: box/chris/general/bin/bbackupd/BackupDaemon.cpp
===================================================================
--- box/chris/general/bin/bbackupd/BackupDaemon.cpp	2007-09-21 23:03:10 UTC (rev 1853)
+++ box/chris/general/bin/bbackupd/BackupDaemon.cpp	2007-09-21 23:05:25 UTC (rev 1854)
@@ -76,6 +76,13 @@
 #include "Logging.h"
 #include "autogen_ClientException.h"
 
+#ifdef WIN32
+	#include "Win32ServiceFunctions.h"
+	#include "Win32BackupService.h"
+
+	extern Win32BackupService* gpDaemonService;
+#endif
+
 #include "MemLeakFindOn.h"
 
 static const time_t MAX_SLEEP_TIME = 1024;
@@ -116,6 +123,11 @@
 	  mpCommandSocketInfo(0),
 	  mDeleteUnusedRootDirEntriesAfter(0),
 	  mLogAllFileAccess(false)
+	#ifdef WIN32
+	, mInstallService(false),
+	  mRemoveService(false),
+	  mRunAsService(false)
+	#endif
 {
 	// Only ever one instance of a daemon
 	SSLLib::Initialise();
@@ -276,6 +288,83 @@
 }
 
 #ifdef WIN32
+std::string BackupDaemon::GetOptionString()
+{
+	std::string oldOpts = this->Daemon::GetOptionString();
+	ASSERT(oldOpts.find("s") == std::string::npos);
+	ASSERT(oldOpts.find("S") == std::string::npos);
+	ASSERT(oldOpts.find("i") == std::string::npos);
+	ASSERT(oldOpts.find("r") == std::string::npos);
+	return oldOpts + "sS:ir";
+}
+
+int BackupDaemon::ProcessOption(signed int option)
+{
+	switch(option)
+	{
+		case 's':
+		{
+			mRunAsService = true;
+			return 0;
+		}
+
+		case 'S':
+		{
+			mServiceName = optarg;
+			return 0;
+		}
+
+		case 'i':
+		{
+			mInstallService = true;
+			return 0;
+		}
+
+		case 'r':
+		{
+			mRemoveService = true;
+			return 0;
+		}
+
+		default:
+		{
+			return this->Daemon::ProcessOption(option);
+		}
+	}
+}
+
+int BackupDaemon::Main(const std::string &rConfigFileName)
+{
+	if (mInstallService)
+	{
+		return InstallService(rConfigFileName.c_str());
+	}
+
+	if (mRemoveService)
+	{
+		return RemoveService();
+	}
+
+	int returnCode;
+
+	if (mRunAsService)
+	{
+		// We will be called reentrantly by the Service Control
+		// Manager, and we had better not call OurService again!
+		mRunAsService = false;
+
+		BOX_INFO("Box Backup service starting");
+		returnCode = OurService(rConfigFileName.c_str());
+		BOX_INFO("Box Backup service shut down");
+	}
+	else
+	{
+		returnCode = this->Daemon::Main(rConfigFileName);
+	}
+	
+	return returnCode;
+}
+
 void BackupDaemon::RunHelperThread(void)
 {
 	const Configuration &conf(GetConfiguration());

Modified: box/chris/general/bin/bbackupd/BackupDaemon.h
===================================================================
--- box/chris/general/bin/bbackupd/BackupDaemon.h	2007-09-21 23:03:10 UTC (rev 1853)
+++ box/chris/general/bin/bbackupd/BackupDaemon.h	2007-09-21 23:05:25 UTC (rev 1854)
@@ -58,7 +58,14 @@
 		box_time_t & theLastSyncTime, box_time_t & theNextSyncTime);
 	bool DeleteStoreObjectInfo() const;
 	BackupDaemon(const BackupDaemon &);
+
 public:
+	#ifdef WIN32
+		// add command-line options to handle Windows services
+		std::string GetOptionString();
+		int ProcessOption(signed int option);
+		int Main(const std::string &rConfigFileName);
+	#endif
 
 	void Run();
 	virtual const char *DaemonName() const;
@@ -416,6 +423,8 @@
 
 	private:
 	bool mDoSyncFlagOut, mSyncIsForcedOut;
+	bool mInstallService, mRemoveService, mRunAsService;
+	std::string mServiceName;
 	HANDLE mhMessageToSendEvent, mhCommandReceivedEvent;
 	CRITICAL_SECTION mMessageQueueLock;
 	std::vector<std::string> mMessageList;

Modified: box/chris/general/bin/bbackupd/bbackupd.cpp
===================================================================
--- box/chris/general/bin/bbackupd/bbackupd.cpp	2007-09-21 23:03:10 UTC (rev 1853)
+++ box/chris/general/bin/bbackupd/bbackupd.cpp	2007-09-21 23:05:25 UTC (rev 1854)
@@ -35,63 +35,12 @@
 	
 #ifdef WIN32
 
-	if(argc == 2 &&
-		(::strcmp(argv[1], "--help") == 0 ||
-		 ::strcmp(argv[1], "-h") == 0))
-	{
-		printf("-h help, -i install service, -r remove service,\n"
-			"-c <config file> start daemon now");
-		return 2;
-	}
-	if(argc == 2 && ::strcmp(argv[1], "-r") == 0)
-	{
-		return RemoveService();
-	}
-	if((argc == 2 || argc == 3) && ::strcmp(argv[1], "-i") == 0)
-	{
-		const char* config = NULL;
-		if (argc == 3)
-		{
-			config = argv[2];
-		}
-		return InstallService(config);
-	}
-
-	bool runAsWin32Service = false;
-	if (argc >= 2 && ::strcmp(argv[1], "--service") == 0)
-	{
-		runAsWin32Service = true;
-	}
-
-	gpDaemonService = new Win32BackupService();
-
 	EnableBackupRights();
 
-	if (runAsWin32Service)
-	{
-		BOX_INFO("Box Backup service starting");
-
-		char* config = NULL;
-		if (argc >= 3)
-		{
-			config = strdup(argv[2]);
-		}
-
-		ExitCode = OurService(config);
-
-		if (config)
-		{
-			free(config);
-		}
-
-		BOX_INFO("Box Backup service shut down");
-	}
-	else
-	{
-		ExitCode = gpDaemonService->Main(
-			BOX_GET_DEFAULT_BBACKUPD_CONFIG_FILE, argc, argv);
-	}
-
+	gpDaemonService = new Win32BackupService();
+	ExitCode = gpDaemonService->Daemon::Main(
+		BOX_GET_DEFAULT_BBACKUPD_CONFIG_FILE,
+		argc, argv);
 	delete gpDaemonService;
 
 #else // !WIN32