Changeset 991


Ignore:
Timestamp:
12/10/2006 23:14:16 (5 years ago)
Author:
chris
Message:
  • Initialise Windows sockets automatically for all daemons on Win32
  • Write PID files on Win32
File:
1 edited

Legend:

Unmodified
Added
Removed
  • box/trunk/lib/server/Daemon.cpp

    r456 r991  
    2222#ifdef HAVE_SYSLOG_H 
    2323        #include <syslog.h> 
     24#endif 
     25 
     26#ifdef WIN32 
     27        #include <ws2tcpip.h> 
    2428#endif 
    2529 
     
    143147                                fprintf(stderr, "%s: failed to start: " 
    144148                                        "failed to open configuration file: " 
    145                                         "%s", DaemonName(),  
     149                                        "%s\n", DaemonName(),  
    146150                                        mConfigFileName.c_str()); 
    147151#ifdef WIN32 
     
    190194                        THROW_EXCEPTION(ServerException, DaemoniseFailed) 
    191195                } 
     196#endif // !WIN32 
    192197                 
    193198                // Server configuration 
     
    198203                pidFileName = serverConfig.GetKeyValue("PidFile"); 
    199204                FileHandleGuard<(O_WRONLY | O_CREAT | O_TRUNC), (S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH)> pidFile(pidFileName.c_str()); 
    200                  
     205         
     206#ifndef WIN32    
    201207                // Handle changing to a different user 
    202208                if(serverConfig.KeyExists("User")) 
     
    268274                // open the log 
    269275                ::openlog(DaemonName(), LOG_PID, LOG_LOCAL6); 
     276 
    270277                // Log the start message 
    271278                ::syslog(LOG_INFO, "Starting daemon (config: %s) (version "  
    272279                        BOX_VERSION ")", mConfigFileName.c_str()); 
    273280 
    274 #ifndef WIN32 
    275281                // Write PID to file 
    276282                char pid[32]; 
     283 
     284#ifdef WIN32 
     285                int pidsize = sprintf(pid, "%d", (int)GetCurrentProcessId()); 
     286#else 
    277287                int pidsize = sprintf(pid, "%d", (int)getpid()); 
     288#endif 
     289 
    278290                if(::write(pidFile, pid, pidsize) != pidsize) 
    279291                { 
     
    281293                        THROW_EXCEPTION(ServerException, DaemoniseFailed) 
    282294                } 
    283 #endif 
    284295                 
    285296                // Set up memory leak reporting 
     
    353364                return 1; 
    354365        } 
     366 
     367#ifdef WIN32 
     368        // Under win32 we must initialise the Winsock library 
     369        // before using sockets 
     370 
     371        WSADATA info; 
     372 
     373        if (WSAStartup(0x0101, &info) == SOCKET_ERROR) 
     374        { 
     375                // will not run without sockets 
     376                ::syslog(LOG_ERR, "Failed to initialise Windows Sockets"); 
     377                THROW_EXCEPTION(CommonException, Internal) 
     378        } 
     379#endif 
     380 
     381        int retcode = 0; 
    355382         
    356383        // Main Daemon running 
     
    382409                                                errors.c_str()); 
    383410                                        // And give up 
    384                                         return 1; 
     411                                        retcode = 1; 
     412                                        break; 
    385413                                } 
    386414                                 
     
    410438                        "(%d/%d)", DaemonName(), e.what(), e.GetType(),  
    411439                        e.GetSubType()); 
    412                 return 1; 
     440                retcode = 1; 
    413441        } 
    414442        catch(std::exception &e) 
     
    416444                ::syslog(LOG_ERR, "%s: terminating due to exception %s",  
    417445                        DaemonName(), e.what()); 
    418                 return 1; 
     446                retcode = 1; 
    419447        } 
    420448        catch(...) 
     
    422450                ::syslog(LOG_ERR, "%s: terminating due to unknown exception", 
    423451                        DaemonName()); 
    424                 return 1; 
    425         } 
    426          
    427         return 0; 
     452                retcode = 1; 
     453        } 
     454 
     455#ifdef WIN32 
     456        WSACleanup(); 
     457#endif 
     458         
     459        return retcode; 
    428460} 
    429461 
Note: See TracChangeset for help on using the changeset viewer.