Merge branch 'pull/collectd-4.0' into collectd-4.0
[collectd.git] / src / email.c
index c8c3982..594ef96 100644 (file)
@@ -4,8 +4,7 @@
  *
  * This program is free software; you can redistribute it and/or modify it
  * under the terms of the GNU General Public License as published by the
- * Free Software Foundation; either version 2 of the License, or (at your
- * option) any later version.
+ * Free Software Foundation; only version 2 of the License is applicable.
  *
  * This program is distributed in the hope that it will be useful, but
  * WITHOUT ANY WARRANTY; without even the implied warranty of
 # include <pthread.h>
 #endif
 
-#if HAVE_SYS_SELECT_H
-#      include <sys/select.h>
-#endif /* HAVE_SYS_SELECT_H */
-
-#if HAVE_SYS_SOCKET_H
-#      include <sys/socket.h>
-#endif /* HAVE_SYS_SOCKET_H */
-
-/* *sigh* glibc does not define UNIX_PATH_MAX in sys/un.h ... */
-#if HAVE_LINUX_UN_H
-#      include <linux/un.h>
-#elif HAVE_SYS_UN_H
-#      include <sys/un.h>
-#endif /* HAVE_LINUX_UN_H | HAVE_SYS_UN_H */
+#include <sys/socket.h>
+#include <sys/un.h>
+#include <sys/select.h>
 
 /* some systems (e.g. Darwin) seem to not define UNIX_PATH_MAX at all */
 #ifndef UNIX_PATH_MAX
@@ -137,36 +125,6 @@ static const char *config_keys[] =
 };
 static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
 
-static data_source_t gauge_dsrc[1] =
-{
-       {"value", DS_TYPE_GAUGE, 0.0, NAN}
-};
-
-static data_set_t email_count_ds =
-{
-       "email_count", 1, gauge_dsrc
-};
-
-static data_set_t email_size_ds =
-{
-       "email_size", 1, gauge_dsrc
-};
-
-static data_set_t spam_check_ds =
-{
-       "spam_check", 1, gauge_dsrc
-};
-
-static data_source_t spam_score_dsrc[1] =
-{
-       {"score", DS_TYPE_GAUGE, NAN, NAN}
-};
-
-static data_set_t spam_score_ds =
-{
-       "spam_score", 1, spam_score_dsrc
-};
-
 /* socket configuration */
 static char *sock_group = COLLECTD_GRP_NAME;
 static int  sock_perms  = S_IRWXU | S_IRWXG;
@@ -176,8 +134,8 @@ static int  max_conns   = MAX_CONNS;
 static int disabled = 0;
 
 /* thread managing "client" connections */
-static pthread_t connector;
-static int connector_socket;
+static pthread_t connector = (pthread_t) 0;
+static int connector_socket = -1;
 
 /* tell the collector threads that a new connection is available */
 static pthread_cond_t conn_available = PTHREAD_COND_INITIALIZER;
@@ -190,7 +148,7 @@ static conn_list_t conns;
 static pthread_cond_t collector_available = PTHREAD_COND_INITIALIZER;
 
 /* collector threads */
-static collector_t **collectors;
+static collector_t **collectors = NULL;
 
 static pthread_mutex_t available_mutex = PTHREAD_MUTEX_INITIALIZER;
 static int available_collectors;
@@ -519,9 +477,10 @@ static void *collect (void *arg)
                } /* while (loop) */
 
                close (connection->socket);
-
                free (connection);
 
+               this->socket = -1;
+
                pthread_mutex_lock (&available_mutex);
                ++available_collectors;
                pthread_mutex_unlock (&available_mutex);
@@ -559,6 +518,7 @@ static void *open_connection (void *arg)
                                        + strlen(addr.sun_path))) {
                char errbuf[1024];
                disabled = 1;
+               connector_socket = -1; /* TODO: close? */
                log_err ("bind() failed: %s",
                                sstrerror (errno, errbuf, sizeof (errbuf)));
                pthread_exit ((void *)1);
@@ -568,6 +528,7 @@ static void *open_connection (void *arg)
        if (-1 == listen (connector_socket, 5)) {
                char errbuf[1024];
                disabled = 1;
+               connector_socket = -1; /* TODO: close? */
                log_err ("listen() failed: %s",
                                sstrerror (errno, errbuf, sizeof (errbuf)));
                pthread_exit ((void *)1);
@@ -635,13 +596,14 @@ static void *open_connection (void *arg)
 
                for (i = 0; i < max_conns; ++i) {
                        collectors[i] = (collector_t *)smalloc (sizeof (collector_t));
-                       collectors[i]->socket = 0;
+                       collectors[i]->socket = -1;
 
                        if (0 != (err = pthread_create (&collectors[i]->thread, &ptattr,
                                                        collect, collectors[i]))) {
                                char errbuf[1024];
                                log_err ("pthread_create() failed: %s",
                                                sstrerror (errno, errbuf, sizeof (errbuf)));
+                               collectors[i]->thread = (pthread_t) 0;
                        }
                }
 
@@ -669,6 +631,7 @@ static void *open_connection (void *arg)
                                if (EINTR != errno) {
                                        char errbuf[1024];
                                        disabled = 1;
+                                       connector_socket = -1; /* TODO: close? */
                                        log_err ("accept() failed: %s",
                                                        sstrerror (errno, errbuf, sizeof (errbuf)));
                                        pthread_exit ((void *)1);
@@ -719,23 +682,40 @@ static int email_shutdown (void)
 {
        int i = 0;
 
-       if (disabled)
-               return (0);
+       if (connector != ((pthread_t) 0)) {
+               pthread_kill (connector, SIGTERM);
+               connector = (pthread_t) 0;
+       }
 
-       pthread_kill (connector, SIGTERM);
-       close (connector_socket);
+       if (connector_socket >= 0) {
+               close (connector_socket);
+               connector_socket = -1;
+       }
 
        /* don't allow any more connections to be processed */
        pthread_mutex_lock (&conns_mutex);
 
-       for (i = 0; i < max_conns; ++i) {
-               pthread_kill (collectors[i]->thread, SIGTERM);
-               close (collectors[i]->socket);
-       }
+       if (collectors != NULL) {
+               for (i = 0; i < max_conns; ++i) {
+                       if (collectors[i] == NULL)
+                               continue;
+
+                       if (collectors[i]->thread != ((pthread_t) 0)) {
+                               pthread_kill (collectors[i]->thread, SIGTERM);
+                               collectors[i]->thread = (pthread_t) 0;
+                       }
+
+                       if (collectors[i]->socket >= 0) {
+                               close (collectors[i]->socket);
+                               collectors[i]->socket = -1;
+                       }
+               }
+       } /* if (collectors != NULL) */
 
        pthread_mutex_unlock (&conns_mutex);
 
        unlink (SOCK_PATH);
+       errno = 0;
 
        return (0);
 } /* static void email_shutdown (void) */
@@ -799,7 +779,8 @@ static int email_read (void)
 {
        type_t *ptr;
 
-       double sc;
+       double score_old;
+       int score_count_old;
 
        static type_list_t *cnt;
        static type_list_t *sz;
@@ -848,13 +829,15 @@ static int email_read (void)
        /* spam score */
        pthread_mutex_lock (&score_mutex);
 
-       sc = score;
+       score_old = score;
+       score_count_old = score_count;
        score = 0.0;
        score_count = 0;
 
        pthread_mutex_unlock (&score_mutex);
 
-       email_submit ("spam_score", "", sc);
+       if (score_count_old > 0)
+               email_submit ("spam_score", "", score_old);
 
        /* spam checks */
        pthread_mutex_lock (&check_mutex);
@@ -871,16 +854,10 @@ static int email_read (void)
 
 void module_register (void)
 {
-       plugin_register_data_set (&email_count_ds);
-       plugin_register_data_set (&email_size_ds);
-       plugin_register_data_set (&spam_check_ds);
-       plugin_register_data_set (&spam_score_ds);
-
        plugin_register_config ("email", email_config, config_keys, config_keys_num);
        plugin_register_init ("email", email_init);
        plugin_register_read ("email", email_read);
        plugin_register_shutdown ("email", email_shutdown);
-} /* void module_register (void) */
+} /* void module_register */
 
 /* vim: set sw=4 ts=4 tw=78 noexpandtab : */
-