Merge pull request #1596 from rubenk/fix-a-few-more-prototypes
[collectd.git] / src / python.c
1 /**
2  * collectd - src/python.c
3  * Copyright (C) 2009  Sven Trenkel
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  *   Sven Trenkel <collectd at semidefinite.de>  
25  **/
26
27 #include <Python.h>
28 #include <structmember.h>
29
30 #include <signal.h>
31 #if HAVE_PTHREAD_H
32 # include <pthread.h>
33 #endif
34
35 #include "collectd.h"
36 #include "common.h"
37
38 #include "cpython.h"
39
40 typedef struct cpy_callback_s {
41         char *name;
42         PyObject *callback;
43         PyObject *data;
44         struct cpy_callback_s *next;
45 } cpy_callback_t;
46
47 static char log_doc[] = "This function sends a string to all logging plugins.";
48
49 static char flush_doc[] = "flush([plugin][, timeout][, identifier]) -> None\n"
50                 "\n"
51                 "Flushes the cache of another plugin.";
52
53 static char unregister_doc[] = "Unregisters a callback. This function needs exactly one parameter either\n"
54                 "the function to unregister or the callback identifier to unregister.";
55
56 static char reg_log_doc[] = "register_log(callback[, data][, name]) -> identifier\n"
57                 "\n"
58                 "Register a callback function for log messages.\n"
59                 "\n"
60                 "'callback' is a callable object that will be called every time something\n"
61                 "    is logged.\n"
62                 "'data' is an optional object that will be passed back to the callback\n"
63                 "    function every time it is called.\n"
64                 "'name' is an optional identifier for this callback. The default name\n"
65                 "    is 'python.<module>'.\n"
66                 "    Every callback needs a unique identifier, so if you want to\n"
67                 "    register this callback multiple time from the same module you need\n"
68                 "    to specify a name here.\n"
69                 "'identifier' is the full identifier assigned to this callback.\n"
70                 "\n"
71                 "The callback function will be called with two or three parameters:\n"
72                 "severity: An integer that should be compared to the LOG_ constants.\n"
73                 "message: The text to be logged.\n"
74                 "data: The optional data parameter passed to the register function.\n"
75                 "    If the parameter was omitted it will be omitted here, too.";
76
77 static char reg_init_doc[] = "register_init(callback[, data][, name]) -> identifier\n"
78                 "\n"
79                 "Register a callback function that will be executed once after the config.\n"
80                 "file has been read, all plugins heve been loaded and the collectd has\n"
81                 "forked into the background.\n"
82                 "\n"
83                 "'callback' is a callable object that will be executed.\n"
84                 "'data' is an optional object that will be passed back to the callback\n"
85                 "    function when it is called.\n"
86                 "'name' is an optional identifier for this callback. The default name\n"
87                 "    is 'python.<module>'.\n"
88                 "    Every callback needs a unique identifier, so if you want to\n"
89                 "    register this callback multiple time from the same module you need\n"
90                 "    to specify a name here.\n"
91                 "'identifier' is the full identifier assigned to this callback.\n"
92                 "\n"
93                 "The callback function will be called without parameters, except for\n"
94                 "data if it was supplied.";
95
96 static char reg_config_doc[] = "register_config(callback[, data][, name]) -> identifier\n"
97                 "\n"
98                 "Register a callback function for config file entries.\n"
99                 "'callback' is a callable object that will be called for every config block.\n"
100                 "'data' is an optional object that will be passed back to the callback\n"
101                 "    function every time it is called.\n"
102                 "'name' is an optional identifier for this callback. The default name\n"
103                 "    is 'python.<module>'.\n"
104                 "    Every callback needs a unique identifier, so if you want to\n"
105                 "    register this callback multiple time from the same module you need\n"
106                 "    to specify a name here.\n"
107                 "'identifier' is the full identifier assigned to this callback.\n"
108                 "\n"
109                 "The callback function will be called with one or two parameters:\n"
110                 "config: A Config object.\n"
111                 "data: The optional data parameter passed to the register function.\n"
112                 "    If the parameter was omitted it will be omitted here, too.";
113
114 static char reg_read_doc[] = "register_read(callback[, interval][, data][, name]) -> identifier\n"
115                 "\n"
116                 "Register a callback function for reading data. It will just be called\n"
117                 "in a fixed interval to signal that it's time to dispatch new values.\n"
118                 "'callback' is a callable object that will be called every time something\n"
119                 "    is logged.\n"
120                 "'interval' is the number of seconds between between calls to the callback\n"
121                 "    function. Full float precision is supported here.\n"
122                 "'data' is an optional object that will be passed back to the callback\n"
123                 "    function every time it is called.\n"
124                 "'name' is an optional identifier for this callback. The default name\n"
125                 "    is 'python.<module>'.\n"
126                 "    Every callback needs a unique identifier, so if you want to\n"
127                 "    register this callback multiple time from the same module you need\n"
128                 "    to specify a name here.\n"
129                 "'identifier' is the full identifier assigned to this callback.\n"
130                 "\n"
131                 "The callback function will be called without parameters, except for\n"
132                 "data if it was supplied.";
133
134 static char reg_write_doc[] = "register_write(callback[, data][, name]) -> identifier\n"
135                 "\n"
136                 "Register a callback function to receive values dispatched by other plugins.\n"
137                 "'callback' is a callable object that will be called every time a value\n"
138                 "    is dispatched.\n"
139                 "'data' is an optional object that will be passed back to the callback\n"
140                 "    function every time it is called.\n"
141                 "'name' is an optional identifier for this callback. The default name\n"
142                 "    is 'python.<module>'.\n"
143                 "    Every callback needs a unique identifier, so if you want to\n"
144                 "    register this callback multiple time from the same module you need\n"
145                 "    to specify a name here.\n"
146                 "'identifier' is the full identifier assigned to this callback.\n"
147                 "\n"
148                 "The callback function will be called with one or two parameters:\n"
149                 "values: A Values object which is a copy of the dispatched values.\n"
150                 "data: The optional data parameter passed to the register function.\n"
151                 "    If the parameter was omitted it will be omitted here, too.";
152
153 static char reg_notification_doc[] = "register_notification(callback[, data][, name]) -> identifier\n"
154                 "\n"
155                 "Register a callback function for notifications.\n"
156                 "'callback' is a callable object that will be called every time a notification\n"
157                 "    is dispatched.\n"
158                 "'data' is an optional object that will be passed back to the callback\n"
159                 "    function every time it is called.\n"
160                 "'name' is an optional identifier for this callback. The default name\n"
161                 "    is 'python.<module>'.\n"
162                 "    Every callback needs a unique identifier, so if you want to\n"
163                 "    register this callback multiple time from the same module you need\n"
164                 "    to specify a name here.\n"
165                 "'identifier' is the full identifier assigned to this callback.\n"
166                 "\n"
167                 "The callback function will be called with one or two parameters:\n"
168                 "notification: A copy of the notification that was dispatched.\n"
169                 "data: The optional data parameter passed to the register function.\n"
170                 "    If the parameter was omitted it will be omitted here, too.";
171
172 static char reg_flush_doc[] = "register_flush(callback[, data][, name]) -> identifier\n"
173                 "\n"
174                 "Register a callback function for flush messages.\n"
175                 "'callback' is a callable object that will be called every time a plugin\n"
176                 "    requests a flush for either this or all plugins.\n"
177                 "'data' is an optional object that will be passed back to the callback\n"
178                 "    function every time it is called.\n"
179                 "'name' is an optional identifier for this callback. The default name\n"
180                 "    is 'python.<module>'.\n"
181                 "    Every callback needs a unique identifier, so if you want to\n"
182                 "    register this callback multiple time from the same module you need\n"
183                 "    to specify a name here.\n"
184                 "'identifier' is the full identifier assigned to this callback.\n"
185                 "\n"
186                 "The callback function will be called with two or three parameters:\n"
187                 "timeout: Indicates that only data older than 'timeout' seconds is to\n"
188                 "    be flushed.\n"
189                 "id: Specifies which values are to be flushed.\n"
190                 "data: The optional data parameter passed to the register function.\n"
191                 "    If the parameter was omitted it will be omitted here, too.";
192
193 static char reg_shutdown_doc[] = "register_shutdown(callback[, data][, name]) -> identifier\n"
194                 "\n"
195                 "Register a callback function for collectd shutdown.\n"
196                 "'callback' is a callable object that will be called once collectd is\n"
197                 "    shutting down.\n"
198                 "'data' is an optional object that will be passed back to the callback\n"
199                 "    function if it is called.\n"
200                 "'name' is an optional identifier for this callback. The default name\n"
201                 "    is 'python.<module>'.\n"
202                 "    Every callback needs a unique identifier, so if you want to\n"
203                 "    register this callback multiple time from the same module you need\n"
204                 "    to specify a name here.\n"
205                 "'identifier' is the full identifier assigned to this callback.\n"
206                 "\n"
207                 "The callback function will be called with no parameters except for\n"
208                 "    data if it was supplied.";
209
210
211 static int do_interactive = 0;
212
213 /* This is our global thread state. Python saves some stuff in thread-local
214  * storage. So if we allow the interpreter to run in the background
215  * (the scriptwriters might have created some threads from python), we have
216  * to save the state so we can resume it later after shutdown. */
217
218 static PyThreadState *state;
219
220 static PyObject *sys_path, *cpy_format_exception;
221
222 static cpy_callback_t *cpy_config_callbacks;
223 static cpy_callback_t *cpy_init_callbacks;
224 static cpy_callback_t *cpy_shutdown_callbacks;
225
226 static void cpy_destroy_user_data(void *data) {
227         cpy_callback_t *c = data;
228         free(c->name);
229         Py_DECREF(c->callback);
230         Py_XDECREF(c->data);
231         free(c);
232 }
233
234 /* You must hold the GIL to call this function!
235  * But if you managed to extract the callback parameter then you probably already do. */
236
237 static void cpy_build_name(char *buf, size_t size, PyObject *callback, const char *name) {
238         const char *module = NULL;
239         PyObject *mod = NULL;
240         
241         if (name != NULL) {
242                 snprintf(buf, size, "python.%s", name);
243                 return;
244         }
245         
246         mod = PyObject_GetAttrString(callback, "__module__"); /* New reference. */
247         if (mod != NULL)
248                 module = cpy_unicode_or_bytes_to_string(&mod);
249         
250         if (module != NULL) {
251                 snprintf(buf, size, "python.%s", module);
252                 Py_XDECREF(mod);
253                 PyErr_Clear();
254                 return;
255         }
256         Py_XDECREF(mod);
257         
258         snprintf(buf, size, "python.%p", callback);
259         PyErr_Clear();
260 }
261
262 void cpy_log_exception(const char *context) {
263         int l = 0, i;
264         const char *typename = NULL, *message = NULL;
265         PyObject *type, *value, *traceback, *tn, *m, *list;
266         
267         PyErr_Fetch(&type, &value, &traceback);
268         PyErr_NormalizeException(&type, &value, &traceback);
269         if (type == NULL) return;
270         tn = PyObject_GetAttrString(type, "__name__"); /* New reference. */
271         m = PyObject_Str(value); /* New reference. */
272         if (tn != NULL)
273                 typename = cpy_unicode_or_bytes_to_string(&tn);
274         if (m != NULL)
275                 message = cpy_unicode_or_bytes_to_string(&m);
276         if (typename == NULL)
277                 typename = "NamelessException";
278         if (message == NULL)
279                 message = "N/A";
280         Py_BEGIN_ALLOW_THREADS
281         ERROR("Unhandled python exception in %s: %s: %s", context, typename, message);
282         Py_END_ALLOW_THREADS
283         Py_XDECREF(tn);
284         Py_XDECREF(m);
285         if (!cpy_format_exception || !traceback) {
286                 PyErr_Clear();
287                 Py_DECREF(type);
288                 Py_XDECREF(value);
289                 Py_XDECREF(traceback);
290                 return;
291         }
292         list = PyObject_CallFunction(cpy_format_exception, "NNN", type, value, traceback); /* New reference. Steals references from "type", "value" and "traceback". */
293         if (list)
294                 l = PyObject_Length(list);
295
296         for (i = 0; i < l; ++i) {
297                 PyObject *line;
298                 char const *msg;
299                 char *cpy;
300
301                 line = PyList_GET_ITEM(list, i); /* Borrowed reference. */
302                 Py_INCREF(line);
303
304                 msg = cpy_unicode_or_bytes_to_string(&line);
305                 Py_DECREF(line);
306                 if (msg == NULL)
307                         continue;
308
309                 cpy = strdup(msg);
310                 if (cpy == NULL)
311                         continue;
312
313                 if (cpy[strlen(cpy) - 1] == '\n')
314                         cpy[strlen(cpy) - 1] = 0;
315
316                 Py_BEGIN_ALLOW_THREADS
317                 ERROR("%s", cpy);
318                 Py_END_ALLOW_THREADS
319
320                 free(cpy);
321         }
322
323         Py_XDECREF(list);
324         PyErr_Clear();
325 }
326
327 static int cpy_read_callback(user_data_t *data) {
328         cpy_callback_t *c = data->data;
329         PyObject *ret;
330
331         CPY_LOCK_THREADS
332                 ret = PyObject_CallFunctionObjArgs(c->callback, c->data, (void *) 0); /* New reference. */
333                 if (ret == NULL) {
334                         cpy_log_exception("read callback");
335                 } else {
336                         Py_DECREF(ret);
337                 }
338         CPY_RELEASE_THREADS
339         if (ret == NULL)
340                 return 1;
341         return 0;
342 }
343
344 static int cpy_write_callback(const data_set_t *ds, const value_list_t *value_list, user_data_t *data) {
345         int i;
346         cpy_callback_t *c = data->data;
347         PyObject *ret, *list, *temp, *dict = NULL;
348         Values *v;
349
350         CPY_LOCK_THREADS
351                 list = PyList_New(value_list->values_len); /* New reference. */
352                 if (list == NULL) {
353                         cpy_log_exception("write callback");
354                         CPY_RETURN_FROM_THREADS 0;
355                 }
356                 for (i = 0; i < value_list->values_len; ++i) {
357                         if (ds->ds[i].type == DS_TYPE_COUNTER) {
358                                 if ((long) value_list->values[i].counter == value_list->values[i].counter)
359                                         PyList_SetItem(list, i, PyInt_FromLong(value_list->values[i].counter));
360                                 else
361                                         PyList_SetItem(list, i, PyLong_FromUnsignedLongLong(value_list->values[i].counter));
362                         } else if (ds->ds[i].type == DS_TYPE_GAUGE) {
363                                 PyList_SetItem(list, i, PyFloat_FromDouble(value_list->values[i].gauge));
364                         } else if (ds->ds[i].type == DS_TYPE_DERIVE) {
365                                 if ((long) value_list->values[i].derive == value_list->values[i].derive)
366                                         PyList_SetItem(list, i, PyInt_FromLong(value_list->values[i].derive));
367                                 else
368                                         PyList_SetItem(list, i, PyLong_FromLongLong(value_list->values[i].derive));
369                         } else if (ds->ds[i].type == DS_TYPE_ABSOLUTE) {
370                                 if ((long) value_list->values[i].absolute == value_list->values[i].absolute)
371                                         PyList_SetItem(list, i, PyInt_FromLong(value_list->values[i].absolute));
372                                 else
373                                         PyList_SetItem(list, i, PyLong_FromUnsignedLongLong(value_list->values[i].absolute));
374                         } else {
375                                 Py_BEGIN_ALLOW_THREADS
376                                 ERROR("cpy_write_callback: Unknown value type %d.", ds->ds[i].type);
377                                 Py_END_ALLOW_THREADS
378                                 Py_DECREF(list);
379                                 CPY_RETURN_FROM_THREADS 0;
380                         }
381                         if (PyErr_Occurred() != NULL) {
382                                 cpy_log_exception("value building for write callback");
383                                 Py_DECREF(list);
384                                 CPY_RETURN_FROM_THREADS 0;
385                         }
386                 }
387                 dict = PyDict_New();  /* New reference. */
388                 if (value_list->meta) {
389                         int num;
390                         char **table;
391                         meta_data_t *meta = value_list->meta;
392
393                         num = meta_data_toc(meta, &table);
394                         for (i = 0; i < num; ++i) {
395                                 int type;
396                                 char *string;
397                                 int64_t si;
398                                 uint64_t ui;
399                                 double d;
400                                 _Bool b;
401                                 
402                                 type = meta_data_type(meta, table[i]);
403                                 if (type == MD_TYPE_STRING) {
404                                         if (meta_data_get_string(meta, table[i], &string))
405                                                 continue;
406                                         temp = cpy_string_to_unicode_or_bytes(string);  /* New reference. */
407                                         free(string);
408                                         PyDict_SetItemString(dict, table[i], temp);
409                                         Py_XDECREF(temp);
410                                 } else if (type == MD_TYPE_SIGNED_INT) {
411                                         if (meta_data_get_signed_int(meta, table[i], &si))
412                                                 continue;
413                                         temp = PyObject_CallFunctionObjArgs((void *) &SignedType, PyLong_FromLongLong(si), (void *) 0);  /* New reference. */
414                                         PyDict_SetItemString(dict, table[i], temp);
415                                         Py_XDECREF(temp);
416                                 } else if (type == MD_TYPE_UNSIGNED_INT) {
417                                         if (meta_data_get_unsigned_int(meta, table[i], &ui))
418                                                 continue;
419                                         temp = PyObject_CallFunctionObjArgs((void *) &UnsignedType, PyLong_FromUnsignedLongLong(ui), (void *) 0);  /* New reference. */
420                                         PyDict_SetItemString(dict, table[i], temp);
421                                         Py_XDECREF(temp);
422                                 } else if (type == MD_TYPE_DOUBLE) {
423                                         if (meta_data_get_double(meta, table[i], &d))
424                                                 continue;
425                                         temp = PyFloat_FromDouble(d);  /* New reference. */
426                                         PyDict_SetItemString(dict, table[i], temp);
427                                         Py_XDECREF(temp);
428                                 } else if (type == MD_TYPE_BOOLEAN) {
429                                         if (meta_data_get_boolean(meta, table[i], &b))
430                                                 continue;
431                                         if (b)
432                                                 PyDict_SetItemString(dict, table[i], Py_True);
433                                         else
434                                                 PyDict_SetItemString(dict, table[i], Py_False);
435                                 }
436                                 free(table[i]);
437                         }
438                         free(table);
439                 }
440                 v = (Values *) Values_New(); /* New reference. */
441                 sstrncpy(v->data.host, value_list->host, sizeof(v->data.host));
442                 sstrncpy(v->data.type, value_list->type, sizeof(v->data.type));
443                 sstrncpy(v->data.type_instance, value_list->type_instance, sizeof(v->data.type_instance));
444                 sstrncpy(v->data.plugin, value_list->plugin, sizeof(v->data.plugin));
445                 sstrncpy(v->data.plugin_instance, value_list->plugin_instance, sizeof(v->data.plugin_instance));
446                 v->data.time = CDTIME_T_TO_DOUBLE(value_list->time);
447                 v->interval = CDTIME_T_TO_DOUBLE(value_list->interval);
448                 Py_CLEAR(v->values);
449                 v->values = list;
450                 Py_CLEAR(v->meta);
451                 v->meta = dict;  /* Steals a reference. */
452                 ret = PyObject_CallFunctionObjArgs(c->callback, v, c->data, (void *) 0); /* New reference. */
453                 Py_XDECREF(v);
454                 if (ret == NULL) {
455                         cpy_log_exception("write callback");
456                 } else {
457                         Py_DECREF(ret);
458                 }
459         CPY_RELEASE_THREADS
460         return 0;
461 }
462
463 static int cpy_notification_callback(const notification_t *notification, user_data_t *data) {
464         cpy_callback_t *c = data->data;
465         PyObject *ret, *notify;
466         Notification *n;
467
468         CPY_LOCK_THREADS
469                 notify = Notification_New(); /* New reference. */
470                 n = (Notification *) notify;
471                 sstrncpy(n->data.host, notification->host, sizeof(n->data.host));
472                 sstrncpy(n->data.type, notification->type, sizeof(n->data.type));
473                 sstrncpy(n->data.type_instance, notification->type_instance, sizeof(n->data.type_instance));
474                 sstrncpy(n->data.plugin, notification->plugin, sizeof(n->data.plugin));
475                 sstrncpy(n->data.plugin_instance, notification->plugin_instance, sizeof(n->data.plugin_instance));
476                 n->data.time = CDTIME_T_TO_DOUBLE(notification->time);
477                 sstrncpy(n->message, notification->message, sizeof(n->message));
478                 n->severity = notification->severity;
479                 ret = PyObject_CallFunctionObjArgs(c->callback, n, c->data, (void *) 0); /* New reference. */
480                 Py_XDECREF(notify);
481                 if (ret == NULL) {
482                         cpy_log_exception("notification callback");
483                 } else {
484                         Py_DECREF(ret);
485                 }
486         CPY_RELEASE_THREADS
487         return 0;
488 }
489
490 static void cpy_log_callback(int severity, const char *message, user_data_t *data) {
491         cpy_callback_t * c = data->data;
492         PyObject *ret, *text;
493
494         CPY_LOCK_THREADS
495         text = cpy_string_to_unicode_or_bytes(message);  /* New reference. */
496         if (c->data == NULL)
497                 ret = PyObject_CallFunction(c->callback, "iN", severity, text); /* New reference. Steals a reference from "text". */
498         else
499                 ret = PyObject_CallFunction(c->callback, "iNO", severity, text, c->data); /* New reference. Steals a reference from "text". */
500
501         if (ret == NULL) {
502                 /* FIXME */
503                 /* Do we really want to trigger a log callback because a log callback failed?
504                  * Probably not. */
505                 PyErr_Print();
506                 /* In case someone wanted to be clever, replaced stderr and failed at that. */
507                 PyErr_Clear();
508         } else {
509                 Py_DECREF(ret);
510         }
511         CPY_RELEASE_THREADS
512 }
513
514 static void cpy_flush_callback(int timeout, const char *id, user_data_t *data) {
515         cpy_callback_t * c = data->data;
516         PyObject *ret, *text;
517
518         CPY_LOCK_THREADS
519         text = cpy_string_to_unicode_or_bytes(id);
520         if (c->data == NULL)
521                 ret = PyObject_CallFunction(c->callback, "iN", timeout, text); /* New reference. */
522         else
523                 ret = PyObject_CallFunction(c->callback, "iNO", timeout, text, c->data); /* New reference. */
524
525         if (ret == NULL) {
526                 cpy_log_exception("flush callback");
527         } else {
528                 Py_DECREF(ret);
529         }
530         CPY_RELEASE_THREADS
531 }
532
533 static PyObject *cpy_register_generic(cpy_callback_t **list_head, PyObject *args, PyObject *kwds) {
534         char buf[512];
535         cpy_callback_t *c;
536         char *name = NULL;
537         PyObject *callback = NULL, *data = NULL, *mod = NULL;
538         static char *kwlist[] = {"callback", "data", "name", NULL};
539         
540         if (PyArg_ParseTupleAndKeywords(args, kwds, "O|Oet", kwlist, &callback, &data, NULL, &name) == 0) return NULL;
541         if (PyCallable_Check(callback) == 0) {
542                 PyMem_Free(name);
543                 PyErr_SetString(PyExc_TypeError, "callback needs a be a callable object.");
544                 return NULL;
545         }
546         cpy_build_name(buf, sizeof(buf), callback, name);
547
548         Py_INCREF(callback);
549         Py_XINCREF(data);
550
551         c = malloc(sizeof(*c));
552         if (c == NULL)
553                 return NULL;
554         memset (c, 0, sizeof (*c));
555
556         c->name = strdup(buf);
557         c->callback = callback;
558         c->data = data;
559         c->next = *list_head;
560         *list_head = c;
561         Py_XDECREF(mod);
562         PyMem_Free(name);
563         return cpy_string_to_unicode_or_bytes(buf);
564 }
565
566 static PyObject *cpy_flush(cpy_callback_t **list_head, PyObject *args, PyObject *kwds) {
567         int timeout = -1;
568         char *plugin = NULL, *identifier = NULL;
569         static char *kwlist[] = {"plugin", "timeout", "identifier", NULL};
570         
571         if (PyArg_ParseTupleAndKeywords(args, kwds, "|etiet", kwlist, NULL, &plugin, &timeout, NULL, &identifier) == 0) return NULL;
572         Py_BEGIN_ALLOW_THREADS
573         plugin_flush(plugin, timeout, identifier);
574         Py_END_ALLOW_THREADS
575         PyMem_Free(plugin);
576         PyMem_Free(identifier);
577         Py_RETURN_NONE;
578 }
579
580 static PyObject *cpy_register_config(PyObject *self, PyObject *args, PyObject *kwds) {
581         return cpy_register_generic(&cpy_config_callbacks, args, kwds);
582 }
583
584 static PyObject *cpy_register_init(PyObject *self, PyObject *args, PyObject *kwds) {
585         return cpy_register_generic(&cpy_init_callbacks, args, kwds);
586 }
587
588 typedef int reg_function_t(const char *name, void *callback, void *data);
589
590 static PyObject *cpy_register_generic_userdata(void *reg, void *handler, PyObject *args, PyObject *kwds) {
591         char buf[512];
592         reg_function_t *register_function = (reg_function_t *) reg;
593         cpy_callback_t *c = NULL;
594         user_data_t user_data;
595         char *name = NULL;
596         PyObject *callback = NULL, *data = NULL;
597         static char *kwlist[] = {"callback", "data", "name", NULL};
598         
599         if (PyArg_ParseTupleAndKeywords(args, kwds, "O|Oet", kwlist, &callback, &data, NULL, &name) == 0) return NULL;
600         if (PyCallable_Check(callback) == 0) {
601                 PyMem_Free(name);
602                 PyErr_SetString(PyExc_TypeError, "callback needs a be a callable object.");
603                 return NULL;
604         }
605         cpy_build_name(buf, sizeof(buf), callback, name);
606         PyMem_Free(name);
607         
608         Py_INCREF(callback);
609         Py_XINCREF(data);
610
611         c = malloc(sizeof(*c));
612         if (c == NULL)
613                 return NULL;
614         memset (c, 0, sizeof (*c));
615
616         c->name = strdup(buf);
617         c->callback = callback;
618         c->data = data;
619         c->next = NULL;
620
621         memset (&user_data, 0, sizeof (user_data));
622         user_data.free_func = cpy_destroy_user_data;
623         user_data.data = c;
624
625         register_function(buf, handler, &user_data);
626         return cpy_string_to_unicode_or_bytes(buf);
627 }
628
629 static PyObject *cpy_register_read(PyObject *self, PyObject *args, PyObject *kwds) {
630         char buf[512];
631         cpy_callback_t *c = NULL;
632         user_data_t user_data;
633         double interval = 0;
634         char *name = NULL;
635         PyObject *callback = NULL, *data = NULL;
636         struct timespec ts;
637         static char *kwlist[] = {"callback", "interval", "data", "name", NULL};
638         
639         if (PyArg_ParseTupleAndKeywords(args, kwds, "O|dOet", kwlist, &callback, &interval, &data, NULL, &name) == 0) return NULL;
640         if (PyCallable_Check(callback) == 0) {
641                 PyMem_Free(name);
642                 PyErr_SetString(PyExc_TypeError, "callback needs a be a callable object.");
643                 return NULL;
644         }
645         cpy_build_name(buf, sizeof(buf), callback, name);
646         PyMem_Free(name);
647         
648         Py_INCREF(callback);
649         Py_XINCREF(data);
650
651         c = malloc(sizeof(*c));
652         if (c == NULL)
653                 return NULL;
654         memset (c, 0, sizeof (*c));
655
656         c->name = strdup(buf);
657         c->callback = callback;
658         c->data = data;
659         c->next = NULL;
660
661         memset (&user_data, 0, sizeof (user_data));
662         user_data.free_func = cpy_destroy_user_data;
663         user_data.data = c;
664
665         ts.tv_sec = interval;
666         ts.tv_nsec = (interval - ts.tv_sec) * 1000000000;
667         plugin_register_complex_read(/* group = */ "python", buf,
668                         cpy_read_callback, &ts, &user_data);
669
670         return cpy_string_to_unicode_or_bytes(buf);
671 }
672
673 static PyObject *cpy_register_log(PyObject *self, PyObject *args, PyObject *kwds) {
674         return cpy_register_generic_userdata((void *) plugin_register_log,
675                         (void *) cpy_log_callback, args, kwds);
676 }
677
678 static PyObject *cpy_register_write(PyObject *self, PyObject *args, PyObject *kwds) {
679         return cpy_register_generic_userdata((void *) plugin_register_write,
680                         (void *) cpy_write_callback, args, kwds);
681 }
682
683 static PyObject *cpy_register_notification(PyObject *self, PyObject *args, PyObject *kwds) {
684         return cpy_register_generic_userdata((void *) plugin_register_notification,
685                         (void *) cpy_notification_callback, args, kwds);
686 }
687
688 static PyObject *cpy_register_flush(PyObject *self, PyObject *args, PyObject *kwds) {
689         return cpy_register_generic_userdata((void *) plugin_register_flush,
690                         (void *) cpy_flush_callback, args, kwds);
691 }
692
693 static PyObject *cpy_register_shutdown(PyObject *self, PyObject *args, PyObject *kwds) {
694         return cpy_register_generic(&cpy_shutdown_callbacks, args, kwds);
695 }
696
697 static PyObject *cpy_error(PyObject *self, PyObject *args) {
698         char *text;
699         if (PyArg_ParseTuple(args, "et", NULL, &text) == 0) return NULL;
700         Py_BEGIN_ALLOW_THREADS
701         plugin_log(LOG_ERR, "%s", text);
702         Py_END_ALLOW_THREADS
703         PyMem_Free(text);
704         Py_RETURN_NONE;
705 }
706
707 static PyObject *cpy_warning(PyObject *self, PyObject *args) {
708         char *text;
709         if (PyArg_ParseTuple(args, "et", NULL, &text) == 0) return NULL;
710         Py_BEGIN_ALLOW_THREADS
711         plugin_log(LOG_WARNING, "%s", text);
712         Py_END_ALLOW_THREADS
713         PyMem_Free(text);
714         Py_RETURN_NONE;
715 }
716
717 static PyObject *cpy_notice(PyObject *self, PyObject *args) {
718         char *text;
719         if (PyArg_ParseTuple(args, "et", NULL, &text) == 0) return NULL;
720         Py_BEGIN_ALLOW_THREADS
721         plugin_log(LOG_NOTICE, "%s", text);
722         Py_END_ALLOW_THREADS
723         PyMem_Free(text);
724         Py_RETURN_NONE;
725 }
726
727 static PyObject *cpy_info(PyObject *self, PyObject *args) {
728         char *text;
729         if (PyArg_ParseTuple(args, "et", NULL, &text) == 0) return NULL;
730         Py_BEGIN_ALLOW_THREADS
731         plugin_log(LOG_INFO, "%s", text);
732         Py_END_ALLOW_THREADS
733         PyMem_Free(text);
734         Py_RETURN_NONE;
735 }
736
737 static PyObject *cpy_debug(PyObject *self, PyObject *args) {
738 #ifdef COLLECT_DEBUG
739         char *text;
740         if (PyArg_ParseTuple(args, "et", NULL, &text) == 0) return NULL;
741         Py_BEGIN_ALLOW_THREADS
742         plugin_log(LOG_DEBUG, "%s", text);
743         Py_END_ALLOW_THREADS
744         PyMem_Free(text);
745 #endif
746         Py_RETURN_NONE;
747 }
748
749 static PyObject *cpy_unregister_generic(cpy_callback_t **list_head, PyObject *arg, const char *desc) {
750         char buf[512];
751         const char *name;
752         cpy_callback_t *prev = NULL, *tmp;
753
754         Py_INCREF(arg);
755         name = cpy_unicode_or_bytes_to_string(&arg);
756         if (name == NULL) {
757                 PyErr_Clear();
758                 if (!PyCallable_Check(arg)) {
759                         PyErr_SetString(PyExc_TypeError, "This function needs a string or a callable object as its only parameter.");
760                         Py_DECREF(arg);
761                         return NULL;
762                 }
763                 cpy_build_name(buf, sizeof(buf), arg, NULL);
764                 name = buf;
765         }
766         for (tmp = *list_head; tmp; prev = tmp, tmp = tmp->next)
767                 if (strcmp(name, tmp->name) == 0)
768                         break;
769
770         Py_DECREF(arg);
771         if (tmp == NULL) {
772                 PyErr_Format(PyExc_RuntimeError, "Unable to unregister %s callback '%s'.", desc, name);
773                 return NULL;
774         }
775         /* Yes, this is actually save. To call this function the caller has to
776          * hold the GIL. Well, save as long as there is only one GIL anyway ... */
777         if (prev == NULL)
778                 *list_head = tmp->next;
779         else
780                 prev->next = tmp->next;
781         cpy_destroy_user_data(tmp);
782         Py_RETURN_NONE;
783 }
784
785 typedef int cpy_unregister_function_t(const char *name);
786
787 static PyObject *cpy_unregister_generic_userdata(cpy_unregister_function_t *unreg, PyObject *arg, const char *desc) {
788         char buf[512];
789         const char *name;
790
791         Py_INCREF(arg);
792         name = cpy_unicode_or_bytes_to_string(&arg);
793         if (name == NULL) {
794                 PyErr_Clear();
795                 if (!PyCallable_Check(arg)) {
796                         PyErr_SetString(PyExc_TypeError, "This function needs a string or a callable object as its only parameter.");
797                         Py_DECREF(arg);
798                         return NULL;
799                 }
800                 cpy_build_name(buf, sizeof(buf), arg, NULL);
801                 name = buf;
802         }
803         if (unreg(name) == 0) {
804                 Py_DECREF(arg);
805                 Py_RETURN_NONE;
806         }
807         PyErr_Format(PyExc_RuntimeError, "Unable to unregister %s callback '%s'.", desc, name);
808         Py_DECREF(arg);
809         return NULL;
810 }
811
812 static PyObject *cpy_unregister_log(PyObject *self, PyObject *arg) {
813         return cpy_unregister_generic_userdata(plugin_unregister_log, arg, "log");
814 }
815
816 static PyObject *cpy_unregister_init(PyObject *self, PyObject *arg) {
817         return cpy_unregister_generic(&cpy_init_callbacks, arg, "init");
818 }
819
820 static PyObject *cpy_unregister_config(PyObject *self, PyObject *arg) {
821         return cpy_unregister_generic(&cpy_config_callbacks, arg, "config");
822 }
823
824 static PyObject *cpy_unregister_read(PyObject *self, PyObject *arg) {
825         return cpy_unregister_generic_userdata(plugin_unregister_read, arg, "read");
826 }
827
828 static PyObject *cpy_unregister_write(PyObject *self, PyObject *arg) {
829         return cpy_unregister_generic_userdata(plugin_unregister_write, arg, "write");
830 }
831
832 static PyObject *cpy_unregister_notification(PyObject *self, PyObject *arg) {
833         return cpy_unregister_generic_userdata(plugin_unregister_notification, arg, "notification");
834 }
835
836 static PyObject *cpy_unregister_flush(PyObject *self, PyObject *arg) {
837         return cpy_unregister_generic_userdata(plugin_unregister_flush, arg, "flush");
838 }
839
840 static PyObject *cpy_unregister_shutdown(PyObject *self, PyObject *arg) {
841         return cpy_unregister_generic(&cpy_shutdown_callbacks, arg, "shutdown");
842 }
843
844 static PyMethodDef cpy_methods[] = {
845         {"debug", cpy_debug, METH_VARARGS, log_doc},
846         {"info", cpy_info, METH_VARARGS, log_doc},
847         {"notice", cpy_notice, METH_VARARGS, log_doc},
848         {"warning", cpy_warning, METH_VARARGS, log_doc},
849         {"error", cpy_error, METH_VARARGS, log_doc},
850         {"flush", (PyCFunction) cpy_flush, METH_VARARGS | METH_KEYWORDS, flush_doc},
851         {"register_log", (PyCFunction) cpy_register_log, METH_VARARGS | METH_KEYWORDS, reg_log_doc},
852         {"register_init", (PyCFunction) cpy_register_init, METH_VARARGS | METH_KEYWORDS, reg_init_doc},
853         {"register_config", (PyCFunction) cpy_register_config, METH_VARARGS | METH_KEYWORDS, reg_config_doc},
854         {"register_read", (PyCFunction) cpy_register_read, METH_VARARGS | METH_KEYWORDS, reg_read_doc},
855         {"register_write", (PyCFunction) cpy_register_write, METH_VARARGS | METH_KEYWORDS, reg_write_doc},
856         {"register_notification", (PyCFunction) cpy_register_notification, METH_VARARGS | METH_KEYWORDS, reg_notification_doc},
857         {"register_flush", (PyCFunction) cpy_register_flush, METH_VARARGS | METH_KEYWORDS, reg_flush_doc},
858         {"register_shutdown", (PyCFunction) cpy_register_shutdown, METH_VARARGS | METH_KEYWORDS, reg_shutdown_doc},
859         {"unregister_log", cpy_unregister_log, METH_O, unregister_doc},
860         {"unregister_init", cpy_unregister_init, METH_O, unregister_doc},
861         {"unregister_config", cpy_unregister_config, METH_O, unregister_doc},
862         {"unregister_read", cpy_unregister_read, METH_O, unregister_doc},
863         {"unregister_write", cpy_unregister_write, METH_O, unregister_doc},
864         {"unregister_notification", cpy_unregister_notification, METH_O, unregister_doc},
865         {"unregister_flush", cpy_unregister_flush, METH_O, unregister_doc},
866         {"unregister_shutdown", cpy_unregister_shutdown, METH_O, unregister_doc},
867         {0, 0, 0, 0}
868 };
869
870 static int cpy_shutdown(void) {
871         cpy_callback_t *c;
872         PyObject *ret;
873         
874         /* This can happen if the module was loaded but not configured. */
875         if (state != NULL)
876                 PyEval_RestoreThread(state);
877
878         for (c = cpy_shutdown_callbacks; c; c = c->next) {
879                 ret = PyObject_CallFunctionObjArgs(c->callback, c->data, (void *) 0); /* New reference. */
880                 if (ret == NULL)
881                         cpy_log_exception("shutdown callback");
882                 else
883                         Py_DECREF(ret);
884         }
885         PyErr_Print();
886         Py_Finalize();
887         return 0;
888 }
889
890 static void cpy_int_handler(int sig) {
891         return;
892 }
893
894 static void *cpy_interactive(void *data) {
895         sigset_t sigset;
896         struct sigaction sig_int_action, old;
897         
898         /* Signal handler in a plugin? Bad stuff, but the best way to
899          * handle it I guess. In an interactive session people will
900          * press Ctrl+C at some time, which will generate a SIGINT.
901          * This will cause collectd to shutdown, thus killing the
902          * interactive interpreter, and leaving the terminal in a
903          * mess. Chances are, this isn't what the user wanted to do.
904          * 
905          * So this is the plan:
906          * 1. Block SIGINT in the main thread.
907          * 2. Install our own signal handler that does nothing.
908          * 3. Unblock SIGINT in the interactive thread.
909          *
910          * This will make sure that SIGINT won't kill collectd but
911          * still interrupt syscalls like sleep and pause.
912          * It does not raise a KeyboardInterrupt exception because so
913          * far nobody managed to figure out how to do that. */
914         memset (&sig_int_action, '\0', sizeof (sig_int_action));
915         sig_int_action.sa_handler = cpy_int_handler;
916         sigaction (SIGINT, &sig_int_action, &old);
917         
918         sigemptyset(&sigset);
919         sigaddset(&sigset, SIGINT);
920         pthread_sigmask(SIG_UNBLOCK, &sigset, NULL);
921         PyEval_AcquireThread(state);
922         if (PyImport_ImportModule("readline") == NULL) {
923                 /* This interactive session will suck. */
924                 cpy_log_exception("interactive session init");
925         }
926         PyRun_InteractiveLoop(stdin, "<stdin>");
927         PyErr_Print();
928         PyEval_ReleaseThread(state);
929         NOTICE("python: Interactive interpreter exited, stopping collectd ...");
930         /* Restore the original collectd SIGINT handler and raise SIGINT.
931          * The main thread still has SIGINT blocked and there's nothing we
932          * can do about that so this thread will handle it. But that's not
933          * important, except that it won't interrupt the main loop and so
934          * it might take a few seconds before collectd really shuts down. */
935         sigaction (SIGINT, &old, NULL);
936         raise(SIGINT);
937         pause();
938         return NULL;
939 }
940
941 static int cpy_init(void) {
942         cpy_callback_t *c;
943         PyObject *ret;
944         static pthread_t thread;
945         sigset_t sigset;
946         
947         if (!Py_IsInitialized()) {
948                 WARNING("python: Plugin loaded but not configured.");
949                 plugin_unregister_shutdown("python");
950                 return 0;
951         }
952         PyEval_InitThreads();
953         /* Now it's finally OK to use python threads. */
954         for (c = cpy_init_callbacks; c; c = c->next) {
955                 ret = PyObject_CallFunctionObjArgs(c->callback, c->data, (void *) 0); /* New reference. */
956                 if (ret == NULL)
957                         cpy_log_exception("init callback");
958                 else
959                         Py_DECREF(ret);
960         }
961         sigemptyset(&sigset);
962         sigaddset(&sigset, SIGINT);
963         pthread_sigmask(SIG_BLOCK, &sigset, NULL);
964         state = PyEval_SaveThread();
965         if (do_interactive) {
966                 if (plugin_thread_create(&thread, NULL, cpy_interactive, NULL)) {
967                         ERROR("python: Error creating thread for interactive interpreter.");
968                 }
969         }
970
971         return 0;
972 }
973
974 static PyObject *cpy_oconfig_to_pyconfig(oconfig_item_t *ci, PyObject *parent) {
975         int i;
976         PyObject *item, *values, *children, *tmp;
977         
978         if (parent == NULL)
979                 parent = Py_None;
980         
981         values = PyTuple_New(ci->values_num); /* New reference. */
982         for (i = 0; i < ci->values_num; ++i) {
983                 if (ci->values[i].type == OCONFIG_TYPE_STRING) {
984                         PyTuple_SET_ITEM(values, i, cpy_string_to_unicode_or_bytes(ci->values[i].value.string));
985                 } else if (ci->values[i].type == OCONFIG_TYPE_NUMBER) {
986                         PyTuple_SET_ITEM(values, i, PyFloat_FromDouble(ci->values[i].value.number));
987                 } else if (ci->values[i].type == OCONFIG_TYPE_BOOLEAN) {
988                         PyTuple_SET_ITEM(values, i, PyBool_FromLong(ci->values[i].value.boolean));
989                 }
990         }
991         
992         tmp = cpy_string_to_unicode_or_bytes(ci->key);
993         item = PyObject_CallFunction((void *) &ConfigType, "NONO", tmp, parent, values, Py_None);
994         if (item == NULL)
995                 return NULL;
996         children = PyTuple_New(ci->children_num); /* New reference. */
997         for (i = 0; i < ci->children_num; ++i) {
998                 PyTuple_SET_ITEM(children, i, cpy_oconfig_to_pyconfig(ci->children + i, item));
999         }
1000         tmp = ((Config *) item)->children;
1001         ((Config *) item)->children = children;
1002         Py_XDECREF(tmp);
1003         return item;
1004 }
1005
1006 #ifdef IS_PY3K
1007 static struct PyModuleDef collectdmodule = {
1008         PyModuleDef_HEAD_INIT,
1009         "collectd",   /* name of module */
1010         "The python interface to collectd", /* module documentation, may be NULL */
1011         -1,
1012         cpy_methods
1013 };
1014
1015 PyMODINIT_FUNC PyInit_collectd(void) {
1016         return PyModule_Create(&collectdmodule);
1017 }
1018 #endif
1019
1020 static int cpy_init_python(void) {
1021         char *argv = "";
1022         PyObject *sys;
1023         PyObject *module;
1024
1025 #ifdef IS_PY3K
1026         /* Add a builtin module, before Py_Initialize */
1027         PyImport_AppendInittab("collectd", PyInit_collectd);
1028 #endif
1029         
1030         Py_Initialize();
1031         
1032         PyType_Ready(&ConfigType);
1033         PyType_Ready(&PluginDataType);
1034         ValuesType.tp_base = &PluginDataType;
1035         PyType_Ready(&ValuesType);
1036         NotificationType.tp_base = &PluginDataType;
1037         PyType_Ready(&NotificationType);
1038         SignedType.tp_base = &PyLong_Type;
1039         PyType_Ready(&SignedType);
1040         UnsignedType.tp_base = &PyLong_Type;
1041         PyType_Ready(&UnsignedType);
1042         sys = PyImport_ImportModule("sys"); /* New reference. */
1043         if (sys == NULL) {
1044                 cpy_log_exception("python initialization");
1045                 return 1;
1046         }
1047         sys_path = PyObject_GetAttrString(sys, "path"); /* New reference. */
1048         Py_DECREF(sys);
1049         if (sys_path == NULL) {
1050                 cpy_log_exception("python initialization");
1051                 return 1;
1052         }
1053         PySys_SetArgv(1, &argv);
1054         PyList_SetSlice(sys_path, 0, 1, NULL);
1055
1056 #ifdef IS_PY3K
1057         module = PyImport_ImportModule("collectd");
1058 #else
1059         module = Py_InitModule("collectd", cpy_methods); /* Borrowed reference. */
1060 #endif
1061         PyModule_AddObject(module, "Config", (void *) &ConfigType); /* Steals a reference. */
1062         PyModule_AddObject(module, "Values", (void *) &ValuesType); /* Steals a reference. */
1063         PyModule_AddObject(module, "Notification", (void *) &NotificationType); /* Steals a reference. */
1064         PyModule_AddObject(module, "Signed", (void *) &SignedType); /* Steals a reference. */
1065         PyModule_AddObject(module, "Unsigned", (void *) &UnsignedType); /* Steals a reference. */
1066         PyModule_AddIntConstant(module, "LOG_DEBUG", LOG_DEBUG);
1067         PyModule_AddIntConstant(module, "LOG_INFO", LOG_INFO);
1068         PyModule_AddIntConstant(module, "LOG_NOTICE", LOG_NOTICE);
1069         PyModule_AddIntConstant(module, "LOG_WARNING", LOG_WARNING);
1070         PyModule_AddIntConstant(module, "LOG_ERROR", LOG_ERR);
1071         PyModule_AddIntConstant(module, "NOTIF_FAILURE", NOTIF_FAILURE);
1072         PyModule_AddIntConstant(module, "NOTIF_WARNING", NOTIF_WARNING);
1073         PyModule_AddIntConstant(module, "NOTIF_OKAY", NOTIF_OKAY);
1074         return 0;
1075 }
1076
1077 static int cpy_config(oconfig_item_t *ci) {
1078         int i;
1079         PyObject *tb;
1080
1081         /* Ok in theory we shouldn't do initialization at this point
1082          * but we have to. In order to give python scripts a chance
1083          * to register a config callback we need to be able to execute
1084          * python code during the config callback so we have to start
1085          * the interpreter here. */
1086         /* Do *not* use the python "thread" module at this point! */
1087
1088         if (!Py_IsInitialized() && cpy_init_python()) return 1;
1089
1090         for (i = 0; i < ci->children_num; ++i) {
1091                 oconfig_item_t *item = ci->children + i;
1092                 
1093                 if (strcasecmp(item->key, "Interactive") == 0) {
1094                         if (item->values_num != 1 || item->values[0].type != OCONFIG_TYPE_BOOLEAN)
1095                                 continue;
1096                         do_interactive = item->values[0].value.boolean;
1097                 } else if (strcasecmp(item->key, "Encoding") == 0) {
1098                         if (item->values_num != 1 || item->values[0].type != OCONFIG_TYPE_STRING)
1099                                 continue;
1100                         /* Why is this even necessary? And undocumented? */
1101                         if (PyUnicode_SetDefaultEncoding(item->values[0].value.string))
1102                                 cpy_log_exception("setting default encoding");
1103                 } else if (strcasecmp(item->key, "LogTraces") == 0) {
1104                         if (item->values_num != 1 || item->values[0].type != OCONFIG_TYPE_BOOLEAN)
1105                                 continue;
1106                         if (!item->values[0].value.boolean) {
1107                                 Py_XDECREF(cpy_format_exception);
1108                                 cpy_format_exception = NULL;
1109                                 continue;
1110                         }
1111                         if (cpy_format_exception)
1112                                 continue;
1113                         tb = PyImport_ImportModule("traceback"); /* New reference. */
1114                         if (tb == NULL) {
1115                                 cpy_log_exception("python initialization");
1116                                 continue;
1117                         }
1118                         cpy_format_exception = PyObject_GetAttrString(tb, "format_exception"); /* New reference. */
1119                         Py_DECREF(tb);
1120                         if (cpy_format_exception == NULL)
1121                                 cpy_log_exception("python initialization");
1122                 } else if (strcasecmp(item->key, "ModulePath") == 0) {
1123                         char *dir = NULL;
1124                         PyObject *dir_object;
1125                         
1126                         if (cf_util_get_string(item, &dir) != 0) 
1127                                 continue;
1128                         dir_object = cpy_string_to_unicode_or_bytes(dir); /* New reference. */
1129                         if (dir_object == NULL) {
1130                                 ERROR("python plugin: Unable to convert \"%s\" to "
1131                                       "a python object.", dir);
1132                                 free(dir);
1133                                 cpy_log_exception("python initialization");
1134                                 continue;
1135                         }
1136                         if (PyList_Append(sys_path, dir_object) != 0) {
1137                                 ERROR("python plugin: Unable to append \"%s\" to "
1138                                       "python module path.", dir);
1139                                 cpy_log_exception("python initialization");
1140                         }
1141                         Py_DECREF(dir_object);
1142                         free(dir);
1143                 } else if (strcasecmp(item->key, "Import") == 0) {
1144                         char *module_name = NULL;
1145                         PyObject *module;
1146                         
1147                         if (cf_util_get_string(item, &module_name) != 0) 
1148                                 continue;
1149                         module = PyImport_ImportModule(module_name); /* New reference. */
1150                         if (module == NULL) {
1151                                 ERROR("python plugin: Error importing module \"%s\".", module_name);
1152                                 cpy_log_exception("importing module");
1153                         }
1154                         free(module_name);
1155                         Py_XDECREF(module);
1156                 } else if (strcasecmp(item->key, "Module") == 0) {
1157                         char *name = NULL;
1158                         cpy_callback_t *c;
1159                         PyObject *ret;
1160                         
1161                         if (cf_util_get_string(item, &name) != 0)
1162                                 continue;
1163                         for (c = cpy_config_callbacks; c; c = c->next) {
1164                                 if (strcasecmp(c->name + 7, name) == 0)
1165                                         break;
1166                         }
1167                         if (c == NULL) {
1168                                 WARNING("python plugin: Found a configuration for the \"%s\" plugin, "
1169                                         "but the plugin isn't loaded or didn't register "
1170                                         "a configuration callback.", name);
1171                                 free(name);
1172                                 continue;
1173                         }
1174                         free(name);
1175                         if (c->data == NULL)
1176                                 ret = PyObject_CallFunction(c->callback, "N",
1177                                         cpy_oconfig_to_pyconfig(item, NULL)); /* New reference. */
1178                         else
1179                                 ret = PyObject_CallFunction(c->callback, "NO",
1180                                         cpy_oconfig_to_pyconfig(item, NULL), c->data); /* New reference. */
1181                         if (ret == NULL)
1182                                 cpy_log_exception("loading module");
1183                         else
1184                                 Py_DECREF(ret);
1185                 } else {
1186                         WARNING("python plugin: Ignoring unknown config key \"%s\".", item->key);
1187                 }
1188         }
1189         return 0;
1190 }
1191
1192 void module_register(void) {
1193         plugin_register_complex_config("python", cpy_config);
1194         plugin_register_init("python", cpy_init);
1195         plugin_register_shutdown("python", cpy_shutdown);
1196 }