Merge branch 'master' into collectd-4
[collectd.git] / src / exec.c
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  **/
21
22 #include "collectd.h"
23 #include "common.h"
24 #include "plugin.h"
25
26 #include <sys/types.h>
27 #include <pwd.h>
28 #include <signal.h>
29
30 #include <pthread.h>
31
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 };
44
45 /*
46  * Private variables
47  */
48 static data_source_t dsrc_counter[1] =
49 {
50   {"value", DS_TYPE_COUNTER, NAN, NAN}
51 };
52
53 static data_set_t ds_counter =
54 {
55   "counter", STATIC_ARRAY_SIZE (dsrc_counter), dsrc_counter
56 };
57
58 static data_source_t dsrc_gauge[1] =
59 {
60   {"value", DS_TYPE_GAUGE, NAN, NAN}
61 };
62
63 static data_set_t ds_gauge =
64 {
65   "gauge", STATIC_ARRAY_SIZE (dsrc_gauge), dsrc_gauge
66 };
67
68 static const char *config_keys[] =
69 {
70   "Exec"
71 };
72 static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
73
74 static program_list_t *pl_head = NULL;
75
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));
88
89     pl->user = strdup (value);
90     if (pl->user == NULL)
91     {
92       sfree (pl);
93       return (1);
94     }
95
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     }
108
109     if (*pl->exec == '\0')
110     {
111       sfree (pl->user);
112       sfree (pl);
113       return (1);
114     }
115
116     pl->next = pl_head;
117     pl_head = pl;
118   }
119   else
120   {
121     return (-1);
122   }
123
124   return (0);
125 } /* int exec_config */
126
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;
131
132   DEBUG ("type_instance = %s; value = %llu;", type_instance, value);
133
134   values[0].counter = value;
135
136   vl.values = values;
137   vl.values_len = 1;
138   vl.time = time (NULL);
139   strcpy (vl.host, hostname_g);
140   strcpy (vl.plugin, "exec");
141   strcpy (vl.plugin_instance, "");
142   strncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
143
144   plugin_dispatch_values ("counter", &vl);
145 } /* void submit_counter */
146
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;
151
152   DEBUG ("type_instance = %s; value = %lf;", type_instance, value);
153
154   values[0].gauge = value;
155
156   vl.values = values;
157   vl.values_len = 1;
158   vl.time = time (NULL);
159   strcpy (vl.host, hostname_g);
160   strcpy (vl.plugin, "exec");
161   strcpy (vl.plugin_instance, "");
162   strncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
163
164   plugin_dispatch_values ("gauge", &vl);
165 } /* void submit_counter */
166
167 static void exec_child (program_list_t *pl)
168 {
169   int status;
170   int uid;
171   char *arg0;
172
173   struct passwd *sp_ptr;
174   struct passwd sp;
175   char pwnambuf[2048];
176   char errbuf[1024];
177
178   sp_ptr = NULL;
179   status = getpwnam_r (pl->user, &sp, pwnambuf, sizeof (pwnambuf), &sp_ptr);
180   if (status != 0)
181   {
182     ERROR ("exec plugin: getpwnam_r failed: %s",
183         sstrerror (errno, errbuf, sizeof (errbuf)));
184     exit (-1);
185   }
186   if (sp_ptr == NULL)
187   {
188     ERROR ("exec plugin: No such user: `%s'", pl->user);
189     exit (-1);
190   }
191
192   uid = sp.pw_uid;
193   if (uid == 0)
194   {
195     ERROR ("exec plugin: Cowardly refusing to exec program as root.");
196     exit (-1);
197   }
198
199   status = setuid (uid);
200   if (status != 0)
201   {
202     ERROR ("exec plugin: setuid failed: %s",
203         sstrerror (errno, errbuf, sizeof (errbuf)));
204     exit (-1);
205   }
206
207   arg0 = strrchr (pl->exec, '/');
208   if (arg0 != NULL)
209     arg0++;
210   if ((arg0 == NULL) || (*arg0 == '\0'))
211     arg0 = pl->exec;
212
213   status = execlp (pl->exec, arg0, (char *) 0);
214
215   ERROR ("exec plugin: exec failed: %s",
216       sstrerror (errno, errbuf, sizeof (errbuf)));
217   exit (-1);
218 } /* void exec_child */
219
220 static int fork_child (program_list_t *pl)
221 {
222   int fd_pipe[2];
223   int status;
224
225   if (pl->pid != 0)
226     return (-1);
227
228   status = pipe (fd_pipe);
229   if (status != 0)
230   {
231     char errbuf[1024];
232     ERROR ("exec plugin: pipe failed: %s",
233         sstrerror (errno, errbuf, sizeof (errbuf)));
234     return (-1);
235   }
236
237   pl->pid = fork ();
238   if (pl->pid < 0)
239   {
240     char errbuf[1024];
241     ERROR ("exec plugin: fork failed: %s",
242         sstrerror (errno, errbuf, sizeof (errbuf)));
243     return (-1);
244   }
245   else if (pl->pid == 0)
246   {
247     close (fd_pipe[0]);
248
249     /* Connect the pipe to STDOUT and STDERR */
250     if (fd_pipe[1] != STDOUT_FILENO)
251       dup2 (fd_pipe[1], STDOUT_FILENO);
252     if (fd_pipe[1] != STDERR_FILENO)
253       dup2 (fd_pipe[1], STDERR_FILENO);
254     if ((fd_pipe[1] != STDOUT_FILENO) && (fd_pipe[1] != STDERR_FILENO))
255       close (fd_pipe[1]);
256
257     exec_child (pl);
258     /* does not return */
259   }
260
261   close (fd_pipe[1]);
262   return (fd_pipe[0]);
263 } /* int fork_child */
264
265 static void *exec_read_one (void *arg)
266 {
267   program_list_t *pl = (program_list_t *) arg;
268   int fd;
269   FILE *fh;
270   char buffer[1024];
271
272   fd = fork_child (pl);
273   if (fd < 0)
274     pthread_exit ((void *) 1);
275
276   assert (pl->pid != 0);
277
278   fh = fdopen (fd, "r");
279   if (fh == NULL)
280   {
281     char errbuf[1024];
282     ERROR ("exec plugin: fdopen (%i) failed: %s", fd,
283         sstrerror (errno, errbuf, sizeof (errbuf)));
284     kill (pl->pid, SIGTERM);
285     close (fd);
286     pthread_exit ((void *) 1);
287   }
288
289   while (fgets (buffer, sizeof (buffer), fh) != NULL)
290   {
291     int len;
292     char *type;
293     char *type_instance;
294     char *value;
295
296     DEBUG ("buffer = %s", buffer);
297
298     len = strlen (buffer);
299     if (len < 5)
300       continue;
301
302     if (buffer[0] == '#')
303       continue;
304
305     type = buffer;
306
307     type_instance = strchr (type, ',');
308     if (type_instance == NULL)
309       continue;
310     *type_instance = '\0';
311     type_instance++;
312
313     if ((strcasecmp ("counter", type) != 0)
314         && (strcasecmp ("gauge", type) != 0))
315     {
316       WARNING ("exec plugin: Received invalid type: %s", type);
317       continue;
318     }
319
320     value = strchr (type_instance, ',');
321     if (value == NULL)
322       continue;
323     *value = '\0';
324     value++;
325
326     DEBUG ("value = %s", value);
327
328     if (strcasecmp ("counter", type) == 0)
329       submit_counter (type_instance, atoll (value));
330     else
331       submit_gauge (type_instance, atof (value));
332   } /* while (fgets) */
333
334   fclose (fh);
335   pl->pid = 0;
336
337   pthread_exit ((void *) 0);
338   return (NULL);
339 } /* void *exec_read_one */
340
341 static int exec_read (void)
342 {
343   program_list_t *pl;
344
345   for (pl = pl_head; pl != NULL; pl = pl->next)
346   {
347     pthread_t t;
348     pthread_attr_t attr;
349
350     if (pl->pid != 0)
351       continue;
352
353     pthread_attr_init (&attr);
354     pthread_attr_setdetachstate (&attr, PTHREAD_CREATE_DETACHED);
355     pthread_create (&t, &attr, exec_read_one, (void *) pl);
356   } /* for (pl) */
357
358   return (0);
359 } /* int exec_read */
360
361 static int exec_shutdown (void)
362 {
363   program_list_t *pl;
364   program_list_t *next;
365
366   pl = pl_head;
367   while (pl != NULL)
368   {
369     next = pl->next;
370
371     if (pl->pid > 0)
372     {
373       kill (pl->pid, SIGTERM);
374       INFO ("exec plugin: Sent SIGTERM to %hu", (unsigned short int) pl->pid);
375     }
376
377     sfree (pl->user);
378     sfree (pl);
379
380     pl = next;
381   } /* while (pl) */
382   pl_head = NULL;
383
384   return (0);
385 } /* int exec_shutdown */
386
387 void module_register (modreg_e load)
388 {
389   if (load & MR_DATASETS)
390   {
391     plugin_register_data_set (&ds_counter);
392     plugin_register_data_set (&ds_gauge);
393   }
394
395   if (load & MR_READ)
396   {
397     plugin_register_config ("exec", exec_config, config_keys, config_keys_num);
398     plugin_register_read ("exec", exec_read);
399     plugin_register_shutdown ("exec", exec_shutdown);
400   }
401 } /* void module_register */
402
403 /*
404  * vim:shiftwidth=2:softtabstop=2:tabstop=8
405  */