afb-svc: make service existing during its initialisation
[src/app-framework-binder.git] / src / afb-api-so-v2.c
1 /*
2  * Copyright (C) 2016, 2017 "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 #define AFB_BINDING_PRAGMA_NO_VERBOSE_MACRO
20
21 #include <stdlib.h>
22 #include <string.h>
23 #include <dlfcn.h>
24 #include <assert.h>
25
26 #include <afb/afb-binding-v2.h>
27 #include <json-c/json.h>
28
29 #include "afb-api.h"
30 #include "afb-api-so-v2.h"
31 #include "afb-apiset.h"
32 #include "afb-svc.h"
33 #include "afb-ditf.h"
34 #include "afb-evt.h"
35 #include "afb-common.h"
36 #include "afb-context.h"
37 #include "afb-api-so.h"
38 #include "afb-xreq.h"
39 #include "jobs.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 /*
49  * Description of a binding
50  */
51 struct api_so_v2 {
52         const struct afb_binding_v2 *binding;   /* descriptor */
53         struct afb_binding_data_v2 *data;       /* data */
54         void *handle;                   /* context of dlopen */
55         struct afb_svc *service;        /* handler for service started */
56         struct afb_ditf ditf;           /* daemon interface */
57 };
58
59 static const struct afb_verb_v2 *search(struct api_so_v2 *desc, const char *name)
60 {
61         const struct afb_verb_v2 *verb;
62
63         verb = desc->binding->verbs;
64         while (verb->verb && strcasecmp(verb->verb, name))
65                 verb++;
66         return verb->verb ? verb : NULL;
67         return NULL;
68 }
69
70 static void call_cb(void *closure, struct afb_xreq *xreq)
71 {
72         struct api_so_v2 *desc = closure;
73         const struct afb_verb_v2 *verb;
74
75         verb = search(desc, xreq->verb);
76         afb_xreq_call_verb_v2(xreq, verb);
77 }
78
79 struct call_sync
80 {
81         struct api_so_v2 *desc;
82         struct afb_xreq *xreq;
83 };
84
85 static void call_sync_cb_cb(int signum, void *closure)
86 {
87         struct call_sync *cs = closure;
88         if (!signum)
89                 call_cb(cs->desc, cs->xreq);
90         else  {
91                 if (!cs->xreq->replied)
92                         afb_xreq_fail(cs->xreq, "aborted", "internal error");
93         }
94 }
95
96 static void call_sync_cb(void *closure, struct afb_xreq *xreq)
97 {
98         struct call_sync cs = { .desc = closure, .xreq = xreq };
99
100         if (jobs_call(closure, 0, call_sync_cb_cb, &cs))
101                 call_cb(closure, xreq);
102 }
103
104 static int service_start_cb(void *closure, int share_session, int onneed, struct afb_apiset *apiset)
105 {
106         int rc;
107         int (*start)();
108         void (*onevent)(const char *event, struct json_object *object);
109
110         struct api_so_v2 *desc = closure;
111
112         /* check state */
113         if (desc->service != NULL) {
114                 /* not an error when onneed */
115                 if (onneed != 0)
116                         return 0;
117
118                 /* already started: it is an error */
119                 ERROR("Service %s already started", desc->binding->api);
120                 return -1;
121         }
122
123         /* get the initialisation */
124         start = desc->binding->init;
125         onevent = desc->binding->onevent;
126         if (start == NULL && onevent == NULL) {
127                 /* not an error when onneed */
128                 if (onneed != 0)
129                         return 0;
130
131                 /* no initialisation method */
132                 ERROR("Binding %s is not a service", desc->binding->api);
133                 return -1;
134         }
135
136         /* get the event handler if any */
137         desc->service = afb_svc_create(desc->binding->api, apiset, share_session, onevent, &desc->data->service);
138         if (desc->service == NULL) {
139                 /* starting error */
140                 ERROR("Starting service %s failed", desc->binding->api);
141                 return -1;
142         }
143
144         /* Starts the service */
145         rc = afb_svc_start_v2(desc->service, start);
146         if (rc < 0) {
147                 /* initialisation error */
148                 ERROR("Initialisation of service %s failed (%d): %m", desc->binding->api, rc);
149                 afb_svc_destroy(desc->service, &desc->data->service);
150                 desc->service = NULL;
151                 return rc;
152         }
153
154
155         return 0;
156 }
157
158 static void update_hooks_cb(void *closure)
159 {
160         struct api_so_v2 *desc = closure;
161         afb_ditf_update_hook(&desc->ditf);
162         if (desc->service)
163                 afb_svc_update_hook(desc->service);
164 }
165
166 static int get_verbosity_cb(void *closure)
167 {
168         struct api_so_v2 *desc = closure;
169         return desc->data->verbosity;
170 }
171
172 static void set_verbosity_cb(void *closure, int level)
173 {
174         struct api_so_v2 *desc = closure;
175         desc->data->verbosity = level;
176 }
177
178 static struct json_object *addperm(struct json_object *o, struct json_object *x)
179 {
180         struct json_object *a;
181
182         if (!o)
183                 return x;
184
185         if (!json_object_object_get_ex(o, "allOf", &a)) {
186                 a = json_object_new_array();
187                 json_object_array_add(a, o);
188                 o = json_object_new_object();
189                 json_object_object_add(o, "allOf", a);
190         }
191         json_object_array_add(a, x);
192         return o;
193 }
194
195 static struct json_object *addperm_key_val(struct json_object *o, const char *key, struct json_object *val)
196 {
197         struct json_object *x = json_object_new_object();
198         json_object_object_add(x, key, val);
199         return addperm(o, x);
200 }
201
202 static struct json_object *addperm_key_valstr(struct json_object *o, const char *key, const char *val)
203 {
204         return addperm_key_val(o, key, json_object_new_string(val));
205 }
206
207 static struct json_object *addperm_key_valint(struct json_object *o, const char *key, int val)
208 {
209         return addperm_key_val(o, key, json_object_new_int(val));
210 }
211
212 static struct json_object *make_description_openAPIv3(struct api_so_v2 *desc)
213 {
214         char buffer[256];
215         const struct afb_verb_v2 *verb;
216         struct json_object *r, *f, *a, *i, *p, *g;
217
218         r = json_object_new_object();
219         json_object_object_add(r, "openapi", json_object_new_string("3.0.0"));
220
221         i = json_object_new_object();
222         json_object_object_add(r, "info", i);
223         json_object_object_add(i, "title", json_object_new_string(desc->binding->api));
224         json_object_object_add(i, "version", json_object_new_string("0.0.0"));
225         json_object_object_add(i, "description", json_object_new_string(desc->binding->info ?: desc->binding->api));
226
227         p = json_object_new_object();
228         json_object_object_add(r, "paths", p);
229         verb = desc->binding->verbs;
230         while (verb->verb) {
231                 buffer[0] = '/';
232                 strncpy(buffer + 1, verb->verb, sizeof buffer - 1);
233                 buffer[sizeof buffer - 1] = 0;
234                 f = json_object_new_object();
235                 json_object_object_add(p, buffer, f);
236                 g = json_object_new_object();
237                 json_object_object_add(f, "get", g);
238
239                 a = NULL;
240                 if (verb->session & AFB_SESSION_CLOSE_V2)
241                         a = addperm_key_valstr(a, "session", "close");
242                 if (verb->session & AFB_SESSION_CHECK_V2)
243                         a = addperm_key_valstr(a, "session", "check");
244                 if (verb->session & AFB_SESSION_REFRESH_V2)
245                         a = addperm_key_valstr(a, "token", "refresh");
246                 if (verb->session & AFB_SESSION_LOA_MASK_V2)
247                         a = addperm_key_valint(a, "LOA", verb->session & AFB_SESSION_LOA_MASK_V2);
248 #if 0
249                 if (verb->auth)
250                         a = 
251 #endif
252                 if (a)
253                         json_object_object_add(g, "x-permissions", a);
254
255                 a = json_object_new_object();
256                 json_object_object_add(g, "responses", a);
257                 f = json_object_new_object();
258                 json_object_object_add(a, "200", f);
259                 json_object_object_add(f, "description", json_object_new_string(verb->info?:verb->verb));
260                 verb++;
261         }
262         return r;
263 }
264
265 static struct json_object *describe_cb(void *closure)
266 {
267         struct api_so_v2 *desc = closure;
268         struct json_object *r = desc->binding->specification ? json_tokener_parse(desc->binding->specification) : NULL;
269         if (!r)
270                 r = make_description_openAPIv3(desc);
271         return r;
272 }
273
274 static struct afb_api_itf so_v2_api_itf = {
275         .call = call_cb,
276         .service_start = service_start_cb,
277         .update_hooks = update_hooks_cb,
278         .get_verbosity = get_verbosity_cb,
279         .set_verbosity = set_verbosity_cb,
280         .describe = describe_cb
281 };
282
283 static struct afb_api_itf so_v2_sync_api_itf = {
284         .call = call_sync_cb,
285         .service_start = service_start_cb,
286         .update_hooks = update_hooks_cb,
287         .get_verbosity = get_verbosity_cb,
288         .set_verbosity = set_verbosity_cb,
289         .describe = describe_cb
290 };
291
292 int afb_api_so_v2_add_binding(const struct afb_binding_v2 *binding, void *handle, struct afb_apiset *apiset, struct afb_binding_data_v2 *data)
293 {
294         int rc;
295         struct api_so_v2 *desc;
296         struct afb_api afb_api;
297
298         /* basic checks */
299         assert(binding);
300         assert(binding->api);
301         assert(binding->verbs);
302         assert(data);
303
304         /* allocates the description */
305         desc = calloc(1, sizeof *desc);
306         if (desc == NULL) {
307                 ERROR("out of memory");
308                 goto error;
309         }
310         desc->binding = binding;
311         desc->data = data;
312         desc->handle = handle;
313         desc->service = NULL;
314
315         /* init the interface */
316         desc->data->verbosity = verbosity;
317         afb_ditf_init_v2(&desc->ditf, binding->api, data);
318
319         /* init the binding */
320         if (binding->preinit) {
321                 INFO("binding %s calling preinit function", binding->api);
322                 rc = binding->preinit();
323                 if (rc < 0) {
324                         ERROR("binding %s preinit function failed...", binding->api);
325                         goto error2;
326                 }
327         }
328
329         /* records the binding */
330         afb_api.closure = desc;
331         afb_api.itf = binding->noconcurrency ? &so_v2_sync_api_itf : &so_v2_api_itf;
332         if (afb_apiset_add(apiset, binding->api, afb_api) < 0) {
333                 ERROR("binding %s can't be registered to set %s...", binding->api, afb_apiset_name(apiset));
334                 goto error2;
335         }
336         INFO("binding %s added to set %s", binding->api, afb_apiset_name(apiset));
337         return 1;
338
339 error2:
340         free(desc);
341 error:
342         return -1;
343 }
344
345 int afb_api_so_v2_add(const char *path, void *handle, struct afb_apiset *apiset)
346 {
347         const struct afb_binding_v2 *binding;
348         struct afb_binding_data_v2 *data;
349
350         /* retrieves the register function */
351         binding = dlsym(handle, afb_api_so_v2_descriptor);
352         data = dlsym(handle, afb_api_so_v2_data);
353         if (!binding && !data)
354                 return 0;
355
356         INFO("binding [%s] looks like an AFB binding V2", path);
357
358         /* basic checks */
359         if (!binding || !data) {
360                 ERROR("binding [%s] incomplete symbol set: %s is missing",
361                         path, binding ? afb_api_so_v2_data : afb_api_so_v2_descriptor);
362                 goto error;
363         }
364         if (binding->api == NULL || *binding->api == 0) {
365                 ERROR("binding [%s] bad api name...", path);
366                 goto error;
367         }
368         if (!afb_api_is_valid_name(binding->api)) {
369                 ERROR("binding [%s] invalid api name...", path);
370                 goto error;
371         }
372 #if 0
373         if (binding->specification == NULL || *binding->specification == 0) {
374                 ERROR("binding [%s] bad specification...", path);
375                 goto error;
376         }
377 #endif
378         if (binding->verbs == NULL) {
379                 ERROR("binding [%s] no verbs...", path);
380                 goto error;
381         }
382
383         return afb_api_so_v2_add_binding(binding, handle, apiset, data);
384
385  error:
386         return -1;
387 }
388