Launch job at a earlier step
[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                 xreq->sessionflags = (int)verb->session;
147                 xreq->group = desc;
148                 xreq->callback = verb->callback;
149                 afb_xreq_call(xreq);
150         }
151 }
152
153 static int service_start_cb(void *closure, int share_session, int onneed)
154 {
155         int (*start)(const struct afb_binding_interface *interface, struct afb_service service);
156         void (*onevent)(const char *event, struct json_object *object);
157
158         struct api_so_v2 *desc = closure;
159
160         /* check state */
161         if (desc->service != NULL) {
162                 /* not an error when onneed */
163                 if (onneed != 0)
164                         return 0;
165
166                 /* already started: it is an error */
167                 ERROR("Service %s already started", desc->binding->api);
168                 return -1;
169         }
170
171         /* get the initialisation */
172         start = desc->binding->start;
173         if (start == NULL) {
174                 /* not an error when onneed */
175                 if (onneed != 0)
176                         return 0;
177
178                 /* no initialisation method */
179                 ERROR("Binding %s is not a service", desc->binding->api);
180                 return -1;
181         }
182
183         /* get the event handler if any */
184         onevent = desc->binding->onevent;
185         desc->service = afb_svc_create_v2(share_session, onevent, start, &desc->interface);
186         if (desc->service == NULL) {
187                 /* starting error */
188                 ERROR("Starting service %s failed", desc->binding->api);
189                 return -1;
190         }
191
192         return 0;
193 }
194
195 int afb_api_so_v2_add(const char *path, void *handle)
196 {
197         int rc;
198         struct api_so_v2 *desc;
199         struct afb_binding_v2 *binding;
200
201         /* retrieves the register function */
202         binding = dlsym(handle, afb_api_so_v2_descriptor);
203         if (!binding)
204                 return 0;
205
206         INFO("binding [%s] looks like an AFB binding V2", path);
207
208         /* basic checks */
209         if (binding->api == NULL || *binding->api == 0) {
210                 ERROR("binding [%s] bad api name...", path);
211                 goto error;
212         }
213         if (!afb_apis_is_valid_api_name(binding->api)) {
214                 ERROR("binding [%s] invalid api name...", path);
215                 goto error;
216         }
217         if (binding->specification == NULL || *binding->specification == 0) {
218                 ERROR("binding [%s] bad specification...", path);
219                 goto error;
220         }
221         if (binding->verbs == NULL) {
222                 ERROR("binding [%s] no verbs...", path);
223                 goto error;
224         }
225
226         /* allocates the description */
227         desc = calloc(1, sizeof *desc);
228         if (desc == NULL) {
229                 ERROR("out of memory");
230                 goto error;
231         }
232         desc->binding = binding;
233         desc->handle = handle;
234
235         /* init the interface */
236         desc->interface.verbosity = verbosity;
237         desc->interface.mode = AFB_MODE_LOCAL;
238         desc->interface.daemon.itf = &daemon_itf;
239         desc->interface.daemon.closure = desc;
240
241         /* for log purpose, a fake binding is needed here */
242
243         /* init the binding */
244         if (binding->init) {
245                 NOTICE("binding %s [%s] calling init function", binding->api, path);
246                 rc = binding->init(&desc->interface);
247                 if (rc < 0) {
248                         ERROR("binding %s [%s] initialisation function failed...", binding->api, path);
249                         goto error2;
250                 }
251         }
252
253         /* records the binding */
254         desc->apilength = strlen(binding->api);
255         if (afb_apis_add(binding->api, (struct afb_api){
256                         .closure = desc,
257                         .call = call_cb,
258                         .service_start = service_start_cb }) < 0) {
259                 ERROR("binding [%s] can't be registered...", path);
260                 goto error2;
261         }
262         NOTICE("binding %s loaded with API prefix %s", path, binding->api);
263         return 1;
264
265 error2:
266         free(desc);
267 error:
268         return -1;
269 }
270