X-Git-Url: https://git.verplant.org/?a=blobdiff_plain;f=src%2Fpython.c;h=884bb054ace00a0b6eed302457930cf6c52bb2d0;hb=88001af68cb86d0d95fa143b031359b10eddbe1f;hp=4d294ddb25a850d89da28f7d82803e7edd2fe249;hpb=e5b10d0175c7f6c1db0567a103ae61c54d946e9d;p=collectd.git diff --git a/src/python.c b/src/python.c index 4d294ddb..884bb054 100644 --- a/src/python.c +++ b/src/python.c @@ -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 @@ -622,7 +624,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,8 +649,10 @@ 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; + user_data_t user_data = { + .data = c, + .free_func = cpy_destroy_user_data + }; register_function(buf, handler, &user_data); return cpy_string_to_unicode_or_bytes(buf); @@ -658,7 +661,6 @@ static PyObject *cpy_register_generic_userdata(void *reg, void *handler, PyObjec 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,8 +687,10 @@ 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; + user_data_t user_data = { + .data = c, + .free_func = cpy_destroy_user_data + }; plugin_register_complex_read(/* group = */ "python", buf, cpy_read_callback, DOUBLE_TO_CDTIME_T (interval), &user_data); @@ -910,13 +914,8 @@ static int cpy_shutdown(void) { return 0; } -static void cpy_int_handler(int sig) { - return; -} - static void *cpy_interactive(void *data) { - sigset_t sigset; - struct sigaction old; + 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,46 +925,40 @@ 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); + * still interrupt syscalls like sleep and pause. */ - sigemptyset(&sigset); - sigaddset(&sigset, SIGINT); - pthread_sigmask(SIG_UNBLOCK, &sigset, NULL); PyEval_AcquireThread(state); if (PyImport_ImportModule("readline") == NULL) { /* This interactive session will suck. */ cpy_log_exception("interactive session init"); - } + } + cur_sig = PyOS_setsig(SIGINT, python_sigint_handler); + /* We totally forked just now. Everyone saw that, right? */ + PyOS_AfterFork(); PyRun_InteractiveLoop(stdin, ""); + PyOS_setsig(SIGINT, cur_sig); PyErr_Print(); PyEval_ReleaseThread(state); 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; static pthread_t thread; - sigset_t sigset; if (!Py_IsInitialized()) { WARNING("python: Plugin loaded but not configured."); @@ -981,10 +974,8 @@ static int cpy_init(void) { else Py_DECREF(ret); } - sigemptyset(&sigset); - sigaddset(&sigset, SIGINT); - pthread_sigmask(SIG_BLOCK, &sigset, NULL); state = PyEval_SaveThread(); + main_thread = pthread_self(); if (do_interactive) { if (plugin_thread_create(&thread, NULL, cpy_interactive, NULL)) { ERROR("python: Error creating thread for interactive interpreter."); @@ -1040,6 +1031,7 @@ PyMODINIT_FUNC PyInit_collectd(void) { #endif static int cpy_init_python(void) { + PyOS_sighandler_t cur_sig; PyObject *sys; PyObject *module; @@ -1051,7 +1043,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); @@ -1132,12 +1127,16 @@ static int cpy_config(oconfig_item_t *ci) { #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)) + if (PyUnicode_SetDefaultEncoding(encoding)) { cpy_log_exception("setting default encoding"); - sfree(encoding); + status = 1; + } #endif + sfree(encoding); } else if (strcasecmp(item->key, "LogTraces") == 0) { _Bool log_traces; if (cf_util_get_boolean(item, &log_traces) != 0) {