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