Fedora 30 packaging fix issu
[src/app-framework-binder.git] / src / afb-api-so-v3.c
1 /*
2  * Copyright (C) 2018 "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 #define _GNU_SOURCE
19
20 #include <stdlib.h>
21 #include <string.h>
22 #include <dlfcn.h>
23 #include <assert.h>
24 #include <stdarg.h>
25
26 #include <json-c/json.h>
27 #include <afb/afb-binding-v3.h>
28
29 #include "afb-api.h"
30 #include "afb-api-so-v3.h"
31 #include "afb-api-v3.h"
32 #include "afb-apiset.h"
33 #include "afb-export.h"
34 #include "verbose.h"
35
36 /*
37  * names of symbols
38  */
39 static const char afb_api_so_v3_desc[] = "afbBindingV3";
40 static const char afb_api_so_v3_root[] = "afbBindingV3root";
41 static const char afb_api_so_v3_entry[] = "afbBindingV3entry";
42
43 struct args
44 {
45         struct afb_api_x3 **root;
46         const struct afb_binding_v3 *desc;
47         int (*entry)(struct afb_api_x3 *);
48 };
49
50 static int init(void *closure, struct afb_api_x3 *api)
51 {
52         const struct args *a = closure;
53         int rc = 0;
54
55         *a->root = api;
56         if (a->desc) {
57                 api->userdata = a->desc->userdata;
58                 rc = afb_api_v3_set_binding_fields(a->desc, api);
59         }
60
61         if (rc >= 0 && a->entry)
62                 rc = afb_api_v3_safe_preinit(api, a->entry);
63
64         if (rc >= 0)
65                 afb_api_x3_seal(api);
66
67         return rc;
68 }
69
70 int afb_api_so_v3_add(const char *path, void *handle, struct afb_apiset *declare_set, struct afb_apiset * call_set)
71 {
72         struct args a;
73         struct afb_api_v3 *api;
74         struct afb_export *export;
75
76         /* retrieves the register function */
77         a.root = dlsym(handle, afb_api_so_v3_root);
78         a.desc = dlsym(handle, afb_api_so_v3_desc);
79         a.entry = dlsym(handle, afb_api_so_v3_entry);
80         if (!a.root && !a.desc && !a.entry)
81                 return 0;
82
83         INFO("binding [%s] looks like an AFB binding V3", path);
84
85         /* basic checks */
86         if (!a.root) {
87                 ERROR("binding [%s] incomplete symbol set: %s is missing",
88                         path, afb_api_so_v3_root);
89                 goto error;
90         }
91         if (a.desc) {
92                 if (a.desc->api == NULL || *a.desc->api == 0) {
93                         ERROR("binding [%s] bad api name...", path);
94                         goto error;
95                 }
96                 if (!afb_api_is_valid_name(a.desc->api)) {
97                         ERROR("binding [%s] invalid api name...", path);
98                         goto error;
99                 }
100                 if (!a.entry)
101                         a.entry = a.desc->preinit;
102                 else if (a.desc->preinit) {
103                         ERROR("binding [%s] clash: you can't define %s and %s.preinit, choose only one",
104                                 path, afb_api_so_v3_entry, afb_api_so_v3_desc);
105                         goto error;
106                 }
107
108                 api = afb_api_v3_create(declare_set, call_set, a.desc->api, a.desc->info, a.desc->noconcurrency, init, &a, 0, NULL, path);
109                 if (api)
110                         return 1;
111         } else {
112                 if (!a.entry) {
113                         ERROR("binding [%s] incomplete symbol set: %s is missing",
114                                 path, afb_api_so_v3_entry);
115                         goto error;
116                 }
117
118                 export = afb_export_create_none_for_path(declare_set, call_set, path, init, &a);
119                 if (export) {
120                         /*
121                          *  No call is done to afb_export_unref(export) because:
122                          *   - legacy applications may use the root API emitting messages
123                          *   - it allows writting applications like bindings without API
124                          *  But this has the sad effect to introduce a kind of leak.
125                          *  To avoid this, if necessary further developement should list bindings
126                          *  and their data.
127                          */
128                         return 1;
129                 }
130         }
131
132         ERROR("binding [%s] initialisation failed", path);
133
134 error:
135         return -1;
136 }
137