Pass a threadsafe ctx into fr_connection_pool create callback
[freeradius.git] / src / modules / rlm_couchbase / mod.c
index f963e2e..5f9d606 100644 (file)
@@ -28,14 +28,27 @@ RCSID("$Id$");
 #include <freeradius-devel/radiusd.h>
 
 #include <libcouchbase/couchbase.h>
-#include <json/json.h>
+#include <json.h>
 
 #include "mod.h"
 #include "couchbase.h"
 #include "jsonc_missing.h"
 
+/* free couchbase instance handle and any additional context memory */
+static int _mod_conn_free(rlm_couchbase_handle_t *chandle)
+{
+       lcb_t cb_inst = chandle->handle;                /* couchbase instance */
+
+       /* destroy/free couchbase instance */
+       lcb_destroy(cb_inst);
+
+       /* return */
+       return 0;
+}
+
 /* create new connection pool handle */
-void *mod_conn_create(void *instance) {
+void *mod_conn_create(TALLOC_CTX *ctx, void *instance)
+{
        rlm_couchbase_t *inst = instance;           /* module instance pointer */
        rlm_couchbase_handle_t *chandle = NULL;     /* connection handle pointer */
        cookie_t *cookie = NULL;                    /* couchbase cookie */
@@ -43,7 +56,7 @@ void *mod_conn_create(void *instance) {
        lcb_error_t cb_error = LCB_SUCCESS;         /* couchbase error status */
 
        /* create instance */
-       cb_inst = couchbase_init_connection(inst->server, inst->bucket, inst->pass);
+       cb_inst = couchbase_init_connection(inst->server, inst->bucket, inst->password);
 
        /* check couchbase instance status */
        if ((cb_error = lcb_get_last_error(cb_inst)) != LCB_SUCCESS) {
@@ -55,7 +68,9 @@ void *mod_conn_create(void *instance) {
        }
 
        /* allocate memory for couchbase connection instance abstraction */
-       chandle = talloc_zero(inst, rlm_couchbase_handle_t);
+       chandle = talloc_zero(ctx, rlm_couchbase_handle_t);
+       talloc_set_destructor(chandle, _mod_conn_free);
+
        cookie = talloc_zero(chandle, cookie_t);
 
        /* initialize cookie error holder */
@@ -70,7 +85,8 @@ void *mod_conn_create(void *instance) {
 }
 
 /* verify valid couchbase connection handle */
-int mod_conn_alive(UNUSED void *instance, void *handle) {
+int mod_conn_alive(UNUSED void *instance, void *handle)
+{
        rlm_couchbase_handle_t *chandle = handle;   /* connection handle pointer */
        lcb_t cb_inst = chandle->handle;            /* couchbase instance */
        lcb_error_t cb_error = LCB_SUCCESS;         /* couchbase error status */
@@ -88,23 +104,9 @@ int mod_conn_alive(UNUSED void *instance, void *handle) {
        return true;
 }
 
-/* free couchbase instance handle and any additional context memory */
-int mod_conn_delete(UNUSED void *instance, void *handle) {
-       rlm_couchbase_handle_t *chandle = handle;       /* connection instance handle */
-       lcb_t cb_inst = chandle->handle;                /* couchbase instance */
-
-       /* destroy/free couchbase instance */
-       lcb_destroy(cb_inst);
-
-       /* free handle */
-       talloc_free(chandle);
-
-       /* return */
-       return true;
-}
-
 /* build json object for mapping radius attributes to json elements */
-int mod_build_attribute_element_map(CONF_SECTION *conf, void *instance) {
+int mod_build_attribute_element_map(CONF_SECTION *conf, void *instance)
+{
        rlm_couchbase_t *inst = instance;   /* our module instance */
        CONF_SECTION *cs;                   /* module config section */
        CONF_ITEM *ci;                      /* config item */
@@ -161,7 +163,8 @@ int mod_build_attribute_element_map(CONF_SECTION *conf, void *instance) {
 }
 
 /* map free radius attribute to user defined json element name */
-int mod_attribute_to_element(const char *name, json_object *map, void *buf) {
+int mod_attribute_to_element(const char *name, json_object *map, void *buf)
+{
        json_object *jval;  /* json object values */
 
        /* clear buffer */
@@ -196,7 +199,8 @@ int mod_attribute_to_element(const char *name, json_object *map, void *buf) {
 /* inject value pairs into given request
  * that are defined in the passed json object
  */
-void *mod_json_object_to_value_pairs(json_object *json, const char *section, REQUEST *request) {
+void *mod_json_object_to_value_pairs(json_object *json, const char *section, REQUEST *request)
+{
        json_object *jobj, *jval, *jop;     /* json object pointers */
        TALLOC_CTX *ctx;                    /* talloc context for pairmake */
        VALUE_PAIR *vp, **ptr;              /* value pair and value pair pointer for pairmake */
@@ -284,7 +288,8 @@ void *mod_json_object_to_value_pairs(json_object *json, const char *section, REQ
 /* convert freeradius value/pair to json object
  * basic structure taken from freeradius function
  * vp_prints_value_json in src/lib/print.c */
-json_object *mod_value_pair_to_json_object(REQUEST *request, VALUE_PAIR *vp) {
+json_object *mod_value_pair_to_json_object(REQUEST *request, VALUE_PAIR *vp)
+{
        char value[255];    /* radius attribute value */
 
        /* add this attribute/value pair to our json output */
@@ -355,7 +360,8 @@ json_object *mod_value_pair_to_json_object(REQUEST *request, VALUE_PAIR *vp) {
 }
 
 /* check current value of start timestamp in json body and update if needed */
-int mod_ensure_start_timestamp(json_object *json, VALUE_PAIR *vps) {
+int mod_ensure_start_timestamp(json_object *json, VALUE_PAIR *vps)
+{
        json_object *jval;      /* json object value */
        struct tm tm;           /* struct to hold event time */
        time_t ts = 0;          /* values to hold time in seconds */