Code

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