X-Git-Url: https://git.octo.it/?a=blobdiff_plain;f=src%2Fpython.c;h=0dae99d8242f69804a2c2b3e1ffee2e96b132aa9;hb=0a8741b9061f8df4a78a448c021612db06e17425;hp=deab4be111f0f33651d049037051a8ea79074cae;hpb=9540b75f95d984fd90fca34545674ba3e8f34e8d;p=collectd.git diff --git a/src/python.c b/src/python.c index deab4be1..0dae99d8 100644 --- a/src/python.c +++ b/src/python.c @@ -197,7 +197,7 @@ static char reg_flush_doc[] = "register_flush(callback[, data][, name]) -> ident "The callback function will be called with two or three parameters:\n" "timeout: Indicates that only data older than 'timeout' seconds is to\n" " be flushed.\n" - "id: Specifies which values are to be flushed.\n" + "id: Specifies which values are to be flushed. Might be None.\n" "data: The optional data parameter passed to the register function.\n" " If the parameter was omitted it will be omitted here, too."; @@ -219,6 +219,8 @@ static char reg_shutdown_doc[] = "register_shutdown(callback[, data][, name]) -> " data if it was supplied."; +static pthread_t main_thread; +static PyOS_sighandler_t python_sigint_handler; static _Bool do_interactive = 0; /* This is our global thread state. Python saves some stuff in thread-local @@ -234,12 +236,23 @@ static cpy_callback_t *cpy_config_callbacks; static cpy_callback_t *cpy_init_callbacks; static cpy_callback_t *cpy_shutdown_callbacks; +/* Make sure to hold the GIL while modifying these. */ +static int cpy_shutdown_triggered = 0; +static int cpy_num_callbacks = 0; + static void cpy_destroy_user_data(void *data) { cpy_callback_t *c = data; free(c->name); + CPY_LOCK_THREADS Py_DECREF(c->callback); Py_XDECREF(c->data); free(c); + --cpy_num_callbacks; + if (!cpy_num_callbacks && cpy_shutdown_triggered) { + Py_Finalize(); + return; + } + CPY_RELEASE_THREADS } /* You must hold the GIL to call this function! @@ -387,12 +400,11 @@ static int cpy_write_callback(const data_set_t *ds, const value_list_t *value_li } dict = PyDict_New(); /* New reference. */ if (value_list->meta) { - int num; char **table; meta_data_t *meta = value_list->meta; - num = meta_data_toc(meta, &table); - for (size_t i = 0; i < num; ++i) { + int num = meta_data_toc(meta, &table); + for (int i = 0; i < num; ++i) { int type; char *string; int64_t si; @@ -517,7 +529,12 @@ static void cpy_flush_callback(int timeout, const char *id, user_data_t *data) { PyObject *ret, *text; CPY_LOCK_THREADS - text = cpy_string_to_unicode_or_bytes(id); + if (id) { + text = cpy_string_to_unicode_or_bytes(id); + } else { + text = Py_None; + Py_INCREF(text); + } if (c->data == NULL) ret = PyObject_CallFunction(c->callback, "iN", timeout, text); /* New reference. */ else @@ -557,6 +574,7 @@ static PyObject *cpy_register_generic(cpy_callback_t **list_head, PyObject *args c->callback = callback; c->data = data; c->next = *list_head; + ++cpy_num_callbacks; *list_head = c; Py_XDECREF(mod); PyMem_Free(name); @@ -622,7 +640,6 @@ static PyObject *cpy_register_generic_userdata(void *reg, void *handler, PyObjec char buf[512]; reg_function_t *register_function = (reg_function_t *) reg; cpy_callback_t *c = NULL; - user_data_t user_data = { 0 }; char *name = NULL; PyObject *callback = NULL, *data = NULL; static char *kwlist[] = {"callback", "data", "name", NULL}; @@ -648,17 +665,18 @@ static PyObject *cpy_register_generic_userdata(void *reg, void *handler, PyObjec c->data = data; c->next = NULL; - user_data.free_func = cpy_destroy_user_data; - user_data.data = c; + register_function(buf, handler, &(user_data_t) { + .data = c, + .free_func = cpy_destroy_user_data, + }); - register_function(buf, handler, &user_data); + ++cpy_num_callbacks; return cpy_string_to_unicode_or_bytes(buf); } static PyObject *cpy_register_read(PyObject *self, PyObject *args, PyObject *kwds) { char buf[512]; cpy_callback_t *c = NULL; - user_data_t user_data = { 0 }; double interval = 0; char *name = NULL; PyObject *callback = NULL, *data = NULL; @@ -685,11 +703,13 @@ static PyObject *cpy_register_read(PyObject *self, PyObject *args, PyObject *kwd c->data = data; c->next = NULL; - user_data.free_func = cpy_destroy_user_data; - user_data.data = c; - plugin_register_complex_read(/* group = */ "python", buf, - cpy_read_callback, DOUBLE_TO_CDTIME_T (interval), &user_data); + cpy_read_callback, DOUBLE_TO_CDTIME_T (interval), + &(user_data_t) { + .data = c, + .free_func = cpy_destroy_user_data, + }); + ++cpy_num_callbacks; return cpy_string_to_unicode_or_bytes(buf); } @@ -795,8 +815,8 @@ static PyObject *cpy_unregister_generic(cpy_callback_t **list_head, PyObject *ar PyErr_Format(PyExc_RuntimeError, "Unable to unregister %s callback '%s'.", desc, name); return NULL; } - /* Yes, this is actually save. To call this function the caller has to - * hold the GIL. Well, save as long as there is only one GIL anyway ... */ + /* Yes, this is actually safe. To call this function the caller has to + * hold the GIL. Well, safe as long as there is only one GIL anyway ... */ if (prev == NULL) *list_head = tmp->next; else @@ -805,6 +825,15 @@ static PyObject *cpy_unregister_generic(cpy_callback_t **list_head, PyObject *ar Py_RETURN_NONE; } +static void cpy_unregister_list(cpy_callback_t **list_head) { + cpy_callback_t *cur, *next; + for (cur = *list_head; cur; cur = next) { + next = cur->next; + cpy_destroy_user_data(cur); + } + *list_head = NULL; +} + typedef int cpy_unregister_function_t(const char *name); static PyObject *cpy_unregister_generic_userdata(cpy_unregister_function_t *unreg, PyObject *arg, const char *desc) { @@ -894,9 +923,17 @@ static PyMethodDef cpy_methods[] = { static int cpy_shutdown(void) { PyObject *ret; - /* This can happen if the module was loaded but not configured. */ - if (state != NULL) - PyEval_RestoreThread(state); + if (!state) { + printf("================================================================\n"); + printf("collectd shutdown while running an interactive session. This will\n"); + printf("probably leave your terminal in a mess.\n"); + printf("Run the command \"reset\" to get it back into a usable state.\n"); + printf("You can press Ctrl+D in the interactive session to\n"); + printf("close collectd and avoid this problem in the future.\n"); + printf("================================================================\n"); + } + + CPY_LOCK_THREADS for (cpy_callback_t *c = cpy_shutdown_callbacks; c; c = c->next) { ret = PyObject_CallFunctionObjArgs(c->callback, c->data, (void *) 0); /* New reference. */ @@ -906,17 +943,25 @@ static int cpy_shutdown(void) { Py_DECREF(ret); } PyErr_Print(); - Py_Finalize(); - return 0; -} -static void cpy_int_handler(int sig) { - return; + Py_BEGIN_ALLOW_THREADS + cpy_unregister_list(&cpy_config_callbacks); + cpy_unregister_list(&cpy_init_callbacks); + cpy_unregister_list(&cpy_shutdown_callbacks); + cpy_shutdown_triggered = 1; + Py_END_ALLOW_THREADS + + if (!cpy_num_callbacks) { + Py_Finalize(); + return 0; + } + + CPY_RELEASE_THREADS + return 0; } -static void *cpy_interactive(void *data) { - sigset_t sigset; - struct sigaction old; +static void *cpy_interactive(void *pipefd) { + PyOS_sighandler_t cur_sig; /* Signal handler in a plugin? Bad stuff, but the best way to * handle it I guess. In an interactive session people will @@ -926,54 +971,67 @@ static void *cpy_interactive(void *data) { * mess. Chances are, this isn't what the user wanted to do. * * So this is the plan: - * 1. Block SIGINT in the main thread. - * 2. Install our own signal handler that does nothing. - * 3. Unblock SIGINT in the interactive thread. + * 1. Restore Python's own signal handler + * 2. Tell Python we just forked so it will accept this thread + * as the main one. No version of Python will ever handle + * interrupts anywhere but in the main thread. + * 3. After the interactive loop is done, restore collectd's + * SIGINT handler. + * 4. Raise SIGINT for a clean shutdown. The signal is sent to + * the main thread to ensure it wakes up the main interval + * sleep so that collectd shuts down immediately not in 10 + * seconds. * * This will make sure that SIGINT won't kill collectd but - * still interrupt syscalls like sleep and pause. - * It does not raise a KeyboardInterrupt exception because so - * far nobody managed to figure out how to do that. */ - struct sigaction sig_int_action = { - .sa_handler = cpy_int_handler - }; - sigaction (SIGINT, &sig_int_action, &old); - - sigemptyset(&sigset); - sigaddset(&sigset, SIGINT); - pthread_sigmask(SIG_UNBLOCK, &sigset, NULL); - PyEval_AcquireThread(state); + * still interrupt syscalls like sleep and pause. */ + if (PyImport_ImportModule("readline") == NULL) { /* This interactive session will suck. */ cpy_log_exception("interactive session init"); } + cur_sig = PyOS_setsig(SIGINT, python_sigint_handler); + PyOS_AfterFork(); + PyEval_InitThreads(); + close(*(int *) pipefd); PyRun_InteractiveLoop(stdin, ""); + PyOS_setsig(SIGINT, cur_sig); PyErr_Print(); - PyEval_ReleaseThread(state); + state = PyEval_SaveThread(); NOTICE("python: Interactive interpreter exited, stopping collectd ..."); - /* Restore the original collectd SIGINT handler and raise SIGINT. - * The main thread still has SIGINT blocked and there's nothing we - * can do about that so this thread will handle it. But that's not - * important, except that it won't interrupt the main loop and so - * it might take a few seconds before collectd really shuts down. */ - sigaction (SIGINT, &old, NULL); - raise(SIGINT); - pause(); + pthread_kill(main_thread, SIGINT); return NULL; } static int cpy_init(void) { PyObject *ret; + int pipefd[2]; + char buf; static pthread_t thread; - sigset_t sigset; if (!Py_IsInitialized()) { WARNING("python: Plugin loaded but not configured."); plugin_unregister_shutdown("python"); + Py_Finalize(); return 0; } - PyEval_InitThreads(); - /* Now it's finally OK to use python threads. */ + main_thread = pthread_self(); + if (do_interactive) { + if (pipe(pipefd)) { + ERROR("python: Unable to create pipe."); + return 1; + } + if (plugin_thread_create(&thread, NULL, cpy_interactive, pipefd + 1, + "python interpreter")) { + ERROR("python: Error creating thread for interactive interpreter."); + } + if(read(pipefd[0], &buf, 1)) + ; + (void)close(pipefd[0]); + } else { + PyEval_InitThreads(); + state = PyEval_SaveThread(); + } + CPY_LOCK_THREADS for (cpy_callback_t *c = cpy_init_callbacks; c; c = c->next) { ret = PyObject_CallFunctionObjArgs(c->callback, c->data, (void *) 0); /* New reference. */ if (ret == NULL) @@ -981,15 +1039,7 @@ static int cpy_init(void) { else Py_DECREF(ret); } - sigemptyset(&sigset); - sigaddset(&sigset, SIGINT); - pthread_sigmask(SIG_BLOCK, &sigset, NULL); - state = PyEval_SaveThread(); - if (do_interactive) { - if (plugin_thread_create(&thread, NULL, cpy_interactive, NULL)) { - ERROR("python: Error creating thread for interactive interpreter."); - } - } + CPY_RELEASE_THREADS return 0; } @@ -1040,6 +1090,7 @@ PyMODINIT_FUNC PyInit_collectd(void) { #endif static int cpy_init_python(void) { + PyOS_sighandler_t cur_sig; PyObject *sys; PyObject *module; @@ -1051,7 +1102,10 @@ static int cpy_init_python(void) { char *argv = ""; #endif + /* Chances are the current signal handler is already SIG_DFL, but let's make sure. */ + cur_sig = PyOS_setsig(SIGINT, SIG_DFL); Py_Initialize(); + python_sigint_handler = PyOS_setsig(SIGINT, cur_sig); PyType_Ready(&ConfigType); PyType_Ready(&PluginDataType); @@ -1124,21 +1178,23 @@ static int cpy_config(oconfig_item_t *ci) { continue; } } else if (strcasecmp(item->key, "Encoding") == 0) { -#ifdef IS_PY3K - ERROR("python: \"Encoding\" was used in the config file but Python3 was used, which does not support changing encodings"); - status = 1; - continue; -#endif char *encoding = NULL; if (cf_util_get_string(item, &encoding) != 0) { status = 1; continue; } +#ifdef IS_PY3K + ERROR("python: \"Encoding\" was used in the config file but Python3 was used, which does not support changing encodings"); + status = 1; + sfree(encoding); + continue; +#else /* Why is this even necessary? And undocumented? */ if (PyUnicode_SetDefaultEncoding(encoding)) { cpy_log_exception("setting default encoding"); status = 1; } +#endif sfree(encoding); } else if (strcasecmp(item->key, "LogTraces") == 0) { _Bool log_traces;