2 * collectd - src/exec.c
3 * Copyright (C) 2007-2009 Florian octo Forster
4 * Copyright (C) 2007-2009 Sebastian Harl
5 * Copyright (C) 2008 Peter Holik
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation; only version 2 of the License is applicable.
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
21 * Florian octo Forster <octo at verplant.org>
22 * Sebastian Harl <sh at tokkee.org>
23 * Peter Holik <peter at holik.at>
30 #include "utils_cmd_putval.h"
31 #include "utils_cmd_putnotif.h"
33 #include <sys/types.h>
40 #define PL_NORMAL 0x01
41 #define PL_NOTIF_ACTION 0x02
43 #define PL_RUNNING 0x10
49 * Access to this structure is serialized using the `pl_lock' lock and the
50 * `PL_RUNNING' flag. The execution of notifications is *not* serialized, so
51 * all functions used to handle notifications MUST NOT write to this structure.
52 * The `pid' and `status' fields are thus unused if the `PL_NOTIF_ACTION' flag
54 * The `PL_RUNNING' flag is set in `exec_read' and unset in `exec_read_one'.
56 struct program_list_s;
57 typedef struct program_list_s program_list_t;
70 typedef struct program_list_and_notification_s
74 } program_list_and_notification_t;
79 static program_list_t *pl_head = NULL;
80 static pthread_mutex_t pl_lock = PTHREAD_MUTEX_INITIALIZER;
85 static void sigchld_handler (int __attribute__((unused)) signal) /* {{{ */
89 while ((pid = waitpid (-1, &status, WNOHANG)) > 0)
92 for (pl = pl_head; pl != NULL; pl = pl->next)
97 } /* while (waitpid) */
98 } /* void sigchld_handler }}} */
100 static int exec_config_exec (oconfig_item_t *ci) /* {{{ */
106 if (ci->children_num != 0)
108 WARNING ("exec plugin: The config option `%s' may not be a block.",
112 if (ci->values_num < 2)
114 WARNING ("exec plugin: The config option `%s' needs at least two "
115 "arguments.", ci->key);
118 if ((ci->values[0].type != OCONFIG_TYPE_STRING)
119 || (ci->values[1].type != OCONFIG_TYPE_STRING))
121 WARNING ("exec plugin: The first two arguments to the `%s' option must "
122 "be string arguments.", ci->key);
126 pl = (program_list_t *) malloc (sizeof (program_list_t));
129 ERROR ("exec plugin: malloc failed.");
132 memset (pl, '\0', sizeof (program_list_t));
134 if (strcasecmp ("NotificationExec", ci->key) == 0)
135 pl->flags |= PL_NOTIF_ACTION;
137 pl->flags |= PL_NORMAL;
139 pl->user = strdup (ci->values[0].value.string);
140 if (pl->user == NULL)
142 ERROR ("exec plugin: strdup failed.");
147 pl->group = strchr (pl->user, ':');
148 if (pl->group != NULL)
154 pl->exec = strdup (ci->values[1].value.string);
155 if (pl->exec == NULL)
157 ERROR ("exec plugin: strdup failed.");
163 pl->argv = (char **) malloc (ci->values_num * sizeof (char *));
164 if (pl->argv == NULL)
166 ERROR ("exec plugin: malloc failed.");
172 memset (pl->argv, '\0', ci->values_num * sizeof (char *));
175 char *tmp = strrchr (ci->values[1].value.string, '/');
177 sstrncpy (buffer, ci->values[1].value.string, sizeof (buffer));
179 sstrncpy (buffer, tmp + 1, sizeof (buffer));
181 pl->argv[0] = strdup (buffer);
182 if (pl->argv[0] == NULL)
184 ERROR ("exec plugin: malloc failed.");
192 for (i = 1; i < (ci->values_num - 1); i++)
194 if (ci->values[i + 1].type == OCONFIG_TYPE_STRING)
196 pl->argv[i] = strdup (ci->values[i + 1].value.string);
200 if (ci->values[i + 1].type == OCONFIG_TYPE_NUMBER)
202 ssnprintf (buffer, sizeof (buffer), "%lf",
203 ci->values[i + 1].value.number);
207 if (ci->values[i + 1].value.boolean)
208 sstrncpy (buffer, "true", sizeof (buffer));
210 sstrncpy (buffer, "false", sizeof (buffer));
213 pl->argv[i] = strdup (buffer);
216 if (pl->argv[i] == NULL)
218 ERROR ("exec plugin: strdup failed.");
223 if (i < (ci->values_num - 1))
236 for (i = 0; pl->argv[i] != NULL; i++)
238 DEBUG ("exec plugin: argv[%i] = %s", i, pl->argv[i]);
245 } /* int exec_config_exec }}} */
247 static int exec_config (oconfig_item_t *ci) /* {{{ */
251 for (i = 0; i < ci->children_num; i++)
253 oconfig_item_t *child = ci->children + i;
254 if ((strcasecmp ("Exec", child->key) == 0)
255 || (strcasecmp ("NotificationExec", child->key) == 0))
256 exec_config_exec (child);
259 WARNING ("exec plugin: Unknown config option `%s'.", child->key);
264 } /* int exec_config }}} */
266 static void exec_child (program_list_t *pl) /* {{{ */
273 struct passwd *sp_ptr;
279 status = getpwnam_r (pl->user, &sp, nambuf, sizeof (nambuf), &sp_ptr);
282 ERROR ("exec plugin: getpwnam_r failed: %s",
283 sstrerror (errno, errbuf, sizeof (errbuf)));
288 ERROR ("exec plugin: No such user: `%s'", pl->user);
296 ERROR ("exec plugin: Cowardly refusing to exec program as root.");
300 /* The group configured in the configfile is set as effective group, because
301 * this way the forked process can (re-)gain the user's primary group. */
303 if (NULL != pl->group)
305 if ('\0' != *pl->group) {
306 struct group *gr_ptr = NULL;
309 status = getgrnam_r (pl->group, &gr, nambuf, sizeof (nambuf), &gr_ptr);
312 ERROR ("exec plugin: getgrnam_r failed: %s",
313 sstrerror (errno, errbuf, sizeof (errbuf)));
318 ERROR ("exec plugin: No such group: `%s'", pl->group);
328 } /* if (pl->group == NULL) */
339 if ((gid != egid) && (egid != -1))
345 setgroups (glist_len, glist);
347 #endif /* HAVE_SETGROUPS */
349 status = setgid (gid);
352 ERROR ("exec plugin: setgid (%i) failed: %s",
353 gid, sstrerror (errno, errbuf, sizeof (errbuf)));
359 status = setegid (egid);
362 ERROR ("exec plugin: setegid (%i) failed: %s",
363 egid, sstrerror (errno, errbuf, sizeof (errbuf)));
368 status = setuid (uid);
371 ERROR ("exec plugin: setuid (%i) failed: %s",
372 uid, sstrerror (errno, errbuf, sizeof (errbuf)));
376 status = execvp (pl->exec, pl->argv);
378 ERROR ("exec plugin: exec failed: %s",
379 sstrerror (errno, errbuf, sizeof (errbuf)));
381 } /* void exec_child }}} */
384 * Creates three pipes (one for reading, one for writing and one for errors),
385 * forks a child, sets up the pipes so that fd_in is connected to STDIN of
386 * the child and fd_out is connected to STDOUT and fd_err is connected to STDERR
387 * of the child. Then is calls `exec_child'.
389 static int fork_child (program_list_t *pl, int *fd_in, int *fd_out, int *fd_err) /* {{{ */
401 status = pipe (fd_pipe_in);
404 ERROR ("exec plugin: pipe failed: %s",
405 sstrerror (errno, errbuf, sizeof (errbuf)));
409 status = pipe (fd_pipe_out);
412 ERROR ("exec plugin: pipe failed: %s",
413 sstrerror (errno, errbuf, sizeof (errbuf)));
417 status = pipe (fd_pipe_err);
420 ERROR ("exec plugin: pipe failed: %s",
421 sstrerror (errno, errbuf, sizeof (errbuf)));
428 ERROR ("exec plugin: fork failed: %s",
429 sstrerror (errno, errbuf, sizeof (errbuf)));
437 /* Close all file descriptors but the pipe end we need. */
438 fd_num = getdtablesize ();
439 for (fd = 0; fd < fd_num; fd++)
441 if ((fd == fd_pipe_in[0])
442 || (fd == fd_pipe_out[1])
443 || (fd == fd_pipe_err[1]))
448 /* Connect the `in' pipe to STDIN */
449 if (fd_pipe_in[0] != STDIN_FILENO)
451 dup2 (fd_pipe_in[0], STDIN_FILENO);
452 close (fd_pipe_in[0]);
455 /* Now connect the `out' pipe to STDOUT */
456 if (fd_pipe_out[1] != STDOUT_FILENO)
458 dup2 (fd_pipe_out[1], STDOUT_FILENO);
459 close (fd_pipe_out[1]);
462 /* Now connect the `out' pipe to STDOUT */
463 if (fd_pipe_err[1] != STDERR_FILENO)
465 dup2 (fd_pipe_err[1], STDERR_FILENO);
466 close (fd_pipe_err[1]);
470 /* does not return */
473 close (fd_pipe_in[0]);
474 close (fd_pipe_out[1]);
475 close (fd_pipe_err[1]);
478 *fd_in = fd_pipe_in[1];
480 close (fd_pipe_in[1]);
483 *fd_out = fd_pipe_out[0];
485 close (fd_pipe_out[0]);
488 *fd_err = fd_pipe_err[0];
490 close (fd_pipe_err[0]);
493 } /* int fork_child }}} */
495 static int parse_line (char *buffer) /* {{{ */
497 if (strncasecmp ("PUTVAL", buffer, strlen ("PUTVAL")) == 0)
498 return (handle_putval (stdout, buffer));
499 else if (strncasecmp ("PUTNOTIF", buffer, strlen ("PUTNOTIF")) == 0)
500 return (handle_putnotif (stdout, buffer));
503 /* For backwards compatibility */
505 /* Let's annoy the user a bit.. */
506 INFO ("exec plugin: Prepending `PUTVAL' to this line: %s", buffer);
507 ssnprintf (tmp, sizeof (tmp), "PUTVAL %s", buffer);
508 return (handle_putval (stdout, tmp));
510 } /* int parse_line }}} */
512 static void *exec_read_one (void *arg) /* {{{ */
514 program_list_t *pl = (program_list_t *) arg;
515 int fd, fd_err, highest_fd;
518 char buffer[1200]; /* if not completely read */
519 char buffer_err[1024];
520 char *pbuffer = buffer;
521 char *pbuffer_err = buffer_err;
523 status = fork_child (pl, NULL, &fd, &fd_err);
525 pthread_exit ((void *) 1);
528 assert (pl->pid != 0);
532 FD_SET(fd_err, &fdset);
534 /* Determine the highest file descriptor */
535 highest_fd = (fd > fd_err) ? fd : fd_err;
537 /* We use a copy of fdset, as select modifies it */
540 while (select(highest_fd + 1, ©, NULL, NULL, NULL ) > 0)
544 if (FD_ISSET(fd, ©))
548 len = read(fd, pbuffer, sizeof(buffer) - 1 - (pbuffer - buffer));
552 if (errno == EAGAIN || errno == EINTR) continue;
555 else if (len == 0) break; /* We've reached EOF */
559 len += pbuffer - buffer;
562 while ((pnl = strchr(pbuffer, '\n')))
565 if (*(pnl-1) == '\r' ) *(pnl-1) = '\0';
567 parse_line (pbuffer);
571 /* not completely read ? */
572 if (pbuffer - buffer < len)
574 len -= pbuffer - buffer;
575 memmove(buffer, pbuffer, len);
576 pbuffer = buffer + len;
581 else if (FD_ISSET(fd_err, ©))
585 len = read(fd_err, pbuffer_err, sizeof(buffer_err) - 1 - (pbuffer_err - buffer_err));
589 if (errno == EAGAIN || errno == EINTR) continue;
594 /* We've reached EOF */
595 NOTICE ("exec plugin: Program `%s' has closed STDERR.",
598 FD_CLR (fd_err, &fdset);
604 pbuffer_err[len] = '\0';
606 len += pbuffer_err - buffer_err;
607 pbuffer_err = buffer_err;
609 while ((pnl = strchr(pbuffer_err, '\n')))
612 if (*(pnl-1) == '\r' ) *(pnl-1) = '\0';
614 ERROR ("exec plugin: exec_read_one: error = %s", pbuffer_err);
618 /* not completely read ? */
619 if (pbuffer_err - buffer_err < len)
621 len -= pbuffer_err - buffer_err;
622 memmove(buffer_err, pbuffer_err, len);
623 pbuffer_err = buffer_err + len;
626 pbuffer_err = buffer_err;
632 DEBUG ("exec plugin: exec_read_one: Waiting for `%s' to exit.", pl->exec);
633 if (waitpid (pl->pid, &status, 0) > 0)
636 DEBUG ("exec plugin: Child %i exited with status %i.",
637 (int) pl->pid, pl->status);
641 pthread_mutex_lock (&pl_lock);
642 pl->flags &= ~PL_RUNNING;
643 pthread_mutex_unlock (&pl_lock);
649 pthread_exit ((void *) 0);
651 } /* void *exec_read_one }}} */
653 static void *exec_notification_one (void *arg) /* {{{ */
655 program_list_t *pl = ((program_list_and_notification_t *) arg)->pl;
656 notification_t *n = &((program_list_and_notification_t *) arg)->n;
657 notification_meta_t *meta;
662 const char *severity;
664 pid = fork_child (pl, &fd, NULL, NULL);
667 pthread_exit ((void *) 1);
670 fh = fdopen (fd, "w");
674 ERROR ("exec plugin: fdopen (%i) failed: %s", fd,
675 sstrerror (errno, errbuf, sizeof (errbuf)));
676 kill (pl->pid, SIGTERM);
680 pthread_exit ((void *) 1);
683 severity = "FAILURE";
684 if (n->severity == NOTIF_WARNING)
685 severity = "WARNING";
686 else if (n->severity == NOTIF_OKAY)
692 severity, (unsigned int) n->time);
694 /* Print the optional fields */
695 if (strlen (n->host) > 0)
696 fprintf (fh, "Host: %s\n", n->host);
697 if (strlen (n->plugin) > 0)
698 fprintf (fh, "Plugin: %s\n", n->plugin);
699 if (strlen (n->plugin_instance) > 0)
700 fprintf (fh, "PluginInstance: %s\n", n->plugin_instance);
701 if (strlen (n->type) > 0)
702 fprintf (fh, "Type: %s\n", n->type);
703 if (strlen (n->type_instance) > 0)
704 fprintf (fh, "TypeInstance: %s\n", n->type_instance);
706 for (meta = n->meta; meta != NULL; meta = meta->next)
708 if (meta->type == NM_TYPE_STRING)
709 fprintf (fh, "%s: %s\n", meta->name, meta->nm_value.nm_string);
710 else if (meta->type == NM_TYPE_SIGNED_INT)
711 fprintf (fh, "%s: %"PRIi64"\n", meta->name, meta->nm_value.nm_signed_int);
712 else if (meta->type == NM_TYPE_UNSIGNED_INT)
713 fprintf (fh, "%s: %"PRIu64"\n", meta->name, meta->nm_value.nm_unsigned_int);
714 else if (meta->type == NM_TYPE_DOUBLE)
715 fprintf (fh, "%s: %e\n", meta->name, meta->nm_value.nm_double);
716 else if (meta->type == NM_TYPE_BOOLEAN)
717 fprintf (fh, "%s: %s\n", meta->name,
718 meta->nm_value.nm_boolean ? "true" : "false");
721 fprintf (fh, "\n%s\n", n->message);
726 waitpid (pid, &status, 0);
728 DEBUG ("exec plugin: Child %i exited with status %i.",
731 plugin_notification_meta_free (n->meta);
734 pthread_exit ((void *) 0);
736 } /* void *exec_notification_one }}} */
738 static int exec_init (void) /* {{{ */
742 memset (&sa, '\0', sizeof (sa));
743 sa.sa_handler = sigchld_handler;
744 sigaction (SIGCHLD, &sa, NULL);
747 } /* int exec_init }}} */
749 static int exec_read (void) /* {{{ */
753 for (pl = pl_head; pl != NULL; pl = pl->next)
758 /* Only execute `normal' style executables here. */
759 if ((pl->flags & PL_NORMAL) == 0)
762 pthread_mutex_lock (&pl_lock);
763 /* Skip if a child is already running. */
764 if ((pl->flags & PL_RUNNING) != 0)
766 pthread_mutex_unlock (&pl_lock);
769 pl->flags |= PL_RUNNING;
770 pthread_mutex_unlock (&pl_lock);
772 pthread_attr_init (&attr);
773 pthread_attr_setdetachstate (&attr, PTHREAD_CREATE_DETACHED);
774 pthread_create (&t, &attr, exec_read_one, (void *) pl);
778 } /* int exec_read }}} */
780 static int exec_notification (const notification_t *n)
783 program_list_and_notification_t *pln;
785 for (pl = pl_head; pl != NULL; pl = pl->next)
790 /* Only execute `notification' style executables here. */
791 if ((pl->flags & PL_NOTIF_ACTION) == 0)
794 /* Skip if a child is already running. */
798 pln = (program_list_and_notification_t *) malloc (sizeof
799 (program_list_and_notification_t));
802 ERROR ("exec plugin: malloc failed.");
807 memcpy (&pln->n, n, sizeof (notification_t));
809 /* Set the `meta' member to NULL, otherwise `plugin_notification_meta_copy'
810 * will run into an endless loop. */
812 plugin_notification_meta_copy (&pln->n, n);
814 pthread_attr_init (&attr);
815 pthread_attr_setdetachstate (&attr, PTHREAD_CREATE_DETACHED);
816 pthread_create (&t, &attr, exec_notification_one, (void *) pln);
820 } /* int exec_notification */
822 static int exec_shutdown (void) /* {{{ */
825 program_list_t *next;
834 kill (pl->pid, SIGTERM);
835 INFO ("exec plugin: Sent SIGTERM to %hu", (unsigned short int) pl->pid);
846 } /* int exec_shutdown }}} */
848 void module_register (void)
850 plugin_register_complex_config ("exec", exec_config);
851 plugin_register_init ("exec", exec_init);
852 plugin_register_read ("exec", exec_read);
853 plugin_register_notification ("exec", exec_notification);
854 plugin_register_shutdown ("exec", exec_shutdown);
855 } /* void module_register */
858 * vim:shiftwidth=2:softtabstop=2:tabstop=8:fdm=marker