Skip to content

Commit a180715

Browse files
committed
add %interpreter.module.force()
and tests.
1 parent 9bca197 commit a180715

4 files changed

Lines changed: 82 additions & 2 deletions

File tree

doc/antora/modules/reference/pages/xlat/index.adoc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ references, function calls, etc. The table below gives more examples of expansi
2323
| xref:xlat/attribute.adoc[attributes] | Expand the value of a named attribute.
2424
| xref:xlat/function.adoc[functions] | Function call syntax.
2525
| xref:xlat/misc/misc.adoc[misc] | Miscellaneous functions.
26+
| xref:xlat/interpreter.adoc[interpreter] | Query the interpreter status, or set interpreter flags.
2627
| xref:xlat/str/index.adoc[str] | String handling.
2728
| xref:xlat/time/index.adoc[time] | Time handling.
2829
| xref:xlat/alternation.adoc[condition] | Conditionally expand a string.

doc/antora/modules/reference/pages/xlat/interpreter.adoc

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,12 +82,18 @@ Which section of a virtual server is processing the request.
8282

8383
== %interpreter('rcode')
8484

85-
The current interpreter return code, e.g. `handle`, or `ok`, etc.
85+
Return the current interpreter xref:unlang/return_codes.adoc[rcode], e.g. `handled`, or `ok`, etc.
8686

8787
== %interpreter('server')
8888

8989
The name of the virtual server which is running the request.
9090

91+
== %interpreter.module.force('name', 'noop')
92+
93+
Force a module xref:unlang/return_codes.adoc[rcode], e.g. `handled` or `ok`, etc
94+
95+
A module can be "un-forced" by not passing a second argument.
96+
9197
=== %client(<key>)
9298

9399
Refers to a variable that was defined in the client section for the

src/lib/unlang/interpret.c

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2383,6 +2383,50 @@ static xlat_action_t unlang_interpret_lb_child_xlat(TALLOC_CTX *ctx, fr_dcursor_
23832383
}
23842384

23852385

2386+
static xlat_arg_parser_t const unlang_interpret_module_force_args[] = {
2387+
{ .required = true, .single = true, .type = FR_TYPE_STRING },
2388+
{ .required = false, .single = true, .type = FR_TYPE_STRING },
2389+
XLAT_ARG_PARSER_TERMINATOR
2390+
};
2391+
2392+
2393+
/** Enable a module
2394+
*
2395+
* @ingroup xlat_functions
2396+
*/
2397+
static xlat_action_t unlang_interpret_module_force(UNUSED TALLOC_CTX *ctx, UNUSED fr_dcursor_t *out,
2398+
UNUSED xlat_ctx_t const *xctx,
2399+
request_t *request, fr_value_box_list_t *in)
2400+
{
2401+
fr_value_box_t *arg = fr_value_box_list_head(in);
2402+
char const *name = arg->vb_strvalue;
2403+
rlm_rcode_t rcode;
2404+
module_instance_t *mi;
2405+
2406+
mi = module_rlm_static_by_name(NULL, name);
2407+
if (!mi) {
2408+
RDEBUG("Unknown module '%s'", name);
2409+
return XLAT_ACTION_FAIL;
2410+
}
2411+
2412+
2413+
arg = fr_value_box_list_next(in, arg);
2414+
if (arg) {
2415+
rcode = fr_table_value_by_str(rcode_table, arg->vb_strvalue, RLM_MODULE_NOT_SET);
2416+
if (rcode == RLM_MODULE_NOT_SET) {
2417+
RDEBUG("Unknown rcode '%s'", arg->vb_strvalue);
2418+
return XLAT_ACTION_FAIL;
2419+
}
2420+
} else {
2421+
rcode = RLM_MODULE_NOT_SET;
2422+
}
2423+
2424+
module_thread_force(module_thread(mi), rcode);
2425+
return XLAT_ACTION_DONE;
2426+
}
2427+
2428+
2429+
23862430
/** Return the current virtual server for this request
23872431
*
23882432
* @param[in] request To return virtual server for.
@@ -2531,9 +2575,12 @@ int unlang_interpret_init_global(TALLOC_CTX *ctx)
25312575
if (unlikely((xlat = xlat_func_register(ctx, "interpreter", unlang_interpret_xlat, FR_TYPE_VOID)) == NULL)) return -1;
25322576
xlat_func_args_set(xlat, unlang_interpret_xlat_args);
25332577

2534-
if (unlikely((xlat = xlat_func_register(ctx, "interpreter.load-balance.persist", unlang_interpret_lb_persist_xlat, FR_TYPE_VOID)) == NULL)) return -1;
2578+
if (unlikely((xlat = xlat_func_register(ctx, "interpreter.load-balance.persist", unlang_interpret_lb_persist_xlat, FR_TYPE_NULL)) == NULL)) return -1;
25352579
if (unlikely((xlat = xlat_func_register(ctx, "interpreter.load-balance.child", unlang_interpret_lb_child_xlat, FR_TYPE_UINT8)) == NULL)) return -1;
25362580

2581+
if (unlikely((xlat = xlat_func_register(ctx, "interpreter.module.force", unlang_interpret_module_force, FR_TYPE_NULL)) == NULL)) return -1;
2582+
xlat_func_args_set(xlat, unlang_interpret_module_force_args);
2583+
25372584
if (unlikely((xlat = xlat_func_register(ctx, "cancel", unlang_cancel_xlat, FR_TYPE_VOID)) == NULL)) return -1;
25382585
xlat_func_args_set(xlat, unlang_cancel_xlat_args);
25392586

src/tests/keywords/module-force

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#
2+
# Check that the "ok" module works, then disable it, then re-enable it.
3+
#
4+
ok
5+
if (!ok) {
6+
test_fail
7+
}
8+
9+
%interpreter.module.force('ok', 'fail')
10+
11+
try {
12+
ok
13+
}
14+
catch fail {
15+
16+
}
17+
catch {
18+
test_fail
19+
}
20+
21+
%interpreter.module.force('ok')
22+
ok
23+
if (!ok) {
24+
test_fail
25+
}
26+
success

0 commit comments

Comments
 (0)