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