]> git.ipfire.org Git - thirdparty/freeradius-server.git/commitdiff
Use macros to reduce boilerplate
authorNick Porter <nick@portercomputing.co.uk>
Mon, 7 Oct 2024 08:45:36 +0000 (09:45 +0100)
committerNick Porter <nick@portercomputing.co.uk>
Fri, 11 Oct 2024 09:48:18 +0000 (10:48 +0100)
src/modules/rlm_sql/drivers/rlm_sql_mysql/rlm_sql_mysql.c
src/modules/rlm_sql/drivers/rlm_sql_postgresql/rlm_sql_postgresql.c
src/modules/rlm_sql/drivers/rlm_sql_sqlite/rlm_sql_sqlite.c
src/modules/rlm_sql/rlm_sql_trunk.h [new file with mode: 0644]

index 8678471e81d00e659aa336aa1454589adfef37bc..cfb75d65336b00f055a7837f769dcaa870e2b591 100644 (file)
@@ -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)
 {
index a268845590dcf21ba020d0587fdbfe7058aa8ea2..c71c00e4688da84972c580d4dee1b22258e8559a 100644 (file)
@@ -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)
 {
index b1c5e86e651160cb0a92c697c1efa9002b7a70b0..347e5148ee602e35e48a37e8fd93299b1bf03e6b 100644 (file)
@@ -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)
@@ -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 (file)
index 0000000..9220009
--- /dev/null
@@ -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); \
+}