Exemplo n.º 1
0
/*------------------------------------------------------------------------*/
void HandleSigTerm()
/*------------------------------------------------------------------------*/
{
    struct timeval  timeout;
    fd_set          readfds;
    fd_set          writefds;
    int             highfd          = 0;
    int             fdcount         = 0;

    SLPDLog("****************************************\n");
    SLPDLogTime();
    SLPDLog("SLPD daemon shutting down\n");
    SLPDLog("****************************************\n");

    /* close all incoming sockets */
    SLPDIncomingDeinit();

    /* unregister with all DAs */
    SLPDKnownDADeinit();

    timeout.tv_sec  = 5;
    timeout.tv_usec = 0; 

    /* Do a dead DA passive advert to tell everyone we're goin' down */
    SLPDKnownDAPassiveDAAdvert(0, 1);

    /* if possible wait until all outgoing socket are done and closed */
    while(SLPDOutgoingDeinit(1))
    {
        FD_ZERO(&writefds);
        FD_ZERO(&readfds);
        LoadFdSets(&G_OutgoingSocketList, &highfd, &readfds,&writefds);
        fdcount = select(highfd+1,&readfds,&writefds,0,&timeout);
        if(fdcount == 0)
        {
            break;
        }

        SLPDOutgoingHandler(&fdcount,&readfds,&writefds);
    }

    SLPDOutgoingDeinit(0);

    SLPDLog("****************************************\n");
    SLPDLogTime();
    SLPDLog("SLPD daemon shut down\n");
    SLPDLog("****************************************\n");

#ifdef DEBUG
    #ifdef ENABLE_SECURITY
    SLPDSpiDeinit();
    #endif
    SLPDDatabaseDeinit();
    SLPDPropertyDeinit();
    xmalloc_deinit();    
#endif

}
Exemplo n.º 2
0
/*=========================================================================*/
int main(int argc, char* argv[])
/*=========================================================================*/
{
    fd_set          readfds;
    fd_set          writefds;
    int             highfd;
    int             fdcount         = 0;
    
    /*------------------------------*/
    /* Make sure we are root        */
    /*------------------------------*/
    if(getuid() != 0)
    {
        SLPFatal("slpd must be started by root\n");
    }
     
    
    /*------------------------*/
    /* Parse the command line */
    /*------------------------*/
    if(SLPDParseCommandLine(argc,argv))
    {
        SLPFatal("Invalid command line\n");
    }

    
    /*------------------------------*/
    /* Initialize the log file      */
    /*------------------------------*/
    SLPLogFileOpen(G_SlpdCommandLine.logfile, 0);
    SLPLog("****************************************\n");
    SLPLog("*** SLPD daemon started              ***\n");
    SLPLog("****************************************\n");
    SLPLog("command line = %s\n",argv[0]);
    
    
    /*--------------------------------------------------*/
    /* Initialize for the first time                    */
    /*--------------------------------------------------*/
    SLPDPropertyInit(G_SlpdCommandLine.cfgfile);
    SLPDDatabaseInit(G_SlpdCommandLine.regfile);
    SLPDIncomingInit();
    SLPDOutgoingInit();
    SLPDKnownDAInit();
    /* TODO: Check error codes on all init functions */
    
    /*---------------------------*/
    /* make slpd run as a daemon */
    /*---------------------------*/
    if(Daemonize(G_SlpdCommandLine.pidfile))
    {
        SLPFatal("Could not run as daemon\n");
    }

    /*-----------------------*/
    /* Setup signal handlers */ 
    /*-----------------------*/
    if(SetUpSignalHandlers())
    {
        SLPFatal("Could not set up signal handlers.\n");
    }
    
    /*------------------------------*/
    /* Set up alarm to age database */
    /*------------------------------*/
    alarm(SLPD_AGE_INTERVAL);

    /*-----------*/
    /* Main loop */
    /*-----------*/
    G_SIGALRM   = 0;
    G_SIGTERM   = 0;
    G_SIGHUP    = 0;    
	SLPLog("Initialization complete\n\n");
    while(G_SIGTERM == 0)
    {
        /*--------------------------------------------------------*/
        /* Load the fdsets up with all valid sockets in the list  */
        /*--------------------------------------------------------*/
        highfd = 0;
        FD_ZERO(&readfds);
        FD_ZERO(&writefds);
        LoadFdSets(&G_IncomingSocketList, &highfd, &readfds,&writefds);
        LoadFdSets(&G_OutgoingSocketList, &highfd, &readfds,&writefds);
        
        /*--------------------------------------------------*/
        /* Before select(), check to see if we got a signal */
        /*--------------------------------------------------*/
        if(G_SIGALRM || G_SIGHUP)
        {
            goto HANDLE_SIGNAL;
        }
        
        /*-------------*/
        /* Main select */
        /*-------------*/
        fdcount = select(highfd+1,&readfds,&writefds,0,0);
        if(fdcount > 0) /* fdcount will be < 0 when interrupted by a signal */
        {
            SLPDIncomingHandler(&fdcount,&readfds,&writefds);
            SLPDOutgoingHandler(&fdcount,&readfds,&writefds);
        }

        /*----------------*/
        /* Handle signals */
        /*----------------*/
HANDLE_SIGNAL:
        if(G_SIGHUP)
        {
            /* Reinitialize */
            SLPLog("****************************************\n");
            SLPLog("*** SLPD daemon restarted            ***\n");
            SLPLog("****************************************\n");
            SLPLog("Got SIGHUP reinitializing... \n");
        
            SLPDPropertyInit(G_SlpdCommandLine.cfgfile);
            SLPDDatabaseInit(G_SlpdCommandLine.regfile);
            /* Don't reinitialize Incoming because we can't rebind to */
            /* slp reserved port because we are not root now          */
            /* SLPDIncomingInit(); */
            SLPDOutgoingInit();
            SLPDKnownDAInit();
            G_SIGHUP = 0;     
        } 
        if(G_SIGALRM)
        {
            /* TODO: add call to do passive DAAdvert */
            SLPDIncomingAge(SLPD_AGE_INTERVAL);
            SLPDOutgoingAge(SLPD_AGE_INTERVAL);
            SLPDDatabaseAge(SLPD_AGE_INTERVAL);
            SLPDKnownDAActiveDiscovery();
            G_SIGALRM = 0;
            alarm(SLPD_AGE_INTERVAL);
        }
                            

    } /* End of main loop */

    SLPLog("Got SIGTERM.  Going down\n");

    #if(defined DEBUG)
    SLPDIncomingDeinit();
    SLPDOutgoingDeinit();
    printf("Number of calls to SLPBufferAlloc() = %i\n",G_Debug_SLPBufferAllocCount);
    printf("Number of calls to SLPBufferFree() = %i\n",G_Debug_SLPBufferFreeCount);
    #endif
    return 0;
}