The FreeRADIUS server $Id: 15bac2a4c627c01d1aa2047687b3418955ac7f00 $
Loading...
Searching...
No Matches
interpret.c
Go to the documentation of this file.
1/*
2 * This program is free software; you can redistribute it and/or modify
3 * it under the terms of the GNU General Public License as published by
4 * the Free Software Foundation; either version 2 of the License, or
5 * (at your option) any later version.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
11 *
12 * You should have received a copy of the GNU General Public License
13 * along with this program; if not, write to the Free Software
14 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
15 */
16
17/**
18 * $Id: 2840aa3c7624f1af1d708a97d5fcad1c52f0dec7 $
19 *
20 * @file unlang/interpret.c
21 * @brief Execute compiled unlang structures using an iterative interpret.
22 *
23 * @copyright 2006-2016 The FreeRADIUS server project
24 */
25RCSID("$Id: 2840aa3c7624f1af1d708a97d5fcad1c52f0dec7 $")
26
27#include <freeradius-devel/unlang/action.h>
28#include <freeradius-devel/unlang/interpret.h>
29#include <freeradius-devel/util/timer.h>
30#include <freeradius-devel/server/base.h>
31#include <freeradius-devel/server/modpriv.h>
32#include <freeradius-devel/server/rcode.h>
33#include <freeradius-devel/unlang/xlat_func.h>
34#include <freeradius-devel/unlang/mod_action.h>
35
36#include "interpret_priv.h"
37#include "unlang_priv.h"
38#include "module_priv.h"
39
40
41/** The default interpreter instance for this thread
42 */
44
46 { L("fail"), UNLANG_ACTION_FAIL },
47 { L("calculate-result"), UNLANG_ACTION_CALCULATE_RESULT },
48 { L("next"), UNLANG_ACTION_EXECUTE_NEXT },
49 { L("pushed-child"), UNLANG_ACTION_PUSHED_CHILD },
50 { L("yield"), UNLANG_ACTION_YIELD }
51};
53
60
61#ifndef NDEBUG
62#include <freeradius-devel/unlang/module_priv.h>
63
72
73/** Try and figure out where p_result points to
74 *
75 * If it's somewhere other than these three locations, it's probably wrong.
76 */
77static int find_p_result_location(p_result_location_t *location, void **chunk, request_t *request, void *ptr)
78{
79 unlang_stack_t *stack = request->stack;
81 unsigned int i;
82
83 for (i = 0; i <= (unsigned int)stack->depth; i++) {
84 frame = &stack->frame[i];
85 if (frame->state && (ptr >= (void *)frame->state) &&
86 (ptr < ((void *)((uint8_t *)frame->state + talloc_get_size(frame->state))))) {
87 *location = P_RESULT_LOCATION_STATE;
88 *chunk = frame->state;
89 return i;
90 }
91
92 if (ptr == &frame->section_result) {
93 *location = P_RESULT_LOCATION_FRAME;
94 *chunk = NULL;
95 return i;
96 }
97
98 if (ptr == &frame->scratch_result) {
99 *location = P_RESULT_LOCATION_SCRATCH;
100 *chunk = NULL;
101 return i;
102 }
103
104 if (!frame->instruction) continue;
105
106 switch (frame->instruction->type) {
108 {
109 unlang_frame_state_module_t *mod_state = talloc_get_type_abort(frame->state, unlang_frame_state_module_t);
110
111 if (!mod_state->rctx) continue;
112
113 if ((ptr >= (void *)mod_state->rctx) &&
114 (ptr < ((void *)((uint8_t *)mod_state->rctx + talloc_get_size(mod_state->rctx))))) {
116 *chunk = mod_state->rctx;
117 return i;
118 }
119
120 /*
121 * We don't know where the child frame is, so we can't
122 * determine where the p_result is.
123 */
124 }
125 continue;
126
127 default:
128 break;
129 }
130 }
131
132 *location = P_RESULT_LOCATION_UNKNOWN;
133 *chunk = NULL;
134 return -1;
135}
136
138 { L("frame"), P_RESULT_LOCATION_FRAME },
139 { L("module_rctx"), P_RESULT_LOCATION_MODULE_RCTX },
140 { L("scratch"), P_RESULT_LOCATION_SCRATCH },
141 { L("state"), P_RESULT_LOCATION_STATE },
142 { L("unknown"), P_RESULT_LOCATION_UNKNOWN }
143};
145
146static void instruction_dump(request_t *request, unlang_t const *instruction)
147{
148 RINDENT();
149 if (!instruction) {
150 RDEBUG2("instruction <none>");
151 REXDENT();
152 return;
153 }
154
155 RDEBUG2("type %s", unlang_ops[instruction->type].name);
156 RDEBUG2("name %s", instruction->name);
157 RDEBUG2("debug_name %s", instruction->debug_name);
158 REXDENT();
159}
160
161static void CC_HINT(nonnull) actions_dump(request_t *request, unlang_t const *instruction)
162{
163 int i;
164
165 RDEBUG2("actions");
166 RINDENT();
167 for (i = 0; i < RLM_MODULE_NUMCODES; i++) {
168 RDEBUG2("%s: %s",
169 fr_table_str_by_value(mod_rcode_table, i, "<invalid>"),
170 mod_action_name[instruction->actions.actions[i]]);
171 }
172 REXDENT();
173}
174
175static void frame_dump(request_t *request, unlang_stack_frame_t *frame, bool with_actions)
176{
177 unlang_op_t *op = NULL;
178
179 if (frame->instruction) {
180 op = &unlang_ops[frame->instruction->type];
181 instruction_dump(request, frame->instruction);
182 }
183
184 RINDENT();
185 if (frame->state) RDEBUG2("state %s (%p)", talloc_get_name(frame->state), frame->state);
186 if (frame->next) {
187 RDEBUG2("next %s", frame->next->debug_name);
188 } else {
189 RDEBUG2("next <none>");
190 }
191
193
194 if (is_private_result(frame)) {
195 int location;
197 void *chunk;
198
199 RDEBUG2("p_rcode %s", fr_table_str_by_value(mod_rcode_table, frame->p_result->rcode, "<invalid>"));
200 RDEBUG2("p_priority %s", mod_action_name[frame->p_result->priority]);
201
202 location = find_p_result_location(&type, &chunk, request, frame->p_result);
203 RDEBUG2("p_location %s [%i] %p (%s)", fr_table_str_by_value(p_result_location_table, type, "<invalid>"),
204 location, frame->p_result, chunk ? talloc_get_name(chunk) : "<none>"
205 );
206 } else {
207 RDEBUG2("sec_rcode %s", fr_table_str_by_value(mod_rcode_table, frame->section_result.rcode, "<invalid>"));
208 RDEBUG2("sec_priority %s", mod_action_name[frame->section_result.priority]);
209 }
210 RDEBUG2("scr_rcode %s", fr_table_str_by_value(mod_rcode_table, frame->scratch_result.rcode, "<invalid>"));
211 RDEBUG2("scr_priority %s", mod_action_name[frame->scratch_result.priority]);
212 RDEBUG2("top_frame %s", is_top_frame(frame) ? "yes" : "no");
213 RDEBUG2("repeat %s", is_repeatable(frame) ? "yes" : "no");
214 RDEBUG2("yielded %s", is_yielded(frame) ? "yes" : "no");
215 RDEBUG2("unwind %s", is_unwinding(frame) ? "yes" : "no");
216
217 if (frame->instruction) {
218 RDEBUG2("control %s%s%s",
219 is_break_point(frame) ? "b" : "-",
220 is_return_point(frame) ? "r" : "-",
221 is_continue_point(frame) ? "c" : "-"
222 );
223 if (with_actions) actions_dump(request, frame->instruction);
224 }
225
226 /*
227 * Call the custom frame dump function
228 */
229 if (op && op->dump) op->dump(request, frame);
230 REXDENT();
231}
232
233static void stack_dump_body(request_t *request, bool with_actions)
234{
235 int i;
236 unlang_stack_t *stack = request->stack;
237
238 RDEBUG2("----- Begin stack debug [depth %i] -----",
239 stack->depth);
240 for (i = stack->depth; i >= 0; i--) {
241 unlang_stack_frame_t *frame = &stack->frame[i];
242 RDEBUG2("[%d] Frame contents", i);
243 frame_dump(request, frame, with_actions);
244 }
245 RDEBUG2("----- End stack debug [depth %i] -------", stack->depth);
246}
247
248void stack_dump(request_t *request)
249{
250 stack_dump_body(request, false);
251}
252
254{
255 stack_dump_body(request, true);
256}
257#define DUMP_STACK if (DEBUG_ENABLED5) stack_dump(request)
258#else
259#define DUMP_STACK
260#endif
261
262/** Push a new frame onto the stack
263 *
264 * @param[in] p_result Where to write the result of evaluating the section.
265 * If NULL, results will be written to frame->section_result and will
266 * be automatically merged with the next highest frame when this one
267 * is popped.
268 * @param[in] request to push the frame onto.
269 * @param[in] instruction One or more unlang_t nodes describing the operations to execute.
270 * @param[in] conf Configuration for the frame. If NULL, the following values areused:
271 * - default result = UNLANG_RESULT_NOT_SET
272 * - top_frame = UNLANG_SUB_FRAME
273 * - no_rcode = false
274 * @param[in] do_next_sibling Whether to only execute the first node in the #unlang_t program
275 * or to execute subsequent nodes.
276 * @return
277 * - 0 on success.
278 * - -1 on call stack too deep.
279 */
281 unlang_t const *instruction, unlang_frame_conf_t const *conf, bool do_next_sibling)
282{
283 unlang_stack_t *stack = request->stack;
285
286 static unlang_frame_conf_t default_conf = {
288 .top_frame = UNLANG_SUB_FRAME
289 };
290
291 if (!conf) conf = &default_conf;
292
293 if (!instruction) return -1;
294
295#ifndef NDEBUG
296 if (DEBUG_ENABLED5) RDEBUG3("unlang_interpret_push called with instruction type \"%s\" - args %s %s",
297 instruction ? instruction->debug_name : "<none>",
298 do_next_sibling ? "UNLANG_NEXT_SIBLING" : "UNLANG_NEXT_STOP",
299 conf->top_frame ? "UNLANG_TOP_FRAME" : "UNLANG_SUB_FRAME");
300#endif
301
302 /*
303 * This is not a cancellation point.
304 *
305 * If we cancel here bad things happen inside the interpret.
306 */
307 if (stack->depth >= (UNLANG_STACK_MAX - 1)) {
308 RERROR("Call stack is too deep");
309 return - 1;
310 }
311
312 stack->depth++;
313
314 /*
315 * Initialize the next stack frame.
316 */
317 frame = &stack->frame[stack->depth];
318 memset(frame, 0, sizeof(*frame));
319
320 frame->instruction = instruction;
321
322 if (do_next_sibling && instruction->list) {
323 frame->next = unlang_list_next(instruction->list, instruction);
324 }
325 /* else frame->next MUST be NULL */
326
328 if (conf->top_frame) top_frame_set(frame);
329
330 frame->p_result = p_result ? p_result : &frame->section_result;
331 *frame->p_result = conf->default_result;
332
333 frame->indent = request->log.indent;
334
335 frame_state_init(stack, frame);
336
337 return 0;
338}
339
340typedef struct {
341 fr_dict_t const *old_dict; //!< the previous dictionary for the request
342 request_t *request; //!< the request
344
346{
347 fr_pair_t *vp, *prev;
348
349 /*
350 * Local variables are appended to the end of the list. So we remove them by walking backwards
351 * from the end of the list.
352 */
353 vp = fr_pair_list_tail(&ref->request->local_pairs);
354 while (vp) {
355 prev = fr_pair_list_prev(&ref->request->local_pairs, vp);
356 if (vp->da->dict != ref->request->local_dict) {
357 break;
358 }
359
360 (void) fr_pair_delete(&ref->request->local_pairs, vp);
361 vp = prev;
362 }
363
364 ref->request->local_dict = ref->old_dict;
365
366 return 0;
367}
368
369/** Push the children of the current frame onto a new frame onto the stack
370 *
371 * @param[out] p_result set to RLM_MOULDE_FAIL if pushing the children fails
372 * @param[in] request to push the frame onto.
373 * @param[in] default_rcode The default result.
374 * @param[in] do_next_sibling Whether to only execute the first node in the #unlang_t program
375 * or to execute subsequent nodes.
376 * @return
377 * - UNLANG_ACTION_PUSHED_CHILD on success.
378 * - UNLANG_ACTION_EXECUTE_NEXT do nothing, but just go to the next sibling instruction
379 * - UNLANG_ACTION_FAIL, fatal error, usually stack overflow.
380 */
382 rlm_rcode_t default_rcode, bool do_next_sibling)
383{
384 unlang_stack_t *stack = request->stack;
385 unlang_stack_frame_t *frame = &stack->frame[stack->depth]; /* Quiet static analysis */
388
390
392
393 /*
394 * The compiler catches most of these, EXCEPT for the
395 * top-level 'recv Access-Request' etc. Which can exist,
396 * and can be empty.
397 */
398 if (unlang_list_empty(&g->children)) {
399 RDEBUG2("... ignoring empty subsection ...");
401 }
402
403 if (unlang_interpret_push(p_result, request, unlang_list_head(&g->children),
404 FRAME_CONF(default_rcode, UNLANG_SUB_FRAME), do_next_sibling) < 0) {
406 }
407
409
410 /*
411 * Note that we do NOT create the variables, This way we don't have to worry about any
412 * uninitialized values. If the admin tries to use the variable without initializing it, they
413 * will get a "no such attribute" error.
414 */
415 if (!frame->state) {
416 MEM(ref = talloc(stack, unlang_variable_ref_t));
417 frame->state = ref;
418 } else {
419 MEM(ref = talloc(frame->state, unlang_variable_ref_t));
420 }
421
422 /*
423 * Set the destructor to clean up local variables.
424 */
425 ref->request = request;
426 ref->old_dict = request->local_dict;
427 request->local_dict = g->variables->dict;
428 talloc_set_destructor(ref, _local_variables_free);
429
431}
432
433static void instruction_retry_handler(UNUSED fr_timer_list_t *tl, UNUSED fr_time_t now, void *ctx);
434
435/** Update the current result after each instruction, and after popping each stack frame
436 *
437 * @note Sets stack->scratch to be the the result of the frame being popped.
438 *
439 * @param[in] request The current request.
440 * @param[in] frame The current stack frame.
441 * @param[in] result from the previous action.
442 * @return
443 * - UNLANG_FRAME_ACTION_NEXT evaluate more instructions.
444 * - UNLANG_FRAME_ACTION_POP the final result has been calculated for this frame.
445 */
446static inline CC_HINT(always_inline)
448{
449 unlang_t const *instruction = frame->instruction;
450 unlang_stack_t *stack = request->stack;
451 unlang_result_t *frame_result = frame->p_result;
452
453 if (is_unwinding(frame)) {
454 RDEBUG4("** [%i] %s - unwinding frame", stack->depth, __FUNCTION__);
456 }
457
458 /*
459 * Don't calculate a new return code for the frame, just skip
460 * to the next instruction.
461 */
462 if (result->rcode == RLM_MODULE_NOT_SET) {
463 RDEBUG4("** [%i] %s - skipping frame, no result set",
464 stack->depth, __FUNCTION__);
466 }
467
468 fr_assert(MOD_ACTION_VALID(frame_result->priority));
469 fr_assert(MOD_ACTION_VALID(result->priority));
470
471 RDEBUG4("** [%i] %s - have (%s %s) frame or module returned (%s %s)",
472 stack->depth, __FUNCTION__,
473 fr_table_str_by_value(mod_rcode_table, frame_result->rcode, "<invalid>"),
474 mod_action_name[frame_result->priority],
475 fr_table_str_by_value(mod_rcode_table, result->rcode, "<invalid>"),
476 mod_action_name[result->priority]);
477
478 /*
479 * Update request->rcode if the instruction says we should
480 * We don't care about priorities for this.
481 *
482 * This is the field that's evaluated in unlang conditions
483 * like `if (ok)`.
484 */
485 if (is_rcode_set(frame) && (request->rcode != result->rcode)) {
486 RDEBUG3("Setting request->rcode to '%s'",
487 fr_table_str_by_value(rcode_table, result->rcode, "<INVALID>"));
488 request->rcode = result->rcode;
489 }
490
491 /*
492 * The array holds a default priority for this return
493 * code. Grab it in preference to any unset priority.
494 */
495 if (result->priority == MOD_ACTION_NOT_SET) {
496 result->priority = instruction->actions.actions[result->rcode];
497
498 fr_assert(MOD_ACTION_VALID(result->priority));
499
500 RDEBUG4("** [%i] %s - using default instruction priority for %s, %s",
501 stack->depth, __FUNCTION__,
502 fr_table_str_by_value(mod_rcode_table, result->rcode, "<invalid>"),
503 mod_action_name[result->priority]);
504 }
505
506 /*
507 * Deal with special priorities which indicate we need
508 * to do something in addition to modifying the frame's
509 * rcode.
510 */
511 switch (result->priority) {
512 /*
513 * The child's prioriy value indicates we
514 * should return from this frame.
515 */
517 RDEBUG4("** [%i] %s - action says to return with (%s %s)",
518 stack->depth, __FUNCTION__,
519 fr_table_str_by_value(mod_rcode_table, result->rcode, "<invalid>"),
520 mod_action_name[result->priority]);
521
522 *frame_result = UNLANG_RESULT_RCODE(result->rcode);
524
525 /*
526 * Reject means we should return, but
527 * with a reject rcode. This allows the
528 * user to change normally positive rcodes
529 * into negative ones.
530 *
531 * They could also just check the rcode
532 * after the module returns...
533 */
535 RDEBUG4("** [%i] %s - action says to return with (%s %s)",
536 stack->depth, __FUNCTION__,
538 mod_action_name[result->priority]);
539
540 *frame_result = UNLANG_RESULT_RCODE(RLM_MODULE_REJECT);
542
543 case MOD_ACTION_RETRY:
544 {
545 unlang_retry_t *retry = frame->retry;
546
547 RDEBUG4("** [%i] %s - action says to retry with",
548 stack->depth, __FUNCTION__);
549
550 /*
551 * If this is the first time doing the retry,
552 * then allocate the structure and set the timer.
553 */
554 if (!retry) {
555 MEM(frame->retry = retry = talloc_zero(stack, unlang_retry_t));
556
557 retry->request = request;
558 retry->depth = stack->depth;
559 retry->state = FR_RETRY_CONTINUE;
560 retry->count = 1;
561
562 /*
563 * Set a timer which automatically fires
564 * if there's a timeout. And parent it
565 * from the retry structure, so that the
566 * timer is automatically freed when the
567 * frame is cleaned up.
568 */
569 if (fr_time_delta_ispos(instruction->actions.retry.mrd)) {
570 if (fr_timer_in(retry, unlang_interpret_event_list(request)->tl, &retry->ev, instruction->actions.retry.mrd,
571 false, instruction_retry_handler, retry) < 0) {
572 RPEDEBUG("Failed inserting retry event");
573 *frame_result = UNLANG_RESULT_RCODE(RLM_MODULE_FAIL);
574 goto finalize;
575 }
576 }
577
578 } else {
579 /*
580 * We've been told to stop doing retries,
581 * probably from a timeout.
582 */
583 if (retry->state != FR_RETRY_CONTINUE) goto timeout;
584
585 /*
586 * Clamp it at the maximum count.
587 */
588 if (instruction->actions.retry.mrc > 0) {
589 retry->count++;
590
591 if (retry->count >= instruction->actions.retry.mrc) {
592 retry->state = FR_RETRY_MRC;
593
594 REDEBUG("Retries hit max_rtx_count (%u) - returning 'timeout'", instruction->actions.retry.mrc);
595
596 timeout:
598 goto finalize;
599 }
600 }
601 }
602
603 RINDENT();
604 if (instruction->actions.retry.mrc) {
605 RDEBUG("... retrying (%u/%u)", retry->count, instruction->actions.retry.mrc);
606 } else {
607 RDEBUG("... retrying");
608 }
609 REXDENT();
610
611 talloc_free(frame->state);
613 frame_state_init(stack, frame); /* Don't change p_result */
615 default:
616 break;
617 }
618 }
619
620finalize:
621 /*
622 * We're higher or equal to previous priority, remember this
623 * return code and priority.
624 */
625 if (result->priority >= frame_result->priority) {
626 fr_assert(MOD_ACTION_VALID(result->priority));
627 fr_assert(MOD_ACTION_VALID(frame_result->priority));
628
629 RDEBUG4("** [%i] %s - overwriting existing result (%s %s) with higher priority (%s %s)",
630 stack->depth, __FUNCTION__,
631 fr_table_str_by_value(mod_rcode_table, frame_result->rcode, "<invalid>"),
632 mod_action_name[frame_result->priority],
633 fr_table_str_by_value(mod_rcode_table, result->rcode, "<invalid>"),
634 mod_action_name[result->priority]);
635 *frame->p_result = *result;
636 }
637
638 /*
639 * Determine if we should continue processing siblings
640 * or pop the frame ending the section.
641 */
643}
644
645/** Function called to merge inter-stack-frame results
646 *
647 * This function is called whenever a frame is popped from the stack.
648 *
649 * 'result' is the result from the frame being popped, and 'frame' is the next highest frame in the stack.
650 *
651 * The logic here is very similar to result_eval(), with two important differences:
652 * - The priority of the lower frame is ignored, and the default priority of the higher frame is used.
653 * Unless the higher frame's priority is MOD_ACTION_NOT_SET, in which case the lower frame's priority is used.
654 */
655static inline CC_HINT(always_inline)
657{
658 unlang_stack_t *stack = request->stack;
659 unlang_result_t our_result = *result;
660
661 fr_assert(MOD_ACTION_VALID(result->priority));
662
663 /*
664 * When a stack frame is being popped, the priority of the
665 * source (lower) frame is ignored, and the default priority
666 * of the destination (higher) frame is used.
667 *
668 * We could (easily) add support for preserving the priority
669 * from the lower frame, if the priority of the higher frame
670 * was MOD_ACTION_NOT_SET, but there are no concrete use
671 * cases for this yet.
672 */
673 if (result->rcode != RLM_MODULE_NOT_SET) {
674 fr_assert(MOD_ACTION_VALID(frame->instruction->actions.actions[result->rcode]));
675 our_result.priority = frame->instruction->actions.actions[result->rcode];
676 }
677
678 RDEBUG4("** [%i] %s - using instruction priority for higher frame (%s, %s)",
679 stack->depth, __FUNCTION__,
680 fr_table_str_by_value(mod_rcode_table, our_result.rcode, "<invalid>"),
681 mod_action_name[our_result.priority]);
682
683 return result_calculate(request, frame, &our_result);
684}
685
686static inline CC_HINT(always_inline) void instruction_done_debug(request_t *request, unlang_stack_frame_t *frame, unlang_t const *instruction)
687{
688 if (has_debug_braces(instruction)) {
689 REXDENT();
690
691 /*
692 * If we're at debug level 1, don't emit the closing
693 * brace as the opening brace wasn't emitted.
694 *
695 * Not a typo, we don't want to print the scratch_result
696 * here, aka the ones the section actually returned,
697 * vs the section result, which may have just been left
698 * at defaults.
699 */
701 RDEBUG("# %s %s%s%s", frame->instruction->debug_name,
702 frame->p_result == &frame->section_result ? "(" : "((",
704 frame->p_result == &frame->section_result ? ")" : "))");
705 } else {
706 RDEBUG2("} # %s %s%s%s", frame->instruction->debug_name,
707 frame->p_result == &frame->section_result ? "(" : "((",
709 frame->p_result == &frame->section_result ? ")" : "))");
710 }
711 }
712}
713
714/** Evaluates all the unlang nodes in a section
715 *
716 * This function interprets a list of unlang instructions at a given level using the same
717 * stack frame, and pushes additional frames onto the stack as needed.
718 *
719 * This function can be seen as moving horizontally.
720 *
721 * @param[in] request The current request.
722 * @param[in] frame The current stack frame.
723 * @return
724 * - UNLANG_FRAME_ACTION_NEXT evaluate more instructions in the current stack frame
725 * which may not be the same frame as when this function
726 * was called.
727 * - UNLANG_FRAME_ACTION_POP the final result has been calculated for this frame.
728 */
729static inline CC_HINT(always_inline)
731{
732 unlang_stack_t *stack = request->stack;
733 unlang_result_t *scratch = &frame->scratch_result;
734
735 /*
736 * Loop over all the instructions in this list.
737 */
738 while (frame->instruction) {
739 unlang_t const *instruction = frame->instruction;
742
744
745 fr_assert(instruction->debug_name != NULL); /* if this happens, all bets are off. */
746 fr_assert(unlang_ops[instruction->type].interpret != NULL);
747 fr_assert(frame->process != NULL);
748
749 REQUEST_VERIFY(request);
750
751 /*
752 * We're running this frame, so it can't possibly be yielded.
753 */
754 if (is_yielded(frame)) {
755 RDEBUG("%s - Resuming execution", instruction->debug_name);
756 yielded_clear(frame);
757 }
758
759#ifndef NDEBUG
760 /*
761 * Failure testing!
762 */
763 if (request->ins_max) {
764 request->ins_count++;
765
766 if (request->ins_count >= request->ins_max) {
767 RERROR("Failing request due to maximum instruction count %" PRIu64, request->ins_max);
768
770 }
771 }
772#endif
773
774 /*
775 * We're not re-entering this frame, this is the first
776 * time we're evaluating this instruction, so we should
777 * print debug braces and indent.
778 */
779 if (!is_repeatable(frame)) {
780 if (has_debug_braces(frame)) {
781 RDEBUG2("%s {", instruction->debug_name);
782 RINDENT();
783 }
784 /*
785 * Clear the repeatable flag so this frame
786 * won't get executed again unless it specifically
787 * requests it.
788 *
789 * The flag may still be set again during the
790 * process function to indicate that the frame
791 * should be evaluated again.
792 */
793 } else {
794 repeatable_clear(frame);
795 }
796
797 /*
798 * Execute an operation
799 */
800 RDEBUG4("** [%i] %s >> %s", stack->depth, __FUNCTION__,
801 unlang_ops[instruction->type].name);
802
804
805 /*
806 * catch plays games with the frame so we skip
807 * to the next catch section at a given depth,
808 * it's not safe to access frame->instruction
809 * after this point, and the cached instruction
810 * should be used instead.
811 */
812 ua = frame->process(&frame->scratch_result, request, frame);
813
815
816 RDEBUG4("** [%i] %s << %s (%s %s)", stack->depth, __FUNCTION__,
818 fr_table_str_by_value(mod_rcode_table, scratch->rcode, "<INVALID>"),
819 mod_action_name[scratch->priority]);
820
821 /*
822 * If the frame is cancelled we ignore the
823 * return code of the process function and
824 * pop the frame. We'll keep popping
825 * frames until we hit a non-cancelled frame
826 * or the top frame.
827 */
828 if (is_unwinding(frame)) goto calculate_result;
829
830 switch (ua) {
831 /*
832 * The operation resulted in additional frames
833 * being pushed onto the stack, execution should
834 * now continue at the deepest frame.
835 */
837 fr_assert_msg(&stack->frame[stack->depth] > frame,
838 "Instruction %s returned UNLANG_ACTION_PUSHED_CHILD, "
839 "but stack depth was not increased",
840 instruction->name);
843
844 /*
845 * Yield control back to the scheduler, or whatever
846 * called the interpreter.
847 */
849 fr_assert_msg(&stack->frame[stack->depth] == frame,
850 "Instruction %s returned UNLANG_ACTION_YIELD, but pushed additional "
851 "frames for evaluation. Instruction should return UNLANG_ACTION_PUSHED_CHILD "
852 "instead", instruction->name);
854 yielded_set(frame);
855 RDEBUG4("** [%i] %s - yielding with current (%s %s)", stack->depth, __FUNCTION__,
856 fr_table_str_by_value(mod_rcode_table, scratch->rcode, "<invalid>"),
857 mod_action_name[scratch->priority]);
859
860 /*
861 * This action is intended to be returned by library
862 * functions. It reduces boilerplate.
863 */
865 /*
866 * Let unlang_calculate figure out if this is the final result
867 */
868 frame->scratch_result = UNLANG_RESULT_RCODE(RLM_MODULE_FAIL);
870
871 /*
872 * Instruction finished execution,
873 * check to see what we need to do next, and update
874 * the section rcode and priority.
875 */
877 calculate_result:
878 /*
879 * Merge in the scratch result _before_ printing
880 * out the rcode for the frame, so get what we'll
881 * actually return.
882 */
883 fa = result_calculate(request, frame, &frame->scratch_result);
884
885 instruction_done_debug(request, frame, instruction);
886
887 switch (fa) {
889 goto pop;
890
892 if (has_debug_braces(instruction)) {
893 REXDENT();
894 RDEBUG2("} # retrying the same section");
895 }
896 continue; /* with the current instruction */
897
898 default:
899 break;
900 }
901 break;
902
903 /*
904 * Execute the next instruction in this frame
905 */
907 if (has_debug_braces(instruction)) {
908 REXDENT();
909 RDEBUG2("}");
910 }
911 break;
912 } /* switch over return code from the interpret function */
913
914 frame_next(stack, frame);
915 }
916
917pop:
918 fr_assert(MOD_ACTION_VALID(frame->p_result->priority));
919
920 RDEBUG4("** [%i] %s - done current subsection with (%s %s), %s",
921 stack->depth, __FUNCTION__,
922 fr_table_str_by_value(mod_rcode_table, frame->p_result->rcode, "<invalid>"),
923 mod_action_name[frame->p_result->priority],
924 frame->p_result == &(frame->section_result) ? "will set higher frame rcode" : "will NOT set higher frame rcode (p_result)");
925
927}
928
929/** Run the interpreter for a current request
930 *
931 * This function runs the interpreter for a request. It deals with popping
932 * stack frames, and calaculating the final result for the frame.
933 *
934 * @param[in] request to run. If this is an internal request
935 * the request may be freed by the interpreter.
936 * @param[in] running Is the interpreter already running.
937 * @return The final request rcode.
938 */
939CC_HINT(hot) rlm_rcode_t unlang_interpret(request_t *request, bool running)
940{
941 unlang_stack_t *stack = request->stack;
942 unlang_interpret_t *intp = stack->intp;
943 unlang_stack_frame_t *frame = &stack->frame[stack->depth];
944
945 /*
946 * This is needed to ensure that if a frame is marked
947 * for unwinding whilst the request is yielded, we
948 * unwind the cancelled frame correctly, instead of
949 * continuing.
950 */
952
953#ifndef NDEBUG
954 if (DEBUG_ENABLED5) DEBUG("###### unlang_interpret is starting");
956#endif
957
958 fr_assert(!unlang_request_is_scheduled(request)); /* if we're running it, it can't be scheduled */
959 fr_assert_msg(intp, "request has no interpreter associated");
960
961 RDEBUG4("** [%i] %s - interpret entered", stack->depth, __FUNCTION__);
962 if (!running) intp->funcs.resume(request, intp->uctx);
963
964 for (;;) {
965 fr_assert(stack->depth > 0);
967
968 RDEBUG4("** [%i] %s - frame action %s", stack->depth, __FUNCTION__,
970 switch (fa) {
971 next:
972 RDEBUG4("** [%i] %s - frame action next", stack->depth, __FUNCTION__);
974
975 case UNLANG_FRAME_ACTION_NEXT: /* Evaluate the current frame */
976 frame = &stack->frame[stack->depth];
977 fa = frame_eval(request, frame);
978 if (fa != UNLANG_FRAME_ACTION_POP) continue;
979
980 RDEBUG4("** [%i] %s - frame action %s", stack->depth, __FUNCTION__,
983
984 case UNLANG_FRAME_ACTION_POP: /* Pop this frame and check the one beneath it */
985 {
986 bool top_frame = is_top_frame(frame);
987 bool private_result = is_private_result(frame);
988
989 unlang_result_t section_result = frame->section_result; /* record the result of the frame before we pop it*/
990
992
993 /*
994 * Triggers can run modules which pop, and then the stack is empty.
995 */
996 if (unlikely(stack->depth == 0)) {
997 break;
998 }
999
1000 /*
1001 * Head on back up the stack
1002 */
1003 frame_pop(request, stack);
1004 RDEBUG4("** [%i] %s - frame popped", stack->depth + 1, __FUNCTION__);
1005
1006 /*
1007 * Update the stack frame
1008 */
1009 frame = &stack->frame[stack->depth];
1010 DUMP_STACK;
1011
1012 /*
1013 * Transition back to the C stack
1014 *
1015 * We still need to merge in the previous frame's result,
1016 * but we don't care about the action, as we're returning.
1017 */
1018 if (top_frame) {
1019 if (!private_result) result_calculate(request, frame, &section_result);
1020 break; /* stop */
1021 }
1022
1023 /*
1024 * Don't process the section result for a frame if
1025 * the result is being consumed by a module.
1026 */
1027 if (private_result) {
1029 /*
1030 * Merge lower frame into higher frame.
1031 *
1032 * this _MUST_ be done, even on resume, because the
1033 * section result needs to be updated for the frame
1034 * being resumed, in case it cares about the rcode
1035 * like transaction sections.
1036 */
1037 } else {
1038 fa = result_pop(request, frame, &section_result);
1039 }
1040
1041 /*
1042 * Resume a "foreach" loop, or a "load-balance" section
1043 * or anything else that needs to be checked on the way
1044 * back on up the stack. Here we just resume evaluating
1045 * the frame, we don't advance the instruction.
1046 */
1047 if (!is_unwinding(frame) && is_repeatable(frame)) goto next;
1048
1049 /*
1050 * Close out the section we entered earlier
1051 */
1052 instruction_done_debug(request, frame, frame->instruction);
1053
1055
1056 /*
1057 * If we're continuing after popping a frame
1058 * then we advance the instruction else we
1059 * end up executing the same code over and over...
1060 */
1061 switch (fa) {
1063 DEBUG4("** [%i] %s - continuing after subsection with (%s %s)",
1064 stack->depth, __FUNCTION__,
1067 frame_next(stack, frame);
1068 goto next;
1069
1070 /*
1071 * Else if we're really done with this frame
1072 * print some helpful debug...
1073 */
1074 default:
1075 RDEBUG4("** [%i] %s - done current subsection with (%s %s)",
1076 stack->depth, __FUNCTION__,
1079 continue;
1080 }
1081
1082 }
1083
1085 /* Cannot yield from a nested call to unlang_interpret */
1086 fr_assert(!running);
1087
1088 RDEBUG4("** [%i] %s - interpret yielding", stack->depth, __FUNCTION__);
1089 intp->funcs.yield(request, intp->uctx);
1090 return RLM_MODULE_NOT_SET;
1091
1092 case UNLANG_FRAME_ACTION_RETRY: /* retry the current frame */
1093 goto next;
1094 }
1095 break;
1096 }
1097
1098 fr_assert(stack->depth >= 0);
1099
1100 /*
1101 * We're at the top frame, return the result from the
1102 * stack, and get rid of the top frame.
1103 */
1104 RDEBUG4("** [%i] %s - interpret exiting, returning (%s)", stack->depth, __FUNCTION__,
1106
1107 DUMP_STACK;
1108
1109 {
1110 rlm_rcode_t rcode;
1111 /*
1112 * Record this now as the done functions may free
1113 * the request.
1114 *
1115 * Note: We use p_result here, as that's where the
1116 * result of evaluating the frame was written.
1117 * We don't use the section_result, as that may have
1118 * been left as its default value which may be 0
1119 * (reject).
1120 */
1121 rcode = frame->p_result->rcode;
1122
1123 /*
1124 * This usually means the request is complete in its
1125 * entirety.
1126 */
1127 if ((stack->depth == 0) && !running) unlang_interpret_request_done(request);
1128
1129 return rcode;
1130 }
1131}
1132
1134 .self = {
1136 .name = "empty-group",
1137 .debug_name = "empty-group",
1138 .actions = {
1139 .actions = {
1151 },
1152 .retry = RETRY_INIT,
1153 },
1154 },
1155 .children = {
1156 .head = {
1157 .entry = {
1158 .prev = &empty_group.children.head.entry,
1159 .next = &empty_group.children.head.entry,
1160 }
1161 },
1162 },
1163};
1164
1165/** Push a configuration section onto the request stack for later interpretation.
1166 *
1167 */
1169{
1170 unlang_t *instruction = NULL;
1171
1172 /*
1173 * Interpretable unlang instructions are stored as CONF_DATA
1174 * associated with sections.
1175 */
1176 if (cs) {
1177 instruction = (unlang_t *)cf_data_value(cf_data_find(cs, unlang_group_t, NULL));
1178 if (!instruction) {
1179 REDEBUG("Failed to find pre-compiled unlang for section %s ... { ... }",
1180 cf_section_name1(cs));
1181 return -1;
1182 }
1183 }
1184
1185 return unlang_interpret_push_instruction(p_result, request, instruction, conf);
1186}
1187
1188/** Push an instruction onto the request stack for later interpretation.
1189 *
1190 */
1192{
1193 unlang_stack_t *stack = request->stack;
1194
1195 if (!instruction) {
1196 instruction = unlang_group_to_generic(&empty_group);
1197 }
1198
1199 /*
1200 * Push the default action, and the instruction which has
1201 * no action.
1202 */
1203 if (unlang_interpret_push(p_result, request, instruction, conf, UNLANG_NEXT_SIBLING) < 0) {
1204 return -1;
1205 }
1206
1207 RDEBUG4("** [%i] %s - substack begins", stack->depth, __FUNCTION__);
1208
1209 return 0;
1210}
1211
1212/** Allocate a new unlang stack
1213 *
1214 * @param[in] ctx to allocate stack in.
1215 * @return
1216 * - A new stack on success.
1217 * - NULL on OOM.
1218 */
1219void *unlang_interpret_stack_alloc(TALLOC_CTX *ctx)
1220{
1221 /*
1222 * Should never be evaluated, is just here to reduce
1223 * branches, so we don't need to check for frame->instruction.
1224 */
1225 static unlang_t unlang_instruction = {
1226 .debug_name = "top",
1227 .actions = DEFAULT_MOD_ACTIONS,
1228 };
1229
1231
1232 /*
1233 * If we have talloc_pooled_object allocate the
1234 * stack as a combined chunk/pool, with memory
1235 * to hold at mutable data for at least a quarter
1236 * of the maximum number of stack frames.
1237 *
1238 * Having a dedicated pool for mutable stack data
1239 * means we don't have memory fragmentations issues
1240 * as we would if request were used as the pool.
1241 *
1242 * This number is pretty arbitrary, but it seems
1243 * like too low level to make into a tuneable.
1244 */
1245 MEM(stack = talloc_zero_pooled_object(ctx, unlang_stack_t, UNLANG_STACK_MAX, 128)); /* 128 bytes per state */
1246 stack->frame[0].p_result = &stack->frame[0].section_result;
1247 stack->frame[0].scratch_result = UNLANG_RESULT_NOT_SET;
1248 stack->frame[0].section_result = UNLANG_RESULT_NOT_SET;
1249 stack->frame[0].instruction = &unlang_instruction; /* The top frame has no instruction, so we use a dummy one */
1250
1251 return stack;
1252}
1253
1254/** Indicate to the caller of the interpreter that this request is complete
1255 *
1256 */
1258{
1259 unlang_stack_t *stack = request->stack;
1260 unlang_interpret_t *intp;
1261
1262 if (!fr_cond_assert(stack != NULL)) return;
1263
1264 intp = stack->intp;
1265
1266 request->master_state = REQUEST_DONE;
1267 switch (request->type) {
1269 intp->funcs.done_external(request, frame_current(request)->section_result.rcode, intp->uctx);
1270 break;
1271
1273 intp->funcs.done_internal(request, frame_current(request)->section_result.rcode, intp->uctx);
1274 break;
1275
1277 intp->funcs.done_detached(request, frame_current(request)->section_result.rcode, intp->uctx); /* Callback will usually free the request */
1278 break;
1279 }
1280}
1281
1282/** Tell the interpreter to detach the request
1283 *
1284 * This function should not be called directly use unlang_interpret_signal(request, FR_SIGNAL_DETACH) instead.
1285 * This will ensure all frames on the request's stack receive the detach signal.
1286 */
1287static inline CC_HINT(always_inline)
1289{
1290 unlang_stack_t *stack = request->stack;
1291 unlang_interpret_t *intp;
1292
1293 if (!fr_cond_assert(stack != NULL)) return;
1294
1295 if (!request_is_detachable(request)) return;
1296
1297 intp = stack->intp;
1298
1299 intp->funcs.detach(request, intp->uctx);
1300}
1301
1303{
1304 unlang_stack_t *stack = request->stack;
1305 unlang_interpret_t *intp;
1306
1307 if (!fr_cond_assert(stack != NULL)) return;
1308
1309 intp = stack->intp;
1310
1311 request->priority = priority;
1312
1313 if (intp->funcs.prioritise) intp->funcs.prioritise(request, intp->uctx);
1314}
1315
1316/** Cancel any pending retry
1317 *
1318 * @param[in] request The current request.
1319 */
1321{
1322 unlang_stack_t *stack = request->stack;
1323 unlang_stack_frame_t *frame = &stack->frame[stack->depth];
1324
1325 TALLOC_FREE(frame->retry);
1326}
1327
1328
1329/** Delivers a frame to one or more frames in the stack
1330 *
1331 * This is typically called via an "async" action, i.e. an action outside
1332 * of the normal processing of the request.
1333 *
1334 * For FR_SIGNAL_CANCEL all frames are marked up for cancellation, but the
1335 * cancellation is handled by the interpret.
1336 *
1337 * Other signal types are delivered immediately, inrrespecitve of whether
1338 * the request is currently being processed or not.
1339 *
1340 * Signaling stops at the "limit" frame. This is so that keywords
1341 * such as "timeout" and "limit" can signal frames *lower* than theirs
1342 * to stop, but then continue with their own work.
1343 *
1344 * @note It's better (clearer) to use one of the unwind_* functions
1345 * unless the entire request is being cancelled.
1346 *
1347 * @param[in] request The current request.
1348 * @param[in] action to signal.
1349 * @param[in] limit the frame at which to stop signaling.
1350 */
1351void unlang_stack_signal(request_t *request, fr_signal_t action, int limit)
1352{
1353 unlang_stack_frame_t *frame;
1354 unlang_stack_t *stack = request->stack;
1355 int i, depth = stack->depth;
1356
1357 (void)talloc_get_type_abort(request, request_t); /* Check the request hasn't already been freed */
1358
1359 fr_assert(stack->depth >= 1);
1360
1361 /*
1362 * Does not complete the unwinding here, just marks
1363 * up the frames for unwinding. The request must
1364 * be marked as runnable to complete the cancellation.
1365 */
1366 if (action == FR_SIGNAL_CANCEL) unwind_to_depth(stack, limit);
1367
1368 /*
1369 * Walk back up the stack, calling signal handlers
1370 * to cancel any pending operations and free/release
1371 * any resources.
1372 *
1373 * There may be multiple resumption points in the
1374 * stack, as modules can push xlats and function
1375 * calls.
1376 *
1377 * Note: Slightly confusingly, a cancellation signal
1378 * can still be delivered to a frame that is not
1379 * cancellable, but the frame won't be automatically
1380 * unwound.
1381 */
1382 for (i = depth; i >= limit; i--) {
1383 frame = &stack->frame[i];
1384 if (frame->signal) {
1385 frame->signal(request, frame, action);
1386
1387 /*
1388 * Once the cancellation function has been
1389 * called, the frame is no longer in a state
1390 * where it can accept further signals.
1391 */
1392 if (action == FR_SIGNAL_CANCEL) frame->signal = NULL;
1393
1394 /*
1395 * If the frame is cancelled, we don't do any retries.
1396 */
1397 TALLOC_FREE(frame->retry);
1398 }
1399 }
1400}
1401
1402/** Send a signal (usually stop) to a request
1403 *
1404 * This is typically called via an "async" action, i.e. an action
1405 * outside of the normal processing of the request.
1406 *
1407 * @note This does NOT immediately stop the request, it just deliveres
1408 * signals, and in the case of a cancel, marks up frames for unwinding
1409 * and adds it to the runnable queue if it's yielded.
1410 *
1411 * @note This function should be safe to call anywhere.
1412 *
1413 * @param[in] request The current request.
1414 * @param[in] action to signal.
1415 */
1417{
1418 unlang_stack_t *stack = request->stack;
1419
1420 switch (action) {
1421 case FR_SIGNAL_DETACH:
1422 /*
1423 * Ensure the request is able to be detached
1424 * else don't signal.
1425 */
1426 if (!fr_cond_assert(request_is_detachable(request))) return;
1427 break;
1428
1429 default:
1430 break;
1431 }
1432
1433 /*
1434 * Requests that haven't been run through the interpreter
1435 * yet should have a stack depth of zero, so we don't
1436 * need to do anything.
1437 */
1438 if (!stack || stack->depth == 0) return;
1439
1440 unlang_stack_signal(request, action, 1);
1441
1442 switch (action) {
1443 case FR_SIGNAL_CANCEL:
1444 {
1445 unlang_stack_frame_t *frame = &stack->frame[stack->depth];
1446 /*
1447 * Let anything that cares, know that the
1448 * request was forcefully stopped.
1449 */
1450 request->master_state = REQUEST_STOP_PROCESSING;
1451
1452 /*
1453 * Give cancelled requests the highest priority
1454 * to get them to release resources ASAP.
1455 */
1456 unlang_interpret_request_prioritise(request, UINT32_MAX);
1457
1458 /*
1459 * If the request is yielded, mark it as runnable
1460 *
1461 * If the request was _not_ cancelled, it means
1462 * it's not cancellable, and we need to let the
1463 * request progress normally.
1464 *
1465 * A concrete example of this, is the parent of
1466 * subrequests, which must not continue until
1467 * the subrequest is done.
1468 */
1469 if (stack && is_yielded(frame) && is_unwinding(frame) && !unlang_request_is_scheduled(request)) {
1471 }
1472 }
1473 break;
1474
1475 case FR_SIGNAL_DETACH:
1476 /*
1477 * Cleanup any cross-request pointers, and mark the
1478 * request as detached. When the request completes it
1479 * should by automatically freed.
1480 */
1482 break;
1483
1484 default:
1485 break;
1486 }
1487}
1488
1490{
1491 unlang_retry_t *retry = talloc_get_type_abort(ctx, unlang_retry_t);
1492 request_t *request = talloc_get_type_abort(retry->request, request_t);
1493
1494 RDEBUG("retry timeout reached, signalling interpreter to cancel.");
1495
1496 /*
1497 * Signal all lower frames to exit.
1498 */
1499 unlang_stack_signal(request, FR_SIGNAL_CANCEL, retry->depth);
1500
1501 retry->state = FR_RETRY_MRD;
1503}
1504
1506{
1507 request_t *request = talloc_get_type_abort(ctx, request_t);
1508
1509 RDEBUG("Maximum timeout reached, signalling interpreter to stop the request.");
1510
1511 /*
1512 * Stop the entire request.
1513 */
1515}
1516
1517
1518/** Set a timeout for a request.
1519 *
1520 * The timeout is associated with the current stack frame.
1521 *
1522 */
1524{
1525 unlang_stack_t *stack = request->stack;
1526 unlang_stack_frame_t *frame = &stack->frame[stack->depth];
1527 unlang_retry_t *retry;
1528 TALLOC_CTX *frame_ctx;
1529
1530 fr_assert(!frame->retry);
1532
1533 frame_ctx = frame->state;
1534 if (!frame_ctx) frame_ctx = stack;
1535
1536 frame->retry = retry = talloc_zero(frame_ctx, unlang_retry_t);
1537 if (!frame->retry) return -1;
1538
1539 retry->request = request;
1540 retry->depth = stack->depth;
1541 retry->state = FR_RETRY_CONTINUE;
1542 retry->count = 1;
1543
1544 return fr_timer_in(retry, unlang_interpret_event_list(request)->tl, &retry->ev, timeout,
1545 false, instruction_timeout_handler, request);
1546}
1547
1548
1549/** Return the depth of the request's stack
1550 *
1551 */
1553{
1554 unlang_stack_t *stack = request->stack;
1555
1556 return stack->depth;
1557}
1558
1559/** Get the last instruction result OR the last frame that was popped
1560 *
1561 * @param[in] request The current request.
1562 * @return the current rcode for the frame.
1563 */
1565{
1566 return frame_current(request)->p_result->rcode;
1567}
1568
1569/** Get the last instruction priority OR the last frame that was popped
1570 *
1571 * @param[in] request The current request.
1572 * @return the current rcode for the frame.
1573 */
1578
1579/** Get the last instruction result OR the last frame that was popped
1580 *
1581 * @param[in] request The current request.
1582 * @return the current result for the frame.
1583 */
1585{
1586 return frame_current(request)->p_result;
1587}
1588
1589/** Return whether a request is currently scheduled
1590 *
1591 */
1593{
1594 unlang_stack_t *stack = request->stack;
1595 unlang_interpret_t *intp = stack->intp;
1596
1597 return intp->funcs.scheduled(request, intp->uctx);
1598}
1599
1600/** Return whether a request has been cancelled
1601 */
1603{
1604 return (request->master_state == REQUEST_STOP_PROCESSING);
1605}
1606
1607/** Return whether a request has been marked done
1608 */
1610{
1611 return (request->master_state == REQUEST_DONE);
1612}
1613
1614/** Check if a request as resumable.
1615 *
1616 * @param[in] request The current request.
1617 * @return
1618 * - true if the request is resumable (i.e. has yielded)
1619 * - false if the request is not resumable (i.e. has not yielded)
1620 */
1622{
1623 unlang_stack_t *stack = request->stack;
1624 unlang_stack_frame_t *frame = &stack->frame[stack->depth];
1625
1626 return is_yielded(frame);
1627}
1628
1629/** Mark a request as resumable.
1630 *
1631 * It's not called "unlang_interpret", because it doesn't actually
1632 * resume the request, it just schedules it for resumption.
1633 *
1634 * @note that this schedules the request for resumption. It does not immediately
1635 * start running the request.
1636 *
1637 * @param[in] request The current request.
1638 */
1640{
1641 unlang_stack_t *stack = request->stack;
1642 unlang_interpret_t *intp = stack->intp;
1643 unlang_stack_frame_t *frame = &stack->frame[stack->depth];
1644
1645 bool scheduled = unlang_request_is_scheduled(request);
1646
1647 /*
1648 * The request hasn't yielded, OR it's already been
1649 * marked as runnable. Don't do anything.
1650 *
1651 * The IO code, or children have no idea where they're
1652 * being called from. They just ask to mark the parent
1653 * resumable when they're done. So we have to check here
1654 * if this request is resumable.
1655 *
1656 * If the parent called the child directly, then the
1657 * parent hasn't yielded, so it isn't resumable. When
1658 * the child is done, the parent will automatically
1659 * continue running. We therefore don't need to insert
1660 * the parent into the backlog.
1661 *
1662 * Multiple child request may also mark a parent request
1663 * runnable, before the parent request starts running.
1664 */
1665 if (!is_yielded(frame) || scheduled) {
1666 RDEBUG3("Not marking request %s as runnable due to%s%s",
1667 request->name,
1668 !is_yielded(frame) ?
1669 " it not being yielded " : "", scheduled ? " it already being scheduled" : "");
1670 return;
1671 }
1672
1673 RDEBUG3("Interpreter - Request marked as runnable");
1674
1675 intp->funcs.mark_runnable(request, intp->uctx);
1676}
1677
1678/** Get a talloc_ctx which is valid only for this frame
1679 *
1680 * @param[in] request The current request.
1681 * @return
1682 * - a TALLOC_CTX which is valid only for this stack frame
1683 */
1685{
1686 unlang_stack_t *stack = request->stack;
1687 unlang_stack_frame_t *frame = &stack->frame[stack->depth];
1688
1689 if (frame->state) return (TALLOC_CTX *)frame->state;
1690
1691 /*
1692 * If the frame doesn't ordinarily have a
1693 * state, assume the caller knows what it's
1694 * doing and allocate one.
1695 */
1696 return (TALLOC_CTX *)(frame->state = talloc_new(request));
1697}
1698
1700 { .required = false, .single = true, .type = FR_TYPE_TIME_DELTA },
1702};
1703
1704static xlat_action_t unlang_cancel_xlat(TALLOC_CTX *ctx, fr_dcursor_t *out,
1705 UNUSED xlat_ctx_t const *xctx,
1706 request_t *request, fr_value_box_list_t *args);
1707
1708/** Signal the request to stop executing
1709 *
1710 * The request can't be running at this point because we're in the event
1711 * loop. This means the request is always in a consistent state when
1712 * the timeout event fires, even if that's state is waiting on I/O.
1713 */
1715{
1716 request_t *request = talloc_get_type_abort(uctx, request_t);
1717
1718 RDEBUG2("Request canceled by dynamic timeout");
1719 /*
1720 * Cleans up the memory allocated to hold
1721 * the pointer, not the event itself.
1722 */
1723 talloc_free(request_data_get(request, (void *)unlang_cancel_xlat, 0));
1724
1726}
1727
1728/** Allows a request to dynamically alter its own lifetime
1729 *
1730 * %cancel(<timeout>)
1731 *
1732 * If timeout is 0, then the request is immediately cancelled.
1733 */
1735 UNUSED xlat_ctx_t const *xctx,
1736 request_t *request, fr_value_box_list_t *args)
1737{
1738 fr_value_box_t *timeout;
1740 fr_timer_t **ev_p, **ev_p_og;
1741 fr_value_box_t *vb;
1742 fr_time_t when = fr_time_from_sec(0); /* Invalid clang complaints if we don't set this */
1743
1744 XLAT_ARGS(args, &timeout);
1745
1746 /*
1747 * No timeout means cancel immediately, so yield allowing
1748 * the interpreter to run the event we added to cancel
1749 * the request.
1750 *
1751 * We call unlang_xlat_yield to keep the interpreter happy
1752 * as it expects to see a resume function set.
1753 */
1754 if (!timeout || fr_time_delta_eq(timeout->vb_time_delta, fr_time_delta_from_sec(0))) {
1756 return XLAT_ACTION_DONE;
1757 }
1758
1759 /*
1760 * First see if we already have a timeout event
1761 * that was previously added by this xlat.
1762 */
1763 ev_p = ev_p_og = request_data_get(request, (void *)unlang_cancel_xlat, 0);
1764 if (ev_p) {
1765 if (*ev_p) when = fr_timer_when(*ev_p); /* *ev_p should never be NULL, really... */
1766 } else {
1767 /*
1768 * Must not be parented from the request
1769 * as this is freed by request data.
1770 */
1771 MEM(ev_p = talloc_zero(NULL, fr_timer_t *));
1772 }
1773
1774 if (unlikely(fr_timer_in(ev_p, el->tl, ev_p,
1775 timeout ? timeout->vb_time_delta : fr_time_delta_from_sec(0),
1776 false, unlang_cancel_event, request) < 0)) {
1777 RPERROR("Failed inserting cancellation event");
1778 talloc_free(ev_p);
1779 return XLAT_ACTION_FAIL;
1780 }
1781 if (unlikely(request_data_add(request, (void *)unlang_cancel_xlat, 0,
1782 UNCONST(fr_timer_t **, ev_p), true, true, false) < 0)) {
1783 RPERROR("Failed associating cancellation event with request");
1784 talloc_free(ev_p);
1785 return XLAT_ACTION_FAIL;
1786 }
1787
1788 if (ev_p_og) {
1789 MEM(vb = fr_value_box_alloc(ctx, FR_TYPE_TIME_DELTA, NULL));
1790
1791 /*
1792 * Return how long before the previous
1793 * cancel event would have fired.
1794 *
1795 * This can be useful for doing stacked
1796 * cancellations in policy.
1797 */
1798 vb->vb_time_delta = fr_time_sub(when, unlang_interpret_event_list(request)->tl->time());
1800 }
1801
1802 /*
1803 * No value if this is the first cleanup event
1804 */
1805 return XLAT_ACTION_DONE;
1806}
1807
1809 { .required = true, .single = true, .type = FR_TYPE_STRING },
1811};
1812
1813/** Get information about the interpreter state
1814 *
1815 * @ingroup xlat_functions
1816 */
1818 UNUSED xlat_ctx_t const *xctx,
1819 request_t *request, fr_value_box_list_t *in)
1820{
1821 unlang_stack_t *stack = request->stack;
1822 int depth = stack->depth;
1823 unlang_stack_frame_t *frame;
1824 unlang_t const *instruction;
1825 fr_value_box_t *arg = fr_value_box_list_head(in);
1826 char const *fmt = arg->vb_strvalue;
1827 fr_value_box_t *vb;
1828
1829 MEM(vb = fr_value_box_alloc_null(ctx));
1830
1831 /*
1832 * Find the correct stack frame.
1833 */
1834 while (*fmt == '.') {
1835 if (depth <= 1) {
1836 if (fr_value_box_bstrndup(vb, vb, NULL, "<underflow>", 11, false) < 0) {
1837 error:
1838 talloc_free(vb);
1839 return XLAT_ACTION_FAIL;
1840 }
1841 goto finish;
1842 }
1843
1844 fmt++;
1845 depth--;
1846 }
1847
1848 /*
1849 * Get the current instruction.
1850 */
1851 frame = &stack->frame[depth];
1852 instruction = frame->instruction;
1853
1854 /*
1855 * Nothing there...
1856 */
1857 if (!instruction) {
1858 talloc_free(vb);
1859 return XLAT_ACTION_DONE;
1860 }
1861
1862 /*
1863 * How deep the current stack is.
1864 */
1865 if (strcmp(fmt, "depth") == 0) {
1866 fr_value_box_int32(vb, NULL, depth, false);
1867 goto finish;
1868 }
1869
1870 /*
1871 * The current module
1872 */
1873 if (strcmp(fmt, "module") == 0) {
1874 if (fr_value_box_strdup(vb, vb, NULL, request->module, false) < 0) goto error;
1875
1876 goto finish;
1877 }
1878
1879 /*
1880 * Name of the instruction.
1881 */
1882 if (strcmp(fmt, "name") == 0) {
1883 if (fr_value_box_bstrndup(vb, vb, NULL, instruction->name,
1884 strlen(instruction->name), false) < 0) goto error;
1885 goto finish;
1886 }
1887
1888 /*
1889 * The request processing stage.
1890 */
1891 if (strcmp(fmt, "processing_stage") == 0) {
1892 if (fr_value_box_strdup(vb, vb, NULL, request->component, false) < 0) goto error;
1893
1894 goto finish;
1895 }
1896
1897 /*
1898 * The current return code.
1899 */
1900 if (strcmp(fmt, "rcode") == 0) {
1901 if (fr_value_box_strdup(vb, vb, NULL, fr_table_str_by_value(rcode_table, request->rcode, "<INVALID>"), false) < 0) goto error;
1902
1903 goto finish;
1904 }
1905
1906 /*
1907 * The virtual server handling the request
1908 */
1909 if (strcmp(fmt, "server") == 0) {
1910 request_t *our_request;
1911 CONF_SECTION *server = NULL;
1912
1913 /*
1914 * If we're being pedantic subrequests don't have a virtual
1915 * server associated with them unless they go call {}.
1916 *
1917 * But we're not being pendantic, so go back up the request
1918 * list ooking for a call frame.
1919 *
1920 * Unfortunately for detached subrequests we still won't find
1921 * the actual virtual server...
1922 */
1923 for (our_request = request; our_request && server == NULL; our_request = our_request->parent) {
1924 server = unlang_call_current(our_request);
1925 }
1926 if (server == NULL) goto finish;
1927
1928 if (fr_value_box_strdup(vb, vb, NULL, cf_section_name2(server), false) < 0) goto error;
1929
1930 goto finish;
1931 }
1932
1933 /*
1934 * Unlang instruction type.
1935 */
1936 if (strcmp(fmt, "type") == 0) {
1937 if (fr_value_box_bstrndup(vb, vb, NULL, unlang_ops[instruction->type].name,
1938 strlen(unlang_ops[instruction->type].name), false) < 0) goto error;
1939
1940 goto finish;
1941 }
1942
1943 /*
1944 * All of the remaining things need a CONF_ITEM.
1945 */
1946 if (!instruction->ci) {
1947 if (fr_value_box_bstrndup(vb, vb, NULL, "<INVALID>", 9, false) < 0) goto error;
1948
1949 goto finish;
1950 }
1951
1952 /*
1953 * Line number of the current section.
1954 */
1955 if (strcmp(fmt, "line") == 0) {
1956 fr_value_box_int32(vb, NULL, cf_lineno(instruction->ci), false);
1957
1958 goto finish;
1959 }
1960
1961 /*
1962 * Filename of the current section.
1963 */
1964 if (strcmp(fmt, "filename") == 0) {
1965 if (fr_value_box_strdup(vb, vb, NULL, cf_filename(instruction->ci), false) < 0) goto error;
1966
1967 goto finish;
1968 }
1969
1970finish:
1971 if (vb->type != FR_TYPE_NULL) {
1973 } else {
1974 talloc_free(vb);
1975 }
1976
1977 return XLAT_ACTION_DONE;
1978}
1979
1980/** Initialize a unlang compiler / interpret.
1981 *
1982 * @param[in] ctx to bind lifetime of the interpret to.
1983 * Shouldn't be any free order issues here as
1984 * the interpret itself has no state.
1985 * But event loop should be stopped before
1986 * freeing the interpret.
1987 * @param[in] el for any timer or I/O events.
1988 * @param[in] funcs Callbacks to used to communicate request
1989 * state to our owner.
1990 * @param[in] uctx Data to pass to callbacks.
1991 */
1993 fr_event_list_t *el, unlang_request_func_t *funcs, void *uctx)
1994{
1995 unlang_interpret_t *intp;
1996
1997 fr_assert(funcs->init_internal);
1998
1999 fr_assert(funcs->done_internal);
2000 fr_assert(funcs->done_detached);
2001 fr_assert(funcs->done_external);
2002
2003 fr_assert(funcs->detach);
2004 fr_assert(funcs->yield);
2005 fr_assert(funcs->resume);
2006 fr_assert(funcs->mark_runnable);
2007 fr_assert(funcs->scheduled);
2008
2009 MEM(intp = talloc(ctx, unlang_interpret_t));
2010 *intp = (unlang_interpret_t){
2011 .el = el,
2012 .funcs = *funcs,
2013 .uctx = uctx
2014 };
2015
2016 return intp;
2017}
2018
2019/** Discard the bottom most frame on the request's stack
2020 *
2021 * This is used for cleaning up after errors. i.e. the caller
2022 * uses a push function, and experiences an error and needs to
2023 * remove the frame that was just pushed.
2024 */
2026{
2027 frame_pop(request, request->stack);
2028}
2029
2030/** Set a specific interpreter for a request
2031 *
2032 */
2034{
2035 unlang_stack_t *stack = request->stack;
2036 stack->intp = intp;
2037}
2038
2039/** Get the interpreter set for a request
2040 *
2041 */
2043{
2044 unlang_stack_t *stack = request->stack;
2045
2046 return stack->intp;
2047}
2048
2049/** Get the event list for the current interpreter
2050 *
2051 */
2053{
2054 unlang_stack_t *stack = request->stack;
2055
2056 if (!stack->intp) return NULL;
2057
2058 return stack->intp->el;
2059}
2060
2061/** Set the default interpreter for this thread
2062 *
2063 */
2065{
2066 if (intp) (void)talloc_get_type_abort(intp, unlang_interpret_t);
2067
2068 intp_thread_default = intp;
2069}
2070
2071/** Get the default interpreter for this thread
2072 *
2073 * This allows detached requests to be executed asynchronously
2074 */
2076{
2077 if (!intp_thread_default) return NULL;
2078
2079 return talloc_get_type_abort(intp_thread_default, unlang_interpret_t);
2080}
2081
2083{
2084 xlat_t *xlat;
2085 /*
2086 * Should be void, but someone decided not to register multiple xlats
2087 * breaking the convention we use everywhere else in the server...
2088 */
2089 if (unlikely((xlat = xlat_func_register(ctx, "interpreter", unlang_interpret_xlat, FR_TYPE_VOID)) == NULL)) return -1;
2091
2092 if (unlikely((xlat = xlat_func_register(ctx, "cancel", unlang_cancel_xlat, FR_TYPE_VOID)) == NULL)) return -1;
2094
2095 return 0;
2096}
#define RETURN_UNLANG_ACTION_FATAL
Definition action.h:44
unlang_action_t
Returned by unlang_op_t calls, determine the next action of the interpreter.
Definition action.h:35
@ UNLANG_ACTION_PUSHED_CHILD
unlang_t pushed a new child onto the stack, execute it instead of continuing.
Definition action.h:39
@ UNLANG_ACTION_EXECUTE_NEXT
Execute the next unlang_t.
Definition action.h:38
@ UNLANG_ACTION_FAIL
Encountered an unexpected error.
Definition action.h:36
@ UNLANG_ACTION_CALCULATE_RESULT
Calculate a new section rlm_rcode_t value.
Definition action.h:37
@ UNLANG_ACTION_YIELD
Temporarily pause execution until an event occurs.
Definition action.h:41
va_list args
Definition acutest.h:772
static int const char * fmt
Definition acutest.h:575
#define UNCONST(_type, _ptr)
Remove const qualification from a pointer.
Definition build.h:167
#define RCSID(id)
Definition build.h:487
#define L(_str)
Helper for initialising arrays of string literals.
Definition build.h:209
#define FALL_THROUGH
clang 10 doesn't recognised the FALL-THROUGH comment anymore
Definition build.h:324
#define unlikely(_x)
Definition build.h:383
#define UNUSED
Definition build.h:317
#define NUM_ELEMENTS(_t)
Definition build.h:339
CONF_SECTION * unlang_call_current(request_t *request)
Return the last virtual server that was called.
Definition call.c:214
A section grouping multiple CONF_PAIR.
Definition cf_priv.h:101
char const * cf_section_name2(CONF_SECTION const *cs)
Return the second identifier of a CONF_SECTION.
Definition cf_util.c:1184
void * cf_data_value(CONF_DATA const *cd)
Return the user assigned value of CONF_DATA.
Definition cf_util.c:1743
char const * cf_section_name1(CONF_SECTION const *cs)
Return the second identifier of a CONF_SECTION.
Definition cf_util.c:1170
#define cf_lineno(_cf)
Definition cf_util.h:104
#define cf_data_find(_cf, _type, _name)
Definition cf_util.h:243
#define cf_filename(_cf)
Definition cf_util.h:107
fr_table_num_sorted_t const mod_rcode_table[]
Definition compile.c:75
static int fr_dcursor_append(fr_dcursor_t *cursor, void *v)
Insert a single item at the end of the list.
Definition dcursor.h:408
static int fr_dcursor_insert(fr_dcursor_t *cursor, void *v)
Insert directly after the current item.
Definition dcursor.h:437
#define fr_cond_assert(_x)
Calls panic_action ifndef NDEBUG, else logs error and evaluates to value of _x.
Definition debug.h:131
#define fr_assert_msg(_x, _msg,...)
Calls panic_action ifndef NDEBUG, else logs error and causes the server to exit immediately with code...
Definition debug.h:202
#define MEM(x)
Definition debug.h:36
#define DEBUG(fmt,...)
Definition dhcpclient.c:39
static fr_slen_t in
Definition dict.h:884
static xlat_action_t unlang_interpret_xlat(TALLOC_CTX *ctx, fr_dcursor_t *out, UNUSED xlat_ctx_t const *xctx, request_t *request, fr_value_box_list_t *in)
Get information about the interpreter state.
Definition interpret.c:1817
talloc_free(hp)
void unlang_interpret_request_prioritise(request_t *request, uint32_t priority)
Definition interpret.c:1302
static size_t unlang_action_table_len
Definition interpret.c:52
void stack_dump_with_actions(request_t *request)
Definition interpret.c:253
static fr_table_num_ordered_t const unlang_frame_action_table[]
Definition interpret.c:54
static void unlang_interpret_request_detach(request_t *request)
Tell the interpreter to detach the request.
Definition interpret.c:1288
rlm_rcode_t unlang_interpret(request_t *request, bool running)
Run the interpreter for a current request.
Definition interpret.c:939
bool unlang_request_is_done(request_t const *request)
Return whether a request has been marked done.
Definition interpret.c:1609
static void stack_dump_body(request_t *request, bool with_actions)
Definition interpret.c:233
static unlang_group_t empty_group
Definition interpret.c:1133
unlang_result_t * unlang_interpret_result(request_t *request)
Get the last instruction result OR the last frame that was popped.
Definition interpret.c:1584
static int find_p_result_location(p_result_location_t *location, void **chunk, request_t *request, void *ptr)
Try and figure out where p_result points to.
Definition interpret.c:77
void unlang_interpet_frame_discard(request_t *request)
Discard the bottom most frame on the request's stack.
Definition interpret.c:2025
int unlang_interpret_set_timeout(request_t *request, fr_time_delta_t timeout)
Set a timeout for a request.
Definition interpret.c:1523
void unlang_interpret_request_done(request_t *request)
Indicate to the caller of the interpreter that this request is complete.
Definition interpret.c:1257
static unlang_frame_action_t frame_eval(request_t *request, unlang_stack_frame_t *frame)
Evaluates all the unlang nodes in a section.
Definition interpret.c:730
void unlang_interpret_set(request_t *request, unlang_interpret_t *intp)
Set a specific interpreter for a request.
Definition interpret.c:2033
unlang_interpret_t * unlang_interpret_get(request_t *request)
Get the interpreter set for a request.
Definition interpret.c:2042
int unlang_interpret_stack_depth(request_t *request)
Return the depth of the request's stack.
Definition interpret.c:1552
void unlang_interpret_mark_runnable(request_t *request)
Mark a request as resumable.
Definition interpret.c:1639
static xlat_arg_parser_t const unlang_interpret_xlat_args[]
Definition interpret.c:1808
TALLOC_CTX * unlang_interpret_frame_talloc_ctx(request_t *request)
Get a talloc_ctx which is valid only for this frame.
Definition interpret.c:1684
bool unlang_request_is_scheduled(request_t const *request)
Return whether a request is currently scheduled.
Definition interpret.c:1592
int unlang_interpret_init_global(TALLOC_CTX *ctx)
Definition interpret.c:2082
void stack_dump(request_t *request)
Definition interpret.c:248
p_result_location_t
Definition interpret.c:64
@ P_RESULT_LOCATION_FRAME
Definition interpret.c:66
@ P_RESULT_LOCATION_FUNCTION_RCTX
Definition interpret.c:70
@ P_RESULT_LOCATION_UNKNOWN
Definition interpret.c:65
@ P_RESULT_LOCATION_SCRATCH
Definition interpret.c:67
@ P_RESULT_LOCATION_MODULE_RCTX
Definition interpret.c:69
@ P_RESULT_LOCATION_STATE
Definition interpret.c:68
unlang_interpret_t * unlang_interpret_get_thread_default(void)
Get the default interpreter for this thread.
Definition interpret.c:2075
fr_dict_t const * old_dict
the previous dictionary for the request
Definition interpret.c:341
void * unlang_interpret_stack_alloc(TALLOC_CTX *ctx)
Allocate a new unlang stack.
Definition interpret.c:1219
void unlang_interpret_set_thread_default(unlang_interpret_t *intp)
Set the default interpreter for this thread.
Definition interpret.c:2064
static fr_table_num_ordered_t const p_result_location_table[]
Definition interpret.c:137
#define DUMP_STACK
Definition interpret.c:257
unlang_mod_action_t unlang_interpret_priority(request_t *request)
Get the last instruction priority OR the last frame that was popped.
Definition interpret.c:1574
static void instruction_retry_handler(UNUSED fr_timer_list_t *tl, UNUSED fr_time_t now, void *ctx)
Definition interpret.c:1489
unlang_interpret_t * unlang_interpret_init(TALLOC_CTX *ctx, fr_event_list_t *el, unlang_request_func_t *funcs, void *uctx)
Initialize a unlang compiler / interpret.
Definition interpret.c:1992
static void instruction_timeout_handler(UNUSED fr_timer_list_t *tl, UNUSED fr_time_t now, void *ctx)
Definition interpret.c:1505
bool unlang_request_is_cancelled(request_t const *request)
Return whether a request has been cancelled.
Definition interpret.c:1602
int unlang_interpret_push_instruction(unlang_result_t *p_result, request_t *request, void *instruction, unlang_frame_conf_t const *conf)
Push an instruction onto the request stack for later interpretation.
Definition interpret.c:1191
static void unlang_cancel_event(UNUSED fr_timer_list_t *tl, UNUSED fr_time_t now, void *uctx)
Signal the request to stop executing.
Definition interpret.c:1714
void unlang_interpret_signal(request_t *request, fr_signal_t action)
Send a signal (usually stop) to a request.
Definition interpret.c:1416
static xlat_arg_parser_t const unlang_cancel_xlat_args[]
Definition interpret.c:1699
int unlang_interpret_push_section(unlang_result_t *p_result, request_t *request, CONF_SECTION *cs, unlang_frame_conf_t const *conf)
Push a configuration section onto the request stack for later interpretation.
Definition interpret.c:1168
static unlang_frame_action_t result_calculate(request_t *request, unlang_stack_frame_t *frame, unlang_result_t *result)
Update the current result after each instruction, and after popping each stack frame.
Definition interpret.c:447
static fr_table_num_ordered_t const unlang_action_table[]
Definition interpret.c:45
static int _local_variables_free(unlang_variable_ref_t *ref)
Definition interpret.c:345
static size_t p_result_location_table_len
Definition interpret.c:144
static void instruction_dump(request_t *request, unlang_t const *instruction)
Definition interpret.c:146
bool unlang_interpret_is_resumable(request_t *request)
Check if a request as resumable.
Definition interpret.c:1621
int unlang_interpret_push(unlang_result_t *p_result, request_t *request, unlang_t const *instruction, unlang_frame_conf_t const *conf, bool do_next_sibling)
Push a new frame onto the stack.
Definition interpret.c:280
static void instruction_done_debug(request_t *request, unlang_stack_frame_t *frame, unlang_t const *instruction)
Definition interpret.c:686
request_t * request
the request
Definition interpret.c:342
void unlang_interpret_request_cancel_retry(request_t *request)
Cancel any pending retry.
Definition interpret.c:1320
static unlang_frame_action_t result_pop(request_t *request, unlang_stack_frame_t *frame, unlang_result_t *result)
Function called to merge inter-stack-frame results.
Definition interpret.c:656
void unlang_stack_signal(request_t *request, fr_signal_t action, int limit)
Delivers a frame to one or more frames in the stack.
Definition interpret.c:1351
static xlat_action_t unlang_cancel_xlat(TALLOC_CTX *ctx, fr_dcursor_t *out, UNUSED xlat_ctx_t const *xctx, request_t *request, fr_value_box_list_t *args)
Allows a request to dynamically alter its own lifetime.
Definition interpret.c:1734
static size_t unlang_frame_action_table_len
Definition interpret.c:59
rlm_rcode_t unlang_interpret_rcode(request_t *request)
Get the last instruction result OR the last frame that was popped.
Definition interpret.c:1564
static void frame_dump(request_t *request, unlang_stack_frame_t *frame, bool with_actions)
Definition interpret.c:175
unlang_action_t unlang_interpret_push_children(unlang_result_t *p_result, request_t *request, rlm_rcode_t default_rcode, bool do_next_sibling)
Push the children of the current frame onto a new frame onto the stack.
Definition interpret.c:381
static void actions_dump(request_t *request, unlang_t const *instruction)
Definition interpret.c:161
static _Thread_local unlang_interpret_t * intp_thread_default
The default interpreter instance for this thread.
Definition interpret.c:43
fr_event_list_t * unlang_interpret_event_list(request_t *request)
Get the event list for the current interpreter.
Definition interpret.c:2052
unlang_result_t default_result
The default result for the frame.
Definition interpret.h:146
#define UNLANG_STACK_MAX
The maximum depth of the stack.
Definition interpret.h:39
unlang_request_prioritise_t prioritise
Function to re-priotise a request in the runnable queue.
Definition interpret.h:129
unlang_mod_action_t priority
The priority or action for that rcode.
Definition interpret.h:136
#define UNLANG_RESULT_NOT_SET
Definition interpret.h:139
#define FRAME_CONF(_default_rcode, _top_frame)
Definition interpret.h:152
unlang_request_done_t done_internal
Function called when an internal request completes.
Definition interpret.h:119
unlang_request_resume_t resume
Function called when a request is resumed.
Definition interpret.h:124
#define UNLANG_SUB_FRAME
Definition interpret.h:37
unlang_request_done_t done_external
Function called when a external request completes.
Definition interpret.h:118
unlang_request_init_t detach
Function called when a request is detached.
Definition interpret.h:122
unlang_request_runnable_t mark_runnable
Function called when a request needs to be added back to the runnable queue.
Definition interpret.h:125
rlm_rcode_t rcode
The current rcode, from executing the instruction or merging the result from a frame.
Definition interpret.h:134
unlang_request_yield_t yield
Function called when a request yields.
Definition interpret.h:123
unlang_request_done_t done_detached
Function called when a detached request completes.
Definition interpret.h:120
unlang_request_scheduled_t scheduled
Function to check if a request is already scheduled.
Definition interpret.h:127
#define UNLANG_RESULT_RCODE(_x)
Definition interpret.h:140
unlang_request_init_t init_internal
Function called to initialise an internal request.
Definition interpret.h:116
struct unlang_interpret_s unlang_interpret_t
Interpreter handle.
Definition interpret.h:48
Configuration structure to make it easier to pass configuration options to initialise the frame with.
Definition interpret.h:144
External functions provided by the owner of the interpret.
Definition interpret.h:110
Private declarations for the unlang interpreter.
fr_event_list_t * el
unlang_request_func_t funcs
#define REXDENT()
Exdent (unindent) R* messages by one level.
Definition log.h:455
#define DEBUG_ENABLED5
True if global debug level 1-5 messages are enabled.
Definition log.h:261
#define RDEBUG3(fmt,...)
Definition log.h:355
#define RERROR(fmt,...)
Definition log.h:310
#define DEBUG4(_fmt,...)
Definition log.h:267
#define RPERROR(fmt,...)
Definition log.h:314
#define RPEDEBUG(fmt,...)
Definition log.h:388
#define RDEBUG4(fmt,...)
Definition log.h:356
#define RINDENT()
Indent R* messages by one level.
Definition log.h:442
unlang_op_t unlang_ops[UNLANG_TYPE_MAX]
Different operations the interpreter can execute.
Definition base.c:31
Stores all information relating to an event list.
Definition event.c:377
static char * stack[MAX_STACK]
Definition radmin.c:159
@ FR_TYPE_TIME_DELTA
A period of time measured in nanoseconds.
@ FR_TYPE_STRING
String of printable characters.
@ FR_TYPE_NULL
Invalid (uninitialised) attribute type.
@ FR_TYPE_VOID
User data.
unsigned int uint32_t
unsigned char uint8_t
static uint8_t depth(fr_minmax_heap_index_t i)
Definition minmax_heap.c:83
const char * mod_action_name[MOD_PRIORITY_MAX+1]
Definition mod_action.c:113
#define DEFAULT_MOD_ACTIONS
Definition mod_action.h:73
unlang_mod_action_t
Definition mod_action.h:37
@ MOD_ACTION_NOT_SET
default "not set by anything"
Definition mod_action.h:38
@ MOD_ACTION_RETURN
stop processing the section, and return the rcode with unset priority
Definition mod_action.h:41
@ MOD_ACTION_REJECT
change the rcode to REJECT, with unset priority
Definition mod_action.h:40
@ MOD_ACTION_RETRY
retry the instruction, MUST also set a retry config
Definition mod_action.h:39
#define MOD_ACTION_VALID(_x)
Definition mod_action.h:65
fr_retry_config_t retry
Definition mod_action.h:70
unlang_mod_action_t actions[RLM_MODULE_NUMCODES]
Definition mod_action.h:69
Declarations for the unlang module interface.
void * rctx
for resume / signal
Definition module_priv.h:63
A module stack entry.
Definition module_priv.h:45
int fr_pair_delete(fr_pair_list_t *list, fr_pair_t *vp)
Remove fr_pair_t from a list and free.
Definition pair.c:1830
#define fr_assert(_expr)
Definition rad_assert.h:38
#define REDEBUG(fmt,...)
#define RDEBUG_ENABLED2()
#define RDEBUG2(fmt,...)
#define RDEBUG(fmt,...)
#define RDEBUG_ENABLED()
static rs_t * conf
Definition radsniff.c:53
fr_table_num_sorted_t const rcode_table[]
Definition rcode.c:35
rlm_rcode_t
Return codes indicating the result of the module call.
Definition rcode.h:44
@ RLM_MODULE_FAIL
Module failed, don't reply.
Definition rcode.h:48
@ RLM_MODULE_REJECT
Immediately reject the request.
Definition rcode.h:47
@ RLM_MODULE_TIMEOUT
Module (or section) timed out.
Definition rcode.h:56
@ RLM_MODULE_NOT_SET
Error resolving rcode (should not be returned by modules).
Definition rcode.h:45
@ RLM_MODULE_NUMCODES
How many valid return codes there are.
Definition rcode.h:57
#define REQUEST_VERIFY(_x)
Definition request.h:309
@ REQUEST_TYPE_EXTERNAL
A request received on the wire.
Definition request.h:178
@ REQUEST_TYPE_INTERNAL
A request generated internally.
Definition request.h:179
@ REQUEST_TYPE_DETACHED
A request that was generated internally, but is now detached (not associated with a parent request....
Definition request.h:180
#define request_is_detachable(_x)
Definition request.h:187
@ REQUEST_DONE
Request has completed.
Definition request.h:89
@ REQUEST_STOP_PROCESSING
Request has been signalled to stop.
Definition request.h:88
void * request_data_get(request_t *request, void const *unique_ptr, int unique_int)
Get opaque data from a request.
#define request_data_add(_request, _unique_ptr, _unique_int, _opaque, _free_on_replace, _free_on_parent, _persist)
Add opaque data to a request_t.
fr_signal_t
Signals that can be generated/processed by request signal handlers.
Definition signal.h:38
@ FR_SIGNAL_CANCEL
Request has been cancelled.
Definition signal.h:40
@ FR_SIGNAL_DETACH
Request is being detached from its parent.
Definition signal.h:45
fr_aka_sim_id_type_t type
fr_pair_t * vp
Stores an attribute, a value and various bits of other data.
Definition pair.h:68
fr_dict_attr_t const *_CONST da
Dictionary attribute defines the attribute number, vendor and type of the pair.
Definition pair.h:69
#define fr_table_str_by_value(_table, _number, _def)
Convert an integer to a string.
Definition table.h:772
An element in an arbitrarily ordered array of name to num mappings.
Definition table.h:57
#define talloc_zero_pooled_object(_ctx, _type, _num_subobjects, _total_subobjects_size)
Definition talloc.h:177
static fr_time_delta_t fr_time_delta_from_sec(int64_t sec)
Definition time.h:590
#define fr_time_delta_ispos(_a)
Definition time.h:290
#define fr_time_delta_eq(_a, _b)
Definition time.h:287
static fr_time_t fr_time_from_sec(time_t when)
Convert a time_t (wallclock time) to a fr_time_t (internal time)
Definition time.h:858
#define fr_time_sub(_a, _b)
Subtract one time from another.
Definition time.h:229
A time delta, a difference in time measured in nanoseconds.
Definition time.h:80
"server local" time.
Definition time.h:69
fr_time_t fr_timer_when(fr_timer_t *ev)
Internal timestamp representing when the timer should fire.
Definition timer.c:720
An event timer list.
Definition timer.c:50
A timer event.
Definition timer.c:84
#define fr_timer_in(...)
Definition timer.h:87
static fr_event_list_t * el
#define XLAT_ARGS(_list,...)
Populate local variables with value boxes from the input list.
Definition xlat.h:383
unsigned int required
Argument must be present, and non-empty.
Definition xlat.h:146
#define XLAT_ARG_PARSER_TERMINATOR
Definition xlat.h:170
xlat_action_t
Definition xlat.h:37
@ XLAT_ACTION_FAIL
An xlat function failed.
Definition xlat.h:44
@ XLAT_ACTION_DONE
We're done evaluating this level of nesting.
Definition xlat.h:43
Definition for a single argument consumend by an xlat function.
Definition xlat.h:145
Private interpreter structures and functions.
fr_retry_state_t state
#define unlang_frame_perf_resume(_x)
unlang_result_t section_result
The aggregate result of executing all siblings in this section.
static void frame_pop(request_t *request, unlang_stack_t *stack)
Pop a stack frame, removing any associated dynamically allocated state.
static void frame_next(unlang_stack_t *stack, unlang_stack_frame_t *frame)
Advance to the next sibling instruction.
@ UNLANG_FRAME_FLAG_NONE
No flags.
static bool is_repeatable(unlang_stack_frame_t const *frame)
#define UNLANG_NEXT_SIBLING
Definition unlang_priv.h:99
unlang_result_t * p_result
Where to write the result of executing the current instruction.
static void repeatable_clear(unlang_stack_frame_t *frame)
static unlang_action_t unwind_to_depth(unlang_stack_t *stack, unsigned int to_depth)
Mark up frames as cancelled so they're immediately popped by the interpreter.
unlang_retry_t * retry
if the frame is being retried.
unlang_signal_t signal
function to call when signalling this stack frame
char const * debug_name
Printed in log messages when the node is executed.
void * state
Stack frame specialisations.
unlang_mod_actions_t actions
Priorities, etc. for the various return codes.
static void frame_state_init(unlang_stack_t *stack, unlang_stack_frame_t *frame)
Initialise memory and instruction for a frame when a new instruction is to be evaluated.
unlang_dump_t dump
Dump additional information about the frame state.
static unlang_t * unlang_group_to_generic(unlang_group_t const *p)
rindent_t indent
Indent level of the request when the frame was created.
fr_timer_t * ev
unlang_process_t interpret
Function to interpret the keyword.
unlang_result_t scratch_result
The result of executing the current instruction.
int depth
of this retry structure
request_t * request
CONF_ITEM * ci
used to generate this item
static bool is_top_frame(unlang_stack_frame_t const *frame)
static unlang_group_t * unlang_generic_to_group(unlang_t const *p)
static unlang_stack_frame_t * frame_current(request_t *request)
unlang_list_t children
static bool is_private_result(unlang_stack_frame_t const *frame)
char const * name
Unknown...
static bool is_break_point(unlang_stack_frame_t const *frame)
#define has_debug_braces(_thing)
@ UNLANG_TYPE_GROUP
Grouping section.
Definition unlang_priv.h:51
@ UNLANG_TYPE_MODULE
Module method.
Definition unlang_priv.h:49
unlang_t const * instruction
The unlang node we're evaluating.
static bool is_rcode_set(unlang_stack_frame_t const *frame)
static bool is_yielded(unlang_stack_frame_t const *frame)
static void top_frame_set(unlang_stack_frame_t *frame)
unlang_variable_t * variables
rarely used, so we don't usually need it
char const * name
Name of the keyword.
unlang_frame_action_t
Allows the frame evaluator to signal the interpreter.
Definition unlang_priv.h:89
@ UNLANG_FRAME_ACTION_POP
Pop the current frame, and check the next one further up in the stack for what to do next.
Definition unlang_priv.h:90
@ UNLANG_FRAME_ACTION_YIELD
Temporarily return control back to the caller on the C stack.
Definition unlang_priv.h:94
@ UNLANG_FRAME_ACTION_NEXT
Process the next instruction at this level.
Definition unlang_priv.h:93
@ UNLANG_FRAME_ACTION_RETRY
retry the current frame
Definition unlang_priv.h:92
static void yielded_set(unlang_stack_frame_t *frame)
static bool is_continue_point(unlang_stack_frame_t const *frame)
static void yielded_clear(unlang_stack_frame_t *frame)
#define unlang_frame_perf_yield(_x)
#define unlang_frame_perf_cleanup(_x)
unlang_t const * next
The next unlang node we will evaluate.
fr_dict_t * dict
our dictionary
static bool is_return_point(unlang_stack_frame_t const *frame)
unlang_process_t process
function to call for interpreting this stack frame
unlang_type_t type
The specialisation of this node.
unlang_frame_flag_t flag
Flags that mark up the frame for various things such as being the point where break,...
unlang_list_t * list
so we have fewer run-time dereferences
static bool is_unwinding(unlang_stack_frame_t const *frame)
Generic representation of a grouping.
An unlang operation.
A node in a graph of unlang_op_t (s) that we execute.
Our interpreter stack, as distinct from the C stack.
An unlang stack associated with a request.
fr_pair_t * fr_pair_list_tail(fr_pair_list_t const *list)
Get the tail of a valuepair list.
Definition pair_inline.c:55
fr_pair_t * fr_pair_list_prev(fr_pair_list_t const *list, fr_pair_t const *item))
Get the previous item in a valuepair list before a specific entry.
Definition pair_inline.c:82
#define RETRY_INIT
Definition retry.h:39
uint32_t mrc
Maximum retransmission count.
Definition retry.h:36
@ FR_RETRY_MRC
reached maximum retransmission count
Definition retry.h:47
@ FR_RETRY_CONTINUE
Definition retry.h:46
@ FR_RETRY_MRD
reached maximum retransmission duration
Definition retry.h:48
fr_time_delta_t mrd
Maximum retransmission duration.
Definition retry.h:35
int fr_value_box_strdup(TALLOC_CTX *ctx, fr_value_box_t *dst, fr_dict_attr_t const *enumv, char const *src, bool tainted)
Copy a nul terminated string to a fr_value_box_t.
Definition value.c:4634
int fr_value_box_bstrndup(TALLOC_CTX *ctx, fr_value_box_t *dst, fr_dict_attr_t const *enumv, char const *src, size_t len, bool tainted)
Copy a string to to a fr_value_box_t.
Definition value.c:4853
#define fr_value_box_alloc(_ctx, _type, _enumv)
Allocate a value box of a specific type.
Definition value.h:644
int nonnull(2, 5))
#define fr_value_box_alloc_null(_ctx)
Allocate a value box for later use with a value assignment function.
Definition value.h:655
static size_t char ** out
Definition value.h:1024
An xlat calling ctx.
Definition xlat_ctx.h:49
int xlat_func_args_set(xlat_t *x, xlat_arg_parser_t const args[])
Register the arguments of an xlat.
Definition xlat_func.c:363
xlat_t * xlat_func_register(TALLOC_CTX *ctx, char const *name, xlat_func_t func, fr_type_t return_type)
Register an xlat function.
Definition xlat_func.c:216