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