/*------------------------------------------------------------------------*/ void HandleSigHup() /*------------------------------------------------------------------------*/ { /* Reinitialize */ SLPLog("****************************************\n"); SLPLogTime(); SLPLog("SLPD daemon reset by SIGHUP\n"); SLPLog("****************************************\n\n"); /* unregister with all DAs */ SLPDKnownDADeinit(); /* re-read properties */ SLPDPropertyInit(G_SlpdCommandLine.cfgfile); /* Re-read the static registration file (slp.reg)*/ SLPDDatabaseReInit(G_SlpdCommandLine.regfile); /* Rebuild Known DA database */ SLPDKnownDAInit(); SLPLog("****************************************\n"); SLPLogTime(); SLPLog("SLPD daemon reset finished\n"); SLPLog("****************************************\n\n"); }
/*=========================================================================*/ int main(int argc, char* argv[]) /*=========================================================================*/ { fd_set readfds; fd_set writefds; int highfd; int fdcount = 0; /*------------------------*/ /* Parse the command line */ /*------------------------*/ if(SLPDParseCommandLine(argc,argv)) { SLPFatal("Invalid command line\n"); } /*------------------------------*/ /* Make sure we are root */ /*------------------------------*/ if(getuid() != 0) { SLPFatal("slpd must be started by root\n"); } /*--------------------------------------*/ /* Make sure we are not already running */ /*--------------------------------------*/ if(CheckPid(G_SlpdCommandLine.pidfile)) { SLPFatal("slpd is already running. Check %s\n", G_SlpdCommandLine.pidfile); } /*------------------------------*/ /* Initialize the log file */ /*------------------------------*/ if(SLPLogFileOpen(G_SlpdCommandLine.logfile, 1)) { SLPFatal("Could not open logfile %s\n",G_SlpdCommandLine.logfile); } /*------------------------*/ /* Seed the XID generator */ /*------------------------*/ SLPXidSeed(); /*---------------------*/ /* Log startup message */ /*---------------------*/ SLPLog("****************************************\n"); SLPLogTime(); SLPLog("SLPD daemon started\n"); SLPLog("****************************************\n"); SLPLog("Command line = %s\n",argv[0]); SLPLog("Using configuration file = %s\n",G_SlpdCommandLine.cfgfile); SLPLog("Using registration file = %s\n",G_SlpdCommandLine.regfile); /*--------------------------------------------------*/ /* Initialize for the first time */ /*--------------------------------------------------*/ if(SLPDPropertyInit(G_SlpdCommandLine.cfgfile) || SLPDDatabaseInit(G_SlpdCommandLine.regfile) || SLPDIncomingInit() || SLPDOutgoingInit() || SLPDKnownDAInit()) { SLPFatal("slpd initialization failed\n"); } SLPLog("Agent Interfaces = %s\n",G_SlpdProperty.interfaces); SLPLog("Agent URL = %s\n",G_SlpdProperty.myUrl); /*---------------------------*/ /* make slpd run as a daemon */ /*---------------------------*/ if(Daemonize(G_SlpdCommandLine.pidfile)) { SLPFatal("Could not daemonize\n"); } /*-----------------------*/ /* Setup signal handlers */ /*-----------------------*/ if(SetUpSignalHandlers()) { SLPFatal("Error setting up signal handlers.\n"); } /*------------------------------*/ /* Set up alarm to age database */ /*------------------------------*/ alarm(SLPD_AGE_INTERVAL); /*-----------*/ /* Main loop */ /*-----------*/ SLPLog("Startup complete entering main run loop ...\n\n"); G_SIGALRM = 0; G_SIGTERM = 0; G_SIGHUP = 0; 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); } } /* End of main loop */ /* Got SIGTERM */ HandleSigTerm(); return 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; }
/** 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; }
/*=========================================================================*/ int main(int argc, char* argv[]) /*=========================================================================*/ { fd_set readfds; fd_set writefds; int highfd = 0; int fdcount = 0; SLPDSocket* sock = 0; SLPDSocket* del = 0; SLPDSocketList socketlist = {0,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); SLPDSocketInit(&socketlist); /*---------------------------*/ /* 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_TIMEOUT); /*-----------*/ /* Main loop */ /*-----------*/ while(G_SIGTERM == 0) { 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); SLPDSocketInit(&socketlist); G_SIGHUP = 0; } /*--------------------------------------------------------*/ /* Load the fdsets up with all of the sockets in the list */ /*--------------------------------------------------------*/ highfd = 0; FD_ZERO(&readfds); FD_ZERO(&writefds); sock = socketlist.head; while(sock) { if(sock->fd > highfd) { highfd = sock->fd; } switch(sock->state) { case DATAGRAM_UNICAST: case DATAGRAM_MULTICAST: FD_SET(sock->fd,&readfds); break; case SOCKET_LISTEN: if(socketlist.count < SLPD_MAX_SOCKETS) { FD_SET(sock->fd,&readfds); } break; case STREAM_READ: case STREAM_FIRST_READ: FD_SET(sock->fd,&readfds); break; case STREAM_WRITE: case STREAM_FIRST_WRITE: FD_SET(sock->fd,&writefds); break; case SOCKET_CLOSE: default: break; } sock = (SLPDSocket*)sock->listitem.next; } /*-----------------------------------------------*/ /* Check to see if we we should age the database */ /*-----------------------------------------------*/ /* there is a reason this is here instead of somewhere else, but I */ /* can't remember what it was. */ if(G_SIGALRM) { SLPDDatabaseAge(SLPD_AGE_TIMEOUT); G_SIGALRM = 0; alarm(SLPD_AGE_TIMEOUT); } /*-------------*/ /* Main select */ /*-------------*/ fdcount = select(highfd+1,&readfds,&writefds,0,0); if(fdcount > 0) { sock = socketlist.head; while(sock && fdcount) { if(FD_ISSET(sock->fd,&readfds)) { switch(sock->state) { case SOCKET_LISTEN: HandleSocketListen(&socketlist,sock); break; case DATAGRAM_UNICAST: case DATAGRAM_MULTICAST: HandleDatagramRead(&socketlist,sock); break; case STREAM_READ: case STREAM_FIRST_READ: HandleStreamRead(&socketlist,sock); break; default: break; } fdcount --; } if(FD_ISSET(sock->fd,&writefds)) { HandleStreamWrite(&socketlist,sock); fdcount --; } /* Should we close the socket */ /* TODO: Close aged sockets */ if(sock->state == SOCKET_CLOSE) { del = sock; sock = (SLPDSocket*)sock->listitem.next; HandleSocketClose(&socketlist,del); } else { sock = (SLPDSocket*)sock->listitem.next; } } } } SLPLog("Got SIGTERM. Going down\n"); return 0; }
/*--------------------------------------------------------------------------*/ void ServiceStart (int argc, char **argv) /*--------------------------------------------------------------------------*/ { fd_set readfds; fd_set writefds; int highfd; int fdcount = 0; time_t curtime; time_t alarmtime; struct timeval timeout; WSADATA wsaData; WORD wVersionRequested = MAKEWORD(1,1); /*------------------------*/ /* Service initialization */ /*------------------------*/ if(!ReportStatusToSCMgr(SERVICE_START_PENDING, /* service state*/ NO_ERROR, /* exit code */ 3000)) /* wait hint */ { goto cleanup; } if(WSAStartup(wVersionRequested, &wsaData) != 0) { (void)ReportStatusToSCMgr(SERVICE_STOP_PENDING, NO_ERROR, 0); goto cleanup; } /*------------------------*/ /* Parse the command line */ /*------------------------*/ if(SLPDParseCommandLine(argc,argv)) { ReportStatusToSCMgr(SERVICE_STOP_PENDING, /* service state */ NO_ERROR, /* exit code */ 0); /* wait hint */ goto cleanup_winsock; } if(!ReportStatusToSCMgr(SERVICE_START_PENDING, /* service state */ NO_ERROR, /* exit code */ 3000)) /* wait hint */ { goto cleanup_winsock; } /*------------------------------*/ /* Initialize the log file */ /*------------------------------*/ if(SLPDLogFileOpen(G_SlpdCommandLine.logfile, 1)) { SLPDLog("Could not open logfile %s\n",G_SlpdCommandLine.logfile); goto cleanup_winsock; } /*------------------------*/ /* 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); if(!ReportStatusToSCMgr(SERVICE_START_PENDING, /* service state */ NO_ERROR, /* exit code */ 3000)) /* wait hint */ { goto cleanup_winsock; } /*--------------------------------------------------*/ /* Initialize for the first time */ /*--------------------------------------------------*/ if(SLPDPropertyInit(G_SlpdCommandLine.cfgfile) || SLPDDatabaseInit(G_SlpdCommandLine.regfile) || SLPDIncomingInit() || SLPDOutgoingInit() || SLPDKnownDAInit()) { SLPDLog("slpd initialization failed\n"); goto cleanup_winsock; } SLPDLog("Agent Interfaces = %s\n",G_SlpdProperty.interfaces); /* Service is now running, perform work until shutdown */ if(!ReportStatusToSCMgr(SERVICE_RUNNING, /* service state */ NO_ERROR, /* exit code */ 0)) /* wait hint */ { goto cleanup_winsock; } /*-----------*/ /* Main loop */ /*-----------*/ SLPDLog("Startup complete entering main run loop ...\n\n"); G_SIGTERM = 0; curtime = time(&alarmtime); alarmtime = curtime + SLPD_AGE_INTERVAL; 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) { goto HANDLE_SIGNAL; } /*-------------*/ /* Main select */ /*-------------*/ timeout.tv_sec = SLPD_AGE_INTERVAL; timeout.tv_usec = 0; fdcount = select(highfd+1,&readfds,&writefds,0,&timeout); if(fdcount > 0) /* fdcount will be < 0 when timed out */ { SLPDIncomingHandler(&fdcount,&readfds,&writefds); SLPDOutgoingHandler(&fdcount,&readfds,&writefds); } /*----------------*/ /* Handle signals */ /*----------------*/ HANDLE_SIGNAL: curtime = time(&curtime); if(curtime >= alarmtime) { HandleSigAlrm(); alarmtime = curtime + SLPD_AGE_INTERVAL; } } /* End of main loop */ /* Got SIGTERM */ HandleSigTerm(); cleanup_winsock: WSACleanup(); cleanup: ; }