2017-06-20 02:35:06 +00:00
|
|
|
/*! \file signal.c
|
|
|
|
* Generic signalling/notification infrastructure. */
|
|
|
|
/*
|
|
|
|
* (C) 2009 by Holger Hans Peter Freyther <zecke@selfish.org>
|
2010-02-20 19:34:29 +00:00
|
|
|
* All Rights Reserved
|
|
|
|
*
|
2017-11-12 16:00:26 +00:00
|
|
|
* SPDX-License-Identifier: GPL-2.0+
|
|
|
|
*
|
2010-02-20 19:34:29 +00:00
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2011-03-22 15:36:13 +00:00
|
|
|
#include <osmocom/core/signal.h>
|
|
|
|
#include <osmocom/core/talloc.h>
|
|
|
|
#include <osmocom/core/linuxlist.h>
|
2010-02-20 19:34:29 +00:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <errno.h>
|
|
|
|
|
2011-08-17 14:19:46 +00:00
|
|
|
/*! \addtogroup signal
|
|
|
|
* @{
|
2017-06-20 02:35:06 +00:00
|
|
|
* Generic signalling/notification infrastructure.
|
|
|
|
*
|
|
|
|
* \file signal.c */
|
2011-08-17 14:19:46 +00:00
|
|
|
|
|
|
|
|
2010-02-20 19:34:29 +00:00
|
|
|
void *tall_sigh_ctx;
|
|
|
|
static LLIST_HEAD(signal_handler_list);
|
|
|
|
|
|
|
|
struct signal_handler {
|
|
|
|
struct llist_head entry;
|
|
|
|
unsigned int subsys;
|
2011-05-07 10:42:45 +00:00
|
|
|
osmo_signal_cbfn *cbfn;
|
2010-02-20 19:34:29 +00:00
|
|
|
void *data;
|
|
|
|
};
|
|
|
|
|
2018-08-16 17:05:19 +00:00
|
|
|
/*! Initialize a signal_handler talloc context for \ref osmo_signal_register_handler.
|
|
|
|
* Create a talloc context called "osmo_signal".
|
|
|
|
* \param[in] root_ctx talloc context used as parent for the new "osmo_signal" ctx.
|
|
|
|
* \returns the new osmo_signal talloc context, e.g. for reporting
|
|
|
|
*/
|
|
|
|
void *osmo_signal_talloc_ctx_init(void *root_ctx) {
|
|
|
|
tall_sigh_ctx = talloc_named_const(tall_sigh_ctx, 0, "osmo_signal");
|
|
|
|
return tall_sigh_ctx;
|
|
|
|
}
|
2010-02-20 19:34:29 +00:00
|
|
|
|
2017-06-19 22:17:59 +00:00
|
|
|
/*! Register a new signal handler
|
2011-08-17 14:19:46 +00:00
|
|
|
* \param[in] subsys Subsystem number
|
|
|
|
* \param[in] cbfn Callback function
|
|
|
|
* \param[in] data Data passed through to callback
|
2016-04-25 10:11:20 +00:00
|
|
|
* \returns 0 on success; negative in case of error
|
2011-08-17 14:19:46 +00:00
|
|
|
*/
|
2011-05-07 10:42:45 +00:00
|
|
|
int osmo_signal_register_handler(unsigned int subsys,
|
|
|
|
osmo_signal_cbfn *cbfn, void *data)
|
2010-02-20 19:34:29 +00:00
|
|
|
{
|
|
|
|
struct signal_handler *sig_data;
|
|
|
|
|
|
|
|
sig_data = talloc(tall_sigh_ctx, struct signal_handler);
|
|
|
|
if (!sig_data)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
|
|
|
memset(sig_data, 0, sizeof(*sig_data));
|
|
|
|
|
|
|
|
sig_data->subsys = subsys;
|
|
|
|
sig_data->data = data;
|
|
|
|
sig_data->cbfn = cbfn;
|
|
|
|
|
|
|
|
/* FIXME: check if we already have a handler for this subsys/cbfn/data */
|
|
|
|
|
|
|
|
llist_add_tail(&sig_data->entry, &signal_handler_list);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-06-19 22:17:59 +00:00
|
|
|
/*! Unregister signal handler
|
2011-08-17 14:19:46 +00:00
|
|
|
* \param[in] subsys Subsystem number
|
|
|
|
* \param[in] cbfn Callback function
|
|
|
|
* \param[in] data Data passed through to callback
|
|
|
|
*/
|
2011-05-07 10:42:45 +00:00
|
|
|
void osmo_signal_unregister_handler(unsigned int subsys,
|
|
|
|
osmo_signal_cbfn *cbfn, void *data)
|
2010-02-20 19:34:29 +00:00
|
|
|
{
|
|
|
|
struct signal_handler *handler;
|
|
|
|
|
|
|
|
llist_for_each_entry(handler, &signal_handler_list, entry) {
|
|
|
|
if (handler->cbfn == cbfn && handler->data == data
|
|
|
|
&& subsys == handler->subsys) {
|
|
|
|
llist_del(&handler->entry);
|
|
|
|
talloc_free(handler);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-19 22:17:59 +00:00
|
|
|
/*! dispatch (deliver) a new signal to all registered handlers
|
2011-08-17 14:19:46 +00:00
|
|
|
* \param[in] subsys Subsystem number
|
|
|
|
* \param[in] signal Signal number,
|
|
|
|
* \param[in] signal_data Data to be passed along to handlers
|
|
|
|
*/
|
2011-05-07 10:42:45 +00:00
|
|
|
void osmo_signal_dispatch(unsigned int subsys, unsigned int signal,
|
|
|
|
void *signal_data)
|
2010-02-20 19:34:29 +00:00
|
|
|
{
|
|
|
|
struct signal_handler *handler;
|
|
|
|
|
|
|
|
llist_for_each_entry(handler, &signal_handler_list, entry) {
|
|
|
|
if (handler->subsys != subsys)
|
|
|
|
continue;
|
|
|
|
(*handler->cbfn)(subsys, signal, handler->data, signal_data);
|
|
|
|
}
|
|
|
|
}
|
2011-08-17 14:19:46 +00:00
|
|
|
|
2012-04-18 19:53:23 +00:00
|
|
|
/*! @} */
|