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: 285d661597ef41df7d15f4a0acc2d29850e89ece $
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: 285d661597ef41df7d15f4a0acc2d29850e89ece $")
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 unlang_interpret_signal(request, FR_SIGNAL_CANCEL); /* also stops the request and does cleanups */
103 }
104}
105
107{
108 unlang_frame_state_timeout_t *state = talloc_get_type_abort(frame->state, unlang_frame_state_timeout_t);
109
110 /*
111 * No timeout, we go to the next instruction.
112 *
113 * Unless the next instruction is a "catch timeout", in which case we skip it.
114 */
115 if (!state->fired) return UNLANG_ACTION_CALCULATE_RESULT;
116
118}
119
121{
122 unlang_frame_state_timeout_t *state = talloc_get_type_abort(frame->state, unlang_frame_state_timeout_t);
123
124 /*
125 * Save current indentation for the error path.
126 */
127 RINDENT_SAVE(state, request);
128
129 if (fr_timer_in(state, unlang_interpret_event_list(request)->tl, &state->ev, state->timeout,
130 false, unlang_timeout_handler, state) < 0) {
131 RPEDEBUG("Failed inserting event");
133 }
134
136
137 return unlang_interpret_push_children(p_result, request, frame->result, UNLANG_NEXT_SIBLING);
138}
139
141{
142 unlang_frame_state_timeout_t *state = talloc_get_type_abort(frame->state, unlang_frame_state_timeout_t);
143 fr_value_box_t *box = fr_value_box_list_head(&state->result);
144
145 /*
146 * compile_timeout() ensures that the tmpl is cast to time_delta, so we don't have to do any more work here.
147 */
148 state->timeout = box->vb_time_delta;
149
150 return unlang_timeout_set(p_result, request, frame);
151}
152
154{
156 unlang_timeout_t *gext;
157 unlang_frame_state_timeout_t *state = talloc_get_type_abort(frame->state, unlang_frame_state_timeout_t);
158 unlang_stack_t *stack = request->stack;
159
161 gext = unlang_group_to_timeout(g);
162
163 /*
164 * +1 so we don't mark the timeout frame as cancelled,
165 * we want unlang_timeout_resume_done to be called.
166 */
167 state->depth = stack->depth + 1;
168 state->request = request;
169
170 if (!gext->vpt) {
171 state->timeout = gext->timeout;
172 return unlang_timeout_set(p_result, request, frame);
173 }
174
175 fr_value_box_list_init(&state->result);
176
177 if (unlang_tmpl_push(state, &state->result, request, gext->vpt, NULL) < 0) return UNLANG_ACTION_FAIL;
178
180
182}
183
184/** When a timeout fires, run the given section.
185 *
186 * @param[in] request to push timeout onto
187 * @param[in] timeout when to run the timeout
188 * @param[in] cs section to run when the timeout fires.
189 * @param[in] top_frame Set to UNLANG_TOP_FRAME if the interpreter should return.
190 * Set to UNLANG_SUB_FRAME if the interprer should continue.
191 *
192 * @return
193 * - 0 on success.
194 * - -1 on failure.
195 */
196int unlang_timeout_section_push(request_t *request, CONF_SECTION *cs, fr_time_delta_t timeout, bool top_frame)
197{
198 /** Static instruction for performing xlat evaluations
199 *
200 */
201 static unlang_t timeout_instruction = {
203 .name = "timeout",
204 .debug_name = "timeout",
205 .actions = {
206 .actions = {
207 [RLM_MODULE_REJECT] = 0,
208 [RLM_MODULE_FAIL] = MOD_ACTION_RETURN, /* Exit out of nested levels */
209 [RLM_MODULE_OK] = 0,
210 [RLM_MODULE_HANDLED] = 0,
211 [RLM_MODULE_INVALID] = 0,
214 [RLM_MODULE_NOOP] = 0,
215 [RLM_MODULE_TIMEOUT] = MOD_ACTION_RETURN, /* Exit out of nested levels */
217 },
218 .retry = RETRY_INIT,
219 },
220 };
221
223 unlang_stack_t *stack = request->stack;
225 unlang_t *instruction;
226
227 /*
228 * Get the instruction we are supposed to run on timeout.
229 */
230 instruction = (unlang_t *)cf_data_value(cf_data_find(cs, unlang_group_t, NULL));
231 if (!instruction) {
232 REDEBUG("Failed to find pre-compiled unlang for section %s { ... }",
233 cf_section_name1(cs));
234 return -1;
235 }
236
237 /*
238 * Push a new timeout frame onto the stack
239 */
240 if (unlang_interpret_push(request, &timeout_instruction,
241 RLM_MODULE_NOT_SET, UNLANG_NEXT_STOP, top_frame) < 0) return -1;
242 frame = &stack->frame[stack->depth];
243
244 /*
245 * Allocate its state, and set the timeout.
246 */
247 MEM(frame->state = state = talloc_zero(stack, unlang_frame_state_timeout_t));
248
249 RINDENT_SAVE(state, request);
250 state->depth = stack->depth;
251 state->request = request;
252 state->timeout = timeout;
253 state->instruction = instruction;
254
255 if (fr_timer_in(state, unlang_interpret_event_list(request)->tl, &state->ev, timeout,
256 false, unlang_timeout_handler, state) < 0) {
257 RPEDEBUG("Failed setting timeout for section %s", cf_section_name1(cs));
258 return -1;
259 }
260
262
263 return 0;
264
265}
266
268{
270 &(unlang_op_t){
271 .name = "timeout",
272 .interpret = unlang_timeout,
274 .signal = unlang_timeout_signal,
275 .frame_state_size = sizeof(unlang_frame_state_timeout_t),
276 .frame_state_type = "unlang_frame_state_timeout_t",
277 });
278}
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_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
@ UNLANG_ACTION_CALCULATE_RESULT
Calculate a new section rlm_rcode_t value.
Definition action.h:37
#define RCSID(id)
Definition build.h:485
#define unlikely(_x)
Definition build.h:383
#define UNUSED
Definition build.h:317
A section grouping multiple CONF_PAIR.
Definition cf_priv.h:101
void * cf_data_value(CONF_DATA const *cd)
Return the user assigned value of CONF_DATA.
Definition cf_util.c:1762
char const * cf_section_name1(CONF_SECTION const *cs)
Return the second identifier of a CONF_SECTION.
Definition cf_util.c:1170
#define cf_data_find(_cf, _type, _name)
Definition cf_util.h:244
#define MEM(x)
Definition debug.h:36
Declarations for the "group" keyword.
unlang_action_t unlang_interpret_push_children(UNUSED rlm_rcode_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:239
int unlang_interpret_push_instruction(request_t *request, void *instruction, rlm_rcode_t default_rcode, bool top_frame)
Push an instruction onto the request stack for later interpretation.
Definition interpret.c:949
void unlang_interpret_mark_runnable(request_t *request)
Mark a request as resumable.
Definition interpret.c:1360
bool unlang_request_is_scheduled(request_t const *request)
Return whether a request is currently scheduled.
Definition interpret.c:1313
int unlang_interpret_push(request_t *request, unlang_t const *instruction, rlm_rcode_t default_rcode, bool do_next_sibling, bool top_frame)
Push a new frame onto the stack.
Definition interpret.c:143
void unlang_interpret_signal(request_t *request, fr_signal_t action)
Send a signal (usually stop) to a request.
Definition interpret.c:1147
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:1087
fr_event_list_t * unlang_interpret_event_list(request_t *request)
Get the event list for the current interpreter.
Definition interpret.c:1757
#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
void unlang_register(int type, unlang_op_t *op)
Register an operation with the interpreter.
Definition base.c:63
static char * stack[MAX_STACK]
Definition radmin.c:159
Unlang module actions.
@ MOD_ACTION_RETURN
Definition mod_action.h:40
#define REDEBUG(fmt,...)
Definition radclient.h:52
#define RDEBUG(fmt,...)
Definition radclient.h:53
rlm_rcode_t
Return codes indicating the result of the module call.
Definition rcode.h:40
@ RLM_MODULE_INVALID
The module considers the request invalid.
Definition rcode.h:45
@ RLM_MODULE_OK
The module is OK, continue.
Definition rcode.h:43
@ RLM_MODULE_FAIL
Module failed, don't reply.
Definition rcode.h:42
@ RLM_MODULE_DISALLOW
Reject the request (user is locked out).
Definition rcode.h:46
@ RLM_MODULE_REJECT
Immediately reject the request.
Definition rcode.h:41
@ RLM_MODULE_TIMEOUT
Module (or section) timed out.
Definition rcode.h:50
@ RLM_MODULE_NOTFOUND
User not found.
Definition rcode.h:47
@ RLM_MODULE_UPDATED
OK (pairs modified).
Definition rcode.h:49
@ RLM_MODULE_NOT_SET
Error resolving rcode (should not be returned by modules).
Definition rcode.h:52
@ RLM_MODULE_NOOP
Module succeeded without doing anything.
Definition rcode.h:48
@ RLM_MODULE_HANDLED
The module handled the request, so stop.
Definition rcode.h:44
#define RETURN_MODULE_TIMEOUT
Definition rcode.h:65
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
A time delta, a difference in time measured in nanoseconds.
Definition time.h:80
"server local" time.
Definition time.h:69
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:267
static unlang_action_t unlang_timeout_resume_done(UNUSED rlm_rcode_t *p_result, UNUSED request_t *request, unlang_stack_frame_t *frame)
Definition timeout.c:106
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
fr_time_delta_t timeout
Definition timeout.c:38
static unlang_action_t unlang_timeout_done(rlm_rcode_t *p_result, request_t *request, unlang_stack_frame_t *frame)
Definition timeout.c:140
static unlang_action_t unlang_timeout_set(rlm_rcode_t *p_result, request_t *request, unlang_stack_frame_t *frame)
Definition timeout.c:120
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:196
unlang_t * instruction
to run on timeout
Definition timeout.c:45
static unlang_action_t unlang_timeout(rlm_rcode_t *p_result, request_t *request, unlang_stack_frame_t *frame)
Definition timeout.c:153
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, 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:260
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
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)
rlm_rcode_t result
The result from executing the instruction.
@ 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.
#define RETRY_INIT
Definition retry.h:39