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