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