email plugin: Prevent a segfault in read_line ().
[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 /* linked list of collector thread control information */
104 typedef struct collector {
105         pthread_t thread;
106
107         /* socket to read data from */
108         int socket;
109
110         /* buffer to read data to */
111         char buffer[BUFSIZE];
112         int  idx; /* current position in buffer */
113
114         struct collector *next;
115 } collector_t;
116
117 typedef struct {
118         collector_t *head;
119         collector_t *tail;
120 } collector_list_t;
121 #endif /* EMAIL_HAVE_READ */
122
123 /*
124  * Private variables
125  */
126 #if EMAIL_HAVE_READ
127 /* valid configuration file keys */
128 static char *config_keys[] =
129 {
130         "SocketGroup",
131         "SocketPerms",
132         "MaxConns",
133         NULL
134 };
135 static int config_keys_num = 3;
136
137 /* socket configuration */
138 static char *sock_group = COLLECTD_GRP_NAME;
139 static int  sock_perms  = S_IRWXU | S_IRWXG;
140 static int  max_conns   = MAX_CONNS;
141
142 /* state of the plugin */
143 static int disabled = 0;
144
145 /* thread managing "client" connections */
146 static pthread_t connector;
147 static int connector_socket;
148
149 /* tell the connector thread that a collector is available */
150 static pthread_cond_t collector_available = PTHREAD_COND_INITIALIZER;
151
152 /* collector threads that are in use */
153 static pthread_mutex_t active_mutex = PTHREAD_MUTEX_INITIALIZER;
154 static collector_list_t active;
155
156 /* collector threads that are available for use */
157 static pthread_mutex_t available_mutex = PTHREAD_MUTEX_INITIALIZER;
158 static collector_list_t available;
159
160 static pthread_mutex_t count_mutex = PTHREAD_MUTEX_INITIALIZER;
161 static type_list_t count;
162
163 static pthread_mutex_t size_mutex = PTHREAD_MUTEX_INITIALIZER;
164 static type_list_t size;
165
166 static pthread_mutex_t score_mutex = PTHREAD_MUTEX_INITIALIZER;
167 static double score;
168 static int score_count;
169
170 static pthread_mutex_t check_mutex = PTHREAD_MUTEX_INITIALIZER;
171 static type_list_t check;
172 #endif /* EMAIL_HAVE_READ */
173
174 #define COUNT_FILE "email/email-%s.rrd"
175 static char *count_ds_def[] =
176 {
177         "DS:count:GAUGE:"COLLECTD_HEARTBEAT":0:U",
178         NULL
179 };
180 static int count_ds_num = 1;
181
182 #define SIZE_FILE  "email/email_size-%s.rrd"
183 static char *size_ds_def[] =
184 {
185         "DS:size:GAUGE:"COLLECTD_HEARTBEAT":0:U",
186         NULL
187 };
188 static int size_ds_num = 1;
189
190 #define SCORE_FILE "email/spam_score.rrd"
191 static char *score_ds_def[] =
192 {
193         "DS:score:GAUGE:"COLLECTD_HEARTBEAT":U:U",
194         NULL
195 };
196 static int score_ds_num = 1;
197
198 #define CHECK_FILE "email/spam_check-%s.rrd"
199 static char *check_ds_def[] =
200 {
201         "DS:hits:GAUGE:"COLLECTD_HEARTBEAT":0:U",
202         NULL
203 };
204 static int check_ds_num = 1;
205
206 #if EMAIL_HAVE_READ
207 static int email_config (char *key, char *value)
208 {
209         if (0 == strcasecmp (key, "SocketGroup")) {
210                 sock_group = sstrdup (value);
211         }
212         else if (0 == strcasecmp (key, "SocketPerms")) {
213                 /* the user is responsible for providing reasonable values */
214                 sock_perms = (int)strtol (value, NULL, 8);
215         }
216         else if (0 == strcasecmp (key, "MaxConns")) {
217                 long int tmp = strtol (value, NULL, 0);
218
219                 if (tmp < 1) {
220                         fprintf (stderr, "email plugin: `MaxConns' was set to invalid "
221                                         "value %li, will use default %i.\n",
222                                         tmp, MAX_CONNS);
223                         max_conns = MAX_CONNS;
224                 }
225                 else if (tmp > MAX_CONNS_LIMIT) {
226                         fprintf (stderr, "email plugin: `MaxConns' was set to invalid "
227                                         "value %li, will use hardcoded limit %i.\n",
228                                         tmp, MAX_CONNS_LIMIT);
229                         max_conns = MAX_CONNS_LIMIT;
230                 }
231                 else {
232                         max_conns = (int)tmp;
233                 }
234         }
235         else {
236                 return -1;
237         }
238         return 0;
239 } /* static int email_config (char *, char *) */
240
241 /* Increment the value of the given name in the given list by incr. */
242 static void type_list_incr (type_list_t *list, char *name, int incr)
243 {
244         if (NULL == list->head) {
245                 list->head = (type_t *)smalloc (sizeof (type_t));
246
247                 list->head->name  = sstrdup (name);
248                 list->head->value = incr;
249                 list->head->next  = NULL;
250
251                 list->tail = list->head;
252         }
253         else {
254                 type_t *ptr;
255
256                 for (ptr = list->head; NULL != ptr; ptr = ptr->next) {
257                         if (0 == strcmp (name, ptr->name))
258                                 break;
259                 }
260
261                 if (NULL == ptr) {
262                         list->tail->next = (type_t *)smalloc (sizeof (type_t));
263                         list->tail = list->tail->next;
264
265                         list->tail->name  = sstrdup (name);
266                         list->tail->value = incr;
267                         list->tail->next  = NULL;
268                 }
269                 else {
270                         ptr->value += incr;
271                 }
272         }
273         return;
274 } /* static void type_list_incr (type_list_t *, char *) */
275
276 /* Read a single character from the socket. If an error occurs or end-of-file
277  * is reached return '\0'. */
278 char read_char (collector_t *src)
279 {
280         char ret = '\0';
281
282         fd_set fdset;
283
284         FD_ZERO (&fdset);
285         FD_SET (src->socket, &fdset);
286
287         if (-1 == select (src->socket + 1, &fdset, NULL, NULL, NULL)) {
288                 syslog (LOG_ERR, "select() failed: %s", strerror (errno));
289                 return '\0';
290         }
291
292         assert (FD_ISSET (src->socket, &fdset));
293
294         do {
295                 ssize_t len = 0;
296
297                 errno = 0;
298                 if (0 > (len = read (src->socket, (void *)&ret, 1))) {
299                         if (EINTR != errno) {
300                                 syslog (LOG_ERR, "read() failed: %s", strerror (errno));
301                                 return '\0';
302                         }
303                 }
304
305                 if (0 == len)
306                         return '\0';
307         } while (EINTR == errno);
308         return ret;
309 } /* char read_char (collector_t *) */
310
311 /* Read a single line (terminated by '\n') from the the socket.
312  *
313  * The return value is zero terminated and does not contain any newline
314  * characters. In case that no complete line is available (non-blocking mode
315  * should be enabled) an empty string is returned.
316  *
317  * If an error occurs or end-of-file is reached return NULL.
318  *
319  * IMPORTANT NOTE: If there is no newline character found in BUFSIZE
320  * characters of the input stream, the line will will be ignored! By
321  * definition we should not get any longer input lines, thus this is
322  * acceptable in this case ;-) */
323 char *read_line (collector_t *src)
324 {
325         int  i = 0;
326         char *ret;
327
328         assert (BUFSIZE > src->idx);
329
330         for (i = 0; i < src->idx; ++i) {
331                 if ('\n' == src->buffer[i])
332                         break;
333         }
334
335         if (i == src->idx) {
336                 fd_set fdset;
337         
338                 ssize_t len = 0;
339
340                 FD_ZERO (&fdset);
341                 FD_SET (src->socket, &fdset);
342
343                 if (-1 == select (src->socket + 1, &fdset, NULL, NULL, NULL)) {
344                         syslog (LOG_ERR, "select() failed: %s", strerror (errno));
345                         return NULL;
346                 }
347
348                 assert (FD_ISSET (src->socket, &fdset));
349
350                 do {
351                         errno = 0;
352                         if (0 > (len = read (src->socket,
353                                                         (void *)(&(src->buffer[0]) + src->idx),
354                                                         BUFSIZE - src->idx))) {
355                                 if (EINTR != errno) {
356                                         syslog (LOG_ERR, "read() failed: %s", strerror (errno));
357                                         return NULL;
358                                 }
359                         }
360
361                         if (0 == len)
362                                 return NULL;
363                 } while (EINTR == errno);
364
365                 src->idx += len;
366
367                 for (i = src->idx - len; i < src->idx; ++i) {
368                         if ('\n' == src->buffer[i])
369                                 break;
370                 }
371
372                 if (i == src->idx) {
373                         ret = (char *)smalloc (1);
374
375                         ret[0] = '\0';
376
377                         if (BUFSIZE == src->idx) { /* no space left in buffer */
378                                 while ('\n' != read_char (src))
379                                         /* ignore complete line */;
380
381                                 src->idx = 0;
382                         }
383                         return ret;
384                 }
385         }
386
387         ret = (char *)smalloc (i + 1);
388         memcpy (ret, &(src->buffer[0]), i + 1);
389         ret[i] = '\0';
390
391         src->idx -= (i + 1);
392
393         if (0 == src->idx)
394                 src->buffer[0] = '\0';
395         else
396                 memmove (&(src->buffer[0]), &(src->buffer[i + 1]), src->idx);
397         return ret;
398 } /* char *read_line (collector_t *) */
399
400 static void *collect (void *arg)
401 {
402         collector_t *this = (collector_t *)arg;
403         
404         int loop = 1;
405
406         { /* put the socket in non-blocking mode */
407                 int flags = 0;
408
409                 errno = 0;
410                 if (-1 == fcntl (this->socket, F_GETFL, &flags)) {
411                         syslog (LOG_ERR, "fcntl() failed: %s", strerror (errno));
412                         loop = 0;
413                 }
414
415                 errno = 0;
416                 if (-1 == fcntl (this->socket, F_SETFL, flags | O_NONBLOCK)) {
417                         syslog (LOG_ERR, "fcntl() failed: %s", strerror (errno));
418                         loop = 0;
419                 }
420         }
421
422         while (loop) {
423                 char *line = read_line (this);
424
425                 if (NULL == line) {
426                         loop = 0;
427                         break;
428                 }
429
430                 if ('\0' == line[0]) {
431                         free (line);
432                         continue;
433                 }
434
435                 if (':' != line[1]) {
436                         syslog (LOG_ERR, "email: syntax error in line '%s'", line);
437                         free (line);
438                         continue;
439                 }
440
441                 if ('e' == line[0]) { /* e:<type>:<bytes> */
442                         char *ptr  = NULL;
443                         char *type = strtok_r (line + 2, ":", &ptr);
444                         char *tmp  = strtok_r (NULL, ":", &ptr);
445                         int  bytes = 0;
446
447                         if (NULL == tmp) {
448                                 syslog (LOG_ERR, "email: syntax error in line '%s'", line);
449                                 free (line);
450                                 continue;
451                         }
452
453                         bytes = atoi (tmp);
454
455                         pthread_mutex_lock (&count_mutex);
456                         type_list_incr (&count, type, 1);
457                         pthread_mutex_unlock (&count_mutex);
458
459                         pthread_mutex_lock (&size_mutex);
460                         type_list_incr (&size, type, bytes);
461                         pthread_mutex_unlock (&size_mutex);
462                 }
463                 else if ('s' == line[0]) { /* s:<value> */
464                         pthread_mutex_lock (&score_mutex);
465                         score = (score * (double)score_count + atof (line + 2))
466                                         / (double)(score_count + 1);
467                         ++score_count;
468                         pthread_mutex_unlock (&score_mutex);
469                 }
470                 else if ('c' == line[0]) { /* c:<type1>[,<type2>,...] */
471                         char *ptr  = NULL;
472                         char *type = strtok_r (line + 2, ",", &ptr);
473
474                         do {
475                                 pthread_mutex_lock (&check_mutex);
476                                 type_list_incr (&check, type, 1);
477                                 pthread_mutex_unlock (&check_mutex);
478                         } while (NULL != (type = strtok_r (NULL, ",", &ptr)));
479                 }
480                 else {
481                         syslog (LOG_ERR, "email: unknown type '%c'", line[0]);
482                 }
483
484                 free (line);
485         }
486
487         /* put this thread back into the available list */
488         pthread_mutex_lock (&active_mutex);
489         {
490                 collector_t *last;
491                 collector_t *ptr;
492
493                 last = NULL;
494
495                 for (ptr = active.head; NULL != ptr; last = ptr, ptr = ptr->next) {
496                         if (0 != pthread_equal (ptr->thread, this->thread))
497                                 break;
498                 }
499
500                 /* the current thread _has_ to be in the active list */
501                 assert (NULL != ptr);
502
503                 if (NULL == last) {
504                         active.head = ptr->next;
505                 }
506                 else {
507                         last->next = ptr->next;
508
509                         if (NULL == last->next) {
510                                 active.tail = last;
511                         }
512                 }
513         }
514         pthread_mutex_unlock (&active_mutex);
515
516         this->next = NULL;
517
518         pthread_mutex_lock (&available_mutex);
519
520         if (NULL == available.head) {
521                 available.head = this;
522                 available.tail = this;
523         }
524         else {
525                 available.tail->next = this;
526                 available.tail = this;
527         }
528
529         pthread_mutex_unlock (&available_mutex);
530
531         pthread_cond_signal (&collector_available);
532         pthread_exit ((void *)0);
533 } /* void *collect (void *) */
534
535 static void *open_connection (void *arg)
536 {
537         struct sockaddr_un addr;
538
539         /* create UNIX socket */
540         errno = 0;
541         if (-1 == (connector_socket = socket (PF_UNIX, SOCK_STREAM, 0))) {
542                 disabled = 1;
543                 syslog (LOG_ERR, "socket() failed: %s", strerror (errno));
544                 pthread_exit ((void *)1);
545         }
546
547         addr.sun_family = AF_UNIX;
548
549         strncpy (addr.sun_path, SOCK_PATH, (size_t)(UNIX_PATH_MAX - 1));
550         addr.sun_path[UNIX_PATH_MAX - 1] = '\0';
551         unlink (addr.sun_path);
552
553         errno = 0;
554         if (-1 == bind (connector_socket, (struct sockaddr *)&addr,
555                                 offsetof (struct sockaddr_un, sun_path)
556                                         + strlen(addr.sun_path))) {
557                 disabled = 1;
558                 syslog (LOG_ERR, "bind() failed: %s", strerror (errno));
559                 pthread_exit ((void *)1);
560         }
561
562         errno = 0;
563         if (-1 == listen (connector_socket, 5)) {
564                 disabled = 1;
565                 syslog (LOG_ERR, "listen() failed: %s", strerror (errno));
566                 pthread_exit ((void *)1);
567         }
568
569         if ((uid_t)0 == geteuid ()) {
570                 struct group *grp;
571
572                 errno = 0;
573                 if (NULL != (grp = getgrnam (sock_group))) {
574                         errno = 0;
575                         if (0 != chown (SOCK_PATH, (uid_t)-1, grp->gr_gid)) {
576                                 syslog (LOG_WARNING, "chown() failed: %s", strerror (errno));
577                         }
578                 }
579                 else {
580                         syslog (LOG_WARNING, "getgrnam() failed: %s", strerror (errno));
581                 }
582         }
583         else {
584                 syslog (LOG_WARNING, "not running as root");
585         }
586
587         errno = 0;
588         if (0 != chmod (SOCK_PATH, sock_perms)) {
589                 syslog (LOG_WARNING, "chmod() failed: %s", strerror (errno));
590         }
591
592         { /* initialize queue of available threads */
593                 int i = 0;
594
595                 collector_t *last;
596
597                 active.head = NULL;
598                 active.tail = NULL;
599
600                 available.head = (collector_t *)smalloc (sizeof (collector_t));
601                 available.tail = available.head;
602                 available.tail->next = NULL;
603
604                 last = available.head;
605
606                 for (i = 1; i < max_conns; ++i) {
607                         last->next = (collector_t *)smalloc (sizeof (collector_t));
608                         last = last->next;
609                         available.tail = last;
610                         available.tail->next = NULL;
611                 }
612         }
613
614         while (1) {
615                 int remote = 0;
616                 int err    = 0;
617
618                 collector_t *collector;
619
620                 pthread_attr_t ptattr;
621
622                 pthread_mutex_lock (&available_mutex);
623                 while (NULL == available.head) {
624                         pthread_cond_wait (&collector_available, &available_mutex);
625                 }
626                 pthread_mutex_unlock (&available_mutex);
627
628                 do {
629                         errno = 0;
630                         if (-1 == (remote = accept (connector_socket, NULL, NULL))) {
631                                 if (EINTR != errno) {
632                                         disabled = 1;
633                                         syslog (LOG_ERR, "accept() failed: %s", strerror (errno));
634                                         pthread_exit ((void *)1);
635                                 }
636                         }
637                 } while (EINTR == errno);
638
639                 /* assign connection to next available thread */
640                 pthread_mutex_lock (&available_mutex);
641
642                 collector = available.head;
643                 collector->socket = remote;
644
645                 if (available.head == available.tail) {
646                         available.head = NULL;
647                         available.tail = NULL;
648                 }
649                 else {
650                         available.head = available.head->next;
651                 }
652
653                 pthread_mutex_unlock (&available_mutex);
654
655                 collector->idx  = 0;
656                 collector->next = NULL;
657
658                 pthread_attr_init (&ptattr);
659                 pthread_attr_setdetachstate (&ptattr, PTHREAD_CREATE_DETACHED);
660
661                 if (0 == (err = pthread_create (&collector->thread, &ptattr, collect,
662                                 (void *)collector))) {
663                         pthread_mutex_lock (&active_mutex);
664
665                         if (NULL == active.head) {
666                                 active.head = collector;
667                                 active.tail = collector;
668                         }
669                         else {
670                                 active.tail->next = collector;
671                                 active.tail = collector;
672                         }
673
674                         pthread_mutex_unlock (&active_mutex);
675                 }
676                 else {
677                         pthread_mutex_lock (&available_mutex);
678
679                         if (NULL == available.head) {
680                                 available.head = collector;
681                                 available.tail = collector;
682                         }
683                         else {
684                                 available.tail->next = collector;
685                                 available.tail = collector;
686                         }
687
688                         pthread_mutex_unlock (&available_mutex);
689
690                         close (remote);
691                         syslog (LOG_ERR, "pthread_create() failed: %s", strerror (err));
692                 }
693
694                 pthread_attr_destroy (&ptattr);
695         }
696         pthread_exit ((void *)0);
697 } /* void *open_connection (void *) */
698 #endif /* EMAIL_HAVE_READ */
699
700 static void email_init (void)
701 {
702 #if EMAIL_HAVE_READ
703         int err = 0;
704
705         if (0 != (err = pthread_create (&connector, NULL,
706                                 open_connection, NULL))) {
707                 disabled = 1;
708                 syslog (LOG_ERR, "pthread_create() failed: %s", strerror (err));
709                 return;
710         }
711 #endif /* EMAIL_HAVE_READ */
712         return;
713 } /* static void email_init (void) */
714
715 #if EMAIL_HAVE_READ
716 static void email_shutdown (void)
717 {
718         collector_t *ptr;
719
720         if (disabled)
721                 return;
722
723         close (connector_socket);
724         pthread_kill (connector, SIGTERM);
725
726         pthread_mutex_lock (&active_mutex);
727
728         for (ptr = active.head; NULL != ptr; ptr = ptr->next) {
729                 close (ptr->socket);
730                 pthread_kill (ptr->thread, SIGTERM);
731         }
732
733         pthread_mutex_unlock (&active_mutex);
734
735         unlink (SOCK_PATH);
736         return;
737 } /* static void email_shutdown (void) */
738 #endif /* EMAIL_HAVE_READ */
739
740 static void count_write (char *host, char *inst, char *val)
741 {
742         char file[BUFSIZE] = "";
743         int  len           = 0;
744
745         len = snprintf (file, BUFSIZE, COUNT_FILE, inst);
746         if ((len < 0) || (len >= BUFSIZE))
747                 return;
748
749         rrd_update_file (host, file, val, count_ds_def, count_ds_num);
750         return;
751 } /* static void email_write (char *host, char *inst, char *val) */
752
753 static void size_write (char *host, char *inst, char *val)
754 {
755         char file[BUFSIZE] = "";
756         int  len           = 0;
757
758         len = snprintf (file, BUFSIZE, SIZE_FILE, inst);
759         if ((len < 0) || (len >= BUFSIZE))
760                 return;
761
762         rrd_update_file (host, file, val, size_ds_def, size_ds_num);
763         return;
764 } /* static void size_write (char *host, char *inst, char *val) */
765
766 static void score_write (char *host, char *inst, char *val)
767 {
768         rrd_update_file (host, SCORE_FILE, val, score_ds_def, score_ds_num);
769         return;
770 } /* static void score_write (char *host, char *inst, char *val) */
771
772 static void check_write (char *host, char *inst, char *val)
773 {
774         char file[BUFSIZE] = "";
775         int  len           = 0;
776
777         len = snprintf (file, BUFSIZE, CHECK_FILE, inst);
778         if ((len < 0) || (len >= BUFSIZE))
779                 return;
780
781         rrd_update_file (host, file, val, check_ds_def, check_ds_num);
782         return;
783 } /* static void check_write (char *host, char *inst, char *val) */
784
785 #if EMAIL_HAVE_READ
786 static void type_submit (char *plugin, char *inst, int value)
787 {
788         char buf[BUFSIZE] = "";
789         int  len          = 0;
790
791         if (0 == value)
792                 return;
793
794         len = snprintf (buf, BUFSIZE, "%u:%i", (unsigned int)curtime, value);
795         if ((len < 0) || (len >= BUFSIZE))
796                 return;
797
798         plugin_submit (plugin, inst, buf);
799         return;
800 } /* static void type_submit (char *, char *, int) */
801
802 static void score_submit (double value)
803 {
804         char buf[BUFSIZE] = "";
805         int  len          = 0;
806
807         if (0.0 == value)
808                 return;
809
810         len = snprintf (buf, BUFSIZE, "%u:%.2f", (unsigned int)curtime, value);
811         if ((len < 0) || (len >= BUFSIZE))
812                 return;
813
814         plugin_submit ("email_spam_score", NULL, buf);
815         return;
816 }
817
818 static void email_read (void)
819 {
820         type_t *ptr;
821
822         if (disabled)
823                 return;
824
825         pthread_mutex_lock (&count_mutex);
826
827         for (ptr = count.head; NULL != ptr; ptr = ptr->next) {
828                 type_submit ("email_count", ptr->name, ptr->value);
829                 ptr->value = 0;
830         }
831
832         pthread_mutex_unlock (&count_mutex);
833
834         pthread_mutex_lock (&size_mutex);
835
836         for (ptr = size.head; NULL != ptr; ptr = ptr->next) {
837                 type_submit ("email_size", ptr->name, ptr->value);
838                 ptr->value = 0;
839         }
840
841         pthread_mutex_unlock (&size_mutex);
842
843         pthread_mutex_lock (&score_mutex);
844
845         score_submit (score);
846         score = 0.0;
847         score_count = 0;
848
849         pthread_mutex_unlock (&score_mutex);
850
851         pthread_mutex_lock (&check_mutex);
852
853         for (ptr = check.head; NULL != ptr; ptr = ptr->next) {
854                 type_submit ("email_spam_check", ptr->name, ptr->value);
855                 ptr->value = 0;
856         }
857
858         pthread_mutex_unlock (&check_mutex);
859         return;
860 } /* static void read (void) */
861 #else /* if !EMAIL_HAVE_READ */
862 # define email_read NULL
863 #endif
864
865 void module_register (void)
866 {
867         plugin_register (MODULE_NAME, email_init, email_read, NULL);
868         plugin_register ("email_count", NULL, NULL, count_write);
869         plugin_register ("email_size", NULL, NULL, size_write);
870         plugin_register ("email_spam_score", NULL, NULL, score_write);
871         plugin_register ("email_spam_check", NULL, NULL, check_write);
872 #if EMAIL_HAVE_READ
873         plugin_register_shutdown (MODULE_NAME, email_shutdown);
874         cf_register (MODULE_NAME, email_config, config_keys, config_keys_num);
875 #endif /* EMAIL_HAVE_READ */
876         return;
877 } /* void module_register (void) */
878
879 /* vim: set sw=4 ts=4 tw=78 noexpandtab : */
880