/**
* collectd - src/exec.c
- * Copyright (C) 2007,2008 Florian octo Forster
+ * Copyright (C) 2007-2009 Florian octo Forster
+ * Copyright (C) 2007-2009 Sebastian Harl
+ * Copyright (C) 2008 Peter Holik
*
* 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
*
* Authors:
* Florian octo Forster <octo at verplant.org>
+ * Sebastian Harl <sh at tokkee.org>
+ * Peter Holik <peter at holik.at>
**/
+#define _BSD_SOURCE /* For setgroups */
+
#include "collectd.h"
#include "common.h"
#include "plugin.h"
/*
* Functions
*/
-static void sigchld_handler (int signal) /* {{{ */
+static void sigchld_handler (int __attribute__((unused)) signal) /* {{{ */
{
pid_t pid;
int status;
{
char *tmp = strrchr (ci->values[1].value.string, '/');
if (tmp == NULL)
- strncpy (buffer, ci->values[1].value.string, sizeof (buffer));
+ sstrncpy (buffer, ci->values[1].value.string, sizeof (buffer));
else
- strncpy (buffer, tmp + 1, sizeof (buffer));
- buffer[sizeof (buffer) - 1] = '\0';
+ sstrncpy (buffer, tmp + 1, sizeof (buffer));
}
pl->argv[0] = strdup (buffer);
if (pl->argv[0] == NULL)
{
if (ci->values[i + 1].type == OCONFIG_TYPE_NUMBER)
{
- snprintf (buffer, sizeof (buffer), "%lf",
+ ssnprintf (buffer, sizeof (buffer), "%lf",
ci->values[i + 1].value.number);
}
else
{
if (ci->values[i + 1].value.boolean)
- strncpy (buffer, "true", sizeof (buffer));
+ sstrncpy (buffer, "true", sizeof (buffer));
else
- strncpy (buffer, "false", sizeof (buffer));
+ sstrncpy (buffer, "false", sizeof (buffer));
}
- buffer[sizeof (buffer) - 1] = '\0';
pl->argv[i] = strdup (buffer);
}
return (0);
} /* int exec_config }}} */
+static void set_environment (void) /* {{{ */
+{
+ char buffer[1024];
+
+ ssnprintf (buffer, sizeof (buffer), "%i", interval_g);
+ setenv ("COLLECTD_INTERVAL", buffer, /* overwrite = */ 1);
+
+ ssnprintf (buffer, sizeof (buffer), "%s", hostname_g);
+ setenv ("COLLECTD_HOSTNAME", buffer, /* overwrite = */ 1);
+} /* }}} void set_environment */
+
static void exec_child (program_list_t *pl) /* {{{ */
{
int status;
exit (-1);
} /* void exec_child }}} */
+static void reset_signal_mask (void) /* {{{ */
+{
+ sigset_t ss;
+
+ memset (&ss, 0, sizeof (ss));
+ sigemptyset (&ss);
+ sigprocmask (SIG_SETMASK, &ss, /* old mask = */ NULL);
+} /* }}} void reset_signal_mask */
+
/*
* Creates three pipes (one for reading, one for writing and one for errors),
* forks a child, sets up the pipes so that fd_in is connected to STDIN of
close (fd_pipe_err[1]);
}
+ set_environment ();
+
+ /* Unblock all signals */
+ reset_signal_mask ();
+
exec_child (pl);
/* does not return */
}
static int parse_line (char *buffer) /* {{{ */
{
- char *fields[256];
- int fields_num;
-
- fields[0] = "PUTVAL";
- fields_num = strsplit (buffer, fields + 1, STATIC_ARRAY_SIZE(fields) - 1);
-
- if (strcasecmp (fields[1], "putval") == 0)
- return (handle_putval (stdout, fields + 1, fields_num));
- else if (strcasecmp (fields[1], "putnotif") == 0)
- return (handle_putnotif (stdout, fields + 1, fields_num));
-
- /* compatibility code */
- return (handle_putval (stdout, fields, fields_num + 1));
+ if (strncasecmp ("PUTVAL", buffer, strlen ("PUTVAL")) == 0)
+ return (handle_putval (stdout, buffer));
+ else if (strncasecmp ("PUTNOTIF", buffer, strlen ("PUTNOTIF")) == 0)
+ return (handle_putnotif (stdout, buffer));
+ else
+ {
+ /* For backwards compatibility */
+ char tmp[1220];
+ /* Let's annoy the user a bit.. */
+ INFO ("exec plugin: Prepending `PUTVAL' to this line: %s", buffer);
+ ssnprintf (tmp, sizeof (tmp), "PUTVAL %s", buffer);
+ return (handle_putval (stdout, tmp));
+ }
} /* int parse_line }}} */
static void *exec_read_one (void *arg) /* {{{ */
if (errno == EAGAIN || errno == EINTR) continue;
break;
}
- else if (len == 0) break; /* We've reached EOF */
+ else if (len == 0)
+ {
+ /* We've reached EOF */
+ NOTICE ("exec plugin: Program `%s' has closed STDERR.",
+ pl->exec);
+ close (fd_err);
+ FD_CLR (fd_err, &fdset);
+ highest_fd = fd;
+ fd_err = -1;
+ continue;
+ }
pbuffer_err[len] = '\0';
copy = fdset;
}
+ DEBUG ("exec plugin: exec_read_one: Waiting for `%s' to exit.", pl->exec);
if (waitpid (pl->pid, &status, 0) > 0)
pl->status = status;
pthread_mutex_unlock (&pl_lock);
close (fd);
- close (fd_err);
+ if (fd_err >= 0)
+ close (fd_err);
pthread_exit ((void *) 0);
return (NULL);
static void *exec_notification_one (void *arg) /* {{{ */
{
program_list_t *pl = ((program_list_and_notification_t *) arg)->pl;
- const notification_t *n = &((program_list_and_notification_t *) arg)->n;
+ notification_t *n = &((program_list_and_notification_t *) arg)->n;
+ notification_meta_t *meta;
int fd;
FILE *fh;
int pid;
if (strlen (n->type_instance) > 0)
fprintf (fh, "TypeInstance: %s\n", n->type_instance);
+ for (meta = n->meta; meta != NULL; meta = meta->next)
+ {
+ if (meta->type == NM_TYPE_STRING)
+ fprintf (fh, "%s: %s\n", meta->name, meta->nm_value.nm_string);
+ else if (meta->type == NM_TYPE_SIGNED_INT)
+ fprintf (fh, "%s: %"PRIi64"\n", meta->name, meta->nm_value.nm_signed_int);
+ else if (meta->type == NM_TYPE_UNSIGNED_INT)
+ fprintf (fh, "%s: %"PRIu64"\n", meta->name, meta->nm_value.nm_unsigned_int);
+ else if (meta->type == NM_TYPE_DOUBLE)
+ fprintf (fh, "%s: %e\n", meta->name, meta->nm_value.nm_double);
+ else if (meta->type == NM_TYPE_BOOLEAN)
+ fprintf (fh, "%s: %s\n", meta->name,
+ meta->nm_value.nm_boolean ? "true" : "false");
+ }
+
fprintf (fh, "\n%s\n", n->message);
fflush (fh);
DEBUG ("exec plugin: Child %i exited with status %i.",
pid, status);
+ if (n->meta != NULL)
+ plugin_notification_meta_free (n->meta);
+ n->meta = NULL;
sfree (arg);
pthread_exit ((void *) 0);
return (NULL);
return (0);
} /* int exec_read }}} */
-static int exec_notification (const notification_t *n)
+static int exec_notification (const notification_t *n,
+ user_data_t __attribute__((unused)) *user_data)
{
program_list_t *pl;
program_list_and_notification_t *pln;
pln->pl = pl;
memcpy (&pln->n, n, sizeof (notification_t));
+ /* Set the `meta' member to NULL, otherwise `plugin_notification_meta_copy'
+ * will run into an endless loop. */
+ pln->n.meta = NULL;
+ plugin_notification_meta_copy (&pln->n, n);
+
pthread_attr_init (&attr);
pthread_attr_setdetachstate (&attr, PTHREAD_CREATE_DETACHED);
pthread_create (&t, &attr, exec_notification_one, (void *) pln);
} /* for (pl) */
return (0);
-} /* int exec_notification */
+} /* }}} int exec_notification */
static int exec_shutdown (void) /* {{{ */
{
plugin_register_complex_config ("exec", exec_config);
plugin_register_init ("exec", exec_init);
plugin_register_read ("exec", exec_read);
- plugin_register_notification ("exec", exec_notification);
+ plugin_register_notification ("exec", exec_notification,
+ /* user_data = */ NULL);
plugin_register_shutdown ("exec", exec_shutdown);
} /* void module_register */