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>
37 struct program_list_s;
38 typedef struct program_list_s program_list_t;
51 static const char *config_keys[] =
55 static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
57 static program_list_t *pl_head = NULL;
62 static int exec_config (const char *key, const char *value)
64 if (strcasecmp ("Exec", key) == 0)
67 pl = (program_list_t *) malloc (sizeof (program_list_t));
70 memset (pl, '\0', sizeof (program_list_t));
72 pl->user = strdup (value);
79 pl->exec = strchr (pl->user, ' ');
86 while (*pl->exec == ' ')
92 if (*pl->exec == '\0')
102 pl->group = strchr (pl->user, ':');
103 if (NULL != pl->group) {
114 } /* int exec_config */
116 static void exec_child (program_list_t *pl)
124 struct passwd *sp_ptr;
130 status = getpwnam_r (pl->user, &sp, nambuf, sizeof (nambuf), &sp_ptr);
133 ERROR ("exec plugin: getpwnam_r failed: %s",
134 sstrerror (errno, errbuf, sizeof (errbuf)));
139 ERROR ("exec plugin: No such user: `%s'", pl->user);
147 ERROR ("exec plugin: Cowardly refusing to exec program as root.");
151 /* The group configured in the configfile is set as effective group, because
152 * this way the forked process can (re-)gain the user's primary group. */
154 if (NULL != pl->group)
156 if ('\0' != *pl->group) {
157 struct group *gr_ptr = NULL;
160 status = getgrnam_r (pl->group, &gr, nambuf, sizeof (nambuf), &gr_ptr);
163 ERROR ("exec plugin: getgrnam_r failed: %s",
164 sstrerror (errno, errbuf, sizeof (errbuf)));
169 ERROR ("exec plugin: No such group: `%s'", pl->group);
179 } /* if (pl->group == NULL) */
190 if ((gid != egid) && (egid != -1))
196 setgroups (glist_len, glist);
198 #endif /* HAVE_SETGROUPS */
200 status = setgid (gid);
203 ERROR ("exec plugin: setgid (%i) failed: %s",
204 gid, sstrerror (errno, errbuf, sizeof (errbuf)));
210 status = setegid (egid);
213 ERROR ("exec plugin: setegid (%i) failed: %s",
214 egid, sstrerror (errno, errbuf, sizeof (errbuf)));
219 status = setuid (uid);
222 ERROR ("exec plugin: setuid (%i) failed: %s",
223 uid, sstrerror (errno, errbuf, sizeof (errbuf)));
227 arg0 = strrchr (pl->exec, '/');
230 if ((arg0 == NULL) || (*arg0 == '\0'))
233 status = execlp (pl->exec, arg0, (char *) 0);
235 ERROR ("exec plugin: exec failed: %s",
236 sstrerror (errno, errbuf, sizeof (errbuf)));
238 } /* void exec_child */
240 static int fork_child (program_list_t *pl)
248 status = pipe (fd_pipe);
252 ERROR ("exec plugin: pipe failed: %s",
253 sstrerror (errno, errbuf, sizeof (errbuf)));
261 ERROR ("exec plugin: fork failed: %s",
262 sstrerror (errno, errbuf, sizeof (errbuf)));
265 else if (pl->pid == 0)
269 /* Connect the pipe to STDOUT and STDERR */
270 if (fd_pipe[1] != STDOUT_FILENO)
271 dup2 (fd_pipe[1], STDOUT_FILENO);
272 if (fd_pipe[1] != STDERR_FILENO)
273 dup2 (fd_pipe[1], STDERR_FILENO);
274 if ((fd_pipe[1] != STDOUT_FILENO) && (fd_pipe[1] != STDERR_FILENO))
278 /* does not return */
283 } /* int fork_child */
285 static int parse_line (char *buffer)
290 fields[0] = "PUTVAL";
291 fields_num = strsplit (buffer, &fields[1], STATIC_ARRAY_SIZE(fields) - 1);
293 handle_putval (stdout, fields, fields_num + 1);
295 } /* int parse_line */
297 static void *exec_read_one (void *arg)
299 program_list_t *pl = (program_list_t *) arg;
304 fd = fork_child (pl);
306 pthread_exit ((void *) 1);
308 assert (pl->pid != 0);
310 fh = fdopen (fd, "r");
314 ERROR ("exec plugin: fdopen (%i) failed: %s", fd,
315 sstrerror (errno, errbuf, sizeof (errbuf)));
316 kill (pl->pid, SIGTERM);
319 pthread_exit ((void *) 1);
322 while (fgets (buffer, sizeof (buffer), fh) != NULL)
326 len = strlen (buffer);
328 /* Remove newline from end. */
329 while ((len > 0) && ((buffer[len - 1] == '\n')
330 || (buffer[len - 1] == '\r')))
331 buffer[--len] = '\0';
333 DEBUG ("exec plugin: exec_read_one: buffer = %s", buffer);
336 } /* while (fgets) */
341 pthread_exit ((void *) 0);
343 } /* void *exec_read_one */
345 static int exec_read (void)
349 for (pl = pl_head; pl != NULL; pl = pl->next)
357 pthread_attr_init (&attr);
358 pthread_attr_setdetachstate (&attr, PTHREAD_CREATE_DETACHED);
359 pthread_create (&t, &attr, exec_read_one, (void *) pl);
363 } /* int exec_read */
365 static int exec_shutdown (void)
368 program_list_t *next;
377 kill (pl->pid, SIGTERM);
378 INFO ("exec plugin: Sent SIGTERM to %hu", (unsigned short int) pl->pid);
389 } /* int exec_shutdown */
391 void module_register (void)
393 plugin_register_config ("exec", exec_config, config_keys, config_keys_num);
394 plugin_register_read ("exec", exec_read);
395 plugin_register_shutdown ("exec", exec_shutdown);
396 } /* void module_register */
399 * vim:shiftwidth=2:softtabstop=2:tabstop=8