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