📄 slpd_main.c
字号:
/***************************************************************************//* *//* Project: OpenSLP - OpenSource implementation of Service Location *//* Protocol Version 2 *//* *//* File: slpd_main.c *//* *//* Abstract: Main daemon loop *//* *//*-------------------------------------------------------------------------*//* *//* Please submit patches to http://www.openslp.org *//* *//*-------------------------------------------------------------------------*//* *//* Copyright (C) 2000 Caldera Systems, Inc *//* All rights reserved. *//* *//* Redistribution and use in source and binary forms, with or without *//* modification, are permitted provided that the following conditions are *//* met: */ /* *//* Redistributions of source code must retain the above copyright *//* notice, this list of conditions and the following disclaimer. *//* *//* Redistributions in binary form must reproduce the above copyright *//* notice, this list of conditions and the following disclaimer in *//* the documentation and/or other materials provided with the *//* distribution. *//* *//* Neither the name of Caldera Systems nor the names of its *//* contributors may be used to endorse or promote products derived *//* from this software without specific prior written permission. *//* *//* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS *//* `AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT *//* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR *//* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE CALDERA *//* SYSTEMS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, *//* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT *//* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, *//* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON *//* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT *//* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE *//* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. *//* *//***************************************************************************/#include "slpd.h"/*=========================================================================*//* slpd includes *//*=========================================================================*/#include "slpd_log.h"#include "slpd_socket.h"#include "slpd_incoming.h"#include "slpd_outgoing.h"#include "slpd_database.h"#include "slpd_cmdline.h"#include "slpd_knownda.h"#include "slpd_property.h"#ifdef ENABLE_SLPv2_SECURITY#include "slpd_spi.h"#endif/*=========================================================================*//* common code includes *//*=========================================================================*/#include "slp_xmalloc.h"#include "slp_xid.h"/*==========================================================================*/int G_SIGALRM;int G_SIGTERM;int G_SIGHUP; #ifdef DEBUGint G_SIGINT; /* Signal being used for dumping registrations */#endif /*==========================================================================*//*-------------------------------------------------------------------------*/void LoadFdSets(SLPList* socklist, int* highfd, fd_set* readfds, fd_set* writefds)/*-------------------------------------------------------------------------*/{ SLPDSocket* sock = 0; SLPDSocket* del = 0; sock = (SLPDSocket*)socklist->head; while(sock) { if(sock->fd > *highfd) { *highfd = sock->fd; } switch(sock->state) { case DATAGRAM_UNICAST: case DATAGRAM_MULTICAST: case DATAGRAM_BROADCAST: FD_SET(sock->fd,readfds); break; case SOCKET_LISTEN: if(socklist->count < SLPD_MAX_SOCKETS) { FD_SET(sock->fd,readfds); } break; case STREAM_READ: case STREAM_READ_FIRST: FD_SET(sock->fd,readfds); break; case STREAM_WRITE: case STREAM_WRITE_FIRST: case STREAM_CONNECT_BLOCK: FD_SET(sock->fd,writefds); break; case SOCKET_CLOSE: del = sock; break; default: break; } sock = (SLPDSocket*)sock->listitem.next; if(del) { SLPDSocketFree((SLPDSocket*)SLPListUnlink(socklist,(SLPListItem*)del)); del = 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_SLPv2_SECURITY SLPDSpiDeinit(); #endif SLPDDatabaseDeinit(); SLPDPropertyDeinit(); SLPDLogFileClose(); xmalloc_deinit(); #endif}/*------------------------------------------------------------------------*/void HandleSigHup()/*------------------------------------------------------------------------*/{ /* Reinitialize */ SLPDLog("****************************************\n"); SLPDLogTime(); SLPDLog("SLPD daemon reset by SIGHUP\n"); SLPDLog("****************************************\n\n"); /* unregister with all DAs */ SLPDKnownDADeinit(); /* re-read properties */ SLPDPropertyInit(G_SlpdCommandLine.cfgfile);#ifdef ENABLE_SLPv2_SECURITY /* Re-initialize SPI stuff*/ SLPDSpiInit(G_SlpdCommandLine.spifile);#endif /* Re-read the static registration file (slp.reg)*/ SLPDDatabaseReInit(G_SlpdCommandLine.regfile); /* Rebuild Known DA database */ SLPDKnownDAInit(); SLPDLog("****************************************\n"); SLPDLogTime(); SLPDLog("SLPD daemon reset finished\n"); SLPDLog("****************************************\n\n");}/*------------------------------------------------------------------------*/void HandleSigAlrm()/*------------------------------------------------------------------------*/{ SLPDIncomingAge(SLPD_AGE_INTERVAL); SLPDOutgoingAge(SLPD_AGE_INTERVAL); SLPDKnownDAImmortalRefresh(SLPD_AGE_INTERVAL); SLPDKnownDAPassiveDAAdvert(SLPD_AGE_INTERVAL,0); SLPDKnownDAActiveDiscovery(SLPD_AGE_INTERVAL); SLPDDatabaseAge(SLPD_AGE_INTERVAL,G_SlpdProperty.isDA);}#ifdef DEBUG/*--------------------------------------------------------------------------*/void HandleSigInt()/*--------------------------------------------------------------------------*/{ SLPDIncomingSocketDump(); SLPDOutgoingSocketDump(); SLPDKnownDADump(); SLPDDatabaseDump();}#endif#ifndef _WIN32/*-------------------------------------------------------------------------*/int CheckPid(const char* pidfile)/* Check a pid file to see if slpd is already running *//* *//* Returns: 0 on success. non-zero on failure *//*-------------------------------------------------------------------------*/{ pid_t pid; FILE* fd; char pidstr[14]; /*------------------------------------------*/ /* make sure that we're not running already */ /*------------------------------------------*/ /* read the pid from the file */ fd = fopen(pidfile,"r"); if(fd) { memset(pidstr,0,14); fread(pidstr,13,1,fd); pid = atoi(pidstr); if(pid) { if(kill(pid,0) == 0) { /* we are already running */ return -1; } } fclose(fd); } return 0;}/*-------------------------------------------------------------------------*/int WritePid(const char* pidfile, pid_t pid)/* Write the pid file *//* *//* Returns: 0 on success. non-zero on failure *//*-------------------------------------------------------------------------*/{ FILE* fd; char pidstr[14]; /* write my pid to the pidfile */ fd = fopen(pidfile,"w"); if(fd) { sprintf(pidstr,"%i",(int)pid); fwrite(pidstr,strlen(pidstr),1,fd); fclose(fd); }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -