From: Nick Porter Date: Tue, 16 Jul 2024 11:21:38 +0000 (+0100) Subject: Use a helper macro for SQL trunk notify callback to avoid boilerplate X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=d10067283cdb32acade6de7d2ab62bd4f037ffb5;p=thirdparty%2Ffreeradius-server.git Use a helper macro for SQL trunk notify callback to avoid boilerplate --- diff --git a/src/lib/server/trunk.h b/src/lib/server/trunk.h index 6c6db3e6698..7f6ce09baa7 100644 --- a/src/lib/server/trunk.h +++ b/src/lib/server/trunk.h @@ -937,6 +937,54 @@ bool trunk_request_search(trunk_request_t *treq, void *ptr); #undef _CONST +/** Helper macro for building generic trunk notify callback + * + * @param _name of the callback function to build + * @param _type of the conn->h handle. Needs to contain an fd element. + */ +#define TRUNK_NOTIFY_FUNC(_name, _type) \ +static void _conn_writeable(UNUSED fr_event_list_t *el, UNUSED int fd, UNUSED int flags, void *uctx) \ +{ \ + trunk_connection_t *tconn = talloc_get_type_abort(uctx, trunk_connection_t); \ + trunk_connection_signal_writable(tconn); \ +} \ +static void _conn_readable(UNUSED fr_event_list_t *el, UNUSED int fd, UNUSED int flags, void *uctx) \ +{ \ + trunk_connection_t *tconn = talloc_get_type_abort(uctx, trunk_connection_t); \ + trunk_connection_signal_readable(tconn); \ +} \ +static void _conn_error(UNUSED fr_event_list_t *el, UNUSED int fd, UNUSED int flags, int fd_errno, void *uctx) \ +{ \ + trunk_connection_t *tconn = talloc_get_type_abort(uctx, trunk_connection_t); \ + ERROR("%s - Connection failed: %s", tconn->conn->name, fr_syserror(fd_errno)); \ + connection_signal_reconnect(tconn->conn, CONNECTION_FAILED); \ +} \ +static void _name(trunk_connection_t *tconn, connection_t *conn, \ + fr_event_list_t *el, trunk_connection_event_t notify_on, UNUSED void *uctx) \ +{ \ + _type *c = talloc_get_type_abort(conn->h, _type); \ + fr_event_fd_cb_t read_fn = NULL, write_fn = NULL; \ + switch (notify_on) { \ + case TRUNK_CONN_EVENT_NONE: \ + fr_event_fd_delete(el, c->fd, FR_EVENT_FILTER_IO); \ + return; \ + case TRUNK_CONN_EVENT_READ: \ + read_fn = _conn_readable; \ + break; \ + case TRUNK_CONN_EVENT_WRITE: \ + write_fn = _conn_writeable; \ + break; \ + case TRUNK_CONN_EVENT_BOTH: \ + read_fn = _conn_readable; \ + write_fn = _conn_writeable; \ + break; \ + } \ + if (fr_event_fd_insert(c, NULL, el, c->fd, read_fn, write_fn, _conn_error, tconn) <0) { \ + PERROR("Failed inserting FD event"); \ + trunk_connection_signal_reconnect(tconn, CONNECTION_FAILED); \ + } \ +} + #ifdef __cplusplus } #endif 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 4043b4070f0..76213d7b461 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 @@ -827,25 +827,6 @@ 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); } -static void sql_conn_writable(UNUSED fr_event_list_t *el, UNUSED int fd, UNUSED int flags, void *uctx) -{ - trunk_connection_t *tconn = talloc_get_type_abort(uctx, trunk_connection_t); - trunk_connection_signal_writable(tconn); -} - -static void sql_conn_readable(UNUSED fr_event_list_t *el, UNUSED int fd, UNUSED int flags, void *uctx) -{ - trunk_connection_t *tconn = talloc_get_type_abort(uctx, trunk_connection_t); - trunk_connection_signal_readable(tconn); -} - -static void sql_conn_error(UNUSED fr_event_list_t *el, UNUSED int fd, UNUSED int flags, int fd_errno, void *uctx) -{ - trunk_connection_t *tconn = talloc_get_type_abort(uctx, trunk_connection_t); - ERROR("%s - Connection failed: %s", tconn->conn->name, fr_syserror(fd_errno)); - connection_signal_reconnect(tconn->conn, CONNECTION_FAILED); -} - /** Allocate an SQL trunk connection * * @param[in] tconn Trunk handle. @@ -875,37 +856,7 @@ static connection_t *sql_trunk_connection_alloc(trunk_connection_t *tconn, fr_ev return conn; } -static void sql_trunk_connection_notify(trunk_connection_t *tconn, connection_t *conn, - fr_event_list_t *el, - trunk_connection_event_t notify_on, UNUSED void *uctx) -{ - rlm_sql_mysql_conn_t *sql_conn = talloc_get_type_abort(conn->h, rlm_sql_mysql_conn_t); - fr_event_fd_cb_t read_fn = NULL, write_fn = NULL; - - switch (notify_on) { - case TRUNK_CONN_EVENT_NONE: - fr_event_fd_delete(el, sql_conn->fd, FR_EVENT_FILTER_IO); - return; - - case TRUNK_CONN_EVENT_READ: - read_fn = sql_conn_readable; - break; - - case TRUNK_CONN_EVENT_WRITE: - write_fn = sql_conn_writable; - break; - - case TRUNK_CONN_EVENT_BOTH: - read_fn = sql_conn_readable; - write_fn = sql_conn_writable; - break; - } - - if (fr_event_fd_insert(sql_conn, NULL, el, sql_conn->fd, read_fn, write_fn, sql_conn_error, tconn) < 0) { - PERROR("Failed inserting FD event"); - trunk_connection_signal_reconnect(tconn, CONNECTION_FAILED); - } -} +TRUNK_NOTIFY_FUNC(sql_trunk_connection_notify, rlm_sql_mysql_conn_t) static void sql_trunk_request_mux(UNUSED fr_event_list_t *el, trunk_connection_t *tconn, connection_t *conn, UNUSED void *uctx)