Changeset 1831


Ignore:
Timestamp:
14/09/2007 22:23:23 (4 years ago)
Author:
chris
Message:

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.

File:
1 edited

Legend:

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

    r1828 r1831  
    127127        } 
    128128 
    129 #ifdef WIN32 
    130         // Create the event object to signal from main thread to worker 
    131         // when new messages are queued to be sent to the command socket. 
    132         mhMessageToSendEvent = CreateEvent(NULL, TRUE, FALSE, NULL); 
    133         if(mhMessageToSendEvent == INVALID_HANDLE_VALUE) 
    134         { 
    135                 BOX_ERROR("Failed to create event object: error " << 
    136                         GetLastError()); 
    137                 exit(1); 
    138         } 
    139  
    140         // Create the event object to signal from worker to main thread 
    141         // when a command has been received on the command socket. 
    142         mhCommandReceivedEvent = CreateEvent(NULL, TRUE, FALSE, NULL); 
    143         if(mhCommandReceivedEvent == INVALID_HANDLE_VALUE) 
    144         { 
    145                 BOX_ERROR("Failed to create event object: error " << 
    146                         GetLastError()); 
    147                 exit(1); 
    148         } 
    149  
    150         // Create the critical section to protect the message queue 
    151         InitializeCriticalSection(&mMessageQueueLock); 
    152  
    153         // Create a thread to handle the named pipe 
    154         HANDLE hThread; 
    155         unsigned int dwThreadId; 
    156  
    157         hThread = (HANDLE) _beginthreadex(  
    158                 NULL,                        // default security attributes  
    159                 0,                           // use default stack size   
    160                 HelperThread,                // thread function  
    161                 this,                        // argument to thread function  
    162                 0,                           // use default creation flags  
    163                 &dwThreadId);                // returns the thread identifier  
    164 #endif 
     129        #ifdef WIN32 
     130                // Create the event object to signal from main thread to 
     131                // worker when new messages are queued to be sent to the 
     132                // command socket. 
     133 
     134                mhMessageToSendEvent = CreateEvent(NULL, TRUE, FALSE, NULL); 
     135                if(mhMessageToSendEvent == INVALID_HANDLE_VALUE) 
     136                { 
     137                        BOX_ERROR("Failed to create event object: error " << 
     138                                GetLastError()); 
     139                        exit(1); 
     140                } 
     141 
     142                // Create the event object to signal from worker to main thread 
     143                // when a command has been received on the command socket. 
     144 
     145                mhCommandReceivedEvent = CreateEvent(NULL, TRUE, FALSE, NULL); 
     146                if(mhCommandReceivedEvent == INVALID_HANDLE_VALUE) 
     147                { 
     148                        BOX_ERROR("Failed to create event object: error " << 
     149                                GetLastError()); 
     150                        exit(1); 
     151                } 
     152 
     153                // Create the critical section to protect the message queue 
     154                InitializeCriticalSection(&mMessageQueueLock); 
     155        #endif 
    165156} 
    166157 
     
    288279void BackupDaemon::RunHelperThread(void) 
    289280{ 
     281        const Configuration &conf(GetConfiguration()); 
    290282        mpCommandSocketInfo = new CommandSocketInfo; 
    291283        WinNamedPipeStream& rSocket(mpCommandSocketInfo->mListeningSocket); 
     
    297289                try 
    298290                { 
    299                         rSocket.Accept(BOX_NAMED_PIPE_NAME); 
     291                        std::string socket = conf.GetKeyValue("CommandSocket"); 
     292                        rSocket.Accept(socket); 
    300293                } 
    301294                catch (BoxException &e) 
     
    332325                        // Send a header line summarising the configuration  
    333326                        // and current state 
    334                         const Configuration &conf(GetConfiguration()); 
    335327                        char summary[256]; 
    336328                        size_t summarySize = sprintf(summary,  
     
    513505        Timers::Init(); 
    514506         
    515 #ifdef WIN32 
    516         try 
    517         { 
    518                 Run2(); 
    519         } 
    520         catch(...) 
    521         { 
    522                 Timers::Cleanup(); 
    523                 throw; 
    524         } 
    525 #else // ! WIN32 
    526         // Ignore SIGPIPE (so that if a command connection is broken, the daemon doesn't terminate) 
    527         ::signal(SIGPIPE, SIG_IGN); 
    528  
    529         // Create a command socket? 
    530         const Configuration &conf(GetConfiguration()); 
    531         if(conf.KeyExists("CommandSocket")) 
    532         { 
    533                 // Yes, create a local UNIX socket 
    534                 mpCommandSocketInfo = new CommandSocketInfo; 
    535                 const char *socketName = conf.GetKeyValue("CommandSocket").c_str(); 
    536                 ::unlink(socketName); 
    537                 mpCommandSocketInfo->mListeningSocket.Listen(Socket::TypeUNIX, socketName); 
    538         } 
     507        #ifdef WIN32 
     508                // Create a thread to handle the named pipe 
     509                HANDLE hThread; 
     510                unsigned int dwThreadId; 
     511 
     512                hThread = (HANDLE) _beginthreadex(  
     513                        NULL,         // default security attributes  
     514                        0,            // use default stack size   
     515                        HelperThread, // thread function  
     516                        this,         // argument to thread function  
     517                        0,            // use default creation flags  
     518                        &dwThreadId); // returns the thread identifier  
     519        #else 
     520                // Ignore SIGPIPE so that if a command connection is broken, 
     521                // the daemon doesn't terminate. 
     522                ::signal(SIGPIPE, SIG_IGN); 
     523 
     524                // Create a command socket? 
     525                const Configuration &conf(GetConfiguration()); 
     526                if(conf.KeyExists("CommandSocket")) 
     527                { 
     528                        // Yes, create a local UNIX socket 
     529                        mpCommandSocketInfo = new CommandSocketInfo; 
     530                        const char *socketName = 
     531                                conf.GetKeyValue("CommandSocket").c_str(); 
     532                        ::unlink(socketName); 
     533                        mpCommandSocketInfo->mListeningSocket.Listen( 
     534                                Socket::TypeUNIX, socketName); 
     535                } 
     536        #endif // !WIN32 
    539537 
    540538        // Handle things nicely on exceptions 
     
    545543        catch(...) 
    546544        { 
     545                #ifdef WIN32 
     546                        // Don't delete the socket, as the helper thread 
     547                        // is probably still using it. Let Windows clean 
     548                        // up after us. 
     549                #else 
    547550                if(mpCommandSocketInfo != 0) 
    548551                { 
     
    564567                        mpCommandSocketInfo = 0; 
    565568                } 
     569                #endif // WIN32 
    566570 
    567571                Timers::Cleanup(); 
     
    570574        } 
    571575 
    572         // Clean up 
    573         if(mpCommandSocketInfo != 0) 
    574         { 
    575                 delete mpCommandSocketInfo; 
    576                 mpCommandSocketInfo = 0; 
    577         } 
    578 #endif 
     576        #ifndef WIN32 
     577                // Clean up 
     578                if(mpCommandSocketInfo != 0) 
     579                { 
     580                        delete mpCommandSocketInfo; 
     581                        mpCommandSocketInfo = 0; 
     582                } 
     583        #endif 
    579584         
    580585        Timers::Cleanup(); 
Note: See TracChangeset for help on using the changeset viewer.