mirror of
https://github.com/NLnetLabs/unbound.git
synced 2026-08-17 21:25:50 +02:00
Merge branch 'dev/pythonmod-multi-instance/v1.9.0' into dev/all-merged/master
This commit is contained in:
@@ -1026,7 +1026,7 @@ struct config_file {
|
||||
char* control_key_file;
|
||||
char* control_cert_file;
|
||||
int do_daemonize;
|
||||
char* python_script;
|
||||
struct config_strlist* python_script;
|
||||
};
|
||||
|
||||
/* ************************************************************************************ *
|
||||
|
||||
+51
-28
@@ -64,6 +64,15 @@ typedef struct PyThreadState PyThreadState;
|
||||
typedef void* PyGILState_STATE;
|
||||
#endif
|
||||
|
||||
/**
|
||||
* counter for python module instances
|
||||
* incremented by pythonmod_init(...)
|
||||
*/
|
||||
int py_mod_count = 0;
|
||||
|
||||
/** Python main thread */
|
||||
PyThreadState* mainthr;
|
||||
|
||||
/**
|
||||
* Global state for the module.
|
||||
*/
|
||||
@@ -72,8 +81,6 @@ struct pythonmod_env {
|
||||
/** Python script filename. */
|
||||
const char* fname;
|
||||
|
||||
/** Python main thread */
|
||||
PyThreadState* mainthr;
|
||||
/** Python module. */
|
||||
PyObject* module;
|
||||
|
||||
@@ -242,11 +249,15 @@ cleanup:
|
||||
|
||||
int pythonmod_init(struct module_env* env, int id)
|
||||
{
|
||||
int py_mod_idx = py_mod_count++;
|
||||
|
||||
/* Initialize module */
|
||||
FILE* script_py = NULL;
|
||||
PyObject* py_init_arg, *res;
|
||||
PyGILState_STATE gil;
|
||||
int init_standard = 1;
|
||||
int init_standard = 1, i = 0;
|
||||
|
||||
struct config_strlist* cfg_item = env->cfg->python_script;
|
||||
|
||||
struct pythonmod_env* pe = (struct pythonmod_env*)calloc(1, sizeof(struct pythonmod_env));
|
||||
if (!pe)
|
||||
@@ -258,14 +269,21 @@ int pythonmod_init(struct module_env* env, int id)
|
||||
env->modinfo[id] = (void*) pe;
|
||||
|
||||
/* Initialize module */
|
||||
pe->fname = env->cfg->python_script;
|
||||
pe->fname=NULL; i = 0;
|
||||
while (cfg_item!=NULL) {
|
||||
if (py_mod_idx==i++) {
|
||||
pe->fname=cfg_item->str;
|
||||
break;
|
||||
}
|
||||
cfg_item = cfg_item->next;
|
||||
}
|
||||
if(pe->fname==NULL || pe->fname[0]==0) {
|
||||
log_err("pythonmod: no script given.");
|
||||
log_err("pythonmod[%d]: no script given.", py_mod_idx);
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* Initialize Python libraries */
|
||||
if (!Py_IsInitialized())
|
||||
if (py_mod_count==1 && !Py_IsInitialized())
|
||||
{
|
||||
#if PY_MAJOR_VERSION >= 3
|
||||
wchar_t progname[8];
|
||||
@@ -281,29 +299,31 @@ int pythonmod_init(struct module_env* env, int id)
|
||||
Py_Initialize();
|
||||
PyEval_InitThreads();
|
||||
SWIG_init();
|
||||
pe->mainthr = PyEval_SaveThread();
|
||||
mainthr = PyEval_SaveThread();
|
||||
}
|
||||
|
||||
gil = PyGILState_Ensure();
|
||||
|
||||
/* Initialize Python */
|
||||
PyRun_SimpleString("import sys \n");
|
||||
PyRun_SimpleString("sys.path.append('.') \n");
|
||||
if(env->cfg->directory && env->cfg->directory[0]) {
|
||||
char wdir[1524];
|
||||
snprintf(wdir, sizeof(wdir), "sys.path.append('%s') \n",
|
||||
env->cfg->directory);
|
||||
PyRun_SimpleString(wdir);
|
||||
}
|
||||
PyRun_SimpleString("sys.path.append('"RUN_DIR"') \n");
|
||||
PyRun_SimpleString("sys.path.append('"SHARE_DIR"') \n");
|
||||
PyRun_SimpleString("import distutils.sysconfig \n");
|
||||
PyRun_SimpleString("sys.path.append(distutils.sysconfig.get_python_lib(1,0)) \n");
|
||||
if (PyRun_SimpleString("from unboundmodule import *\n") < 0)
|
||||
{
|
||||
log_err("pythonmod: cannot initialize core module: unboundmodule.py");
|
||||
PyGILState_Release(gil);
|
||||
return 0;
|
||||
if (py_mod_count==1) {
|
||||
/* Initialize Python */
|
||||
PyRun_SimpleString("import sys \n");
|
||||
PyRun_SimpleString("sys.path.append('.') \n");
|
||||
if(env->cfg->directory && env->cfg->directory[0]) {
|
||||
char wdir[1524];
|
||||
snprintf(wdir, sizeof(wdir), "sys.path.append('%s') \n",
|
||||
env->cfg->directory);
|
||||
PyRun_SimpleString(wdir);
|
||||
}
|
||||
PyRun_SimpleString("sys.path.append('"RUN_DIR"') \n");
|
||||
PyRun_SimpleString("sys.path.append('"SHARE_DIR"') \n");
|
||||
PyRun_SimpleString("import distutils.sysconfig \n");
|
||||
PyRun_SimpleString("sys.path.append(distutils.sysconfig.get_python_lib(1,0)) \n");
|
||||
if (PyRun_SimpleString("from unboundmodule import *\n") < 0)
|
||||
{
|
||||
log_err("pythonmod: cannot initialize core module: unboundmodule.py");
|
||||
PyGILState_Release(gil);
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
/* Check Python file load */
|
||||
@@ -340,6 +360,7 @@ int pythonmod_init(struct module_env* env, int id)
|
||||
(void)PyParser_SimpleParseFile(script_py, pe->fname, Py_file_input);
|
||||
log_py_err();
|
||||
PyGILState_Release(gil);
|
||||
fclose(script_py);
|
||||
return 0;
|
||||
}
|
||||
fclose(script_py);
|
||||
@@ -424,9 +445,11 @@ void pythonmod_deinit(struct module_env* env, int id)
|
||||
Py_XDECREF(pe->data);
|
||||
PyGILState_Release(gil);
|
||||
|
||||
PyEval_RestoreThread(pe->mainthr);
|
||||
Py_Finalize();
|
||||
pe->mainthr = NULL;
|
||||
if(--py_mod_count==0) {
|
||||
PyEval_RestoreThread(mainthr);
|
||||
Py_Finalize();
|
||||
mainthr = NULL;
|
||||
}
|
||||
}
|
||||
pe->fname = NULL;
|
||||
free(pe);
|
||||
|
||||
Reference in New Issue
Block a user