bef27e2e01b0e5cea495f6ab5de245d1ade2c57c
[src/app-framework-binder.git] / src / afb-api-so-v3.c
1 /*
2  * Copyright (C) 2018, 2019 "IoT.bzh"
3  * Author José Bollo <jose.bollo@iot.bzh>
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *   http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17
18 #if WITH_DYNAMIC_BINDING
19
20 #define _GNU_SOURCE
21
22 #include <stdlib.h>
23 #include <string.h>
24 #include <dlfcn.h>
25 #include <assert.h>
26 #include <stdarg.h>
27
28 #include <json-c/json.h>
29 #include <afb/afb-binding-v3.h>
30
31 #include "afb-api.h"
32 #include "afb-api-so-v3.h"
33 #include "afb-api-v3.h"
34 #include "afb-apiset.h"
35 #include "afb-export.h"
36 #include "verbose.h"
37
38 /*
39  * names of symbols
40  */
41 static const char afb_api_so_v3_desc[] = "afbBindingV3";
42 static const char afb_api_so_v3_root[] = "afbBindingV3root";
43 static const char afb_api_so_v3_entry[] = "afbBindingV3entry";
44
45 struct args
46 {
47         struct afb_api_x3 **root;
48         const struct afb_binding_v3 *desc;
49         int (*entry)(struct afb_api_x3 *);
50 };
51
52 static int init(void *closure, struct afb_api_x3 *api)
53 {
54         const struct args *a = closure;
55         int rc = 0;
56
57         *a->root = api;
58         if (a->desc) {
59                 api->userdata = a->desc->userdata;
60                 rc = afb_api_v3_set_binding_fields(a->desc, api);
61         }
62
63         if (rc >= 0 && a->entry)
64                 rc = afb_api_v3_safe_preinit(api, a->entry);
65
66         if (rc >= 0)
67                 afb_api_x3_seal(api);
68
69         return rc;
70 }
71
72 int afb_api_so_v3_add(const char *path, void *handle, struct afb_apiset *declare_set, struct afb_apiset * call_set)
73 {
74         struct args a;
75         struct afb_api_v3 *api;
76         struct afb_export *export;
77
78         /* retrieves important exported symbols */
79         a.desc = dlsym(handle, afb_api_so_v3_desc);
80         a.entry = dlsym(handle, afb_api_so_v3_entry);
81         if (!a.desc && !a.entry)
82                 return 0;
83
84         INFO("binding [%s] looks like an AFB binding V3", path);
85
86         /* basic checks */
87         a.root = dlsym(handle, afb_api_so_v3_root);
88         if (!a.root) {
89                 ERROR("binding [%s] incomplete symbol set: %s is missing",
90                         path, afb_api_so_v3_root);
91                 goto error;
92         }
93         if (a.desc) {
94                 if (a.desc->api == NULL || *a.desc->api == 0) {
95                         ERROR("binding [%s] bad api name...", path);
96                         goto error;
97                 }
98                 if (!afb_api_is_valid_name(a.desc->api)) {
99                         ERROR("binding [%s] invalid api name...", path);
100                         goto error;
101                 }
102                 if (!a.entry)
103                         a.entry = a.desc->preinit;
104                 else if (a.desc->preinit) {
105                         ERROR("binding [%s] clash: you can't define %s and %s.preinit, choose only one",
106                                 path, afb_api_so_v3_entry, afb_api_so_v3_desc);
107                         goto error;
108                 }
109
110                 api = afb_api_v3_create(declare_set, call_set, a.desc->api, a.desc->info, a.desc->noconcurrency, init, &a, 0, NULL, path);
111                 if (api)
112                         return 1;
113         } else {
114                 if (!a.entry) {
115                         ERROR("binding [%s] incomplete symbol set: %s is missing",
116                                 path, afb_api_so_v3_entry);
117                         goto error;
118                 }
119
120                 export = afb_export_create_none_for_path(declare_set, call_set, path, init, &a);
121                 if (export) {
122                         /*
123                          *  No call is done to afb_export_unref(export) because:
124                          *   - legacy applications may use the root API emitting messages
125                          *   - it allows writting applications like bindings without API
126                          *  But this has the sad effect to introduce a kind of leak.
127                          *  To avoid this, if necessary further developement should list bindings
128                          *  and their data.
129                          */
130                         return 1;
131                 }
132         }
133
134         ERROR("binding [%s] initialisation failed", path);
135
136 error:
137         return -1;
138 }
139
140 #endif