exec plugin: Replace `getpwnam' with `getpwnam_r'.
[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_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   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_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
177   sp_ptr = NULL;
178   status = getpwnam_r (pl->user, &sp, pwnambuf, sizeof (pwnambuf), &sp_ptr);
179   if (status != 0)
180   {
181     syslog (LOG_ERR, "exec plugin: getpwnam_r failed: %s", strerror (status));
182     exit (-1);
183   }
184   if (sp_ptr == NULL)
185   {
186     syslog (LOG_ERR, "exec plugin: No such user: `%s'", pl->user);
187     exit (-1);
188   }
189
190   uid = sp.pw_uid;
191   if (uid == 0)
192   {
193     syslog (LOG_ERR, "exec plugin: Cowardly refusing to exec program as root.");
194     exit (-1);
195   }
196
197   status = setuid (uid);
198   if (status != 0)
199   {
200     syslog (LOG_ERR, "exec plugin: setuid failed: %s", strerror (errno));
201     exit (-1);
202   }
203
204   arg0 = strrchr (pl->exec, '/');
205   if (arg0 != NULL)
206     arg0++;
207   if ((arg0 == NULL) || (*arg0 == '\0'))
208     arg0 = pl->exec;
209
210   status = execlp (pl->exec, arg0, (char *) 0);
211
212   syslog (LOG_ERR, "exec plugin: exec failed: %s", strerror (errno));
213   exit (-1);
214 } /* void exec_child */
215
216 static int fork_child (program_list_t *pl)
217 {
218   int fd_pipe[2];
219   int status;
220
221   if (pl->pid != 0)
222     return (-1);
223
224   status = pipe (fd_pipe);
225   if (status != 0)
226   {
227     syslog (LOG_ERR, "exec plugin: pipe failed: %s", strerror (errno));
228     return (-1);
229   }
230
231   pl->pid = fork ();
232   if (pl->pid < 0)
233   {
234     syslog (LOG_ERR, "exec plugin: fork failed: %s", strerror (errno));
235     return (-1);
236   }
237   else if (pl->pid == 0)
238   {
239     close (fd_pipe[0]);
240
241     /* Connect the pipe to STDOUT and STDERR */
242     if (fd_pipe[1] != STDOUT_FILENO)
243       dup2 (fd_pipe[1], STDOUT_FILENO);
244     if (fd_pipe[1] != STDERR_FILENO)
245       dup2 (fd_pipe[1], STDERR_FILENO);
246     if ((fd_pipe[1] != STDOUT_FILENO) && (fd_pipe[1] != STDERR_FILENO))
247       close (fd_pipe[1]);
248
249     exec_child (pl);
250     /* does not return */
251   }
252
253   close (fd_pipe[1]);
254   return (fd_pipe[0]);
255 } /* int fork_child */
256
257 static void *exec_read_one (void *arg)
258 {
259   program_list_t *pl = (program_list_t *) arg;
260   int fd;
261   FILE *fh;
262   char buffer[1024];
263
264   fd = fork_child (pl);
265   if (fd < 0)
266     pthread_exit ((void *) 1);
267
268   assert (pl->pid != 0);
269
270   fh = fdopen (fd, "r");
271   if (fh == NULL)
272   {
273     syslog (LOG_ERR, "exec plugin: fdopen (%i) failed: %s", fd,
274         strerror (errno));
275     kill (pl->pid, SIGTERM);
276     close (fd);
277     pthread_exit ((void *) 1);
278   }
279
280   while (fgets (buffer, sizeof (buffer), fh) != NULL)
281   {
282     int len;
283     char *type;
284     char *type_instance;
285     char *value;
286
287     DBG ("buffer = %s", buffer);
288
289     len = strlen (buffer);
290     if (len < 5)
291       continue;
292
293     if (buffer[0] == '#')
294       continue;
295
296     type = buffer;
297
298     type_instance = strchr (type, ',');
299     if (type_instance == NULL)
300       continue;
301     *type_instance = '\0';
302     type_instance++;
303
304     if ((strcasecmp ("counter", type) != 0)
305         && (strcasecmp ("gauge", type) != 0))
306     {
307       syslog (LOG_WARNING, "exec plugin: Received invalid type: %s", type);
308       continue;
309     }
310
311     value = strchr (type_instance, ',');
312     if (value == NULL)
313       continue;
314     *value = '\0';
315     value++;
316
317     DBG ("value = %s", value);
318
319     if (strcasecmp ("counter", type) == 0)
320       submit_counter (type_instance, atoll (value));
321     else
322       submit_gauge (type_instance, atof (value));
323   } /* while (fgets) */
324
325   fclose (fh);
326   pl->pid = 0;
327
328   pthread_exit ((void *) 0);
329 } /* void *exec_read_one */
330
331 static int exec_read (void)
332 {
333   program_list_t *pl;
334
335   for (pl = pl_head; pl != NULL; pl = pl->next)
336   {
337     pthread_t t;
338     pthread_attr_t attr;
339
340     if (pl->pid != 0)
341       continue;
342
343     pthread_attr_init (&attr);
344     pthread_attr_setdetachstate (&attr, PTHREAD_CREATE_DETACHED);
345     pthread_create (&t, &attr, exec_read_one, (void *) pl);
346   } /* for (pl) */
347
348   return (0);
349 } /* int exec_read */
350
351 void module_register (void)
352 {
353   plugin_register_data_set (&ds_counter);
354   plugin_register_data_set (&ds_gauge);
355   plugin_register_config ("exec", exec_config, config_keys, config_keys_num);
356   plugin_register_read ("exec", exec_read);
357 } /* void module_register */
358
359 /*
360  * vim:shiftwidth=2:softtabstop=2:tabstop=8
361  */