Changeset 1854


Ignore:
Timestamp:
22/09/2007 00:05:25 (4 years ago)
Author:
chris
Message:

Use Daemon's delegated option processing instead of our own hacks.

Move Windows service startup, installation and removal to BackupDaemon?.

Location:
box/chris/general/bin/bbackupd
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • box/chris/general/bin/bbackupd/BackupDaemon.cpp

    r1831 r1854  
    7777#include "autogen_ClientException.h" 
    7878 
     79#ifdef WIN32 
     80        #include "Win32ServiceFunctions.h" 
     81        #include "Win32BackupService.h" 
     82 
     83        extern Win32BackupService* gpDaemonService; 
     84#endif 
     85 
    7986#include "MemLeakFindOn.h" 
    8087 
     
    117124          mDeleteUnusedRootDirEntriesAfter(0), 
    118125          mLogAllFileAccess(false) 
     126        #ifdef WIN32 
     127        , mInstallService(false), 
     128          mRemoveService(false), 
     129          mRunAsService(false) 
     130        #endif 
    119131{ 
    120132        // Only ever one instance of a daemon 
     
    277289 
    278290#ifdef WIN32 
     291std::string BackupDaemon::GetOptionString() 
     292{ 
     293        std::string oldOpts = this->Daemon::GetOptionString(); 
     294        ASSERT(oldOpts.find("s") == std::string::npos); 
     295        ASSERT(oldOpts.find("S") == std::string::npos); 
     296        ASSERT(oldOpts.find("i") == std::string::npos); 
     297        ASSERT(oldOpts.find("r") == std::string::npos); 
     298        return oldOpts + "sS:ir"; 
     299} 
     300 
     301int BackupDaemon::ProcessOption(signed int option) 
     302{ 
     303        switch(option) 
     304        { 
     305                case 's': 
     306                { 
     307                        mRunAsService = true; 
     308                        return 0; 
     309                } 
     310 
     311                case 'S': 
     312                { 
     313                        mServiceName = optarg; 
     314                        return 0; 
     315                } 
     316 
     317                case 'i': 
     318                { 
     319                        mInstallService = true; 
     320                        return 0; 
     321                } 
     322 
     323                case 'r': 
     324                { 
     325                        mRemoveService = true; 
     326                        return 0; 
     327                } 
     328 
     329                default: 
     330                { 
     331                        return this->Daemon::ProcessOption(option); 
     332                } 
     333        } 
     334} 
     335 
     336int BackupDaemon::Main(const std::string &rConfigFileName) 
     337{ 
     338        if (mInstallService) 
     339        { 
     340                return InstallService(rConfigFileName.c_str()); 
     341        } 
     342 
     343        if (mRemoveService) 
     344        { 
     345                return RemoveService(); 
     346        } 
     347 
     348        int returnCode; 
     349 
     350        if (mRunAsService) 
     351        { 
     352                // We will be called reentrantly by the Service Control 
     353                // Manager, and we had better not call OurService again! 
     354                mRunAsService = false; 
     355 
     356                BOX_INFO("Box Backup service starting"); 
     357                returnCode = OurService(rConfigFileName.c_str()); 
     358                BOX_INFO("Box Backup service shut down"); 
     359        } 
     360        else 
     361        { 
     362                returnCode = this->Daemon::Main(rConfigFileName); 
     363        } 
     364         
     365        return returnCode; 
     366} 
     367 
    279368void BackupDaemon::RunHelperThread(void) 
    280369{ 
  • box/chris/general/bin/bbackupd/BackupDaemon.h

    r1805 r1854  
    5959        bool DeleteStoreObjectInfo() const; 
    6060        BackupDaemon(const BackupDaemon &); 
     61 
    6162public: 
     63        #ifdef WIN32 
     64                // add command-line options to handle Windows services 
     65                std::string GetOptionString(); 
     66                int ProcessOption(signed int option); 
     67                int Main(const std::string &rConfigFileName); 
     68        #endif 
    6269 
    6370        void Run(); 
     
    417424        private: 
    418425        bool mDoSyncFlagOut, mSyncIsForcedOut; 
     426        bool mInstallService, mRemoveService, mRunAsService; 
     427        std::string mServiceName; 
    419428        HANDLE mhMessageToSendEvent, mhCommandReceivedEvent; 
    420429        CRITICAL_SECTION mMessageQueueLock; 
  • box/chris/general/bin/bbackupd/bbackupd.cpp

    r1813 r1854  
    3636#ifdef WIN32 
    3737 
    38         if(argc == 2 && 
    39                 (::strcmp(argv[1], "--help") == 0 || 
    40                  ::strcmp(argv[1], "-h") == 0)) 
    41         { 
    42                 printf("-h help, -i install service, -r remove service,\n" 
    43                         "-c <config file> start daemon now"); 
    44                 return 2; 
    45         } 
    46         if(argc == 2 && ::strcmp(argv[1], "-r") == 0) 
    47         { 
    48                 return RemoveService(); 
    49         } 
    50         if((argc == 2 || argc == 3) && ::strcmp(argv[1], "-i") == 0) 
    51         { 
    52                 const char* config = NULL; 
    53                 if (argc == 3) 
    54                 { 
    55                         config = argv[2]; 
    56                 } 
    57                 return InstallService(config); 
    58         } 
    59  
    60         bool runAsWin32Service = false; 
    61         if (argc >= 2 && ::strcmp(argv[1], "--service") == 0) 
    62         { 
    63                 runAsWin32Service = true; 
    64         } 
     38        EnableBackupRights(); 
    6539 
    6640        gpDaemonService = new Win32BackupService(); 
    67  
    68         EnableBackupRights(); 
    69  
    70         if (runAsWin32Service) 
    71         { 
    72                 BOX_INFO("Box Backup service starting"); 
    73  
    74                 char* config = NULL; 
    75                 if (argc >= 3) 
    76                 { 
    77                         config = strdup(argv[2]); 
    78                 } 
    79  
    80                 ExitCode = OurService(config); 
    81  
    82                 if (config) 
    83                 { 
    84                         free(config); 
    85                 } 
    86  
    87                 BOX_INFO("Box Backup service shut down"); 
    88         } 
    89         else 
    90         { 
    91                 ExitCode = gpDaemonService->Main( 
    92                         BOX_GET_DEFAULT_BBACKUPD_CONFIG_FILE, argc, argv); 
    93         } 
    94  
     41        ExitCode = gpDaemonService->Daemon::Main( 
     42                BOX_GET_DEFAULT_BBACKUPD_CONFIG_FILE, 
     43                argc, argv); 
    9544        delete gpDaemonService; 
    9645 
Note: See TracChangeset for help on using the changeset viewer.