X-Git-Url: https://gerrit.automotivelinux.org/gerrit/gitweb?a=blobdiff_plain;f=src%2Fafb-hook.c;h=d2ea0b6667757f5546d7362af2473b4893ee19a8;hb=65353dce81a629e042800bb7b86fcd869a76727e;hp=75e8a691fa31cf922c76e6f0d0a6bd0a7e7714e8;hpb=4521c1e7ae5371ab9d639adc617d17fb4e8ded0c;p=src%2Fapp-framework-binder.git diff --git a/src/afb-hook.c b/src/afb-hook.c index 75e8a691..d2ea0b66 100644 --- a/src/afb-hook.c +++ b/src/afb-hook.c @@ -1,5 +1,5 @@ /* - * Copyright (C) 2016, 2017, 2018 "IoT.bzh" + * Copyright (C) 2015-2020 "IoT.bzh" * Author José Bollo * * Licensed under the Apache License, Version 2.0 (the "License"); @@ -15,6 +15,8 @@ * limitations under the License. */ +#if WITH_AFB_HOOK /***********************************************************/ + #define _GNU_SOURCE #include @@ -26,6 +28,9 @@ #include #include +#if !defined(JSON_C_TO_STRING_NOSLASHESCAPE) +#define JSON_C_TO_STRING_NOSLASHESCAPE 0 +#endif #include #include @@ -45,7 +50,7 @@ #define MATCH(pattern,string) (\ pattern \ ? !fnmatch((pattern),(string),FNM_CASEFOLD|FNM_EXTMATCH|FNM_PERIOD) \ - : (string)[0] != '.') + : afb_api_is_public(string)) #define MATCH_API(pattern,string) MATCH(pattern,string) #define MATCH_VERB(pattern,string) MATCH(pattern,string) @@ -113,8 +118,9 @@ struct afb_hook_global { void *closure; /**< closure for callbacks */ }; -/* synchronisation across threads */ +/* synchronization across threads */ static pthread_rwlock_t rwlock = PTHREAD_RWLOCK_INITIALIZER; +static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; /* list of hooks for xreq */ static struct afb_hook_xreq *list_of_xreq_hooks = NULL; @@ -168,24 +174,6 @@ static char *_pbuf_(const char *fmt, va_list args, char **palloc, char *sbuf, si return sbuf; } -#if 0 /* old behaviour: use NOTICE */ -static void _hook_(const char *fmt1, const char *fmt2, va_list arg2, ...) -{ - char *tag, *data, *mem1, *mem2, buf1[256], buf2[2000]; - va_list arg1; - - data = _pbuf_(fmt2, arg2, &mem2, buf2, sizeof buf2, NULL); - - va_start(arg1, arg2); - tag = _pbuf_(fmt1, arg1, &mem1, buf1, sizeof buf1, NULL); - va_end(arg1); - - NOTICE("[HOOK %s] %s", tag, data); - - free(mem1); - free(mem2); -} -#else /* new behaviour: emits directly to stderr */ static void _hook_(const char *fmt1, const char *fmt2, va_list arg2, ...) { static const char chars[] = "HOOK: [] \n"; @@ -193,27 +181,31 @@ static void _hook_(const char *fmt1, const char *fmt2, va_list arg2, ...) struct iovec iov[5]; va_list arg1; + /* "HOOK: [" */ iov[0].iov_base = (void*)&chars[0]; iov[0].iov_len = 7; + /* fmt1 ... */ va_start(arg1, arg2); iov[1].iov_base = _pbuf_(fmt1, arg1, &mem1, buf1, sizeof buf1, &iov[1].iov_len); va_end(arg1); + /* "] " */ iov[2].iov_base = (void*)&chars[7]; iov[2].iov_len = 2; + /* fmt2 arg2 */ iov[3].iov_base = _pbuf_(fmt2, arg2, &mem2, buf2, sizeof buf2, &iov[3].iov_len); + /* "\n" */ iov[4].iov_base = (void*)&chars[9]; iov[4].iov_len = 1; - writev(2, iov, 5); + (void)writev(2, iov, 5); free(mem1); free(mem2); } -#endif static void _hook_xreq_(const struct afb_xreq *xreq, const char *format, ...) { @@ -225,16 +217,18 @@ static void _hook_xreq_(const struct afb_xreq *xreq, const char *format, ...) static void hook_xreq_begin_cb(void *closure, const struct afb_hookid *hookid, const struct afb_xreq *xreq) { - if (!xreq->cred) + struct afb_cred *cred = xreq->context.credentials; + + if (!cred) _hook_xreq_(xreq, "BEGIN"); else _hook_xreq_(xreq, "BEGIN uid=%d=%s gid=%d pid=%d label=%s id=%s", - (int)xreq->cred->uid, - xreq->cred->user, - (int)xreq->cred->gid, - (int)xreq->cred->pid, - xreq->cred->label?:"(null)", - xreq->cred->id?:"(null)" + (int)cred->uid, + cred->user, + (int)cred->gid, + (int)cred->pid, + cred->label?:"(null)", + cred->id?:"(null)" ); } @@ -245,7 +239,7 @@ static void hook_xreq_end_cb(void *closure, const struct afb_hookid *hookid, con static void hook_xreq_json_cb(void *closure, const struct afb_hookid *hookid, const struct afb_xreq *xreq, struct json_object *obj) { - _hook_xreq_(xreq, "json() -> %s", json_object_to_json_string(obj)); + _hook_xreq_(xreq, "json() -> %s", json_object_to_json_string_ext(obj, JSON_C_TO_STRING_NOSLASHESCAPE)); } static void hook_xreq_get_cb(void *closure, const struct afb_hookid *hookid, const struct afb_xreq *xreq, const char *name, struct afb_arg arg) @@ -255,7 +249,7 @@ static void hook_xreq_get_cb(void *closure, const struct afb_hookid *hookid, con static void hook_xreq_reply_cb(void *closure, const struct afb_hookid *hookid, const struct afb_xreq *xreq, struct json_object *obj, const char *error, const char *info) { - _hook_xreq_(xreq, "reply[%s](%s, %s)", error?:"success", json_object_to_json_string(obj), info); + _hook_xreq_(xreq, "reply[%s](%s, %s)", error?:"success", json_object_to_json_string_ext(obj, JSON_C_TO_STRING_NOSLASHESCAPE), info); } static void hook_xreq_legacy_context_get_cb(void *closure, const struct afb_hookid *hookid, const struct afb_xreq *xreq, void *value) @@ -300,22 +294,22 @@ static void hook_xreq_unsubscribe_cb(void *closure, const struct afb_hookid *hoo static void hook_xreq_subcall_cb(void *closure, const struct afb_hookid *hookid, const struct afb_xreq *xreq, const char *api, const char *verb, struct json_object *args) { - _hook_xreq_(xreq, "subcall(%s/%s, %s) ...", api, verb, json_object_to_json_string(args)); + _hook_xreq_(xreq, "subcall(%s/%s, %s) ...", api, verb, json_object_to_json_string_ext(args, JSON_C_TO_STRING_NOSLASHESCAPE)); } static void hook_xreq_subcall_result_cb(void *closure, const struct afb_hookid *hookid, const struct afb_xreq *xreq, struct json_object *object, const char *error, const char *info) { - _hook_xreq_(xreq, " ...subcall... [%s] -> %s (%s)", error?:"success", json_object_to_json_string(object), info?:""); + _hook_xreq_(xreq, " ...subcall... [%s] -> %s (%s)", error?:"success", json_object_to_json_string_ext(object, JSON_C_TO_STRING_NOSLASHESCAPE), info?:""); } static void hook_xreq_subcallsync_cb(void *closure, const struct afb_hookid *hookid, const struct afb_xreq *xreq, const char *api, const char *verb, struct json_object *args) { - _hook_xreq_(xreq, "subcallsync(%s/%s, %s) ...", api, verb, json_object_to_json_string(args)); + _hook_xreq_(xreq, "subcallsync(%s/%s, %s) ...", api, verb, json_object_to_json_string_ext(args, JSON_C_TO_STRING_NOSLASHESCAPE)); } static void hook_xreq_subcallsync_result_cb(void *closure, const struct afb_hookid *hookid, const struct afb_xreq *xreq, int status, struct json_object *object, const char *error, const char *info) { - _hook_xreq_(xreq, " ...subcallsync... %d [%s] -> %s (%s)", status, error?:"success", json_object_to_json_string(object), info?:""); + _hook_xreq_(xreq, " ...subcallsync... %d [%s] -> %s (%s)", status, error?:"success", json_object_to_json_string_ext(object, JSON_C_TO_STRING_NOSLASHESCAPE), info?:""); } static void hook_xreq_vverbose_cb(void *closure, const struct afb_hookid *hookid, const struct afb_xreq *xreq, int level, const char *file, int line, const char *func, const char *fmt, va_list args) @@ -368,7 +362,7 @@ static void hook_xreq_get_uid_cb(void *closure, const struct afb_hookid *hookid, static void hook_xreq_get_client_info_cb(void *closure, const struct afb_hookid *hookid, const struct afb_xreq *xreq, struct json_object *result) { - _hook_xreq_(xreq, "get_client_info() -> %s", json_object_to_json_string(result)); + _hook_xreq_(xreq, "get_client_info() -> %s", json_object_to_json_string_ext(result, JSON_C_TO_STRING_NOSLASHESCAPE)); } static struct afb_hook_xreq_itf hook_xreq_default_itf = { @@ -410,7 +404,8 @@ static struct afb_hook_xreq_itf hook_xreq_default_itf = { init_hookid(&hookid); \ hook = list_of_xreq_hooks; \ while (hook) { \ - if (hook->itf->hook_xreq_##func \ + if (hook->refcount \ + && hook->itf->hook_xreq_##func \ && (hook->flags & afb_hook_flag_req_##flag) != 0 \ && (!hook->session || hook->session == xreq->context.session) \ && MATCH_API(hook->api, xreq->request.called_api) \ @@ -566,10 +561,10 @@ struct json_object *afb_hook_xreq_get_client_info(const struct afb_xreq *xreq, s void afb_hook_init_xreq(struct afb_xreq *xreq) { - static int reqindex; + static int reqindex = 0; int f, flags; - int add; + int add, x; struct afb_hook_xreq *hook; /* scan hook list to get the expected flags */ @@ -591,11 +586,10 @@ void afb_hook_init_xreq(struct afb_xreq *xreq) /* store the hooking data */ xreq->hookflags = flags; if (flags) { - pthread_rwlock_wrlock(&rwlock); - if (++reqindex < 0) - reqindex = 1; - xreq->hookindex = reqindex; - pthread_rwlock_unlock(&rwlock); + do { + x = __atomic_load_n(&reqindex, __ATOMIC_RELAXED); + xreq->hookindex = (x + 1) % 1000000 ?: 1; + } while (x != __atomic_exchange_n(&reqindex, xreq->hookindex, __ATOMIC_RELAXED)); } } @@ -628,10 +622,10 @@ struct afb_hook_xreq *afb_hook_create_xreq(const char *api, const char *verb, st hook->closure = closure; /* record the hook */ - pthread_rwlock_wrlock(&rwlock); + pthread_mutex_lock(&mutex); hook->next = list_of_xreq_hooks; list_of_xreq_hooks = hook; - pthread_rwlock_unlock(&rwlock); + pthread_mutex_unlock(&mutex); /* returns it */ return hook; @@ -639,31 +633,34 @@ struct afb_hook_xreq *afb_hook_create_xreq(const char *api, const char *verb, st struct afb_hook_xreq *afb_hook_addref_xreq(struct afb_hook_xreq *hook) { - pthread_rwlock_wrlock(&rwlock); - hook->refcount++; - pthread_rwlock_unlock(&rwlock); + __atomic_add_fetch(&hook->refcount, 1, __ATOMIC_RELAXED); return hook; } -void afb_hook_unref_xreq(struct afb_hook_xreq *hook) +static void hook_clean_xreq() { - struct afb_hook_xreq **prv; + struct afb_hook_xreq **prv, *hook, *head; - if (hook) { - pthread_rwlock_wrlock(&rwlock); - if (--hook->refcount) - hook = NULL; - else { - /* unlink */ - prv = &list_of_xreq_hooks; - while (*prv && *prv != hook) + if (pthread_rwlock_trywrlock(&rwlock) == 0) { + /* unlink under mutex */ + head = NULL; + pthread_mutex_lock(&mutex); + prv = &list_of_xreq_hooks; + while ((hook = *prv)) { + if (hook->refcount) prv = &(*prv)->next; - if(*prv) + else { *prv = hook->next; + hook->next = head; + head = hook; + } } + pthread_mutex_unlock(&mutex); pthread_rwlock_unlock(&rwlock); - if (hook) { - /* free */ + + /* free found hooks */ + while((hook = head)) { + head = hook->next; free(hook->api); free(hook->verb); if (hook->session) @@ -673,6 +670,12 @@ void afb_hook_unref_xreq(struct afb_hook_xreq *hook) } } +void afb_hook_unref_xreq(struct afb_hook_xreq *hook) +{ + if (hook && !__atomic_sub_fetch(&hook->refcount, 1, __ATOMIC_RELAXED)) + hook_clean_xreq(); +} + /****************************************************************************** * section: default callbacks for tracing daemon interface *****************************************************************************/ @@ -687,12 +690,12 @@ static void _hook_api_(const struct afb_export *export, const char *format, ...) static void hook_api_event_broadcast_before_cb(void *closure, const struct afb_hookid *hookid, const struct afb_export *export, const char *name, struct json_object *object) { - _hook_api_(export, "event_broadcast.before(%s, %s)....", name, json_object_to_json_string(object)); + _hook_api_(export, "event_broadcast.before(%s, %s)....", name, json_object_to_json_string_ext(object, JSON_C_TO_STRING_NOSLASHESCAPE)); } static void hook_api_event_broadcast_after_cb(void *closure, const struct afb_hookid *hookid, const struct afb_export *export, const char *name, struct json_object *object, int result) { - _hook_api_(export, "event_broadcast.after(%s, %s) -> %d", name, json_object_to_json_string(object), result); + _hook_api_(export, "event_broadcast.after(%s, %s) -> %d", name, json_object_to_json_string_ext(object, JSON_C_TO_STRING_NOSLASHESCAPE), result); } static void hook_api_get_event_loop_cb(void *closure, const struct afb_hookid *hookid, const struct afb_export *export, struct sd_event *result) @@ -735,26 +738,32 @@ static void hook_api_event_make_cb(void *closure, const struct afb_hookid *hooki static void hook_api_rootdir_get_fd_cb(void *closure, const struct afb_hookid *hookid, const struct afb_export *export, int result) { - char path[PATH_MAX]; + char path[PATH_MAX], proc[100]; + ssize_t s; + if (result < 0) _hook_api_(export, "rootdir_get_fd() -> %d, %m", result); else { - sprintf(path, "/proc/self/fd/%d", result); - readlink(path, path, sizeof path); + snprintf(proc, sizeof proc, "/proc/self/fd/%d", result); + s = readlink(proc, path, sizeof path); + path[s < 0 ? 0 : s >= sizeof path ? sizeof path - 1 : s] = 0; _hook_api_(export, "rootdir_get_fd() -> %d = %s", result, path); } } static void hook_api_rootdir_open_locale_cb(void *closure, const struct afb_hookid *hookid, const struct afb_export *export, const char *filename, int flags, const char *locale, int result) { - char path[PATH_MAX]; + char path[PATH_MAX], proc[100]; + ssize_t s; + if (!locale) locale = "(null)"; if (result < 0) _hook_api_(export, "rootdir_open_locale(%s, %d, %s) -> %d, %m", filename, flags, locale, result); else { - sprintf(path, "/proc/self/fd/%d", result); - readlink(path, path, sizeof path); + snprintf(proc, sizeof proc, "/proc/self/fd/%d", result); + s = readlink(proc, path, sizeof path); + path[s < 0 ? 0 : s >= sizeof path ? sizeof path - 1 : s] = 0; _hook_api_(export, "rootdir_open_locale(%s, %d, %s) -> %d = %s", filename, flags, locale, result, path); } } @@ -796,32 +805,32 @@ static void hook_api_start_after_cb(void *closure, const struct afb_hookid *hook static void hook_api_on_event_before_cb(void *closure, const struct afb_hookid *hookid, const struct afb_export *export, const char *event, int event_x2, struct json_object *object) { - _hook_api_(export, "on_event.before(%s, %d, %s)", event, event_x2, json_object_to_json_string(object)); + _hook_api_(export, "on_event.before(%s, %d, %s)", event, event_x2, json_object_to_json_string_ext(object, JSON_C_TO_STRING_NOSLASHESCAPE)); } static void hook_api_on_event_after_cb(void *closure, const struct afb_hookid *hookid, const struct afb_export *export, const char *event, int event_x2, struct json_object *object) { - _hook_api_(export, "on_event.after(%s, %d, %s)", event, event_x2, json_object_to_json_string(object)); + _hook_api_(export, "on_event.after(%s, %d, %s)", event, event_x2, json_object_to_json_string_ext(object, JSON_C_TO_STRING_NOSLASHESCAPE)); } static void hook_api_call_cb(void *closure, const struct afb_hookid *hookid, const struct afb_export *export, const char *api, const char *verb, struct json_object *args) { - _hook_api_(export, "call(%s/%s, %s) ...", api, verb, json_object_to_json_string(args)); + _hook_api_(export, "call(%s/%s, %s) ...", api, verb, json_object_to_json_string_ext(args, JSON_C_TO_STRING_NOSLASHESCAPE)); } static void hook_api_call_result_cb(void *closure, const struct afb_hookid *hookid, const struct afb_export *export, struct json_object *object, const char *error, const char *info) { - _hook_api_(export, " ...call... [%s] -> %s (%s)", error?:"success", json_object_to_json_string(object), info?:""); + _hook_api_(export, " ...call... [%s] -> %s (%s)", error?:"success", json_object_to_json_string_ext(object, JSON_C_TO_STRING_NOSLASHESCAPE), info?:""); } static void hook_api_callsync_cb(void *closure, const struct afb_hookid *hookid, const struct afb_export *export, const char *api, const char *verb, struct json_object *args) { - _hook_api_(export, "callsync(%s/%s, %s) ...", api, verb, json_object_to_json_string(args)); + _hook_api_(export, "callsync(%s/%s, %s) ...", api, verb, json_object_to_json_string_ext(args, JSON_C_TO_STRING_NOSLASHESCAPE)); } static void hook_api_callsync_result_cb(void *closure, const struct afb_hookid *hookid, const struct afb_export *export, int status, struct json_object *object, const char *error, const char *info) { - _hook_api_(export, " ...callsync... %d [%s] -> %s (%s)", status, error?:"success", json_object_to_json_string(object), info?:""); + _hook_api_(export, " ...callsync... %d [%s] -> %s (%s)", status, error?:"success", json_object_to_json_string_ext(object, JSON_C_TO_STRING_NOSLASHESCAPE), info?:""); } static void hook_api_new_api_before_cb(void *closure, const struct afb_hookid *hookid, const struct afb_export *export, const char *api, const char *info, int noconcurrency) @@ -896,12 +905,17 @@ static void hook_api_delete_api_cb(void *closure, const struct afb_hookid *hooki static void hook_api_on_event_handler_before_cb(void *closure, const struct afb_hookid *hookid, const struct afb_export *export, const char *event, int event_x2, struct json_object *object, const char *pattern) { - _hook_api_(export, "on_event_handler[%s].before(%s, %d, %s)", pattern, event, event_x2, json_object_to_json_string(object)); + _hook_api_(export, "on_event_handler[%s].before(%s, %d, %s)", pattern, event, event_x2, json_object_to_json_string_ext(object, JSON_C_TO_STRING_NOSLASHESCAPE)); } static void hook_api_on_event_handler_after_cb(void *closure, const struct afb_hookid *hookid, const struct afb_export *export, const char *event, int event_x2, struct json_object *object, const char *pattern) { - _hook_api_(export, "on_event_handler[%s].after(%s, %d, %s)", pattern, event, event_x2, json_object_to_json_string(object)); + _hook_api_(export, "on_event_handler[%s].after(%s, %d, %s)", pattern, event, event_x2, json_object_to_json_string_ext(object, JSON_C_TO_STRING_NOSLASHESCAPE)); +} + +static void hook_api_settings_cb(void *closure, const struct afb_hookid *hookid, const struct afb_export *export, struct json_object *object) +{ + _hook_api_(export, "settings -> %s", json_object_to_json_string_ext(object, JSON_C_TO_STRING_NOSLASHESCAPE)); } static struct afb_hook_api_itf hook_api_default_itf = { @@ -943,6 +957,7 @@ static struct afb_hook_api_itf hook_api_default_itf = { .hook_api_delete_api = hook_api_delete_api_cb, .hook_api_on_event_handler_before = hook_api_on_event_handler_before_cb, .hook_api_on_event_handler_after = hook_api_on_event_handler_after_cb, + .hook_api_settings = hook_api_settings_cb, }; /****************************************************************************** @@ -957,7 +972,8 @@ static struct afb_hook_api_itf hook_api_default_itf = { init_hookid(&hookid); \ hook = list_of_api_hooks; \ while (hook) { \ - if (hook->itf->hook_api_##func \ + if (hook->refcount \ + && hook->itf->hook_api_##func \ && (hook->flags & afb_hook_flag_api_##flag) != 0 \ && MATCH_API(hook->api, apiname)) { \ hook->itf->hook_api_##func(hook->closure, &hookid, __VA_ARGS__); \ @@ -1180,6 +1196,12 @@ void afb_hook_api_on_event_handler_after(const struct afb_export *export, const _HOOK_API_2_(on_event_handler, on_event_handler_after, export, event, event_x2, object, pattern); } +struct json_object *afb_hook_api_settings(const struct afb_export *export, struct json_object *object) +{ + _HOOK_API_(settings, export, object); + return object; +} + /****************************************************************************** * section: hooking export *****************************************************************************/ @@ -1224,10 +1246,10 @@ struct afb_hook_api *afb_hook_create_api(const char *api, int flags, struct afb_ hook->closure = closure; /* record the hook */ - pthread_rwlock_wrlock(&rwlock); + pthread_mutex_lock(&mutex); hook->next = list_of_api_hooks; list_of_api_hooks = hook; - pthread_rwlock_unlock(&rwlock); + pthread_mutex_unlock(&mutex); /* returns it */ return hook; @@ -1235,37 +1257,46 @@ struct afb_hook_api *afb_hook_create_api(const char *api, int flags, struct afb_ struct afb_hook_api *afb_hook_addref_api(struct afb_hook_api *hook) { - pthread_rwlock_wrlock(&rwlock); - hook->refcount++; - pthread_rwlock_unlock(&rwlock); + __atomic_add_fetch(&hook->refcount, 1, __ATOMIC_RELAXED); return hook; } -void afb_hook_unref_api(struct afb_hook_api *hook) +static void hook_clean_api() { - struct afb_hook_api **prv; + struct afb_hook_api **prv, *hook, *head; - if (hook) { - pthread_rwlock_wrlock(&rwlock); - if (--hook->refcount) - hook = NULL; - else { - /* unlink */ - prv = &list_of_api_hooks; - while (*prv && *prv != hook) + if (pthread_rwlock_trywrlock(&rwlock) == 0) { + /* unlink under mutex */ + head = NULL; + pthread_mutex_lock(&mutex); + prv = &list_of_api_hooks; + while ((hook = *prv)) { + if (hook->refcount) prv = &(*prv)->next; - if(*prv) + else { *prv = hook->next; + hook->next = head; + head = hook; + } } + pthread_mutex_unlock(&mutex); pthread_rwlock_unlock(&rwlock); - if (hook) { - /* free */ + + /* free found hooks */ + while((hook = head)) { + head = hook->next; free(hook->api); free(hook); } } } +void afb_hook_unref_api(struct afb_hook_api *hook) +{ + if (hook && !__atomic_sub_fetch(&hook->refcount, 1, __ATOMIC_RELAXED)) + hook_clean_api(); +} + /****************************************************************************** * section: default callbacks for tracing service interface (evt) *****************************************************************************/ @@ -1285,23 +1316,23 @@ static void hook_evt_create_cb(void *closure, const struct afb_hookid *hookid, c static void hook_evt_push_before_cb(void *closure, const struct afb_hookid *hookid, const char *evt, int id, struct json_object *obj) { - _hook_evt_(evt, id, "push.before(%s)", json_object_to_json_string(obj)); + _hook_evt_(evt, id, "push.before(%s)", json_object_to_json_string_ext(obj, JSON_C_TO_STRING_NOSLASHESCAPE)); } static void hook_evt_push_after_cb(void *closure, const struct afb_hookid *hookid, const char *evt, int id, struct json_object *obj, int result) { - _hook_evt_(evt, id, "push.after(%s) -> %d", json_object_to_json_string(obj), result); + _hook_evt_(evt, id, "push.after(%s) -> %d", json_object_to_json_string_ext(obj, JSON_C_TO_STRING_NOSLASHESCAPE), result); } static void hook_evt_broadcast_before_cb(void *closure, const struct afb_hookid *hookid, const char *evt, int id, struct json_object *obj) { - _hook_evt_(evt, id, "broadcast.before(%s)", json_object_to_json_string(obj)); + _hook_evt_(evt, id, "broadcast.before(%s)", json_object_to_json_string_ext(obj, JSON_C_TO_STRING_NOSLASHESCAPE)); } static void hook_evt_broadcast_after_cb(void *closure, const struct afb_hookid *hookid, const char *evt, int id, struct json_object *obj, int result) { - _hook_evt_(evt, id, "broadcast.after(%s) -> %d", json_object_to_json_string(obj), result); + _hook_evt_(evt, id, "broadcast.after(%s) -> %d", json_object_to_json_string_ext(obj, JSON_C_TO_STRING_NOSLASHESCAPE), result); } static void hook_evt_name_cb(void *closure, const struct afb_hookid *hookid, const char *evt, int id, const char *result) @@ -1341,7 +1372,8 @@ static struct afb_hook_evt_itf hook_evt_default_itf = { init_hookid(&hookid); \ hook = list_of_evt_hooks; \ while (hook) { \ - if (hook->itf->hook_evt_##what \ + if (hook->refcount \ + && hook->itf->hook_evt_##what \ && (hook->flags & afb_hook_flag_evt_##what) != 0 \ && MATCH_EVENT(hook->pattern, evt)) { \ hook->itf->hook_evt_##what(hook->closure, &hookid, __VA_ARGS__); \ @@ -1436,10 +1468,10 @@ struct afb_hook_evt *afb_hook_create_evt(const char *pattern, int flags, struct hook->closure = closure; /* record the hook */ - pthread_rwlock_wrlock(&rwlock); + pthread_mutex_lock(&mutex); hook->next = list_of_evt_hooks; list_of_evt_hooks = hook; - pthread_rwlock_unlock(&rwlock); + pthread_mutex_unlock(&mutex); /* returns it */ return hook; @@ -1447,37 +1479,46 @@ struct afb_hook_evt *afb_hook_create_evt(const char *pattern, int flags, struct struct afb_hook_evt *afb_hook_addref_evt(struct afb_hook_evt *hook) { - pthread_rwlock_wrlock(&rwlock); - hook->refcount++; - pthread_rwlock_unlock(&rwlock); + __atomic_add_fetch(&hook->refcount, 1, __ATOMIC_RELAXED); return hook; } -void afb_hook_unref_evt(struct afb_hook_evt *hook) +static void hook_clean_evt() { - struct afb_hook_evt **prv; + struct afb_hook_evt **prv, *hook, *head; - if (hook) { - pthread_rwlock_wrlock(&rwlock); - if (--hook->refcount) - hook = NULL; - else { - /* unlink */ - prv = &list_of_evt_hooks; - while (*prv && *prv != hook) + if (pthread_rwlock_trywrlock(&rwlock) == 0) { + /* unlink under mutex */ + head = NULL; + pthread_mutex_lock(&mutex); + prv = &list_of_evt_hooks; + while ((hook = *prv)) { + if (hook->refcount) prv = &(*prv)->next; - if(*prv) + else { *prv = hook->next; + hook->next = head; + head = hook; + } } + pthread_mutex_unlock(&mutex); pthread_rwlock_unlock(&rwlock); - if (hook) { - /* free */ + + /* free found hooks */ + while((hook = head)) { + head = hook->next; free(hook->pattern); free(hook); } } } +void afb_hook_unref_evt(struct afb_hook_evt *hook) +{ + if (hook && !__atomic_sub_fetch(&hook->refcount, 1, __ATOMIC_RELAXED)) + hook_clean_evt(); +} + /****************************************************************************** * section: default callbacks for sessions (session) *****************************************************************************/ @@ -1492,7 +1533,7 @@ static void _hook_session_(struct afb_session *session, const char *format, ...) static void hook_session_create_cb(void *closure, const struct afb_hookid *hookid, struct afb_session *session) { - _hook_session_(session, "create -> token=%s", afb_session_token(session)); + _hook_session_(session, "create"); } static void hook_session_close_cb(void *closure, const struct afb_hookid *hookid, struct afb_session *session) @@ -1505,11 +1546,6 @@ static void hook_session_destroy_cb(void *closure, const struct afb_hookid *hook _hook_session_(session, "destroy"); } -static void hook_session_renew_cb(void *closure, const struct afb_hookid *hookid, struct afb_session *session) -{ - _hook_session_(session, "renew -> token=%s", afb_session_token(session)); -} - static void hook_session_addref_cb(void *closure, const struct afb_hookid *hookid, struct afb_session *session) { _hook_session_(session, "addref"); @@ -1524,7 +1560,6 @@ static struct afb_hook_session_itf hook_session_default_itf = { .hook_session_create = hook_session_create_cb, .hook_session_close = hook_session_close_cb, .hook_session_destroy = hook_session_destroy_cb, - .hook_session_renew = hook_session_renew_cb, .hook_session_addref = hook_session_addref_cb, .hook_session_unref = hook_session_unref_cb }; @@ -1541,7 +1576,8 @@ static struct afb_hook_session_itf hook_session_default_itf = { init_hookid(&hookid); \ hook = list_of_session_hooks; \ while (hook) { \ - if (hook->itf->hook_session_##what \ + if (hook->refcount \ + && hook->itf->hook_session_##what \ && (hook->flags & afb_hook_flag_session_##what) != 0 \ && MATCH_SESSION(hook->pattern, (sessid?:(sessid=afb_session_uuid(session))))) { \ hook->itf->hook_session_##what(hook->closure, &hookid, __VA_ARGS__); \ @@ -1565,11 +1601,6 @@ void afb_hook_session_destroy(struct afb_session *session) _HOOK_SESSION_(destroy, session); } -void afb_hook_session_renew(struct afb_session *session) -{ - _HOOK_SESSION_(renew, session); -} - void afb_hook_session_addref(struct afb_session *session) { _HOOK_SESSION_(addref, session); @@ -1608,10 +1639,10 @@ struct afb_hook_session *afb_hook_create_session(const char *pattern, int flags, hook->closure = closure; /* record the hook */ - pthread_rwlock_wrlock(&rwlock); + pthread_mutex_lock(&mutex); hook->next = list_of_session_hooks; list_of_session_hooks = hook; - pthread_rwlock_unlock(&rwlock); + pthread_mutex_unlock(&mutex); /* returns it */ return hook; @@ -1619,37 +1650,46 @@ struct afb_hook_session *afb_hook_create_session(const char *pattern, int flags, struct afb_hook_session *afb_hook_addref_session(struct afb_hook_session *hook) { - pthread_rwlock_wrlock(&rwlock); - hook->refcount++; - pthread_rwlock_unlock(&rwlock); + __atomic_add_fetch(&hook->refcount, 1, __ATOMIC_RELAXED); return hook; } -void afb_hook_unref_session(struct afb_hook_session *hook) +static void hook_clean_session() { - struct afb_hook_session **prv; + struct afb_hook_session **prv, *hook, *head; - if (hook) { - pthread_rwlock_wrlock(&rwlock); - if (--hook->refcount) - hook = NULL; - else { - /* unlink */ - prv = &list_of_session_hooks; - while (*prv && *prv != hook) + if (pthread_rwlock_trywrlock(&rwlock) == 0) { + /* unlink under mutex */ + head = NULL; + pthread_mutex_lock(&mutex); + prv = &list_of_session_hooks; + while ((hook = *prv)) { + if (hook->refcount) prv = &(*prv)->next; - if(*prv) + else { *prv = hook->next; + hook->next = head; + head = hook; + } } + pthread_mutex_unlock(&mutex); pthread_rwlock_unlock(&rwlock); - if (hook) { - /* free */ + + /* free found hooks */ + while((hook = head)) { + head = hook->next; free(hook->pattern); free(hook); } } } +void afb_hook_unref_session(struct afb_hook_session *hook) +{ + if (hook && !__atomic_sub_fetch(&hook->refcount, 1, __ATOMIC_RELAXED)) + hook_clean_session(); +} + /****************************************************************************** * section: default callbacks for globals (global) *****************************************************************************/ @@ -1695,7 +1735,8 @@ static struct afb_hook_global_itf hook_global_default_itf = { init_hookid(&hookid); \ hook = list_of_global_hooks; \ while (hook) { \ - if (hook->itf->hook_global_##what \ + if (hook->refcount \ + && hook->itf->hook_global_##what \ && (hook->flags & afb_hook_flag_global_##what) != 0) { \ hook->itf->hook_global_##what(hook->closure, &hookid, __VA_ARGS__); \ } \ @@ -1720,7 +1761,8 @@ static void update_global() pthread_rwlock_rdlock(&rwlock); hook = list_of_global_hooks; while (hook) { - flags = hook->flags; + if (hook->refcount) + flags = hook->flags; hook = hook->next; } verbose_observer = (flags & afb_hook_flag_global_vverbose) ? afb_hook_global_vverbose : NULL; @@ -1743,10 +1785,10 @@ struct afb_hook_global *afb_hook_create_global(int flags, struct afb_hook_global hook->closure = closure; /* record the hook */ - pthread_rwlock_wrlock(&rwlock); + pthread_mutex_lock(&mutex); hook->next = list_of_global_hooks; list_of_global_hooks = hook; - pthread_rwlock_unlock(&rwlock); + pthread_mutex_unlock(&mutex); /* update hooking */ update_global(); @@ -1757,36 +1799,46 @@ struct afb_hook_global *afb_hook_create_global(int flags, struct afb_hook_global struct afb_hook_global *afb_hook_addref_global(struct afb_hook_global *hook) { - pthread_rwlock_wrlock(&rwlock); - hook->refcount++; - pthread_rwlock_unlock(&rwlock); + __atomic_add_fetch(&hook->refcount, 1, __ATOMIC_RELAXED); return hook; } -void afb_hook_unref_global(struct afb_hook_global *hook) +static void hook_clean_global() { - struct afb_hook_global **prv; + struct afb_hook_global **prv, *hook, *head; - if (hook) { - pthread_rwlock_wrlock(&rwlock); - if (--hook->refcount) - hook = NULL; - else { - /* unlink */ - prv = &list_of_global_hooks; - while (*prv && *prv != hook) + if (pthread_rwlock_trywrlock(&rwlock) == 0) { + /* unlink under mutex */ + head = NULL; + pthread_mutex_lock(&mutex); + prv = &list_of_global_hooks; + while ((hook = *prv)) { + if (hook->refcount) prv = &(*prv)->next; - if(*prv) + else { *prv = hook->next; + hook->next = head; + head = hook; + } } + pthread_mutex_unlock(&mutex); pthread_rwlock_unlock(&rwlock); - if (hook) { - /* free */ - free(hook); - /* update hooking */ - update_global(); + /* free found hooks */ + while((hook = head)) { + head = hook->next; + free(hook); } } } +void afb_hook_unref_global(struct afb_hook_global *hook) +{ + if (hook && !__atomic_sub_fetch(&hook->refcount, 1, __ATOMIC_RELAXED)) { + /* update hooking */ + update_global(); + hook_clean_global(); + } +} + +#endif /* WITH_AFB_HOOK *******************************************************/