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"
26 #include <sys/types.h>
27 #include <pwd.h>
29 #include <pthread.h>
31 /*
32 * Private data types
33 */
34 struct program_list_s;
35 typedef struct program_list_s program_list_t;
36 struct program_list_s
37 {
38 char *user;
39 char *exec;
40 int pid;
41 program_list_t *next;
42 };
44 /*
45 * Private variables
46 */
47 static data_source_t dsrc_counter[1] =
48 {
49 {"value", DS_TYPE_COUNTER, NAN, NAN}
50 };
52 static data_set_t ds_counter =
53 {
54 "counter", STATIC_ARRAY_SIZE (dsrc_counter), dsrc_counter
55 };
57 static data_source_t dsrc_gauge[1] =
58 {
59 {"value", DS_TYPE_GAUGE, NAN, NAN}
60 };
62 static data_set_t ds_gauge =
63 {
64 "gauge", STATIC_ARRAY_SIZE (dsrc_gauge), dsrc_gauge
65 };
67 static const char *config_keys[] =
68 {
69 "Exec"
70 };
71 static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
73 static program_list_t *pl_head = NULL;
75 /*
76 * Functions
77 */
78 static int exec_config (const char *key, const char *value)
79 {
80 if (strcasecmp ("Exec", key) == 0)
81 {
82 program_list_t *pl;
83 pl = (program_list_t *) malloc (sizeof (program_list_t));
84 if (pl == NULL)
85 return (1);
86 memset (pl, '\0', sizeof (program_list_t));
88 pl->user = strdup (value);
89 if (pl->user == NULL)
90 {
91 sfree (pl);
92 return (1);
93 }
95 pl->exec = strchr (pl->user, ' ');
96 if (pl->exec == NULL)
97 {
98 sfree (pl->user);
99 sfree (pl);
100 return (1);
101 }
102 while (*pl->exec == ' ')
103 {
104 *pl->exec = '\0';
105 pl->exec++;
106 }
108 if (*pl->exec == '\0')
109 {
110 sfree (pl->user);
111 sfree (pl);
112 return (1);
113 }
115 pl->next = pl_head;
116 pl_head = pl;
117 }
118 else
119 {
120 return (-1);
121 }
123 return (0);
124 } /* int exec_config */
126 static void submit_counter (const char *type_instance, counter_t value)
127 {
128 value_t values[1];
129 value_list_t vl = VALUE_LIST_INIT;
131 DEBUG ("type_instance = %s; value = %llu;", type_instance, value);
133 values[0].counter = value;
135 vl.values = values;
136 vl.values_len = 1;
137 vl.time = time (NULL);
138 strcpy (vl.host, hostname_g);
139 strcpy (vl.plugin, "exec");
140 strcpy (vl.plugin_instance, "");
141 strncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
143 plugin_dispatch_values ("counter", &vl);
144 } /* void submit_counter */
146 static void submit_gauge (const char *type_instance, gauge_t value)
147 {
148 value_t values[1];
149 value_list_t vl = VALUE_LIST_INIT;
151 DEBUG ("type_instance = %s; value = %lf;", type_instance, value);
153 values[0].gauge = value;
155 vl.values = values;
156 vl.values_len = 1;
157 vl.time = time (NULL);
158 strcpy (vl.host, hostname_g);
159 strcpy (vl.plugin, "exec");
160 strcpy (vl.plugin_instance, "");
161 strncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
163 plugin_dispatch_values ("gauge", &vl);
164 } /* void submit_counter */
166 static void exec_child (program_list_t *pl)
167 {
168 int status;
169 int uid;
170 char *arg0;
172 struct passwd *sp_ptr;
173 struct passwd sp;
174 char pwnambuf[2048];
175 char errbuf[1024];
177 sp_ptr = NULL;
178 status = getpwnam_r (pl->user, &sp, pwnambuf, sizeof (pwnambuf), &sp_ptr);
179 if (status != 0)
180 {
181 ERROR ("exec plugin: getpwnam_r failed: %s",
182 sstrerror (errno, errbuf, sizeof (errbuf)));
183 exit (-1);
184 }
185 if (sp_ptr == NULL)
186 {
187 ERROR ("exec plugin: No such user: `%s'", pl->user);
188 exit (-1);
189 }
191 uid = sp.pw_uid;
192 if (uid == 0)
193 {
194 ERROR ("exec plugin: Cowardly refusing to exec program as root.");
195 exit (-1);
196 }
198 status = setuid (uid);
199 if (status != 0)
200 {
201 ERROR ("exec plugin: setuid failed: %s",
202 sstrerror (errno, errbuf, sizeof (errbuf)));
203 exit (-1);
204 }
206 arg0 = strrchr (pl->exec, '/');
207 if (arg0 != NULL)
208 arg0++;
209 if ((arg0 == NULL) || (*arg0 == '\0'))
210 arg0 = pl->exec;
212 status = execlp (pl->exec, arg0, (char *) 0);
214 ERROR ("exec plugin: exec failed: %s",
215 sstrerror (errno, errbuf, sizeof (errbuf)));
216 exit (-1);
217 } /* void exec_child */
219 static int fork_child (program_list_t *pl)
220 {
221 int fd_pipe[2];
222 int status;
224 if (pl->pid != 0)
225 return (-1);
227 status = pipe (fd_pipe);
228 if (status != 0)
229 {
230 char errbuf[1024];
231 ERROR ("exec plugin: pipe failed: %s",
232 sstrerror (errno, errbuf, sizeof (errbuf)));
233 return (-1);
234 }
236 pl->pid = fork ();
237 if (pl->pid < 0)
238 {
239 char errbuf[1024];
240 ERROR ("exec plugin: fork failed: %s",
241 sstrerror (errno, errbuf, sizeof (errbuf)));
242 return (-1);
243 }
244 else if (pl->pid == 0)
245 {
246 close (fd_pipe[0]);
248 /* Connect the pipe to STDOUT and STDERR */
249 if (fd_pipe[1] != STDOUT_FILENO)
250 dup2 (fd_pipe[1], STDOUT_FILENO);
251 if (fd_pipe[1] != STDERR_FILENO)
252 dup2 (fd_pipe[1], STDERR_FILENO);
253 if ((fd_pipe[1] != STDOUT_FILENO) && (fd_pipe[1] != STDERR_FILENO))
254 close (fd_pipe[1]);
256 exec_child (pl);
257 /* does not return */
258 }
260 close (fd_pipe[1]);
261 return (fd_pipe[0]);
262 } /* int fork_child */
264 static void *exec_read_one (void *arg)
265 {
266 program_list_t *pl = (program_list_t *) arg;
267 int fd;
268 FILE *fh;
269 char buffer[1024];
271 fd = fork_child (pl);
272 if (fd < 0)
273 pthread_exit ((void *) 1);
275 assert (pl->pid != 0);
277 fh = fdopen (fd, "r");
278 if (fh == NULL)
279 {
280 char errbuf[1024];
281 ERROR ("exec plugin: fdopen (%i) failed: %s", fd,
282 sstrerror (errno, errbuf, sizeof (errbuf)));
283 kill (pl->pid, SIGTERM);
284 close (fd);
285 pthread_exit ((void *) 1);
286 }
288 while (fgets (buffer, sizeof (buffer), fh) != NULL)
289 {
290 int len;
291 char *type;
292 char *type_instance;
293 char *value;
295 DEBUG ("buffer = %s", buffer);
297 len = strlen (buffer);
298 if (len < 5)
299 continue;
301 if (buffer[0] == '#')
302 continue;
304 type = buffer;
306 type_instance = strchr (type, ',');
307 if (type_instance == NULL)
308 continue;
309 *type_instance = '\0';
310 type_instance++;
312 if ((strcasecmp ("counter", type) != 0)
313 && (strcasecmp ("gauge", type) != 0))
314 {
315 WARNING ("exec plugin: Received invalid type: %s", type);
316 continue;
317 }
319 value = strchr (type_instance, ',');
320 if (value == NULL)
321 continue;
322 *value = '\0';
323 value++;
325 DEBUG ("value = %s", value);
327 if (strcasecmp ("counter", type) == 0)
328 submit_counter (type_instance, atoll (value));
329 else
330 submit_gauge (type_instance, atof (value));
331 } /* while (fgets) */
333 fclose (fh);
334 pl->pid = 0;
336 pthread_exit ((void *) 0);
337 } /* void *exec_read_one */
339 static int exec_read (void)
340 {
341 program_list_t *pl;
343 for (pl = pl_head; pl != NULL; pl = pl->next)
344 {
345 pthread_t t;
346 pthread_attr_t attr;
348 if (pl->pid != 0)
349 continue;
351 pthread_attr_init (&attr);
352 pthread_attr_setdetachstate (&attr, PTHREAD_CREATE_DETACHED);
353 pthread_create (&t, &attr, exec_read_one, (void *) pl);
354 } /* for (pl) */
356 return (0);
357 } /* int exec_read */
359 void module_register (modreg_e load)
360 {
361 if (load & MR_DATASETS)
362 {
363 plugin_register_data_set (&ds_counter);
364 plugin_register_data_set (&ds_gauge);
365 }
367 if (load & MR_READ)
368 {
369 plugin_register_config ("exec", exec_config, config_keys, config_keys_num);
370 plugin_register_read ("exec", exec_read);
371 }
372 } /* void module_register */
374 /*
375 * vim:shiftwidth=2:softtabstop=2:tabstop=8
376 */