2 * collectd - src/exec.c
3 * Copyright (C) 2007,2008 Florian octo Forster
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; only version 2 of the License is applicable.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 * Florian octo Forster <octo at verplant.org>
26 #include "utils_cmd_putval.h"
27 #include "utils_cmd_putnotif.h"
29 #include <sys/types.h>
36 #define PL_NORMAL 0x01
37 #define PL_NOTIF_ACTION 0x02
39 #define PL_RUNNING 0x10
45 * Access to this structure is serialized using the `pl_lock' lock and the
46 * `PL_RUNNING' flag. The execution of notifications is *not* serialized, so
47 * all functions used to handle notifications MUST NOT write to this structure.
48 * The `pid' and `status' fields are thus unused if the `PL_NOTIF_ACTION' flag
50 * The `PL_RUNNING' flag is set in `exec_read' and unset in `exec_read_one'.
52 struct program_list_s;
53 typedef struct program_list_s program_list_t;
66 typedef struct program_list_and_notification_s
70 } program_list_and_notification_t;
75 static program_list_t *pl_head = NULL;
76 static pthread_mutex_t pl_lock = PTHREAD_MUTEX_INITIALIZER;
81 static void sigchld_handler (int signal) /* {{{ */
85 while ((pid = waitpid (-1, &status, WNOHANG)) > 0)
88 for (pl = pl_head; pl != NULL; pl = pl->next)
93 } /* while (waitpid) */
94 } /* void sigchld_handler }}} */
96 static int exec_config_exec (oconfig_item_t *ci) /* {{{ */
102 if (ci->children_num != 0)
104 WARNING ("exec plugin: The config option `%s' may not be a block.",
108 if (ci->values_num < 2)
110 WARNING ("exec plugin: The config option `%s' needs at least two "
111 "arguments.", ci->key);
114 if ((ci->values[0].type != OCONFIG_TYPE_STRING)
115 || (ci->values[1].type != OCONFIG_TYPE_STRING))
117 WARNING ("exec plugin: The first two arguments to the `%s' option must "
118 "be string arguments.", ci->key);
122 pl = (program_list_t *) malloc (sizeof (program_list_t));
125 ERROR ("exec plugin: malloc failed.");
128 memset (pl, '\0', sizeof (program_list_t));
130 if (strcasecmp ("NotificationExec", ci->key) == 0)
131 pl->flags |= PL_NOTIF_ACTION;
133 pl->flags |= PL_NORMAL;
135 pl->user = strdup (ci->values[0].value.string);
136 if (pl->user == NULL)
138 ERROR ("exec plugin: strdup failed.");
143 pl->group = strchr (pl->user, ':');
144 if (pl->group != NULL)
150 pl->exec = strdup (ci->values[1].value.string);
151 if (pl->exec == NULL)
153 ERROR ("exec plugin: strdup failed.");
159 pl->argv = (char **) malloc (ci->values_num * sizeof (char *));
160 if (pl->argv == NULL)
162 ERROR ("exec plugin: malloc failed.");
168 memset (pl->argv, '\0', ci->values_num * sizeof (char *));
171 char *tmp = strrchr (ci->values[1].value.string, '/');
173 sstrncpy (buffer, ci->values[1].value.string, sizeof (buffer));
175 sstrncpy (buffer, tmp + 1, sizeof (buffer));
177 pl->argv[0] = strdup (buffer);
178 if (pl->argv[0] == NULL)
180 ERROR ("exec plugin: malloc failed.");
188 for (i = 1; i < (ci->values_num - 1); i++)
190 if (ci->values[i + 1].type == OCONFIG_TYPE_STRING)
192 pl->argv[i] = strdup (ci->values[i + 1].value.string);
196 if (ci->values[i + 1].type == OCONFIG_TYPE_NUMBER)
198 ssnprintf (buffer, sizeof (buffer), "%lf",
199 ci->values[i + 1].value.number);
203 if (ci->values[i + 1].value.boolean)
204 sstrncpy (buffer, "true", sizeof (buffer));
206 sstrncpy (buffer, "false", sizeof (buffer));
209 pl->argv[i] = strdup (buffer);
212 if (pl->argv[i] == NULL)
214 ERROR ("exec plugin: strdup failed.");
219 if (i < (ci->values_num - 1))
232 for (i = 0; pl->argv[i] != NULL; i++)
234 DEBUG ("exec plugin: argv[%i] = %s", i, pl->argv[i]);
241 } /* int exec_config_exec }}} */
243 static int exec_config (oconfig_item_t *ci) /* {{{ */
247 for (i = 0; i < ci->children_num; i++)
249 oconfig_item_t *child = ci->children + i;
250 if ((strcasecmp ("Exec", child->key) == 0)
251 || (strcasecmp ("NotificationExec", child->key) == 0))
252 exec_config_exec (child);
255 WARNING ("exec plugin: Unknown config option `%s'.", child->key);
260 } /* int exec_config }}} */
262 static void exec_child (program_list_t *pl) /* {{{ */
269 struct passwd *sp_ptr;
275 status = getpwnam_r (pl->user, &sp, nambuf, sizeof (nambuf), &sp_ptr);
278 ERROR ("exec plugin: getpwnam_r failed: %s",
279 sstrerror (errno, errbuf, sizeof (errbuf)));
284 ERROR ("exec plugin: No such user: `%s'", pl->user);
292 ERROR ("exec plugin: Cowardly refusing to exec program as root.");
296 /* The group configured in the configfile is set as effective group, because
297 * this way the forked process can (re-)gain the user's primary group. */
299 if (NULL != pl->group)
301 if ('\0' != *pl->group) {
302 struct group *gr_ptr = NULL;
305 status = getgrnam_r (pl->group, &gr, nambuf, sizeof (nambuf), &gr_ptr);
308 ERROR ("exec plugin: getgrnam_r failed: %s",
309 sstrerror (errno, errbuf, sizeof (errbuf)));
314 ERROR ("exec plugin: No such group: `%s'", pl->group);
324 } /* if (pl->group == NULL) */
335 if ((gid != egid) && (egid != -1))
341 setgroups (glist_len, glist);
343 #endif /* HAVE_SETGROUPS */
345 status = setgid (gid);
348 ERROR ("exec plugin: setgid (%i) failed: %s",
349 gid, sstrerror (errno, errbuf, sizeof (errbuf)));
355 status = setegid (egid);
358 ERROR ("exec plugin: setegid (%i) failed: %s",
359 egid, sstrerror (errno, errbuf, sizeof (errbuf)));
364 status = setuid (uid);
367 ERROR ("exec plugin: setuid (%i) failed: %s",
368 uid, sstrerror (errno, errbuf, sizeof (errbuf)));
372 status = execvp (pl->exec, pl->argv);
374 ERROR ("exec plugin: exec failed: %s",
375 sstrerror (errno, errbuf, sizeof (errbuf)));
377 } /* void exec_child }}} */
380 * Creates three pipes (one for reading, one for writing and one for errors),
381 * forks a child, sets up the pipes so that fd_in is connected to STDIN of
382 * the child and fd_out is connected to STDOUT and fd_err is connected to STDERR
383 * of the child. Then is calls `exec_child'.
385 static int fork_child (program_list_t *pl, int *fd_in, int *fd_out, int *fd_err) /* {{{ */
397 status = pipe (fd_pipe_in);
400 ERROR ("exec plugin: pipe failed: %s",
401 sstrerror (errno, errbuf, sizeof (errbuf)));
405 status = pipe (fd_pipe_out);
408 ERROR ("exec plugin: pipe failed: %s",
409 sstrerror (errno, errbuf, sizeof (errbuf)));
413 status = pipe (fd_pipe_err);
416 ERROR ("exec plugin: pipe failed: %s",
417 sstrerror (errno, errbuf, sizeof (errbuf)));
424 ERROR ("exec plugin: fork failed: %s",
425 sstrerror (errno, errbuf, sizeof (errbuf)));
433 /* Close all file descriptors but the pipe end we need. */
434 fd_num = getdtablesize ();
435 for (fd = 0; fd < fd_num; fd++)
437 if ((fd == fd_pipe_in[0])
438 || (fd == fd_pipe_out[1])
439 || (fd == fd_pipe_err[1]))
444 /* Connect the `in' pipe to STDIN */
445 if (fd_pipe_in[0] != STDIN_FILENO)
447 dup2 (fd_pipe_in[0], STDIN_FILENO);
448 close (fd_pipe_in[0]);
451 /* Now connect the `out' pipe to STDOUT */
452 if (fd_pipe_out[1] != STDOUT_FILENO)
454 dup2 (fd_pipe_out[1], STDOUT_FILENO);
455 close (fd_pipe_out[1]);
458 /* Now connect the `out' pipe to STDOUT */
459 if (fd_pipe_err[1] != STDERR_FILENO)
461 dup2 (fd_pipe_err[1], STDERR_FILENO);
462 close (fd_pipe_err[1]);
466 /* does not return */
469 close (fd_pipe_in[0]);
470 close (fd_pipe_out[1]);
471 close (fd_pipe_err[1]);
474 *fd_in = fd_pipe_in[1];
476 close (fd_pipe_in[1]);
479 *fd_out = fd_pipe_out[0];
481 close (fd_pipe_out[0]);
484 *fd_err = fd_pipe_err[0];
486 close (fd_pipe_err[0]);
489 } /* int fork_child }}} */
491 static int parse_line (char *buffer) /* {{{ */
493 if (strncasecmp ("PUTVAL", buffer, strlen ("PUTVAL")) == 0)
494 return (handle_putval (stdout, buffer));
495 else if (strncasecmp ("PUTNOTIF", buffer, strlen ("PUTNOTIF")) == 0)
496 return (handle_putnotif (stdout, buffer));
499 /* For backwards compatibility */
501 /* Let's annoy the user a bit.. */
502 INFO ("exec plugin: Prepending `PUTVAL' to this line: %s", buffer);
503 ssnprintf (tmp, sizeof (tmp), "PUTVAL %s", buffer);
504 return (handle_putval (stdout, tmp));
506 } /* int parse_line }}} */
508 static void *exec_read_one (void *arg) /* {{{ */
510 program_list_t *pl = (program_list_t *) arg;
511 int fd, fd_err, highest_fd;
514 char buffer[1200]; /* if not completely read */
515 char buffer_err[1024];
516 char *pbuffer = buffer;
517 char *pbuffer_err = buffer_err;
519 status = fork_child (pl, NULL, &fd, &fd_err);
521 pthread_exit ((void *) 1);
524 assert (pl->pid != 0);
528 FD_SET(fd_err, &fdset);
530 /* Determine the highest file descriptor */
531 highest_fd = (fd > fd_err) ? fd : fd_err;
533 /* We use a copy of fdset, as select modifies it */
536 while (select(highest_fd + 1, ©, NULL, NULL, NULL ) > 0)
540 if (FD_ISSET(fd, ©))
544 len = read(fd, pbuffer, sizeof(buffer) - 1 - (pbuffer - buffer));
548 if (errno == EAGAIN || errno == EINTR) continue;
551 else if (len == 0) break; /* We've reached EOF */
555 len += pbuffer - buffer;
558 while ((pnl = strchr(pbuffer, '\n')))
561 if (*(pnl-1) == '\r' ) *(pnl-1) = '\0';
563 parse_line (pbuffer);
567 /* not completely read ? */
568 if (pbuffer - buffer < len)
570 len -= pbuffer - buffer;
571 memmove(buffer, pbuffer, len);
572 pbuffer = buffer + len;
577 else if (FD_ISSET(fd_err, ©))
581 len = read(fd_err, pbuffer_err, sizeof(buffer_err) - 1 - (pbuffer_err - buffer_err));
585 if (errno == EAGAIN || errno == EINTR) continue;
588 else if (len == 0) break; /* We've reached EOF */
590 pbuffer_err[len] = '\0';
592 len += pbuffer_err - buffer_err;
593 pbuffer_err = buffer_err;
595 while ((pnl = strchr(pbuffer_err, '\n')))
598 if (*(pnl-1) == '\r' ) *(pnl-1) = '\0';
600 ERROR ("exec plugin: exec_read_one: error = %s", pbuffer_err);
604 /* not completely read ? */
605 if (pbuffer_err - buffer_err < len)
607 len -= pbuffer_err - buffer_err;
608 memmove(buffer_err, pbuffer_err, len);
609 pbuffer_err = buffer_err + len;
612 pbuffer_err = buffer_err;
618 if (waitpid (pl->pid, &status, 0) > 0)
621 DEBUG ("exec plugin: Child %i exited with status %i.",
622 (int) pl->pid, pl->status);
626 pthread_mutex_lock (&pl_lock);
627 pl->flags &= ~PL_RUNNING;
628 pthread_mutex_unlock (&pl_lock);
633 pthread_exit ((void *) 0);
635 } /* void *exec_read_one }}} */
637 static void *exec_notification_one (void *arg) /* {{{ */
639 program_list_t *pl = ((program_list_and_notification_t *) arg)->pl;
640 notification_t *n = &((program_list_and_notification_t *) arg)->n;
641 notification_meta_t *meta;
646 const char *severity;
648 pid = fork_child (pl, &fd, NULL, NULL);
651 pthread_exit ((void *) 1);
654 fh = fdopen (fd, "w");
658 ERROR ("exec plugin: fdopen (%i) failed: %s", fd,
659 sstrerror (errno, errbuf, sizeof (errbuf)));
660 kill (pl->pid, SIGTERM);
664 pthread_exit ((void *) 1);
667 severity = "FAILURE";
668 if (n->severity == NOTIF_WARNING)
669 severity = "WARNING";
670 else if (n->severity == NOTIF_OKAY)
676 severity, (unsigned int) n->time);
678 /* Print the optional fields */
679 if (strlen (n->host) > 0)
680 fprintf (fh, "Host: %s\n", n->host);
681 if (strlen (n->plugin) > 0)
682 fprintf (fh, "Plugin: %s\n", n->plugin);
683 if (strlen (n->plugin_instance) > 0)
684 fprintf (fh, "PluginInstance: %s\n", n->plugin_instance);
685 if (strlen (n->type) > 0)
686 fprintf (fh, "Type: %s\n", n->type);
687 if (strlen (n->type_instance) > 0)
688 fprintf (fh, "TypeInstance: %s\n", n->type_instance);
690 for (meta = n->meta; meta != NULL; meta = meta->next)
692 if (meta->type == NM_TYPE_STRING)
693 fprintf (fh, "%s: %s\n", meta->name, meta->value_string);
694 else if (meta->type == NM_TYPE_SIGNED_INT)
695 fprintf (fh, "%s: %"PRIi64"\n", meta->name, meta->value_signed_int);
696 else if (meta->type == NM_TYPE_UNSIGNED_INT)
697 fprintf (fh, "%s: %"PRIu64"\n", meta->name, meta->value_unsigned_int);
698 else if (meta->type == NM_TYPE_DOUBLE)
699 fprintf (fh, "%s: %e\n", meta->name, meta->value_double);
700 else if (meta->type == NM_TYPE_BOOLEAN)
701 fprintf (fh, "%s: %s\n", meta->name,
702 meta->value_boolean ? "true" : "false");
705 fprintf (fh, "\n%s\n", n->message);
710 waitpid (pid, &status, 0);
712 DEBUG ("exec plugin: Child %i exited with status %i.",
715 plugin_notification_meta_free (n);
717 pthread_exit ((void *) 0);
719 } /* void *exec_notification_one }}} */
721 static int exec_init (void) /* {{{ */
725 memset (&sa, '\0', sizeof (sa));
726 sa.sa_handler = sigchld_handler;
727 sigaction (SIGCHLD, &sa, NULL);
730 } /* int exec_init }}} */
732 static int exec_read (void) /* {{{ */
736 for (pl = pl_head; pl != NULL; pl = pl->next)
741 /* Only execute `normal' style executables here. */
742 if ((pl->flags & PL_NORMAL) == 0)
745 pthread_mutex_lock (&pl_lock);
746 /* Skip if a child is already running. */
747 if ((pl->flags & PL_RUNNING) != 0)
749 pthread_mutex_unlock (&pl_lock);
752 pl->flags |= PL_RUNNING;
753 pthread_mutex_unlock (&pl_lock);
755 pthread_attr_init (&attr);
756 pthread_attr_setdetachstate (&attr, PTHREAD_CREATE_DETACHED);
757 pthread_create (&t, &attr, exec_read_one, (void *) pl);
761 } /* int exec_read }}} */
763 static int exec_notification (const notification_t *n)
766 program_list_and_notification_t *pln;
768 for (pl = pl_head; pl != NULL; pl = pl->next)
773 /* Only execute `notification' style executables here. */
774 if ((pl->flags & PL_NOTIF_ACTION) == 0)
777 /* Skip if a child is already running. */
781 pln = (program_list_and_notification_t *) malloc (sizeof
782 (program_list_and_notification_t));
785 ERROR ("exec plugin: malloc failed.");
790 memcpy (&pln->n, n, sizeof (notification_t));
792 /* Set the `meta' member to NULL, otherwise `plugin_notification_meta_copy'
793 * will run into an endless loop. */
795 plugin_notification_meta_copy (&pln->n, n);
797 pthread_attr_init (&attr);
798 pthread_attr_setdetachstate (&attr, PTHREAD_CREATE_DETACHED);
799 pthread_create (&t, &attr, exec_notification_one, (void *) pln);
803 } /* int exec_notification */
805 static int exec_shutdown (void) /* {{{ */
808 program_list_t *next;
817 kill (pl->pid, SIGTERM);
818 INFO ("exec plugin: Sent SIGTERM to %hu", (unsigned short int) pl->pid);
829 } /* int exec_shutdown }}} */
831 void module_register (void)
833 plugin_register_complex_config ("exec", exec_config);
834 plugin_register_init ("exec", exec_init);
835 plugin_register_read ("exec", exec_read);
836 plugin_register_notification ("exec", exec_notification);
837 plugin_register_shutdown ("exec", exec_shutdown);
838 } /* void module_register */
841 * vim:shiftwidth=2:softtabstop=2:tabstop=8:fdm=marker