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