static void inner_main(void *closure, int argc, char **argv) { #ifdef SWIGINIT SWIGINIT #else SWIG_init(); /* SWIG init function */ #endif scm_shell(argc, argv); /* scheme interpreter */ /* never reached: scm_shell will perform an exit */ }
void *decisionProcessor(void *pc) { pktcore_t *pcore = (pktcore_t *) pc; gpacket_t *in_pkt; SWIG_init(); /* Initialize the wrapped GINIC module*/ int pktsize; pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL); while (1) { verbose(2, "[decisionProcessor]:: Waiting for a packet..."); readQueue(pcore->decisionQ, (void **) &in_pkt, &pktsize); pthread_testcancel(); verbose(2, "[decisionProcessor]:: Got a packet for further processing..."); /* Classical router processor.*/ classicalDecisionQProcessor(pcore, in_pkt); } }
static int RbGlobalInitialize(const CMPIBroker* broker, CMPIStatus* st) { char *loadpath; VALUE searchpath; _SBLIM_TRACE(1,("<%d> Ruby: RbGlobalInitialize, stack @ %p", getpid(), &searchpath)); ruby_init(); ruby_init_loadpath(); extern void SWIG_init(); SWIG_init(); searchpath = rb_gv_get("$:"); /* Append /usr/share/cmpi to $: */ rb_ary_push(searchpath, rb_str_new2("/usr/share/cmpi")); /* Check RUBY_PROVIDERS_DIR_ENV if its a dir, append to $: */ loadpath = getenv(RUBY_PROVIDERS_DIR_ENV); if (loadpath) { struct stat buf; if (stat(loadpath, &buf)) { _SBLIM_TRACE(1,("<%d> Can't stat $%s '%s'", getpid(), RUBY_PROVIDERS_DIR_ENV, loadpath)); return -1; } if ((buf.st_mode & S_IFDIR) == 0) { _SBLIM_TRACE(1,("<%d> Not a directory: $%s '%s'", getpid(), RUBY_PROVIDERS_DIR_ENV, loadpath)); return -1; } _SBLIM_TRACE(1,("<%d> Loading providers from: $%s '%s'", getpid(), RUBY_PROVIDERS_DIR_ENV, loadpath)); rb_ary_push(searchpath, rb_str_new2(loadpath)); } else { _SBLIM_TRACE(0, ("<%d> Hmm, %s not set ?!", getpid(), RUBY_PROVIDERS_DIR_ENV)); } return 0; }
int python_call_fns(char* function,fnsDesc *desc){ PyObject *pFunc = NULL, *pValue= NULL, *pArg = NULL; int ret=0; if (!Py_IsInitialized()) return -1; SWIG_init(); // Initialise SWIG types if (pModule != NULL) { pFunc = PyObject_GetAttrString(pModule, function); if (pFunc && PyCallable_Check(pFunc)) { pArg = SWIG_NewPointerObj(SWIG_as_voidptr(desc), SWIGTYPE_p_fns_desc, SWIG_POINTER_NEW | 0 ); if (pArg) { pValue = PyObject_CallFunction(pFunc, "O", pArg); } if (pValue != NULL) { ret = PyInt_AsLong(pValue); //printf("Result of call: %d\n",ret); Py_DECREF(pValue); } else { Py_DECREF(pFunc); PyErr_Print(); fprintf(stderr, "Call failed\n"); return -1; } } else { if (PyErr_Occurred()) PyErr_Print(); fprintf(stderr, "Cannot find function \n"); } Py_XDECREF(pFunc); } else { PyErr_Print(); fprintf(stderr, "Failed to load \"%s\"\n",function); } return ret; }
static int PyGlobalInitialize() { /* debug("<%d/0x%x> PyGlobalInitialize() called", getpid(), pthread_self()); */ if (_TARGET_INIT) { /* debug("<%d/0x%x> PyGlobalInitialize() returning: already initialized", getpid(), pthread_self()); */ return 0; } _TARGET_INIT=1;//true debug("<%d/0x%x> Python: Loading", getpid(), pthread_self()); Py_SetProgramName(PLUGIN_FILE); Py_Initialize(); SWIGEXPORT void SWIG_init(void); SWIG_init(); pluginMainPyThreadState = PyGILState_GetThisThreadState(); PyEval_ReleaseThread(pluginMainPyThreadState); debug("<%d/0x%x> PyGlobalInitialize() succeeded", getpid(), pthread_self()); return 0; }
int pythonmod_init(struct module_env* env, int id) { /* Initialize module */ FILE* script_py = NULL; PyObject* py_init_arg, *res; PyGILState_STATE gil; int init_standard = 1; #if PY_MAJOR_VERSION < 3 PyObject* PyFileObject = NULL; #endif struct pythonmod_env* pe = (struct pythonmod_env*)calloc(1, sizeof(struct pythonmod_env)); if (!pe) { log_err("pythonmod: malloc failure"); return 0; } env->modinfo[id] = (void*) pe; /* Initialize module */ pe->fname = env->cfg->python_script; if(pe->fname==NULL || pe->fname[0]==0) { log_err("pythonmod: no script given."); return 0; } /* Initialize Python libraries */ if (!Py_IsInitialized()) { #if PY_MAJOR_VERSION >= 3 wchar_t progname[8]; mbstowcs(progname, "unbound", 8); #else char *progname = "unbound"; #endif Py_SetProgramName(progname); Py_NoSiteFlag = 1; #if PY_MAJOR_VERSION >= 3 PyImport_AppendInittab(SWIG_name, (void*)SWIG_init); #endif Py_Initialize(); PyEval_InitThreads(); SWIG_init(); pe->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; } /* Check Python file load */ /* uses python to open the file, this works on other platforms, * eg. Windows, to open the file in the correct mode for python */ #if PY_MAJOR_VERSION < 3 PyFileObject = PyFile_FromString((char*)pe->fname, "r"); script_py = PyFile_AsFile(PyFileObject); #else script_py = _Py_fopen(pe->fname, "r"); #endif if (script_py == NULL) { log_err("pythonmod: can't open file %s for reading", pe->fname); PyGILState_Release(gil); return 0; } /* Load file */ pe->module = PyImport_AddModule("__main__"); pe->dict = PyModule_GetDict(pe->module); pe->data = Py_None; Py_INCREF(pe->data); PyModule_AddObject(pe->module, "mod_env", pe->data); /* TODO: deallocation of pe->... if an error occurs */ if (PyRun_SimpleFile(script_py, pe->fname) < 0) { log_err("pythonmod: can't parse Python script %s", pe->fname); /* print the error to logs too, run it again */ fseek(script_py, 0, SEEK_SET); /* we don't run the file, like this, because then side-effects * s = PyRun_File(script_py, pe->fname, Py_file_input, * PyModule_GetDict(PyImport_AddModule("__main__")), pe->dict); * could happen (again). Instead we parse the file again to get * the error string in the logs, for when the daemon has stderr * removed. SimpleFile run already printed to stderr, for then * this is called from unbound-checkconf or unbound -dd the user * has a nice formatted error. */ /* ignore the NULL return of _node, it is NULL due to the parse failure * that we are expecting */ (void)PyParser_SimpleParseFile(script_py, pe->fname, Py_file_input); log_py_err(); PyGILState_Release(gil); return 0; } #if PY_MAJOR_VERSION < 3 Py_XDECREF(PyFileObject); #else fclose(script_py); #endif if ((pe->func_init = PyDict_GetItemString(pe->dict, "init_standard")) == NULL) { init_standard = 0; if ((pe->func_init = PyDict_GetItemString(pe->dict, "init")) == NULL) { log_err("pythonmod: function init is missing in %s", pe->fname); PyGILState_Release(gil); return 0; } } if ((pe->func_deinit = PyDict_GetItemString(pe->dict, "deinit")) == NULL) { log_err("pythonmod: function deinit is missing in %s", pe->fname); PyGILState_Release(gil); return 0; } if ((pe->func_operate = PyDict_GetItemString(pe->dict, "operate")) == NULL) { log_err("pythonmod: function operate is missing in %s", pe->fname); PyGILState_Release(gil); return 0; } if ((pe->func_inform = PyDict_GetItemString(pe->dict, "inform_super")) == NULL) { log_err("pythonmod: function inform_super is missing in %s", pe->fname); PyGILState_Release(gil); return 0; } if (init_standard) { py_init_arg = SWIG_NewPointerObj((void*) env, SWIGTYPE_p_module_env, 0); } else { py_init_arg = SWIG_NewPointerObj((void*) env->cfg, SWIGTYPE_p_config_file, 0); } res = PyObject_CallFunction(pe->func_init, "iO", id, py_init_arg); if (PyErr_Occurred()) { log_err("pythonmod: Exception occurred in function init"); log_py_err(); Py_XDECREF(res); Py_XDECREF(py_init_arg); PyGILState_Release(gil); return 0; } Py_XDECREF(res); Py_XDECREF(py_init_arg); PyGILState_Release(gil); return 1; }
int pythonmod_init(struct module_env* env, int id) { /* Initialize module */ FILE* script_py = NULL; PyObject* py_cfg, *res; PyGILState_STATE gil; struct pythonmod_env* pe = (struct pythonmod_env*)calloc(1, sizeof(struct pythonmod_env)); if (!pe) { log_err("pythonmod: malloc failure"); return 0; } env->modinfo[id] = (void*) pe; /* Initialize module */ pe->fname = env->cfg->python_script; if(pe->fname==NULL || pe->fname[0]==0) { log_err("pythonmod: no script given."); return 0; } /* Initialize Python libraries */ if (!Py_IsInitialized()) { #if PY_MAJOR_VERSION >= 3 wchar_t progname[8]; mbstowcs(progname, "unbound", 8); #else char *progname = "unbound"; #endif Py_SetProgramName(progname); Py_NoSiteFlag = 1; Py_Initialize(); PyEval_InitThreads(); SWIG_init(); pe->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; } /* Check Python file load */ if ((script_py = fopen(pe->fname, "r")) == NULL) { log_err("pythonmod: can't open file %s for reading", pe->fname); PyGILState_Release(gil); return 0; } /* Load file */ pe->module = PyImport_AddModule("__main__"); pe->dict = PyModule_GetDict(pe->module); pe->data = Py_None; Py_INCREF(pe->data); PyModule_AddObject(pe->module, "mod_env", pe->data); /* TODO: deallocation of pe->... if an error occurs */ if (PyRun_SimpleFile(script_py, pe->fname) < 0) { log_err("pythonmod: can't parse Python script %s", pe->fname); PyGILState_Release(gil); return 0; } fclose(script_py); if ((pe->func_init = PyDict_GetItemString(pe->dict, "init")) == NULL) { log_err("pythonmod: function init is missing in %s", pe->fname); PyGILState_Release(gil); return 0; } if ((pe->func_deinit = PyDict_GetItemString(pe->dict, "deinit")) == NULL) { log_err("pythonmod: function deinit is missing in %s", pe->fname); PyGILState_Release(gil); return 0; } if ((pe->func_operate = PyDict_GetItemString(pe->dict, "operate")) == NULL) { log_err("pythonmod: function operate is missing in %s", pe->fname); PyGILState_Release(gil); return 0; } if ((pe->func_inform = PyDict_GetItemString(pe->dict, "inform_super")) == NULL) { log_err("pythonmod: function inform_super is missing in %s", pe->fname); PyGILState_Release(gil); return 0; } py_cfg = SWIG_NewPointerObj((void*) env->cfg, SWIGTYPE_p_config_file, 0); res = PyObject_CallFunction(pe->func_init, "iO", id, py_cfg); if (PyErr_Occurred()) { log_err("pythonmod: Exception occurred in function init"); PyErr_Print(); } Py_XDECREF(res); Py_XDECREF(py_cfg); PyGILState_Release(gil); return 1; }