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) /* {{{ */
496 fields[0] = "PUTVAL";
497 fields_num = strsplit (buffer, fields + 1, STATIC_ARRAY_SIZE(fields) - 1);
499 if (strcasecmp (fields[1], "putval") == 0)
500 return (handle_putval (stdout, fields + 1, fields_num));
501 else if (strcasecmp (fields[1], "putnotif") == 0)
502 return (handle_putnotif (stdout, fields + 1, fields_num));
504 /* compatibility code */
505 return (handle_putval (stdout, fields, fields_num + 1));
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 const notification_t *n = &((program_list_and_notification_t *) arg)->n;
645 const char *severity;
647 pid = fork_child (pl, &fd, NULL, NULL);
650 pthread_exit ((void *) 1);
653 fh = fdopen (fd, "w");
657 ERROR ("exec plugin: fdopen (%i) failed: %s", fd,
658 sstrerror (errno, errbuf, sizeof (errbuf)));
659 kill (pl->pid, SIGTERM);
663 pthread_exit ((void *) 1);
666 severity = "FAILURE";
667 if (n->severity == NOTIF_WARNING)
668 severity = "WARNING";
669 else if (n->severity == NOTIF_OKAY)
675 severity, (unsigned int) n->time);
677 /* Print the optional fields */
678 if (strlen (n->host) > 0)
679 fprintf (fh, "Host: %s\n", n->host);
680 if (strlen (n->plugin) > 0)
681 fprintf (fh, "Plugin: %s\n", n->plugin);
682 if (strlen (n->plugin_instance) > 0)
683 fprintf (fh, "PluginInstance: %s\n", n->plugin_instance);
684 if (strlen (n->type) > 0)
685 fprintf (fh, "Type: %s\n", n->type);
686 if (strlen (n->type_instance) > 0)
687 fprintf (fh, "TypeInstance: %s\n", n->type_instance);
689 fprintf (fh, "\n%s\n", n->message);
694 waitpid (pid, &status, 0);
696 DEBUG ("exec plugin: Child %i exited with status %i.",
700 pthread_exit ((void *) 0);
702 } /* void *exec_notification_one }}} */
704 static int exec_init (void) /* {{{ */
708 memset (&sa, '\0', sizeof (sa));
709 sa.sa_handler = sigchld_handler;
710 sigaction (SIGCHLD, &sa, NULL);
713 } /* int exec_init }}} */
715 static int exec_read (void) /* {{{ */
719 for (pl = pl_head; pl != NULL; pl = pl->next)
724 /* Only execute `normal' style executables here. */
725 if ((pl->flags & PL_NORMAL) == 0)
728 pthread_mutex_lock (&pl_lock);
729 /* Skip if a child is already running. */
730 if ((pl->flags & PL_RUNNING) != 0)
732 pthread_mutex_unlock (&pl_lock);
735 pl->flags |= PL_RUNNING;
736 pthread_mutex_unlock (&pl_lock);
738 pthread_attr_init (&attr);
739 pthread_attr_setdetachstate (&attr, PTHREAD_CREATE_DETACHED);
740 pthread_create (&t, &attr, exec_read_one, (void *) pl);
744 } /* int exec_read }}} */
746 static int exec_notification (const notification_t *n)
749 program_list_and_notification_t *pln;
751 for (pl = pl_head; pl != NULL; pl = pl->next)
756 /* Only execute `notification' style executables here. */
757 if ((pl->flags & PL_NOTIF_ACTION) == 0)
760 /* Skip if a child is already running. */
764 pln = (program_list_and_notification_t *) malloc (sizeof
765 (program_list_and_notification_t));
768 ERROR ("exec plugin: malloc failed.");
773 memcpy (&pln->n, n, sizeof (notification_t));
775 pthread_attr_init (&attr);
776 pthread_attr_setdetachstate (&attr, PTHREAD_CREATE_DETACHED);
777 pthread_create (&t, &attr, exec_notification_one, (void *) pln);
781 } /* int exec_notification */
783 static int exec_shutdown (void) /* {{{ */
786 program_list_t *next;
795 kill (pl->pid, SIGTERM);
796 INFO ("exec plugin: Sent SIGTERM to %hu", (unsigned short int) pl->pid);
807 } /* int exec_shutdown }}} */
809 void module_register (void)
811 plugin_register_complex_config ("exec", exec_config);
812 plugin_register_init ("exec", exec_init);
813 plugin_register_read ("exec", exec_read);
814 plugin_register_notification ("exec", exec_notification);
815 plugin_register_shutdown ("exec", exec_shutdown);
816 } /* void module_register */
819 * vim:shiftwidth=2:softtabstop=2:tabstop=8:fdm=marker