aboutsummaryrefslogtreecommitdiffstats
path: root/src/libserver/events.c
blob: 4a7d5d6343a963ffaa16454f54b78840197fe3ff (plain)
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
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
/*-
 * 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 "rspamd.h"
#include "contrib/uthash/utlist.h"
#include "events.h"
#include "cryptobox.h"

#define RSPAMD_SESSION_FLAG_WATCHING (1 << 0)
#define RSPAMD_SESSION_FLAG_DESTROYING (1 << 1)

#define RSPAMD_SESSION_IS_WATCHING(s) ((s)->flags & RSPAMD_SESSION_FLAG_WATCHING)
#define RSPAMD_SESSION_IS_DESTROYING(s) ((s)->flags & RSPAMD_SESSION_FLAG_DESTROYING)

#define msg_err_session(...) rspamd_default_log_function(G_LOG_LEVEL_CRITICAL, \
        "events", session->pool->tag.uid, \
        G_STRFUNC, \
        __VA_ARGS__)
#define msg_warn_session(...)   rspamd_default_log_function (G_LOG_LEVEL_WARNING, \
        "events", session->pool->tag.uid, \
        G_STRFUNC, \
        __VA_ARGS__)
#define msg_info_session(...)   rspamd_default_log_function (G_LOG_LEVEL_INFO, \
        "events", session->pool->tag.uid, \
        G_STRFUNC, \
        __VA_ARGS__)
#define msg_debug_session(...)  rspamd_default_log_function (G_LOG_LEVEL_DEBUG, \
        "events", session->pool->tag.uid, \
        G_STRFUNC, \
        __VA_ARGS__)

struct rspamd_watch_stack {
	event_watcher_t cb;
	gpointer ud;
	struct rspamd_watch_stack *next;
};

struct rspamd_async_watcher {
	struct rspamd_watch_stack *st;
	guint remain;
	gint id;
};

struct rspamd_async_event {
	GQuark subsystem;
	event_finalizer_t fin;
	void *user_data;
	struct rspamd_async_watcher *w;
};

struct rspamd_async_session {
	session_finalizer_t fin;
	event_finalizer_t restore;
	event_finalizer_t cleanup;
	GHashTable *events;
	void *user_data;
	rspamd_mempool_t *pool;
	struct rspamd_async_watcher *cur_watcher;
	guint flags;
};

static gboolean
rspamd_event_equal (gconstpointer a, gconstpointer b)
{
	const struct rspamd_async_event *ev1 = a, *ev2 = b;

	if (ev1->fin == ev2->fin) {
		return ev1->user_data == ev2->user_data;
	}

	return FALSE;
}

static guint
rspamd_event_hash (gconstpointer a)
{
	const struct rspamd_async_event *ev = a;
	rspamd_cryptobox_fast_hash_state_t st;
	union {
		event_finalizer_t f;
		gpointer p;
	} u;

	u.f = ev->fin;

	rspamd_cryptobox_fast_hash_init (&st, rspamd_hash_seed ());
	rspamd_cryptobox_fast_hash_update (&st, &ev->user_data, sizeof (gpointer));
	rspamd_cryptobox_fast_hash_update (&st, &u, sizeof (u));

	return rspamd_cryptobox_fast_hash_final (&st);
}


struct rspamd_async_session *
rspamd_session_create (rspamd_mempool_t * pool, session_finalizer_t fin,
	event_finalizer_t restore, event_finalizer_t cleanup, void *user_data)
{
	struct rspamd_async_session *new;

	new = rspamd_mempool_alloc0 (pool, sizeof (struct rspamd_async_session));
	new->pool = pool;
	new->fin = fin;
	new->restore = restore;
	new->cleanup = cleanup;
	new->user_data = user_data;
	new->events = g_hash_table_new (rspamd_event_hash, rspamd_event_equal);

	rspamd_mempool_add_destructor (pool,
		(rspamd_mempool_destruct_t) g_hash_table_destroy,
		new->events);

	return new;
}

void
rspamd_session_add_event (struct rspamd_async_session *session,
	event_finalizer_t fin,
	void *user_data,
	GQuark subsystem)
{
	struct rspamd_async_event *new;

	if (session == NULL) {
		msg_err ("session is NULL");
		return;
	}

	new = rspamd_mempool_alloc (session->pool,
			sizeof (struct rspamd_async_event));
	new->fin = fin;
	new->user_data = user_data;
	new->subsystem = subsystem;

	if (RSPAMD_SESSION_IS_WATCHING (session)) {
		new->w = session->cur_watcher;
		new->w->remain ++;
		msg_debug_session ("added event: %p, pending %d events, "
				"subsystem: %s, watcher: %d",
				user_data,
				g_hash_table_size (session->events),
				g_quark_to_string (subsystem),
				new->w->id);
	}
	else {
		new->w = NULL;
		msg_debug_session ("added event: %p, pending %d events, "
				"subsystem: %s, no watcher!",
				user_data,
				g_hash_table_size (session->events),
				g_quark_to_string (subsystem));
	}

	g_hash_table_insert (session->events, new, new);
}

static inline void
rspamd_session_call_watcher_stack (struct rspamd_async_session *session,
		struct rspamd_async_watcher *w)
{
	struct rspamd_watch_stack *st;

	LL_FOREACH (w->st, st) {
		st->cb (session->user_data, st->ud);
	}

	w->st = NULL;
}

void
rspamd_session_remove_event (struct rspamd_async_session *session,
	event_finalizer_t fin,
	void *ud)
{
	struct rspamd_async_event search_ev, *found_ev;

	if (session == NULL) {
		msg_err ("session is NULL");
		return;
	}

	/* Search for event */
	search_ev.fin = fin;
	search_ev.user_data = ud;
	found_ev = g_hash_table_lookup (session->events, &search_ev);
	g_assert (found_ev != NULL);

	/* Remove event */
	fin (ud);

	/* Call watcher if needed */
	if (found_ev->w) {
		msg_debug_session ("removed event: %p, subsystem: %s, "
				"pending %d events, watcher: %d (%d pending)", ud,
				g_quark_to_string (found_ev->subsystem),
				g_hash_table_size (session->events),
				found_ev->w->id, found_ev->w->remain);

		if (found_ev->w->remain > 0) {
			if (--found_ev->w->remain == 0) {
				rspamd_session_call_watcher_stack (session, found_ev->w);
			}
		}
	}
	else {
		msg_debug_session ("removed event: %p, subsystem: %s, "
				"pending %d events, no watcher!", ud,
				g_quark_to_string (found_ev->subsystem),
				g_hash_table_size (session->events));
	}

	g_hash_table_remove (session->events, found_ev);

	rspamd_session_pending (session);
}

static gboolean
rspamd_session_destroy_callback (gpointer k, gpointer v, gpointer d)
{
	struct rspamd_async_event *ev = v;
	struct rspamd_async_session *session = d;

	/* Call event's finalizer */
	msg_debug_session ("removed event on destroy: %p, subsystem: %s",
			ev->user_data,
			g_quark_to_string (ev->subsystem));

	if (ev->fin != NULL) {
		ev->fin (ev->user_data);
	}

	/* We ignore watchers on session destroying */

	return TRUE;
}

gboolean
rspamd_session_destroy (struct rspamd_async_session *session)
{
	if (session == NULL) {
		msg_err ("session is NULL");
		return FALSE;
	}

	if (!(session->flags & RSPAMD_SESSION_FLAG_DESTROYING)) {
		session->flags |= RSPAMD_SESSION_FLAG_DESTROYING;
		rspamd_session_cleanup (session);

		if (session->cleanup != NULL) {
			session->cleanup (session->user_data);
		}
	}

	return TRUE;
}

void
rspamd_session_cleanup (struct rspamd_async_session *session)
{
	if (session == NULL) {
		msg_err ("session is NULL");
		return;
	}

	g_hash_table_foreach_remove (session->events,
			rspamd_session_destroy_callback,
			session);
}

gboolean
rspamd_session_pending (struct rspamd_async_session *session)
{
	gboolean ret = TRUE;

	if (g_hash_table_size (session->events) == 0) {
		if (session->fin != NULL) {
			msg_debug_session ("call fin handler, as no events are pending");

			if (!session->fin (session->user_data)) {
				/* Session finished incompletely, perform restoration */
				msg_debug_session ("restore incomplete session");
				if (session->restore != NULL) {
					session->restore (session->user_data);
				}
			}
			else {
				ret = FALSE;
			}
		}

		ret = FALSE;
	}

	return ret;
}

void
rspamd_session_watch_start (struct rspamd_async_session *session,
		gint id,
		event_watcher_t cb,
		gpointer ud)
{
	struct rspamd_watch_stack *st_elt;

	g_assert (session != NULL);
	g_assert (!RSPAMD_SESSION_IS_WATCHING (session));

	if (session->cur_watcher == NULL) {
		session->cur_watcher = rspamd_mempool_alloc0 (session->pool,
				sizeof (*session->cur_watcher));
	}

	st_elt = rspamd_mempool_alloc (session->pool, sizeof (*st_elt));
	st_elt->cb = cb;
	st_elt->ud = ud;
	LL_PREPEND (session->cur_watcher->st, st_elt);

	session->cur_watcher->id = id;
	session->flags |= RSPAMD_SESSION_FLAG_WATCHING;
}

guint
rspamd_session_watch_stop (struct rspamd_async_session *session)
{
	guint remain;

	g_assert (session != NULL);
	g_assert (RSPAMD_SESSION_IS_WATCHING (session));

	remain = session->cur_watcher->remain;

	if (remain > 0) {
		/* Avoid reusing */
		session->cur_watcher = NULL;
	}

	session->flags &= ~RSPAMD_SESSION_FLAG_WATCHING;

	return remain;
}


guint
rspamd_session_events_pending (struct rspamd_async_session *session)
{
	guint npending;

	g_assert (session != NULL);

	npending = g_hash_table_size (session->events);
	msg_debug_session ("pending %d events", npending);

	if (RSPAMD_SESSION_IS_WATCHING (session)) {
		npending += session->cur_watcher->remain;
		msg_debug_session ("pending %d watchers, id: %d",
				session->cur_watcher->remain, session->cur_watcher->id);
	}

	return npending;
}

inline void
rspamd_session_watcher_push_callback (struct rspamd_async_session *session,
		struct rspamd_async_watcher *w,
		event_watcher_t cb,
		gpointer ud)
{
	struct rspamd_watch_stack *st;

	g_assert (session != NULL);

	if (w == NULL) {
		if (RSPAMD_SESSION_IS_WATCHING (session)) {
			w = session->cur_watcher;
		}
		else {
			return;
		}
	}

	if (w) {
		w->remain ++;
		msg_debug_session ("push session, watcher: %d, %d events",
				w->id,
				w->remain);

		if (cb) {
			st = rspamd_mempool_alloc (session->pool, sizeof (*st));
			st->cb = cb;
			st->ud = ud;

			LL_PREPEND (w->st, st);
		}
	}
}

void
rspamd_session_watcher_push (struct rspamd_async_session *session)
{
	rspamd_session_watcher_push_callback (session, NULL, NULL, NULL);
}

void
rspamd_session_watcher_push_specific (struct rspamd_async_session *session,
		struct rspamd_async_watcher *w)
{
	rspamd_session_watcher_push_callback (session, w, NULL, NULL);
}

void
rspamd_session_watcher_pop (struct rspamd_async_session *session,
		struct rspamd_async_watcher *w)
{
	g_assert (session != NULL);

	if (w && w->remain > 0) {
		msg_debug_session ("pop session, watcher: %d, %d events", w->id,
				w->remain);
		w->remain --;

		if (w->remain == 0) {
			rspamd_session_call_watcher_stack (session, w);
		}
	}
}

struct rspamd_async_watcher*
rspamd_session_get_watcher (struct rspamd_async_session *session)
{
	g_assert (session != NULL);

	if (RSPAMD_SESSION_IS_WATCHING (session)) {
		return session->cur_watcher;
	}
	else {
		return NULL;
	}
}