email plugin: Kill threads before closing the associated socket
[collectd.git] / src / email.c
1 /**
2  * collectd - src/email.c
3  * Copyright (C) 2006  Sebastian Harl
4  *
5  * This program is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License as published by the
7  * Free Software Foundation; either version 2 of the License, or (at your
8  * option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License along
16  * with this program; if not, write to the Free Software Foundation, Inc.,
17  * 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
18  *
19  * Author:
20  *   Sebastian Harl <sh at tokkee.org>
21  **/
22
23 /*
24  * This plugin communicates with a spam filter, a virus scanner or similar
25  * software using a UNIX socket and a very simple protocol:
26  *
27  * e-mail type (e.g. ham, spam, virus, ...) and size
28  * e:<type>:<bytes>
29  *
30  * spam score
31  * s:<value>
32  *
33  * successful spam checks
34  * c:<type1>[,<type2>,...]
35  */
36
37 #include "collectd.h"
38 #include "common.h"
39 #include "plugin.h"
40
41 #include "configfile.h"
42
43 #if HAVE_LIBPTHREAD
44 # include <pthread.h>
45 # define EMAIL_HAVE_READ 1
46 #else
47 # define EMAIL_HAVE_READ 0
48 #endif
49
50 #if HAVE_SYS_SELECT_H
51 #       include <sys/select.h>
52 #endif /* HAVE_SYS_SELECT_H */
53
54 #if HAVE_SYS_SOCKET_H
55 #       include <sys/socket.h>
56 #endif /* HAVE_SYS_SOCKET_H */
57
58 /* *sigh* glibc does not define UNIX_PATH_MAX in sys/un.h ... */
59 #if HAVE_LINUX_UN_H
60 #       include <linux/un.h>
61 #elif HAVE_SYS_UN_H
62 #       include <sys/un.h>
63 #endif /* HAVE_LINUX_UN_H | HAVE_SYS_UN_H */
64
65 /* some systems (e.g. Darwin) seem to not define UNIX_PATH_MAX at all */
66 #ifndef UNIX_PATH_MAX
67 # define UNIX_PATH_MAX sizeof (((struct sockaddr_un *)0)->sun_path)
68 #endif /* UNIX_PATH_MAX */
69
70 #if HAVE_GRP_H
71 #       include <grp.h>
72 #endif /* HAVE_GRP_H */
73
74 #define MODULE_NAME "email"
75
76 /* 256 bytes ought to be enough for anybody ;-) */
77 #define BUFSIZE 256
78
79 #ifndef COLLECTD_SOCKET_PREFIX
80 # define COLLECTD_SOCKET_PREFIX "/tmp/.collectd-"
81 #endif /* COLLECTD_SOCKET_PREFIX */
82
83 #define SOCK_PATH COLLECTD_SOCKET_PREFIX"email"
84 #define MAX_CONNS 5
85 #define MAX_CONNS_LIMIT 16384
86
87 /*
88  * Private data structures
89  */
90 #if EMAIL_HAVE_READ
91 /* linked list of email and check types */
92 typedef struct type {
93         char        *name;
94         int         value;
95         struct type *next;
96 } type_t;
97
98 typedef struct {
99         type_t *head;
100         type_t *tail;
101 } type_list_t;
102
103 /* collector thread control information */
104 typedef struct collector {
105         pthread_t thread;
106
107         /* socket descriptor of the current/last connection */
108         int socket;
109 } collector_t;
110
111 /* linked list of pending connections */
112 typedef struct conn {
113         /* socket to read data from */
114         int socket;
115
116         /* buffer to read data to */
117         char *buffer;
118         int  idx; /* current write position in buffer */
119         int  length; /* length of the current line, i.e. index of '\0' */
120
121         struct conn *next;
122 } conn_t;
123
124 typedef struct {
125         conn_t *head;
126         conn_t *tail;
127 } conn_list_t;
128 #endif /* EMAIL_HAVE_READ */
129
130 /*
131  * Private variables
132  */
133 #if EMAIL_HAVE_READ
134 /* valid configuration file keys */
135 static char *config_keys[] =
136 {
137         "SocketGroup",
138         "SocketPerms",
139         "MaxConns",
140         NULL
141 };
142 static int config_keys_num = 3;
143
144 /* socket configuration */
145 static char *sock_group = COLLECTD_GRP_NAME;
146 static int  sock_perms  = S_IRWXU | S_IRWXG;
147 static int  max_conns   = MAX_CONNS;
148
149 /* state of the plugin */
150 static int disabled = 0;
151
152 /* thread managing "client" connections */
153 static pthread_t connector;
154 static int connector_socket;
155
156 /* tell the collector threads that a new connection is available */
157 static pthread_cond_t conn_available = PTHREAD_COND_INITIALIZER;
158
159 /* connections that are waiting to be processed */
160 static pthread_mutex_t conns_mutex = PTHREAD_MUTEX_INITIALIZER;
161 static conn_list_t conns;
162
163 /* tell the connector thread that a collector is available */
164 static pthread_cond_t collector_available = PTHREAD_COND_INITIALIZER;
165
166 /* collector threads */
167 static collector_t **collectors;
168
169 static pthread_mutex_t available_mutex = PTHREAD_MUTEX_INITIALIZER;
170 static int available_collectors;
171
172 static pthread_mutex_t count_mutex = PTHREAD_MUTEX_INITIALIZER;
173 static type_list_t count;
174
175 static pthread_mutex_t size_mutex = PTHREAD_MUTEX_INITIALIZER;
176 static type_list_t size;
177
178 static pthread_mutex_t score_mutex = PTHREAD_MUTEX_INITIALIZER;
179 static double score;
180 static int score_count;
181
182 static pthread_mutex_t check_mutex = PTHREAD_MUTEX_INITIALIZER;
183 static type_list_t check;
184 #endif /* EMAIL_HAVE_READ */
185
186 #define COUNT_FILE "email/email-%s.rrd"
187 static char *count_ds_def[] =
188 {
189         "DS:count:GAUGE:"COLLECTD_HEARTBEAT":0:U",
190         NULL
191 };
192 static int count_ds_num = 1;
193
194 #define SIZE_FILE  "email/email_size-%s.rrd"
195 static char *size_ds_def[] =
196 {
197         "DS:size:GAUGE:"COLLECTD_HEARTBEAT":0:U",
198         NULL
199 };
200 static int size_ds_num = 1;
201
202 #define SCORE_FILE "email/spam_score.rrd"
203 static char *score_ds_def[] =
204 {
205         "DS:score:GAUGE:"COLLECTD_HEARTBEAT":U:U",
206         NULL
207 };
208 static int score_ds_num = 1;
209
210 #define CHECK_FILE "email/spam_check-%s.rrd"
211 static char *check_ds_def[] =
212 {
213         "DS:hits:GAUGE:"COLLECTD_HEARTBEAT":0:U",
214         NULL
215 };
216 static int check_ds_num = 1;
217
218 #if EMAIL_HAVE_READ
219 static int email_config (char *key, char *value)
220 {
221         if (0 == strcasecmp (key, "SocketGroup")) {
222                 sock_group = sstrdup (value);
223         }
224         else if (0 == strcasecmp (key, "SocketPerms")) {
225                 /* the user is responsible for providing reasonable values */
226                 sock_perms = (int)strtol (value, NULL, 8);
227         }
228         else if (0 == strcasecmp (key, "MaxConns")) {
229                 long int tmp = strtol (value, NULL, 0);
230
231                 if (tmp < 1) {
232                         fprintf (stderr, "email plugin: `MaxConns' was set to invalid "
233                                         "value %li, will use default %i.\n",
234                                         tmp, MAX_CONNS);
235                         max_conns = MAX_CONNS;
236                 }
237                 else if (tmp > MAX_CONNS_LIMIT) {
238                         fprintf (stderr, "email plugin: `MaxConns' was set to invalid "
239                                         "value %li, will use hardcoded limit %i.\n",
240                                         tmp, MAX_CONNS_LIMIT);
241                         max_conns = MAX_CONNS_LIMIT;
242                 }
243                 else {
244                         max_conns = (int)tmp;
245                 }
246         }
247         else {
248                 return -1;
249         }
250         return 0;
251 } /* static int email_config (char *, char *) */
252
253 /* Increment the value of the given name in the given list by incr. */
254 static void type_list_incr (type_list_t *list, char *name, int incr)
255 {
256         if (NULL == list->head) {
257                 list->head = (type_t *)smalloc (sizeof (type_t));
258
259                 list->head->name  = sstrdup (name);
260                 list->head->value = incr;
261                 list->head->next  = NULL;
262
263                 list->tail = list->head;
264         }
265         else {
266                 type_t *ptr;
267
268                 for (ptr = list->head; NULL != ptr; ptr = ptr->next) {
269                         if (0 == strcmp (name, ptr->name))
270                                 break;
271                 }
272
273                 if (NULL == ptr) {
274                         list->tail->next = (type_t *)smalloc (sizeof (type_t));
275                         list->tail = list->tail->next;
276
277                         list->tail->name  = sstrdup (name);
278                         list->tail->value = incr;
279                         list->tail->next  = NULL;
280                 }
281                 else {
282                         ptr->value += incr;
283                 }
284         }
285         return;
286 } /* static void type_list_incr (type_list_t *, char *) */
287
288 /* Read a single character from the socket. If an error occurs or end-of-file
289  * is reached return '\0'. */
290 char read_char (conn_t *src)
291 {
292         char ret = '\0';
293
294         fd_set fdset;
295
296         FD_ZERO (&fdset);
297         FD_SET (src->socket, &fdset);
298
299         if (-1 == select (src->socket + 1, &fdset, NULL, NULL, NULL)) {
300                 syslog (LOG_ERR, "select() failed: %s", strerror (errno));
301                 return '\0';
302         }
303
304         assert (FD_ISSET (src->socket, &fdset));
305
306         do {
307                 ssize_t len = 0;
308
309                 errno = 0;
310                 if (0 > (len = read (src->socket, (void *)&ret, 1))) {
311                         if (EINTR != errno) {
312                                 syslog (LOG_ERR, "read() failed: %s", strerror (errno));
313                                 return '\0';
314                         }
315                 }
316
317                 if (0 == len)
318                         return '\0';
319         } while (EINTR == errno);
320         return ret;
321 } /* char read_char (conn_t *) */
322
323 /* Read a single line (terminated by '\n') from the the socket.
324  *
325  * The return value is zero terminated and does not contain any newline
326  * characters.
327  *
328  * If an error occurs or end-of-file is reached return NULL.
329  *
330  * IMPORTANT NOTE: If there is no newline character found in BUFSIZE
331  * characters of the input stream, the line will will be ignored! By
332  * definition we should not get any longer input lines, thus this is
333  * acceptable in this case ;-) */
334 char *read_line (conn_t *src)
335 {
336         int i = 0;
337
338         assert ((BUFSIZE >= src->idx) && (src->idx >= 0));
339         assert ((src->idx > src->length) || (src->length == 0));
340
341         if (src->length > 0) { /* remove old line */
342                 src->idx -= (src->length + 1);
343                 memmove (src->buffer, src->buffer + src->length + 1, src->idx);
344                 src->length = 0;
345         }
346
347         for (i = 0; i < src->idx; ++i) {
348                 if ('\n' == src->buffer[i])
349                         break;
350         }
351
352         if (i == src->idx) {
353                 fd_set fdset;
354
355                 ssize_t len = 0;
356
357                 FD_ZERO (&fdset);
358                 FD_SET (src->socket, &fdset);
359
360                 if (-1 == select (src->socket + 1, &fdset, NULL, NULL, NULL)) {
361                         syslog (LOG_ERR, "select() failed: %s", strerror (errno));
362                         return NULL;
363                 }
364
365                 assert (FD_ISSET (src->socket, &fdset));
366
367                 do {
368                         errno = 0;
369                         if (0 > (len = read (src->socket,
370                                                         (void *)(src->buffer + src->idx),
371                                                         BUFSIZE - src->idx))) {
372                                 if (EINTR != errno) {
373                                         syslog (LOG_ERR, "read() failed: %s", strerror (errno));
374                                         return NULL;
375                                 }
376                         }
377
378                         if (0 == len)
379                                 return NULL;
380                 } while (EINTR == errno);
381
382                 src->idx += len;
383
384                 for (i = src->idx - len; i < src->idx; ++i) {
385                         if ('\n' == src->buffer[i])
386                                 break;
387                 }
388
389                 if (i == src->idx) {
390                         src->length = 0;
391
392                         if (BUFSIZE == src->idx) { /* no space left in buffer */
393                                 while ('\n' != read_char (src))
394                                         /* ignore complete line */;
395
396                                 src->idx = 0;
397                         }
398                         return read_line (src);
399                 }
400         }
401
402         src->buffer[i] = '\0';
403         src->length    = i;
404
405         return src->buffer;
406 } /* char *read_line (conn_t *) */
407
408 static void *collect (void *arg)
409 {
410         collector_t *this = (collector_t *)arg;
411
412         char *buffer = (char *)smalloc (BUFSIZE);
413
414         while (1) {
415                 int loop = 1;
416
417                 conn_t *connection;
418
419                 pthread_mutex_lock (&conns_mutex);
420
421                 while (NULL == conns.head) {
422                         pthread_cond_wait (&conn_available, &conns_mutex);
423                 }
424
425                 connection = conns.head;
426                 conns.head = conns.head->next;
427
428                 if (NULL == conns.head) {
429                         conns.tail = NULL;
430                 }
431
432                 this->socket = connection->socket;
433
434                 pthread_mutex_unlock (&conns_mutex);
435
436                 connection->buffer = buffer;
437                 connection->idx    = 0;
438                 connection->length = 0;
439
440                 { /* put the socket in non-blocking mode */
441                         int flags = 0;
442
443                         errno = 0;
444                         if (-1 == fcntl (connection->socket, F_GETFL, &flags)) {
445                                 syslog (LOG_ERR, "fcntl() failed: %s", strerror (errno));
446                                 loop = 0;
447                         }
448
449                         errno = 0;
450                         if (-1 == fcntl (connection->socket, F_SETFL, flags | O_NONBLOCK)) {
451                                 syslog (LOG_ERR, "fcntl() failed: %s", strerror (errno));
452                                 loop = 0;
453                         }
454                 }
455
456                 while (loop) {
457                         char *line = read_line (connection);
458
459                         if (NULL == line) {
460                                 loop = 0;
461                                 break;
462                         }
463
464                         if (':' != line[1]) {
465                                 syslog (LOG_ERR, "email: syntax error in line '%s'", line);
466                                 continue;
467                         }
468
469                         if ('e' == line[0]) { /* e:<type>:<bytes> */
470                                 char *ptr  = NULL;
471                                 char *type = strtok_r (line + 2, ":", &ptr);
472                                 char *tmp  = strtok_r (NULL, ":", &ptr);
473                                 int  bytes = 0;
474
475                                 if (NULL == tmp) {
476                                         syslog (LOG_ERR, "email: syntax error in line '%s'", line);
477                                         continue;
478                                 }
479
480                                 bytes = atoi (tmp);
481
482                                 pthread_mutex_lock (&count_mutex);
483                                 type_list_incr (&count, type, 1);
484                                 pthread_mutex_unlock (&count_mutex);
485
486                                 pthread_mutex_lock (&size_mutex);
487                                 type_list_incr (&size, type, bytes);
488                                 pthread_mutex_unlock (&size_mutex);
489                         }
490                         else if ('s' == line[0]) { /* s:<value> */
491                                 pthread_mutex_lock (&score_mutex);
492                                 score = (score * (double)score_count + atof (line + 2))
493                                                 / (double)(score_count + 1);
494                                 ++score_count;
495                                 pthread_mutex_unlock (&score_mutex);
496                         }
497                         else if ('c' == line[0]) { /* c:<type1>[,<type2>,...] */
498                                 char *ptr  = NULL;
499                                 char *type = strtok_r (line + 2, ",", &ptr);
500
501                                 do {
502                                         pthread_mutex_lock (&check_mutex);
503                                         type_list_incr (&check, type, 1);
504                                         pthread_mutex_unlock (&check_mutex);
505                                 } while (NULL != (type = strtok_r (NULL, ",", &ptr)));
506                         }
507                         else {
508                                 syslog (LOG_ERR, "email: unknown type '%c'", line[0]);
509                         }
510                 } /* while (loop) */
511
512                 close (connection->socket);
513
514                 free (connection);
515
516                 pthread_mutex_lock (&available_mutex);
517                 ++available_collectors;
518                 pthread_mutex_unlock (&available_mutex);
519
520                 pthread_cond_signal (&collector_available);
521         } /* while (1) */
522
523         free (buffer);
524         pthread_exit ((void *)0);
525 } /* void *collect (void *) */
526
527 static void *open_connection (void *arg)
528 {
529         struct sockaddr_un addr;
530
531         /* create UNIX socket */
532         errno = 0;
533         if (-1 == (connector_socket = socket (PF_UNIX, SOCK_STREAM, 0))) {
534                 disabled = 1;
535                 syslog (LOG_ERR, "socket() failed: %s", strerror (errno));
536                 pthread_exit ((void *)1);
537         }
538
539         addr.sun_family = AF_UNIX;
540
541         strncpy (addr.sun_path, SOCK_PATH, (size_t)(UNIX_PATH_MAX - 1));
542         addr.sun_path[UNIX_PATH_MAX - 1] = '\0';
543         unlink (addr.sun_path);
544
545         errno = 0;
546         if (-1 == bind (connector_socket, (struct sockaddr *)&addr,
547                                 offsetof (struct sockaddr_un, sun_path)
548                                         + strlen(addr.sun_path))) {
549                 disabled = 1;
550                 syslog (LOG_ERR, "bind() failed: %s", strerror (errno));
551                 pthread_exit ((void *)1);
552         }
553
554         errno = 0;
555         if (-1 == listen (connector_socket, 5)) {
556                 disabled = 1;
557                 syslog (LOG_ERR, "listen() failed: %s", strerror (errno));
558                 pthread_exit ((void *)1);
559         }
560
561         if ((uid_t)0 == geteuid ()) {
562                 struct group *grp;
563
564                 errno = 0;
565                 if (NULL != (grp = getgrnam (sock_group))) {
566                         errno = 0;
567                         if (0 != chown (SOCK_PATH, (uid_t)-1, grp->gr_gid)) {
568                                 syslog (LOG_WARNING, "chown() failed: %s", strerror (errno));
569                         }
570                 }
571                 else {
572                         syslog (LOG_WARNING, "getgrnam() failed: %s", strerror (errno));
573                 }
574         }
575         else {
576                 syslog (LOG_WARNING, "not running as root");
577         }
578
579         errno = 0;
580         if (0 != chmod (SOCK_PATH, sock_perms)) {
581                 syslog (LOG_WARNING, "chmod() failed: %s", strerror (errno));
582         }
583
584         { /* initialize collector threads */
585                 int i   = 0;
586                 int err = 0;
587
588                 pthread_attr_t ptattr;
589
590                 conns.head = NULL;
591                 conns.tail = NULL;
592
593                 pthread_attr_init (&ptattr);
594                 pthread_attr_setdetachstate (&ptattr, PTHREAD_CREATE_DETACHED);
595
596                 available_collectors = max_conns;
597
598                 collectors =
599                         (collector_t **)smalloc (max_conns * sizeof (collector_t *));
600
601                 for (i = 0; i < max_conns; ++i) {
602                         collectors[i] = (collector_t *)smalloc (sizeof (collector_t));
603                         collectors[i]->socket = 0;
604
605                         if (0 != (err = pthread_create (&collectors[i]->thread, &ptattr,
606                                                         collect, collectors[i]))) {
607                                 syslog (LOG_ERR, "pthread_create() failed: %s",
608                                                 strerror (err));
609                         }
610                 }
611
612                 pthread_attr_destroy (&ptattr);
613         }
614
615         while (1) {
616                 int remote = 0;
617
618                 conn_t *connection;
619
620                 pthread_mutex_lock (&available_mutex);
621
622                 while (0 == available_collectors) {
623                         pthread_cond_wait (&collector_available, &available_mutex);
624                 }
625
626                 --available_collectors;
627
628                 pthread_mutex_unlock (&available_mutex);
629
630                 do {
631                         errno = 0;
632                         if (-1 == (remote = accept (connector_socket, NULL, NULL))) {
633                                 if (EINTR != errno) {
634                                         disabled = 1;
635                                         syslog (LOG_ERR, "accept() failed: %s", strerror (errno));
636                                         pthread_exit ((void *)1);
637                                 }
638                         }
639                 } while (EINTR == errno);
640
641                 connection = (conn_t *)smalloc (sizeof (conn_t));
642
643                 connection->socket = remote;
644                 connection->next   = NULL;
645
646                 pthread_mutex_lock (&conns_mutex);
647
648                 if (NULL == conns.head) {
649                         conns.head = connection;
650                         conns.tail = connection;
651                 }
652                 else {
653                         conns.tail->next = connection;
654                         conns.tail = conns.tail->next;
655                 }
656
657                 pthread_mutex_unlock (&conns_mutex);
658
659                 pthread_cond_signal (&conn_available);
660         }
661         pthread_exit ((void *)0);
662 } /* void *open_connection (void *) */
663 #endif /* EMAIL_HAVE_READ */
664
665 static void email_init (void)
666 {
667 #if EMAIL_HAVE_READ
668         int err = 0;
669
670         if (0 != (err = pthread_create (&connector, NULL,
671                                 open_connection, NULL))) {
672                 disabled = 1;
673                 syslog (LOG_ERR, "pthread_create() failed: %s", strerror (err));
674                 return;
675         }
676 #endif /* EMAIL_HAVE_READ */
677         return;
678 } /* static void email_init (void) */
679
680 #if EMAIL_HAVE_READ
681 static void email_shutdown (void)
682 {
683         int i = 0;
684
685         if (disabled)
686                 return;
687
688         pthread_kill (connector, SIGTERM);
689         close (connector_socket);
690
691         /* don't allow any more connections to be processed */
692         pthread_mutex_lock (&conns_mutex);
693
694         for (i = 0; i < max_conns; ++i) {
695                 pthread_kill (collectors[i]->thread, SIGTERM);
696                 close (collectors[i]->socket);
697         }
698
699         pthread_mutex_unlock (&conns_mutex);
700
701         unlink (SOCK_PATH);
702         return;
703 } /* static void email_shutdown (void) */
704 #endif /* EMAIL_HAVE_READ */
705
706 static void count_write (char *host, char *inst, char *val)
707 {
708         char file[BUFSIZE] = "";
709         int  len           = 0;
710
711         len = snprintf (file, BUFSIZE, COUNT_FILE, inst);
712         if ((len < 0) || (len >= BUFSIZE))
713                 return;
714
715         rrd_update_file (host, file, val, count_ds_def, count_ds_num);
716         return;
717 } /* static void email_write (char *host, char *inst, char *val) */
718
719 static void size_write (char *host, char *inst, char *val)
720 {
721         char file[BUFSIZE] = "";
722         int  len           = 0;
723
724         len = snprintf (file, BUFSIZE, SIZE_FILE, inst);
725         if ((len < 0) || (len >= BUFSIZE))
726                 return;
727
728         rrd_update_file (host, file, val, size_ds_def, size_ds_num);
729         return;
730 } /* static void size_write (char *host, char *inst, char *val) */
731
732 static void score_write (char *host, char *inst, char *val)
733 {
734         rrd_update_file (host, SCORE_FILE, val, score_ds_def, score_ds_num);
735         return;
736 } /* static void score_write (char *host, char *inst, char *val) */
737
738 static void check_write (char *host, char *inst, char *val)
739 {
740         char file[BUFSIZE] = "";
741         int  len           = 0;
742
743         len = snprintf (file, BUFSIZE, CHECK_FILE, inst);
744         if ((len < 0) || (len >= BUFSIZE))
745                 return;
746
747         rrd_update_file (host, file, val, check_ds_def, check_ds_num);
748         return;
749 } /* static void check_write (char *host, char *inst, char *val) */
750
751 #if EMAIL_HAVE_READ
752 static void type_submit (char *plugin, char *inst, int value)
753 {
754         char buf[BUFSIZE] = "";
755         int  len          = 0;
756
757         if (0 == value)
758                 return;
759
760         len = snprintf (buf, BUFSIZE, "%u:%i", (unsigned int)curtime, value);
761         if ((len < 0) || (len >= BUFSIZE))
762                 return;
763
764         plugin_submit (plugin, inst, buf);
765         return;
766 } /* static void type_submit (char *, char *, int) */
767
768 static void score_submit (double value)
769 {
770         char buf[BUFSIZE] = "";
771         int  len          = 0;
772
773         if (0.0 == value)
774                 return;
775
776         len = snprintf (buf, BUFSIZE, "%u:%.2f", (unsigned int)curtime, value);
777         if ((len < 0) || (len >= BUFSIZE))
778                 return;
779
780         plugin_submit ("email_spam_score", NULL, buf);
781         return;
782 }
783
784 static void email_read (void)
785 {
786         type_t *ptr;
787
788         if (disabled)
789                 return;
790
791         pthread_mutex_lock (&count_mutex);
792
793         for (ptr = count.head; NULL != ptr; ptr = ptr->next) {
794                 type_submit ("email_count", ptr->name, ptr->value);
795                 ptr->value = 0;
796         }
797
798         pthread_mutex_unlock (&count_mutex);
799
800         pthread_mutex_lock (&size_mutex);
801
802         for (ptr = size.head; NULL != ptr; ptr = ptr->next) {
803                 type_submit ("email_size", ptr->name, ptr->value);
804                 ptr->value = 0;
805         }
806
807         pthread_mutex_unlock (&size_mutex);
808
809         pthread_mutex_lock (&score_mutex);
810
811         score_submit (score);
812         score = 0.0;
813         score_count = 0;
814
815         pthread_mutex_unlock (&score_mutex);
816
817         pthread_mutex_lock (&check_mutex);
818
819         for (ptr = check.head; NULL != ptr; ptr = ptr->next) {
820                 type_submit ("email_spam_check", ptr->name, ptr->value);
821                 ptr->value = 0;
822         }
823
824         pthread_mutex_unlock (&check_mutex);
825         return;
826 } /* static void read (void) */
827 #else /* if !EMAIL_HAVE_READ */
828 # define email_read NULL
829 #endif
830
831 void module_register (void)
832 {
833         plugin_register (MODULE_NAME, email_init, email_read, NULL);
834         plugin_register ("email_count", NULL, NULL, count_write);
835         plugin_register ("email_size", NULL, NULL, size_write);
836         plugin_register ("email_spam_score", NULL, NULL, score_write);
837         plugin_register ("email_spam_check", NULL, NULL, check_write);
838 #if EMAIL_HAVE_READ
839         plugin_register_shutdown (MODULE_NAME, email_shutdown);
840         cf_register (MODULE_NAME, email_config, config_keys, config_keys_num);
841 #endif /* EMAIL_HAVE_READ */
842         return;
843 } /* void module_register (void) */
844
845 /* vim: set sw=4 ts=4 tw=78 noexpandtab : */
846