X-Git-Url: https://git.octo.it/?a=blobdiff_plain;f=src%2Fexec.c;h=00b9c7511de57d8d2fe54dfeb81f2b21b8816f38;hb=34749e7ebe508b1d563af287b180e951b7822bcd;hp=d726f708987540102a3056422799db66d608dff9;hpb=14b7c735bf93b5a6260a0e064bccc28dc7581c7f;p=collectd.git diff --git a/src/exec.c b/src/exec.c index d726f708..00b9c751 100644 --- a/src/exec.c +++ b/src/exec.c @@ -22,13 +22,19 @@ #include "collectd.h" #include "common.h" #include "plugin.h" +#include "utils_cmd_putval.h" #include #include +#include #include #include +#define PL_NORMAL 0x01 +#define PL_NOTIF_ACTION 0x02 +#define PL_NAGIOS_PLUGIN 0x04 + /* * Private data types */ @@ -37,86 +43,223 @@ typedef struct program_list_s program_list_t; struct program_list_s { char *user; + char *group; char *exec; + char **argv; int pid; + int status; + int flags; program_list_t *next; }; /* * Private variables */ -static const char *config_keys[] = -{ - "Exec" -}; -static int config_keys_num = STATIC_ARRAY_SIZE (config_keys); - static program_list_t *pl_head = NULL; /* * Functions */ -static int exec_config (const char *key, const char *value) +static void sigchld_handler (int signal) /* {{{ */ { - if (strcasecmp ("Exec", key) == 0) + pid_t pid; + int status; + while ((pid = waitpid (-1, &status, WNOHANG)) > 0) { program_list_t *pl; - pl = (program_list_t *) malloc (sizeof (program_list_t)); - if (pl == NULL) - return (1); - memset (pl, '\0', sizeof (program_list_t)); + for (pl = pl_head; pl != NULL; pl = pl->next) + if (pl->pid == pid) + break; + if (pl != NULL) + pl->status = status; + } /* while (waitpid) */ +} /* void sigchld_handler }}} */ + +static int exec_config_exec (oconfig_item_t *ci) /* {{{ */ +{ + program_list_t *pl; + char buffer[128]; + int i; - pl->user = strdup (value); - if (pl->user == NULL) - { - sfree (pl); - return (1); - } + if (ci->children_num != 0) + { + WARNING ("exec plugin: The config option `%s' may not be a block.", + ci->key); + return (-1); + } + if (ci->values_num < 2) + { + WARNING ("exec plugin: The config option `%s' needs at least two " + "arguments.", ci->key); + return (-1); + } + if ((ci->values[0].type != OCONFIG_TYPE_STRING) + || (ci->values[1].type != OCONFIG_TYPE_STRING)) + { + WARNING ("exec plugin: The first two arguments to the `%s' option must " + "be string arguments.", ci->key); + return (-1); + } + + pl = (program_list_t *) malloc (sizeof (program_list_t)); + if (pl == NULL) + { + ERROR ("exec plugin: malloc failed."); + return (-1); + } + memset (pl, '\0', sizeof (program_list_t)); + + if (strcasecmp ("NagiosExec", ci->key) == 0) + pl->flags |= PL_NAGIOS_PLUGIN; + else if (strcasecmp ("NotificationExec", ci->key) == 0) + pl->flags |= PL_NOTIF_ACTION; + else + pl->flags |= PL_NORMAL; + + pl->user = strdup (ci->values[0].value.string); + if (pl->user == NULL) + { + ERROR ("exec plugin: strdup failed."); + sfree (pl); + return (-1); + } + + pl->group = strchr (pl->user, ':'); + if (pl->group != NULL) + { + *pl->group = '\0'; + pl->group++; + } + + pl->exec = strdup (ci->values[1].value.string); + if (pl->exec == NULL) + { + ERROR ("exec plugin: strdup failed."); + sfree (pl->user); + sfree (pl); + return (-1); + } + + pl->argv = (char **) malloc (ci->values_num * sizeof (char *)); + if (pl->argv == NULL) + { + ERROR ("exec plugin: malloc failed."); + sfree (pl->exec); + sfree (pl->user); + sfree (pl); + return (-1); + } + memset (pl->argv, '\0', ci->values_num * sizeof (char *)); + + { + char *tmp = strrchr (ci->values[1].value.string, '/'); + if (tmp == NULL) + strncpy (buffer, ci->values[1].value.string, sizeof (buffer)); + else + strncpy (buffer, tmp + 1, sizeof (buffer)); + buffer[sizeof (buffer) - 1] = '\0'; + } + pl->argv[0] = strdup (buffer); + if (pl->argv[0] == NULL) + { + ERROR ("exec plugin: malloc failed."); + sfree (pl->argv); + sfree (pl->exec); + sfree (pl->user); + sfree (pl); + return (-1); + } - pl->exec = strchr (pl->user, ' '); - if (pl->exec == NULL) + for (i = 1; i < (ci->values_num - 1); i++) + { + if (ci->values[i + 1].type == OCONFIG_TYPE_STRING) { - sfree (pl->user); - sfree (pl); - return (1); + pl->argv[i] = strdup (ci->values[i + 1].value.string); } - while (*pl->exec == ' ') + else { - *pl->exec = '\0'; - pl->exec++; + if (ci->values[i + 1].type == OCONFIG_TYPE_NUMBER) + { + snprintf (buffer, sizeof (buffer), "%lf", + ci->values[i + 1].value.number); + } + else + { + if (ci->values[i + 1].value.boolean) + strncpy (buffer, "true", sizeof (buffer)); + else + strncpy (buffer, "false", sizeof (buffer)); + } + buffer[sizeof (buffer) - 1] = '\0'; + + pl->argv[i] = strdup (buffer); } - if (*pl->exec == '\0') + if (pl->argv[i] == NULL) { - sfree (pl->user); - sfree (pl); - return (1); + ERROR ("exec plugin: strdup failed."); + break; } + } /* for (i) */ - pl->next = pl_head; - pl_head = pl; - } - else + if (i < (ci->values_num - 1)) { + while ((--i) >= 0) + { + sfree (pl->argv[i]); + } + sfree (pl->argv); + sfree (pl->exec); + sfree (pl->user); + sfree (pl); return (-1); } + for (i = 0; pl->argv[i] != NULL; i++) + { + DEBUG ("exec plugin: argv[%i] = %s", i, pl->argv[i]); + } + + pl->next = pl_head; + pl_head = pl; + + return (0); +} /* int exec_config_exec }}} */ + +static int exec_config (oconfig_item_t *ci) /* {{{ */ +{ + int i; + + for (i = 0; i < ci->children_num; i++) + { + oconfig_item_t *child = ci->children + i; + if ((strcasecmp ("Exec", child->key) == 0) + || (strcasecmp ("NagiosExec", child->key) == 0) + || (strcasecmp ("NotificationExec", child->key) == 0)) + exec_config_exec (child); + else + { + WARNING ("exec plugin: Unknown config option `%s'.", child->key); + } + } /* for (i) */ + return (0); -} /* int exec_config */ +} /* int exec_config }}} */ -static void exec_child (program_list_t *pl) +static void exec_child (program_list_t *pl) /* {{{ */ { int status; int uid; - char *arg0; + int gid; + int egid; struct passwd *sp_ptr; struct passwd sp; - char pwnambuf[2048]; + char nambuf[2048]; char errbuf[1024]; sp_ptr = NULL; - status = getpwnam_r (pl->user, &sp, pwnambuf, sizeof (pwnambuf), &sp_ptr); + status = getpwnam_r (pl->user, &sp, nambuf, sizeof (nambuf), &sp_ptr); if (status != 0) { ERROR ("exec plugin: getpwnam_r failed: %s", @@ -130,34 +273,78 @@ static void exec_child (program_list_t *pl) } uid = sp.pw_uid; + gid = sp.pw_gid; if (uid == 0) { ERROR ("exec plugin: Cowardly refusing to exec program as root."); exit (-1); } - status = setuid (uid); + /* 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: getgrnam_r failed: %s", + 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) */ + + status = setgid (gid); if (status != 0) { - ERROR ("exec plugin: setuid failed: %s", - sstrerror (errno, errbuf, sizeof (errbuf))); + ERROR ("exec plugin: setgid (%i) failed: %s", + gid, sstrerror (errno, errbuf, sizeof (errbuf))); exit (-1); } - arg0 = strrchr (pl->exec, '/'); - if (arg0 != NULL) - arg0++; - if ((arg0 == NULL) || (*arg0 == '\0')) - arg0 = pl->exec; + if (egid != -1) + { + status = setegid (egid); + if (status != 0) + { + ERROR ("exec plugin: setegid (%i) failed: %s", + egid, sstrerror (errno, errbuf, sizeof (errbuf))); + exit (-1); + } + } + + status = setuid (uid); + if (status != 0) + { + ERROR ("exec plugin: setuid (%i) failed: %s", + uid, sstrerror (errno, errbuf, sizeof (errbuf))); + exit (-1); + } - status = execlp (pl->exec, arg0, (char *) 0); + status = execvp (pl->exec, pl->argv); ERROR ("exec plugin: exec failed: %s", sstrerror (errno, errbuf, sizeof (errbuf))); exit (-1); -} /* void exec_child */ +} /* void exec_child }}} */ -static int fork_child (program_list_t *pl) +static int fork_child (program_list_t *pl) /* {{{ */ { int fd_pipe[2]; int status; @@ -200,92 +387,27 @@ static int fork_child (program_list_t *pl) close (fd_pipe[1]); return (fd_pipe[0]); -} /* int fork_child */ +} /* int fork_child }}} */ -static int parse_line (char *buffer) +static int parse_line (char *buffer) /* {{{ */ { - char *fields[4]; + char *fields[256]; int fields_num; - char *hostname; - char *plugin; - char *plugin_instance; - char *type; - char *type_instance; - - const data_set_t *ds; - value_list_t vl = VALUE_LIST_INIT; - - int status; - - fields_num = strsplit (buffer, fields, 4); - if (fields_num != 2) - { - WARNING ("exec plugin: Number of fields is not 2."); - return (-1); - } - - status = parse_identifier (fields[0], &hostname, - &plugin, &plugin_instance, - &type, &type_instance); - if (status != 0) - { - WARNING ("exec plugin: Cannot parse `%s'", fields[0]); - return (-1); - } - - if ((strlen (hostname) >= sizeof (vl.host)) - || (strlen (plugin) >= sizeof (vl.plugin)) - || ((plugin_instance != NULL) - && (strlen (plugin_instance) >= sizeof (vl.plugin_instance))) - || ((type_instance != NULL) - && (strlen (type_instance) >= sizeof (vl.type_instance)))) - { - WARNING ("exec plugin: An identifier is too long."); - return (-1); - } - - strcpy (vl.host, hostname); - strcpy (vl.plugin, plugin); - if (plugin_instance != NULL) - strcpy (vl.plugin_instance, plugin_instance); - if (type_instance != NULL) - strcpy (vl.type_instance, type_instance); - - ds = plugin_get_ds (type); - if (ds == NULL) - { - WARNING ("exec plugin: No such type: `%s'", type); - return (-1); - } - - vl.values_len = ds->ds_num; - vl.values = (value_t *) malloc (sizeof (value_t) * vl.values_len); - if (vl.values == NULL) - return (-1); + fields[0] = "PUTVAL"; + fields_num = strsplit (buffer, &fields[1], STATIC_ARRAY_SIZE(fields) - 1); - /* Sets vl.values and vl.time */ - status = parse_values (fields[1], &vl, ds); - if (status != 0) - { - WARNING ("exec plugin: Cannot parse `%s'", fields[1]); - sfree (vl.values); - return (-1); - } - - plugin_dispatch_values (type, &vl); - - sfree (vl.values); - + handle_putval (stdout, fields, fields_num + 1); return (0); -} /* int parse_line */ +} /* int parse_line }}} */ -static void *exec_read_one (void *arg) +static void *exec_read_one (void *arg) /* {{{ */ { program_list_t *pl = (program_list_t *) arg; int fd; FILE *fh; char buffer[1024]; + int status; fd = fork_child (pl); if (fd < 0) @@ -300,10 +422,12 @@ static void *exec_read_one (void *arg) ERROR ("exec plugin: fdopen (%i) failed: %s", fd, sstrerror (errno, errbuf, sizeof (errbuf))); kill (pl->pid, SIGTERM); + pl->pid = 0; close (fd); pthread_exit ((void *) 1); } + buffer[0] = '\0'; while (fgets (buffer, sizeof (buffer), fh) != NULL) { int len; @@ -317,17 +441,60 @@ static void *exec_read_one (void *arg) DEBUG ("exec plugin: exec_read_one: buffer = %s", buffer); + if (pl->flags & PL_NAGIOS_PLUGIN) + break; + parse_line (buffer); } /* while (fgets) */ fclose (fh); - pl->pid = 0; + if (waitpid (pl->pid, &status, 0) > 0) + pl->status = status; + + DEBUG ("exec plugin: Child %i exited with status %i.", + (int) pl->pid, pl->status); + + if (pl->flags & PL_NAGIOS_PLUGIN) + { + notification_t n; + + memset (&n, '\0', sizeof (n)); + + n.severity = NOTIF_FAILURE; + if (pl->status == 0) + n.severity = NOTIF_OKAY; + else if (pl->status == 1) + n.severity = NOTIF_WARNING; + + strncpy (n.message, buffer, sizeof (n.message)); + n.message[sizeof (n.message) - 1] = '\0'; + + n.time = time (NULL); + + strncpy (n.host, hostname_g, sizeof (n.host)); + n.host[sizeof (n.host) - 1] = '\0'; + + plugin_dispatch_notification (&n); + } + + pl->pid = 0; pthread_exit ((void *) 0); return (NULL); -} /* void *exec_read_one */ +} /* void *exec_read_one }}} */ + +static int exec_init (void) /* {{{ */ +{ + struct sigaction sa; + + memset (&sa, '\0', sizeof (sa)); + sa.sa_handler = sigchld_handler; + sigaction (SIGCHLD, &sa, NULL); + + return (0); +} /* int exec_init }}} */ -static int exec_read (void) +static int exec_read (void) /* {{{ */ { program_list_t *pl; @@ -345,9 +512,9 @@ static int exec_read (void) } /* for (pl) */ return (0); -} /* int exec_read */ +} /* int exec_read }}} */ -static int exec_shutdown (void) +static int exec_shutdown (void) /* {{{ */ { program_list_t *pl; program_list_t *next; @@ -371,15 +538,16 @@ static int exec_shutdown (void) pl_head = NULL; return (0); -} /* int exec_shutdown */ +} /* int exec_shutdown }}} */ void module_register (void) { - plugin_register_config ("exec", exec_config, config_keys, config_keys_num); + plugin_register_complex_config ("exec", exec_config); + plugin_register_init ("exec", exec_init); plugin_register_read ("exec", exec_read); plugin_register_shutdown ("exec", exec_shutdown); } /* void module_register */ /* - * vim:shiftwidth=2:softtabstop=2:tabstop=8 + * vim:shiftwidth=2:softtabstop=2:tabstop=8:fdm=marker */