-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
338 lines (290 loc) · 8.85 KB
/
main.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
/* Services -- main source file.
* Copyright (c) 1996-1999 Andy Church <[email protected]>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program (see the file COPYING); if not, write to the
* Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#include "services.h"
#include "timeout.h"
#include "version.h"
/******** Global variables! ********/
/* Command-line options: (note that configuration variables are in config.c) */
char *services_dir = SERVICES_DIR; /* -dir dirname */
char *log_filename = LOG_FILENAME; /* -log filename */
int debug = 0; /* -debug */
int readonly = 0; /* -readonly */
int skeleton = 0; /* -skeleton */
int nofork = 0; /* -nofork */
int forceload = 0; /* -forceload */
/* Set to 1 if we are to quit */
int quitting = 0;
/* Set to 1 if we are to quit after saving databases */
int delayed_quit = 0;
/* Contains a message as to why services is terminating */
char *quitmsg = NULL;
/* Input buffer - global, so we can dump it if something goes wrong */
char inbuf[BUFSIZE];
/* Socket for talking to server */
int servsock = -1;
/* Should we update the databases now? */
int save_data = 0;
/* At what time were we started? */
time_t start_time;
/******** Local variables! ********/
/* Set to 1 if we are waiting for input */
static int waiting = 0;
/* Set to 1 after we've set everything up */
static int started = 0;
/* If we get a signal, use this to jump out of the main loop. */
static jmp_buf panic_jmp;
/*************************************************************************/
/* If we get a weird signal, come here. */
void sighandler(int signum)
{
if (started) {
if (signum == SIGHUP) { /* SIGHUP = save databases and restart */
save_data = -2;
signal(SIGHUP, SIG_IGN);
logeo("Recibido SIGHUP, reiniciando.");
if (!quitmsg)
quitmsg = "Reiniciando por señal SIGHUP";
longjmp(panic_jmp, 1);
} else if (signum == SIGTERM) {
save_data = 1;
delayed_quit = 1;
signal(SIGTERM, SIG_IGN);
signal(SIGHUP, SIG_IGN);
logeo("Recibido SIGTERM, saliendo.");
quitmsg = "Cierre conexion por señal SIGTERM";
longjmp(panic_jmp, 1);
} else if (signum == SIGINT || signum == SIGQUIT) {
/* nothing -- terminate below */
} else if (!waiting) {
logeo("PANIC! buffer = %s", inbuf);
/* Cut off if this would make IRC command >510 characters. */
if (strlen(inbuf) > 448) {
inbuf[446] = '>';
inbuf[447] = '>';
inbuf[448] = 0;
}
canalopers(NULL, "PANIC! buffer = %s\r\n", inbuf);
} else if (waiting < 0) {
/* This is static on the off-chance we run low on stack */
static char buf[BUFSIZE];
switch (waiting) {
case -1: snprintf(buf, sizeof(buf), "in timed_update");
break;
case -11: snprintf(buf, sizeof(buf), "saving %s", NickDBName);
break;
case -12: snprintf(buf, sizeof(buf), "saving %s", ChanDBName);
break;
case -14: snprintf(buf, sizeof(buf), "saving %s", OperDBName);
break;
case -15: snprintf(buf, sizeof(buf), "saving %s", AutokillDBName);
break;
case -16: snprintf(buf, sizeof(buf), "saving %s", NewsDBName);
break;
case -21: snprintf(buf, sizeof(buf), "expiring nicknames");
break;
case -22: snprintf(buf, sizeof(buf), "expiring channels");
break;
case -24: snprintf(buf, sizeof(buf), "saving %s", XDBName);
break;
case -25: snprintf(buf, sizeof(buf), "expiring autokills");
break;
default : snprintf(buf, sizeof(buf), "waiting=%d", waiting);
}
canalopers(NULL, "PANIC! %s (%s)", buf, strsignal(signum));
logeo("PANIC! %s (%s)", buf, strsignal(signum));
}
}
if (signum == SIGUSR1 || !(quitmsg = malloc(BUFSIZE))) {
quitmsg = "Out of memory!";
quitting = 1;
} else {
#if HAVE_STRSIGNAL
snprintf(quitmsg, BUFSIZE, "Services ha terminado: %s", strsignal(signum));
#else
snprintf(quitmsg, BUFSIZE, "Services ha terminado por señal %d", signum);
#endif
quitting = 1;
}
if (started)
longjmp(panic_jmp, 1);
else {
logeo("%s", quitmsg);
if (isatty(2))
fprintf(stderr, "%s\n", quitmsg);
exit(1);
}
}
/*************************************************************************/
/* Main routine. (What does it look like? :-) ) */
int main(int ac, char **av, char **envp)
{
volatile time_t last_update; /* When did we last update the databases? */
volatile time_t last_expire; /* When did we last expire nicks/channels? */
volatile time_t last_check; /* When did we last check timeouts? */
int i;
char *progname;
/* Find program name. */
if ((progname = strrchr(av[0], '/')) != NULL)
progname++;
else
progname = av[0];
/* Were we run under "listnicks" or "listchans"? Do appropriate stuff
* if so. */
if (strcmp(progname, "listnicks") == 0) {
do_listnicks(ac, av);
return 0;
} else if (strcmp(progname, "listchans") == 0) {
do_listchans(ac, av);
return 0;
}
/* Initialization stuff. */
if ((i = init(ac, av)) != 0)
return i;
/* We have a line left over from earlier, so process it first. */
process();
/* Set up timers. */
last_update = time(NULL);
last_expire = time(NULL);
last_check = time(NULL);
/* The signal handler routine will drop back here with quitting != 0
* if it gets called. */
setjmp(panic_jmp);
started = 1;
/*** Main loop. ***/
while (!quitting) {
time_t t = time(NULL);
if (debug >= 2)
logeo("debug: Top of main loop");
if (!readonly && (save_data || t-last_expire >= ExpireTimeout)) {
if (debug)
logeo("debug: Running expire routines");
/* canalopers(ServerName, "Ejecutando rutinas de expiracion");*/
if (!skeleton) {
#if defined(SOPORTE_JOOMLA15)
waiting = -20;
mirar_tablas();
#endif
waiting = -21;
expire_nicks();
waiting = -22;
expire_chans();
waiting = -23;
/*expire_creg();*/
}
waiting = -25;
expire_akills();
expire_aregistras();
expire_alimits();
expire_anicks();
last_expire = t;
}
if (!readonly && (save_data || t-last_update >= UpdateTimeout)) {
waiting = -2;
if (debug)
logeo("debug: Saving databases");
/* canalopers(ServerName, "Grabando DB's"); */
if (!skeleton) {
waiting = -11;
save_ns_dbase();
waiting = -12;
save_cs_dbase();
waiting = -13;
save_cr_dbase();
}
waiting = -14;
save_os_dbase();
waiting = -15;
save_akill();
waiting = -16;
save_news();
waiting = -17;
save_spam();
waiting = -18;
save_ipv();
waiting = -19;
save_jok_dbase();
waiting = -20;
save_aregistra();
waiting = -21;
save_alimit();
waiting = -22;
save_anick();
waiting = -23;
save_achanakick();
waiting = -24;
/*save_x_dbase();*/
if (save_data < 0)
break; /* out of main loop */
save_data = 0;
last_update = t;
}
if (delayed_quit)
break;
waiting = -1;
if (t-last_check >= TimeoutCheck) {
check_timeouts();
last_check = t;
}
waiting = 1;
i = (int)(long)sgets2(inbuf, sizeof(inbuf), servsock);
waiting = 0;
if (i > 0) {
process();
} else if (i == 0) {
int errno_save = errno;
quitmsg = malloc(BUFSIZE);
if (quitmsg) {
snprintf(quitmsg, BUFSIZE,
"Read error from server: %s", strerror(errno_save));
} else {
quitmsg = "Read error from server";
}
quitting = 1;
}
waiting = -4;
}
/* Check for restart instead of exit */
if (save_data == -2) {
#if defined(SERVICES_BIN)
logeo("Reiniciando");
if (!quitmsg)
quitmsg = "Reiniciando";
send_cmd(ServerName, "SQUIT %s 0 :%s", ServerName, quitmsg);
disconn(servsock);
close_log();
execve(SERVICES_BIN, av, envp);
if (!readonly) {
open_log();
log_perror("Restart failed");
close_log();
}
return 1;
#else
quitmsg = "Restart attempt failed--SERVICES_BIN not defined (rerun configure)";
#endif
}
/* Disconnect and exit */
if (!quitmsg)
quitmsg = "Services ha terminado, razón desconocida";
logeo("%s", quitmsg);
if (started)
send_cmd(ServerName, "SQUIT %s 0 :%s", ServerName, quitmsg);
disconn(servsock);
return 0;
}
/*************************************************************************/