X-Git-Url: https://git.octo.it/?a=blobdiff_plain;f=src%2Fexec.c;h=0445b14a91f879bdfccb2cd44024586741ce5888;hb=808f54945bd554bdfb43e1efc2cdf6de18aefc4c;hp=c5ea155ebc53f10e4d191bc84dca0828476a953a;hpb=f1ba2733d2c58fed90c13e9ae31cb7c1f3c5613e;p=collectd.git diff --git a/src/exec.c b/src/exec.c index c5ea155e..0445b14a 100644 --- a/src/exec.c +++ b/src/exec.c @@ -270,14 +270,15 @@ static void set_environment (void) /* {{{ */ 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); @@ -286,71 +287,11 @@ static void set_environment (void) /* {{{ */ } /* }}} 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) { @@ -397,7 +338,7 @@ static void exec_child (program_list_t *pl) /* {{{ */ 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))); @@ -413,6 +354,31 @@ static void reset_signal_mask (void) /* {{{ */ 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 @@ -421,46 +387,89 @@ static void reset_signal_mask (void) /* {{{ */ */ 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); + goto failed; } else if (pid == 0) { @@ -492,7 +501,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 +513,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 +537,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) /* {{{ */ @@ -635,17 +651,22 @@ static void *exec_read_one (void *arg) /* {{{ */ 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); + 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; } @@ -821,7 +842,8 @@ static int exec_read (void) /* {{{ */ 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); @@ -864,7 +886,8 @@ static int exec_notification (const notification_t *n, /* {{{ */ 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);