Simplify functions for calls
[src/app-framework-binder.git] / src / afb-api-so.c
index 39a6e9b..daa3533 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2016 "IoT.bzh"
+ * Copyright (C) 2016, 2017 "IoT.bzh"
  * Author José Bollo <jose.bollo@iot.bzh>
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
@@ -22,6 +22,7 @@
 #include <assert.h>
 #include <string.h>
 #include <dirent.h>
+#include <errno.h>
 #include <dlfcn.h>
 #include <unistd.h>
 #include <limits.h>
 #include <afb/afb-req-itf.h>
 #include <afb/afb-event-itf.h>
 
-#include "session.h"
+#include "afb-session.h"
 #include "afb-common.h"
 #include "afb-context.h"
 #include "afb-apis.h"
 #include "afb-api-so.h"
-#include "afb-sig-handler.h"
+#include "afb-thread.h"
 #include "afb-evt.h"
 #include "afb-svc.h"
 #include "verbose.h"
@@ -53,34 +54,34 @@ struct api_so_desc {
        struct afb_binding_interface interface; /* interface for the binding */
 };
 
-struct monitoring {
-       struct afb_req req;
-       void (*action)(struct afb_req);
-};
-
 static const char binding_register_function_v1[] = "afbBindingV1Register";
 static const char binding_service_init_function_v1[] = "afbBindingV1ServiceInit";
 static const char binding_service_event_function_v1[] = "afbBindingV1ServiceEvent";
 
 static int api_timeout = 15;
 
-static struct afb_event afb_api_so_event_make(struct api_so_desc *desc, const char *name);
-static int afb_api_so_event_broadcast(struct api_so_desc *desc, const char *name, struct json_object *object);
-static void afb_api_so_vverbose(struct api_so_desc *desc, int level, const char *file, int line, const char *fmt, va_list args);
+static struct afb_event afb_api_so_event_make_cb(void *closure, const char *name);
+static int afb_api_so_event_broadcast_cb(void *closure, const char *name, struct json_object *object);
+static void afb_api_so_vverbose_cb(void *closure, int level, const char *file, int line, const char *fmt, va_list args);
+static int afb_api_so_rootdir_get_fd(void *closure);
+static int afb_api_so_rootdir_open_locale(void *closure, const char *filename, int flags, const char *locale);
 
 static const struct afb_daemon_itf daemon_itf = {
-       .event_broadcast = (void*)afb_api_so_event_broadcast,
-       .get_event_loop = (void*)afb_common_get_event_loop,
-       .get_user_bus = (void*)afb_common_get_user_bus,
-       .get_system_bus = (void*)afb_common_get_system_bus,
-       .vverbose = (void*)afb_api_so_vverbose,
-       .event_make = (void*)afb_api_so_event_make
+       .event_broadcast = afb_api_so_event_broadcast_cb,
+       .get_event_loop = afb_common_get_event_loop,
+       .get_user_bus = afb_common_get_user_bus,
+       .get_system_bus = afb_common_get_system_bus,
+       .vverbose = afb_api_so_vverbose_cb,
+       .event_make = afb_api_so_event_make_cb,
+       .rootdir_get_fd = afb_api_so_rootdir_get_fd,
+       .rootdir_open_locale = afb_api_so_rootdir_open_locale
 };
 
-static struct afb_event afb_api_so_event_make(struct api_so_desc *desc, const char *name)
+static struct afb_event afb_api_so_event_make_cb(void *closure, const char *name)
 {
        size_t length;
        char *event;
+       struct api_so_desc *desc = closure;
 
        /* makes the event name */
        assert(desc->binding != NULL);
@@ -94,10 +95,11 @@ static struct afb_event afb_api_so_event_make(struct api_so_desc *desc, const ch
        return afb_evt_create_event(event);
 }
 
-static int afb_api_so_event_broadcast(struct api_so_desc *desc, const char *name, struct json_object *object)
+static int afb_api_so_event_broadcast_cb(void *closure, const char *name, struct json_object *object)
 {
        size_t length;
        char *event;
+       struct api_so_desc *desc = closure;
 
        /* makes the event name */
        assert(desc->binding != NULL);
@@ -110,9 +112,10 @@ static int afb_api_so_event_broadcast(struct api_so_desc *desc, const char *name
        return afb_evt_broadcast(event, object);
 }
 
-static void afb_api_so_vverbose(struct api_so_desc *desc, int level, const char *file, int line, const char *fmt, va_list args)
+static void afb_api_so_vverbose_cb(void *closure, int level, const char *file, int line, const char *fmt, va_list args)
 {
        char *p;
+       struct api_so_desc *desc = closure;
 
        if (vasprintf(&p, fmt, args) < 0)
                vverbose(level, file, line, fmt, args);
@@ -122,37 +125,37 @@ static void afb_api_so_vverbose(struct api_so_desc *desc, int level, const char
        }
 }
 
-static void monitored_call(int signum, struct monitoring *data)
+static int afb_api_so_rootdir_get_fd(void *closure)
 {
-       if (signum != 0)
-               afb_req_fail_f(data->req, "aborted", "signal %s(%d) caught", strsignal(signum), signum);
-       else
-               data->action(data->req);
+       return afb_common_rootdir_get_fd();
 }
 
-static void call_check(struct afb_req req, struct afb_context *context, const struct afb_verb_desc_v1 *verb)
+static int afb_api_so_rootdir_open_locale(void *closure, const char *filename, int flags, const char *locale)
 {
-       struct monitoring data;
+       return afb_common_rootdir_open_locale(filename, flags, locale);
+}
 
+static int call_check(struct afb_req req, struct afb_context *context, const struct afb_verb_desc_v1 *verb)
+{
        int stag = (int)verb->session;
 
        if ((stag & (AFB_SESSION_CREATE|AFB_SESSION_CLOSE|AFB_SESSION_RENEW|AFB_SESSION_CHECK|AFB_SESSION_LOA_EQ)) != 0) {
                if (!afb_context_check(context)) {
                        afb_context_close(context);
                        afb_req_fail(req, "failed", "invalid token's identity");
-                       return;
-               }       
+                       return 0;
+               }
        }
 
        if ((stag & AFB_SESSION_CREATE) != 0) {
                if (afb_context_check_loa(context, 1)) {
                        afb_req_fail(req, "failed", "invalid creation state");
-                       return;
+                       return 0;
                }
                afb_context_change_loa(context, 1);
                afb_context_refresh(context);
        }
-       
+
        if ((stag & (AFB_SESSION_CREATE | AFB_SESSION_RENEW)) != 0)
                afb_context_refresh(context);
 
@@ -165,7 +168,7 @@ static void call_check(struct afb_req req, struct afb_context *context, const st
                int loa = (stag >> AFB_SESSION_LOA_SHIFT) & AFB_SESSION_LOA_MASK;
                if (!afb_context_check_loa(context, loa)) {
                        afb_req_fail(req, "failed", "invalid LOA");
-                       return;
+                       return 0;
                }
        }
 
@@ -173,33 +176,34 @@ static void call_check(struct afb_req req, struct afb_context *context, const st
                int loa = (stag >> AFB_SESSION_LOA_SHIFT) & AFB_SESSION_LOA_MASK;
                if (afb_context_check_loa(context, loa + 1)) {
                        afb_req_fail(req, "failed", "invalid LOA");
-                       return;
+                       return 0;
                }
        }
-
-       data.req = req;
-       data.action = verb->callback;
-       afb_sig_monitor((void*)monitored_call, &data, api_timeout);
+       return 1;
 }
 
-static void call(struct api_so_desc *desc, struct afb_req req, struct afb_context *context, const char *verb, size_t lenverb)
+static void call_cb(void *closure, struct afb_req req, struct afb_context *context, const char *strverb)
 {
-       const struct afb_verb_desc_v1 *v;
-
-       v = desc->binding->v1.verbs;
-       while (v->name && (strncasecmp(v->name, verb, lenverb) || v->name[lenverb]))
-               v++;
-       if (v->name)
-               call_check(req, context, v);
-       else
-               afb_req_fail_f(req, "unknown-verb", "verb %.*s unknown within api %s", (int)lenverb, verb, desc->binding->v1.prefix);
+       const struct afb_verb_desc_v1 *verb;
+       struct api_so_desc *desc = closure;
+
+       verb = desc->binding->v1.verbs;
+       while (verb->name && strcasecmp(verb->name, strverb))
+               verb++;
+       if (!verb->name)
+               afb_req_fail_f(req, "unknown-verb", "verb %s unknown within api %s", strverb, desc->binding->v1.prefix);
+       else if (call_check(req, context, verb)) {
+               afb_thread_req_call(req, verb->callback, api_timeout, desc);
+       }
 }
 
-static int service_start(struct api_so_desc *desc, int share_session, int onneed)
+static int service_start_cb(void *closure, int share_session, int onneed)
 {
        int (*init)(struct afb_service service);
        void (*onevent)(const char *event, struct json_object *object);
 
+       struct api_so_desc *desc = closure;
+
        /* check state */
        if (desc->service != NULL) {
                /* not an error when onneed */
@@ -246,12 +250,14 @@ int afb_api_so_add_binding(const char *path)
        void *handle;
        struct api_so_desc *desc;
        struct afb_binding *(*register_function) (const struct afb_binding_interface *interface);
+       struct afb_verb_desc_v1 fake_verb;
+       struct afb_binding fake_binding;
 
        // This is a loadable library let's check if it's a binding
        rc = 0;
        handle = dlopen(path, RTLD_NOW | RTLD_LOCAL);
        if (handle == NULL) {
-               ERROR("binding [%s] not loadable", path);
+               ERROR("binding [%s] not loadable: %s", path, dlerror());
                goto error;
        }
 
@@ -278,6 +284,14 @@ int afb_api_so_add_binding(const char *path)
        desc->interface.daemon.itf = &daemon_itf;
        desc->interface.daemon.closure = desc;
 
+       /* for log purpose, a fake binding is needed here */
+       desc->binding = &fake_binding;
+       fake_binding.type = AFB_BINDING_VERSION_1;
+       fake_binding.v1.info = path;
+       fake_binding.v1.prefix = path;
+       fake_binding.v1.verbs = &fake_verb;
+       fake_verb.name = NULL;
+
        /* init the binding */
        NOTICE("binding [%s] calling registering function %s", path, binding_register_function_v1);
        desc->binding = register_function(&desc->interface);
@@ -312,8 +326,8 @@ int afb_api_so_add_binding(const char *path)
        desc->apilength = strlen(desc->binding->v1.prefix);
        if (afb_apis_add(desc->binding->v1.prefix, (struct afb_api){
                        .closure = desc,
-                       .call = (void*)call,
-                       .service_start = (void*)service_start }) < 0) {
+                       .call = call_cb,
+                       .service_start = service_start_cb }) < 0) {
                ERROR("binding [%s] can't be registered...", path);
                goto error3;
        }
@@ -331,7 +345,7 @@ error:
 static int adddirs(char path[PATH_MAX], size_t end)
 {
        DIR *dir;
-       struct dirent ent, *result;
+       struct dirent *dent;
        size_t len;
 
        /* open the DIR now */
@@ -346,29 +360,34 @@ static int adddirs(char path[PATH_MAX], size_t end)
        if (end)
                path[end++] = '/';
        for (;;) {
-               readdir_r(dir, &ent, &result);
-               if (result == NULL)
+               errno = 0;
+               dent = readdir(dir);
+               if (dent == NULL) {
+                       if (errno != 0)
+                               ERROR("read error while scanning directory %.*s: %m", (int)(end - 1), path);
                        break;
+               }
 
-               len = strlen(ent.d_name);
+               len = strlen(dent->d_name);
                if (len + end >= PATH_MAX) {
-                       ERROR("path too long while scanning bindings for %s", ent.d_name);
+                       ERROR("path too long while scanning bindings for %s", dent->d_name);
                        continue;
                }
-               memcpy(&path[end], ent.d_name, len+1);
-               if (ent.d_type == DT_DIR) {
+               if (dent->d_type == DT_DIR) {
                        /* case of directories */
-                       if (ent.d_name[0] == '.') {
+                       if (dent->d_name[0] == '.') {
                                if (len == 1)
                                        continue;
-                               if (ent.d_name[1] == '.' && len == 2)
+                               if (dent->d_name[1] == '.' && len == 2)
                                        continue;
                        }
+                       memcpy(&path[end], dent->d_name, len+1);
                        adddirs(path, end+len);;
-               } else if (ent.d_type == DT_REG) {
+               } else if (dent->d_type == DT_REG) {
                        /* case of files */
-                       if (!strstr(ent.d_name, ".so"))
+                       if (memcmp(&dent->d_name[len - 3], ".so", 4))
                                continue;
+                       memcpy(&path[end], dent->d_name, len+1);
                        if (afb_api_so_add_binding(path) < 0)
                                return -1;
                }