Pass a threadsafe ctx into fr_connection_pool create callback
[freeradius.git] / src / modules / rlm_redis / rlm_redis.c
index 72e558c..f07d494 100644 (file)
@@ -30,24 +30,17 @@ RCSID("$Id$")
 #include "rlm_redis.h"
 
 static const CONF_PARSER module_config[] = {
-       { "hostname", PW_TYPE_STRING_PTR | PW_TYPE_DEPRECATED,
-         offsetof(REDIS_INST, hostname), NULL, NULL},
-       { "server", PW_TYPE_STRING_PTR | PW_TYPE_REQUIRED,
-         offsetof(REDIS_INST, hostname), NULL, NULL},
-       { "port", PW_TYPE_INTEGER,
-         offsetof(REDIS_INST, port), NULL, "6379"},
-       { "database", PW_TYPE_INTEGER,
-         offsetof(REDIS_INST, database), NULL, "0"},
-       { "password", PW_TYPE_STRING_PTR,
-         offsetof(REDIS_INST, password), NULL, NULL},
+       { "hostname", FR_CONF_OFFSET(PW_TYPE_STRING | PW_TYPE_DEPRECATED, REDIS_INST, hostname), NULL },
+       { "server", FR_CONF_OFFSET(PW_TYPE_STRING | PW_TYPE_REQUIRED, REDIS_INST, hostname), NULL },
+       { "port", FR_CONF_OFFSET(PW_TYPE_SHORT, REDIS_INST, port), "6379" },
+       { "database", FR_CONF_OFFSET(PW_TYPE_INTEGER, REDIS_INST, database), "0" },
+       { "password", FR_CONF_OFFSET(PW_TYPE_STRING | PW_TYPE_SECRET, REDIS_INST, password), NULL },
 
        { NULL, -1, 0, NULL, NULL} /* end the list */
 };
 
-static int mod_conn_delete(UNUSED void *instance, void *handle)
+static int _mod_conn_free(REDISSOCK *dissocket)
 {
-       REDISSOCK *dissocket = handle;
-
        redisFree(dissocket->conn);
 
        if (dissocket->reply) {
@@ -55,29 +48,27 @@ static int mod_conn_delete(UNUSED void *instance, void *handle)
                dissocket->reply = NULL;
        }
 
-       talloc_free(dissocket);
-       return 1;
+       return 0;
 }
 
-static void *mod_conn_create(void *ctx)
+static void *mod_conn_create(TALLOC_CTX *ctx, void *instance)
 {
-       REDIS_INST *inst = ctx;
+       REDIS_INST *inst = instance;
        REDISSOCK *dissocket = NULL;
        redisContext *conn;
+       redisReply *reply = NULL;
        char buffer[1024];
 
        conn = redisConnect(inst->hostname, inst->port);
        if (conn->err) return NULL;
 
        if (inst->password) {
-               redisReply *reply = NULL;
-
                snprintf(buffer, sizeof(buffer), "AUTH %s", inst->password);
 
                reply = redisCommand(conn, buffer);
                if (!reply) {
-                       ERROR("rlm_redis (%s): Failed to run AUTH",
-                              inst->xlat_name);
+                       ERROR("rlm_redis (%s): Failed to run AUTH", inst->xlat_name);
+
                do_close:
                        if (reply) freeReplyObject(reply);
                        redisFree(conn);
@@ -102,8 +93,6 @@ static void *mod_conn_create(void *ctx)
        }
 
        if (inst->database) {
-               redisReply *reply = NULL;
-
                snprintf(buffer, sizeof(buffer), "SELECT %d", inst->database);
 
                reply = redisCommand(conn, buffer);
@@ -131,8 +120,9 @@ static void *mod_conn_create(void *ctx)
                }
        }
 
-       dissocket = talloc_zero(inst, REDISSOCK);
+       dissocket = talloc_zero(ctx, REDISSOCK);
        dissocket->conn = conn;
+       talloc_set_destructor(dissocket, _mod_conn_free);
 
        return dissocket;
 }
@@ -146,12 +136,7 @@ static ssize_t redis_xlat(void *instance, REQUEST *request, char const *fmt, cha
        char buffer[21];
 
        dissocket = fr_connection_get(inst->pool);
-       if (!dissocket) {
-               ERROR("rlm_redis (%s): redis_get_socket() failed",
-                      inst->xlat_name);
-
-               return -1;
-       }
+       if (!dissocket) return -1;
 
        /* Query failed for some reason, release socket and return */
        if (rlm_redis_query(&dissocket, inst, fmt, request) < 0) {
@@ -184,13 +169,13 @@ static ssize_t redis_xlat(void *instance, REQUEST *request, char const *fmt, cha
                ret = -1;
                goto release;
        }
-       
+
        strlcpy(out, buffer_ptr, freespace);
 
 release:
        rlm_redis_finish_query(dissocket);
        fr_connection_release(inst->pool, dissocket);
-       
+
        return ret;
 }
 
@@ -243,7 +228,7 @@ int rlm_redis_query(REDISSOCK **dissocket_p, REDIS_INST *inst,
 
                dissocket->reply = redisCommand(dissocket->conn, query);
                if (!dissocket->reply) {
-                       RERROR("Failed after re-connect", inst->xlat_name);
+                       RERROR("Failed after re-connect");
                        fr_connection_del(inst->pool, dissocket);
                        goto error;
                }
@@ -252,7 +237,7 @@ int rlm_redis_query(REDISSOCK **dissocket_p, REDIS_INST *inst,
        }
 
        if (dissocket->reply->type == REDIS_REPLY_ERROR) {
-               RERROR("Query failed, %s", inst->xlat_name, query);
+               RERROR("Query failed, %s", query);
                return -1;
        }
 
@@ -275,16 +260,23 @@ int rlm_redis_finish_query(REDISSOCK *dissocket)
 
 static int mod_instantiate(CONF_SECTION *conf, void *instance)
 {
+       static bool version_done;
+
        REDIS_INST *inst = instance;
 
+       if (!version_done) {
+               version_done = true;
+
+               INFO("rlm_redis: libhiredis version: %i.%i.%i", HIREDIS_MAJOR, HIREDIS_MINOR, HIREDIS_PATCH);
+       }
+
        inst->xlat_name = cf_section_name2(conf);
 
-       if (!inst->xlat_name)
-               inst->xlat_name = cf_section_name1(conf);
+       if (!inst->xlat_name) inst->xlat_name = cf_section_name1(conf);
 
        xlat_register(inst->xlat_name, redis_xlat, NULL, inst); /* FIXME! */
 
-       inst->pool = fr_connection_pool_init(conf, inst, mod_conn_create, NULL, mod_conn_delete, NULL);
+       inst->pool = fr_connection_pool_init(conf, inst, mod_conn_create, NULL, NULL, NULL);
        if (!inst->pool) {
                return -1;
        }