The FreeRADIUS server  $Id: 15bac2a4c627c01d1aa2047687b3418955ac7f00 $
base.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: e5555ed76b7f2453e656103f55b0541963f530ca $
19  *
20  * @file unlang/base.c
21  * @brief Base, utility functions for the unlang library.
22  *
23  * @copyright 2019 The FreeRADIUS server project
24  */
25 RCSID("$Id: e5555ed76b7f2453e656103f55b0541963f530ca $")
26 
27 #include "unlang_priv.h"
28 
30 
31 /** Different operations the interpreter can execute
32  */
34 
35 /** Return whether a section has unlang data associated with it
36  *
37  * @param[in] cs to check.
38  * @return
39  * - true if it has data.
40  * - false if it doesn't have data.
41  */
43 {
44  return (cf_data_find(cs, unlang_group_t, NULL) != NULL);
45 }
46 
47 /** Register an operation with the interpreter
48  *
49  * The main purpose of this registration API is to avoid intermixing the xlat,
50  * condition, map APIs with the interpreter, i.e. the callbacks needed for that
51  * functionality can be in their own source files, and we don't need to include
52  * supporting types and function declarations in the interpreter.
53  *
54  * Later, this could potentially be used to register custom operations for modules.
55  *
56  * The reason why there's a function instead of accessing the unlang_op array
57  * directly, is because 'type' really needs to go away, as needing to add ops to
58  * the unlang_type_t enum breaks the pluggable module model. If there's no
59  * explicit/consistent type values we need to enumerate the operations ourselves.
60  *
61  * @param[in] type Operation identifier. Used to map compiled unlang code
62  * to operations.
63  * @param[in] op unlang_op to register.
64  */
66 {
67  fr_assert(type < UNLANG_TYPE_MAX); /* Unlang max isn't a valid type */
68 
69  memcpy(&unlang_ops[type], op, sizeof(unlang_ops[type]));
70 }
71 
72 static TALLOC_CTX *unlang_ctx = NULL;
73 
75 {
76  if (instance_count > 0) {
78  return 0;
79  }
80 
81  unlang_ctx = talloc_init("unlang");
82  if (!unlang_ctx) return -1;
83 
84  /*
85  * Explicitly initialise the xlat tree, and perform dictionary lookups.
86  */
87  if (xlat_global_init(unlang_ctx) < 0) {
88  fail:
89  TALLOC_FREE(unlang_ctx);
90 
91  memset(unlang_ops, 0, sizeof(unlang_ops));
92  return -1;
93  }
94 
96 
97  /*
98  * Operations which can fail, and which require cleanup.
99  */
100  if (unlang_subrequest_op_init() < 0) goto fail;
101 
102  /*
103  * Register operations for the default keywords. The
104  * operations listed below cannot fail, and do not
105  * require cleanup.
106  */
113  unlang_map_init();
126  unlang_try_init();
128 
129  instance_count++;
130 
131  return 0;
132 }
133 
135 {
136  if (--instance_count > 0) return;
137 
139  TALLOC_FREE(unlang_ctx);
140 
141  memset(unlang_ops, 0, sizeof(unlang_ops));
142 
144 }
#define RCSID(id)
Definition: build.h:444
void unlang_call_init(void)
Definition: call.c:249
void unlang_caller_init(void)
Definition: caller.c:55
void unlang_catch_init(void)
Definition: catch.c:113
A section grouping multiple CONF_PAIR.
Definition: cf_priv.h:89
#define cf_data_find(_cf, _type, _name)
Definition: cf_util.h:220
void unlang_compile_init(TALLOC_CTX *ctx)
Definition: compile.c:4956
void unlang_condition_init(void)
Definition: condition.c:110
void unlang_detach_init(void)
Initialise subrequest ops.
Definition: detach.c:54
void unlang_foreach_init(TALLOC_CTX *ctx)
Definition: foreach.c:260
void unlang_function_init(void)
Definition: function.c:337
void unlang_group_init(void)
Definition: group.c:46
int unlang_interpret_init_global(TALLOC_CTX *ctx)
Definition: interpret.c:1775
void unlang_register(int type, unlang_op_t *op)
Register an operation with the interpreter.
Definition: base.c:65
unlang_op_t unlang_ops[UNLANG_TYPE_MAX]
Different operations the interpreter can execute.
Definition: base.c:33
void unlang_global_free(void)
Definition: base.c:134
bool unlang_section(CONF_SECTION *cs)
Return whether a section has unlang data associated with it.
Definition: base.c:42
static uint32_t instance_count
Definition: base.c:29
static TALLOC_CTX * unlang_ctx
Definition: base.c:72
int unlang_global_init(void)
Definition: base.c:74
void unlang_map_init(void)
Definition: map.c:374
void unlang_limit_init(void)
Definition: limit.c:124
void unlang_load_balance_init(void)
Definition: load_balance.c:248
unsigned int uint32_t
Definition: merged_model.c:33
void unlang_parallel_init(void)
Definition: parallel.c:493
void unlang_return_init(void)
Definition: return.c:43
void unlang_module_init(void)
Definition: module.c:1067
fr_assert(0)
fr_aka_sim_id_type_t type
int unlang_subrequest_op_init(void)
Initialise subrequest ops.
Definition: subrequest.c:274
void unlang_subrequest_op_free(void)
Definition: subrequest.c:291
void unlang_switch_init(void)
Definition: switch.c:142
void unlang_timeout_init(void)
Definition: timeout.c:140
void unlang_tmpl_init(void)
Definition: tmpl.c:327
void unlang_transaction_init(void)
Definition: transaction.c:123
void unlang_try_init(void)
Definition: try.c:42
void unlang_edit_init(void)
Definition: edit.c:1700
int xlat_global_init(TALLOC_CTX *ctx)
Global initialisation for xlat.
void xlat_global_free(void)
De-register all xlat functions we created.
@ UNLANG_TYPE_MAX
Definition: unlang_priv.h:87
Generic representation of a grouping.
Definition: unlang_priv.h:155
An unlang operation.
Definition: unlang_priv.h:214