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