2 * collectd - src/email.c
3 * Copyright (C) 2006-2008 Sebastian Harl
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
24 * Sebastian Harl <sh at tokkee.org>
28 * This plugin communicates with a spam filter, a virus scanner or similar
29 * software using a UNIX socket and a very simple protocol:
31 * e-mail type (e.g. ham, spam, virus, ...) and size
37 * successful spam checks
38 * c:<type1>[,<type2>,...]
48 #include <sys/select.h>
51 /* some systems (e.g. Darwin) seem to not define UNIX_PATH_MAX at all */
53 #define UNIX_PATH_MAX sizeof(((struct sockaddr_un *)0)->sun_path)
54 #endif /* UNIX_PATH_MAX */
58 #endif /* HAVE_GRP_H */
60 #define SOCK_PATH LOCALSTATEDIR "/run/" PACKAGE_NAME "-email"
62 #define MAX_CONNS_LIMIT 16384
64 #define log_debug(...) DEBUG("email: "__VA_ARGS__)
65 #define log_err(...) ERROR("email: "__VA_ARGS__)
66 #define log_warn(...) WARNING("email: "__VA_ARGS__)
69 * Private data structures
71 /* linked list of email and check types */
83 /* collector thread control information */
84 typedef struct collector {
87 /* socket descriptor of the current/last connection */
91 /* linked list of pending connections */
93 /* socket to read data from */
96 /* linked list of connections */
108 /* valid configuration file keys */
109 static const char *config_keys[] = {"SocketFile", "SocketGroup", "SocketPerms",
111 static int config_keys_num = STATIC_ARRAY_SIZE(config_keys);
113 /* socket configuration */
114 static char *sock_file;
115 static char *sock_group;
116 static int sock_perms = S_IRWXU | S_IRWXG;
117 static int max_conns = MAX_CONNS;
119 /* state of the plugin */
122 /* thread managing "client" connections */
123 static pthread_t connector = (pthread_t)0;
124 static int connector_socket = -1;
126 /* tell the collector threads that a new connection is available */
127 static pthread_cond_t conn_available = PTHREAD_COND_INITIALIZER;
129 /* connections that are waiting to be processed */
130 static pthread_mutex_t conns_mutex = PTHREAD_MUTEX_INITIALIZER;
131 static conn_list_t conns;
133 /* tell the connector thread that a collector is available */
134 static pthread_cond_t collector_available = PTHREAD_COND_INITIALIZER;
136 /* collector threads */
137 static collector_t **collectors;
139 static pthread_mutex_t available_mutex = PTHREAD_MUTEX_INITIALIZER;
140 static int available_collectors;
142 static pthread_mutex_t count_mutex = PTHREAD_MUTEX_INITIALIZER;
143 static type_list_t list_count;
144 static type_list_t list_count_copy;
146 static pthread_mutex_t size_mutex = PTHREAD_MUTEX_INITIALIZER;
147 static type_list_t list_size;
148 static type_list_t list_size_copy;
150 static pthread_mutex_t score_mutex = PTHREAD_MUTEX_INITIALIZER;
152 static int score_count;
154 static pthread_mutex_t check_mutex = PTHREAD_MUTEX_INITIALIZER;
155 static type_list_t list_check;
156 static type_list_t list_check_copy;
161 static int email_config(const char *key, const char *value) {
162 if (strcasecmp(key, "SocketFile") == 0) {
163 if (sock_file != NULL)
165 sock_file = sstrdup(value);
166 } else if (strcasecmp(key, "SocketGroup") == 0) {
167 if (sock_group != NULL)
169 sock_group = sstrdup(value);
170 } else if (strcasecmp(key, "SocketPerms") == 0) {
171 /* the user is responsible for providing reasonable values */
172 sock_perms = (int)strtol(value, NULL, 8);
173 } else if (strcasecmp(key, "MaxConns") == 0) {
174 long int tmp = strtol(value, NULL, 0);
177 fprintf(stderr, "email plugin: `MaxConns' was set to invalid "
178 "value %li, will use default %i.\n",
180 ERROR("email plugin: `MaxConns' was set to invalid "
181 "value %li, will use default %i.\n",
183 max_conns = MAX_CONNS;
184 } else if (tmp > MAX_CONNS_LIMIT) {
185 fprintf(stderr, "email plugin: `MaxConns' was set to invalid "
186 "value %li, will use hardcoded limit %i.\n",
187 tmp, MAX_CONNS_LIMIT);
188 ERROR("email plugin: `MaxConns' was set to invalid "
189 "value %li, will use hardcoded limit %i.\n",
190 tmp, MAX_CONNS_LIMIT);
191 max_conns = MAX_CONNS_LIMIT;
193 max_conns = (int)tmp;
199 } /* static int email_config (char *, char *) */
201 /* Increment the value of the given name in the given list by incr. */
202 static void type_list_incr(type_list_t *list, char *name, int incr) {
203 if (list->head == NULL) {
204 list->head = smalloc(sizeof(*list->head));
206 list->head->name = sstrdup(name);
207 list->head->value = incr;
208 list->head->next = NULL;
210 list->tail = list->head;
214 for (ptr = list->head; NULL != ptr; ptr = ptr->next) {
215 if (strcmp(name, ptr->name) == 0)
220 list->tail->next = smalloc(sizeof(*list->tail->next));
221 list->tail = list->tail->next;
223 list->tail->name = sstrdup(name);
224 list->tail->value = incr;
225 list->tail->next = NULL;
231 } /* static void type_list_incr (type_list_t *, char *) */
233 static void *collect(void *arg) {
234 collector_t *this = (collector_t *)arg;
239 pthread_mutex_lock(&conns_mutex);
241 while (conns.head == NULL) {
242 pthread_cond_wait(&conn_available, &conns_mutex);
245 connection = conns.head;
246 conns.head = conns.head->next;
248 if (conns.head == NULL) {
252 pthread_mutex_unlock(&conns_mutex);
254 /* make the socket available to the global
255 * thread and connection management */
256 this->socket = connection->socket;
258 log_debug("collect: handling connection on fd #%i", fileno(this->socket));
261 /* 256 bytes ought to be enough for anybody ;-) */
262 char line[256 + 1]; /* line + '\0' */
266 if (fgets(line, sizeof(line), this->socket) == NULL) {
268 log_err("collect: reading from socket (fd #%i) "
270 fileno(this->socket), STRERRNO);
276 if ((line[len - 1] != '\n') && (line[len - 1] != '\r')) {
277 log_warn("collect: line too long (> %" PRIsz " characters): "
279 sizeof(line) - 1, line);
281 while (fgets(line, sizeof(line), this->socket) != NULL)
282 if ((line[len - 1] == '\n') || (line[len - 1] == '\r'))
286 if (len < 3) { /* [a-z] ':' '\n' */
292 log_debug("collect: line = '%s'", line);
294 if (line[1] != ':') {
295 log_err("collect: syntax error in line '%s'", line);
299 if (line[0] == 'e') { /* e:<type>:<bytes> */
300 char *type = line + 2;
301 char *bytes_str = strchr(type, ':');
302 if (bytes_str == NULL) {
303 log_err("collect: syntax error in line '%s'", line);
310 pthread_mutex_lock(&count_mutex);
311 type_list_incr(&list_count, type, /* increment = */ 1);
312 pthread_mutex_unlock(&count_mutex);
314 int bytes = atoi(bytes_str);
316 pthread_mutex_lock(&size_mutex);
317 type_list_incr(&list_size, type, /* increment = */ bytes);
318 pthread_mutex_unlock(&size_mutex);
320 } else if (line[0] == 's') { /* s:<value> */
321 pthread_mutex_lock(&score_mutex);
322 score = (score * (double)score_count + atof(line + 2)) /
323 (double)(score_count + 1);
325 pthread_mutex_unlock(&score_mutex);
326 } else if (line[0] == 'c') { /* c:<type1>[,<type2>,...] */
327 char *dummy = line + 2;
331 pthread_mutex_lock(&check_mutex);
332 while ((type = strtok_r(dummy, ",", &endptr)) != NULL) {
334 type_list_incr(&list_check, type, /* increment = */ 1);
336 pthread_mutex_unlock(&check_mutex);
338 log_err("collect: unknown type '%c'", line[0]);
342 log_debug("Shutting down connection on fd #%i", fileno(this->socket));
344 fclose(connection->socket);
349 pthread_mutex_lock(&available_mutex);
350 ++available_collectors;
351 pthread_mutex_unlock(&available_mutex);
353 pthread_cond_signal(&collector_available);
356 pthread_exit((void *)0);
358 } /* static void *collect (void *) */
360 static void *open_connection(void __attribute__((unused)) * arg) {
361 const char *path = (NULL == sock_file) ? SOCK_PATH : sock_file;
362 const char *group = (NULL == sock_group) ? COLLECTD_GRP_NAME : sock_group;
364 /* create UNIX socket */
366 if ((connector_socket = socket(PF_UNIX, SOCK_STREAM, 0)) == -1) {
368 log_err("socket() failed: %s", STRERRNO);
369 pthread_exit((void *)1);
372 struct sockaddr_un addr = {
373 .sun_family = AF_UNIX,
375 sstrncpy(addr.sun_path, path, (size_t)(UNIX_PATH_MAX - 1));
378 if (bind(connector_socket, (struct sockaddr *)&addr,
379 offsetof(struct sockaddr_un, sun_path) + strlen(addr.sun_path)) ==
382 close(connector_socket);
383 connector_socket = -1;
384 log_err("bind() failed: %s", STRERRNO);
385 pthread_exit((void *)1);
389 if (listen(connector_socket, 5) == -1) {
391 close(connector_socket);
392 connector_socket = -1;
393 log_err("listen() failed: %s", STRERRNO);
394 pthread_exit((void *)1);
402 long int grbuf_size = sysconf(_SC_GETGR_R_SIZE_MAX);
404 grbuf_size = sysconf(_SC_PAGESIZE);
407 char grbuf[grbuf_size];
410 status = getgrnam_r(group, &sg, grbuf, sizeof(grbuf), &grp);
412 log_warn("getgrnam_r (%s) failed: %s", group, STRERROR(status));
413 } else if (grp == NULL) {
414 log_warn("No such group: `%s'", group);
416 status = chown(path, (uid_t)-1, grp->gr_gid);
418 log_warn("chown (%s, -1, %i) failed: %s", path, (int)grp->gr_gid,
425 if (chmod(path, sock_perms) != 0) {
426 log_warn("chmod() failed: %s", STRERRNO);
429 { /* initialize collector threads */
430 pthread_attr_t ptattr;
435 pthread_attr_init(&ptattr);
436 pthread_attr_setdetachstate(&ptattr, PTHREAD_CREATE_DETACHED);
438 available_collectors = max_conns;
440 collectors = smalloc(max_conns * sizeof(*collectors));
442 for (int i = 0; i < max_conns; ++i) {
443 collectors[i] = smalloc(sizeof(*collectors[i]));
444 collectors[i]->socket = NULL;
446 if (plugin_thread_create(&collectors[i]->thread, &ptattr, collect,
447 collectors[i], "email collector") != 0) {
448 log_err("plugin_thread_create() failed: %s", STRERRNO);
449 collectors[i]->thread = (pthread_t)0;
453 pthread_attr_destroy(&ptattr);
461 pthread_mutex_lock(&available_mutex);
463 while (available_collectors == 0) {
464 pthread_cond_wait(&collector_available, &available_mutex);
467 --available_collectors;
469 pthread_mutex_unlock(&available_mutex);
474 remote = accept(connector_socket, NULL, NULL);
480 close(connector_socket);
481 connector_socket = -1;
482 log_err("accept() failed: %s", STRERRNO);
483 pthread_exit((void *)1);
486 /* access() succeeded. */
490 connection = calloc(1, sizeof(*connection));
491 if (connection == NULL) {
496 connection->socket = fdopen(remote, "r");
497 connection->next = NULL;
499 if (connection->socket == NULL) {
505 pthread_mutex_lock(&conns_mutex);
507 if (conns.head == NULL) {
508 conns.head = connection;
509 conns.tail = connection;
511 conns.tail->next = connection;
512 conns.tail = conns.tail->next;
515 pthread_mutex_unlock(&conns_mutex);
517 pthread_cond_signal(&conn_available);
520 pthread_exit((void *)0);
522 } /* static void *open_connection (void *) */
524 static int email_init(void) {
525 if (plugin_thread_create(&connector, NULL, open_connection, NULL,
526 "email listener") != 0) {
528 log_err("plugin_thread_create() failed: %s", STRERRNO);
533 } /* int email_init */
535 static void type_list_free(type_list_t *t) {
539 while (this != NULL) {
540 type_t *next = this->next;
552 static int email_shutdown(void) {
553 if (connector != ((pthread_t)0)) {
554 pthread_kill(connector, SIGTERM);
555 connector = (pthread_t)0;
558 if (connector_socket >= 0) {
559 close(connector_socket);
560 connector_socket = -1;
563 /* don't allow any more connections to be processed */
564 pthread_mutex_lock(&conns_mutex);
566 available_collectors = 0;
568 if (collectors != NULL) {
569 for (int i = 0; i < max_conns; ++i) {
570 if (collectors[i] == NULL)
573 if (collectors[i]->thread != ((pthread_t)0)) {
574 pthread_kill(collectors[i]->thread, SIGTERM);
575 collectors[i]->thread = (pthread_t)0;
578 if (collectors[i]->socket != NULL) {
579 fclose(collectors[i]->socket);
580 collectors[i]->socket = NULL;
583 sfree(collectors[i]);
586 } /* if (collectors != NULL) */
588 pthread_mutex_unlock(&conns_mutex);
590 type_list_free(&list_count);
591 type_list_free(&list_count_copy);
592 type_list_free(&list_size);
593 type_list_free(&list_size_copy);
594 type_list_free(&list_check);
595 type_list_free(&list_check_copy);
597 unlink((sock_file == NULL) ? SOCK_PATH : sock_file);
602 } /* static void email_shutdown (void) */
604 static void email_submit(const char *type, const char *type_instance,
606 value_list_t vl = VALUE_LIST_INIT;
608 vl.values = &(value_t){.gauge = value};
610 sstrncpy(vl.plugin, "email", sizeof(vl.plugin));
611 sstrncpy(vl.type, type, sizeof(vl.type));
612 sstrncpy(vl.type_instance, type_instance, sizeof(vl.type_instance));
614 plugin_dispatch_values(&vl);
615 } /* void email_submit */
617 /* Copy list l1 to list l2. l2 may partly exist already, but it is assumed
618 * that neither the order nor the name of any element of either list is
619 * changed and no elements are deleted. The values of l1 are reset to zero
620 * after they have been copied to l2. */
621 static void copy_type_list(type_list_t *l1, type_list_t *l2) {
624 for (type_t *ptr1 = l1->head, *ptr2 = l2->head; ptr1 != NULL;
625 ptr1 = ptr1->next, last = ptr2, ptr2 = ptr2->next) {
627 ptr2 = smalloc(sizeof(*ptr2));
640 if (ptr2->name == NULL) {
641 ptr2->name = sstrdup(ptr1->name);
644 ptr2->value = ptr1->value;
650 static int email_read(void) {
658 pthread_mutex_lock(&count_mutex);
660 copy_type_list(&list_count, &list_count_copy);
662 pthread_mutex_unlock(&count_mutex);
664 for (type_t *ptr = list_count_copy.head; ptr != NULL; ptr = ptr->next) {
665 email_submit("email_count", ptr->name, ptr->value);
669 pthread_mutex_lock(&size_mutex);
671 copy_type_list(&list_size, &list_size_copy);
673 pthread_mutex_unlock(&size_mutex);
675 for (type_t *ptr = list_size_copy.head; ptr != NULL; ptr = ptr->next) {
676 email_submit("email_size", ptr->name, ptr->value);
680 pthread_mutex_lock(&score_mutex);
683 score_count_old = score_count;
687 pthread_mutex_unlock(&score_mutex);
689 if (score_count_old > 0)
690 email_submit("spam_score", "", score_old);
693 pthread_mutex_lock(&check_mutex);
695 copy_type_list(&list_check, &list_check_copy);
697 pthread_mutex_unlock(&check_mutex);
699 for (type_t *ptr = list_check_copy.head; ptr != NULL; ptr = ptr->next)
700 email_submit("spam_check", ptr->name, ptr->value);
703 } /* int email_read */
705 void module_register(void) {
706 plugin_register_config("email", email_config, config_keys, config_keys_num);
707 plugin_register_init("email", email_init);
708 plugin_register_read("email", email_read);
709 plugin_register_shutdown("email", email_shutdown);
710 } /* void module_register */