Merge branch 'pull/collectd-4' 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
29 #include <pthread.h>
30
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 };
43
44 /*
45  * Private variables
46  */
47 static data_source_t dsrc_counter[1] =
48 {
49   {"value", DS_TYPE_COUNTER, NAN, NAN}
50 };
51
52 static data_set_t ds_counter =
53 {
54   "counter", STATIC_ARRAY_SIZE (dsrc_counter), dsrc_counter
55 };
56
57 static data_source_t dsrc_gauge[1] =
58 {
59   {"value", DS_TYPE_GAUGE, NAN, NAN}
60 };
61
62 static data_set_t ds_gauge =
63 {
64   "gauge", STATIC_ARRAY_SIZE (dsrc_gauge), dsrc_gauge
65 };
66
67 static const char *config_keys[] =
68 {
69   "Exec"
70 };
71 static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
72
73 static program_list_t *pl_head = NULL;
74
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));
87
88     pl->user = strdup (value);
89     if (pl->user == NULL)
90     {
91       sfree (pl);
92       return (1);
93     }
94
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     }
107
108     if (*pl->exec == '\0')
109     {
110       sfree (pl->user);
111       sfree (pl);
112       return (1);
113     }
114
115     pl->next = pl_head;
116     pl_head = pl;
117   }
118   else
119   {
120     return (-1);
121   }
122
123   return (0);
124 } /* int exec_config */
125
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;
130
131   DEBUG ("type_instance = %s; value = %llu;", type_instance, value);
132
133   values[0].counter = value;
134
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));
142
143   plugin_dispatch_values ("counter", &vl);
144 } /* void submit_counter */
145
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;
150
151   DEBUG ("type_instance = %s; value = %lf;", type_instance, value);
152
153   values[0].gauge = value;
154
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));
162
163   plugin_dispatch_values ("gauge", &vl);
164 } /* void submit_counter */
165
166 static void exec_child (program_list_t *pl)
167 {
168   int status;
169   int uid;
170   char *arg0;
171
172   struct passwd *sp_ptr;
173   struct passwd sp;
174   char pwnambuf[2048];
175   char errbuf[1024];
176
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   }
190
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   }
197
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   }
205
206   arg0 = strrchr (pl->exec, '/');
207   if (arg0 != NULL)
208     arg0++;
209   if ((arg0 == NULL) || (*arg0 == '\0'))
210     arg0 = pl->exec;
211
212   status = execlp (pl->exec, arg0, (char *) 0);
213
214   ERROR ("exec plugin: exec failed: %s",
215       sstrerror (errno, errbuf, sizeof (errbuf)));
216   exit (-1);
217 } /* void exec_child */
218
219 static int fork_child (program_list_t *pl)
220 {
221   int fd_pipe[2];
222   int status;
223
224   if (pl->pid != 0)
225     return (-1);
226
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   }
235
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]);
247
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]);
255
256     exec_child (pl);
257     /* does not return */
258   }
259
260   close (fd_pipe[1]);
261   return (fd_pipe[0]);
262 } /* int fork_child */
263
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];
270
271   fd = fork_child (pl);
272   if (fd < 0)
273     pthread_exit ((void *) 1);
274
275   assert (pl->pid != 0);
276
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   }
287
288   while (fgets (buffer, sizeof (buffer), fh) != NULL)
289   {
290     int len;
291     char *type;
292     char *type_instance;
293     char *value;
294
295     DEBUG ("buffer = %s", buffer);
296
297     len = strlen (buffer);
298     if (len < 5)
299       continue;
300
301     if (buffer[0] == '#')
302       continue;
303
304     type = buffer;
305
306     type_instance = strchr (type, ',');
307     if (type_instance == NULL)
308       continue;
309     *type_instance = '\0';
310     type_instance++;
311
312     if ((strcasecmp ("counter", type) != 0)
313         && (strcasecmp ("gauge", type) != 0))
314     {
315       WARNING ("exec plugin: Received invalid type: %s", type);
316       continue;
317     }
318
319     value = strchr (type_instance, ',');
320     if (value == NULL)
321       continue;
322     *value = '\0';
323     value++;
324
325     DEBUG ("value = %s", value);
326
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) */
332
333   fclose (fh);
334   pl->pid = 0;
335
336   pthread_exit ((void *) 0);
337 } /* void *exec_read_one */
338
339 static int exec_read (void)
340 {
341   program_list_t *pl;
342
343   for (pl = pl_head; pl != NULL; pl = pl->next)
344   {
345     pthread_t t;
346     pthread_attr_t attr;
347
348     if (pl->pid != 0)
349       continue;
350
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) */
355
356   return (0);
357 } /* int exec_read */
358
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   }
366
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 */
373
374 /*
375  * vim:shiftwidth=2:softtabstop=2:tabstop=8
376  */