[PATCH 9/10] rlm_python: 09-reimplement-instantiate--detach.patch

Paul P Komkoff Jr i at stingr.net
Mon Jan 30 23:26:32 CET 2006


Reimplement instantiate & detach

---
commit bc293834b5669396a77ba07a20ca9f60c9c34050
tree 462bbe5457760d3594e6d58f7a669ecca00b0dc9
parent 3af430ff7cc509f59963279178e0909b9983af3a
author <stingray at boxster64.stingr.net> Sun, 29 Jan 2006 15:57:07 +0300
committer <stingray at boxster64.stingr.net> Sun, 29 Jan 2006 15:57:07 +0300

 src/modules/rlm_python/rlm_python.c |  228 ++++++++++++++++-------------------
 1 files changed, 107 insertions(+), 121 deletions(-)

diff --git a/src/modules/rlm_python/rlm_python.c b/src/modules/rlm_python/rlm_python.c
--- a/src/modules/rlm_python/rlm_python.c
+++ b/src/modules/rlm_python/rlm_python.c
@@ -461,6 +461,30 @@ failed:
         return -1;
 }
 
+static void python_objclear(PyObject **ob) {
+        if (*ob != NULL) {
+                Py_DECREF(*ob);
+                *ob = NULL;
+        }
+}
+
+static void python_instance_clear(struct rlm_python_t *data) {
+        python_objclear(&data->pFunc_instantiate);
+        python_objclear(&data->pFunc_authorize);
+        python_objclear(&data->pFunc_authenticate);
+        python_objclear(&data->pFunc_preacct);
+        python_objclear(&data->pFunc_accounting);
+        python_objclear(&data->pFunc_checksimul);
+        python_objclear(&data->pFunc_detach);
+
+        python_objclear(&data->pModule_instantiate);
+        python_objclear(&data->pModule_authorize);
+        python_objclear(&data->pModule_authenticate);
+        python_objclear(&data->pModule_preacct);
+        python_objclear(&data->pModule_accounting);
+        python_objclear(&data->pModule_checksimul);
+        python_objclear(&data->pModule_detach);
+}
 
 /*
  *	Do any per-module initialization that is separate to each
@@ -473,81 +497,94 @@ failed:
  *	in *instance otherwise put a null pointer there.
  *
  */
-static int python_instantiate(CONF_SECTION *conf, void **instance)
-{
-    struct rlm_python_t *data;
 
-    /*
-	 *	Set up a storage area for instance data
-	 */
-    data = rad_malloc(sizeof(*data));
-    if (!data) {
-      return -1;
-    }
-    memset(data, 0, sizeof(*data));
-
-    /*
-	 *	If the configuration parameters can't be parsed, then
-	 *	fail.
-	 */
-    if (cf_section_parse(conf, data, module_config) < 0) {
-	free(data);
-	return -1;
-    }
-
-
-    /*
-     * Import user modules.
-     */
-
-    if (python_load_function(data->mod_instantiate, data->func_instantiate,
-		&data->pModule_instantiate, &data->pFunc_instantiate)==-1) {
-	/* TODO: check if we need to cleanup data */
-	return -1;
-    }
-
-    if (python_load_function(data->mod_authenticate, data->func_authenticate,
-		&data->pModule_authenticate, &data->pFunc_authenticate)==-1) {
-	/* TODO: check if we need to cleanup data */
-	return -1;
-    }
-
-    if (python_load_function(data->mod_authorize, data->func_authorize,
-		&data->pModule_authorize, &data->pFunc_authorize)==-1) {
-	/* TODO: check if we need to cleanup data */
-	return -1;
-    }
-
-    if (python_load_function(data->mod_preacct, data->func_preacct,
-		&data->pModule_preacct, &data->pFunc_preacct)==-1) {
-	/* TODO: check if we need to cleanup data */
-	return -1;
-    }
-
-    if (python_load_function(data->mod_accounting, data->func_accounting,
-		&data->pModule_accounting, &data->pFunc_accounting)==-1) {
-	/* TODO: check if we need to cleanup data */
-	return -1;
-    }
-
-    if (python_load_function(data->mod_checksimul, data->func_checksimul,
-		&data->pModule_checksimul, &data->pFunc_checksimul)==-1) {
-	/* TODO: check if we need to cleanup data */
-	return -1;
-    }
-
-    if (python_load_function(data->mod_detach, data->func_detach,
-		&data->pModule_detach, &data->pFunc_detach)==-1) {
-	/* TODO: check if we need to cleanup data */
-	return -1;
-    }
+static int python_instantiate(CONF_SECTION *conf, void **instance) {
+        struct rlm_python_t    *data = NULL;
+
+        /*
+         *      Set up a storage area for instance data
+         */
+        if ((data = malloc(sizeof(*data))) == NULL)
+                return -1;
+        bzero(data, sizeof(*data));
+
+        /*
+         *      If the configuration parameters can't be parsed, then
+         *      fail.
+         */
+        if (cf_section_parse(conf, data, module_config) < 0) {
+                free(data);
+                return -1;
+        }
+
+        /*
+         * Import user modules.
+         */
+        if (python_load_function(data->mod_instantiate,
+                                data->func_instantiate,
+                                &data->pModule_instantiate,
+                                &data->pFunc_instantiate) < 0)
+                goto failed;
+
+        if (python_load_function(data->mod_authenticate,
+                                data->func_authenticate,
+                                &data->pModule_authenticate,
+                                &data->pFunc_authenticate) < 0)
+                goto failed;
+
+        if (python_load_function(data->mod_authorize,
+                                data->func_authorize,
+                                &data->pModule_authorize,
+                                &data->pFunc_authorize) < 0)
+                goto failed;
+
+        if (python_load_function(data->mod_preacct,
+                                data->func_preacct,
+                                &data->pModule_preacct,
+                                &data->pFunc_preacct) < 0)
+                goto failed;
+
+        if (python_load_function(data->mod_accounting,
+                                data->func_accounting,
+                                &data->pModule_accounting,
+                                &data->pFunc_accounting) < 0)
+                goto failed;
+
+        if (python_load_function(data->mod_checksimul,
+                                data->func_checksimul,
+                                &data->pModule_checksimul,
+                                &data->pFunc_checksimul) < 0)
+                goto failed;
+
+        if (python_load_function(data->mod_detach,
+                                data->func_detach,
+                                &data->pModule_detach,
+                                &data->pFunc_detach) < 0)
+                goto failed;
+
+        *instance = data;
+        /* Call the instantiate function.  No request.  Use the return value. */
+
+        return python_function(NULL, data->pFunc_instantiate, "instantiate");
+failed:
+        python_error();
+        python_instance_clear(data);
+        return -1;
+}
+
+static int python_detach(void *instance) {
+        struct rlm_python_t    *data = (struct rlm_python_t *) instance;
+        int             ret;
 
-    *instance=data;
+        ret = python_function(NULL, data->pFunc_detach, "detach");
 
-    /* Call the instantiate function.  No request.  Use the return value. */
-    return python_function(NULL, data->pFunc_instantiate, "instantiate");
+        python_instance_clear(data);
+
+        free(data);
+        return ret;
 }
 
+
 /* Wrapper functions */
 static int python_authorize(void *instance, REQUEST *request)
 {
@@ -589,57 +626,6 @@ static int python_checksimul(void *insta
 }
 
 
-static int python_detach(void *instance)
-{
-    int return_value;
-
-    /* Default return value is failure */
-    return_value = -1;
-
-    if (((struct rlm_python_t *)instance)->pFunc_detach &&
-	PyCallable_Check(((struct rlm_python_t *)instance)->pFunc_detach)) {
-
-	PyObject *pArgs, *pValue;
-
-	/* call the function with an empty tuple */
-
-	pArgs = PyTuple_New(0);
-	pValue = PyObject_CallObject(((struct rlm_python_t *)instance)->pFunc_detach,
-				     pArgs);
-
-	if (pValue == NULL) {
-	    python_error();
-	    return -1;
-	}
-	else {
-	    if (!PyInt_Check(pValue)) {
-		radlog(L_ERR, "detach: return value not an integer");
-	    }
-	    else {
-		return_value = PyInt_AsLong(pValue);
-	    }
-	}
-
-	/* Decrease reference counts for the argument and return tuple */
-	Py_DECREF(pArgs);
-	Py_DECREF(pValue);
-    }
-
-    free(instance);
-
-#if 0
-    /* xxx test delete module object so it will be reloaded later.
-     * xxx useless since we can't SIGHUP reliably, anyway.
-     */
-    PyObject_Del(((struct rlm_python_t *)instance)->pModule_accounting);
-#endif
-
-    radlog(L_DBG, "python_detach done");
-
-    /* Return the specified by the Python module */
-    return return_value;
-}
-
 /*
  *	The module name should be the only globally exported symbol.
  *	That is, everything else should be 'static'.
-- 
Paul P 'Stingray' Komkoff Jr // http://stingr.net/key <- my pgp key
 This message represents the official view of the voices in my head



More information about the Freeradius-Devel mailing list