📄 slpd_main.c
字号:
return 0;}/*-------------------------------------------------------------------------*/int Daemonize(const char* pidfile)/* Turn the calling process into a daemon (detach from tty setuid(), etc *//* */ /* Returns: zero on success non-zero if slpd could not daemonize (or if *//* slpd is already running . *//*-------------------------------------------------------------------------*/{ FILE* fd; struct passwd* pwent; pid_t pid; char pidstr[14]; /* fork() if we should detach */ if(G_SlpdCommandLine.detach) { pid = fork(); } else { pid = getpid(); } /* parent or child? */ switch(pid) { case -1: return -1; case 0: /* child lives */ break; default: /* parent writes pid (or child) pid file and dies */ fd = fopen(pidfile,"w"); if(fd) { sprintf(pidstr,"%i",(int)pid); fwrite(pidstr,strlen(pidstr),1,fd); fclose(fd); } if(G_SlpdCommandLine.detach) { exit(0); } break; } close(0); close(1); close(2); setsid(); /* will only fail if we are already the process group leader */ /*----------------*/ /* suid to daemon */ /*----------------*/ /* TODO: why do the following lines mess up my signal handlers? */ pwent = getpwnam("daemon"); if(pwent) { if(setgroups(1, &pwent->pw_gid) < 0 || setgid(pwent->pw_gid) < 0 || setuid(pwent->pw_uid) < 0) { /* TODO: should we log here and return fail */ exit(1); } } else exit(1); /*--------------------*/ /* Set cwd to / (root)*/ /*--------------------*/ chdir("/"); return 0;}/*--------------------------------------------------------------------------*/void SignalHandler(int signum)/*--------------------------------------------------------------------------*/{ switch(signum) { case SIGALRM: G_SIGALRM = 1; break; case SIGTERM: G_SIGTERM = 1; break; case SIGHUP: G_SIGHUP = 1; break;#ifdef DEBUG case SIGINT: G_SIGINT = 1; break;#endif case SIGPIPE: default: break; }}/*-------------------------------------------------------------------------*/int SetUpSignalHandlers()/*-------------------------------------------------------------------------*/{ int result; struct sigaction sa; sa.sa_handler = SignalHandler; sigemptyset(&sa.sa_mask); sa.sa_flags = 0;//SA_ONESHOT;#if defined(HAVE_SA_RESTORER) sa.sa_restorer = 0;#endif result = sigaction(SIGALRM,&sa,0); result |= sigaction(SIGTERM,&sa,0); result |= sigaction(SIGPIPE,&sa,0);#ifdef DEBUG result |= sigaction(SIGINT,&sa,0);#endif signal(SIGHUP,SignalHandler); //result |= sigaction(SIGHUP,&sa,0); return result;}/*=========================================================================*/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_SLPv2_SECURITY SLPDLog("Using SPI file = %s\n",G_SlpdCommandLine.spifile);#endif /*--------------------------------------------------*/ /* Initialize for the first time */ /*--------------------------------------------------*/ if(SLPDPropertyInit(G_SlpdCommandLine.cfgfile) ||#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); 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;}#endif /*ifndef _WIN32 */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -