plugins/python/debug: adapt debug refcount solution of sudoers plugin

This commit is contained in:
Robert Manner
2020-02-17 16:14:30 +01:00
committed by Todd C. Miller
parent a0c2de4cef
commit 8a9218d161
2 changed files with 26 additions and 13 deletions

View File

@@ -413,10 +413,11 @@ python_plugin_deinit(struct PluginContext *plugin_ctx)
Py_Finalize(); Py_Finalize();
} }
python_debug_deregister();
py_ctx_reset(); py_ctx_reset();
} }
python_debug_deregister();
debug_return; debug_return;
} }

View File

@@ -39,6 +39,7 @@
static int python_debug_instance = SUDO_DEBUG_INSTANCE_INITIALIZER; static int python_debug_instance = SUDO_DEBUG_INSTANCE_INITIALIZER;
static unsigned int python_debug_refcnt;
static const char *const python_subsystem_names[] = { static const char *const python_subsystem_names[] = {
"py_calls", // logs c -> py calls "py_calls", // logs c -> py calls
@@ -79,20 +80,14 @@ bool
python_debug_register(const char *program, python_debug_register(const char *program,
struct sudo_conf_debug_file_list *debug_files) struct sudo_conf_debug_file_list *debug_files)
{ {
int instance = python_debug_instance;
struct sudo_debug_file *debug_file, *debug_next; struct sudo_debug_file *debug_file, *debug_next;
/* Already initialized? */
if (python_debug_instance != SUDO_DEBUG_INSTANCE_INITIALIZER) {
sudo_debug_set_active_instance(python_debug_instance);
}
/* Setup debugging if indicated. */ /* Setup debugging if indicated. */
if (debug_files != NULL && !TAILQ_EMPTY(debug_files)) { if (debug_files != NULL && !TAILQ_EMPTY(debug_files)) {
if (program != NULL) { if (program != NULL) {
python_debug_instance = sudo_debug_register(program, instance = sudo_debug_register(program, python_subsystem_names,
python_subsystem_names, (unsigned int *)python_subsystem_ids, debug_files); (unsigned int *)python_subsystem_ids, debug_files);
if (python_debug_instance == SUDO_DEBUG_INSTANCE_ERROR)
return false;
} }
TAILQ_FOREACH_SAFE(debug_file, debug_files, entries, debug_next) { TAILQ_FOREACH_SAFE(debug_file, debug_files, entries, debug_next) {
TAILQ_REMOVE(debug_files, debug_file, entries); TAILQ_REMOVE(debug_files, debug_file, entries);
@@ -101,6 +96,21 @@ python_debug_register(const char *program,
free(debug_file); free(debug_file);
} }
} }
switch (instance) {
case SUDO_DEBUG_INSTANCE_ERROR:
return false;
case SUDO_DEBUG_INSTANCE_INITIALIZER:
/* Nothing to do */
break;
default:
/* New debug instance or additional reference on existing one. */
python_debug_instance = instance;
sudo_debug_set_active_instance(python_debug_instance);
python_debug_refcnt++;
break;
}
return true; return true;
} }
@@ -112,9 +122,11 @@ python_debug_deregister(void)
{ {
debug_decl(python_debug_deregister, PYTHON_DEBUG_INTERNAL); debug_decl(python_debug_deregister, PYTHON_DEBUG_INTERNAL);
if (python_debug_instance != SUDO_DEBUG_INSTANCE_INITIALIZER) { if (python_debug_refcnt != 0) {
sudo_debug_exit(__func__, __FILE__, __LINE__, sudo_debug_subsys); sudo_debug_exit(__func__, __FILE__, __LINE__, sudo_debug_subsys);
sudo_debug_deregister(python_debug_instance); if (--python_debug_refcnt == 0) {
if (sudo_debug_deregister(python_debug_instance) < 1)
python_debug_instance = SUDO_DEBUG_INSTANCE_INITIALIZER; python_debug_instance = SUDO_DEBUG_INSTANCE_INITIALIZER;
} }
}
} }