int main(int argc, char *argv[]) { char* endptr; sysop = _sysop; syserrm = _syserrm; syserrc = _syserrc; check_plugin = _check_plugin; #if ENABLE_SEM do_inter_lock_init = _do_inter_lock_init; do_inter_lock_reset = _do_inter_lock_reset; do_inter_lock = _do_inter_lock; do_inter_unlock = _do_inter_unlock; #endif /*------------------------ get host name */ if (gethostname((char*)hostname,255) == -1) dm_error(errno, "gethostname"); /*------------------------ parse arguments */ if (argc != 2) usage_error(0); switch (serverport = strtol(argv[1], &endptr, 0)) { case LONG_MAX: case LONG_MIN: if (errno) usage_error(errno); } if (! *(argv[1]) || *endptr) usage_error(errno); serverport += getportoffset(); initfds(); /*------------- make server socket and listen on it ----------------- Note: all communication errors that can only be due to faulty code (rather than an external condition) are reported through 'error' and thus lead to termination of this process. */ run_dnode_mill(); }
int session(void) { fd_set rfds; struct timeval *timeout; int error; struct myaddrs *p; char pid_file[MAXPATHLEN]; FILE *fp; pid_t racoon_pid = 0; int i; /* initialize schedular */ sched_init(); init_signal(); #ifdef ENABLE_ADMINPORT if (admin_init() < 0) exit(1); #endif initmyaddr(); if (isakmp_init() < 0) exit(1); initfds(); #ifdef ENABLE_NATT natt_keepalive_init (); #endif if (privsep_init() != 0) exit(1); for (i = 0; i <= NSIG; i++) sigreq[i] = 0; /* write .pid file */ racoon_pid = getpid(); if (lcconf->pathinfo[LC_PATHTYPE_PIDFILE] == NULL) strlcpy(pid_file, _PATH_VARRUN "racoon.pid", MAXPATHLEN); else if (lcconf->pathinfo[LC_PATHTYPE_PIDFILE][0] == '/') strlcpy(pid_file, lcconf->pathinfo[LC_PATHTYPE_PIDFILE], MAXPATHLEN); else { strlcat(pid_file, _PATH_VARRUN, MAXPATHLEN); strlcat(pid_file, lcconf->pathinfo[LC_PATHTYPE_PIDFILE], MAXPATHLEN); } fp = fopen(pid_file, "w"); if (fp) { if (fchmod(fileno(fp), S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH) == -1) { syslog(LOG_ERR, "%s", strerror(errno)); fclose(fp); exit(1); } fprintf(fp, "%ld\n", (long)racoon_pid); fclose(fp); } else { plog(LLV_ERROR, LOCATION, NULL, "cannot open %s", pid_file); } while (1) { if (dying) rfds = maskdying; else rfds = mask0; /* * asynchronous requests via signal. * make sure to reset sigreq to 0. */ check_sigreq(); /* scheduling */ timeout = schedular(); error = select(nfds, &rfds, (fd_set *)0, (fd_set *)0, timeout); if (error < 0) { switch (errno) { case EINTR: continue; default: plog(LLV_ERROR, LOCATION, NULL, "failed to select (%s)\n", strerror(errno)); return -1; } /*NOTREACHED*/ } #ifdef ENABLE_ADMINPORT if ((lcconf->sock_admin != -1) && (FD_ISSET(lcconf->sock_admin, &rfds))) admin_handler(); #endif for (p = lcconf->myaddrs; p; p = p->next) { if (!p->addr) continue; if (FD_ISSET(p->sock, &rfds)) isakmp_handler(p->sock); } if (FD_ISSET(lcconf->sock_pfkey, &rfds)) pfkey_handler(); if (lcconf->rtsock >= 0 && FD_ISSET(lcconf->rtsock, &rfds)) { if (update_myaddrs() && lcconf->autograbaddr) sched_new(5, check_rtsock, NULL); initfds(); } } }
int IterativeServer::selectLoop() { MustBeTrue(ok_); MustBeTrue(!endpoints_.empty()); int maxsocket = -1; fd_set readfds; initfds(readfds, master_readfds_, readhandlers_, maxsocket); fd_set writefds; initfds(writefds, master_writefds_, writehandlers_, maxsocket); fd_set exceptionfds; initfds(exceptionfds, master_exceptionfds_, exceptionhandlers_, maxsocket); for (Timer zero(0,0); !endpoints_.empty(); ) { // reset descriptor list fd_set *preadfds; fd_set *pwritefds; fd_set *pexceptionfds; if (!readhandlers_.empty()) { memcpy(&readfds, &master_readfds_, sizeof(master_readfds_)); preadfds = &readfds; } else { preadfds = NULL; } if (!writehandlers_.empty()) { memcpy(&writefds, &master_writefds_, sizeof(master_writefds_)); pwritefds = &writefds; } else { pwritefds = NULL; } if (!exceptionhandlers_.empty()) { memcpy(&exceptionfds, &master_exceptionfds_, sizeof(master_exceptionfds_)); pexceptionfds = &exceptionfds; } else { pexceptionfds = NULL; } // do we have any timers? timeval *ptimer, timer; for (ptimer=NULL; !timerqueue_.empty(); ) { TimerQueueItem tqi = timerqueue_.top(); CTMRSIter ctit = canceledtimers_.find(CanceledTimerQueueItem(tqi)); if (ctit != canceledtimers_.end()) { canceledtimers_.erase(ctit); continue; } Timer timeout = tqi.timer_; Timer now; now.setToNow(); timeout -= now; if (timeout < zero) timeout = zero; timeout.setTimeval(timer); ptimer = &timer; } // wait for an event int status = ::select(maxsocket+1, preadfds, pwritefds, pexceptionfds, ptimer); if (status < 0) { // error of some type ok_ = false; return(-1); } else if (status == 0) { // did timer fire? if (ptimer == &timer) { // we have a timeout. check queue Timer now; now.setToNow(); while (!timerqueue_.empty()) { if (timerqueue_.top().timer_ > now) break; TimerQueueItem tqi = timerqueue_.top(); timerqueue_.pop(); calltimerhandler(tqi, master_readfds_, master_writefds_, master_exceptionfds_); } } } else { // we have i/o to handle for (int socket=0; socket<=maxsocket; ++socket) { // skip if not ready if (preadfds != NULL && FD_ISSET(socket, preadfds)) { callIOhandler(socket, master_readfds_, readhandlers_, master_writefds_, master_exceptionfds_); } if (pwritefds != NULL && FD_ISSET(socket, pwritefds)) { callIOhandler(socket, master_writefds_, writehandlers_, master_readfds_, master_exceptionfds_); } if (pexceptionfds != NULL && FD_ISSET(socket, pexceptionfds)) { callIOhandler(socket, master_exceptionfds_, exceptionhandlers_, master_readfds_, master_writefds_); } } } } return 0; }