]> git.ipfire.org Git - thirdparty/freeradius-server.git/commitdiff
Pass the private version of dl_inst down in the mctx
authorArran Cudbard-Bell <a.cudbardb@freeradius.org>
Wed, 10 Nov 2021 17:41:26 +0000 (11:41 -0600)
committerArran Cudbard-Bell <a.cudbardb@freeradius.org>
Wed, 10 Nov 2021 17:41:26 +0000 (11:41 -0600)
src/lib/server/dl_module.c
src/lib/server/dl_module.h
src/lib/server/module.h
src/lib/unlang/module.c

index 517d34822674f633d86cefc6a54605a98b996a22..7a86cae8f499c8305469733f482421358807c022 100644 (file)
@@ -24,8 +24,6 @@
  * @copyright 2016-2019 Arran Cudbard-Bell (a.cudbardb@freeradius.org)
  */
 RCSID("$Id$")
-
-#include <freeradius-devel/server/dl_module.h>
 #include <freeradius-devel/server/log.h>
 #include <freeradius-devel/util/debug.h>
 
@@ -35,6 +33,9 @@ RCSID("$Id$")
 #include <ctype.h>
 #include <unistd.h>
 
+#define _DL_MODULE_PRIVATE 1
+#include <freeradius-devel/server/dl_module.h>
+
 #define DL_INIT_CHECK fr_assert(dl_module_loader)
 
 /** Wrapper struct around dl_loader_t
@@ -276,7 +277,7 @@ static void dl_module_instance_data_alloc(dl_module_inst_t *dl_inst, dl_module_t
         *      succeed, and will create a talloc chunk header.
         *
         *      This is needed so we can resolve instance data back to
-        *      dl_module_instance_t/dl_module_t/dl_t.
+        *      dl_module_inst_t/dl_module_t/dl_t.
         */
        MEM(data = talloc_zero_array(dl_inst, uint8_t, module->common->inst_size));
 
index dc33e0e08ae5a5512793982e8b67ee30331f470d..bc98572c8b2cea9c3e2af1d1b23b29c41dcec361 100644 (file)
@@ -40,6 +40,15 @@ RCSIDH(dl_module_h, "$Id$")
 extern "C" {
 #endif
 
+#ifdef _CONST
+#  error _CONST can only be defined in the local header
+#endif
+#ifndef _DL_MODULE_PRIVATE
+#  define _CONST const
+#else
+#  define _CONST
+#endif
+
 #ifdef __APPLE__
 #  define DL_EXTENSION ".dylib"
 #elif defined (WIN32)
@@ -124,19 +133,19 @@ typedef struct {
  */
 typedef struct dl_module_s dl_module_t;
 struct dl_module_s {
-       dl_t                            *dl;            //!< Dynamic loader handle.
+       dl_t                            * _CONST dl;            //!< Dynamic loader handle.
 
-       dl_module_t const               *parent;        //!< of this module.
+       dl_module_t const               * _CONST parent;        //!< of this module.
 
-       dl_module_type_t                type;           //!< of this module.
+       dl_module_type_t                _CONST type;            //!< of this module.
 
-       dl_module_common_t const        *common;        //!< Symbol exported by the module, containing its public
-                                                       //!< functions, name and behaviour control flags.
+       dl_module_common_t const        * _CONST common;        //!< Symbol exported by the module, containing its public
+                                                               //!< functions, name and behaviour control flags.
 
-       CONF_SECTION                    *conf;          //!< The module's global configuration
-                                                       ///< (as opposed to the instance, configuration).
-                                                       ///< May be NULL.
-       bool                            in_tree;
+       CONF_SECTION                    * _CONST conf;          //!< The module's global configuration
+                                                               ///< (as opposed to the instance, configuration).
+                                                               ///< May be NULL.
+       bool                            _CONST in_tree;
 };
 
 /** A module/inst tuple
@@ -145,11 +154,11 @@ struct dl_module_s {
  */
 typedef struct dl_module_instance_s dl_module_inst_t;
 struct dl_module_instance_s {
-       char const              *name;                  //!< Instance name.
-       dl_module_t const       *module;                //!< Module
-       void                    *data;                  //!< Module instance's parsed configuration.
-       CONF_SECTION            *conf;                  //!< Module's instance configuration.
-       dl_module_inst_t const  *parent;                //!< Parent module's instance (if any).
+       char const                      * _CONST name;          //!< Instance name.
+       dl_module_t const               * _CONST module;        //!< Module
+       void                            * _CONST data;          //!< Module instance's parsed configuration.
+       CONF_SECTION                    * _CONST conf;          //!< Module's instance configuration.
+       dl_module_inst_t const          * _CONST parent;        //!< Parent module's instance (if any).
 };
 
 extern fr_table_num_sorted_t const dl_module_type_prefix[];
@@ -176,6 +185,8 @@ dl_loader_t         *dl_loader_from_module_loader(dl_module_loader_t *dl_module_loader)
 
 dl_module_loader_t     *dl_module_loader_init(char const *lib_dir);
 
+#undef _CONST
+
 #ifdef __cplusplus
 }
 #endif
index dacc599087965dd8dba9f97a5b4167de86c147b1..f66b962f23b433a915fb3c2d935b78a1b9285885 100644 (file)
@@ -265,6 +265,7 @@ typedef struct {
  *
  */
 struct module_ctx_s {
+       dl_module_inst_t                *dl_inst;       //!< dl API handle for the module.
        void                            *instance;      //!< Global instance data for the module.
        void                            *thread;        //!< Thread specific instance data.
        void                            *rctx;          //!< Resume ctx that a module previously set.
index f133d6286140f3319385ac628578fedbddfb14ca..0b6ce0efbc0b7e73bfd332ee877e127dc7b1f742 100644 (file)
@@ -45,7 +45,8 @@ typedef struct {
        unlang_module_fd_event_t        fd_read;        //!< Function to call when FD is readable.
        unlang_module_fd_event_t        fd_write;       //!< Function to call when FD is writable.
        unlang_module_fd_event_t        fd_error;       //!< Function to call when FD has errored.
-       void const                      *inst;          //!< Module instance to pass to callbacks.
+       dl_module_inst_t                *dl_inst;       //!< Module instance to pass to callbacks.
+                                                       ///< Use dl_inst->data to get instance data.
        void                            *thread;        //!< Thread specific module instance.
        void const                      *ctx;           //!< ctx data to pass to callbacks.
        fr_event_timer_t const          *ev;            //!< Event in this worker's event heap.
@@ -63,18 +64,14 @@ static unlang_action_t unlang_module_resume(rlm_rcode_t *p_result, request_t *re
 static void unlang_event_fd_read_handler(UNUSED fr_event_list_t *el, int fd, UNUSED int flags, void *ctx)
 {
        unlang_module_event_t *ev = talloc_get_type_abort(ctx, unlang_module_event_t);
-       void *mutable_ctx;
-       void *mutable_inst;
 
        fr_assert(ev->fd == fd);
 
-       memcpy(&mutable_ctx, &ev->ctx, sizeof(mutable_ctx));
-       memcpy(&mutable_inst, &ev->inst, sizeof(mutable_inst));
-
        ev->fd_read(&(module_ctx_t){
-                       .instance = mutable_inst,
+                       .dl_inst = ev->dl_inst,
+                       .instance = ev->dl_inst->data,
                        .thread = ev->thread,
-                       .rctx = mutable_ctx },
+                       .rctx = UNCONST(void *, ev->ctx) },
                    ev->request, fd);
 }
 
@@ -111,16 +108,12 @@ static int _unlang_event_free(unlang_module_event_t *ev)
 static void unlang_module_event_timeout_handler(UNUSED fr_event_list_t *el, fr_time_t now, void *ctx)
 {
        unlang_module_event_t *ev = talloc_get_type_abort(ctx, unlang_module_event_t);
-       void *mutable_ctx;
-       void *mutable_inst;
-
-       memcpy(&mutable_ctx, &ev->ctx, sizeof(mutable_ctx));
-       memcpy(&mutable_inst, &ev->inst, sizeof(mutable_inst));
 
        ev->timeout(&(module_ctx_t){
-                       .instance = mutable_inst,
+                       .dl_inst = ev->dl_inst,
+                       .instance = ev->dl_inst->data,
                        .thread = ev->thread,
-                       .rctx = mutable_ctx
+                       .rctx = UNCONST(void *, ev->ctx)
                    }, ev->request, now);
        talloc_free(ev);
 }
@@ -161,7 +154,7 @@ int unlang_module_timeout_add(request_t *request, unlang_module_timeout_t callba
                .request = request,
                .fd = -1,
                .timeout = callback,
-               .inst = mc->instance->dl_inst->data,
+               .dl_inst = mc->instance->dl_inst,
                .thread = state->thread,
                .ctx = ctx
        };
@@ -209,18 +202,13 @@ int unlang_module_timeout_delete(request_t *request, void const *ctx)
 static void unlang_event_fd_write_handler(UNUSED fr_event_list_t *el, int fd, UNUSED int flags, void *ctx)
 {
        unlang_module_event_t *ev = talloc_get_type_abort(ctx, unlang_module_event_t);
-       void *mutable_ctx;
-       void *mutable_inst;
-
        fr_assert(ev->fd == fd);
 
-       memcpy(&mutable_ctx, &ev->ctx, sizeof(mutable_ctx));
-       memcpy(&mutable_inst, &ev->inst, sizeof(mutable_inst));
-
        ev->fd_write(&(module_ctx_t){
-                       .instance = mutable_inst,
+                       .dl_inst = ev->dl_inst,
+                       .instance = ev->dl_inst->data,
                        .thread = ev->thread,
-                       .rctx = mutable_ctx },
+                       .rctx = UNCONST(void *, ev->ctx) },
                     ev->request, fd);
 }
 
@@ -236,18 +224,14 @@ static void unlang_event_fd_error_handler(UNUSED fr_event_list_t *el, int fd,
                                          UNUSED int flags, UNUSED int fd_errno, void *ctx)
 {
        unlang_module_event_t *ev = talloc_get_type_abort(ctx, unlang_module_event_t);
-       void *mutable_ctx;
-       void *mutable_inst;
 
        fr_assert(ev->fd == fd);
 
-       memcpy(&mutable_ctx, &ev->ctx, sizeof(mutable_ctx));
-       memcpy(&mutable_inst, &ev->inst, sizeof(mutable_inst));
-
        ev->fd_error(&(module_ctx_t){
-                       .instance = mutable_inst,
+                       .dl_inst = ev->dl_inst,
+                       .instance = ev->dl_inst->data,
                        .thread = ev->thread,
-                       .rctx = mutable_ctx
+                       .rctx = UNCONST(void *, ev->ctx)
                     }, ev->request, fd);
 }
 
@@ -302,7 +286,7 @@ int unlang_module_fd_add(request_t *request,
        ev->fd_read = read;
        ev->fd_write = write;
        ev->fd_error = error;
-       ev->inst = mc->instance->dl_inst->data;
+       ev->dl_inst = mc->instance->dl_inst;
        ev->thread = state->thread;
        ev->ctx = ctx;
 
@@ -665,6 +649,7 @@ static void unlang_module_signal(request_t *request, unlang_stack_frame_t *frame
        request->module = mc->instance->name;
        safe_lock(mc->instance);
        state->signal(&(module_ctx_t){
+                       .dl_inst = mc->instance->dl_inst,
                        .instance = mc->instance->dl_inst->data,
                        .thread = state->thread->data,
                        .rctx = state->rctx
@@ -763,6 +748,7 @@ static unlang_action_t unlang_module_resume(rlm_rcode_t *p_result, request_t *re
        safe_lock(mc->instance);
        ua = resume(&state->rcode,
                    &(module_ctx_t){
+                       .dl_inst = mc->instance->dl_inst,
                        .instance = mc->instance->dl_inst->data,
                        .thread = state->thread->data,
                        .rctx = state->rctx,
@@ -972,6 +958,7 @@ static unlang_action_t unlang_module(rlm_rcode_t *p_result, request_t *request,
        safe_lock(mc->instance);        /* Noop unless instance->mutex set */
        ua = mc->method(&state->rcode,
                        &(module_ctx_t){
+                               .dl_inst = mc->instance->dl_inst,
                                .instance = mc->instance->dl_inst->data,
                                .thread = state->thread->data
                        },