Merge branch 'collectd-3.11' into collectd-4.0
[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 const char *config_keys[] =
49 {
50   "Exec"
51 };
52 static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
53
54 static program_list_t *pl_head = NULL;
55
56 /*
57  * Functions
58  */
59 static int exec_config (const char *key, const char *value)
60 {
61   if (strcasecmp ("Exec", key) == 0)
62   {
63     program_list_t *pl;
64     pl = (program_list_t *) malloc (sizeof (program_list_t));
65     if (pl == NULL)
66       return (1);
67     memset (pl, '\0', sizeof (program_list_t));
68
69     pl->user = strdup (value);
70     if (pl->user == NULL)
71     {
72       sfree (pl);
73       return (1);
74     }
75
76     pl->exec = strchr (pl->user, ' ');
77     if (pl->exec == NULL)
78     {
79       sfree (pl->user);
80       sfree (pl);
81       return (1);
82     }
83     while (*pl->exec == ' ')
84     {
85       *pl->exec = '\0';
86       pl->exec++;
87     }
88
89     if (*pl->exec == '\0')
90     {
91       sfree (pl->user);
92       sfree (pl);
93       return (1);
94     }
95
96     pl->next = pl_head;
97     pl_head = pl;
98   }
99   else
100   {
101     return (-1);
102   }
103
104   return (0);
105 } /* int exec_config */
106
107 static void exec_child (program_list_t *pl)
108 {
109   int status;
110   int uid;
111   char *arg0;
112
113   struct passwd *sp_ptr;
114   struct passwd sp;
115   char pwnambuf[2048];
116   char errbuf[1024];
117
118   sp_ptr = NULL;
119   status = getpwnam_r (pl->user, &sp, pwnambuf, sizeof (pwnambuf), &sp_ptr);
120   if (status != 0)
121   {
122     ERROR ("exec plugin: getpwnam_r failed: %s",
123         sstrerror (errno, errbuf, sizeof (errbuf)));
124     exit (-1);
125   }
126   if (sp_ptr == NULL)
127   {
128     ERROR ("exec plugin: No such user: `%s'", pl->user);
129     exit (-1);
130   }
131
132   uid = sp.pw_uid;
133   if (uid == 0)
134   {
135     ERROR ("exec plugin: Cowardly refusing to exec program as root.");
136     exit (-1);
137   }
138
139   status = setuid (uid);
140   if (status != 0)
141   {
142     ERROR ("exec plugin: setuid failed: %s",
143         sstrerror (errno, errbuf, sizeof (errbuf)));
144     exit (-1);
145   }
146
147   arg0 = strrchr (pl->exec, '/');
148   if (arg0 != NULL)
149     arg0++;
150   if ((arg0 == NULL) || (*arg0 == '\0'))
151     arg0 = pl->exec;
152
153   status = execlp (pl->exec, arg0, (char *) 0);
154
155   ERROR ("exec plugin: exec failed: %s",
156       sstrerror (errno, errbuf, sizeof (errbuf)));
157   exit (-1);
158 } /* void exec_child */
159
160 static int fork_child (program_list_t *pl)
161 {
162   int fd_pipe[2];
163   int status;
164
165   if (pl->pid != 0)
166     return (-1);
167
168   status = pipe (fd_pipe);
169   if (status != 0)
170   {
171     char errbuf[1024];
172     ERROR ("exec plugin: pipe failed: %s",
173         sstrerror (errno, errbuf, sizeof (errbuf)));
174     return (-1);
175   }
176
177   pl->pid = fork ();
178   if (pl->pid < 0)
179   {
180     char errbuf[1024];
181     ERROR ("exec plugin: fork failed: %s",
182         sstrerror (errno, errbuf, sizeof (errbuf)));
183     return (-1);
184   }
185   else if (pl->pid == 0)
186   {
187     close (fd_pipe[0]);
188
189     /* Connect the pipe to STDOUT and STDERR */
190     if (fd_pipe[1] != STDOUT_FILENO)
191       dup2 (fd_pipe[1], STDOUT_FILENO);
192     if (fd_pipe[1] != STDERR_FILENO)
193       dup2 (fd_pipe[1], STDERR_FILENO);
194     if ((fd_pipe[1] != STDOUT_FILENO) && (fd_pipe[1] != STDERR_FILENO))
195       close (fd_pipe[1]);
196
197     exec_child (pl);
198     /* does not return */
199   }
200
201   close (fd_pipe[1]);
202   return (fd_pipe[0]);
203 } /* int fork_child */
204
205 static int parse_line (char *buffer)
206 {
207   char *fields[4];
208   int fields_num;
209
210   char *hostname;
211   char *plugin;
212   char *plugin_instance;
213   char *type;
214   char *type_instance;
215
216   const data_set_t *ds;
217   value_list_t vl = VALUE_LIST_INIT;
218
219   int status;
220
221   fields_num = strsplit (buffer, fields, 4);
222   if (fields_num != 2)
223   {
224     WARNING ("exec plugin: Number of fields is not 2.");
225     return (-1);
226   }
227
228   status = parse_identifier (fields[0], &hostname,
229       &plugin, &plugin_instance,
230       &type, &type_instance);
231   if (status != 0)
232   {
233     WARNING ("exec plugin: Cannot parse `%s'", fields[0]);
234     return (-1);
235   }
236
237   if ((strlen (hostname) >= sizeof (vl.host))
238       || (strlen (plugin) >= sizeof (vl.plugin))
239       || ((plugin_instance != NULL)
240         && (strlen (plugin_instance) >= sizeof (vl.plugin_instance)))
241       || ((type_instance != NULL)
242         && (strlen (type_instance) >= sizeof (vl.type_instance))))
243   {
244     WARNING ("exec plugin: An identifier is too long.");
245     return (-1);
246   }
247
248   strcpy (vl.host, hostname);
249   strcpy (vl.plugin, plugin);
250   if (plugin_instance != NULL)
251     strcpy (vl.plugin_instance, plugin_instance);
252   if (type_instance != NULL)
253     strcpy (vl.type_instance, type_instance);
254
255   ds = plugin_get_ds (type);
256   if (ds == NULL)
257   {
258     WARNING ("exec plugin: No such type: `%s'", type);
259     return (-1);
260   }
261
262   vl.values_len = ds->ds_num;
263   vl.values = (value_t *) malloc (sizeof (value_t) * vl.values_len);
264   if (vl.values == NULL)
265     return (-1);
266
267   /* Sets vl.values and vl.time */
268   status = parse_values (fields[1], &vl, ds);
269   if (status != 0)
270   {
271     WARNING ("exec plugin: Cannot parse `%s'", fields[1]);
272     sfree (vl.values);
273     return (-1);
274   }
275
276   plugin_dispatch_values (type, &vl);
277
278   sfree (vl.values);
279   
280   return (0);
281 } /* int parse_line */
282
283 static void *exec_read_one (void *arg)
284 {
285   program_list_t *pl = (program_list_t *) arg;
286   int fd;
287   FILE *fh;
288   char buffer[1024];
289
290   fd = fork_child (pl);
291   if (fd < 0)
292     pthread_exit ((void *) 1);
293
294   assert (pl->pid != 0);
295
296   fh = fdopen (fd, "r");
297   if (fh == NULL)
298   {
299     char errbuf[1024];
300     ERROR ("exec plugin: fdopen (%i) failed: %s", fd,
301         sstrerror (errno, errbuf, sizeof (errbuf)));
302     kill (pl->pid, SIGTERM);
303     close (fd);
304     pthread_exit ((void *) 1);
305   }
306
307   while (fgets (buffer, sizeof (buffer), fh) != NULL)
308   {
309     int len;
310
311     len = strlen (buffer);
312
313     /* Remove newline from end. */
314     while ((len > 0) && ((buffer[len - 1] == '\n')
315           || (buffer[len - 1] == '\r')))
316       buffer[--len] = '\0';
317
318     DEBUG ("exec plugin: exec_read_one: buffer = %s", buffer);
319
320     parse_line (buffer);
321   } /* while (fgets) */
322
323   fclose (fh);
324   pl->pid = 0;
325
326   pthread_exit ((void *) 0);
327   return (NULL);
328 } /* void *exec_read_one */
329
330 static int exec_read (void)
331 {
332   program_list_t *pl;
333
334   for (pl = pl_head; pl != NULL; pl = pl->next)
335   {
336     pthread_t t;
337     pthread_attr_t attr;
338
339     if (pl->pid != 0)
340       continue;
341
342     pthread_attr_init (&attr);
343     pthread_attr_setdetachstate (&attr, PTHREAD_CREATE_DETACHED);
344     pthread_create (&t, &attr, exec_read_one, (void *) pl);
345   } /* for (pl) */
346
347   return (0);
348 } /* int exec_read */
349
350 static int exec_shutdown (void)
351 {
352   program_list_t *pl;
353   program_list_t *next;
354
355   pl = pl_head;
356   while (pl != NULL)
357   {
358     next = pl->next;
359
360     if (pl->pid > 0)
361     {
362       kill (pl->pid, SIGTERM);
363       INFO ("exec plugin: Sent SIGTERM to %hu", (unsigned short int) pl->pid);
364     }
365
366     sfree (pl->user);
367     sfree (pl);
368
369     pl = next;
370   } /* while (pl) */
371   pl_head = NULL;
372
373   return (0);
374 } /* int exec_shutdown */
375
376 void module_register (void)
377 {
378   plugin_register_config ("exec", exec_config, config_keys, config_keys_num);
379   plugin_register_read ("exec", exec_read);
380   plugin_register_shutdown ("exec", exec_shutdown);
381 } /* void module_register */
382
383 /*
384  * vim:shiftwidth=2:softtabstop=2:tabstop=8
385  */