Merge branch 'collectd-4.2'
[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_cmd_putval.h"
26
27 #include <sys/types.h>
28 #include <pwd.h>
29 #include <grp.h>
30 #include <signal.h>
31
32 #include <pthread.h>
33
34 #define PL_NORMAL        0x01
35 #define PL_NOTIF_ACTION  0x02
36 #define PL_NAGIOS_PLUGIN 0x04
37
38 /*
39  * Private data types
40  */
41 struct program_list_s;
42 typedef struct program_list_s program_list_t;
43 struct program_list_s
44 {
45   char           *user;
46   char           *group;
47   char           *exec;
48   char          **argv;
49   int             pid;
50   int             status;
51   int             flags;
52   program_list_t *next;
53 };
54
55 /*
56  * Private variables
57  */
58 static program_list_t *pl_head = NULL;
59
60 /*
61  * Functions
62  */
63 static void sigchld_handler (int signal) /* {{{ */
64 {
65   pid_t pid;
66   int status;
67   while ((pid = waitpid (-1, &status, WNOHANG)) > 0)
68   {
69     program_list_t *pl;
70     for (pl = pl_head; pl != NULL; pl = pl->next)
71       if (pl->pid == pid)
72         break;
73     if (pl != NULL)
74       pl->status = status;
75   } /* while (waitpid) */
76 } /* void sigchld_handler }}} */
77
78 static int exec_config_exec (oconfig_item_t *ci) /* {{{ */
79 {
80   program_list_t *pl;
81   char buffer[128];
82   int i;
83
84   if (ci->children_num != 0)
85   {
86     WARNING ("exec plugin: The config option `%s' may not be a block.",
87         ci->key);
88     return (-1);
89   }
90   if (ci->values_num < 2)
91   {
92     WARNING ("exec plugin: The config option `%s' needs at least two "
93         "arguments.", ci->key);
94     return (-1);
95   }
96   if ((ci->values[0].type != OCONFIG_TYPE_STRING)
97       || (ci->values[1].type != OCONFIG_TYPE_STRING))
98   {
99     WARNING ("exec plugin: The first two arguments to the `%s' option must "
100         "be string arguments.", ci->key);
101     return (-1);
102   }
103
104   pl = (program_list_t *) malloc (sizeof (program_list_t));
105   if (pl == NULL)
106   {
107     ERROR ("exec plugin: malloc failed.");
108     return (-1);
109   }
110   memset (pl, '\0', sizeof (program_list_t));
111
112   if (strcasecmp ("NagiosExec", ci->key) == 0)
113     pl->flags |= PL_NAGIOS_PLUGIN;
114   else if (strcasecmp ("NotificationExec", ci->key) == 0)
115     pl->flags |= PL_NOTIF_ACTION;
116   else
117     pl->flags |= PL_NORMAL;
118
119   pl->user = strdup (ci->values[0].value.string);
120   if (pl->user == NULL)
121   {
122     ERROR ("exec plugin: strdup failed.");
123     sfree (pl);
124     return (-1);
125   }
126
127   pl->group = strchr (pl->user, ':');
128   if (pl->group != NULL)
129   {
130     *pl->group = '\0';
131     pl->group++;
132   }
133
134   pl->exec = strdup (ci->values[1].value.string);
135   if (pl->exec == NULL)
136   {
137     ERROR ("exec plugin: strdup failed.");
138     sfree (pl->user);
139     sfree (pl);
140     return (-1);
141   }
142
143   pl->argv = (char **) malloc (ci->values_num * sizeof (char *));
144   if (pl->argv == NULL)
145   {
146     ERROR ("exec plugin: malloc failed.");
147     sfree (pl->exec);
148     sfree (pl->user);
149     sfree (pl);
150     return (-1);
151   }
152   memset (pl->argv, '\0', ci->values_num * sizeof (char *));
153
154   {
155     char *tmp = strrchr (ci->values[1].value.string, '/');
156     if (tmp == NULL)
157       strncpy (buffer, ci->values[1].value.string, sizeof (buffer));
158     else
159       strncpy (buffer, tmp + 1, sizeof (buffer));
160     buffer[sizeof (buffer) - 1] = '\0';
161   }
162   pl->argv[0] = strdup (buffer);
163   if (pl->argv[0] == NULL)
164   {
165     ERROR ("exec plugin: malloc failed.");
166     sfree (pl->argv);
167     sfree (pl->exec);
168     sfree (pl->user);
169     sfree (pl);
170     return (-1);
171   }
172
173   for (i = 1; i < (ci->values_num - 1); i++)
174   {
175     if (ci->values[i + 1].type == OCONFIG_TYPE_STRING)
176     {
177       pl->argv[i] = strdup (ci->values[i + 1].value.string);
178     }
179     else
180     {
181       if (ci->values[i + 1].type == OCONFIG_TYPE_NUMBER)
182       {
183         snprintf (buffer, sizeof (buffer), "%lf",
184             ci->values[i + 1].value.number);
185       }
186       else
187       {
188         if (ci->values[i + 1].value.boolean)
189           strncpy (buffer, "true", sizeof (buffer));
190         else
191           strncpy (buffer, "false", sizeof (buffer));
192       }
193       buffer[sizeof (buffer) - 1] = '\0';
194
195       pl->argv[i] = strdup (buffer);
196     }
197
198     if (pl->argv[i] == NULL)
199     {
200       ERROR ("exec plugin: strdup failed.");
201       break;
202     }
203   } /* for (i) */
204
205   if (i < (ci->values_num - 1))
206   {
207     while ((--i) >= 0)
208     {
209       sfree (pl->argv[i]);
210     }
211     sfree (pl->argv);
212     sfree (pl->exec);
213     sfree (pl->user);
214     sfree (pl);
215     return (-1);
216   }
217
218   for (i = 0; pl->argv[i] != NULL; i++)
219   {
220     DEBUG ("exec plugin: argv[%i] = %s", i, pl->argv[i]);
221   }
222
223   pl->next = pl_head;
224   pl_head = pl;
225
226   return (0);
227 } /* int exec_config_exec }}} */
228
229 static int exec_config (oconfig_item_t *ci) /* {{{ */
230 {
231   int i;
232
233   for (i = 0; i < ci->children_num; i++)
234   {
235     oconfig_item_t *child = ci->children + i;
236     if ((strcasecmp ("Exec", child->key) == 0)
237         || (strcasecmp ("NagiosExec", child->key) == 0)
238         || (strcasecmp ("NotificationExec", child->key) == 0))
239       exec_config_exec (child);
240     else
241     {
242       WARNING ("exec plugin: Unknown config option `%s'.", child->key);
243     }
244   } /* for (i) */
245
246   return (0);
247 } /* int exec_config }}} */
248
249 static void exec_child (program_list_t *pl) /* {{{ */
250 {
251   int status;
252   int uid;
253   int gid;
254   int egid;
255
256   struct passwd *sp_ptr;
257   struct passwd sp;
258   char nambuf[2048];
259   char errbuf[1024];
260
261   sp_ptr = NULL;
262   status = getpwnam_r (pl->user, &sp, nambuf, sizeof (nambuf), &sp_ptr);
263   if (status != 0)
264   {
265     ERROR ("exec plugin: getpwnam_r failed: %s",
266         sstrerror (errno, errbuf, sizeof (errbuf)));
267     exit (-1);
268   }
269   if (sp_ptr == NULL)
270   {
271     ERROR ("exec plugin: No such user: `%s'", pl->user);
272     exit (-1);
273   }
274
275   uid = sp.pw_uid;
276   gid = sp.pw_gid;
277   if (uid == 0)
278   {
279     ERROR ("exec plugin: Cowardly refusing to exec program as root.");
280     exit (-1);
281   }
282
283   /* The group configured in the configfile is set as effective group, because
284    * this way the forked process can (re-)gain the user's primary group. */
285   egid = -1;
286   if (NULL != pl->group)
287   {
288     if ('\0' != *pl->group) {
289       struct group *gr_ptr = NULL;
290       struct group gr;
291
292       status = getgrnam_r (pl->group, &gr, nambuf, sizeof (nambuf), &gr_ptr);
293       if (0 != status)
294       {
295         ERROR ("exec plugin: getgrnam_r failed: %s",
296             sstrerror (errno, errbuf, sizeof (errbuf)));
297         exit (-1);
298       }
299       if (NULL == gr_ptr)
300       {
301         ERROR ("exec plugin: No such group: `%s'", pl->group);
302         exit (-1);
303       }
304
305       egid = gr.gr_gid;
306     }
307     else
308     {
309       egid = gid;
310     }
311   } /* if (pl->group == NULL) */
312
313   status = setgid (gid);
314   if (status != 0)
315   {
316     ERROR ("exec plugin: setgid (%i) failed: %s",
317         gid, sstrerror (errno, errbuf, sizeof (errbuf)));
318     exit (-1);
319   }
320
321   if (egid != -1)
322   {
323     status = setegid (egid);
324     if (status != 0)
325     {
326       ERROR ("exec plugin: setegid (%i) failed: %s",
327           egid, sstrerror (errno, errbuf, sizeof (errbuf)));
328       exit (-1);
329     }
330   }
331
332   status = setuid (uid);
333   if (status != 0)
334   {
335     ERROR ("exec plugin: setuid (%i) failed: %s",
336         uid, sstrerror (errno, errbuf, sizeof (errbuf)));
337     exit (-1);
338   }
339
340   status = execvp (pl->exec, pl->argv);
341
342   ERROR ("exec plugin: exec failed: %s",
343       sstrerror (errno, errbuf, sizeof (errbuf)));
344   exit (-1);
345 } /* void exec_child }}} */
346
347 static int fork_child (program_list_t *pl) /* {{{ */
348 {
349   int fd_pipe[2];
350   int status;
351
352   if (pl->pid != 0)
353     return (-1);
354
355   status = pipe (fd_pipe);
356   if (status != 0)
357   {
358     char errbuf[1024];
359     ERROR ("exec plugin: pipe failed: %s",
360         sstrerror (errno, errbuf, sizeof (errbuf)));
361     return (-1);
362   }
363
364   pl->pid = fork ();
365   if (pl->pid < 0)
366   {
367     char errbuf[1024];
368     ERROR ("exec plugin: fork failed: %s",
369         sstrerror (errno, errbuf, sizeof (errbuf)));
370     return (-1);
371   }
372   else if (pl->pid == 0)
373   {
374     close (fd_pipe[0]);
375
376     /* Connect the pipe to STDOUT and STDERR */
377     if (fd_pipe[1] != STDOUT_FILENO)
378       dup2 (fd_pipe[1], STDOUT_FILENO);
379     if (fd_pipe[1] != STDERR_FILENO)
380       dup2 (fd_pipe[1], STDERR_FILENO);
381     if ((fd_pipe[1] != STDOUT_FILENO) && (fd_pipe[1] != STDERR_FILENO))
382       close (fd_pipe[1]);
383
384     exec_child (pl);
385     /* does not return */
386   }
387
388   close (fd_pipe[1]);
389   return (fd_pipe[0]);
390 } /* int fork_child }}} */
391
392 static int parse_line (char *buffer) /* {{{ */
393 {
394   char *fields[256];
395   int fields_num;
396
397   fields[0] = "PUTVAL";
398   fields_num = strsplit (buffer, &fields[1], STATIC_ARRAY_SIZE(fields) - 1);
399
400   handle_putval (stdout, fields, fields_num + 1);
401   return (0);
402 } /* int parse_line }}} */
403
404 static void *exec_read_one (void *arg) /* {{{ */
405 {
406   program_list_t *pl = (program_list_t *) arg;
407   int fd;
408   FILE *fh;
409   char buffer[1024];
410   int status;
411
412   fd = fork_child (pl);
413   if (fd < 0)
414     pthread_exit ((void *) 1);
415
416   assert (pl->pid != 0);
417
418   fh = fdopen (fd, "r");
419   if (fh == NULL)
420   {
421     char errbuf[1024];
422     ERROR ("exec plugin: fdopen (%i) failed: %s", fd,
423         sstrerror (errno, errbuf, sizeof (errbuf)));
424     kill (pl->pid, SIGTERM);
425     pl->pid = 0;
426     close (fd);
427     pthread_exit ((void *) 1);
428   }
429
430   buffer[0] = '\0';
431   while (fgets (buffer, sizeof (buffer), fh) != NULL)
432   {
433     int len;
434
435     len = strlen (buffer);
436
437     /* Remove newline from end. */
438     while ((len > 0) && ((buffer[len - 1] == '\n')
439           || (buffer[len - 1] == '\r')))
440       buffer[--len] = '\0';
441
442     DEBUG ("exec plugin: exec_read_one: buffer = %s", buffer);
443
444     if (pl->flags & PL_NAGIOS_PLUGIN)
445       break;
446
447     parse_line (buffer);
448   } /* while (fgets) */
449
450   fclose (fh);
451
452   if (waitpid (pl->pid, &status, 0) > 0)
453     pl->status = status;
454
455   DEBUG ("exec plugin: Child %i exited with status %i.",
456       (int) pl->pid, pl->status);
457
458   if (pl->flags & PL_NAGIOS_PLUGIN)
459   {
460     notification_t n;
461
462     memset (&n, '\0', sizeof (n));
463     
464     n.severity = NOTIF_FAILURE;
465     if (pl->status == 0)
466       n.severity = NOTIF_OKAY;
467     else if (pl->status == 1)
468       n.severity = NOTIF_WARNING;
469
470     strncpy (n.message, buffer, sizeof (n.message));
471     n.message[sizeof (n.message) - 1] = '\0';
472
473     n.time = time (NULL);
474
475     strncpy (n.host, hostname_g, sizeof (n.host));
476     n.host[sizeof (n.host) - 1] = '\0';
477
478     plugin_dispatch_notification (&n);
479   }
480
481   pl->pid = 0;
482   pthread_exit ((void *) 0);
483   return (NULL);
484 } /* void *exec_read_one }}} */
485
486 static int exec_init (void) /* {{{ */
487 {
488   struct sigaction sa;
489
490   memset (&sa, '\0', sizeof (sa));
491   sa.sa_handler = sigchld_handler;
492   sigaction (SIGCHLD, &sa, NULL);
493
494   return (0);
495 } /* int exec_init }}} */
496
497 static int exec_read (void) /* {{{ */
498 {
499   program_list_t *pl;
500
501   for (pl = pl_head; pl != NULL; pl = pl->next)
502   {
503     pthread_t t;
504     pthread_attr_t attr;
505
506     if (pl->pid != 0)
507       continue;
508
509     pthread_attr_init (&attr);
510     pthread_attr_setdetachstate (&attr, PTHREAD_CREATE_DETACHED);
511     pthread_create (&t, &attr, exec_read_one, (void *) pl);
512   } /* for (pl) */
513
514   return (0);
515 } /* int exec_read }}} */
516
517 static int exec_shutdown (void) /* {{{ */
518 {
519   program_list_t *pl;
520   program_list_t *next;
521
522   pl = pl_head;
523   while (pl != NULL)
524   {
525     next = pl->next;
526
527     if (pl->pid > 0)
528     {
529       kill (pl->pid, SIGTERM);
530       INFO ("exec plugin: Sent SIGTERM to %hu", (unsigned short int) pl->pid);
531     }
532
533     sfree (pl->user);
534     sfree (pl);
535
536     pl = next;
537   } /* while (pl) */
538   pl_head = NULL;
539
540   return (0);
541 } /* int exec_shutdown }}} */
542
543 void module_register (void)
544 {
545   plugin_register_complex_config ("exec", exec_config);
546   plugin_register_init ("exec", exec_init);
547   plugin_register_read ("exec", exec_read);
548   plugin_register_shutdown ("exec", exec_shutdown);
549 } /* void module_register */
550
551 /*
552  * vim:shiftwidth=2:softtabstop=2:tabstop=8:fdm=marker
553  */