email plugin: Only kill threads/close sockets that exist/are opened.
[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 = (pthread_t) 0;
180 static int connector_socket = -1;
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                 free (connection);
523
524                 this->socket = -1;
525
526                 pthread_mutex_lock (&available_mutex);
527                 ++available_collectors;
528                 pthread_mutex_unlock (&available_mutex);
529
530                 pthread_cond_signal (&collector_available);
531         } /* while (1) */
532
533         free (buffer);
534         pthread_exit ((void *)0);
535 } /* static void *collect (void *) */
536
537 static void *open_connection (void *arg)
538 {
539         struct sockaddr_un addr;
540
541         /* create UNIX socket */
542         errno = 0;
543         if (-1 == (connector_socket = socket (PF_UNIX, SOCK_STREAM, 0))) {
544                 char errbuf[1024];
545                 disabled = 1;
546                 log_err ("socket() failed: %s",
547                                 sstrerror (errno, errbuf, sizeof (errbuf)));
548                 pthread_exit ((void *)1);
549         }
550
551         addr.sun_family = AF_UNIX;
552
553         strncpy (addr.sun_path, SOCK_PATH, (size_t)(UNIX_PATH_MAX - 1));
554         addr.sun_path[UNIX_PATH_MAX - 1] = '\0';
555         unlink (addr.sun_path);
556
557         errno = 0;
558         if (-1 == bind (connector_socket, (struct sockaddr *)&addr,
559                                 offsetof (struct sockaddr_un, sun_path)
560                                         + strlen(addr.sun_path))) {
561                 char errbuf[1024];
562                 disabled = 1;
563                 connector_socket = -1; /* TODO: close? */
564                 log_err ("bind() failed: %s",
565                                 sstrerror (errno, errbuf, sizeof (errbuf)));
566                 pthread_exit ((void *)1);
567         }
568
569         errno = 0;
570         if (-1 == listen (connector_socket, 5)) {
571                 char errbuf[1024];
572                 disabled = 1;
573                 connector_socket = -1; /* TODO: close? */
574                 log_err ("listen() failed: %s",
575                                 sstrerror (errno, errbuf, sizeof (errbuf)));
576                 pthread_exit ((void *)1);
577         }
578
579         if ((uid_t) 0 == geteuid ())
580         {
581                 struct group sg;
582                 struct group *grp;
583                 char grbuf[2048];
584                 int status;
585
586                 grp = NULL;
587                 status = getgrnam_r (sock_group, &sg, grbuf, sizeof (grbuf), &grp);
588                 if (status != 0)
589                 {
590                         char errbuf[1024];
591                         log_warn ("getgrnam_r (%s) failed: %s", sock_group,
592                                         sstrerror (errno, errbuf, sizeof (errbuf)));
593                 }
594                 else if (grp == NULL)
595                 {
596                         log_warn ("No such group: `%s'", sock_group);
597                 }
598                 else
599                 {
600                         status = chown (SOCK_PATH, (uid_t) -1, grp->gr_gid);
601                         if (status != 0)
602                         {
603                                 char errbuf[1024];
604                                 log_warn ("chown (%s, -1, %i) failed: %s",
605                                                 SOCK_PATH, (int) grp->gr_gid,
606                                                 sstrerror (errno, errbuf, sizeof (errbuf)));
607                         }
608                 }
609         }
610         else /* geteuid != 0 */
611         {
612                 log_warn ("not running as root");
613         }
614
615         errno = 0;
616         if (0 != chmod (SOCK_PATH, sock_perms)) {
617                 char errbuf[1024];
618                 log_warn ("chmod() failed: %s",
619                                 sstrerror (errno, errbuf, sizeof (errbuf)));
620         }
621
622         { /* initialize collector threads */
623                 int i   = 0;
624                 int err = 0;
625
626                 pthread_attr_t ptattr;
627
628                 conns.head = NULL;
629                 conns.tail = NULL;
630
631                 pthread_attr_init (&ptattr);
632                 pthread_attr_setdetachstate (&ptattr, PTHREAD_CREATE_DETACHED);
633
634                 available_collectors = max_conns;
635
636                 collectors =
637                         (collector_t **)smalloc (max_conns * sizeof (collector_t *));
638
639                 for (i = 0; i < max_conns; ++i) {
640                         collectors[i] = (collector_t *)smalloc (sizeof (collector_t));
641                         collectors[i]->socket = -1;
642
643                         if (0 != (err = pthread_create (&collectors[i]->thread, &ptattr,
644                                                         collect, collectors[i]))) {
645                                 char errbuf[1024];
646                                 log_err ("pthread_create() failed: %s",
647                                                 sstrerror (errno, errbuf, sizeof (errbuf)));
648                                 collectors[i]->thread = (pthread_t) 0;
649                         }
650                 }
651
652                 pthread_attr_destroy (&ptattr);
653         }
654
655         while (1) {
656                 int remote = 0;
657
658                 conn_t *connection;
659
660                 pthread_mutex_lock (&available_mutex);
661
662                 while (0 == available_collectors) {
663                         pthread_cond_wait (&collector_available, &available_mutex);
664                 }
665
666                 --available_collectors;
667
668                 pthread_mutex_unlock (&available_mutex);
669
670                 do {
671                         errno = 0;
672                         if (-1 == (remote = accept (connector_socket, NULL, NULL))) {
673                                 if (EINTR != errno) {
674                                         char errbuf[1024];
675                                         disabled = 1;
676                                         connector_socket = -1; /* TODO: close? */
677                                         log_err ("accept() failed: %s",
678                                                         sstrerror (errno, errbuf, sizeof (errbuf)));
679                                         pthread_exit ((void *)1);
680                                 }
681                         }
682                 } while (EINTR == errno);
683
684                 connection = (conn_t *)smalloc (sizeof (conn_t));
685
686                 connection->socket = remote;
687                 connection->next   = NULL;
688
689                 pthread_mutex_lock (&conns_mutex);
690
691                 if (NULL == conns.head) {
692                         conns.head = connection;
693                         conns.tail = connection;
694                 }
695                 else {
696                         conns.tail->next = connection;
697                         conns.tail = conns.tail->next;
698                 }
699
700                 pthread_mutex_unlock (&conns_mutex);
701
702                 pthread_cond_signal (&conn_available);
703         }
704         pthread_exit ((void *)0);
705 } /* static void *open_connection (void *) */
706
707 static int email_init (void)
708 {
709         int err = 0;
710
711         if (0 != (err = pthread_create (&connector, NULL,
712                                 open_connection, NULL))) {
713                 char errbuf[1024];
714                 disabled = 1;
715                 log_err ("pthread_create() failed: %s",
716                                 sstrerror (errno, errbuf, sizeof (errbuf)));
717                 return (-1);
718         }
719
720         return (0);
721 } /* int email_init */
722
723 static int email_shutdown (void)
724 {
725         int i = 0;
726
727         if (disabled)
728                 return (0);
729
730         if (connector != ((pthread_t) 0)) {
731                 pthread_kill (connector, SIGTERM);
732                 connector = (pthread_t) 0;
733         }
734
735         if (connector_socket >= 0) {
736                 close (connector_socket);
737                 connector_socket = -1;
738         }
739
740         /* don't allow any more connections to be processed */
741         pthread_mutex_lock (&conns_mutex);
742
743         for (i = 0; i < max_conns; ++i) {
744                 if (collectors[i]->thread != ((pthread_t) 0)) {
745                         pthread_kill (collectors[i]->thread, SIGTERM);
746                         collectors[i]->thread = (pthread_t) 0;
747                 }
748                 
749                 if (collectors[i]->socket >= 0) {
750                         close (collectors[i]->socket);
751                         collectors[i]->socket = -1;
752                 }
753         }
754
755         pthread_mutex_unlock (&conns_mutex);
756
757         unlink (SOCK_PATH);
758
759         return (0);
760 } /* static void email_shutdown (void) */
761
762 static void email_submit (const char *type, const char *type_instance, gauge_t value)
763 {
764         value_t values[1];
765         value_list_t vl = VALUE_LIST_INIT;
766
767         values[0].gauge = value;
768
769         vl.values = values;
770         vl.values_len = 1;
771         vl.time = time (NULL);
772         strcpy (vl.host, hostname_g);
773         strcpy (vl.plugin, "email");
774         strncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
775
776         plugin_dispatch_values (type, &vl);
777 } /* void email_submit */
778
779 /* Copy list l1 to list l2. l2 may partly exist already, but it is assumed
780  * that neither the order nor the name of any element of either list is
781  * changed and no elements are deleted. The values of l1 are reset to zero
782  * after they have been copied to l2. */
783 static void copy_type_list (type_list_t *l1, type_list_t *l2)
784 {
785         type_t *ptr1;
786         type_t *ptr2;
787
788         type_t *last = NULL;
789
790         for (ptr1 = l1->head, ptr2 = l2->head; NULL != ptr1;
791                         ptr1 = ptr1->next, last = ptr2, ptr2 = ptr2->next) {
792                 if (NULL == ptr2) {
793                         ptr2 = (type_t *)smalloc (sizeof (type_t));
794                         ptr2->name = NULL;
795                         ptr2->next = NULL;
796
797                         if (NULL == last) {
798                                 l2->head = ptr2;
799                         }
800                         else {
801                                 last->next = ptr2;
802                         }
803
804                         l2->tail = ptr2;
805                 }
806
807                 if (NULL == ptr2->name) {
808                         ptr2->name = sstrdup (ptr1->name);
809                 }
810
811                 ptr2->value = ptr1->value;
812                 ptr1->value = 0;
813         }
814         return;
815 }
816
817 static int email_read (void)
818 {
819         type_t *ptr;
820
821         double sc;
822
823         static type_list_t *cnt;
824         static type_list_t *sz;
825         static type_list_t *chk;
826
827         if (disabled)
828                 return (-1);
829
830         if (NULL == cnt) {
831                 cnt = (type_list_t *)smalloc (sizeof (type_list_t));
832                 cnt->head = NULL;
833         }
834
835         if (NULL == sz) {
836                 sz = (type_list_t *)smalloc (sizeof (type_list_t));
837                 sz->head = NULL;
838         }
839
840         if (NULL == chk) {
841                 chk = (type_list_t *)smalloc (sizeof (type_list_t));
842                 chk->head = NULL;
843         }
844
845         /* email count */
846         pthread_mutex_lock (&count_mutex);
847
848         copy_type_list (&count, cnt);
849
850         pthread_mutex_unlock (&count_mutex);
851
852         for (ptr = cnt->head; NULL != ptr; ptr = ptr->next) {
853                 email_submit ("email_count", ptr->name, ptr->value);
854         }
855
856         /* email size */
857         pthread_mutex_lock (&size_mutex);
858
859         copy_type_list (&size, sz);
860
861         pthread_mutex_unlock (&size_mutex);
862
863         for (ptr = sz->head; NULL != ptr; ptr = ptr->next) {
864                 email_submit ("email_size", ptr->name, ptr->value);
865         }
866
867         /* spam score */
868         pthread_mutex_lock (&score_mutex);
869
870         sc = score;
871         score = 0.0;
872         score_count = 0;
873
874         pthread_mutex_unlock (&score_mutex);
875
876         email_submit ("spam_score", "", sc);
877
878         /* spam checks */
879         pthread_mutex_lock (&check_mutex);
880
881         copy_type_list (&check, chk);
882
883         pthread_mutex_unlock (&check_mutex);
884
885         for (ptr = chk->head; NULL != ptr; ptr = ptr->next)
886                 email_submit ("spam_check", ptr->name, ptr->value);
887
888         return (0);
889 } /* int email_read */
890
891 void module_register (modreg_e load)
892 {
893         if (load & MR_DATASETS)
894         {
895                 plugin_register_data_set (&email_count_ds);
896                 plugin_register_data_set (&email_size_ds);
897                 plugin_register_data_set (&spam_check_ds);
898                 plugin_register_data_set (&spam_score_ds);
899         }
900
901         if (load & MR_READ)
902         {
903                 plugin_register_config ("email", email_config, config_keys, config_keys_num);
904                 plugin_register_init ("email", email_init);
905                 plugin_register_read ("email", email_read);
906         }
907         plugin_register_shutdown ("email", email_shutdown);
908 } /* void module_register */
909
910 /* vim: set sw=4 ts=4 tw=78 noexpandtab : */