afb-hook: Allow to unhook some apis
[src/app-framework-binder.git] / src / afb-hook.c
index 7ae713e..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"
 
 /**
@@ -364,6 +365,11 @@ static void hook_xreq_context_make_default_cb(void *closure, const struct afb_ho
        _hook_xreq_(xreq, "context_make(replace=%s, %p, %p, %p) -> %p", replace?"yes":"no", create_value, free_value, create_closure, result);
 }
 
+static void hook_xreq_get_uid_default_cb(void *closure, const struct afb_hookid *hookid, const struct afb_xreq *xreq, int result)
+{
+       _hook_xreq_(xreq, "get_uid() -> %d", result);
+}
+
 static struct afb_hook_xreq_itf hook_xreq_default_itf = {
        .hook_xreq_begin = hook_xreq_begin_default_cb,
        .hook_xreq_end = hook_xreq_end_default_cb,
@@ -390,7 +396,8 @@ static struct afb_hook_xreq_itf hook_xreq_default_itf = {
        .hook_xreq_subcall_req_result = hook_xreq_subcall_req_result_default_cb,
        .hook_xreq_has_permission = hook_xreq_has_permission_default_cb,
        .hook_xreq_get_application_id = hook_xreq_get_application_id_default_cb,
-       .hook_xreq_context_make = hook_xreq_context_make_default_cb
+       .hook_xreq_context_make = hook_xreq_context_make_default_cb,
+       .hook_xreq_get_uid = hook_xreq_get_uid_default_cb,
 };
 
 /******************************************************************************
@@ -556,6 +563,12 @@ void *afb_hook_xreq_context_make(const struct afb_xreq *xreq, int replace, void
        return result;
 }
 
+int afb_hook_xreq_get_uid(const struct afb_xreq *xreq, int result)
+{
+       _HOOK_XREQ_(get_uid, xreq, result);
+       return result;
+}
+
 /******************************************************************************
  * section: hooking xreqs
  *****************************************************************************/
@@ -570,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;
@@ -907,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;
 }
 
@@ -1119,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;
 }
 
@@ -1331,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;
 }