Fix a NULL pointer dereference during shutdown
[collectd.git] / src / plugin.c
1 /**
2  * collectd - src/plugin.c
3  * Copyright (C) 2005-2013  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 collectd.org>
20  *   Sebastian Harl <sh at tokkee.org>
21  **/
22
23 #include "collectd.h"
24 #include "utils_complain.h"
25
26 #include <ltdl.h>
27
28 #if HAVE_PTHREAD_H
29 # include <pthread.h>
30 #endif
31
32 #include "common.h"
33 #include "plugin.h"
34 #include "configfile.h"
35 #include "utils_avltree.h"
36 #include "utils_llist.h"
37 #include "utils_heap.h"
38 #include "utils_cache.h"
39 #include "filter_chain.h"
40
41 /*
42  * Private structures
43  */
44 struct callback_func_s
45 {
46         void *cf_callback;
47         user_data_t cf_udata;
48         plugin_ctx_t cf_ctx;
49 };
50 typedef struct callback_func_s callback_func_t;
51
52 #define RF_SIMPLE  0
53 #define RF_COMPLEX 1
54 #define RF_REMOVE  65535
55 struct read_func_s
56 {
57         /* `read_func_t' "inherits" from `callback_func_t'.
58          * The `rf_super' member MUST be the first one in this structure! */
59 #define rf_callback rf_super.cf_callback
60 #define rf_udata rf_super.cf_udata
61 #define rf_ctx rf_super.cf_ctx
62         callback_func_t rf_super;
63         char rf_group[DATA_MAX_NAME_LEN];
64         char rf_name[DATA_MAX_NAME_LEN];
65         int rf_type;
66         struct timespec rf_interval;
67         struct timespec rf_effective_interval;
68         struct timespec rf_next_read;
69 };
70 typedef struct read_func_s read_func_t;
71
72 struct write_queue_s;
73 typedef struct write_queue_s write_queue_t;
74 struct write_queue_s
75 {
76         value_list_t *vl;
77         plugin_ctx_t ctx;
78         write_queue_t *next;
79 };
80
81 /*
82  * Private variables
83  */
84 static llist_t *list_init;
85 static llist_t *list_write;
86 static llist_t *list_flush;
87 static llist_t *list_missing;
88 static llist_t *list_shutdown;
89 static llist_t *list_log;
90 static llist_t *list_notification;
91
92 static fc_chain_t *pre_cache_chain = NULL;
93 static fc_chain_t *post_cache_chain = NULL;
94
95 static c_avl_tree_t *data_sets;
96
97 static char *plugindir = NULL;
98
99 static c_heap_t       *read_heap = NULL;
100 static llist_t        *read_list;
101 static int             read_loop = 1;
102 static pthread_mutex_t read_lock = PTHREAD_MUTEX_INITIALIZER;
103 static pthread_cond_t  read_cond = PTHREAD_COND_INITIALIZER;
104 static pthread_t      *read_threads = NULL;
105 static int             read_threads_num = 0;
106
107 static write_queue_t  *write_queue_head;
108 static write_queue_t  *write_queue_tail;
109 static _Bool           write_loop = 1;
110 static pthread_mutex_t write_lock = PTHREAD_MUTEX_INITIALIZER;
111 static pthread_cond_t  write_cond = PTHREAD_COND_INITIALIZER;
112 static pthread_t      *write_threads = NULL;
113 static size_t          write_threads_num = 0;
114
115 static pthread_key_t   plugin_ctx_key;
116 static _Bool           plugin_ctx_key_initialized = 0;
117
118 /*
119  * Static functions
120  */
121 static int plugin_dispatch_values_internal (value_list_t *vl);
122
123 static const char *plugin_get_dir (void)
124 {
125         if (plugindir == NULL)
126                 return (PLUGINDIR);
127         else
128                 return (plugindir);
129 }
130
131 static void destroy_callback (callback_func_t *cf) /* {{{ */
132 {
133         if (cf == NULL)
134                 return;
135
136         if ((cf->cf_udata.data != NULL) && (cf->cf_udata.free_func != NULL))
137         {
138                 cf->cf_udata.free_func (cf->cf_udata.data);
139                 cf->cf_udata.data = NULL;
140                 cf->cf_udata.free_func = NULL;
141         }
142         sfree (cf);
143 } /* }}} void destroy_callback */
144
145 static void destroy_all_callbacks (llist_t **list) /* {{{ */
146 {
147         llentry_t *le;
148
149         if (*list == NULL)
150                 return;
151
152         le = llist_head (*list);
153         while (le != NULL)
154         {
155                 llentry_t *le_next;
156
157                 le_next = le->next;
158
159                 sfree (le->key);
160                 destroy_callback (le->value);
161                 le->value = NULL;
162
163                 le = le_next;
164         }
165
166         llist_destroy (*list);
167         *list = NULL;
168 } /* }}} void destroy_all_callbacks */
169
170 static void destroy_read_heap (void) /* {{{ */
171 {
172         if (read_heap == NULL)
173                 return;
174
175         while (42)
176         {
177                 callback_func_t *cf;
178
179                 cf = c_heap_get_root (read_heap);
180                 if (cf == NULL)
181                         break;
182
183                 destroy_callback (cf);
184         }
185
186         c_heap_destroy (read_heap);
187         read_heap = NULL;
188 } /* }}} void destroy_read_heap */
189
190 static int register_callback (llist_t **list, /* {{{ */
191                 const char *name, callback_func_t *cf)
192 {
193         llentry_t *le;
194         char *key;
195
196         if (*list == NULL)
197         {
198                 *list = llist_create ();
199                 if (*list == NULL)
200                 {
201                         ERROR ("plugin: register_callback: "
202                                         "llist_create failed.");
203                         destroy_callback (cf);
204                         return (-1);
205                 }
206         }
207
208         key = strdup (name);
209         if (key == NULL)
210         {
211                 ERROR ("plugin: register_callback: strdup failed.");
212                 destroy_callback (cf);
213                 return (-1);
214         }
215
216         le = llist_search (*list, name);
217         if (le == NULL)
218         {
219                 le = llentry_create (key, cf);
220                 if (le == NULL)
221                 {
222                         ERROR ("plugin: register_callback: "
223                                         "llentry_create failed.");
224                         free (key);
225                         destroy_callback (cf);
226                         return (-1);
227                 }
228
229                 llist_append (*list, le);
230         }
231         else
232         {
233                 callback_func_t *old_cf;
234
235                 old_cf = le->value;
236                 le->value = cf;
237
238                 WARNING ("plugin: register_callback: "
239                                 "a callback named `%s' already exists - "
240                                 "overwriting the old entry!", name);
241
242                 destroy_callback (old_cf);
243                 sfree (key);
244         }
245
246         return (0);
247 } /* }}} int register_callback */
248
249 static int create_register_callback (llist_t **list, /* {{{ */
250                 const char *name, void *callback, user_data_t *ud)
251 {
252         callback_func_t *cf;
253
254         cf = (callback_func_t *) malloc (sizeof (*cf));
255         if (cf == NULL)
256         {
257                 ERROR ("plugin: create_register_callback: malloc failed.");
258                 return (-1);
259         }
260         memset (cf, 0, sizeof (*cf));
261
262         cf->cf_callback = callback;
263         if (ud == NULL)
264         {
265                 cf->cf_udata.data = NULL;
266                 cf->cf_udata.free_func = NULL;
267         }
268         else
269         {
270                 cf->cf_udata = *ud;
271         }
272
273         cf->cf_ctx = plugin_get_ctx ();
274
275         return (register_callback (list, name, cf));
276 } /* }}} int create_register_callback */
277
278 static int plugin_unregister (llist_t *list, const char *name) /* {{{ */
279 {
280         llentry_t *e;
281
282         if (list == NULL)
283                 return (-1);
284
285         e = llist_search (list, name);
286         if (e == NULL)
287                 return (-1);
288
289         llist_remove (list, e);
290
291         sfree (e->key);
292         destroy_callback (e->value);
293
294         llentry_destroy (e);
295
296         return (0);
297 } /* }}} int plugin_unregister */
298
299 /*
300  * (Try to) load the shared object `file'. Won't complain if it isn't a shared
301  * object, but it will bitch about a shared object not having a
302  * ``module_register'' symbol..
303  */
304 static int plugin_load_file (char *file, uint32_t flags)
305 {
306         lt_dlhandle dlh;
307         void (*reg_handle) (void);
308
309         lt_dlinit ();
310         lt_dlerror (); /* clear errors */
311
312 #if LIBTOOL_VERSION == 2
313         if (flags & PLUGIN_FLAGS_GLOBAL) {
314                 lt_dladvise advise;
315                 lt_dladvise_init(&advise);
316                 lt_dladvise_global(&advise);
317                 dlh = lt_dlopenadvise(file, advise);
318                 lt_dladvise_destroy(&advise);
319         } else {
320                 dlh = lt_dlopen (file);
321         }
322 #else /* if LIBTOOL_VERSION == 1 */
323         if (flags & PLUGIN_FLAGS_GLOBAL)
324                 WARNING ("plugin_load_file: The global flag is not supported, "
325                                 "libtool 2 is required for this.");
326         dlh = lt_dlopen (file);
327 #endif
328
329         if (dlh == NULL)
330         {
331                 char errbuf[1024] = "";
332
333                 ssnprintf (errbuf, sizeof (errbuf),
334                                 "lt_dlopen (\"%s\") failed: %s. "
335                                 "The most common cause for this problem are "
336                                 "missing dependencies. Use ldd(1) to check "
337                                 "the dependencies of the plugin "
338                                 "/ shared object.",
339                                 file, lt_dlerror ());
340
341                 ERROR ("%s", errbuf);
342                 /* Make sure this is printed to STDERR in any case, but also
343                  * make sure it's printed only once. */
344                 if (list_log != NULL)
345                         fprintf (stderr, "ERROR: %s\n", errbuf);
346
347                 return (1);
348         }
349
350         if ((reg_handle = (void (*) (void)) lt_dlsym (dlh, "module_register")) == NULL)
351         {
352                 WARNING ("Couldn't find symbol \"module_register\" in \"%s\": %s\n",
353                                 file, lt_dlerror ());
354                 lt_dlclose (dlh);
355                 return (-1);
356         }
357
358         (*reg_handle) ();
359
360         return (0);
361 }
362
363 static _Bool timeout_reached(struct timespec timeout)
364 {
365         struct timeval now;
366         gettimeofday(&now, NULL);
367         return (now.tv_sec >= timeout.tv_sec && now.tv_usec >= (timeout.tv_nsec / 1000));
368 }
369
370 static void *plugin_read_thread (void __attribute__((unused)) *args)
371 {
372         while (read_loop != 0)
373         {
374                 read_func_t *rf;
375                 plugin_ctx_t old_ctx;
376                 cdtime_t now;
377                 int status;
378                 int rf_type;
379                 int rc;
380
381                 /* Get the read function that needs to be read next.
382                  * We don't need to hold "read_lock" for the heap, but we need
383                  * to call c_heap_get_root() and pthread_cond_wait() in the
384                  * same protected block. */
385                 pthread_mutex_lock (&read_lock);
386                 rf = c_heap_get_root (read_heap);
387                 if (rf == NULL)
388                 {
389                         pthread_cond_wait (&read_cond, &read_lock);
390                         pthread_mutex_unlock (&read_lock);
391                         continue;
392                 }
393                 pthread_mutex_unlock (&read_lock);
394
395                 if ((rf->rf_interval.tv_sec == 0) && (rf->rf_interval.tv_nsec == 0))
396                 {
397                         /* this should not happen, because the interval is set
398                          * for each plugin when loading it
399                          * XXX: issue a warning? */
400                         now = cdtime ();
401
402                         CDTIME_T_TO_TIMESPEC (plugin_get_interval (), &rf->rf_interval);
403
404                         rf->rf_effective_interval = rf->rf_interval;
405
406                         CDTIME_T_TO_TIMESPEC (now, &rf->rf_next_read);
407                 }
408
409                 /* sleep until this entry is due,
410                  * using pthread_cond_timedwait */
411                 pthread_mutex_lock (&read_lock);
412                 /* In pthread_cond_timedwait, spurious wakeups are possible
413                  * (and really happen, at least on NetBSD with > 1 CPU), thus
414                  * we need to re-evaluate the condition every time
415                  * pthread_cond_timedwait returns. */
416                 rc = 0;
417                 while ((read_loop != 0)
418                                 && !timeout_reached(rf->rf_next_read)
419                                 && rc == 0)
420                 {
421                         rc = pthread_cond_timedwait (&read_cond, &read_lock,
422                                 &rf->rf_next_read);
423                 }
424
425                 /* Must hold `read_lock' when accessing `rf->rf_type'. */
426                 rf_type = rf->rf_type;
427                 pthread_mutex_unlock (&read_lock);
428
429                 /* Check if we're supposed to stop.. This may have interrupted
430                  * the sleep, too. */
431                 if (read_loop == 0)
432                 {
433                         /* Insert `rf' again, so it can be free'd correctly */
434                         c_heap_insert (read_heap, rf);
435                         break;
436                 }
437
438                 /* The entry has been marked for deletion. The linked list
439                  * entry has already been removed by `plugin_unregister_read'.
440                  * All we have to do here is free the `read_func_t' and
441                  * continue. */
442                 if (rf_type == RF_REMOVE)
443                 {
444                         DEBUG ("plugin_read_thread: Destroying the `%s' "
445                                         "callback.", rf->rf_name);
446                         destroy_callback ((callback_func_t *) rf);
447                         rf = NULL;
448                         continue;
449                 }
450
451                 DEBUG ("plugin_read_thread: Handling `%s'.", rf->rf_name);
452
453                 old_ctx = plugin_set_ctx (rf->rf_ctx);
454
455                 if (rf_type == RF_SIMPLE)
456                 {
457                         int (*callback) (void);
458
459                         callback = rf->rf_callback;
460                         status = (*callback) ();
461                 }
462                 else
463                 {
464                         plugin_read_cb callback;
465
466                         assert (rf_type == RF_COMPLEX);
467
468                         callback = rf->rf_callback;
469                         status = (*callback) (&rf->rf_udata);
470                 }
471
472                 plugin_set_ctx (old_ctx);
473
474                 /* If the function signals failure, we will increase the
475                  * intervals in which it will be called. */
476                 if (status != 0)
477                 {
478                         rf->rf_effective_interval.tv_sec *= 2;
479                         rf->rf_effective_interval.tv_nsec *= 2;
480                         NORMALIZE_TIMESPEC (rf->rf_effective_interval);
481
482                         if (rf->rf_effective_interval.tv_sec >= 86400)
483                         {
484                                 rf->rf_effective_interval.tv_sec = 86400;
485                                 rf->rf_effective_interval.tv_nsec = 0;
486                         }
487
488                         NOTICE ("read-function of plugin `%s' failed. "
489                                         "Will suspend it for %i seconds.",
490                                         rf->rf_name,
491                                         (int) rf->rf_effective_interval.tv_sec);
492                 }
493                 else
494                 {
495                         /* Success: Restore the interval, if it was changed. */
496                         rf->rf_effective_interval = rf->rf_interval;
497                 }
498
499                 /* update the ``next read due'' field */
500                 now = cdtime ();
501
502                 DEBUG ("plugin_read_thread: Effective interval of the "
503                                 "%s plugin is %i.%09i.",
504                                 rf->rf_name,
505                                 (int) rf->rf_effective_interval.tv_sec,
506                                 (int) rf->rf_effective_interval.tv_nsec);
507
508                 /* Calculate the next (absolute) time at which this function
509                  * should be called. */
510                 rf->rf_next_read.tv_sec = rf->rf_next_read.tv_sec
511                         + rf->rf_effective_interval.tv_sec;
512                 rf->rf_next_read.tv_nsec = rf->rf_next_read.tv_nsec
513                         + rf->rf_effective_interval.tv_nsec;
514                 NORMALIZE_TIMESPEC (rf->rf_next_read);
515
516                 /* Check, if `rf_next_read' is in the past. */
517                 if (TIMESPEC_TO_CDTIME_T (&rf->rf_next_read) < now)
518                 {
519                         /* `rf_next_read' is in the past. Insert `now'
520                          * so this value doesn't trail off into the
521                          * past too much. */
522                         CDTIME_T_TO_TIMESPEC (now, &rf->rf_next_read);
523                 }
524
525                 DEBUG ("plugin_read_thread: Next read of the %s plugin at %i.%09i.",
526                                 rf->rf_name,
527                                 (int) rf->rf_next_read.tv_sec,
528                                 (int) rf->rf_next_read.tv_nsec);
529
530                 /* Re-insert this read function into the heap again. */
531                 c_heap_insert (read_heap, rf);
532         } /* while (read_loop) */
533
534         pthread_exit (NULL);
535         return ((void *) 0);
536 } /* void *plugin_read_thread */
537
538 static void start_read_threads (int num)
539 {
540         int i;
541
542         if (read_threads != NULL)
543                 return;
544
545         read_threads = (pthread_t *) calloc (num, sizeof (pthread_t));
546         if (read_threads == NULL)
547         {
548                 ERROR ("plugin: start_read_threads: calloc failed.");
549                 return;
550         }
551
552         read_threads_num = 0;
553         for (i = 0; i < num; i++)
554         {
555                 if (pthread_create (read_threads + read_threads_num, NULL,
556                                         plugin_read_thread, NULL) == 0)
557                 {
558                         read_threads_num++;
559                 }
560                 else
561                 {
562                         ERROR ("plugin: start_read_threads: pthread_create failed.");
563                         return;
564                 }
565         } /* for (i) */
566 } /* void start_read_threads */
567
568 static void stop_read_threads (void)
569 {
570         int i;
571
572         if (read_threads == NULL)
573                 return;
574
575         INFO ("collectd: Stopping %i read threads.", read_threads_num);
576
577         pthread_mutex_lock (&read_lock);
578         read_loop = 0;
579         DEBUG ("plugin: stop_read_threads: Signalling `read_cond'");
580         pthread_cond_broadcast (&read_cond);
581         pthread_mutex_unlock (&read_lock);
582
583         for (i = 0; i < read_threads_num; i++)
584         {
585                 if (pthread_join (read_threads[i], NULL) != 0)
586                 {
587                         ERROR ("plugin: stop_read_threads: pthread_join failed.");
588                 }
589                 read_threads[i] = (pthread_t) 0;
590         }
591         sfree (read_threads);
592         read_threads_num = 0;
593 } /* void stop_read_threads */
594
595 static void plugin_value_list_free (value_list_t *vl) /* {{{ */
596 {
597         if (vl == NULL)
598                 return;
599
600         meta_data_destroy (vl->meta);
601         sfree (vl->values);
602         sfree (vl);
603 } /* }}} void plugin_value_list_free */
604
605 static value_list_t *plugin_value_list_clone (value_list_t const *vl_orig) /* {{{ */
606 {
607         value_list_t *vl;
608
609         if (vl_orig == NULL)
610                 return (NULL);
611
612         vl = malloc (sizeof (*vl));
613         if (vl == NULL)
614                 return (NULL);
615         memcpy (vl, vl_orig, sizeof (*vl));
616
617         vl->values = calloc (vl_orig->values_len, sizeof (*vl->values));
618         if (vl->values == NULL)
619         {
620                 plugin_value_list_free (vl);
621                 return (NULL);
622         }
623         memcpy (vl->values, vl_orig->values,
624                         vl_orig->values_len * sizeof (*vl->values));
625
626         vl->meta = meta_data_clone (vl->meta);
627         if ((vl_orig->meta != NULL) && (vl->meta == NULL))
628         {
629                 plugin_value_list_free (vl);
630                 return (NULL);
631         }
632
633         if (vl->time == 0)
634                 vl->time = cdtime ();
635
636         /* Fill in the interval from the thread context, if it is zero. */
637         if (vl->interval == 0)
638         {
639                 plugin_ctx_t ctx = plugin_get_ctx ();
640
641                 if (ctx.interval != 0)
642                         vl->interval = ctx.interval;
643                 else
644                 {
645                         char name[6 * DATA_MAX_NAME_LEN];
646                         FORMAT_VL (name, sizeof (name), vl);
647                         ERROR ("plugin_value_list_clone: Unable to determine "
648                                         "interval from context for "
649                                         "value list \"%s\". "
650                                         "This indicates a broken plugin. "
651                                         "Please report this problem to the "
652                                         "collectd mailing list or at "
653                                         "<http://collectd.org/bugs/>.", name);
654                         vl->interval = cf_get_default_interval ();
655                 }
656         }
657
658         return (vl);
659 } /* }}} value_list_t *plugin_value_list_clone */
660
661 static int plugin_write_enqueue (value_list_t const *vl) /* {{{ */
662 {
663         write_queue_t *q;
664
665         q = malloc (sizeof (*q));
666         if (q == NULL)
667                 return (ENOMEM);
668         q->next = NULL;
669
670         q->vl = plugin_value_list_clone (vl);
671         if (q->vl == NULL)
672         {
673                 sfree (q);
674                 return (ENOMEM);
675         }
676
677         /* Store context of caller (read plugin); otherwise, it would not be
678          * available to the write plugins when actually dispatching the
679          * value-list later on. */
680         q->ctx = plugin_get_ctx ();
681
682         pthread_mutex_lock (&write_lock);
683
684         if (write_queue_tail == NULL)
685         {
686                 write_queue_head = q;
687                 write_queue_tail = q;
688         }
689         else
690         {
691                 write_queue_tail->next = q;
692                 write_queue_tail = q;
693         }
694
695         pthread_cond_signal (&write_cond);
696         pthread_mutex_unlock (&write_lock);
697
698         return (0);
699 } /* }}} int plugin_write_enqueue */
700
701 static value_list_t *plugin_write_dequeue (void) /* {{{ */
702 {
703         write_queue_t *q;
704         value_list_t *vl;
705
706         pthread_mutex_lock (&write_lock);
707
708         while (write_loop && (write_queue_head == NULL))
709                 pthread_cond_wait (&write_cond, &write_lock);
710
711         if (write_queue_head == NULL)
712         {
713                 pthread_mutex_unlock (&write_lock);
714                 return (NULL);
715         }
716
717         q = write_queue_head;
718         write_queue_head = q->next;
719         if (write_queue_head == NULL)
720                 write_queue_tail = NULL;
721
722         pthread_mutex_unlock (&write_lock);
723
724         (void) plugin_set_ctx (q->ctx);
725
726         vl = q->vl;
727         sfree (q);
728         return (vl);
729 } /* }}} value_list_t *plugin_write_dequeue */
730
731 static void *plugin_write_thread (void __attribute__((unused)) *args) /* {{{ */
732 {
733         while (write_loop)
734         {
735                 value_list_t *vl = plugin_write_dequeue ();
736                 if (vl == NULL)
737                         continue;
738
739                 plugin_dispatch_values_internal (vl);
740
741                 plugin_value_list_free (vl);
742         }
743
744         pthread_exit (NULL);
745         return ((void *) 0);
746 } /* }}} void *plugin_write_thread */
747
748 static void start_write_threads (size_t num) /* {{{ */
749 {
750         size_t i;
751
752         if (write_threads != NULL)
753                 return;
754
755         write_threads = (pthread_t *) calloc (num, sizeof (pthread_t));
756         if (write_threads == NULL)
757         {
758                 ERROR ("plugin: start_write_threads: calloc failed.");
759                 return;
760         }
761
762         write_threads_num = 0;
763         for (i = 0; i < num; i++)
764         {
765                 int status;
766
767                 status = pthread_create (write_threads + write_threads_num,
768                                 /* attr = */ NULL,
769                                 plugin_write_thread,
770                                 /* arg = */ NULL);
771                 if (status != 0)
772                 {
773                         char errbuf[1024];
774                         ERROR ("plugin: start_write_threads: pthread_create failed "
775                                         "with status %i (%s).", status,
776                                         sstrerror (status, errbuf, sizeof (errbuf)));
777                         return;
778                 }
779
780                 write_threads_num++;
781         } /* for (i) */
782 } /* }}} void start_write_threads */
783
784 static void stop_write_threads (void) /* {{{ */
785 {
786         write_queue_t *q;
787         int i;
788
789         if (write_threads == NULL)
790                 return;
791
792         INFO ("collectd: Stopping %zu write threads.", write_threads_num);
793
794         pthread_mutex_lock (&write_lock);
795         write_loop = 0;
796         DEBUG ("plugin: stop_write_threads: Signalling `write_cond'");
797         pthread_cond_broadcast (&write_cond);
798         pthread_mutex_unlock (&write_lock);
799
800         for (i = 0; i < write_threads_num; i++)
801         {
802                 if (pthread_join (write_threads[i], NULL) != 0)
803                 {
804                         ERROR ("plugin: stop_write_threads: pthread_join failed.");
805                 }
806                 write_threads[i] = (pthread_t) 0;
807         }
808         sfree (write_threads);
809         write_threads_num = 0;
810
811         pthread_mutex_lock (&write_lock);
812         i = 0;
813         for (q = write_queue_head; q != NULL; )
814         {
815                 write_queue_t *q1 = q;
816                 plugin_value_list_free (q->vl);
817                 q = q->next;
818                 sfree (q1);
819                 i++;
820         }
821         write_queue_head = NULL;
822         write_queue_tail = NULL;
823         pthread_mutex_unlock (&write_lock);
824
825         if (i > 0)
826         {
827                 WARNING ("plugin: %i value list%s left after shutting down "
828                                 "the write threads.",
829                                 i, (i == 1) ? " was" : "s were");
830         }
831 } /* }}} void stop_write_threads */
832
833 /*
834  * Public functions
835  */
836 void plugin_set_dir (const char *dir)
837 {
838         if (plugindir != NULL)
839                 free (plugindir);
840
841         if (dir == NULL)
842                 plugindir = NULL;
843         else if ((plugindir = strdup (dir)) == NULL)
844         {
845                 char errbuf[1024];
846                 ERROR ("strdup failed: %s",
847                                 sstrerror (errno, errbuf, sizeof (errbuf)));
848         }
849 }
850
851 #define BUFSIZE 512
852 int plugin_load (const char *type, uint32_t flags)
853 {
854         DIR  *dh;
855         const char *dir;
856         char  filename[BUFSIZE] = "";
857         char  typename[BUFSIZE];
858         int   typename_len;
859         int   ret;
860         struct stat    statbuf;
861         struct dirent *de;
862         int status;
863
864         dir = plugin_get_dir ();
865         ret = 1;
866
867         /* `cpu' should not match `cpufreq'. To solve this we add `.so' to the
868          * type when matching the filename */
869         status = ssnprintf (typename, sizeof (typename), "%s.so", type);
870         if ((status < 0) || ((size_t) status >= sizeof (typename)))
871         {
872                 WARNING ("plugin_load: Filename too long: \"%s.so\"", type);
873                 return (-1);
874         }
875         typename_len = strlen (typename);
876
877         if ((dh = opendir (dir)) == NULL)
878         {
879                 char errbuf[1024];
880                 ERROR ("plugin_load: opendir (%s) failed: %s", dir,
881                                 sstrerror (errno, errbuf, sizeof (errbuf)));
882                 return (-1);
883         }
884
885         while ((de = readdir (dh)) != NULL)
886         {
887                 if (strncasecmp (de->d_name, typename, typename_len))
888                         continue;
889
890                 status = ssnprintf (filename, sizeof (filename),
891                                 "%s/%s", dir, de->d_name);
892                 if ((status < 0) || ((size_t) status >= sizeof (filename)))
893                 {
894                         WARNING ("plugin_load: Filename too long: \"%s/%s\"",
895                                         dir, de->d_name);
896                         continue;
897                 }
898
899                 if (lstat (filename, &statbuf) == -1)
900                 {
901                         char errbuf[1024];
902                         WARNING ("plugin_load: stat (\"%s\") failed: %s",
903                                         filename,
904                                         sstrerror (errno, errbuf, sizeof (errbuf)));
905                         continue;
906                 }
907                 else if (!S_ISREG (statbuf.st_mode))
908                 {
909                         /* don't follow symlinks */
910                         WARNING ("plugin_load: %s is not a regular file.",
911                                         filename);
912                         continue;
913                 }
914
915                 status = plugin_load_file (filename, flags);
916                 if (status == 0)
917                 {
918                         /* success */
919                         ret = 0;
920                         break;
921                 }
922                 else
923                 {
924                         ERROR ("plugin_load: Load plugin \"%s\" failed with "
925                                         "status %i.", type, status);
926                 }
927         }
928
929         closedir (dh);
930
931         if (filename[0] == 0)
932                 ERROR ("plugin_load: Could not find plugin \"%s\" in %s",
933                                 type, dir);
934
935         return (ret);
936 }
937
938 /*
939  * The `register_*' functions follow
940  */
941 int plugin_register_config (const char *name,
942                 int (*callback) (const char *key, const char *val),
943                 const char **keys, int keys_num)
944 {
945         cf_register (name, callback, keys, keys_num);
946         return (0);
947 } /* int plugin_register_config */
948
949 int plugin_register_complex_config (const char *type,
950                 int (*callback) (oconfig_item_t *))
951 {
952         return (cf_register_complex (type, callback));
953 } /* int plugin_register_complex_config */
954
955 int plugin_register_init (const char *name,
956                 int (*callback) (void))
957 {
958         return (create_register_callback (&list_init, name, (void *) callback,
959                                 /* user_data = */ NULL));
960 } /* plugin_register_init */
961
962 static int plugin_compare_read_func (const void *arg0, const void *arg1)
963 {
964         const read_func_t *rf0;
965         const read_func_t *rf1;
966
967         rf0 = arg0;
968         rf1 = arg1;
969
970         if (rf0->rf_next_read.tv_sec < rf1->rf_next_read.tv_sec)
971                 return (-1);
972         else if (rf0->rf_next_read.tv_sec > rf1->rf_next_read.tv_sec)
973                 return (1);
974         else if (rf0->rf_next_read.tv_nsec < rf1->rf_next_read.tv_nsec)
975                 return (-1);
976         else if (rf0->rf_next_read.tv_nsec > rf1->rf_next_read.tv_nsec)
977                 return (1);
978         else
979                 return (0);
980 } /* int plugin_compare_read_func */
981
982 /* Add a read function to both, the heap and a linked list. The linked list if
983  * used to look-up read functions, especially for the remove function. The heap
984  * is used to determine which plugin to read next. */
985 static int plugin_insert_read (read_func_t *rf)
986 {
987         int status;
988         llentry_t *le;
989
990         pthread_mutex_lock (&read_lock);
991
992         if (read_list == NULL)
993         {
994                 read_list = llist_create ();
995                 if (read_list == NULL)
996                 {
997                         pthread_mutex_unlock (&read_lock);
998                         ERROR ("plugin_insert_read: read_list failed.");
999                         return (-1);
1000                 }
1001         }
1002
1003         if (read_heap == NULL)
1004         {
1005                 read_heap = c_heap_create (plugin_compare_read_func);
1006                 if (read_heap == NULL)
1007                 {
1008                         pthread_mutex_unlock (&read_lock);
1009                         ERROR ("plugin_insert_read: c_heap_create failed.");
1010                         return (-1);
1011                 }
1012         }
1013
1014         le = llist_search (read_list, rf->rf_name);
1015         if (le != NULL)
1016         {
1017                 pthread_mutex_unlock (&read_lock);
1018                 WARNING ("The read function \"%s\" is already registered. "
1019                                 "Check for duplicate \"LoadPlugin\" lines "
1020                                 "in your configuration!",
1021                                 rf->rf_name);
1022                 return (EINVAL);
1023         }
1024
1025         le = llentry_create (rf->rf_name, rf);
1026         if (le == NULL)
1027         {
1028                 pthread_mutex_unlock (&read_lock);
1029                 ERROR ("plugin_insert_read: llentry_create failed.");
1030                 return (-1);
1031         }
1032
1033         status = c_heap_insert (read_heap, rf);
1034         if (status != 0)
1035         {
1036                 pthread_mutex_unlock (&read_lock);
1037                 ERROR ("plugin_insert_read: c_heap_insert failed.");
1038                 llentry_destroy (le);
1039                 return (-1);
1040         }
1041
1042         /* This does not fail. */
1043         llist_append (read_list, le);
1044
1045         /* Wake up all the read threads. */
1046         pthread_cond_broadcast (&read_cond);
1047         pthread_mutex_unlock (&read_lock);
1048         return (0);
1049 } /* int plugin_insert_read */
1050
1051 static int read_cb_wrapper (user_data_t *ud)
1052 {
1053         int (*callback) (void);
1054
1055         if (ud == NULL)
1056                 return -1;
1057
1058         callback = ud->data;
1059         return callback();
1060 } /* int read_cb_wrapper */
1061
1062 int plugin_register_read (const char *name,
1063                 int (*callback) (void))
1064 {
1065         read_func_t *rf;
1066         plugin_ctx_t ctx = plugin_get_ctx ();
1067         int status;
1068
1069         if (ctx.interval != 0) {
1070                 /* If ctx.interval is not zero (== use the plugin or global
1071                  * interval), we need to use the "complex" read callback,
1072                  * because only that allows to specify a different interval.
1073                  * Wrap the callback using read_cb_wrapper(). */
1074                 struct timespec interval;
1075                 user_data_t user_data;
1076
1077                 user_data.data = callback;
1078                 user_data.free_func = NULL;
1079
1080                 CDTIME_T_TO_TIMESPEC (ctx.interval, &interval);
1081                 return plugin_register_complex_read (/* group = */ NULL,
1082                                 name, read_cb_wrapper, &interval, &user_data);
1083         }
1084
1085         DEBUG ("plugin_register_read: default_interval = %.3f",
1086                         CDTIME_T_TO_DOUBLE(plugin_get_interval ()));
1087
1088         rf = malloc (sizeof (*rf));
1089         if (rf == NULL)
1090         {
1091                 ERROR ("plugin_register_read: malloc failed.");
1092                 return (ENOMEM);
1093         }
1094
1095         memset (rf, 0, sizeof (read_func_t));
1096         rf->rf_callback = (void *) callback;
1097         rf->rf_udata.data = NULL;
1098         rf->rf_udata.free_func = NULL;
1099         rf->rf_ctx = ctx;
1100         rf->rf_group[0] = '\0';
1101         sstrncpy (rf->rf_name, name, sizeof (rf->rf_name));
1102         rf->rf_type = RF_SIMPLE;
1103         rf->rf_interval.tv_sec = 0;
1104         rf->rf_interval.tv_nsec = 0;
1105         rf->rf_effective_interval = rf->rf_interval;
1106
1107         status = plugin_insert_read (rf);
1108         if (status != 0)
1109                 sfree (rf);
1110
1111         return (status);
1112 } /* int plugin_register_read */
1113
1114 int plugin_register_complex_read (const char *group, const char *name,
1115                 plugin_read_cb callback,
1116                 const struct timespec *interval,
1117                 user_data_t *user_data)
1118 {
1119         read_func_t *rf;
1120         plugin_ctx_t ctx = plugin_get_ctx ();
1121         int status;
1122
1123         rf = malloc (sizeof (*rf));
1124         if (rf == NULL)
1125         {
1126                 ERROR ("plugin_register_complex_read: malloc failed.");
1127                 return (ENOMEM);
1128         }
1129
1130         memset (rf, 0, sizeof (read_func_t));
1131         rf->rf_callback = (void *) callback;
1132         if (group != NULL)
1133                 sstrncpy (rf->rf_group, group, sizeof (rf->rf_group));
1134         else
1135                 rf->rf_group[0] = '\0';
1136         sstrncpy (rf->rf_name, name, sizeof (rf->rf_name));
1137         rf->rf_type = RF_COMPLEX;
1138         if (interval != NULL)
1139         {
1140                 rf->rf_interval = *interval;
1141         }
1142         else if (ctx.interval != 0)
1143         {
1144                 CDTIME_T_TO_TIMESPEC (ctx.interval, &rf->rf_interval);
1145         }
1146         rf->rf_effective_interval = rf->rf_interval;
1147
1148         DEBUG ("plugin_register_read: interval = %i.%09i",
1149                         (int) rf->rf_interval.tv_sec,
1150                         (int) rf->rf_interval.tv_nsec);
1151
1152         /* Set user data */
1153         if (user_data == NULL)
1154         {
1155                 rf->rf_udata.data = NULL;
1156                 rf->rf_udata.free_func = NULL;
1157         }
1158         else
1159         {
1160                 rf->rf_udata = *user_data;
1161         }
1162
1163         rf->rf_ctx = ctx;
1164
1165         status = plugin_insert_read (rf);
1166         if (status != 0)
1167                 sfree (rf);
1168
1169         return (status);
1170 } /* int plugin_register_complex_read */
1171
1172 int plugin_register_write (const char *name,
1173                 plugin_write_cb callback, user_data_t *ud)
1174 {
1175         return (create_register_callback (&list_write, name,
1176                                 (void *) callback, ud));
1177 } /* int plugin_register_write */
1178
1179 int plugin_register_flush (const char *name,
1180                 plugin_flush_cb callback, user_data_t *ud)
1181 {
1182         return (create_register_callback (&list_flush, name,
1183                                 (void *) callback, ud));
1184 } /* int plugin_register_flush */
1185
1186 int plugin_register_missing (const char *name,
1187                 plugin_missing_cb callback, user_data_t *ud)
1188 {
1189         return (create_register_callback (&list_missing, name,
1190                                 (void *) callback, ud));
1191 } /* int plugin_register_missing */
1192
1193 int plugin_register_shutdown (const char *name,
1194                 int (*callback) (void))
1195 {
1196         return (create_register_callback (&list_shutdown, name,
1197                                 (void *) callback, /* user_data = */ NULL));
1198 } /* int plugin_register_shutdown */
1199
1200 int plugin_register_data_set (const data_set_t *ds)
1201 {
1202         data_set_t *ds_copy;
1203         int i;
1204
1205         if ((data_sets != NULL)
1206                         && (c_avl_get (data_sets, ds->type, NULL) == 0))
1207         {
1208                 NOTICE ("Replacing DS `%s' with another version.", ds->type);
1209                 plugin_unregister_data_set (ds->type);
1210         }
1211         else if (data_sets == NULL)
1212         {
1213                 data_sets = c_avl_create ((int (*) (const void *, const void *)) strcmp);
1214                 if (data_sets == NULL)
1215                         return (-1);
1216         }
1217
1218         ds_copy = (data_set_t *) malloc (sizeof (data_set_t));
1219         if (ds_copy == NULL)
1220                 return (-1);
1221         memcpy(ds_copy, ds, sizeof (data_set_t));
1222
1223         ds_copy->ds = (data_source_t *) malloc (sizeof (data_source_t)
1224                         * ds->ds_num);
1225         if (ds_copy->ds == NULL)
1226         {
1227                 free (ds_copy);
1228                 return (-1);
1229         }
1230
1231         for (i = 0; i < ds->ds_num; i++)
1232                 memcpy (ds_copy->ds + i, ds->ds + i, sizeof (data_source_t));
1233
1234         return (c_avl_insert (data_sets, (void *) ds_copy->type, (void *) ds_copy));
1235 } /* int plugin_register_data_set */
1236
1237 int plugin_register_log (const char *name,
1238                 plugin_log_cb callback, user_data_t *ud)
1239 {
1240         return (create_register_callback (&list_log, name,
1241                                 (void *) callback, ud));
1242 } /* int plugin_register_log */
1243
1244 int plugin_register_notification (const char *name,
1245                 plugin_notification_cb callback, user_data_t *ud)
1246 {
1247         return (create_register_callback (&list_notification, name,
1248                                 (void *) callback, ud));
1249 } /* int plugin_register_log */
1250
1251 int plugin_unregister_config (const char *name)
1252 {
1253         cf_unregister (name);
1254         return (0);
1255 } /* int plugin_unregister_config */
1256
1257 int plugin_unregister_complex_config (const char *name)
1258 {
1259         cf_unregister_complex (name);
1260         return (0);
1261 } /* int plugin_unregister_complex_config */
1262
1263 int plugin_unregister_init (const char *name)
1264 {
1265         return (plugin_unregister (list_init, name));
1266 }
1267
1268 int plugin_unregister_read (const char *name) /* {{{ */
1269 {
1270         llentry_t *le;
1271         read_func_t *rf;
1272
1273         if (name == NULL)
1274                 return (-ENOENT);
1275
1276         pthread_mutex_lock (&read_lock);
1277
1278         if (read_list == NULL)
1279         {
1280                 pthread_mutex_unlock (&read_lock);
1281                 return (-ENOENT);
1282         }
1283
1284         le = llist_search (read_list, name);
1285         if (le == NULL)
1286         {
1287                 pthread_mutex_unlock (&read_lock);
1288                 WARNING ("plugin_unregister_read: No such read function: %s",
1289                                 name);
1290                 return (-ENOENT);
1291         }
1292
1293         llist_remove (read_list, le);
1294
1295         rf = le->value;
1296         assert (rf != NULL);
1297         rf->rf_type = RF_REMOVE;
1298
1299         pthread_mutex_unlock (&read_lock);
1300
1301         llentry_destroy (le);
1302
1303         DEBUG ("plugin_unregister_read: Marked `%s' for removal.", name);
1304
1305         return (0);
1306 } /* }}} int plugin_unregister_read */
1307
1308 static int compare_read_func_group (llentry_t *e, void *ud) /* {{{ */
1309 {
1310         read_func_t *rf    = e->value;
1311         char        *group = ud;
1312
1313         return strcmp (rf->rf_group, (const char *)group);
1314 } /* }}} int compare_read_func_group */
1315
1316 int plugin_unregister_read_group (const char *group) /* {{{ */
1317 {
1318         llentry_t *le;
1319         read_func_t *rf;
1320
1321         int found = 0;
1322
1323         if (group == NULL)
1324                 return (-ENOENT);
1325
1326         pthread_mutex_lock (&read_lock);
1327
1328         if (read_list == NULL)
1329         {
1330                 pthread_mutex_unlock (&read_lock);
1331                 return (-ENOENT);
1332         }
1333
1334         while (42)
1335         {
1336                 le = llist_search_custom (read_list,
1337                                 compare_read_func_group, (void *)group);
1338
1339                 if (le == NULL)
1340                         break;
1341
1342                 ++found;
1343
1344                 llist_remove (read_list, le);
1345
1346                 rf = le->value;
1347                 assert (rf != NULL);
1348                 rf->rf_type = RF_REMOVE;
1349
1350                 llentry_destroy (le);
1351
1352                 DEBUG ("plugin_unregister_read_group: "
1353                                 "Marked `%s' (group `%s') for removal.",
1354                                 rf->rf_name, group);
1355         }
1356
1357         pthread_mutex_unlock (&read_lock);
1358
1359         if (found == 0)
1360         {
1361                 WARNING ("plugin_unregister_read_group: No such "
1362                                 "group of read function: %s", group);
1363                 return (-ENOENT);
1364         }
1365
1366         return (0);
1367 } /* }}} int plugin_unregister_read_group */
1368
1369 int plugin_unregister_write (const char *name)
1370 {
1371         return (plugin_unregister (list_write, name));
1372 }
1373
1374 int plugin_unregister_flush (const char *name)
1375 {
1376         return (plugin_unregister (list_flush, name));
1377 }
1378
1379 int plugin_unregister_missing (const char *name)
1380 {
1381         return (plugin_unregister (list_missing, name));
1382 }
1383
1384 int plugin_unregister_shutdown (const char *name)
1385 {
1386         return (plugin_unregister (list_shutdown, name));
1387 }
1388
1389 int plugin_unregister_data_set (const char *name)
1390 {
1391         data_set_t *ds;
1392
1393         if (data_sets == NULL)
1394                 return (-1);
1395
1396         if (c_avl_remove (data_sets, name, NULL, (void *) &ds) != 0)
1397                 return (-1);
1398
1399         sfree (ds->ds);
1400         sfree (ds);
1401
1402         return (0);
1403 } /* int plugin_unregister_data_set */
1404
1405 int plugin_unregister_log (const char *name)
1406 {
1407         return (plugin_unregister (list_log, name));
1408 }
1409
1410 int plugin_unregister_notification (const char *name)
1411 {
1412         return (plugin_unregister (list_notification, name));
1413 }
1414
1415 void plugin_init_all (void)
1416 {
1417         const char *chain_name;
1418         llentry_t *le;
1419         int status;
1420
1421         /* Init the value cache */
1422         uc_init ();
1423
1424         chain_name = global_option_get ("PreCacheChain");
1425         pre_cache_chain = fc_chain_get_by_name (chain_name);
1426
1427         chain_name = global_option_get ("PostCacheChain");
1428         post_cache_chain = fc_chain_get_by_name (chain_name);
1429
1430         {
1431                 char const *tmp = global_option_get ("WriteThreads");
1432                 int num = atoi (tmp);
1433
1434                 if (num < 1)
1435                         num = 5;
1436
1437                 start_write_threads ((size_t) num);
1438         }
1439
1440         if ((list_init == NULL) && (read_heap == NULL))
1441                 return;
1442
1443         /* Calling all init callbacks before checking if read callbacks
1444          * are available allows the init callbacks to register the read
1445          * callback. */
1446         le = llist_head (list_init);
1447         while (le != NULL)
1448         {
1449                 callback_func_t *cf;
1450                 plugin_init_cb callback;
1451                 plugin_ctx_t old_ctx;
1452
1453                 cf = le->value;
1454                 old_ctx = plugin_set_ctx (cf->cf_ctx);
1455                 callback = cf->cf_callback;
1456                 status = (*callback) ();
1457                 plugin_set_ctx (old_ctx);
1458
1459                 if (status != 0)
1460                 {
1461                         ERROR ("Initialization of plugin `%s' "
1462                                         "failed with status %i. "
1463                                         "Plugin will be unloaded.",
1464                                         le->key, status);
1465                         /* Plugins that register read callbacks from the init
1466                          * callback should take care of appropriate error
1467                          * handling themselves. */
1468                         /* FIXME: Unload _all_ functions */
1469                         plugin_unregister_read (le->key);
1470                 }
1471
1472                 le = le->next;
1473         }
1474
1475         /* Start read-threads */
1476         if (read_heap != NULL)
1477         {
1478                 const char *rt;
1479                 int num;
1480                 rt = global_option_get ("ReadThreads");
1481                 num = atoi (rt);
1482                 if (num != -1)
1483                         start_read_threads ((num > 0) ? num : 5);
1484         }
1485 } /* void plugin_init_all */
1486
1487 /* TODO: Rename this function. */
1488 void plugin_read_all (void)
1489 {
1490         uc_check_timeout ();
1491
1492         return;
1493 } /* void plugin_read_all */
1494
1495 /* Read function called when the `-T' command line argument is given. */
1496 int plugin_read_all_once (void)
1497 {
1498         int status;
1499         int return_status = 0;
1500
1501         if (read_heap == NULL)
1502         {
1503                 NOTICE ("No read-functions are registered.");
1504                 return (0);
1505         }
1506
1507         while (42)
1508         {
1509                 read_func_t *rf;
1510                 plugin_ctx_t old_ctx;
1511
1512                 rf = c_heap_get_root (read_heap);
1513                 if (rf == NULL)
1514                         break;
1515
1516                 old_ctx = plugin_set_ctx (rf->rf_ctx);
1517
1518                 if (rf->rf_type == RF_SIMPLE)
1519                 {
1520                         int (*callback) (void);
1521
1522                         callback = rf->rf_callback;
1523                         status = (*callback) ();
1524                 }
1525                 else
1526                 {
1527                         plugin_read_cb callback;
1528
1529                         callback = rf->rf_callback;
1530                         status = (*callback) (&rf->rf_udata);
1531                 }
1532
1533                 plugin_set_ctx (old_ctx);
1534
1535                 if (status != 0)
1536                 {
1537                         NOTICE ("read-function of plugin `%s' failed.",
1538                                         rf->rf_name);
1539                         return_status = -1;
1540                 }
1541
1542                 destroy_callback ((void *) rf);
1543         }
1544
1545         return (return_status);
1546 } /* int plugin_read_all_once */
1547
1548 int plugin_write (const char *plugin, /* {{{ */
1549                 const data_set_t *ds, const value_list_t *vl)
1550 {
1551   llentry_t *le;
1552   int status;
1553
1554   if (vl == NULL)
1555     return (EINVAL);
1556
1557   if (list_write == NULL)
1558     return (ENOENT);
1559
1560   if (ds == NULL)
1561   {
1562     ds = plugin_get_ds (vl->type);
1563     if (ds == NULL)
1564     {
1565       ERROR ("plugin_write: Unable to lookup type `%s'.", vl->type);
1566       return (ENOENT);
1567     }
1568   }
1569
1570   if (plugin == NULL)
1571   {
1572     int success = 0;
1573     int failure = 0;
1574
1575     le = llist_head (list_write);
1576     while (le != NULL)
1577     {
1578       callback_func_t *cf = le->value;
1579       plugin_write_cb callback;
1580
1581       /* do not switch plugin context; rather keep the context (interval)
1582        * information of the calling read plugin */
1583
1584       DEBUG ("plugin: plugin_write: Writing values via %s.", le->key);
1585       callback = cf->cf_callback;
1586       status = (*callback) (ds, vl, &cf->cf_udata);
1587       if (status != 0)
1588         failure++;
1589       else
1590         success++;
1591
1592       le = le->next;
1593     }
1594
1595     if ((success == 0) && (failure != 0))
1596       status = -1;
1597     else
1598       status = 0;
1599   }
1600   else /* plugin != NULL */
1601   {
1602     callback_func_t *cf;
1603     plugin_write_cb callback;
1604
1605     le = llist_head (list_write);
1606     while (le != NULL)
1607     {
1608       if (strcasecmp (plugin, le->key) == 0)
1609         break;
1610
1611       le = le->next;
1612     }
1613
1614     if (le == NULL)
1615       return (ENOENT);
1616
1617     cf = le->value;
1618
1619     /* do not switch plugin context; rather keep the context (interval)
1620      * information of the calling read plugin */
1621
1622     DEBUG ("plugin: plugin_write: Writing values via %s.", le->key);
1623     callback = cf->cf_callback;
1624     status = (*callback) (ds, vl, &cf->cf_udata);
1625   }
1626
1627   return (status);
1628 } /* }}} int plugin_write */
1629
1630 int plugin_flush (const char *plugin, cdtime_t timeout, const char *identifier)
1631 {
1632   llentry_t *le;
1633
1634   if (list_flush == NULL)
1635     return (0);
1636
1637   le = llist_head (list_flush);
1638   while (le != NULL)
1639   {
1640     callback_func_t *cf;
1641     plugin_flush_cb callback;
1642     plugin_ctx_t old_ctx;
1643
1644     if ((plugin != NULL)
1645         && (strcmp (plugin, le->key) != 0))
1646     {
1647       le = le->next;
1648       continue;
1649     }
1650
1651     cf = le->value;
1652     old_ctx = plugin_set_ctx (cf->cf_ctx);
1653     callback = cf->cf_callback;
1654
1655     (*callback) (timeout, identifier, &cf->cf_udata);
1656
1657     plugin_set_ctx (old_ctx);
1658
1659     le = le->next;
1660   }
1661   return (0);
1662 } /* int plugin_flush */
1663
1664 void plugin_shutdown_all (void)
1665 {
1666         llentry_t *le;
1667
1668         stop_read_threads ();
1669
1670         destroy_all_callbacks (&list_init);
1671
1672         pthread_mutex_lock (&read_lock);
1673         llist_destroy (read_list);
1674         read_list = NULL;
1675         pthread_mutex_unlock (&read_lock);
1676
1677         destroy_read_heap ();
1678
1679         plugin_flush (/* plugin = */ NULL,
1680                         /* timeout = */ 0,
1681                         /* identifier = */ NULL);
1682
1683         le = NULL;
1684         if (list_shutdown != NULL)
1685                 le = llist_head (list_shutdown);
1686
1687         while (le != NULL)
1688         {
1689                 callback_func_t *cf;
1690                 plugin_shutdown_cb callback;
1691                 plugin_ctx_t old_ctx;
1692
1693                 cf = le->value;
1694                 old_ctx = plugin_set_ctx (cf->cf_ctx);
1695                 callback = cf->cf_callback;
1696
1697                 /* Advance the pointer before calling the callback allows
1698                  * shutdown functions to unregister themselves. If done the
1699                  * other way around the memory `le' points to will be freed
1700                  * after callback returns. */
1701                 le = le->next;
1702
1703                 (*callback) ();
1704
1705                 plugin_set_ctx (old_ctx);
1706         }
1707
1708         stop_write_threads ();
1709
1710         /* Write plugins which use the `user_data' pointer usually need the
1711          * same data available to the flush callback. If this is the case, set
1712          * the free_function to NULL when registering the flush callback and to
1713          * the real free function when registering the write callback. This way
1714          * the data isn't freed twice. */
1715         destroy_all_callbacks (&list_flush);
1716         destroy_all_callbacks (&list_missing);
1717         destroy_all_callbacks (&list_write);
1718
1719         destroy_all_callbacks (&list_notification);
1720         destroy_all_callbacks (&list_shutdown);
1721         destroy_all_callbacks (&list_log);
1722 } /* void plugin_shutdown_all */
1723
1724 int plugin_dispatch_missing (const value_list_t *vl) /* {{{ */
1725 {
1726   llentry_t *le;
1727
1728   if (list_missing == NULL)
1729     return (0);
1730
1731   le = llist_head (list_missing);
1732   while (le != NULL)
1733   {
1734     callback_func_t *cf;
1735     plugin_missing_cb callback;
1736     plugin_ctx_t old_ctx;
1737     int status;
1738
1739     cf = le->value;
1740     old_ctx = plugin_set_ctx (cf->cf_ctx);
1741     callback = cf->cf_callback;
1742
1743     status = (*callback) (vl, &cf->cf_udata);
1744     plugin_set_ctx (old_ctx);
1745     if (status != 0)
1746     {
1747       if (status < 0)
1748       {
1749         ERROR ("plugin_dispatch_missing: Callback function \"%s\" "
1750             "failed with status %i.",
1751             le->key, status);
1752         return (status);
1753       }
1754       else
1755       {
1756         return (0);
1757       }
1758     }
1759
1760     le = le->next;
1761   }
1762   return (0);
1763 } /* int }}} plugin_dispatch_missing */
1764
1765 static int plugin_dispatch_values_internal (value_list_t *vl)
1766 {
1767         int status;
1768         static c_complain_t no_write_complaint = C_COMPLAIN_INIT_STATIC;
1769
1770         value_t *saved_values;
1771         int      saved_values_len;
1772
1773         data_set_t *ds;
1774
1775         int free_meta_data = 0;
1776
1777         if ((vl == NULL) || (vl->type[0] == 0)
1778                         || (vl->values == NULL) || (vl->values_len < 1))
1779         {
1780                 ERROR ("plugin_dispatch_values: Invalid value list "
1781                                 "from plugin %s.", vl->plugin);
1782                 return (-1);
1783         }
1784
1785         /* Free meta data only if the calling function didn't specify any. In
1786          * this case matches and targets may add some and the calling function
1787          * may not expect (and therefore free) that data. */
1788         if (vl->meta == NULL)
1789                 free_meta_data = 1;
1790
1791         if (list_write == NULL)
1792                 c_complain_once (LOG_WARNING, &no_write_complaint,
1793                                 "plugin_dispatch_values: No write callback has been "
1794                                 "registered. Please load at least one output plugin, "
1795                                 "if you want the collected data to be stored.");
1796
1797         if (data_sets == NULL)
1798         {
1799                 ERROR ("plugin_dispatch_values: No data sets registered. "
1800                                 "Could the types database be read? Check "
1801                                 "your `TypesDB' setting!");
1802                 return (-1);
1803         }
1804
1805         if (c_avl_get (data_sets, vl->type, (void *) &ds) != 0)
1806         {
1807                 char ident[6 * DATA_MAX_NAME_LEN];
1808
1809                 FORMAT_VL (ident, sizeof (ident), vl);
1810                 INFO ("plugin_dispatch_values: Dataset not found: %s "
1811                                 "(from \"%s\"), check your types.db!",
1812                                 vl->type, ident);
1813                 return (-1);
1814         }
1815
1816         /* Assured by plugin_value_list_clone(). The time is determined at
1817          * _enqueue_ time. */
1818         assert (vl->time != 0);
1819         assert (vl->interval != 0);
1820
1821         DEBUG ("plugin_dispatch_values: time = %.3f; interval = %.3f; "
1822                         "host = %s; "
1823                         "plugin = %s; plugin_instance = %s; "
1824                         "type = %s; type_instance = %s;",
1825                         CDTIME_T_TO_DOUBLE (vl->time),
1826                         CDTIME_T_TO_DOUBLE (vl->interval),
1827                         vl->host,
1828                         vl->plugin, vl->plugin_instance,
1829                         vl->type, vl->type_instance);
1830
1831 #if COLLECT_DEBUG
1832         assert (0 == strcmp (ds->type, vl->type));
1833 #else
1834         if (0 != strcmp (ds->type, vl->type))
1835                 WARNING ("plugin_dispatch_values: (ds->type = %s) != (vl->type = %s)",
1836                                 ds->type, vl->type);
1837 #endif
1838
1839 #if COLLECT_DEBUG
1840         assert (ds->ds_num == vl->values_len);
1841 #else
1842         if (ds->ds_num != vl->values_len)
1843         {
1844                 ERROR ("plugin_dispatch_values: ds->type = %s: "
1845                                 "(ds->ds_num = %i) != "
1846                                 "(vl->values_len = %i)",
1847                                 ds->type, ds->ds_num, vl->values_len);
1848                 return (-1);
1849         }
1850 #endif
1851
1852         escape_slashes (vl->host, sizeof (vl->host));
1853         escape_slashes (vl->plugin, sizeof (vl->plugin));
1854         escape_slashes (vl->plugin_instance, sizeof (vl->plugin_instance));
1855         escape_slashes (vl->type, sizeof (vl->type));
1856         escape_slashes (vl->type_instance, sizeof (vl->type_instance));
1857
1858         /* Copy the values. This way, we can assure `targets' that they get
1859          * dynamically allocated values, which they can free and replace if
1860          * they like. */
1861         if ((pre_cache_chain != NULL) || (post_cache_chain != NULL))
1862         {
1863                 saved_values     = vl->values;
1864                 saved_values_len = vl->values_len;
1865
1866                 vl->values = (value_t *) calloc (vl->values_len,
1867                                 sizeof (*vl->values));
1868                 if (vl->values == NULL)
1869                 {
1870                         ERROR ("plugin_dispatch_values: calloc failed.");
1871                         vl->values = saved_values;
1872                         return (-1);
1873                 }
1874                 memcpy (vl->values, saved_values,
1875                                 vl->values_len * sizeof (*vl->values));
1876         }
1877         else /* if ((pre == NULL) && (post == NULL)) */
1878         {
1879                 saved_values     = NULL;
1880                 saved_values_len = 0;
1881         }
1882
1883         if (pre_cache_chain != NULL)
1884         {
1885                 status = fc_process_chain (ds, vl, pre_cache_chain);
1886                 if (status < 0)
1887                 {
1888                         WARNING ("plugin_dispatch_values: Running the "
1889                                         "pre-cache chain failed with "
1890                                         "status %i (%#x).",
1891                                         status, status);
1892                 }
1893                 else if (status == FC_TARGET_STOP)
1894                 {
1895                         /* Restore the state of the value_list so that plugins
1896                          * don't get confused.. */
1897                         if (saved_values != NULL)
1898                         {
1899                                 free (vl->values);
1900                                 vl->values     = saved_values;
1901                                 vl->values_len = saved_values_len;
1902                         }
1903                         return (0);
1904                 }
1905         }
1906
1907         /* Update the value cache */
1908         uc_update (ds, vl);
1909
1910         if (post_cache_chain != NULL)
1911         {
1912                 status = fc_process_chain (ds, vl, post_cache_chain);
1913                 if (status < 0)
1914                 {
1915                         WARNING ("plugin_dispatch_values: Running the "
1916                                         "post-cache chain failed with "
1917                                         "status %i (%#x).",
1918                                         status, status);
1919                 }
1920         }
1921         else
1922                 fc_default_action (ds, vl);
1923
1924         /* Restore the state of the value_list so that plugins don't get
1925          * confused.. */
1926         if (saved_values != NULL)
1927         {
1928                 free (vl->values);
1929                 vl->values     = saved_values;
1930                 vl->values_len = saved_values_len;
1931         }
1932
1933         if ((free_meta_data != 0) && (vl->meta != NULL))
1934         {
1935                 meta_data_destroy (vl->meta);
1936                 vl->meta = NULL;
1937         }
1938
1939         return (0);
1940 } /* int plugin_dispatch_values_internal */
1941
1942 int plugin_dispatch_values (value_list_t const *vl)
1943 {
1944         int status;
1945
1946         status = plugin_write_enqueue (vl);
1947         if (status != 0)
1948         {
1949                 char errbuf[1024];
1950                 ERROR ("plugin_dispatch_values: plugin_write_enqueue failed "
1951                                 "with status %i (%s).", status,
1952                                 sstrerror (status, errbuf, sizeof (errbuf)));
1953                 return (status);
1954         }
1955
1956         return (0);
1957 }
1958
1959 int plugin_dispatch_notification (const notification_t *notif)
1960 {
1961         llentry_t *le;
1962         /* Possible TODO: Add flap detection here */
1963
1964         DEBUG ("plugin_dispatch_notification: severity = %i; message = %s; "
1965                         "time = %.3f; host = %s;",
1966                         notif->severity, notif->message,
1967                         CDTIME_T_TO_DOUBLE (notif->time), notif->host);
1968
1969         /* Nobody cares for notifications */
1970         if (list_notification == NULL)
1971                 return (-1);
1972
1973         le = llist_head (list_notification);
1974         while (le != NULL)
1975         {
1976                 callback_func_t *cf;
1977                 plugin_notification_cb callback;
1978                 int status;
1979
1980                 /* do not switch plugin context; rather keep the context
1981                  * (interval) information of the calling plugin */
1982
1983                 cf = le->value;
1984                 callback = cf->cf_callback;
1985                 status = (*callback) (notif, &cf->cf_udata);
1986                 if (status != 0)
1987                 {
1988                         WARNING ("plugin_dispatch_notification: Notification "
1989                                         "callback %s returned %i.",
1990                                         le->key, status);
1991                 }
1992
1993                 le = le->next;
1994         }
1995
1996         return (0);
1997 } /* int plugin_dispatch_notification */
1998
1999 void plugin_log (int level, const char *format, ...)
2000 {
2001         char msg[1024];
2002         va_list ap;
2003         llentry_t *le;
2004
2005 #if !COLLECT_DEBUG
2006         if (level >= LOG_DEBUG)
2007                 return;
2008 #endif
2009
2010         va_start (ap, format);
2011         vsnprintf (msg, sizeof (msg), format, ap);
2012         msg[sizeof (msg) - 1] = '\0';
2013         va_end (ap);
2014
2015         if (list_log == NULL)
2016         {
2017                 fprintf (stderr, "%s\n", msg);
2018                 return;
2019         }
2020
2021         le = llist_head (list_log);
2022         while (le != NULL)
2023         {
2024                 callback_func_t *cf;
2025                 plugin_log_cb callback;
2026
2027                 cf = le->value;
2028                 callback = cf->cf_callback;
2029
2030                 /* do not switch plugin context; rather keep the context
2031                  * (interval) information of the calling plugin */
2032
2033                 (*callback) (level, msg, &cf->cf_udata);
2034
2035                 le = le->next;
2036         }
2037 } /* void plugin_log */
2038
2039 int parse_log_severity (const char *severity)
2040 {
2041         int log_level = -1;
2042
2043         if ((0 == strcasecmp (severity, "emerg"))
2044                         || (0 == strcasecmp (severity, "alert"))
2045                         || (0 == strcasecmp (severity, "crit"))
2046                         || (0 == strcasecmp (severity, "err")))
2047                 log_level = LOG_ERR;
2048         else if (0 == strcasecmp (severity, "warning"))
2049                 log_level = LOG_WARNING;
2050         else if (0 == strcasecmp (severity, "notice"))
2051                 log_level = LOG_NOTICE;
2052         else if (0 == strcasecmp (severity, "info"))
2053                 log_level = LOG_INFO;
2054 #if COLLECT_DEBUG
2055         else if (0 == strcasecmp (severity, "debug"))
2056                 log_level = LOG_DEBUG;
2057 #endif /* COLLECT_DEBUG */
2058
2059         return (log_level);
2060 } /* int parse_log_severity */
2061
2062 int parse_notif_severity (const char *severity)
2063 {
2064         int notif_severity = -1;
2065
2066         if (strcasecmp (severity, "FAILURE") == 0)
2067                 notif_severity = NOTIF_FAILURE;
2068         else if (strcmp (severity, "OKAY") == 0)
2069                 notif_severity = NOTIF_OKAY;
2070         else if ((strcmp (severity, "WARNING") == 0)
2071                         || (strcmp (severity, "WARN") == 0))
2072                 notif_severity = NOTIF_WARNING;
2073
2074         return (notif_severity);
2075 } /* int parse_notif_severity */
2076
2077 const data_set_t *plugin_get_ds (const char *name)
2078 {
2079         data_set_t *ds;
2080
2081         if (c_avl_get (data_sets, name, (void *) &ds) != 0)
2082         {
2083                 DEBUG ("No such dataset registered: %s", name);
2084                 return (NULL);
2085         }
2086
2087         return (ds);
2088 } /* data_set_t *plugin_get_ds */
2089
2090 static int plugin_notification_meta_add (notification_t *n,
2091     const char *name,
2092     enum notification_meta_type_e type,
2093     const void *value)
2094 {
2095   notification_meta_t *meta;
2096   notification_meta_t *tail;
2097
2098   if ((n == NULL) || (name == NULL) || (value == NULL))
2099   {
2100     ERROR ("plugin_notification_meta_add: A pointer is NULL!");
2101     return (-1);
2102   }
2103
2104   meta = (notification_meta_t *) malloc (sizeof (notification_meta_t));
2105   if (meta == NULL)
2106   {
2107     ERROR ("plugin_notification_meta_add: malloc failed.");
2108     return (-1);
2109   }
2110   memset (meta, 0, sizeof (notification_meta_t));
2111
2112   sstrncpy (meta->name, name, sizeof (meta->name));
2113   meta->type = type;
2114
2115   switch (type)
2116   {
2117     case NM_TYPE_STRING:
2118     {
2119       meta->nm_value.nm_string = strdup ((const char *) value);
2120       if (meta->nm_value.nm_string == NULL)
2121       {
2122         ERROR ("plugin_notification_meta_add: strdup failed.");
2123         sfree (meta);
2124         return (-1);
2125       }
2126       break;
2127     }
2128     case NM_TYPE_SIGNED_INT:
2129     {
2130       meta->nm_value.nm_signed_int = *((int64_t *) value);
2131       break;
2132     }
2133     case NM_TYPE_UNSIGNED_INT:
2134     {
2135       meta->nm_value.nm_unsigned_int = *((uint64_t *) value);
2136       break;
2137     }
2138     case NM_TYPE_DOUBLE:
2139     {
2140       meta->nm_value.nm_double = *((double *) value);
2141       break;
2142     }
2143     case NM_TYPE_BOOLEAN:
2144     {
2145       meta->nm_value.nm_boolean = *((_Bool *) value);
2146       break;
2147     }
2148     default:
2149     {
2150       ERROR ("plugin_notification_meta_add: Unknown type: %i", type);
2151       sfree (meta);
2152       return (-1);
2153     }
2154   } /* switch (type) */
2155
2156   meta->next = NULL;
2157   tail = n->meta;
2158   while ((tail != NULL) && (tail->next != NULL))
2159     tail = tail->next;
2160
2161   if (tail == NULL)
2162     n->meta = meta;
2163   else
2164     tail->next = meta;
2165
2166   return (0);
2167 } /* int plugin_notification_meta_add */
2168
2169 int plugin_notification_meta_add_string (notification_t *n,
2170     const char *name,
2171     const char *value)
2172 {
2173   return (plugin_notification_meta_add (n, name, NM_TYPE_STRING, value));
2174 }
2175
2176 int plugin_notification_meta_add_signed_int (notification_t *n,
2177     const char *name,
2178     int64_t value)
2179 {
2180   return (plugin_notification_meta_add (n, name, NM_TYPE_SIGNED_INT, &value));
2181 }
2182
2183 int plugin_notification_meta_add_unsigned_int (notification_t *n,
2184     const char *name,
2185     uint64_t value)
2186 {
2187   return (plugin_notification_meta_add (n, name, NM_TYPE_UNSIGNED_INT, &value));
2188 }
2189
2190 int plugin_notification_meta_add_double (notification_t *n,
2191     const char *name,
2192     double value)
2193 {
2194   return (plugin_notification_meta_add (n, name, NM_TYPE_DOUBLE, &value));
2195 }
2196
2197 int plugin_notification_meta_add_boolean (notification_t *n,
2198     const char *name,
2199     _Bool value)
2200 {
2201   return (plugin_notification_meta_add (n, name, NM_TYPE_BOOLEAN, &value));
2202 }
2203
2204 int plugin_notification_meta_copy (notification_t *dst,
2205     const notification_t *src)
2206 {
2207   notification_meta_t *meta;
2208
2209   assert (dst != NULL);
2210   assert (src != NULL);
2211   assert (dst != src);
2212   assert ((src->meta == NULL) || (src->meta != dst->meta));
2213
2214   for (meta = src->meta; meta != NULL; meta = meta->next)
2215   {
2216     if (meta->type == NM_TYPE_STRING)
2217       plugin_notification_meta_add_string (dst, meta->name,
2218           meta->nm_value.nm_string);
2219     else if (meta->type == NM_TYPE_SIGNED_INT)
2220       plugin_notification_meta_add_signed_int (dst, meta->name,
2221           meta->nm_value.nm_signed_int);
2222     else if (meta->type == NM_TYPE_UNSIGNED_INT)
2223       plugin_notification_meta_add_unsigned_int (dst, meta->name,
2224           meta->nm_value.nm_unsigned_int);
2225     else if (meta->type == NM_TYPE_DOUBLE)
2226       plugin_notification_meta_add_double (dst, meta->name,
2227           meta->nm_value.nm_double);
2228     else if (meta->type == NM_TYPE_BOOLEAN)
2229       plugin_notification_meta_add_boolean (dst, meta->name,
2230           meta->nm_value.nm_boolean);
2231   }
2232
2233   return (0);
2234 } /* int plugin_notification_meta_copy */
2235
2236 int plugin_notification_meta_free (notification_meta_t *n)
2237 {
2238   notification_meta_t *this;
2239   notification_meta_t *next;
2240
2241   if (n == NULL)
2242   {
2243     ERROR ("plugin_notification_meta_free: n == NULL!");
2244     return (-1);
2245   }
2246
2247   this = n;
2248   while (this != NULL)
2249   {
2250     next = this->next;
2251
2252     if (this->type == NM_TYPE_STRING)
2253     {
2254       free ((char *)this->nm_value.nm_string);
2255       this->nm_value.nm_string = NULL;
2256     }
2257     sfree (this);
2258
2259     this = next;
2260   }
2261
2262   return (0);
2263 } /* int plugin_notification_meta_free */
2264
2265 static void plugin_ctx_destructor (void *ctx)
2266 {
2267         sfree (ctx);
2268 } /* void plugin_ctx_destructor */
2269
2270 static plugin_ctx_t ctx_init = { /* interval = */ 0 };
2271
2272 static plugin_ctx_t *plugin_ctx_create (void)
2273 {
2274         plugin_ctx_t *ctx;
2275
2276         ctx = malloc (sizeof (*ctx));
2277         if (ctx == NULL) {
2278                 char errbuf[1024];
2279                 ERROR ("Failed to allocate plugin context: %s",
2280                                 sstrerror (errno, errbuf, sizeof (errbuf)));
2281                 return NULL;
2282         }
2283
2284         *ctx = ctx_init;
2285         assert (plugin_ctx_key_initialized);
2286         pthread_setspecific (plugin_ctx_key, ctx);
2287         DEBUG("Created new plugin context.");
2288         return (ctx);
2289 } /* int plugin_ctx_create */
2290
2291 void plugin_init_ctx (void)
2292 {
2293         pthread_key_create (&plugin_ctx_key, plugin_ctx_destructor);
2294         plugin_ctx_key_initialized = 1;
2295 } /* void plugin_init_ctx */
2296
2297 plugin_ctx_t plugin_get_ctx (void)
2298 {
2299         plugin_ctx_t *ctx;
2300
2301         assert (plugin_ctx_key_initialized);
2302         ctx = pthread_getspecific (plugin_ctx_key);
2303
2304         if (ctx == NULL) {
2305                 ctx = plugin_ctx_create ();
2306                 /* this must no happen -- exit() instead? */
2307                 if (ctx == NULL)
2308                         return ctx_init;
2309         }
2310
2311         return (*ctx);
2312 } /* plugin_ctx_t plugin_get_ctx */
2313
2314 plugin_ctx_t plugin_set_ctx (plugin_ctx_t ctx)
2315 {
2316         plugin_ctx_t *c;
2317         plugin_ctx_t old;
2318
2319         assert (plugin_ctx_key_initialized);
2320         c = pthread_getspecific (plugin_ctx_key);
2321
2322         if (c == NULL) {
2323                 c = plugin_ctx_create ();
2324                 /* this must no happen -- exit() instead? */
2325                 if (c == NULL)
2326                         return ctx_init;
2327         }
2328
2329         old = *c;
2330         *c = ctx;
2331
2332         return (old);
2333 } /* void plugin_set_ctx */
2334
2335 cdtime_t plugin_get_interval (void)
2336 {
2337         cdtime_t interval;
2338
2339         interval = plugin_get_ctx().interval;
2340         if (interval > 0)
2341                 return interval;
2342
2343         return cf_get_default_interval ();
2344 } /* cdtime_t plugin_get_interval */
2345
2346 typedef struct {
2347         plugin_ctx_t ctx;
2348         void *(*start_routine) (void *);
2349         void *arg;
2350 } plugin_thread_t;
2351
2352 static void *plugin_thread_start (void *arg)
2353 {
2354         plugin_thread_t *plugin_thread = arg;
2355
2356         void *(*start_routine) (void *) = plugin_thread->start_routine;
2357         void *plugin_arg = plugin_thread->arg;
2358
2359         plugin_set_ctx (plugin_thread->ctx);
2360
2361         free (plugin_thread);
2362
2363         return start_routine (plugin_arg);
2364 } /* void *plugin_thread_start */
2365
2366 int plugin_thread_create (pthread_t *thread, const pthread_attr_t *attr,
2367                 void *(*start_routine) (void *), void *arg)
2368 {
2369         plugin_thread_t *plugin_thread;
2370
2371         plugin_thread = malloc (sizeof (*plugin_thread));
2372         if (plugin_thread == NULL)
2373                 return -1;
2374
2375         plugin_thread->ctx           = plugin_get_ctx ();
2376         plugin_thread->start_routine = start_routine;
2377         plugin_thread->arg           = arg;
2378
2379         return pthread_create (thread, attr,
2380                         plugin_thread_start, plugin_thread);
2381 } /* int plugin_thread_create */
2382
2383 /* vim: set sw=8 ts=8 noet fdm=marker : */