X-Git-Url: https://git.octo.it/?a=blobdiff_plain;f=src%2Fpython.c;h=912c18ae227bbd3d8379130ce1f5942d7af80eea;hb=92498b8a0e1d50c7e06d435395ac0ac307cc190c;hp=deab4be111f0f33651d049037051a8ea79074cae;hpb=5b4053d27b6a24e2f18e678f0d8c3343b7dfad7c;p=collectd.git diff --git a/src/python.c b/src/python.c index deab4be1..912c18ae 100644 --- a/src/python.c +++ b/src/python.c @@ -219,7 +219,10 @@ 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; +static int do_interactive = 0; /* This is our global thread state. Python saves some stuff in thread-local * storage. So if we allow the interpreter to run in the background @@ -910,13 +913,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 +924,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); - - sigemptyset(&sigset); - sigaddset(&sigset, SIGINT); - pthread_sigmask(SIG_UNBLOCK, &sigset, NULL); + * still interrupt syscalls like sleep and pause. */ + 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 +973,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 +1030,7 @@ PyMODINIT_FUNC PyInit_collectd(void) { #endif static int cpy_init_python(void) { + PyOS_sighandler_t cur_sig; PyObject *sys; PyObject *module; @@ -1051,7 +1042,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 +1118,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;