Ensure that subcall_sync use subcall
[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-xreq.h"
34 #include "verbose.h"
35
36 /*
37  * names of symbols
38  */
39 static const char afb_api_so_v2_descriptor[] = "afbBindingV2";
40
41 /*
42  * Description of a binding
43  */
44 struct api_so_v2 {
45         struct afb_binding_v2 *binding; /* descriptor */
46         size_t apilength;               /* length of the API name */
47         void *handle;                   /* context of dlopen */
48         struct afb_svc *service;        /* handler for service started */
49         struct afb_binding_interface interface; /* interface for the binding */
50 };
51
52 static struct afb_event afb_api_so_event_make_cb(void *closure, const char *name);
53 static int afb_api_so_event_broadcast_cb(void *closure, const char *name, struct json_object *object);
54 static void afb_api_so_vverbose_cb(void *closure, int level, const char *file, int line, const char *fmt, va_list args);
55 static int afb_api_so_rootdir_get_fd(void *closure);
56 static int afb_api_so_rootdir_open_locale(void *closure, const char *filename, int flags, const char *locale);
57
58 static const struct afb_daemon_itf daemon_itf = {
59         .event_broadcast = afb_api_so_event_broadcast_cb,
60         .get_event_loop = afb_common_get_event_loop,
61         .get_user_bus = afb_common_get_user_bus,
62         .get_system_bus = afb_common_get_system_bus,
63         .vverbose = afb_api_so_vverbose_cb,
64         .event_make = afb_api_so_event_make_cb,
65         .rootdir_get_fd = afb_api_so_rootdir_get_fd,
66         .rootdir_open_locale = afb_api_so_rootdir_open_locale
67 };
68
69 static struct afb_event afb_api_so_event_make_cb(void *closure, const char *name)
70 {
71         size_t length;
72         char *event;
73         struct api_so_v2 *desc = closure;
74
75         /* makes the event name */
76         assert(desc->binding != NULL);
77         length = strlen(name);
78         event = alloca(length + 2 + desc->apilength);
79         memcpy(event, desc->binding->api, desc->apilength);
80         event[desc->apilength] = '/';
81         memcpy(event + desc->apilength + 1, name, length + 1);
82
83         /* crate the event */
84         return afb_evt_create_event(event);
85 }
86
87 static int afb_api_so_event_broadcast_cb(void *closure, const char *name, struct json_object *object)
88 {
89         size_t length;
90         char *event;
91         struct api_so_v2 *desc = closure;
92
93         /* makes the event name */
94         assert(desc->binding != NULL);
95         length = strlen(name);
96         event = alloca(length + 2 + desc->apilength);
97         memcpy(event, desc->binding->api, desc->apilength);
98         event[desc->apilength] = '/';
99         memcpy(event + desc->apilength + 1, name, length + 1);
100
101         return afb_evt_broadcast(event, object);
102 }
103
104 static void afb_api_so_vverbose_cb(void *closure, int level, const char *file, int line, const char *fmt, va_list args)
105 {
106         char *p;
107         struct api_so_v2 *desc = closure;
108
109         if (vasprintf(&p, fmt, args) < 0)
110                 vverbose(level, file, line, fmt, args);
111         else {
112                 verbose(level, file, line, "%s {binding %s}", p, desc->binding->api);
113                 free(p);
114         }
115 }
116
117 static int afb_api_so_rootdir_get_fd(void *closure)
118 {
119         return afb_common_rootdir_get_fd();
120 }
121
122 static int afb_api_so_rootdir_open_locale(void *closure, const char *filename, int flags, const char *locale)
123 {
124         return afb_common_rootdir_open_locale(filename, flags, locale);
125 }
126
127 static const struct afb_verb_v2 *search(struct api_so_v2 *desc, const char *verb)
128 {
129         const struct afb_verb_v2 *result;
130
131         result = desc->binding->verbs;
132         while (result->verb && strcasecmp(result->verb, verb))
133                 result++;
134         return result->verb ? result : NULL;
135 }
136
137 static void call_cb(void *closure, struct afb_xreq *xreq)
138 {
139         struct api_so_v2 *desc = closure;
140         const struct afb_verb_v2 *verb;
141
142         verb = search(desc, xreq->verb);
143         if (!verb)
144                 afb_xreq_fail_f(xreq, "unknown-verb", "verb %s unknown within api %s", xreq->verb, desc->binding->api);
145         else
146                 afb_xreq_call(xreq, verb->session, verb->callback);
147 }
148
149 static int service_start_cb(void *closure, int share_session, int onneed)
150 {
151         int (*start)(const struct afb_binding_interface *interface, struct afb_service service);
152         void (*onevent)(const char *event, struct json_object *object);
153
154         struct api_so_v2 *desc = closure;
155
156         /* check state */
157         if (desc->service != NULL) {
158                 /* not an error when onneed */
159                 if (onneed != 0)
160                         return 0;
161
162                 /* already started: it is an error */
163                 ERROR("Service %s already started", desc->binding->api);
164                 return -1;
165         }
166
167         /* get the initialisation */
168         start = desc->binding->start;
169         if (start == NULL) {
170                 /* not an error when onneed */
171                 if (onneed != 0)
172                         return 0;
173
174                 /* no initialisation method */
175                 ERROR("Binding %s is not a service", desc->binding->api);
176                 return -1;
177         }
178
179         /* get the event handler if any */
180         onevent = desc->binding->onevent;
181         desc->service = afb_svc_create_v2(share_session, onevent, start, &desc->interface);
182         if (desc->service == NULL) {
183                 /* starting error */
184                 ERROR("Starting service %s failed", desc->binding->api);
185                 return -1;
186         }
187
188         return 0;
189 }
190
191 int afb_api_so_v2_add(const char *path, void *handle)
192 {
193         int rc;
194         struct api_so_v2 *desc;
195         struct afb_binding_v2 *binding;
196
197         /* retrieves the register function */
198         binding = dlsym(handle, afb_api_so_v2_descriptor);
199         if (!binding)
200                 return 0;
201
202         INFO("binding [%s] looks like an AFB binding V2", path);
203
204         /* basic checks */
205         if (binding->api == NULL || *binding->api == 0) {
206                 ERROR("binding [%s] bad api name...", path);
207                 goto error;
208         }
209         if (!afb_apis_is_valid_api_name(binding->api)) {
210                 ERROR("binding [%s] invalid api name...", path);
211                 goto error;
212         }
213         if (binding->specification == NULL || *binding->specification == 0) {
214                 ERROR("binding [%s] bad specification...", path);
215                 goto error;
216         }
217         if (binding->verbs == NULL) {
218                 ERROR("binding [%s] no verbs...", path);
219                 goto error;
220         }
221
222         /* allocates the description */
223         desc = calloc(1, sizeof *desc);
224         if (desc == NULL) {
225                 ERROR("out of memory");
226                 goto error;
227         }
228         desc->binding = binding;
229         desc->handle = handle;
230
231         /* init the interface */
232         desc->interface.verbosity = verbosity;
233         desc->interface.mode = AFB_MODE_LOCAL;
234         desc->interface.daemon.itf = &daemon_itf;
235         desc->interface.daemon.closure = desc;
236
237         /* for log purpose, a fake binding is needed here */
238
239         /* init the binding */
240         if (binding->init) {
241                 NOTICE("binding %s [%s] calling init function", binding->api, path);
242                 rc = binding->init(&desc->interface);
243                 if (rc < 0) {
244                         ERROR("binding %s [%s] initialisation function failed...", binding->api, path);
245                         goto error2;
246                 }
247         }
248
249         /* records the binding */
250         desc->apilength = strlen(binding->api);
251         if (afb_apis_add(binding->api, (struct afb_api){
252                         .closure = desc,
253                         .call = call_cb,
254                         .service_start = service_start_cb }) < 0) {
255                 ERROR("binding [%s] can't be registered...", path);
256                 goto error2;
257         }
258         NOTICE("binding %s loaded with API prefix %s", path, binding->api);
259         return 1;
260
261 error2:
262         free(desc);
263 error:
264         return -1;
265 }
266