#include <stdio.h>
#include <time.h>
#include <sys/time.h>
-
-int newserv_shutdown_pending;
+#include <signal.h>
void initseed();
+void init_logfile();
+void siginthandler(int sig);
+void sigusr1handler(int sig);
+void sigsegvhandler(int sig);
+void sighuphandler(int sig);
+void handlecore(void);
+void handlesignals(void);
+
+int newserv_shutdown_pending;
+static int newserv_sigint_pending, newserv_sigusr1_pending, newserv_sighup_pending;
+static void (*oldsegv)(int);
int main(int argc, char **argv) {
initseed();
inithooks();
inithandlers();
initschedule();
+
+ init_logfile();
initsstring();
/* Loading the modules will bring in the bulk of the code */
initmodules();
+ signal(SIGINT, siginthandler);
+ signal(SIGUSR1, sigusr1handler);
+ signal(SIGHUP, sighuphandler);
+ oldsegv = signal(SIGSEGV, sigsegvhandler);
/* Main loop */
for(;;) {
handleevents(10);
doscheduledevents(time(NULL));
+
if (newserv_shutdown_pending) {
newserv_shutdown();
break;
}
+
+ handlesignals();
}
-
- nsfreeall(POOL_SSTRING);
+
+ freeconfig();
+ finisstring();
+
+ fini_logfile();
+ finischedule();
+ finihandlers();
+
nsexit();
}
+void handlesignals(void) {
+ if (newserv_sigusr1_pending) {
+ signal(SIGUSR1, sigusr1handler);
+ Error("core", ERR_INFO, "SIGUSR1 received.");
+ triggerhook(HOOK_CORE_SIGUSR1, NULL);
+ newserv_sigusr1_pending=0;
+ }
+
+ if (newserv_sighup_pending) {
+ signal(SIGHUP, sighuphandler);
+ Error("core", ERR_INFO, "SIGHUP received, rehashing...");
+ triggerhook(HOOK_CORE_REHASH, (void *)1);
+ newserv_sighup_pending=0;
+ }
+
+ if (newserv_sigint_pending) {
+ Error("core", ERR_INFO, "SIGINT received, terminating.");
+ triggerhook(HOOK_CORE_SIGINT, NULL);
+ newserv_sigint_pending=0;
+ newserv_shutdown_pending=1;
+ }
+}
+
/*
* seed the pseudo-random number generator, rand()
*/
gettimeofday(&t, NULL);
srand(t.tv_usec);
}
+
+void siginthandler(int sig) {
+ newserv_sigint_pending = 1;
+}
+
+void sigusr1handler(int sig) {
+ newserv_sigusr1_pending = 1;
+}
+
+void sighuphandler(int sig) {
+ newserv_sighup_pending = 1;
+}
+
+void sigsegvhandler(int sig) {
+ handlecore();
+
+ oldsegv(sig);
+}