The FreeRADIUS server  $Id: 15bac2a4c627c01d1aa2047687b3418955ac7f00 $
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: a33421f8573e1eca48fee3cea415727d9dd35344 $
19  *
20  * @file unlang/timeout.c
21  * @brief Unlang "timeout" keyword evaluation.
22  *
23  * @copyright 2022 Network RADIUS SAS (legal@networkradius.com)
24  */
25 RCSID("$Id: a33421f8573e1eca48fee3cea415727d9dd35344 $")
26 
27 #include "group_priv.h"
28 #include "timeout_priv.h"
29 
30 typedef struct {
31  bool success;
32  int depth;
37 
38  fr_value_box_list_t result;
40 
42 {
43  unlang_frame_state_timeout_t *state = talloc_get_type_abort(ctx, unlang_frame_state_timeout_t);
44  request_t *request = talloc_get_type_abort(state->request, request_t);
45 
46  RDEBUG("Timeout reached, signalling interpreter to cancel child section.");
47 
48  /*
49  * Has to be done BEFORE cancelling the frames, as one might be yielded.
50  */
52 
53  /*
54  * Signal all lower frames to exit.
55  */
56  unlang_frame_signal(request, FR_SIGNAL_CANCEL, state->depth);
57  state->success = false;
58 }
59 
61 {
62  unlang_frame_state_timeout_t *state = talloc_get_type_abort(frame->state, unlang_frame_state_timeout_t);
63 
64  if (!state->success) {
65  RINDENT_RESTORE(request, state);
66 
67  RWDEBUG("Timeout exceeded");
68  return UNLANG_ACTION_FAIL;
69  }
70 
72 }
73 
75 {
76  unlang_frame_state_timeout_t *state = talloc_get_type_abort(frame->state, unlang_frame_state_timeout_t);
78 
79  /*
80  * Save current indentation for the error path.
81  */
82  RINDENT_SAVE(state, request);
83 
84  timeout = fr_time_add(fr_time(), state->timeout);
85 
86  if (fr_event_timer_at(state, unlang_interpret_event_list(request), &state->ev, timeout,
87  unlang_timeout_handler, state) < 0) {
88  RPEDEBUG("Failed inserting event");
89  *p_result = RLM_MODULE_FAIL;
91  }
92 
94  state->success = true;
95 
96  return unlang_interpret_push_children(p_result, request, frame->result, UNLANG_NEXT_SIBLING);
97 }
98 
100 {
101  unlang_frame_state_timeout_t *state = talloc_get_type_abort(frame->state, unlang_frame_state_timeout_t);
102  fr_value_box_t *box = fr_value_box_list_head(&state->result);
103 
104  /*
105  * compile_timeout() ensures that the tmpl is cast to time_delta, so we don't have to do any more work here.
106  */
107  state->timeout = box->vb_time_delta;
108 
109  return unlang_timeout_set(p_result, request, frame);
110 }
111 
113 {
114  unlang_group_t *g;
115  unlang_timeout_t *gext;
116  unlang_frame_state_timeout_t *state = talloc_get_type_abort(frame->state, unlang_frame_state_timeout_t);
117  unlang_stack_t *stack = request->stack;
118 
120  gext = unlang_group_to_timeout(g);
121 
122  state->depth = stack->depth;
123  state->request = request;
124 
125  if (!gext->vpt) {
126  state->timeout = gext->timeout;
127  return unlang_timeout_set(p_result, request, frame);
128  }
129 
130  fr_value_box_list_init(&state->result);
131 
132  if (unlang_tmpl_push(state, &state->result, request, gext->vpt, NULL) < 0) return UNLANG_ACTION_FAIL;
133 
135 
137 }
138 
139 
141 {
143  &(unlang_op_t){
144  .name = "timeout",
145  .interpret = unlang_timeout,
146  .debug_braces = true,
147  .frame_state_size = sizeof(unlang_frame_state_timeout_t),
148  .frame_state_type = "unlang_frame_state_timeout_t",
149  });
150 }
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:444
#define UNUSED
Definition: build.h:313
static fr_time_delta_t timeout
Definition: dhcpclient.c:54
#define fr_event_timer_at(...)
Definition: event.h:250
fr_event_list_t * unlang_interpret_event_list(request_t *request)
Get the event list for the current interpreter.
Definition: interpret.c:1745
void unlang_interpret_mark_runnable(request_t *request)
Mark a request as resumable.
Definition: interpret.c:1340
void unlang_frame_signal(request_t *request, fr_signal_t action, int limit)
Send a signal (usually stop) to a request.
Definition: interpret.c:1130
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:243
#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
Definition: log.h:40
void unlang_register(int type, unlang_op_t *op)
Register an operation with the interpreter.
Definition: base.c:65
Stores all information relating to an event list.
Definition: event.c:411
A timer event.
Definition: event.c:102
static char * stack[MAX_STACK]
Definition: radmin.c:158
#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_FAIL
Module failed, don't reply.
Definition: rcode.h:42
#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_event_list_t *el, UNUSED fr_time_t now, void *ctx)
Definition: timeout.c:41
void unlang_timeout_init(void)
Definition: timeout.c:140
static unlang_action_t unlang_timeout_xlat_done(rlm_rcode_t *p_result, request_t *request, unlang_stack_frame_t *frame)
Definition: timeout.c:99
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:60
fr_event_timer_t const * ev
Definition: timeout.c:36
fr_time_delta_t timeout
Definition: timeout.c:33
static unlang_action_t unlang_timeout_set(rlm_rcode_t *p_result, request_t *request, unlang_stack_frame_t *frame)
Definition: timeout.c:74
fr_value_box_list_t result
Definition: timeout.c:38
static unlang_action_t unlang_timeout(rlm_rcode_t *p_result, request_t *request, unlang_stack_frame_t *frame)
Definition: timeout.c:112
static unlang_timeout_t * unlang_group_to_timeout(unlang_group_t *g)
Cast a group structure to the timeout keyword extension.
Definition: timeout_priv.h:40
fr_time_delta_t timeout
Definition: timeout_priv.h:34
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:259
static fr_event_list_t * el
#define UNLANG_NEXT_SIBLING
Definition: unlang_priv.h:103
void * state
Stack frame specialisations.
Definition: unlang_priv.h:304
static unlang_group_t * unlang_generic_to_group(unlang_t const *p)
Definition: unlang_priv.h:539
@ UNLANG_TYPE_TIMEOUT
time-based timeouts.
Definition: unlang_priv.h:78
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.
Definition: unlang_priv.h:526
unlang_t const * instruction
The unlang node we're evaluating.
Definition: unlang_priv.h:289
rlm_rcode_t result
The result from executing the instruction.
Definition: unlang_priv.h:308
Generic representation of a grouping.
Definition: unlang_priv.h:155
An unlang operation.
Definition: unlang_priv.h:214
Our interpreter stack, as distinct from the C stack.
Definition: unlang_priv.h:288
An unlang stack associated with a request.
Definition: unlang_priv.h:322