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