The FreeRADIUS server $Id: 15bac2a4c627c01d1aa2047687b3418955ac7f00 $
Loading...
Searching...
No Matches
timeout.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: 2a85cb4636fc9d7932d0e51083c9434aab89012f $
19 *
20 * @file unlang/timeout.c
21 * @brief Unlang "timeout" keyword evaluation.
22 *
23 * @copyright 2022 Network RADIUS SAS (legal@networkradius.com)
24 */
25RCSID("$Id: 2a85cb4636fc9d7932d0e51083c9434aab89012f $")
26
27#include <freeradius-devel/unlang/timeout.h>
28#include <freeradius-devel/unlang/interpret.h>
29#include <freeradius-devel/server/rcode.h>
30#include "group_priv.h"
31#include "timeout_priv.h"
32#include "mod_action.h"
33#include "unlang_priv.h"
34
35typedef struct {
36 bool fired;
37 int depth;
42
43 fr_value_box_list_t result;
44
45 unlang_t *instruction; //!< to run on timeout
47
48/** Immediately cancel the timeout if the frame is cancelled
49 */
51{
52 unlang_frame_state_timeout_t *state = talloc_get_type_abort(frame->state, unlang_frame_state_timeout_t);
53
54 if (action == FR_SIGNAL_CANCEL) {
55 TALLOC_FREE(state->ev);
56 }
57}
58
60{
61 unlang_frame_state_timeout_t *state = talloc_get_type_abort(ctx, unlang_frame_state_timeout_t);
62 request_t *request = talloc_get_type_abort(state->request, request_t);
63 unlang_stack_t *stack = request->stack;
64 unlang_stack_frame_t *frame = &stack->frame[stack->depth];
65 char const *module;
66
67 /*
68 * Don't log in the context of the request
69 */
70 module = request->module;
71 request->module = NULL;
72 RDEBUG("Timeout reached, signalling interpreter to cancel child section.");
73 request->module = module;
74
75 /*
76 * Signal all the frames upto, but not including the timeout
77 * frame to cancel.
78 *
79 * unlang_timeout_resume_done then runs, and returns "timeout"
80 */
82
83 /*
84 * If the frame is yielded (needs to be resumed), but was cancelled
85 * we now need to mark it runnable again so it's unwound.
86 *
87 * If the frame _isn't_ cancelled, then it's non-cancellable and
88 * something else will run it to completion, and mark
89 * the request as complete.
90 */
92 state->fired = true;
93
94 RINDENT_RESTORE(request, state);
95
96 if (!state->instruction) return;
97
98 /*
99 * Push something else onto the stack to execute.
100 */
102 FRAME_CONF(RLM_MODULE_TIMEOUT, true)) < 0)) {
103 unlang_interpret_signal(request, FR_SIGNAL_CANCEL); /* also stops the request and does cleanups */
104 }
105}
106
108{
109 unlang_frame_state_timeout_t *state = talloc_get_type_abort(frame->state, unlang_frame_state_timeout_t);
110
111 /*
112 * No timeout, we go to the next instruction.
113 *
114 * Unless the next instruction is a "catch timeout", in which case we skip it.
115 */
116 if (!state->fired) return UNLANG_ACTION_EXECUTE_NEXT; /* Don't modify the return code*/
117
119}
120
122{
123 unlang_frame_state_timeout_t *state = talloc_get_type_abort(frame->state, unlang_frame_state_timeout_t);
124
125 /*
126 * Save current indentation for the error path.
127 */
128 RINDENT_SAVE(state, request);
129
130 if (fr_timer_in(state, unlang_interpret_event_list(request)->tl, &state->ev, state->timeout,
131 false, unlang_timeout_handler, state) < 0) {
132 RPEDEBUG("Failed inserting event");
134 }
135
137
139}
140
142{
143 unlang_frame_state_timeout_t *state = talloc_get_type_abort(frame->state, unlang_frame_state_timeout_t);
144 fr_value_box_t *box = fr_value_box_list_head(&state->result);
145
146 /*
147 * compile_timeout() ensures that the tmpl is cast to time_delta, so we don't have to do any more work here.
148 */
149 state->timeout = box->vb_time_delta;
150
151 return unlang_timeout_set(p_result, request, frame);
152}
153
155{
157 unlang_timeout_t *gext;
158 unlang_frame_state_timeout_t *state = talloc_get_type_abort(frame->state, unlang_frame_state_timeout_t);
159 unlang_stack_t *stack = request->stack;
160
162 gext = unlang_group_to_timeout(g);
163
164 /*
165 * +1 so we don't mark the timeout frame as cancelled,
166 * we want unlang_timeout_resume_done to be called.
167 */
168 state->depth = stack->depth + 1;
169 state->request = request;
170
171 if (!gext->vpt) {
172 state->timeout = gext->timeout;
173 return unlang_timeout_set(p_result, request, frame);
174 }
175
176 fr_value_box_list_init(&state->result);
177
178 if (unlang_tmpl_push(state, NULL, &state->result, request, gext->vpt, NULL) < 0) return UNLANG_ACTION_FAIL;
179
181
183}
184
185/** When a timeout fires, run the given section.
186 *
187 * @param[in] request to push timeout onto
188 * @param[in] timeout when to run the timeout
189 * @param[in] cs section to run when the timeout fires.
190 * @param[in] top_frame Set to UNLANG_TOP_FRAME if the interpreter should return.
191 * Set to UNLANG_SUB_FRAME if the interprer should continue.
192 *
193 * @return
194 * - 0 on success.
195 * - -1 on failure.
196 */
197int unlang_timeout_section_push(request_t *request, CONF_SECTION *cs, fr_time_delta_t timeout, bool top_frame)
198{
199 /** Static instruction for performing xlat evaluations
200 *
201 */
202 static unlang_t timeout_instruction = {
204 .name = "timeout",
205 .debug_name = "timeout",
207 };
208
210 unlang_stack_t *stack = request->stack;
212 unlang_t *instruction;
213
214 /*
215 * Get the instruction we are supposed to run on timeout.
216 */
217 instruction = (unlang_t *)cf_data_value(cf_data_find(cs, unlang_group_t, NULL));
218 if (!instruction) {
219 REDEBUG("Failed to find pre-compiled unlang for section %s { ... }",
220 cf_section_name1(cs));
221 return -1;
222 }
223
224 /*
225 * Push a new timeout frame onto the stack
226 */
227 if (unlang_interpret_push(NULL, request, &timeout_instruction,
228 FRAME_CONF(RLM_MODULE_NOT_SET, top_frame), UNLANG_NEXT_STOP) < 0) return -1;
229 frame = &stack->frame[stack->depth];
230
231 /*
232 * Allocate its state, and set the timeout.
233 */
234 MEM(frame->state = state = talloc_zero(stack, unlang_frame_state_timeout_t));
235
236 RINDENT_SAVE(state, request);
237 state->depth = stack->depth;
238 state->request = request;
239 state->timeout = timeout;
240 state->instruction = instruction;
241
242 if (fr_timer_in(state, unlang_interpret_event_list(request)->tl, &state->ev, timeout,
243 false, unlang_timeout_handler, state) < 0) {
244 RPEDEBUG("Failed setting timeout for section %s", cf_section_name1(cs));
245 return -1;
246 }
247
249
250 return 0;
251
252}
253
255{
257 char const *name2;
258 unlang_t *c;
260 unlang_timeout_t *gext;
262 tmpl_t *vpt = NULL;
263 fr_token_t token;
264
265 /*
266 * Timeout <time ref>
267 */
268 name2 = cf_section_name2(cs);
269 if (!name2) {
270 cf_log_err(cs, "You must specify a time value for 'timeout'");
271 print_url:
272 cf_log_err(ci, DOC_KEYWORD_REF(timeout));
273 return NULL;
274 }
275
276 if (!cf_item_next(cs, NULL)) return UNLANG_IGNORE;
277
279 if (!g) return NULL;
280
281 gext = unlang_group_to_timeout(g);
282
283 token = cf_section_name2_quote(cs);
284
285 if ((token == T_BARE_WORD) && isdigit((uint8_t) *name2)) {
286 if (fr_time_delta_from_str(&timeout, name2, strlen(name2), FR_TIME_RES_SEC) < 0) {
287 cf_log_err(cs, "Failed parsing time delta %s - %s",
288 name2, fr_strerror());
289 return NULL;
290 }
291 } else {
292 ssize_t slen;
293 tmpl_rules_t t_rules;
294
295 /*
296 * We don't allow unknown attributes here.
297 */
298 t_rules = *(unlang_ctx->rules);
299 t_rules.attr.allow_unknown = false;
300 RULES_VERIFY(&t_rules);
301
302 slen = tmpl_afrom_substr(gext, &vpt,
303 &FR_SBUFF_IN(name2, strlen(name2)),
304 token,
305 NULL,
306 &t_rules);
307 if (!vpt) {
308 cf_canonicalize_error(cs, slen, "Failed parsing argument to 'timeout'", name2);
309 talloc_free(g);
310 return NULL;
311 }
312
313 /*
314 * Fixup the tmpl so that we know it's somewhat sane.
315 */
316 if (!pass2_fixup_tmpl(gext, &vpt, cf_section_to_item(cs), unlang_ctx->rules->attr.dict_def)) {
317 talloc_free(g);
318 return NULL;
319 }
320
321 if (tmpl_is_list(vpt)) {
322 cf_log_err(cs, "Cannot use list as argument for 'timeout' statement");
323 error:
324 talloc_free(g);
325 goto print_url;
326 }
327
329 cf_log_err(cs, "Cannot use regular expression as argument for 'timeout' statement");
330 goto error;
331 }
332
333 /*
334 * Attribute or data MUST be cast to TIME_DELTA.
335 */
337 cf_log_perr(cs, "Failed setting cast type");
338 goto error;
339 }
340 }
341
342 /*
343 * Compile the contents of a "timeout".
344 */
346 if (!c) return NULL;
347
349 gext = unlang_group_to_timeout(g);
350 gext->timeout = timeout;
351 gext->vpt = vpt;
352
353 return c;
354}
355
357{
359 .name = "timeout",
360 .type = UNLANG_TYPE_TIMEOUT,
362
363 .compile = unlang_compile_timeout,
364 .interpret = unlang_timeout,
365 .signal = unlang_timeout_signal,
366
367 .unlang_size = sizeof(unlang_timeout_t),
368 .unlang_name = "unlang_timeout_t",
369
370 .frame_state_size = sizeof(unlang_frame_state_timeout_t),
371 .frame_state_type = "unlang_frame_state_timeout_t",
372 });
373}
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_STOP_PROCESSING
Break out of processing the current request (unwind).
Definition action.h:42
@ UNLANG_ACTION_FAIL
Encountered an unexpected error.
Definition action.h:36
#define RCSID(id)
Definition build.h:485
#define unlikely(_x)
Definition build.h:383
#define UNUSED
Definition build.h:317
#define RULES_VERIFY(_cs, _rules)
Definition cf_file.c:180
Common header for all CONF_* types.
Definition cf_priv.h:49
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:1762
CONF_ITEM * cf_section_to_item(CONF_SECTION const *cs)
Cast a CONF_SECTION to a CONF_ITEM.
Definition cf_util.c:737
char const * cf_section_name1(CONF_SECTION const *cs)
Return the second identifier of a CONF_SECTION.
Definition cf_util.c:1170
CONF_SECTION * cf_item_to_section(CONF_ITEM const *ci)
Cast a CONF_ITEM to a CONF_SECTION.
Definition cf_util.c:683
fr_token_t cf_section_name2_quote(CONF_SECTION const *cs)
Return the quoting of the name2 identifier.
Definition cf_util.c:1229
#define cf_log_err(_cf, _fmt,...)
Definition cf_util.h:289
#define cf_data_find(_cf, _type, _name)
Definition cf_util.h:244
#define cf_canonicalize_error(_ci, _slen, _msg, _str)
Definition cf_util.h:367
#define cf_item_next(_parent, _curr)
Definition cf_util.h:92
#define cf_log_perr(_cf, _fmt,...)
Definition cf_util.h:296
unlang_t * unlang_compile_section(unlang_t *parent, unlang_compile_ctx_t *unlang_ctx, CONF_SECTION *cs, unlang_type_t type)
Definition compile.c:1517
bool pass2_fixup_tmpl(UNUSED TALLOC_CTX *ctx, tmpl_t **vpt_p, CONF_ITEM const *ci, fr_dict_t const *dict)
Definition compile.c:96
unlang_group_t * unlang_group_allocate(unlang_t *parent, CONF_SECTION *cs, unlang_type_t type)
Definition compile.c:448
#define MEM(x)
Definition debug.h:36
Declarations for the "group" keyword.
void unlang_interpret_mark_runnable(request_t *request)
Mark a request as resumable.
Definition interpret.c:1610
bool unlang_request_is_scheduled(request_t const *request)
Return whether a request is currently scheduled.
Definition interpret.c:1563
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:1179
void unlang_interpret_signal(request_t *request, fr_signal_t action)
Send a signal (usually stop) to a request.
Definition interpret.c:1386
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
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:1326
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:384
fr_event_list_t * unlang_interpret_event_list(request_t *request)
Get the event list for the current interpreter.
Definition interpret.c:2007
#define FRAME_CONF(_default_rcode, _top_frame)
Definition interpret.h:152
#define RINDENT_SAVE(_x, _request)
Save indentation for later restoral.
Definition log.h:388
#define RINDENT_RESTORE(_request, _x)
Definition log.h:392
#define RPEDEBUG(fmt,...)
Definition log.h:376
static TALLOC_CTX * unlang_ctx
Definition base.c:71
void unlang_register(unlang_op_t *op)
Register an operation with the interpreter.
Definition base.c:56
talloc_free(reap)
static char * stack[MAX_STACK]
Definition radmin.c:159
@ FR_TYPE_TIME_DELTA
A period of time measured in nanoseconds.
long int ssize_t
unsigned char uint8_t
Unlang module actions.
#define MOD_ACTIONS_FAIL_TIMEOUT_RETURN
Definition mod_action.h:69
#define REDEBUG(fmt,...)
Definition radclient.h:52
#define RDEBUG(fmt,...)
Definition radclient.h:53
@ RLM_MODULE_TIMEOUT
Module (or section) timed out.
Definition rcode.h:52
@ RLM_MODULE_NOT_SET
Error resolving rcode (should not be returned by modules).
Definition rcode.h:41
#define RETURN_UNLANG_TIMEOUT
Definition rcode.h:67
#define FR_SBUFF_IN(_start, _len_or_end)
#define tmpl_contains_regex(vpt)
Definition tmpl.h:226
ssize_t tmpl_afrom_substr(TALLOC_CTX *ctx, tmpl_t **out, fr_sbuff_t *in, fr_token_t quote, fr_sbuff_parse_rules_t const *p_rules, tmpl_rules_t const *t_rules))
Convert an arbitrary string into a tmpl_t.
static bool tmpl_is_list(tmpl_t const *vpt)
Definition tmpl.h:920
static fr_slen_t vpt
Definition tmpl.h:1269
tmpl_attr_rules_t attr
Rules/data for parsing attribute references.
Definition tmpl.h:335
int tmpl_cast_set(tmpl_t *vpt, fr_type_t type)
Set a cast for a tmpl.
Optional arguments passed to vp_tmpl functions.
Definition tmpl.h:332
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
uint8_t allow_unknown
Allow unknown attributes i.e.
Definition tmpl.h:301
fr_slen_t fr_time_delta_from_str(fr_time_delta_t *out, char const *in, size_t inlen, fr_time_res_t hint)
Create fr_time_delta_t from a string.
Definition time.c:412
static fr_time_delta_t fr_time_delta_from_sec(int64_t sec)
Definition time.h:590
@ FR_TIME_RES_SEC
Definition time.h:50
A time delta, a difference in time measured in nanoseconds.
Definition time.h:80
"server local" time.
Definition time.h:69
static unlang_action_t unlang_timeout_done(unlang_result_t *p_result, request_t *request, unlang_stack_frame_t *frame)
Definition timeout.c:141
static void unlang_timeout_handler(UNUSED fr_timer_list_t *tl, UNUSED fr_time_t now, void *ctx)
Definition timeout.c:59
void unlang_timeout_init(void)
Definition timeout.c:356
static void unlang_timeout_signal(UNUSED request_t *request, unlang_stack_frame_t *frame, fr_signal_t action)
Immediately cancel the timeout if the frame is cancelled.
Definition timeout.c:50
static unlang_t * unlang_compile_timeout(unlang_t *parent, unlang_compile_ctx_t *unlang_ctx, CONF_ITEM const *ci)
Definition timeout.c:254
static unlang_action_t unlang_timeout_set(UNUSED unlang_result_t *p_result, request_t *request, unlang_stack_frame_t *frame)
Definition timeout.c:121
static unlang_action_t unlang_timeout_resume_done(unlang_result_t *p_result, UNUSED request_t *request, unlang_stack_frame_t *frame)
Definition timeout.c:107
fr_time_delta_t timeout
Definition timeout.c:38
fr_value_box_list_t result
Definition timeout.c:43
int unlang_timeout_section_push(request_t *request, CONF_SECTION *cs, fr_time_delta_t timeout, bool top_frame)
When a timeout fires, run the given section.
Definition timeout.c:197
unlang_t * instruction
to run on timeout
Definition timeout.c:45
static unlang_action_t unlang_timeout(unlang_result_t *p_result, request_t *request, unlang_stack_frame_t *frame)
Definition timeout.c:154
fr_time_delta_t timeout
static unlang_timeout_t * unlang_group_to_timeout(unlang_group_t *g)
Cast a group structure to the timeout keyword extension.
An event timer list.
Definition timer.c:50
A timer event.
Definition timer.c:84
#define fr_timer_in(...)
Definition timer.h:87
int unlang_tmpl_push(TALLOC_CTX *ctx, unlang_result_t *p_result, fr_value_box_list_t *out, request_t *request, tmpl_t const *tmpl, unlang_tmpl_args_t *args)
Push a tmpl onto the stack for evaluation.
Definition tmpl.c:273
enum fr_token fr_token_t
@ T_BARE_WORD
Definition token.h:120
Private interpreter structures and functions.
#define UNLANG_NEXT_SIBLING
Definition unlang_priv.h:98
void * state
Stack frame specialisations.
#define UNLANG_NEXT_STOP
Definition unlang_priv.h:97
#define UNLANG_IGNORE
static unlang_group_t * unlang_generic_to_group(unlang_t const *p)
@ UNLANG_TYPE_TIMEOUT
time-based timeouts.
Definition unlang_priv.h:72
static void frame_repeat(unlang_stack_frame_t *frame, unlang_process_t process)
Mark the current stack frame up for repeat, and set a new process function.
unlang_t const * instruction
The unlang node we're evaluating.
static bool is_yielded(unlang_stack_frame_t const *frame)
@ UNLANG_OP_FLAG_DEBUG_BRACES
Print debug braces.
@ UNLANG_OP_FLAG_RCODE_SET
Set request->rcode to the result of this operation.
unlang_type_t type
The specialisation of this node.
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.
static fr_slen_t parent
Definition pair.h:839
char const * fr_strerror(void)
Get the last library error.
Definition strerror.c:553
#define DOC_KEYWORD_REF(_x)
Definition version.h:89