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
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
|
#include <sys/types.h>
#include <sys/time.h>
#include <sys/wait.h>
#include <sys/param.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <errno.h>
#include <signal.h>
#ifdef HAVE_LIBUTIL_H
#include <libutil.h>
#endif
#include <syslog.h>
#include <EXTERN.h> /* from the Perl distribution */
#include <perl.h> /* from the Perl distribution */
#include "main.h"
#include "cfg_file.h"
#include "util.h"
struct config_file *cfg;
static void sig_handler (int );
static struct rspamd_worker * fork_worker (struct rspamd_main *, int, int, enum process_type);
sig_atomic_t do_restart;
sig_atomic_t do_terminate;
sig_atomic_t child_dead;
sig_atomic_t child_ready;
extern int yynerrs;
extern FILE *yyin;
extern void boot_DynaLoader (pTHX_ CV* cv);
extern void boot_Socket (pTHX_ CV* cv);
PerlInterpreter *perl_interpreter;
/* XXX: remove this shit when it would be clear why perl need this line */
PerlInterpreter *my_perl;
static
void sig_handler (int signo)
{
switch (signo) {
case SIGHUP:
do_restart = 1;
do_reopen_log = 1;
break;
case SIGINT:
case SIGTERM:
do_terminate = 1;
break;
case SIGCHLD:
child_dead = 1;
break;
case SIGUSR2:
child_ready = 1;
break;
}
}
void
xs_init(pTHX)
{
dXSUB_SYS;
/* DynaLoader is a special case */
newXS ("DynaLoader::boot_DynaLoader", boot_DynaLoader, __FILE__);
}
static void
init_filters (struct config_file *cfg)
{
struct perl_module *module;
LIST_FOREACH (module, &cfg->perl_modules, next) {
if (module->path) {
require_pv (module->path);
}
}
}
static struct rspamd_worker *
fork_worker (struct rspamd_main *rspamd, int listen_sock, int reconfig, enum process_type type)
{
struct rspamd_worker *cur;
char *cfg_file;
FILE *f;
struct config_file *tmp_cfg;
/* Starting worker process */
cur = (struct rspamd_worker *)g_malloc (sizeof (struct rspamd_worker));
if (cur) {
/* Reconfig needed */
if (reconfig) {
tmp_cfg = (struct config_file *) g_malloc (sizeof (struct config_file));
if (tmp_cfg) {
bzero (tmp_cfg, sizeof (struct config_file));
tmp_cfg->cfg_pool = memory_pool_new (32768);
cfg_file = memory_pool_strdup (tmp_cfg->cfg_pool, rspamd->cfg->cfg_name);
f = fopen (rspamd->cfg->cfg_name , "r");
if (f == NULL) {
msg_warn ("fork_worker: cannot open file: %s", rspamd->cfg->cfg_name );
}
else {
yyin = f;
yyrestart (yyin);
if (yyparse() != 0 || yynerrs > 0) {
msg_warn ("fork_worker: yyparse: cannot parse config file, %d errors", yynerrs);
fclose (f);
}
else {
free_config (rspamd->cfg);
g_free (rspamd->cfg);
rspamd->cfg = tmp_cfg;
rspamd->cfg->cfg_name = cfg_file;
}
}
}
}
bzero (cur, sizeof (struct rspamd_worker));
TAILQ_INSERT_HEAD (&rspamd->workers, cur, next);
cur->srv = rspamd;
cur->pid = fork();
switch (cur->pid) {
case 0:
/* TODO: add worker code */
switch (type) {
case TYPE_CONTROLLER:
setproctitle ("controller process");
pidfile_close (rspamd->pfh);
msg_info ("fork_worker: starting controller process %d", getpid ());
cur->type = TYPE_CONTROLLER;
start_controller (cur);
break;
case TYPE_WORKER:
default:
setproctitle ("worker process");
pidfile_close (rspamd->pfh);
msg_info ("fork_worker: starting worker process %d", getpid ());
cur->type = TYPE_WORKER;
start_worker (cur, listen_sock);
break;
}
break;
case -1:
msg_err ("fork_worker: cannot fork main process. %m");
pidfile_remove (rspamd->pfh);
exit (-errno);
break;
}
}
return cur;
}
int
main (int argc, char **argv)
{
struct rspamd_main *rspamd;
struct module_ctx *cur_module = NULL;
int res = 0, i, listen_sock;
struct sigaction signals;
struct rspamd_worker *cur, *cur_tmp, *active_worker;
struct sockaddr_un *un_addr;
FILE *f;
pid_t wrk;
char *args[] = { "", "-e", "0", NULL };
rspamd = (struct rspamd_main *)g_malloc (sizeof (struct rspamd_main));
bzero (rspamd, sizeof (struct rspamd_main));
rspamd->server_pool = memory_pool_new (memory_pool_get_size ());
cfg = (struct config_file *)g_malloc (sizeof (struct config_file));
rspamd->cfg = cfg;
if (!rspamd || !rspamd->cfg) {
fprintf(stderr, "Cannot allocate memory\n");
exit(-errno);
}
do_terminate = 0;
do_restart = 0;
child_dead = 0;
child_ready = 0;
do_reopen_log = 0;
active_worker = NULL;
rspamd->stat = memory_pool_alloc_shared (rspamd->server_pool, sizeof (struct rspamd_stat));
bzero (rspamd->stat, sizeof (struct rspamd_stat));
bzero (rspamd->cfg, sizeof (struct config_file));
rspamd->cfg->cfg_pool = memory_pool_new (memory_pool_get_size ());
init_defaults (rspamd->cfg);
bzero (&signals, sizeof (struct sigaction));
rspamd->cfg->cfg_name = memory_pool_strdup (rspamd->cfg->cfg_pool, FIXED_CONFIG_FILE);
read_cmd_line (argc, argv, rspamd->cfg);
msg_warn ("(main) starting...");
#ifndef HAVE_SETPROCTITLE
init_title (argc, argv, environ);
#endif
f = fopen (rspamd->cfg->cfg_name , "r");
if (f == NULL) {
msg_warn ("cannot open file: %s", rspamd->cfg->cfg_name );
return EBADF;
}
yyin = f;
if (yyparse() != 0 || yynerrs > 0) {
msg_warn ("yyparse: cannot parse config file, %d errors", yynerrs);
return EBADF;
}
fclose (f);
rspamd->cfg->cfg_name = memory_pool_strdup (rspamd->cfg->cfg_pool, rspamd->cfg->cfg_name );
/* Strictly set temp dir */
if (!rspamd->cfg->temp_dir) {
msg_warn ("tempdir is not set, trying to use $TMPDIR");
rspamd->cfg->temp_dir = memory_pool_strdup (rspamd->cfg->cfg_pool, getenv ("TMPDIR"));
if (!rspamd->cfg->temp_dir) {
msg_warn ("$TMPDIR is empty too, using /tmp as default");
rspamd->cfg->temp_dir = memory_pool_strdup (rspamd->cfg->cfg_pool, "/tmp");
}
}
switch (cfg->log_type) {
case RSPAMD_LOG_CONSOLE:
if (!rspamd->cfg->no_fork) {
fprintf (stderr, "Cannot log to console while daemonized, disable logging");
cfg->log_fd = -1;
}
else {
cfg->log_fd = 2;
}
g_log_set_default_handler (file_log_function, cfg);
break;
case RSPAMD_LOG_FILE:
if (cfg->log_file == NULL || open_log (cfg) == -1) {
fprintf (stderr, "Fatal error, cannot open logfile, exiting");
exit (EXIT_FAILURE);
}
g_log_set_default_handler (file_log_function, cfg);
break;
case RSPAMD_LOG_SYSLOG:
if (open_log (cfg) == -1) {
fprintf (stderr, "Fatal error, cannot open syslog facility, exiting");
exit (EXIT_FAILURE);
}
g_log_set_default_handler (syslog_log_function, cfg);
break;
}
if (!rspamd->cfg->no_fork && daemon (1, 1) == -1) {
fprintf (stderr, "Cannot daemonize\n");
exit (-errno);
}
if (write_pid (rspamd) == -1) {
msg_err ("main: cannot write pid file %s", rspamd->cfg->pid_file);
exit (-errno);
}
/* Init C modules */
for (i = 0; i < MODULES_NUM; i ++) {
cur_module = memory_pool_alloc (rspamd->cfg->cfg_pool, sizeof (struct module_ctx));
if (modules[i].module_init_func(cfg, &cur_module) == 0) {
g_hash_table_insert (cfg->c_modules, (gpointer)modules[i].name, cur_module);
}
}
rspamd->pid = getpid();
rspamd->type = TYPE_MAIN;
init_signals (&signals, sig_handler);
/* Init perl interpreter */
PERL_SYS_INIT3 (&argc, &argv, &env);
perl_interpreter = perl_alloc ();
if (perl_interpreter == NULL) {
msg_err ("main: cannot allocate perl interpreter, %m");
exit (-errno);
}
my_perl = perl_interpreter;
PERL_SET_CONTEXT (perl_interpreter);
perl_construct (perl_interpreter);
PL_exit_flags |= PERL_EXIT_DESTRUCT_END;
perl_parse (perl_interpreter, xs_init, 3, args, NULL);
/* Block signals to use sigsuspend in future */
sigprocmask(SIG_BLOCK, &signals.sa_mask, NULL);
if (rspamd->cfg->bind_family == AF_INET) {
if ((listen_sock = make_socket (rspamd->cfg->bind_host, rspamd->cfg->bind_port)) == -1) {
msg_err ("main: cannot create tcp listen socket. %m");
exit(-errno);
}
}
else {
un_addr = (struct sockaddr_un *) g_malloc (sizeof (struct sockaddr_un));
if (!un_addr || (listen_sock = make_unix_socket (rspamd->cfg->bind_host, un_addr)) == -1) {
msg_err ("main: cannot create unix listen socket. %m");
exit(-errno);
}
}
if (listen (listen_sock, -1) == -1) {
msg_err ("main: cannot listen on socket. %m");
exit(-errno);
}
TAILQ_INIT (&rspamd->workers);
setproctitle ("main process");
/* Init statfile pool */
rspamd->statfile_pool = statfile_pool_new (cfg->max_statfile_size);
for (i = 0; i < cfg->workers_number; i++) {
fork_worker (rspamd, listen_sock, 0, TYPE_WORKER);
}
/* Start controller if enabled */
if (cfg->controller_enabled) {
fork_worker (rspamd, listen_sock, 0, TYPE_CONTROLLER);
}
/* Signal processing cycle */
for (;;) {
msg_debug ("main: calling sigsuspend");
sigemptyset (&signals.sa_mask);
sigsuspend (&signals.sa_mask);
if (do_terminate) {
msg_debug ("main: catch termination signal, waiting for childs");
pass_signal_worker (&rspamd->workers, SIGTERM);
break;
}
if (child_dead) {
child_dead = 0;
msg_debug ("main: catch SIGCHLD signal, finding terminated worker");
/* Remove dead child form childs list */
wrk = waitpid (0, &res, 0);
TAILQ_FOREACH_SAFE (cur, &rspamd->workers, next, cur_tmp) {
if (wrk == cur->pid) {
/* Catch situations if active worker is abnormally terminated */
if (cur == active_worker) {
active_worker = NULL;
}
TAILQ_REMOVE(&rspamd->workers, cur, next);
if (cur->type == TYPE_CONTROLLER) {
msg_info ("main: do not restart dead controller");
g_free (cur);
break;
}
if (WIFEXITED (res) && WEXITSTATUS (res) == 0) {
/* Normal worker termination, do not fork one more */
msg_info ("main: worker process %d terminated normally", cur->pid);
}
else {
if (WIFSIGNALED (res)) {
msg_warn ("main: worker process %d terminated abnormally by signal: %d",
cur->pid, WTERMSIG(res));
}
else {
msg_warn ("main: worker process %d terminated abnormally", cur->pid);
}
/* Fork another worker in replace of dead one */
fork_worker (rspamd, listen_sock, 0, cur->type);
}
g_free (cur);
}
}
}
if (do_restart) {
do_restart = 0;
if (active_worker == NULL) {
/* Start new worker that would reread configuration*/
active_worker = fork_worker (rspamd, listen_sock, 1, TYPE_WORKER);
}
/* Do not start new workers untill active worker is not ready for accept */
}
if (child_ready) {
child_ready = 0;
if (active_worker != NULL) {
msg_info ("main: worker process %d has been successfully started", active_worker->pid);
TAILQ_FOREACH_SAFE (cur, &rspamd->workers, next, cur_tmp) {
if (cur != active_worker && !cur->is_dying) {
/* Send to old workers SIGUSR2 */
kill (cur->pid, SIGUSR2);
cur->is_dying = 1;
}
}
active_worker = NULL;
}
}
}
/* Wait for workers termination */
while (!TAILQ_EMPTY(&rspamd->workers)) {
cur = TAILQ_FIRST(&rspamd->workers);
waitpid (cur->pid, &res, 0);
msg_debug ("main(cleaning): worker process %d terminated", cur->pid);
TAILQ_REMOVE(&rspamd->workers, cur, next);
g_free(cur);
}
msg_info ("main: terminating...");
if (rspamd->cfg->bind_family == AF_UNIX) {
unlink (rspamd->cfg->bind_host);
}
free_config (rspamd->cfg);
g_free (rspamd->cfg);
g_free (rspamd);
return (res);
}
/*
* vi:ts=4
*/
|