[collectd] email plugin: Make it possible to build the plugin write-only.

Florian Forster octo<span style="display: none;">.trailing-username</span>(a)<span style="display: none;">leading-domain.</span>verplant.org
Sun Dec 3 10:44:57 CET 2006


From: Florian Forster <octo<span style="display: none;">.trailing-username</span>(a)<span style="display: none;">leading-domain.</span>leeloo.lan.home.verplant.org>

So far the plugin simply imported `pthread.h'. If this headerfile (or the
library for that case) was not present, building the plugin would fail. This
patch makes `libpthread' a soft dependency, i. e. it's only necessary if you
want to read from the socket. Without `libpthread' being available the plugin
can still be used in server processes.

Signed-off-by: Florian Forster <octo<span style="display: none;">.trailing-username</span>(a)<span style="display: none;">leading-domain.</span>leeloo.lan.home.verplant.org>
---
 src/Makefile.am |    5 ++++-
 src/email.c     |   51 +++++++++++++++++++++++++++++++++++++--------------
 2 files changed, 41 insertions(+), 15 deletions(-)

diff --git a/src/Makefile.am b/src/Makefile.am
index 9e9b466..fcbcb16 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -173,7 +173,10 @@ endif
 if BUILD_MODULE_EMAIL
 pkglib_LTLIBRARIES += email.la
 email_la_SOURCES = email.c
-email_la_LDFLAGS = -module -avoid-version -lpthread
+email_la_LDFLAGS = -module -avoid-version
+if BUILD_WITH_LIBPTHREAD
+email_la_LDFLAGS += -lpthread
+endif
 collectd_LDADD += "-dlopen" email.la
 collectd_DEPENDENCIES += email.la
 endif
diff --git a/src/email.c b/src/email.c
index 37194fb..8d2c891 100644
--- a/src/email.c
+++ b/src/email.c
@@ -38,7 +38,12 @@
 #include "common.h"
 #include "plugin.h"
 
-#include <pthread.h>
+#if HAVE_LIBPTHREAD
+# include <pthread.h>
+# define EMAIL_HAVE_READ 1
+#else
+# define EMAIL_HAVE_READ 0
+#endif
 
 #if HAVE_SYS_SELECT_H
 #	include <sys/select.h>
@@ -67,6 +72,10 @@
 #define SOCK_PATH "/tmp/.collectd-email"
 #define MAX_CONNS 5
 
+/*
+ * Private data structures
+ */
+#if EMAIL_HAVE_READ
 /* linked list of email and check types */
 typedef struct type {
 	char        *name;
@@ -97,7 +106,12 @@ typedef struct {
 	collector_t *head;
 	collector_t *tail;
 } collector_list_t;
+#endif /* EMAIL_HAVE_READ */
 
+/*
+ * Private variables
+ */
+#if EMAIL_HAVE_READ
 /* state of the plugin */
 static int disabled = 0;
 
@@ -115,6 +129,20 @@ static collector_list_t active;
 static pthread_mutex_t available_mutex = PTHREAD_MUTEX_INITIALIZER;
 static collector_list_t available;
 
+static pthread_mutex_t count_mutex = PTHREAD_MUTEX_INITIALIZER;
+static type_list_t count;
+
+static pthread_mutex_t size_mutex = PTHREAD_MUTEX_INITIALIZER;
+static type_list_t size;
+
+static pthread_mutex_t score_mutex = PTHREAD_MUTEX_INITIALIZER;
+static double score;
+static int score_count;
+
+static pthread_mutex_t check_mutex = PTHREAD_MUTEX_INITIALIZER;
+static type_list_t check;
+#endif /* EMAIL_HAVE_READ */
+
 #define COUNT_FILE "email/email-%s.rrd"
 static char *count_ds_def[] =
 {
@@ -123,9 +151,6 @@ static char *count_ds_def[] =
 };
 static int count_ds_num = 1;
 
-static pthread_mutex_t count_mutex = PTHREAD_MUTEX_INITIALIZER;
-static type_list_t count;
-
 #define SIZE_FILE  "email/email_size-%s.rrd"
 static char *size_ds_def[] =
 {
@@ -134,9 +159,6 @@ static char *size_ds_def[] =
 };
 static int size_ds_num = 1;
 
-static pthread_mutex_t size_mutex = PTHREAD_MUTEX_INITIALIZER;
-static type_list_t size;
-
 #define SCORE_FILE "email/spam_score.rrd"
 static char *score_ds_def[] =
 {
@@ -145,10 +167,6 @@ static char *score_ds_def[] =
 };
 static int score_ds_num = 1;
 
-static pthread_mutex_t score_mutex = PTHREAD_MUTEX_INITIALIZER;
-static double score;
-static int score_count;
-
 #define CHECK_FILE "email/spam_check-%s.rrd"
 static char *check_ds_def[] =
 {
@@ -157,9 +175,7 @@ static char *check_ds_def[] =
 };
 static int check_ds_num = 1;
 
-static pthread_mutex_t check_mutex = PTHREAD_MUTEX_INITIALIZER;
-static type_list_t check;
-
+#if EMAIL_HAVE_READ
 /* Increment the value of the given name in the given list by incr. */
 static void type_list_incr (type_list_t *list, char *name, int incr)
 {
@@ -619,9 +635,11 @@ static void *open_connection (void *arg)
 	}
 	pthread_exit ((void *)0);
 } /* void *open_connection (void *) */
+#endif /* EMAIL_HAVE_READ */
 
 static void email_init (void)
 {
+#if EMAIL_HAVE_READ
 	int err = 0;
 
 	if (0 != (err = pthread_create (&connector, NULL,
@@ -630,6 +648,7 @@ static void email_init (void)
 		syslog (LOG_ERR, "pthread_create() failed: %s", strerror (err));
 		return;
 	}
+#endif /* EMAIL_HAVE_READ */
 	return;
 } /* static void email_init (void) */
 
@@ -678,6 +697,7 @@ static void check_write (char *host, cha
 	return;
 } /* static void check_write (char *host, char *inst, char *val) */
 
+#if EMAIL_HAVE_READ
 static void type_submit (char *plugin, char *inst, int value)
 {
 	char buf[BUFSIZE] = "";
@@ -753,6 +773,9 @@ static void email_read (void)
 	pthread_mutex_unlock (&check_mutex);
 	return;
 } /* static void read (void) */
+#else /* if !EMAIL_HAVE_READ */
+# define email_read NULL
+#endif
 
 void module_register (void)
 {
-- 
1.4.3.5




More information about the collectd mailing list