From 782adcfd29c1d19a903b85e1519acf8902e00d2b Mon Sep 17 00:00:00 2001 From: Uli Schlachter Date: Wed, 24 Jan 2018 09:05:00 +0100 Subject: [PATCH 1/2] signal.h: Introduce signal_array_getbyname() This is a nice utility function that actually does what most callers want, instead of signal_array_getbyid() which requires callers to come up with the signal id. Signed-off-by: Uli Schlachter --- common/signal.h | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/common/signal.h b/common/signal.h index 46bad94d0..390e5a8da 100644 --- a/common/signal.h +++ b/common/signal.h @@ -54,6 +54,13 @@ signal_array_getbyid(signal_array_t *arr, unsigned long id) return signal_array_lookup(arr, &sig); } +static inline signal_t * +signal_array_getbyname(signal_array_t *arr, const char *name) +{ + signal_t sig = { .id = a_strhash((const unsigned char *) NONULL(name)) }; + return signal_array_lookup(arr, &sig); +} + /** Connect a signal inside a signal array. * You are in charge of reference counting. * \param arr The signal array. From 3f93bf4cd5fae46785378c4591aa6759f4b8b74a Mon Sep 17 00:00:00 2001 From: Uli Schlachter Date: Wed, 24 Jan 2018 09:13:35 +0100 Subject: [PATCH 2/2] Use signal_array_getbyname() where appropriate Signed-off-by: Uli Schlachter --- common/luaobject.c | 6 ++---- common/signal.h | 3 +-- dbus.c | 9 +++------ spawn.c | 6 ++---- 4 files changed, 8 insertions(+), 16 deletions(-) diff --git a/common/luaobject.c b/common/luaobject.c index acd4bdee4..562b5e98e 100644 --- a/common/luaobject.c +++ b/common/luaobject.c @@ -230,8 +230,7 @@ luaA_object_disconnect_signal_from_stack(lua_State *L, int oud, void signal_object_emit(lua_State *L, signal_array_t *arr, const char *name, int nargs) { - signal_t *sigfound = signal_array_getbyid(arr, - a_strhash((const unsigned char *) name)); + signal_t *sigfound = signal_array_getbyname(arr, name); if(sigfound) { @@ -279,8 +278,7 @@ luaA_object_emit_signal(lua_State *L, int oud, luaA_warn(L, "Trying to emit signal '%s' on invalid object", name); return; } - signal_t *sigfound = signal_array_getbyid(&obj->signals, - a_strhash((const unsigned char *) name)); + signal_t *sigfound = signal_array_getbyname(&obj->signals, name); if(sigfound) { int nbfunc = sigfound->sigfuncs.len; diff --git a/common/signal.h b/common/signal.h index 390e5a8da..ff6153eeb 100644 --- a/common/signal.h +++ b/common/signal.h @@ -91,8 +91,7 @@ signal_connect(signal_array_t *arr, const char *name, const void *ref) static inline bool signal_disconnect(signal_array_t *arr, const char *name, const void *ref) { - signal_t *sigfound = signal_array_getbyid(arr, - a_strhash((const unsigned char *) name)); + signal_t *sigfound = signal_array_getbyname(arr, name); if(sigfound) { foreach(func, sigfound->sigfuncs) diff --git a/dbus.c b/dbus.c index 354852ad1..48a6ad850 100644 --- a/dbus.c +++ b/dbus.c @@ -411,16 +411,14 @@ a_dbus_process_request(DBusConnection *dbus_connection, DBusMessage *msg) if(dbus_message_get_no_reply(msg)) { - signal_t *sigfound = signal_array_getbyid(&dbus_signals, - a_strhash((const unsigned char *) NONULL(interface))); + signal_t *sigfound = signal_array_getbyname(&dbus_signals, interface); /* emit signals */ if(sigfound) signal_object_emit(L, &dbus_signals, NONULL(interface), nargs); } else { - signal_t *sig = signal_array_getbyid(&dbus_signals, - a_strhash((const unsigned char *) NONULL(interface))); + signal_t *sig = signal_array_getbyname(&dbus_signals, interface); if(sig) { /* there can be only ONE handler to send reply */ @@ -763,8 +761,7 @@ luaA_dbus_connect_signal(lua_State *L) { const char *name = luaL_checkstring(L, 1); luaA_checkfunction(L, 2); - signal_t *sig = signal_array_getbyid(&dbus_signals, - a_strhash((const unsigned char *) name)); + signal_t *sig = signal_array_getbyname(&dbus_signals, name); if(sig) { luaA_warn(L, "cannot add signal %s on D-Bus, already existing", name); lua_pushnil(L); diff --git a/spawn.c b/spawn.c index da2afdccc..0bb21fa98 100644 --- a/spawn.c +++ b/spawn.c @@ -107,8 +107,7 @@ spawn_monitor_timeout(gpointer sequence) { if(spawn_sequence_remove(sequence)) { - signal_t *sig = signal_array_getbyid(&global_signals, - a_strhash((const unsigned char *) "spawn::timeout")); + signal_t *sig = signal_array_getbyname(&global_signals, "spawn::timeout"); if(sig) { /* send a timeout signal */ @@ -215,8 +214,7 @@ spawn_monitor_event(SnMonitorEvent *event, void *data) } /* send the signal */ - signal_t *sig = signal_array_getbyid(&global_signals, - a_strhash((const unsigned char *) event_type_str)); + signal_t *sig = signal_array_getbyname(&global_signals, event_type_str); if(sig) {