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