src/utils_cmd_putval.[ch]: Allow identifiers to include spaces.
[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 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 #if !COLLECT_DEBUG
496 #error "TODO: PUTNOTIF"
497   else if (strcasecmp (fields[1], "putnotif") == 0)
498     return (handle_putnotif (stdout, fields + 1, fields_num));
499 #endif
500   else
501   {
502     /* For backwards compatibility */
503     char tmp[1220];
504     /* Let's annoy the user a bit.. */
505     INFO ("exec plugin: Prepending `PUTVAL' to this line: %s", buffer);
506     ssnprintf (tmp, sizeof (tmp), "PUTVAL %s", buffer);
507     return (handle_putval (stdout, tmp));
508   }
509 } /* int parse_line }}} */
510
511 static void *exec_read_one (void *arg) /* {{{ */
512 {
513   program_list_t *pl = (program_list_t *) arg;
514   int fd, fd_err, highest_fd;
515   fd_set fdset, copy;
516   int status;
517   char buffer[1200];  /* if not completely read */
518   char buffer_err[1024];
519   char *pbuffer = buffer;
520   char *pbuffer_err = buffer_err;
521
522   status = fork_child (pl, NULL, &fd, &fd_err);
523   if (status < 0)
524     pthread_exit ((void *) 1);
525   pl->pid = status;
526
527   assert (pl->pid != 0);
528
529   FD_ZERO( &fdset );
530   FD_SET(fd, &fdset);
531   FD_SET(fd_err, &fdset);
532
533   /* Determine the highest file descriptor */
534   highest_fd = (fd > fd_err) ? fd : fd_err;
535
536   /* We use a copy of fdset, as select modifies it */
537   copy = fdset;
538
539   while (select(highest_fd + 1, &copy, NULL, NULL, NULL ) > 0)
540   {
541     int len;
542
543     if (FD_ISSET(fd, &copy))
544     {
545       char *pnl;
546
547       len = read(fd, pbuffer, sizeof(buffer) - 1 - (pbuffer - buffer));
548
549       if (len < 0)
550       {
551         if (errno == EAGAIN || errno == EINTR)  continue;
552         break;
553       }
554       else if (len == 0) break;  /* We've reached EOF */
555
556       pbuffer[len] = '\0';
557
558       len += pbuffer - buffer;
559       pbuffer = buffer;
560
561       while ((pnl = strchr(pbuffer, '\n')))
562       {
563         *pnl = '\0';
564         if (*(pnl-1) == '\r' ) *(pnl-1) = '\0';
565
566         parse_line (pbuffer);
567
568         pbuffer = ++pnl;
569       }
570       /* not completely read ? */
571       if (pbuffer - buffer < len)
572       {
573         len -= pbuffer - buffer;
574         memmove(buffer, pbuffer, len);
575         pbuffer = buffer + len;
576       }
577       else
578         pbuffer = buffer;
579     }
580     else if (FD_ISSET(fd_err, &copy))
581     {
582       char *pnl;
583
584       len = read(fd_err, pbuffer_err, sizeof(buffer_err) - 1 - (pbuffer_err - buffer_err));
585
586       if (len < 0)
587       {
588         if (errno == EAGAIN || errno == EINTR)  continue;
589         break;
590       }
591       else if (len == 0) break;  /* We've reached EOF */
592
593       pbuffer_err[len] = '\0';
594
595       len += pbuffer_err - buffer_err;
596       pbuffer_err = buffer_err;
597
598       while ((pnl = strchr(pbuffer_err, '\n')))
599       {
600         *pnl = '\0';
601         if (*(pnl-1) == '\r' ) *(pnl-1) = '\0';
602
603         ERROR ("exec plugin: exec_read_one: error = %s", pbuffer_err);
604
605         pbuffer_err = ++pnl;
606       }
607       /* not completely read ? */
608       if (pbuffer_err - buffer_err < len)
609       {
610         len -= pbuffer_err - buffer_err;
611         memmove(buffer_err, pbuffer_err, len);
612         pbuffer_err = buffer_err + len;
613       }
614       else
615         pbuffer_err = buffer_err;
616     }
617     /* reset copy */
618     copy = fdset;
619   }
620
621   if (waitpid (pl->pid, &status, 0) > 0)
622     pl->status = status;
623
624   DEBUG ("exec plugin: Child %i exited with status %i.",
625       (int) pl->pid, pl->status);
626
627   pl->pid = 0;
628
629   pthread_mutex_lock (&pl_lock);
630   pl->flags &= ~PL_RUNNING;
631   pthread_mutex_unlock (&pl_lock);
632
633   close (fd);
634   close (fd_err);
635
636   pthread_exit ((void *) 0);
637   return (NULL);
638 } /* void *exec_read_one }}} */
639
640 static void *exec_notification_one (void *arg) /* {{{ */
641 {
642   program_list_t *pl = ((program_list_and_notification_t *) arg)->pl;
643   notification_t *n = &((program_list_and_notification_t *) arg)->n;
644   notification_meta_t *meta;
645   int fd;
646   FILE *fh;
647   int pid;
648   int status;
649   const char *severity;
650
651   pid = fork_child (pl, &fd, NULL, NULL);
652   if (pid < 0) {
653     sfree (arg);
654     pthread_exit ((void *) 1);
655   }
656
657   fh = fdopen (fd, "w");
658   if (fh == NULL)
659   {
660     char errbuf[1024];
661     ERROR ("exec plugin: fdopen (%i) failed: %s", fd,
662         sstrerror (errno, errbuf, sizeof (errbuf)));
663     kill (pl->pid, SIGTERM);
664     pl->pid = 0;
665     close (fd);
666     sfree (arg);
667     pthread_exit ((void *) 1);
668   }
669
670   severity = "FAILURE";
671   if (n->severity == NOTIF_WARNING)
672     severity = "WARNING";
673   else if (n->severity == NOTIF_OKAY)
674     severity = "OKAY";
675
676   fprintf (fh,
677       "Severity: %s\n"
678       "Time: %u\n",
679       severity, (unsigned int) n->time);
680
681   /* Print the optional fields */
682   if (strlen (n->host) > 0)
683     fprintf (fh, "Host: %s\n", n->host);
684   if (strlen (n->plugin) > 0)
685     fprintf (fh, "Plugin: %s\n", n->plugin);
686   if (strlen (n->plugin_instance) > 0)
687     fprintf (fh, "PluginInstance: %s\n", n->plugin_instance);
688   if (strlen (n->type) > 0)
689     fprintf (fh, "Type: %s\n", n->type);
690   if (strlen (n->type_instance) > 0)
691     fprintf (fh, "TypeInstance: %s\n", n->type_instance);
692
693   for (meta = n->meta; meta != NULL; meta = meta->next)
694   {
695     if (meta->type == NM_TYPE_STRING)
696       fprintf (fh, "%s: %s\n", meta->name, meta->value_string);
697     else if (meta->type == NM_TYPE_SIGNED_INT)
698       fprintf (fh, "%s: %"PRIi64"\n", meta->name, meta->value_signed_int);
699     else if (meta->type == NM_TYPE_UNSIGNED_INT)
700       fprintf (fh, "%s: %"PRIu64"\n", meta->name, meta->value_unsigned_int);
701     else if (meta->type == NM_TYPE_DOUBLE)
702       fprintf (fh, "%s: %e\n", meta->name, meta->value_double);
703     else if (meta->type == NM_TYPE_BOOLEAN)
704       fprintf (fh, "%s: %s\n", meta->name,
705           meta->value_boolean ? "true" : "false");
706   }
707
708   fprintf (fh, "\n%s\n", n->message);
709
710   fflush (fh);
711   fclose (fh);
712
713   waitpid (pid, &status, 0);
714
715   DEBUG ("exec plugin: Child %i exited with status %i.",
716       pid, status);
717
718   plugin_notification_meta_free (n);
719   sfree (arg);
720   pthread_exit ((void *) 0);
721   return (NULL);
722 } /* void *exec_notification_one }}} */
723
724 static int exec_init (void) /* {{{ */
725 {
726   struct sigaction sa;
727
728   memset (&sa, '\0', sizeof (sa));
729   sa.sa_handler = sigchld_handler;
730   sigaction (SIGCHLD, &sa, NULL);
731
732   return (0);
733 } /* int exec_init }}} */
734
735 static int exec_read (void) /* {{{ */
736 {
737   program_list_t *pl;
738
739   for (pl = pl_head; pl != NULL; pl = pl->next)
740   {
741     pthread_t t;
742     pthread_attr_t attr;
743
744     /* Only execute `normal' style executables here. */
745     if ((pl->flags & PL_NORMAL) == 0)
746       continue;
747
748     pthread_mutex_lock (&pl_lock);
749     /* Skip if a child is already running. */
750     if ((pl->flags & PL_RUNNING) != 0)
751     {
752       pthread_mutex_unlock (&pl_lock);
753       continue;
754     }
755     pl->flags |= PL_RUNNING;
756     pthread_mutex_unlock (&pl_lock);
757
758     pthread_attr_init (&attr);
759     pthread_attr_setdetachstate (&attr, PTHREAD_CREATE_DETACHED);
760     pthread_create (&t, &attr, exec_read_one, (void *) pl);
761   } /* for (pl) */
762
763   return (0);
764 } /* int exec_read }}} */
765
766 static int exec_notification (const notification_t *n)
767 {
768   program_list_t *pl;
769   program_list_and_notification_t *pln;
770
771   for (pl = pl_head; pl != NULL; pl = pl->next)
772   {
773     pthread_t t;
774     pthread_attr_t attr;
775
776     /* Only execute `notification' style executables here. */
777     if ((pl->flags & PL_NOTIF_ACTION) == 0)
778       continue;
779
780     /* Skip if a child is already running. */
781     if (pl->pid != 0)
782       continue;
783
784     pln = (program_list_and_notification_t *) malloc (sizeof
785         (program_list_and_notification_t));
786     if (pln == NULL)
787     {
788       ERROR ("exec plugin: malloc failed.");
789       continue;
790     }
791
792     pln->pl = pl;
793     memcpy (&pln->n, n, sizeof (notification_t));
794
795     /* Set the `meta' member to NULL, otherwise `plugin_notification_meta_copy'
796      * will run into an endless loop. */
797     pln->n.meta = NULL;
798     plugin_notification_meta_copy (&pln->n, n);
799
800     pthread_attr_init (&attr);
801     pthread_attr_setdetachstate (&attr, PTHREAD_CREATE_DETACHED);
802     pthread_create (&t, &attr, exec_notification_one, (void *) pln);
803   } /* for (pl) */
804
805   return (0);
806 } /* int exec_notification */
807
808 static int exec_shutdown (void) /* {{{ */
809 {
810   program_list_t *pl;
811   program_list_t *next;
812
813   pl = pl_head;
814   while (pl != NULL)
815   {
816     next = pl->next;
817
818     if (pl->pid > 0)
819     {
820       kill (pl->pid, SIGTERM);
821       INFO ("exec plugin: Sent SIGTERM to %hu", (unsigned short int) pl->pid);
822     }
823
824     sfree (pl->user);
825     sfree (pl);
826
827     pl = next;
828   } /* while (pl) */
829   pl_head = NULL;
830
831   return (0);
832 } /* int exec_shutdown }}} */
833
834 void module_register (void)
835 {
836   plugin_register_complex_config ("exec", exec_config);
837   plugin_register_init ("exec", exec_init);
838   plugin_register_read ("exec", exec_read);
839   plugin_register_notification ("exec", exec_notification);
840   plugin_register_shutdown ("exec", exec_shutdown);
841 } /* void module_register */
842
843 /*
844  * vim:shiftwidth=2:softtabstop=2:tabstop=8:fdm=marker
845  */