From 8808bb49a20e3e569b230b1b8acdf6f74e1c6bd2 Mon Sep 17 00:00:00 2001 From: Harald Welte Date: Sat, 7 Jan 2017 11:11:03 +0100 Subject: [PATCH] Add osmo_fsm_find_by_name() and avoid registering FSM with same name This addresses a FIXME in the fsm.c code: osmo_fsm_register() should fail in case a FSM with the given name already exists. Change-Id: I5fd882939859c79581eba70c14cbafd64560b583 --- include/osmocom/core/fsm.h | 1 + src/fsm.c | 14 +++++++++++++- 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/include/osmocom/core/fsm.h b/include/osmocom/core/fsm.h index a2c20b788..952f82fa8 100644 --- a/include/osmocom/core/fsm.h +++ b/include/osmocom/core/fsm.h @@ -140,6 +140,7 @@ void osmo_fsm_log_addr(bool log_addr); int osmo_fsm_register(struct osmo_fsm *fsm); void osmo_fsm_unregister(struct osmo_fsm *fsm); +struct osmo_fsm *osmo_fsm_find_by_name(const char *name); struct osmo_fsm_inst *osmo_fsm_inst_alloc(struct osmo_fsm *fsm, void *ctx, void *priv, int log_level, const char *id); struct osmo_fsm_inst *osmo_fsm_inst_alloc_child(struct osmo_fsm *fsm, diff --git a/src/fsm.c b/src/fsm.c index 19705b9f6..666dbe374 100644 --- a/src/fsm.c +++ b/src/fsm.c @@ -20,6 +20,7 @@ #include #include +#include #include #include @@ -102,6 +103,16 @@ void osmo_fsm_log_addr(bool log_addr) fsm_log_addr = log_addr; } +struct osmo_fsm *osmo_fsm_find_by_name(const char *name) +{ + struct osmo_fsm *fsm; + llist_for_each_entry(fsm, &g_fsms, list) { + if (!strcmp(name, fsm->name)) + return fsm; + } + return NULL; +} + /*! \brief register a FSM with the core * * A FSM descriptor needs to be registered with the core before any @@ -112,7 +123,8 @@ void osmo_fsm_log_addr(bool log_addr) */ int osmo_fsm_register(struct osmo_fsm *fsm) { - /* FIXME:check for duplicate name? */ + if (osmo_fsm_find_by_name(fsm->name)) + return -EEXIST; llist_add_tail(&fsm->list, &g_fsms); INIT_LLIST_HEAD(&fsm->instances);