afb-hook: Allow to unhook some apis
authorJosé Bollo <jose.bollo@iot.bzh>
Fri, 11 Aug 2017 15:00:08 +0000 (17:00 +0200)
committerJosé Bollo <jose.bollo@iot.bzh>
Wed, 31 Jan 2018 13:55:56 +0000 (14:55 +0100)
The apis starting with a $ (dollar) will not be
tracked by hooks

Change-Id: I3b83458c288ef1eb27993d4c71785b32e5a72deb
Signed-off-by: José Bollo <jose.bollo@iot.bzh>
src/afb-api.h
src/afb-hook.c

index dd5d4e0..a9de843 100644 (file)
@@ -39,3 +39,8 @@ struct afb_api
 };
 
 extern int afb_api_is_valid_name(const char *name);
+
+#define AFB_API_UNHOOKABLE_PREFIX_CHAR    '$'
+#define AFB_API_UNHOOKABLE_PREFIX_STRING  "$"
+#define afb_api_is_hookable(api)          ((api)[0] != AFB_API_UNHOOKABLE_PREFIX_CHAR)
+
index 54058de..b5dbe15 100644 (file)
@@ -38,6 +38,7 @@
 #include "afb-xreq.h"
 #include "afb-export.h"
 #include "afb-evt.h"
+#include "afb-api.h"
 #include "verbose.h"
 
 /**
@@ -582,19 +583,21 @@ void afb_hook_init_xreq(struct afb_xreq *xreq)
 
        /* scan hook list to get the expected flags */
        flags = 0;
-       pthread_rwlock_rdlock(&rwlock);
-       hook = list_of_xreq_hooks;
-       while (hook) {
-               f = hook->flags & afb_hook_flags_req_all;
-               add = f != 0
-                  && (!hook->session || hook->session == xreq->context.session)
-                  && (!hook->api || !strcasecmp(hook->api, xreq->request.api))
-                  && (!hook->verb || !strcasecmp(hook->verb, xreq->request.verb));
-               if (add)
-                       flags |= f;
-               hook = hook->next;
+       if (afb_api_is_hookable(xreq->request.api)) {
+               pthread_rwlock_rdlock(&rwlock);
+               hook = list_of_xreq_hooks;
+               while (hook) {
+                       f = hook->flags & afb_hook_flags_req_all;
+                       add = f != 0
+                          && (!hook->session || hook->session == xreq->context.session)
+                          && (!hook->api || !strcasecmp(hook->api, xreq->request.api))
+                          && (!hook->verb || !strcasecmp(hook->verb, xreq->request.verb));
+                       if (add)
+                               flags |= f;
+                       hook = hook->next;
+               }
+               pthread_rwlock_unlock(&rwlock);
        }
-       pthread_rwlock_unlock(&rwlock);
 
        /* store the hooking data */
        xreq->hookflags = flags;
@@ -919,15 +922,17 @@ int afb_hook_flags_ditf(const char *api)
        int flags;
        struct afb_hook_ditf *hook;
 
-       pthread_rwlock_rdlock(&rwlock);
        flags = 0;
-       hook = list_of_ditf_hooks;
-       while (hook) {
-               if (!api || !hook->api || !strcasecmp(hook->api, api))
-                       flags |= hook->flags;
-               hook = hook->next;
+       if (!api || afb_api_is_hookable(api)) {
+               pthread_rwlock_rdlock(&rwlock);
+               hook = list_of_ditf_hooks;
+               while (hook) {
+                       if (!api || !hook->api || !strcasecmp(hook->api, api))
+                               flags |= hook->flags;
+                       hook = hook->next;
+               }
+               pthread_rwlock_unlock(&rwlock);
        }
-       pthread_rwlock_unlock(&rwlock);
        return flags;
 }
 
@@ -1131,15 +1136,17 @@ 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;
+       if (!api || afb_api_is_hookable(api)) {
+               pthread_rwlock_rdlock(&rwlock);
+               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);
        }
-       pthread_rwlock_unlock(&rwlock);
        return flags;
 }
 
@@ -1343,15 +1350,17 @@ int afb_hook_flags_evt(const char *name)
        int flags;
        struct afb_hook_evt *hook;
 
-       pthread_rwlock_rdlock(&rwlock);
        flags = 0;
-       hook = list_of_evt_hooks;
-       while (hook) {
-               if (!name || !hook->pattern || !fnmatch(hook->pattern, name, FNM_CASEFOLD))
-                       flags |= hook->flags;
-               hook = hook->next;
+       if (!name || afb_api_is_hookable(name)) {
+               pthread_rwlock_rdlock(&rwlock);
+               hook = list_of_evt_hooks;
+               while (hook) {
+                       if (!name || !hook->pattern || !fnmatch(hook->pattern, name, FNM_CASEFOLD))
+                               flags |= hook->flags;
+                       hook = hook->next;
+               }
+               pthread_rwlock_unlock(&rwlock);
        }
-       pthread_rwlock_unlock(&rwlock);
        return flags;
 }