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: f72e545e8e3043e48558b711c3acd8b92423ad02 $
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: f72e545e8e3043e48558b711c3acd8b92423ad02 $")
26
27#include <freeradius-devel/unlang/timeout.h>
28#include "group_priv.h"
29#include "timeout_priv.h"
30#include "unlang_priv.h"
31
32typedef struct {
33 bool success;
34 int depth;
39
40 fr_value_box_list_t result;
41
42 unlang_t *instruction; //!< to run on timeout
44
46{
47 unlang_frame_state_timeout_t *state = talloc_get_type_abort(ctx, unlang_frame_state_timeout_t);
48 request_t *request = talloc_get_type_abort(state->request, request_t);
49
50 RDEBUG("Timeout reached, signalling interpreter to cancel child section.");
51
52 /*
53 * Has to be done BEFORE cancelling the frames, as one might be yielded.
54 */
56
57 /*
58 * Signal all lower frames to exit, but the request can keep running.
59 */
61 state->success = false;
62
63 RINDENT_RESTORE(request, state);
64
65 if (!state->instruction) return;
66
67 if (unlang_interpret_push_instruction(request, state->instruction, RLM_MODULE_FAIL, true) < 0) {
68 unlang_interpret_signal(request, FR_SIGNAL_CANCEL); /* also stops the request and does cleanups */
69 }
70}
71
73{
74 unlang_frame_state_timeout_t *state = talloc_get_type_abort(frame->state, unlang_frame_state_timeout_t);
75 unlang_t *unlang;
76
77 unlang = frame->instruction->next;
78
79 /*
80 * No timeout, we go to the next instruction.
81 *
82 * Unless the next instruction is a "catch timeout", in which case we skip it.
83 */
84 if (state->success) {
85 if (!unlang || (unlang->type != UNLANG_TYPE_CATCH)) {
87 }
88
89 /*
90 * We skip the "catch" section if there's no timeout.
91 */
92 return frame_set_next(frame, unlang->next);
93 }
94
95 RWDEBUG("Timeout exceeded");
96
97 /*
98 * If there's a next instruction, AND it's a "catch", then we catch the timeout.
99 */
100 if (unlang && (unlang->type == UNLANG_TYPE_CATCH)) {
101 return frame_set_next(frame, unlang);
102 }
103
104 return UNLANG_ACTION_FAIL;
105}
106
108{
109 unlang_frame_state_timeout_t *state = talloc_get_type_abort(frame->state, unlang_frame_state_timeout_t);
110 fr_time_t timeout;
111
112 /*
113 * Save current indentation for the error path.
114 */
115 RINDENT_SAVE(state, request);
116
117 timeout = fr_time_add(fr_time(), state->timeout);
118
119 if (fr_timer_at(state, unlang_interpret_event_list(request)->tl, &state->ev, timeout,
120 false, unlang_timeout_handler, state) < 0) {
121 RPEDEBUG("Failed inserting event");
122 *p_result = RLM_MODULE_FAIL;
124 }
125
127 state->success = true;
128
129 return unlang_interpret_push_children(p_result, request, frame->result, UNLANG_NEXT_SIBLING);
130}
131
133{
134 unlang_frame_state_timeout_t *state = talloc_get_type_abort(frame->state, unlang_frame_state_timeout_t);
135 fr_value_box_t *box = fr_value_box_list_head(&state->result);
136
137 /*
138 * compile_timeout() ensures that the tmpl is cast to time_delta, so we don't have to do any more work here.
139 */
140 state->timeout = box->vb_time_delta;
141
142 return unlang_timeout_set(p_result, request, frame);
143}
144
146{
148 unlang_timeout_t *gext;
149 unlang_frame_state_timeout_t *state = talloc_get_type_abort(frame->state, unlang_frame_state_timeout_t);
150 unlang_stack_t *stack = request->stack;
151
153 gext = unlang_group_to_timeout(g);
154
155 state->depth = stack->depth;
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, &state->result, request, gext->vpt, NULL) < 0) return UNLANG_ACTION_FAIL;
166
168
170}
171
172/** When a timeout fires, run the given section.
173 *
174 * @param[in] request to push timeout onto
175 * @param[in] timeout when to run the timeout
176 * @param[in] cs section to run when the timeout fires.
177 * @param[in] top_frame Set to UNLANG_TOP_FRAME if the interpreter should return.
178 * Set to UNLANG_SUB_FRAME if the interprer should continue.
179 *
180 * @return
181 * - 0 on success.
182 * - -1 on failure.
183 */
184int unlang_timeout_section_push(request_t *request, CONF_SECTION *cs, fr_time_delta_t timeout, bool top_frame)
185{
186 /** Static instruction for performing xlat evaluations
187 *
188 */
189 static unlang_t timeout_instruction = {
191 .name = "timeout",
192 .debug_name = "timeout",
193 .actions = {
194 .actions = {
195 [RLM_MODULE_REJECT] = 0,
196 [RLM_MODULE_FAIL] = MOD_ACTION_RETURN, /* Exit out of nested levels */
197 [RLM_MODULE_OK] = 0,
198 [RLM_MODULE_HANDLED] = 0,
199 [RLM_MODULE_INVALID] = 0,
202 [RLM_MODULE_NOOP] = 0,
204 },
205 .retry = RETRY_INIT,
206 },
207 };
208
210 unlang_stack_t *stack = request->stack;
212 unlang_t *instruction;
213 fr_time_t when;
214
215 /*
216 * Get the instruction we are supposed to run on timeout.
217 */
218 instruction = (unlang_t *)cf_data_value(cf_data_find(cs, unlang_group_t, NULL));
219 if (!instruction) {
220 REDEBUG("Failed to find pre-compiled unlang for section %s { ... }",
221 cf_section_name1(cs));
222 return -1;
223 }
224
225 /*
226 * Push a new timeout frame onto the stack
227 */
228 if (unlang_interpret_push(request, &timeout_instruction,
229 RLM_MODULE_NOT_SET, UNLANG_NEXT_STOP, top_frame) < 0) return -1;
230 frame = &stack->frame[stack->depth];
231
232 /*
233 * Allocate its state, and set the timeout.
234 */
235 MEM(frame->state = state = talloc_zero(stack, unlang_frame_state_timeout_t));
236
237 RINDENT_SAVE(state, request);
238 state->depth = stack->depth;
239 state->request = request;
240 state->timeout = timeout;
241 state->instruction = instruction;
242 state->success = true;
243
244 when = fr_time_add(fr_time(), timeout);
245
246 if (fr_timer_at(state, unlang_interpret_event_list(request)->tl, &state->ev, when,
247 false, unlang_timeout_handler, state) < 0) {
248 RPEDEBUG("Failed setting timeout for section %s", cf_section_name1(cs));
249 return -1;
250 }
251
253
254 return 0;
255
256}
257
259{
261 &(unlang_op_t){
262 .name = "timeout",
263 .interpret = unlang_timeout,
264 .debug_braces = true,
265 .frame_state_size = sizeof(unlang_frame_state_timeout_t),
266 .frame_state_type = "unlang_frame_state_timeout_t",
267 });
268}
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:43
@ 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 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.
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:1032
void unlang_interpret_mark_runnable(request_t *request)
Mark a request as resumable.
Definition interpret.c:1418
void unlang_frame_signal(request_t *request, fr_signal_t action, int limit)
Send a signal (usually stop) to a request.
Definition interpret.c:1158
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:159
void unlang_interpret_signal(request_t *request, fr_signal_t action)
Send a signal (usually stop) to a request.
Definition interpret.c:1212
unlang_action_t unlang_interpret_push_children(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:255
fr_event_list_t * unlang_interpret_event_list(request_t *request)
Get the event list for the current interpreter.
Definition interpret.c:1822
#define RWDEBUG(fmt,...)
Definition log.h:361
#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:158
@ 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_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:51
@ 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
@ FR_SIGNAL_CANCEL
Request has been cancelled.
Definition signal.h:40
#define fr_time()
Allow us to arbitrarily manipulate time.
Definition state_test.c:8
#define fr_time_add(_a, _b)
Add a time/time delta together.
Definition time.h:196
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:45
void unlang_timeout_init(void)
Definition timeout.c:258
static unlang_action_t unlang_timeout_xlat_done(rlm_rcode_t *p_result, request_t *request, unlang_stack_frame_t *frame)
Definition timeout.c:132
static unlang_action_t unlang_timeout_resume_done(UNUSED rlm_rcode_t *p_result, request_t *request, unlang_stack_frame_t *frame)
Definition timeout.c:72
fr_time_delta_t timeout
Definition timeout.c:35
static unlang_action_t unlang_timeout_set(rlm_rcode_t *p_result, request_t *request, unlang_stack_frame_t *frame)
Definition timeout.c:107
fr_value_box_list_t result
Definition timeout.c:40
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:184
unlang_t * instruction
to run on timeout
Definition timeout.c:42
static unlang_action_t unlang_timeout(rlm_rcode_t *p_result, request_t *request, unlang_stack_frame_t *frame)
Definition timeout.c:145
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:75
#define fr_timer_at(...)
Definition timer.h:80
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:261
Private interpreter structures and functions.
unlang_t * next
Next node (executed on UNLANG_ACTION_EXECUTE_NEXT et al).
#define UNLANG_NEXT_SIBLING
Definition unlang_priv.h:93
void * state
Stack frame specialisations.
#define UNLANG_NEXT_STOP
Definition unlang_priv.h:92
static unlang_group_t * unlang_generic_to_group(unlang_t const *p)
@ UNLANG_TYPE_TIMEOUT
time-based timeouts.
Definition unlang_priv.h:68
@ UNLANG_TYPE_CATCH
catch a previous try
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.
rlm_rcode_t result
The result from executing the instruction.
static unlang_action_t frame_set_next(unlang_stack_frame_t *frame, unlang_t *unlang)
unlang_type_t type
The specialisation of this node.
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