]> git.ipfire.org Git - thirdparty/freeradius-server.git/commitdiff
Move common utility inline functions into unlang_priv.h
authorArran Cudbard-Bell <a.cudbardb@freeradius.org>
Wed, 24 Mar 2021 22:20:19 +0000 (22:20 +0000)
committerArran Cudbard-Bell <a.cudbardb@freeradius.org>
Wed, 31 Mar 2021 15:10:35 +0000 (16:10 +0100)
src/lib/unlang/call.c
src/lib/unlang/interpret.c
src/lib/unlang/interpret.h
src/lib/unlang/module.c
src/lib/unlang/parallel.c
src/lib/unlang/subrequest.c
src/lib/unlang/unlang_priv.h

index 7f5c308c401e6c95494a007d60df5de1503cc37e..d917683941ba4cdd4d95e5d570c174d47c672c7c 100644 (file)
@@ -173,7 +173,7 @@ CONF_SECTION *unlang_call_current(request_t *request)
         *      Work back from the deepest frame
         *      looking for modules.
         */
-       for (depth = unlang_current_depth(request); depth > 0; depth--) {
+       for (depth = stack_depth_current(request); depth > 0; depth--) {
                unlang_stack_frame_t    *frame = &stack->frame[depth];
 
                /*
index f789b29a475c38e0389da7445d2cf54461edf370..c77a3fff17c57ec94e73eb2e7754f39aa64428a4 100644 (file)
@@ -108,50 +108,6 @@ static void stack_dump(request_t *request)
 #define DUMP_STACK
 #endif
 
-/** Different operations the interpret can execute
- */
-unlang_op_t unlang_ops[UNLANG_TYPE_MAX];
-
-
-static inline void frame_state_init(unlang_stack_t *stack, unlang_stack_frame_t *frame)
-{
-       unlang_t const  *instruction = frame->instruction;
-       unlang_op_t     *op;
-       char const      *name;
-
-       op = &unlang_ops[instruction->type];
-       name = op->frame_state_name ? op->frame_state_name : __location__;
-
-       frame->process = op->interpret;
-       frame->signal = op->signal;
-
-#ifdef HAVE_TALLOC_ZERO_POOLED_OBJECT
-       /*
-        *      Pooled object
-        */
-       if (op->frame_state_pool_size && op->frame_state_size) {
-               MEM(frame->state = _talloc_zero_pooled_object(stack,
-                                                             op->frame_state_size, name,
-                                                             op->frame_state_pool_objects,
-                                                             op->frame_state_pool_size));
-       } else
-#endif
-       /*
-        *      Pool
-        */
-       if (op->frame_state_pool_size && !op->frame_state_size) {
-               MEM(frame->state = talloc_pool(stack,
-                                              op->frame_state_pool_size +
-                                              ((20 + 68 + 15) * op->frame_state_pool_objects))); /* from samba talloc.c */
-               talloc_set_name_const(frame->state, name);
-       /*
-        *      Object
-        */
-       } else if (op->frame_state_size) {
-               MEM(frame->state = _talloc_zero(stack, op->frame_state_size, name));
-       }
-}
-
 /** Push a new frame onto the stack
  *
  * @param[in] request          to push the frame onto.
@@ -345,60 +301,6 @@ unlang_frame_action_t result_calculate(request_t *request, unlang_stack_frame_t
        return frame->next ? UNLANG_FRAME_ACTION_NEXT : UNLANG_FRAME_ACTION_POP;
 }
 
-/** Cleanup any lingering frame state
- *
- */
-static inline CC_HINT(always_inline) void frame_cleanup(unlang_stack_frame_t *frame)
-{
-       /*
-        *      Don't clear top_frame flag, bad things happen...
-        */
-       repeatable_clear(frame);
-       break_point_clear(frame);
-       return_point_clear(frame);
-       yielded_clear(frame);
-       if (frame->state) {
-               talloc_free_children(frame->state); /* *(ev->parent) = NULL in event.c */
-               TALLOC_FREE(frame->state);
-       }
-}
-
-/** Advance to the next sibling instruction
- *
- */
-static inline CC_HINT(always_inline) void frame_next(unlang_stack_t *stack, unlang_stack_frame_t *frame)
-{
-       frame_cleanup(frame);
-       frame->instruction = frame->next;
-
-       if (!frame->instruction) return;
-
-       frame->next = frame->instruction->next;
-
-       frame_state_init(stack, frame);
-}
-
-/** Pop a stack frame, removing any associated dynamically allocated state
- *
- * @param[in] stack    frame to pop.
- */
-static inline CC_HINT(always_inline) void frame_pop(unlang_stack_t *stack)
-{
-       unlang_stack_frame_t *frame;
-
-       fr_assert(stack->depth > 1);
-
-       frame = &stack->frame[stack->depth];
-
-       frame_cleanup(frame);
-
-       frame = &stack->frame[--stack->depth];
-
-       if (stack->unwind && is_repeatable(frame) && !is_break_point(frame) && !is_return_point(frame)) {
-               repeatable_clear(frame);
-       }
-}
-
 /** Evaluates all the unlang nodes in a section
  *
  * @param[in] request          The current request.
@@ -1255,6 +1157,17 @@ unlang_interpret_t *unlang_interpret_init(TALLOC_CTX *ctx,
        return intp;
 }
 
+/** Discard the bottom most frame on the request's stack
+ *
+ * This is used for cleaning up after errors. i.e. the caller
+ * uses a push function, and experiences an error and needs to
+ * remove the frame that was just pushed.
+ */
+void unlang_interpet_frame_discard(request_t *request)
+{
+       frame_pop(request->stack);
+}
+
 /** Set a specific interpreter for a request
  *
  */
index c27f90c1ec34e66843644f84836f8a60d7e2b032..5d943e9b1dfc9fc7f6e9360b95bb38136ee61fe0 100644 (file)
@@ -113,6 +113,8 @@ int                 unlang_interpret_push_instruction(request_t *request, void *instruction,
 unlang_interpret_t     *unlang_interpret_init(TALLOC_CTX *ctx,
                                               fr_event_list_t *el, unlang_request_func_t *func, void *uctx);
 
+void                   unlang_interpet_frame_discard(request_t *request);
+
 void                   unlang_interpret_set(request_t *request, unlang_interpret_t *intp);
 
 unlang_interpret_t     *unlang_interpret_get(request_t *request);
index 460d27dddbfe6cb0082464c4421bfd2a467b1e66..b539ee63a415c1b604ca4a8c20d4fa7e24685ae5 100644 (file)
@@ -656,7 +656,7 @@ static unlang_action_t unlang_module_resume(rlm_rcode_t *p_result, request_t *re
        unlang_module_t                 *mc = unlang_generic_to_module(frame->instruction);
        char const                      *caller;
        rlm_rcode_t                     rcode = *p_result;
-       int                             stack_depth = unlang_current_depth(request);
+       int                             stack_depth = stack_depth_current(request);
        unlang_action_t                 ua;
 
        fr_assert(state->resume != NULL);
@@ -695,8 +695,8 @@ static unlang_action_t unlang_module_resume(rlm_rcode_t *p_result, request_t *re
        }
 
        if (ua == UNLANG_ACTION_YIELD) {
-               if (stack_depth < unlang_current_depth(request)) return UNLANG_ACTION_PUSHED_CHILD;
-               fr_assert(stack_depth == unlang_current_depth(request));
+               if (stack_depth < stack_depth_current(request)) return UNLANG_ACTION_PUSHED_CHILD;
+               fr_assert(stack_depth == stack_depth_current(request));
                *p_result = rcode;
                return UNLANG_ACTION_YIELD;
        }
@@ -774,7 +774,7 @@ static unlang_action_t unlang_module(rlm_rcode_t *p_result, request_t *request,
 {
        unlang_module_t                 *mc;
        unlang_frame_state_module_t     *state = talloc_get_type_abort(frame->state, unlang_frame_state_module_t);
-       int                             stack_depth = unlang_current_depth(request);
+       int                             stack_depth = stack_depth_current(request);
        char const                      *caller;
        rlm_rcode_t                     rcode = RLM_MODULE_NOOP;
        unlang_action_t                 ua;
@@ -790,7 +790,7 @@ static unlang_action_t unlang_module(rlm_rcode_t *p_result, request_t *request,
        mc = unlang_generic_to_module(frame->instruction);
        fr_assert(mc);
 
-       RDEBUG4("[%i] %s - %s (%s)", unlang_current_depth(request), __FUNCTION__,
+       RDEBUG4("[%i] %s - %s (%s)", stack_depth_current(request), __FUNCTION__,
                mc->instance->name, mc->instance->module->name);
 
        /*
@@ -812,7 +812,7 @@ static unlang_action_t unlang_module(rlm_rcode_t *p_result, request_t *request,
        /*
         *      Don't allow returning _through_ modules
         */
-       return_point_set(unlang_current_frame(request));
+       return_point_set(frame_current(request));
 
        /*
         *      For logging unresponsive children.
@@ -852,8 +852,8 @@ static unlang_action_t unlang_module(rlm_rcode_t *p_result, request_t *request,
 
        if (ua == UNLANG_ACTION_YIELD) {
                state->thread->active_callers++;
-               if (stack_depth < unlang_current_depth(request)) return UNLANG_ACTION_PUSHED_CHILD;
-               fr_assert(stack_depth == unlang_current_depth(request));
+               if (stack_depth < stack_depth_current(request)) return UNLANG_ACTION_PUSHED_CHILD;
+               fr_assert(stack_depth == stack_depth_current(request));
                frame->process = unlang_module_resume;
                return UNLANG_ACTION_YIELD;
        }
index b192e475a5cddd2aa2c94a59f7bdb3a6f6bbfc5c..5a292756ec4e95e47ef5863f47cbd42f46877400 100644 (file)
@@ -116,7 +116,7 @@ static void unlang_parallel_child_signal(request_t *request, fr_state_signal_t a
 
        if (action != FR_SIGNAL_DETACH) return;
 
-       frame = unlang_current_frame(request->parent);
+       frame = frame_current(request->parent);
        state = talloc_get_type_abort(frame->state, unlang_parallel_state_t);
 
        RDEBUG3("parallel - child %s (%d/%d) DETACHED",
@@ -159,7 +159,7 @@ static unlang_action_t unlang_parallel_child_done(UNUSED rlm_rcode_t *p_result,
                 *      Reach into the parent to get the unlang_parallel_state_t
                 *      for the whole parallel block.
                 */
-               unlang_stack_frame_t            *frame = unlang_current_frame(request->parent);
+               unlang_stack_frame_t            *frame = frame_current(request->parent);
                unlang_parallel_state_t         *state = talloc_get_type_abort(frame->state, unlang_parallel_state_t);
 
                RDEBUG3("parallel - child %s (%d/%d) EXITED",
@@ -227,7 +227,7 @@ static unlang_action_t unlang_parallel_resume(rlm_rcode_t *p_result, request_t *
                        state->priority = priority;
 
                        RDEBUG4("** [%i] %s - over-riding result from higher priority to (%s %d)",
-                               unlang_current_depth(request), __FUNCTION__,
+                               stack_depth_current(request), __FUNCTION__,
                                fr_table_str_by_value(mod_rcode_table, result, "<invalid>"),
                                priority);
                }
@@ -375,7 +375,7 @@ static unlang_action_t unlang_parallel_process(rlm_rcode_t *p_result, request_t
                                                           unlang_parallel_child_signal,
                                                           UNLANG_TOP_FRAME,
                                                           &state->children[i]) < 0) goto error;
-                       child_frame = unlang_current_frame(child);
+                       child_frame = frame_current(child);
                        return_point_set(child_frame);          /* Don't unwind this frame */
 
                        state->children[i].num = i;
index e77fcb3089c8ab7187194f198bd2edc1862d21bc..b174f446428fb460f06019c12c57759e8a9a1288 100644 (file)
@@ -429,7 +429,7 @@ static unlang_action_t unlang_detach(rlm_rcode_t *p_result, request_t *request,
        /*
         *      Get the PARENT's stack.
         */
-       parent_frame = unlang_current_frame(request->parent);
+       parent_frame = frame_current(request->parent);
        parent_state = talloc_get_type_abort(frame->state, unlang_frame_state_subrequest_t);
 
        if (!parent_state->detachable) {
index 51600e647af9461454844d46f75e9c61d9cca514..a4818f98fa5fece71459625e17ce4ee6a2164d14 100644 (file)
@@ -260,6 +260,15 @@ typedef struct {
        unlang_stack_frame_t    frame[UNLANG_STACK_MAX];        //!< The stack...
 } unlang_stack_t;
 
+/** Different operations the interpreter can execute
+ */
+extern unlang_op_t unlang_ops[];
+
+#define MOD_NUM_TYPES (UNLANG_TYPE_XLAT + 1)
+
+extern fr_table_num_sorted_t const mod_rcode_table[];
+extern size_t mod_rcode_table_len;
+
 #define UNWIND_FLAG_NONE               0x00                    //!< No flags.
 #define UNWIND_FLAG_REPEAT             0x01                    //!< Repeat the frame on the way up the stack.
 #define UNWIND_FLAG_TOP_FRAME          0x02                    //!< are we the top frame of the stack?
@@ -305,28 +314,112 @@ static inline unlang_action_t unwind_all(unlang_stack_t *stack)
        return UNLANG_ACTION_UNWIND;
 }
 
-static inline unlang_stack_frame_t *unlang_current_frame(request_t *request)
+static inline unlang_stack_frame_t *frame_current(request_t *request)
 {
        unlang_stack_t *stack = request->stack;
 
        return &stack->frame[stack->depth];
 }
 
-static inline int unlang_current_depth(request_t *request)
+static inline int stack_depth_current(request_t *request)
 {
        unlang_stack_t *stack = request->stack;
 
        return stack->depth;
 }
 
-/** Different operations the interpreter can execute
+static inline void frame_state_init(unlang_stack_t *stack, unlang_stack_frame_t *frame)
+{
+       unlang_t const  *instruction = frame->instruction;
+       unlang_op_t     *op;
+       char const      *name;
+
+       op = &unlang_ops[instruction->type];
+       name = op->frame_state_name ? op->frame_state_name : __location__;
+
+       frame->process = op->interpret;
+       frame->signal = op->signal;
+
+#ifdef HAVE_TALLOC_ZERO_POOLED_OBJECT
+       /*
+        *      Pooled object
+        */
+       if (op->frame_state_pool_size && op->frame_state_size) {
+               MEM(frame->state = _talloc_zero_pooled_object(stack,
+                                                             op->frame_state_size, name,
+                                                             op->frame_state_pool_objects,
+                                                             op->frame_state_pool_size));
+       } else
+#endif
+       /*
+        *      Pool
+        */
+       if (op->frame_state_pool_size && !op->frame_state_size) {
+               MEM(frame->state = talloc_pool(stack,
+                                              op->frame_state_pool_size +
+                                              ((20 + 68 + 15) * op->frame_state_pool_objects))); /* from samba talloc.c */
+               talloc_set_name_const(frame->state, name);
+       /*
+        *      Object
+        */
+       } else if (op->frame_state_size) {
+               MEM(frame->state = _talloc_zero(stack, op->frame_state_size, name));
+       }
+}
+
+/** Cleanup any lingering frame state
+ *
  */
-extern unlang_op_t unlang_ops[];
+static inline void frame_cleanup(unlang_stack_frame_t *frame)
+{
+       /*
+        *      Don't clear top_frame flag, bad things happen...
+        */
+       repeatable_clear(frame);
+       break_point_clear(frame);
+       return_point_clear(frame);
+       yielded_clear(frame);
+       if (frame->state) {
+               talloc_free_children(frame->state); /* *(ev->parent) = NULL in event.c */
+               TALLOC_FREE(frame->state);
+       }
+}
 
-#define MOD_NUM_TYPES (UNLANG_TYPE_XLAT + 1)
+/** Advance to the next sibling instruction
+ *
+ */
+static inline void frame_next(unlang_stack_t *stack, unlang_stack_frame_t *frame)
+{
+       frame_cleanup(frame);
+       frame->instruction = frame->next;
 
-extern fr_table_num_sorted_t const mod_rcode_table[];
-extern size_t mod_rcode_table_len;
+       if (!frame->instruction) return;
+
+       frame->next = frame->instruction->next;
+
+       frame_state_init(stack, frame);
+}
+
+/** Pop a stack frame, removing any associated dynamically allocated state
+ *
+ * @param[in] stack    frame to pop.
+ */
+static inline void frame_pop(unlang_stack_t *stack)
+{
+       unlang_stack_frame_t *frame;
+
+       fr_assert(stack->depth > 1);
+
+       frame = &stack->frame[stack->depth];
+
+       frame_cleanup(frame);
+
+       frame = &stack->frame[--stack->depth];
+
+       if (stack->unwind && is_repeatable(frame) && !is_break_point(frame) && !is_return_point(frame)) {
+               repeatable_clear(frame);
+       }
+}
 
 /** @name Conversion functions for converting #unlang_t to its specialisations
  *