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: b0c790ec248fde8ec20a45ec1d71e4ff14b6c2fe $
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: b0c790ec248fde8ec20a45ec1d71e4ff14b6c2fe $")
26
27#include <freeradius-devel/unlang/interpret.h>
28#include "group_priv.h"
29#include "timeout_priv.h"
30#include "mod_action.h"
31#include "unlang_priv.h"
32
43
44/** Immediately cancel the timeout if the frame is cancelled
45 */
47{
48 unlang_frame_state_timeout_t *state = talloc_get_type_abort(frame->state, unlang_frame_state_timeout_t);
49
50 if (action == FR_SIGNAL_CANCEL) {
51 TALLOC_FREE(state->ev);
52 }
53}
54
56{
57 unlang_frame_state_timeout_t *state = talloc_get_type_abort(ctx, unlang_frame_state_timeout_t);
58 request_t *request = talloc_get_type_abort(state->request, request_t);
59 unlang_stack_t *stack = request->stack;
60 unlang_stack_frame_t *frame = &stack->frame[stack->depth];
61 char const *module;
62
63 /*
64 * Don't log in the context of the request
65 */
66 module = request->module;
67 request->module = NULL;
68 RDEBUG("Timeout reached, signalling interpreter to cancel child section.");
69 request->module = module;
70
71 /*
72 * Signal all the frames upto, but not including the timeout
73 * frame to cancel.
74 *
75 * unlang_timeout_resume_done then runs, and returns "timeout"
76 */
78
79 /*
80 * If the frame is yielded (needs to be resumed), but was cancelled
81 * we now need to mark it runnable again so it's unwound.
82 *
83 * If the frame _isn't_ cancelled, then it's non-cancellable and
84 * something else will run it to completion, and mark
85 * the request as complete.
86 */
88 state->fired = true;
89
90 RINDENT_RESTORE(request, state);
91}
92
94{
95 unlang_frame_state_timeout_t *state = talloc_get_type_abort(frame->state, unlang_frame_state_timeout_t);
96
97 /*
98 * No timeout, we go to the next instruction.
99 *
100 * Unless the next instruction is a "catch timeout", in which case we skip it.
101 */
102 if (!state->fired) return UNLANG_ACTION_EXECUTE_NEXT; /* Don't modify the return code*/
103
105}
106
108{
109 unlang_frame_state_timeout_t *state = talloc_get_type_abort(frame->state, unlang_frame_state_timeout_t);
110
111 /*
112 * Save current indentation for the error path.
113 */
114 RINDENT_SAVE(state, request);
115
116 if (fr_timer_in(state, unlang_interpret_event_list(request)->tl, &state->ev, state->timeout,
117 false, unlang_timeout_handler, state) < 0) {
118 RPEDEBUG("Failed inserting event");
120 }
121
123
125}
126
128{
129 unlang_frame_state_timeout_t *state = talloc_get_type_abort(frame->state, unlang_frame_state_timeout_t);
130 fr_value_box_t *box = fr_value_box_list_head(&state->result);
131
132 if (!box) return UNLANG_ACTION_FAIL;
133
134 /*
135 * compile_timeout() ensures that the tmpl is cast to time_delta, so we don't have to do any more work here.
136 */
137 state->timeout = box->vb_time_delta;
138
139 /*
140 * Negative times, and times less than 1us are treated as "zero".
141 */
144 }
145
146 return unlang_timeout_set(p_result, request, frame);
147}
148
150{
152 unlang_timeout_t *gext;
153 unlang_frame_state_timeout_t *state = talloc_get_type_abort(frame->state, unlang_frame_state_timeout_t);
154 unlang_stack_t *stack = request->stack;
155
157 gext = unlang_group_to_timeout(g);
158
159 /*
160 * +1 so we don't mark the timeout frame as cancelled,
161 * we want unlang_timeout_resume_done to be called.
162 */
163 state->depth = stack->depth + 1;
164 state->request = request;
165
166 if (!gext->vpt) {
167 state->timeout = gext->timeout;
168 return unlang_timeout_set(p_result, request, frame);
169 }
170
171 fr_value_box_list_init(&state->result);
172
173 if (unlang_tmpl_push(state, NULL, &state->result, request, gext->vpt, NULL, UNLANG_SUB_FRAME) < 0) return UNLANG_ACTION_FAIL;
174
176
178}
179
181{
183 char const *name2;
184 unlang_t *c;
186 unlang_timeout_t *gext;
188 tmpl_t *vpt = NULL;
189 fr_token_t token;
190
191 /*
192 * Timeout <time ref>
193 */
194 name2 = cf_section_name2(cs);
195 if (!name2) {
196 cf_log_err(cs, "You must specify a time value for 'timeout'");
197 print_url:
198 cf_log_err(ci, DOC_KEYWORD_REF(timeout));
199 return NULL;
200 }
201
202 if (!cf_item_next(cs, NULL)) return UNLANG_IGNORE;
203
205 if (!g) return NULL;
206
207 gext = unlang_group_to_timeout(g);
208
209 token = cf_section_name2_quote(cs);
210
211 if ((token == T_BARE_WORD) && isdigit((uint8_t) *name2)) {
212 if (fr_time_delta_from_str(&timeout, name2, strlen(name2), FR_TIME_RES_SEC) < 0) {
213 cf_log_err(cs, "Failed parsing time delta %s - %s",
214 name2, fr_strerror());
215 return NULL;
216 }
217 } else {
218 ssize_t slen;
219 tmpl_rules_t t_rules;
220
221 /*
222 * We don't allow unknown attributes here.
223 */
224 t_rules = *(unlang_ctx->rules);
225 t_rules.attr.allow_unknown = false;
226 RULES_VERIFY(&t_rules);
227
228 slen = tmpl_afrom_substr(gext, &vpt,
229 &FR_SBUFF_IN_STR(name2),
230 token,
231 NULL,
232 &t_rules);
233 if (!vpt) {
234 cf_canonicalize_error(cs, slen, "Failed parsing argument to 'timeout'", name2);
235 talloc_free(g);
236 return NULL;
237 }
238
239 /*
240 * Fixup the tmpl so that we know it's somewhat sane.
241 */
242 if (!pass2_fixup_tmpl(gext, &vpt, cf_section_to_item(cs), unlang_ctx->rules->attr.dict_def)) {
243 talloc_free(g);
244 return NULL;
245 }
246
247 if (tmpl_is_list(vpt)) {
248 cf_log_err(cs, "Cannot use list as argument for 'timeout' statement");
249 error:
250 talloc_free(g);
251 goto print_url;
252 }
253
255 cf_log_err(cs, "Cannot use regular expression as argument for 'timeout' statement");
256 goto error;
257 }
258
259 /*
260 * Attribute or data MUST be cast to TIME_DELTA.
261 */
263 cf_log_perr(cs, "Failed setting cast type");
264 goto error;
265 }
266 }
267
268 /*
269 * Compile the contents of a "timeout".
270 */
272 if (!c) return NULL;
273
275 gext = unlang_group_to_timeout(g);
276 gext->timeout = timeout;
277 gext->vpt = vpt;
278
279 return c;
280}
281
283{
285 .name = "timeout",
286 .type = UNLANG_TYPE_TIMEOUT,
288
289 .compile = unlang_compile_timeout,
290 .interpret = unlang_timeout,
291 .signal = unlang_timeout_signal,
292
293 .unlang_size = sizeof(unlang_timeout_t),
294 .unlang_name = "unlang_timeout_t",
295
296 .frame_state_size = sizeof(unlang_frame_state_timeout_t),
297 .frame_state_type = "unlang_frame_state_timeout_t",
298 });
299}
#define RETURN_UNLANG_ACTION_FATAL
Definition action.h:44
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_FAIL
Encountered an unexpected error.
Definition action.h:36
#define RCSID(id)
Definition build.h:512
#define UNUSED
Definition build.h:336
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:1187
CONF_ITEM * cf_section_to_item(CONF_SECTION const *cs)
Cast a CONF_SECTION to a CONF_ITEM.
Definition cf_util.c:739
CONF_SECTION * cf_item_to_section(CONF_ITEM const *ci)
Cast a CONF_ITEM to a CONF_SECTION.
Definition cf_util.c:685
fr_token_t cf_section_name2_quote(CONF_SECTION const *cs)
Return the quoting of the name2 identifier.
Definition cf_util.c:1232
#define cf_log_err(_cf, _fmt,...)
Definition cf_util.h:287
#define cf_canonicalize_error(_ci, _slen, _msg, _str)
Definition cf_util.h:365
#define cf_item_next(_parent, _curr)
Definition cf_util.h:89
#define cf_log_perr(_cf, _fmt,...)
Definition cf_util.h:294
unlang_t * unlang_compile_section(unlang_t *parent, unlang_compile_ctx_t *unlang_ctx, CONF_SECTION *cs, unlang_type_t type)
Definition compile.c:1513
bool pass2_fixup_tmpl(UNUSED TALLOC_CTX *ctx, tmpl_t **vpt_p, CONF_ITEM const *ci, fr_dict_t const *dict)
Definition compile.c:88
unlang_group_t * unlang_group_allocate(unlang_t *parent, CONF_SECTION *cs, unlang_type_t type)
Definition compile.c:452
Declarations for the "group" keyword.
talloc_free(hp)
void unlang_interpret_mark_runnable(request_t *request)
Mark a request as resumable.
Definition interpret.c:1988
bool unlang_request_is_scheduled(request_t const *request)
Return whether a request is currently scheduled.
Definition interpret.c:1941
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:1704
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:719
fr_event_list_t * unlang_interpret_event_list(request_t *request)
Get the event list for the current interpreter.
Definition interpret.c:2409
#define UNLANG_SUB_FRAME
Definition interpret.h:37
#define RINDENT_SAVE(_x, _request)
Save indentation for later restoral.
Definition log.h:400
#define RINDENT_RESTORE(_request, _x)
Definition log.h:404
#define RPEDEBUG(fmt,...)
Definition log.h:388
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
static char * stack[MAX_STACK]
Definition radmin.c:158
@ 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,...)
@ RLM_MODULE_NOT_SET
Error resolving rcode (should not be returned by modules).
Definition rcode.h:45
#define RETURN_UNLANG_TIMEOUT
Definition rcode.h:71
#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:339
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:336
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
unsigned int allow_unknown
Allow unknown attributes i.e.
Definition tmpl.h:303
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
#define fr_time_delta_lteq(_a, _b)
Definition time.h:286
static fr_time_delta_t fr_time_delta_from_usec(int64_t usec)
Definition time.h:568
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:127
static void unlang_timeout_handler(UNUSED fr_timer_list_t *tl, UNUSED fr_time_t now, void *ctx)
Definition timeout.c:55
void unlang_timeout_init(void)
Definition timeout.c:282
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:46
static unlang_t * unlang_compile_timeout(unlang_t *parent, unlang_compile_ctx_t *unlang_ctx, CONF_ITEM const *ci)
Definition timeout.c:180
static unlang_action_t unlang_timeout_set(UNUSED unlang_result_t *p_result, request_t *request, unlang_stack_frame_t *frame)
Definition timeout.c:107
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:93
fr_time_delta_t timeout
Definition timeout.c:36
fr_value_box_list_t result
Definition timeout.c:41
static unlang_action_t unlang_timeout(unlang_result_t *p_result, request_t *request, unlang_stack_frame_t *frame)
Definition timeout.c:149
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:49
A timer event.
Definition timer.c:83
#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:118
Private interpreter structures and functions.
#define RULES_VERIFY(_rules)
#define UNLANG_NEXT_SIBLING
Definition unlang_priv.h:99
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:73
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:858
char const * fr_strerror(void)
Get the last library error.
Definition strerror.c:553
#define DOC_KEYWORD_REF(_x)
Definition version.h:89