2 * collectd - src/exec.c
3 * Copyright (C) 2007 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>
25 #include "utils_cmd_putval.h"
27 #include <sys/types.h>
36 struct program_list_s;
37 typedef struct program_list_s program_list_t;
49 static const char *config_keys[] =
53 static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
55 static program_list_t *pl_head = NULL;
60 static int exec_config (const char *key, const char *value)
62 if (strcasecmp ("Exec", key) == 0)
65 pl = (program_list_t *) malloc (sizeof (program_list_t));
68 memset (pl, '\0', sizeof (program_list_t));
70 pl->user = strdup (value);
77 pl->exec = strchr (pl->user, ' ');
84 while (*pl->exec == ' ')
90 if (*pl->exec == '\0')
106 } /* int exec_config */
108 static void exec_child (program_list_t *pl)
114 struct passwd *sp_ptr;
120 status = getpwnam_r (pl->user, &sp, pwnambuf, sizeof (pwnambuf), &sp_ptr);
123 ERROR ("exec plugin: getpwnam_r failed: %s",
124 sstrerror (errno, errbuf, sizeof (errbuf)));
129 ERROR ("exec plugin: No such user: `%s'", pl->user);
136 ERROR ("exec plugin: Cowardly refusing to exec program as root.");
140 status = setuid (uid);
143 ERROR ("exec plugin: setuid failed: %s",
144 sstrerror (errno, errbuf, sizeof (errbuf)));
148 arg0 = strrchr (pl->exec, '/');
151 if ((arg0 == NULL) || (*arg0 == '\0'))
154 status = execlp (pl->exec, arg0, (char *) 0);
156 ERROR ("exec plugin: exec failed: %s",
157 sstrerror (errno, errbuf, sizeof (errbuf)));
159 } /* void exec_child */
161 static int fork_child (program_list_t *pl)
169 status = pipe (fd_pipe);
173 ERROR ("exec plugin: pipe failed: %s",
174 sstrerror (errno, errbuf, sizeof (errbuf)));
182 ERROR ("exec plugin: fork failed: %s",
183 sstrerror (errno, errbuf, sizeof (errbuf)));
186 else if (pl->pid == 0)
190 /* Connect the pipe to STDOUT and STDERR */
191 if (fd_pipe[1] != STDOUT_FILENO)
192 dup2 (fd_pipe[1], STDOUT_FILENO);
193 if (fd_pipe[1] != STDERR_FILENO)
194 dup2 (fd_pipe[1], STDERR_FILENO);
195 if ((fd_pipe[1] != STDOUT_FILENO) && (fd_pipe[1] != STDERR_FILENO))
199 /* does not return */
204 } /* int fork_child */
206 static int parse_line (char *buffer)
211 fields[0] = "PUTVAL";
212 fields_num = strsplit (buffer, &fields[1], STATIC_ARRAY_SIZE(fields) - 1);
214 handle_putval (stdout, fields, fields_num + 1);
215 } /* int parse_line */
217 static void *exec_read_one (void *arg)
219 program_list_t *pl = (program_list_t *) arg;
224 fd = fork_child (pl);
226 pthread_exit ((void *) 1);
228 assert (pl->pid != 0);
230 fh = fdopen (fd, "r");
234 ERROR ("exec plugin: fdopen (%i) failed: %s", fd,
235 sstrerror (errno, errbuf, sizeof (errbuf)));
236 kill (pl->pid, SIGTERM);
238 pthread_exit ((void *) 1);
241 while (fgets (buffer, sizeof (buffer), fh) != NULL)
245 len = strlen (buffer);
247 /* Remove newline from end. */
248 while ((len > 0) && ((buffer[len - 1] == '\n')
249 || (buffer[len - 1] == '\r')))
250 buffer[--len] = '\0';
252 DEBUG ("exec plugin: exec_read_one: buffer = %s", buffer);
255 } /* while (fgets) */
260 pthread_exit ((void *) 0);
262 } /* void *exec_read_one */
264 static int exec_read (void)
268 for (pl = pl_head; pl != NULL; pl = pl->next)
276 pthread_attr_init (&attr);
277 pthread_attr_setdetachstate (&attr, PTHREAD_CREATE_DETACHED);
278 pthread_create (&t, &attr, exec_read_one, (void *) pl);
282 } /* int exec_read */
284 static int exec_shutdown (void)
287 program_list_t *next;
296 kill (pl->pid, SIGTERM);
297 INFO ("exec plugin: Sent SIGTERM to %hu", (unsigned short int) pl->pid);
308 } /* int exec_shutdown */
310 void module_register (void)
312 plugin_register_config ("exec", exec_config, config_keys, config_keys_num);
313 plugin_register_read ("exec", exec_read);
314 plugin_register_shutdown ("exec", exec_shutdown);
315 } /* void module_register */
318 * vim:shiftwidth=2:softtabstop=2:tabstop=8