Skip to content

Commit

Permalink
add xlat wrapper function for bootstrap / instantiate ephemeral
Browse files Browse the repository at this point in the history
* xlat_bootstrap() - when reading the configuration files
  pretty much only called by xlat_redundant()

* xlat_instantiate_ephemeral() - when we have a #request_t and
  are parsing things at run-time

* xlat_finalize() - when we have a #tmpl_rules_t, and we're not
  sure if we're run-time or config load time.

The issue is that the various xlat tokenize funtions call each
other in different combinations.  The only way to consistently
know if we're run-time or config time is via the #tmpl_rules_t.

This change (also via previous commits) allows us to get rid of
the various public tokenize_ephemeral() functions, which were
confusing the issue.
  • Loading branch information
alandekok committed Oct 7, 2023
1 parent c3904f8 commit 79615ab
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 25 deletions.
9 changes: 1 addition & 8 deletions src/lib/server/tmpl_tokenize.c
Original file line number Diff line number Diff line change
Expand Up @@ -3278,14 +3278,7 @@ fr_slen_t tmpl_afrom_substr(TALLOC_CTX *ctx, tmpl_t **out,
* so that their instance data will be created.
*/
if (head) {
int rcode;

if (!t_rules->at_runtime) {
rcode = xlat_bootstrap(head);
} else {
rcode = xlat_instantiate_ephemeral(head, t_rules->xlat.runtime_el);
}
if (rcode < 0) {
if (xlat_finalize(head, t_rules) < 0) {
fr_strerror_const("Failed to bootstrap xlat");
FR_SBUFF_ERROR_RETURN(&our_in);
}
Expand Down
2 changes: 2 additions & 0 deletions src/lib/unlang/xlat.h
Original file line number Diff line number Diff line change
Expand Up @@ -466,6 +466,8 @@ int xlat_bootstrap(xlat_exp_head_t *root);

void xlat_instances_free(void);

int xlat_finalize(xlat_exp_head_t *head, tmpl_rules_t const *t_rules); /* xlat_bootstrap() or xlat_instantiate_ephemeral() */

/*
* xlat_purify.c
*/
Expand Down
8 changes: 1 addition & 7 deletions src/lib/unlang/xlat_expr.c
Original file line number Diff line number Diff line change
Expand Up @@ -2833,7 +2833,6 @@ static const fr_sbuff_term_t operator_terms = FR_SBUFF_TERMS(
static fr_slen_t xlat_tokenize_expression_internal(TALLOC_CTX *ctx, xlat_exp_head_t **out, fr_sbuff_t *in,
fr_sbuff_parse_rules_t const *p_rules, tmpl_rules_t const *t_rules, bool cond)
{
int rcode;
ssize_t slen;
fr_sbuff_parse_rules_t *bracket_rules = NULL;
fr_sbuff_parse_rules_t *terminal_rules = NULL;
Expand Down Expand Up @@ -2908,12 +2907,7 @@ static fr_slen_t xlat_tokenize_expression_internal(TALLOC_CTX *ctx, xlat_exp_hea
* Add nodes that need to be bootstrapped to
* the registry.
*/
if (!t_rules || !t_rules->xlat.runtime_el) {
rcode = xlat_bootstrap(head);
} else {
rcode = xlat_instantiate_ephemeral(head, t_rules->xlat.runtime_el);
}
if (rcode < 0) {
if (xlat_finalize(head, t_rules) < 0) {
talloc_free(head);
return -1;
}
Expand Down
28 changes: 25 additions & 3 deletions src/lib/unlang/xlat_inst.c
Original file line number Diff line number Diff line change
Expand Up @@ -348,9 +348,31 @@ static int _xlat_instantiate_ephemeral_walker(xlat_exp_t *node, void *uctx)
return 0;
}


/** Bootstrap static xlats, or instantiate ephemeral ones.
*
* @note - this function should be called when we have a
* #tmpl_rules_t. i.e. instead of calling xlat_bootstrap() or
* xlat_instantiate_ephemeral()
*
* @param[in] head of xlat tree to create instance data for.
* @param[in] t_rules parsing rules with #fr_event_list_t
*/
int xlat_finalize(xlat_exp_head_t *head, tmpl_rules_t const *t_rules)
{
if (!t_rules || !t_rules->xlat.runtime_el) {
fr_assert(!t_rules->at_runtime);
return xlat_bootstrap(head);
}

fr_assert(t_rules->at_runtime);
return xlat_instantiate_ephemeral(head, t_rules->xlat.runtime_el);
}

/** Create instance data for "ephemeral" xlats
*
* @note This must only be used for xlats created at runtime.
* @note This function should only be called from routines which get
* passed a #request_t.
*
* @param[in] head of xlat tree to create instance data for.
* @param[in] el event list used to run any instantiate data
Expand Down Expand Up @@ -587,9 +609,9 @@ static int _xlat_bootstrap_walker(xlat_exp_t *node, UNUSED void *uctx)

/** Create instance data for "permanent" xlats
*
* @note This must only be used for xlats created during startup.
* @note This must only be used for xlats which are read from the configuration files.
* IF THIS IS CALLED FOR XLATS TOKENIZED AT RUNTIME YOU WILL LEAK LARGE AMOUNTS OF MEMORY.
* USE xlat_instantiate_request() INSTEAD.
* If the caller has a #tmpl_rules_t, it should call xlat_finalize() instead.
*
* @param[in] head of xlat tree to create instance data for.
*/
Expand Down
8 changes: 1 addition & 7 deletions src/lib/unlang/xlat_tokenize.c
Original file line number Diff line number Diff line change
Expand Up @@ -1860,7 +1860,6 @@ fr_slen_t xlat_tokenize_argv(TALLOC_CTX *ctx, xlat_exp_head_t **out, fr_sbuff_t
fr_slen_t xlat_tokenize(TALLOC_CTX *ctx, xlat_exp_head_t **out, fr_sbuff_t *in,
fr_sbuff_parse_rules_t const *p_rules, tmpl_rules_t const *t_rules)
{
int rcode;
fr_sbuff_t our_in = FR_SBUFF(in);
xlat_exp_head_t *head;

Expand All @@ -1881,12 +1880,7 @@ fr_slen_t xlat_tokenize(TALLOC_CTX *ctx, xlat_exp_head_t **out, fr_sbuff_t *in,
* Add nodes that need to be bootstrapped to
* the registry.
*/
if (!t_rules || !t_rules->xlat.runtime_el) {
rcode = xlat_bootstrap(head);
} else {
rcode = xlat_instantiate_ephemeral(head, t_rules->xlat.runtime_el);
}
if (rcode < 0) {
if (xlat_finalize(head, t_rules) < 0) {
talloc_free(head);
return 0;
}
Expand Down

0 comments on commit 79615ab

Please sign in to comment.