From: Nick Porter Date: Mon, 7 Oct 2024 08:45:36 +0000 (+0100) Subject: Use macros to reduce boilerplate X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=efa885ee314982373fc3b49aad19efd3caf20dce;p=thirdparty%2Ffreeradius-server.git Use macros to reduce boilerplate --- diff --git a/src/modules/rlm_sql/drivers/rlm_sql_mysql/rlm_sql_mysql.c b/src/modules/rlm_sql/drivers/rlm_sql_mysql/rlm_sql_mysql.c index 8678471e81d..cfb75d65336 100644 --- a/src/modules/rlm_sql/drivers/rlm_sql_mysql/rlm_sql_mysql.c +++ b/src/modules/rlm_sql/drivers/rlm_sql_mysql/rlm_sql_mysql.c @@ -50,6 +50,7 @@ DIAG_ON(strict-prototypes) #endif #include "rlm_sql.h" +#include "rlm_sql_trunk.h" typedef enum { SERVER_WARNINGS_AUTO = 0, @@ -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) @@ -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) { diff --git a/src/modules/rlm_sql/drivers/rlm_sql_postgresql/rlm_sql_postgresql.c b/src/modules/rlm_sql/drivers/rlm_sql_postgresql/rlm_sql_postgresql.c index a268845590d..c71c00e4688 100644 --- a/src/modules/rlm_sql/drivers/rlm_sql_postgresql/rlm_sql_postgresql.c +++ b/src/modules/rlm_sql/drivers/rlm_sql_postgresql/rlm_sql_postgresql.c @@ -51,6 +51,7 @@ RCSID("$Id$") #include "config.h" #include "rlm_sql.h" +#include "rlm_sql_trunk.h" #ifndef NAMEDATALEN # define NAMEDATALEN 64 @@ -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) @@ -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) { diff --git a/src/modules/rlm_sql/drivers/rlm_sql_sqlite/rlm_sql_sqlite.c b/src/modules/rlm_sql/drivers/rlm_sql_sqlite/rlm_sql_sqlite.c index b1c5e86e651..347e5148ee6 100644 --- a/src/modules/rlm_sql/drivers/rlm_sql_sqlite/rlm_sql_sqlite.c +++ b/src/modules/rlm_sql/drivers/rlm_sql_sqlite/rlm_sql_sqlite.c @@ -34,6 +34,7 @@ RCSID("$Id$") #include #include "rlm_sql.h" +#include "rlm_sql_trunk.h" #include "config.h" #define BOOTSTRAP_MAX (1048576 * 10) @@ -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, @@ -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) diff --git a/src/modules/rlm_sql/rlm_sql_trunk.h b/src/modules/rlm_sql/rlm_sql_trunk.h new file mode 100644 index 00000000000..92200096d06 --- /dev/null +++ b/src/modules/rlm_sql/rlm_sql_trunk.h @@ -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); \ +}