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