Merge branch 'collectd-4.6'
[collectd.git] / src / exec.c
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  **/
21
22 #include "collectd.h"
23 #include "common.h"
24 #include "plugin.h"
25
26 #include "utils_cmd_putval.h"
27 #include "utils_cmd_putnotif.h"
28
29 #include <sys/types.h>
30 #include <pwd.h>
31 #include <grp.h>
32 #include <signal.h>
33
34 #include <pthread.h>
35
36 #define PL_NORMAL        0x01
37 #define PL_NOTIF_ACTION  0x02
38
39 #define PL_RUNNING       0x10
40
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 };
65
66 typedef struct program_list_and_notification_s
67 {
68   program_list_t *pl;
69   notification_t n;
70 } program_list_and_notification_t;
71
72 /*
73  * Private variables
74  */
75 static program_list_t *pl_head = NULL;
76 static pthread_mutex_t pl_lock = PTHREAD_MUTEX_INITIALIZER;
77
78 /*
79  * Functions
80  */
81 static void sigchld_handler (int __attribute__((unused)) 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 }}} */
95
96 static int exec_config_exec (oconfig_item_t *ci) /* {{{ */
97 {
98   program_list_t *pl;
99   char buffer[128];
100   int i;
101
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   }
121
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));
129
130   if (strcasecmp ("NotificationExec", ci->key) == 0)
131     pl->flags |= PL_NOTIF_ACTION;
132   else
133     pl->flags |= PL_NORMAL;
134
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   }
142
143   pl->group = strchr (pl->user, ':');
144   if (pl->group != NULL)
145   {
146     *pl->group = '\0';
147     pl->group++;
148   }
149
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   }
158
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 *));
169
170   {
171     char *tmp = strrchr (ci->values[1].value.string, '/');
172     if (tmp == NULL)
173       sstrncpy (buffer, ci->values[1].value.string, sizeof (buffer));
174     else
175       sstrncpy (buffer, tmp + 1, sizeof (buffer));
176   }
177   pl->argv[0] = strdup (buffer);
178   if (pl->argv[0] == NULL)
179   {
180     ERROR ("exec plugin: malloc failed.");
181     sfree (pl->argv);
182     sfree (pl->exec);
183     sfree (pl->user);
184     sfree (pl);
185     return (-1);
186   }
187
188   for (i = 1; i < (ci->values_num - 1); i++)
189   {
190     if (ci->values[i + 1].type == OCONFIG_TYPE_STRING)
191     {
192       pl->argv[i] = strdup (ci->values[i + 1].value.string);
193     }
194     else
195     {
196       if (ci->values[i + 1].type == OCONFIG_TYPE_NUMBER)
197       {
198         ssnprintf (buffer, sizeof (buffer), "%lf",
199             ci->values[i + 1].value.number);
200       }
201       else
202       {
203         if (ci->values[i + 1].value.boolean)
204           sstrncpy (buffer, "true", sizeof (buffer));
205         else
206           sstrncpy (buffer, "false", sizeof (buffer));
207       }
208
209       pl->argv[i] = strdup (buffer);
210     }
211
212     if (pl->argv[i] == NULL)
213     {
214       ERROR ("exec plugin: strdup failed.");
215       break;
216     }
217   } /* for (i) */
218
219   if (i < (ci->values_num - 1))
220   {
221     while ((--i) >= 0)
222     {
223       sfree (pl->argv[i]);
224     }
225     sfree (pl->argv);
226     sfree (pl->exec);
227     sfree (pl->user);
228     sfree (pl);
229     return (-1);
230   }
231
232   for (i = 0; pl->argv[i] != NULL; i++)
233   {
234     DEBUG ("exec plugin: argv[%i] = %s", i, pl->argv[i]);
235   }
236
237   pl->next = pl_head;
238   pl_head = pl;
239
240   return (0);
241 } /* int exec_config_exec }}} */
242
243 static int exec_config (oconfig_item_t *ci) /* {{{ */
244 {
245   int i;
246
247   for (i = 0; i < ci->children_num; i++)
248   {
249     oconfig_item_t *child = ci->children + i;
250     if ((strcasecmp ("Exec", child->key) == 0)
251         || (strcasecmp ("NotificationExec", child->key) == 0))
252       exec_config_exec (child);
253     else
254     {
255       WARNING ("exec plugin: Unknown config option `%s'.", child->key);
256     }
257   } /* for (i) */
258
259   return (0);
260 } /* int exec_config }}} */
261
262 static void exec_child (program_list_t *pl) /* {{{ */
263 {
264   int status;
265   int uid;
266   int gid;
267   int egid;
268
269   struct passwd *sp_ptr;
270   struct passwd sp;
271   char nambuf[2048];
272   char errbuf[1024];
273
274   sp_ptr = NULL;
275   status = getpwnam_r (pl->user, &sp, nambuf, sizeof (nambuf), &sp_ptr);
276   if (status != 0)
277   {
278     ERROR ("exec plugin: getpwnam_r failed: %s",
279         sstrerror (errno, errbuf, sizeof (errbuf)));
280     exit (-1);
281   }
282   if (sp_ptr == NULL)
283   {
284     ERROR ("exec plugin: No such user: `%s'", pl->user);
285     exit (-1);
286   }
287
288   uid = sp.pw_uid;
289   gid = sp.pw_gid;
290   if (uid == 0)
291   {
292     ERROR ("exec plugin: Cowardly refusing to exec program as root.");
293     exit (-1);
294   }
295
296   /* The group configured in the configfile is set as effective group, because
297    * this way the forked process can (re-)gain the user's primary group. */
298   egid = -1;
299   if (NULL != pl->group)
300   {
301     if ('\0' != *pl->group) {
302       struct group *gr_ptr = NULL;
303       struct group gr;
304
305       status = getgrnam_r (pl->group, &gr, nambuf, sizeof (nambuf), &gr_ptr);
306       if (0 != status)
307       {
308         ERROR ("exec plugin: getgrnam_r failed: %s",
309             sstrerror (errno, errbuf, sizeof (errbuf)));
310         exit (-1);
311       }
312       if (NULL == gr_ptr)
313       {
314         ERROR ("exec plugin: No such group: `%s'", pl->group);
315         exit (-1);
316       }
317
318       egid = gr.gr_gid;
319     }
320     else
321     {
322       egid = gid;
323     }
324   } /* if (pl->group == NULL) */
325
326 #if HAVE_SETGROUPS
327   if (getuid () == 0)
328   {
329     gid_t  glist[2];
330     size_t glist_len;
331
332     glist[0] = gid;
333     glist_len = 1;
334
335     if ((gid != egid) && (egid != -1))
336     {
337       glist[1] = egid;
338       glist_len = 2;
339     }
340
341     setgroups (glist_len, glist);
342   }
343 #endif /* HAVE_SETGROUPS */
344
345   status = setgid (gid);
346   if (status != 0)
347   {
348     ERROR ("exec plugin: setgid (%i) failed: %s",
349         gid, sstrerror (errno, errbuf, sizeof (errbuf)));
350     exit (-1);
351   }
352
353   if (egid != -1)
354   {
355     status = setegid (egid);
356     if (status != 0)
357     {
358       ERROR ("exec plugin: setegid (%i) failed: %s",
359           egid, sstrerror (errno, errbuf, sizeof (errbuf)));
360       exit (-1);
361     }
362   }
363
364   status = setuid (uid);
365   if (status != 0)
366   {
367     ERROR ("exec plugin: setuid (%i) failed: %s",
368         uid, sstrerror (errno, errbuf, sizeof (errbuf)));
369     exit (-1);
370   }
371
372   status = execvp (pl->exec, pl->argv);
373
374   ERROR ("exec plugin: exec failed: %s",
375       sstrerror (errno, errbuf, sizeof (errbuf)));
376   exit (-1);
377 } /* void exec_child }}} */
378
379 /*
380  * Creates three pipes (one for reading, one for writing and one for errors),
381  * forks a child, sets up the pipes so that fd_in is connected to STDIN of
382  * the child and fd_out is connected to STDOUT and fd_err is connected to STDERR
383  * of the child. Then is calls `exec_child'.
384  */
385 static int fork_child (program_list_t *pl, int *fd_in, int *fd_out, int *fd_err) /* {{{ */
386 {
387   int fd_pipe_in[2];
388   int fd_pipe_out[2];
389   int fd_pipe_err[2];
390   char errbuf[1024];
391   int status;
392   int pid;
393
394   if (pl->pid != 0)
395     return (-1);
396
397   status = pipe (fd_pipe_in);
398   if (status != 0)
399   {
400     ERROR ("exec plugin: pipe failed: %s",
401         sstrerror (errno, errbuf, sizeof (errbuf)));
402     return (-1);
403   }
404
405   status = pipe (fd_pipe_out);
406   if (status != 0)
407   {
408     ERROR ("exec plugin: pipe failed: %s",
409         sstrerror (errno, errbuf, sizeof (errbuf)));
410     return (-1);
411   }
412
413   status = pipe (fd_pipe_err);
414   if (status != 0)
415   {
416     ERROR ("exec plugin: pipe failed: %s",
417         sstrerror (errno, errbuf, sizeof (errbuf)));
418     return (-1);
419   }
420
421   pid = fork ();
422   if (pid < 0)
423   {
424     ERROR ("exec plugin: fork failed: %s",
425         sstrerror (errno, errbuf, sizeof (errbuf)));
426     return (-1);
427   }
428   else if (pid == 0)
429   {
430     int fd_num;
431     int fd;
432
433     /* Close all file descriptors but the pipe end we need. */
434     fd_num = getdtablesize ();
435     for (fd = 0; fd < fd_num; fd++)
436     {
437       if ((fd == fd_pipe_in[0])
438           || (fd == fd_pipe_out[1])
439           || (fd == fd_pipe_err[1]))
440         continue;
441       close (fd);
442     }
443
444     /* Connect the `in' pipe to STDIN */
445     if (fd_pipe_in[0] != STDIN_FILENO)
446     {
447       dup2 (fd_pipe_in[0], STDIN_FILENO);
448       close (fd_pipe_in[0]);
449     }
450
451     /* Now connect the `out' pipe to STDOUT */
452     if (fd_pipe_out[1] != STDOUT_FILENO)
453     {
454       dup2 (fd_pipe_out[1], STDOUT_FILENO);
455       close (fd_pipe_out[1]);
456     }
457
458     /* Now connect the `out' pipe to STDOUT */
459     if (fd_pipe_err[1] != STDERR_FILENO)
460     {
461       dup2 (fd_pipe_err[1], STDERR_FILENO);
462       close (fd_pipe_err[1]);
463     }
464
465     exec_child (pl);
466     /* does not return */
467   }
468
469   close (fd_pipe_in[0]);
470   close (fd_pipe_out[1]);
471   close (fd_pipe_err[1]);
472
473   if (fd_in != NULL)
474     *fd_in = fd_pipe_in[1];
475   else
476     close (fd_pipe_in[1]);
477
478   if (fd_out != NULL)
479     *fd_out = fd_pipe_out[0];
480   else
481     close (fd_pipe_out[0]);
482
483   if (fd_err != NULL)
484     *fd_err = fd_pipe_err[0];
485   else
486     close (fd_pipe_err[0]);
487
488   return (pid);
489 } /* int fork_child }}} */
490
491 static int parse_line (char *buffer) /* {{{ */
492 {
493   if (strncasecmp ("PUTVAL", buffer, strlen ("PUTVAL")) == 0)
494     return (handle_putval (stdout, buffer));
495   else if (strncasecmp ("PUTNOTIF", buffer, strlen ("PUTNOTIF")) == 0)
496     return (handle_putnotif (stdout, buffer));
497   else
498   {
499     /* For backwards compatibility */
500     char tmp[1220];
501     /* Let's annoy the user a bit.. */
502     INFO ("exec plugin: Prepending `PUTVAL' to this line: %s", buffer);
503     ssnprintf (tmp, sizeof (tmp), "PUTVAL %s", buffer);
504     return (handle_putval (stdout, tmp));
505   }
506 } /* int parse_line }}} */
507
508 static void *exec_read_one (void *arg) /* {{{ */
509 {
510   program_list_t *pl = (program_list_t *) arg;
511   int fd, fd_err, highest_fd;
512   fd_set fdset, copy;
513   int status;
514   char buffer[1200];  /* if not completely read */
515   char buffer_err[1024];
516   char *pbuffer = buffer;
517   char *pbuffer_err = buffer_err;
518
519   status = fork_child (pl, NULL, &fd, &fd_err);
520   if (status < 0)
521     pthread_exit ((void *) 1);
522   pl->pid = status;
523
524   assert (pl->pid != 0);
525
526   FD_ZERO( &fdset );
527   FD_SET(fd, &fdset);
528   FD_SET(fd_err, &fdset);
529
530   /* Determine the highest file descriptor */
531   highest_fd = (fd > fd_err) ? fd : fd_err;
532
533   /* We use a copy of fdset, as select modifies it */
534   copy = fdset;
535
536   while (select(highest_fd + 1, &copy, NULL, NULL, NULL ) > 0)
537   {
538     int len;
539
540     if (FD_ISSET(fd, &copy))
541     {
542       char *pnl;
543
544       len = read(fd, pbuffer, sizeof(buffer) - 1 - (pbuffer - buffer));
545
546       if (len < 0)
547       {
548         if (errno == EAGAIN || errno == EINTR)  continue;
549         break;
550       }
551       else if (len == 0) break;  /* We've reached EOF */
552
553       pbuffer[len] = '\0';
554
555       len += pbuffer - buffer;
556       pbuffer = buffer;
557
558       while ((pnl = strchr(pbuffer, '\n')))
559       {
560         *pnl = '\0';
561         if (*(pnl-1) == '\r' ) *(pnl-1) = '\0';
562
563         parse_line (pbuffer);
564
565         pbuffer = ++pnl;
566       }
567       /* not completely read ? */
568       if (pbuffer - buffer < len)
569       {
570         len -= pbuffer - buffer;
571         memmove(buffer, pbuffer, len);
572         pbuffer = buffer + len;
573       }
574       else
575         pbuffer = buffer;
576     }
577     else if (FD_ISSET(fd_err, &copy))
578     {
579       char *pnl;
580
581       len = read(fd_err, pbuffer_err, sizeof(buffer_err) - 1 - (pbuffer_err - buffer_err));
582
583       if (len < 0)
584       {
585         if (errno == EAGAIN || errno == EINTR)  continue;
586         break;
587       }
588       else if (len == 0)
589       {
590         /* We've reached EOF */
591         NOTICE ("exec plugin: Program `%s' has closed STDERR.",
592             pl->exec);
593         close (fd_err);
594         FD_CLR (fd_err, &fdset);
595         highest_fd = fd;
596         fd_err = -1;
597         continue;
598       }
599
600       pbuffer_err[len] = '\0';
601
602       len += pbuffer_err - buffer_err;
603       pbuffer_err = buffer_err;
604
605       while ((pnl = strchr(pbuffer_err, '\n')))
606       {
607         *pnl = '\0';
608         if (*(pnl-1) == '\r' ) *(pnl-1) = '\0';
609
610         ERROR ("exec plugin: exec_read_one: error = %s", pbuffer_err);
611
612         pbuffer_err = ++pnl;
613       }
614       /* not completely read ? */
615       if (pbuffer_err - buffer_err < len)
616       {
617         len -= pbuffer_err - buffer_err;
618         memmove(buffer_err, pbuffer_err, len);
619         pbuffer_err = buffer_err + len;
620       }
621       else
622         pbuffer_err = buffer_err;
623     }
624     /* reset copy */
625     copy = fdset;
626   }
627
628   DEBUG ("exec plugin: exec_read_one: Waiting for `%s' to exit.", pl->exec);
629   if (waitpid (pl->pid, &status, 0) > 0)
630     pl->status = status;
631
632   DEBUG ("exec plugin: Child %i exited with status %i.",
633       (int) pl->pid, pl->status);
634
635   pl->pid = 0;
636
637   pthread_mutex_lock (&pl_lock);
638   pl->flags &= ~PL_RUNNING;
639   pthread_mutex_unlock (&pl_lock);
640
641   close (fd);
642   if (fd_err >= 0)
643     close (fd_err);
644
645   pthread_exit ((void *) 0);
646   return (NULL);
647 } /* void *exec_read_one }}} */
648
649 static void *exec_notification_one (void *arg) /* {{{ */
650 {
651   program_list_t *pl = ((program_list_and_notification_t *) arg)->pl;
652   notification_t *n = &((program_list_and_notification_t *) arg)->n;
653   notification_meta_t *meta;
654   int fd;
655   FILE *fh;
656   int pid;
657   int status;
658   const char *severity;
659
660   pid = fork_child (pl, &fd, NULL, NULL);
661   if (pid < 0) {
662     sfree (arg);
663     pthread_exit ((void *) 1);
664   }
665
666   fh = fdopen (fd, "w");
667   if (fh == NULL)
668   {
669     char errbuf[1024];
670     ERROR ("exec plugin: fdopen (%i) failed: %s", fd,
671         sstrerror (errno, errbuf, sizeof (errbuf)));
672     kill (pl->pid, SIGTERM);
673     pl->pid = 0;
674     close (fd);
675     sfree (arg);
676     pthread_exit ((void *) 1);
677   }
678
679   severity = "FAILURE";
680   if (n->severity == NOTIF_WARNING)
681     severity = "WARNING";
682   else if (n->severity == NOTIF_OKAY)
683     severity = "OKAY";
684
685   fprintf (fh,
686       "Severity: %s\n"
687       "Time: %u\n",
688       severity, (unsigned int) n->time);
689
690   /* Print the optional fields */
691   if (strlen (n->host) > 0)
692     fprintf (fh, "Host: %s\n", n->host);
693   if (strlen (n->plugin) > 0)
694     fprintf (fh, "Plugin: %s\n", n->plugin);
695   if (strlen (n->plugin_instance) > 0)
696     fprintf (fh, "PluginInstance: %s\n", n->plugin_instance);
697   if (strlen (n->type) > 0)
698     fprintf (fh, "Type: %s\n", n->type);
699   if (strlen (n->type_instance) > 0)
700     fprintf (fh, "TypeInstance: %s\n", n->type_instance);
701
702   for (meta = n->meta; meta != NULL; meta = meta->next)
703   {
704     if (meta->type == NM_TYPE_STRING)
705       fprintf (fh, "%s: %s\n", meta->name, meta->nm_value.nm_string);
706     else if (meta->type == NM_TYPE_SIGNED_INT)
707       fprintf (fh, "%s: %"PRIi64"\n", meta->name, meta->nm_value.nm_signed_int);
708     else if (meta->type == NM_TYPE_UNSIGNED_INT)
709       fprintf (fh, "%s: %"PRIu64"\n", meta->name, meta->nm_value.nm_unsigned_int);
710     else if (meta->type == NM_TYPE_DOUBLE)
711       fprintf (fh, "%s: %e\n", meta->name, meta->nm_value.nm_double);
712     else if (meta->type == NM_TYPE_BOOLEAN)
713       fprintf (fh, "%s: %s\n", meta->name,
714           meta->nm_value.nm_boolean ? "true" : "false");
715   }
716
717   fprintf (fh, "\n%s\n", n->message);
718
719   fflush (fh);
720   fclose (fh);
721
722   waitpid (pid, &status, 0);
723
724   DEBUG ("exec plugin: Child %i exited with status %i.",
725       pid, status);
726
727   plugin_notification_meta_free (n->meta);
728   n->meta = NULL;
729   sfree (arg);
730   pthread_exit ((void *) 0);
731   return (NULL);
732 } /* void *exec_notification_one }}} */
733
734 static int exec_init (void) /* {{{ */
735 {
736   struct sigaction sa;
737
738   memset (&sa, '\0', sizeof (sa));
739   sa.sa_handler = sigchld_handler;
740   sigaction (SIGCHLD, &sa, NULL);
741
742   return (0);
743 } /* int exec_init }}} */
744
745 static int exec_read (void) /* {{{ */
746 {
747   program_list_t *pl;
748
749   for (pl = pl_head; pl != NULL; pl = pl->next)
750   {
751     pthread_t t;
752     pthread_attr_t attr;
753
754     /* Only execute `normal' style executables here. */
755     if ((pl->flags & PL_NORMAL) == 0)
756       continue;
757
758     pthread_mutex_lock (&pl_lock);
759     /* Skip if a child is already running. */
760     if ((pl->flags & PL_RUNNING) != 0)
761     {
762       pthread_mutex_unlock (&pl_lock);
763       continue;
764     }
765     pl->flags |= PL_RUNNING;
766     pthread_mutex_unlock (&pl_lock);
767
768     pthread_attr_init (&attr);
769     pthread_attr_setdetachstate (&attr, PTHREAD_CREATE_DETACHED);
770     pthread_create (&t, &attr, exec_read_one, (void *) pl);
771   } /* for (pl) */
772
773   return (0);
774 } /* int exec_read }}} */
775
776 static int exec_notification (const notification_t *n,
777     user_data_t __attribute__((unused)) *user_data)
778 {
779   program_list_t *pl;
780   program_list_and_notification_t *pln;
781
782   for (pl = pl_head; pl != NULL; pl = pl->next)
783   {
784     pthread_t t;
785     pthread_attr_t attr;
786
787     /* Only execute `notification' style executables here. */
788     if ((pl->flags & PL_NOTIF_ACTION) == 0)
789       continue;
790
791     /* Skip if a child is already running. */
792     if (pl->pid != 0)
793       continue;
794
795     pln = (program_list_and_notification_t *) malloc (sizeof
796         (program_list_and_notification_t));
797     if (pln == NULL)
798     {
799       ERROR ("exec plugin: malloc failed.");
800       continue;
801     }
802
803     pln->pl = pl;
804     memcpy (&pln->n, n, sizeof (notification_t));
805
806     /* Set the `meta' member to NULL, otherwise `plugin_notification_meta_copy'
807      * will run into an endless loop. */
808     pln->n.meta = NULL;
809     plugin_notification_meta_copy (&pln->n, n);
810
811     pthread_attr_init (&attr);
812     pthread_attr_setdetachstate (&attr, PTHREAD_CREATE_DETACHED);
813     pthread_create (&t, &attr, exec_notification_one, (void *) pln);
814   } /* for (pl) */
815
816   return (0);
817 } /* }}} int exec_notification */
818
819 static int exec_shutdown (void) /* {{{ */
820 {
821   program_list_t *pl;
822   program_list_t *next;
823
824   pl = pl_head;
825   while (pl != NULL)
826   {
827     next = pl->next;
828
829     if (pl->pid > 0)
830     {
831       kill (pl->pid, SIGTERM);
832       INFO ("exec plugin: Sent SIGTERM to %hu", (unsigned short int) pl->pid);
833     }
834
835     sfree (pl->user);
836     sfree (pl);
837
838     pl = next;
839   } /* while (pl) */
840   pl_head = NULL;
841
842   return (0);
843 } /* int exec_shutdown }}} */
844
845 void module_register (void)
846 {
847   plugin_register_complex_config ("exec", exec_config);
848   plugin_register_init ("exec", exec_init);
849   plugin_register_read ("exec", exec_read);
850   plugin_register_notification ("exec", exec_notification,
851       /* user_data = */ NULL);
852   plugin_register_shutdown ("exec", exec_shutdown);
853 } /* void module_register */
854
855 /*
856  * vim:shiftwidth=2:softtabstop=2:tabstop=8:fdm=marker
857  */