exec plugin: Implemented a first version.
[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 #include "utils_debug.h"
26
27 #include <sys/types.h>
28 #include <pwd.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   DBG ("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);
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   DBG ("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);
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   struct passwd *sp;
170   int status;
171   int uid;
172   char *arg0;
173
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   }
181
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   }
188
189   status = setuid (uid);
190   if (status != 0)
191   {
192     syslog (LOG_ERR, "exec plugin: setuid failed: %s", strerror (errno));
193     exit (-1);
194   }
195
196   arg0 = strrchr (pl->exec, '/');
197   if (arg0 != NULL)
198     arg0++;
199   if ((arg0 == NULL) || (*arg0 == '\0'))
200     arg0 = pl->exec;
201
202   status = execlp (pl->exec, arg0, (char *) 0);
203
204   syslog (LOG_ERR, "exec plugin: exec failed: %s", strerror (errno));
205   exit (-1);
206 } /* void exec_child */
207
208 static int fork_child (program_list_t *pl)
209 {
210   int fd_pipe[2];
211   int status;
212
213   if (pl->pid != 0)
214     return (-1);
215
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   }
222
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]);
232
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]);
240
241     exec_child (pl);
242     /* does not return */
243   }
244
245   close (fd_pipe[1]);
246   return (fd_pipe[0]);
247 } /* int fork_child */
248
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];
255
256   fd = fork_child (pl);
257   if (fd < 0)
258     pthread_exit ((void *) 1);
259
260   assert (pl->pid != 0);
261
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   }
271
272   while (fgets (buffer, sizeof (buffer), fh) != NULL)
273   {
274     int len;
275     char *type;
276     char *type_instance;
277     char *value;
278
279     DBG ("buffer = %s", buffer);
280
281     len = strlen (buffer);
282     if (len < 5)
283       continue;
284
285     if (buffer[0] == '#')
286       continue;
287
288     type = buffer;
289
290     type_instance = strchr (type, ',');
291     if (type_instance == NULL)
292       continue;
293     *type_instance = '\0';
294     type_instance++;
295
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     }
302
303     value = strchr (type_instance, ',');
304     if (value == NULL)
305       continue;
306     *value = '\0';
307     value++;
308
309     DBG ("value = %s", value);
310
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) */
316
317   fclose (fh);
318   pl->pid = 0;
319
320   pthread_exit ((void *) 0);
321 } /* void *exec_read_one */
322
323 static int exec_read (void)
324 {
325   program_list_t *pl;
326
327   for (pl = pl_head; pl != NULL; pl = pl->next)
328   {
329     pthread_t t;
330     pthread_attr_t attr;
331
332     if (pl->pid != 0)
333       continue;
334
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) */
339
340   return (0);
341 } /* int exec_read */
342
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 */
350
351 /*
352  * vim:shiftwidth=2:softtabstop=2:tabstop=8
353  */