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>
26 #include <sys/types.h>
35 struct program_list_s;
36 typedef struct program_list_s program_list_t;
48 static const char *config_keys[] =
52 static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
54 static program_list_t *pl_head = NULL;
59 static int exec_config (const char *key, const char *value)
61 if (strcasecmp ("Exec", key) == 0)
64 pl = (program_list_t *) malloc (sizeof (program_list_t));
67 memset (pl, '\0', sizeof (program_list_t));
69 pl->user = strdup (value);
76 pl->exec = strchr (pl->user, ' ');
83 while (*pl->exec == ' ')
89 if (*pl->exec == '\0')
105 } /* int exec_config */
107 static void exec_child (program_list_t *pl)
113 struct passwd *sp_ptr;
119 status = getpwnam_r (pl->user, &sp, pwnambuf, sizeof (pwnambuf), &sp_ptr);
122 ERROR ("exec plugin: getpwnam_r failed: %s",
123 sstrerror (errno, errbuf, sizeof (errbuf)));
128 ERROR ("exec plugin: No such user: `%s'", pl->user);
135 ERROR ("exec plugin: Cowardly refusing to exec program as root.");
139 status = setuid (uid);
142 ERROR ("exec plugin: setuid failed: %s",
143 sstrerror (errno, errbuf, sizeof (errbuf)));
147 arg0 = strrchr (pl->exec, '/');
150 if ((arg0 == NULL) || (*arg0 == '\0'))
153 status = execlp (pl->exec, arg0, (char *) 0);
155 ERROR ("exec plugin: exec failed: %s",
156 sstrerror (errno, errbuf, sizeof (errbuf)));
158 } /* void exec_child */
160 static int fork_child (program_list_t *pl)
168 status = pipe (fd_pipe);
172 ERROR ("exec plugin: pipe failed: %s",
173 sstrerror (errno, errbuf, sizeof (errbuf)));
181 ERROR ("exec plugin: fork failed: %s",
182 sstrerror (errno, errbuf, sizeof (errbuf)));
185 else if (pl->pid == 0)
189 /* Connect the pipe to STDOUT and STDERR */
190 if (fd_pipe[1] != STDOUT_FILENO)
191 dup2 (fd_pipe[1], STDOUT_FILENO);
192 if (fd_pipe[1] != STDERR_FILENO)
193 dup2 (fd_pipe[1], STDERR_FILENO);
194 if ((fd_pipe[1] != STDOUT_FILENO) && (fd_pipe[1] != STDERR_FILENO))
198 /* does not return */
203 } /* int fork_child */
205 static int parse_line (char *buffer)
212 char *plugin_instance;
216 const data_set_t *ds;
217 value_list_t vl = VALUE_LIST_INIT;
221 fields_num = strsplit (buffer, fields, 4);
224 WARNING ("exec plugin: Number of fields is not 2.");
228 status = parse_identifier (fields[0], &hostname,
229 &plugin, &plugin_instance,
230 &type, &type_instance);
233 WARNING ("exec plugin: Cannot parse `%s'", fields[0]);
237 if ((strlen (hostname) >= sizeof (vl.host))
238 || (strlen (plugin) >= sizeof (vl.plugin))
239 || ((plugin_instance != NULL)
240 && (strlen (plugin_instance) >= sizeof (vl.plugin_instance)))
241 || ((type_instance != NULL)
242 && (strlen (type_instance) >= sizeof (vl.type_instance))))
244 WARNING ("exec plugin: An identifier is too long.");
248 strcpy (vl.host, hostname);
249 strcpy (vl.plugin, plugin);
250 if (plugin_instance != NULL)
251 strcpy (vl.plugin_instance, plugin_instance);
252 if (type_instance != NULL)
253 strcpy (vl.type_instance, type_instance);
255 ds = plugin_get_ds (type);
258 WARNING ("exec plugin: No such type: `%s'", type);
262 vl.values_len = ds->ds_num;
263 vl.values = (value_t *) malloc (sizeof (value_t) * vl.values_len);
264 if (vl.values == NULL)
267 /* Sets vl.values and vl.time */
268 status = parse_values (fields[1], &vl, ds);
271 WARNING ("exec plugin: Cannot parse `%s'", fields[1]);
276 plugin_dispatch_values (type, &vl);
281 } /* int parse_line */
283 static void *exec_read_one (void *arg)
285 program_list_t *pl = (program_list_t *) arg;
290 fd = fork_child (pl);
292 pthread_exit ((void *) 1);
294 assert (pl->pid != 0);
296 fh = fdopen (fd, "r");
300 ERROR ("exec plugin: fdopen (%i) failed: %s", fd,
301 sstrerror (errno, errbuf, sizeof (errbuf)));
302 kill (pl->pid, SIGTERM);
304 pthread_exit ((void *) 1);
307 while (fgets (buffer, sizeof (buffer), fh) != NULL)
311 len = strlen (buffer);
313 /* Remove newline from end. */
314 while ((len > 0) && ((buffer[len - 1] == '\n')
315 || (buffer[len - 1] == '\r')))
316 buffer[--len] = '\0';
318 DEBUG ("exec plugin: exec_read_one: buffer = %s", buffer);
321 } /* while (fgets) */
326 pthread_exit ((void *) 0);
328 } /* void *exec_read_one */
330 static int exec_read (void)
334 for (pl = pl_head; pl != NULL; pl = pl->next)
342 pthread_attr_init (&attr);
343 pthread_attr_setdetachstate (&attr, PTHREAD_CREATE_DETACHED);
344 pthread_create (&t, &attr, exec_read_one, (void *) pl);
348 } /* int exec_read */
350 static int exec_shutdown (void)
353 program_list_t *next;
362 kill (pl->pid, SIGTERM);
363 INFO ("exec plugin: Sent SIGTERM to %hu", (unsigned short int) pl->pid);
374 } /* int exec_shutdown */
376 void module_register (void)
378 plugin_register_config ("exec", exec_config, config_keys, config_keys_num);
379 plugin_register_read ("exec", exec_read);
380 plugin_register_shutdown ("exec", exec_shutdown);
381 } /* void module_register */
384 * vim:shiftwidth=2:softtabstop=2:tabstop=8