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 strncpy (buffer, ci->values[1].value.string, sizeof (buffer));
175 strncpy (buffer, tmp + 1, sizeof (buffer));
176 buffer[sizeof (buffer) - 1] = '\0';
178 pl->argv[0] = strdup (buffer);
179 if (pl->argv[0] == NULL)
181 ERROR ("exec plugin: malloc failed.");
189 for (i = 1; i < (ci->values_num - 1); i++)
191 if (ci->values[i + 1].type == OCONFIG_TYPE_STRING)
193 pl->argv[i] = strdup (ci->values[i + 1].value.string);
197 if (ci->values[i + 1].type == OCONFIG_TYPE_NUMBER)
199 snprintf (buffer, sizeof (buffer), "%lf",
200 ci->values[i + 1].value.number);
204 if (ci->values[i + 1].value.boolean)
205 strncpy (buffer, "true", sizeof (buffer));
207 strncpy (buffer, "false", sizeof (buffer));
209 buffer[sizeof (buffer) - 1] = '\0';
211 pl->argv[i] = strdup (buffer);
214 if (pl->argv[i] == NULL)
216 ERROR ("exec plugin: strdup failed.");
221 if (i < (ci->values_num - 1))
234 for (i = 0; pl->argv[i] != NULL; i++)
236 DEBUG ("exec plugin: argv[%i] = %s", i, pl->argv[i]);
243 } /* int exec_config_exec }}} */
245 static int exec_config (oconfig_item_t *ci) /* {{{ */
249 for (i = 0; i < ci->children_num; i++)
251 oconfig_item_t *child = ci->children + i;
252 if ((strcasecmp ("Exec", child->key) == 0)
253 || (strcasecmp ("NotificationExec", child->key) == 0))
254 exec_config_exec (child);
257 WARNING ("exec plugin: Unknown config option `%s'.", child->key);
262 } /* int exec_config }}} */
264 static void exec_child (program_list_t *pl) /* {{{ */
271 struct passwd *sp_ptr;
277 status = getpwnam_r (pl->user, &sp, nambuf, sizeof (nambuf), &sp_ptr);
280 ERROR ("exec plugin: getpwnam_r failed: %s",
281 sstrerror (errno, errbuf, sizeof (errbuf)));
286 ERROR ("exec plugin: No such user: `%s'", pl->user);
294 ERROR ("exec plugin: Cowardly refusing to exec program as root.");
298 /* The group configured in the configfile is set as effective group, because
299 * this way the forked process can (re-)gain the user's primary group. */
301 if (NULL != pl->group)
303 if ('\0' != *pl->group) {
304 struct group *gr_ptr = NULL;
307 status = getgrnam_r (pl->group, &gr, nambuf, sizeof (nambuf), &gr_ptr);
310 ERROR ("exec plugin: getgrnam_r failed: %s",
311 sstrerror (errno, errbuf, sizeof (errbuf)));
316 ERROR ("exec plugin: No such group: `%s'", pl->group);
326 } /* if (pl->group == NULL) */
337 if ((gid != egid) && (egid != -1))
343 setgroups (glist_len, glist);
345 #endif /* HAVE_SETGROUPS */
347 status = setgid (gid);
350 ERROR ("exec plugin: setgid (%i) failed: %s",
351 gid, sstrerror (errno, errbuf, sizeof (errbuf)));
357 status = setegid (egid);
360 ERROR ("exec plugin: setegid (%i) failed: %s",
361 egid, sstrerror (errno, errbuf, sizeof (errbuf)));
366 status = setuid (uid);
369 ERROR ("exec plugin: setuid (%i) failed: %s",
370 uid, sstrerror (errno, errbuf, sizeof (errbuf)));
374 status = execvp (pl->exec, pl->argv);
376 ERROR ("exec plugin: exec failed: %s",
377 sstrerror (errno, errbuf, sizeof (errbuf)));
379 } /* void exec_child }}} */
382 * Creates two pipes (one for reading, ong for writing), forks a child, sets up
383 * the pipes so that fd_in is connected to STDIN of the child and fd_out is
384 * connected to STDOUT and STDERR of the child. Then is calls `exec_child'.
386 static int fork_child (program_list_t *pl, int *fd_in, int *fd_out) /* {{{ */
396 status = pipe (fd_pipe_in);
400 ERROR ("exec plugin: pipe failed: %s",
401 sstrerror (errno, errbuf, sizeof (errbuf)));
405 status = pipe (fd_pipe_out);
409 ERROR ("exec plugin: pipe failed: %s",
410 sstrerror (errno, errbuf, sizeof (errbuf)));
418 ERROR ("exec plugin: fork failed: %s",
419 sstrerror (errno, errbuf, sizeof (errbuf)));
427 /* Close all file descriptors but the pipe end we need. */
428 fd_num = getdtablesize ();
429 for (fd = 0; fd < fd_num; fd++)
431 if ((fd == fd_pipe_in[0]) || (fd == fd_pipe_out[1]))
436 /* If the `out' pipe has the filedescriptor STDIN we have to be careful
437 * with the `dup's below. So, if this is the case we have to handle the
438 * `out' pipe first. */
439 if (fd_pipe_out[1] == STDIN_FILENO)
441 int new_fileno = (fd_pipe_in[0] == STDOUT_FILENO)
442 ? STDERR_FILENO : STDOUT_FILENO;
443 dup2 (fd_pipe_out[1], new_fileno);
444 close (fd_pipe_out[1]);
445 fd_pipe_out[1] = new_fileno;
447 /* Now `fd_pipe_out[1]' is either `STDOUT' or `STDERR', but definitely not
450 /* Connect the `in' pipe to STDIN */
451 if (fd_pipe_in[0] != STDIN_FILENO)
453 dup2 (fd_pipe_in[0], STDIN_FILENO);
454 close (fd_pipe_in[0]);
455 fd_pipe_in[0] = STDIN_FILENO;
458 /* Now connect the `out' pipe to STDOUT and STDERR */
459 if (fd_pipe_out[1] != STDOUT_FILENO)
460 dup2 (fd_pipe_out[1], STDOUT_FILENO);
461 if (fd_pipe_out[1] != STDERR_FILENO)
462 dup2 (fd_pipe_out[1], STDERR_FILENO);
464 /* If the pipe has some FD that's something completely different, close it
466 if ((fd_pipe_out[1] != STDOUT_FILENO) && (fd_pipe_out[1] != STDERR_FILENO))
468 close (fd_pipe_out[1]);
469 fd_pipe_out[1] = STDOUT_FILENO;
473 /* does not return */
476 close (fd_pipe_in[0]);
477 close (fd_pipe_out[1]);
480 *fd_in = fd_pipe_in[1];
482 close (fd_pipe_in[1]);
485 *fd_out = fd_pipe_out[0];
487 close (fd_pipe_out[0]);
490 } /* int fork_child }}} */
492 static int parse_line (char *buffer) /* {{{ */
497 fields[0] = "PUTVAL";
498 fields_num = strsplit (buffer, fields + 1, STATIC_ARRAY_SIZE(fields) - 1);
500 if (strcasecmp (fields[1], "putval") == 0)
501 return (handle_putval (stdout, fields + 1, fields_num));
502 else if (strcasecmp (fields[1], "putnotif") == 0)
503 return (handle_putnotif (stdout, fields + 1, fields_num));
505 /* compatibility code */
506 return (handle_putval (stdout, fields, fields_num + 1));
507 } /* int parse_line }}} */
509 static void *exec_read_one (void *arg) /* {{{ */
511 program_list_t *pl = (program_list_t *) arg;
517 status = fork_child (pl, NULL, &fd);
519 pthread_exit ((void *) 1);
522 assert (pl->pid != 0);
524 fh = fdopen (fd, "r");
528 ERROR ("exec plugin: fdopen (%i) failed: %s", fd,
529 sstrerror (errno, errbuf, sizeof (errbuf)));
530 kill (pl->pid, SIGTERM);
533 pthread_exit ((void *) 1);
537 while (fgets (buffer, sizeof (buffer), fh) != NULL)
541 len = strlen (buffer);
543 /* Remove newline from end. */
544 while ((len > 0) && ((buffer[len - 1] == '\n')
545 || (buffer[len - 1] == '\r')))
546 buffer[--len] = '\0';
548 DEBUG ("exec plugin: exec_read_one: buffer = %s", buffer);
551 } /* while (fgets) */
555 if (waitpid (pl->pid, &status, 0) > 0)
558 DEBUG ("exec plugin: Child %i exited with status %i.",
559 (int) pl->pid, pl->status);
563 pthread_mutex_lock (&pl_lock);
564 pl->flags &= ~PL_RUNNING;
565 pthread_mutex_unlock (&pl_lock);
567 pthread_exit ((void *) 0);
569 } /* void *exec_read_one }}} */
571 static void *exec_notification_one (void *arg) /* {{{ */
573 program_list_t *pl = ((program_list_and_notification_t *) arg)->pl;
574 const notification_t *n = &((program_list_and_notification_t *) arg)->n;
579 const char *severity;
581 pid = fork_child (pl, &fd, NULL);
584 pthread_exit ((void *) 1);
587 fh = fdopen (fd, "w");
591 ERROR ("exec plugin: fdopen (%i) failed: %s", fd,
592 sstrerror (errno, errbuf, sizeof (errbuf)));
593 kill (pl->pid, SIGTERM);
597 pthread_exit ((void *) 1);
600 severity = "FAILURE";
601 if (n->severity == NOTIF_WARNING)
602 severity = "WARNING";
603 else if (n->severity == NOTIF_OKAY)
609 severity, (unsigned int) n->time);
611 /* Print the optional fields */
612 if (strlen (n->host) > 0)
613 fprintf (fh, "Host: %s\n", n->host);
614 if (strlen (n->plugin) > 0)
615 fprintf (fh, "Plugin: %s\n", n->plugin);
616 if (strlen (n->plugin_instance) > 0)
617 fprintf (fh, "PluginInstance: %s\n", n->plugin_instance);
618 if (strlen (n->type) > 0)
619 fprintf (fh, "Type: %s\n", n->type);
620 if (strlen (n->type_instance) > 0)
621 fprintf (fh, "TypeInstance: %s\n", n->type_instance);
623 fprintf (fh, "\n%s\n", n->message);
628 waitpid (pid, &status, 0);
630 DEBUG ("exec plugin: Child %i exited with status %i.",
634 pthread_exit ((void *) 0);
636 } /* void *exec_notification_one }}} */
638 static int exec_init (void) /* {{{ */
642 memset (&sa, '\0', sizeof (sa));
643 sa.sa_handler = sigchld_handler;
644 sigaction (SIGCHLD, &sa, NULL);
647 } /* int exec_init }}} */
649 static int exec_read (void) /* {{{ */
653 for (pl = pl_head; pl != NULL; pl = pl->next)
658 /* Only execute `normal' style executables here. */
659 if ((pl->flags & PL_NORMAL) == 0)
662 pthread_mutex_lock (&pl_lock);
663 /* Skip if a child is already running. */
664 if ((pl->flags & PL_RUNNING) != 0)
666 pthread_mutex_unlock (&pl_lock);
669 pl->flags |= PL_RUNNING;
670 pthread_mutex_unlock (&pl_lock);
672 pthread_attr_init (&attr);
673 pthread_attr_setdetachstate (&attr, PTHREAD_CREATE_DETACHED);
674 pthread_create (&t, &attr, exec_read_one, (void *) pl);
678 } /* int exec_read }}} */
680 static int exec_notification (const notification_t *n)
683 program_list_and_notification_t *pln;
685 for (pl = pl_head; pl != NULL; pl = pl->next)
690 /* Only execute `notification' style executables here. */
691 if ((pl->flags & PL_NOTIF_ACTION) == 0)
694 /* Skip if a child is already running. */
698 pln = (program_list_and_notification_t *) malloc (sizeof
699 (program_list_and_notification_t));
702 ERROR ("exec plugin: malloc failed.");
707 memcpy (&pln->n, n, sizeof (notification_t));
709 pthread_attr_init (&attr);
710 pthread_attr_setdetachstate (&attr, PTHREAD_CREATE_DETACHED);
711 pthread_create (&t, &attr, exec_notification_one, (void *) pln);
715 } /* int exec_notification */
717 static int exec_shutdown (void) /* {{{ */
720 program_list_t *next;
729 kill (pl->pid, SIGTERM);
730 INFO ("exec plugin: Sent SIGTERM to %hu", (unsigned short int) pl->pid);
741 } /* int exec_shutdown }}} */
743 void module_register (void)
745 plugin_register_complex_config ("exec", exec_config);
746 plugin_register_init ("exec", exec_init);
747 plugin_register_read ("exec", exec_read);
748 plugin_register_notification ("exec", exec_notification);
749 plugin_register_shutdown ("exec", exec_shutdown);
750 } /* void module_register */
753 * vim:shiftwidth=2:softtabstop=2:tabstop=8:fdm=marker