2 * Copyright (C) 2016 "IoT.bzh"
3 * Author José Bollo <jose.bollo@iot.bzh>
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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 #define NO_BINDING_VERBOSE_MACRO
28 #include <sys/types.h>
31 #include <afb/afb-binding.h>
32 #include <afb/afb-req-itf.h>
33 #include <afb/afb-event-itf.h>
36 #include "afb-common.h"
37 #include "afb-context.h"
39 #include "afb-api-so.h"
40 #include "afb-sig-handler.h"
46 * Description of a binding
49 struct afb_binding *binding; /* descriptor */
50 size_t apilength; /* length of the API name */
51 void *handle; /* context of dlopen */
52 struct afb_svc *service; /* handler for service started */
53 struct afb_binding_interface interface; /* interface for the binding */
58 void (*action)(struct afb_req);
61 static const char binding_register_function_v1[] = "afbBindingV1Register";
62 static const char binding_service_init_function_v1[] = "afbBindingV1ServiceInit";
63 static const char binding_service_event_function_v1[] = "afbBindingV1ServiceEvent";
65 static int api_timeout = 15;
67 static struct afb_event afb_api_so_event_make(struct api_so_desc *desc, const char *name);
68 static int afb_api_so_event_broadcast(struct api_so_desc *desc, const char *name, struct json_object *object);
69 static void afb_api_so_vverbose(struct api_so_desc *desc, int level, const char *file, int line, const char *fmt, va_list args);
71 static const struct afb_daemon_itf daemon_itf = {
72 .event_broadcast = (void*)afb_api_so_event_broadcast,
73 .get_event_loop = (void*)afb_common_get_event_loop,
74 .get_user_bus = (void*)afb_common_get_user_bus,
75 .get_system_bus = (void*)afb_common_get_system_bus,
76 .vverbose = (void*)afb_api_so_vverbose,
77 .event_make = (void*)afb_api_so_event_make
80 static struct afb_event afb_api_so_event_make(struct api_so_desc *desc, const char *name)
85 /* makes the event name */
86 assert(desc->binding != NULL);
87 length = strlen(name);
88 event = alloca(length + 2 + desc->apilength);
89 memcpy(event, desc->binding->v1.prefix, desc->apilength);
90 event[desc->apilength] = '/';
91 memcpy(event + desc->apilength + 1, name, length + 1);
94 return afb_evt_create_event(event);
97 static int afb_api_so_event_broadcast(struct api_so_desc *desc, const char *name, struct json_object *object)
102 /* makes the event name */
103 assert(desc->binding != NULL);
104 length = strlen(name);
105 event = alloca(length + 2 + desc->apilength);
106 memcpy(event, desc->binding->v1.prefix, desc->apilength);
107 event[desc->apilength] = '/';
108 memcpy(event + desc->apilength + 1, name, length + 1);
110 return afb_evt_broadcast(event, object);
113 static void afb_api_so_vverbose(struct api_so_desc *desc, int level, const char *file, int line, const char *fmt, va_list args)
117 if (vasprintf(&p, fmt, args) < 0)
118 vverbose(level, file, line, fmt, args);
120 verbose(level, file, line, "%s {binding %s}", p, desc->binding->v1.prefix);
125 static void monitored_call(int signum, struct monitoring *data)
128 afb_req_fail_f(data->req, "aborted", "signal %s(%d) caught", strsignal(signum), signum);
130 data->action(data->req);
133 static void call_check(struct afb_req req, struct afb_context *context, const struct afb_verb_desc_v1 *verb)
135 struct monitoring data;
137 int stag = (int)verb->session;
139 if ((stag & (AFB_SESSION_CREATE|AFB_SESSION_CLOSE|AFB_SESSION_RENEW|AFB_SESSION_CHECK|AFB_SESSION_LOA_EQ)) != 0) {
140 if (!afb_context_check(context)) {
141 afb_context_close(context);
142 afb_req_fail(req, "failed", "invalid token's identity");
147 if ((stag & AFB_SESSION_CREATE) != 0) {
148 if (afb_context_check_loa(context, 1)) {
149 afb_req_fail(req, "failed", "invalid creation state");
152 afb_context_change_loa(context, 1);
153 afb_context_refresh(context);
156 if ((stag & (AFB_SESSION_CREATE | AFB_SESSION_RENEW)) != 0)
157 afb_context_refresh(context);
159 if ((stag & AFB_SESSION_CLOSE) != 0) {
160 afb_context_change_loa(context, 0);
161 afb_context_close(context);
164 if ((stag & AFB_SESSION_LOA_GE) != 0) {
165 int loa = (stag >> AFB_SESSION_LOA_SHIFT) & AFB_SESSION_LOA_MASK;
166 if (!afb_context_check_loa(context, loa)) {
167 afb_req_fail(req, "failed", "invalid LOA");
172 if ((stag & AFB_SESSION_LOA_LE) != 0) {
173 int loa = (stag >> AFB_SESSION_LOA_SHIFT) & AFB_SESSION_LOA_MASK;
174 if (afb_context_check_loa(context, loa + 1)) {
175 afb_req_fail(req, "failed", "invalid LOA");
181 data.action = verb->callback;
182 afb_sig_monitor((void*)monitored_call, &data, api_timeout);
185 static void call(struct api_so_desc *desc, struct afb_req req, struct afb_context *context, const char *verb, size_t lenverb)
187 const struct afb_verb_desc_v1 *v;
189 v = desc->binding->v1.verbs;
190 while (v->name && (strncasecmp(v->name, verb, lenverb) || v->name[lenverb]))
193 call_check(req, context, v);
195 afb_req_fail_f(req, "unknown-verb", "verb %.*s unknown within api %s", (int)lenverb, verb, desc->binding->v1.prefix);
198 static int service_start(struct api_so_desc *desc, int share_session, int onneed)
200 int (*init)(struct afb_service service);
201 void (*onevent)(const char *event, struct json_object *object);
204 if (desc->service != NULL) {
205 /* not an error when onneed */
209 /* already started: it is an error */
210 ERROR("Service %s already started", desc->binding->v1.prefix);
214 /* get the initialisation */
215 init = dlsym(desc->handle, binding_service_init_function_v1);
217 /* not an error when onneed */
221 /* no initialisation method */
222 ERROR("Binding %s is not a service", desc->binding->v1.prefix);
226 /* get the event handler if any */
227 onevent = dlsym(desc->handle, binding_service_event_function_v1);
228 desc->service = afb_svc_create(share_session, init, onevent);
229 if (desc->service == NULL) {
231 ERROR("Starting service %s failed", desc->binding->v1.prefix);
238 void afb_api_so_set_timeout(int to)
243 int afb_api_so_add_binding(const char *path)
247 struct api_so_desc *desc;
248 struct afb_binding *(*register_function) (const struct afb_binding_interface *interface);
250 // This is a loadable library let's check if it's a binding
252 handle = dlopen(path, RTLD_NOW | RTLD_LOCAL);
253 if (handle == NULL) {
254 ERROR("binding [%s] not loadable", path);
258 /* retrieves the register function */
259 register_function = dlsym(handle, binding_register_function_v1);
260 if (!register_function) {
261 ERROR("binding [%s] is not an AFB binding", path);
264 INFO("binding [%s] is a valid AFB binding", path);
267 /* allocates the description */
268 desc = calloc(1, sizeof *desc);
270 ERROR("out of memory");
273 desc->handle = handle;
275 /* init the interface */
276 desc->interface.verbosity = verbosity;
277 desc->interface.mode = AFB_MODE_LOCAL;
278 desc->interface.daemon.itf = &daemon_itf;
279 desc->interface.daemon.closure = desc;
281 /* init the binding */
282 NOTICE("binding [%s] calling registering function %s", path, binding_register_function_v1);
283 desc->binding = register_function(&desc->interface);
284 if (desc->binding == NULL) {
285 ERROR("binding [%s] register function failed. continuing...", path);
289 /* check the returned structure */
290 if (desc->binding->type != AFB_BINDING_VERSION_1) {
291 ERROR("binding [%s] invalid type %d...", path, desc->binding->type);
294 if (desc->binding->v1.prefix == NULL || *desc->binding->v1.prefix == 0) {
295 ERROR("binding [%s] bad prefix...", path);
298 if (!afb_apis_is_valid_api_name(desc->binding->v1.prefix)) {
299 ERROR("binding [%s] invalid prefix...", path);
302 if (desc->binding->v1.info == NULL || *desc->binding->v1.info == 0) {
303 ERROR("binding [%s] bad description...", path);
306 if (desc->binding->v1.verbs == NULL) {
307 ERROR("binding [%s] no APIs...", path);
311 /* records the binding */
312 desc->apilength = strlen(desc->binding->v1.prefix);
313 if (afb_apis_add(desc->binding->v1.prefix, (struct afb_api){
316 .service_start = (void*)service_start }) < 0) {
317 ERROR("binding [%s] can't be registered...", path);
320 NOTICE("binding %s loaded with API prefix %s", path, desc->binding->v1.prefix);
331 static int adddirs(char path[PATH_MAX], size_t end)
334 struct dirent ent, *result;
337 /* open the DIR now */
340 ERROR("can't scan binding directory %s, %m", path);
343 INFO("Scanning dir=[%s] for bindings", path);
345 /* scan each entry */
349 readdir_r(dir, &ent, &result);
353 len = strlen(ent.d_name);
354 if (len + end >= PATH_MAX) {
355 ERROR("path too long while scanning bindings for %s", ent.d_name);
358 memcpy(&path[end], ent.d_name, len+1);
359 if (ent.d_type == DT_DIR) {
360 /* case of directories */
361 if (ent.d_name[0] == '.') {
364 if (ent.d_name[1] == '.' && len == 2)
367 adddirs(path, end+len);;
368 } else if (ent.d_type == DT_REG) {
370 if (!strstr(ent.d_name, ".so"))
372 if (afb_api_so_add_binding(path) < 0)
380 int afb_api_so_add_directory(const char *path)
383 char buffer[PATH_MAX];
385 length = strlen(path);
386 if (length >= sizeof(buffer)) {
387 ERROR("path too long %lu [%.99s...]", (unsigned long)length, path);
391 memcpy(buffer, path, length + 1);
392 return adddirs(buffer, length);
395 int afb_api_so_add_path(const char *path)
400 rc = stat(path, &st);
402 ERROR("Invalid binding path [%s]: %m", path);
403 else if (S_ISDIR(st.st_mode))
404 rc = afb_api_so_add_directory(path);
405 else if (strstr(path, ".so"))
406 rc = afb_api_so_add_binding(path);
408 INFO("not a binding [%s], skipped", path);
412 int afb_api_so_add_pathset(const char *pathset)
414 static char sep[] = ":";
417 ps = strdupa(pathset);
419 p = strsep(&ps, sep);
422 if (afb_api_so_add_path(p) < 0)