From 891cf7aa3028cd32ee8c4f13cc24ec0cef774620 Mon Sep 17 00:00:00 2001 From: Chris Wilson Date: Wed, 17 Oct 2007 12:53:52 +0000 Subject: Use Daemon's delegated option processing instead of our own hacks. Move Windows service startup, installation and removal to BackupDaemon. (merges [1854]) --- bin/bbackupd/BackupDaemon.cpp | 89 +++++++++++++++++++++++++++++++++++++++++++ bin/bbackupd/BackupDaemon.h | 9 +++++ bin/bbackupd/bbackupd.cpp | 59 ++-------------------------- 3 files changed, 102 insertions(+), 55 deletions(-) (limited to 'bin') diff --git a/bin/bbackupd/BackupDaemon.cpp b/bin/bbackupd/BackupDaemon.cpp index 1b7cf41f..2f993b36 100644 --- a/bin/bbackupd/BackupDaemon.cpp +++ b/bin/bbackupd/BackupDaemon.cpp @@ -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 @@ BackupDaemon::BackupDaemon() 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 @@ void BackupDaemon::DeleteAllLocations() } #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()); diff --git a/bin/bbackupd/BackupDaemon.h b/bin/bbackupd/BackupDaemon.h index 580cbea6..3951a8aa 100644 --- a/bin/bbackupd/BackupDaemon.h +++ b/bin/bbackupd/BackupDaemon.h @@ -58,7 +58,14 @@ private: 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; @@ -418,6 +425,8 @@ public: private: bool mDoSyncFlagOut, mSyncIsForcedOut; + bool mInstallService, mRemoveService, mRunAsService; + std::string mServiceName; HANDLE mhMessageToSendEvent, mhCommandReceivedEvent; CRITICAL_SECTION mMessageQueueLock; std::vector mMessageList; diff --git a/bin/bbackupd/bbackupd.cpp b/bin/bbackupd/bbackupd.cpp index e094d499..a0f275b3 100644 --- a/bin/bbackupd/bbackupd.cpp +++ b/bin/bbackupd/bbackupd.cpp @@ -35,63 +35,12 @@ int main(int argc, const char *argv[]) #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 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 -- cgit v1.2.3