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: 4d259bb333fe64f80adf9e24b8fb0690f824c39e $
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: 4d259bb333fe64f80adf9e24b8fb0690f824c39e $")
26
27#include <freeradius-devel/unlang/interpret.h>
28#include <freeradius-devel/server/rcode.h>
29#include "group_priv.h"
30#include "timeout_priv.h"
31#include "mod_action.h"
32#include "unlang_priv.h"
33
44
45/** Immediately cancel the timeout if the frame is cancelled
46 */
48{
49 unlang_frame_state_timeout_t *state = talloc_get_type_abort(frame->state, unlang_frame_state_timeout_t);
50
51 if (action == FR_SIGNAL_CANCEL) {
52 TALLOC_FREE(state->ev);
53 }
54}
55
57{
58 unlang_frame_state_timeout_t *state = talloc_get_type_abort(ctx, unlang_frame_state_timeout_t);
59 request_t *request = talloc_get_type_abort(state->request, request_t);
60 unlang_stack_t *stack = request->stack;
61 unlang_stack_frame_t *frame = &stack->frame[stack->depth];
62 char const *module;
63
64 /*
65 * Don't log in the context of the request
66 */
67 module = request->module;
68 request->module = NULL;
69 RDEBUG("Timeout reached, signalling interpreter to cancel child section.");
70 request->module = module;
71
72 /*
73 * Signal all the frames upto, but not including the timeout
74 * frame to cancel.
75 *
76 * unlang_timeout_resume_done then runs, and returns "timeout"
77 */
79
80 /*
81 * If the frame is yielded (needs to be resumed), but was cancelled
82 * we now need to mark it runnable again so it's unwound.
83 *
84 * If the frame _isn't_ cancelled, then it's non-cancellable and
85 * something else will run it to completion, and mark
86 * the request as complete.
87 */
89 state->fired = true;
90
91 RINDENT_RESTORE(request, state);
92}
93
95{
96 unlang_frame_state_timeout_t *state = talloc_get_type_abort(frame->state, unlang_frame_state_timeout_t);
97
98 /*
99 * No timeout, we go to the next instruction.
100 *
101 * Unless the next instruction is a "catch timeout", in which case we skip it.
102 */
103 if (!state->fired) return UNLANG_ACTION_EXECUTE_NEXT; /* Don't modify the return code*/
104
106}
107
109{
110 unlang_frame_state_timeout_t *state = talloc_get_type_abort(frame->state, unlang_frame_state_timeout_t);
111
112 /*
113 * Save current indentation for the error path.
114 */
115 RINDENT_SAVE(state, request);
116
117 if (fr_timer_in(state, unlang_interpret_event_list(request)->tl, &state->ev, state->timeout,
118 false, unlang_timeout_handler, state) < 0) {
119 RPEDEBUG("Failed inserting event");
121 }
122
124
126}
127
129{
130 unlang_frame_state_timeout_t *state = talloc_get_type_abort(frame->state, unlang_frame_state_timeout_t);
131 fr_value_box_t *box = fr_value_box_list_head(&state->result);
132
133 /*
134 * compile_timeout() ensures that the tmpl is cast to time_delta, so we don't have to do any more work here.
135 */
136 state->timeout = box->vb_time_delta;
137
138 return unlang_timeout_set(p_result, request, frame);
139}
140
142{
144 unlang_timeout_t *gext;
145 unlang_frame_state_timeout_t *state = talloc_get_type_abort(frame->state, unlang_frame_state_timeout_t);
146 unlang_stack_t *stack = request->stack;
147
149 gext = unlang_group_to_timeout(g);
150
151 /*
152 * +1 so we don't mark the timeout frame as cancelled,
153 * we want unlang_timeout_resume_done to be called.
154 */
155 state->depth = stack->depth + 1;
156 state->request = request;
157
158 if (!gext->vpt) {
159 state->timeout = gext->timeout;
160 return unlang_timeout_set(p_result, request, frame);
161 }
162
163 fr_value_box_list_init(&state->result);
164
165 if (unlang_tmpl_push(state, NULL, &state->result, request, gext->vpt, NULL, UNLANG_SUB_FRAME) < 0) return UNLANG_ACTION_FAIL;
166
168
170}
171
173{
175 char const *name2;
176 unlang_t *c;
178 unlang_timeout_t *gext;
180 tmpl_t *vpt = NULL;
181 fr_token_t token;
182
183 /*
184 * Timeout <time ref>
185 */
186 name2 = cf_section_name2(cs);
187 if (!name2) {
188 cf_log_err(cs, "You must specify a time value for 'timeout'");
189 print_url:
190 cf_log_err(ci, DOC_KEYWORD_REF(timeout));
191 return NULL;
192 }
193
194 if (!cf_item_next(cs, NULL)) return UNLANG_IGNORE;
195
197 if (!g) return NULL;
198
199 gext = unlang_group_to_timeout(g);
200
201 token = cf_section_name2_quote(cs);
202
203 if ((token == T_BARE_WORD) && isdigit((uint8_t) *name2)) {
204 if (fr_time_delta_from_str(&timeout, name2, strlen(name2), FR_TIME_RES_SEC) < 0) {
205 cf_log_err(cs, "Failed parsing time delta %s - %s",
206 name2, fr_strerror());
207 return NULL;
208 }
209 } else {
210 ssize_t slen;
211 tmpl_rules_t t_rules;
212
213 /*
214 * We don't allow unknown attributes here.
215 */
216 t_rules = *(unlang_ctx->rules);
217 t_rules.attr.allow_unknown = false;
218 RULES_VERIFY(&t_rules);
219
220 slen = tmpl_afrom_substr(gext, &vpt,
221 &FR_SBUFF_IN_STR(name2),
222 token,
223 NULL,
224 &t_rules);
225 if (!vpt) {
226 cf_canonicalize_error(cs, slen, "Failed parsing argument to 'timeout'", name2);
227 talloc_free(g);
228 return NULL;
229 }
230
231 /*
232 * Fixup the tmpl so that we know it's somewhat sane.
233 */
234 if (!pass2_fixup_tmpl(gext, &vpt, cf_section_to_item(cs), unlang_ctx->rules->attr.dict_def)) {
235 talloc_free(g);
236 return NULL;
237 }
238
239 if (tmpl_is_list(vpt)) {
240 cf_log_err(cs, "Cannot use list as argument for 'timeout' statement");
241 error:
242 talloc_free(g);
243 goto print_url;
244 }
245
247 cf_log_err(cs, "Cannot use regular expression as argument for 'timeout' statement");
248 goto error;
249 }
250
251 /*
252 * Attribute or data MUST be cast to TIME_DELTA.
253 */
255 cf_log_perr(cs, "Failed setting cast type");
256 goto error;
257 }
258 }
259
260 /*
261 * Compile the contents of a "timeout".
262 */
264 if (!c) return NULL;
265
267 gext = unlang_group_to_timeout(g);
268 gext->timeout = timeout;
269 gext->vpt = vpt;
270
271 return c;
272}
273
275{
277 .name = "timeout",
278 .type = UNLANG_TYPE_TIMEOUT,
280
281 .compile = unlang_compile_timeout,
282 .interpret = unlang_timeout,
283 .signal = unlang_timeout_signal,
284
285 .unlang_size = sizeof(unlang_timeout_t),
286 .unlang_name = "unlang_timeout_t",
287
288 .frame_state_size = sizeof(unlang_frame_state_timeout_t),
289 .frame_state_type = "unlang_frame_state_timeout_t",
290 });
291}
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 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
CONF_ITEM * cf_section_to_item(CONF_SECTION const *cs)
Cast a CONF_SECTION to a CONF_ITEM.
Definition cf_util.c:737
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_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:1508
bool pass2_fixup_tmpl(UNUSED TALLOC_CTX *ctx, tmpl_t **vpt_p, CONF_ITEM const *ci, fr_dict_t const *dict)
Definition compile.c:95
unlang_group_t * unlang_group_allocate(unlang_t *parent, CONF_SECTION *cs, unlang_type_t type)
Definition compile.c:447
Declarations for the "group" keyword.
void unlang_interpret_mark_runnable(request_t *request)
Mark a request as resumable.
Definition interpret.c:1620
bool unlang_request_is_scheduled(request_t const *request)
Return whether a request is currently scheduled.
Definition interpret.c:1573
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:1337
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:2017
#define UNLANG_SUB_FRAME
Definition interpret.h:37
#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 RDEBUG(fmt,...)
Definition radclient.h:53
@ 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_STR(_start)
#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:128
static void unlang_timeout_handler(UNUSED fr_timer_list_t *tl, UNUSED fr_time_t now, void *ctx)
Definition timeout.c:56
void unlang_timeout_init(void)
Definition timeout.c:274
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:47
static unlang_t * unlang_compile_timeout(unlang_t *parent, unlang_compile_ctx_t *unlang_ctx, CONF_ITEM const *ci)
Definition timeout.c:172
static unlang_action_t unlang_timeout_set(UNUSED unlang_result_t *p_result, request_t *request, unlang_stack_frame_t *frame)
Definition timeout.c:108
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:94
fr_time_delta_t timeout
Definition timeout.c:37
fr_value_box_list_t result
Definition timeout.c:42
static unlang_action_t unlang_timeout(unlang_result_t *p_result, request_t *request, unlang_stack_frame_t *frame)
Definition timeout.c:141
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, bool top_frame)
Push a tmpl onto the stack for evaluation.
Definition tmpl.c:276
enum fr_token fr_token_t
@ T_BARE_WORD
Definition token.h:120
Private interpreter structures and functions.
#define UNLANG_NEXT_SIBLING
void * state
Stack frame specialisations.
#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:74
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.
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:841
char const * fr_strerror(void)
Get the last library error.
Definition strerror.c:553
#define DOC_KEYWORD_REF(_x)
Definition version.h:89