Fedora 30 packaging fix issu
[src/app-framework-binder.git] / src / afb-api-so-v2.c
1 /*
2  * Copyright (C) 2016, 2017, 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 #include <errno.h>
26
27 #include <json-c/json.h>
28 #include <afb/afb-binding-v2.h>
29
30 #include "afb-api.h"
31 #include "afb-api-so-v2.h"
32 #include "afb-apiset.h"
33 #include "afb-auth.h"
34 #include "afb-export.h"
35 #include "afb-evt.h"
36 #include "afb-context.h"
37 #include "afb-api-so.h"
38 #include "afb-xreq.h"
39 #include "sig-monitor.h"
40 #include "verbose.h"
41
42 /*
43  * names of symbols
44  */
45 static const char afb_api_so_v2_descriptor[] = "afbBindingV2";
46 static const char afb_api_so_v2_data[] = "afbBindingV2data";
47
48 struct preinit
49 {
50         int return_code;
51         const struct afb_binding_v2 *binding;
52 };
53
54 static const struct afb_verb_v2 *search(const struct afb_binding_v2 *binding, const char *name)
55 {
56         const struct afb_verb_v2 *verb;
57
58         verb = binding->verbs;
59         while (verb->verb && strcasecmp(verb->verb, name))
60                 verb++;
61         return verb->verb ? verb : NULL;
62         return NULL;
63 }
64
65 void afb_api_so_v2_process_call(const struct afb_binding_v2 *binding, struct afb_xreq *xreq)
66 {
67         const struct afb_verb_v2 *verb;
68
69         verb = search(binding, xreq->request.called_verb);
70         afb_xreq_call_verb_v2(xreq, verb);
71 }
72
73 struct json_object *afb_api_so_v2_make_description_openAPIv3(const struct afb_binding_v2 *binding, const char *apiname)
74 {
75         char buffer[256];
76         const struct afb_verb_v2 *verb;
77         struct json_object *r, *f, *a, *i, *p, *g;
78         enum json_tokener_error jerr;
79
80         if (binding->specification) {
81                 r = json_tokener_parse_verbose(binding->specification, &jerr);
82                 if (jerr == json_tokener_success)
83                         return r;
84         }
85
86         r = json_object_new_object();
87         json_object_object_add(r, "openapi", json_object_new_string("3.0.0"));
88
89         i = json_object_new_object();
90         json_object_object_add(r, "info", i);
91         json_object_object_add(i, "title", json_object_new_string(apiname));
92         json_object_object_add(i, "version", json_object_new_string("0.0.0"));
93         json_object_object_add(i, "description", json_object_new_string(binding->info ?: apiname));
94
95         p = json_object_new_object();
96         json_object_object_add(r, "paths", p);
97         verb = binding->verbs;
98         while (verb->verb) {
99                 buffer[0] = '/';
100                 strncpy(buffer + 1, verb->verb, sizeof buffer - 1);
101                 buffer[sizeof buffer - 1] = 0;
102                 f = json_object_new_object();
103                 json_object_object_add(p, buffer, f);
104                 g = json_object_new_object();
105                 json_object_object_add(f, "get", g);
106
107                 a = afb_auth_json_v2(verb->auth, verb->session);
108                 if (a)
109                         json_object_object_add(g, "x-permissions", a);
110
111                 a = json_object_new_object();
112                 json_object_object_add(g, "responses", a);
113                 f = json_object_new_object();
114                 json_object_object_add(a, "200", f);
115                 json_object_object_add(f, "description", json_object_new_string(verb->info?:verb->verb));
116                 verb++;
117         }
118         return r;
119 }
120
121 static void do_preinit(int sig, void *closure)
122 {
123         struct preinit *preinit = closure;
124
125         if (!sig)
126                 preinit->return_code = preinit->binding->preinit();
127         else {
128                 errno = EINTR;
129                 preinit->return_code = -1;
130         }
131 };
132
133 int afb_api_so_v2_add_binding(
134                 const struct afb_binding_v2 *binding,
135                 void *handle,
136                 struct afb_apiset *declare_set,
137                 struct afb_apiset * call_set,
138                 struct afb_binding_data_v2 *data,
139                 const char *path)
140 {
141         int rc;
142         struct afb_export *export;
143         struct preinit preinit;
144
145         /* basic checks */
146         assert(binding);
147         assert(binding->api);
148         assert(binding->verbs);
149         assert(data);
150
151         /* allocates the description */
152         export = afb_export_create_v2(declare_set, call_set, binding->api, binding, data, binding->init, binding->onevent, path);
153         if (!export) {
154                 ERROR("out of memory");
155                 goto error;
156         }
157
158         /* records the binding */
159         if (afb_export_declare(export, binding->noconcurrency) < 0) {
160                 ERROR("binding %s can't be registered to set %s...", afb_export_apiname(export), afb_apiset_name(declare_set));
161                 goto error;
162         }
163         /* init the binding */
164         if (binding->preinit) {
165                 INFO("binding %s calling preinit function", binding->api);
166                 preinit.binding = binding;
167                 sig_monitor(0, do_preinit, &preinit);
168                 rc = preinit.return_code;
169                 if (rc < 0) {
170                         ERROR("binding %s preinit function failed...", afb_export_apiname(export));
171                         afb_export_undeclare(export);
172                         goto error;
173                 }
174         }
175
176         INFO("binding %s added to set %s", afb_export_apiname(export), afb_apiset_name(declare_set));
177         return 1;
178
179 error:
180         afb_export_unref(export);
181
182         return -1;
183 }
184
185 int afb_api_so_v2_add(const char *path, void *handle, struct afb_apiset *declare_set, struct afb_apiset * call_set)
186 {
187         const struct afb_binding_v2 *binding;
188         struct afb_binding_data_v2 *data;
189
190         /* retrieves the register function */
191         binding = dlsym(handle, afb_api_so_v2_descriptor);
192         data = dlsym(handle, afb_api_so_v2_data);
193         if (!binding && !data)
194                 return 0;
195
196         INFO("binding [%s] looks like an AFB binding V2", path);
197
198         /* basic checks */
199         if (!binding || !data) {
200                 ERROR("binding [%s] incomplete symbol set: %s is missing",
201                         path, binding ? afb_api_so_v2_data : afb_api_so_v2_descriptor);
202                 goto error;
203         }
204         if (binding->api == NULL || *binding->api == 0) {
205                 ERROR("binding [%s] bad api name...", path);
206                 goto error;
207         }
208         if (!afb_api_is_valid_name(binding->api)) {
209                 ERROR("binding [%s] invalid api name...", path);
210                 goto error;
211         }
212
213         if (binding->verbs == NULL) {
214                 ERROR("binding [%s] no verbs...", path);
215                 goto error;
216         }
217
218         return afb_api_so_v2_add_binding(binding, handle, declare_set, call_set, data, path);
219
220  error:
221         return -1;
222 }
223