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
|
/*-
* Copyright 2016 Vsevolod Stakhov
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "config.h"
#include "libutil/util.h"
#include "libserver/cfg_file.h"
#include "libserver/cfg_rcl.h"
#include "libserver/worker_util.h"
#include "libserver/rspamd_control.h"
#include "libutil/addr.h"
#include "lua/lua_common.h"
#include "unix-std.h"
#include "utlist.h"
#ifdef HAVE_GLOB_H
#include <glob.h>
#endif
static gpointer init_log_helper (struct rspamd_config *cfg);
static void start_log_helper (struct rspamd_worker *worker);
worker_t log_helper_worker = {
"log_helper", /* Name */
init_log_helper, /* Init function */
start_log_helper, /* Start function */
RSPAMD_WORKER_UNIQUE | RSPAMD_WORKER_KILLABLE,
RSPAMD_WORKER_SOCKET_NONE, /* No socket */
RSPAMD_WORKER_VER /* Version info */
};
static const guint64 rspamd_log_helper_magic = 0x1090bb46aaa74c9aULL;
/*
* Worker's context
*/
struct log_helper_ctx {
guint64 magic;
/* Events base */
struct event_base *ev_base;
/* DNS resolver */
struct rspamd_dns_resolver *resolver;
/* Config */
struct rspamd_config *cfg;
/* END OF COMMON PART */
struct event log_ev;
struct rspamd_worker_lua_script *scripts;
lua_State *L;
gint pair[2];
};
static gpointer
init_log_helper (struct rspamd_config *cfg)
{
struct log_helper_ctx *ctx;
GQuark type;
type = g_quark_try_string ("log_helper");
(void)type;
ctx = rspamd_mempool_alloc (cfg->cfg_pool, sizeof (*ctx));
ctx->magic = rspamd_log_helper_magic;
ctx->cfg = cfg;
return ctx;
}
static void
rspamd_log_helper_read (gint fd, short what, gpointer ud)
{
struct log_helper_ctx *ctx = ud;
guchar buf[8192];
gssize r;
guint32 n, i, nextra;
struct rspamd_protocol_log_message_sum *sm;
struct rspamd_worker_lua_script *sc;
struct rspamd_config **pcfg;
struct event_base **pevbase;
r = read (fd, buf, sizeof (buf));
if (r >= (gssize)sizeof (struct rspamd_protocol_log_message_sum)) {
memcpy (&n, buf, sizeof (n));
memcpy (&nextra, buf + sizeof (n), sizeof (nextra));
if (n + nextra !=
(r - sizeof (*sm)) / sizeof (struct rspamd_protocol_log_symbol_result)) {
msg_warn ("cannot read data from log pipe: bad length: %d elements "
"announced but %d available", n + nextra,
(gint)((r - sizeof (*sm)) /
sizeof (struct rspamd_protocol_log_symbol_result)));
}
else {
sm = g_malloc (r);
memcpy (sm, buf, r);
DL_FOREACH (ctx->scripts, sc) {
lua_rawgeti (ctx->L, LUA_REGISTRYINDEX, sc->cbref);
lua_pushnumber (ctx->L, sm->score);
lua_pushnumber (ctx->L, sm->required_score);
lua_createtable (ctx->L, n, 0);
for (i = 0; i < n; i ++) {
lua_createtable (ctx->L, 2, 0);
lua_pushnumber (ctx->L, sm->results[i].id);
lua_rawseti (ctx->L, -2, 1);
lua_pushnumber (ctx->L, sm->results[i].score);
lua_rawseti (ctx->L, -2, 2);
lua_rawseti (ctx->L, -2, (i + 1));
}
pcfg = lua_newuserdata (ctx->L, sizeof (*pcfg));
*pcfg = ctx->cfg;
rspamd_lua_setclass (ctx->L, "rspamd{config}", -1);
lua_pushnumber (ctx->L, sm->settings_id);
lua_createtable (ctx->L, nextra, 0);
for (i = 0; i < nextra; i ++) {
lua_createtable (ctx->L, 2, 0);
lua_pushnumber (ctx->L, sm->results[i + n].id);
lua_rawseti (ctx->L, -2, 1);
lua_pushnumber (ctx->L, sm->results[i + n].score);
lua_rawseti (ctx->L, -2, 2);
lua_rawseti (ctx->L, -2, (i + 1));
}
pevbase = lua_newuserdata (ctx->L, sizeof (*pevbase));
*pevbase = ctx->ev_base;
rspamd_lua_setclass (ctx->L, "rspamd{ev_base}", -1);
if (lua_pcall (ctx->L, 7, 0, 0) != 0) {
msg_err ("error executing log handler code: %s",
lua_tostring (ctx->L, -1));
lua_pop (ctx->L, 1);
}
}
g_free (sm);
}
}
else if (r == -1) {
if (errno != EAGAIN && errno != EINTR) {
msg_warn ("cannot read data from log pipe: %s", strerror (errno));
event_del (&ctx->log_ev);
}
}
else if (r == 0) {
msg_warn ("cannot read data from log pipe: EOF");
event_del (&ctx->log_ev);
}
}
static void
rspamd_log_helper_reply_handler (struct rspamd_worker *worker,
struct rspamd_srv_reply *rep, gint rep_fd,
gpointer ud)
{
struct log_helper_ctx *ctx = ud;
close (ctx->pair[1]);
msg_info ("start waiting for log events");
event_set (&ctx->log_ev, ctx->pair[0], EV_READ | EV_PERSIST,
rspamd_log_helper_read, ctx);
event_base_set (ctx->ev_base, &ctx->log_ev);
event_add (&ctx->log_ev, NULL);
}
static void
start_log_helper (struct rspamd_worker *worker)
{
struct log_helper_ctx *ctx = worker->ctx;
gssize r = -1;
gint nscripts = 0;
struct rspamd_worker_lua_script *tmp;
static struct rspamd_srv_command srv_cmd;
ctx->ev_base = rspamd_prepare_worker (worker,
"log_helper",
NULL);
ctx->cfg = worker->srv->cfg;
ctx->scripts = worker->cf->scripts;
ctx->L = ctx->cfg->lua_state;
ctx->resolver = dns_resolver_init (worker->srv->logger,
ctx->ev_base,
worker->srv->cfg);
rspamd_upstreams_library_config (worker->srv->cfg, ctx->cfg->ups_ctx,
ctx->ev_base, ctx->resolver->r);
DL_COUNT (worker->cf->scripts, tmp, nscripts);
msg_info ("started log_helper worker with %d scripts", nscripts);
r = rspamd_socketpair (ctx->pair, FALSE);
if (r == -1) {
msg_err ("cannot create socketpair: %s, exiting now", strerror (errno));
/* Prevent new processes spawning */
exit (EXIT_SUCCESS);
}
memset (&srv_cmd, 0, sizeof (srv_cmd));
srv_cmd.type = RSPAMD_SRV_LOG_PIPE;
srv_cmd.cmd.log_pipe.type = RSPAMD_LOG_PIPE_SYMBOLS;
/* Wait for startup being completed */
rspamd_mempool_lock_mutex (worker->srv->start_mtx);
rspamd_srv_send_command (worker, ctx->ev_base, &srv_cmd, ctx->pair[1],
rspamd_log_helper_reply_handler, ctx);
rspamd_mempool_unlock_mutex (worker->srv->start_mtx);
rspamd_lua_run_postloads (ctx->cfg->lua_state, ctx->cfg, ctx->ev_base,
worker);
event_base_loop (ctx->ev_base, 0);
close (ctx->pair[0]);
rspamd_worker_block_signals ();
rspamd_log_close (worker->srv->logger);
REF_RELEASE (ctx->cfg);
exit (EXIT_SUCCESS);
}
|