1 /**
2 * collectd - src/exec.c
3 * Copyright (C) 2007,2008 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 **/
22 #include "collectd.h"
23 #include "common.h"
24 #include "plugin.h"
26 #include "utils_cmd_putval.h"
27 #include "utils_cmd_putnotif.h"
29 #include <sys/types.h>
30 #include <pwd.h>
31 #include <grp.h>
32 #include <signal.h>
34 #include <pthread.h>
36 #define PL_NORMAL 0x01
37 #define PL_NOTIF_ACTION 0x02
39 #define PL_RUNNING 0x10
41 /*
42 * Private data types
43 */
44 /*
45 * Access to this structure is serialized using the `pl_lock' lock and the
46 * `PL_RUNNING' flag. The execution of notifications is *not* serialized, so
47 * all functions used to handle notifications MUST NOT write to this structure.
48 * The `pid' and `status' fields are thus unused if the `PL_NOTIF_ACTION' flag
49 * is set.
50 * The `PL_RUNNING' flag is set in `exec_read' and unset in `exec_read_one'.
51 */
52 struct program_list_s;
53 typedef struct program_list_s program_list_t;
54 struct program_list_s
55 {
56 char *user;
57 char *group;
58 char *exec;
59 char **argv;
60 int pid;
61 int status;
62 int flags;
63 program_list_t *next;
64 };
66 typedef struct program_list_and_notification_s
67 {
68 program_list_t *pl;
69 notification_t n;
70 } program_list_and_notification_t;
72 /*
73 * Private variables
74 */
75 static program_list_t *pl_head = NULL;
76 static pthread_mutex_t pl_lock = PTHREAD_MUTEX_INITIALIZER;
78 /*
79 * Functions
80 */
81 static void sigchld_handler (int signal) /* {{{ */
82 {
83 pid_t pid;
84 int status;
85 while ((pid = waitpid (-1, &status, WNOHANG)) > 0)
86 {
87 program_list_t *pl;
88 for (pl = pl_head; pl != NULL; pl = pl->next)
89 if (pl->pid == pid)
90 break;
91 if (pl != NULL)
92 pl->status = status;
93 } /* while (waitpid) */
94 } /* void sigchld_handler }}} */
96 static int exec_config_exec (oconfig_item_t *ci) /* {{{ */
97 {
98 program_list_t *pl;
99 char buffer[128];
100 int i;
102 if (ci->children_num != 0)
103 {
104 WARNING ("exec plugin: The config option `%s' may not be a block.",
105 ci->key);
106 return (-1);
107 }
108 if (ci->values_num < 2)
109 {
110 WARNING ("exec plugin: The config option `%s' needs at least two "
111 "arguments.", ci->key);
112 return (-1);
113 }
114 if ((ci->values[0].type != OCONFIG_TYPE_STRING)
115 || (ci->values[1].type != OCONFIG_TYPE_STRING))
116 {
117 WARNING ("exec plugin: The first two arguments to the `%s' option must "
118 "be string arguments.", ci->key);
119 return (-1);
120 }
122 pl = (program_list_t *) malloc (sizeof (program_list_t));
123 if (pl == NULL)
124 {
125 ERROR ("exec plugin: malloc failed.");
126 return (-1);
127 }
128 memset (pl, '\0', sizeof (program_list_t));
130 if (strcasecmp ("NotificationExec", ci->key) == 0)
131 pl->flags |= PL_NOTIF_ACTION;
132 else
133 pl->flags |= PL_NORMAL;
135 pl->user = strdup (ci->values[0].value.string);
136 if (pl->user == NULL)
137 {
138 ERROR ("exec plugin: strdup failed.");
139 sfree (pl);
140 return (-1);
141 }
143 pl->group = strchr (pl->user, ':');
144 if (pl->group != NULL)
145 {
146 *pl->group = '\0';
147 pl->group++;
148 }
150 pl->exec = strdup (ci->values[1].value.string);
151 if (pl->exec == NULL)
152 {
153 ERROR ("exec plugin: strdup failed.");
154 sfree (pl->user);
155 sfree (pl);
156 return (-1);
157 }
159 pl->argv = (char **) malloc (ci->values_num * sizeof (char *));
160 if (pl->argv == NULL)
161 {
162 ERROR ("exec plugin: malloc failed.");
163 sfree (pl->exec);
164 sfree (pl->user);
165 sfree (pl);
166 return (-1);
167 }
168 memset (pl->argv, '\0', ci->values_num * sizeof (char *));
170 {
171 char *tmp = strrchr (ci->values[1].value.string, '/');
172 if (tmp == NULL)
173 strncpy (buffer, ci->values[1].value.string, sizeof (buffer));
174 else
175 strncpy (buffer, tmp + 1, sizeof (buffer));
176 buffer[sizeof (buffer) - 1] = '\0';
177 }
178 pl->argv[0] = strdup (buffer);
179 if (pl->argv[0] == NULL)
180 {
181 ERROR ("exec plugin: malloc failed.");
182 sfree (pl->argv);
183 sfree (pl->exec);
184 sfree (pl->user);
185 sfree (pl);
186 return (-1);
187 }
189 for (i = 1; i < (ci->values_num - 1); i++)
190 {
191 if (ci->values[i + 1].type == OCONFIG_TYPE_STRING)
192 {
193 pl->argv[i] = strdup (ci->values[i + 1].value.string);
194 }
195 else
196 {
197 if (ci->values[i + 1].type == OCONFIG_TYPE_NUMBER)
198 {
199 snprintf (buffer, sizeof (buffer), "%lf",
200 ci->values[i + 1].value.number);
201 }
202 else
203 {
204 if (ci->values[i + 1].value.boolean)
205 strncpy (buffer, "true", sizeof (buffer));
206 else
207 strncpy (buffer, "false", sizeof (buffer));
208 }
209 buffer[sizeof (buffer) - 1] = '\0';
211 pl->argv[i] = strdup (buffer);
212 }
214 if (pl->argv[i] == NULL)
215 {
216 ERROR ("exec plugin: strdup failed.");
217 break;
218 }
219 } /* for (i) */
221 if (i < (ci->values_num - 1))
222 {
223 while ((--i) >= 0)
224 {
225 sfree (pl->argv[i]);
226 }
227 sfree (pl->argv);
228 sfree (pl->exec);
229 sfree (pl->user);
230 sfree (pl);
231 return (-1);
232 }
234 for (i = 0; pl->argv[i] != NULL; i++)
235 {
236 DEBUG ("exec plugin: argv[%i] = %s", i, pl->argv[i]);
237 }
239 pl->next = pl_head;
240 pl_head = pl;
242 return (0);
243 } /* int exec_config_exec }}} */
245 static int exec_config (oconfig_item_t *ci) /* {{{ */
246 {
247 int i;
249 for (i = 0; i < ci->children_num; i++)
250 {
251 oconfig_item_t *child = ci->children + i;
252 if ((strcasecmp ("Exec", child->key) == 0)
253 || (strcasecmp ("NotificationExec", child->key) == 0))
254 exec_config_exec (child);
255 else
256 {
257 WARNING ("exec plugin: Unknown config option `%s'.", child->key);
258 }
259 } /* for (i) */
261 return (0);
262 } /* int exec_config }}} */
264 static void exec_child (program_list_t *pl) /* {{{ */
265 {
266 int status;
267 int uid;
268 int gid;
269 int egid;
271 struct passwd *sp_ptr;
272 struct passwd sp;
273 char nambuf[2048];
274 char errbuf[1024];
276 sp_ptr = NULL;
277 status = getpwnam_r (pl->user, &sp, nambuf, sizeof (nambuf), &sp_ptr);
278 if (status != 0)
279 {
280 ERROR ("exec plugin: getpwnam_r failed: %s",
281 sstrerror (errno, errbuf, sizeof (errbuf)));
282 exit (-1);
283 }
284 if (sp_ptr == NULL)
285 {
286 ERROR ("exec plugin: No such user: `%s'", pl->user);
287 exit (-1);
288 }
290 uid = sp.pw_uid;
291 gid = sp.pw_gid;
292 if (uid == 0)
293 {
294 ERROR ("exec plugin: Cowardly refusing to exec program as root.");
295 exit (-1);
296 }
298 /* The group configured in the configfile is set as effective group, because
299 * this way the forked process can (re-)gain the user's primary group. */
300 egid = -1;
301 if (NULL != pl->group)
302 {
303 if ('\0' != *pl->group) {
304 struct group *gr_ptr = NULL;
305 struct group gr;
307 status = getgrnam_r (pl->group, &gr, nambuf, sizeof (nambuf), &gr_ptr);
308 if (0 != status)
309 {
310 ERROR ("exec plugin: getgrnam_r failed: %s",
311 sstrerror (errno, errbuf, sizeof (errbuf)));
312 exit (-1);
313 }
314 if (NULL == gr_ptr)
315 {
316 ERROR ("exec plugin: No such group: `%s'", pl->group);
317 exit (-1);
318 }
320 egid = gr.gr_gid;
321 }
322 else
323 {
324 egid = gid;
325 }
326 } /* if (pl->group == NULL) */
328 #if HAVE_SETGROUPS
329 if (getuid () == 0)
330 {
331 gid_t glist[2];
332 size_t glist_len;
334 glist[0] = gid;
335 glist_len = 1;
337 if ((gid != egid) && (egid != -1))
338 {
339 glist[1] = egid;
340 glist_len = 2;
341 }
343 setgroups (glist_len, glist);
344 }
345 #endif /* HAVE_SETGROUPS */
347 status = setgid (gid);
348 if (status != 0)
349 {
350 ERROR ("exec plugin: setgid (%i) failed: %s",
351 gid, sstrerror (errno, errbuf, sizeof (errbuf)));
352 exit (-1);
353 }
355 if (egid != -1)
356 {
357 status = setegid (egid);
358 if (status != 0)
359 {
360 ERROR ("exec plugin: setegid (%i) failed: %s",
361 egid, sstrerror (errno, errbuf, sizeof (errbuf)));
362 exit (-1);
363 }
364 }
366 status = setuid (uid);
367 if (status != 0)
368 {
369 ERROR ("exec plugin: setuid (%i) failed: %s",
370 uid, sstrerror (errno, errbuf, sizeof (errbuf)));
371 exit (-1);
372 }
374 status = execvp (pl->exec, pl->argv);
376 ERROR ("exec plugin: exec failed: %s",
377 sstrerror (errno, errbuf, sizeof (errbuf)));
378 exit (-1);
379 } /* void exec_child }}} */
381 /*
382 * Creates three pipes (one for reading, one for writing and one for errors),
383 * forks a child, sets up the pipes so that fd_in is connected to STDIN of
384 * the child and fd_out is connected to STDOUT and fd_err is connected to STDERR
385 * of the child. Then is calls `exec_child'.
386 */
387 static int fork_child (program_list_t *pl, int *fd_in, int *fd_out, int *fd_err) /* {{{ */
388 {
389 int fd_pipe_in[2];
390 int fd_pipe_out[2];
391 int fd_pipe_err[2];
392 char errbuf[1024];
393 int status;
394 int pid;
396 if (pl->pid != 0)
397 return (-1);
399 status = pipe (fd_pipe_in);
400 if (status != 0)
401 {
402 ERROR ("exec plugin: pipe failed: %s",
403 sstrerror (errno, errbuf, sizeof (errbuf)));
404 return (-1);
405 }
407 status = pipe (fd_pipe_out);
408 if (status != 0)
409 {
410 ERROR ("exec plugin: pipe failed: %s",
411 sstrerror (errno, errbuf, sizeof (errbuf)));
412 return (-1);
413 }
415 status = pipe (fd_pipe_err);
416 if (status != 0)
417 {
418 ERROR ("exec plugin: pipe failed: %s",
419 sstrerror (errno, errbuf, sizeof (errbuf)));
420 return (-1);
421 }
423 pid = fork ();
424 if (pid < 0)
425 {
426 ERROR ("exec plugin: fork failed: %s",
427 sstrerror (errno, errbuf, sizeof (errbuf)));
428 return (-1);
429 }
430 else if (pid == 0)
431 {
432 int fd_num;
433 int fd;
435 /* Close all file descriptors but the pipe end we need. */
436 fd_num = getdtablesize ();
437 for (fd = 0; fd < fd_num; fd++)
438 {
439 if ((fd == fd_pipe_in[0])
440 || (fd == fd_pipe_out[1])
441 || (fd == fd_pipe_err[1]))
442 continue;
443 close (fd);
444 }
446 /* Connect the `in' pipe to STDIN */
447 if (fd_pipe_in[0] != STDIN_FILENO)
448 {
449 dup2 (fd_pipe_in[0], STDIN_FILENO);
450 close (fd_pipe_in[0]);
451 }
453 /* Now connect the `out' pipe to STDOUT */
454 if (fd_pipe_out[1] != STDOUT_FILENO)
455 {
456 dup2 (fd_pipe_out[1], STDOUT_FILENO);
457 close (fd_pipe_out[1]);
458 }
460 /* Now connect the `out' pipe to STDOUT */
461 if (fd_pipe_err[1] != STDERR_FILENO)
462 {
463 dup2 (fd_pipe_err[1], STDERR_FILENO);
464 close (fd_pipe_err[1]);
465 }
467 exec_child (pl);
468 /* does not return */
469 }
471 close (fd_pipe_in[0]);
472 close (fd_pipe_out[1]);
473 close (fd_pipe_err[1]);
475 if (fd_in != NULL)
476 *fd_in = fd_pipe_in[1];
477 else
478 close (fd_pipe_in[1]);
480 if (fd_out != NULL)
481 *fd_out = fd_pipe_out[0];
482 else
483 close (fd_pipe_out[0]);
485 if (fd_err != NULL)
486 *fd_err = fd_pipe_err[0];
487 else
488 close (fd_pipe_err[0]);
490 return (pid);
491 } /* int fork_child }}} */
493 static int parse_line (char *buffer) /* {{{ */
494 {
495 char *fields[256];
496 int fields_num;
498 fields[0] = "PUTVAL";
499 fields_num = strsplit (buffer, fields + 1, STATIC_ARRAY_SIZE(fields) - 1);
501 if (strcasecmp (fields[1], "putval") == 0)
502 return (handle_putval (stdout, fields + 1, fields_num));
503 else if (strcasecmp (fields[1], "putnotif") == 0)
504 return (handle_putnotif (stdout, fields + 1, fields_num));
506 /* compatibility code */
507 return (handle_putval (stdout, fields, fields_num + 1));
508 } /* int parse_line }}} */
510 static void *exec_read_one (void *arg) /* {{{ */
511 {
512 program_list_t *pl = (program_list_t *) arg;
513 int fd, fd_err, highest_fd;
514 fd_set fdset, copy;
515 int status;
516 char buffer[1200]; /* if not completely read */
517 char buffer_err[1024];
518 char *pbuffer = buffer;
519 char *pbuffer_err = buffer_err;
521 status = fork_child (pl, NULL, &fd, &fd_err);
522 if (status < 0)
523 pthread_exit ((void *) 1);
524 pl->pid = status;
526 assert (pl->pid != 0);
528 FD_ZERO( &fdset );
529 FD_SET(fd, &fdset);
530 FD_SET(fd_err, &fdset);
532 /* Determine the highest file descriptor */
533 highest_fd = (fd > fd_err) ? fd : fd_err;
535 /* We use a copy of fdset, as select modifies it */
536 copy = fdset;
538 while (select(highest_fd + 1, ©, NULL, NULL, NULL ) > 0)
539 {
540 int len;
542 if (FD_ISSET(fd, ©))
543 {
544 char *pnl;
546 len = read(fd, pbuffer, sizeof(buffer) - 1 - (pbuffer - buffer));
548 if (len < 0)
549 {
550 if (errno == EAGAIN || errno == EINTR) continue;
551 break;
552 }
553 else if (len == 0) break; /* We've reached EOF */
555 pbuffer[len] = '\0';
557 len += pbuffer - buffer;
558 pbuffer = buffer;
560 while ((pnl = strchr(pbuffer, '\n')))
561 {
562 *pnl = '\0';
563 if (*(pnl-1) == '\r' ) *(pnl-1) = '\0';
565 parse_line (pbuffer);
567 pbuffer = ++pnl;
568 }
569 /* not completely read ? */
570 if (pbuffer - buffer < len)
571 {
572 len -= pbuffer - buffer;
573 memmove(buffer, pbuffer, len);
574 pbuffer = buffer + len;
575 }
576 else
577 pbuffer = buffer;
578 }
579 else if (FD_ISSET(fd_err, ©))
580 {
581 char *pnl;
583 len = read(fd_err, pbuffer_err, sizeof(buffer_err) - 1 - (pbuffer_err - buffer_err));
585 if (len < 0)
586 {
587 if (errno == EAGAIN || errno == EINTR) continue;
588 break;
589 }
590 else if (len == 0) break; /* We've reached EOF */
592 pbuffer_err[len] = '\0';
594 len += pbuffer_err - buffer_err;
595 pbuffer_err = buffer_err;
597 while ((pnl = strchr(pbuffer_err, '\n')))
598 {
599 *pnl = '\0';
600 if (*(pnl-1) == '\r' ) *(pnl-1) = '\0';
602 ERROR ("exec plugin: exec_read_one: error = %s", pbuffer_err);
604 pbuffer_err = ++pnl;
605 }
606 /* not completely read ? */
607 if (pbuffer_err - buffer_err < len)
608 {
609 len -= pbuffer_err - buffer_err;
610 memmove(buffer_err, pbuffer_err, len);
611 pbuffer_err = buffer_err + len;
612 }
613 else
614 pbuffer_err = buffer_err;
615 }
616 /* reset copy */
617 copy = fdset;
618 }
620 if (waitpid (pl->pid, &status, 0) > 0)
621 pl->status = status;
623 DEBUG ("exec plugin: Child %i exited with status %i.",
624 (int) pl->pid, pl->status);
626 pl->pid = 0;
628 pthread_mutex_lock (&pl_lock);
629 pl->flags &= ~PL_RUNNING;
630 pthread_mutex_unlock (&pl_lock);
632 close (fd);
633 close (fd_err);
635 pthread_exit ((void *) 0);
636 return (NULL);
637 } /* void *exec_read_one }}} */
639 static void *exec_notification_one (void *arg) /* {{{ */
640 {
641 program_list_t *pl = ((program_list_and_notification_t *) arg)->pl;
642 const notification_t *n = &((program_list_and_notification_t *) arg)->n;
643 int fd;
644 FILE *fh;
645 int pid;
646 int status;
647 const char *severity;
649 pid = fork_child (pl, &fd, NULL, NULL);
650 if (pid < 0) {
651 sfree (arg);
652 pthread_exit ((void *) 1);
653 }
655 fh = fdopen (fd, "w");
656 if (fh == NULL)
657 {
658 char errbuf[1024];
659 ERROR ("exec plugin: fdopen (%i) failed: %s", fd,
660 sstrerror (errno, errbuf, sizeof (errbuf)));
661 kill (pl->pid, SIGTERM);
662 pl->pid = 0;
663 close (fd);
664 sfree (arg);
665 pthread_exit ((void *) 1);
666 }
668 severity = "FAILURE";
669 if (n->severity == NOTIF_WARNING)
670 severity = "WARNING";
671 else if (n->severity == NOTIF_OKAY)
672 severity = "OKAY";
674 fprintf (fh,
675 "Severity: %s\n"
676 "Time: %u\n",
677 severity, (unsigned int) n->time);
679 /* Print the optional fields */
680 if (strlen (n->host) > 0)
681 fprintf (fh, "Host: %s\n", n->host);
682 if (strlen (n->plugin) > 0)
683 fprintf (fh, "Plugin: %s\n", n->plugin);
684 if (strlen (n->plugin_instance) > 0)
685 fprintf (fh, "PluginInstance: %s\n", n->plugin_instance);
686 if (strlen (n->type) > 0)
687 fprintf (fh, "Type: %s\n", n->type);
688 if (strlen (n->type_instance) > 0)
689 fprintf (fh, "TypeInstance: %s\n", n->type_instance);
691 fprintf (fh, "\n%s\n", n->message);
693 fflush (fh);
694 fclose (fh);
696 waitpid (pid, &status, 0);
698 DEBUG ("exec plugin: Child %i exited with status %i.",
699 pid, status);
701 sfree (arg);
702 pthread_exit ((void *) 0);
703 return (NULL);
704 } /* void *exec_notification_one }}} */
706 static int exec_init (void) /* {{{ */
707 {
708 struct sigaction sa;
710 memset (&sa, '\0', sizeof (sa));
711 sa.sa_handler = sigchld_handler;
712 sigaction (SIGCHLD, &sa, NULL);
714 return (0);
715 } /* int exec_init }}} */
717 static int exec_read (void) /* {{{ */
718 {
719 program_list_t *pl;
721 for (pl = pl_head; pl != NULL; pl = pl->next)
722 {
723 pthread_t t;
724 pthread_attr_t attr;
726 /* Only execute `normal' style executables here. */
727 if ((pl->flags & PL_NORMAL) == 0)
728 continue;
730 pthread_mutex_lock (&pl_lock);
731 /* Skip if a child is already running. */
732 if ((pl->flags & PL_RUNNING) != 0)
733 {
734 pthread_mutex_unlock (&pl_lock);
735 continue;
736 }
737 pl->flags |= PL_RUNNING;
738 pthread_mutex_unlock (&pl_lock);
740 pthread_attr_init (&attr);
741 pthread_attr_setdetachstate (&attr, PTHREAD_CREATE_DETACHED);
742 pthread_create (&t, &attr, exec_read_one, (void *) pl);
743 } /* for (pl) */
745 return (0);
746 } /* int exec_read }}} */
748 static int exec_notification (const notification_t *n)
749 {
750 program_list_t *pl;
751 program_list_and_notification_t *pln;
753 for (pl = pl_head; pl != NULL; pl = pl->next)
754 {
755 pthread_t t;
756 pthread_attr_t attr;
758 /* Only execute `notification' style executables here. */
759 if ((pl->flags & PL_NOTIF_ACTION) == 0)
760 continue;
762 /* Skip if a child is already running. */
763 if (pl->pid != 0)
764 continue;
766 pln = (program_list_and_notification_t *) malloc (sizeof
767 (program_list_and_notification_t));
768 if (pln == NULL)
769 {
770 ERROR ("exec plugin: malloc failed.");
771 continue;
772 }
774 pln->pl = pl;
775 memcpy (&pln->n, n, sizeof (notification_t));
777 pthread_attr_init (&attr);
778 pthread_attr_setdetachstate (&attr, PTHREAD_CREATE_DETACHED);
779 pthread_create (&t, &attr, exec_notification_one, (void *) pln);
780 } /* for (pl) */
782 return (0);
783 } /* int exec_notification */
785 static int exec_shutdown (void) /* {{{ */
786 {
787 program_list_t *pl;
788 program_list_t *next;
790 pl = pl_head;
791 while (pl != NULL)
792 {
793 next = pl->next;
795 if (pl->pid > 0)
796 {
797 kill (pl->pid, SIGTERM);
798 INFO ("exec plugin: Sent SIGTERM to %hu", (unsigned short int) pl->pid);
799 }
801 sfree (pl->user);
802 sfree (pl);
804 pl = next;
805 } /* while (pl) */
806 pl_head = NULL;
808 return (0);
809 } /* int exec_shutdown }}} */
811 void module_register (void)
812 {
813 plugin_register_complex_config ("exec", exec_config);
814 plugin_register_init ("exec", exec_init);
815 plugin_register_read ("exec", exec_read);
816 plugin_register_notification ("exec", exec_notification);
817 plugin_register_shutdown ("exec", exec_shutdown);
818 } /* void module_register */
820 /*
821 * vim:shiftwidth=2:softtabstop=2:tabstop=8:fdm=marker
822 */