Ejemplo n.º 1
0
/*=========================================================================*/
int main(int argc, char* argv[])
/*=========================================================================*/
{
    fd_set          readfds;
    fd_set          writefds;
    int             highfd;
    int             fdcount         = 0;

#ifdef DEBUG
    xmalloc_init("/var/log/slpd_xmalloc.log",0);
#endif

    /*------------------------*/
    /* Parse the command line */
    /*------------------------*/
    if(SLPDParseCommandLine(argc,argv))
    {
        SLPDFatal("Invalid command line\n");
    }

    /*------------------------------*/
    /* Make sure we are root        */
    /*------------------------------*/
    if(getuid() != 0)
    {
        SLPDFatal("slpd must be started by root\n");
    }

    /*--------------------------------------*/
    /* Make sure we are not already running */
    /*--------------------------------------*/
    if(CheckPid(G_SlpdCommandLine.pidfile))
    {
        SLPDFatal("slpd is already running. Check %s\n",
                 G_SlpdCommandLine.pidfile);
    }

    /*------------------------------*/
    /* Initialize the log file      */
    /*------------------------------*/
    if(SLPDLogFileOpen(G_SlpdCommandLine.logfile, 1))
    {
        SLPDFatal("Could not open logfile %s\n",G_SlpdCommandLine.logfile);
    }

    /*------------------------*/
    /* Seed the XID generator */
    /*------------------------*/
    SLPXidSeed();

    /*---------------------*/
    /* Log startup message */
    /*---------------------*/
    SLPDLog("****************************************\n");
    SLPDLogTime();
    SLPDLog("SLPD daemon started\n");
    SLPDLog("****************************************\n");
    SLPDLog("Command line = %s\n",argv[0]);
    SLPDLog("Using configuration file = %s\n",G_SlpdCommandLine.cfgfile);
    SLPDLog("Using registration file = %s\n",G_SlpdCommandLine.regfile);
#ifdef ENABLE_SECURITY
    SLPDLog("Using SPI file = %s\n",G_SlpdCommandLine.spifile);
#endif
   
    /*--------------------------------------------------*/
    /* Initialize for the first time                    */
    /*--------------------------------------------------*/
    if(SLPDPropertyInit(G_SlpdCommandLine.cfgfile) ||
#ifdef ENABLE_SECURITY
       SLPDSpiInit(G_SlpdCommandLine.spifile) ||
#endif     
       SLPDDatabaseInit(G_SlpdCommandLine.regfile) ||
       SLPDIncomingInit() ||
       SLPDOutgoingInit() ||
       SLPDKnownDAInit())
    {
        SLPDFatal("slpd initialization failed\n");
    }
    SLPDLog("Agent Interfaces = %s\n",G_SlpdProperty.interfaces);
    SLPDLog("Agent URL = %s\n",G_SlpdProperty.myUrl);

    /*---------------------------*/
    /* make slpd run as a daemon */
    /*---------------------------*/
    if(Daemonize(G_SlpdCommandLine.pidfile))
    {
        SLPDFatal("Could not daemonize\n");
    }

    /*-----------------------*/
    /* Setup signal handlers */
    /*-----------------------*/
    if(SetUpSignalHandlers())
    {
        SLPDFatal("Error setting up signal handlers.\n");
    }

    /*------------------------------*/
    /* Set up alarm to age database */
    /*------------------------------*/
    alarm(SLPD_AGE_INTERVAL);

    /*-----------*/
    /* Main loop */
    /*-----------*/
    SLPDLog("Startup complete entering main run loop ...\n\n");
    G_SIGALRM   = 0;
    G_SIGTERM   = 0;
    G_SIGHUP    = 0;    
#ifdef DEBUG
    G_SIGINT    = 0;
#endif

    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)
        {
            HandleSigHup();
            G_SIGHUP = 0;
        }
        if(G_SIGALRM)
        {
            HandleSigAlrm();
            G_SIGALRM = 0;
            alarm(SLPD_AGE_INTERVAL);
        }
#ifdef DEBUG
	if (G_SIGINT)
	{
	    HandleSigInt();
	    G_SIGINT = 0;
	}			
#endif

    } /* End of main loop */

    /* Got SIGTERM */
    HandleSigTerm();

    return 0;
}
Ejemplo n.º 2
0
/** Process main entry point.
 *
 * @param[in] argc - The number of command line arguments passed in @p argv.
 * @param[in] argv - An array of pointers to command line arguments.
 *
 * @return Zero on success, or a non-zero shell error code.
 *
 * @remarks This routine contains the main server loop.
 */
int main(int argc, char * argv[])
{
   fd_set readfds;
   fd_set writefds;
   int highfd;
   int fdcount = 0;
   time_t curtime;
   struct timeval timeout;

#ifdef DEBUG
   xmalloc_init("/var/log/slpd_xmalloc.log", 0);
#endif

   /* Parse the command line */
   if (SLPDParseCommandLine(argc,argv))
      SLPDFatal("Invalid command line\n");

   /* make sure we are root */
   if (getuid() != 0)
      SLPDFatal("slpd must be started by root\n");

   /* make sure we are not already running */
   if (CheckPid(G_SlpdCommandLine.pidfile))
      SLPDFatal("slpd is already running. Check %s\n",
            G_SlpdCommandLine.pidfile);

   /* Initialize the preferences so we know if the log file is to be
      overwritten or appended.*/
   if (SLPDPropertyInit(G_SlpdCommandLine.cfgfile))
      SLPDFatal("slpd initialization failed during property load\n");

   /* make slpd run as a daemon */
   if (Daemonize(G_SlpdCommandLine.pidfile))
      SLPDFatal("Could not daemonize\n");

   /* initialize the log file */
   if (SLPDLogFileOpen(G_SlpdCommandLine.logfile, G_SlpdProperty.appendLog))
      SLPDFatal("Could not open logfile %s\n",G_SlpdCommandLine.logfile);

   /* seed the XID generator */
   SLPXidSeed();

   /* log startup message */
   SLPDLog("****************************************\n");
   SLPDLogTime();
   SLPDLog("SLPD daemon started\n");
   SLPDLog("****************************************\n");
   SLPDLog("Command line = %s\n", argv[0]);
   SLPDLog("Using configuration file = %s\n", G_SlpdCommandLine.cfgfile);
   SLPDLog("Using registration file = %s\n", G_SlpdCommandLine.regfile);
#ifdef ENABLE_SLPv2_SECURITY
   SLPDLog("Using SPI file = %s\n", G_SlpdCommandLine.spifile);
#endif

   /* initialize for the first time */
   SLPDPropertyReinit();  /*So we get any property-related log messages*/
   if (
#ifdef ENABLE_SLPv2_SECURITY
         SLPDSpiInit(G_SlpdCommandLine.spifile) ||
#endif     
         SLPDDatabaseInit(G_SlpdCommandLine.regfile)
         || SLPDIncomingInit() 
         || SLPDOutgoingInit() 
         || SLPDKnownDAInit())
      SLPDFatal("slpd initialization failed\n");
   SLPDLog("Agent Interfaces = %s\n", G_SlpdProperty.interfaces);
   if (G_SlpdProperty.port != SLP_RESERVED_PORT)
      SLPDLog("Using port %d instead of default %d\n", G_SlpdProperty.port, SLP_RESERVED_PORT);

   /* drop privileges to reduce security risk */
   if (DropPrivileges())
      SLPDFatal("Could not drop privileges\n");

   /* Setup signal handlers */
   if (SetUpSignalHandlers())
      SLPDFatal("Error setting up signal handlers.\n");

   /* Set up alarm to age database -- a shorter start, so SAs register with us quickly on our startup */
   alarm(2);

   /* Main loop */
   SLPDLog("Startup complete entering main run loop ...\n\n");
   G_SIGALRM   = 0;
   G_SIGTERM   = 0;
   G_SIGHUP    = 0;    
#ifdef DEBUG
   G_SIGINT    = 0;
   G_SIGUSR1   = 0;    
#endif

   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 -- we time out every second so the outgoing retries can occur*/
      time(&curtime);  
      timeout.tv_sec = 1;
      timeout.tv_usec = 0;
      fdcount = select(highfd + 1, &readfds, &writefds, 0, &timeout);
      if (fdcount > 0) /* fdcount will be < 0 when interrupted by a signal */
      {
         SLPDIncomingHandler(&fdcount, &readfds, &writefds);
         SLPDOutgoingHandler(&fdcount, &readfds, &writefds);
         SLPDOutgoingRetry(time(0) - curtime);
      }
      else if (fdcount == 0)
         SLPDOutgoingRetry(time(0) - curtime);


HANDLE_SIGNAL:

      if (G_SIGHUP)
      {
         HandleSigHup();
         G_SIGHUP = 0;
      }
      if (G_SIGALRM)
      {
         HandleSigAlrm();
         G_SIGALRM = 0;
         alarm(SLPD_AGE_INTERVAL);
      }

#ifdef DEBUG
      if (G_SIGINT)
      {
         HandleSigInt();
         G_SIGINT = 0;
      }        

      if (G_SIGUSR1)
      {
         HandleSigUsr1();
         G_SIGUSR1 = 0;
      }
#endif

   } /* End of main loop */

   /* Got SIGTERM */
   HandleSigTerm();

   return 0;
}