1 /**
2 * collectd - src/perl.c
3 * Copyright (C) 2007, 2008 Sebastian Harl
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 * Author:
19 * Sebastian Harl <sh at tokkee.org>
20 **/
22 /*
23 * This plugin embeds a Perl interpreter into collectd and provides an
24 * interface for collectd plugins written in perl.
25 */
27 /* do not automatically get the thread specific perl interpreter */
28 #define PERL_NO_GET_CONTEXT
30 #define DONT_POISON_SPRINTF_YET 1
31 #include "collectd.h"
32 #undef DONT_POISON_SPRINTF_YET
34 #include "configfile.h"
36 #include <EXTERN.h>
37 #include <perl.h>
39 #if __GNUC__
40 # pragma GCC poison sprintf
41 #endif
43 #include <XSUB.h>
45 /* Some versions of Perl define their own version of DEBUG... :-/ */
46 #ifdef DEBUG
47 # undef DEBUG
48 #endif /* DEBUG */
50 /* ... while we want the definition found in plugin.h. */
51 #include "plugin.h"
52 #include "common.h"
54 #include <pthread.h>
56 #if !defined(USE_ITHREADS)
57 # error "Perl does not support ithreads!"
58 #endif /* !defined(USE_ITHREADS) */
60 /* clear the Perl sub's stack frame
61 * (this should only be used inside an XSUB) */
62 #define CLEAR_STACK_FRAME PL_stack_sp = PL_stack_base + *PL_markstack_ptr
64 #define PLUGIN_INIT 0
65 #define PLUGIN_READ 1
66 #define PLUGIN_WRITE 2
67 #define PLUGIN_SHUTDOWN 3
68 #define PLUGIN_LOG 4
69 #define PLUGIN_NOTIF 5
71 #define PLUGIN_TYPES 6
73 #define PLUGIN_DATASET 255
75 #define log_debug(...) DEBUG ("perl: " __VA_ARGS__)
76 #define log_info(...) INFO ("perl: " __VA_ARGS__)
77 #define log_warn(...) WARNING ("perl: " __VA_ARGS__)
78 #define log_err(...) ERROR ("perl: " __VA_ARGS__)
80 /* this is defined in DynaLoader.a */
81 void boot_DynaLoader (PerlInterpreter *, CV *);
83 static XS (Collectd_plugin_register_ds);
84 static XS (Collectd_plugin_unregister_ds);
85 static XS (Collectd_plugin_dispatch_values);
86 static XS (Collectd_plugin_dispatch_notification);
87 static XS (Collectd_plugin_log);
88 static XS (Collectd_call_by_name);
90 /*
91 * private data types
92 */
94 typedef struct c_ithread_s {
95 /* the thread's Perl interpreter */
96 PerlInterpreter *interp;
98 /* double linked list of threads */
99 struct c_ithread_s *prev;
100 struct c_ithread_s *next;
101 } c_ithread_t;
103 typedef struct {
104 c_ithread_t *head;
105 c_ithread_t *tail;
107 #if COLLECT_DEBUG
108 /* some usage stats */
109 int number_of_threads;
110 #endif /* COLLECT_DEBUG */
112 pthread_mutex_t mutex;
113 } c_ithread_list_t;
115 /*
116 * private variables
117 */
119 /* if perl_threads != NULL perl_threads->head must
120 * point to the "base" thread */
121 static c_ithread_list_t *perl_threads = NULL;
123 /* the key used to store each pthread's ithread */
124 static pthread_key_t perl_thr_key;
126 static int perl_argc = 0;
127 static char **perl_argv = NULL;
129 static char base_name[DATA_MAX_NAME_LEN] = "";
131 static struct {
132 char name[64];
133 XS ((*f));
134 } api[] =
135 {
136 { "Collectd::plugin_register_data_set", Collectd_plugin_register_ds },
137 { "Collectd::plugin_unregister_data_set", Collectd_plugin_unregister_ds },
138 { "Collectd::plugin_dispatch_values", Collectd_plugin_dispatch_values },
139 { "Collectd::plugin_dispatch_notification",
140 Collectd_plugin_dispatch_notification },
141 { "Collectd::plugin_log", Collectd_plugin_log },
142 { "Collectd::call_by_name", Collectd_call_by_name },
143 { "", NULL }
144 };
146 struct {
147 char name[64];
148 int value;
149 } constants[] =
150 {
151 { "Collectd::TYPE_INIT", PLUGIN_INIT },
152 { "Collectd::TYPE_READ", PLUGIN_READ },
153 { "Collectd::TYPE_WRITE", PLUGIN_WRITE },
154 { "Collectd::TYPE_SHUTDOWN", PLUGIN_SHUTDOWN },
155 { "Collectd::TYPE_LOG", PLUGIN_LOG },
156 { "Collectd::TYPE_NOTIF", PLUGIN_NOTIF },
157 { "Collectd::TYPE_DATASET", PLUGIN_DATASET },
158 { "Collectd::DS_TYPE_COUNTER", DS_TYPE_COUNTER },
159 { "Collectd::DS_TYPE_GAUGE", DS_TYPE_GAUGE },
160 { "Collectd::LOG_ERR", LOG_ERR },
161 { "Collectd::LOG_WARNING", LOG_WARNING },
162 { "Collectd::LOG_NOTICE", LOG_NOTICE },
163 { "Collectd::LOG_INFO", LOG_INFO },
164 { "Collectd::LOG_DEBUG", LOG_DEBUG },
165 { "Collectd::NOTIF_FAILURE", NOTIF_FAILURE },
166 { "Collectd::NOTIF_WARNING", NOTIF_WARNING },
167 { "Collectd::NOTIF_OKAY", NOTIF_OKAY },
168 { "", 0 }
169 };
171 struct {
172 char name[64];
173 char *var;
174 } g_strings[] =
175 {
176 { "Collectd::hostname_g", hostname_g },
177 { "", NULL }
178 };
180 struct {
181 char name[64];
182 int *var;
183 } g_integers[] =
184 {
185 { "Collectd::interval_g", &interval_g },
186 { "", NULL }
187 };
189 /*
190 * Helper functions for data type conversion.
191 */
193 /*
194 * data source:
195 * [
196 * {
197 * name => $ds_name,
198 * type => $ds_type,
199 * min => $ds_min,
200 * max => $ds_max
201 * },
202 * ...
203 * ]
204 */
205 static int hv2data_source (pTHX_ HV *hash, data_source_t *ds)
206 {
207 SV **tmp = NULL;
209 if ((NULL == hash) || (NULL == ds))
210 return -1;
212 if (NULL != (tmp = hv_fetch (hash, "name", 4, 0))) {
213 strncpy (ds->name, SvPV_nolen (*tmp), DATA_MAX_NAME_LEN);
214 ds->name[DATA_MAX_NAME_LEN - 1] = '\0';
215 }
216 else {
217 log_err ("hv2data_source: No DS name given.");
218 return -1;
219 }
221 if (NULL != (tmp = hv_fetch (hash, "type", 4, 0))) {
222 ds->type = SvIV (*tmp);
224 if ((DS_TYPE_COUNTER != ds->type) && (DS_TYPE_GAUGE != ds->type)) {
225 log_err ("hv2data_source: Invalid DS type.");
226 return -1;
227 }
228 }
229 else {
230 ds->type = DS_TYPE_COUNTER;
231 }
233 if (NULL != (tmp = hv_fetch (hash, "min", 3, 0)))
234 ds->min = SvNV (*tmp);
235 else
236 ds->min = NAN;
238 if (NULL != (tmp = hv_fetch (hash, "max", 3, 0)))
239 ds->max = SvNV (*tmp);
240 else
241 ds->max = NAN;
242 return 0;
243 } /* static data_source_t *hv2data_source (HV *) */
245 static int av2value (pTHX_ char *name, AV *array, value_t *value, int len)
246 {
247 const data_set_t *ds;
249 int i = 0;
251 if ((NULL == name) || (NULL == array) || (NULL == value))
252 return -1;
254 if (av_len (array) < len - 1)
255 len = av_len (array) + 1;
257 if (0 >= len)
258 return -1;
260 ds = plugin_get_ds (name);
261 if (NULL == ds) {
262 log_err ("av2value: Unknown dataset \"%s\"", name);
263 return -1;
264 }
266 if (ds->ds_num < len) {
267 log_warn ("av2value: Value length exceeds data set length.");
268 len = ds->ds_num;
269 }
271 for (i = 0; i < len; ++i) {
272 SV **tmp = av_fetch (array, i, 0);
274 if (NULL != tmp) {
275 if (DS_TYPE_COUNTER == ds->ds[i].type)
276 value[i].counter = SvIV (*tmp);
277 else
278 value[i].gauge = SvNV (*tmp);
279 }
280 else {
281 return -1;
282 }
283 }
284 return len;
285 } /* static int av2value (char *, AV *, value_t *, int) */
287 static int data_set2av (pTHX_ data_set_t *ds, AV *array)
288 {
289 int i = 0;
291 if ((NULL == ds) || (NULL == array))
292 return -1;
294 av_extend (array, ds->ds_num);
296 for (i = 0; i < ds->ds_num; ++i) {
297 HV *source = newHV ();
299 if (NULL == hv_store (source, "name", 4,
300 newSVpv (ds->ds[i].name, 0), 0))
301 return -1;
303 if (NULL == hv_store (source, "type", 4, newSViv (ds->ds[i].type), 0))
304 return -1;
306 if (! isnan (ds->ds[i].min))
307 if (NULL == hv_store (source, "min", 3,
308 newSVnv (ds->ds[i].min), 0))
309 return -1;
311 if (! isnan (ds->ds[i].max))
312 if (NULL == hv_store (source, "max", 3,
313 newSVnv (ds->ds[i].max), 0))
314 return -1;
316 if (NULL == av_store (array, i, newRV_noinc ((SV *)source)))
317 return -1;
318 }
319 return 0;
320 } /* static int data_set2av (data_set_t *, AV *) */
322 static int value_list2hv (pTHX_ value_list_t *vl, data_set_t *ds, HV *hash)
323 {
324 AV *values = NULL;
326 int i = 0;
327 int len = 0;
329 if ((NULL == vl) || (NULL == ds) || (NULL == hash))
330 return -1;
332 len = vl->values_len;
334 if (ds->ds_num < len) {
335 log_warn ("value2av: Value length exceeds data set length.");
336 len = ds->ds_num;
337 }
339 values = newAV ();
340 av_extend (values, len - 1);
342 for (i = 0; i < len; ++i) {
343 SV *val = NULL;
345 if (DS_TYPE_COUNTER == ds->ds[i].type)
346 val = newSViv (vl->values[i].counter);
347 else
348 val = newSVnv (vl->values[i].gauge);
350 if (NULL == av_store (values, i, val)) {
351 av_undef (values);
352 return -1;
353 }
354 }
356 if (NULL == hv_store (hash, "values", 6, newRV_noinc ((SV *)values), 0))
357 return -1;
359 if (0 != vl->time)
360 if (NULL == hv_store (hash, "time", 4, newSViv (vl->time), 0))
361 return -1;
363 if ('\0' != vl->host[0])
364 if (NULL == hv_store (hash, "host", 4, newSVpv (vl->host, 0), 0))
365 return -1;
367 if ('\0' != vl->plugin[0])
368 if (NULL == hv_store (hash, "plugin", 6, newSVpv (vl->plugin, 0), 0))
369 return -1;
371 if ('\0' != vl->plugin_instance[0])
372 if (NULL == hv_store (hash, "plugin_instance", 15,
373 newSVpv (vl->plugin_instance, 0), 0))
374 return -1;
376 if ('\0' != vl->type_instance[0])
377 if (NULL == hv_store (hash, "type_instance", 13,
378 newSVpv (vl->type_instance, 0), 0))
379 return -1;
380 return 0;
381 } /* static int value2av (value_list_t *, data_set_t *, HV *) */
383 static int notification2hv (pTHX_ notification_t *n, HV *hash)
384 {
385 if (NULL == hv_store (hash, "severity", 8, newSViv (n->severity), 0))
386 return -1;
388 if (0 != n->time)
389 if (NULL == hv_store (hash, "time", 4, newSViv (n->time), 0))
390 return -1;
392 if ('\0' != *n->message)
393 if (NULL == hv_store (hash, "message", 7, newSVpv (n->message, 0), 0))
394 return -1;
396 if ('\0' != *n->host)
397 if (NULL == hv_store (hash, "host", 4, newSVpv (n->host, 0), 0))
398 return -1;
400 if ('\0' != *n->plugin)
401 if (NULL == hv_store (hash, "plugin", 6, newSVpv (n->plugin, 0), 0))
402 return -1;
404 if ('\0' != *n->plugin_instance)
405 if (NULL == hv_store (hash, "plugin_instance", 15,
406 newSVpv (n->plugin_instance, 0), 0))
407 return -1;
409 if ('\0' != *n->type)
410 if (NULL == hv_store (hash, "type", 4, newSVpv (n->type, 0), 0))
411 return -1;
413 if ('\0' != *n->type_instance)
414 if (NULL == hv_store (hash, "type_instance", 13,
415 newSVpv (n->type_instance, 0), 0))
416 return -1;
417 return 0;
418 } /* static int notification2hv (notification_t *, HV *) */
420 /*
421 * Internal functions.
422 */
424 static char *get_module_name (char *buf, size_t buf_len, const char *module) {
425 int status = 0;
426 if (base_name[0] == '\0')
427 status = snprintf (buf, buf_len, "%s", module);
428 else
429 status = snprintf (buf, buf_len, "%s::%s", base_name, module);
430 if ((status < 0) || ((unsigned int)status >= buf_len))
431 return (NULL);
432 buf[buf_len - 1] = '\0';
433 return (buf);
434 } /* char *get_module_name */
436 /*
437 * Add a plugin's data set definition.
438 */
439 static int pplugin_register_data_set (pTHX_ char *name, AV *dataset)
440 {
441 int len = -1;
442 int ret = 0;
443 int i = 0;
445 data_source_t *ds = NULL;
446 data_set_t *set = NULL;
448 if ((NULL == name) || (NULL == dataset))
449 return -1;
451 len = av_len (dataset);
453 if (-1 == len)
454 return -1;
456 ds = (data_source_t *)smalloc ((len + 1) * sizeof (data_source_t));
457 set = (data_set_t *)smalloc (sizeof (data_set_t));
459 for (i = 0; i <= len; ++i) {
460 SV **elem = av_fetch (dataset, i, 0);
462 if (NULL == elem)
463 return -1;
465 if (! (SvROK (*elem) && (SVt_PVHV == SvTYPE (SvRV (*elem))))) {
466 log_err ("pplugin_register_data_set: Invalid data source.");
467 return -1;
468 }
470 if (-1 == hv2data_source (aTHX_ (HV *)SvRV (*elem), &ds[i]))
471 return -1;
473 log_debug ("pplugin_register_data_set: "
474 "DS.name = \"%s\", DS.type = %i, DS.min = %f, DS.max = %f",
475 ds[i].name, ds[i].type, ds[i].min, ds[i].max);
476 }
478 strncpy (set->type, name, DATA_MAX_NAME_LEN);
479 set->type[DATA_MAX_NAME_LEN - 1] = '\0';
481 set->ds_num = len + 1;
482 set->ds = ds;
484 ret = plugin_register_data_set (set);
486 free (ds);
487 free (set);
488 return ret;
489 } /* static int pplugin_register_data_set (char *, SV *) */
491 /*
492 * Remove a plugin's data set definition.
493 */
494 static int pplugin_unregister_data_set (char *name)
495 {
496 if (NULL == name)
497 return 0;
498 return plugin_unregister_data_set (name);
499 } /* static int pplugin_unregister_data_set (char *) */
501 /*
502 * Submit the values to the write functions.
503 *
504 * value list:
505 * {
506 * values => [ @values ],
507 * time => $time,
508 * host => $host,
509 * plugin => $plugin,
510 * plugin_instance => $pinstance,
511 * type_instance => $tinstance,
512 * }
513 */
514 static int pplugin_dispatch_values (pTHX_ char *name, HV *values)
515 {
516 value_list_t list = VALUE_LIST_INIT;
517 value_t *val = NULL;
519 SV **tmp = NULL;
521 int ret = 0;
523 if ((NULL == name) || (NULL == values))
524 return -1;
526 if ((NULL == (tmp = hv_fetch (values, "values", 6, 0)))
527 || (! (SvROK (*tmp) && (SVt_PVAV == SvTYPE (SvRV (*tmp)))))) {
528 log_err ("pplugin_dispatch_values: No valid values given.");
529 return -1;
530 }
532 {
533 AV *array = (AV *)SvRV (*tmp);
534 int len = av_len (array) + 1;
536 if (len <= 0)
537 return -1;
539 val = (value_t *)smalloc (len * sizeof (value_t));
541 list.values_len = av2value (aTHX_ name, (AV *)SvRV (*tmp), val, len);
542 list.values = val;
544 if (-1 == list.values_len) {
545 sfree (val);
546 return -1;
547 }
548 }
550 if (NULL != (tmp = hv_fetch (values, "time", 4, 0))) {
551 list.time = (time_t)SvIV (*tmp);
552 }
553 else {
554 list.time = time (NULL);
555 }
557 if (NULL != (tmp = hv_fetch (values, "host", 4, 0))) {
558 strncpy (list.host, SvPV_nolen (*tmp), DATA_MAX_NAME_LEN);
559 list.host[DATA_MAX_NAME_LEN - 1] = '\0';
560 }
561 else {
562 sstrncpy (list.host, hostname_g, sizeof (list.host));
563 }
565 if (NULL != (tmp = hv_fetch (values, "plugin", 6, 0))) {
566 strncpy (list.plugin, SvPV_nolen (*tmp), DATA_MAX_NAME_LEN);
567 list.plugin[DATA_MAX_NAME_LEN - 1] = '\0';
568 }
570 if (NULL != (tmp = hv_fetch (values, "plugin_instance", 15, 0))) {
571 strncpy (list.plugin_instance, SvPV_nolen (*tmp), DATA_MAX_NAME_LEN);
572 list.plugin_instance[DATA_MAX_NAME_LEN - 1] = '\0';
573 }
575 if (NULL != (tmp = hv_fetch (values, "type_instance", 13, 0))) {
576 strncpy (list.type_instance, SvPV_nolen (*tmp), DATA_MAX_NAME_LEN);
577 list.type_instance[DATA_MAX_NAME_LEN - 1] = '\0';
578 }
580 ret = plugin_dispatch_values (name, &list);
582 sfree (val);
583 return ret;
584 } /* static int pplugin_dispatch_values (char *, HV *) */
586 /*
587 * Dispatch a notification.
588 *
589 * notification:
590 * {
591 * severity => $severity,
592 * time => $time,
593 * message => $msg,
594 * host => $host,
595 * plugin => $plugin,
596 * type => $type,
597 * plugin_instance => $instance,
598 * type_instance => $type_instance
599 * }
600 */
601 static int pplugin_dispatch_notification (pTHX_ HV *notif)
602 {
603 notification_t n;
605 SV **tmp = NULL;
607 if (NULL == notif)
608 return -1;
610 memset (&n, 0, sizeof (n));
612 if (NULL != (tmp = hv_fetch (notif, "severity", 8, 0)))
613 n.severity = SvIV (*tmp);
614 else
615 n.severity = NOTIF_FAILURE;
617 if (NULL != (tmp = hv_fetch (notif, "time", 4, 0)))
618 n.time = (time_t)SvIV (*tmp);
619 else
620 n.time = time (NULL);
622 if (NULL != (tmp = hv_fetch (notif, "message", 7, 0)))
623 strncpy (n.message, SvPV_nolen (*tmp), sizeof (n.message));
624 n.message[sizeof (n.message) - 1] = '\0';
626 if (NULL != (tmp = hv_fetch (notif, "host", 4, 0)))
627 strncpy (n.host, SvPV_nolen (*tmp), sizeof (n.host));
628 else
629 strncpy (n.host, hostname_g, sizeof (n.host));
630 n.host[sizeof (n.host) - 1] = '\0';
632 if (NULL != (tmp = hv_fetch (notif, "plugin", 6, 0)))
633 strncpy (n.plugin, SvPV_nolen (*tmp), sizeof (n.plugin));
634 n.plugin[sizeof (n.plugin) - 1] = '\0';
636 if (NULL != (tmp = hv_fetch (notif, "plugin_instance", 15, 0)))
637 strncpy (n.plugin_instance, SvPV_nolen (*tmp),
638 sizeof (n.plugin_instance));
639 n.plugin_instance[sizeof (n.plugin_instance) - 1] = '\0';
641 if (NULL != (tmp = hv_fetch (notif, "type", 4, 0)))
642 strncpy (n.type, SvPV_nolen (*tmp), sizeof (n.type));
643 n.type[sizeof (n.type) - 1] = '\0';
645 if (NULL != (tmp = hv_fetch (notif, "type_instance", 13, 0)))
646 strncpy (n.type_instance, SvPV_nolen (*tmp), sizeof (n.type_instance));
647 n.type_instance[sizeof (n.type_instance) - 1] = '\0';
648 return plugin_dispatch_notification (&n);
649 } /* static int pplugin_dispatch_notification (HV *) */
651 /*
652 * Call all working functions of the given type.
653 */
654 static int pplugin_call_all (pTHX_ int type, ...)
655 {
656 int retvals = 0;
658 va_list ap;
659 int ret = 0;
661 dSP;
663 if ((type < 0) || (type >= PLUGIN_TYPES))
664 return -1;
666 va_start (ap, type);
668 ENTER;
669 SAVETMPS;
671 PUSHMARK (SP);
673 XPUSHs (sv_2mortal (newSViv ((IV)type)));
675 if (PLUGIN_WRITE == type) {
676 /*
677 * $_[0] = $plugin_type;
678 *
679 * $_[1] =
680 * [
681 * {
682 * name => $ds_name,
683 * type => $ds_type,
684 * min => $ds_min,
685 * max => $ds_max
686 * },
687 * ...
688 * ];
689 *
690 * $_[2] =
691 * {
692 * values => [ $v1, ... ],
693 * time => $time,
694 * host => $hostname,
695 * plugin => $plugin,
696 * plugin_instance => $instance,
697 * type_instance => $type_instance
698 * };
699 */
700 data_set_t *ds;
701 value_list_t *vl;
703 AV *pds = newAV ();
704 HV *pvl = newHV ();
706 ds = va_arg (ap, data_set_t *);
707 vl = va_arg (ap, value_list_t *);
709 if (-1 == data_set2av (aTHX_ ds, pds)) {
710 av_clear (pds);
711 av_undef (pds);
712 pds = Nullav;
713 ret = -1;
714 }
716 if (-1 == value_list2hv (aTHX_ vl, ds, pvl)) {
717 hv_clear (pvl);
718 hv_undef (pvl);
719 pvl = Nullhv;
720 ret = -1;
721 }
723 XPUSHs (sv_2mortal (newSVpv (ds->type, 0)));
724 XPUSHs (sv_2mortal (newRV_noinc ((SV *)pds)));
725 XPUSHs (sv_2mortal (newRV_noinc ((SV *)pvl)));
726 }
727 else if (PLUGIN_LOG == type) {
728 /*
729 * $_[0] = $level;
730 *
731 * $_[1] = $message;
732 */
733 XPUSHs (sv_2mortal (newSViv (va_arg (ap, int))));
734 XPUSHs (sv_2mortal (newSVpv (va_arg (ap, char *), 0)));
735 }
736 else if (PLUGIN_NOTIF == type) {
737 /*
738 * $_[0] =
739 * {
740 * severity => $severity,
741 * time => $time,
742 * message => $msg,
743 * host => $host,
744 * plugin => $plugin,
745 * type => $type,
746 * plugin_instance => $instance,
747 * type_instance => $type_instance
748 * };
749 */
750 notification_t *n;
751 HV *notif = newHV ();
753 n = va_arg (ap, notification_t *);
755 if (-1 == notification2hv (aTHX_ n, notif)) {
756 hv_clear (notif);
757 hv_undef (notif);
758 notif = Nullhv;
759 ret = -1;
760 }
762 XPUSHs (sv_2mortal (newRV_noinc ((SV *)notif)));
763 }
765 PUTBACK;
767 retvals = call_pv ("Collectd::plugin_call_all", G_SCALAR);
769 SPAGAIN;
770 if (0 < retvals) {
771 SV *tmp = POPs;
772 if (! SvTRUE (tmp))
773 ret = -1;
774 }
776 PUTBACK;
777 FREETMPS;
778 LEAVE;
780 va_end (ap);
781 return ret;
782 } /* static int pplugin_call_all (int, ...) */
784 /*
785 * Exported Perl API.
786 */
788 /*
789 * Collectd::plugin_register_data_set (type, dataset).
790 *
791 * type:
792 * type of the dataset
793 *
794 * dataset:
795 * dataset to be registered
796 */
797 static XS (Collectd_plugin_register_ds)
798 {
799 SV *data = NULL;
800 int ret = 0;
802 dXSARGS;
804 if (2 != items) {
805 log_err ("Usage: Collectd::plugin_register_data_set(type, dataset)");
806 XSRETURN_EMPTY;
807 }
809 log_debug ("Collectd::plugin_register_data_set: "
810 "type = \"%s\", dataset = \"%s\"",
811 SvPV_nolen (ST (0)), SvPV_nolen (ST (1)));
813 data = ST (1);
815 if (SvROK (data) && (SVt_PVAV == SvTYPE (SvRV (data)))) {
816 ret = pplugin_register_data_set (aTHX_ SvPV_nolen (ST (0)),
817 (AV *)SvRV (data));
818 }
819 else {
820 log_err ("Collectd::plugin_register_data_set: Invalid data.");
821 XSRETURN_EMPTY;
822 }
824 if (0 == ret)
825 XSRETURN_YES;
826 else
827 XSRETURN_EMPTY;
828 } /* static XS (Collectd_plugin_register_ds) */
830 /*
831 * Collectd::plugin_unregister_data_set (type).
832 *
833 * type:
834 * type of the dataset
835 */
836 static XS (Collectd_plugin_unregister_ds)
837 {
838 dXSARGS;
840 if (1 != items) {
841 log_err ("Usage: Collectd::plugin_unregister_data_set(type)");
842 XSRETURN_EMPTY;
843 }
845 log_debug ("Collectd::plugin_unregister_data_set: type = \"%s\"",
846 SvPV_nolen (ST (0)));
848 if (0 == pplugin_unregister_data_set (SvPV_nolen (ST (0))))
849 XSRETURN_YES;
850 else
851 XSRETURN_EMPTY;
852 } /* static XS (Collectd_plugin_register_ds) */
854 /*
855 * Collectd::plugin_dispatch_values (name, values).
856 *
857 * name:
858 * name of the plugin
859 *
860 * values:
861 * value list to submit
862 */
863 static XS (Collectd_plugin_dispatch_values)
864 {
865 SV *values = NULL;
867 int ret = 0;
869 dXSARGS;
871 if (2 != items) {
872 log_err ("Usage: Collectd::plugin_dispatch_values(name, values)");
873 XSRETURN_EMPTY;
874 }
876 log_debug ("Collectd::plugin_dispatch_values: "
877 "name = \"%s\", values=\"%s\"",
878 SvPV_nolen (ST (0)), SvPV_nolen (ST (1)));
880 values = ST (1);
882 if (! (SvROK (values) && (SVt_PVHV == SvTYPE (SvRV (values))))) {
883 log_err ("Collectd::plugin_dispatch_values: Invalid values.");
884 XSRETURN_EMPTY;
885 }
887 if ((NULL == ST (0)) || (NULL == values))
888 XSRETURN_EMPTY;
890 ret = pplugin_dispatch_values (aTHX_ SvPV_nolen (ST (0)),
891 (HV *)SvRV (values));
893 if (0 == ret)
894 XSRETURN_YES;
895 else
896 XSRETURN_EMPTY;
897 } /* static XS (Collectd_plugin_dispatch_values) */
899 /*
900 * Collectd::plugin_dispatch_notification (notif).
901 *
902 * notif:
903 * notification to dispatch
904 */
905 static XS (Collectd_plugin_dispatch_notification)
906 {
907 SV *notif = NULL;
909 int ret = 0;
911 dXSARGS;
913 if (1 != items) {
914 log_err ("Usage: Collectd::plugin_dispatch_notification(notif)");
915 XSRETURN_EMPTY;
916 }
918 log_debug ("Collectd::plugin_dispatch_notification: notif = \"%s\"",
919 SvPV_nolen (ST (0)));
921 notif = ST (0);
923 if (! (SvROK (notif) && (SVt_PVHV == SvTYPE (SvRV (notif))))) {
924 log_err ("Collectd::plugin_dispatch_notification: Invalid notif.");
925 XSRETURN_EMPTY;
926 }
928 ret = pplugin_dispatch_notification (aTHX_ (HV *)SvRV (notif));
930 if (0 == ret)
931 XSRETURN_YES;
932 else
933 XSRETURN_EMPTY;
934 } /* static XS (Collectd_plugin_dispatch_notification) */
936 /*
937 * Collectd::plugin_log (level, message).
938 *
939 * level:
940 * log level (LOG_DEBUG, ... LOG_ERR)
941 *
942 * message:
943 * log message
944 */
945 static XS (Collectd_plugin_log)
946 {
947 dXSARGS;
949 if (2 != items) {
950 log_err ("Usage: Collectd::plugin_log(level, message)");
951 XSRETURN_EMPTY;
952 }
954 plugin_log (SvIV (ST (0)), SvPV_nolen (ST (1)));
955 XSRETURN_YES;
956 } /* static XS (Collectd_plugin_log) */
958 /*
959 * Collectd::call_by_name (...).
960 *
961 * Call a Perl sub identified by its name passed through $Collectd::cb_name.
962 */
963 static XS (Collectd_call_by_name)
964 {
965 SV *tmp = NULL;
966 char *name = NULL;
968 if (NULL == (tmp = get_sv ("Collectd::cb_name", 0))) {
969 sv_setpv (get_sv ("@", 1), "cb_name has not been set");
970 CLEAR_STACK_FRAME;
971 return;
972 }
974 name = SvPV_nolen (tmp);
976 if (NULL == get_cv (name, 0)) {
977 sv_setpvf (get_sv ("@", 1), "unknown callback \"%s\"", name);
978 CLEAR_STACK_FRAME;
979 return;
980 }
982 /* simply pass on the subroutine call without touching the stack,
983 * thus leaving any arguments and return values in place */
984 call_pv (name, 0);
985 } /* static XS (Collectd_call_by_name) */
987 /*
988 * collectd's perl interpreter based thread implementation.
989 *
990 * This has been inspired by Perl's ithreads introduced in version 5.6.0.
991 */
993 /* must be called with perl_threads->mutex locked */
994 static void c_ithread_destroy (c_ithread_t *ithread)
995 {
996 dTHXa (ithread->interp);
998 assert (NULL != perl_threads);
1000 PERL_SET_CONTEXT (aTHX);
1001 log_debug ("Shutting down Perl interpreter %p...", aTHX);
1003 #if COLLECT_DEBUG
1004 sv_report_used ();
1006 --perl_threads->number_of_threads;
1007 #endif /* COLLECT_DEBUG */
1009 perl_destruct (aTHX);
1010 perl_free (aTHX);
1012 if (NULL == ithread->prev)
1013 perl_threads->head = ithread->next;
1014 else
1015 ithread->prev->next = ithread->next;
1017 if (NULL == ithread->next)
1018 perl_threads->tail = ithread->prev;
1019 else
1020 ithread->next->prev = ithread->prev;
1022 sfree (ithread);
1023 return;
1024 } /* static void c_ithread_destroy (c_ithread_t *) */
1026 static void c_ithread_destructor (void *arg)
1027 {
1028 c_ithread_t *ithread = (c_ithread_t *)arg;
1029 c_ithread_t *t = NULL;
1031 if (NULL == perl_threads)
1032 return;
1034 pthread_mutex_lock (&perl_threads->mutex);
1036 for (t = perl_threads->head; NULL != t; t = t->next)
1037 if (t == ithread)
1038 break;
1040 /* the ithread no longer exists */
1041 if (NULL == t)
1042 return;
1044 c_ithread_destroy (ithread);
1046 pthread_mutex_unlock (&perl_threads->mutex);
1047 return;
1048 } /* static void c_ithread_destructor (void *) */
1050 /* must be called with perl_threads->mutex locked */
1051 static c_ithread_t *c_ithread_create (PerlInterpreter *base)
1052 {
1053 c_ithread_t *t = NULL;
1054 dTHXa (NULL);
1056 assert (NULL != perl_threads);
1058 t = (c_ithread_t *)smalloc (sizeof (c_ithread_t));
1059 memset (t, 0, sizeof (c_ithread_t));
1061 t->interp = (NULL == base)
1062 ? NULL
1063 : perl_clone (base, CLONEf_KEEP_PTR_TABLE);
1065 aTHX = t->interp;
1067 if (NULL != base) {
1068 av_clear (PL_endav);
1069 av_undef (PL_endav);
1070 PL_endav = Nullav;
1071 }
1073 #if COLLECT_DEBUG
1074 ++perl_threads->number_of_threads;
1075 #endif /* COLLECT_DEBUG */
1077 t->next = NULL;
1079 if (NULL == perl_threads->tail) {
1080 perl_threads->head = t;
1081 t->prev = NULL;
1082 }
1083 else {
1084 perl_threads->tail->next = t;
1085 t->prev = perl_threads->tail;
1086 }
1088 perl_threads->tail = t;
1090 pthread_setspecific (perl_thr_key, (const void *)t);
1091 return t;
1092 } /* static c_ithread_t *c_ithread_create (PerlInterpreter *) */
1094 /*
1095 * Interface to collectd.
1096 */
1098 static int perl_init (void)
1099 {
1100 dTHX;
1102 if (NULL == perl_threads)
1103 return 0;
1105 if (NULL == aTHX) {
1106 c_ithread_t *t = NULL;
1108 pthread_mutex_lock (&perl_threads->mutex);
1109 t = c_ithread_create (perl_threads->head->interp);
1110 pthread_mutex_unlock (&perl_threads->mutex);
1112 aTHX = t->interp;
1113 }
1115 log_debug ("perl_init: c_ithread: interp = %p (active threads: %i)",
1116 aTHX, perl_threads->number_of_threads);
1117 return pplugin_call_all (aTHX_ PLUGIN_INIT);
1118 } /* static int perl_init (void) */
1120 static int perl_read (void)
1121 {
1122 dTHX;
1124 if (NULL == perl_threads)
1125 return 0;
1127 if (NULL == aTHX) {
1128 c_ithread_t *t = NULL;
1130 pthread_mutex_lock (&perl_threads->mutex);
1131 t = c_ithread_create (perl_threads->head->interp);
1132 pthread_mutex_unlock (&perl_threads->mutex);
1134 aTHX = t->interp;
1135 }
1137 log_debug ("perl_read: c_ithread: interp = %p (active threads: %i)",
1138 aTHX, perl_threads->number_of_threads);
1139 return pplugin_call_all (aTHX_ PLUGIN_READ);
1140 } /* static int perl_read (void) */
1142 static int perl_write (const data_set_t *ds, const value_list_t *vl)
1143 {
1144 dTHX;
1146 if (NULL == perl_threads)
1147 return 0;
1149 if (NULL == aTHX) {
1150 c_ithread_t *t = NULL;
1152 pthread_mutex_lock (&perl_threads->mutex);
1153 t = c_ithread_create (perl_threads->head->interp);
1154 pthread_mutex_unlock (&perl_threads->mutex);
1156 aTHX = t->interp;
1157 }
1159 log_debug ("perl_write: c_ithread: interp = %p (active threads: %i)",
1160 aTHX, perl_threads->number_of_threads);
1161 return pplugin_call_all (aTHX_ PLUGIN_WRITE, ds, vl);
1162 } /* static int perl_write (const data_set_t *, const value_list_t *) */
1164 static void perl_log (int level, const char *msg)
1165 {
1166 dTHX;
1168 if (NULL == perl_threads)
1169 return;
1171 if (NULL == aTHX) {
1172 c_ithread_t *t = NULL;
1174 pthread_mutex_lock (&perl_threads->mutex);
1175 t = c_ithread_create (perl_threads->head->interp);
1176 pthread_mutex_unlock (&perl_threads->mutex);
1178 aTHX = t->interp;
1179 }
1181 pplugin_call_all (aTHX_ PLUGIN_LOG, level, msg);
1182 return;
1183 } /* static void perl_log (int, const char *) */
1185 static int perl_notify (const notification_t *notif)
1186 {
1187 dTHX;
1189 if (NULL == perl_threads)
1190 return 0;
1192 if (NULL == aTHX) {
1193 c_ithread_t *t = NULL;
1195 pthread_mutex_lock (&perl_threads->mutex);
1196 t = c_ithread_create (perl_threads->head->interp);
1197 pthread_mutex_unlock (&perl_threads->mutex);
1199 aTHX = t->interp;
1200 }
1201 return pplugin_call_all (aTHX_ PLUGIN_NOTIF, notif);
1202 } /* static int perl_notify (const notification_t *) */
1204 static int perl_shutdown (void)
1205 {
1206 c_ithread_t *t = NULL;
1208 int ret = 0;
1210 dTHX;
1212 plugin_unregister_complex_config ("perl");
1214 if (NULL == perl_threads)
1215 return 0;
1217 if (NULL == aTHX) {
1218 c_ithread_t *t = NULL;
1220 pthread_mutex_lock (&perl_threads->mutex);
1221 t = c_ithread_create (perl_threads->head->interp);
1222 pthread_mutex_unlock (&perl_threads->mutex);
1224 aTHX = t->interp;
1225 }
1227 log_debug ("perl_shutdown: c_ithread: interp = %p (active threads: %i)",
1228 aTHX, perl_threads->number_of_threads);
1230 plugin_unregister_log ("perl");
1231 plugin_unregister_notification ("perl");
1232 plugin_unregister_init ("perl");
1233 plugin_unregister_read ("perl");
1234 plugin_unregister_write ("perl");
1236 ret = pplugin_call_all (aTHX_ PLUGIN_SHUTDOWN);
1238 pthread_mutex_lock (&perl_threads->mutex);
1239 t = perl_threads->tail;
1241 while (NULL != t) {
1242 c_ithread_t *thr = t;
1244 /* the pointer has to be advanced before destroying
1245 * the thread as this will free the memory */
1246 t = t->prev;
1248 c_ithread_destroy (thr);
1249 }
1251 pthread_mutex_unlock (&perl_threads->mutex);
1252 pthread_mutex_destroy (&perl_threads->mutex);
1254 sfree (perl_threads);
1256 pthread_key_delete (perl_thr_key);
1258 PERL_SYS_TERM ();
1260 plugin_unregister_shutdown ("perl");
1261 return ret;
1262 } /* static void perl_shutdown (void) */
1264 /*
1265 * Access functions for global variables.
1266 *
1267 * These functions implement the "magic" used to access
1268 * the global variables from Perl.
1269 */
1271 static int g_pv_get (pTHX_ SV *var, MAGIC *mg)
1272 {
1273 char *pv = mg->mg_ptr;
1274 sv_setpv (var, pv);
1275 return 0;
1276 } /* static int g_pv_get (pTHX_ SV *, MAGIC *) */
1278 static int g_pv_set (pTHX_ SV *var, MAGIC *mg)
1279 {
1280 char *pv = mg->mg_ptr;
1281 strncpy (pv, SvPV_nolen (var), DATA_MAX_NAME_LEN);
1282 pv[DATA_MAX_NAME_LEN - 1] = '\0';
1283 return 0;
1284 } /* static int g_pv_set (pTHX_ SV *, MAGIC *) */
1286 static int g_iv_get (pTHX_ SV *var, MAGIC *mg)
1287 {
1288 int *iv = (int *)mg->mg_ptr;
1289 sv_setiv (var, *iv);
1290 return 0;
1291 } /* static int g_iv_get (pTHX_ SV *, MAGIC *) */
1293 static int g_iv_set (pTHX_ SV *var, MAGIC *mg)
1294 {
1295 int *iv = (int *)mg->mg_ptr;
1296 *iv = (int)SvIV (var);
1297 return 0;
1298 } /* static int g_iv_set (pTHX_ SV *, MAGIC *) */
1300 static MGVTBL g_pv_vtbl = { g_pv_get, g_pv_set, NULL, NULL, NULL, NULL, NULL };
1301 static MGVTBL g_iv_vtbl = { g_iv_get, g_iv_set, NULL, NULL, NULL, NULL, NULL };
1303 /* bootstrap the Collectd module */
1304 static void xs_init (pTHX)
1305 {
1306 HV *stash = NULL;
1307 SV *tmp = NULL;
1308 char *file = __FILE__;
1310 int i = 0;
1312 dXSUB_SYS;
1314 /* enable usage of Perl modules using shared libraries */
1315 newXS ("DynaLoader::boot_DynaLoader", boot_DynaLoader, file);
1317 /* register API */
1318 for (i = 0; NULL != api[i].f; ++i)
1319 newXS (api[i].name, api[i].f, file);
1321 stash = gv_stashpv ("Collectd", 1);
1323 /* export "constants" */
1324 for (i = 0; '\0' != constants[i].name[0]; ++i)
1325 newCONSTSUB (stash, constants[i].name, newSViv (constants[i].value));
1327 /* export global variables
1328 * by adding "magic" to the SV's representing the globale variables
1329 * perl is able to automagically call the get/set function when
1330 * accessing any such variable (this is basically the same as using
1331 * tie() in Perl) */
1332 /* global strings */
1333 for (i = 0; '\0' != g_strings[i].name[0]; ++i) {
1334 tmp = get_sv (g_strings[i].name, 1);
1335 sv_magicext (tmp, NULL, PERL_MAGIC_ext, &g_pv_vtbl,
1336 g_strings[i].var, 0);
1337 }
1339 /* global integers */
1340 for (i = 0; '\0' != g_integers[i].name[0]; ++i) {
1341 tmp = get_sv (g_integers[i].name, 1);
1342 sv_magicext (tmp, NULL, PERL_MAGIC_ext, &g_iv_vtbl,
1343 (char *)g_integers[i].var, 0);
1344 }
1345 return;
1346 } /* static void xs_init (pTHX) */
1348 /* Initialize the global Perl interpreter. */
1349 static int init_pi (int argc, char **argv)
1350 {
1351 dTHXa (NULL);
1353 if (NULL != perl_threads)
1354 return 0;
1356 log_info ("Initializing Perl interpreter...");
1357 #if COLLECT_DEBUG
1358 {
1359 int i = 0;
1361 for (i = 0; i < argc; ++i)
1362 log_debug ("argv[%i] = \"%s\"", i, argv[i]);
1363 }
1364 #endif /* COLLECT_DEBUG */
1366 if (0 != pthread_key_create (&perl_thr_key, c_ithread_destructor)) {
1367 log_err ("init_pi: pthread_key_create failed");
1369 /* this must not happen - cowardly giving up if it does */
1370 exit (1);
1371 }
1373 #ifdef __FreeBSD__
1374 /* On FreeBSD, PERL_SYS_INIT3 expands to some expression which
1375 * triggers a "value computed is not used" warning by gcc. */
1376 (void)
1377 #endif
1378 PERL_SYS_INIT3 (&argc, &argv, &environ);
1380 perl_threads = (c_ithread_list_t *)smalloc (sizeof (c_ithread_list_t));
1381 memset (perl_threads, 0, sizeof (c_ithread_list_t));
1383 pthread_mutex_init (&perl_threads->mutex, NULL);
1384 /* locking the mutex should not be necessary at this point
1385 * but let's just do it for the sake of completeness */
1386 pthread_mutex_lock (&perl_threads->mutex);
1388 perl_threads->head = c_ithread_create (NULL);
1389 perl_threads->tail = perl_threads->head;
1391 if (NULL == (perl_threads->head->interp = perl_alloc ())) {
1392 log_err ("init_pi: Not enough memory.");
1393 exit (3);
1394 }
1396 aTHX = perl_threads->head->interp;
1397 pthread_mutex_unlock (&perl_threads->mutex);
1399 perl_construct (aTHX);
1401 PL_exit_flags |= PERL_EXIT_DESTRUCT_END;
1403 if (0 != perl_parse (aTHX_ xs_init, argc, argv, NULL)) {
1404 log_err ("init_pi: Unable to bootstrap Collectd.");
1405 exit (1);
1406 }
1408 /* Set $0 to "collectd" because perl_parse() has to set it to "-e". */
1409 sv_setpv (get_sv ("0", 0), "collectd");
1411 perl_run (aTHX);
1413 plugin_register_log ("perl", perl_log);
1414 plugin_register_notification ("perl", perl_notify);
1415 plugin_register_init ("perl", perl_init);
1417 plugin_register_read ("perl", perl_read);
1419 plugin_register_write ("perl", perl_write);
1420 plugin_register_shutdown ("perl", perl_shutdown);
1421 return 0;
1422 } /* static int init_pi (const char **, const int) */
1424 /*
1425 * LoadPlugin "<Plugin>"
1426 */
1427 static int perl_config_loadplugin (pTHX_ oconfig_item_t *ci)
1428 {
1429 char module_name[DATA_MAX_NAME_LEN];
1431 char *value = NULL;
1433 if ((0 != ci->children_num) || (1 != ci->values_num)
1434 || (OCONFIG_TYPE_STRING != ci->values[0].type)) {
1435 log_err ("LoadPlugin expects a single string argument.");
1436 return 1;
1437 }
1439 value = ci->values[0].value.string;
1441 if (NULL == get_module_name (module_name, sizeof (module_name), value)) {
1442 log_err ("Invalid module name %s", value);
1443 return (1);
1444 }
1446 init_pi (perl_argc, perl_argv);
1447 assert (NULL != perl_threads);
1448 assert (NULL != perl_threads->head);
1450 aTHX = perl_threads->head->interp;
1452 log_debug ("perl_config: loading perl plugin \"%s\"", value);
1453 load_module (PERL_LOADMOD_NOIMPORT,
1454 newSVpv (module_name, strlen (module_name)), Nullsv);
1455 return 0;
1456 } /* static int perl_config_loadplugin (oconfig_item_it *) */
1458 /*
1459 * BaseName "<Name>"
1460 */
1461 static int perl_config_basename (pTHX_ oconfig_item_t *ci)
1462 {
1463 char *value = NULL;
1465 if ((0 != ci->children_num) || (1 != ci->values_num)
1466 || (OCONFIG_TYPE_STRING != ci->values[0].type)) {
1467 log_err ("BaseName expects a single string argument.");
1468 return 1;
1469 }
1471 value = ci->values[0].value.string;
1473 log_debug ("perl_config: Setting plugin basename to \"%s\"", value);
1474 strncpy (base_name, value, sizeof (base_name));
1475 base_name[sizeof (base_name) - 1] = '\0';
1476 return 0;
1477 } /* static int perl_config_basename (oconfig_item_it *) */
1479 /*
1480 * EnableDebugger "<Package>"|""
1481 */
1482 static int perl_config_enabledebugger (pTHX_ oconfig_item_t *ci)
1483 {
1484 char *value = NULL;
1486 if ((0 != ci->children_num) || (1 != ci->values_num)
1487 || (OCONFIG_TYPE_STRING != ci->values[0].type)) {
1488 log_err ("EnableDebugger expects a single string argument.");
1489 return 1;
1490 }
1492 if (NULL != perl_threads) {
1493 log_warn ("EnableDebugger has no effects if used after LoadPlugin.");
1494 return 1;
1495 }
1497 value = ci->values[0].value.string;
1499 perl_argv = (char **)realloc (perl_argv,
1500 (++perl_argc + 1) * sizeof (char *));
1502 if (NULL == perl_argv) {
1503 log_err ("perl_config: Not enough memory.");
1504 exit (3);
1505 }
1507 if ('\0' == value[0]) {
1508 perl_argv[perl_argc - 1] = "-d";
1509 }
1510 else {
1511 perl_argv[perl_argc - 1] = (char *)smalloc (strlen (value) + 4);
1512 sstrncpy (perl_argv[perl_argc - 1], "-d:", 4);
1513 sstrncpy (perl_argv[perl_argc - 1] + 3, value, strlen (value) + 1);
1514 }
1516 perl_argv[perl_argc] = NULL;
1517 return 0;
1518 } /* static int perl_config_enabledebugger (oconfig_item_it *) */
1520 /*
1521 * IncludeDir "<Dir>"
1522 */
1523 static int perl_config_includedir (pTHX_ oconfig_item_t *ci)
1524 {
1525 char *value = NULL;
1527 if ((0 != ci->children_num) || (1 != ci->values_num)
1528 || (OCONFIG_TYPE_STRING != ci->values[0].type)) {
1529 log_err ("IncludeDir expects a single string argument.");
1530 return 1;
1531 }
1533 value = ci->values[0].value.string;
1535 if (NULL == aTHX) {
1536 perl_argv = (char **)realloc (perl_argv,
1537 (++perl_argc + 1) * sizeof (char *));
1539 if (NULL == perl_argv) {
1540 log_err ("perl_config: Not enough memory.");
1541 exit (3);
1542 }
1544 perl_argv[perl_argc - 1] = (char *)smalloc (strlen (value) + 3);
1545 sstrncpy(perl_argv[perl_argc - 1], "-I", 3);
1546 sstrncpy(perl_argv[perl_argc - 1] + 2, value, strlen (value) + 1);
1548 perl_argv[perl_argc] = NULL;
1549 }
1550 else {
1551 /* prepend the directory to @INC */
1552 av_unshift (GvAVn (PL_incgv), 1);
1553 av_store (GvAVn (PL_incgv), 0, newSVpv (value, strlen (value)));
1554 }
1555 return 0;
1556 } /* static int perl_config_includedir (oconfig_item_it *) */
1558 static int perl_config (oconfig_item_t *ci)
1559 {
1560 int i = 0;
1562 dTHXa (NULL);
1564 for (i = 0; i < ci->children_num; ++i) {
1565 oconfig_item_t *c = ci->children + i;
1567 if (NULL != perl_threads)
1568 aTHX = PERL_GET_CONTEXT;
1570 if (0 == strcasecmp (c->key, "LoadPlugin"))
1571 perl_config_loadplugin (aTHX_ c);
1572 else if (0 == strcasecmp (c->key, "BaseName"))
1573 perl_config_basename (aTHX_ c);
1574 else if (0 == strcasecmp (c->key, "EnableDebugger"))
1575 perl_config_enabledebugger (aTHX_ c);
1576 else if (0 == strcasecmp (c->key, "IncludeDir"))
1577 perl_config_includedir (aTHX_ c);
1578 else
1579 log_warn ("Ignoring unknown config key \"%s\".", c->key);
1580 }
1581 return 0;
1582 } /* static int perl_config (oconfig_item_t *) */
1584 void module_register (void)
1585 {
1586 perl_argc = 4;
1587 perl_argv = (char **)smalloc ((perl_argc + 1) * sizeof (char *));
1589 /* default options for the Perl interpreter */
1590 perl_argv[0] = "";
1591 perl_argv[1] = "-MCollectd";
1592 perl_argv[2] = "-e";
1593 perl_argv[3] = "1";
1594 perl_argv[4] = NULL;
1596 plugin_register_complex_config ("perl", perl_config);
1597 return;
1598 } /* void module_register (void) */
1600 /* vim: set sw=4 ts=4 tw=78 noexpandtab : */