blob: fbe22d726670d4126172a60463b2806a09c90e6b [file] [log] [blame]
Harald Welte136e7372016-05-29 10:53:17 +09001/* Osmocom generic Finite State Machine implementation
2 *
3 * (C) 2016 by Harald Welte <laforge@gnumonks.org>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
18 * MA 02110-1301, USA.
19 */
20
21#include <errno.h>
22#include <stdbool.h>
Harald Welte8808bb42017-01-07 11:11:03 +010023#include <string.h>
Harald Welte136e7372016-05-29 10:53:17 +090024
25#include <osmocom/core/fsm.h>
26#include <osmocom/core/talloc.h>
27#include <osmocom/core/logging.h>
28#include <osmocom/core/utils.h>
29
30/*! \addtogroup fsm
31 * @{
Harald Welte136e7372016-05-29 10:53:17 +090032 * \brief Finite State Machine abstraction
33 *
34 * This is a generic C-language abstraction for implementing finite
35 * state machines within the Osmocom framework. It is intended to
36 * replace existing hand-coded or even only implicitly existing FSMs
37 * all over the existing code base.
38 *
39 * An libosmocore FSM is described by its \ref osmo_fsm description,
40 * which in turn refers to an array of \ref osmo_fsm_state descriptor,
41 * each describing a single state in the FSM.
42 *
43 * The general idea is that all actions performed within one state are
44 * located at one position in the code (the state's action function),
45 * as opposed to the 'message-centric' view of e.g. the existing
46 * state machines of the LAPD(m) coe, where there is one message for
47 * eahc possible event (primitive), and the function then needs to
48 * concern itself on how to handle that event over all possible states.
49 *
50 * For each state, there is a bit-mask of permitted input events for
51 * this state, as well as a bit-mask of permitted new output states to
52 * which the state can change. Furthermore, there is a function
53 * pointer implementing the actual handling of the input events
54 * occurring whilst in thta state.
55 *
56 * Furthermore, each state offers a function pointer that can be
57 * executed just before leaving a state, and another one just after
58 * entering a state.
59 *
60 * When transitioning into a new state, an optional timer number and
61 * time-out can be passed along. The timer is started just after
62 * entering the new state, and will call the \ref osmo_fsm timer_cb
63 * function once it expires. This is intended to be used in telecom
64 * state machines where a given timer (identified by a certain number)
65 * is started to terminate the fsm or terminate the fsm once expected
66 * events are not happening before timeout expiration.
67 *
68 * As there can often be many concurrent FSMs of one given class, we
69 * introduce the concept of \ref osmo_fsm_inst, i.e. an FSM instance.
70 * The instance keeps the actual state, while the \ref osmo_fsm
71 * descriptor contains the static/const descriptor of the FSM's states
72 * and possible transitions.
73 *
74 * osmo_fsm are integrated with the libosmocore logging system. The
75 * logging sub-system is determined by the FSM descriptor, as we assume
76 * one FSM (let's say one related to a location update procedure) is
77 * inevitably always tied to a sub-system. The logging level however
78 * is configurable for each FSM instance, to ensure that e.g. DEBUG
79 * logging can be used for the LU procedure of one subscriber, while
80 * NOTICE level is used for all other subscribers.
81 *
82 * In order to attach private state to the \ref osmo_fsm_inst, it
83 * offers an opaque priv pointer.
Harald Welte136e7372016-05-29 10:53:17 +090084 */
85
Harald Welte96e2a002017-06-12 21:44:18 +020086/*! \file fsm.c */
87
Harald Welte34193912017-01-07 11:49:55 +010088LLIST_HEAD(osmo_g_fsms);
Harald Welte136e7372016-05-29 10:53:17 +090089static bool fsm_log_addr = true;
90
91/*! \brief specify if FSM instance addresses should be logged or not
92 *
93 * By default, the FSM name includes the pointer address of the \ref
Neels Hofmeyra3953e02016-12-14 18:34:30 +010094 * osmo_fsm_inst. This behavior can be disabled (and re-enabled)
Harald Welte136e7372016-05-29 10:53:17 +090095 * using this function.
96 *
97 * \param[in] log_addr Indicate if FSM instance address shall be logged
98 */
99void osmo_fsm_log_addr(bool log_addr)
100{
Max61281f42016-11-01 10:49:31 +0100101 fsm_log_addr = log_addr;
Harald Welte136e7372016-05-29 10:53:17 +0900102}
103
Harald Welte8808bb42017-01-07 11:11:03 +0100104struct osmo_fsm *osmo_fsm_find_by_name(const char *name)
105{
106 struct osmo_fsm *fsm;
Harald Welte34193912017-01-07 11:49:55 +0100107 llist_for_each_entry(fsm, &osmo_g_fsms, list) {
Harald Welte8808bb42017-01-07 11:11:03 +0100108 if (!strcmp(name, fsm->name))
109 return fsm;
110 }
111 return NULL;
112}
113
Harald Welte4585e672017-04-16 17:23:56 +0200114struct osmo_fsm_inst *osmo_fsm_inst_find_by_name(const struct osmo_fsm *fsm,
115 const char *name)
116{
117 struct osmo_fsm_inst *fi;
118
119 llist_for_each_entry(fi, &fsm->instances, list) {
120 if (!strcmp(name, fi->name))
121 return fi;
122 }
123 return NULL;
124}
125
126struct osmo_fsm_inst *osmo_fsm_inst_find_by_id(const struct osmo_fsm *fsm,
127 const char *id)
128{
129 struct osmo_fsm_inst *fi;
130
131 llist_for_each_entry(fi, &fsm->instances, list) {
132 if (!strcmp(id, fi->id))
133 return fi;
134 }
135 return NULL;
136}
137
Harald Welte136e7372016-05-29 10:53:17 +0900138/*! \brief register a FSM with the core
139 *
140 * A FSM descriptor needs to be registered with the core before any
141 * instances can be created for it.
142 *
143 * \param[in] fsm Descriptor of Finite State Machine to be registered
144 * \returns 0 on success; negative on error
145 */
146int osmo_fsm_register(struct osmo_fsm *fsm)
147{
Harald Welte8808bb42017-01-07 11:11:03 +0100148 if (osmo_fsm_find_by_name(fsm->name))
149 return -EEXIST;
Harald Welte34193912017-01-07 11:49:55 +0100150 llist_add_tail(&fsm->list, &osmo_g_fsms);
Harald Welte136e7372016-05-29 10:53:17 +0900151 INIT_LLIST_HEAD(&fsm->instances);
152
153 return 0;
154}
155
156/*! \brief unregister a FSM from the core
157 *
158 * Once the FSM descriptor is unregistered, active instances can still
159 * use it, but no new instances may be created for it.
160 *
161 * \param[in] fsm Descriptor of Finite State Machine to be removed
162 */
163void osmo_fsm_unregister(struct osmo_fsm *fsm)
164{
165 llist_del(&fsm->list);
166}
167
168/* small wrapper function around timer expiration (for logging) */
169static void fsm_tmr_cb(void *data)
170{
171 struct osmo_fsm_inst *fi = data;
172 struct osmo_fsm *fsm = fi->fsm;
Harald Weltef627c0f2016-06-18 10:36:25 +0200173 uint32_t T = fi->T;
Harald Welte136e7372016-05-29 10:53:17 +0900174
175 LOGPFSM(fi, "Timeout of T%u\n", fi->T);
176
Harald Weltef627c0f2016-06-18 10:36:25 +0200177 if (fsm->timer_cb) {
178 int rc = fsm->timer_cb(fi);
Harald Welte9e83c3d2017-04-16 17:24:46 +0200179 if (rc != 1) {
180 fi->T = 0;
Harald Weltef627c0f2016-06-18 10:36:25 +0200181 return;
Harald Welte9e83c3d2017-04-16 17:24:46 +0200182 }
Harald Weltef627c0f2016-06-18 10:36:25 +0200183 LOGPFSM(fi, "timer_cb requested termination\n");
184 } else
185 LOGPFSM(fi, "No timer_cb, automatic termination\n");
186
187 /* if timer_cb returns 1 or there is no timer_cb */
Harald Welte9e83c3d2017-04-16 17:24:46 +0200188 fi->T = 0;
Harald Weltef627c0f2016-06-18 10:36:25 +0200189 osmo_fsm_inst_term(fi, OSMO_FSM_TERM_TIMEOUT, &T);
Harald Welte136e7372016-05-29 10:53:17 +0900190}
191
192/*! \brief allocate a new instance of a specified FSM
193 * \param[in] fsm Descriptor of the FSM
194 * \param[in] ctx talloc context from which to allocate memory
195 * \param[in] priv private data reference store in fsm instance
196 * \param[in] log_level The log level for events of this FSM
197 * \returns newly-allocated, initialized and registered FSM instance
198 */
199struct osmo_fsm_inst *osmo_fsm_inst_alloc(struct osmo_fsm *fsm, void *ctx, void *priv,
200 int log_level, const char *id)
201{
202 struct osmo_fsm_inst *fi = talloc_zero(ctx, struct osmo_fsm_inst);
203
204 fi->fsm = fsm;
205 fi->priv = priv;
206 fi->log_level = log_level;
Pablo Neira Ayuso44f423f2017-05-08 18:00:28 +0200207 osmo_timer_setup(&fi->timer, fsm_tmr_cb, fi);
Harald Weltef3239112016-07-10 15:11:45 +0200208 if (id)
209 fi->id = talloc_strdup(fi, id);
Harald Welte136e7372016-05-29 10:53:17 +0900210
211 if (!fsm_log_addr) {
212 if (id)
213 fi->name = talloc_asprintf(fi, "%s(%s)", fsm->name, id);
214 else
215 fi->name = talloc_asprintf(fi, "%s", fsm->name);
216 } else {
217 if (id)
218 fi->name = talloc_asprintf(fi, "%s(%s)[%p]", fsm->name,
219 id, fi);
220 else
221 fi->name = talloc_asprintf(fi, "%s[%p]", fsm->name, fi);
222 }
223
224 INIT_LLIST_HEAD(&fi->proc.children);
225 INIT_LLIST_HEAD(&fi->proc.child);
226 llist_add(&fi->list, &fsm->instances);
227
228 LOGPFSM(fi, "Allocated\n");
229
230 return fi;
231}
232
233/*! \brief allocate a new instance of a specified FSM as child of
234 * other FSM instance
235 *
236 * This is like \ref osmo_fsm_inst_alloc but using the parent FSM as
237 * talloc context, and inheriting the log level of the parent.
238 *
239 * \param[in] fsm Descriptor of the to-be-allocated FSM
240 * \param[in] parent Parent FSM instance
241 * \param[in] parent_term_event Event to be sent to parent when terminating
242 * \returns newly-allocated, initialized and registered FSM instance
243 */
244struct osmo_fsm_inst *osmo_fsm_inst_alloc_child(struct osmo_fsm *fsm,
245 struct osmo_fsm_inst *parent,
246 uint32_t parent_term_event)
247{
248 struct osmo_fsm_inst *fi;
249
250 fi = osmo_fsm_inst_alloc(fsm, parent, NULL, parent->log_level,
251 parent->id);
252 if (!fi) {
253 /* indicate immediate termination to caller */
254 osmo_fsm_inst_dispatch(parent, parent_term_event, NULL);
255 return NULL;
256 }
257
258 LOGPFSM(fi, "is child of %s\n", osmo_fsm_inst_name(parent));
259
260 fi->proc.parent = parent;
261 fi->proc.parent_term_event = parent_term_event;
262 llist_add(&fi->proc.child, &parent->proc.children);
263
264 return fi;
265}
266
267/*! \brief delete a given instance of a FSM
268 * \param[in] fsm The FSM to be un-registered and deleted
269 */
270void osmo_fsm_inst_free(struct osmo_fsm_inst *fi)
271{
Max3de97e12016-11-02 10:37:58 +0100272 LOGPFSM(fi, "Deallocated\n");
Harald Welte136e7372016-05-29 10:53:17 +0900273 osmo_timer_del(&fi->timer);
274 llist_del(&fi->list);
275 talloc_free(fi);
276}
277
278/*! \brief get human-readable name of FSM event
279 * \param[in] fsm FSM descriptor of event
280 * \param[in] event Event integer value
281 * \returns string rendering of the event
282 */
283const char *osmo_fsm_event_name(struct osmo_fsm *fsm, uint32_t event)
284{
285 static char buf[32];
286 if (!fsm->event_names) {
287 snprintf(buf, sizeof(buf), "%u", event);
288 return buf;
289 } else
290 return get_value_string(fsm->event_names, event);
291}
292
293/*! \brief get human-readable name of FSM instance
294 * \param[in] fi FSM instance
295 * \returns string rendering of the FSM identity
296 */
297const char *osmo_fsm_inst_name(struct osmo_fsm_inst *fi)
298{
299 if (!fi)
300 return "NULL";
301
302 if (fi->name)
303 return fi->name;
304 else
305 return fi->fsm->name;
306}
307
308/*! \brief get human-readable name of FSM instance
309 * \param[in] fsm FSM descriptor
310 * \param[in] state FSM state number
311 * \returns string rendering of the FSM state
312 */
313const char *osmo_fsm_state_name(struct osmo_fsm *fsm, uint32_t state)
314{
315 static char buf[32];
316 if (state >= fsm->num_states) {
317 snprintf(buf, sizeof(buf), "unknown %u", state);
318 return buf;
319 } else
320 return fsm->states[state].name;
321}
322
323/*! \brief perform a state change of the given FSM instance
324 *
Neels Hofmeyr725698a2016-12-14 17:24:54 +0100325 * Best invoke via the osmo_fsm_inst_state_chg() macro which logs the source
326 * file where the state change was effected. Alternatively, you may pass \a
327 * file as NULL to use the normal file/line indication instead.
328 *
Harald Welte136e7372016-05-29 10:53:17 +0900329 * All changes to the FSM instance state must be made via this
330 * function. It verifies that the existing state actually permits a
331 * transiiton to new_state.
332 *
333 * timeout_secs and T are optional parameters, and only have any effect
334 * if timeout_secs is not 0. If the timeout function is used, then the
335 * new_state is entered, and the FSM instances timer is set to expire
336 * in timeout_secs functions. At that time, the FSM's timer_cb
337 * function will be called for handling of the timeout by the user.
338 *
339 * \param[in] fi FSM instance whose state is to change
340 * \param[in] new_state The new state into which we should change
341 * \param[in] timeout_secs Timeout in seconds (if !=0)
342 * \param[in] T Timer number (if \ref timeout_secs != 0)
Neels Hofmeyrb805cc12016-12-23 04:23:18 +0100343 * \param[in] file Calling source file (from osmo_fsm_inst_state_chg macro)
344 * \param[in] line Calling source line (from osmo_fsm_inst_state_chg macro)
Harald Welte136e7372016-05-29 10:53:17 +0900345 * \returns 0 on success; negative on error
346 */
Neels Hofmeyr725698a2016-12-14 17:24:54 +0100347int _osmo_fsm_inst_state_chg(struct osmo_fsm_inst *fi, uint32_t new_state,
348 unsigned long timeout_secs, int T,
349 const char *file, int line)
Harald Welte136e7372016-05-29 10:53:17 +0900350{
351 struct osmo_fsm *fsm = fi->fsm;
352 uint32_t old_state = fi->state;
353 const struct osmo_fsm_state *st = &fsm->states[fi->state];
354
355 /* validate if new_state is a valid state */
356 if (!(st->out_state_mask & (1 << new_state))) {
Neels Hofmeyr725698a2016-12-14 17:24:54 +0100357 LOGPFSMLSRC(fi, LOGL_ERROR, file, line,
358 "transition to state %s not permitted!\n",
359 osmo_fsm_state_name(fsm, new_state));
Harald Welte136e7372016-05-29 10:53:17 +0900360 return -EPERM;
361 }
362
Harald Welte02a66722016-07-10 15:13:51 +0200363 /* delete the old timer */
364 osmo_timer_del(&fi->timer);
365
Harald Welte136e7372016-05-29 10:53:17 +0900366 if (st->onleave)
367 st->onleave(fi, new_state);
368
Neels Hofmeyr725698a2016-12-14 17:24:54 +0100369 LOGPFSMSRC(fi, file, line, "state_chg to %s\n",
370 osmo_fsm_state_name(fsm, new_state));
Harald Welte136e7372016-05-29 10:53:17 +0900371 fi->state = new_state;
Harald Welte460f9ef2016-08-01 00:38:36 +0200372 st = &fsm->states[new_state];
Harald Welte136e7372016-05-29 10:53:17 +0900373
Harald Welte136e7372016-05-29 10:53:17 +0900374 if (timeout_secs) {
Harald Weltef627c0f2016-06-18 10:36:25 +0200375 fi->T = T;
376 osmo_timer_schedule(&fi->timer, timeout_secs, 0);
Harald Welte136e7372016-05-29 10:53:17 +0900377 }
378
Harald Welte673018f2016-07-10 15:09:43 +0200379 /* Call 'onenter' last, user might terminate FSM from there */
380 if (st->onenter)
381 st->onenter(fi, old_state);
382
Harald Welte136e7372016-05-29 10:53:17 +0900383 return 0;
384}
385
386/*! \brief dispatch an event to an osmocom finite state machine instance
387 *
Neels Hofmeyr725698a2016-12-14 17:24:54 +0100388 * Best invoke via the osmo_fsm_inst_dispatch() macro which logs the source
389 * file where the event was effected. Alternatively, you may pass \a file as
390 * NULL to use the normal file/line indication instead.
391 *
Harald Welte136e7372016-05-29 10:53:17 +0900392 * Any incoming events to \ref osmo_fsm instances must be dispatched to
393 * them via this function. It verifies, whether the event is permitted
394 * based on the current state of the FSM. If not, -1 is returned.
395 *
396 * \param[in] fi FSM instance
397 * \param[in] event Event to send to FSM instance
398 * \param[in] data Data to pass along with the event
Neels Hofmeyrb805cc12016-12-23 04:23:18 +0100399 * \param[in] file Calling source file (from osmo_fsm_inst_dispatch macro)
400 * \param[in] line Calling source line (from osmo_fsm_inst_dispatch macro)
Harald Welte136e7372016-05-29 10:53:17 +0900401 * \returns 0 in case of success; negative on error
402 */
Neels Hofmeyr725698a2016-12-14 17:24:54 +0100403int _osmo_fsm_inst_dispatch(struct osmo_fsm_inst *fi, uint32_t event, void *data,
404 const char *file, int line)
Harald Welte136e7372016-05-29 10:53:17 +0900405{
406 struct osmo_fsm *fsm;
407 const struct osmo_fsm_state *fs;
408
409 if (!fi) {
Neels Hofmeyrc7155df2016-12-23 04:24:51 +0100410 LOGPSRC(DLGLOBAL, LOGL_ERROR, file, line,
411 "Trying to dispatch event %u to non-existent"
412 " FSM instance!\n", event);
Harald Welte136e7372016-05-29 10:53:17 +0900413 osmo_log_backtrace(DLGLOBAL, LOGL_ERROR);
414 return -ENODEV;
415 }
416
417 fsm = fi->fsm;
418 OSMO_ASSERT(fi->state < fsm->num_states);
419 fs = &fi->fsm->states[fi->state];
420
Neels Hofmeyr725698a2016-12-14 17:24:54 +0100421 LOGPFSMSRC(fi, file, line,
422 "Received Event %s\n", osmo_fsm_event_name(fsm, event));
Harald Welte136e7372016-05-29 10:53:17 +0900423
424 if (((1 << event) & fsm->allstate_event_mask) && fsm->allstate_action) {
425 fsm->allstate_action(fi, event, data);
426 return 0;
427 }
428
429 if (!((1 << event) & fs->in_event_mask)) {
Neels Hofmeyr725698a2016-12-14 17:24:54 +0100430 LOGPFSMLSRC(fi, LOGL_ERROR, file, line,
431 "Event %s not permitted\n",
432 osmo_fsm_event_name(fsm, event));
Harald Welte136e7372016-05-29 10:53:17 +0900433 return -1;
434 }
435 fs->action(fi, event, data);
436
437 return 0;
438}
439
440/*! \brief Terminate FSM instance with given cause
441 *
442 * This safely terminates the given FSM instance by first iterating
443 * over all children and sending them a termination event. Next, it
444 * calls the FSM descriptors cleanup function (if any), followed by
445 * releasing any memory associated with the FSM instance.
446 *
447 * Finally, the parent FSM instance (if any) is notified using the
448 * parent termination event configured at time of FSM instance start.
449 *
450 * \param[in] fi FSM instance to be terminated
451 * \param[in] cause Cause / reason for termination
Neels Hofmeyrb805cc12016-12-23 04:23:18 +0100452 * \param[in] data Opaque event data to be passed with the parent term event
453 * \param[in] file Calling source file (from osmo_fsm_inst_term macro)
454 * \param[in] line Calling source line (from osmo_fsm_inst_term macro)
Harald Welte136e7372016-05-29 10:53:17 +0900455 */
Neels Hofmeyr725698a2016-12-14 17:24:54 +0100456void _osmo_fsm_inst_term(struct osmo_fsm_inst *fi,
457 enum osmo_fsm_term_cause cause, void *data,
458 const char *file, int line)
Harald Welte136e7372016-05-29 10:53:17 +0900459{
Neels Hofmeyr3faa0142016-12-18 23:41:41 +0100460 struct osmo_fsm_inst *parent;
Harald Welte136e7372016-05-29 10:53:17 +0900461 uint32_t parent_term_event = fi->proc.parent_term_event;
462
Neels Hofmeyr5c5c78a2016-12-14 18:35:47 +0100463 LOGPFSMSRC(fi, file, line, "Terminating (cause = %s)\n",
464 osmo_fsm_term_cause_name(cause));
Harald Welte136e7372016-05-29 10:53:17 +0900465
Neels Hofmeyrc014f602016-12-23 04:26:39 +0100466 _osmo_fsm_inst_term_children(fi, OSMO_FSM_TERM_PARENT, NULL,
467 file, line);
468
469 /* delete ourselves from the parent */
Neels Hofmeyr3faa0142016-12-18 23:41:41 +0100470 parent = fi->proc.parent;
Neels Hofmeyrc014f602016-12-23 04:26:39 +0100471 if (parent)
472 LOGPFSMSRC(fi, file, line, "Removing from parent %s\n",
473 osmo_fsm_inst_name(parent));
474 llist_del(&fi->proc.child);
475
476 /* call destructor / clean-up function */
477 if (fi->fsm->cleanup)
478 fi->fsm->cleanup(fi, cause);
479
480 LOGPFSMSRC(fi, file, line, "Freeing instance\n");
Neels Hofmeyr3faa0142016-12-18 23:41:41 +0100481 /* Fetch parent again in case it has changed. */
482 parent = fi->proc.parent;
Neels Hofmeyrc014f602016-12-23 04:26:39 +0100483 osmo_fsm_inst_free(fi);
484
485 /* indicate our termination to the parent */
486 if (parent && cause != OSMO_FSM_TERM_PARENT)
487 _osmo_fsm_inst_dispatch(parent, parent_term_event, data,
488 file, line);
489}
490
491/*! \brief Terminate all child FSM instances of an FSM instance.
492 *
493 * Iterate over all children and send them a termination event, with the given
494 * cause. Pass OSMO_FSM_TERM_PARENT to avoid dispatching events from the
495 * terminated child FSMs.
496 *
497 * \param[in] fi FSM instance that should be cleared of child FSMs
498 * \param[in] cause Cause / reason for termination (OSMO_FSM_TERM_PARENT)
499 * \param[in] data Opaque event data to be passed with the parent term events
500 * \param[in] file Calling source file (from osmo_fsm_inst_term_children macro)
501 * \param[in] line Calling source line (from osmo_fsm_inst_term_children macro)
502 */
503void _osmo_fsm_inst_term_children(struct osmo_fsm_inst *fi,
504 enum osmo_fsm_term_cause cause,
505 void *data,
506 const char *file, int line)
507{
508 struct osmo_fsm_inst *first_child, *last_seen_first_child;
509
Neels Hofmeyr06ac9b42016-12-20 12:05:19 +0100510 /* iterate over all children, starting from the beginning every time:
511 * terminating an FSM may emit events that cause other FSMs to also
512 * terminate and remove themselves from this list. */
513 last_seen_first_child = NULL;
514 while (!llist_empty(&fi->proc.children)) {
515 first_child = llist_entry(fi->proc.children.next,
516 typeof(*first_child),
517 proc.child);
518
519 /* paranoia: do not loop forever */
520 if (first_child == last_seen_first_child) {
521 LOGPFSMLSRC(fi, LOGL_ERROR, file, line,
522 "Internal error while terminating child"
523 " FSMs: a child FSM is stuck\n");
524 break;
525 }
526 last_seen_first_child = first_child;
527
Harald Welte136e7372016-05-29 10:53:17 +0900528 /* terminate child */
Neels Hofmeyrc014f602016-12-23 04:26:39 +0100529 _osmo_fsm_inst_term(first_child, cause, data,
Neels Hofmeyr725698a2016-12-14 17:24:54 +0100530 file, line);
Harald Welte136e7372016-05-29 10:53:17 +0900531 }
Harald Welte136e7372016-05-29 10:53:17 +0900532}
533
Neels Hofmeyr5c5c78a2016-12-14 18:35:47 +0100534const struct value_string osmo_fsm_term_cause_names[] = {
Neels Hofmeyr18080962016-12-16 13:43:54 +0100535 OSMO_VALUE_STRING(OSMO_FSM_TERM_PARENT),
536 OSMO_VALUE_STRING(OSMO_FSM_TERM_REQUEST),
537 OSMO_VALUE_STRING(OSMO_FSM_TERM_REGULAR),
538 OSMO_VALUE_STRING(OSMO_FSM_TERM_ERROR),
539 OSMO_VALUE_STRING(OSMO_FSM_TERM_TIMEOUT),
Neels Hofmeyr5c5c78a2016-12-14 18:35:47 +0100540 { 0, NULL }
541};
542
Harald Welte136e7372016-05-29 10:53:17 +0900543/*! @} */