--- a/src/exec.c 2014-05-22 13:55:45.607883583 +0200 +++ b/src/exec.c 2014-05-22 16:11:16.759364628 +0200 @@ -84,6 +84,23 @@ static pthread_mutex_t pl_lock = PTHREAD /* * Functions */ +static void free_pl(program_list_t *pl) +{ + /* free memory in children */ + program_list_t *next; + while (pl != NULL) { + int i; + next = pl->next; + for (i=0; pl->argv && pl->argv[i]; i++) + sfree (pl->argv[i]); + sfree (pl->argv); + sfree (pl->exec); + sfree (pl->user); + sfree (pl); + pl = next; + } /* while (pl) */ +} + static void sigchld_handler (int __attribute__((unused)) signal) /* {{{ */ { pid_t pid; @@ -142,7 +159,7 @@ static int exec_config_exec (oconfig_ite if (pl->user == NULL) { ERROR ("exec plugin: strdup failed."); - sfree (pl); + free_pl(pl); return (-1); } @@ -157,8 +174,7 @@ static int exec_config_exec (oconfig_ite if (pl->exec == NULL) { ERROR ("exec plugin: strdup failed."); - sfree (pl->user); - sfree (pl); + free_pl(pl); return (-1); } @@ -166,9 +182,7 @@ static int exec_config_exec (oconfig_ite if (pl->argv == NULL) { ERROR ("exec plugin: malloc failed."); - sfree (pl->exec); - sfree (pl->user); - sfree (pl); + free_pl(pl); return (-1); } memset (pl->argv, '\0', ci->values_num * sizeof (char *)); @@ -184,10 +198,7 @@ static int exec_config_exec (oconfig_ite if (pl->argv[0] == NULL) { ERROR ("exec plugin: malloc failed."); - sfree (pl->argv); - sfree (pl->exec); - sfree (pl->user); - sfree (pl); + free_pl(pl); return (-1); } @@ -224,14 +235,7 @@ static int exec_config_exec (oconfig_ite if (i < (ci->values_num - 1)) { - while ((--i) >= 0) - { - sfree (pl->argv[i]); - } - sfree (pl->argv); - sfree (pl->exec); - sfree (pl->user); - sfree (pl); + free_pl(pl); return (-1); } @@ -427,7 +431,7 @@ static int fork_child (program_list_t *p int status; int pid; - if (pl->pid != 0) + if (!pl || pl->pid != 0) return (-1); status = pipe (fd_pipe_in); @@ -466,6 +470,9 @@ static int fork_child (program_list_t *p int fd_num; int fd; + /* free memory in children */ + free_pl(pl_head); + /* Close all file descriptors but the pipe end we need. */ fd_num = getdtablesize (); for (fd = 0; fd < fd_num; fd++) @@ -552,10 +559,6 @@ static void *exec_read_one (void *arg) / int fd, fd_err, highest_fd; fd_set fdset, copy; int status; - char buffer[1200]; /* if not completely read */ - char buffer_err[1024]; - char *pbuffer = buffer; - char *pbuffer_err = buffer_err; status = fork_child (pl, NULL, &fd, &fd_err); if (status < 0) @@ -564,6 +567,7 @@ static void *exec_read_one (void *arg) / pthread_mutex_lock (&pl_lock); pl->flags &= ~PL_RUNNING; pthread_mutex_unlock (&pl_lock); + free_pl (arg); pthread_exit ((void *) 1); } pl->pid = status; @@ -583,6 +587,10 @@ static void *exec_read_one (void *arg) / while (1) { int len; + char buffer[1200]; /* if not completely read */ + char buffer_err[1024]; + char *pbuffer = buffer; + char *pbuffer_err = buffer_err; status = select (highest_fd + 1, ©, NULL, NULL, NULL); if (status < 0) @@ -719,7 +727,7 @@ static void *exec_notification_one (void pid = fork_child (pl, &fd, NULL, NULL); if (pid < 0) { - sfree (arg); + free_pl (arg); pthread_exit ((void *) 1); } @@ -732,7 +740,7 @@ static void *exec_notification_one (void kill (pl->pid, SIGTERM); pl->pid = 0; close (fd); - sfree (arg); + free_pl (arg); pthread_exit ((void *) 1); } @@ -787,7 +795,7 @@ static void *exec_notification_one (void if (n->meta != NULL) plugin_notification_meta_free (n->meta); n->meta = NULL; - sfree (arg); + free_pl (arg); pthread_exit ((void *) 0); return (NULL); } /* void *exec_notification_one }}} */ @@ -895,11 +903,9 @@ static int exec_shutdown (void) /* {{{ * INFO ("exec plugin: Sent SIGTERM to %hu", (unsigned short int) pl->pid); } - sfree (pl->user); - sfree (pl); - pl = next; } /* while (pl) */ + free_pl(pl_head); pl_head = NULL; return (0);