]>
Commit | Line | Data |
---|---|---|
2c5db955 CP |
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" | |
010f1c74 | 8 | #include "nsmalloc.h" |
2c5db955 CP |
9 | |
10 | #include <stdlib.h> | |
11 | #include <stdio.h> | |
e29554df | 12 | #include <string.h> |
2c5db955 CP |
13 | #include <time.h> |
14 | #include <sys/time.h> | |
88c8f330 | 15 | #include <signal.h> |
2c5db955 CP |
16 | |
17 | void initseed(); | |
280505a5 | 18 | void init_logfile(); |
761decff | 19 | void fini_logfile(); |
88c8f330 | 20 | void siginthandler(int sig); |
b8e79109 | 21 | void sigusr1handler(int sig); |
cdc37146 | 22 | void sigsegvhandler(int sig); |
f69c0032 | 23 | void sighuphandler(int sig); |
cdc37146 | 24 | void handlecore(void); |
f69c0032 | 25 | void handlesignals(void); |
2c5db955 | 26 | |
f69c0032 | 27 | int newserv_shutdown_pending; |
efa44d15 | 28 | static int newserv_sigint_pending, newserv_sigusr1_pending, newserv_sighup_pending; |
506a636c CP |
29 | static void (*oldsegv)(int); |
30 | ||
2c5db955 | 31 | int main(int argc, char **argv) { |
e29554df GB |
32 | char *config = "newserv.conf"; |
33 | ||
2c5db955 CP |
34 | initseed(); |
35 | inithooks(); | |
36 | inithandlers(); | |
37 | initschedule(); | |
280505a5 | 38 | |
39 | init_logfile(); | |
2c5db955 CP |
40 | |
41 | initsstring(); | |
42 | ||
43 | if (argc>1) { | |
e29554df GB |
44 | if (strcmp(argv[1], "--help")==0) { |
45 | printf("Syntax: %s [config]\n", argv[0]); | |
46 | puts(""); | |
47 | printf("Default configuration file unless specified: %s\n", config); | |
48 | ||
49 | return 0; | |
50 | } | |
51 | ||
52 | config = argv[1]; | |
2c5db955 CP |
53 | } |
54 | ||
e29554df GB |
55 | initconfig(config); |
56 | ||
2c5db955 CP |
57 | /* Loading the modules will bring in the bulk of the code */ |
58 | initmodules(); | |
88c8f330 | 59 | signal(SIGINT, siginthandler); |
b8e79109 | 60 | signal(SIGUSR1, sigusr1handler); |
f69c0032 | 61 | signal(SIGHUP, sighuphandler); |
506a636c | 62 | oldsegv = signal(SIGSEGV, sigsegvhandler); |
2c5db955 CP |
63 | |
64 | /* Main loop */ | |
65 | for(;;) { | |
66 | handleevents(10); | |
67 | doscheduledevents(time(NULL)); | |
f69c0032 | 68 | |
83951d54 | 69 | if (newserv_shutdown_pending) { |
70 | newserv_shutdown(); | |
71 | break; | |
72 | } | |
b8e79109 | 73 | |
f69c0032 | 74 | handlesignals(); |
2c5db955 | 75 | } |
707c5824 | 76 | |
707c5824 CP |
77 | freeconfig(); |
78 | finisstring(); | |
bd293217 | 79 | |
4522b71a P |
80 | fini_logfile(); |
81 | finischedule(); | |
82 | finihandlers(); | |
83 | ||
bd293217 | 84 | nsexit(); |
3efa0380 GB |
85 | |
86 | return 0; | |
2c5db955 CP |
87 | } |
88 | ||
f69c0032 CP |
89 | void handlesignals(void) { |
90 | if (newserv_sigusr1_pending) { | |
91 | signal(SIGUSR1, sigusr1handler); | |
92 | Error("core", ERR_INFO, "SIGUSR1 received."); | |
93 | triggerhook(HOOK_CORE_SIGUSR1, NULL); | |
94 | newserv_sigusr1_pending=0; | |
95 | } | |
96 | ||
97 | if (newserv_sighup_pending) { | |
d21a13c0 | 98 | signal(SIGHUP, sighuphandler); |
f69c0032 CP |
99 | Error("core", ERR_INFO, "SIGHUP received, rehashing..."); |
100 | triggerhook(HOOK_CORE_REHASH, (void *)1); | |
101 | newserv_sighup_pending=0; | |
102 | } | |
efa44d15 CP |
103 | |
104 | if (newserv_sigint_pending) { | |
105 | Error("core", ERR_INFO, "SIGINT received, terminating."); | |
106 | triggerhook(HOOK_CORE_SIGINT, NULL); | |
107 | newserv_sigint_pending=0; | |
108 | newserv_shutdown_pending=1; | |
109 | } | |
f69c0032 CP |
110 | } |
111 | ||
2c5db955 CP |
112 | /* |
113 | * seed the pseudo-random number generator, rand() | |
114 | */ | |
115 | void initseed() { | |
116 | struct timeval t; | |
117 | ||
118 | gettimeofday(&t, NULL); | |
119 | srand(t.tv_usec); | |
120 | } | |
88c8f330 CP |
121 | |
122 | void siginthandler(int sig) { | |
efa44d15 | 123 | newserv_sigint_pending = 1; |
88c8f330 | 124 | } |
b8e79109 | 125 | |
126 | void sigusr1handler(int sig) { | |
127 | newserv_sigusr1_pending = 1; | |
128 | } | |
cdc37146 | 129 | |
f69c0032 CP |
130 | void sighuphandler(int sig) { |
131 | newserv_sighup_pending = 1; | |
132 | } | |
133 | ||
cdc37146 CP |
134 | void sigsegvhandler(int sig) { |
135 | handlecore(); | |
506a636c CP |
136 | |
137 | oldsegv(sig); | |
cdc37146 | 138 | } |