diff options
author | José Bollo <jose.bollo@iot.bzh> | 2017-05-30 16:28:55 +0200 |
---|---|---|
committer | José Bollo <jose.bollo@iot.bzh> | 2017-05-30 16:28:55 +0200 |
commit | bb5806063c450da26a38e1915f076723d00df260 (patch) | |
tree | 1503c2a4dc0558bc87f82bec6f6490d4a495a028 | |
parent | 52384789f904e7ebbfb901e15cc62521c9b9468f (diff) |
Adds hooks for service (svc)
Change-Id: I42265fcee9eae8219ef0cf56d3c8446d2c0fc015
Signed-off-by: José Bollo <jose.bollo@iot.bzh>
-rw-r--r-- | src/afb-api-so-v1.c | 2 | ||||
-rw-r--r-- | src/afb-api-so-v2.c | 2 | ||||
-rw-r--r-- | src/afb-config.c | 12 | ||||
-rw-r--r-- | src/afb-config.h | 1 | ||||
-rw-r--r-- | src/afb-hook.c | 235 | ||||
-rw-r--r-- | src/afb-hook.h | 44 | ||||
-rw-r--r-- | src/afb-svc.c | 66 | ||||
-rw-r--r-- | src/afb-svc.h | 31 | ||||
-rw-r--r-- | src/main.c | 2 |
9 files changed, 364 insertions, 31 deletions
diff --git a/src/afb-api-so-v1.c b/src/afb-api-so-v1.c index b7b29e44..9adb9973 100644 --- a/src/afb-api-so-v1.c +++ b/src/afb-api-so-v1.c @@ -122,6 +122,8 @@ static void update_hooks_cb(void *closure) { struct api_so_v1 *desc = closure; afb_ditf_update_hook(&desc->ditf); + if (desc->service) + afb_svc_update_hook(desc->service); } static int get_verbosity_cb(void *closure) diff --git a/src/afb-api-so-v2.c b/src/afb-api-so-v2.c index 11fa334b..2cf181bf 100644 --- a/src/afb-api-so-v2.c +++ b/src/afb-api-so-v2.c @@ -147,6 +147,8 @@ static void update_hooks_cb(void *closure) { struct api_so_v2 *desc = closure; afb_ditf_update_hook(&desc->ditf); + if (desc->service) + afb_svc_update_hook(desc->service); } static int get_verbosity_cb(void *closure) diff --git a/src/afb-config.c b/src/afb-config.c index 80dde93f..ebacee99 100644 --- a/src/afb-config.c +++ b/src/afb-config.c @@ -91,6 +91,7 @@ #define DISPLAY_HELP 'h' #define SET_QUIET 'q' #define SET_RNDTOKEN 'r' +#define SET_TRACESVC 'S' #define SET_TRACEREQ 'T' #define SET_AUTH_TOKEN 't' #define SET_UPLOAD_DIR 'u' @@ -152,6 +153,7 @@ static AFB_options cliOptions[] = { {SET_TRACEREQ, 1, "tracereq", "Log the requests: no, common, extra, all"}, {SET_TRACEDITF, 1, "traceditf", "Log the requests: no, common, extra, all"}, + {SET_TRACESVC, 1, "tracesvc", "Log the requests: no, all"}, {ADD_CALL, 1, "call", "call at start format of val: API/VERB:json-args"}, {SET_NO_HTTPD, 0, "no-httpd", "Forbids HTTP service"}, @@ -184,6 +186,12 @@ static struct enumdesc traceditf_desc[] = { { NULL, 0 } }; +static struct enumdesc tracesvc_desc[] = { + { "no", 0 }, + { "all", afb_hook_flags_svc_all }, + { NULL, 0 } +}; + static struct enumdesc mode_desc[] = { { "local", AFB_MODE_LOCAL }, { "remote", AFB_MODE_REMOTE }, @@ -506,6 +514,10 @@ static void parse_arguments(int argc, char **argv, struct afb_config *config) config->traceditf = argvalenum(optc, traceditf_desc); break; + case SET_TRACESVC: + config->tracesvc = argvalenum(optc, tracesvc_desc); + break; + case SET_NO_HTTPD: noarg(optc); config->noHttpd = 1; diff --git a/src/afb-config.h b/src/afb-config.h index a636a788..acf1f38f 100644 --- a/src/afb-config.h +++ b/src/afb-config.h @@ -56,6 +56,7 @@ struct afb_config { int mode; // mode of listening int tracereq; int traceditf; + int tracesvc; int noHttpd; }; diff --git a/src/afb-hook.c b/src/afb-hook.c index bc33c493..cade990c 100644 --- a/src/afb-hook.c +++ b/src/afb-hook.c @@ -35,6 +35,7 @@ #include "afb-cred.h" #include "afb-xreq.h" #include "afb-ditf.h" +#include "afb-svc.h" #include "verbose.h" /** @@ -63,6 +64,18 @@ struct afb_hook_ditf { void *closure; /**< closure for callbacks */ }; +/** + * Definition of a hook for svc + */ +struct afb_hook_svc { + struct afb_hook_svc *next; /**< next hook */ + unsigned refcount; /**< reference count */ + char *api; /**< api hooked or NULL for any */ + unsigned flags; /**< hook flags */ + struct afb_hook_svc_itf *itf; /**< interface of hook */ + void *closure; /**< closure for callbacks */ +}; + /* synchronisation across threads */ static pthread_rwlock_t rwlock = PTHREAD_RWLOCK_INITIALIZER; @@ -72,6 +85,9 @@ static struct afb_hook_xreq *list_of_xreq_hooks = NULL; /* list of hooks for ditf */ static struct afb_hook_ditf *list_of_ditf_hooks = NULL; +/* list of hooks for svc */ +static struct afb_hook_svc *list_of_svc_hooks = NULL; + /****************************************************************************** * section: default callbacks for tracing requests *****************************************************************************/ @@ -722,3 +738,222 @@ void afb_hook_unref_ditf(struct afb_hook_ditf *hook) } } +/****************************************************************************** + * section: default callbacks for tracing service interface (svc) + *****************************************************************************/ + +static void _hook_svc_(const struct afb_svc *svc, const char *format, ...) +{ + int len; + char *buffer; + va_list ap; + + va_start(ap, format); + len = vasprintf(&buffer, format, ap); + va_end(ap); + + if (len < 0) + NOTICE("hook svc-%s allocation error for %s", svc->api, format); + else { + NOTICE("hook svc-%s %s", svc->api, buffer); + free(buffer); + } +} + +static void hook_svc_start_before_default_cb(void *closure, const struct afb_svc *svc) +{ + _hook_svc_(svc, "start.before"); +} + +static void hook_svc_start_after_default_cb(void *closure, const struct afb_svc *svc, int status) +{ + _hook_svc_(svc, "start.after -> %d", status); +} + +static void hook_svc_on_event_before_default_cb(void *closure, const struct afb_svc *svc, const char *event, int eventid, struct json_object *object) +{ + _hook_svc_(svc, "on_event.before(%s, %d, %s)", event, eventid, json_object_to_json_string(object)); +} + +static void hook_svc_on_event_after_default_cb(void *closure, const struct afb_svc *svc, const char *event, int eventid, struct json_object *object) +{ + _hook_svc_(svc, "on_event.after(%s, %d, %s)", event, eventid, json_object_to_json_string(object)); +} + +static void hook_svc_call_default_cb(void *closure, const struct afb_svc *svc, const char *api, const char *verb, struct json_object *args) +{ + _hook_svc_(svc, "call(%s/%s, %s) ...", api, verb, json_object_to_json_string(args)); +} + +static void hook_svc_call_result_default_cb(void *closure, const struct afb_svc *svc, int status, struct json_object *result) +{ + _hook_svc_(svc, " ...call... -> %d: %s", status, json_object_to_json_string(result)); +} + +static void hook_svc_callsync_default_cb(void *closure, const struct afb_svc *svc, const char *api, const char *verb, struct json_object *args) +{ + _hook_svc_(svc, "callsync(%s/%s, %s) ...", api, verb, json_object_to_json_string(args)); +} + +static void hook_svc_callsync_result_default_cb(void *closure, const struct afb_svc *svc, int status, struct json_object *result) +{ + _hook_svc_(svc, " ...callsync... -> %d: %s", status, json_object_to_json_string(result)); +} + +static struct afb_hook_svc_itf hook_svc_default_itf = { + .hook_svc_start_before = hook_svc_start_before_default_cb, + .hook_svc_start_after = hook_svc_start_after_default_cb, + .hook_svc_on_event_before = hook_svc_on_event_before_default_cb, + .hook_svc_on_event_after = hook_svc_on_event_after_default_cb, + .hook_svc_call = hook_svc_call_default_cb, + .hook_svc_call_result = hook_svc_call_result_default_cb, + .hook_svc_callsync = hook_svc_callsync_default_cb, + .hook_svc_callsync_result = hook_svc_callsync_result_default_cb +}; + +/****************************************************************************** + * section: hooks for tracing service interface (svc) + *****************************************************************************/ + +#define _HOOK_SVC_(what,...) \ + struct afb_hook_svc *hook; \ + pthread_rwlock_rdlock(&rwlock); \ + hook = list_of_svc_hooks; \ + while (hook) { \ + if (hook->itf->hook_svc_##what \ + && (hook->flags & afb_hook_flag_svc_##what) != 0 \ + && (!hook->api || !strcasecmp(hook->api, svc->api))) { \ + hook->itf->hook_svc_##what(hook->closure, __VA_ARGS__); \ + } \ + hook = hook->next; \ + } \ + pthread_rwlock_unlock(&rwlock); + +void afb_hook_svc_start_before(const struct afb_svc *svc) +{ + _HOOK_SVC_(start_before, svc); +} + +int afb_hook_svc_start_after(const struct afb_svc *svc, int status) +{ + _HOOK_SVC_(start_after, svc, status); + return status; +} + +void afb_hook_svc_on_event_before(const struct afb_svc *svc, const char *event, int eventid, struct json_object *object) +{ + _HOOK_SVC_(on_event_before, svc, event, eventid, object); +} + +void afb_hook_svc_on_event_after(const struct afb_svc *svc, const char *event, int eventid, struct json_object *object) +{ + _HOOK_SVC_(on_event_after, svc, event, eventid, object); +} + +void afb_hook_svc_call(const struct afb_svc *svc, const char *api, const char *verb, struct json_object *args) +{ + _HOOK_SVC_(call, svc, api, verb, args); +} + +void afb_hook_svc_call_result(const struct afb_svc *svc, int status, struct json_object *result) +{ + _HOOK_SVC_(call_result, svc, status, result); +} + +void afb_hook_svc_callsync(const struct afb_svc *svc, const char *api, const char *verb, struct json_object *args) +{ + _HOOK_SVC_(callsync, svc, api, verb, args); +} + +int afb_hook_svc_callsync_result(const struct afb_svc *svc, int status, struct json_object *result) +{ + _HOOK_SVC_(callsync_result, svc, status, result); + return status; +} + +/****************************************************************************** + * section: hooking services (svc) + *****************************************************************************/ + +int afb_hook_flags_svc(const char *api) +{ + int flags; + struct afb_hook_svc *hook; + + pthread_rwlock_rdlock(&rwlock); + flags = 0; + hook = list_of_svc_hooks; + while (hook) { + if (!api || !hook->api || !strcasecmp(hook->api, api)) + flags |= hook->flags; + hook = hook->next; + } + pthread_rwlock_unlock(&rwlock); + return flags; +} + +struct afb_hook_svc *afb_hook_create_svc(const char *api, int flags, struct afb_hook_svc_itf *itf, void *closure) +{ + struct afb_hook_svc *hook; + + /* alloc the result */ + hook = calloc(1, sizeof *hook); + if (hook == NULL) + return NULL; + + /* get a copy of the names */ + hook->api = api ? strdup(api) : NULL; + if (api && !hook->api) { + free(hook); + return NULL; + } + + /* initialise the rest */ + hook->refcount = 1; + hook->flags = flags; + hook->itf = itf ? itf : &hook_svc_default_itf; + hook->closure = closure; + + /* record the hook */ + pthread_rwlock_wrlock(&rwlock); + hook->next = list_of_svc_hooks; + list_of_svc_hooks = hook; + pthread_rwlock_unlock(&rwlock); + + /* returns it */ + return hook; +} + +struct afb_hook_svc *afb_hook_addref_svc(struct afb_hook_svc *hook) +{ + pthread_rwlock_wrlock(&rwlock); + hook->refcount++; + pthread_rwlock_unlock(&rwlock); + return hook; +} + +void afb_hook_unref_svc(struct afb_hook_svc *hook) +{ + struct afb_hook_svc **prv; + + if (hook) { + pthread_rwlock_wrlock(&rwlock); + if (--hook->refcount) + hook = NULL; + else { + /* unlink */ + prv = &list_of_svc_hooks; + while (*prv && *prv != hook) + prv = &(*prv)->next; + if(*prv) + *prv = hook->next; + } + pthread_rwlock_unlock(&rwlock); + if (hook) { + /* free */ + free(hook->api); + free(hook); + } + } +} + diff --git a/src/afb-hook.h b/src/afb-hook.h index 2447556c..8fe30d5b 100644 --- a/src/afb-hook.h +++ b/src/afb-hook.h @@ -25,6 +25,7 @@ struct afb_event; struct afb_session; struct afb_xreq; struct afb_ditf; +struct afb_svc; struct sd_bus; struct sd_event; @@ -176,3 +177,46 @@ extern struct afb_hook_ditf *afb_hook_create_ditf(const char *api, int flags, st extern struct afb_hook_ditf *afb_hook_addref_ditf(struct afb_hook_ditf *hook); extern void afb_hook_unref_ditf(struct afb_hook_ditf *hook); +/********************************************************* +* section hooking svc (service interface) +*********************************************************/ + +#define afb_hook_flag_svc_start_before 0x000001 +#define afb_hook_flag_svc_start_after 0x000002 +#define afb_hook_flag_svc_on_event_before 0x000004 +#define afb_hook_flag_svc_on_event_after 0x000008 +#define afb_hook_flag_svc_call 0x000010 +#define afb_hook_flag_svc_call_result 0x000020 +#define afb_hook_flag_svc_callsync 0x000040 +#define afb_hook_flag_svc_callsync_result 0x000080 + +#define afb_hook_flags_svc_all (afb_hook_flag_svc_start_before|afb_hook_flag_svc_start_after\ + |afb_hook_flag_svc_on_event_before|afb_hook_flag_svc_on_event_after\ + |afb_hook_flag_svc_call|afb_hook_flag_svc_call_result\ + |afb_hook_flag_svc_callsync|afb_hook_flag_svc_callsync_result) + +struct afb_hook_svc_itf { + void (*hook_svc_start_before)(void *closure, const struct afb_svc *svc); + void (*hook_svc_start_after)(void *closure, const struct afb_svc *svc, int status); + void (*hook_svc_on_event_before)(void *closure, const struct afb_svc *svc, const char *event, int eventid, struct json_object *object); + void (*hook_svc_on_event_after)(void *closure, const struct afb_svc *svc, const char *event, int eventid, struct json_object *object); + void (*hook_svc_call)(void *closure, const struct afb_svc *svc, const char *api, const char *verb, struct json_object *args); + void (*hook_svc_call_result)(void *closure, const struct afb_svc *svc, int status, struct json_object *result); + void (*hook_svc_callsync)(void *closure, const struct afb_svc *svc, const char *api, const char *verb, struct json_object *args); + void (*hook_svc_callsync_result)(void *closure, const struct afb_svc *svc, int status, struct json_object *result); +}; + +extern void afb_hook_svc_start_before(const struct afb_svc *svc); +extern int afb_hook_svc_start_after(const struct afb_svc *svc, int status); +extern void afb_hook_svc_on_event_before(const struct afb_svc *svc, const char *event, int eventid, struct json_object *object); +extern void afb_hook_svc_on_event_after(const struct afb_svc *svc, const char *event, int eventid, struct json_object *object); +extern void afb_hook_svc_call(const struct afb_svc *svc, const char *api, const char *verb, struct json_object *args); +extern void afb_hook_svc_call_result(const struct afb_svc *svc, int status, struct json_object *result); +extern void afb_hook_svc_callsync(const struct afb_svc *svc, const char *api, const char *verb, struct json_object *args); +extern int afb_hook_svc_callsync_result(const struct afb_svc *svc, int status, struct json_object *result); + +extern int afb_hook_flags_svc(const char *api); +extern struct afb_hook_svc *afb_hook_create_svc(const char *api, int flags, struct afb_hook_svc_itf *itf, void *closure); +extern struct afb_hook_svc *afb_hook_addref_svc(struct afb_hook_svc *hook); +extern void afb_hook_unref_svc(struct afb_hook_svc *hook); + diff --git a/src/afb-svc.c b/src/afb-svc.c index fad13286..97312e6c 100644 --- a/src/afb-svc.c +++ b/src/afb-svc.c @@ -34,29 +34,12 @@ #include "afb-xreq.h" #include "afb-cred.h" #include "afb-apiset.h" +#include "afb-hook.h" #include "jobs.h" #include "verbose.h" -/* - * Structure for recording service - */ -struct afb_svc -{ - /* api/prefix */ - const char *api; - - /* session of the service */ - struct afb_session *session; - - /* the apiset for the service */ - struct afb_apiset *apiset; - /* event listener of the service or NULL */ - struct afb_evt_listener *listener; - - /* on event callback for the service */ - void (*on_event)(const char *event, struct json_object *object); -}; +#define HOOK(x,...) if((svc)->hookflags & afb_hook_flag_svc_##x) afb_hook_svc_##x(__VA_ARGS__) /* * Structure for requests initiated by the service @@ -165,6 +148,7 @@ static struct afb_svc *afb_svc_alloc( goto error; } + svc->hookflags = afb_hook_flags_svc(svc->api); return svc; error: @@ -201,7 +185,9 @@ struct afb_svc *afb_svc_create_v1( /* initialises the svc now */ if (start) { + HOOK(start_before, svc); rc = start(to_afb_service(svc)); + HOOK(start_after, svc, rc); if (rc < 0) goto error; } @@ -244,7 +230,9 @@ struct afb_svc *afb_svc_create_v2( /* starts the svc if needed */ if (start) { + HOOK(start_before, svc); rc = start(); + HOOK(start_after, svc, rc); if (rc < 0) goto error; } @@ -256,13 +244,21 @@ error: return NULL; } +void afb_svc_update_hook(struct afb_svc *svc) +{ + svc->hookflags = afb_hook_flags_svc(svc->api); +} + /* * Propagates the event to the service */ static void svc_on_event(void *closure, const char *event, int eventid, struct json_object *object) { struct afb_svc *svc = closure; + + HOOK(on_event_before, svc, event, eventid, object); svc->on_event(event, object); + HOOK(on_event_after, svc, event, eventid, object); json_object_put(object); } @@ -324,7 +320,9 @@ static void svcreq_reply(struct afb_xreq *xreq, int iserror, json_object *obj) { struct svc_req *svcreq = CONTAINER_OF_XREQ(struct svc_req, xreq); if (svcreq->callback) { + struct afb_svc *svc = svcreq->svc; svcreq->callback(svcreq->closure, iserror, obj); + HOOK(call_result, svc, iserror, obj); json_object_put(obj); } else { svcreq->iserror = iserror; @@ -356,6 +354,8 @@ static void svc_call(void *closure, const char *api, const char *verb, struct js struct svc_req *svcreq; struct json_object *ierr; + HOOK(call, svc, api, verb, args); + /* allocates the request */ svcreq = svcreq_create(svc, api, verb, args); if (svcreq == NULL) { @@ -363,6 +363,7 @@ static void svc_call(void *closure, const char *api, const char *verb, struct js json_object_put(args); ierr = afb_msg_json_internal_error(); callback(cbclosure, 1, ierr); + HOOK(call_result, svc, 1, ierr); json_object_put(ierr); return; } @@ -383,6 +384,8 @@ static int svc_call_sync(void *closure, const char *api, const char *verb, struc struct svc_req *svcreq; int rc; + HOOK(callsync, svc, api, verb, args); + /* allocates the request */ svcreq = svcreq_create(svc, api, verb, args); if (svcreq == NULL) { @@ -390,19 +393,20 @@ static int svc_call_sync(void *closure, const char *api, const char *verb, struc errno = ENOMEM; json_object_put(args); *result = afb_msg_json_internal_error(); - return -1; + rc = 0; + } else { + /* initialises the request */ + svcreq->jobloop = NULL; + svcreq->callback = NULL; + svcreq->result = NULL; + svcreq->iserror = 1; + afb_xreq_addref(&svcreq->xreq); + rc = jobs_enter(NULL, 0, svcreq_sync_enter, svcreq); + rc = rc >= 0 && !svcreq->iserror; + *result = (rc || svcreq->result) ? svcreq->result : afb_msg_json_internal_error(); + afb_xreq_unref(&svcreq->xreq); } - - /* initialises the request */ - svcreq->jobloop = NULL; - svcreq->callback = NULL; - svcreq->result = NULL; - svcreq->iserror = 1; - afb_xreq_addref(&svcreq->xreq); - rc = jobs_enter(NULL, 0, svcreq_sync_enter, svcreq); - rc = rc >= 0 && !svcreq->iserror; - *result = (rc || svcreq->result) ? svcreq->result : afb_msg_json_internal_error(); - afb_xreq_unref(&svcreq->xreq); + HOOK(callsync_result, svc, rc, *result); return rc; } diff --git a/src/afb-svc.h b/src/afb-svc.h index 84b03d83..fb82e3c4 100644 --- a/src/afb-svc.h +++ b/src/afb-svc.h @@ -19,9 +19,37 @@ struct afb_svc; struct afb_service; +struct afb_session; struct afb_apiset; +struct afb_evt_listener; struct afb_binding_data_v2; +struct json_object; + +/* + * Structure for recording service + */ +struct afb_svc +{ + /* api/prefix */ + const char *api; + + /* session of the service */ + struct afb_session *session; + + /* the apiset for the service */ + struct afb_apiset *apiset; + + /* event listener of the service or NULL */ + struct afb_evt_listener *listener; + + /* on event callback for the service */ + void (*on_event)(const char *event, struct json_object *object); + + /* hooking flags */ + int hookflags; +}; + extern struct afb_svc *afb_svc_create_v1( const char *api, struct afb_apiset *apiset, @@ -36,3 +64,6 @@ extern struct afb_svc *afb_svc_create_v2( int (*start)(), void (*on_event)(const char *event, struct json_object *object), struct afb_binding_data_v2 *data); + +extern void afb_svc_update_hook(struct afb_svc *svc); + @@ -572,6 +572,8 @@ static void start() afb_hook_create_xreq(NULL, NULL, NULL, config->tracereq, NULL, NULL); if (config->traceditf) afb_hook_create_ditf(NULL, config->traceditf, NULL, NULL); + if (config->tracesvc) + afb_hook_create_svc(NULL, config->tracesvc, NULL, NULL); /* load bindings */ apiset_start_list(config->dbus_clients, afb_api_dbus_add_client, "the afb-dbus client"); |