diff options
author | Vsevolod Stakhov <vsevolod@highsecure.ru> | 2014-07-23 12:45:28 +0100 |
---|---|---|
committer | Vsevolod Stakhov <vsevolod@highsecure.ru> | 2014-07-23 12:45:28 +0100 |
commit | e0483657ff6cf1adc828ccce457814d61fe90a0d (patch) | |
tree | 5183e4163f40b81b3e7d5f51488d360883782154 /src/libutil | |
parent | 7962087e808fb824aa3af6d41d02abc92916ba1e (diff) | |
download | rspamd-e0483657ff6cf1adc828ccce457814d61fe90a0d.tar.gz rspamd-e0483657ff6cf1adc828ccce457814d61fe90a0d.zip |
Unify code style.
Diffstat (limited to 'src/libutil')
32 files changed, 2616 insertions, 1675 deletions
diff --git a/src/libutil/aio_event.c b/src/libutil/aio_event.c index ccda37083..e2edb8a8c 100644 --- a/src/libutil/aio_event.c +++ b/src/libutil/aio_event.c @@ -21,8 +21,8 @@ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ -#include "config.h" #include "aio_event.h" +#include "config.h" #include "main.h" #ifdef HAVE_SYS_EVENTFD_H @@ -47,7 +47,8 @@ # define SYS_io_submit 209 # define SYS_io_cancel 210 #else -# warning "aio is not supported on this platform, please contact author for details" +# warning \ + "aio is not supported on this platform, please contact author for details" # define SYS_io_setup 0 # define SYS_io_destroy 0 # define SYS_io_getevents 0 @@ -101,7 +102,7 @@ typedef enum io_iocb_cmd { struct iocb { /* these are internal to the kernel/libc. */ guint64 aio_data; /* data to be returned in event's data */ - guint32 PADDED(aio_key, aio_reserved1); + guint32 PADDED (aio_key, aio_reserved1); /* the kernel sets aio_key to the req # */ /* common fields */ @@ -127,27 +128,31 @@ struct iocb { }; struct io_event { - guint64 data; /* the data field from the iocb */ - guint64 obj; /* what iocb this event came from */ - gint64 res; /* result code for this event */ - gint64 res2; /* secondary result */ + guint64 data; /* the data field from the iocb */ + guint64 obj; /* what iocb this event came from */ + gint64 res; /* result code for this event */ + gint64 res2; /* secondary result */ }; /* Linux specific io calls */ static int io_setup (guint nr_reqs, aio_context_t *ctx) { - return syscall (SYS_io_setup, nr_reqs, ctx); + return syscall (SYS_io_setup, nr_reqs, ctx); } static int io_destroy (aio_context_t ctx) { - return syscall (SYS_io_destroy, ctx); + return syscall (SYS_io_destroy, ctx); } static int -io_getevents (aio_context_t ctx, long min_nr, long nr, struct io_event *events, struct timespec *tmo) +io_getevents (aio_context_t ctx, + long min_nr, + long nr, + struct io_event *events, + struct timespec *tmo) { return syscall (SYS_io_getevents, ctx, min_nr, nr, events, tmo); } @@ -155,13 +160,13 @@ io_getevents (aio_context_t ctx, long min_nr, long nr, struct io_event *events, static int io_submit (aio_context_t ctx, long n, struct iocb **paiocb) { - return syscall (SYS_io_submit, ctx, n, paiocb); + return syscall (SYS_io_submit, ctx, n, paiocb); } static int io_cancel (aio_context_t ctx, struct iocb *iocb, struct io_event *result) { - return syscall (SYS_io_cancel, ctx, iocb, result); + return syscall (SYS_io_cancel, ctx, iocb, result); } # ifndef HAVE_SYS_EVENTFD_H @@ -179,7 +184,7 @@ eventfd (guint initval, guint flags) */ struct aio_context { struct event_base *base; - gboolean has_aio; /**< Whether we have aio support on a system */ + gboolean has_aio; /**< Whether we have aio support on a system */ #ifdef LINUX /* Eventfd variant */ gint event_fd; @@ -196,12 +201,12 @@ struct aio_context { static void rspamd_eventfdcb (gint fd, gshort what, gpointer ud) { - struct aio_context *ctx = ud; - guint64 ready; - gint done, i; - struct io_event event[32]; - struct timespec ts; - struct io_cbdata *ev_data; + struct aio_context *ctx = ud; + guint64 ready; + gint done, i; + struct io_event event[32]; + struct timespec ts; + struct io_cbdata *ev_data; /* Eventfd returns number of events ready got from kernel */ if (read (fd, &ready, 8) != 8) { @@ -221,10 +226,14 @@ rspamd_eventfdcb (gint fd, gshort what, gpointer ud) if (done > 0) { ready -= done; - for (i = 0; i < done; i ++) { + for (i = 0; i < done; i++) { ev_data = (struct io_cbdata *) (uintptr_t) event[i].data; /* Call this callback */ - ev_data->cb (ev_data->fd, event[i].res, ev_data->len, ev_data->buf, ev_data->ud); + ev_data->cb (ev_data->fd, + event[i].res, + ev_data->len, + ev_data->buf, + ev_data->ud); if (ev_data->io_buf) { free (ev_data->io_buf); } @@ -247,10 +256,10 @@ rspamd_eventfdcb (gint fd, gshort what, gpointer ud) /** * Initialize aio with specified event base */ -struct aio_context* +struct aio_context * rspamd_aio_init (struct event_base *base) { - struct aio_context *new; + struct aio_context *new; /* First of all we need to detect which type of aio we can try to use */ new = g_malloc0 (sizeof (struct aio_context)); @@ -269,7 +278,11 @@ rspamd_aio_init (struct event_base *base) close (new->event_fd); } else { - event_set (&new->eventfd_ev, new->event_fd, EV_READ|EV_PERSIST, rspamd_eventfdcb, new); + event_set (&new->eventfd_ev, + new->event_fd, + EV_READ | EV_PERSIST, + rspamd_eventfdcb, + new); event_base_set (new->base, &new->eventfd_ev); event_add (&new->eventfd_ev, NULL); if (io_setup (MAX_AIO_EV, &new->io_ctx) == -1) { @@ -294,7 +307,7 @@ rspamd_aio_init (struct event_base *base) gint rspamd_aio_open (struct aio_context *ctx, const gchar *path, int flags) { - gint fd = -1; + gint fd = -1; /* Fallback */ if (!ctx->has_aio) { return open (path, flags); @@ -315,14 +328,20 @@ rspamd_aio_open (struct aio_context *ctx, const gchar *path, int flags) * Asynchronous read of file */ gint -rspamd_aio_read (gint fd, gpointer buf, guint64 len, guint64 offset, struct aio_context *ctx, rspamd_aio_cb cb, gpointer ud) +rspamd_aio_read (gint fd, + gpointer buf, + guint64 len, + guint64 offset, + struct aio_context *ctx, + rspamd_aio_cb cb, + gpointer ud) { - struct io_cbdata *cbdata; - gint r = -1; + struct io_cbdata *cbdata; + gint r = -1; if (ctx->has_aio) { #ifdef LINUX - struct iocb *iocb[1]; + struct iocb *iocb[1]; cbdata = g_slice_alloc (sizeof (struct io_cbdata)); cbdata->cb = cb; @@ -385,14 +404,20 @@ blocking: * Asynchronous write of file */ gint -rspamd_aio_write (gint fd, gpointer buf, guint64 len, guint64 offset, struct aio_context *ctx, rspamd_aio_cb cb, gpointer ud) +rspamd_aio_write (gint fd, + gpointer buf, + guint64 len, + guint64 offset, + struct aio_context *ctx, + rspamd_aio_cb cb, + gpointer ud) { - struct io_cbdata *cbdata; - gint r = -1; + struct io_cbdata *cbdata; + gint r = -1; if (ctx->has_aio) { #ifdef LINUX - struct iocb *iocb[1]; + struct iocb *iocb[1]; cbdata = g_slice_alloc (sizeof (struct io_cbdata)); cbdata->cb = cb; @@ -461,12 +486,12 @@ blocking: gint rspamd_aio_close (gint fd, struct aio_context *ctx) { - gint r = -1; + gint r = -1; if (ctx->has_aio) { #ifdef LINUX - struct iocb iocb; - struct io_event ev; + struct iocb iocb; + struct io_event ev; memset (&iocb, 0, sizeof (struct iocb)); iocb.aio_fildes = fd; diff --git a/src/libutil/aio_event.h b/src/libutil/aio_event.h index 45f6015de..7eaf95c2b 100644 --- a/src/libutil/aio_event.h +++ b/src/libutil/aio_event.h @@ -35,12 +35,13 @@ struct aio_context; /** * Callback for notifying */ -typedef void (*rspamd_aio_cb) (gint fd, gint res, guint64 len, gpointer data, gpointer ud); +typedef void (*rspamd_aio_cb) (gint fd, gint res, guint64 len, gpointer data, + gpointer ud); /** * Initialize aio with specified event base */ -struct aio_context* rspamd_aio_init (struct event_base *base); +struct aio_context * rspamd_aio_init (struct event_base *base); /** * Open file for aio @@ -51,13 +52,13 @@ gint rspamd_aio_open (struct aio_context *ctx, const gchar *path, int flags); * Asynchronous read of file */ gint rspamd_aio_read (gint fd, gpointer buf, guint64 len, guint64 offset, - struct aio_context *ctx, rspamd_aio_cb cb, gpointer ud); + struct aio_context *ctx, rspamd_aio_cb cb, gpointer ud); /** * Asynchronous write of file */ gint rspamd_aio_write (gint fd, gpointer buf, guint64 len, guint64 offset, - struct aio_context *ctx, rspamd_aio_cb cb, gpointer ud); + struct aio_context *ctx, rspamd_aio_cb cb, gpointer ud); /** * Close of aio operations diff --git a/src/libutil/bloom.c b/src/libutil/bloom.c index f857d2e49..a77346d5c 100644 --- a/src/libutil/bloom.c +++ b/src/libutil/bloom.c @@ -22,48 +22,59 @@ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ -#include "config.h" #include "bloom.h" +#include "config.h" #include "xxhash.h" /* 4 bits are used for counting (implementing delete operation) */ #define SIZE_BIT 4 /* These macroes are for 4 bits for counting element */ -#define INCBIT(a, n, acc) do { \ - acc = a[n * SIZE_BIT / CHAR_BIT] & (0xF << (n % (CHAR_BIT / SIZE_BIT) * SIZE_BIT)); \ - acc ++; \ - acc &= 0xF; \ - \ - a[n * SIZE_BIT / CHAR_BIT] &= (0xF << (4 - (n % (CHAR_BIT/SIZE_BIT) * SIZE_BIT))); \ - a[n * SIZE_BIT / CHAR_BIT] |= (acc << (n % (CHAR_BIT/SIZE_BIT) * SIZE_BIT)); \ +#define INCBIT(a, n, acc) do { \ + acc = \ + a[n * SIZE_BIT / CHAR_BIT] & (0xF << \ + (n % (CHAR_BIT / SIZE_BIT) * SIZE_BIT)); \ + acc ++; \ + acc &= 0xF; \ + \ + a[n * SIZE_BIT / \ + CHAR_BIT] &= (0xF << (4 - (n % (CHAR_BIT / SIZE_BIT) * SIZE_BIT))); \ + a[n * SIZE_BIT / \ + CHAR_BIT] |= (acc << (n % (CHAR_BIT / SIZE_BIT) * SIZE_BIT)); \ } while (0); -#define DECBIT(a, n, acc) do { \ - acc = a[n * SIZE_BIT / CHAR_BIT] & (0xF << (n % (CHAR_BIT / SIZE_BIT) * SIZE_BIT)); \ - acc --; \ - acc &= 0xF; \ - \ - a[n * SIZE_BIT / CHAR_BIT] &= (0xF << (4 - (n % (CHAR_BIT/SIZE_BIT) * SIZE_BIT))); \ - a[n * SIZE_BIT / CHAR_BIT] |= (acc << (n % (CHAR_BIT/SIZE_BIT) * SIZE_BIT)); \ +#define DECBIT(a, n, acc) do { \ + acc = \ + a[n * SIZE_BIT / CHAR_BIT] & (0xF << \ + (n % (CHAR_BIT / SIZE_BIT) * SIZE_BIT)); \ + acc --; \ + acc &= 0xF; \ + \ + a[n * SIZE_BIT / \ + CHAR_BIT] &= (0xF << (4 - (n % (CHAR_BIT / SIZE_BIT) * SIZE_BIT))); \ + a[n * SIZE_BIT / \ + CHAR_BIT] |= (acc << (n % (CHAR_BIT / SIZE_BIT) * SIZE_BIT)); \ } while (0); -#define GETBIT(a, n) (a[n * SIZE_BIT / CHAR_BIT] & (0xF << (n % (CHAR_BIT/SIZE_BIT) * SIZE_BIT))) +#define GETBIT(a, \ + n) (a[n * SIZE_BIT / CHAR_BIT] & (0xF << \ + (n % (CHAR_BIT / SIZE_BIT) * SIZE_BIT))) /* Common hash functions */ -rspamd_bloom_filter_t * +rspamd_bloom_filter_t * rspamd_bloom_create (size_t size, size_t nfuncs, ...) { - rspamd_bloom_filter_t *bloom; - va_list l; - gsize n; + rspamd_bloom_filter_t *bloom; + va_list l; + gsize n; if (!(bloom = g_malloc (sizeof (rspamd_bloom_filter_t)))) { return NULL; } - if (!(bloom->a = g_new0 (gchar, (size + CHAR_BIT - 1) / CHAR_BIT * SIZE_BIT))) { + if (!(bloom->a = + g_new0 (gchar, (size + CHAR_BIT - 1) / CHAR_BIT * SIZE_BIT))) { g_free (bloom); return NULL; } @@ -96,9 +107,9 @@ rspamd_bloom_destroy (rspamd_bloom_filter_t * bloom) gboolean rspamd_bloom_add (rspamd_bloom_filter_t * bloom, const gchar *s) { - size_t n, len; - u_char t; - guint v; + size_t n, len; + u_char t; + guint v; if (s == NULL) { return FALSE; @@ -115,9 +126,9 @@ rspamd_bloom_add (rspamd_bloom_filter_t * bloom, const gchar *s) gboolean rspamd_bloom_del (rspamd_bloom_filter_t * bloom, const gchar *s) { - size_t n, len; - u_char t; - guint v; + size_t n, len; + u_char t; + guint v; if (s == NULL) { return FALSE; @@ -135,8 +146,8 @@ rspamd_bloom_del (rspamd_bloom_filter_t * bloom, const gchar *s) gboolean rspamd_bloom_check (rspamd_bloom_filter_t * bloom, const gchar *s) { - size_t n, len; - guint v; + size_t n, len; + guint v; if (s == NULL) { return FALSE; diff --git a/src/libutil/bloom.h b/src/libutil/bloom.h index 380143c80..9866e9988 100644 --- a/src/libutil/bloom.h +++ b/src/libutil/bloom.h @@ -4,10 +4,10 @@ #include "config.h" typedef struct rspamd_bloom_filter_s { - size_t asize; - gchar *a; - size_t nfuncs; - guint32 *seeds; + size_t asize; + gchar *a; + size_t nfuncs; + guint32 *seeds; } rspamd_bloom_filter_t; @@ -23,7 +23,7 @@ typedef struct rspamd_bloom_filter_s { * @param nfuncs number of hash functions * @param ... hash functions list */ -rspamd_bloom_filter_t* rspamd_bloom_create (size_t size, size_t nfuncs, ...); +rspamd_bloom_filter_t * rspamd_bloom_create (size_t size, size_t nfuncs, ...); /* * Destroy bloom filter diff --git a/src/libutil/diff.c b/src/libutil/diff.c index 4038d8680..cee175ceb 100644 --- a/src/libutil/diff.c +++ b/src/libutil/diff.c @@ -39,8 +39,8 @@ #include "diff.h" -#define FV(k) _v(ctx, (k), 0) -#define RV(k) _v(ctx, (k), 1) +#define FV(k) _v (ctx, (k), 0) +#define RV(k) _v (ctx, (k), 1) #define MAX_DIFF 1024 @@ -58,7 +58,8 @@ struct middle_snake }; static -void maybe_resize_array(GArray *arr, guint k) +void +maybe_resize_array (GArray *arr, guint k) { if (k > arr->len) { g_array_set_size (arr, k); @@ -67,7 +68,7 @@ void maybe_resize_array(GArray *arr, guint k) } static void -_setv(struct _ctx *ctx, gint k, gint r, gint val) +_setv (struct _ctx *ctx, gint k, gint r, gint val) { gint j; gint *i; @@ -81,7 +82,7 @@ _setv(struct _ctx *ctx, gint k, gint r, gint val) } static gint -_v(struct _ctx *ctx, gint k, gint r) +_v (struct _ctx *ctx, gint k, gint r) { gint j; @@ -91,8 +92,8 @@ _v(struct _ctx *ctx, gint k, gint r) } static gint -_find_middle_snake(const void *a, gint aoff, gint n, const void *b, - gint boff, gint m, struct _ctx *ctx, struct middle_snake *ms) +_find_middle_snake (const void *a, gint aoff, gint n, const void *b, + gint boff, gint m, struct _ctx *ctx, struct middle_snake *ms) { gint delta, odd, mid, d; @@ -101,7 +102,7 @@ _find_middle_snake(const void *a, gint aoff, gint n, const void *b, mid = (n + m) / 2; mid += odd; - _setv (ctx, 1, 0, 0); + _setv (ctx, 1, 0, 0); _setv (ctx, delta - 1, 1, n); for (d = 0; d <= mid; d++) { @@ -112,11 +113,11 @@ _find_middle_snake(const void *a, gint aoff, gint n, const void *b, } for (k = d; k >= -d; k -= 2) { - if (k == -d || (k != d && FV(k - 1) < FV(k + 1))) { - x = FV(k + 1); + if (k == -d || (k != d && FV (k - 1) < FV (k + 1))) { + x = FV (k + 1); } else { - x = FV(k - 1) + 1; + x = FV (k - 1) + 1; } y = x - k; @@ -131,7 +132,7 @@ _find_middle_snake(const void *a, gint aoff, gint n, const void *b, _setv (ctx, k, 0, x); if (odd && k >= (delta - (d - 1)) && k <= (delta + (d - 1))) { - if (x >= RV(k)) { + if (x >= RV (k)) { ms->u = x; ms->v = y; return 2 * d - 1; @@ -141,11 +142,11 @@ _find_middle_snake(const void *a, gint aoff, gint n, const void *b, for (k = d; k >= -d; k -= 2) { gint kr = (n - m) + k; - if (k == d || (k != -d && RV(kr - 1) < RV(kr + 1))) { - x = RV(kr - 1); + if (k == d || (k != -d && RV (kr - 1) < RV (kr + 1))) { + x = RV (kr - 1); } else { - x = RV(kr + 1) - 1; + x = RV (kr + 1) - 1; } y = x - kr; @@ -160,7 +161,7 @@ _find_middle_snake(const void *a, gint aoff, gint n, const void *b, _setv (ctx, kr, 1, x); if (!odd && kr >= -d && kr <= d) { - if (x <= FV(kr)) { + if (x <= FV (kr)) { ms->x = x; ms->y = y; return 2 * d; @@ -175,7 +176,7 @@ _find_middle_snake(const void *a, gint aoff, gint n, const void *b, } static void -_edit(struct _ctx *ctx, gint op, gint off, gint len) +_edit (struct _ctx *ctx, gint op, gint off, gint len) { struct diff_edit *e = NULL, newe; @@ -201,8 +202,8 @@ _edit(struct _ctx *ctx, gint op, gint off, gint len) } static gint -_ses(const void *a, gint aoff, gint n, const void *b, gint boff, - gint m, struct _ctx *ctx) +_ses (const void *a, gint aoff, gint n, const void *b, gint boff, + gint m, struct _ctx *ctx) { struct middle_snake ms = { .x = 0, @@ -269,22 +270,22 @@ _ses(const void *a, gint aoff, gint n, const void *b, gint boff, if (m > n) { if (x == u) { - _edit (ctx, DIFF_MATCH, aoff, n); + _edit (ctx, DIFF_MATCH, aoff, n); _edit (ctx, DIFF_INSERT, boff + (m - 1), 1); } else { _edit (ctx, DIFF_INSERT, boff, 1); - _edit (ctx, DIFF_MATCH, aoff, n); + _edit (ctx, DIFF_MATCH, aoff, n); } } else { if (x == u) { - _edit (ctx, DIFF_MATCH, aoff, m); + _edit (ctx, DIFF_MATCH, aoff, m); _edit (ctx, DIFF_DELETE, aoff + (n - 1), 1); } else { - _edit (ctx, DIFF_DELETE, aoff, 1); - _edit (ctx, DIFF_MATCH, aoff + 1, m); + _edit (ctx, DIFF_DELETE, aoff, 1); + _edit (ctx, DIFF_MATCH, aoff + 1, m); } } } @@ -294,8 +295,8 @@ _ses(const void *a, gint aoff, gint n, const void *b, gint boff, } gint -rspamd_diff(const void *a, gint aoff, gint n, const void *b, gint boff, gint m, - gint dmax, GArray *ses, gint *sn) +rspamd_diff (const void *a, gint aoff, gint n, const void *b, gint boff, gint m, + gint dmax, GArray *ses, gint *sn) { struct _ctx ctx; gint d, x, y; @@ -345,14 +346,14 @@ compare_diff_distance_unnormalized (f_str_t *s1, f_str_t *s2) ses = g_array_sized_new (FALSE, TRUE, sizeof (struct diff_edit), MAX_DIFF); if (rspamd_diff (s1->begin, 0, s1->len, - s2->begin, 0, s2->len, MAX_DIFF, ses, NULL) == -1) { + s2->begin, 0, s2->len, MAX_DIFF, ses, NULL) == -1) { /* Diff failed, strings are different */ g_array_free (ses, TRUE); return 0; } - for (i = 0; i < ses->len; i ++) { - e = &g_array_index(ses, struct diff_edit, i); + for (i = 0; i < ses->len; i++) { + e = &g_array_index (ses, struct diff_edit, i); if (e->op != DIFF_MATCH) { distance += e->len; } @@ -367,7 +368,10 @@ guint32 compare_diff_distance (f_str_t *s1, f_str_t *s2) { - return 100 - (2 * compare_diff_distance_unnormalized (s1, s2) * 100) / (s1->len + s2->len); + return 100 - + (2 * + compare_diff_distance_unnormalized (s1, + s2) * 100) / (s1->len + s2->len); } @@ -394,9 +398,9 @@ compare_diff_distance_normalized (f_str_t *s1, f_str_t *s2) if (!g_ascii_isspace (*h)) { *t++ = g_ascii_tolower (*h); } - h ++; - p1 ++; - r1 --; + h++; + p1++; + r1--; } t1.begin = b1; @@ -409,9 +413,9 @@ compare_diff_distance_normalized (f_str_t *s1, f_str_t *s2) if (!g_ascii_isspace (*h)) { *t++ = g_ascii_tolower (*h); } - h ++; - p2 ++; - r2 --; + h++; + p2++; + r2--; } t2.begin = b2; @@ -424,20 +428,20 @@ compare_diff_distance_normalized (f_str_t *s1, f_str_t *s2) h = p1; while (r1 > 0) { if (!g_ascii_isspace (*h)) { - cur_diff ++; + cur_diff++; } - r1 --; - h ++; + r1--; + h++; } } else if (r2 > 0) { h = p2; while (r2 > 0) { if (!g_ascii_isspace (*h)) { - cur_diff ++; + cur_diff++; } - r2 --; - h ++; + r2--; + h++; } } diff --git a/src/libutil/diff.h b/src/libutil/diff.h index cea5e5d4a..7caa35dd8 100644 --- a/src/libutil/diff.h +++ b/src/libutil/diff.h @@ -55,8 +55,15 @@ struct diff_edit * @param sn here would be stored a number of differences between a and b * @return distance between strings or -1 in case of error */ -gint rspamd_diff(const void *a, gint aoff, gint n, const void *b, gint boff, gint m, - gint dmax, GArray *ses, gint *sn); +gint rspamd_diff (const void *a, + gint aoff, + gint n, + const void *b, + gint boff, + gint m, + gint dmax, + GArray *ses, + gint *sn); /* * Calculate distance between two strings (in percentage) using diff algorithm. @@ -68,7 +75,7 @@ guint32 compare_diff_distance (f_str_t *s1, f_str_t *s2); * Calculate distance between two strings (in percentage) using diff algorithm. Strings are normalized before: * all spaces are removed and all characters are lowercased. * @return 100 in case of identical strings and 0 in case of totally different strings. -*/ + */ guint32 compare_diff_distance_normalized (f_str_t *s1, f_str_t *s2); #endif /* DIFF_H_ */ diff --git a/src/libutil/fstring.c b/src/libutil/fstring.c index 098824101..a83bbd662 100644 --- a/src/libutil/fstring.c +++ b/src/libutil/fstring.c @@ -30,7 +30,7 @@ ssize_t fstrchr (f_str_t * src, gchar c) { - register size_t cur = 0; + register size_t cur = 0; while (cur < src->len) { if (*(src->begin + cur) == c) { @@ -48,7 +48,7 @@ fstrchr (f_str_t * src, gchar c) ssize_t fstrrchr (f_str_t * src, gchar c) { - register ssize_t cur = src->len; + register ssize_t cur = src->len; while (cur > 0) { if (*(src->begin + cur) == c) { @@ -66,7 +66,7 @@ fstrrchr (f_str_t * src, gchar c) ssize_t fstrstr (f_str_t * orig, f_str_t * pattern) { - register size_t cur = 0, pcur = 0; + register size_t cur = 0, pcur = 0; if (pattern->len > orig->len) { return -1; @@ -97,16 +97,18 @@ fstrstr (f_str_t * orig, f_str_t * pattern) ssize_t fstrstri (f_str_t * orig, f_str_t * pattern) { - register size_t cur = 0, pcur = 0; + register size_t cur = 0, pcur = 0; if (pattern->len > orig->len) { return -1; } while (cur < orig->len) { - if (g_ascii_tolower (*(orig->begin + cur)) == g_ascii_tolower (*pattern->begin)) { + if (g_ascii_tolower (*(orig->begin + cur)) == + g_ascii_tolower (*pattern->begin)) { while (cur < orig->len && pcur < pattern->len) { - if (g_ascii_tolower (*(orig->begin + cur)) != g_ascii_tolower (*(pattern->begin + pcur))) { + if (g_ascii_tolower (*(orig->begin + cur)) != + g_ascii_tolower (*(pattern->begin + pcur))) { pcur = 0; break; } @@ -127,14 +129,14 @@ fstrstri (f_str_t * orig, f_str_t * pattern) * word contains parsed word * * Return: -1 - no new words can be extracted - * 1 - word was extracted and there are more words - * 0 - last word extracted + * 1 - word was extracted and there are more words + * 0 - last word extracted */ gint fstrtok (f_str_t * text, const gchar *sep, f_tok_t * state) { - register size_t cur; - const gchar *csep = sep; + register size_t cur; + const gchar *csep = sep; if (state->pos >= text->len) { return -1; @@ -170,7 +172,7 @@ fstrtok (f_str_t * text, const gchar *sep, f_tok_t * state) size_t fstrcpy (f_str_t * dest, f_str_t * src) { - register size_t cur = 0; + register size_t cur = 0; if (dest->size < src->len) { return 0; @@ -190,8 +192,8 @@ fstrcpy (f_str_t * dest, f_str_t * src) size_t fstrcat (f_str_t * dest, f_str_t * src) { - register size_t cur = 0; - gchar *p = dest->begin + dest->len; + register size_t cur = 0; + gchar *p = dest->begin + dest->len; if (dest->size < src->len + dest->len) { return 0; @@ -212,10 +214,10 @@ fstrcat (f_str_t * dest, f_str_t * src) /* * Make copy of string to 0-terminated string */ -gchar * +gchar * fstrcstr (f_str_t * str, rspamd_mempool_t * pool) { - gchar *res; + gchar *res; res = rspamd_mempool_alloc (pool, str->len + 1); /* Do not allow multiply \0 characters */ @@ -247,7 +249,7 @@ fstrpush (f_str_t * dest, gchar c) gint fstrpush_unichar (f_str_t * dest, gunichar c) { - int l; + int l; if (dest->size < dest->len) { /* Need to reallocate string */ return 0; @@ -261,10 +263,10 @@ fstrpush_unichar (f_str_t * dest, gunichar c) /* * Allocate memory for f_str_t */ -f_str_t * +f_str_t * fstralloc (rspamd_mempool_t * pool, size_t len) { - f_str_t *res = rspamd_mempool_alloc (pool, sizeof (f_str_t)); + f_str_t *res = rspamd_mempool_alloc (pool, sizeof (f_str_t)); res->begin = rspamd_mempool_alloc (pool, len); @@ -276,10 +278,10 @@ fstralloc (rspamd_mempool_t * pool, size_t len) /* * Allocate memory for f_str_t from temporary pool */ -f_str_t * +f_str_t * fstralloc_tmp (rspamd_mempool_t * pool, size_t len) { - f_str_t *res = rspamd_mempool_alloc_tmp (pool, sizeof (f_str_t)); + f_str_t *res = rspamd_mempool_alloc_tmp (pool, sizeof (f_str_t)); res->begin = rspamd_mempool_alloc_tmp (pool, len); @@ -291,10 +293,10 @@ fstralloc_tmp (rspamd_mempool_t * pool, size_t len) /* * Truncate string to its len */ -f_str_t * +f_str_t * fstrtruncate (rspamd_mempool_t * pool, f_str_t * orig) { - f_str_t *res; + f_str_t *res; if (orig == NULL || orig->len == 0 || orig->size <= orig->len) { return orig; @@ -312,10 +314,10 @@ fstrtruncate (rspamd_mempool_t * pool, f_str_t * orig) /* * Enlarge string to new size */ -f_str_t * +f_str_t * fstrgrow (rspamd_mempool_t * pool, f_str_t * orig, size_t newlen) { - f_str_t *res; + f_str_t *res; if (orig == NULL || orig->len == 0 || orig->size >= newlen) { return orig; @@ -333,7 +335,7 @@ fstrgrow (rspamd_mempool_t * pool, f_str_t * orig, size_t newlen) static guint32 fstrhash_c (gchar c, guint32 hval) { - guint32 tmp; + guint32 tmp; /* * xor in the current byte against each byte of hval * (which alone gaurantees that every bit of input will have @@ -364,9 +366,9 @@ fstrhash_c (gchar c, guint32 hval) guint32 fstrhash (f_str_t * str) { - size_t i; - guint32 hval; - gchar *c = str->begin; + size_t i; + guint32 hval; + gchar *c = str->begin; if (str == NULL) { return 0; @@ -385,11 +387,11 @@ fstrhash (f_str_t * str) guint32 fstrhash_lowercase (f_str_t * str, gboolean is_utf) { - gsize i; - guint32 j, hval; - const gchar *p = str->begin, *end = NULL; - gchar t; - gunichar uc; + gsize i; + guint32 j, hval; + const gchar *p = str->begin, *end = NULL; + gchar t; + gunichar uc; if (str == NULL) { return 0; @@ -403,7 +405,7 @@ fstrhash_lowercase (f_str_t * str, gboolean is_utf) } while (p < end) { uc = g_unichar_tolower (g_utf8_get_char (p)); - for (j = 0; j < sizeof (gunichar); j ++) { + for (j = 0; j < sizeof (gunichar); j++) { t = (uc >> (j * 8)) & 0xff; if (t != 0) { hval = fstrhash_c (t, hval); @@ -427,8 +429,8 @@ fstrhash_lowercase (f_str_t * str, gboolean is_utf) void fstrstrip (f_str_t * str) { - gchar *p = str->begin; - guint r = 0; + gchar *p = str->begin; + guint r = 0; while (r < str->len) { if (g_ascii_isspace (*p)) { diff --git a/src/libutil/fstring.h b/src/libutil/fstring.h index bd680e365..3cba00592 100644 --- a/src/libutil/fstring.h +++ b/src/libutil/fstring.h @@ -8,7 +8,8 @@ #include "config.h" #include "mem_pool.h" -#define update_buf_size(x) (x)->free = (x)->buf->size - ((x)->pos - (x)->buf->begin); (x)->buf->len = (x)->pos - (x)->buf->begin +#define update_buf_size(x) (x)->free = (x)->buf->size - \ + ((x)->pos - (x)->buf->begin); (x)->buf->len = (x)->pos - (x)->buf->begin typedef struct f_str_s { gchar *begin; @@ -76,22 +77,22 @@ gint fstrpush_unichar (f_str_t *dest, gunichar c); /* * Allocate memory for f_str_t */ -f_str_t* fstralloc (rspamd_mempool_t *pool, size_t len); +f_str_t * fstralloc (rspamd_mempool_t *pool, size_t len); /* * Allocate memory for f_str_t from temporary pool */ -f_str_t* fstralloc_tmp (rspamd_mempool_t *pool, size_t len); +f_str_t * fstralloc_tmp (rspamd_mempool_t *pool, size_t len); /* * Truncate string to its len */ -f_str_t* fstrtruncate (rspamd_mempool_t *pool, f_str_t *orig); +f_str_t * fstrtruncate (rspamd_mempool_t *pool, f_str_t *orig); /* * Enlarge string to new size */ -f_str_t* fstrgrow (rspamd_mempool_t *pool, f_str_t *orig, size_t newlen); +f_str_t * fstrgrow (rspamd_mempool_t *pool, f_str_t *orig, size_t newlen); /* * Return specified character @@ -110,7 +111,7 @@ guint32 fstrhash_lowercase (f_str_t *str, gboolean is_utf); /* * Make copy of string to 0-terminated string */ -gchar* fstrcstr (f_str_t *str, rspamd_mempool_t *pool); +gchar * fstrcstr (f_str_t *str, rspamd_mempool_t *pool); /* * Strip fstr string from space symbols diff --git a/src/libutil/fuzzy.c b/src/libutil/fuzzy.c index 7e8a01ce3..a58fee654 100644 --- a/src/libutil/fuzzy.c +++ b/src/libutil/fuzzy.c @@ -24,30 +24,31 @@ #include "config.h" -#include "mem_pool.h" #include "fstring.h" #include "fuzzy.h" +#include "main.h" +#include "mem_pool.h" #include "message.h" #include "url.h" -#include "main.h" #define ROLL_WINDOW_SIZE 9 #define MIN_FUZZY_BLOCK_SIZE 3 #define HASH_INIT 0x28021967 -static const char *b64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; +static const char *b64 = + "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; struct roll_state { - guint32 h[3]; - gchar window[ROLL_WINDOW_SIZE]; - gint n; + guint32 h[3]; + gchar window[ROLL_WINDOW_SIZE]; + gint n; }; -static struct roll_state rs; +static struct roll_state rs; /* Rolling hash function based on Adler-32 checksum */ -static guint32 +static guint32 fuzzy_roll_hash (guint c) { /* Check window position */ @@ -72,19 +73,20 @@ fuzzy_roll_hash (guint c) } /* A simple non-rolling hash, based on the FNV hash */ -static guint32 +static guint32 fuzzy_fnv_hash (guint c, guint32 hval) { hval ^= c; - hval += (hval << 1) + (hval << 4) + (hval << 7) + (hval << 8) + (hval << 24); + hval += + (hval << 1) + (hval << 4) + (hval << 7) + (hval << 8) + (hval << 24); return hval; } /* Calculate blocksize depending on length of input */ -static guint32 +static guint32 fuzzy_blocksize (guint32 len) { - guint32 nlen = MIN_FUZZY_BLOCK_SIZE; + guint32 nlen = MIN_FUZZY_BLOCK_SIZE; while (nlen * (FUZZY_HASHLEN - 1) < len) { nlen *= 2; @@ -141,12 +143,12 @@ fuzzy_update2 (fuzzy_hash_t * h1, fuzzy_hash_t *h2, guint c) guint32 lev_distance (gchar *s1, gint len1, gchar *s2, gint len2) { - gint i; - gint *row; /* we only need to keep one row of costs */ - gint *end; - gint half, nx; - gchar *sx, *char2p, char1; - gint *p, D, x, offset, c3; + gint i; + gint *row; /* we only need to keep one row of costs */ + gint *end; + gint half, nx; + gchar *sx, *char2p, char1; + gint *p, D, x, offset, c3; /* strip common prefix */ while (len1 > 0 && len2 > 0 && *s1 == *s2) { @@ -252,13 +254,13 @@ lev_distance (gchar *s1, gint len1, gchar *s2, gint len2) } /* Calculate fuzzy hash for specified string */ -fuzzy_hash_t * +fuzzy_hash_t * fuzzy_init (f_str_t * in, rspamd_mempool_t * pool) { - fuzzy_hash_t *new; - guint i, repeats = 0; - gchar *c = in->begin, last = '\0'; - gsize real_len = 0; + fuzzy_hash_t *new; + guint i, repeats = 0; + gchar *c = in->begin, last = '\0'; + gsize real_len = 0; new = rspamd_mempool_alloc0 (pool, sizeof (fuzzy_hash_t)); bzero (&rs, sizeof (rs)); @@ -270,7 +272,7 @@ fuzzy_init (f_str_t * in, rspamd_mempool_t * pool) repeats = 0; } if (!g_ascii_isspace (*c) && !g_ascii_ispunct (*c) && repeats < 3) { - real_len ++; + real_len++; } last = *c; c++; @@ -301,10 +303,10 @@ fuzzy_init (f_str_t * in, rspamd_mempool_t * pool) return new; } -fuzzy_hash_t * +fuzzy_hash_t * fuzzy_init_byte_array (GByteArray * in, rspamd_mempool_t * pool) { - f_str_t f; + f_str_t f; f.begin = (gchar *)in->data; f.len = in->len; @@ -313,15 +315,17 @@ fuzzy_init_byte_array (GByteArray * in, rspamd_mempool_t * pool) } void -fuzzy_init_part (struct mime_text_part *part, rspamd_mempool_t *pool, gsize max_diff) +fuzzy_init_part (struct mime_text_part *part, + rspamd_mempool_t *pool, + gsize max_diff) { - fuzzy_hash_t *new, *new2; - gchar *c, *end, *begin; - gsize real_len = 0, len = part->content->len; - GList *cur_offset; - struct process_exception *cur_ex = NULL; - gunichar uc; - gboolean write_diff = FALSE; + fuzzy_hash_t *new, *new2; + gchar *c, *end, *begin; + gsize real_len = 0, len = part->content->len; + GList *cur_offset; + struct process_exception *cur_ex = NULL; + gunichar uc; + gboolean write_diff = FALSE; cur_offset = part->urls_offset; if (cur_offset != NULL) { @@ -347,7 +351,7 @@ fuzzy_init_part (struct mime_text_part *part, rspamd_mempool_t *pool, gsize max_ else { uc = g_utf8_get_char (c); if (g_unichar_isalnum (uc)) { - real_len ++; + real_len++; } c = g_utf8_next_char (c); } @@ -364,7 +368,7 @@ fuzzy_init_part (struct mime_text_part *part, rspamd_mempool_t *pool, gsize max_ } else { if (!g_ascii_isspace (*c) && !g_ascii_ispunct (*c)) { - real_len ++; + real_len++; } c++; } @@ -450,7 +454,7 @@ fuzzy_init_part (struct mime_text_part *part, rspamd_mempool_t *pool, gsize max_ gint fuzzy_compare_hashes (fuzzy_hash_t * h1, fuzzy_hash_t * h2) { - gint res, l1, l2; + gint res, l1, l2; /* If we have hashes of different size, input strings are too different */ if (h1->block_size != h2->block_size) { @@ -493,6 +497,6 @@ fuzzy_compare_parts (struct mime_text_part *p1, struct mime_text_part *p2) return 0; } -/* - * vi:ts=4 +/* + * vi:ts=4 */ diff --git a/src/libutil/fuzzy.h b/src/libutil/fuzzy.h index c226c5765..2e3c8a752 100644 --- a/src/libutil/fuzzy.h +++ b/src/libutil/fuzzy.h @@ -7,17 +7,17 @@ #define RSPAMD_FUZZY_H #include "config.h" -#include "mem_pool.h" #include "fstring.h" +#include "mem_pool.h" #define FUZZY_HASHLEN 64 typedef struct fuzzy_hash_s { - gchar hash_pipe[FUZZY_HASHLEN]; /**< result hash */ - guint32 block_size; /**< current blocksize */ - guint32 rh; /**< roll hash value */ - guint32 h; /**< hash of block */ - guint32 hi; /**< current index in hash pipe */ + gchar hash_pipe[FUZZY_HASHLEN]; /**< result hash */ + guint32 block_size; /**< current blocksize */ + guint32 rh; /**< roll hash value */ + guint32 h; /**< hash of block */ + guint32 hi; /**< current index in hash pipe */ } fuzzy_hash_t; struct mime_text_part; @@ -44,13 +44,15 @@ fuzzy_hash_t * fuzzy_init_byte_array (GByteArray *in, rspamd_mempool_t *pool); * @param max_diff maximum text length to use diff algorithm in comparasions * @return fuzzy_hash object allocated in pool */ -void fuzzy_init_part (struct mime_text_part *part, rspamd_mempool_t *pool, gsize max_diff); +void fuzzy_init_part (struct mime_text_part *part, + rspamd_mempool_t *pool, + gsize max_diff); /** - * Compare score of difference between two hashes + * Compare score of difference between two hashes * @param h1 first hash * @param h2 second hash - * @return result in percents 0 - different hashes, 100 - identical hashes + * @return result in percents 0 - different hashes, 100 - identical hashes */ gint fuzzy_compare_hashes (fuzzy_hash_t *h1, fuzzy_hash_t *h2); diff --git a/src/libutil/hash.c b/src/libutil/hash.c index 3bb381651..0e8b1646e 100644 --- a/src/libutil/hash.c +++ b/src/libutil/hash.c @@ -33,11 +33,13 @@ * will use this function internally. */ static inline struct rspamd_hash_node ** -rspamd_hash_lookup_node (rspamd_hash_t * hash, gconstpointer key, guint * hash_return) +rspamd_hash_lookup_node (rspamd_hash_t * hash, + gconstpointer key, + guint * hash_return) { - struct rspamd_hash_node **node_ptr, *node; - guint hash_value; - hash_value = (*hash->hash_func) (key); + struct rspamd_hash_node **node_ptr, *node; + guint hash_value; + hash_value = (*hash->hash_func)(key); if (hash->shared) { rspamd_mempool_rlock_rwlock (hash->lock); @@ -58,7 +60,8 @@ rspamd_hash_lookup_node (rspamd_hash_t * hash, gconstpointer key, guint * hash_r */ if (hash->key_equal_func) { while ((node = *node_ptr)) { - if (node->key_hash == hash_value && hash->key_equal_func (node->key, key)) { + if (node->key_hash == hash_value && + hash->key_equal_func (node->key, key)) { break; } node_ptr = &(*node_ptr)->next; @@ -83,9 +86,10 @@ rspamd_hash_lookup_node (rspamd_hash_t * hash, gconstpointer key, guint * hash_r * No table resize is performed. */ static void -rspamd_hash_remove_node (rspamd_hash_t * hash, struct rspamd_hash_node ***node_ptr_ptr) +rspamd_hash_remove_node (rspamd_hash_t * hash, + struct rspamd_hash_node ***node_ptr_ptr) { - struct rspamd_hash_node **node_ptr, *node; + struct rspamd_hash_node **node_ptr, *node; if (hash->shared) { rspamd_mempool_wlock_rwlock (hash->lock); @@ -108,19 +112,23 @@ rspamd_hash_remove_node (rspamd_hash_t * hash, struct rspamd_hash_node ***node_p static void rspamd_hash_resize (rspamd_hash_t * hash) { - struct rspamd_hash_node **new_nodes; - struct rspamd_hash_node *node, *next; - guint hash_val; - gint new_size, i; + struct rspamd_hash_node **new_nodes; + struct rspamd_hash_node *node, *next; + guint hash_val; + gint new_size, i; new_size = g_spaced_primes_closest (hash->nnodes); new_size = CLAMP (new_size, HASH_TABLE_MIN_SIZE, HASH_TABLE_MAX_SIZE); if (hash->shared) { - new_nodes = rspamd_mempool_alloc_shared (hash->pool, sizeof (struct rspamd_hash_node *) * new_size); + new_nodes = + rspamd_mempool_alloc_shared (hash->pool, + sizeof (struct rspamd_hash_node *) * new_size); } else { - new_nodes = rspamd_mempool_alloc (hash->pool, sizeof (struct rspamd_hash_node *) * new_size); + new_nodes = + rspamd_mempool_alloc (hash->pool, + sizeof (struct rspamd_hash_node *) * new_size); } if (hash->shared) { @@ -150,46 +158,56 @@ rspamd_hash_resize (rspamd_hash_t * hash) static inline void rspamd_hash_maybe_resize (rspamd_hash_t * hash) { - gint nnodes = hash->nnodes; - gint size = hash->size; + gint nnodes = hash->nnodes; + gint size = hash->size; - if ((size >= 3 * nnodes && size > HASH_TABLE_MIN_SIZE) || (3 * size <= nnodes && size < HASH_TABLE_MAX_SIZE)) { + if ((size >= 3 * nnodes && + size > HASH_TABLE_MIN_SIZE) || + (3 * size <= nnodes && size < HASH_TABLE_MAX_SIZE)) { rspamd_hash_resize (hash); } } /* Create new hash in specified pool */ -rspamd_hash_t * -rspamd_hash_new (rspamd_mempool_t * pool, GHashFunc hash_func, GEqualFunc key_equal_func) +rspamd_hash_t * +rspamd_hash_new (rspamd_mempool_t * pool, + GHashFunc hash_func, + GEqualFunc key_equal_func) { - rspamd_hash_t *hash; + rspamd_hash_t *hash; hash = rspamd_mempool_alloc (pool, sizeof (rspamd_hash_t)); hash->size = HASH_TABLE_MIN_SIZE; hash->nnodes = 0; hash->hash_func = hash_func ? hash_func : g_direct_hash; hash->key_equal_func = key_equal_func; - hash->nodes = rspamd_mempool_alloc0 (pool, sizeof (struct rspamd_hash_node *) * hash->size); + hash->nodes = rspamd_mempool_alloc0 (pool, + sizeof (struct rspamd_hash_node *) * hash->size); hash->shared = 0; hash->pool = pool; return hash; } -/* - * Create new hash in specified pool using shared memory +/* + * Create new hash in specified pool using shared memory */ -rspamd_hash_t * -rspamd_hash_new_shared (rspamd_mempool_t * pool, GHashFunc hash_func, GEqualFunc key_equal_func, gint size) +rspamd_hash_t * +rspamd_hash_new_shared (rspamd_mempool_t * pool, + GHashFunc hash_func, + GEqualFunc key_equal_func, + gint size) { - rspamd_hash_t *hash; + rspamd_hash_t *hash; hash = rspamd_mempool_alloc_shared (pool, sizeof (rspamd_hash_t)); hash->size = size; hash->nnodes = 0; hash->hash_func = hash_func ? hash_func : g_direct_hash; hash->key_equal_func = key_equal_func; - hash->nodes = rspamd_mempool_alloc0_shared (pool, sizeof (struct rspamd_hash_node *) * hash->size); + hash->nodes = + rspamd_mempool_alloc0_shared (pool, + sizeof (struct rspamd_hash_node *) * hash->size); hash->shared = 1; /* Get mutex from pool for locking on insert/remove operations */ hash->lock = rspamd_mempool_get_rwlock (pool); @@ -198,14 +216,14 @@ rspamd_hash_new_shared (rspamd_mempool_t * pool, GHashFunc hash_func, GEqualFunc return hash; } -/* - * Insert item in hash +/* + * Insert item in hash */ void rspamd_hash_insert (rspamd_hash_t * hash, gpointer key, gpointer value) { - struct rspamd_hash_node **node_ptr, *node; - guint key_hash; + struct rspamd_hash_node **node_ptr, *node; + guint key_hash; g_return_if_fail (hash != NULL); node_ptr = rspamd_hash_lookup_node (hash, key, &key_hash); @@ -219,10 +237,14 @@ rspamd_hash_insert (rspamd_hash_t * hash, gpointer key, gpointer value) } else { if (hash->shared) { - node = rspamd_mempool_alloc_shared (hash->pool, sizeof (struct rspamd_hash_node)); + node = + rspamd_mempool_alloc_shared (hash->pool, + sizeof (struct rspamd_hash_node)); } else { - node = rspamd_mempool_alloc (hash->pool, sizeof (struct rspamd_hash_node)); + node = + rspamd_mempool_alloc (hash->pool, + sizeof (struct rspamd_hash_node)); } node->key = key; @@ -242,13 +264,13 @@ rspamd_hash_insert (rspamd_hash_t * hash, gpointer key, gpointer value) } } -/* - * Remove item from hash +/* + * Remove item from hash */ gboolean rspamd_hash_remove (rspamd_hash_t * hash, gpointer key) { - struct rspamd_hash_node **node_ptr; + struct rspamd_hash_node **node_ptr; g_return_val_if_fail (hash != NULL, FALSE); @@ -262,13 +284,13 @@ rspamd_hash_remove (rspamd_hash_t * hash, gpointer key) return TRUE; } -/* - * Lookup item from hash +/* + * Lookup item from hash */ gpointer rspamd_hash_lookup (rspamd_hash_t * hash, gpointer key) { - struct rspamd_hash_node *node; + struct rspamd_hash_node *node; g_return_val_if_fail (hash != NULL, NULL); node = *rspamd_hash_lookup_node (hash, key, NULL); @@ -276,14 +298,14 @@ rspamd_hash_lookup (rspamd_hash_t * hash, gpointer key) return node ? node->value : NULL; } -/* - * Iterate throught hash +/* + * Iterate throught hash */ void rspamd_hash_foreach (rspamd_hash_t * hash, GHFunc func, gpointer user_data) { - struct rspamd_hash_node *node; - gint i; + struct rspamd_hash_node *node; + gint i; g_return_if_fail (hash != NULL); g_return_if_fail (func != NULL); @@ -293,7 +315,7 @@ rspamd_hash_foreach (rspamd_hash_t * hash, GHFunc func, gpointer user_data) } for (i = 0; i < hash->size; i++) { for (node = hash->nodes[i]; node; node = node->next) { - (*func) (node->key, node->value, user_data); + (*func)(node->key, node->value, user_data); } } if (hash->shared) { @@ -308,7 +330,7 @@ rspamd_hash_foreach (rspamd_hash_t * hash, GHFunc func, gpointer user_data) static void rspamd_lru_hash_destroy_node (gpointer v) { - rspamd_lru_element_t *node = v; + rspamd_lru_element_t *node = v; if (node->hash->value_destroy) { node->hash->value_destroy (node->data); @@ -317,10 +339,14 @@ rspamd_lru_hash_destroy_node (gpointer v) g_slice_free1 (sizeof (rspamd_lru_element_t), node); } -static rspamd_lru_element_t* -rspamd_lru_create_node (rspamd_lru_hash_t *hash, gpointer key, gpointer value, time_t now, guint ttl) +static rspamd_lru_element_t * +rspamd_lru_create_node (rspamd_lru_hash_t *hash, + gpointer key, + gpointer value, + time_t now, + guint ttl) { - rspamd_lru_element_t *node; + rspamd_lru_element_t *node; node = g_slice_alloc (sizeof (rspamd_lru_element_t)); node->data = value; @@ -340,14 +366,21 @@ rspamd_lru_create_node (rspamd_lru_hash_t *hash, gpointer key, gpointer value, t * @param key_equal_func pointer to function for comparing keys * @return new rspamd_hash object */ -rspamd_lru_hash_t* -rspamd_lru_hash_new (GHashFunc hash_func, GEqualFunc key_equal_func, gint maxsize, gint maxage, - GDestroyNotify key_destroy, GDestroyNotify value_destroy) +rspamd_lru_hash_t * +rspamd_lru_hash_new (GHashFunc hash_func, + GEqualFunc key_equal_func, + gint maxsize, + gint maxage, + GDestroyNotify key_destroy, + GDestroyNotify value_destroy) { - rspamd_lru_hash_t *new; + rspamd_lru_hash_t *new; new = g_malloc (sizeof (rspamd_lru_hash_t)); - new->storage = g_hash_table_new_full (hash_func, key_equal_func, key_destroy, rspamd_lru_hash_destroy_node); + new->storage = g_hash_table_new_full (hash_func, + key_equal_func, + key_destroy, + rspamd_lru_hash_destroy_node); new->maxage = maxage; new->maxsize = maxsize; new->value_destroy = value_destroy; @@ -368,13 +401,19 @@ rspamd_lru_hash_new (GHashFunc hash_func, GEqualFunc key_equal_func, gint maxsiz * @param key_equal_func pointer to function for comparing keys * @return new rspamd_hash object */ -rspamd_lru_hash_t* -rspamd_lru_hash_new_full (GHashFunc hash_func, GEqualFunc key_equal_func, - gint maxsize, gint maxage, GDestroyNotify key_destroy, GDestroyNotify value_destroy, - gpointer storage, lru_cache_insert_func insert_func, lru_cache_lookup_func lookup_func, - lru_cache_delete_func delete_func) +rspamd_lru_hash_t * +rspamd_lru_hash_new_full (GHashFunc hash_func, + GEqualFunc key_equal_func, + gint maxsize, + gint maxage, + GDestroyNotify key_destroy, + GDestroyNotify value_destroy, + gpointer storage, + lru_cache_insert_func insert_func, + lru_cache_lookup_func lookup_func, + lru_cache_delete_func delete_func) { - rspamd_lru_hash_t *new; + rspamd_lru_hash_t *new; new = g_malloc (sizeof (rspamd_lru_hash_t)); new->storage = storage; @@ -400,7 +439,7 @@ rspamd_lru_hash_new_full (GHashFunc hash_func, GEqualFunc key_equal_func, gpointer rspamd_lru_hash_lookup (rspamd_lru_hash_t *hash, gpointer key, time_t now) { - rspamd_lru_element_t *res; + rspamd_lru_element_t *res; if ((res = hash->lookup_func (hash->storage, key)) != NULL) { if (res->ttl != 0) { @@ -434,17 +473,17 @@ rspamd_lru_hash_lookup (rspamd_lru_hash_t *hash, gpointer key, time_t now) */ void rspamd_lru_hash_insert (rspamd_lru_hash_t *hash, gpointer key, gpointer value, - time_t now, guint ttl) + time_t now, guint ttl) { - rspamd_lru_element_t *res; - gint removed = 0; + rspamd_lru_element_t *res; + gint removed = 0; if ((res = hash->lookup_func (hash->storage, key)) != NULL) { hash->delete_func (hash->storage, res->key); } else { if (hash->maxsize > 0 && - (gint)g_queue_get_length (hash->q) >= hash->maxsize) { + (gint)g_queue_get_length (hash->q) >= hash->maxsize) { /* Expire some elements */ res = g_queue_peek_tail (hash->q); if (hash->maxage > 0) { @@ -456,7 +495,7 @@ rspamd_lru_hash_insert (rspamd_lru_hash_t *hash, gpointer key, gpointer value, break; } res = g_queue_peek_tail (hash->q); - removed ++; + removed++; } } if (removed == 0) { diff --git a/src/libutil/hash.h b/src/libutil/hash.h index c5d4639af..7f4f82b43 100644 --- a/src/libutil/hash.h +++ b/src/libutil/hash.h @@ -10,49 +10,50 @@ #include "mem_pool.h" struct rspamd_hash_node { - gpointer key; - gpointer value; - guint key_hash; + gpointer key; + gpointer value; + guint key_hash; struct rspamd_hash_node *next; }; typedef struct rspamd_hash_s { - gint size; - gint nnodes; + gint size; + gint nnodes; struct rspamd_hash_node **nodes; - GHashFunc hash_func; - GEqualFunc key_equal_func; - gint shared; - rspamd_mempool_rwlock_t *lock; - rspamd_mempool_t *pool; + GHashFunc hash_func; + GEqualFunc key_equal_func; + gint shared; + rspamd_mempool_rwlock_t *lock; + rspamd_mempool_t *pool; } rspamd_hash_t; -typedef void (*lru_cache_insert_func)(gpointer storage, gpointer key, gpointer value); +typedef void (*lru_cache_insert_func)(gpointer storage, gpointer key, + gpointer value); typedef gpointer (*lru_cache_lookup_func)(gpointer storage, gpointer key); typedef gboolean (*lru_cache_delete_func)(gpointer storage, gpointer key); typedef void (*lru_cache_destroy_func)(gpointer storage); typedef struct rspamd_lru_hash_s { - gint maxsize; - gint maxage; - GDestroyNotify value_destroy; - GDestroyNotify key_destroy; - GQueue *q; - gpointer storage; - lru_cache_insert_func insert_func; - lru_cache_lookup_func lookup_func; - lru_cache_delete_func delete_func; - lru_cache_destroy_func destroy_func; + gint maxsize; + gint maxage; + GDestroyNotify value_destroy; + GDestroyNotify key_destroy; + GQueue *q; + gpointer storage; + lru_cache_insert_func insert_func; + lru_cache_lookup_func lookup_func; + lru_cache_delete_func delete_func; + lru_cache_destroy_func destroy_func; } rspamd_lru_hash_t; typedef struct rspamd_lru_element_s { - gpointer data; - gpointer key; - time_t store_time; - guint ttl; - rspamd_lru_hash_t *hash; - GList *link; + gpointer data; + gpointer key; + time_t store_time; + guint ttl; + rspamd_lru_hash_t *hash; + GList *link; } rspamd_lru_element_t; @@ -65,7 +66,9 @@ typedef struct rspamd_lru_element_s { * @param key_equal_func pointer to function for comparing keys * @return new rspamd_hash object */ -rspamd_hash_t* rspamd_hash_new (rspamd_mempool_t *pool, GHashFunc hash_func, GEqualFunc key_equal_func); +rspamd_hash_t * rspamd_hash_new (rspamd_mempool_t *pool, + GHashFunc hash_func, + GEqualFunc key_equal_func); /** * Create new hash in specified pool using shared memory @@ -74,7 +77,10 @@ rspamd_hash_t* rspamd_hash_new (rspamd_mempool_t *pool, GHashFunc hash_func, GEq * @param key_equal_func pointer to function for comparing keys * @return new rspamd_hash object */ -rspamd_hash_t* rspamd_hash_new_shared (rspamd_mempool_t *pool, GHashFunc hash_func, GEqualFunc key_equal_func, gint size); +rspamd_hash_t * rspamd_hash_new_shared (rspamd_mempool_t *pool, + GHashFunc hash_func, + GEqualFunc key_equal_func, + gint size); /** * Insert item in hash @@ -99,7 +105,7 @@ gboolean rspamd_hash_remove (rspamd_hash_t *hash, gpointer key); */ gpointer rspamd_hash_lookup (rspamd_hash_t *hash, gpointer key); -/** +/** * Iterate throught hash * @param hash hash object * @param func user's function that would be called for each key/value pair @@ -115,8 +121,12 @@ void rspamd_hash_foreach (rspamd_hash_t *hash, GHFunc func, gpointer user_data); * @param key_equal_func pointer to function for comparing keys * @return new rspamd_hash object */ -rspamd_lru_hash_t* rspamd_lru_hash_new (GHashFunc hash_func, GEqualFunc key_equal_func, - gint maxsize, gint maxage, GDestroyNotify key_destroy, GDestroyNotify value_destroy); +rspamd_lru_hash_t * rspamd_lru_hash_new (GHashFunc hash_func, + GEqualFunc key_equal_func, + gint maxsize, + gint maxage, + GDestroyNotify key_destroy, + GDestroyNotify value_destroy); /** * Create new lru hash with custom storage @@ -126,25 +136,36 @@ rspamd_lru_hash_t* rspamd_lru_hash_new (GHashFunc hash_func, GEqualFunc key_equa * @param key_equal_func pointer to function for comparing keys * @return new rspamd_hash object */ -rspamd_lru_hash_t* rspamd_lru_hash_new_full (GHashFunc hash_func, GEqualFunc key_equal_func, - gint maxsize, gint maxage, GDestroyNotify key_destroy, GDestroyNotify value_destroy, - gpointer storage, lru_cache_insert_func insert_func, lru_cache_lookup_func lookup_func, - lru_cache_delete_func delete_func); +rspamd_lru_hash_t * rspamd_lru_hash_new_full (GHashFunc hash_func, + GEqualFunc key_equal_func, + gint maxsize, + gint maxage, + GDestroyNotify key_destroy, + GDestroyNotify value_destroy, + gpointer storage, + lru_cache_insert_func insert_func, + lru_cache_lookup_func lookup_func, + lru_cache_delete_func delete_func); /** * Lookup item from hash * @param hash hash object * @param key key to find * @return value of key or NULL if key is not found */ -gpointer rspamd_lru_hash_lookup (rspamd_lru_hash_t *hash, gpointer key, time_t now); +gpointer rspamd_lru_hash_lookup (rspamd_lru_hash_t *hash, + gpointer key, + time_t now); /** * Insert item in hash * @param hash hash object * @param key key to insert * @param value value of key */ -void rspamd_lru_hash_insert (rspamd_lru_hash_t *hash, gpointer key, gpointer value, - time_t now, guint ttl); +void rspamd_lru_hash_insert (rspamd_lru_hash_t *hash, + gpointer key, + gpointer value, + time_t now, + guint ttl); /** * Remove lru hash diff --git a/src/libutil/http.c b/src/libutil/http.c index 420dd165c..58eda7267 100644 --- a/src/libutil/http.c +++ b/src/libutil/http.c @@ -23,10 +23,10 @@ #include "config.h" #include "http.h" -#include "utlist.h" -#include "util.h" -#include "printf.h" #include "logger.h" +#include "printf.h" +#include "util.h" +#include "utlist.h" struct rspamd_http_connection_private { GString *buf; @@ -65,9 +65,9 @@ static const struct _rspamd_http_magic { [HTTP_MAGIC_JPG] = { "jpg", "image/jpeg" }, }; -static gchar *http_week[] = { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" }; -static gchar *http_month[] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", - "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" }; +static gchar *http_week[] = { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" }; +static gchar *http_month[] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", + "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" }; #define HTTP_ERROR http_error_quark () @@ -228,13 +228,13 @@ rspamd_http_parse_date (const gchar *header, gsize len) if (fmt == rfc822) { if (*p < '0' || *p > '9' || *(p + 1) < '0' || *(p + 1) > '9' - || *(p + 2) < '0' || *(p + 2) > '9' || *(p + 3) < '0' - || *(p + 3) > '9') { + || *(p + 2) < '0' || *(p + 2) > '9' || *(p + 3) < '0' + || *(p + 3) > '9') { return (time_t)-1; } year = (*p - '0') * 1000 + (*(p + 1) - '0') * 100 - + (*(p + 2) - '0') * 10 + *(p + 3) - '0'; + + (*(p + 2) - '0') * 10 + *(p + 3) - '0'; p += 4; } @@ -312,13 +312,13 @@ rspamd_http_parse_date (const gchar *header, gsize len) } if (*p < '0' || *p > '9' || *(p + 1) < '0' || *(p + 1) > '9' - || *(p + 2) < '0' || *(p + 2) > '9' || *(p + 3) < '0' - || *(p + 3) > '9') { + || *(p + 2) < '0' || *(p + 2) > '9' || *(p + 3) < '0' + || *(p + 3) > '9') { return (time_t)-1; } year = (*p - '0') * 1000 + (*(p + 1) - '0') * 100 - + (*(p + 2) - '0') * 10 + *(p + 3) - '0'; + + (*(p + 2) - '0') * 10 + *(p + 3) - '0'; } if (hour > 23 || min > 59 || sec > 59) { @@ -348,24 +348,24 @@ rspamd_http_parse_date (const gchar *header, gsize len) /* Gauss' formula for Gregorian days since March 1, 1 BC */ time = (guint64) ( - /* days in years including leap years since March 1, 1 BC */ + /* days in years including leap years since March 1, 1 BC */ - 365 * year + year / 4 - year / 100 + year / 400 + 365 * year + year / 4 - year / 100 + year / 400 - /* days before the month */ + /* days before the month */ - + 367 * month / 12 - 30 + + 367 * month / 12 - 30 - /* days before the day */ + /* days before the day */ - + day - 1 + + day - 1 - /* - * 719527 days were between March 1, 1 BC and March 1, 1970, - * 31 and 28 days were in January and February 1970 - */ + /* + * 719527 days were between March 1, 1 BC and March 1, 1970, + * 31 and 28 days were in January and February 1970 + */ - - 719527 + 31 + 28) * 86400 + hour * 3600 + min * 60 + sec; + - 719527 + 31 + 28) * 86400 + hour * 3600 + min * 60 + sec; return (time_t) time; } @@ -380,9 +380,10 @@ rspamd_http_check_date (struct rspamd_http_connection_private *priv) } static gint -rspamd_http_on_url (http_parser* parser, const gchar *at, size_t length) +rspamd_http_on_url (http_parser * parser, const gchar *at, size_t length) { - struct rspamd_http_connection *conn = (struct rspamd_http_connection *)parser->data; + struct rspamd_http_connection *conn = + (struct rspamd_http_connection *)parser->data; struct rspamd_http_connection_private *priv; priv = conn->priv; @@ -393,9 +394,10 @@ rspamd_http_on_url (http_parser* parser, const gchar *at, size_t length) } static gint -rspamd_http_on_status (http_parser* parser, const gchar *at, size_t length) +rspamd_http_on_status (http_parser * parser, const gchar *at, size_t length) { - struct rspamd_http_connection *conn = (struct rspamd_http_connection *)parser->data; + struct rspamd_http_connection *conn = + (struct rspamd_http_connection *)parser->data; struct rspamd_http_connection_private *priv; priv = conn->priv; @@ -411,9 +413,12 @@ rspamd_http_on_status (http_parser* parser, const gchar *at, size_t length) } static gint -rspamd_http_on_header_field (http_parser* parser, const gchar *at, size_t length) +rspamd_http_on_header_field (http_parser * parser, + const gchar *at, + size_t length) { - struct rspamd_http_connection *conn = (struct rspamd_http_connection *)parser->data; + struct rspamd_http_connection *conn = + (struct rspamd_http_connection *)parser->data; struct rspamd_http_connection_private *priv; priv = conn->priv; @@ -438,9 +443,12 @@ rspamd_http_on_header_field (http_parser* parser, const gchar *at, size_t length } static gint -rspamd_http_on_header_value (http_parser* parser, const gchar *at, size_t length) +rspamd_http_on_header_value (http_parser * parser, + const gchar *at, + size_t length) { - struct rspamd_http_connection *conn = (struct rspamd_http_connection *)parser->data; + struct rspamd_http_connection *conn = + (struct rspamd_http_connection *)parser->data; struct rspamd_http_connection_private *priv; priv = conn->priv; @@ -457,9 +465,10 @@ rspamd_http_on_header_value (http_parser* parser, const gchar *at, size_t length } static int -rspamd_http_on_headers_complete (http_parser* parser) +rspamd_http_on_headers_complete (http_parser * parser) { - struct rspamd_http_connection *conn = (struct rspamd_http_connection *)parser->data; + struct rspamd_http_connection *conn = + (struct rspamd_http_connection *)parser->data; struct rspamd_http_connection_private *priv; priv = conn->priv; @@ -484,9 +493,10 @@ rspamd_http_on_headers_complete (http_parser* parser) } static int -rspamd_http_on_body (http_parser* parser, const gchar *at, size_t length) +rspamd_http_on_body (http_parser * parser, const gchar *at, size_t length) { - struct rspamd_http_connection *conn = (struct rspamd_http_connection *)parser->data; + struct rspamd_http_connection *conn = + (struct rspamd_http_connection *)parser->data; struct rspamd_http_connection_private *priv; priv = conn->priv; @@ -501,9 +511,10 @@ rspamd_http_on_body (http_parser* parser, const gchar *at, size_t length) } static int -rspamd_http_on_message_complete (http_parser* parser) +rspamd_http_on_message_complete (http_parser * parser) { - struct rspamd_http_connection *conn = (struct rspamd_http_connection *)parser->data; + struct rspamd_http_connection *conn = + (struct rspamd_http_connection *)parser->data; struct rspamd_http_connection_private *priv; int ret = 0; @@ -515,7 +526,10 @@ rspamd_http_on_message_complete (http_parser* parser) ret = conn->body_handler (conn, priv->msg, NULL, 0); } else { - ret = conn->body_handler (conn, priv->msg, priv->msg->body->str, priv->msg->body->len); + ret = conn->body_handler (conn, + priv->msg, + priv->msg->body->str, + priv->msg->body->len); } rspamd_http_connection_unref (conn); } @@ -557,13 +571,13 @@ rspamd_http_write_helper (struct rspamd_http_connection *conn) start = &priv->out[0]; niov = priv->outlen; remain = priv->wr_pos; - for (i = 0; i < priv->outlen && remain > 0; i ++) { + for (i = 0; i < priv->outlen && remain > 0; i++) { /* Find out the first iov required */ start = &priv->out[i]; if (start->iov_len <= remain) { remain -= start->iov_len; start = &priv->out[i + 1]; - niov --; + niov--; } else { start->iov_base = (void *)((char *)start->iov_base + remain); @@ -575,7 +589,9 @@ rspamd_http_write_helper (struct rspamd_http_connection *conn) r = writev (conn->fd, start, MIN (IOV_MAX, niov)); if (r == -1) { - err = g_error_new (HTTP_ERROR, errno, "IO write error: %s", strerror (errno)); + err = + g_error_new (HTTP_ERROR, errno, "IO write error: %s", strerror ( + errno)); rspamd_http_connection_ref (conn); conn->error_handler (conn, err); rspamd_http_connection_unref (conn); @@ -613,7 +629,10 @@ rspamd_http_event_handler (int fd, short what, gpointer ud) if (what == EV_READ) { r = read (fd, buf->str, buf->allocated_len); if (r == -1) { - err = g_error_new (HTTP_ERROR, errno, "IO read error: %s", strerror (errno)); + err = g_error_new (HTTP_ERROR, + errno, + "IO read error: %s", + strerror (errno)); conn->error_handler (conn, err); g_error_free (err); return; @@ -624,7 +643,9 @@ rspamd_http_event_handler (int fd, short what, gpointer ud) return; } else { - err = g_error_new (HTTP_ERROR, errno, "IO read error: unexpected EOF"); + err = g_error_new (HTTP_ERROR, + errno, + "IO read error: unexpected EOF"); conn->error_handler (conn, err); g_error_free (err); return; @@ -633,9 +654,11 @@ rspamd_http_event_handler (int fd, short what, gpointer ud) else { buf->len = r; rspamd_http_connection_ref (conn); - if (http_parser_execute (&priv->parser, &priv->parser_cb, buf->str, r) != (size_t)r) { + if (http_parser_execute (&priv->parser, &priv->parser_cb, buf->str, + r) != (size_t)r) { err = g_error_new (HTTP_ERROR, priv->parser.http_errno, - "HTTP parser error: %s", http_errno_description (priv->parser.http_errno)); + "HTTP parser error: %s", + http_errno_description (priv->parser.http_errno)); conn->error_handler (conn, err); g_error_free (err); rspamd_http_connection_unref (conn); @@ -658,12 +681,12 @@ rspamd_http_event_handler (int fd, short what, gpointer ud) } } -struct rspamd_http_connection* +struct rspamd_http_connection * rspamd_http_connection_new (rspamd_http_body_handler_t body_handler, - rspamd_http_error_handler_t error_handler, - rspamd_http_finish_handler_t finish_handler, - enum rspamd_http_options opts, - enum rspamd_http_connection_type type) + rspamd_http_error_handler_t error_handler, + rspamd_http_finish_handler_t finish_handler, + enum rspamd_http_options opts, + enum rspamd_http_connection_type type) { struct rspamd_http_connection *new; struct rspamd_http_connection_private *priv; @@ -684,7 +707,8 @@ rspamd_http_connection_new (rspamd_http_body_handler_t body_handler, /* Init priv */ priv = g_slice_alloc0 (sizeof (struct rspamd_http_connection_private)); - http_parser_init (&priv->parser, type == RSPAMD_HTTP_SERVER ? HTTP_REQUEST : HTTP_RESPONSE); + http_parser_init (&priv->parser, + type == RSPAMD_HTTP_SERVER ? HTTP_REQUEST : HTTP_RESPONSE); priv->parser.data = new; priv->parser_cb.on_url = rspamd_http_on_url; priv->parser_cb.on_status = rspamd_http_on_status; @@ -734,19 +758,20 @@ rspamd_http_connection_free (struct rspamd_http_connection *conn) priv = conn->priv; rspamd_http_connection_reset (conn); g_slice_free1 (sizeof (struct rspamd_http_connection_private), priv); - g_slice_free1 (sizeof (struct rspamd_http_connection), conn); + g_slice_free1 (sizeof (struct rspamd_http_connection), conn); } void rspamd_http_connection_read_message (struct rspamd_http_connection *conn, - gpointer ud, gint fd, struct timeval *timeout, struct event_base *base) + gpointer ud, gint fd, struct timeval *timeout, struct event_base *base) { struct rspamd_http_connection_private *priv = conn->priv; struct rspamd_http_message *req; conn->fd = fd; conn->ud = ud; - req = rspamd_http_new_message (conn->type == RSPAMD_HTTP_SERVER ? HTTP_REQUEST : HTTP_RESPONSE); + req = rspamd_http_new_message ( + conn->type == RSPAMD_HTTP_SERVER ? HTTP_REQUEST : HTTP_RESPONSE); priv->msg = req; if (timeout == NULL) { @@ -760,15 +785,19 @@ rspamd_http_connection_read_message (struct rspamd_http_connection *conn, priv->buf = g_string_sized_new (BUFSIZ); priv->new_header = TRUE; - event_set (&priv->ev, fd, EV_READ | EV_PERSIST, rspamd_http_event_handler, conn); + event_set (&priv->ev, + fd, + EV_READ | EV_PERSIST, + rspamd_http_event_handler, + conn); event_base_set (base, &priv->ev); event_add (&priv->ev, priv->ptv); } void rspamd_http_connection_write_message (struct rspamd_http_connection *conn, - struct rspamd_http_message *msg, const gchar *host, const gchar *mime_type, - gpointer ud, gint fd, struct timeval *timeout, struct event_base *base) + struct rspamd_http_message *msg, const gchar *host, const gchar *mime_type, + gpointer ud, gint fd, struct timeval *timeout, struct event_base *base) { struct rspamd_http_connection_private *priv = conn->priv; struct rspamd_http_header *hdr; @@ -820,29 +849,32 @@ rspamd_http_connection_write_message (struct rspamd_http_connection *conn, if (msg->method < HTTP_SYMBOLS) { ptm = gmtime (&msg->date); t = *ptm; - rspamd_snprintf (datebuf, sizeof (datebuf), "%s, %02d %s %4d %02d:%02d:%02d GMT", - http_week[t.tm_wday], - t.tm_mday, - http_month[t.tm_mon], - t.tm_year + 1900, - t.tm_hour, - t.tm_min, - t.tm_sec); + rspamd_snprintf (datebuf, + sizeof (datebuf), + "%s, %02d %s %4d %02d:%02d:%02d GMT", + http_week[t.tm_wday], + t.tm_mday, + http_month[t.tm_mon], + t.tm_year + 1900, + t.tm_hour, + t.tm_min, + t.tm_sec); if (mime_type == NULL) { mime_type = "text/plain"; } rspamd_printf_gstring (priv->buf, "HTTP/1.1 %d %s\r\n" - "Connection: close\r\n" - "Server: %s\r\n" - "Date: %s\r\n" - "Content-Length: %z\r\n" - "Content-Type: %s\r\n", - msg->code, - msg->status ? msg->status->str : rspamd_http_code_to_str (msg->code), - "rspamd/" RVERSION, - datebuf, - bodylen, - mime_type); + "Connection: close\r\n" + "Server: %s\r\n" + "Date: %s\r\n" + "Content-Length: %z\r\n" + "Content-Type: %s\r\n", + msg->code, + msg->status ? msg->status->str : rspamd_http_code_to_str (msg-> + code), + "rspamd/" RVERSION, + datebuf, + bodylen, + mime_type); } else { /* Legacy spamd reply */ @@ -853,21 +885,22 @@ rspamd_http_connection_write_message (struct rspamd_http_connection *conn, /* Format request */ if (host != NULL) { rspamd_printf_gstring (priv->buf, "%s %v HTTP/1.1\r\n" - "Connection: close\r\n" - "Host: %s\r\n" - "Content-Length: %z\r\n", + "Connection: close\r\n" + "Host: %s\r\n" + "Content-Length: %z\r\n", http_method_str (msg->method), msg->url, host, bodylen); } else { /* Fallback to HTTP/1.0 */ rspamd_printf_gstring (priv->buf, "%s %v HTTP/1.0\r\n" - "Content-Length: %z\r\n", + "Content-Length: %z\r\n", http_method_str (msg->method), msg->url, bodylen); } } /* Allocate iov */ priv->wr_total = bodylen + priv->buf->len + 2; - DL_FOREACH (msg->headers, hdr) { + DL_FOREACH (msg->headers, hdr) + { /* <name><: ><value><\r\n> */ priv->wr_total += hdr->name->len + hdr->value->len + 4; priv->outlen += 4; @@ -879,7 +912,8 @@ rspamd_http_connection_write_message (struct rspamd_http_connection *conn, priv->out[0].iov_base = priv->buf->str; priv->out[0].iov_len = priv->buf->len; i = 1; - LL_FOREACH (msg->headers, hdr) { + LL_FOREACH (msg->headers, hdr) + { priv->out[i].iov_base = hdr->name->str; priv->out[i++].iov_len = hdr->name->len; priv->out[i].iov_base = ": "; @@ -907,7 +941,7 @@ rspamd_http_connection_write_message (struct rspamd_http_connection *conn, event_add (&priv->ev, priv->ptv); } -struct rspamd_http_message* +struct rspamd_http_message * rspamd_http_new_message (enum http_parser_type type) { struct rspamd_http_message *new; @@ -935,8 +969,9 @@ rspamd_http_message_free (struct rspamd_http_message *msg) { struct rspamd_http_header *hdr, *tmp_hdr; - LL_FOREACH_SAFE (msg->headers, hdr, tmp_hdr) { - g_string_free (hdr->name, TRUE); + LL_FOREACH_SAFE (msg->headers, hdr, tmp_hdr) + { + g_string_free (hdr->name, TRUE); g_string_free (hdr->value, TRUE); g_slice_free1 (sizeof (struct rspamd_http_header), hdr); } @@ -952,9 +987,10 @@ rspamd_http_message_free (struct rspamd_http_message *msg) g_slice_free1 (sizeof (struct rspamd_http_message), msg); } -void rspamd_http_message_add_header (struct rspamd_http_message *msg, - const gchar *name, - const gchar *value) +void +rspamd_http_message_add_header (struct rspamd_http_message *msg, + const gchar *name, + const gchar *value) { struct rspamd_http_header *hdr; @@ -966,15 +1002,17 @@ void rspamd_http_message_add_header (struct rspamd_http_message *msg, } } -const gchar* -rspamd_http_message_find_header (struct rspamd_http_message *msg, const gchar *name) +const gchar * +rspamd_http_message_find_header (struct rspamd_http_message *msg, + const gchar *name) { struct rspamd_http_header *hdr; const gchar *res = NULL; guint slen = strlen (name); if (msg != NULL) { - LL_FOREACH (msg->headers, hdr) { + LL_FOREACH (msg->headers, hdr) + { if (hdr->name->len == slen) { if (memcmp (hdr->name->str, name, slen) == 0) { res = hdr->value->str; @@ -1005,7 +1043,8 @@ rspamd_http_entry_free (struct rspamd_http_connection_entry *entry) } static void -rspamd_http_router_error_handler (struct rspamd_http_connection *conn, GError *err) +rspamd_http_router_error_handler (struct rspamd_http_connection *conn, + GError *err) { struct rspamd_http_connection_entry *entry = conn->ud; struct rspamd_http_message *msg; @@ -1027,8 +1066,14 @@ rspamd_http_router_error_handler (struct rspamd_http_connection *conn, GError *e msg->code = err->code; msg->body = g_string_new (err->message); rspamd_http_connection_reset (entry->conn); - rspamd_http_connection_write_message (entry->conn, msg, NULL, - "text/plain", entry, entry->conn->fd, entry->rt->ptv, entry->rt->ev_base); + rspamd_http_connection_write_message (entry->conn, + msg, + NULL, + "text/plain", + entry, + entry->conn->fd, + entry->rt->ptv, + entry->rt->ev_base); entry->is_reply = TRUE; } } @@ -1043,9 +1088,9 @@ rspamd_http_router_detect_ct (const gchar *path) if (dot == NULL) { return http_file_types[HTTP_MAGIC_PLAIN].ct; } - dot ++; + dot++; - for (i = 0; i < G_N_ELEMENTS (http_file_types); i ++) { + for (i = 0; i < G_N_ELEMENTS (http_file_types); i++) { if (strcmp (http_file_types[i].ext, dot) == 0) { return http_file_types[i].ct; } @@ -1065,11 +1110,11 @@ rspamd_http_router_is_subdir (const gchar *parent, const gchar *sub) if (*sub != *parent) { return FALSE; } - parent ++; - sub ++; + parent++; + sub++; } - parent --; + parent--; if (*parent == G_DIR_SEPARATOR) { return TRUE; } @@ -1079,7 +1124,7 @@ rspamd_http_router_is_subdir (const gchar *parent, const gchar *sub) static gboolean rspamd_http_router_try_file (struct rspamd_http_connection_entry *entry, - struct rspamd_http_message *msg, gboolean expand_path) + struct rspamd_http_message *msg, gboolean expand_path) { struct stat st; gint fd; @@ -1087,17 +1132,17 @@ rspamd_http_router_try_file (struct rspamd_http_connection_entry *entry, struct rspamd_http_message *reply_msg; rspamd_snprintf (filebuf, sizeof (filebuf), "%s%c%v", - entry->rt->default_fs_path, G_DIR_SEPARATOR, msg->url); + entry->rt->default_fs_path, G_DIR_SEPARATOR, msg->url); if (realpath (filebuf, realbuf) == NULL || - lstat (realbuf, &st) == -1) { + lstat (realbuf, &st) == -1) { return FALSE; } if (S_ISDIR (st.st_mode) && expand_path) { /* Try to append 'index.html' to the url */ g_string_append_printf (msg->url, "%c%s", G_DIR_SEPARATOR, - "index.html"); + "index.html"); return rspamd_http_router_try_file (entry, msg, FALSE); } else if (!S_ISREG (st.st_mode)) { @@ -1107,8 +1152,9 @@ rspamd_http_router_try_file (struct rspamd_http_connection_entry *entry, /* We also need to ensure that file is inside the defined dir */ rspamd_strlcpy (filebuf, realbuf, sizeof (filebuf)); dir = dirname (filebuf); - if (dir == NULL || !rspamd_http_router_is_subdir (entry->rt->default_fs_path, - dir)) { + if (dir == NULL || + !rspamd_http_router_is_subdir (entry->rt->default_fs_path, + dir)) { return FALSE; } @@ -1136,15 +1182,15 @@ rspamd_http_router_try_file (struct rspamd_http_connection_entry *entry, /* XXX: detect content type */ rspamd_http_connection_write_message (entry->conn, reply_msg, NULL, - rspamd_http_router_detect_ct (realbuf), entry, entry->conn->fd, - entry->rt->ptv, entry->rt->ev_base); + rspamd_http_router_detect_ct (realbuf), entry, entry->conn->fd, + entry->rt->ptv, entry->rt->ev_base); return TRUE; } static int rspamd_http_router_finish_handler (struct rspamd_http_connection *conn, - struct rspamd_http_message *msg) + struct rspamd_http_message *msg) { struct rspamd_http_connection_entry *entry = conn->ud; rspamd_http_router_handler_t handler = NULL; @@ -1152,7 +1198,8 @@ rspamd_http_router_finish_handler (struct rspamd_http_connection *conn, struct rspamd_http_message *err_msg; GError *err; - G_STATIC_ASSERT (sizeof (rspamd_http_router_handler_t) == sizeof (gpointer)); + G_STATIC_ASSERT (sizeof (rspamd_http_router_handler_t) == + sizeof (gpointer)); if (entry->is_reply) { /* Request is finished, it is safe to free a connection */ @@ -1170,7 +1217,7 @@ rspamd_http_router_finish_handler (struct rspamd_http_connection *conn, } else { if (entry->rt->default_fs_path == NULL || - !rspamd_http_router_try_file (entry, msg, TRUE)) { + !rspamd_http_router_try_file (entry, msg, TRUE)) { err = g_error_new (HTTP_ERROR, 404, "Not found"); if (entry->rt->error_handler != NULL) { @@ -1181,9 +1228,14 @@ rspamd_http_router_finish_handler (struct rspamd_http_connection *conn, err_msg->code = err->code; err_msg->body = g_string_new (err->message); rspamd_http_connection_reset (entry->conn); - rspamd_http_connection_write_message (entry->conn, err_msg, NULL, - "text/plain", entry, entry->conn->fd, - entry->rt->ptv, entry->rt->ev_base); + rspamd_http_connection_write_message (entry->conn, + err_msg, + NULL, + "text/plain", + entry, + entry->conn->fd, + entry->rt->ptv, + entry->rt->ev_base); g_error_free (err); } } @@ -1192,13 +1244,13 @@ rspamd_http_router_finish_handler (struct rspamd_http_connection *conn, return 0; } -struct rspamd_http_connection_router* +struct rspamd_http_connection_router * rspamd_http_router_new (rspamd_http_router_error_handler_t eh, - rspamd_http_router_finish_handler_t fh, - struct timeval *timeout, struct event_base *base, - const char *default_fs_path) + rspamd_http_router_finish_handler_t fh, + struct timeval *timeout, struct event_base *base, + const char *default_fs_path) { - struct rspamd_http_connection_router* new; + struct rspamd_http_connection_router * new; struct stat st; new = g_slice_alloc (sizeof (struct rspamd_http_connection_router)); @@ -1235,10 +1287,11 @@ rspamd_http_router_new (rspamd_http_router_error_handler_t eh, void rspamd_http_router_add_path (struct rspamd_http_connection_router *router, - const gchar *path, rspamd_http_router_handler_t handler) + const gchar *path, rspamd_http_router_handler_t handler) { gpointer ptr; - G_STATIC_ASSERT (sizeof (rspamd_http_router_handler_t) == sizeof (gpointer)); + G_STATIC_ASSERT (sizeof (rspamd_http_router_handler_t) == + sizeof (gpointer)); if (path != NULL && handler != NULL && router != NULL) { memcpy (&ptr, &handler, sizeof (ptr)); @@ -1248,7 +1301,7 @@ rspamd_http_router_add_path (struct rspamd_http_connection_router *router, void rspamd_http_router_handle_socket (struct rspamd_http_connection_router *router, - gint fd, gpointer ud) + gint fd, gpointer ud) { struct rspamd_http_connection_entry *conn; @@ -1257,11 +1310,14 @@ rspamd_http_router_handle_socket (struct rspamd_http_connection_router *router, conn->ud = ud; conn->is_reply = FALSE; - conn->conn = rspamd_http_connection_new (NULL, rspamd_http_router_error_handler, - rspamd_http_router_finish_handler, 0, RSPAMD_HTTP_SERVER); + conn->conn = rspamd_http_connection_new (NULL, + rspamd_http_router_error_handler, + rspamd_http_router_finish_handler, + 0, + RSPAMD_HTTP_SERVER); rspamd_http_connection_read_message (conn->conn, conn, fd, router->ptv, - router->ev_base); + router->ev_base); LL_PREPEND (router->conns, conn); } @@ -1271,7 +1327,8 @@ rspamd_http_router_free (struct rspamd_http_connection_router *router) struct rspamd_http_connection_entry *conn, *tmp; if (router) { - LL_FOREACH_SAFE (router->conns, conn, tmp) { + LL_FOREACH_SAFE (router->conns, conn, tmp) + { rspamd_http_entry_free (conn); } diff --git a/src/libutil/http.h b/src/libutil/http.h index 695f23896..c6a142e3b 100644 --- a/src/libutil/http.h +++ b/src/libutil/http.h @@ -67,7 +67,7 @@ struct rspamd_http_message { * Options for HTTP connection */ enum rspamd_http_options { - RSPAMD_HTTP_BODY_PARTIAL = 0x1//!< RSPAMD_HTTP_BODY_PARTIAL + RSPAMD_HTTP_BODY_PARTIAL = 0x1 //!< RSPAMD_HTTP_BODY_PARTIAL }; struct rspamd_http_connection_private; @@ -76,20 +76,24 @@ struct rspamd_http_connection_router; struct rspamd_http_connection_entry; typedef int (*rspamd_http_body_handler_t) (struct rspamd_http_connection *conn, - struct rspamd_http_message *msg, - const gchar *chunk, - gsize len); + struct rspamd_http_message *msg, + const gchar *chunk, + gsize len); -typedef void (*rspamd_http_error_handler_t) (struct rspamd_http_connection *conn, GError *err); +typedef void (*rspamd_http_error_handler_t) (struct rspamd_http_connection *conn, + GError *err); typedef int (*rspamd_http_finish_handler_t) (struct rspamd_http_connection *conn, - struct rspamd_http_message *msg); + struct rspamd_http_message *msg); -typedef int (*rspamd_http_router_handler_t) (struct rspamd_http_connection_entry *conn_ent, - struct rspamd_http_message *msg); -typedef void (*rspamd_http_router_error_handler_t) (struct rspamd_http_connection_entry *conn_ent, - GError *err); -typedef void (*rspamd_http_router_finish_handler_t) (struct rspamd_http_connection_entry *conn_ent); +typedef int (*rspamd_http_router_handler_t) (struct rspamd_http_connection_entry + *conn_ent, + struct rspamd_http_message *msg); +typedef void (*rspamd_http_router_error_handler_t) (struct + rspamd_http_connection_entry *conn_ent, + GError *err); +typedef void (*rspamd_http_router_finish_handler_t) (struct + rspamd_http_connection_entry *conn_ent); /** * HTTP connection structure @@ -132,12 +136,12 @@ struct rspamd_http_connection_router { * @param opts options * @return new connection structure */ -struct rspamd_http_connection* rspamd_http_connection_new ( - rspamd_http_body_handler_t body_handler, - rspamd_http_error_handler_t error_handler, - rspamd_http_finish_handler_t finish_handler, - enum rspamd_http_options opts, - enum rspamd_http_connection_type type); +struct rspamd_http_connection * rspamd_http_connection_new ( + rspamd_http_body_handler_t body_handler, + rspamd_http_error_handler_t error_handler, + rspamd_http_finish_handler_t finish_handler, + enum rspamd_http_options opts, + enum rspamd_http_connection_type type); /** * Handle a request using socket fd and user data ud @@ -146,11 +150,11 @@ struct rspamd_http_connection* rspamd_http_connection_new ( * @param fd fd to read/write */ void rspamd_http_connection_read_message ( - struct rspamd_http_connection *conn, - gpointer ud, - gint fd, - struct timeval *timeout, - struct event_base *base); + struct rspamd_http_connection *conn, + gpointer ud, + gint fd, + struct timeval *timeout, + struct event_base *base); /** * Send reply using initialised connection @@ -160,14 +164,14 @@ void rspamd_http_connection_read_message ( * @param fd fd to read/write */ void rspamd_http_connection_write_message ( - struct rspamd_http_connection *conn, - struct rspamd_http_message *msg, - const gchar *host, - const gchar *mime_type, - gpointer ud, - gint fd, - struct timeval *timeout, - struct event_base *base); + struct rspamd_http_connection *conn, + struct rspamd_http_message *msg, + const gchar *host, + const gchar *mime_type, + gpointer ud, + gint fd, + struct timeval *timeout, + struct event_base *base); /** * Free connection structure @@ -183,7 +187,7 @@ void rspamd_http_connection_free (struct rspamd_http_connection *conn); static inline struct rspamd_http_connection * rspamd_http_connection_ref (struct rspamd_http_connection *conn) { - conn->ref ++; + conn->ref++; return conn; } @@ -210,7 +214,7 @@ void rspamd_http_connection_reset (struct rspamd_http_connection *conn); * @param code code to pass * @return new reply object */ -struct rspamd_http_message* rspamd_http_new_message (enum http_parser_type type); +struct rspamd_http_message * rspamd_http_new_message (enum http_parser_type type); /** * Append a header to reply @@ -218,14 +222,17 @@ struct rspamd_http_message* rspamd_http_new_message (enum http_parser_type type) * @param name * @param value */ -void rspamd_http_message_add_header (struct rspamd_http_message *rep, const gchar *name, const gchar *value); +void rspamd_http_message_add_header (struct rspamd_http_message *rep, + const gchar *name, + const gchar *value); /** * Search for a specified header in message * @param rep message * @param name name of header */ -const gchar* rspamd_http_message_find_header (struct rspamd_http_message *rep, const gchar *name); +const gchar * rspamd_http_message_find_header (struct rspamd_http_message *rep, + const gchar *name); /** * Free HTTP reply @@ -249,18 +256,18 @@ time_t rspamd_http_parse_date (const gchar *header, gsize len); * the specified directory * @return */ -struct rspamd_http_connection_router* rspamd_http_router_new ( - rspamd_http_router_error_handler_t eh, - rspamd_http_router_finish_handler_t fh, - struct timeval *timeout, - struct event_base *base, - const char *default_fs_path); +struct rspamd_http_connection_router * rspamd_http_router_new ( + rspamd_http_router_error_handler_t eh, + rspamd_http_router_finish_handler_t fh, + struct timeval *timeout, + struct event_base *base, + const char *default_fs_path); /** * Add new path to the router */ void rspamd_http_router_add_path (struct rspamd_http_connection_router *router, - const gchar *path, rspamd_http_router_handler_t handler); + const gchar *path, rspamd_http_router_handler_t handler); /** * Handle new accepted socket @@ -268,8 +275,10 @@ void rspamd_http_router_add_path (struct rspamd_http_connection_router *router, * @param fd server socket * @param ud opaque userdata */ -void rspamd_http_router_handle_socket (struct rspamd_http_connection_router *router, - gint fd, gpointer ud); +void rspamd_http_router_handle_socket ( + struct rspamd_http_connection_router *router, + gint fd, + gpointer ud); /** * Free router and all connections associated diff --git a/src/libutil/logger.c b/src/libutil/logger.c index 47e308ecd..a3211078a 100644 --- a/src/libutil/logger.c +++ b/src/libutil/logger.c @@ -25,9 +25,9 @@ #include "config.h" #include "logger.h" -#include "util.h" #include "main.h" #include "map.h" +#include "util.h" /* How much message should be repeated before it is count to be repeated one */ #define REPEATS_MIN 3 @@ -38,29 +38,29 @@ * It is NOT shared between processes and is created by main process */ struct rspamd_logger_s { - rspamd_log_func_t log_func; - struct rspamd_config *cfg; + rspamd_log_func_t log_func; + struct rspamd_config *cfg; struct { - guint32 size; - guint32 used; - u_char *buf; + guint32 size; + guint32 used; + u_char *buf; } io_buf; - gint fd; - gboolean is_buffered; - gboolean enabled; - gboolean is_debug; - gboolean throttling; - time_t throttling_time; - sig_atomic_t do_reopen_log; - enum rspamd_log_type type; - pid_t pid; - GQuark process_type; - radix_tree_t *debug_ip; - guint32 last_line_cksum; - guint32 repeats; - gchar *saved_message; - gchar *saved_function; - GMutex *mtx; + gint fd; + gboolean is_buffered; + gboolean enabled; + gboolean is_debug; + gboolean throttling; + time_t throttling_time; + sig_atomic_t do_reopen_log; + enum rspamd_log_type type; + pid_t pid; + GQuark process_type; + radix_tree_t *debug_ip; + guint32 last_line_cksum; + guint32 repeats; + gchar *saved_message; + gchar *saved_function; + GMutex *mtx; }; static const gchar lf_chr = '\n'; @@ -69,13 +69,13 @@ static rspamd_logger_t *default_logger = NULL; static void -syslog_log_function (const gchar * log_domain, const gchar *function, - GLogLevelFlags log_level, const gchar * message, - gboolean forced, gpointer arg); +syslog_log_function (const gchar * log_domain, const gchar *function, + GLogLevelFlags log_level, const gchar * message, + gboolean forced, gpointer arg); static void -file_log_function (const gchar * log_domain, const gchar *function, - GLogLevelFlags log_level, const gchar * message, - gboolean forced, gpointer arg); +file_log_function (const gchar * log_domain, const gchar *function, + GLogLevelFlags log_level, const gchar * message, + gboolean forced, gpointer arg); /** * Calculate checksum for log line (used for repeating logic) @@ -83,31 +83,36 @@ file_log_function (const gchar * log_domain, const gchar *function, static inline guint32 rspamd_log_calculate_cksum (const gchar *message, size_t mlen) { - const gchar *bp = message; - const gchar *be = bp + mlen; - guint32 hval = 0; - - while (bp < be) { - hval += (hval<<1) + (hval<<4) + (hval<<7) + (hval<<8) + (hval<<24); - hval ^= (guint32)*bp++; - } - - /* return our new hash value */ - return hval; - + const gchar *bp = message; + const gchar *be = bp + mlen; + guint32 hval = 0; + + while (bp < be) { + hval += + (hval << + 1) + (hval << 4) + (hval << 7) + (hval << 8) + (hval << 24); + hval ^= (guint32) * bp++; + } + + /* return our new hash value */ + return hval; + } /* * Write a line to log file (unbuffered) */ static void -direct_write_log_line (rspamd_logger_t *rspamd_log, void *data, gint count, gboolean is_iov) +direct_write_log_line (rspamd_logger_t *rspamd_log, + void *data, + gint count, + gboolean is_iov) { - gchar errmsg[128]; - struct iovec *iov; - const gchar *line; - gint r; - + gchar errmsg[128]; + struct iovec *iov; + const gchar *line; + gint r; + if (rspamd_log->enabled) { if (is_iov) { iov = (struct iovec *)data; @@ -119,7 +124,10 @@ direct_write_log_line (rspamd_logger_t *rspamd_log, void *data, gint count, gboo } if (r == -1) { /* We cannot write message to file, so we need to detect error and make decision */ - r = rspamd_snprintf (errmsg, sizeof (errmsg), "direct_write_log_line: cannot write log line: %s", strerror (errno)); + r = rspamd_snprintf (errmsg, + sizeof (errmsg), + "direct_write_log_line: cannot write log line: %s", + strerror (errno)); if (errno == EIO || errno == EINTR) { /* Descriptor is somehow invalid, try to restart */ reopen_log (rspamd_log); @@ -128,7 +136,8 @@ direct_write_log_line (rspamd_logger_t *rspamd_log, void *data, gint count, gboo direct_write_log_line (rspamd_log, data, count, is_iov); } } - else if (errno == EFAULT || errno == EINVAL || errno == EFBIG || errno == ENOSPC) { + else if (errno == EFAULT || errno == EINVAL || errno == EFBIG || + errno == ENOSPC) { /* Rare case */ rspamd_log->throttling = TRUE; rspamd_log->throttling_time = time (NULL); @@ -156,7 +165,7 @@ rspamd_escape_log_string (gchar *str) else if (*p == '\n' || *p == '\r') { *p = ' '; } - p ++; + p++; } } @@ -174,16 +183,17 @@ open_log_priv (rspamd_logger_t *rspamd_log, uid_t uid, gid_t gid) rspamd_log->enabled = TRUE; return 0; case RSPAMD_LOG_FILE: - rspamd_log->fd = open (rspamd_log->cfg->log_file, O_CREAT | O_WRONLY | O_APPEND, + rspamd_log->fd = open (rspamd_log->cfg->log_file, + O_CREAT | O_WRONLY | O_APPEND, S_IWUSR | S_IRUSR | S_IRGRP | S_IROTH); if (rspamd_log->fd == -1) { fprintf (stderr, "open_log: cannot open desired log file: %s, %s", - rspamd_log->cfg->log_file, strerror (errno)); + rspamd_log->cfg->log_file, strerror (errno)); return -1; } if (fchown (rspamd_log->fd, uid, gid) == -1) { fprintf (stderr, "open_log: cannot chown desired log file: %s, %s", - rspamd_log->cfg->log_file, strerror (errno)); + rspamd_log->cfg->log_file, strerror (errno)); close (rspamd_log->fd); return -1; } @@ -196,7 +206,7 @@ open_log_priv (rspamd_logger_t *rspamd_log, uid_t uid, gid_t gid) void close_log_priv (rspamd_logger_t *rspamd_log, uid_t uid, gid_t gid) { - gchar tmpbuf[256]; + gchar tmpbuf[256]; flush_log_buf (rspamd_log); switch (rspamd_log->type) { @@ -209,17 +219,30 @@ close_log_priv (rspamd_logger_t *rspamd_log, uid_t uid, gid_t gid) case RSPAMD_LOG_FILE: if (rspamd_log->enabled) { if (rspamd_log->repeats > REPEATS_MIN) { - rspamd_snprintf (tmpbuf, sizeof (tmpbuf), "Last message repeated %ud times", rspamd_log->repeats); + rspamd_snprintf (tmpbuf, + sizeof (tmpbuf), + "Last message repeated %ud times", + rspamd_log->repeats); rspamd_log->repeats = 0; if (rspamd_log->saved_message) { - file_log_function (NULL, rspamd_log->saved_function, rspamd_log->cfg->log_level, rspamd_log->saved_message, TRUE, rspamd_log); + file_log_function (NULL, + rspamd_log->saved_function, + rspamd_log->cfg->log_level, + rspamd_log->saved_message, + TRUE, + rspamd_log); g_free (rspamd_log->saved_message); g_free (rspamd_log->saved_function); rspamd_log->saved_message = NULL; rspamd_log->saved_function = NULL; - } + } /* It is safe to use temporary buffer here as it is not static */ - file_log_function (NULL, __FUNCTION__, rspamd_log->cfg->log_level, tmpbuf, TRUE, rspamd_log); + file_log_function (NULL, + __FUNCTION__, + rspamd_log->cfg->log_level, + tmpbuf, + TRUE, + rspamd_log); return; } @@ -275,12 +298,14 @@ reopen_log (rspamd_logger_t *logger) * Setup logger */ void -rspamd_set_logger (struct rspamd_config *cfg, GQuark ptype, struct rspamd_main *rspamd) +rspamd_set_logger (struct rspamd_config *cfg, + GQuark ptype, + struct rspamd_main *rspamd) { - gchar **strvec, *p, *err; - gint num, i, k; - struct in_addr addr; - guint32 mask = 0xFFFFFFFF; + gchar **strvec, *p, *err; + gint num, i, k; + struct in_addr addr; + guint32 mask = 0xFFFFFFFF; if (rspamd->logger == NULL) { rspamd->logger = g_malloc (sizeof (rspamd_logger_t)); @@ -299,16 +324,16 @@ rspamd_set_logger (struct rspamd_config *cfg, GQuark ptype, struct rspamd_main * #endif switch (cfg->log_type) { - case RSPAMD_LOG_CONSOLE: - rspamd->logger->log_func = file_log_function; - rspamd->logger->fd = STDERR_FILENO; - break; - case RSPAMD_LOG_SYSLOG: - rspamd->logger->log_func = syslog_log_function; - break; - case RSPAMD_LOG_FILE: - rspamd->logger->log_func = file_log_function; - break; + case RSPAMD_LOG_CONSOLE: + rspamd->logger->log_func = file_log_function; + rspamd->logger->fd = STDERR_FILENO; + break; + case RSPAMD_LOG_SYSLOG: + rspamd->logger->log_func = syslog_log_function; + break; + case RSPAMD_LOG_FILE: + rspamd->logger->log_func = file_log_function; + break; } rspamd->logger->cfg = cfg; @@ -330,8 +355,10 @@ rspamd_set_logger (struct rspamd_config *cfg, GQuark ptype, struct rspamd_main * radix_tree_free (rspamd->logger->debug_ip); } rspamd->logger->debug_ip = radix_tree_create (); - if (!add_map (rspamd->cfg, rspamd->cfg->debug_ip_map, "IP addresses for which debug logs are enabled", - read_radix_list, fin_radix_list, (void **)&rspamd->logger->debug_ip)) { + if (!add_map (rspamd->cfg, rspamd->cfg->debug_ip_map, + "IP addresses for which debug logs are enabled", + read_radix_list, fin_radix_list, + (void **)&rspamd->logger->debug_ip)) { /* Try to parse it as list */ strvec = g_strsplit_set (rspamd->cfg->debug_ip_map, ",; ", 0); num = g_strv_length (strvec); @@ -342,7 +369,7 @@ rspamd_set_logger (struct rspamd_config *cfg, GQuark ptype, struct rspamd_main * if ((p = strchr (strvec[i], '/')) != NULL) { /* Try to extract mask */ *p = '\0'; - p ++; + p++; errno = 0; k = strtoul (p, &err, 10); if (errno != 0 || *err != '\0' || k > 32) { @@ -355,7 +382,8 @@ rspamd_set_logger (struct rspamd_config *cfg, GQuark ptype, struct rspamd_main * if (inet_aton (strvec[i], &addr)) { /* Check ip */ mask = mask << (32 - k); - radix32tree_insert (rspamd->logger->debug_ip, ntohl (addr.s_addr), mask, 1); + radix32tree_insert (rspamd->logger->debug_ip, + ntohl (addr.s_addr), mask, 1); } } g_strfreev (strvec); @@ -385,19 +413,27 @@ update_log_pid (GQuark ptype, rspamd_logger_t *rspamd_log) void flush_log_buf (rspamd_logger_t *rspamd_log) { - if (rspamd_log->is_buffered && (rspamd_log->type == RSPAMD_LOG_CONSOLE || rspamd_log->type == RSPAMD_LOG_FILE)) { - direct_write_log_line (rspamd_log, rspamd_log->io_buf.buf, rspamd_log->io_buf.used, FALSE); + if (rspamd_log->is_buffered && + (rspamd_log->type == RSPAMD_LOG_CONSOLE || rspamd_log->type == + RSPAMD_LOG_FILE)) { + direct_write_log_line (rspamd_log, + rspamd_log->io_buf.buf, + rspamd_log->io_buf.used, + FALSE); rspamd_log->io_buf.used = 0; } } void -rspamd_common_logv (rspamd_logger_t *rspamd_log, GLogLevelFlags log_level, const gchar *function, - const gchar *fmt, va_list args) +rspamd_common_logv (rspamd_logger_t *rspamd_log, + GLogLevelFlags log_level, + const gchar *function, + const gchar *fmt, + va_list args) { - static gchar logbuf[BUFSIZ]; - u_char *end; + static gchar logbuf[BUFSIZ]; + u_char *end; if (rspamd_log == NULL) { rspamd_log = default_logger; @@ -417,7 +453,12 @@ rspamd_common_logv (rspamd_logger_t *rspamd_log, GLogLevelFlags log_level, const end = rspamd_vsnprintf (logbuf, sizeof (logbuf), fmt, args); *end = '\0'; rspamd_escape_log_string (logbuf); - rspamd_log->log_func (NULL, function, log_level, logbuf, FALSE, rspamd_log); + rspamd_log->log_func (NULL, + function, + log_level, + logbuf, + FALSE, + rspamd_log); g_mutex_unlock (rspamd_log->mtx); } } @@ -426,10 +467,13 @@ rspamd_common_logv (rspamd_logger_t *rspamd_log, GLogLevelFlags log_level, const * This log functions select real logger and write message if level is less or equal to configured log level */ void -rspamd_common_log_function (rspamd_logger_t *rspamd_log, GLogLevelFlags log_level, - const gchar *function, const gchar *fmt, ...) +rspamd_common_log_function (rspamd_logger_t *rspamd_log, + GLogLevelFlags log_level, + const gchar *function, + const gchar *fmt, + ...) { - va_list vp; + va_list vp; va_start (vp, fmt); rspamd_common_logv (rspamd_log, log_level, function, fmt, vp); @@ -438,17 +482,17 @@ rspamd_common_log_function (rspamd_logger_t *rspamd_log, GLogLevelFlags log_leve void rspamd_default_logv (GLogLevelFlags log_level, const gchar *function, - const gchar *fmt, va_list args) + const gchar *fmt, va_list args) { rspamd_common_logv (NULL, log_level, function, fmt, args); } void rspamd_default_log_function (GLogLevelFlags log_level, - const gchar *function, const gchar *fmt, ...) + const gchar *function, const gchar *fmt, ...) { - va_list vp; + va_list vp; va_start (vp, fmt); rspamd_default_logv (log_level, function, fmt, vp); @@ -462,10 +506,12 @@ rspamd_default_log_function (GLogLevelFlags log_level, static void fill_buffer (rspamd_logger_t *rspamd_log, const struct iovec *iov, gint iovcnt) { - gint i; + gint i; - for (i = 0; i < iovcnt; i ++) { - memcpy (rspamd_log->io_buf.buf + rspamd_log->io_buf.used, iov[i].iov_base, iov[i].iov_len); + for (i = 0; i < iovcnt; i++) { + memcpy (rspamd_log->io_buf.buf + rspamd_log->io_buf.used, + iov[i].iov_base, + iov[i].iov_len); rspamd_log->io_buf.used += iov[i].iov_len; } @@ -475,18 +521,20 @@ fill_buffer (rspamd_logger_t *rspamd_log, const struct iovec *iov, gint iovcnt) * Write message to buffer or to file (using direct_write_log_line function) */ static void -file_log_helper (rspamd_logger_t *rspamd_log, const struct iovec *iov, gint iovcnt) +file_log_helper (rspamd_logger_t *rspamd_log, + const struct iovec *iov, + gint iovcnt) { - size_t len = 0; - gint i; + size_t len = 0; + gint i; - if (! rspamd_log->is_buffered) { + if (!rspamd_log->is_buffered) { /* Write string directly */ direct_write_log_line (rspamd_log, (void *)iov, iovcnt, TRUE); } else { /* Calculate total length */ - for (i = 0; i < iovcnt; i ++) { + for (i = 0; i < iovcnt; i++) { len += iov[i].iov_len; } /* Fill buffer */ @@ -511,11 +559,16 @@ file_log_helper (rspamd_logger_t *rspamd_log, const struct iovec *iov, gint iovc * Syslog interface for logging */ static void -syslog_log_function (const gchar * log_domain, const gchar *function, GLogLevelFlags log_level, const gchar * message, gboolean forced, gpointer arg) +syslog_log_function (const gchar * log_domain, + const gchar *function, + GLogLevelFlags log_level, + const gchar * message, + gboolean forced, + gpointer arg) { - rspamd_logger_t *rspamd_log = arg; + rspamd_logger_t *rspamd_log = arg; - if (! rspamd_log->enabled) { + if (!rspamd_log->enabled) { return; } if (function == NULL) { @@ -556,20 +609,25 @@ syslog_log_function (const gchar * log_domain, const gchar *function, GLogLevelF * Main file interface for logging */ static void -file_log_function (const gchar * log_domain, const gchar *function, GLogLevelFlags log_level, const gchar * message, gboolean forced, gpointer arg) +file_log_function (const gchar * log_domain, + const gchar *function, + GLogLevelFlags log_level, + const gchar * message, + gboolean forced, + gpointer arg) { - gchar tmpbuf[256], timebuf[32]; - time_t now; - struct tm *tms; - struct iovec iov[4]; - gint r = 0; - guint32 cksum; - size_t mlen; - const gchar *cptype = NULL; - gboolean got_time = FALSE; - rspamd_logger_t *rspamd_log = arg; - - if (! rspamd_log->enabled) { + gchar tmpbuf[256], timebuf[32]; + time_t now; + struct tm *tms; + struct iovec iov[4]; + gint r = 0; + guint32 cksum; + size_t mlen; + const gchar *cptype = NULL; + gboolean got_time = FALSE; + rspamd_logger_t *rspamd_log = arg; + + if (!rspamd_log->enabled) { return; } @@ -591,8 +649,9 @@ file_log_function (const gchar * log_domain, const gchar *function, GLogLevelFla mlen = strlen (message); cksum = rspamd_log_calculate_cksum (message, mlen); if (cksum == rspamd_log->last_line_cksum) { - rspamd_log->repeats ++; - if (rspamd_log->repeats > REPEATS_MIN && rspamd_log->repeats < REPEATS_MAX) { + rspamd_log->repeats++; + if (rspamd_log->repeats > REPEATS_MIN && rspamd_log->repeats < + REPEATS_MAX) { /* Do not log anything */ if (rspamd_log->saved_message == 0) { rspamd_log->saved_message = g_strdup (message); @@ -601,14 +660,32 @@ file_log_function (const gchar * log_domain, const gchar *function, GLogLevelFla return; } else if (rspamd_log->repeats > REPEATS_MAX) { - rspamd_snprintf (tmpbuf, sizeof (tmpbuf), "Last message repeated %ud times", rspamd_log->repeats); + rspamd_snprintf (tmpbuf, + sizeof (tmpbuf), + "Last message repeated %ud times", + rspamd_log->repeats); rspamd_log->repeats = 0; /* It is safe to use temporary buffer here as it is not static */ if (rspamd_log->saved_message) { - file_log_function (log_domain, rspamd_log->saved_function, log_level, rspamd_log->saved_message, forced, arg); - } - file_log_function (log_domain, __FUNCTION__, log_level, tmpbuf, forced, arg); - file_log_function (log_domain, function, log_level, message, forced, arg); + file_log_function (log_domain, + rspamd_log->saved_function, + log_level, + rspamd_log->saved_message, + forced, + arg); + } + file_log_function (log_domain, + __FUNCTION__, + log_level, + tmpbuf, + forced, + arg); + file_log_function (log_domain, + function, + log_level, + message, + forced, + arg); rspamd_log->repeats = REPEATS_MIN + 1; return; } @@ -617,18 +694,36 @@ file_log_function (const gchar * log_domain, const gchar *function, GLogLevelFla /* Reset counter if new message differs from saved message */ rspamd_log->last_line_cksum = cksum; if (rspamd_log->repeats > REPEATS_MIN) { - rspamd_snprintf (tmpbuf, sizeof (tmpbuf), "Last message repeated %ud times", rspamd_log->repeats); + rspamd_snprintf (tmpbuf, + sizeof (tmpbuf), + "Last message repeated %ud times", + rspamd_log->repeats); rspamd_log->repeats = 0; if (rspamd_log->saved_message) { - file_log_function (log_domain, rspamd_log->saved_function, log_level, rspamd_log->saved_message, forced, arg); + file_log_function (log_domain, + rspamd_log->saved_function, + log_level, + rspamd_log->saved_message, + forced, + arg); g_free (rspamd_log->saved_message); g_free (rspamd_log->saved_function); rspamd_log->saved_message = NULL; rspamd_log->saved_function = NULL; - } - file_log_function (log_domain, __FUNCTION__, log_level, tmpbuf, forced, arg); + } + file_log_function (log_domain, + __FUNCTION__, + log_level, + tmpbuf, + forced, + arg); /* It is safe to use temporary buffer here as it is not static */ - file_log_function (log_domain, function, log_level, message, forced, arg); + file_log_function (log_domain, + function, + log_level, + message, + forced, + arg); return; } else { @@ -637,7 +732,7 @@ file_log_function (const gchar * log_domain, const gchar *function, GLogLevelFla } if (rspamd_log->cfg->log_extended) { - if (! got_time) { + if (!got_time) { now = time (NULL); } @@ -665,10 +760,21 @@ file_log_function (const gchar * log_domain, const gchar *function, GLogLevelFla r = 0; } if (function == NULL) { - r += rspamd_snprintf (tmpbuf + r, sizeof (tmpbuf) - r, "%s #%P(%s) ", timebuf, rspamd_log->pid, cptype); + r += rspamd_snprintf (tmpbuf + r, + sizeof (tmpbuf) - r, + "%s #%P(%s) ", + timebuf, + rspamd_log->pid, + cptype); } else { - r += rspamd_snprintf (tmpbuf + r, sizeof (tmpbuf) -r, "%s #%P(%s) %s: ", timebuf, rspamd_log->pid, cptype, function); + r += rspamd_snprintf (tmpbuf + r, + sizeof (tmpbuf) - r, + "%s #%P(%s) %s: ", + timebuf, + rspamd_log->pid, + cptype, + function); } /* Construct IOV for log line */ iov[0].iov_base = tmpbuf; @@ -712,16 +818,17 @@ file_log_function (const gchar * log_domain, const gchar *function, GLogLevelFla */ void rspamd_conditional_debug (rspamd_logger_t *rspamd_log, - rspamd_inet_addr_t *addr, const gchar *function, const gchar *fmt, ...) + rspamd_inet_addr_t *addr, const gchar *function, const gchar *fmt, ...) { - static gchar logbuf[BUFSIZ]; - va_list vp; - u_char *end; + static gchar logbuf[BUFSIZ]; + va_list vp; + u_char *end; - if (rspamd_log->cfg->log_level >= G_LOG_LEVEL_DEBUG || rspamd_log->is_debug) { + if (rspamd_log->cfg->log_level >= G_LOG_LEVEL_DEBUG || + rspamd_log->is_debug) { if (rspamd_log->debug_ip && addr != NULL) { if (addr->af == AF_INET && radix32tree_find (rspamd_log->debug_ip, - ntohl (addr->addr.s4.sin_addr.s_addr)) == RADIX_NO_VALUE) { + ntohl (addr->addr.s4.sin_addr.s_addr)) == RADIX_NO_VALUE) { return; } } @@ -731,21 +838,34 @@ rspamd_conditional_debug (rspamd_logger_t *rspamd_log, *end = '\0'; rspamd_escape_log_string (logbuf); va_end (vp); - rspamd_log->log_func (NULL, function, G_LOG_LEVEL_DEBUG, logbuf, TRUE, rspamd_log); + rspamd_log->log_func (NULL, + function, + G_LOG_LEVEL_DEBUG, + logbuf, + TRUE, + rspamd_log); g_mutex_unlock (rspamd_log->mtx); } -} +} /** * Wrapper for glib logger */ void -rspamd_glib_log_function (const gchar *log_domain, GLogLevelFlags log_level, const gchar *message, gpointer arg) +rspamd_glib_log_function (const gchar *log_domain, + GLogLevelFlags log_level, + const gchar *message, + gpointer arg) { - rspamd_logger_t *rspamd_log = arg; + rspamd_logger_t *rspamd_log = arg; if (rspamd_log->enabled) { g_mutex_lock (rspamd_log->mtx); - rspamd_log->log_func (log_domain, NULL, log_level, message, FALSE, rspamd_log); + rspamd_log->log_func (log_domain, + NULL, + log_level, + message, + FALSE, + rspamd_log); g_mutex_unlock (rspamd_log->mtx); } } diff --git a/src/libutil/logger.h b/src/libutil/logger.h index 4b1c74410..8d4b3763b 100644 --- a/src/libutil/logger.h +++ b/src/libutil/logger.h @@ -1,21 +1,24 @@ #ifndef RSPAMD_LOGGER_H #define RSPAMD_LOGGER_H -#include "config.h" #include "cfg_file.h" +#include "config.h" #include "radix.h" #include "util.h" -typedef void (*rspamd_log_func_t)(const gchar * log_domain, const gchar *function, - GLogLevelFlags log_level, const gchar * message, - gboolean forced, gpointer arg); +typedef void (*rspamd_log_func_t)(const gchar * log_domain, + const gchar *function, + GLogLevelFlags log_level, const gchar * message, + gboolean forced, gpointer arg); typedef struct rspamd_logger_s rspamd_logger_t; /** * Init logger */ -void rspamd_set_logger (struct rspamd_config *cfg, GQuark ptype, struct rspamd_main *main); +void rspamd_set_logger (struct rspamd_config *cfg, + GQuark ptype, + struct rspamd_main *main); /** * Open log file or initialize other structures */ @@ -55,28 +58,33 @@ void flush_log_buf (rspamd_logger_t *logger); * Log function that is compatible for glib messages */ void rspamd_glib_log_function (const gchar *log_domain, - GLogLevelFlags log_level, const gchar *message, gpointer arg); + GLogLevelFlags log_level, const gchar *message, gpointer arg); /** * Function with variable number of arguments support */ void rspamd_common_log_function (rspamd_logger_t *logger, - GLogLevelFlags log_level, const gchar *function, const gchar *fmt, ...); + GLogLevelFlags log_level, const gchar *function, const gchar *fmt, ...); void rspamd_common_logv (rspamd_logger_t *logger, - GLogLevelFlags log_level, const gchar *function, const gchar *fmt, va_list args); + GLogLevelFlags log_level, + const gchar *function, + const gchar *fmt, + va_list args); /** * Conditional debug function */ void rspamd_conditional_debug (rspamd_logger_t *logger, - rspamd_inet_addr_t *addr, const gchar *function, const gchar *fmt, ...) ; + rspamd_inet_addr_t *addr, const gchar *function, const gchar *fmt, ...); /** * Function with variable number of arguments support that uses static default logger */ -void rspamd_default_log_function (GLogLevelFlags log_level, const gchar *function, - const gchar *fmt, ...); +void rspamd_default_log_function (GLogLevelFlags log_level, + const gchar *function, + const gchar *fmt, + ...); /** * Varargs version of default log function @@ -85,7 +93,10 @@ void rspamd_default_log_function (GLogLevelFlags log_level, const gchar *functio * @param fmt * @param args */ -void rspamd_default_logv (GLogLevelFlags log_level, const gchar *function, const gchar *fmt, va_list args); +void rspamd_default_logv (GLogLevelFlags log_level, + const gchar *function, + const gchar *fmt, + va_list args); /** * Temporary turn on debug @@ -101,17 +112,42 @@ void rspamd_log_nodebug (rspamd_logger_t *logger); /* Logging in postfix style */ #if defined(RSPAMD_MAIN) -#define msg_err(...) rspamd_common_log_function(rspamd_main->logger, G_LOG_LEVEL_CRITICAL, __FUNCTION__, __VA_ARGS__) -#define msg_warn(...) rspamd_common_log_function(rspamd_main->logger, G_LOG_LEVEL_WARNING, __FUNCTION__, __VA_ARGS__) -#define msg_info(...) rspamd_common_log_function(rspamd_main->logger, G_LOG_LEVEL_INFO, __FUNCTION__, __VA_ARGS__) -#define msg_debug(...) rspamd_conditional_debug(rspamd_main->logger, NULL, __FUNCTION__, __VA_ARGS__) -#define debug_task(...) rspamd_conditional_debug(rspamd_main->logger, &task->from_addr, __FUNCTION__, __VA_ARGS__) +#define msg_err(...) rspamd_common_log_function (rspamd_main->logger, \ + G_LOG_LEVEL_CRITICAL, \ + __FUNCTION__, \ + __VA_ARGS__) +#define msg_warn(...) rspamd_common_log_function (rspamd_main->logger, \ + G_LOG_LEVEL_WARNING, \ + __FUNCTION__, \ + __VA_ARGS__) +#define msg_info(...) rspamd_common_log_function (rspamd_main->logger, \ + G_LOG_LEVEL_INFO, \ + __FUNCTION__, \ + __VA_ARGS__) +#define msg_debug(...) rspamd_conditional_debug (rspamd_main->logger, \ + NULL, \ + __FUNCTION__, \ + __VA_ARGS__) +#define debug_task(...) rspamd_conditional_debug (rspamd_main->logger, \ + &task->from_addr, \ + __FUNCTION__, \ + __VA_ARGS__) #else -#define msg_err(...) rspamd_default_log_function(G_LOG_LEVEL_CRITICAL, __FUNCTION__, __VA_ARGS__) -#define msg_warn(...) rspamd_default_log_function(G_LOG_LEVEL_WARNING, __FUNCTION__, __VA_ARGS__) -#define msg_info(...) rspamd_default_log_function(G_LOG_LEVEL_INFO, __FUNCTION__, __VA_ARGS__) -#define msg_debug(...) rspamd_default_log_function(G_LOG_LEVEL_DEBUG, __FUNCTION__, __VA_ARGS__) -#define debug_task(...) rspamd_default_log_function(G_LOG_LEVEL_DEBUG, __FUNCTION__, __VA_ARGS__) +#define msg_err(...) rspamd_default_log_function (G_LOG_LEVEL_CRITICAL, \ + __FUNCTION__, \ + __VA_ARGS__) +#define msg_warn(...) rspamd_default_log_function (G_LOG_LEVEL_WARNING, \ + __FUNCTION__, \ + __VA_ARGS__) +#define msg_info(...) rspamd_default_log_function (G_LOG_LEVEL_INFO, \ + __FUNCTION__, \ + __VA_ARGS__) +#define msg_debug(...) rspamd_default_log_function (G_LOG_LEVEL_DEBUG, \ + __FUNCTION__, \ + __VA_ARGS__) +#define debug_task(...) rspamd_default_log_function (G_LOG_LEVEL_DEBUG, \ + __FUNCTION__, \ + __VA_ARGS__) #endif #endif diff --git a/src/libutil/map.c b/src/libutil/map.c index ffe3a1e6f..21612ddfb 100644 --- a/src/libutil/map.c +++ b/src/libutil/map.c @@ -26,33 +26,33 @@ * Implementation of map files handling */ #include "config.h" -#include "map.h" #include "http.h" #include "main.h" -#include "util.h" +#include "map.h" #include "mem_pool.h" +#include "util.h" -static const gchar *hash_fill = "1"; +static const gchar *hash_fill = "1"; /* Http reply */ struct http_reply { - gint code; - GHashTable *headers; - gchar *cur_header; - gint parser_state; + gint code; + GHashTable *headers; + gchar *cur_header; + gint parser_state; }; struct http_callback_data { - struct event ev; - struct event_base *ev_base; - struct timeval tv; - struct rspamd_map *map; - struct http_map_data *data; - struct http_reply *reply; - struct map_cb_data cbdata; - - gint state; - gint fd; + struct event ev; + struct event_base *ev_base; + struct timeval tv; + struct rspamd_map *map; + struct http_map_data *data; + struct http_reply *reply; + struct map_cb_data cbdata; + + gint state; + gint fd; }; /* Value in seconds after whitch we would try to do stat on list file */ @@ -65,12 +65,17 @@ struct http_callback_data { * Helper for HTTP connection establishment */ static gint -connect_http (struct rspamd_map *map, struct http_map_data *data, gboolean is_async) +connect_http (struct rspamd_map *map, + struct http_map_data *data, + gboolean is_async) { - gint sock; + gint sock; if ((sock = make_tcp_socket (data->addr, FALSE, is_async)) == -1) { - msg_info ("cannot connect to http server %s: %d, %s", data->host, errno, strerror (errno)); + msg_info ("cannot connect to http server %s: %d, %s", + data->host, + errno, + strerror (errno)); return -1; } @@ -81,17 +86,27 @@ connect_http (struct rspamd_map *map, struct http_map_data *data, gboolean is_as * Write HTTP request */ static void -write_http_request (struct rspamd_map *map, struct http_map_data *data, gint sock) +write_http_request (struct rspamd_map *map, + struct http_map_data *data, + gint sock) { - gchar outbuf[BUFSIZ], datebuf[128]; - gint r; - struct tm *tm; + gchar outbuf[BUFSIZ], datebuf[128]; + gint r; + struct tm *tm; tm = gmtime (&data->last_checked); strftime (datebuf, sizeof (datebuf), "%a, %d %b %Y %H:%M:%S %Z", tm); - r = rspamd_snprintf (outbuf, sizeof (outbuf), "GET %s%s HTTP/1.1" CRLF "Connection: close" CRLF "Host: %s" CRLF, (*data->path == '/') ? "" : "/", data->path, data->host); + r = rspamd_snprintf (outbuf, + sizeof (outbuf), + "GET %s%s HTTP/1.1" CRLF "Connection: close" CRLF "Host: %s" CRLF, + (*data->path == '/') ? "" : "/", + data->path, + data->host); if (data->last_checked != 0) { - r += rspamd_snprintf (outbuf + r, sizeof (outbuf) - r, "If-Modified-Since: %s" CRLF, datebuf); + r += rspamd_snprintf (outbuf + r, + sizeof (outbuf) - r, + "If-Modified-Since: %s" CRLF, + datebuf); } r += rspamd_snprintf (outbuf + r, sizeof (outbuf) - r, CRLF); @@ -104,16 +119,16 @@ write_http_request (struct rspamd_map *map, struct http_map_data *data, gint soc /** * FSM for parsing HTTP reply */ -static gchar * +static gchar * parse_http_reply (gchar * chunk, gint len, struct http_reply *reply) { - gchar *s, *p, *err_str, *tmp; + gchar *s, *p, *err_str, *tmp; p = chunk; s = chunk; while (p - chunk < len) { switch (reply->parser_state) { - /* Search status code */ + /* Search status code */ case 0: /* Search for status code */ if (*p != ' ') { @@ -131,7 +146,7 @@ parse_http_reply (gchar * chunk, gint len, struct http_reply *reply) continue; } break; - /* Skip to end of line */ + /* Skip to end of line */ case 1: if (*p == '\n') { /* Switch to read header state */ @@ -140,7 +155,7 @@ parse_http_reply (gchar * chunk, gint len, struct http_reply *reply) /* Each skipped symbol is proceeded */ s = ++p; break; - /* Read header value */ + /* Read header value */ case 2: if (*p == ':') { reply->cur_header = g_malloc (p - s + 1); @@ -153,7 +168,7 @@ parse_http_reply (gchar * chunk, gint len, struct http_reply *reply) } p++; break; - /* Skip spaces after header name */ + /* Skip spaces after header name */ case 3: if (*p != ' ') { s = p; @@ -163,13 +178,14 @@ parse_http_reply (gchar * chunk, gint len, struct http_reply *reply) p++; } break; - /* Read header value */ + /* Read header value */ case 4: if (*p == '\r') { if (reply->cur_header != NULL) { tmp = g_malloc (p - s + 1); rspamd_strlcpy (tmp, s, p - s + 1); - g_hash_table_insert (reply->headers, reply->cur_header, tmp); + g_hash_table_insert (reply->headers, reply->cur_header, + tmp); reply->cur_header = NULL; } reply->parser_state = 1; @@ -198,13 +214,14 @@ parse_http_reply (gchar * chunk, gint len, struct http_reply *reply) static gint read_chunk_header (gchar * buf, gint len, struct http_map_data *data) { - gchar chunkbuf[32], *p, *c, *err_str; - gint skip = 0; + gchar chunkbuf[32], *p, *c, *err_str; + gint skip = 0; p = chunkbuf; c = buf; /* Find hex digits */ - while (g_ascii_isxdigit (*c) && p - chunkbuf < (gint)(sizeof (chunkbuf) - 1) && skip < len) { + while (g_ascii_isxdigit (*c) && p - chunkbuf < + (gint)(sizeof (chunkbuf) - 1) && skip < len) { *p++ = *c++; skip++; } @@ -231,11 +248,15 @@ read_chunk_header (gchar * buf, gint len, struct http_map_data *data) /** * Helper callback for reading chunked reply */ -static gboolean -read_http_chunked (gchar * buf, size_t len, struct rspamd_map *map, struct http_map_data *data, struct map_cb_data *cbdata) +static gboolean +read_http_chunked (gchar * buf, + size_t len, + struct rspamd_map *map, + struct http_map_data *data, + struct map_cb_data *cbdata) { - gchar *p = buf, *remain; - gint skip = 0; + gchar *p = buf, *remain; + gint skip = 0; if (data->chunked == 1) { /* Read first chunk data */ @@ -301,14 +322,20 @@ read_http_chunked (gchar * buf, size_t len, struct rspamd_map *map, struct http_ /** * Callback for reading HTTP reply */ -static gboolean -read_http_common (struct rspamd_map *map, struct http_map_data *data, struct http_reply *reply, struct map_cb_data *cbdata, gint fd) +static gboolean +read_http_common (struct rspamd_map *map, + struct http_map_data *data, + struct http_reply *reply, + struct map_cb_data *cbdata, + gint fd) { - gchar *remain, *pos; - ssize_t r; - gchar *te, *date; - - if ((r = read (fd, data->read_buf + data->rlen, sizeof (data->read_buf) - data->rlen)) > 0) { + gchar *remain, *pos; + ssize_t r; + gchar *te, *date; + + if ((r = + read (fd, data->read_buf + data->rlen, sizeof (data->read_buf) - + data->rlen)) > 0) { r += data->rlen; data->rlen = 0; remain = parse_http_reply (data->read_buf, r, reply); @@ -325,7 +352,9 @@ read_http_common (struct rspamd_map *map, struct http_map_data *data, struct htt if (reply->parser_state == 6) { /* If reply header is parsed successfully, try to read further data */ if (reply->code != 200 && reply->code != 304) { - msg_err ("got error reply from server %s, %d", data->host, reply->code); + msg_err ("got error reply from server %s, %d", + data->host, + reply->code); return FALSE; } else if (reply->code == 304) { @@ -335,7 +364,9 @@ read_http_common (struct rspamd_map *map, struct http_map_data *data, struct htt pos = data->read_buf; /* Check for chunked */ if (data->chunked == 0) { - if ((te = g_hash_table_lookup (reply->headers, "Transfer-Encoding")) != NULL) { + if ((te = + g_hash_table_lookup (reply->headers, + "Transfer-Encoding")) != NULL) { if (g_ascii_strcasecmp (te, "chunked") == 0) { data->chunked = 1; } @@ -381,9 +412,9 @@ read_http_common (struct rspamd_map *map, struct http_map_data *data, struct htt static void read_http_sync (struct rspamd_map *map, struct http_map_data *data) { - struct map_cb_data cbdata; - gint fd; - struct http_reply *repl; + struct map_cb_data cbdata; + gint fd; + struct http_reply *repl; if (map->read_callback == NULL || map->fin_callback == NULL) { msg_err ("bad callback for reading map file"); @@ -404,9 +435,12 @@ read_http_sync (struct rspamd_map *map, struct http_map_data *data) repl = g_malloc (sizeof (struct http_reply)); repl->parser_state = 0; repl->code = 404; - repl->headers = g_hash_table_new_full (rspamd_strcase_hash, rspamd_strcase_equal, g_free, g_free); + repl->headers = g_hash_table_new_full (rspamd_strcase_hash, + rspamd_strcase_equal, + g_free, + g_free); - while (read_http_common (map, data, repl, &cbdata, fd)); + while (read_http_common (map, data, repl, &cbdata, fd)) ; close (fd); @@ -426,10 +460,10 @@ read_http_sync (struct rspamd_map *map, struct http_map_data *data) static void read_map_file (struct rspamd_map *map, struct file_map_data *data) { - struct map_cb_data cbdata; - gchar buf[BUFSIZ], *remain; - ssize_t r; - gint fd, rlen; + struct map_cb_data cbdata; + gchar buf[BUFSIZ], *remain; + ssize_t r; + gint fd, rlen; if (map->read_callback == NULL || map->fin_callback == NULL) { msg_err ("bad callback for reading map file"); @@ -437,7 +471,8 @@ read_map_file (struct rspamd_map *map, struct file_map_data *data) } if ((fd = open (data->filename, O_RDONLY)) == -1) { - msg_warn ("cannot open file '%s': %s", data->filename, strerror (errno)); + msg_warn ("cannot open file '%s': %s", data->filename, + strerror (errno)); return; } @@ -467,10 +502,14 @@ read_map_file (struct rspamd_map *map, struct file_map_data *data) /** * FSM for parsing lists */ -gchar * -abstract_parse_kv_list (rspamd_mempool_t * pool, gchar * chunk, gint len, struct map_cb_data *data, insert_func func) +gchar * +abstract_parse_kv_list (rspamd_mempool_t * pool, + gchar * chunk, + gint len, + struct map_cb_data *data, + insert_func func) { - gchar *c, *p, *key = NULL, *value = NULL; + gchar *c, *p, *key = NULL, *value = NULL; p = chunk; c = p; @@ -526,7 +565,7 @@ abstract_parse_kv_list (rspamd_mempool_t * pool, gchar * chunk, gint len, struct } } else { - p ++; + p++; } break; case 2: @@ -536,7 +575,7 @@ abstract_parse_kv_list (rspamd_mempool_t * pool, gchar * chunk, gint len, struct data->state = 0; } else { - p ++; + p++; } break; case 99: @@ -557,7 +596,7 @@ abstract_parse_kv_list (rspamd_mempool_t * pool, gchar * chunk, gint len, struct case 100: /* Skip \r\n and whitespaces */ if (*p == '\r' || *p == '\n' || g_ascii_isspace (*p)) { - p ++; + p++; } else { c = p; @@ -571,10 +610,14 @@ abstract_parse_kv_list (rspamd_mempool_t * pool, gchar * chunk, gint len, struct return c; } -gchar * -abstract_parse_list (rspamd_mempool_t * pool, gchar * chunk, gint len, struct map_cb_data *data, insert_func func) +gchar * +abstract_parse_list (rspamd_mempool_t * pool, + gchar * chunk, + gint len, + struct map_cb_data *data, + insert_func func) { - gchar *s, *p, *str, *start; + gchar *s, *p, *str, *start; p = chunk; start = p; @@ -584,7 +627,7 @@ abstract_parse_list (rspamd_mempool_t * pool, gchar * chunk, gint len, struct ma while (p - chunk < len) { switch (data->state) { - /* READ_SYMBOL */ + /* READ_SYMBOL */ case 0: if (*p == '#') { /* Got comment */ @@ -623,7 +666,7 @@ abstract_parse_list (rspamd_mempool_t * pool, gchar * chunk, gint len, struct ma p++; } break; - /* SKIP_COMMENT */ + /* SKIP_COMMENT */ case 1: /* Skip comment till end of line */ if (*p == '\r' || *p == '\n') { @@ -652,13 +695,13 @@ abstract_parse_list (rspamd_mempool_t * pool, gchar * chunk, gint len, struct ma static void radix_tree_insert_helper (gpointer st, gconstpointer key, gpointer value) { - radix_tree_t *tree = st; + radix_tree_t *tree = st; - guint32 mask = 0xFFFFFFFF; - guint32 ip; - gchar *token, *ipnet, *err_str, **strv, **cur; - struct in_addr ina; - gint k; + guint32 mask = 0xFFFFFFFF; + guint32 ip; + gchar *token, *ipnet, *err_str, **strv, **cur; + struct in_addr ina; + gint k; /* Split string if there are multiple items inside a single string */ strv = g_strsplit_set ((gchar *)key, " ,;", 0); @@ -677,7 +720,10 @@ radix_tree_insert_helper (gpointer st, gconstpointer key, gpointer value) /* Get mask */ k = strtoul (ipnet, &err_str, 10); if (errno != 0) { - msg_warn ("invalid netmask, error detected on symbol: %s, erorr: %s", err_str, strerror (errno)); + msg_warn ( + "invalid netmask, error detected on symbol: %s, erorr: %s", + err_str, + strerror (errno)); k = 32; } else if (k > 32 || k < 0) { @@ -698,10 +744,12 @@ radix_tree_insert_helper (gpointer st, gconstpointer key, gpointer value) ip = ntohl ((guint32) ina.s_addr); k = radix32tree_insert (tree, ip, mask, 1); if (k == -1) { - msg_warn ("cannot insert ip to tree: %s, mask %X", inet_ntoa (ina), mask); + msg_warn ("cannot insert ip to tree: %s, mask %X", inet_ntoa ( + ina), mask); } else if (k == 1) { - msg_warn ("ip %s, mask %X, value already exists", inet_ntoa (ina), mask); + msg_warn ("ip %s, mask %X, value already exists", inet_ntoa ( + ina), mask); } cur++; } @@ -710,13 +758,21 @@ radix_tree_insert_helper (gpointer st, gconstpointer key, gpointer value) } /* Helpers */ -gchar * -read_host_list (rspamd_mempool_t * pool, gchar * chunk, gint len, struct map_cb_data *data) +gchar * +read_host_list (rspamd_mempool_t * pool, + gchar * chunk, + gint len, + struct map_cb_data *data) { if (data->cur_data == NULL) { - data->cur_data = g_hash_table_new (rspamd_strcase_hash, rspamd_strcase_equal); + data->cur_data = g_hash_table_new (rspamd_strcase_hash, + rspamd_strcase_equal); } - return abstract_parse_list (pool, chunk, len, data, (insert_func) g_hash_table_insert); + return abstract_parse_list (pool, + chunk, + len, + data, + (insert_func) g_hash_table_insert); } void @@ -727,13 +783,21 @@ fin_host_list (rspamd_mempool_t * pool, struct map_cb_data *data) } } -gchar * -read_kv_list (rspamd_mempool_t * pool, gchar * chunk, gint len, struct map_cb_data *data) +gchar * +read_kv_list (rspamd_mempool_t * pool, + gchar * chunk, + gint len, + struct map_cb_data *data) { if (data->cur_data == NULL) { - data->cur_data = g_hash_table_new (rspamd_strcase_hash, rspamd_strcase_equal); + data->cur_data = g_hash_table_new (rspamd_strcase_hash, + rspamd_strcase_equal); } - return abstract_parse_kv_list (pool, chunk, len, data, (insert_func) g_hash_table_insert); + return abstract_parse_kv_list (pool, + chunk, + len, + data, + (insert_func) g_hash_table_insert); } void @@ -744,13 +808,20 @@ fin_kv_list (rspamd_mempool_t * pool, struct map_cb_data *data) } } -gchar * -read_radix_list (rspamd_mempool_t * pool, gchar * chunk, gint len, struct map_cb_data *data) +gchar * +read_radix_list (rspamd_mempool_t * pool, + gchar * chunk, + gint len, + struct map_cb_data *data) { if (data->cur_data == NULL) { data->cur_data = radix_tree_create (); } - return abstract_parse_list (pool, chunk, len, data, (insert_func) radix_tree_insert_helper); + return abstract_parse_list (pool, + chunk, + len, + data, + (insert_func) radix_tree_insert_helper); } void @@ -767,24 +838,28 @@ fin_radix_list (rspamd_mempool_t * pool, struct map_cb_data *data) static void file_callback (gint fd, short what, void *ud) { - struct rspamd_map *map = ud; - struct file_map_data *data = map->map_data; - struct stat st; - gdouble jittered_sec; + struct rspamd_map *map = ud; + struct file_map_data *data = map->map_data; + struct stat st; + gdouble jittered_sec; /* Plan event again with jitter */ evtimer_del (&map->ev); - jittered_sec = (map->cfg->map_timeout + g_random_double () * map->cfg->map_timeout); + jittered_sec = + (map->cfg->map_timeout + g_random_double () * map->cfg->map_timeout); double_to_tv (jittered_sec, &map->tv); evtimer_add (&map->ev, &map->tv); if (g_atomic_int_get (map->locked)) { - msg_info ("don't try to reread map as it is locked by other process, will reread it later"); + msg_info ( + "don't try to reread map as it is locked by other process, will reread it later"); return; } - if (stat (data->filename, &st) != -1 && (st.st_mtime > data->st.st_mtime || data->st.st_mtime == -1)) { + if (stat (data->filename, + &st) != -1 && + (st.st_mtime > data->st.st_mtime || data->st.st_mtime == -1)) { /* File was modified since last check */ memcpy (&data->st, &st, sizeof (struct stat)); } @@ -818,7 +893,7 @@ free_http_cbdata (struct http_callback_data *cbd) static void http_async_callback (gint fd, short what, void *ud) { - struct http_callback_data *cbd = ud; + struct http_callback_data *cbd = ud; /* Begin of connection */ if (what == EV_WRITE) { @@ -826,7 +901,11 @@ http_async_callback (gint fd, short what, void *ud) /* Can write request */ write_http_request (cbd->map, cbd->data, fd); /* Plan reading */ - event_set (&cbd->ev, cbd->fd, EV_READ | EV_PERSIST, http_async_callback, cbd); + event_set (&cbd->ev, + cbd->fd, + EV_READ | EV_PERSIST, + http_async_callback, + cbd); event_base_set (cbd->ev_base, &cbd->ev); cbd->tv.tv_sec = HTTP_READ_TIMEOUT; cbd->tv.tv_usec = 0; @@ -835,7 +914,10 @@ http_async_callback (gint fd, short what, void *ud) cbd->reply = g_malloc (sizeof (struct http_reply)); cbd->reply->parser_state = 0; cbd->reply->code = 404; - cbd->reply->headers = g_hash_table_new_full (rspamd_strcase_hash, rspamd_strcase_equal, g_free, g_free); + cbd->reply->headers = g_hash_table_new_full (rspamd_strcase_hash, + rspamd_strcase_equal, + g_free, + g_free); cbd->cbdata.state = 0; cbd->cbdata.prev_data = *cbd->map->user_data; cbd->cbdata.cur_data = NULL; @@ -857,13 +939,15 @@ http_async_callback (gint fd, short what, void *ud) /* Got reply, parse it */ else if (what == EV_READ) { if (cbd->state >= 1) { - if (!read_http_common (cbd->map, cbd->data, cbd->reply, &cbd->cbdata, cbd->fd)) { + if (!read_http_common (cbd->map, cbd->data, cbd->reply, + &cbd->cbdata, cbd->fd)) { /* Handle Not-Modified in a special way */ if (cbd->reply->code == 304) { if (cbd->data->last_checked == (time_t)-1) { cbd->data->last_checked = time (NULL); } - msg_info ("data is not modified for server %s", cbd->data->host); + msg_info ("data is not modified for server %s", + cbd->data->host); } else if (cbd->cbdata.cur_data != NULL) { /* Destroy old data and start reading request data */ @@ -900,20 +984,22 @@ http_async_callback (gint fd, short what, void *ud) static void http_callback (gint fd, short what, void *ud) { - struct rspamd_map *map = ud; - struct http_map_data *data = map->map_data; - gint sock; - struct http_callback_data *cbd; - gdouble jittered_sec; + struct rspamd_map *map = ud; + struct http_map_data *data = map->map_data; + gint sock; + struct http_callback_data *cbd; + gdouble jittered_sec; /* Plan event again with jitter */ evtimer_del (&map->ev); - jittered_sec = (map->cfg->map_timeout + g_random_double () * map->cfg->map_timeout); + jittered_sec = + (map->cfg->map_timeout + g_random_double () * map->cfg->map_timeout); double_to_tv (jittered_sec, &map->tv); evtimer_add (&map->ev, &map->tv); if (g_atomic_int_get (map->locked)) { - msg_info ("don't try to reread map as it is locked by other process, will reread it later"); + msg_info ( + "don't try to reread map as it is locked by other process, will reread it later"); return; } @@ -945,10 +1031,10 @@ http_callback (gint fd, short what, void *ud) void start_map_watch (struct rspamd_config *cfg, struct event_base *ev_base) { - GList *cur = cfg->maps; - struct rspamd_map *map; - struct file_map_data *fdata; - gdouble jittered_sec; + GList *cur = cfg->maps; + struct rspamd_map *map; + struct file_map_data *fdata; + gdouble jittered_sec; /* First of all do synced read of data */ while (cur) { @@ -964,7 +1050,9 @@ start_map_watch (struct rspamd_config *cfg, struct event_base *ev_base) read_map_file (map, map->map_data); } /* Plan event with jitter */ - jittered_sec = (map->cfg->map_timeout + g_random_double () * map->cfg->map_timeout) / 2.; + jittered_sec = + (map->cfg->map_timeout + g_random_double () * + map->cfg->map_timeout) / 2.; double_to_tv (jittered_sec, &map->tv); evtimer_add (&map->ev, &map->tv); } @@ -974,7 +1062,9 @@ start_map_watch (struct rspamd_config *cfg, struct event_base *ev_base) /* Read initial data */ read_http_sync (map, map->map_data); /* Plan event with jitter */ - jittered_sec = (map->cfg->map_timeout + g_random_double () * map->cfg->map_timeout); + jittered_sec = + (map->cfg->map_timeout + g_random_double () * + map->cfg->map_timeout); double_to_tv (jittered_sec, &map->tv); evtimer_add (&map->ev, &map->tv); } @@ -982,7 +1072,7 @@ start_map_watch (struct rspamd_config *cfg, struct event_base *ev_base) } } -void +void remove_all_maps (struct rspamd_config *cfg) { g_list_free (cfg->maps); @@ -996,13 +1086,15 @@ remove_all_maps (struct rspamd_config *cfg) gboolean check_map_proto (const gchar *map_line, gint *res, const gchar **pos) { - if (g_ascii_strncasecmp (map_line, "http://", sizeof ("http://") - 1) == 0) { + if (g_ascii_strncasecmp (map_line, "http://", + sizeof ("http://") - 1) == 0) { if (res && pos) { *res = MAP_PROTO_HTTP; *pos = map_line + sizeof ("http://") - 1; } } - else if (g_ascii_strncasecmp (map_line, "file://", sizeof ("file://") - 1) == 0) { + else if (g_ascii_strncasecmp (map_line, "file://", sizeof ("file://") - + 1) == 0) { if (res && pos) { *res = MAP_PROTO_FILE; *pos = map_line + sizeof ("file://") - 1; @@ -1022,17 +1114,21 @@ check_map_proto (const gchar *map_line, gint *res, const gchar **pos) } gboolean -add_map (struct rspamd_config *cfg, const gchar *map_line, const gchar *description, - map_cb_t read_callback, map_fin_cb_t fin_callback, void **user_data) +add_map (struct rspamd_config *cfg, + const gchar *map_line, + const gchar *description, + map_cb_t read_callback, + map_fin_cb_t fin_callback, + void **user_data) { - struct rspamd_map *new_map; - enum fetch_proto proto; - const gchar *def, *p, *hostend; - struct file_map_data *fdata; - struct http_map_data *hdata; - gchar portbuf[6]; - gint i, s, r; - struct addrinfo hints, *res; + struct rspamd_map *new_map; + enum fetch_proto proto; + const gchar *def, *p, *hostend; + struct file_map_data *fdata; + struct http_map_data *hdata; + gchar portbuf[6]; + gint i, s, r; + struct addrinfo hints, *res; /* First of all detect protocol line */ if (!check_map_proto (map_line, (int *)&proto, &def)) { @@ -1049,7 +1145,8 @@ add_map (struct rspamd_config *cfg, const gchar *map_line, const gchar *descript new_map->protocol = proto; new_map->cfg = cfg; new_map->id = g_random_int (); - new_map->locked = rspamd_mempool_alloc0_shared (cfg->cfg_pool, sizeof (gint)); + new_map->locked = + rspamd_mempool_alloc0_shared (cfg->cfg_pool, sizeof (gint)); if (proto == MAP_PROTO_FILE) { new_map->uri = rspamd_mempool_strdup (cfg->cfg_pool, def); @@ -1059,19 +1156,24 @@ add_map (struct rspamd_config *cfg, const gchar *map_line, const gchar *descript new_map->uri = rspamd_mempool_strdup (cfg->cfg_pool, map_line); } if (description != NULL) { - new_map->description = rspamd_mempool_strdup (cfg->cfg_pool, description); + new_map->description = + rspamd_mempool_strdup (cfg->cfg_pool, description); } /* Now check for each proto separately */ if (proto == MAP_PROTO_FILE) { - fdata = rspamd_mempool_alloc0 (cfg->map_pool, sizeof (struct file_map_data)); + fdata = + rspamd_mempool_alloc0 (cfg->map_pool, + sizeof (struct file_map_data)); if (access (def, R_OK) == -1) { if (errno != ENOENT) { msg_err ("cannot open file '%s': %s", def, strerror (errno)); return FALSE; } - msg_info ("map '%s' is not found, but it can be loaded automatically later", def); + msg_info ( + "map '%s' is not found, but it can be loaded automatically later", + def); /* We still can add this file */ fdata->st.st_mtime = -1; } @@ -1082,7 +1184,9 @@ add_map (struct rspamd_config *cfg, const gchar *map_line, const gchar *descript new_map->map_data = fdata; } else if (proto == MAP_PROTO_HTTP) { - hdata = rspamd_mempool_alloc0 (cfg->map_pool, sizeof (struct http_map_data)); + hdata = + rspamd_mempool_alloc0 (cfg->map_pool, + sizeof (struct http_map_data)); /* Try to search port */ if ((p = strchr (def, ':')) != NULL) { hostend = p; @@ -1125,15 +1229,21 @@ add_map (struct rspamd_config *cfg, const gchar *map_line, const gchar *descript if ((r = getaddrinfo (hdata->host, portbuf, &hints, &res)) == 0) { hdata->addr = res; - rspamd_mempool_add_destructor (cfg->cfg_pool, (rspamd_mempool_destruct_t)freeaddrinfo, hdata->addr); + rspamd_mempool_add_destructor (cfg->cfg_pool, + (rspamd_mempool_destruct_t)freeaddrinfo, hdata->addr); } else { - msg_err ("address resolution for %s failed: %s", hdata->host, gai_strerror (r)); + msg_err ("address resolution for %s failed: %s", + hdata->host, + gai_strerror (r)); return FALSE; } /* Now try to connect */ if ((s = make_tcp_socket (hdata->addr, FALSE, FALSE)) == -1) { - msg_info ("cannot connect to http server %s: %d, %s", hdata->host, errno, strerror (errno)); + msg_info ("cannot connect to http server %s: %d, %s", + hdata->host, + errno, + strerror (errno)); return FALSE; } close (s); diff --git a/src/libutil/map.h b/src/libutil/map.h index ddb9882e4..1b5c01f1e 100644 --- a/src/libutil/map.h +++ b/src/libutil/map.h @@ -34,7 +34,7 @@ struct http_map_data { gchar *host; time_t last_checked; gshort chunked; - gchar read_buf[BUFSIZ]; + gchar read_buf[BUFSIZ]; guint32 rlen; guint32 chunk; guint32 chunk_remain; @@ -45,7 +45,8 @@ struct map_cb_data; /** * Callback types */ -typedef gchar* (*map_cb_t)(rspamd_mempool_t *pool, gchar *chunk, gint len, struct map_cb_data *data); +typedef gchar * (*map_cb_t)(rspamd_mempool_t *pool, gchar *chunk, gint len, + struct map_cb_data *data); typedef void (*map_fin_cb_t)(rspamd_mempool_t *pool, struct map_cb_data *data); /** @@ -89,8 +90,12 @@ gboolean check_map_proto (const gchar *map_line, gint *res, const gchar **pos); /** * Add map from line */ -gboolean add_map (struct rspamd_config *cfg, const gchar *map_line, const gchar *description, - map_cb_t read_callback, map_fin_cb_t fin_callback, void **user_data); +gboolean add_map (struct rspamd_config *cfg, + const gchar *map_line, + const gchar *description, + map_cb_t read_callback, + map_fin_cb_t fin_callback, + void **user_data); /** * Start watching of maps by adding events to libevent event loop @@ -102,7 +107,8 @@ void start_map_watch (struct rspamd_config *cfg, struct event_base *ev_base); */ void remove_all_maps (struct rspamd_config *cfg); -typedef void (*insert_func) (gpointer st, gconstpointer key, gconstpointer value); +typedef void (*insert_func) (gpointer st, gconstpointer key, + gconstpointer value); /** * Common callbacks for frequent types of lists @@ -111,24 +117,37 @@ typedef void (*insert_func) (gpointer st, gconstpointer key, /** * Radix list is a list like ip/mask */ -gchar* read_radix_list (rspamd_mempool_t *pool, gchar *chunk, gint len, struct map_cb_data *data); +gchar * read_radix_list (rspamd_mempool_t *pool, + gchar *chunk, + gint len, + struct map_cb_data *data); void fin_radix_list (rspamd_mempool_t *pool, struct map_cb_data *data); /** * Host list is an ordinal list of hosts or domains */ -gchar* read_host_list (rspamd_mempool_t *pool, gchar *chunk, gint len, struct map_cb_data *data); +gchar * read_host_list (rspamd_mempool_t *pool, + gchar *chunk, + gint len, + struct map_cb_data *data); void fin_host_list (rspamd_mempool_t *pool, struct map_cb_data *data); /** * Kv list is an ordinal list of keys and values separated by whitespace */ -gchar* read_kv_list (rspamd_mempool_t *pool, gchar *chunk, gint len, struct map_cb_data *data); +gchar * read_kv_list (rspamd_mempool_t *pool, + gchar *chunk, + gint len, + struct map_cb_data *data); void fin_kv_list (rspamd_mempool_t *pool, struct map_cb_data *data); /** * FSM for lists parsing (support comments, blank lines and partial replies) */ -gchar * abstract_parse_list (rspamd_mempool_t * pool, gchar * chunk, gint len, struct map_cb_data *data, insert_func func); +gchar * abstract_parse_list (rspamd_mempool_t * pool, + gchar * chunk, + gint len, + struct map_cb_data *data, + insert_func func); #endif diff --git a/src/libutil/mem_pool.c b/src/libutil/mem_pool.c index 6ace865a0..630ea6b3c 100644 --- a/src/libutil/mem_pool.c +++ b/src/libutil/mem_pool.c @@ -23,18 +23,18 @@ */ #include "config.h" -#include "mem_pool.h" #include "fstring.h" #include "logger.h" -#include "util.h" #include "main.h" +#include "mem_pool.h" +#include "util.h" /* Sleep time for spin lock in nanoseconds */ #define MUTEX_SLEEP_TIME 10000000L #define MUTEX_SPIN_COUNT 100 #ifdef _THREAD_SAFE -pthread_mutex_t stat_mtx = PTHREAD_MUTEX_INITIALIZER; +pthread_mutex_t stat_mtx = PTHREAD_MUTEX_INITIALIZER; # define STAT_LOCK() do { pthread_mutex_lock (&stat_mtx); } while (0) # define STAT_UNLOCK() do { pthread_mutex_unlock (&stat_mtx); } while (0) #else @@ -42,10 +42,10 @@ pthread_mutex_t stat_mtx = PTHREAD_MUTEX_INITIALIZER; # define STAT_UNLOCK() do {} while (0) #endif -#define POOL_MTX_LOCK() do { rspamd_mutex_lock (pool->mtx); } while (0) -#define POOL_MTX_UNLOCK() do { rspamd_mutex_unlock (pool->mtx); } while (0) +#define POOL_MTX_LOCK() do { rspamd_mutex_lock (pool->mtx); } while (0) +#define POOL_MTX_UNLOCK() do { rspamd_mutex_unlock (pool->mtx); } while (0) -/* +/* * This define specify whether we should check all pools for free space for new object * or just begin scan from current (recently attached) pool * If MEMORY_GREEDY is defined, then we scan all pools to find free space (more CPU usage, slower @@ -56,7 +56,7 @@ pthread_mutex_t stat_mtx = PTHREAD_MUTEX_INITIALIZER; #undef MEMORY_GREEDY /* Internal statistic */ -static rspamd_mempool_stat_t *mem_pool_stat = NULL; +static rspamd_mempool_stat_t *mem_pool_stat = NULL; /** * Function that return free space in pool page @@ -68,17 +68,18 @@ pool_chain_free (struct _pool_chain *chain) return (gint)chain->len - (chain->pos - chain->begin + MEM_ALIGNMENT); } -static struct _pool_chain * +static struct _pool_chain * pool_chain_new (gsize size) { - struct _pool_chain *chain; + struct _pool_chain *chain; g_return_val_if_fail (size > 0, NULL); chain = g_slice_alloc (sizeof (struct _pool_chain)); if (chain == NULL) { - msg_err ("cannot allocate %z bytes, aborting", sizeof (struct _pool_chain)); + msg_err ("cannot allocate %z bytes, aborting", + sizeof (struct _pool_chain)); abort (); } @@ -102,34 +103,46 @@ pool_chain_new (gsize size) static struct _pool_chain_shared * pool_chain_new_shared (gsize size) { - struct _pool_chain_shared *chain; - gpointer map; + struct _pool_chain_shared *chain; + gpointer map; #if defined(HAVE_MMAP_ANON) - map = mmap (NULL, size + sizeof (struct _pool_chain_shared), PROT_READ | PROT_WRITE, MAP_ANON | MAP_SHARED, -1, 0); + map = mmap (NULL, + size + sizeof (struct _pool_chain_shared), + PROT_READ | PROT_WRITE, + MAP_ANON | MAP_SHARED, + -1, + 0); if (map == MAP_FAILED) { - msg_err ("cannot allocate %z bytes, aborting", size + sizeof (struct _pool_chain)); + msg_err ("cannot allocate %z bytes, aborting", size + + sizeof (struct _pool_chain)); abort (); } chain = (struct _pool_chain_shared *)map; chain->begin = ((guint8 *) chain) + sizeof (struct _pool_chain_shared); #elif defined(HAVE_MMAP_ZERO) - gint fd; + gint fd; fd = open ("/dev/zero", O_RDWR); if (fd == -1) { return NULL; } - map = mmap (NULL, size + sizeof (struct _pool_chain_shared), PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); + map = mmap (NULL, + size + sizeof (struct _pool_chain_shared), + PROT_READ | PROT_WRITE, + MAP_SHARED, + fd, + 0); if (map == MAP_FAILED) { - msg_err ("cannot allocate %z bytes, aborting", size + sizeof (struct _pool_chain)); + msg_err ("cannot allocate %z bytes, aborting", size + + sizeof (struct _pool_chain)); abort (); } chain = (struct _pool_chain_shared *)map; chain->begin = ((guint8 *) chain) + sizeof (struct _pool_chain_shared); #else -# error No mmap methods are defined +# error No mmap methods are defined #endif chain->pos = align_ptr (chain->begin, MEM_ALIGNMENT); chain->len = size; @@ -145,46 +158,59 @@ pool_chain_new_shared (gsize size) /** - * Allocate new memory poll + * Allocate new memory poll * @param size size of pool's page * @return new memory pool object */ -rspamd_mempool_t * +rspamd_mempool_t * rspamd_mempool_new (gsize size) { - rspamd_mempool_t *new; - gpointer map; + rspamd_mempool_t *new; + gpointer map; g_return_val_if_fail (size > 0, NULL); /* Allocate statistic structure if it is not allocated before */ if (mem_pool_stat == NULL) { #if defined(HAVE_MMAP_ANON) - map = mmap (NULL, sizeof (rspamd_mempool_stat_t), PROT_READ | PROT_WRITE, MAP_ANON | MAP_SHARED, -1, 0); + map = mmap (NULL, + sizeof (rspamd_mempool_stat_t), + PROT_READ | PROT_WRITE, + MAP_ANON | MAP_SHARED, + -1, + 0); if (map == MAP_FAILED) { - msg_err ("cannot allocate %z bytes, aborting", sizeof (rspamd_mempool_stat_t)); + msg_err ("cannot allocate %z bytes, aborting", + sizeof (rspamd_mempool_stat_t)); abort (); } mem_pool_stat = (rspamd_mempool_stat_t *)map; #elif defined(HAVE_MMAP_ZERO) - gint fd; + gint fd; fd = open ("/dev/zero", O_RDWR); g_assert (fd != -1); - map = mmap (NULL, sizeof (rspamd_mempool_stat_t), PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); + map = mmap (NULL, + sizeof (rspamd_mempool_stat_t), + PROT_READ | PROT_WRITE, + MAP_SHARED, + fd, + 0); if (map == MAP_FAILED) { - msg_err ("cannot allocate %z bytes, aborting", sizeof (rspamd_mempool_stat_t)); + msg_err ("cannot allocate %z bytes, aborting", + sizeof (rspamd_mempool_stat_t)); abort (); } mem_pool_stat = (rspamd_mempool_stat_t *)map; #else -# error No mmap methods are defined +# error No mmap methods are defined #endif memset (map, 0, sizeof (rspamd_mempool_stat_t)); } new = g_slice_alloc (sizeof (rspamd_mempool_t)); if (new == NULL) { - msg_err ("cannot allocate %z bytes, aborting", sizeof (rspamd_mempool_t)); + msg_err ("cannot allocate %z bytes, aborting", + sizeof (rspamd_mempool_t)); abort (); } @@ -203,12 +229,12 @@ rspamd_mempool_new (gsize size) return new; } -static void * +static void * memory_pool_alloc_common (rspamd_mempool_t * pool, gsize size, gboolean is_tmp) { - guint8 *tmp; - struct _pool_chain *new, *cur; - gint free; + guint8 *tmp; + struct _pool_chain *new, *cur; + gint free; if (pool) { POOL_MTX_LOCK (); @@ -229,8 +255,8 @@ memory_pool_alloc_common (rspamd_mempool_t * pool, gsize size, gboolean is_tmp) #endif /* Find free space in pool chain */ while (cur != NULL && - (free = pool_chain_free (cur)) < (gint)size && - cur->next != NULL) { + (free = pool_chain_free (cur)) < (gint)size && + cur->next != NULL) { cur = cur->next; } @@ -241,7 +267,8 @@ memory_pool_alloc_common (rspamd_mempool_t * pool, gsize size, gboolean is_tmp) new = pool_chain_new (pool->first_pool->len); } else { - new = pool_chain_new (size + pool->first_pool->len + MEM_ALIGNMENT); + new = pool_chain_new ( + size + pool->first_pool->len + MEM_ALIGNMENT); } /* Connect to pool subsystem */ if (is_tmp) { @@ -257,7 +284,8 @@ memory_pool_alloc_common (rspamd_mempool_t * pool, gsize size, gboolean is_tmp) } else { mem_pool_stat->oversized_chunks++; - new = pool_chain_new (size + pool->first_pool->len + MEM_ALIGNMENT); + new = pool_chain_new ( + size + pool->first_pool->len + MEM_ALIGNMENT); } /* Attach new pool to chain */ cur->next = new; @@ -284,42 +312,42 @@ memory_pool_alloc_common (rspamd_mempool_t * pool, gsize size, gboolean is_tmp) } -void * +void * rspamd_mempool_alloc (rspamd_mempool_t * pool, gsize size) { return memory_pool_alloc_common (pool, size, FALSE); } -void * +void * rspamd_mempool_alloc_tmp (rspamd_mempool_t * pool, gsize size) { return memory_pool_alloc_common (pool, size, TRUE); } -void * +void * rspamd_mempool_alloc0 (rspamd_mempool_t * pool, gsize size) { - void *pointer = rspamd_mempool_alloc (pool, size); + void *pointer = rspamd_mempool_alloc (pool, size); if (pointer) { memset (pointer, 0, size); } return pointer; } -void * +void * rspamd_mempool_alloc0_tmp (rspamd_mempool_t * pool, gsize size) { - void *pointer = rspamd_mempool_alloc_tmp (pool, size); + void *pointer = rspamd_mempool_alloc_tmp (pool, size); if (pointer) { memset (pointer, 0, size); } return pointer; } -void * +void * rspamd_mempool_alloc0_shared (rspamd_mempool_t * pool, gsize size) { - void *pointer = rspamd_mempool_alloc_shared (pool, size); + void *pointer = rspamd_mempool_alloc_shared (pool, size); if (pointer) { memset (pointer, 0, size); } @@ -334,7 +362,7 @@ rspamd_mempool_alloc_shared (rspamd_mempool_t * pool, gsize size) gint free; if (pool) { - g_return_val_if_fail(size > 0, NULL); + g_return_val_if_fail (size > 0, NULL); POOL_MTX_LOCK () ; @@ -346,7 +374,7 @@ rspamd_mempool_alloc_shared (rspamd_mempool_t * pool, gsize size) /* Find free space in pool chain */ while ((free = pool_chain_free ((struct _pool_chain *) cur)) - < (gint) size && cur->next) { + < (gint) size && cur->next) { cur = cur->next; } if (free < (gint) size && cur->next == NULL) { @@ -358,7 +386,7 @@ rspamd_mempool_alloc_shared (rspamd_mempool_t * pool, gsize size) else { mem_pool_stat->oversized_chunks++; new = pool_chain_new_shared ( - size + pool->first_pool->len + MEM_ALIGNMENT); + size + pool->first_pool->len + MEM_ALIGNMENT); } /* Attach new pool to chain */ cur->next = new; @@ -370,7 +398,7 @@ rspamd_mempool_alloc_shared (rspamd_mempool_t * pool, gsize size) ; return new->begin; } - tmp = align_ptr(cur->pos, MEM_ALIGNMENT); + tmp = align_ptr (cur->pos, MEM_ALIGNMENT); cur->pos = tmp + size; POOL_MTX_UNLOCK () ; @@ -380,11 +408,11 @@ rspamd_mempool_alloc_shared (rspamd_mempool_t * pool, gsize size) } -gchar * +gchar * rspamd_mempool_strdup (rspamd_mempool_t * pool, const gchar *src) { - gsize len; - gchar *newstr; + gsize len; + gchar *newstr; if (src == NULL) { return NULL; @@ -397,10 +425,10 @@ rspamd_mempool_strdup (rspamd_mempool_t * pool, const gchar *src) return newstr; } -gchar * +gchar * rspamd_mempool_fstrdup (rspamd_mempool_t * pool, const struct f_str_s *src) { - gchar *newstr; + gchar *newstr; if (src == NULL) { return NULL; @@ -413,11 +441,11 @@ rspamd_mempool_fstrdup (rspamd_mempool_t * pool, const struct f_str_s *src) } -gchar * +gchar * rspamd_mempool_strdup_shared (rspamd_mempool_t * pool, const gchar *src) { - gsize len; - gchar *newstr; + gsize len; + gchar *newstr; if (src == NULL) { return NULL; @@ -434,10 +462,11 @@ rspamd_mempool_strdup_shared (rspamd_mempool_t * pool, const gchar *src) static struct _pool_chain_shared * memory_pool_find_pool (rspamd_mempool_t * pool, void *pointer) { - struct _pool_chain_shared *cur = pool->shared_pool; + struct _pool_chain_shared *cur = pool->shared_pool; while (cur) { - if ((guint8 *) pointer >= cur->begin && (guint8 *) pointer <= (cur->begin + cur->len)) { + if ((guint8 *) pointer >= cur->begin && (guint8 *) pointer <= + (cur->begin + cur->len)) { return cur; } cur = cur->next; @@ -466,19 +495,19 @@ __mutex_spin (rspamd_mempool_mutex_t * mutex) g_atomic_int_set (&mutex->spin, MUTEX_SPIN_COUNT); } #ifdef HAVE_ASM_PAUSE - __asm __volatile ("pause"); + __asm __volatile ("pause"); #elif defined(HAVE_SCHED_YIELD) (void)sched_yield (); #endif #if defined(HAVE_NANOSLEEP) - struct timespec ts; + struct timespec ts; ts.tv_sec = 0; ts.tv_nsec = MUTEX_SLEEP_TIME; /* Spin */ - while (nanosleep (&ts, &ts) == -1 && errno == EINTR); + while (nanosleep (&ts, &ts) == -1 && errno == EINTR) ; #else -# error No methods to spin are defined +# error No methods to spin are defined #endif return 1; } @@ -497,7 +526,7 @@ memory_pool_mutex_spin (rspamd_mempool_mutex_t * mutex) void rspamd_mempool_lock_shared (rspamd_mempool_t * pool, void *pointer) { - struct _pool_chain_shared *chain; + struct _pool_chain_shared *chain; chain = memory_pool_find_pool (pool, pointer); if (chain == NULL) { @@ -512,7 +541,7 @@ rspamd_mempool_lock_shared (rspamd_mempool_t * pool, void *pointer) void rspamd_mempool_unlock_shared (rspamd_mempool_t * pool, void *pointer) { - struct _pool_chain_shared *chain; + struct _pool_chain_shared *chain; chain = memory_pool_find_pool (pool, pointer); if (chain == NULL) { @@ -527,10 +556,13 @@ rspamd_mempool_unlock_shared (rspamd_mempool_t * pool, void *pointer) } void -rspamd_mempool_add_destructor_full (rspamd_mempool_t * pool, rspamd_mempool_destruct_t func, void *data, - const gchar *function, const gchar *line) +rspamd_mempool_add_destructor_full (rspamd_mempool_t * pool, + rspamd_mempool_destruct_t func, + void *data, + const gchar *function, + const gchar *line) { - struct _pool_destructors *cur; + struct _pool_destructors *cur; cur = rspamd_mempool_alloc (pool, sizeof (struct _pool_destructors)); if (cur) { @@ -546,9 +578,12 @@ rspamd_mempool_add_destructor_full (rspamd_mempool_t * pool, rspamd_mempool_dest } void -rspamd_mempool_replace_destructor (rspamd_mempool_t * pool, rspamd_mempool_destruct_t func, void *old_data, void *new_data) +rspamd_mempool_replace_destructor (rspamd_mempool_t * pool, + rspamd_mempool_destruct_t func, + void *old_data, + void *new_data) { - struct _pool_destructors *tmp; + struct _pool_destructors *tmp; tmp = pool->destructors; while (tmp) { @@ -565,9 +600,9 @@ rspamd_mempool_replace_destructor (rspamd_mempool_t * pool, rspamd_mempool_destr void rspamd_mempool_delete (rspamd_mempool_t * pool) { - struct _pool_chain *cur = pool->first_pool, *tmp; - struct _pool_chain_shared *cur_shared = pool->shared_pool, *tmp_shared; - struct _pool_destructors *destructor = pool->destructors; + struct _pool_chain *cur = pool->first_pool, *tmp; + struct _pool_chain_shared *cur_shared = pool->shared_pool, *tmp_shared; + struct _pool_destructors *destructor = pool->destructors; POOL_MTX_LOCK (); /* Call all pool destructors */ @@ -609,7 +644,8 @@ rspamd_mempool_delete (rspamd_mempool_t * pool) mem_pool_stat->chunks_freed++; mem_pool_stat->bytes_allocated -= tmp_shared->len; STAT_UNLOCK (); - munmap ((void *)tmp_shared, tmp_shared->len + sizeof (struct _pool_chain_shared)); + munmap ((void *)tmp_shared, tmp_shared->len + + sizeof (struct _pool_chain_shared)); } if (pool->variables) { g_hash_table_destroy (pool->variables); @@ -622,9 +658,9 @@ rspamd_mempool_delete (rspamd_mempool_t * pool) } void -rspamd_mempool_cleanup_tmp (rspamd_mempool_t* pool) +rspamd_mempool_cleanup_tmp (rspamd_mempool_t * pool) { - struct _pool_chain *cur = pool->first_pool, *tmp; + struct _pool_chain *cur = pool->first_pool, *tmp; POOL_MTX_LOCK (); cur = pool->first_pool_tmp; @@ -677,12 +713,13 @@ rspamd_mempool_suggest_size (void) #endif } -rspamd_mempool_mutex_t * +rspamd_mempool_mutex_t * rspamd_mempool_get_mutex (rspamd_mempool_t * pool) { - rspamd_mempool_mutex_t *res; + rspamd_mempool_mutex_t *res; if (pool != NULL) { - res = rspamd_mempool_alloc_shared (pool, sizeof (rspamd_mempool_mutex_t)); + res = + rspamd_mempool_alloc_shared (pool, sizeof (rspamd_mempool_mutex_t)); res->lock = 0; res->owner = 0; res->spin = MUTEX_SPIN_COUNT; @@ -705,10 +742,10 @@ rspamd_mempool_unlock_mutex (rspamd_mempool_mutex_t * mutex) (void)g_atomic_int_compare_and_exchange (&mutex->lock, 1, 0); } -rspamd_mempool_rwlock_t * +rspamd_mempool_rwlock_t * rspamd_mempool_get_rwlock (rspamd_mempool_t * pool) { - rspamd_mempool_rwlock_t *lock; + rspamd_mempool_rwlock_t *lock; lock = rspamd_mempool_alloc_shared (pool, sizeof (rspamd_mempool_rwlock_t)); lock->__r_lock = rspamd_mempool_get_mutex (pool); @@ -757,14 +794,18 @@ rspamd_mempool_wunlock_rwlock (rspamd_mempool_rwlock_t * lock) rspamd_mempool_unlock_mutex (lock->__w_lock); } -void -rspamd_mempool_set_variable (rspamd_mempool_t *pool, const gchar *name, gpointer value, rspamd_mempool_destruct_t destructor) +void +rspamd_mempool_set_variable (rspamd_mempool_t *pool, + const gchar *name, + gpointer value, + rspamd_mempool_destruct_t destructor) { if (pool->variables == NULL) { pool->variables = g_hash_table_new (rspamd_str_hash, rspamd_str_equal); } - g_hash_table_insert (pool->variables, rspamd_mempool_strdup (pool, name), value); + g_hash_table_insert (pool->variables, rspamd_mempool_strdup (pool, + name), value); if (destructor != NULL) { rspamd_mempool_add_destructor (pool, destructor, value); } diff --git a/src/libutil/mem_pool.h b/src/libutil/mem_pool.h index 17edf2fb2..2a8546ef0 100644 --- a/src/libutil/mem_pool.h +++ b/src/libutil/mem_pool.h @@ -19,10 +19,10 @@ struct f_str_s; #define MEM_ALIGNMENT sizeof(unsigned long) /* platform word */ #define align_ptr(p, a) \ - (guint8 *) (((uintptr_t) (p) + ((uintptr_t) a - 1)) & ~((uintptr_t) a - 1)) + (guint8 *) (((uintptr_t) (p) + ((uintptr_t) a - 1)) & ~((uintptr_t) a - 1)) -/** - * Destructor type definition +/** + * Destructor type definition */ typedef void (*rspamd_mempool_destruct_t)(void *ptr); @@ -39,10 +39,10 @@ typedef struct memory_pool_mutex_s { * Pool page structure */ struct _pool_chain { - guint8 *begin; /**< begin of pool chain block */ - guint8 *pos; /**< current start of free space in block */ - gsize len; /**< length of block */ - struct _pool_chain *next; /**< chain link */ + guint8 *begin; /**< begin of pool chain block */ + guint8 *pos; /**< current start of free space in block */ + gsize len; /**< length of block */ + struct _pool_chain *next; /**< chain link */ }; /** @@ -60,11 +60,11 @@ struct _pool_chain_shared { * Destructors list item structure */ struct _pool_destructors { - rspamd_mempool_destruct_t func; /**< pointer to destructor */ - void *data; /**< data to free */ - const gchar *function; /**< function from which this destructor was added */ - const gchar *loc; /**< line number */ - struct _pool_destructors *prev; /**< chain link */ + rspamd_mempool_destruct_t func; /**< pointer to destructor */ + void *data; /**< data to free */ + const gchar *function; /**< function from which this destructor was added */ + const gchar *loc; /**< line number */ + struct _pool_destructors *prev; /**< chain link */ }; /** @@ -72,51 +72,51 @@ struct _pool_destructors { */ struct rspamd_mutex_s; typedef struct memory_pool_s { - struct _pool_chain *cur_pool; /**< currently used page */ - struct _pool_chain *first_pool; /**< first page */ - struct _pool_chain *cur_pool_tmp; /**< currently used temporary page */ - struct _pool_chain *first_pool_tmp; /**< first temporary page */ - struct _pool_chain_shared *shared_pool; /**< shared chain */ - struct _pool_destructors *destructors; /**< destructors chain */ - GHashTable *variables; /**< private memory pool variables */ - struct rspamd_mutex_s *mtx; /**< threads lock */ + struct _pool_chain *cur_pool; /**< currently used page */ + struct _pool_chain *first_pool; /**< first page */ + struct _pool_chain *cur_pool_tmp; /**< currently used temporary page */ + struct _pool_chain *first_pool_tmp; /**< first temporary page */ + struct _pool_chain_shared *shared_pool; /**< shared chain */ + struct _pool_destructors *destructors; /**< destructors chain */ + GHashTable *variables; /**< private memory pool variables */ + struct rspamd_mutex_s *mtx; /**< threads lock */ } rspamd_mempool_t; /** * Statistics structure */ typedef struct memory_pool_stat_s { - gsize pools_allocated; /**< total number of allocated pools */ - gsize pools_freed; /**< number of freed pools */ - gsize bytes_allocated; /**< bytes that are allocated with pool allocator */ - gsize chunks_allocated; /**< number of chunks that are allocated */ - gsize shared_chunks_allocated; /**< shared chunks allocated */ - gsize chunks_freed; /**< chunks freed */ - gsize oversized_chunks; /**< oversized chunks */ + gsize pools_allocated; /**< total number of allocated pools */ + gsize pools_freed; /**< number of freed pools */ + gsize bytes_allocated; /**< bytes that are allocated with pool allocator */ + gsize chunks_allocated; /**< number of chunks that are allocated */ + gsize shared_chunks_allocated; /**< shared chunks allocated */ + gsize chunks_freed; /**< chunks freed */ + gsize oversized_chunks; /**< oversized chunks */ } rspamd_mempool_stat_t; /** * Rwlock for locking shared memory regions */ typedef struct memory_pool_rwlock_s { - rspamd_mempool_mutex_t *__r_lock; /**< read mutex (private) */ - rspamd_mempool_mutex_t *__w_lock; /**< write mutex (private) */ + rspamd_mempool_mutex_t *__r_lock; /**< read mutex (private) */ + rspamd_mempool_mutex_t *__w_lock; /**< write mutex (private) */ } rspamd_mempool_rwlock_t; /** - * Allocate new memory poll + * Allocate new memory poll * @param size size of pool's page * @return new memory pool object */ -rspamd_mempool_t* rspamd_mempool_new (gsize size); +rspamd_mempool_t * rspamd_mempool_new (gsize size); -/** +/** * Get memory from pool * @param pool memory pool object * @param size bytes to allocate * @return pointer to allocated object */ -void* rspamd_mempool_alloc (rspamd_mempool_t* pool, gsize size); +void * rspamd_mempool_alloc (rspamd_mempool_t * pool, gsize size); /** * Get memory from temporary pool @@ -124,7 +124,7 @@ void* rspamd_mempool_alloc (rspamd_mempool_t* pool, gsize size); * @param size bytes to allocate * @return pointer to allocated object */ -void* rspamd_mempool_alloc_tmp (rspamd_mempool_t* pool, gsize size); +void * rspamd_mempool_alloc_tmp (rspamd_mempool_t * pool, gsize size); /** * Get memory and set it to zero @@ -132,7 +132,7 @@ void* rspamd_mempool_alloc_tmp (rspamd_mempool_t* pool, gsize size); * @param size bytes to allocate * @return pointer to allocated object */ -void* rspamd_mempool_alloc0 (rspamd_mempool_t* pool, gsize size); +void * rspamd_mempool_alloc0 (rspamd_mempool_t * pool, gsize size); /** * Get memory and set it to zero @@ -140,12 +140,12 @@ void* rspamd_mempool_alloc0 (rspamd_mempool_t* pool, gsize size); * @param size bytes to allocate * @return pointer to allocated object */ -void* rspamd_mempool_alloc0_tmp (rspamd_mempool_t* pool, gsize size); +void * rspamd_mempool_alloc0_tmp (rspamd_mempool_t * pool, gsize size); /** * Cleanup temporary data in pool */ -void rspamd_mempool_cleanup_tmp (rspamd_mempool_t* pool); +void rspamd_mempool_cleanup_tmp (rspamd_mempool_t * pool); /** * Make a copy of string in pool @@ -153,7 +153,7 @@ void rspamd_mempool_cleanup_tmp (rspamd_mempool_t* pool); * @param src source string * @return pointer to newly created string that is copy of src */ -gchar* rspamd_mempool_strdup (rspamd_mempool_t* pool, const gchar *src); +gchar * rspamd_mempool_strdup (rspamd_mempool_t * pool, const gchar *src); /** * Make a copy of fixed string in pool as null terminated string @@ -161,16 +161,18 @@ gchar* rspamd_mempool_strdup (rspamd_mempool_t* pool, const gchar *src); * @param src source string * @return pointer to newly created string that is copy of src */ -gchar* rspamd_mempool_fstrdup (rspamd_mempool_t* pool, const struct f_str_s *src); +gchar * rspamd_mempool_fstrdup (rspamd_mempool_t * pool, + const struct f_str_s *src); /** * Allocate piece of shared memory * @param pool memory pool object * @param size bytes to allocate */ -void* rspamd_mempool_alloc_shared (rspamd_mempool_t* pool, gsize size); -void* rspamd_mempool_alloc0_shared (rspamd_mempool_t *pool, gsize size); -gchar* rspamd_mempool_strdup_shared (rspamd_mempool_t* pool, const gchar *src); +void * rspamd_mempool_alloc_shared (rspamd_mempool_t * pool, gsize size); +void * rspamd_mempool_alloc0_shared (rspamd_mempool_t *pool, gsize size); +gchar * rspamd_mempool_strdup_shared (rspamd_mempool_t * pool, + const gchar *src); /** * Lock chunk of shared memory in which pointer is placed @@ -192,12 +194,15 @@ void rspamd_mempool_lock_shared (rspamd_mempool_t *pool, void *pointer); * @param func pointer to function-destructor * @param data pointer to data that would be passed to destructor */ -void rspamd_mempool_add_destructor_full (rspamd_mempool_t *pool, rspamd_mempool_destruct_t func, void *data, - const gchar *function, const gchar *line); +void rspamd_mempool_add_destructor_full (rspamd_mempool_t *pool, + rspamd_mempool_destruct_t func, + void *data, + const gchar *function, + const gchar *line); /* Macros for common usage */ #define rspamd_mempool_add_destructor(pool, func, data) \ - rspamd_mempool_add_destructor_full(pool, func, data, G_STRFUNC, G_STRLOC) + rspamd_mempool_add_destructor_full (pool, func, data, G_STRFUNC, G_STRLOC) /** * Replace destructor callback to pool for specified pointer @@ -207,7 +212,7 @@ void rspamd_mempool_add_destructor_full (rspamd_mempool_t *pool, rspamd_mempool_ * @param new_data pointer to data that would be passed to destructor */ void rspamd_mempool_replace_destructor (rspamd_mempool_t *pool, - rspamd_mempool_destruct_t func, void *old_data, void *new_data); + rspamd_mempool_destruct_t func, void *old_data, void *new_data); /** * Delete pool, free all its chunks and call destructors chain @@ -215,12 +220,12 @@ void rspamd_mempool_replace_destructor (rspamd_mempool_t *pool, */ void rspamd_mempool_delete (rspamd_mempool_t *pool); -/** +/** * Get new mutex from pool (allocated in shared memory) * @param pool memory pool object * @return mutex object */ -rspamd_mempool_mutex_t* rspamd_mempool_get_mutex (rspamd_mempool_t *pool); +rspamd_mempool_mutex_t * rspamd_mempool_get_mutex (rspamd_mempool_t *pool); /** * Lock mutex @@ -239,7 +244,7 @@ void rspamd_mempool_unlock_mutex (rspamd_mempool_mutex_t *mutex); * @param pool memory pool object * @return rwlock object */ -rspamd_mempool_rwlock_t* rspamd_mempool_get_rwlock (rspamd_mempool_t *pool); +rspamd_mempool_rwlock_t * rspamd_mempool_get_rwlock (rspamd_mempool_t *pool); /** * Aquire read lock @@ -290,7 +295,7 @@ gsize rspamd_mempool_suggest_size (void); * @param destructor pointer to function-destructor */ void rspamd_mempool_set_variable (rspamd_mempool_t *pool, const gchar *name, - gpointer value, rspamd_mempool_destruct_t destructor); + gpointer value, rspamd_mempool_destruct_t destructor); /** * Get memory pool variable @@ -298,7 +303,8 @@ void rspamd_mempool_set_variable (rspamd_mempool_t *pool, const gchar *name, * @param name name of variable * @return NULL or pointer to variable data */ -gpointer rspamd_mempool_get_variable (rspamd_mempool_t *pool, const gchar *name); +gpointer rspamd_mempool_get_variable (rspamd_mempool_t *pool, + const gchar *name); #endif diff --git a/src/libutil/printf.c b/src/libutil/printf.c index ba30587fc..d40b74bca 100644 --- a/src/libutil/printf.c +++ b/src/libutil/printf.c @@ -21,9 +21,9 @@ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ -#include "printf.h" #include "fstring.h" #include "main.h" +#include "printf.h" /** * From FreeBSD libutil code @@ -90,13 +90,13 @@ rspamd_humanize_number (gchar *buf, gchar *last, gint64 num, gboolean bytes) static gchar * rspamd_sprintf_num (gchar *buf, gchar *last, guint64 ui64, gchar zero, - guint hexadecimal, guint width) + guint hexadecimal, guint width) { - gchar *p, temp[sizeof ("18446744073709551615")]; - size_t len; - guint32 ui32; - static gchar hex[] = "0123456789abcdef"; - static gchar HEX[] = "0123456789ABCDEF"; + gchar *p, temp[sizeof ("18446744073709551615")]; + size_t len; + guint32 ui32; + static gchar hex[] = "0123456789abcdef"; + static gchar HEX[] = "0123456789ABCDEF"; p = temp + sizeof(temp); @@ -220,7 +220,7 @@ rspamd_printf_append_gstring (const gchar *buf, glong buflen, gpointer ud) glong rspamd_fprintf (FILE *f, const gchar *fmt, ...) { - va_list args; + va_list args; glong r; va_start (args, fmt); @@ -233,7 +233,7 @@ rspamd_fprintf (FILE *f, const gchar *fmt, ...) glong rspamd_printf (const gchar *fmt, ...) { - va_list args; + va_list args; glong r; va_start (args, fmt); @@ -246,7 +246,7 @@ rspamd_printf (const gchar *fmt, ...) glong rspamd_log_fprintf (FILE *f, const gchar *fmt, ...) { - va_list args; + va_list args; glong r; va_start (args, fmt); @@ -299,31 +299,34 @@ rspamd_printf_gstring (GString *s, const gchar *fmt, ...) return r; } -#define RSPAMD_PRINTF_APPEND(buf, len) \ - do { \ - wr = func ((buf), (len), apd); \ - if (wr < (__typeof(wr))(len)) { \ - goto oob; \ - } \ - written += wr; \ - fmt ++; \ - buf_start = fmt; \ - } while(0) +#define RSPAMD_PRINTF_APPEND(buf, len) \ + do { \ + wr = func ((buf), (len), apd); \ + if (wr < (__typeof (wr))(len)) { \ + goto oob; \ + } \ + written += wr; \ + fmt++; \ + buf_start = fmt; \ + } while (0) glong -rspamd_vprintf_common (rspamd_printf_append_func func, gpointer apd, const gchar *fmt, va_list args) +rspamd_vprintf_common (rspamd_printf_append_func func, + gpointer apd, + const gchar *fmt, + va_list args) { - gchar zero, numbuf[G_ASCII_DTOSTR_BUF_SIZE], *p, *last, c; - const gchar *buf_start = fmt; - gint d; - long double f, scale; - glong written = 0, wr, slen; - gint64 i64; - guint64 ui64; - guint width, sign, hex, humanize, bytes, frac_width, i; - f_str_t *v; - GString *gs; - gboolean bv; + gchar zero, numbuf[G_ASCII_DTOSTR_BUF_SIZE], *p, *last, c; + const gchar *buf_start = fmt; + gint d; + long double f, scale; + glong written = 0, wr, slen; + gint64 i64; + guint64 ui64; + guint width, sign, hex, humanize, bytes, frac_width, i; + f_str_t *v; + GString *gs; + gboolean bv; while (*fmt) { @@ -360,7 +363,7 @@ rspamd_vprintf_common (rspamd_printf_append_func func, gpointer apd, const gchar } - for ( ;; ) { + for (;; ) { switch (*fmt) { case 'u': @@ -387,12 +390,12 @@ rspamd_vprintf_common (rspamd_printf_append_func func, gpointer apd, const gchar humanize = 1; bytes = 1; sign = 0; - fmt ++; + fmt++; continue; case 'h': humanize = 1; sign = 0; - fmt ++; + fmt++; continue; case '.': fmt++; @@ -482,17 +485,17 @@ rspamd_vprintf_common (rspamd_printf_append_func func, gpointer apd, const gchar case 'l': if (sign) { - i64 = (gint64) va_arg(args, glong); + i64 = (gint64) va_arg (args, glong); } else { - ui64 = (guint64) va_arg(args, gulong); + ui64 = (guint64) va_arg (args, gulong); } break; case 'D': if (sign) { - i64 = (gint64) va_arg(args, gint32); + i64 = (gint64) va_arg (args, gint32); } else { - ui64 = (guint64) va_arg(args, guint32); + ui64 = (guint64) va_arg (args, guint32); } break; @@ -540,9 +543,9 @@ rspamd_vprintf_common (rspamd_printf_append_func func, gpointer apd, const gchar } /* - * (gint64) cast is required for msvc6: - * it can not convert guint64 to double - */ + * (gint64) cast is required for msvc6: + * it can not convert guint64 to double + */ ui64 = (guint64) ((f - (gint64) ui64) * scale); p = rspamd_sprintf_num (p, last, ui64, '0', 0, frac_width); @@ -620,7 +623,7 @@ rspamd_vprintf_common (rspamd_printf_append_func func, gpointer apd, const gchar if (sign) { if (i64 < 0) { *p++ = '-'; - ui64 = (guint64) -i64; + ui64 = (guint64) - i64; } else { ui64 = (guint64) i64; diff --git a/src/libutil/printf.h b/src/libutil/printf.h index bd5c62cb6..aee982e41 100644 --- a/src/libutil/printf.h +++ b/src/libutil/printf.h @@ -62,15 +62,20 @@ * @param ud opaque pointer * @return number of characters written */ -typedef glong (*rspamd_printf_append_func)(const gchar *buf, glong buflen, gpointer ud); +typedef glong (*rspamd_printf_append_func)(const gchar *buf, glong buflen, + gpointer ud); glong rspamd_fprintf (FILE *f, const gchar *fmt, ...); glong rspamd_printf (const gchar *fmt, ...); glong rspamd_log_fprintf (FILE *f, const gchar *fmt, ...); glong rspamd_snprintf (gchar *buf, glong max, const gchar *fmt, ...); -gchar *rspamd_vsnprintf (gchar *buf, glong max, const gchar *fmt, va_list args); +gchar * rspamd_vsnprintf (gchar *buf, glong max, const gchar *fmt, + va_list args); glong rspamd_printf_gstring (GString *s, const gchar *fmt, ...); -glong rspamd_vprintf_common (rspamd_printf_append_func func, gpointer apd, const gchar *fmt, va_list args); +glong rspamd_vprintf_common (rspamd_printf_append_func func, + gpointer apd, + const gchar *fmt, + va_list args); #endif /* PRINTF_H_ */ diff --git a/src/libutil/radix.c b/src/libutil/radix.c index 7af665331..c33ac0d05 100644 --- a/src/libutil/radix.c +++ b/src/libutil/radix.c @@ -24,15 +24,15 @@ #include "config.h" -#include "radix.h" #include "mem_pool.h" +#include "radix.h" -static void *radix_alloc (radix_tree_t * tree); +static void * radix_alloc (radix_tree_t * tree); -radix_tree_t * +radix_tree_t * radix_tree_create (void) { - radix_tree_t *tree; + radix_tree_t *tree; tree = g_malloc (sizeof (radix_tree_t)); if (tree == NULL) { @@ -62,10 +62,14 @@ enum radix_insert_type { }; static uintptr_t -radix32tree_insert_common (radix_tree_t * tree, guint32 key, guint32 mask, uintptr_t value, enum radix_insert_type type) +radix32tree_insert_common (radix_tree_t * tree, + guint32 key, + guint32 mask, + uintptr_t value, + enum radix_insert_type type) { - guint32 bit; - radix_node_t *node, *next; + guint32 bit; + radix_node_t *node, *next; bit = 0x80000000; @@ -92,14 +96,14 @@ radix32tree_insert_common (radix_tree_t * tree, guint32 key, guint32 mask, uintp if (node->value != RADIX_NO_VALUE) { /* Value was found, switch on insert type */ switch (type) { - case RADIX_INSERT: - return 1; - case RADIX_ADD: - node->value += value; - return value; - case RADIX_REPLACE: - node->value = value; - return 1; + case RADIX_INSERT: + return 1; + case RADIX_ADD: + node->value += value; + return value; + case RADIX_REPLACE: + node->value = value; + return 1; } } @@ -137,22 +141,33 @@ radix32tree_insert_common (radix_tree_t * tree, guint32 key, guint32 mask, uintp return 0; } -gint -radix32tree_insert (radix_tree_t *tree, guint32 key, guint32 mask, uintptr_t value) +gint +radix32tree_insert (radix_tree_t *tree, + guint32 key, + guint32 mask, + uintptr_t value) { - return (gint)radix32tree_insert_common (tree, key, mask, value, RADIX_INSERT); + return (gint)radix32tree_insert_common (tree, key, mask, value, + RADIX_INSERT); } -uintptr_t +uintptr_t radix32tree_add (radix_tree_t *tree, guint32 key, guint32 mask, uintptr_t value) { return radix32tree_insert_common (tree, key, mask, value, RADIX_ADD); } -gint -radix32tree_replace (radix_tree_t *tree, guint32 key, guint32 mask, uintptr_t value) +gint +radix32tree_replace (radix_tree_t *tree, + guint32 key, + guint32 mask, + uintptr_t value) { - return (gint)radix32tree_insert_common (tree, key, mask, value, RADIX_REPLACE); + return (gint)radix32tree_insert_common (tree, + key, + mask, + value, + RADIX_REPLACE); } /* @@ -162,14 +177,17 @@ radix32tree_replace (radix_tree_t *tree, guint32 key, guint32 mask, uintptr_t va * 5 words total in stack */ static gboolean -radix_recurse_nodes (radix_node_t *node, radix_tree_traverse_func func, void *user_data, gint level) +radix_recurse_nodes (radix_node_t *node, + radix_tree_traverse_func func, + void *user_data, + gint level) { if (node->left) { if (radix_recurse_nodes (node->left, func, user_data, level + 1)) { return TRUE; } } - + if (node->value != RADIX_NO_VALUE) { if (func (node->key, level, node->value, user_data)) { return TRUE; @@ -186,17 +204,19 @@ radix_recurse_nodes (radix_node_t *node, radix_tree_traverse_func func, void *us } void -radix32tree_traverse (radix_tree_t *tree, radix_tree_traverse_func func, void *user_data) +radix32tree_traverse (radix_tree_t *tree, + radix_tree_traverse_func func, + void *user_data) { - radix_recurse_nodes (tree->root, func, user_data, 0); + radix_recurse_nodes (tree->root, func, user_data, 0); } gint radix32tree_delete (radix_tree_t * tree, guint32 key, guint32 mask) { - guint32 bit; - radix_node_t *node; + guint32 bit; + radix_node_t *node; bit = 0x80000000; node = tree->root; @@ -226,7 +246,7 @@ radix32tree_delete (radix_tree_t * tree, guint32 key, guint32 mask) return -1; } - for (;;) { + for (;; ) { if (node->parent->right == node) { node->parent->right = NULL; @@ -257,9 +277,9 @@ radix32tree_delete (radix_tree_t * tree, guint32 key, guint32 mask) uintptr_t radix32tree_find (radix_tree_t * tree, guint32 key) { - guint32 bit; - uintptr_t value; - radix_node_t *node; + guint32 bit; + uintptr_t value; + radix_node_t *node; bit = 0x80000000; value = RADIX_NO_VALUE; @@ -285,10 +305,10 @@ radix32tree_find (radix_tree_t * tree, guint32 key) } -static void * +static void * radix_alloc (radix_tree_t * tree) { - gchar *p; + gchar *p; p = rspamd_mempool_alloc (tree->pool, sizeof (radix_node_t)); @@ -316,6 +336,6 @@ radix32_tree_find_addr (radix_tree_t *tree, rspamd_inet_addr_t *addr) return radix32tree_find (tree, ntohl (addr->addr.s4.sin_addr.s_addr)); } -/* - * vi:ts=4 +/* + * vi:ts=4 */ diff --git a/src/libutil/radix.h b/src/libutil/radix.h index 88f345e3d..cfdefa25b 100644 --- a/src/libutil/radix.h +++ b/src/libutil/radix.h @@ -7,29 +7,30 @@ #define RADIX_NO_VALUE (uintptr_t)-1 -typedef struct radix_node_s radix_node_t; +typedef struct radix_node_s radix_node_t; struct radix_node_s { - radix_node_t *right; - radix_node_t *left; - radix_node_t *parent; - uintptr_t value; + radix_node_t *right; + radix_node_t *left; + radix_node_t *parent; + uintptr_t value; guint32 key; }; typedef struct { - radix_node_t *root; - size_t size; + radix_node_t *root; + size_t size; rspamd_mempool_t *pool; } radix_tree_t; -typedef gboolean (*radix_tree_traverse_func)(guint32 key, guint32 mask, uintptr_t value, void *user_data); +typedef gboolean (*radix_tree_traverse_func)(guint32 key, guint32 mask, + uintptr_t value, void *user_data); /** * Create new radix tree */ -radix_tree_t *radix_tree_create (void); +radix_tree_t * radix_tree_create (void); /** * Insert value to radix tree @@ -37,7 +38,10 @@ radix_tree_t *radix_tree_create (void); * 0 if operation was successfull * -1 if there was some error */ -gint radix32tree_insert (radix_tree_t *tree, guint32 key, guint32 mask, uintptr_t value); +gint radix32tree_insert (radix_tree_t *tree, + guint32 key, + guint32 mask, + uintptr_t value); /** * Add value to radix tree or insert it if value does not exists @@ -45,7 +49,10 @@ gint radix32tree_insert (radix_tree_t *tree, guint32 key, guint32 mask, uintptr_ * 0 if value was inserted * -1 if there was some error */ -uintptr_t radix32tree_add (radix_tree_t *tree, guint32 key, guint32 mask, uintptr_t value); +uintptr_t radix32tree_add (radix_tree_t *tree, + guint32 key, + guint32 mask, + uintptr_t value); /** * Replace value in radix tree or insert it if value does not exists @@ -53,7 +60,10 @@ uintptr_t radix32tree_add (radix_tree_t *tree, guint32 key, guint32 mask, uintpt * 0 if value was inserted * -1 if there was some error */ -gint radix32tree_replace (radix_tree_t *tree, guint32 key, guint32 mask, uintptr_t value); +gint radix32tree_replace (radix_tree_t *tree, + guint32 key, + guint32 mask, + uintptr_t value); /** * Delete value from radix tree @@ -81,7 +91,9 @@ uintptr_t radix32_tree_find_addr (radix_tree_t *tree, rspamd_inet_addr_t *addr); /** * Traverse via the whole tree calling specified callback */ -void radix32tree_traverse (radix_tree_t *tree, radix_tree_traverse_func func, void *user_data); +void radix32tree_traverse (radix_tree_t *tree, + radix_tree_traverse_func func, + void *user_data); /** * Frees radix tree diff --git a/src/libutil/rrd.c b/src/libutil/rrd.c index a0e21eaed..fa9324dc4 100644 --- a/src/libutil/rrd.c +++ b/src/libutil/rrd.c @@ -58,7 +58,7 @@ rrd_dst_from_string (const gchar *str) /** * Convert numeric presentation of dst to string */ -const gchar* +const gchar * rrd_dst_to_string (enum rrd_dst_type type) { switch (type) { @@ -105,7 +105,7 @@ rrd_cf_from_string (const gchar *str) /** * Convert numeric presentation of cf to string */ -const gchar* +const gchar * rrd_cf_to_string (enum rrd_cf_type type) { switch (type) { @@ -127,7 +127,10 @@ rrd_cf_to_string (enum rrd_cf_type type) } void -rrd_make_default_rra (const gchar *cf_name, gulong pdp_cnt, gulong rows, struct rrd_rra_def *rra) +rrd_make_default_rra (const gchar *cf_name, + gulong pdp_cnt, + gulong rows, + struct rrd_rra_def *rra) { rra->pdp_cnt = pdp_cnt; rra->row_cnt = rows; @@ -139,8 +142,8 @@ rrd_make_default_rra (const gchar *cf_name, gulong pdp_cnt, gulong rows, struct void rrd_make_default_ds (const gchar *name, gulong pdp_step, struct rrd_ds_def *ds) { - rspamd_strlcpy (ds->ds_nam, name, sizeof (ds->ds_nam)); - rspamd_strlcpy (ds->dst, "COUNTER", sizeof (ds->dst)); + rspamd_strlcpy (ds->ds_nam, name, sizeof (ds->ds_nam)); + rspamd_strlcpy (ds->dst, "COUNTER", sizeof (ds->dst)); memset (ds->par, 0, sizeof (ds->par)); ds->par[RRD_DS_mrhb_cnt].lv = pdp_step * 2; ds->par[RRD_DS_min_val].dv = NAN; @@ -153,74 +156,90 @@ rrd_make_default_ds (const gchar *name, gulong pdp_step, struct rrd_ds_def *ds) static gboolean rspamd_rrd_check_file (const gchar *filename, gboolean need_data, GError **err) { - gint fd, i; - struct stat st; - struct rrd_file_head head; - struct rrd_rra_def rra; - gint head_size; + gint fd, i; + struct stat st; + struct rrd_file_head head; + struct rrd_rra_def rra; + gint head_size; fd = open (filename, O_RDWR); if (fd == -1) { - g_set_error (err, rrd_error_quark (), errno, "rrd open error: %s", strerror (errno)); + g_set_error (err, + rrd_error_quark (), errno, "rrd open error: %s", strerror (errno)); return FALSE; } if (fstat (fd, &st) == -1) { - g_set_error (err, rrd_error_quark (), errno, "rrd stat error: %s", strerror (errno)); + g_set_error (err, + rrd_error_quark (), errno, "rrd stat error: %s", strerror (errno)); close (fd); return FALSE; } if (st.st_size < (goffset)sizeof (struct rrd_file_head)) { /* We have trimmed file */ - g_set_error (err, rrd_error_quark (), EINVAL, "rrd size is bad: %ud", (guint)st.st_size); + g_set_error (err, rrd_error_quark (), EINVAL, "rrd size is bad: %ud", + (guint)st.st_size); close (fd); return FALSE; } /* Try to read header */ if (read (fd, &head, sizeof (head)) != sizeof (head)) { - g_set_error (err, rrd_error_quark (), errno, "rrd read head error: %s", strerror (errno)); + g_set_error (err, + rrd_error_quark (), errno, "rrd read head error: %s", + strerror (errno)); close (fd); return FALSE; } /* Check magic */ if (memcmp (head.cookie, RRD_COOKIE, sizeof (head.cookie)) != 0 || - memcmp (head.version, RRD_VERSION, sizeof (head.version)) != 0 || - head.float_cookie != RRD_FLOAT_COOKIE) { - g_set_error (err, rrd_error_quark (), EINVAL, "rrd head cookies error: %s", strerror (errno)); + memcmp (head.version, RRD_VERSION, sizeof (head.version)) != 0 || + head.float_cookie != RRD_FLOAT_COOKIE) { + g_set_error (err, + rrd_error_quark (), EINVAL, "rrd head cookies error: %s", + strerror (errno)); close (fd); return FALSE; } /* Check for other params */ if (head.ds_cnt <= 0 || head.rra_cnt <= 0) { - g_set_error (err, rrd_error_quark (), EINVAL, "rrd head cookies error: %s", strerror (errno)); + g_set_error (err, + rrd_error_quark (), EINVAL, "rrd head cookies error: %s", + strerror (errno)); close (fd); return FALSE; } /* Now we can calculate the overall size of rrd */ head_size = sizeof (struct rrd_file_head) + - sizeof (struct rrd_ds_def) * head.ds_cnt + - sizeof (struct rrd_rra_def) * head.rra_cnt + - sizeof (struct rrd_live_head) + - sizeof (struct rrd_pdp_prep) * head.ds_cnt + - sizeof (struct rrd_cdp_prep) * head.ds_cnt * head.rra_cnt + - sizeof (struct rrd_rra_ptr) * head.rra_cnt; + sizeof (struct rrd_ds_def) * head.ds_cnt + + sizeof (struct rrd_rra_def) * head.rra_cnt + + sizeof (struct rrd_live_head) + + sizeof (struct rrd_pdp_prep) * head.ds_cnt + + sizeof (struct rrd_cdp_prep) * head.ds_cnt * head.rra_cnt + + sizeof (struct rrd_rra_ptr) * head.rra_cnt; if (st.st_size < (goffset)head_size) { - g_set_error (err, rrd_error_quark (), errno, "rrd file seems to have stripped header: %d", head_size); + g_set_error (err, + rrd_error_quark (), errno, "rrd file seems to have stripped header: %d", + head_size); close (fd); return FALSE; } if (need_data) { /* Now check rra */ - if (lseek (fd, sizeof (struct rrd_ds_def) * head.ds_cnt, SEEK_CUR) == -1) { - g_set_error (err, rrd_error_quark (), errno, "rrd head lseek error: %s", strerror (errno)); + if (lseek (fd, sizeof (struct rrd_ds_def) * head.ds_cnt, + SEEK_CUR) == -1) { + g_set_error (err, + rrd_error_quark (), errno, "rrd head lseek error: %s", + strerror (errno)); close (fd); return FALSE; } - for (i = 0; i < (gint)head.rra_cnt; i ++) { + for (i = 0; i < (gint)head.rra_cnt; i++) { if (read (fd, &rra, sizeof (rra)) != sizeof (rra)) { - g_set_error (err, rrd_error_quark (), errno, "rrd read rra error: %s", strerror (errno)); + g_set_error (err, + rrd_error_quark (), errno, "rrd read rra error: %s", + strerror (errno)); close (fd); return FALSE; } @@ -228,7 +247,9 @@ rspamd_rrd_check_file (const gchar *filename, gboolean need_data, GError **err) } if (st.st_size != head_size) { - g_set_error (err, rrd_error_quark (), EINVAL, "rrd file seems to have incorrect size: %d, must be %d", (gint)st.st_size, head_size); + g_set_error (err, + rrd_error_quark (), EINVAL, "rrd file seems to have incorrect size: %d, must be %d", + (gint)st.st_size, head_size); close (fd); return FALSE; } @@ -245,7 +266,7 @@ rspamd_rrd_check_file (const gchar *filename, gboolean need_data, GError **err) static void rspamd_rrd_adjust_pointers (struct rspamd_rrd_file *file, gboolean completed) { - guint8 *ptr; + guint8 *ptr; ptr = file->map; file->stat_head = (struct rrd_file_head *)ptr; @@ -259,7 +280,8 @@ rspamd_rrd_adjust_pointers (struct rspamd_rrd_file *file, gboolean completed) file->pdp_prep = (struct rrd_pdp_prep *)ptr; ptr += sizeof (struct rrd_pdp_prep) * file->stat_head->ds_cnt; file->cdp_prep = (struct rrd_cdp_prep *)ptr; - ptr += sizeof (struct rrd_cdp_prep) * file->stat_head->rra_cnt * file->stat_head->ds_cnt; + ptr += sizeof (struct rrd_cdp_prep) * file->stat_head->rra_cnt * + file->stat_head->ds_cnt; file->rra_ptr = (struct rrd_rra_ptr *)ptr; if (completed) { ptr += sizeof (struct rrd_rra_ptr) * file->stat_head->rra_cnt; @@ -277,12 +299,12 @@ rspamd_rrd_adjust_pointers (struct rspamd_rrd_file *file, gboolean completed) * @param err * @return */ -static struct rspamd_rrd_file* +static struct rspamd_rrd_file * rspamd_rrd_open_common (const gchar *filename, gboolean completed, GError **err) { - struct rspamd_rrd_file *new; - gint fd; - struct stat st; + struct rspamd_rrd_file *new; + gint fd; + struct stat st; if (!rspamd_rrd_check_file (filename, completed, err)) { return NULL; @@ -298,20 +320,25 @@ rspamd_rrd_open_common (const gchar *filename, gboolean completed, GError **err) /* Open file */ fd = open (filename, O_RDWR); if (fd == -1) { - g_set_error (err, rrd_error_quark (), errno, "rrd open error: %s", strerror (errno)); + g_set_error (err, + rrd_error_quark (), errno, "rrd open error: %s", strerror (errno)); return FALSE; } if (fstat (fd, &st) == -1) { - g_set_error (err, rrd_error_quark (), errno, "rrd stat error: %s", strerror (errno)); + g_set_error (err, + rrd_error_quark (), errno, "rrd stat error: %s", strerror (errno)); close (fd); return FALSE; } /* Mmap file */ new->size = st.st_size; - if ((new->map = mmap (NULL, st.st_size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0)) == MAP_FAILED) { + if ((new->map = + mmap (NULL, st.st_size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, + 0)) == MAP_FAILED) { close (fd); - g_set_error (err, rrd_error_quark (), ENOMEM, "mmap failed: %s", strerror (errno)); + g_set_error (err, + rrd_error_quark (), ENOMEM, "mmap failed: %s", strerror (errno)); g_slice_free1 (sizeof (struct rspamd_rrd_file), new); return NULL; } @@ -335,7 +362,7 @@ rspamd_rrd_open_common (const gchar *filename, gboolean completed, GError **err) * @param err error pointer * @return rrd file structure */ -struct rspamd_rrd_file* +struct rspamd_rrd_file * rspamd_rrd_open (const gchar *filename, GError **err) { return rspamd_rrd_open_common (filename, TRUE, err); @@ -350,25 +377,31 @@ rspamd_rrd_open (const gchar *filename, GError **err) * @param err error pointer * @return TRUE if file has been created */ -struct rspamd_rrd_file* -rspamd_rrd_create (const gchar *filename, gulong ds_count, gulong rra_count, gulong pdp_step, GError **err) +struct rspamd_rrd_file * +rspamd_rrd_create (const gchar *filename, + gulong ds_count, + gulong rra_count, + gulong pdp_step, + GError **err) { - struct rspamd_rrd_file *new; - struct rrd_file_head head; - struct rrd_ds_def ds; - struct rrd_rra_def rra; - struct rrd_live_head lh; - struct rrd_pdp_prep pdp; - struct rrd_cdp_prep cdp; - struct rrd_rra_ptr rra_ptr; - gint fd; - guint i, j; - struct timeval tv; + struct rspamd_rrd_file *new; + struct rrd_file_head head; + struct rrd_ds_def ds; + struct rrd_rra_def rra; + struct rrd_live_head lh; + struct rrd_pdp_prep pdp; + struct rrd_cdp_prep cdp; + struct rrd_rra_ptr rra_ptr; + gint fd; + guint i, j; + struct timeval tv; /* Open file */ fd = open (filename, O_RDWR | O_CREAT | O_TRUNC, 0644); if (fd == -1) { - g_set_error (err, rrd_error_quark (), errno, "rrd create error: %s", strerror (errno)); + g_set_error (err, + rrd_error_quark (), errno, "rrd create error: %s", + strerror (errno)); return NULL; } @@ -377,13 +410,14 @@ rspamd_rrd_create (const gchar *filename, gulong ds_count, gulong rra_count, gul head.rra_cnt = rra_count; head.ds_cnt = ds_count; head.pdp_step = pdp_step; - memcpy (head.cookie, RRD_COOKIE, sizeof (head.cookie)); + memcpy (head.cookie, RRD_COOKIE, sizeof (head.cookie)); memcpy (head.version, RRD_VERSION, sizeof (head.version)); head.float_cookie = RRD_FLOAT_COOKIE; if (write (fd, &head, sizeof (head)) != sizeof (head)) { close (fd); - g_set_error (err, rrd_error_quark (), errno, "rrd write error: %s", strerror (errno)); + g_set_error (err, + rrd_error_quark (), errno, "rrd write error: %s", strerror (errno)); return NULL; } @@ -391,10 +425,12 @@ rspamd_rrd_create (const gchar *filename, gulong ds_count, gulong rra_count, gul memset (&ds.ds_nam, 0, sizeof (ds.ds_nam)); memcpy (&ds.dst, "COUNTER", sizeof ("COUNTER")); memset (&ds.par, 0, sizeof (ds.par)); - for (i = 0; i < ds_count; i ++) { + for (i = 0; i < ds_count; i++) { if (write (fd, &ds, sizeof (ds)) != sizeof (ds)) { close (fd); - g_set_error (err, rrd_error_quark (), errno, "rrd write error: %s", strerror (errno)); + g_set_error (err, + rrd_error_quark (), errno, "rrd write error: %s", + strerror (errno)); return NULL; } } @@ -403,10 +439,12 @@ rspamd_rrd_create (const gchar *filename, gulong ds_count, gulong rra_count, gul memcpy (&rra.cf_nam, "AVERAGE", sizeof ("AVERAGE")); rra.pdp_cnt = 1; memset (&rra.par, 0, sizeof (rra.par)); - for (i = 0; i < rra_count; i ++) { + for (i = 0; i < rra_count; i++) { if (write (fd, &rra, sizeof (rra)) != sizeof (rra)) { close (fd); - g_set_error (err, rrd_error_quark (), errno, "rrd write error: %s", strerror (errno)); + g_set_error (err, + rrd_error_quark (), errno, "rrd write error: %s", + strerror (errno)); return NULL; } } @@ -418,7 +456,8 @@ rspamd_rrd_create (const gchar *filename, gulong ds_count, gulong rra_count, gul if (write (fd, &lh, sizeof (lh)) != sizeof (lh)) { close (fd); - g_set_error (err, rrd_error_quark (), errno, "rrd write error: %s", strerror (errno)); + g_set_error (err, + rrd_error_quark (), errno, "rrd write error: %s", strerror (errno)); return NULL; } @@ -427,10 +466,12 @@ rspamd_rrd_create (const gchar *filename, gulong ds_count, gulong rra_count, gul memset (&pdp.scratch, 0, sizeof (pdp.scratch)); pdp.scratch[PDP_val].dv = 0.; pdp.scratch[PDP_unkn_sec_cnt].lv = 0; - for (i = 0; i < ds_count; i ++) { + for (i = 0; i < ds_count; i++) { if (write (fd, &pdp, sizeof (pdp)) != sizeof (pdp)) { close (fd); - g_set_error (err, rrd_error_quark (), errno, "rrd write error: %s", strerror (errno)); + g_set_error (err, + rrd_error_quark (), errno, "rrd write error: %s", + strerror (errno)); return NULL; } } @@ -438,12 +479,14 @@ rspamd_rrd_create (const gchar *filename, gulong ds_count, gulong rra_count, gul /* Fill cdp prep */ memset (&cdp.scratch, 0, sizeof (cdp.scratch)); cdp.scratch[CDP_val].dv = NAN; - for (i = 0; i < rra_count; i ++) { + for (i = 0; i < rra_count; i++) { cdp.scratch[CDP_unkn_pdp_cnt].lv = 0; - for (j = 0; j < ds_count; j ++) { + for (j = 0; j < ds_count; j++) { if (write (fd, &cdp, sizeof (cdp)) != sizeof (cdp)) { close (fd); - g_set_error (err, rrd_error_quark (), errno, "rrd write error: %s", strerror (errno)); + g_set_error (err, + rrd_error_quark (), errno, "rrd write error: %s", + strerror (errno)); return NULL; } } @@ -451,10 +494,12 @@ rspamd_rrd_create (const gchar *filename, gulong ds_count, gulong rra_count, gul /* Set row pointers */ memset (&rra_ptr, 0, sizeof (rra_ptr)); - for (i = 0; i < rra_count; i ++) { + for (i = 0; i < rra_count; i++) { if (write (fd, &rra_ptr, sizeof (rra_ptr)) != sizeof (rra_ptr)) { close (fd); - g_set_error (err, rrd_error_quark (), errno, "rrd write error: %s", strerror (errno)); + g_set_error (err, + rrd_error_quark (), errno, "rrd write error: %s", + strerror (errno)); return NULL; } } @@ -476,8 +521,10 @@ gboolean rspamd_rrd_add_ds (struct rspamd_rrd_file *file, GArray *ds, GError **err) { - if (file == NULL || file->stat_head->ds_cnt * sizeof (struct rrd_ds_def) != ds->len) { - g_set_error (err, rrd_error_quark (), EINVAL, "rrd add ds failed: wrong arguments"); + if (file == NULL || file->stat_head->ds_cnt * sizeof (struct rrd_ds_def) != + ds->len) { + g_set_error (err, + rrd_error_quark (), EINVAL, "rrd add ds failed: wrong arguments"); return FALSE; } @@ -497,8 +544,10 @@ rspamd_rrd_add_ds (struct rspamd_rrd_file *file, GArray *ds, GError **err) gboolean rspamd_rrd_add_rra (struct rspamd_rrd_file *file, GArray *rra, GError **err) { - if (file == NULL || file->stat_head->rra_cnt * sizeof (struct rrd_rra_def) != rra->len) { - g_set_error (err, rrd_error_quark (), EINVAL, "rrd add rra failed: wrong arguments"); + if (file == NULL || file->stat_head->rra_cnt * + sizeof (struct rrd_rra_def) != rra->len) { + g_set_error (err, + rrd_error_quark (), EINVAL, "rrd add rra failed: wrong arguments"); return FALSE; } @@ -517,31 +566,34 @@ rspamd_rrd_add_rra (struct rspamd_rrd_file *file, GArray *rra, GError **err) gboolean rspamd_rrd_finalize (struct rspamd_rrd_file *file, GError **err) { - gint fd; - guint i; - gint count = 0; - gdouble vbuf[1024]; - struct stat st; + gint fd; + guint i; + gint count = 0; + gdouble vbuf[1024]; + struct stat st; if (file == NULL || file->filename == NULL) { - g_set_error (err, rrd_error_quark (), EINVAL, "rrd add rra failed: wrong arguments"); + g_set_error (err, + rrd_error_quark (), EINVAL, "rrd add rra failed: wrong arguments"); return FALSE; } fd = open (file->filename, O_RDWR); if (fd == -1) { - g_set_error (err, rrd_error_quark (), errno, "rrd open error: %s", strerror (errno)); + g_set_error (err, + rrd_error_quark (), errno, "rrd open error: %s", strerror (errno)); return FALSE; } if (lseek (fd, 0, SEEK_END) == -1) { - g_set_error (err, rrd_error_quark (), errno, "rrd seek error: %s", strerror (errno)); + g_set_error (err, + rrd_error_quark (), errno, "rrd seek error: %s", strerror (errno)); close (fd); return FALSE; } /* Adjust CDP */ - for (i = 0; i < file->stat_head->rra_cnt; i ++) { + for (i = 0; i < file->stat_head->rra_cnt; i++) { file->cdp_prep->scratch[CDP_unkn_pdp_cnt].lv = 0; /* Randomize row pointer */ file->rra_ptr->cur_row = g_random_int () % file->rra_def[i].row_cnt; @@ -551,14 +603,17 @@ rspamd_rrd_finalize (struct rspamd_rrd_file *file, GError **err) munmap (file->map, file->size); /* Write values */ - for (i = 0; i < G_N_ELEMENTS (vbuf); i ++) { + for (i = 0; i < G_N_ELEMENTS (vbuf); i++) { vbuf[i] = NAN; } while (count > 0) { /* Write values in buffered matter */ - if (write (fd, vbuf, MIN ((gint)G_N_ELEMENTS (vbuf), count) * sizeof (gdouble)) == -1) { - g_set_error (err, rrd_error_quark (), errno, "rrd write error: %s", strerror (errno)); + if (write (fd, vbuf, + MIN ((gint)G_N_ELEMENTS (vbuf), count) * sizeof (gdouble)) == -1) { + g_set_error (err, + rrd_error_quark (), errno, "rrd write error: %s", + strerror (errno)); close (fd); return FALSE; } @@ -566,16 +621,20 @@ rspamd_rrd_finalize (struct rspamd_rrd_file *file, GError **err) } if (fstat (fd, &st) == -1) { - g_set_error (err, rrd_error_quark (), errno, "rrd stat error: %s", strerror (errno)); + g_set_error (err, + rrd_error_quark (), errno, "rrd stat error: %s", strerror (errno)); close (fd); return FALSE; } /* Mmap again */ file->size = st.st_size; - if ((file->map = mmap (NULL, st.st_size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0)) == MAP_FAILED) { + if ((file->map = + mmap (NULL, st.st_size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, + 0)) == MAP_FAILED) { close (fd); - g_set_error (err, rrd_error_quark (), ENOMEM, "mmap failed: %s", strerror (errno)); + g_set_error (err, + rrd_error_quark (), ENOMEM, "mmap failed: %s", strerror (errno)); g_slice_free1 (sizeof (struct rspamd_rrd_file), file); return FALSE; } @@ -597,16 +656,20 @@ rspamd_rrd_finalize (struct rspamd_rrd_file *file, GError **err) * @return */ static gboolean -rspamd_rrd_update_pdp_prep (struct rspamd_rrd_file *file, gdouble *vals, gdouble *pdp_new, gdouble interval) +rspamd_rrd_update_pdp_prep (struct rspamd_rrd_file *file, + gdouble *vals, + gdouble *pdp_new, + gdouble interval) { - guint i; - enum rrd_dst_type type; + guint i; + enum rrd_dst_type type; - for (i = 0; i < file->stat_head->ds_cnt; i ++) { + for (i = 0; i < file->stat_head->ds_cnt; i++) { type = rrd_dst_from_string (file->ds_def[i].dst); if (file->ds_def[i].par[RRD_DS_mrhb_cnt].lv < interval) { - rspamd_strlcpy (file->pdp_prep[i].last_ds, "U", sizeof (file->pdp_prep[i].last_ds)); + rspamd_strlcpy (file->pdp_prep[i].last_ds, "U", + sizeof (file->pdp_prep[i].last_ds)); } if (file->ds_def[i].par[RRD_DS_mrhb_cnt].lv >= interval) { @@ -617,7 +680,8 @@ rspamd_rrd_update_pdp_prep (struct rspamd_rrd_file *file, gdouble *vals, gdouble pdp_new[i] = NAN; } else { - pdp_new[i] = vals[i] - strtod (file->pdp_prep[i].last_ds, NULL); + pdp_new[i] = vals[i] - strtod (file->pdp_prep[i].last_ds, + NULL); } break; case RRD_DST_GAUGE: @@ -635,7 +699,8 @@ rspamd_rrd_update_pdp_prep (struct rspamd_rrd_file *file, gdouble *vals, gdouble } /* Copy value to the last_ds */ if (!isnan (vals[i])) { - rspamd_snprintf (file->pdp_prep[i].last_ds, sizeof (file->pdp_prep[i].last_ds), "%.4f", vals[i]); + rspamd_snprintf (file->pdp_prep[i].last_ds, + sizeof (file->pdp_prep[i].last_ds), "%.4f", vals[i]); } else { file->pdp_prep[i].last_ds[0] = 'U'; @@ -658,15 +723,20 @@ rspamd_rrd_update_pdp_prep (struct rspamd_rrd_file *file, gdouble *vals, gdouble * @param pdp_diff time till last pdp update */ static void -rspamd_rrd_update_pdp_step (struct rspamd_rrd_file *file, gdouble *pdp_new, gdouble *pdp_temp, gdouble interval, - gdouble pre_int, gdouble post_int, gulong pdp_diff) +rspamd_rrd_update_pdp_step (struct rspamd_rrd_file *file, + gdouble *pdp_new, + gdouble *pdp_temp, + gdouble interval, + gdouble pre_int, + gdouble post_int, + gulong pdp_diff) { - guint i; - rrd_value_t *scratch; - gulong heartbeat; + guint i; + rrd_value_t *scratch; + gulong heartbeat; - for (i = 0; i < file->stat_head->ds_cnt; i ++) { + for (i = 0; i < file->stat_head->ds_cnt; i++) { scratch = file->pdp_prep[i].scratch; heartbeat = file->ds_def[i].par[RRD_DS_mrhb_cnt].lv; if (!isnan (pdp_new[i])) { @@ -677,21 +747,22 @@ rspamd_rrd_update_pdp_step (struct rspamd_rrd_file *file, gdouble *pdp_new, gdou pre_int = 0.0; } /* Check interval value for heartbeat for this DS */ - if ((interval > heartbeat) || (file->stat_head->pdp_step / 2.0 < scratch[PDP_unkn_sec_cnt].lv)) { + if ((interval > heartbeat) || + (file->stat_head->pdp_step / 2.0 < scratch[PDP_unkn_sec_cnt].lv)) { pdp_temp[i] = NAN; } else { pdp_temp[i] = scratch[PDP_val].dv / - ((double) (pdp_diff - scratch[PDP_unkn_sec_cnt].lv) - pre_int); + ((double) (pdp_diff - scratch[PDP_unkn_sec_cnt].lv) - pre_int); } - if (isnan (pdp_new[i])) { - scratch[PDP_unkn_sec_cnt].lv = floor (post_int); - scratch[PDP_val].dv = NAN; - } else { - scratch[PDP_unkn_sec_cnt].lv = 0; - scratch[PDP_val].dv = pdp_new[i] / interval * post_int; - } + if (isnan (pdp_new[i])) { + scratch[PDP_unkn_sec_cnt].lv = floor (post_int); + scratch[PDP_val].dv = NAN; + } else { + scratch[PDP_unkn_sec_cnt].lv = 0; + scratch[PDP_val].dv = pdp_new[i] / interval * post_int; + } } } @@ -705,23 +776,28 @@ rspamd_rrd_update_pdp_step (struct rspamd_rrd_file *file, gdouble *pdp_new, gdou * @param pdp_temp temporary pdp points */ static void -rspamd_rrd_update_cdp (struct rspamd_rrd_file *file, gdouble pdp_steps, gdouble pdp_offset, gulong *rra_steps, gulong rra_index, - gdouble *pdp_temp) +rspamd_rrd_update_cdp (struct rspamd_rrd_file *file, + gdouble pdp_steps, + gdouble pdp_offset, + gulong *rra_steps, + gulong rra_index, + gdouble *pdp_temp) { - guint i; - struct rrd_rra_def *rra; - rrd_value_t *scratch; - enum rrd_cf_type cf; - gdouble last_cdp, cur_cdp; - gulong pdp_in_cdp; + guint i; + struct rrd_rra_def *rra; + rrd_value_t *scratch; + enum rrd_cf_type cf; + gdouble last_cdp, cur_cdp; + gulong pdp_in_cdp; rra = &file->rra_def[rra_index]; cf = rrd_cf_from_string (rra->cf_nam); /* Iterate over all DS for this RRA */ - for (i = 0; i < file->stat_head->ds_cnt; i ++) { + for (i = 0; i < file->stat_head->ds_cnt; i++) { /* Get CDP for this RRA and DS */ - scratch = file->cdp_prep[rra_index * file->stat_head->ds_cnt + i].scratch; + scratch = + file->cdp_prep[rra_index * file->stat_head->ds_cnt + i].scratch; if (rra->pdp_cnt > 1) { /* Do we have any CDP to update for this rra ? */ if (rra_steps[rra_index] > 0) { @@ -737,7 +813,8 @@ rspamd_rrd_update_cdp (struct rspamd_rrd_file *file, gdouble pdp_steps, gdouble } /* Check XFF for this rra */ - if (scratch[CDP_unkn_pdp_cnt].lv > rra->pdp_cnt * rra->par[RRA_cdp_xff_val].lv) { + if (scratch[CDP_unkn_pdp_cnt].lv > rra->pdp_cnt * + rra->par[RRA_cdp_xff_val].lv) { /* XFF is reached */ scratch[CDP_primary_val].dv = NAN; } @@ -745,17 +822,26 @@ rspamd_rrd_update_cdp (struct rspamd_rrd_file *file, gdouble pdp_steps, gdouble /* Need to initialize CDP using specified consolidation */ switch (cf) { case RRD_CF_AVERAGE: - last_cdp = isnan (scratch[CDP_val].dv) ? 0.0 : scratch[CDP_val].dv; + last_cdp = + isnan (scratch[CDP_val].dv) ? 0.0 : scratch[CDP_val] + .dv; cur_cdp = isnan (pdp_temp[i]) ? 0.0 : pdp_temp[i]; - scratch[CDP_primary_val].dv = (last_cdp + cur_cdp * pdp_offset) / (rra->pdp_cnt - scratch[CDP_unkn_pdp_cnt].lv); + scratch[CDP_primary_val].dv = + (last_cdp + cur_cdp * + pdp_offset) / + (rra->pdp_cnt - scratch[CDP_unkn_pdp_cnt].lv); break; case RRD_CF_MAXIMUM: - last_cdp = isnan (scratch[CDP_val].dv) ? -INFINITY : scratch[CDP_val].dv; + last_cdp = + isnan (scratch[CDP_val].dv) ? -INFINITY : scratch[ + CDP_val].dv; cur_cdp = isnan (pdp_temp[i]) ? -INFINITY : pdp_temp[i]; scratch[CDP_primary_val].dv = MAX (last_cdp, cur_cdp); break; case RRD_CF_MINIMUM: - last_cdp = isnan (scratch[CDP_val].dv) ? INFINITY : scratch[CDP_val].dv; + last_cdp = + isnan (scratch[CDP_val].dv) ? INFINITY : scratch[ + CDP_val].dv; cur_cdp = isnan (pdp_temp[i]) ? INFINITY : pdp_temp[i]; scratch[CDP_primary_val].dv = MIN (last_cdp, cur_cdp); break; @@ -809,7 +895,8 @@ rspamd_rrd_update_cdp (struct rspamd_rrd_file *file, gdouble pdp_steps, gdouble scratch[CDP_val].dv = pdp_temp[i] * pdp_steps; } else { - scratch[CDP_val].dv = last_cdp + pdp_temp[i] * pdp_steps; + scratch[CDP_val].dv = last_cdp + pdp_temp[i] * + pdp_steps; } break; case RRD_CF_MAXIMUM: @@ -848,26 +935,31 @@ rspamd_rrd_update_cdp (struct rspamd_rrd_file *file, gdouble pdp_steps, gdouble void rspamd_rrd_write_rra (struct rspamd_rrd_file *file, gulong *rra_steps) { - guint i, j, scratch_idx, cdp_idx, k; - struct rrd_rra_def *rra; - gdouble *rra_row; + guint i, j, scratch_idx, cdp_idx, k; + struct rrd_rra_def *rra; + gdouble *rra_row; /* Iterate over all RRA */ - for (i = 0; i < file->stat_head->rra_cnt; i ++) { + for (i = 0; i < file->stat_head->rra_cnt; i++) { rra = &file->rra_def[i]; /* How much steps need to be updated */ - for (j = 0, scratch_idx = CDP_primary_val; j < rra_steps[i]; j ++, scratch_idx = CDP_secondary_val) { + for (j = 0, scratch_idx = CDP_primary_val; + j < rra_steps[i]; + j++, scratch_idx = CDP_secondary_val) { /* Move row ptr */ if (++file->rra_ptr[i].cur_row >= rra->row_cnt) { file->rra_ptr[i].cur_row = 0; } /* Calculate seek */ - rra_row = file->rrd_value + (file->stat_head->ds_cnt * i + file->rra_ptr[i].cur_row); + rra_row = file->rrd_value + + (file->stat_head->ds_cnt * i + file->rra_ptr[i].cur_row); /* Iterate over DS */ - for (k = 0; k < file->stat_head->ds_cnt; k ++) { + for (k = 0; k < file->stat_head->ds_cnt; k++) { cdp_idx = i * file->stat_head->ds_cnt + k; - memcpy (rra_row, &file->cdp_prep[cdp_idx].scratch[scratch_idx].dv, sizeof (gdouble)); - rra_row ++; + memcpy (rra_row, + &file->cdp_prep[cdp_idx].scratch[scratch_idx].dv, + sizeof (gdouble)); + rra_row++; } } } @@ -881,23 +973,28 @@ rspamd_rrd_write_rra (struct rspamd_rrd_file *file, gulong *rra_steps) * @return TRUE if a row has been added */ gboolean -rspamd_rrd_add_record (struct rspamd_rrd_file* file, GArray *points, GError **err) +rspamd_rrd_add_record (struct rspamd_rrd_file * file, + GArray *points, + GError **err) { - gdouble interval, *pdp_new, *pdp_temp, pre_int, post_int; - guint i; - gulong pdp_steps, cur_pdp_count, prev_pdp_step, cur_pdp_step, - prev_pdp_age, cur_pdp_age, *rra_steps, pdp_offset; - struct timeval tv; - - if (file == NULL || file->stat_head->ds_cnt * sizeof (gdouble) != points->len) { - g_set_error (err, rrd_error_quark (), EINVAL, "rrd add points failed: wrong arguments"); + gdouble interval, *pdp_new, *pdp_temp, pre_int, post_int; + guint i; + gulong pdp_steps, cur_pdp_count, prev_pdp_step, cur_pdp_step, + prev_pdp_age, cur_pdp_age, *rra_steps, pdp_offset; + struct timeval tv; + + if (file == NULL || file->stat_head->ds_cnt * sizeof (gdouble) != + points->len) { + g_set_error (err, + rrd_error_quark (), EINVAL, + "rrd add points failed: wrong arguments"); return FALSE; } /* Get interval */ gettimeofday (&tv, NULL); interval = (gdouble)(tv.tv_sec - file->live_head->last_up) + - (gdouble)(tv.tv_usec - file->live_head->last_up_usec) / 1e6f; + (gdouble)(tv.tv_usec - file->live_head->last_up_usec) / 1e6f; /* Update PDP preparation values */ pdp_new = g_malloc (sizeof (gdouble) * file->stat_head->ds_cnt); @@ -905,8 +1002,11 @@ rspamd_rrd_add_record (struct rspamd_rrd_file* file, GArray *points, GError **er /* How much steps need to be updated in each RRA */ rra_steps = g_malloc0 (sizeof (gulong) * file->stat_head->rra_cnt); - if (!rspamd_rrd_update_pdp_prep (file, (gdouble *)points->data, pdp_new, interval)) { - g_set_error (err, rrd_error_quark (), EINVAL, "rrd update pdp failed: wrong arguments"); + if (!rspamd_rrd_update_pdp_prep (file, (gdouble *)points->data, pdp_new, + interval)) { + g_set_error (err, + rrd_error_quark (), EINVAL, + "rrd update pdp failed: wrong arguments"); g_free (pdp_new); g_free (pdp_temp); g_free (rra_steps); @@ -915,7 +1015,7 @@ rspamd_rrd_add_record (struct rspamd_rrd_file* file, GArray *points, GError **er /* Calculate elapsed steps */ /* Age in seconds for previous pdp store */ - prev_pdp_age = file->live_head->last_up % file->stat_head->pdp_step; + prev_pdp_age = file->live_head->last_up % file->stat_head->pdp_step; /* Time in seconds for last pdp update */ prev_pdp_step = file->live_head->last_up - prev_pdp_age; /* Age in seconds from current time to required pdp time */ @@ -924,7 +1024,10 @@ rspamd_rrd_add_record (struct rspamd_rrd_file* file, GArray *points, GError **er cur_pdp_step = tv.tv_sec - cur_pdp_age; if (cur_pdp_step > prev_pdp_step) { - pre_int = (gdouble)(cur_pdp_step - file->live_head->last_up) - ((double)file->live_head->last_up_usec) / 1e6f; + pre_int = + (gdouble)(cur_pdp_step - + file->live_head->last_up) - + ((double)file->live_head->last_up_usec) / 1e6f; post_int = (gdouble)cur_pdp_age + ((double)tv.tv_usec) / 1e6f; } else { @@ -937,10 +1040,11 @@ rspamd_rrd_add_record (struct rspamd_rrd_file* file, GArray *points, GError **er if (pdp_steps == 0) { /* Simple update of pdp prep */ - for (i = 0; i < file->stat_head->ds_cnt; i ++) { + for (i = 0; i < file->stat_head->ds_cnt; i++) { if (isnan (pdp_new[i])) { /* Increment unknown period */ - file->pdp_prep[i].scratch[PDP_unkn_sec_cnt].lv += floor (interval); + file->pdp_prep[i].scratch[PDP_unkn_sec_cnt].lv += floor ( + interval); } else { if (isnan (file->pdp_prep[i].scratch[PDP_val].dv)) { @@ -958,23 +1062,36 @@ rspamd_rrd_add_record (struct rspamd_rrd_file* file, GArray *points, GError **er /* Complex update of PDP, CDP and RRA */ /* Update PDP for this step */ - rspamd_rrd_update_pdp_step (file, pdp_new, pdp_temp, interval, pre_int, post_int, pdp_steps * file->stat_head->pdp_step); + rspamd_rrd_update_pdp_step (file, + pdp_new, + pdp_temp, + interval, + pre_int, + post_int, + pdp_steps * file->stat_head->pdp_step); /* Update CDP points for each RRA*/ - for (i = 0; i < file->stat_head->rra_cnt; i ++) { + for (i = 0; i < file->stat_head->rra_cnt; i++) { /* Calculate pdp offset for this RRA */ - pdp_offset = file->rra_def[i].pdp_cnt - cur_pdp_count % file->rra_def[i].pdp_cnt; + pdp_offset = file->rra_def[i].pdp_cnt - cur_pdp_count % + file->rra_def[i].pdp_cnt; /* How much steps we got for this RRA */ if (pdp_offset <= pdp_steps) { - rra_steps[i] = (pdp_steps - pdp_offset) / file->rra_def[i].pdp_cnt + 1; + rra_steps[i] = + (pdp_steps - pdp_offset) / file->rra_def[i].pdp_cnt + 1; } else { /* This rra have not passed enough pdp steps */ rra_steps[i] = 0; } /* Update this specific CDP */ - rspamd_rrd_update_cdp (file, pdp_steps, pdp_offset, rra_steps, i, pdp_temp); + rspamd_rrd_update_cdp (file, + pdp_steps, + pdp_offset, + rra_steps, + i, + pdp_temp); /* Write RRA */ rspamd_rrd_write_rra (file, rra_steps); } @@ -998,7 +1115,7 @@ rspamd_rrd_add_record (struct rspamd_rrd_file* file, GArray *points, GError **er * @return */ gint -rspamd_rrd_close (struct rspamd_rrd_file* file) +rspamd_rrd_close (struct rspamd_rrd_file * file) { if (file == NULL) { errno = EINVAL; diff --git a/src/libutil/rrd.h b/src/libutil/rrd.h index ff6902894..13ed965ab 100644 --- a/src/libutil/rrd.h +++ b/src/libutil/rrd.h @@ -42,17 +42,17 @@ typedef union { struct rrd_file_head { /* Data Base Identification Section ** */ - gchar cookie[4]; /* RRD */ - gchar version[5]; /* version of the format */ - gdouble float_cookie; /* is it the correct double representation ? */ + gchar cookie[4]; /* RRD */ + gchar version[5]; /* version of the format */ + gdouble float_cookie; /* is it the correct double representation ? */ /* Data Base Structure Definition **** */ gulong ds_cnt; /* how many different ds provid input to the rrd */ gulong rra_cnt; /* how many rras will be maintained in the rrd */ gulong pdp_step; /* pdp interval in seconds */ - rrd_value_t par[10]; /* global parameters ... unused - at the moment */ + rrd_value_t par[10]; /* global parameters ... unused + at the moment */ }; enum rrd_dst_type { @@ -66,9 +66,9 @@ enum rrd_ds_param { RRD_DS_mrhb_cnt = 0, /* minimum required heartbeat */ RRD_DS_min_val, /* the processed input of a ds must */ RRD_DS_max_val, /* be between max_val and min_val - * both can be set to UNKNOWN if you - * do not care. Data outside the limits - * set to UNKNOWN */ + * both can be set to UNKNOWN if you + * do not care. Data outside the limits + * set to UNKNOWN */ RRD_DS_cdef = RRD_DS_mrhb_cnt }; /* pointer to encoded rpn expression only applies to DST_CDEF */ @@ -117,8 +117,8 @@ enum rrd_cf_type { enum rrd_rra_param { RRA_cdp_xff_val = 0, /* what part of the consolidated - * datapoint must be known, to produce a - * valid entry in the rra */ + * datapoint must be known, to produce a + * valid entry in the rra */ /* CF_HWPREDICT: */ RRA_hw_alpha = 1, /* exponential smoothing parameter for the intercept in @@ -161,18 +161,18 @@ enum rrd_rra_param { /* RRA_dependent_rra_idx = 3, */ RRA_window_len = 4, RRA_failure_threshold = 5 - /* For CF_FAILURES, number of violations within the last - * window required to mark a failure. */ + /* For CF_FAILURES, number of violations within the last + * window required to mark a failure. */ }; #define RRD_CF_NAM_SIZE 20 struct rrd_rra_def { - gchar cf_nam[RRD_CF_NAM_SIZE]; /* consolidation function (null term) */ + gchar cf_nam[RRD_CF_NAM_SIZE]; /* consolidation function (null term) */ gulong row_cnt; /* number of entries in the store */ gulong pdp_cnt; /* how many primary data points are - * required for a consolidated data point?*/ + * required for a consolidated data point?*/ rrd_value_t par[MAX_RRA_PAR_EN]; /* index see rra_param_en */ }; @@ -186,17 +186,17 @@ struct rrd_live_head { enum rrd_pdp_param { PDP_unkn_sec_cnt = 0, /* how many seconds of the current - * pdp value is unknown data? */ + * pdp value is unknown data? */ PDP_val }; /* current value of the pdp. this depends on dst */ struct rrd_pdp_prep { gchar last_ds[RRD_LAST_DS_LEN]; /* the last reading from the data - * source. this is stored in ASCII - * to cater for very large counters - * we might encounter in connection - * with SNMP. */ + * source. this is stored in ASCII + * to cater for very large counters + * we might encounter in connection + * with SNMP. */ rrd_value_t scratch[10]; /* contents according to pdp_par_en */ }; @@ -249,9 +249,9 @@ enum rrd_cdp_param { }; struct rrd_cdp_prep { - rrd_value_t scratch[RRD_MAX_CDP_PAR_EN]; - /* contents according to cdp_par_en * - * init state should be NAN */ + rrd_value_t scratch[RRD_MAX_CDP_PAR_EN]; + /* contents according to cdp_par_en * + * init state should be NAN */ }; struct rrd_rra_ptr { @@ -270,7 +270,7 @@ struct rspamd_rrd_file { gdouble *rrd_value; /* list of rrd values */ gchar *filename; - guint8* map; /* mmapped area */ + guint8 * map; /* mmapped area */ gsize size; /* its size */ gboolean finalized; }; @@ -284,7 +284,7 @@ struct rspamd_rrd_file { * @param err error pointer * @return rrd file structure */ -struct rspamd_rrd_file* rspamd_rrd_open (const gchar *filename, GError **err); +struct rspamd_rrd_file * rspamd_rrd_open (const gchar *filename, GError **err); /** * Create basic header for rrd file @@ -295,7 +295,11 @@ struct rspamd_rrd_file* rspamd_rrd_open (const gchar *filename, GError **err); * @param err error pointer * @return TRUE if file has been created */ -struct rspamd_rrd_file* rspamd_rrd_create (const gchar *filename, gulong ds_count, gulong rra_count, gulong pdp_step, GError **err); +struct rspamd_rrd_file * rspamd_rrd_create (const gchar *filename, + gulong ds_count, + gulong rra_count, + gulong pdp_step, + GError **err); /** * Add data sources to rrd file @@ -304,7 +308,9 @@ struct rspamd_rrd_file* rspamd_rrd_create (const gchar *filename, gulong ds_coun * @param err error pointer * @return TRUE if data sources were added */ -gboolean rspamd_rrd_add_ds (struct rspamd_rrd_file* file, GArray *ds, GError **err); +gboolean rspamd_rrd_add_ds (struct rspamd_rrd_file * file, + GArray *ds, + GError **err); /** * Add round robin archives to rrd file @@ -313,7 +319,9 @@ gboolean rspamd_rrd_add_ds (struct rspamd_rrd_file* file, GArray *ds, GError **e * @param err error pointer * @return TRUE if archives were added */ -gboolean rspamd_rrd_add_rra (struct rspamd_rrd_file *file, GArray *rra, GError **err); +gboolean rspamd_rrd_add_rra (struct rspamd_rrd_file *file, + GArray *rra, + GError **err); /** * Finalize rrd file header and initialize all RRA in the file @@ -330,14 +338,16 @@ gboolean rspamd_rrd_finalize (struct rspamd_rrd_file *file, GError **err); * @param err error pointer * @return TRUE if a row has been added */ -gboolean rspamd_rrd_add_record (struct rspamd_rrd_file* file, GArray *points, GError **err); +gboolean rspamd_rrd_add_record (struct rspamd_rrd_file * file, + GArray *points, + GError **err); /** * Close rrd file * @param file * @return */ -gint rspamd_rrd_close (struct rspamd_rrd_file* file); +gint rspamd_rrd_close (struct rspamd_rrd_file * file); /* * Conversion functions @@ -350,7 +360,7 @@ enum rrd_dst_type rrd_dst_from_string (const gchar *str); /** * Convert numeric presentation of dst to string */ -const gchar* rrd_dst_to_string (enum rrd_dst_type type); +const gchar * rrd_dst_to_string (enum rrd_dst_type type); /** * Convert rrd consolidation function type from string to numeric value */ @@ -358,17 +368,22 @@ enum rrd_cf_type rrd_cf_from_string (const gchar *str); /** * Convert numeric presentation of cf to string */ -const gchar* rrd_cf_to_string (enum rrd_cf_type type); +const gchar * rrd_cf_to_string (enum rrd_cf_type type); /* Default RRA and DS */ /** * Create default RRA */ -void rrd_make_default_rra (const gchar *cf_name, gulong pdp_cnt, gulong rows, struct rrd_rra_def *rra); +void rrd_make_default_rra (const gchar *cf_name, + gulong pdp_cnt, + gulong rows, + struct rrd_rra_def *rra); /** * Create default DS */ -void rrd_make_default_ds (const gchar *name, gulong pdp_step, struct rrd_ds_def *ds); +void rrd_make_default_ds (const gchar *name, + gulong pdp_step, + struct rrd_ds_def *ds); #endif /* RRD_H_ */ diff --git a/src/libutil/trie.c b/src/libutil/trie.c index 394c4e939..8c9e0f600 100644 --- a/src/libutil/trie.c +++ b/src/libutil/trie.c @@ -25,10 +25,10 @@ #include "mem_pool.h" #include "trie.h" -rspamd_trie_t* +rspamd_trie_t * rspamd_trie_create (gboolean icase) { - rspamd_trie_t *new; + rspamd_trie_t *new; new = g_malloc (sizeof (rspamd_trie_t)); @@ -48,20 +48,25 @@ rspamd_trie_create (gboolean icase) * Insert a single character as the specified level of the suffix tree */ static struct rspamd_trie_state * -rspamd_trie_insert_char (rspamd_trie_t *trie, guint depth, struct rspamd_trie_state *pos, gchar c) +rspamd_trie_insert_char (rspamd_trie_t *trie, + guint depth, + struct rspamd_trie_state *pos, + gchar c) { - struct rspamd_trie_match *new_match; - struct rspamd_trie_state *new_pos; + struct rspamd_trie_match *new_match; + struct rspamd_trie_state *new_pos; /* New match is inserted before pos */ - new_match = rspamd_mempool_alloc (trie->pool, sizeof (struct rspamd_trie_match)); + new_match = + rspamd_mempool_alloc (trie->pool, sizeof (struct rspamd_trie_match)); new_match->next = pos->match; new_match->c = c; /* Now set match link */ pos->match = new_match; - new_match->state = rspamd_mempool_alloc (trie->pool, sizeof (struct rspamd_trie_state)); + new_match->state = + rspamd_mempool_alloc (trie->pool, sizeof (struct rspamd_trie_state)); new_pos = new_match->state; new_pos->match = NULL; new_pos->fail = &trie->root; @@ -86,7 +91,7 @@ rspamd_trie_insert_char (rspamd_trie_t *trie, guint depth, struct rspamd_trie_st static inline struct rspamd_trie_match * check_match (struct rspamd_trie_state *s, gchar c) { - struct rspamd_trie_match *match = s->match; + struct rspamd_trie_match *match = s->match; while (match && match->c != c) { match = match->next; @@ -98,11 +103,11 @@ check_match (struct rspamd_trie_state *s, gchar c) void rspamd_trie_insert (rspamd_trie_t *trie, const gchar *pattern, gint pattern_id) { - const guchar *p = pattern; - struct rspamd_trie_state *q, *q1, *r, *cur_node; - struct rspamd_trie_match *m, *n; - guint i, depth = 0; - gchar c; + const guchar *p = pattern; + struct rspamd_trie_state *q, *q1, *r, *cur_node; + struct rspamd_trie_match *m, *n; + guint i, depth = 0; + gchar c; /* Insert pattern to the trie */ @@ -118,8 +123,8 @@ rspamd_trie_insert (rspamd_trie_t *trie, const gchar *pattern, gint pattern_id) else { cur_node = m->state; } - p ++; - depth ++; + p++; + depth++; } cur_node->final = depth; @@ -165,13 +170,16 @@ rspamd_trie_insert (rspamd_trie_t *trie, const gchar *pattern, gint pattern_id) } } -const gchar* -rspamd_trie_lookup (rspamd_trie_t *trie, const gchar *buffer, gsize buflen, gint *matched_id) +const gchar * +rspamd_trie_lookup (rspamd_trie_t *trie, + const gchar *buffer, + gsize buflen, + gint *matched_id) { - const guchar *p = buffer, *prev, *ret; - struct rspamd_trie_state *cur_node; - struct rspamd_trie_match *m = NULL; - gchar c; + const guchar *p = buffer, *prev, *ret; + struct rspamd_trie_state *cur_node; + struct rspamd_trie_match *m = NULL; + gchar c; cur_node = &trie->root; @@ -195,9 +203,9 @@ rspamd_trie_lookup (rspamd_trie_t *trie, const gchar *buffer, gsize buflen, gint /* We have tried the pattern but eventually it was not found */ cur_node = &trie->root; ret = p; - p ++; + p++; prev = p; - buflen --; + buflen--; continue; } @@ -213,9 +221,9 @@ rspamd_trie_lookup (rspamd_trie_t *trie, const gchar *buffer, gsize buflen, gint return (const gchar *) ret; } } - p ++; + p++; prev = p; - buflen --; + buflen--; } return NULL; diff --git a/src/libutil/trie.h b/src/libutil/trie.h index 2792ee4a5..037ffd9ee 100644 --- a/src/libutil/trie.h +++ b/src/libutil/trie.h @@ -58,7 +58,7 @@ typedef struct rspamd_trie_s { /* * Create a new suffix trie */ -rspamd_trie_t* rspamd_trie_create (gboolean icase); +rspamd_trie_t * rspamd_trie_create (gboolean icase); /* * Insert a pattern into the trie @@ -66,7 +66,9 @@ rspamd_trie_t* rspamd_trie_create (gboolean icase); * @param pattern text of element * @param pattern_id id of element */ -void rspamd_trie_insert (rspamd_trie_t *trie, const gchar *pattern, gint pattern_id); +void rspamd_trie_insert (rspamd_trie_t *trie, + const gchar *pattern, + gint pattern_id); /* * Search for a text using suffix trie @@ -76,7 +78,10 @@ void rspamd_trie_insert (rspamd_trie_t *trie, const gchar *pattern, gint pattern * @param mached_id on a successfull search here would be stored id of pattern found * @return Position in a text where pattern was found or NULL if no patterns were found */ -const gchar* rspamd_trie_lookup (rspamd_trie_t *trie, const gchar *buffer, gsize buflen, gint *matched_id); +const gchar * rspamd_trie_lookup (rspamd_trie_t *trie, + const gchar *buffer, + gsize buflen, + gint *matched_id); /* * Deallocate suffix trie diff --git a/src/libutil/upstream.c b/src/libutil/upstream.c index f82d3ba50..ee21a5ec3 100644 --- a/src/libutil/upstream.c +++ b/src/libutil/upstream.c @@ -27,7 +27,7 @@ #ifdef _THREAD_SAFE -pthread_rwlock_t upstream_mtx = PTHREAD_RWLOCK_INITIALIZER; +pthread_rwlock_t upstream_mtx = PTHREAD_RWLOCK_INITIALIZER; # define U_RLOCK() do { pthread_rwlock_rdlock (&upstream_mtx); } while (0) # define U_WLOCK() do { pthread_rwlock_wrlock (&upstream_mtx); } while (0) # define U_UNLOCK() do { pthread_rwlock_unlock (&upstream_mtx); } while (0) @@ -45,49 +45,91 @@ pthread_rwlock_t upstream_mtx = PTHREAD_RWLOCK_INITIALIZER; * Init: 0x0 */ -static const guint32 crc32lookup[256] = { - 0x00000000U, 0x77073096U, 0xee0e612cU, 0x990951baU, 0x076dc419U, 0x706af48fU, - 0xe963a535U, 0x9e6495a3U, 0x0edb8832U, 0x79dcb8a4U, 0xe0d5e91eU, 0x97d2d988U, - 0x09b64c2bU, 0x7eb17cbdU, 0xe7b82d07U, 0x90bf1d91U, 0x1db71064U, 0x6ab020f2U, - 0xf3b97148U, 0x84be41deU, 0x1adad47dU, 0x6ddde4ebU, 0xf4d4b551U, 0x83d385c7U, - 0x136c9856U, 0x646ba8c0U, 0xfd62f97aU, 0x8a65c9ecU, 0x14015c4fU, 0x63066cd9U, - 0xfa0f3d63U, 0x8d080df5U, 0x3b6e20c8U, 0x4c69105eU, 0xd56041e4U, 0xa2677172U, - 0x3c03e4d1U, 0x4b04d447U, 0xd20d85fdU, 0xa50ab56bU, 0x35b5a8faU, 0x42b2986cU, - 0xdbbbc9d6U, 0xacbcf940U, 0x32d86ce3U, 0x45df5c75U, 0xdcd60dcfU, 0xabd13d59U, - 0x26d930acU, 0x51de003aU, 0xc8d75180U, 0xbfd06116U, 0x21b4f4b5U, 0x56b3c423U, - 0xcfba9599U, 0xb8bda50fU, 0x2802b89eU, 0x5f058808U, 0xc60cd9b2U, 0xb10be924U, - 0x2f6f7c87U, 0x58684c11U, 0xc1611dabU, 0xb6662d3dU, 0x76dc4190U, 0x01db7106U, - 0x98d220bcU, 0xefd5102aU, 0x71b18589U, 0x06b6b51fU, 0x9fbfe4a5U, 0xe8b8d433U, - 0x7807c9a2U, 0x0f00f934U, 0x9609a88eU, 0xe10e9818U, 0x7f6a0dbbU, 0x086d3d2dU, - 0x91646c97U, 0xe6635c01U, 0x6b6b51f4U, 0x1c6c6162U, 0x856530d8U, 0xf262004eU, - 0x6c0695edU, 0x1b01a57bU, 0x8208f4c1U, 0xf50fc457U, 0x65b0d9c6U, 0x12b7e950U, - 0x8bbeb8eaU, 0xfcb9887cU, 0x62dd1ddfU, 0x15da2d49U, 0x8cd37cf3U, 0xfbd44c65U, - 0x4db26158U, 0x3ab551ceU, 0xa3bc0074U, 0xd4bb30e2U, 0x4adfa541U, 0x3dd895d7U, - 0xa4d1c46dU, 0xd3d6f4fbU, 0x4369e96aU, 0x346ed9fcU, 0xad678846U, 0xda60b8d0U, - 0x44042d73U, 0x33031de5U, 0xaa0a4c5fU, 0xdd0d7cc9U, 0x5005713cU, 0x270241aaU, - 0xbe0b1010U, 0xc90c2086U, 0x5768b525U, 0x206f85b3U, 0xb966d409U, 0xce61e49fU, - 0x5edef90eU, 0x29d9c998U, 0xb0d09822U, 0xc7d7a8b4U, 0x59b33d17U, 0x2eb40d81U, - 0xb7bd5c3bU, 0xc0ba6cadU, 0xedb88320U, 0x9abfb3b6U, 0x03b6e20cU, 0x74b1d29aU, - 0xead54739U, 0x9dd277afU, 0x04db2615U, 0x73dc1683U, 0xe3630b12U, 0x94643b84U, - 0x0d6d6a3eU, 0x7a6a5aa8U, 0xe40ecf0bU, 0x9309ff9dU, 0x0a00ae27U, 0x7d079eb1U, - 0xf00f9344U, 0x8708a3d2U, 0x1e01f268U, 0x6906c2feU, 0xf762575dU, 0x806567cbU, - 0x196c3671U, 0x6e6b06e7U, 0xfed41b76U, 0x89d32be0U, 0x10da7a5aU, 0x67dd4accU, - 0xf9b9df6fU, 0x8ebeeff9U, 0x17b7be43U, 0x60b08ed5U, 0xd6d6a3e8U, 0xa1d1937eU, - 0x38d8c2c4U, 0x4fdff252U, 0xd1bb67f1U, 0xa6bc5767U, 0x3fb506ddU, 0x48b2364bU, - 0xd80d2bdaU, 0xaf0a1b4cU, 0x36034af6U, 0x41047a60U, 0xdf60efc3U, 0xa867df55U, - 0x316e8eefU, 0x4669be79U, 0xcb61b38cU, 0xbc66831aU, 0x256fd2a0U, 0x5268e236U, - 0xcc0c7795U, 0xbb0b4703U, 0x220216b9U, 0x5505262fU, 0xc5ba3bbeU, 0xb2bd0b28U, - 0x2bb45a92U, 0x5cb36a04U, 0xc2d7ffa7U, 0xb5d0cf31U, 0x2cd99e8bU, 0x5bdeae1dU, - 0x9b64c2b0U, 0xec63f226U, 0x756aa39cU, 0x026d930aU, 0x9c0906a9U, 0xeb0e363fU, - 0x72076785U, 0x05005713U, 0x95bf4a82U, 0xe2b87a14U, 0x7bb12baeU, 0x0cb61b38U, - 0x92d28e9bU, 0xe5d5be0dU, 0x7cdcefb7U, 0x0bdbdf21U, 0x86d3d2d4U, 0xf1d4e242U, - 0x68ddb3f8U, 0x1fda836eU, 0x81be16cdU, 0xf6b9265bU, 0x6fb077e1U, 0x18b74777U, - 0x88085ae6U, 0xff0f6a70U, 0x66063bcaU, 0x11010b5cU, 0x8f659effU, 0xf862ae69U, - 0x616bffd3U, 0x166ccf45U, 0xa00ae278U, 0xd70dd2eeU, 0x4e048354U, 0x3903b3c2U, - 0xa7672661U, 0xd06016f7U, 0x4969474dU, 0x3e6e77dbU, 0xaed16a4aU, 0xd9d65adcU, - 0x40df0b66U, 0x37d83bf0U, 0xa9bcae53U, 0xdebb9ec5U, 0x47b2cf7fU, 0x30b5ffe9U, - 0xbdbdf21cU, 0xcabac28aU, 0x53b39330U, 0x24b4a3a6U, 0xbad03605U, 0xcdd70693U, - 0x54de5729U, 0x23d967bfU, 0xb3667a2eU, 0xc4614ab8U, 0x5d681b02U, 0x2a6f2b94U, +static const guint32 crc32lookup[256] = { + 0x00000000U, 0x77073096U, 0xee0e612cU, 0x990951baU, 0x076dc419U, + 0x706af48fU, + 0xe963a535U, 0x9e6495a3U, 0x0edb8832U, 0x79dcb8a4U, 0xe0d5e91eU, + 0x97d2d988U, + 0x09b64c2bU, 0x7eb17cbdU, 0xe7b82d07U, 0x90bf1d91U, 0x1db71064U, + 0x6ab020f2U, + 0xf3b97148U, 0x84be41deU, 0x1adad47dU, 0x6ddde4ebU, 0xf4d4b551U, + 0x83d385c7U, + 0x136c9856U, 0x646ba8c0U, 0xfd62f97aU, 0x8a65c9ecU, 0x14015c4fU, + 0x63066cd9U, + 0xfa0f3d63U, 0x8d080df5U, 0x3b6e20c8U, 0x4c69105eU, 0xd56041e4U, + 0xa2677172U, + 0x3c03e4d1U, 0x4b04d447U, 0xd20d85fdU, 0xa50ab56bU, 0x35b5a8faU, + 0x42b2986cU, + 0xdbbbc9d6U, 0xacbcf940U, 0x32d86ce3U, 0x45df5c75U, 0xdcd60dcfU, + 0xabd13d59U, + 0x26d930acU, 0x51de003aU, 0xc8d75180U, 0xbfd06116U, 0x21b4f4b5U, + 0x56b3c423U, + 0xcfba9599U, 0xb8bda50fU, 0x2802b89eU, 0x5f058808U, 0xc60cd9b2U, + 0xb10be924U, + 0x2f6f7c87U, 0x58684c11U, 0xc1611dabU, 0xb6662d3dU, 0x76dc4190U, + 0x01db7106U, + 0x98d220bcU, 0xefd5102aU, 0x71b18589U, 0x06b6b51fU, 0x9fbfe4a5U, + 0xe8b8d433U, + 0x7807c9a2U, 0x0f00f934U, 0x9609a88eU, 0xe10e9818U, 0x7f6a0dbbU, + 0x086d3d2dU, + 0x91646c97U, 0xe6635c01U, 0x6b6b51f4U, 0x1c6c6162U, 0x856530d8U, + 0xf262004eU, + 0x6c0695edU, 0x1b01a57bU, 0x8208f4c1U, 0xf50fc457U, 0x65b0d9c6U, + 0x12b7e950U, + 0x8bbeb8eaU, 0xfcb9887cU, 0x62dd1ddfU, 0x15da2d49U, 0x8cd37cf3U, + 0xfbd44c65U, + 0x4db26158U, 0x3ab551ceU, 0xa3bc0074U, 0xd4bb30e2U, 0x4adfa541U, + 0x3dd895d7U, + 0xa4d1c46dU, 0xd3d6f4fbU, 0x4369e96aU, 0x346ed9fcU, 0xad678846U, + 0xda60b8d0U, + 0x44042d73U, 0x33031de5U, 0xaa0a4c5fU, 0xdd0d7cc9U, 0x5005713cU, + 0x270241aaU, + 0xbe0b1010U, 0xc90c2086U, 0x5768b525U, 0x206f85b3U, 0xb966d409U, + 0xce61e49fU, + 0x5edef90eU, 0x29d9c998U, 0xb0d09822U, 0xc7d7a8b4U, 0x59b33d17U, + 0x2eb40d81U, + 0xb7bd5c3bU, 0xc0ba6cadU, 0xedb88320U, 0x9abfb3b6U, 0x03b6e20cU, + 0x74b1d29aU, + 0xead54739U, 0x9dd277afU, 0x04db2615U, 0x73dc1683U, 0xe3630b12U, + 0x94643b84U, + 0x0d6d6a3eU, 0x7a6a5aa8U, 0xe40ecf0bU, 0x9309ff9dU, 0x0a00ae27U, + 0x7d079eb1U, + 0xf00f9344U, 0x8708a3d2U, 0x1e01f268U, 0x6906c2feU, 0xf762575dU, + 0x806567cbU, + 0x196c3671U, 0x6e6b06e7U, 0xfed41b76U, 0x89d32be0U, 0x10da7a5aU, + 0x67dd4accU, + 0xf9b9df6fU, 0x8ebeeff9U, 0x17b7be43U, 0x60b08ed5U, 0xd6d6a3e8U, + 0xa1d1937eU, + 0x38d8c2c4U, 0x4fdff252U, 0xd1bb67f1U, 0xa6bc5767U, 0x3fb506ddU, + 0x48b2364bU, + 0xd80d2bdaU, 0xaf0a1b4cU, 0x36034af6U, 0x41047a60U, 0xdf60efc3U, + 0xa867df55U, + 0x316e8eefU, 0x4669be79U, 0xcb61b38cU, 0xbc66831aU, 0x256fd2a0U, + 0x5268e236U, + 0xcc0c7795U, 0xbb0b4703U, 0x220216b9U, 0x5505262fU, 0xc5ba3bbeU, + 0xb2bd0b28U, + 0x2bb45a92U, 0x5cb36a04U, 0xc2d7ffa7U, 0xb5d0cf31U, 0x2cd99e8bU, + 0x5bdeae1dU, + 0x9b64c2b0U, 0xec63f226U, 0x756aa39cU, 0x026d930aU, 0x9c0906a9U, + 0xeb0e363fU, + 0x72076785U, 0x05005713U, 0x95bf4a82U, 0xe2b87a14U, 0x7bb12baeU, + 0x0cb61b38U, + 0x92d28e9bU, 0xe5d5be0dU, 0x7cdcefb7U, 0x0bdbdf21U, 0x86d3d2d4U, + 0xf1d4e242U, + 0x68ddb3f8U, 0x1fda836eU, 0x81be16cdU, 0xf6b9265bU, 0x6fb077e1U, + 0x18b74777U, + 0x88085ae6U, 0xff0f6a70U, 0x66063bcaU, 0x11010b5cU, 0x8f659effU, + 0xf862ae69U, + 0x616bffd3U, 0x166ccf45U, 0xa00ae278U, 0xd70dd2eeU, 0x4e048354U, + 0x3903b3c2U, + 0xa7672661U, 0xd06016f7U, 0x4969474dU, 0x3e6e77dbU, 0xaed16a4aU, + 0xd9d65adcU, + 0x40df0b66U, 0x37d83bf0U, 0xa9bcae53U, 0xdebb9ec5U, 0x47b2cf7fU, + 0x30b5ffe9U, + 0xbdbdf21cU, 0xcabac28aU, 0x53b39330U, 0x24b4a3a6U, 0xbad03605U, + 0xcdd70693U, + 0x54de5729U, 0x23d967bfU, 0xb3667a2eU, 0xc4614ab8U, 0x5d681b02U, + 0x2a6f2b94U, 0xb40bbe37U, 0xc30c8ea1U, 0x5a05df1bU, 0x2d02ef8dU }; @@ -95,7 +137,11 @@ static const guint32 crc32lookup[256] = { * Check upstream parameters and mark it whether valid or dead */ static void -check_upstream (struct upstream *up, time_t now, time_t error_timeout, time_t revive_timeout, size_t max_errors) +check_upstream (struct upstream *up, + time_t now, + time_t error_timeout, + time_t revive_timeout, + size_t max_errors) { if (up->dead) { if (now - up->time >= revive_timeout) { @@ -118,7 +164,7 @@ check_upstream (struct upstream *up, time_t now, time_t error_timeout, time_t re } } -/* +/* * Call this function after failed upstream request */ void @@ -135,7 +181,7 @@ upstream_fail (struct upstream *up, time_t now) } } -/* +/* * Call this function after successfull upstream request */ void @@ -151,15 +197,15 @@ upstream_ok (struct upstream *up, time_t now) up->weight--; } -/* +/* * Mark all upstreams as active. This function is used when all upstreams are marked as inactive */ void revive_all_upstreams (void *ups, size_t members, size_t msize) { - guint i; - struct upstream *cur; - guchar *p; + guint i; + struct upstream *cur; + guchar *p; U_WLOCK (); p = ups; @@ -174,16 +220,22 @@ revive_all_upstreams (void *ups, size_t members, size_t msize) U_UNLOCK (); } -/* +/* * Scan all upstreams for errors and mark upstreams dead or alive depends on conditions, - * return number of alive upstreams + * return number of alive upstreams */ static gint -rescan_upstreams (void *ups, size_t members, size_t msize, time_t now, time_t error_timeout, time_t revive_timeout, size_t max_errors) +rescan_upstreams (void *ups, + size_t members, + size_t msize, + time_t now, + time_t error_timeout, + time_t revive_timeout, + size_t max_errors) { - guint i, alive; - struct upstream *cur; - guchar *p; + guint i, alive; + struct upstream *cur; + guchar *p; /* Recheck all upstreams */ p = ups; @@ -207,18 +259,18 @@ rescan_upstreams (void *ups, size_t members, size_t msize, time_t now, time_t er } /* Return alive upstream by its number */ -static struct upstream * +static struct upstream * get_upstream_by_number (void *ups, size_t members, size_t msize, gint selected) { - guint i; - u_char *p, *c; - struct upstream *cur; + guint i; + u_char *p, *c; + struct upstream *cur; i = 0; p = ups; c = ups; U_RLOCK (); - for (;;) { + for (;; ) { /* Out of range, return NULL */ if (p > c + members * msize) { break; @@ -248,11 +300,11 @@ get_upstream_by_number (void *ups, size_t members, size_t msize, gint selected) /* * Get hash key for specified key (perl hash) */ -static guint32 +static guint32 get_hash_for_key (guint32 hash, const gchar *key, size_t keylen) { - guint32 h, index; - const gchar *end = key + keylen; + guint32 h, index; + const gchar *end = key + keylen; h = ~hash; @@ -277,13 +329,24 @@ get_hash_for_key (guint32 hash, const gchar *key, size_t keylen) /* * Recheck all upstreams and return random active upstream */ -struct upstream * -get_random_upstream (void *ups, size_t members, size_t msize, time_t now, time_t error_timeout, - time_t revive_timeout, size_t max_errors) +struct upstream * +get_random_upstream (void *ups, + size_t members, + size_t msize, + time_t now, + time_t error_timeout, + time_t revive_timeout, + size_t max_errors) { - gint alive, selected; - - alive = rescan_upstreams (ups, members, msize, now, error_timeout, revive_timeout, max_errors); + gint alive, selected; + + alive = rescan_upstreams (ups, + members, + msize, + now, + error_timeout, + revive_timeout, + max_errors); selected = rand () % alive; return get_upstream_by_number (ups, members, msize, selected); @@ -292,16 +355,29 @@ get_random_upstream (void *ups, size_t members, size_t msize, time_t now, time_t /* * Return upstream by hash, that is calculated from active upstreams number */ -struct upstream * -get_upstream_by_hash (void *ups, size_t members, size_t msize, time_t now, time_t error_timeout, - time_t revive_timeout, size_t max_errors, const gchar *key, size_t keylen) +struct upstream * +get_upstream_by_hash (void *ups, + size_t members, + size_t msize, + time_t now, + time_t error_timeout, + time_t revive_timeout, + size_t max_errors, + const gchar *key, + size_t keylen) { - gint alive, tries = 0, r; - guint32 h = 0, ht; - gchar *p, numbuf[4]; - struct upstream *cur; - - alive = rescan_upstreams (ups, members, msize, now, error_timeout, revive_timeout, max_errors); + gint alive, tries = 0, r; + guint32 h = 0, ht; + gchar *p, numbuf[4]; + struct upstream *cur; + + alive = rescan_upstreams (ups, + members, + msize, + now, + error_timeout, + revive_timeout, + max_errors); if (alive == 0) { return NULL; @@ -313,7 +389,7 @@ get_upstream_by_hash (void *ups, size_t members, size_t msize, time_t now, time_ #endif h %= members; - for (;;) { + for (;; ) { p = (gchar *)ups + msize * h; cur = (struct upstream *)p; if (!cur->dead) { @@ -343,16 +419,27 @@ get_upstream_by_hash (void *ups, size_t members, size_t msize, time_t now, time_ /* * Recheck all upstreams and return upstream in round-robin order according to weight and priority */ -struct upstream * -get_upstream_round_robin (void *ups, size_t members, size_t msize, time_t now, time_t error_timeout, - time_t revive_timeout, size_t max_errors) +struct upstream * +get_upstream_round_robin (void *ups, + size_t members, + size_t msize, + time_t now, + time_t error_timeout, + time_t revive_timeout, + size_t max_errors) { - guint max_weight, i; - struct upstream *cur, *selected = NULL; - u_char *p; + guint max_weight, i; + struct upstream *cur, *selected = NULL; + u_char *p; /* Recheck all upstreams */ - (void)rescan_upstreams (ups, members, msize, now, error_timeout, revive_timeout, max_errors); + (void)rescan_upstreams (ups, + members, + msize, + now, + error_timeout, + revive_timeout, + max_errors); p = ups; max_weight = 0; @@ -393,16 +480,27 @@ get_upstream_round_robin (void *ups, size_t members, size_t msize, time_t now, t /* * Recheck all upstreams and return upstream in round-robin order according to only priority (master-slaves) */ -struct upstream * -get_upstream_master_slave (void *ups, size_t members, size_t msize, time_t now, time_t error_timeout, - time_t revive_timeout, size_t max_errors) +struct upstream * +get_upstream_master_slave (void *ups, + size_t members, + size_t msize, + time_t now, + time_t error_timeout, + time_t revive_timeout, + size_t max_errors) { - guint max_weight, i; - struct upstream *cur, *selected = NULL; - u_char *p; + guint max_weight, i; + struct upstream *cur, *selected = NULL; + u_char *p; /* Recheck all upstreams */ - (void)rescan_upstreams (ups, members, msize, now, error_timeout, revive_timeout, max_errors); + (void)rescan_upstreams (ups, + members, + msize, + now, + error_timeout, + revive_timeout, + max_errors); p = ups; max_weight = 0; @@ -437,11 +535,14 @@ ketama_sort_cmp (const void *a1, const void *a2) * Add ketama points for specified upstream */ gint -upstream_ketama_add (struct upstream *up, gchar *up_key, size_t keylen, size_t keypoints) +upstream_ketama_add (struct upstream *up, + gchar *up_key, + size_t keylen, + size_t keypoints) { - guint32 h = 0; - gchar tmp[4]; - guint i; + guint32 h = 0; + gchar tmp[4]; + guint i; /* Allocate ketama points array */ if (up->ketama_points == NULL) { @@ -472,16 +573,29 @@ upstream_ketama_add (struct upstream *up, gchar *up_key, size_t keylen, size_t k /* * Return upstream by hash and find nearest ketama point in some server */ -struct upstream * -get_upstream_by_hash_ketama (void *ups, size_t members, size_t msize, - time_t now, time_t error_timeout, time_t revive_timeout, size_t max_errors, const gchar *key, size_t keylen) +struct upstream * +get_upstream_by_hash_ketama (void *ups, + size_t members, + size_t msize, + time_t now, + time_t error_timeout, + time_t revive_timeout, + size_t max_errors, + const gchar *key, + size_t keylen) { - guint alive, i; - guint32 h = 0, step, middle, d, min_diff = UINT_MAX; - gchar *p; - struct upstream *cur = NULL, *nearest = NULL; - - alive = rescan_upstreams (ups, members, msize, now, error_timeout, revive_timeout, max_errors); + guint alive, i; + guint32 h = 0, step, middle, d, min_diff = UINT_MAX; + gchar *p; + struct upstream *cur = NULL, *nearest = NULL; + + alive = rescan_upstreams (ups, + members, + msize, + now, + error_timeout, + revive_timeout, + max_errors); if (alive == 0) { return NULL; @@ -520,6 +634,6 @@ get_upstream_by_hash_ketama (void *ups, size_t members, size_t msize, #undef U_LOCK #undef U_UNLOCK -/* - * vi:ts=4 +/* + * vi:ts=4 */ diff --git a/src/libutil/upstream.h b/src/libutil/upstream.h index da0a00013..6b2224e30 100644 --- a/src/libutil/upstream.h +++ b/src/libutil/upstream.h @@ -1,20 +1,20 @@ #ifndef UPSTREAM_H #define UPSTREAM_H -#include <sys/types.h> #include <stdint.h> +#include <sys/types.h> /** * Structure of generic upstream */ struct upstream { - guint errors; /**< Errors for this upstream */ - time_t time; /**< Time of marking */ - guint dead; /**< Dead flag */ - guint priority; /**< Fixed priority */ - gint16 weight; /**< Dynamic weight */ - guint32 *ketama_points; /**< Ketama points array */ - size_t ketama_points_size; /**< Ketama array size */ + guint errors; /**< Errors for this upstream */ + time_t time; /**< Time of marking */ + guint dead; /**< Dead flag */ + guint priority; /**< Fixed priority */ + gint16 weight; /**< Dynamic weight */ + guint32 *ketama_points; /**< Ketama points array */ + size_t ketama_points_size; /**< Ketama array size */ }; /** @@ -43,7 +43,10 @@ void revive_all_upstreams (void *ups, size_t members, size_t msize); /** * Add ketama points for upstream */ -gint upstream_ketama_add (struct upstream *up, gchar *up_key, size_t keylen, size_t keypoints); +gint upstream_ketama_add (struct upstream *up, + gchar *up_key, + size_t keylen, + size_t keypoints); /** * Get a random upstream from array of upstreams @@ -55,9 +58,9 @@ gint upstream_ketama_add (struct upstream *up, gchar *up_key, size_t keylen, siz * @param revive_timeout time during which we counts upstream dead * @param max_errors maximum errors during error_timeout to mark upstream dead */ -struct upstream* get_random_upstream (void *ups, size_t members, size_t msize, - time_t now, time_t error_timeout, - time_t revive_timeout, size_t max_errors); +struct upstream * get_random_upstream (void *ups, size_t members, size_t msize, + time_t now, time_t error_timeout, + time_t revive_timeout, size_t max_errors); /** * Get upstream based on hash from array of upstreams @@ -71,10 +74,10 @@ struct upstream* get_random_upstream (void *ups, size_t members, size_t msize, * @param key key for hashing * @param keylen length of the key */ -struct upstream* get_upstream_by_hash (void *ups, size_t members, size_t msize, - time_t now, time_t error_timeout, - time_t revive_timeout, size_t max_errors, - const gchar *key, size_t keylen); +struct upstream * get_upstream_by_hash (void *ups, size_t members, size_t msize, + time_t now, time_t error_timeout, + time_t revive_timeout, size_t max_errors, + const gchar *key, size_t keylen); /** * Get an upstream from array of upstreams based on its current weight @@ -86,9 +89,13 @@ struct upstream* get_upstream_by_hash (void *ups, size_t members, size_t msize, * @param revive_timeout time during which we counts upstream dead * @param max_errors maximum errors during error_timeout to mark upstream dead */ -struct upstream* get_upstream_round_robin (void *ups, size_t members, size_t msize, - time_t now, time_t error_timeout, - time_t revive_timeout, size_t max_errors); +struct upstream * get_upstream_round_robin (void *ups, + size_t members, + size_t msize, + time_t now, + time_t error_timeout, + time_t revive_timeout, + size_t max_errors); /** * Get upstream based on hash from array of upstreams, this functions is using ketama algorithm @@ -102,9 +109,15 @@ struct upstream* get_upstream_round_robin (void *ups, size_t members, size_t msi * @param key key for hashing * @param keylen length of the key */ -struct upstream* get_upstream_by_hash_ketama (void *ups, size_t members, size_t msize, time_t now, - time_t error_timeout, time_t revive_timeout, size_t max_errors, - const gchar *key, size_t keylen); +struct upstream * get_upstream_by_hash_ketama (void *ups, + size_t members, + size_t msize, + time_t now, + time_t error_timeout, + time_t revive_timeout, + size_t max_errors, + const gchar *key, + size_t keylen); /** * Get an upstream from array of upstreams based on its current priority (not weight) @@ -116,12 +129,16 @@ struct upstream* get_upstream_by_hash_ketama (void *ups, size_t members, size_t * @param revive_timeout time during which we counts upstream dead * @param max_errors maximum errors during error_timeout to mark upstream dead */ -struct upstream* get_upstream_master_slave (void *ups, size_t members, size_t msize, - time_t now, time_t error_timeout, - time_t revive_timeout, size_t max_errors); +struct upstream * get_upstream_master_slave (void *ups, + size_t members, + size_t msize, + time_t now, + time_t error_timeout, + time_t revive_timeout, + size_t max_errors); #endif /* UPSTREAM_H */ -/* - * vi:ts=4 +/* + * vi:ts=4 */ diff --git a/src/libutil/util.c b/src/libutil/util.c index 8d1a6353d..8a644bfc2 100644 --- a/src/libutil/util.c +++ b/src/libutil/util.c @@ -23,17 +23,17 @@ */ -#include "config.h" -#include "util.h" #include "cfg_file.h" -#include "main.h" -#include "statfile.h" +#include "config.h" #include "filter.h" +#include "main.h" #include "message.h" +#include "statfile.h" +#include "util.h" #ifdef HAVE_OPENSSL -#include <openssl/rand.h> #include <openssl/err.h> +#include <openssl/rand.h> #endif #ifdef HAVE_TERMIOS_H @@ -53,7 +53,7 @@ gint make_socket_nonblocking (gint fd) { - gint ofl; + gint ofl; ofl = fcntl (fd, F_GETFL, 0); @@ -67,7 +67,7 @@ make_socket_nonblocking (gint fd) gint make_socket_blocking (gint fd) { - gint ofl; + gint ofl; ofl = fcntl (fd, F_GETFL, 0); @@ -81,8 +81,8 @@ make_socket_blocking (gint fd) gint poll_sync_socket (gint fd, gint timeout, short events) { - gint r; - struct pollfd fds[1]; + gint r; + struct pollfd fds[1]; fds->fd = fd; fds->events = events; @@ -98,10 +98,10 @@ poll_sync_socket (gint fd, gint timeout, short events) static gint make_inet_socket (gint type, struct addrinfo *addr, gboolean is_server, - gboolean async, GList **list) + gboolean async, GList **list) { - gint fd, r, optlen, on = 1, s_error; - struct addrinfo *cur; + gint fd, r, optlen, on = 1, s_error; + struct addrinfo *cur; cur = addr; while (cur) { @@ -123,10 +123,18 @@ make_inet_socket (gint type, struct addrinfo *addr, gboolean is_server, } if (is_server) { - setsockopt (fd, SOL_SOCKET, SO_REUSEADDR, (const void *)&on, sizeof (gint)); + setsockopt (fd, + SOL_SOCKET, + SO_REUSEADDR, + (const void *)&on, + sizeof (gint)); #ifdef HAVE_IPV6_V6ONLY if (cur->ai_family == AF_INET6) { - setsockopt (fd, IPPROTO_IPV6, IPV6_V6ONLY, (const void *)&on, sizeof (gint)); + setsockopt (fd, + IPPROTO_IPV6, + IPV6_V6ONLY, + (const void *)&on, + sizeof (gint)); } #endif r = bind (fd, cur->ai_addr, cur->ai_addrlen); @@ -137,12 +145,14 @@ make_inet_socket (gint type, struct addrinfo *addr, gboolean is_server, if (r == -1) { if (errno != EINPROGRESS) { - msg_warn ("bind/connect failed: %d, '%s'", errno, strerror (errno)); + msg_warn ("bind/connect failed: %d, '%s'", errno, + strerror (errno)); goto out; } if (!async) { /* Try to poll */ - if (poll_sync_socket (fd, CONNECT_TIMEOUT * 1000, POLLOUT) <= 0) { + if (poll_sync_socket (fd, CONNECT_TIMEOUT * 1000, + POLLOUT) <= 0) { errno = ETIMEDOUT; msg_warn ("bind/connect failed: timeout"); goto out; @@ -196,10 +206,14 @@ make_udp_socket (struct addrinfo *addr, gboolean is_server, gboolean async) } gint -make_unix_socket (const gchar *path, struct sockaddr_un *addr, gint type, gboolean is_server, gboolean async) +make_unix_socket (const gchar *path, + struct sockaddr_un *addr, + gint type, + gboolean is_server, + gboolean async) { - gint fd = -1, s_error, r, optlen, serrno, on = 1; - struct stat st; + gint fd = -1, s_error, r, optlen, serrno, on = 1; + struct stat st; if (path == NULL) return -1; @@ -216,7 +230,10 @@ make_unix_socket (const gchar *path, struct sockaddr_un *addr, gint type, gboole if (lstat (addr->sun_path, &st) != -1) { if (S_ISSOCK (st.st_mode)) { if (unlink (addr->sun_path) == -1) { - msg_warn ("unlink %s failed: %d, '%s'", addr->sun_path, errno, strerror (errno)); + msg_warn ("unlink %s failed: %d, '%s'", + addr->sun_path, + errno, + strerror (errno)); goto out; } } @@ -229,7 +246,10 @@ make_unix_socket (const gchar *path, struct sockaddr_un *addr, gint type, gboole fd = socket (PF_LOCAL, type, 0); if (fd == -1) { - msg_warn ("socket failed %s: %d, '%s'", addr->sun_path, errno, strerror (errno)); + msg_warn ("socket failed %s: %d, '%s'", + addr->sun_path, + errno, + strerror (errno)); return -1; } @@ -239,11 +259,13 @@ make_unix_socket (const gchar *path, struct sockaddr_un *addr, gint type, gboole /* Set close on exec */ if (fcntl (fd, F_SETFD, FD_CLOEXEC) == -1) { - msg_warn ("fcntl failed %s: %d, '%s'", addr->sun_path, errno, strerror (errno)); + msg_warn ("fcntl failed %s: %d, '%s'", addr->sun_path, errno, + strerror (errno)); goto out; } if (is_server) { - setsockopt (fd, SOL_SOCKET, SO_REUSEADDR, (const void *)&on, sizeof (gint)); + setsockopt (fd, SOL_SOCKET, SO_REUSEADDR, (const void *)&on, + sizeof (gint)); r = bind (fd, (struct sockaddr *)addr, SUN_LEN (addr)); } else { @@ -252,7 +274,10 @@ make_unix_socket (const gchar *path, struct sockaddr_un *addr, gint type, gboole if (r == -1) { if (errno != EINPROGRESS) { - msg_warn ("bind/connect failed %s: %d, '%s'", addr->sun_path, errno, strerror (errno)); + msg_warn ("bind/connect failed %s: %d, '%s'", + addr->sun_path, + errno, + strerror (errno)); goto out; } if (!async) { @@ -283,7 +308,7 @@ make_unix_socket (const gchar *path, struct sockaddr_un *addr, gint type, gboole return (fd); - out: +out: serrno = errno; if (fd != -1) { close (fd); @@ -302,13 +327,13 @@ make_unix_socket (const gchar *path, struct sockaddr_un *addr, gint type, gboole */ gint make_universal_socket (const gchar *credits, guint16 port, - gint type, gboolean async, gboolean is_server, gboolean try_resolve) + gint type, gboolean async, gboolean is_server, gboolean try_resolve) { - struct sockaddr_un un; - struct stat st; - struct addrinfo hints, *res; - gint r; - gchar portbuf[8]; + struct sockaddr_un un; + struct stat st; + struct addrinfo hints, *res; + gint r; + gchar portbuf[8]; if (*credits == '/') { if (is_server) { @@ -328,7 +353,11 @@ make_universal_socket (const gchar *credits, guint16 port, return -1; } else { - return make_unix_socket (credits, &un, type, is_server, async); + return make_unix_socket (credits, + &un, + type, + is_server, + async); } } } @@ -355,7 +384,9 @@ make_universal_socket (const gchar *credits, guint16 port, return r; } else { - msg_err ("address resolution for %s failed: %s", credits, gai_strerror (r)); + msg_err ("address resolution for %s failed: %s", + credits, + gai_strerror (r)); return FALSE; } } @@ -369,16 +400,16 @@ make_universal_socket (const gchar *credits, guint16 port, * @param is_server make this socket as server socket * @param try_resolve try name resolution for a socket (BLOCKING) */ -GList* +GList * make_universal_sockets_list (const gchar *credits, guint16 port, - gint type, gboolean async, gboolean is_server, gboolean try_resolve) + gint type, gboolean async, gboolean is_server, gboolean try_resolve) { - struct sockaddr_un un; - struct stat st; - struct addrinfo hints, *res; - gint r, fd, serrno; - gchar portbuf[8], **strv, **cur; - GList *result = NULL, *rcur; + struct sockaddr_un un; + struct stat st; + struct addrinfo hints, *res; + gint r, fd, serrno; + gchar portbuf[8], **strv, **cur; + GList *result = NULL, *rcur; strv = g_strsplit_set (credits, ",", -1); if (strv == NULL) { @@ -405,7 +436,11 @@ make_universal_sockets_list (const gchar *credits, guint16 port, goto err; } else { - fd = make_unix_socket (credits, &un, type, is_server, async); + fd = make_unix_socket (credits, + &un, + type, + is_server, + async); } } } @@ -440,11 +475,13 @@ make_universal_sockets_list (const gchar *credits, guint16 port, } } else { - msg_err ("address resolution for %s failed: %s", credits, gai_strerror (r)); + msg_err ("address resolution for %s failed: %s", + credits, + gai_strerror (r)); goto err; } } - cur ++; + cur++; } g_strfreev (strv); @@ -472,12 +509,13 @@ err: gint make_socketpair (gint pair[2]) { - gint r; + gint r; r = socketpair (AF_LOCAL, SOCK_STREAM, 0, pair); if (r == -1) { - msg_warn ("socketpair failed: %d, '%s'", errno, strerror (errno), pair[0], pair[1]); + msg_warn ("socketpair failed: %d, '%s'", errno, strerror ( + errno), pair[0], pair[1]); return -1; } /* Set close on exec */ @@ -501,7 +539,7 @@ out: gint write_pid (struct rspamd_main *main) { - pid_t pid; + pid_t pid; if (main->cfg->pid_file == NULL) { return -1; @@ -519,7 +557,8 @@ write_pid (struct rspamd_main *main) #else if (fchown (main->pfh->pf_fd, 0, 0) == -1) { #endif - msg_err ("cannot chown of pidfile %s to 0:0 user", main->cfg->pid_file); + msg_err ("cannot chown of pidfile %s to 0:0 user", + main->cfg->pid_file); } } @@ -530,13 +569,15 @@ write_pid (struct rspamd_main *main) #ifdef HAVE_SA_SIGINFO void -init_signals (struct sigaction *signals, void (*sig_handler)(gint, siginfo_t *, void *)) +init_signals (struct sigaction *signals, void (*sig_handler)(gint, + siginfo_t *, + void *)) #else void init_signals (struct sigaction *signals, void (*sig_handler)(gint)) #endif { - struct sigaction sigpipe_act; + struct sigaction sigpipe_act; /* Setting up signal handlers */ /* SIGUSR1 - reopen config file */ /* SIGUSR2 - worker is ready for accept */ @@ -559,8 +600,8 @@ init_signals (struct sigaction *signals, void (*sig_handler)(gint)) signals->sa_flags = 0; #endif sigaction (SIGTERM, signals, NULL); - sigaction (SIGINT, signals, NULL); - sigaction (SIGHUP, signals, NULL); + sigaction (SIGINT, signals, NULL); + sigaction (SIGHUP, signals, NULL); sigaction (SIGCHLD, signals, NULL); sigaction (SIGUSR1, signals, NULL); sigaction (SIGUSR2, signals, NULL); @@ -577,8 +618,8 @@ init_signals (struct sigaction *signals, void (*sig_handler)(gint)) static void pass_signal_cb (gpointer key, gpointer value, gpointer ud) { - struct rspamd_worker *cur = value; - gint signo = GPOINTER_TO_INT (ud); + struct rspamd_worker *cur = value; + gint signo = GPOINTER_TO_INT (ud); kill (cur->pid, signo); } @@ -586,7 +627,7 @@ pass_signal_cb (gpointer key, gpointer value, gpointer ud) void pass_signal_worker (GHashTable * workers, gint signo) { - g_hash_table_foreach (workers, pass_signal_cb, GINT_TO_POINTER (signo)); + g_hash_table_foreach (workers, pass_signal_cb, GINT_TO_POINTER (signo)); } void @@ -600,9 +641,9 @@ convert_to_lowercase (gchar *str, guint size) #ifndef HAVE_SETPROCTITLE -static gchar *title_buffer = 0; -static size_t title_buffer_size = 0; -static gchar *title_progname, *title_progname_full; +static gchar *title_buffer = 0; +static size_t title_buffer_size = 0; +static gchar *title_progname, *title_progname_full; gint setproctitle (const gchar *fmt, ...) @@ -614,24 +655,33 @@ setproctitle (const gchar *fmt, ...) memset (title_buffer, '\0', title_buffer_size); - ssize_t written; + ssize_t written; if (fmt) { - ssize_t written2; - va_list ap; + ssize_t written2; + va_list ap; - written = snprintf (title_buffer, title_buffer_size, "%s: ", title_progname); + written = snprintf (title_buffer, + title_buffer_size, + "%s: ", + title_progname); if (written < 0 || (size_t) written >= title_buffer_size) return -1; va_start (ap, fmt); - written2 = vsnprintf (title_buffer + written, title_buffer_size - written, fmt, ap); + written2 = vsnprintf (title_buffer + written, + title_buffer_size - written, + fmt, + ap); va_end (ap); if (written2 < 0 || (size_t) written2 >= title_buffer_size - written) return -1; } else { - written = snprintf (title_buffer, title_buffer_size, "%s", title_progname); + written = snprintf (title_buffer, + title_buffer_size, + "%s", + title_progname); if (written < 0 || (size_t) written >= title_buffer_size) return -1; } @@ -643,9 +693,9 @@ setproctitle (const gchar *fmt, ...) } /* - It has to be _init function, because __attribute__((constructor)) - functions gets called without arguments. -*/ + It has to be _init function, because __attribute__((constructor)) + functions gets called without arguments. + */ gint init_title (gint argc, gchar *argv[], gchar *envp[]) @@ -654,8 +704,8 @@ init_title (gint argc, gchar *argv[], gchar *envp[]) /* XXX: try to handle these OSes too */ return 0; #else - gchar *begin_of_buffer = 0, *end_of_buffer = 0; - gint i; + gchar *begin_of_buffer = 0, *end_of_buffer = 0; + gint i; for (i = 0; i < argc; ++i) { if (!begin_of_buffer) @@ -674,7 +724,7 @@ init_title (gint argc, gchar *argv[], gchar *envp[]) if (!end_of_buffer) return 0; - gchar **new_environ = g_malloc ((i + 1) * sizeof (envp[0])); + gchar **new_environ = g_malloc ((i + 1) * sizeof (envp[0])); if (!new_environ) return 0; @@ -691,7 +741,7 @@ init_title (gint argc, gchar *argv[], gchar *envp[]) if (!title_progname_full) goto cleanup_enomem; - gchar *p = strrchr (title_progname_full, '/'); + gchar *p = strrchr (title_progname_full, '/'); if (p) title_progname = p + 1; @@ -708,7 +758,7 @@ init_title (gint argc, gchar *argv[], gchar *envp[]) return 0; - cleanup_enomem: +cleanup_enomem: for (--i; i >= 0; --i) { g_free (new_environ[i]); } @@ -719,13 +769,13 @@ init_title (gint argc, gchar *argv[], gchar *envp[]) #endif #ifndef HAVE_PIDFILE -extern gchar *__progname; -static gint _rspamd_pidfile_remove (rspamd_pidfh_t *pfh, gint freeit); +extern gchar *__progname; +static gint _rspamd_pidfile_remove (rspamd_pidfh_t *pfh, gint freeit); static gint rspamd_pidfile_verify (rspamd_pidfh_t *pfh) { - struct stat sb; + struct stat sb; if (pfh == NULL || pfh->pf_fd == -1) return (-1); @@ -742,15 +792,15 @@ rspamd_pidfile_verify (rspamd_pidfh_t *pfh) static gint rspamd_pidfile_read (const gchar *path, pid_t * pidptr) { - gchar buf[16], *endptr; - gint error, fd, i; + gchar buf[16], *endptr; + gint error, fd, i; fd = open (path, O_RDONLY); if (fd == -1) return (errno); i = read (fd, buf, sizeof (buf) - 1); - error = errno; /* Remember errno in case close() wants to change it. */ + error = errno; /* Remember errno in case close() wants to change it. */ close (fd); if (i == -1) return error; @@ -765,20 +815,23 @@ rspamd_pidfile_read (const gchar *path, pid_t * pidptr) return 0; } -rspamd_pidfh_t * +rspamd_pidfh_t * rspamd_pidfile_open (const gchar *path, mode_t mode, pid_t * pidptr) { - rspamd_pidfh_t *pfh; - struct stat sb; - gint error, fd, len, count; - struct timespec rqtp; + rspamd_pidfh_t *pfh; + struct stat sb; + gint error, fd, len, count; + struct timespec rqtp; pfh = g_malloc (sizeof (*pfh)); if (pfh == NULL) return NULL; if (path == NULL) - len = snprintf (pfh->pf_path, sizeof (pfh->pf_path), "/var/run/%s.pid", g_get_prgname ()); + len = snprintf (pfh->pf_path, + sizeof (pfh->pf_path), + "/var/run/%s.pid", + g_get_prgname ()); else len = snprintf (pfh->pf_path, sizeof (pfh->pf_path), "%s", path); if (len >= (gint)sizeof (pfh->pf_path)) { @@ -800,7 +853,7 @@ rspamd_pidfile_open (const gchar *path, mode_t mode, pid_t * pidptr) rqtp.tv_sec = 0; rqtp.tv_nsec = 5000000; if (errno == EWOULDBLOCK && pidptr != NULL) { - again: +again: errno = rspamd_pidfile_read (pfh->pf_path, pidptr); if (errno == 0) errno = EEXIST; @@ -837,8 +890,8 @@ rspamd_pidfile_open (const gchar *path, mode_t mode, pid_t * pidptr) gint rspamd_pidfile_write (rspamd_pidfh_t *pfh) { - gchar pidstr[16]; - gint error, fd; + gchar pidstr[16]; + gint error, fd; /* * Check remembered descriptor, so we don't overwrite some other @@ -877,7 +930,7 @@ rspamd_pidfile_write (rspamd_pidfh_t *pfh) gint rspamd_pidfile_close (rspamd_pidfh_t *pfh) { - gint error; + gint error; error = rspamd_pidfile_verify (pfh); if (error != 0) { @@ -898,7 +951,7 @@ rspamd_pidfile_close (rspamd_pidfh_t *pfh) static gint _rspamd_pidfile_remove (rspamd_pidfh_t *pfh, gint freeit) { - gint error; + gint error; error = rspamd_pidfile_verify (pfh); if (error != 0) { @@ -936,12 +989,15 @@ rspamd_pidfile_remove (rspamd_pidfh_t *pfh) #endif /* Replace %r with rcpt value and %f with from value, new string is allocated in pool */ -gchar * -resolve_stat_filename (rspamd_mempool_t * pool, gchar *pattern, gchar *rcpt, gchar *from) +gchar * +resolve_stat_filename (rspamd_mempool_t * pool, + gchar *pattern, + gchar *rcpt, + gchar *from) { - gint need_to_format = 0, len = 0; - gint rcptlen, fromlen; - gchar *c = pattern, *new, *s; + gint need_to_format = 0, len = 0; + gint rcptlen, fromlen; + gchar *c = pattern, *new, *s; if (rcpt) { rcptlen = strlen (rcpt); @@ -1007,39 +1063,43 @@ resolve_stat_filename (rspamd_mempool_t * pool, gchar *pattern, gchar *rcpt, gch } #ifdef HAVE_CLOCK_GETTIME -const gchar * -calculate_check_time (struct timeval *tv, struct timespec *begin, gint resolution, guint32 *scan_time) +const gchar * +calculate_check_time (struct timeval *tv, + struct timespec *begin, + gint resolution, + guint32 *scan_time) #else -const gchar * -calculate_check_time (struct timeval *begin, gint resolution, guint32 *scan_time) +const gchar * +calculate_check_time (struct timeval *begin, gint resolution, + guint32 *scan_time) #endif { - double vdiff, diff; - static gchar res[64]; - static gchar fmt[sizeof ("%.10f ms real, %.10f ms virtual")]; - struct timeval tv_now; + double vdiff, diff; + static gchar res[64]; + static gchar fmt[sizeof ("%.10f ms real, %.10f ms virtual")]; + struct timeval tv_now; if (gettimeofday (&tv_now, NULL) == -1) { msg_warn ("gettimeofday failed: %s", strerror (errno)); } #ifdef HAVE_CLOCK_GETTIME - struct timespec ts; + struct timespec ts; - diff = (tv_now.tv_sec - tv->tv_sec) * 1000. + /* Seconds */ - (tv_now.tv_usec - tv->tv_usec) / 1000.; /* Microseconds */ + diff = (tv_now.tv_sec - tv->tv_sec) * 1000. + /* Seconds */ + (tv_now.tv_usec - tv->tv_usec) / 1000.; /* Microseconds */ #ifdef HAVE_CLOCK_PROCESS_CPUTIME_ID clock_gettime (CLOCK_PROCESS_CPUTIME_ID, &ts); #elif defined(HAVE_CLOCK_VIRTUAL) - clock_gettime (CLOCK_VIRTUAL, &ts); + clock_gettime (CLOCK_VIRTUAL, &ts); #else - clock_gettime (CLOCK_REALTIME, &ts); + clock_gettime (CLOCK_REALTIME, &ts); #endif - vdiff = (ts.tv_sec - begin->tv_sec) * 1000. + /* Seconds */ - (ts.tv_nsec - begin->tv_nsec) / 1000000.; /* Nanoseconds */ + vdiff = (ts.tv_sec - begin->tv_sec) * 1000. + /* Seconds */ + (ts.tv_nsec - begin->tv_nsec) / 1000000.; /* Nanoseconds */ #else - diff = (tv_now.tv_sec - begin->tv_sec) * 1000. + /* Seconds */ - (tv_now.tv_usec - begin->tv_usec) / 1000.; /* Microseconds */ + diff = (tv_now.tv_sec - begin->tv_sec) * 1000. + /* Seconds */ + (tv_now.tv_usec - begin->tv_usec) / 1000.; /* Microseconds */ vdiff = diff; #endif @@ -1071,9 +1131,9 @@ rspamd_strcase_equal (gconstpointer v, gconstpointer v2) guint rspamd_strcase_hash (gconstpointer key) { - const gchar *p = key; - gchar buf[256]; - guint h = 0, i = 0; + const gchar *p = key; + gchar buf[256]; + guint h = 0, i = 0; while (*p != '\0') { @@ -1096,7 +1156,7 @@ rspamd_strcase_hash (gconstpointer key) guint rspamd_str_hash (gconstpointer key) { - gsize len; + gsize len; len = strlen ((const gchar *)key); @@ -1113,7 +1173,8 @@ gboolean fstr_strcase_equal (gconstpointer v, gconstpointer v2) { const f_str_t *f1 = v, *f2 = v2; - if (f1->len == f2->len && g_ascii_strncasecmp (f1->begin, f2->begin, f1->len) == 0) { + if (f1->len == f2->len && + g_ascii_strncasecmp (f1->begin, f2->begin, f1->len) == 0) { return TRUE; } @@ -1124,11 +1185,11 @@ fstr_strcase_equal (gconstpointer v, gconstpointer v2) guint fstr_strcase_hash (gconstpointer key) { - const f_str_t *f = key; - const gchar *p; - guint h = 0, i = 0; - gchar buf[256]; - + const f_str_t *f = key; + const gchar *p; + guint h = 0, i = 0; + gchar buf[256]; + p = f->begin; while (p - f->begin < (gint)f->len) { buf[i] = g_ascii_tolower (*p); @@ -1151,7 +1212,7 @@ void gperf_profiler_init (struct rspamd_config *cfg, const gchar *descr) { #if defined(WITH_GPERF_TOOLS) - gchar prof_path[PATH_MAX]; + gchar prof_path[PATH_MAX]; if (getenv ("CPUPROFILE")) { @@ -1160,10 +1221,16 @@ gperf_profiler_init (struct rspamd_config *cfg, const gchar *descr) } /* Try to create temp directory for gmon.out and chdir to it */ if (cfg->profile_path == NULL) { - cfg->profile_path = g_strdup_printf ("%s/rspamd-profile", cfg->temp_dir); + cfg->profile_path = + g_strdup_printf ("%s/rspamd-profile", cfg->temp_dir); } - snprintf (prof_path, sizeof (prof_path), "%s-%s.%d", cfg->profile_path, descr, (gint)getpid ()); + snprintf (prof_path, + sizeof (prof_path), + "%s-%s.%d", + cfg->profile_path, + descr, + (gint)getpid ()); if (ProfilerStart (prof_path)) { /* start ITIMER_PROF timer */ ProfilerRegisterThread (); @@ -1177,94 +1244,94 @@ gperf_profiler_init (struct rspamd_config *cfg, const gchar *descr) #ifdef HAVE_FLOCK /* Flock version */ -gboolean +gboolean lock_file (gint fd, gboolean async) { - gint flags; + gint flags; - if (async) { - flags = LOCK_EX | LOCK_NB; - } - else { - flags = LOCK_EX; - } + if (async) { + flags = LOCK_EX | LOCK_NB; + } + else { + flags = LOCK_EX; + } - if (flock (fd, flags) == -1) { - if (async && errno == EAGAIN) { - return FALSE; - } - msg_warn ("lock on file failed: %s", strerror (errno)); - return FALSE; - } + if (flock (fd, flags) == -1) { + if (async && errno == EAGAIN) { + return FALSE; + } + msg_warn ("lock on file failed: %s", strerror (errno)); + return FALSE; + } - return TRUE; + return TRUE; } -gboolean +gboolean unlock_file (gint fd, gboolean async) { - gint flags; + gint flags; - if (async) { - flags = LOCK_UN | LOCK_NB; - } - else { - flags = LOCK_UN; - } + if (async) { + flags = LOCK_UN | LOCK_NB; + } + else { + flags = LOCK_UN; + } - if (flock (fd, flags) == -1) { - if (async && errno == EAGAIN) { - return FALSE; - } - msg_warn ("lock on file failed: %s", strerror (errno)); - return FALSE; - } + if (flock (fd, flags) == -1) { + if (async && errno == EAGAIN) { + return FALSE; + } + msg_warn ("lock on file failed: %s", strerror (errno)); + return FALSE; + } - return TRUE; + return TRUE; } #else /* HAVE_FLOCK */ /* Fctnl version */ -gboolean +gboolean lock_file (gint fd, gboolean async) { - struct flock fl = { - .l_type = F_WRLCK, - .l_whence = SEEK_SET, - .l_start = 0, - .l_len = 0 - }; + struct flock fl = { + .l_type = F_WRLCK, + .l_whence = SEEK_SET, + .l_start = 0, + .l_len = 0 + }; - if (fcntl (fd, async ? F_SETLK : F_SETLKW, &fl) == -1) { - if (async && (errno == EAGAIN || errno == EACCES)) { - return FALSE; - } - msg_warn ("lock on file failed: %s", strerror (errno)); - return FALSE; - } + if (fcntl (fd, async ? F_SETLK : F_SETLKW, &fl) == -1) { + if (async && (errno == EAGAIN || errno == EACCES)) { + return FALSE; + } + msg_warn ("lock on file failed: %s", strerror (errno)); + return FALSE; + } - return TRUE; + return TRUE; } -gboolean +gboolean unlock_file (gint fd, gboolean async) { - struct flock fl = { - .l_type = F_UNLCK, - .l_whence = SEEK_SET, - .l_start = 0, - .l_len = 0 - }; + struct flock fl = { + .l_type = F_UNLCK, + .l_whence = SEEK_SET, + .l_start = 0, + .l_len = 0 + }; - if (fcntl (fd, async ? F_SETLK : F_SETLKW, &fl) == -1) { - if (async && (errno == EAGAIN || errno == EACCES)) { - return FALSE; - } - msg_warn ("lock on file failed: %s", strerror (errno)); - return FALSE; - } + if (fcntl (fd, async ? F_SETLK : F_SETLKW, &fl) == -1) { + if (async && (errno == EAGAIN || errno == EACCES)) { + return FALSE; + } + msg_warn ("lock on file failed: %s", strerror (errno)); + return FALSE; + } - return TRUE; + return TRUE; } #endif /* HAVE_FLOCK */ @@ -1339,14 +1406,14 @@ rspamd_strlcpy_tolower (gchar *dst, const gchar *src, gsize siz) gint compare_email_func (gconstpointer a, gconstpointer b) { - const struct uri *u1 = a, *u2 = b; - gint r; + const struct uri *u1 = a, *u2 = b; + gint r; if (u1->hostlen != u2->hostlen || u1->hostlen == 0) { return u1->hostlen - u2->hostlen; } else { - if ((r = g_ascii_strncasecmp (u1->host, u2->host, u1->hostlen)) == 0){ + if ((r = g_ascii_strncasecmp (u1->host, u2->host, u1->hostlen)) == 0) { if (u1->userlen != u2->userlen || u1->userlen == 0) { return u1->userlen - u2->userlen; } @@ -1365,8 +1432,8 @@ compare_email_func (gconstpointer a, gconstpointer b) gint compare_url_func (gconstpointer a, gconstpointer b) { - const struct uri *u1 = a, *u2 = b; - int r; + const struct uri *u1 = a, *u2 = b; + int r; if (u1->hostlen != u2->hostlen || u1->hostlen == 0) { return u1->hostlen - u2->hostlen; @@ -1388,15 +1455,15 @@ compare_url_func (gconstpointer a, gconstpointer b) gchar * rspamd_strncasestr (const gchar *s, const gchar *find, gint len) { - gchar c, sc; - gsize mlen; + gchar c, sc; + gsize mlen; if ((c = *find++) != 0) { c = g_ascii_tolower (c); mlen = strlen (find); do { do { - if ((sc = *s++) == 0 || len -- == 0) + if ((sc = *s++) == 0 || len-- == 0) return (NULL); } while (g_ascii_tolower (sc) != c); } while (g_ascii_strncasecmp (s, find, mlen) != 0); @@ -1411,16 +1478,16 @@ rspamd_strncasestr (const gchar *s, const gchar *find, gint len) gboolean rspamd_strtol (const gchar *s, gsize len, glong *value) { - const gchar *p = s, *end = s + len; - gchar c; - glong v = 0; - const glong cutoff = G_MAXLONG / 10, cutlim = G_MAXLONG % 10; - gboolean neg; + const gchar *p = s, *end = s + len; + gchar c; + glong v = 0; + const glong cutoff = G_MAXLONG / 10, cutlim = G_MAXLONG % 10; + gboolean neg; /* Case negative values */ if (*p == '-') { neg = TRUE; - p ++; + p++; } else { neg = FALSE; @@ -1444,7 +1511,7 @@ rspamd_strtol (const gchar *s, gsize len, glong *value) else { return FALSE; } - p ++; + p++; } *value = neg ? -(v) : v; @@ -1457,10 +1524,10 @@ rspamd_strtol (const gchar *s, gsize len, glong *value) gboolean rspamd_strtoul (const gchar *s, gsize len, gulong *value) { - const gchar *p = s, *end = s + len; - gchar c; - gulong v = 0; - const gulong cutoff = G_MAXULONG / 10, cutlim = G_MAXULONG % 10; + const gchar *p = s, *end = s + len; + gchar c; + gulong v = 0; + const gulong cutoff = G_MAXULONG / 10, cutlim = G_MAXULONG % 10; /* Some preparations for range errors */ while (p < end) { @@ -1480,7 +1547,7 @@ rspamd_strtoul (const gchar *s, gsize len, gulong *value) else { return FALSE; } - p ++; + p++; } *value = v; @@ -1505,10 +1572,10 @@ rspamd_fallocate (gint fd, off_t offset, off_t len) * Create new mutex * @return mutex or NULL */ -inline rspamd_mutex_t* +inline rspamd_mutex_t * rspamd_mutex_new (void) { - rspamd_mutex_t *new; + rspamd_mutex_t *new; new = g_slice_alloc (sizeof (rspamd_mutex_t)); #if ((GLIB_MAJOR_VERSION == 2) && (GLIB_MINOR_VERSION > 30)) @@ -1561,10 +1628,10 @@ rspamd_mutex_free (rspamd_mutex_t *mtx) * Create new rwlock * @return */ -rspamd_rwlock_t* +rspamd_rwlock_t * rspamd_rwlock_new (void) { - rspamd_rwlock_t *new; + rspamd_rwlock_t *new; new = g_malloc (sizeof (rspamd_rwlock_t)); #if ((GLIB_MAJOR_VERSION == 2) && (GLIB_MINOR_VERSION > 30)) @@ -1651,8 +1718,8 @@ struct rspamd_thread_data { static gpointer rspamd_thread_func (gpointer ud) { - struct rspamd_thread_data *td = ud; - sigset_t s_mask; + struct rspamd_thread_data *td = ud; + sigset_t s_mask; /* Ignore signals in thread */ sigemptyset (&s_mask); @@ -1682,13 +1749,16 @@ rspamd_thread_func (gpointer ud) * @param err error pointer * @return new thread object that can be joined */ -GThread* -rspamd_create_thread (const gchar *name, GThreadFunc func, gpointer data, GError **err) +GThread * +rspamd_create_thread (const gchar *name, + GThreadFunc func, + gpointer data, + GError **err) { - GThread *new; - struct rspamd_thread_data *td; - static gint32 id; - guint r; + GThread *new; + struct rspamd_thread_data *td; + static gint32 id; + guint r; r = strlen (name); td = g_malloc (sizeof (struct rspamd_thread_data)); @@ -1712,15 +1782,15 @@ murmur32_hash (const guint8 *in, gsize len) { - const guint32 c1 = 0xcc9e2d51; - const guint32 c2 = 0x1b873593; + const guint32 c1 = 0xcc9e2d51; + const guint32 c2 = 0x1b873593; - const int nblocks = len / 4; - const guint32 *blocks = (const guint32 *)(in); - const guint8 *tail; - guint32 h = 0; - gint i; - guint32 k; + const int nblocks = len / 4; + const guint32 *blocks = (const guint32 *)(in); + const guint8 *tail; + guint32 h = 0; + gint i; + guint32 k; if (in == NULL || len == 0) { return 0; @@ -1768,15 +1838,15 @@ murmur32_hash (const guint8 *in, gsize len) void murmur128_hash (const guint8 *in, gsize len, guint64 out[]) { - const guint64 c1 = 0x87c37b91114253d5ULL; - const guint64 c2 = 0x4cf5ad432745937fULL; - const gint nblocks = len / 16; - const guint64 *blocks = (const guint64 *)(in); - const guint8 *tail; - guint64 h1 = 0; - guint64 h2 = 0; - int i; - guint64 k1, k2; + const guint64 c1 = 0x87c37b91114253d5ULL; + const guint64 c2 = 0x4cf5ad432745937fULL; + const gint nblocks = len / 16; + const guint64 *blocks = (const guint64 *)(in); + const guint8 *tail; + guint64 h1 = 0; + guint64 h2 = 0; + int i; + guint64 k1, k2; if (in == NULL || len == 0 || out == NULL) { return; @@ -1785,26 +1855,26 @@ murmur128_hash (const guint8 *in, gsize len, guint64 out[]) tail = (const guint8 *)(in + (nblocks * 16)); for (i = 0; i < nblocks; i++) { - k1 = blocks[i*2+0]; - k2 = blocks[i*2+1]; + k1 = blocks[i * 2 + 0]; + k2 = blocks[i * 2 + 1]; k1 *= c1; - k1 = (k1 << 31) | (k1 >> (64 - 31)); + k1 = (k1 << 31) | (k1 >> (64 - 31)); k1 *= c2; h1 ^= k1; h1 = (h1 << 27) | (h1 >> (64 - 27)); h1 += h2; - h1 = h1*5+0x52dce729; + h1 = h1 * 5 + 0x52dce729; k2 *= c2; - k2 = (k2 << 33) | (k2 >> (64 - 33)); + k2 = (k2 << 33) | (k2 >> (64 - 33)); k2 *= c1; h2 ^= k2; h2 = (h2 << 31) | (h2 >> (64 - 31)); h2 += h1; - h2 = h2*5+0x38495ab5; + h2 = h2 * 5 + 0x38495ab5; } k1 = k2 = 0; @@ -1824,7 +1894,7 @@ murmur128_hash (const guint8 *in, gsize len, guint64 out[]) case 9: k2 ^= (guint64)(tail[ 8]) << 0; k2 *= c2; - k2 = (k2 << 33) | (k2 >> (64 - 33)); + k2 = (k2 << 33) | (k2 >> (64 - 33)); k2 *= c1; h2 ^= k2; @@ -1845,7 +1915,7 @@ murmur128_hash (const guint8 *in, gsize len, guint64 out[]) case 1: k1 ^= (guint64)(tail[ 0]) << 0; k1 *= c1; - k1 = (k1 << 31) | (k1 >> (64 - 31)); + k1 = (k1 << 31) | (k1 >> (64 - 31)); k1 *= c2; h1 ^= k1; }; @@ -1888,11 +1958,13 @@ struct hash_copy_callback_data { static void copy_foreach_callback (gpointer key, gpointer value, gpointer ud) { - struct hash_copy_callback_data *cb = ud; - gpointer nkey, nvalue; + struct hash_copy_callback_data *cb = ud; + gpointer nkey, nvalue; nkey = cb->key_copy_func ? cb->key_copy_func (key, cb->ud) : (gpointer)key; - nvalue = cb->value_copy_func ? cb->value_copy_func (value, cb->ud) : (gpointer)value; + nvalue = + cb->value_copy_func ? cb->value_copy_func (value, + cb->ud) : (gpointer)value; g_hash_table_insert (cb->dst, nkey, nvalue); } /** @@ -1903,12 +1975,13 @@ copy_foreach_callback (gpointer key, gpointer value, gpointer ud) * @param value_copy_func function called to copy or modify values (or NULL) * @param ud user data for copy functions */ -void rspamd_hash_table_copy (GHashTable *src, GHashTable *dst, - gpointer (*key_copy_func)(gconstpointer data, gpointer ud), - gpointer (*value_copy_func)(gconstpointer data, gpointer ud), - gpointer ud) +void +rspamd_hash_table_copy (GHashTable *src, GHashTable *dst, + gpointer (*key_copy_func)(gconstpointer data, gpointer ud), + gpointer (*value_copy_func)(gconstpointer data, gpointer ud), + gpointer ud) { - struct hash_copy_callback_data cb; + struct hash_copy_callback_data cb; if (src != NULL && dst != NULL) { cb.key_copy_func = key_copy_func; cb.value_copy_func = value_copy_func; @@ -1927,7 +2000,7 @@ void rspamd_hash_table_copy (GHashTable *src, GHashTable *dst, gpointer rspamd_str_pool_copy (gconstpointer data, gpointer ud) { - rspamd_mempool_t *pool = ud; + rspamd_mempool_t *pool = ud; return data ? rspamd_mempool_strdup (pool, data) : NULL; } @@ -1941,7 +2014,8 @@ parse_ipmask_v4 (const char *line, struct in_addr *ina, int *mask) bzero (ip_buf, sizeof (ip_buf)); if ((pos = strchr (line, '/')) != NULL) { - rspamd_strlcpy (ip_buf, line, MIN ((gsize)(pos - line), sizeof (ip_buf))); + rspamd_strlcpy (ip_buf, line, + MIN ((gsize)(pos - line), sizeof (ip_buf))); rspamd_strlcpy (mask_buf, pos + 1, sizeof (mask_buf)); } else { @@ -1971,7 +2045,8 @@ parse_ipmask_v4 (const char *line, struct in_addr *ina, int *mask) static volatile sig_atomic_t saved_signo[NSIG]; static -void read_pass_tmp_sig_handler (int s) +void +read_pass_tmp_sig_handler (int s) { saved_signo[s] = 1; @@ -1988,7 +2063,8 @@ rspamd_read_passphrase (gchar *buf, gint size, gint rwflag, gpointer key) gint len = 0; gchar pass[BUFSIZ]; - if (readpassphrase ("Enter passphrase: ", buf, size, RPP_ECHO_OFF | RPP_REQUIRE_TTY) == NULL) { + if (readpassphrase ("Enter passphrase: ", buf, size, RPP_ECHO_OFF | + RPP_REQUIRE_TTY) == NULL) { return 0; } @@ -2014,16 +2090,17 @@ restart: errno = ENOTTY; return 0; } - memcpy(&term, &oterm, sizeof(term)); + memcpy (&term, &oterm, sizeof(term)); term.c_lflag &= ~(ECHO | ECHONL); - (void)tcsetattr(input, TCSAFLUSH, &term); - (void)write (output, "Enter passphrase: ", sizeof ("Enter passphrase: ") - 1); + (void)tcsetattr (input, TCSAFLUSH, &term); + (void)write (output, "Enter passphrase: ", sizeof ("Enter passphrase: ") - + 1); /* Save the current sighandler */ for (i = 0; i < NSIG; i++) { saved_signo[i] = 0; } - sigemptyset(&sa.sa_mask); + sigemptyset (&sa.sa_mask); sa.sa_flags = 0; sa.sa_handler = read_pass_tmp_sig_handler; (void)sigaction (SIGALRM, &sa, &savealrm); @@ -2050,7 +2127,7 @@ restart: /* Restore terminal state */ if (memcmp (&term, &oterm, sizeof (term)) != 0) { while (tcsetattr (input, TCSAFLUSH, &oterm) == -1 && - errno == EINTR && !saved_signo[SIGTTOU]); + errno == EINTR && !saved_signo[SIGTTOU]) ; } /* Restore signal handlers */ @@ -2069,7 +2146,7 @@ restart: /* Send signals pending */ for (i = 0; i < NSIG; i++) { if (saved_signo[i]) { - kill(getpid(), i); + kill (getpid (), i); switch (i) { case SIGTSTP: case SIGTTIN: @@ -2092,15 +2169,16 @@ rspamd_ip_is_valid (rspamd_inet_addr_t *addr) gboolean ret = FALSE; if (G_LIKELY (addr->af == AF_INET)) { - if (memcmp (&addr->addr.s4.sin_addr, &ip4_any, sizeof (struct in_addr)) != 0 && - memcmp (&addr->addr.s4.sin_addr, &ip4_none, - sizeof (struct in_addr)) != 0) { + if (memcmp (&addr->addr.s4.sin_addr, &ip4_any, + sizeof (struct in_addr)) != 0 && + memcmp (&addr->addr.s4.sin_addr, &ip4_none, + sizeof (struct in_addr)) != 0) { ret = TRUE; } } else if (G_UNLIKELY (addr->af == AF_INET6)) { if (memcmp (&addr->addr.s6.sin6_addr, &ip6_any, - sizeof (struct in6_addr)) != 0) { + sizeof (struct in6_addr)) != 0) { ret = TRUE; } } @@ -2173,7 +2251,9 @@ rspamd_gstring_append_double (double val, void *ud) } void -rspamd_ucl_emit_gstring (ucl_object_t *obj, enum ucl_emitter emit_type, GString *target) +rspamd_ucl_emit_gstring (ucl_object_t *obj, + enum ucl_emitter emit_type, + GString *target) { struct ucl_emitter_functions func = { .ucl_emitter_append_character = rspamd_gstring_append_character, @@ -2214,7 +2294,7 @@ rspamd_accept_from_socket (gint sock, rspamd_inet_addr_t *addr) return (nfd); - out: +out: serrno = errno; close (nfd); errno = serrno; @@ -2243,7 +2323,7 @@ rspamd_parse_inet_address (rspamd_inet_addr_t *target, const char *src) return ret; } -const char* +const char * rspamd_inet_address_to_string (rspamd_inet_addr_t *addr) { static char addr_str[INET6_ADDRSTRLEN + 1]; @@ -2251,10 +2331,10 @@ rspamd_inet_address_to_string (rspamd_inet_addr_t *addr) switch (addr->af) { case AF_INET: return inet_ntop (addr->af, &addr->addr.s4.sin_addr, addr_str, - sizeof (addr_str)); + sizeof (addr_str)); case AF_INET6: return inet_ntop (addr->af, &addr->addr.s6.sin6_addr, addr_str, - sizeof (addr_str)); + sizeof (addr_str)); case AF_UNIX: return addr->addr.su.sun_path; } diff --git a/src/libutil/util.h b/src/libutil/util.h index a142f15fb..0b506967d 100644 --- a/src/libutil/util.h +++ b/src/libutil/util.h @@ -2,10 +2,10 @@ #define RSPAMD_UTIL_H #include "config.h" +#include "fstring.h" #include "mem_pool.h" -#include "statfile.h" #include "printf.h" -#include "fstring.h" +#include "statfile.h" #include "ucl.h" struct rspamd_config; @@ -44,7 +44,11 @@ gint make_udp_socket (struct addrinfo *, gboolean is_server, gboolean async); /* * Create and bind or connect unix socket */ -gint make_unix_socket (const gchar *, struct sockaddr_un *, gint type, gboolean is_server, gboolean async); +gint make_unix_socket (const gchar *, + struct sockaddr_un *, + gint type, + gboolean is_server, + gboolean async); /** * Make a universal socket @@ -56,7 +60,7 @@ gint make_unix_socket (const gchar *, struct sockaddr_un *, gint type, gboolean * @param try_resolve try name resolution for a socket (BLOCKING) */ gint make_universal_socket (const gchar *credits, guint16 port, gint type, - gboolean async, gboolean is_server, gboolean try_resolve); + gboolean async, gboolean is_server, gboolean try_resolve); /** * Make a universal sockets @@ -67,8 +71,12 @@ gint make_universal_socket (const gchar *credits, guint16 port, gint type, * @param is_server make this socket as server socket * @param try_resolve try name resolution for a socket (BLOCKING) */ -GList* make_universal_sockets_list (const gchar *credits, guint16 port, gint type, - gboolean async, gboolean is_server, gboolean try_resolve); +GList * make_universal_sockets_list (const gchar *credits, + guint16 port, + gint type, + gboolean async, + gboolean is_server, + gboolean try_resolve); /* * Create socketpair */ @@ -97,7 +105,9 @@ gint poll_sync_socket (gint fd, gint timeout, short events); * Init signals */ #ifdef HAVE_SA_SIGINFO -void init_signals (struct sigaction *sa, void (*sig_handler)(gint, siginfo_t *, void *)); +void init_signals (struct sigaction *sa, void (*sig_handler)(gint, + siginfo_t *, + void *)); #else void init_signals (struct sigaction *sa, void (*sig_handler)(gint)); #endif @@ -115,8 +125,8 @@ void convert_to_lowercase (gchar *str, guint size); /* * Process title utility functions */ -gint init_title(gint argc, gchar *argv[], gchar *envp[]); -gint setproctitle(const gchar *fmt, ...); +gint init_title (gint argc, gchar *argv[], gchar *envp[]); +gint setproctitle (const gchar *fmt, ...); #endif #ifndef HAVE_PIDFILE @@ -126,19 +136,21 @@ gint setproctitle(const gchar *fmt, ...); typedef struct rspamd_pidfh_s { gint pf_fd; #ifdef HAVE_PATH_MAX - gchar pf_path[PATH_MAX + 1]; + gchar pf_path[PATH_MAX + 1]; #elif defined(HAVE_MAXPATHLEN) - gchar pf_path[MAXPATHLEN + 1]; + gchar pf_path[MAXPATHLEN + 1]; #else - gchar pf_path[1024 + 1]; + gchar pf_path[1024 + 1]; #endif - dev_t pf_dev; - ino_t pf_ino; + dev_t pf_dev; + ino_t pf_ino; } rspamd_pidfh_t; -rspamd_pidfh_t *rspamd_pidfile_open(const gchar *path, mode_t mode, pid_t *pidptr); -gint rspamd_pidfile_write(rspamd_pidfh_t *pfh); -gint rspamd_pidfile_close(rspamd_pidfh_t *pfh); -gint rspamd_pidfile_remove(rspamd_pidfh_t *pfh); +rspamd_pidfh_t * rspamd_pidfile_open (const gchar *path, + mode_t mode, + pid_t *pidptr); +gint rspamd_pidfile_write (rspamd_pidfh_t *pfh); +gint rspamd_pidfile_close (rspamd_pidfh_t *pfh); +gint rspamd_pidfile_remove (rspamd_pidfh_t *pfh); #else typedef struct pidfh rspamd_pidfh_t; #define rspamd_pidfile_open pidfile_open @@ -150,14 +162,22 @@ typedef struct pidfh rspamd_pidfh_t; /* * Replace %r with rcpt value and %f with from value, new string is allocated in pool */ -gchar* resolve_stat_filename (rspamd_mempool_t *pool, gchar *pattern, gchar *rcpt, gchar *from); +gchar * resolve_stat_filename (rspamd_mempool_t *pool, + gchar *pattern, + gchar *rcpt, + gchar *from); #ifdef HAVE_CLOCK_GETTIME /* * Calculate check time with specified resolution of timer */ -const gchar* calculate_check_time (struct timeval *tv, struct timespec *begin, gint resolution, guint32 *scan_ms); +const gchar * calculate_check_time (struct timeval *tv, + struct timespec *begin, + gint resolution, + guint32 *scan_ms); #else -const gchar* calculate_check_time (struct timeval *begin, gint resolution, guint32 *scan_ms); +const gchar * calculate_check_time (struct timeval *begin, + gint resolution, + guint32 *scan_ms); #endif /* @@ -225,8 +245,12 @@ gsize rspamd_strlcpy_tolower (gchar *dst, const gchar *src, gsize siz); /* * Convert milliseconds to timeval fields */ -#define msec_to_tv(msec, tv) do { (tv)->tv_sec = (msec) / 1000; (tv)->tv_usec = ((msec) - (tv)->tv_sec * 1000) * 1000; } while(0) -#define double_to_tv(dbl, tv) do { (tv)->tv_sec = (int)(dbl); (tv)->tv_usec = ((dbl) - (int)(dbl))*1000*1000; } while(0) +#define msec_to_tv(msec, tv) do { (tv)->tv_sec = (msec) / 1000; (tv)->tv_usec = \ + ((msec) - (tv)->tv_sec * 1000) * 1000; \ +} while (0) +#define double_to_tv(dbl, tv) do { (tv)->tv_sec = (int)(dbl); (tv)->tv_usec = \ + ((dbl) - (int)(dbl)) * 1000 * 1000; \ +} while (0) #define tv_to_msec(tv) (tv)->tv_sec * 1000 + (tv)->tv_usec / 1000 /* Compare two emails for building emails tree */ @@ -238,7 +262,7 @@ gint compare_url_func (gconstpointer a, gconstpointer b); /* * Find string find in string s ignoring case */ -gchar* rspamd_strncasestr (const gchar *s, const gchar *find, gint len); +gchar * rspamd_strncasestr (const gchar *s, const gchar *find, gint len); /* * Try to convert string of length to long @@ -283,7 +307,7 @@ typedef struct rspamd_rwlock_s { * Create new mutex * @return mutex or NULL */ -rspamd_mutex_t* rspamd_mutex_new (void); +rspamd_mutex_t * rspamd_mutex_new (void); /** * Lock mutex @@ -307,7 +331,7 @@ void rspamd_mutex_free (rspamd_mutex_t *mtx); * Create new rwloc * @return */ -rspamd_rwlock_t* rspamd_rwlock_new (void); +rspamd_rwlock_t * rspamd_rwlock_new (void); /** * Lock rwlock for writing @@ -357,7 +381,10 @@ rspamd_cond_wait (GCond *cond, rspamd_mutex_t *mtx) * @param err error pointer * @return new thread object that can be joined */ -GThread* rspamd_create_thread (const gchar *name, GThreadFunc func, gpointer data, GError **err); +GThread * rspamd_create_thread (const gchar *name, + GThreadFunc func, + gpointer data, + GError **err); /** * Return 32bit murmur hash value for specified input @@ -403,9 +430,9 @@ void murmur128_hash (const guint8 *in, gsize len, guint64 out[]); * @param ud user data for copy functions */ void rspamd_hash_table_copy (GHashTable *src, GHashTable *dst, - gpointer (*key_copy_func)(gconstpointer data, gpointer ud), - gpointer (*value_copy_func)(gconstpointer data, gpointer ud), - gpointer ud); + gpointer (*key_copy_func)(gconstpointer data, gpointer ud), + gpointer (*value_copy_func)(gconstpointer data, gpointer ud), + gpointer ud); /** * Utility function to provide mem_pool copy for rspamd_hash_table_copy function @@ -448,7 +475,9 @@ gboolean rspamd_ip_is_valid (rspamd_inet_addr_t *addr); * @param emit_type emitter type * @param target target string */ -void rspamd_ucl_emit_gstring (ucl_object_t *obj, enum ucl_emitter emit_type, GString *target); +void rspamd_ucl_emit_gstring (ucl_object_t *obj, + enum ucl_emitter emit_type, + GString *target); /** * Accept from listening socket filling addr structure @@ -464,14 +493,15 @@ gint rspamd_accept_from_socket (gint sock, rspamd_inet_addr_t *addr); * @param src IP string representation * @return TRUE if addr has been parsed */ -gboolean rspamd_parse_inet_address (rspamd_inet_addr_t *target, const char *src); +gboolean rspamd_parse_inet_address (rspamd_inet_addr_t *target, + const char *src); /** * Returns string representation of inet address * @param addr * @return statically allocated string pointer (not thread safe) */ -const char* rspamd_inet_address_to_string (rspamd_inet_addr_t *addr); +const char * rspamd_inet_address_to_string (rspamd_inet_addr_t *addr); /** * Returns port number for the specified inet address in host byte order |