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