X-Git-Url: https://gerrit.automotivelinux.org/gerrit/gitweb?a=blobdiff_plain;f=src%2Fafb-export.c;h=b5a4b9e39796c3cbd219bcd722c59235f3972c57;hb=d7e4faba5d1744160d538edd74bc4bdd8a173b1a;hp=fb1888edb31d01db45ce9b1c5fb241d4b6aa4fac;hpb=dfe85ca516c207eadc4ae77066c6706f17068f20;p=src%2Fapp-framework-binder.git diff --git a/src/afb-export.c b/src/afb-export.c index fb1888ed..b5a4b9e3 100644 --- a/src/afb-export.c +++ b/src/afb-export.c @@ -1,5 +1,5 @@ /* - * Copyright (C) 2016, 2017, 2018 "IoT.bzh" + * Copyright (C) 2016-2019 "IoT.bzh" * Author: José Bollo * * Licensed under the Apache License, Version 2.0 (the "License"); @@ -39,7 +39,6 @@ #include "afb-api-so-v2.h" #include "afb-api-v3.h" #include "afb-common.h" -#include "afb-systemd.h" #include "afb-cred.h" #include "afb-evt.h" #include "afb-export.h" @@ -48,8 +47,11 @@ #include "afb-session.h" #include "afb-xreq.h" #include "afb-calls.h" + +#include "systemd.h" #include "jobs.h" #include "verbose.h" +#include "globset.h" #include "sig-monitor.h" #include "wrap-json.h" @@ -57,24 +59,6 @@ * internal types ************************************************************************/ -/* - * structure for handling events - */ -struct event_handler -{ - /* link to the next event handler of the list */ - struct event_handler *next; - - /* function to call on the case of the event */ - void (*callback)(void *, const char*, struct json_object*, struct afb_api_x3*); - - /* closure for the callback */ - void *closure; - - /* the handled pattern */ - char pattern[1]; -}; - /* * Actually supported versions */ @@ -138,7 +122,7 @@ struct afb_export struct afb_evt_listener *listener; /* event handler list */ - struct event_handler *event_handlers; + struct globset *event_handlers; /* creator if any */ struct afb_export *creator; @@ -467,21 +451,21 @@ static int hooked_event_broadcast_cb(struct afb_api_x3 *closure, const char *nam static struct sd_event *hooked_get_event_loop(struct afb_api_x3 *closure) { struct afb_export *export = from_api_x3(closure); - struct sd_event *r = afb_systemd_get_event_loop(); + struct sd_event *r = systemd_get_event_loop(); return afb_hook_api_get_event_loop(export, r); } static struct sd_bus *hooked_get_user_bus(struct afb_api_x3 *closure) { struct afb_export *export = from_api_x3(closure); - struct sd_bus *r = afb_systemd_get_user_bus(); + struct sd_bus *r = systemd_get_user_bus(); return afb_hook_api_get_user_bus(export, r); } static struct sd_bus *hooked_get_system_bus(struct afb_api_x3 *closure) { struct afb_export *export = from_api_x3(closure); - struct sd_bus *r = afb_systemd_get_system_bus(); + struct sd_bus *r = systemd_get_system_bus(); return afb_hook_api_get_system_bus(export, r); } @@ -553,9 +537,9 @@ static const struct afb_daemon_itf_x1 daemon_itf = { .vverbose_v2 = vverbose_cb, .event_make = legacy_event_x1_make_cb, .event_broadcast = event_broadcast_cb, - .get_event_loop = afb_systemd_get_event_loop, - .get_user_bus = afb_systemd_get_user_bus, - .get_system_bus = afb_systemd_get_system_bus, + .get_event_loop = systemd_get_event_loop, + .get_user_bus = systemd_get_user_bus, + .get_system_bus = systemd_get_system_bus, .rootdir_get_fd = afb_common_rootdir_get_fd, .rootdir_open_locale = rootdir_open_locale_cb, .queue_job = queue_job_cb, @@ -1068,9 +1052,9 @@ static const struct afb_api_x3_itf api_x3_itf = { .vverbose = (void*)vverbose_cb, - .get_event_loop = afb_systemd_get_event_loop, - .get_user_bus = afb_systemd_get_user_bus, - .get_system_bus = afb_systemd_get_system_bus, + .get_event_loop = systemd_get_event_loop, + .get_user_bus = systemd_get_user_bus, + .get_system_bus = systemd_get_system_bus, .rootdir_get_fd = afb_common_rootdir_get_fd, .rootdir_open_locale = rootdir_open_locale_cb, .queue_job = queue_job_cb, @@ -1161,7 +1145,8 @@ static const struct afb_api_x3_itf hooked_api_x3_itf = { */ static void listener_of_events(void *closure, const char *event, int eventid, struct json_object *object) { - struct event_handler *handler; + const struct globset_handler *handler; + void (*callback)(void *, const char*, struct json_object*, struct afb_api_x3*); struct afb_export *export = from_api_x3(closure); /* hook the event before */ @@ -1170,26 +1155,24 @@ static void listener_of_events(void *closure, const char *event, int eventid, st /* transmit to specific handlers */ /* search the handler */ - handler = export->event_handlers; - while (handler) { - if (fnmatch(handler->pattern, event, 0)) { - if (!(export->hooksvc & afb_hook_flag_api_on_event_handler)) - handler->callback(handler->closure, event, object, to_api_x3(export)); - else { - afb_hook_api_on_event_handler_before(export, event, eventid, object, handler->pattern); - handler->callback(handler->closure, event, object, to_api_x3(export)); - afb_hook_api_on_event_handler_after(export, event, eventid, object, handler->pattern); - } + handler = export->event_handlers ? globset_match(export->event_handlers, event) : NULL; + if (handler) { + callback = handler->callback; + if (!(export->hooksvc & afb_hook_flag_api_on_event_handler)) + callback(handler->closure, event, object, to_api_x3(export)); + else { + afb_hook_api_on_event_handler_before(export, event, eventid, object, handler->pattern); + callback(handler->closure, event, object, to_api_x3(export)); + afb_hook_api_on_event_handler_after(export, event, eventid, object, handler->pattern); } - handler = handler->next; + } else { + /* transmit to default handler */ + if (export->on_any_event_v3) + export->on_any_event_v3(to_api_x3(export), event, object); + else if (export->on_any_event_v12) + export->on_any_event_v12(event, object); } - /* transmit to default handler */ - if (export->on_any_event_v3) - export->on_any_event_v3(to_api_x3(export), event, object); - else if (export->on_any_event_v12) - export->on_any_event_v12(event, object); - /* hook the event after */ if (export->hooksvc & afb_hook_flag_api_on_event) afb_hook_api_on_event_after(export, event, eventid, object); @@ -1220,40 +1203,32 @@ int afb_export_event_handler_add( void *closure) { int rc; - struct event_handler *handler, **previous; + /* ensure the listener */ rc = ensure_listener(export); if (rc < 0) return rc; - /* search the handler */ - previous = &export->event_handlers; - while ((handler = *previous) && strcasecmp(handler->pattern, pattern)) - previous = &handler->next; - - /* error if found */ - if (handler) { - ERROR("[API %s] event handler %s already exists", export->api.apiname, pattern); - errno = EEXIST; - return -1; + /* ensure the globset for event handling */ + if (!export->event_handlers) { + export->event_handlers = globset_create(); + if (!export->event_handlers) + goto oom_error; } - /* create the event */ - handler = malloc(strlen(pattern) + sizeof * handler); - if (!handler) { - ERROR("[API %s] can't allocate event handler %s", export->api.apiname, pattern); - errno = ENOMEM; + /* add the handler */ + rc = globset_add(export->event_handlers, pattern, callback, closure); + if (rc == 0) + return 0; + + if (errno == EEXIST) { + ERROR("[API %s] event handler %s already exists", export->api.apiname, pattern); return -1; } - /* init and record */ - handler->next = NULL; - handler->callback = callback; - handler->closure = closure; - strcpy(handler->pattern, pattern); - *previous = handler; - - return 0; +oom_error: + ERROR("[API %s] can't allocate event handler %s", export->api.apiname, pattern); + return -1; } int afb_export_event_handler_del( @@ -1261,27 +1236,13 @@ int afb_export_event_handler_del( const char *pattern, void **closure) { - struct event_handler *handler, **previous; - - /* search the handler */ - previous = &export->event_handlers; - while ((handler = *previous) && strcasecmp(handler->pattern, pattern)) - previous = &handler->next; - - /* error if found */ - if (!handler) { - ERROR("[API %s] event handler %s already exists", export->api.apiname, pattern); - errno = ENOENT; - return -1; - } - - /* remove the found event */ - if (closure) - *closure = handler->closure; + if (export->event_handlers + && !globset_del(export->event_handlers, pattern, closure)) + return 0; - *previous = handler->next; - free(handler); - return 0; + ERROR("[API %s] event handler %s not found", export->api.apiname, pattern); + errno = ENOENT; + return -1; } /****************************************************************************** @@ -1346,13 +1307,9 @@ void afb_export_unref(struct afb_export *export) void afb_export_destroy(struct afb_export *export) { - struct event_handler *handler; - if (export) { - while ((handler = export->event_handlers)) { - export->event_handlers = handler->next; - free(handler); - } + if (export->event_handlers) + globset_destroy(export->event_handlers); if (export->listener != NULL) afb_evt_listener_unref(export->listener); afb_session_unref(export->session);