1 /**
2 * collectd - src/exec.c
3 * Copyright (C) 2007-2010 Florian octo Forster
4 * Copyright (C) 2007-2009 Sebastian Harl
5 * Copyright (C) 2008 Peter Holik
6 *
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation; only version 2 of the License is applicable.
10 *
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 *
20 * Authors:
21 * Florian octo Forster <octo at collectd.org>
22 * Sebastian Harl <sh at tokkee.org>
23 * Peter Holik <peter at holik.at>
24 **/
26 #define _DEFAULT_SOURCE
27 #define _BSD_SOURCE /* For setgroups */
29 #include "collectd.h"
30 #include "common.h"
31 #include "plugin.h"
33 #include "utils_cmd_putval.h"
34 #include "utils_cmd_putnotif.h"
36 #include <sys/types.h>
37 #include <pwd.h>
38 #include <grp.h>
39 #include <signal.h>
41 #define PL_NORMAL 0x01
42 #define PL_NOTIF_ACTION 0x02
44 #define PL_RUNNING 0x10
46 /*
47 * Private data types
48 */
49 /*
50 * Access to this structure is serialized using the `pl_lock' lock and the
51 * `PL_RUNNING' flag. The execution of notifications is *not* serialized, so
52 * all functions used to handle notifications MUST NOT write to this structure.
53 * The `pid' and `status' fields are thus unused if the `PL_NOTIF_ACTION' flag
54 * is set.
55 * The `PL_RUNNING' flag is set in `exec_read' and unset in `exec_read_one'.
56 */
57 struct program_list_s;
58 typedef struct program_list_s program_list_t;
59 struct program_list_s
60 {
61 char *user;
62 char *group;
63 char *exec;
64 char **argv;
65 int pid;
66 int status;
67 int flags;
68 program_list_t *next;
69 };
71 typedef struct program_list_and_notification_s
72 {
73 program_list_t *pl;
74 notification_t n;
75 } program_list_and_notification_t;
77 /*
78 * Private variables
79 */
80 static program_list_t *pl_head = NULL;
81 static pthread_mutex_t pl_lock = PTHREAD_MUTEX_INITIALIZER;
83 /*
84 * Functions
85 */
86 static void sigchld_handler (int __attribute__((unused)) signal) /* {{{ */
87 {
88 pid_t pid;
89 int status;
90 while ((pid = waitpid (-1, &status, WNOHANG)) > 0)
91 {
92 program_list_t *pl;
93 for (pl = pl_head; pl != NULL; pl = pl->next)
94 if (pl->pid == pid)
95 break;
96 if (pl != NULL)
97 pl->status = status;
98 } /* while (waitpid) */
99 } /* void sigchld_handler }}} */
101 static int exec_config_exec (oconfig_item_t *ci) /* {{{ */
102 {
103 program_list_t *pl;
104 char buffer[128];
105 int i;
107 if (ci->children_num != 0)
108 {
109 WARNING ("exec plugin: The config option `%s' may not be a block.",
110 ci->key);
111 return (-1);
112 }
113 if (ci->values_num < 2)
114 {
115 WARNING ("exec plugin: The config option `%s' needs at least two "
116 "arguments.", ci->key);
117 return (-1);
118 }
119 if ((ci->values[0].type != OCONFIG_TYPE_STRING)
120 || (ci->values[1].type != OCONFIG_TYPE_STRING))
121 {
122 WARNING ("exec plugin: The first two arguments to the `%s' option must "
123 "be string arguments.", ci->key);
124 return (-1);
125 }
127 pl = calloc (1, sizeof (*pl));
128 if (pl == NULL)
129 {
130 ERROR ("exec plugin: calloc failed.");
131 return (-1);
132 }
134 if (strcasecmp ("NotificationExec", ci->key) == 0)
135 pl->flags |= PL_NOTIF_ACTION;
136 else
137 pl->flags |= PL_NORMAL;
139 pl->user = strdup (ci->values[0].value.string);
140 if (pl->user == NULL)
141 {
142 ERROR ("exec plugin: strdup failed.");
143 sfree (pl);
144 return (-1);
145 }
147 pl->group = strchr (pl->user, ':');
148 if (pl->group != NULL)
149 {
150 *pl->group = '\0';
151 pl->group++;
152 }
154 pl->exec = strdup (ci->values[1].value.string);
155 if (pl->exec == NULL)
156 {
157 ERROR ("exec plugin: strdup failed.");
158 sfree (pl->user);
159 sfree (pl);
160 return (-1);
161 }
163 pl->argv = calloc (ci->values_num, sizeof (*pl->argv));
164 if (pl->argv == NULL)
165 {
166 ERROR ("exec plugin: calloc failed.");
167 sfree (pl->exec);
168 sfree (pl->user);
169 sfree (pl);
170 return (-1);
171 }
173 {
174 char *tmp = strrchr (ci->values[1].value.string, '/');
175 if (tmp == NULL)
176 sstrncpy (buffer, ci->values[1].value.string, sizeof (buffer));
177 else
178 sstrncpy (buffer, tmp + 1, sizeof (buffer));
179 }
180 pl->argv[0] = strdup (buffer);
181 if (pl->argv[0] == NULL)
182 {
183 ERROR ("exec plugin: strdup failed.");
184 sfree (pl->argv);
185 sfree (pl->exec);
186 sfree (pl->user);
187 sfree (pl);
188 return (-1);
189 }
191 for (i = 1; i < (ci->values_num - 1); i++)
192 {
193 if (ci->values[i + 1].type == OCONFIG_TYPE_STRING)
194 {
195 pl->argv[i] = strdup (ci->values[i + 1].value.string);
196 }
197 else
198 {
199 if (ci->values[i + 1].type == OCONFIG_TYPE_NUMBER)
200 {
201 ssnprintf (buffer, sizeof (buffer), "%lf",
202 ci->values[i + 1].value.number);
203 }
204 else
205 {
206 if (ci->values[i + 1].value.boolean)
207 sstrncpy (buffer, "true", sizeof (buffer));
208 else
209 sstrncpy (buffer, "false", sizeof (buffer));
210 }
212 pl->argv[i] = strdup (buffer);
213 }
215 if (pl->argv[i] == NULL)
216 {
217 ERROR ("exec plugin: strdup failed.");
218 break;
219 }
220 } /* for (i) */
222 if (i < (ci->values_num - 1))
223 {
224 while ((--i) >= 0)
225 {
226 sfree (pl->argv[i]);
227 }
228 sfree (pl->argv);
229 sfree (pl->exec);
230 sfree (pl->user);
231 sfree (pl);
232 return (-1);
233 }
235 for (i = 0; pl->argv[i] != NULL; i++)
236 {
237 DEBUG ("exec plugin: argv[%i] = %s", i, pl->argv[i]);
238 }
240 pl->next = pl_head;
241 pl_head = pl;
243 return (0);
244 } /* int exec_config_exec }}} */
246 static int exec_config (oconfig_item_t *ci) /* {{{ */
247 {
248 int i;
250 for (i = 0; i < ci->children_num; i++)
251 {
252 oconfig_item_t *child = ci->children + i;
253 if ((strcasecmp ("Exec", child->key) == 0)
254 || (strcasecmp ("NotificationExec", child->key) == 0))
255 exec_config_exec (child);
256 else
257 {
258 WARNING ("exec plugin: Unknown config option `%s'.", child->key);
259 }
260 } /* for (i) */
262 return (0);
263 } /* int exec_config }}} */
265 static void set_environment (void) /* {{{ */
266 {
267 char buffer[1024];
269 #ifdef HAVE_SETENV
270 ssnprintf (buffer, sizeof (buffer), "%.3f",
271 CDTIME_T_TO_DOUBLE (plugin_get_interval ()));
272 setenv ("COLLECTD_INTERVAL", buffer, /* overwrite = */ 1);
274 ssnprintf (buffer, sizeof (buffer), "%s", hostname_g);
275 setenv ("COLLECTD_HOSTNAME", buffer, /* overwrite = */ 1);
276 #else
277 ssnprintf (buffer, sizeof (buffer), "COLLECTD_INTERVAL=%.3f",
278 CDTIME_T_TO_DOUBLE (plugin_get_interval ()));
279 putenv (buffer);
281 ssnprintf (buffer, sizeof (buffer), "COLLECTD_HOSTNAME=%s", hostname_g);
282 putenv (buffer);
283 #endif
284 } /* }}} void set_environment */
286 __attribute__((noreturn))
287 static void exec_child (program_list_t *pl, int uid, int gid, int egid) /* {{{ */
288 {
289 int status;
290 char errbuf[1024];
292 #if HAVE_SETGROUPS
293 if (getuid () == 0)
294 {
295 gid_t glist[2];
296 size_t glist_len;
298 glist[0] = gid;
299 glist_len = 1;
301 if ((gid != egid) && (egid != -1))
302 {
303 glist[1] = egid;
304 glist_len = 2;
305 }
307 setgroups (glist_len, glist);
308 }
309 #endif /* HAVE_SETGROUPS */
311 status = setgid (gid);
312 if (status != 0)
313 {
314 ERROR ("exec plugin: setgid (%i) failed: %s",
315 gid, sstrerror (errno, errbuf, sizeof (errbuf)));
316 exit (-1);
317 }
319 if (egid != -1)
320 {
321 status = setegid (egid);
322 if (status != 0)
323 {
324 ERROR ("exec plugin: setegid (%i) failed: %s",
325 egid, sstrerror (errno, errbuf, sizeof (errbuf)));
326 exit (-1);
327 }
328 }
330 status = setuid (uid);
331 if (status != 0)
332 {
333 ERROR ("exec plugin: setuid (%i) failed: %s",
334 uid, sstrerror (errno, errbuf, sizeof (errbuf)));
335 exit (-1);
336 }
338 execvp (pl->exec, pl->argv);
340 ERROR ("exec plugin: Failed to execute ``%s'': %s",
341 pl->exec, sstrerror (errno, errbuf, sizeof (errbuf)));
342 exit (-1);
343 } /* void exec_child }}} */
345 static void reset_signal_mask (void) /* {{{ */
346 {
347 sigset_t ss;
349 sigemptyset (&ss);
350 sigprocmask (SIG_SETMASK, &ss, /* old mask = */ NULL);
351 } /* }}} void reset_signal_mask */
353 static int create_pipe (int fd_pipe[2]) /* {{{ */
354 {
355 char errbuf[1024];
356 int status;
358 status = pipe (fd_pipe);
359 if (status != 0)
360 {
361 ERROR ("exec plugin: pipe failed: %s",
362 sstrerror (errno, errbuf, sizeof (errbuf)));
363 return (-1);
364 }
366 return 0;
367 } /* }}} int create_pipe */
369 static void close_pipe (int fd_pipe[2]) /* {{{ */
370 {
371 if (fd_pipe[0] != -1)
372 close (fd_pipe[0]);
374 if (fd_pipe[1] != -1)
375 close (fd_pipe[1]);
376 } /* }}} void close_pipe */
378 /*
379 * Creates three pipes (one for reading, one for writing and one for errors),
380 * forks a child, sets up the pipes so that fd_in is connected to STDIN of
381 * the child and fd_out is connected to STDOUT and fd_err is connected to STDERR
382 * of the child. Then is calls `exec_child'.
383 */
384 static int fork_child (program_list_t *pl, int *fd_in, int *fd_out, int *fd_err) /* {{{ */
385 {
386 int fd_pipe_in[2] = {-1, -1};
387 int fd_pipe_out[2] = {-1, -1};
388 int fd_pipe_err[2] = {-1, -1};
389 char errbuf[1024];
390 int status;
391 int pid;
393 int uid;
394 int gid;
395 int egid;
397 struct passwd *sp_ptr;
398 struct passwd sp;
399 char nambuf[2048];
401 if (pl->pid != 0)
402 return (-1);
404 if ((create_pipe(fd_pipe_in) == -1)
405 || (create_pipe(fd_pipe_out) == -1)
406 || (create_pipe(fd_pipe_err) == -1))
407 goto failed;
409 sp_ptr = NULL;
410 status = getpwnam_r (pl->user, &sp, nambuf, sizeof (nambuf), &sp_ptr);
411 if (status != 0)
412 {
413 ERROR ("exec plugin: Failed to get user information for user ``%s'': %s",
414 pl->user, sstrerror (errno, errbuf, sizeof (errbuf)));
415 goto failed;
416 }
418 if (sp_ptr == NULL)
419 {
420 ERROR ("exec plugin: No such user: `%s'", pl->user);
421 goto failed;
422 }
424 uid = sp.pw_uid;
425 gid = sp.pw_gid;
426 if (uid == 0)
427 {
428 ERROR ("exec plugin: Cowardly refusing to exec program as root.");
429 goto failed;
430 }
432 /* The group configured in the configfile is set as effective group, because
433 * this way the forked process can (re-)gain the user's primary group. */
434 egid = -1;
435 if (NULL != pl->group)
436 {
437 if ('\0' != *pl->group) {
438 struct group *gr_ptr = NULL;
439 struct group gr;
441 status = getgrnam_r (pl->group, &gr, nambuf, sizeof (nambuf), &gr_ptr);
442 if (0 != status)
443 {
444 ERROR ("exec plugin: Failed to get group information "
445 "for group ``%s'': %s", pl->group,
446 sstrerror (errno, errbuf, sizeof (errbuf)));
447 goto failed;
448 }
449 if (NULL == gr_ptr)
450 {
451 ERROR ("exec plugin: No such group: `%s'", pl->group);
452 goto failed;
453 }
455 egid = gr.gr_gid;
456 }
457 else
458 {
459 egid = gid;
460 }
461 } /* if (pl->group == NULL) */
463 pid = fork ();
464 if (pid < 0)
465 {
466 ERROR ("exec plugin: fork failed: %s",
467 sstrerror (errno, errbuf, sizeof (errbuf)));
468 goto failed;
469 }
470 else if (pid == 0)
471 {
472 int fd_num;
473 int fd;
475 /* Close all file descriptors but the pipe end we need. */
476 fd_num = getdtablesize ();
477 for (fd = 0; fd < fd_num; fd++)
478 {
479 if ((fd == fd_pipe_in[0])
480 || (fd == fd_pipe_out[1])
481 || (fd == fd_pipe_err[1]))
482 continue;
483 close (fd);
484 }
486 /* Connect the `in' pipe to STDIN */
487 if (fd_pipe_in[0] != STDIN_FILENO)
488 {
489 dup2 (fd_pipe_in[0], STDIN_FILENO);
490 close (fd_pipe_in[0]);
491 }
493 /* Now connect the `out' pipe to STDOUT */
494 if (fd_pipe_out[1] != STDOUT_FILENO)
495 {
496 dup2 (fd_pipe_out[1], STDOUT_FILENO);
497 close (fd_pipe_out[1]);
498 }
500 /* Now connect the `err' pipe to STDERR */
501 if (fd_pipe_err[1] != STDERR_FILENO)
502 {
503 dup2 (fd_pipe_err[1], STDERR_FILENO);
504 close (fd_pipe_err[1]);
505 }
507 set_environment ();
509 /* Unblock all signals */
510 reset_signal_mask ();
512 exec_child (pl, uid, gid, egid);
513 /* does not return */
514 }
516 close (fd_pipe_in[0]);
517 close (fd_pipe_out[1]);
518 close (fd_pipe_err[1]);
520 if (fd_in != NULL)
521 *fd_in = fd_pipe_in[1];
522 else
523 close (fd_pipe_in[1]);
525 if (fd_out != NULL)
526 *fd_out = fd_pipe_out[0];
527 else
528 close (fd_pipe_out[0]);
530 if (fd_err != NULL)
531 *fd_err = fd_pipe_err[0];
532 else
533 close (fd_pipe_err[0]);
535 return (pid);
537 failed:
538 close_pipe(fd_pipe_in);
539 close_pipe(fd_pipe_out);
540 close_pipe(fd_pipe_err);
542 return (-1);
543 } /* int fork_child }}} */
545 static int parse_line (char *buffer) /* {{{ */
546 {
547 if (strncasecmp ("PUTVAL", buffer, strlen ("PUTVAL")) == 0)
548 return (handle_putval (stdout, buffer));
549 else if (strncasecmp ("PUTNOTIF", buffer, strlen ("PUTNOTIF")) == 0)
550 return (handle_putnotif (stdout, buffer));
551 else
552 {
553 ERROR ("exec plugin: Unable to parse command, ignoring line: \"%s\"",
554 buffer);
555 return (-1);
556 }
557 } /* int parse_line }}} */
559 static void *exec_read_one (void *arg) /* {{{ */
560 {
561 program_list_t *pl = (program_list_t *) arg;
562 int fd, fd_err, highest_fd;
563 fd_set fdset, copy;
564 int status;
565 char buffer[1200]; /* if not completely read */
566 char buffer_err[1024];
567 char *pbuffer = buffer;
568 char *pbuffer_err = buffer_err;
570 status = fork_child (pl, NULL, &fd, &fd_err);
571 if (status < 0)
572 {
573 /* Reset the "running" flag */
574 pthread_mutex_lock (&pl_lock);
575 pl->flags &= ~PL_RUNNING;
576 pthread_mutex_unlock (&pl_lock);
577 pthread_exit ((void *) 1);
578 }
579 pl->pid = status;
581 assert (pl->pid != 0);
583 FD_ZERO( &fdset );
584 FD_SET(fd, &fdset);
585 FD_SET(fd_err, &fdset);
587 /* Determine the highest file descriptor */
588 highest_fd = (fd > fd_err) ? fd : fd_err;
590 /* We use a copy of fdset, as select modifies it */
591 copy = fdset;
593 while (1)
594 {
595 int len;
597 status = select (highest_fd + 1, ©, NULL, NULL, NULL);
598 if (status < 0)
599 {
600 if (errno == EINTR)
601 continue;
602 break;
603 }
605 if (FD_ISSET(fd, ©))
606 {
607 char *pnl;
609 len = read(fd, pbuffer, sizeof(buffer) - 1 - (pbuffer - buffer));
611 if (len < 0)
612 {
613 if (errno == EAGAIN || errno == EINTR) continue;
614 break;
615 }
616 else if (len == 0) break; /* We've reached EOF */
618 pbuffer[len] = '\0';
620 len += pbuffer - buffer;
621 pbuffer = buffer;
623 while ((pnl = strchr(pbuffer, '\n')))
624 {
625 *pnl = '\0';
626 if (*(pnl-1) == '\r' ) *(pnl-1) = '\0';
628 parse_line (pbuffer);
630 pbuffer = ++pnl;
631 }
632 /* not completely read ? */
633 if (pbuffer - buffer < len)
634 {
635 len -= pbuffer - buffer;
636 memmove(buffer, pbuffer, len);
637 pbuffer = buffer + len;
638 }
639 else
640 pbuffer = buffer;
641 }
642 else if (FD_ISSET(fd_err, ©))
643 {
644 char *pnl;
646 len = read(fd_err, pbuffer_err, sizeof(buffer_err) - 1 - (pbuffer_err - buffer_err));
648 if (len < 0)
649 {
650 if (errno == EAGAIN || errno == EINTR)
651 continue;
652 break;
653 }
654 else if (len == 0)
655 {
656 /* We've reached EOF */
657 NOTICE ("exec plugin: Program `%s' has closed STDERR.", pl->exec);
659 /* Remove file descriptor form select() set. */
660 FD_CLR (fd_err, &fdset);
661 copy = fdset;
662 highest_fd = fd;
664 /* Clean up file descriptor */
665 close (fd_err);
666 fd_err = -1;
667 continue;
668 }
670 pbuffer_err[len] = '\0';
672 len += pbuffer_err - buffer_err;
673 pbuffer_err = buffer_err;
675 while ((pnl = strchr(pbuffer_err, '\n')))
676 {
677 *pnl = '\0';
678 if (*(pnl-1) == '\r' ) *(pnl-1) = '\0';
680 ERROR ("exec plugin: exec_read_one: error = %s", pbuffer_err);
682 pbuffer_err = ++pnl;
683 }
684 /* not completely read ? */
685 if (pbuffer_err - buffer_err < len)
686 {
687 len -= pbuffer_err - buffer_err;
688 memmove(buffer_err, pbuffer_err, len);
689 pbuffer_err = buffer_err + len;
690 }
691 else
692 pbuffer_err = buffer_err;
693 }
694 /* reset copy */
695 copy = fdset;
696 }
698 DEBUG ("exec plugin: exec_read_one: Waiting for `%s' to exit.", pl->exec);
699 if (waitpid (pl->pid, &status, 0) > 0)
700 pl->status = status;
702 DEBUG ("exec plugin: Child %i exited with status %i.",
703 (int) pl->pid, pl->status);
705 pl->pid = 0;
707 pthread_mutex_lock (&pl_lock);
708 pl->flags &= ~PL_RUNNING;
709 pthread_mutex_unlock (&pl_lock);
711 close (fd);
712 if (fd_err >= 0)
713 close (fd_err);
715 pthread_exit ((void *) 0);
716 return (NULL);
717 } /* void *exec_read_one }}} */
719 static void *exec_notification_one (void *arg) /* {{{ */
720 {
721 program_list_t *pl = ((program_list_and_notification_t *) arg)->pl;
722 notification_t *n = &((program_list_and_notification_t *) arg)->n;
723 notification_meta_t *meta;
724 int fd;
725 FILE *fh;
726 int pid;
727 int status;
728 const char *severity;
730 pid = fork_child (pl, &fd, NULL, NULL);
731 if (pid < 0) {
732 sfree (arg);
733 pthread_exit ((void *) 1);
734 }
736 fh = fdopen (fd, "w");
737 if (fh == NULL)
738 {
739 char errbuf[1024];
740 ERROR ("exec plugin: fdopen (%i) failed: %s", fd,
741 sstrerror (errno, errbuf, sizeof (errbuf)));
742 kill (pid, SIGTERM);
743 close (fd);
744 sfree (arg);
745 pthread_exit ((void *) 1);
746 }
748 severity = "FAILURE";
749 if (n->severity == NOTIF_WARNING)
750 severity = "WARNING";
751 else if (n->severity == NOTIF_OKAY)
752 severity = "OKAY";
754 fprintf (fh,
755 "Severity: %s\n"
756 "Time: %.3f\n",
757 severity, CDTIME_T_TO_DOUBLE (n->time));
759 /* Print the optional fields */
760 if (strlen (n->host) > 0)
761 fprintf (fh, "Host: %s\n", n->host);
762 if (strlen (n->plugin) > 0)
763 fprintf (fh, "Plugin: %s\n", n->plugin);
764 if (strlen (n->plugin_instance) > 0)
765 fprintf (fh, "PluginInstance: %s\n", n->plugin_instance);
766 if (strlen (n->type) > 0)
767 fprintf (fh, "Type: %s\n", n->type);
768 if (strlen (n->type_instance) > 0)
769 fprintf (fh, "TypeInstance: %s\n", n->type_instance);
771 for (meta = n->meta; meta != NULL; meta = meta->next)
772 {
773 if (meta->type == NM_TYPE_STRING)
774 fprintf (fh, "%s: %s\n", meta->name, meta->nm_value.nm_string);
775 else if (meta->type == NM_TYPE_SIGNED_INT)
776 fprintf (fh, "%s: %"PRIi64"\n", meta->name, meta->nm_value.nm_signed_int);
777 else if (meta->type == NM_TYPE_UNSIGNED_INT)
778 fprintf (fh, "%s: %"PRIu64"\n", meta->name, meta->nm_value.nm_unsigned_int);
779 else if (meta->type == NM_TYPE_DOUBLE)
780 fprintf (fh, "%s: %e\n", meta->name, meta->nm_value.nm_double);
781 else if (meta->type == NM_TYPE_BOOLEAN)
782 fprintf (fh, "%s: %s\n", meta->name,
783 meta->nm_value.nm_boolean ? "true" : "false");
784 }
786 fprintf (fh, "\n%s\n", n->message);
788 fflush (fh);
789 fclose (fh);
791 waitpid (pid, &status, 0);
793 DEBUG ("exec plugin: Child %i exited with status %i.",
794 pid, status);
796 if (n->meta != NULL)
797 plugin_notification_meta_free (n->meta);
798 n->meta = NULL;
799 sfree (arg);
800 pthread_exit ((void *) 0);
801 return (NULL);
802 } /* void *exec_notification_one }}} */
804 static int exec_init (void) /* {{{ */
805 {
806 struct sigaction sa = {
807 .sa_handler = sigchld_handler
808 };
810 sigaction (SIGCHLD, &sa, NULL);
812 return (0);
813 } /* int exec_init }}} */
815 static int exec_read (void) /* {{{ */
816 {
817 program_list_t *pl;
819 for (pl = pl_head; pl != NULL; pl = pl->next)
820 {
821 pthread_t t;
822 pthread_attr_t attr;
824 /* Only execute `normal' style executables here. */
825 if ((pl->flags & PL_NORMAL) == 0)
826 continue;
828 pthread_mutex_lock (&pl_lock);
829 /* Skip if a child is already running. */
830 if ((pl->flags & PL_RUNNING) != 0)
831 {
832 pthread_mutex_unlock (&pl_lock);
833 continue;
834 }
835 pl->flags |= PL_RUNNING;
836 pthread_mutex_unlock (&pl_lock);
838 pthread_attr_init (&attr);
839 pthread_attr_setdetachstate (&attr, PTHREAD_CREATE_DETACHED);
840 plugin_thread_create (&t, &attr, exec_read_one, (void *) pl);
841 pthread_attr_destroy (&attr);
842 } /* for (pl) */
844 return (0);
845 } /* int exec_read }}} */
847 static int exec_notification (const notification_t *n, /* {{{ */
848 user_data_t __attribute__((unused)) *user_data)
849 {
850 program_list_t *pl;
851 program_list_and_notification_t *pln;
853 for (pl = pl_head; pl != NULL; pl = pl->next)
854 {
855 pthread_t t;
856 pthread_attr_t attr;
858 /* Only execute `notification' style executables here. */
859 if ((pl->flags & PL_NOTIF_ACTION) == 0)
860 continue;
862 /* Skip if a child is already running. */
863 if (pl->pid != 0)
864 continue;
866 pln = malloc (sizeof (*pln));
867 if (pln == NULL)
868 {
869 ERROR ("exec plugin: malloc failed.");
870 continue;
871 }
873 pln->pl = pl;
874 memcpy (&pln->n, n, sizeof (notification_t));
876 /* Set the `meta' member to NULL, otherwise `plugin_notification_meta_copy'
877 * will run into an endless loop. */
878 pln->n.meta = NULL;
879 plugin_notification_meta_copy (&pln->n, n);
881 pthread_attr_init (&attr);
882 pthread_attr_setdetachstate (&attr, PTHREAD_CREATE_DETACHED);
883 plugin_thread_create (&t, &attr, exec_notification_one, (void *) pln);
884 pthread_attr_destroy (&attr);
885 } /* for (pl) */
887 return (0);
888 } /* }}} int exec_notification */
890 static int exec_shutdown (void) /* {{{ */
891 {
892 program_list_t *pl;
893 program_list_t *next;
895 pl = pl_head;
896 while (pl != NULL)
897 {
898 next = pl->next;
900 if (pl->pid > 0)
901 {
902 kill (pl->pid, SIGTERM);
903 INFO ("exec plugin: Sent SIGTERM to %hu", (unsigned short int) pl->pid);
904 }
906 sfree (pl->user);
907 sfree (pl);
909 pl = next;
910 } /* while (pl) */
911 pl_head = NULL;
913 return (0);
914 } /* int exec_shutdown }}} */
916 void module_register (void)
917 {
918 plugin_register_complex_config ("exec", exec_config);
919 plugin_register_init ("exec", exec_init);
920 plugin_register_read ("exec", exec_read);
921 plugin_register_notification ("exec", exec_notification,
922 /* user_data = */ NULL);
923 plugin_register_shutdown ("exec", exec_shutdown);
924 } /* void module_register */
926 /*
927 * vim:shiftwidth=2:softtabstop=2:tabstop=8:fdm=marker
928 */