Skip to content

Commit

Permalink
Use macros to reduce boilerplate
Browse files Browse the repository at this point in the history
  • Loading branch information
ndptech committed Oct 11, 2024
1 parent 52a11db commit efa885e
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 123 deletions.
51 changes: 4 additions & 47 deletions src/modules/rlm_sql/drivers/rlm_sql_mysql/rlm_sql_mysql.c
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ DIAG_ON(strict-prototypes)
#endif

#include "rlm_sql.h"
#include "rlm_sql_trunk.h"

typedef enum {
SERVER_WARNINGS_AUTO = 0,
Expand Down Expand Up @@ -848,35 +849,7 @@ static size_t sql_escape_func(UNUSED request_t *request, char *out, size_t outle
return mysql_real_escape_string(&conn->db, out, in, inlen);
}

/** Allocate an SQL trunk connection
*
* @param[in] tconn Trunk handle.
* @param[in] el Event list which will be used for I/O and timer events.
* @param[in] conn_conf Configuration of the connection.
* @param[in] log_prefix What to prefix log messages with.
* @param[in] uctx User context passed to trunk_alloc.
*/
CC_NO_UBSAN(function) /* UBSAN: false positive - public vs private connection_t trips --fsanitize=function*/
static connection_t *sql_trunk_connection_alloc(trunk_connection_t *tconn, fr_event_list_t *el,
connection_conf_t const *conn_conf,
char const *log_prefix, void *uctx)
{
connection_t *conn;
rlm_sql_thread_t *thread = talloc_get_type_abort(uctx, rlm_sql_thread_t);

conn = connection_alloc(tconn, el,
&(connection_funcs_t){
.init = _sql_connection_init,
.close = _sql_connection_close
},
conn_conf, log_prefix, thread->inst);
if (!conn) {
PERROR("Failed allocating state handler for new SQL connection");
return NULL;
}

return conn;
}
SQL_TRUNK_CONNECTION_ALLOC

TRUNK_NOTIFY_FUNC(sql_trunk_connection_notify, rlm_sql_mysql_conn_t)

Expand Down Expand Up @@ -1076,24 +1049,8 @@ static void sql_request_cancel_mux(UNUSED fr_event_list_t *el, trunk_connection_
}
}

static void sql_request_fail(request_t *request, void *preq, UNUSED void *rctx,
UNUSED trunk_request_state_t state, UNUSED void *uctx)
{
fr_sql_query_t *query_ctx = talloc_get_type_abort(preq, fr_sql_query_t);

query_ctx->treq = NULL;
query_ctx->rcode = RLM_SQL_ERROR;

if (request) unlang_interpret_mark_runnable(request);
}

static unlang_action_t sql_query_resume(rlm_rcode_t *p_result, UNUSED int *priority, UNUSED request_t *request, void *uctx)
{
fr_sql_query_t *query_ctx = talloc_get_type_abort(uctx, fr_sql_query_t);

if (query_ctx->rcode == RLM_SQL_OK) RETURN_MODULE_OK;
RETURN_MODULE_FAIL;
}
SQL_QUERY_FAIL
SQL_QUERY_RESUME

static unlang_action_t sql_select_query_resume(rlm_rcode_t *p_result, UNUSED int *priority, UNUSED request_t *request, void *uctx)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ RCSID("$Id$")

#include "config.h"
#include "rlm_sql.h"
#include "rlm_sql_trunk.h"

#ifndef NAMEDATALEN
# define NAMEDATALEN 64
Expand Down Expand Up @@ -346,27 +347,7 @@ static void _sql_connection_close(fr_event_list_t *el, void *h, UNUSED void *uct
talloc_free(h);
}

CC_NO_UBSAN(function) /* UBSAN: false positive - public vs private connection_t trips --fsanitize=function*/
static connection_t *sql_trunk_connection_alloc(trunk_connection_t *tconn, fr_event_list_t *el,
connection_conf_t const *conn_conf,
char const *log_prefix, void *uctx)
{
connection_t *conn;
rlm_sql_thread_t *thread = talloc_get_type_abort(uctx, rlm_sql_thread_t);

conn = connection_alloc(tconn, el,
&(connection_funcs_t) {
.init = _sql_connection_init,
.close = _sql_connection_close
},
conn_conf, log_prefix, thread->inst);
if (!conn) {
PERROR("Failed allocating state handler for new SQL connection");
return NULL;
}

return conn;
}
SQL_TRUNK_CONNECTION_ALLOC

TRUNK_NOTIFY_FUNC(sql_trunk_connection_notify, rlm_sql_postgres_conn_t)

Expand Down Expand Up @@ -554,25 +535,8 @@ static void sql_request_cancel_mux(UNUSED fr_event_list_t *el, trunk_connection_
}
}

static void sql_request_fail(request_t *request, void *preq, UNUSED void *rctx,
UNUSED trunk_request_state_t state, UNUSED void *uctx)
{
fr_sql_query_t *query_ctx = talloc_get_type_abort(preq, fr_sql_query_t);

query_ctx->treq = NULL;
query_ctx->rcode = RLM_SQL_ERROR;

if (request) unlang_interpret_mark_runnable(request);
}

static unlang_action_t sql_query_resume(rlm_rcode_t *p_result, UNUSED int *priority, UNUSED request_t *request, void *uctx)
{
fr_sql_query_t *query_ctx = talloc_get_type_abort(uctx, fr_sql_query_t);

if (query_ctx->rcode != RLM_SQL_OK) RETURN_MODULE_FAIL;

RETURN_MODULE_OK;
}
SQL_QUERY_FAIL
SQL_QUERY_RESUME

static sql_rcode_t sql_fields(char const **out[], fr_sql_query_t *query_ctx, UNUSED rlm_sql_config_t const *config)
{
Expand Down
39 changes: 3 additions & 36 deletions src/modules/rlm_sql/drivers/rlm_sql_sqlite/rlm_sql_sqlite.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ RCSID("$Id$")
#include <sqlite3.h>

#include "rlm_sql.h"
#include "rlm_sql_trunk.h"
#include "config.h"

#define BOOTSTRAP_MAX (1048576 * 10)
Expand Down Expand Up @@ -603,35 +604,7 @@ static int sql_affected_rows(fr_sql_query_t *query_ctx,
return -1;
}

/** Allocate an SQL trunk connection
*
* @param[in] tconn Trunk handle.
* @param[in] el Event list which will be used for I/O and timer events.
* @param[in] conn_conf Configuration of the connection.
* @param[in] log_prefix What to prefix log messages with.
* @param[in] uctx User context passed to trunk_alloc.
*/
CC_NO_UBSAN(function) /* UBSAN: false positive - public vs private connection_t trips --fsanitize=function*/
static connection_t *sql_trunk_connection_alloc(trunk_connection_t *tconn, fr_event_list_t *el,
connection_conf_t const *conn_conf,
char const *log_prefix, void *uctx)
{
connection_t *conn;
rlm_sql_thread_t *thread = talloc_get_type_abort(uctx, rlm_sql_thread_t);

conn = connection_alloc(tconn, el,
&(connection_funcs_t){
.init = _sql_connection_init,
.close = _sql_connection_close
},
conn_conf, log_prefix, thread->inst);
if (!conn) {
PERROR("Failed allocating state handler for new SQL connection");
return NULL;
}

return conn;
}
SQL_TRUNK_CONNECTION_ALLOC

CC_NO_UBSAN(function) /* UBSAN: false positive - public vs private connection_t trips --fsanitize=function*/
static void sql_trunk_request_mux(UNUSED fr_event_list_t *el, trunk_connection_t *tconn,
Expand Down Expand Up @@ -671,13 +644,7 @@ static void sql_trunk_request_mux(UNUSED fr_event_list_t *el, trunk_connection_t
trunk_request_signal_reapable(treq);
}

static unlang_action_t sql_query_resume(rlm_rcode_t *p_result, UNUSED int *priority, UNUSED request_t *request, void *uctx)
{
fr_sql_query_t *query_ctx = talloc_get_type_abort(uctx, fr_sql_query_t);

if (query_ctx->rcode == RLM_SQL_OK) RETURN_MODULE_OK;
RETURN_MODULE_FAIL;
}
SQL_QUERY_RESUME

static void sql_request_fail(UNUSED request_t *request, void *preq, UNUSED void *rctx,
UNUSED trunk_request_state_t state, UNUSED void *uctx)
Expand Down
72 changes: 72 additions & 0 deletions src/modules/rlm_sql/rlm_sql_trunk.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
#pragma once
/*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or (at
* your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
*/

/**
* $Id$
* @file rlm_sql_trunk.h
* @brief Macros to reduce boilerplate in trunk SQL drivers
*
* @copyright 2024 The FreeRADIUS server project
*/
RCSIDH(rlm_sql_trunk_h, "$Id$")

/** Allocate an SQL trunk connection
*
* @param[in] tconn Trunk handle.
* @param[in] el Event list which will be used for I/O and timer events.
* @param[in] conn_conf Configuration of the connection.
* @param[in] log_prefix What to prefix log messages with.
* @param[in] uctx User context passed to trunk_alloc.
*/
#define SQL_TRUNK_CONNECTION_ALLOC \
CC_NO_UBSAN(function) /* UBSAN: false positive - public vs private connection_t trips --fsanitize=function */ \
static connection_t *sql_trunk_connection_alloc(trunk_connection_t *tconn, fr_event_list_t *el, \
connection_conf_t const *conn_conf, \
char const *log_prefix, void *uctx) \
{ \
connection_t *conn; \
rlm_sql_thread_t *thread = talloc_get_type_abort(uctx, rlm_sql_thread_t); \
conn = connection_alloc(tconn, el, \
&(connection_funcs_t){ \
.init = _sql_connection_init, \
.close = _sql_connection_close \
}, \
conn_conf, log_prefix, thread->inst); \
if (!conn) { \
PERROR("Failed allocating state handler for new SQL connection"); \
return NULL; \
} \
return conn; \
}

#define SQL_QUERY_RESUME \
static unlang_action_t sql_query_resume(rlm_rcode_t *p_result, UNUSED int *priority, UNUSED request_t *request, void *uctx) \
{ \
fr_sql_query_t *query_ctx = talloc_get_type_abort(uctx, fr_sql_query_t); \
if (query_ctx->rcode != RLM_SQL_OK) RETURN_MODULE_FAIL; \
RETURN_MODULE_OK; \
}

#define SQL_QUERY_FAIL \
static void sql_request_fail(request_t *request, void *preq, UNUSED void *rctx, \
UNUSED trunk_request_state_t state, UNUSED void *uctx) \
{ \
fr_sql_query_t *query_ctx = talloc_get_type_abort(preq, fr_sql_query_t); \
query_ctx->treq = NULL; \
query_ctx->rcode = RLM_SQL_ERROR; \
if (request) unlang_interpret_mark_runnable(request); \
}

0 comments on commit efa885e

Please sign in to comment.