]> jfr.im git - irc/quakenet/newserv.git/blame_incremental - core/main.c
merge
[irc/quakenet/newserv.git] / core / main.c
... / ...
CommitLineData
1#include "../lib/sstring.h"
2#include "events.h"
3#include "schedule.h"
4#include "hooks.h"
5#include "modules.h"
6#include "config.h"
7#include "error.h"
8#include "nsmalloc.h"
9
10#include <stdlib.h>
11#include <stdio.h>
12#include <time.h>
13#include <sys/time.h>
14#include <signal.h>
15
16int newserv_shutdown_pending;
17int newserv_sigusr1_pending;
18
19void initseed();
20void init_logfile();
21void siginthandler(int sig);
22void sigusr1handler(int sig);
23void sigsegvhandler(int sig);
24void handlecore(void);
25
26static void (*oldsegv)(int);
27
28int main(int argc, char **argv) {
29 initseed();
30 inithooks();
31 inithandlers();
32 initschedule();
33
34 init_logfile();
35
36 initsstring();
37
38 if (argc>1) {
39 initconfig(argv[1]);
40 } else {
41 initconfig("newserv.conf");
42 }
43
44 /* Loading the modules will bring in the bulk of the code */
45 initmodules();
46 signal(SIGINT, siginthandler);
47 signal(SIGUSR1, sigusr1handler);
48 oldsegv = signal(SIGSEGV, sigsegvhandler);
49
50 /* Main loop */
51 for(;;) {
52 handleevents(10);
53 doscheduledevents(time(NULL));
54 if (newserv_shutdown_pending) {
55 newserv_shutdown();
56 break;
57 }
58
59 if (newserv_sigusr1_pending) {
60 signal(SIGUSR1, sigusr1handler);
61 triggerhook(HOOK_CORE_SIGUSR1, NULL);
62 newserv_sigusr1_pending=0;
63 }
64 }
65
66 nsexit();
67
68 freeconfig();
69 finisstring();
70}
71
72/*
73 * seed the pseudo-random number generator, rand()
74 */
75void initseed() {
76 struct timeval t;
77
78 gettimeofday(&t, NULL);
79 srand(t.tv_usec);
80}
81
82void siginthandler(int sig) {
83 newserv_shutdown_pending = 1;
84}
85
86void sigusr1handler(int sig) {
87 newserv_sigusr1_pending = 1;
88}
89
90void sigsegvhandler(int sig) {
91 handlecore();
92
93 oldsegv(sig);
94}
95