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
|
#include <sys/stat.h>
#include <sys/param.h>
#include <sys/types.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <errno.h>
#include <signal.h>
#include <netinet/in.h>
#include <syslog.h>
#include <fcntl.h>
#include <netdb.h>
#include <EXTERN.h> /* from the Perl distribution */
#include <perl.h> /* from the Perl distribution */
#include <glib.h>
#include <event.h>
#include <gmime/gmime.h>
#include "util.h"
#include "main.h"
#include "protocol.h"
#include "upstream.h"
#include "cfg_file.h"
#include "url.h"
#include "modules.h"
#include "message.h"
#define TASK_POOL_SIZE 4095
const f_str_t CRLF = {
/* begin */"\r\n",
/* len */2,
/* size */2
};
extern PerlInterpreter *perl_interpreter;
static
void sig_handler (int signo)
{
switch (signo) {
case SIGINT:
case SIGTERM:
_exit (1);
break;
}
}
static void
sigusr_handler (int fd, short what, void *arg)
{
struct rspamd_worker *worker = (struct rspamd_worker *)arg;
/* Do not accept new connections, preparing to end worker's process */
struct timeval tv;
tv.tv_sec = SOFT_SHUTDOWN_TIME;
tv.tv_usec = 0;
event_del (&worker->sig_ev);
event_del (&worker->bind_ev);
do_reopen_log = 1;
msg_info ("worker's shutdown is pending in %d sec", SOFT_SHUTDOWN_TIME);
event_loopexit (&tv);
return;
}
static void
rcpt_destruct (void *pointer)
{
struct worker_task *task = (struct worker_task *)pointer;
if (task->rcpt) {
g_list_free (task->rcpt);
}
}
static void
free_task (struct worker_task *task)
{
GList *part;
struct mime_part *p;
if (task) {
if (task->memc_ctx) {
memc_close_ctx (task->memc_ctx);
}
while ((part = g_list_first (task->parts))) {
task->parts = g_list_remove_link (task->parts, part);
p = (struct mime_part *)part->data;
g_byte_array_free (p->content, FALSE);
g_list_free_1 (part);
}
memory_pool_delete (task->task_pool);
bufferevent_disable (task->bev, EV_READ | EV_WRITE);
bufferevent_free (task->bev);
close (task->sock);
g_free (task);
}
}
static void
read_socket (struct bufferevent *bev, void *arg)
{
struct worker_task *task = (struct worker_task *)arg;
ssize_t r;
char *s;
switch (task->state) {
case READ_COMMAND:
case READ_HEADER:
s = evbuffer_readline (EVBUFFER_INPUT (bev));
if (read_rspamd_input_line (task, s) != 0) {
task->last_error = "Read error";
task->error_code = RSPAMD_NETWORK_ERROR;
task->state = WRITE_ERROR;
}
if (task->state == WRITE_ERROR || task->state == WRITE_REPLY) {
bufferevent_enable (bev, EV_WRITE);
bufferevent_disable (bev, EV_READ);
}
free (s);
break;
case READ_MESSAGE:
r = bufferevent_read (bev, task->msg->pos, task->msg->free);
if (r > 0) {
task->msg->pos += r;
msg_debug ("read_socket: read %zd bytes from socket, %zd bytes left", r, task->msg->free);
update_buf_size (task->msg);
if (task->msg->free == 0) {
r = process_message (task);
r = process_filters (task);
if (r == -1) {
task->last_error = "Filter processing error";
task->error_code = RSPAMD_FILTER_ERROR;
task->state = WRITE_ERROR;
}
else if (r == 0) {
task->state = WAIT_FILTER;
}
else {
process_statfiles (task);
}
}
if (task->state == WRITE_ERROR || task->state == WRITE_REPLY) {
bufferevent_enable (bev, EV_WRITE);
bufferevent_disable (bev, EV_READ);
evbuffer_drain (bev->output, EVBUFFER_LENGTH (bev->output));
}
}
else {
msg_warn ("read_socket: cannot read data to buffer (free space: %zd): %ld", task->msg->free, (long int)r);
bufferevent_disable (bev, EV_READ);
free_task (task);
}
break;
case WAIT_FILTER:
bufferevent_disable (bev, EV_READ);
break;
}
}
static void
write_socket (struct bufferevent *bev, void *arg)
{
struct worker_task *task = (struct worker_task *)arg;
switch (task->state) {
case WRITE_REPLY:
write_reply (task);
task->state = CLOSING_CONNECTION;
bufferevent_disable (bev, EV_READ);
break;
case WRITE_ERROR:
write_reply (task);
task->state = CLOSING_CONNECTION;
bufferevent_disable (bev, EV_READ);
break;
case CLOSING_CONNECTION:
msg_debug ("write_socket: normally closing connection");
free_task (task);
break;
default:
msg_info ("write_socket: abnormally closing connection");
free_task (task);
break;
}
}
static void
err_socket (struct bufferevent *bev, short what, void *arg)
{
struct worker_task *task = (struct worker_task *)arg;
msg_info ("err_socket: abnormally closing connection");
/* Free buffers */
free_task (task);
}
static void
accept_socket (int fd, short what, void *arg)
{
struct rspamd_worker *worker = (struct rspamd_worker *)arg;
struct sockaddr_storage ss;
struct worker_task *new_task;
socklen_t addrlen = sizeof(ss);
int nfd;
if ((nfd = accept (fd, (struct sockaddr *)&ss, &addrlen)) == -1) {
return;
}
if (event_make_socket_nonblocking(fd) < 0) {
return;
}
new_task = g_malloc (sizeof (struct worker_task));
if (new_task == NULL) {
msg_err ("accept_socket: cannot allocate memory for task, %m");
return;
}
bzero (new_task, sizeof (struct worker_task));
new_task->worker = worker;
new_task->state = READ_COMMAND;
new_task->sock = nfd;
new_task->cfg = worker->srv->cfg;
TAILQ_INIT (&new_task->urls);
new_task->task_pool = memory_pool_new (memory_pool_get_size ());
/* Add destructor for recipients list (it would be better to use anonymous function here */
memory_pool_add_destructor (new_task->task_pool, (pool_destruct_func)rcpt_destruct, new_task);
worker->srv->stat->connections_count ++;
/* Read event */
new_task->bev = bufferevent_new (nfd, read_socket, write_socket, err_socket, (void *)new_task);
bufferevent_enable (new_task->bev, EV_READ);
}
void
start_worker (struct rspamd_worker *worker, int listen_sock)
{
struct sigaction signals;
int i;
worker->srv->pid = getpid ();
worker->srv->type = TYPE_WORKER;
event_init ();
g_mime_init (0);
init_signals (&signals, sig_handler);
sigprocmask (SIG_UNBLOCK, &signals.sa_mask, NULL);
/* SIGUSR2 handler */
signal_set (&worker->sig_ev, SIGUSR2, sigusr_handler, (void *) worker);
signal_add (&worker->sig_ev, NULL);
/* Accept event */
event_set(&worker->bind_ev, listen_sock, EV_READ | EV_PERSIST, accept_socket, (void *)worker);
event_add(&worker->bind_ev, NULL);
/* Perform modules configuring */
for (i = 0; i < MODULES_NUM; i ++) {
modules[i].module_config_func (worker->srv->cfg);
}
/* Send SIGUSR2 to parent */
kill (getppid (), SIGUSR2);
event_loop (0);
}
/*
* vi:ts=4
*/
|