1 /**
2 * collectd - src/exec.c
3 * Copyright (C) 2007 Florian octo Forster
4 *
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.
8 *
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.
13 *
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
17 *
18 * Authors:
19 * Florian octo Forster <octo at verplant.org>
20 **/
22 #include "collectd.h"
23 #include "common.h"
24 #include "plugin.h"
25 #include "utils_debug.h"
27 #include <sys/types.h>
28 #include <pwd.h>
30 #include <pthread.h>
32 /*
33 * Private data types
34 */
35 struct program_list_s;
36 typedef struct program_list_s program_list_t;
37 struct program_list_s
38 {
39 char *user;
40 char *exec;
41 int pid;
42 program_list_t *next;
43 };
45 /*
46 * Private variables
47 */
48 static data_source_t dsrc_counter[1] =
49 {
50 {"value", DS_TYPE_COUNTER, NAN, NAN}
51 };
53 static data_set_t ds_counter =
54 {
55 "counter", STATIC_ARRAY_SIZE (dsrc_counter), dsrc_counter
56 };
58 static data_source_t dsrc_gauge[1] =
59 {
60 {"value", DS_TYPE_GAUGE, NAN, NAN}
61 };
63 static data_set_t ds_gauge =
64 {
65 "gauge", STATIC_ARRAY_SIZE (dsrc_gauge), dsrc_gauge
66 };
68 static const char *config_keys[] =
69 {
70 "Exec"
71 };
72 static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
74 static program_list_t *pl_head = NULL;
76 /*
77 * Functions
78 */
79 static int exec_config (const char *key, const char *value)
80 {
81 if (strcasecmp ("Exec", key) == 0)
82 {
83 program_list_t *pl;
84 pl = (program_list_t *) malloc (sizeof (program_list_t));
85 if (pl == NULL)
86 return (1);
87 memset (pl, '\0', sizeof (program_list_t));
89 pl->user = strdup (value);
90 if (pl->user == NULL)
91 {
92 sfree (pl);
93 return (1);
94 }
96 pl->exec = strchr (pl->user, ' ');
97 if (pl->exec == NULL)
98 {
99 sfree (pl->user);
100 sfree (pl);
101 return (1);
102 }
103 while (*pl->exec == ' ')
104 {
105 *pl->exec = '\0';
106 pl->exec++;
107 }
109 if (*pl->exec == '\0')
110 {
111 sfree (pl->user);
112 sfree (pl);
113 return (1);
114 }
116 pl->next = pl_head;
117 pl_head = pl;
118 }
119 else
120 {
121 return (-1);
122 }
124 return (0);
125 } /* int exec_config */
127 static void submit_counter (const char *type_instance, counter_t value)
128 {
129 value_t values[1];
130 value_list_t vl = VALUE_LIST_INIT;
132 DBG ("type_instance = %s; value = %llu;", type_instance, value);
134 values[0].counter = value;
136 vl.values = values;
137 vl.values_len = 1;
138 vl.time = time (NULL);
139 strcpy (vl.host, hostname);
140 strcpy (vl.plugin, "exec");
141 strcpy (vl.plugin_instance, "");
142 strncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
144 plugin_dispatch_values ("counter", &vl);
145 } /* void submit_counter */
147 static void submit_gauge (const char *type_instance, gauge_t value)
148 {
149 value_t values[1];
150 value_list_t vl = VALUE_LIST_INIT;
152 DBG ("type_instance = %s; value = %lf;", type_instance, value);
154 values[0].gauge = value;
156 vl.values = values;
157 vl.values_len = 1;
158 vl.time = time (NULL);
159 strcpy (vl.host, hostname);
160 strcpy (vl.plugin, "exec");
161 strcpy (vl.plugin_instance, "");
162 strncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
164 plugin_dispatch_values ("gauge", &vl);
165 } /* void submit_counter */
167 static void exec_child (program_list_t *pl)
168 {
169 struct passwd *sp;
170 int status;
171 int uid;
172 char *arg0;
174 /* FIXME: Not thread safe! */
175 sp = getpwnam (pl->user);
176 if (sp == NULL)
177 {
178 syslog (LOG_ERR, "exec plugin: getpwnam failed: %s", strerror (errno));
179 exit (-1);
180 }
182 uid = sp->pw_uid;
183 if (uid == 0)
184 {
185 syslog (LOG_ERR, "exec plugin: Cowardly refusing to exec program as root.");
186 exit (-1);
187 }
189 status = setuid (uid);
190 if (status != 0)
191 {
192 syslog (LOG_ERR, "exec plugin: setuid failed: %s", strerror (errno));
193 exit (-1);
194 }
196 arg0 = strrchr (pl->exec, '/');
197 if (arg0 != NULL)
198 arg0++;
199 if ((arg0 == NULL) || (*arg0 == '\0'))
200 arg0 = pl->exec;
202 status = execlp (pl->exec, arg0, (char *) 0);
204 syslog (LOG_ERR, "exec plugin: exec failed: %s", strerror (errno));
205 exit (-1);
206 } /* void exec_child */
208 static int fork_child (program_list_t *pl)
209 {
210 int fd_pipe[2];
211 int status;
213 if (pl->pid != 0)
214 return (-1);
216 status = pipe (fd_pipe);
217 if (status != 0)
218 {
219 syslog (LOG_ERR, "exec plugin: pipe failed: %s", strerror (errno));
220 return (-1);
221 }
223 pl->pid = fork ();
224 if (pl->pid < 0)
225 {
226 syslog (LOG_ERR, "exec plugin: fork failed: %s", strerror (errno));
227 return (-1);
228 }
229 else if (pl->pid == 0)
230 {
231 close (fd_pipe[0]);
233 /* Connect the pipe to STDOUT and STDERR */
234 if (fd_pipe[1] != STDOUT_FILENO)
235 dup2 (fd_pipe[1], STDOUT_FILENO);
236 if (fd_pipe[1] != STDERR_FILENO)
237 dup2 (fd_pipe[1], STDERR_FILENO);
238 if ((fd_pipe[1] != STDOUT_FILENO) && (fd_pipe[1] != STDERR_FILENO))
239 close (fd_pipe[1]);
241 exec_child (pl);
242 /* does not return */
243 }
245 close (fd_pipe[1]);
246 return (fd_pipe[0]);
247 } /* int fork_child */
249 static void *exec_read_one (void *arg)
250 {
251 program_list_t *pl = (program_list_t *) arg;
252 int fd;
253 FILE *fh;
254 char buffer[1024];
256 fd = fork_child (pl);
257 if (fd < 0)
258 pthread_exit ((void *) 1);
260 assert (pl->pid != 0);
262 fh = fdopen (fd, "r");
263 if (fh == NULL)
264 {
265 syslog (LOG_ERR, "exec plugin: fdopen (%i) failed: %s", fd,
266 strerror (errno));
267 kill (pl->pid, SIGTERM);
268 close (fd);
269 pthread_exit ((void *) 1);
270 }
272 while (fgets (buffer, sizeof (buffer), fh) != NULL)
273 {
274 int len;
275 char *type;
276 char *type_instance;
277 char *value;
279 DBG ("buffer = %s", buffer);
281 len = strlen (buffer);
282 if (len < 5)
283 continue;
285 if (buffer[0] == '#')
286 continue;
288 type = buffer;
290 type_instance = strchr (type, ',');
291 if (type_instance == NULL)
292 continue;
293 *type_instance = '\0';
294 type_instance++;
296 if ((strcasecmp ("counter", type) != 0)
297 && (strcasecmp ("gauge", type) != 0))
298 {
299 syslog (LOG_WARNING, "exec plugin: Received invalid type: %s", type);
300 continue;
301 }
303 value = strchr (type_instance, ',');
304 if (value == NULL)
305 continue;
306 *value = '\0';
307 value++;
309 DBG ("value = %s", value);
311 if (strcasecmp ("counter", type) == 0)
312 submit_counter (type_instance, atoll (value));
313 else
314 submit_gauge (type_instance, atof (value));
315 } /* while (fgets) */
317 fclose (fh);
318 pl->pid = 0;
320 pthread_exit ((void *) 0);
321 } /* void *exec_read_one */
323 static int exec_read (void)
324 {
325 program_list_t *pl;
327 for (pl = pl_head; pl != NULL; pl = pl->next)
328 {
329 pthread_t t;
330 pthread_attr_t attr;
332 if (pl->pid != 0)
333 continue;
335 pthread_attr_init (&attr);
336 pthread_attr_setdetachstate (&attr, PTHREAD_CREATE_DETACHED);
337 pthread_create (&t, &attr, exec_read_one, (void *) pl);
338 } /* for (pl) */
340 return (0);
341 } /* int exec_read */
343 void module_register (void)
344 {
345 plugin_register_data_set (&ds_counter);
346 plugin_register_data_set (&ds_gauge);
347 plugin_register_config ("exec", exec_config, config_keys, config_keys_num);
348 plugin_register_read ("exec", exec_read);
349 } /* void module_register */
351 /*
352 * vim:shiftwidth=2:softtabstop=2:tabstop=8
353 */