Prepare move to bindings version 2
[src/app-framework-binder.git] / src / afb-api-so-v1.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 NO_BINDING_VERBOSE_MACRO
20
21 #include <string.h>
22 #include <dlfcn.h>
23 #include <assert.h>
24
25 #include <afb/afb-binding.h>
26
27 #include "afb-apis.h"
28 #include "afb-svc.h"
29 #include "afb-evt.h"
30 #include "afb-common.h"
31 #include "afb-context.h"
32 #include "afb-api-so.h"
33 #include "afb-thread.h"
34 #include "verbose.h"
35
36 /*
37  * names of symbols
38  */
39 static const char afb_api_so_v1_register[] = "afbBindingV1Register";
40 static const char afb_api_so_v1_service_init[] = "afbBindingV1ServiceInit";
41 static const char afb_api_so_v1_service_event[] = "afbBindingV1ServiceEvent";
42
43 /*
44  * Description of a binding
45  */
46 struct api_so_v1 {
47         struct afb_binding *binding;    /* descriptor */
48         size_t apilength;               /* length of the API name */
49         void *handle;                   /* context of dlopen */
50         struct afb_svc *service;        /* handler for service started */
51         struct afb_binding_interface interface; /* interface for the binding */
52 };
53
54 static struct afb_event afb_api_so_event_make_cb(void *closure, const char *name);
55 static int afb_api_so_event_broadcast_cb(void *closure, const char *name, struct json_object *object);
56 static void afb_api_so_vverbose_cb(void *closure, int level, const char *file, int line, const char *fmt, va_list args);
57 static int afb_api_so_rootdir_get_fd(void *closure);
58 static int afb_api_so_rootdir_open_locale(void *closure, const char *filename, int flags, const char *locale);
59
60 static const struct afb_daemon_itf daemon_itf = {
61         .event_broadcast = afb_api_so_event_broadcast_cb,
62         .get_event_loop = afb_common_get_event_loop,
63         .get_user_bus = afb_common_get_user_bus,
64         .get_system_bus = afb_common_get_system_bus,
65         .vverbose = afb_api_so_vverbose_cb,
66         .event_make = afb_api_so_event_make_cb,
67         .rootdir_get_fd = afb_api_so_rootdir_get_fd,
68         .rootdir_open_locale = afb_api_so_rootdir_open_locale
69 };
70
71 static struct afb_event afb_api_so_event_make_cb(void *closure, const char *name)
72 {
73         size_t length;
74         char *event;
75         struct api_so_v1 *desc = closure;
76
77         /* makes the event name */
78         assert(desc->binding != NULL);
79         length = strlen(name);
80         event = alloca(length + 2 + desc->apilength);
81         memcpy(event, desc->binding->v1.prefix, desc->apilength);
82         event[desc->apilength] = '/';
83         memcpy(event + desc->apilength + 1, name, length + 1);
84
85         /* crate the event */
86         return afb_evt_create_event(event);
87 }
88
89 static int afb_api_so_event_broadcast_cb(void *closure, const char *name, struct json_object *object)
90 {
91         size_t length;
92         char *event;
93         struct api_so_v1 *desc = closure;
94
95         /* makes the event name */
96         assert(desc->binding != NULL);
97         length = strlen(name);
98         event = alloca(length + 2 + desc->apilength);
99         memcpy(event, desc->binding->v1.prefix, desc->apilength);
100         event[desc->apilength] = '/';
101         memcpy(event + desc->apilength + 1, name, length + 1);
102
103         return afb_evt_broadcast(event, object);
104 }
105
106 static void afb_api_so_vverbose_cb(void *closure, int level, const char *file, int line, const char *fmt, va_list args)
107 {
108         char *p;
109         struct api_so_v1 *desc = closure;
110
111         if (vasprintf(&p, fmt, args) < 0)
112                 vverbose(level, file, line, fmt, args);
113         else {
114                 verbose(level, file, line, "%s {binding %s}", p, desc->binding->v1.prefix);
115                 free(p);
116         }
117 }
118
119 static int afb_api_so_rootdir_get_fd(void *closure)
120 {
121         return afb_common_rootdir_get_fd();
122 }
123
124 static int afb_api_so_rootdir_open_locale(void *closure, const char *filename, int flags, const char *locale)
125 {
126         return afb_common_rootdir_open_locale(filename, flags, locale);
127 }
128
129 static int call_check(struct afb_req req, struct afb_context *context, const struct afb_verb_desc_v1 *verb)
130 {
131         int stag = (int)verb->session;
132
133         if ((stag & (AFB_SESSION_CREATE|AFB_SESSION_CLOSE|AFB_SESSION_RENEW|AFB_SESSION_CHECK|AFB_SESSION_LOA_EQ)) != 0) {
134                 if (!afb_context_check(context)) {
135                         afb_context_close(context);
136                         afb_req_fail(req, "failed", "invalid token's identity");
137                         return 0;
138                 }
139         }
140
141         if ((stag & AFB_SESSION_CREATE) != 0) {
142                 if (afb_context_check_loa(context, 1)) {
143                         afb_req_fail(req, "failed", "invalid creation state");
144                         return 0;
145                 }
146                 afb_context_change_loa(context, 1);
147                 afb_context_refresh(context);
148         }
149
150         if ((stag & (AFB_SESSION_CREATE | AFB_SESSION_RENEW)) != 0)
151                 afb_context_refresh(context);
152
153         if ((stag & AFB_SESSION_CLOSE) != 0) {
154                 afb_context_change_loa(context, 0);
155                 afb_context_close(context);
156         }
157
158         if ((stag & AFB_SESSION_LOA_GE) != 0) {
159                 int loa = (stag >> AFB_SESSION_LOA_SHIFT) & AFB_SESSION_LOA_MASK;
160                 if (!afb_context_check_loa(context, loa)) {
161                         afb_req_fail(req, "failed", "invalid LOA");
162                         return 0;
163                 }
164         }
165
166         if ((stag & AFB_SESSION_LOA_LE) != 0) {
167                 int loa = (stag >> AFB_SESSION_LOA_SHIFT) & AFB_SESSION_LOA_MASK;
168                 if (afb_context_check_loa(context, loa + 1)) {
169                         afb_req_fail(req, "failed", "invalid LOA");
170                         return 0;
171                 }
172         }
173         return 1;
174 }
175
176 static void call_cb(void *closure, struct afb_req req, struct afb_context *context, const char *strverb)
177 {
178         const struct afb_verb_desc_v1 *verb;
179         struct api_so_v1 *desc = closure;
180
181         verb = desc->binding->v1.verbs;
182         while (verb->name && strcasecmp(verb->name, strverb))
183                 verb++;
184         if (!verb->name)
185                 afb_req_fail_f(req, "unknown-verb", "verb %s unknown within api %s", strverb, desc->binding->v1.prefix);
186         else if (call_check(req, context, verb)) {
187                 afb_thread_req_call(req, verb->callback, afb_api_so_timeout, desc);
188         }
189 }
190
191 static int service_start_cb(void *closure, int share_session, int onneed)
192 {
193         int (*init)(struct afb_service service);
194         void (*onevent)(const char *event, struct json_object *object);
195
196         struct api_so_v1 *desc = closure;
197
198         /* check state */
199         if (desc->service != NULL) {
200                 /* not an error when onneed */
201                 if (onneed != 0)
202                         return 0;
203
204                 /* already started: it is an error */
205                 ERROR("Service %s already started", desc->binding->v1.prefix);
206                 return -1;
207         }
208
209         /* get the initialisation */
210         init = dlsym(desc->handle, afb_api_so_v1_service_init);
211         if (init == NULL) {
212                 /* not an error when onneed */
213                 if (onneed != 0)
214                         return 0;
215
216                 /* no initialisation method */
217                 ERROR("Binding %s is not a service", desc->binding->v1.prefix);
218                 return -1;
219         }
220
221         /* get the event handler if any */
222         onevent = dlsym(desc->handle, afb_api_so_v1_service_event);
223         desc->service = afb_svc_create(share_session, init, onevent);
224         if (desc->service == NULL) {
225                 /* starting error */
226                 ERROR("Starting service %s failed", desc->binding->v1.prefix);
227                 return -1;
228         }
229
230         return 0;
231 }
232
233 int afb_api_so_v1_add(const char *path, void *handle)
234 {
235         struct api_so_v1 *desc;
236         struct afb_binding *(*register_function) (const struct afb_binding_interface *interface);
237         struct afb_verb_desc_v1 fake_verb;
238         struct afb_binding fake_binding;
239
240         /* retrieves the register function */
241         register_function = dlsym(handle, afb_api_so_v1_register);
242         if (!register_function)
243                 return 0;
244         INFO("binding [%s] is a valid AFB binding V1", path);
245
246         /* allocates the description */
247         desc = calloc(1, sizeof *desc);
248         if (desc == NULL) {
249                 ERROR("out of memory");
250                 goto error;
251         }
252         desc->handle = handle;
253
254         /* init the interface */
255         desc->interface.verbosity = verbosity;
256         desc->interface.mode = AFB_MODE_LOCAL;
257         desc->interface.daemon.itf = &daemon_itf;
258         desc->interface.daemon.closure = desc;
259
260         /* for log purpose, a fake binding is needed here */
261         desc->binding = &fake_binding;
262         fake_binding.type = AFB_BINDING_VERSION_1;
263         fake_binding.v1.info = path;
264         fake_binding.v1.prefix = path;
265         fake_binding.v1.verbs = &fake_verb;
266         fake_verb.name = NULL;
267
268         /* init the binding */
269         NOTICE("binding [%s] calling registering function %s", path, afb_api_so_v1_register);
270         desc->binding = register_function(&desc->interface);
271         if (desc->binding == NULL) {
272                 ERROR("binding [%s] register function failed. continuing...", path);
273                 goto error2;
274         }
275
276         /* check the returned structure */
277         if (desc->binding->type != AFB_BINDING_VERSION_1) {
278                 ERROR("binding [%s] invalid type %d...", path, desc->binding->type);
279                 goto error2;
280         }
281         if (desc->binding->v1.prefix == NULL || *desc->binding->v1.prefix == 0) {
282                 ERROR("binding [%s] bad prefix...", path);
283                 goto error2;
284         }
285         if (!afb_apis_is_valid_api_name(desc->binding->v1.prefix)) {
286                 ERROR("binding [%s] invalid prefix...", path);
287                 goto error2;
288         }
289         if (desc->binding->v1.info == NULL || *desc->binding->v1.info == 0) {
290                 ERROR("binding [%s] bad description...", path);
291                 goto error2;
292         }
293         if (desc->binding->v1.verbs == NULL) {
294                 ERROR("binding [%s] no APIs...", path);
295                 goto error2;
296         }
297
298         /* records the binding */
299         desc->apilength = strlen(desc->binding->v1.prefix);
300         if (afb_apis_add(desc->binding->v1.prefix, (struct afb_api){
301                         .closure = desc,
302                         .call = call_cb,
303                         .service_start = service_start_cb }) < 0) {
304                 ERROR("binding [%s] can't be registered...", path);
305                 goto error2;
306         }
307         NOTICE("binding %s loaded with API prefix %s", path, desc->binding->v1.prefix);
308         return 1;
309
310 error2:
311         free(desc);
312 error:
313         return -1;
314 }
315