diff --git a/src/exec.c b/src/exec.c
index 0d33b290cce6afa2293a247ea7cf3342cc4833e6..83055daceb8d47d5c057e9c72c2d5bad30585441 100644 (file)
--- a/src/exec.c
+++ b/src/exec.c
* Peter Holik <peter at holik.at>
**/
+#define _DEFAULT_SOURCE
#define _BSD_SOURCE /* For setgroups */
#include "collectd.h"
program_list_t *pl;
for (pl = pl_head; pl != NULL; pl = pl->next)
if (pl->pid == pid)
- break;
+ break;
if (pl != NULL)
pl->status = status;
} /* while (waitpid) */
if (ci->children_num != 0)
{
WARNING ("exec plugin: The config option `%s' may not be a block.",
- ci->key);
+ ci->key);
return (-1);
}
if (ci->values_num < 2)
{
WARNING ("exec plugin: The config option `%s' needs at least two "
- "arguments.", ci->key);
+ "arguments.", ci->key);
return (-1);
}
if ((ci->values[0].type != OCONFIG_TYPE_STRING)
|| (ci->values[1].type != OCONFIG_TYPE_STRING))
{
WARNING ("exec plugin: The first two arguments to the `%s' option must "
- "be string arguments.", ci->key);
+ "be string arguments.", ci->key);
return (-1);
}
{
if (ci->values[i + 1].type == OCONFIG_TYPE_NUMBER)
{
- ssnprintf (buffer, sizeof (buffer), "%lf",
- ci->values[i + 1].value.number);
+ ssnprintf (buffer, sizeof (buffer), "%lf",
+ ci->values[i + 1].value.number);
}
else
{
- if (ci->values[i + 1].value.boolean)
- sstrncpy (buffer, "true", sizeof (buffer));
- else
- sstrncpy (buffer, "false", sizeof (buffer));
+ if (ci->values[i + 1].value.boolean)
+ sstrncpy (buffer, "true", sizeof (buffer));
+ else
+ sstrncpy (buffer, "false", sizeof (buffer));
}
pl->argv[i] = strdup (buffer);
{
oconfig_item_t *child = ci->children + i;
if ((strcasecmp ("Exec", child->key) == 0)
- || (strcasecmp ("NotificationExec", child->key) == 0))
+ || (strcasecmp ("NotificationExec", child->key) == 0))
exec_config_exec (child);
else
{
char buffer[1024];
#ifdef HAVE_SETENV
- ssnprintf (buffer, sizeof (buffer), "%.3f", CDTIME_T_TO_DOUBLE (interval_g));
+ ssnprintf (buffer, sizeof (buffer), "%.3f",
+ CDTIME_T_TO_DOUBLE (plugin_get_interval ()));
setenv ("COLLECTD_INTERVAL", buffer, /* overwrite = */ 1);
ssnprintf (buffer, sizeof (buffer), "%s", hostname_g);
setenv ("COLLECTD_HOSTNAME", buffer, /* overwrite = */ 1);
#else
ssnprintf (buffer, sizeof (buffer), "COLLECTD_INTERVAL=%.3f",
- CDTIME_T_TO_DOUBLE (interval_g));
+ CDTIME_T_TO_DOUBLE (plugin_get_interval ()));
putenv (buffer);
ssnprintf (buffer, sizeof (buffer), "COLLECTD_HOSTNAME=%s", hostname_g);
} /* }}} void set_environment */
__attribute__((noreturn))
-static void exec_child (program_list_t *pl) /* {{{ */
+static void exec_child (program_list_t *pl, int uid, int gid, int egid) /* {{{ */
{
int status;
- int uid;
- int gid;
- int egid;
-
- struct passwd *sp_ptr;
- struct passwd sp;
- char nambuf[2048];
char errbuf[1024];
- sp_ptr = NULL;
- status = getpwnam_r (pl->user, &sp, nambuf, sizeof (nambuf), &sp_ptr);
- if (status != 0)
- {
- ERROR ("exec plugin: Failed to get user information for user ``%s'': %s",
- pl->user, sstrerror (errno, errbuf, sizeof (errbuf)));
- exit (-1);
- }
- if (sp_ptr == NULL)
- {
- ERROR ("exec plugin: No such user: `%s'", pl->user);
- exit (-1);
- }
-
- uid = sp.pw_uid;
- gid = sp.pw_gid;
- if (uid == 0)
- {
- ERROR ("exec plugin: Cowardly refusing to exec program as root.");
- exit (-1);
- }
-
- /* The group configured in the configfile is set as effective group, because
- * this way the forked process can (re-)gain the user's primary group. */
- egid = -1;
- if (NULL != pl->group)
- {
- if ('\0' != *pl->group) {
- struct group *gr_ptr = NULL;
- struct group gr;
-
- status = getgrnam_r (pl->group, &gr, nambuf, sizeof (nambuf), &gr_ptr);
- if (0 != status)
- {
- ERROR ("exec plugin: Failed to get group information "
- "for group ``%s'': %s", pl->group,
- sstrerror (errno, errbuf, sizeof (errbuf)));
- exit (-1);
- }
- if (NULL == gr_ptr)
- {
- ERROR ("exec plugin: No such group: `%s'", pl->group);
- exit (-1);
- }
-
- egid = gr.gr_gid;
- }
- else
- {
- egid = gid;
- }
- } /* if (pl->group == NULL) */
-
#if HAVE_SETGROUPS
if (getuid () == 0)
{
if (status != 0)
{
ERROR ("exec plugin: setgid (%i) failed: %s",
- gid, sstrerror (errno, errbuf, sizeof (errbuf)));
+ gid, sstrerror (errno, errbuf, sizeof (errbuf)));
exit (-1);
}
if (status != 0)
{
ERROR ("exec plugin: setegid (%i) failed: %s",
- egid, sstrerror (errno, errbuf, sizeof (errbuf)));
+ egid, sstrerror (errno, errbuf, sizeof (errbuf)));
exit (-1);
}
}
if (status != 0)
{
ERROR ("exec plugin: setuid (%i) failed: %s",
- uid, sstrerror (errno, errbuf, sizeof (errbuf)));
+ uid, sstrerror (errno, errbuf, sizeof (errbuf)));
exit (-1);
}
- status = execvp (pl->exec, pl->argv);
+ execvp (pl->exec, pl->argv);
ERROR ("exec plugin: Failed to execute ``%s'': %s",
pl->exec, sstrerror (errno, errbuf, sizeof (errbuf)));
sigprocmask (SIG_SETMASK, &ss, /* old mask = */ NULL);
} /* }}} void reset_signal_mask */
+static int create_pipe (int fd_pipe[2]) /* {{{ */
+{
+ char errbuf[1024];
+ int status;
+
+ status = pipe (fd_pipe);
+ if (status != 0)
+ {
+ ERROR ("exec plugin: pipe failed: %s",
+ sstrerror (errno, errbuf, sizeof (errbuf)));
+ return (-1);
+ }
+
+ return 0;
+} /* }}} int create_pipe */
+
+static void close_pipe (int fd_pipe[2]) /* {{{ */
+{
+ if (fd_pipe[0] != -1)
+ close (fd_pipe[0]);
+
+ if (fd_pipe[1] != -1)
+ close (fd_pipe[1]);
+} /* }}} void close_pipe */
+
/*
* 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
*/
static int fork_child (program_list_t *pl, int *fd_in, int *fd_out, int *fd_err) /* {{{ */
{
- int fd_pipe_in[2];
- int fd_pipe_out[2];
- int fd_pipe_err[2];
+ int fd_pipe_in[2] = {-1, -1};
+ int fd_pipe_out[2] = {-1, -1};
+ int fd_pipe_err[2] = {-1, -1};
char errbuf[1024];
int status;
int pid;
+ int uid;
+ int gid;
+ int egid;
+
+ struct passwd *sp_ptr;
+ struct passwd sp;
+ char nambuf[2048];
+
if (pl->pid != 0)
return (-1);
- status = pipe (fd_pipe_in);
+ if ((create_pipe(fd_pipe_in) == -1)
+ || (create_pipe(fd_pipe_out) == -1)
+ || (create_pipe(fd_pipe_err) == -1))
+ goto failed;
+
+ sp_ptr = NULL;
+ status = getpwnam_r (pl->user, &sp, nambuf, sizeof (nambuf), &sp_ptr);
if (status != 0)
{
- ERROR ("exec plugin: pipe failed: %s",
- sstrerror (errno, errbuf, sizeof (errbuf)));
- return (-1);
+ ERROR ("exec plugin: Failed to get user information for user ``%s'': %s",
+ pl->user, sstrerror (errno, errbuf, sizeof (errbuf)));
+ goto failed;
}
- status = pipe (fd_pipe_out);
- if (status != 0)
+ if (sp_ptr == NULL)
{
- ERROR ("exec plugin: pipe failed: %s",
- sstrerror (errno, errbuf, sizeof (errbuf)));
- return (-1);
+ ERROR ("exec plugin: No such user: `%s'", pl->user);
+ goto failed;
}
- status = pipe (fd_pipe_err);
- if (status != 0)
+ uid = sp.pw_uid;
+ gid = sp.pw_gid;
+ if (uid == 0)
{
- ERROR ("exec plugin: pipe failed: %s",
- sstrerror (errno, errbuf, sizeof (errbuf)));
- return (-1);
+ ERROR ("exec plugin: Cowardly refusing to exec program as root.");
+ goto failed;
}
+ /* The group configured in the configfile is set as effective group, because
+ * this way the forked process can (re-)gain the user's primary group. */
+ egid = -1;
+ if (NULL != pl->group)
+ {
+ if ('\0' != *pl->group) {
+ struct group *gr_ptr = NULL;
+ struct group gr;
+
+ status = getgrnam_r (pl->group, &gr, nambuf, sizeof (nambuf), &gr_ptr);
+ if (0 != status)
+ {
+ ERROR ("exec plugin: Failed to get group information "
+ "for group ``%s'': %s", pl->group,
+ sstrerror (errno, errbuf, sizeof (errbuf)));
+ goto failed;
+ }
+ if (NULL == gr_ptr)
+ {
+ ERROR ("exec plugin: No such group: `%s'", pl->group);
+ goto failed;
+ }
+
+ egid = gr.gr_gid;
+ }
+ else
+ {
+ egid = gid;
+ }
+ } /* if (pl->group == NULL) */
+
pid = fork ();
if (pid < 0)
{
ERROR ("exec plugin: fork failed: %s",
- sstrerror (errno, errbuf, sizeof (errbuf)));
- return (-1);
+ sstrerror (errno, errbuf, sizeof (errbuf)));
+ goto failed;
}
else if (pid == 0)
{
@@ -472,9 +482,9 @@ static int fork_child (program_list_t *pl, int *fd_in, int *fd_out, int *fd_err)
for (fd = 0; fd < fd_num; fd++)
{
if ((fd == fd_pipe_in[0])
- || (fd == fd_pipe_out[1])
- || (fd == fd_pipe_err[1]))
- continue;
+ || (fd == fd_pipe_out[1])
+ || (fd == fd_pipe_err[1]))
+ continue;
close (fd);
}
@@ -492,7 +502,7 @@ static int fork_child (program_list_t *pl, int *fd_in, int *fd_out, int *fd_err)
close (fd_pipe_out[1]);
}
- /* Now connect the `out' pipe to STDOUT */
+ /* Now connect the `err' pipe to STDERR */
if (fd_pipe_err[1] != STDERR_FILENO)
{
dup2 (fd_pipe_err[1], STDERR_FILENO);
@@ -504,7 +514,7 @@ static int fork_child (program_list_t *pl, int *fd_in, int *fd_out, int *fd_err)
/* Unblock all signals */
reset_signal_mask ();
- exec_child (pl);
+ exec_child (pl, uid, gid, egid);
/* does not return */
}
@@ -528,6 +538,13 @@ static int fork_child (program_list_t *pl, int *fd_in, int *fd_out, int *fd_err)
close (fd_pipe_err[0]);
return (pid);
+
+failed:
+ close_pipe(fd_pipe_in);
+ close_pipe(fd_pipe_out);
+ close_pipe(fd_pipe_err);
+
+ return (-1);
} /* int fork_child }}} */
static int parse_line (char *buffer) /* {{{ */
/* We use a copy of fdset, as select modifies it */
copy = fdset;
- while (select(highest_fd + 1, ©, NULL, NULL, NULL ) > 0)
+ while (1)
{
int len;
+ status = select (highest_fd + 1, ©, NULL, NULL, NULL);
+ if (status < 0)
+ {
+ if (errno == EINTR)
+ continue;
+ break;
+ }
+
if (FD_ISSET(fd, ©))
{
char *pnl;
if (len < 0)
{
- if (errno == EAGAIN || errno == EINTR) continue;
+ if (errno == EAGAIN || errno == EINTR)
+ continue;
break;
}
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;
+ /* We've reached EOF */
+ NOTICE ("exec plugin: Program `%s' has closed STDERR.", pl->exec);
+
+ /* Remove file descriptor form select() set. */
+ FD_CLR (fd_err, &fdset);
+ copy = fdset;
+ highest_fd = fd;
+
+ /* Clean up file descriptor */
+ close (fd_err);
+ fd_err = -1;
+ continue;
}
pbuffer_err[len] = '\0';
{
char errbuf[1024];
ERROR ("exec plugin: fdopen (%i) failed: %s", fd,
- sstrerror (errno, errbuf, sizeof (errbuf)));
- kill (pl->pid, SIGTERM);
- pl->pid = 0;
+ sstrerror (errno, errbuf, sizeof (errbuf)));
+ kill (pid, SIGTERM);
close (fd);
sfree (arg);
pthread_exit ((void *) 1);
fprintf (fh,
"Severity: %s\n"
- "Time: %u\n",
- severity, (unsigned int) n->time);
+ "Time: %.3f\n",
+ severity, CDTIME_T_TO_DOUBLE (n->time));
/* Print the optional fields */
if (strlen (n->host) > 0)
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");
+ meta->nm_value.nm_boolean ? "true" : "false");
}
fprintf (fh, "\n%s\n", n->message);
pthread_attr_init (&attr);
pthread_attr_setdetachstate (&attr, PTHREAD_CREATE_DETACHED);
- pthread_create (&t, &attr, exec_read_one, (void *) pl);
+ plugin_thread_create (&t, &attr, exec_read_one, (void *) pl);
+ pthread_attr_destroy (&attr);
} /* for (pl) */
return (0);
continue;
pln = (program_list_and_notification_t *) malloc (sizeof
- (program_list_and_notification_t));
+ (program_list_and_notification_t));
if (pln == NULL)
{
ERROR ("exec plugin: malloc failed.");
pthread_attr_init (&attr);
pthread_attr_setdetachstate (&attr, PTHREAD_CREATE_DETACHED);
- pthread_create (&t, &attr, exec_notification_one, (void *) pln);
+ plugin_thread_create (&t, &attr, exec_notification_one, (void *) pln);
+ pthread_attr_destroy (&attr);
} /* for (pl) */
return (0);