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