Update copyright dates
[src/app-framework-binder.git] / src / afb-api-so-v2.c
index a13c00e..59a3195 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2016, 2017, 2018 "IoT.bzh"
+ * Copyright (C) 2015-2020 "IoT.bzh"
  * Author José Bollo <jose.bollo@iot.bzh>
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
@@ -15,6 +15,8 @@
  * limitations under the License.
  */
 
+#if WITH_LEGACY_BINDING_V2 && WITH_DYNAMIC_BINDING
+
 #define _GNU_SOURCE
 
 #include <stdlib.h>
@@ -22,6 +24,7 @@
 #include <dlfcn.h>
 #include <assert.h>
 #include <stdarg.h>
+#include <errno.h>
 
 #include <json-c/json.h>
 #include <afb/afb-binding-v2.h>
@@ -35,6 +38,7 @@
 #include "afb-context.h"
 #include "afb-api-so.h"
 #include "afb-xreq.h"
+#include "sig-monitor.h"
 #include "verbose.h"
 
 /*
 static const char afb_api_so_v2_descriptor[] = "afbBindingV2";
 static const char afb_api_so_v2_data[] = "afbBindingV2data";
 
+struct preinit
+{
+       int return_code;
+       const struct afb_binding_v2 *binding;
+};
+
 static const struct afb_verb_v2 *search(const struct afb_binding_v2 *binding, const char *name)
 {
        const struct afb_verb_v2 *verb;
@@ -67,11 +77,11 @@ struct json_object *afb_api_so_v2_make_description_openAPIv3(const struct afb_bi
        char buffer[256];
        const struct afb_verb_v2 *verb;
        struct json_object *r, *f, *a, *i, *p, *g;
-
+       enum json_tokener_error jerr;
 
        if (binding->specification) {
-               r = json_tokener_parse(binding->specification);
-               if (r)
+               r = json_tokener_parse_verbose(binding->specification, &jerr);
+               if (jerr == json_tokener_success)
                        return r;
        }
 
@@ -96,7 +106,7 @@ struct json_object *afb_api_so_v2_make_description_openAPIv3(const struct afb_bi
                g = json_object_new_object();
                json_object_object_add(f, "get", g);
 
-               a = afb_auth_json_v2(verb->auth, verb->session);
+               a = afb_auth_json_x2(verb->auth, verb->session);
                if (a)
                        json_object_object_add(g, "x-permissions", a);
 
@@ -110,10 +120,29 @@ struct json_object *afb_api_so_v2_make_description_openAPIv3(const struct afb_bi
        return r;
 }
 
-int afb_api_so_v2_add_binding(const struct afb_binding_v2 *binding, void *handle, struct afb_apiset *declare_set, struct afb_apiset * call_set, struct afb_binding_data_v2 *data)
+static void do_preinit(int sig, void *closure)
+{
+       struct preinit *preinit = closure;
+
+       if (!sig)
+               preinit->return_code = preinit->binding->preinit();
+       else {
+               errno = EINTR;
+               preinit->return_code = -1;
+       }
+};
+
+int afb_api_so_v2_add_binding(
+               const struct afb_binding_v2 *binding,
+               void *handle,
+               struct afb_apiset *declare_set,
+               struct afb_apiset * call_set,
+               struct afb_binding_data_v2 *data,
+               const char *path)
 {
        int rc;
        struct afb_export *export;
+       struct preinit preinit;
 
        /* basic checks */
        assert(binding);
@@ -122,7 +151,7 @@ int afb_api_so_v2_add_binding(const struct afb_binding_v2 *binding, void *handle
        assert(data);
 
        /* allocates the description */
-       export = afb_export_create_v2(declare_set, call_set, binding->api, binding, data, binding->init, binding->onevent);
+       export = afb_export_create_v2(declare_set, call_set, binding->api, binding, data, binding->init, binding->onevent, path);
        if (!export) {
                ERROR("out of memory");
                goto error;
@@ -136,7 +165,9 @@ int afb_api_so_v2_add_binding(const struct afb_binding_v2 *binding, void *handle
        /* init the binding */
        if (binding->preinit) {
                INFO("binding %s calling preinit function", binding->api);
-               rc = binding->preinit();
+               preinit.binding = binding;
+               sig_monitor(0, do_preinit, &preinit);
+               rc = preinit.return_code;
                if (rc < 0) {
                        ERROR("binding %s preinit function failed...", afb_export_apiname(export));
                        afb_export_undeclare(export);
@@ -186,9 +217,10 @@ int afb_api_so_v2_add(const char *path, void *handle, struct afb_apiset *declare
                goto error;
        }
 
-       return afb_api_so_v2_add_binding(binding, handle, declare_set, call_set, data);
+       return afb_api_so_v2_add_binding(binding, handle, declare_set, call_set, data, path);
 
  error:
        return -1;
 }
 
+#endif