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_cb(void *closure, const char *name);
68 static int afb_api_so_event_broadcast_cb(void *closure, const char *name, struct json_object *object);
69 static void afb_api_so_vverbose_cb(void *closure, 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 = afb_api_so_event_broadcast_cb,
73 .get_event_loop = afb_common_get_event_loop,
74 .get_user_bus = afb_common_get_user_bus,
75 .get_system_bus = afb_common_get_system_bus,
76 .vverbose = afb_api_so_vverbose_cb,
77 .event_make = afb_api_so_event_make_cb
80 static struct afb_event afb_api_so_event_make_cb(void *closure, const char *name)
84 struct api_so_desc *desc = closure;
86 /* makes the event name */
87 assert(desc->binding != NULL);
88 length = strlen(name);
89 event = alloca(length + 2 + desc->apilength);
90 memcpy(event, desc->binding->v1.prefix, desc->apilength);
91 event[desc->apilength] = '/';
92 memcpy(event + desc->apilength + 1, name, length + 1);
95 return afb_evt_create_event(event);
98 static int afb_api_so_event_broadcast_cb(void *closure, const char *name, struct json_object *object)
102 struct api_so_desc *desc = closure;
104 /* makes the event name */
105 assert(desc->binding != NULL);
106 length = strlen(name);
107 event = alloca(length + 2 + desc->apilength);
108 memcpy(event, desc->binding->v1.prefix, desc->apilength);
109 event[desc->apilength] = '/';
110 memcpy(event + desc->apilength + 1, name, length + 1);
112 return afb_evt_broadcast(event, object);
115 static void afb_api_so_vverbose_cb(void *closure, int level, const char *file, int line, const char *fmt, va_list args)
118 struct api_so_desc *desc = closure;
120 if (vasprintf(&p, fmt, args) < 0)
121 vverbose(level, file, line, fmt, args);
123 verbose(level, file, line, "%s {binding %s}", p, desc->binding->v1.prefix);
128 static void monitored_call(int signum, void *arg)
130 struct monitoring *data = arg;
132 afb_req_fail_f(data->req, "aborted", "signal %s(%d) caught", strsignal(signum), signum);
134 data->action(data->req);
137 static void call_check(struct afb_req req, struct afb_context *context, const struct afb_verb_desc_v1 *verb)
139 struct monitoring data;
141 int stag = (int)verb->session;
143 if ((stag & (AFB_SESSION_CREATE|AFB_SESSION_CLOSE|AFB_SESSION_RENEW|AFB_SESSION_CHECK|AFB_SESSION_LOA_EQ)) != 0) {
144 if (!afb_context_check(context)) {
145 afb_context_close(context);
146 afb_req_fail(req, "failed", "invalid token's identity");
151 if ((stag & AFB_SESSION_CREATE) != 0) {
152 if (afb_context_check_loa(context, 1)) {
153 afb_req_fail(req, "failed", "invalid creation state");
156 afb_context_change_loa(context, 1);
157 afb_context_refresh(context);
160 if ((stag & (AFB_SESSION_CREATE | AFB_SESSION_RENEW)) != 0)
161 afb_context_refresh(context);
163 if ((stag & AFB_SESSION_CLOSE) != 0) {
164 afb_context_change_loa(context, 0);
165 afb_context_close(context);
168 if ((stag & AFB_SESSION_LOA_GE) != 0) {
169 int loa = (stag >> AFB_SESSION_LOA_SHIFT) & AFB_SESSION_LOA_MASK;
170 if (!afb_context_check_loa(context, loa)) {
171 afb_req_fail(req, "failed", "invalid LOA");
176 if ((stag & AFB_SESSION_LOA_LE) != 0) {
177 int loa = (stag >> AFB_SESSION_LOA_SHIFT) & AFB_SESSION_LOA_MASK;
178 if (afb_context_check_loa(context, loa + 1)) {
179 afb_req_fail(req, "failed", "invalid LOA");
185 data.action = verb->callback;
186 afb_sig_monitor(monitored_call, &data, api_timeout);
189 static void call_cb(void *closure, struct afb_req req, struct afb_context *context, const char *verb, size_t lenverb)
191 const struct afb_verb_desc_v1 *v;
192 struct api_so_desc *desc = closure;
194 v = desc->binding->v1.verbs;
195 while (v->name && (strncasecmp(v->name, verb, lenverb) || v->name[lenverb]))
198 call_check(req, context, v);
200 afb_req_fail_f(req, "unknown-verb", "verb %.*s unknown within api %s", (int)lenverb, verb, desc->binding->v1.prefix);
203 static int service_start_cb(void *closure, int share_session, int onneed)
205 int (*init)(struct afb_service service);
206 void (*onevent)(const char *event, struct json_object *object);
208 struct api_so_desc *desc = closure;
211 if (desc->service != NULL) {
212 /* not an error when onneed */
216 /* already started: it is an error */
217 ERROR("Service %s already started", desc->binding->v1.prefix);
221 /* get the initialisation */
222 init = dlsym(desc->handle, binding_service_init_function_v1);
224 /* not an error when onneed */
228 /* no initialisation method */
229 ERROR("Binding %s is not a service", desc->binding->v1.prefix);
233 /* get the event handler if any */
234 onevent = dlsym(desc->handle, binding_service_event_function_v1);
235 desc->service = afb_svc_create(share_session, init, onevent);
236 if (desc->service == NULL) {
238 ERROR("Starting service %s failed", desc->binding->v1.prefix);
245 void afb_api_so_set_timeout(int to)
250 int afb_api_so_add_binding(const char *path)
254 struct api_so_desc *desc;
255 struct afb_binding *(*register_function) (const struct afb_binding_interface *interface);
256 struct afb_verb_desc_v1 fake_verb;
257 struct afb_binding fake_binding;
259 // This is a loadable library let's check if it's a binding
261 handle = dlopen(path, RTLD_NOW | RTLD_LOCAL);
262 if (handle == NULL) {
263 ERROR("binding [%s] not loadable", path);
267 /* retrieves the register function */
268 register_function = dlsym(handle, binding_register_function_v1);
269 if (!register_function) {
270 ERROR("binding [%s] is not an AFB binding", path);
273 INFO("binding [%s] is a valid AFB binding", path);
276 /* allocates the description */
277 desc = calloc(1, sizeof *desc);
279 ERROR("out of memory");
282 desc->handle = handle;
284 /* init the interface */
285 desc->interface.verbosity = verbosity;
286 desc->interface.mode = AFB_MODE_LOCAL;
287 desc->interface.daemon.itf = &daemon_itf;
288 desc->interface.daemon.closure = desc;
290 /* for log purpose, a fake binding is needed here */
291 desc->binding = &fake_binding;
292 fake_binding.type = AFB_BINDING_VERSION_1;
293 fake_binding.v1.info = path;
294 fake_binding.v1.prefix = path;
295 fake_binding.v1.verbs = &fake_verb;
296 fake_verb.name = NULL;
298 /* init the binding */
299 NOTICE("binding [%s] calling registering function %s", path, binding_register_function_v1);
300 desc->binding = register_function(&desc->interface);
301 if (desc->binding == NULL) {
302 ERROR("binding [%s] register function failed. continuing...", path);
306 /* check the returned structure */
307 if (desc->binding->type != AFB_BINDING_VERSION_1) {
308 ERROR("binding [%s] invalid type %d...", path, desc->binding->type);
311 if (desc->binding->v1.prefix == NULL || *desc->binding->v1.prefix == 0) {
312 ERROR("binding [%s] bad prefix...", path);
315 if (!afb_apis_is_valid_api_name(desc->binding->v1.prefix)) {
316 ERROR("binding [%s] invalid prefix...", path);
319 if (desc->binding->v1.info == NULL || *desc->binding->v1.info == 0) {
320 ERROR("binding [%s] bad description...", path);
323 if (desc->binding->v1.verbs == NULL) {
324 ERROR("binding [%s] no APIs...", path);
328 /* records the binding */
329 desc->apilength = strlen(desc->binding->v1.prefix);
330 if (afb_apis_add(desc->binding->v1.prefix, (struct afb_api){
333 .service_start = service_start_cb }) < 0) {
334 ERROR("binding [%s] can't be registered...", path);
337 NOTICE("binding %s loaded with API prefix %s", path, desc->binding->v1.prefix);
348 static int adddirs(char path[PATH_MAX], size_t end)
351 struct dirent ent, *result;
354 /* open the DIR now */
357 ERROR("can't scan binding directory %s, %m", path);
360 INFO("Scanning dir=[%s] for bindings", path);
362 /* scan each entry */
366 readdir_r(dir, &ent, &result);
370 len = strlen(ent.d_name);
371 if (len + end >= PATH_MAX) {
372 ERROR("path too long while scanning bindings for %s", ent.d_name);
375 memcpy(&path[end], ent.d_name, len+1);
376 if (ent.d_type == DT_DIR) {
377 /* case of directories */
378 if (ent.d_name[0] == '.') {
381 if (ent.d_name[1] == '.' && len == 2)
384 adddirs(path, end+len);;
385 } else if (ent.d_type == DT_REG) {
387 if (!strstr(ent.d_name, ".so"))
389 if (afb_api_so_add_binding(path) < 0)
397 int afb_api_so_add_directory(const char *path)
400 char buffer[PATH_MAX];
402 length = strlen(path);
403 if (length >= sizeof(buffer)) {
404 ERROR("path too long %lu [%.99s...]", (unsigned long)length, path);
408 memcpy(buffer, path, length + 1);
409 return adddirs(buffer, length);
412 int afb_api_so_add_path(const char *path)
417 rc = stat(path, &st);
419 ERROR("Invalid binding path [%s]: %m", path);
420 else if (S_ISDIR(st.st_mode))
421 rc = afb_api_so_add_directory(path);
422 else if (strstr(path, ".so"))
423 rc = afb_api_so_add_binding(path);
425 INFO("not a binding [%s], skipped", path);
429 int afb_api_so_add_pathset(const char *pathset)
431 static char sep[] = ":";
434 ps = strdupa(pathset);
436 p = strsep(&ps, sep);
439 if (afb_api_so_add_path(p) < 0)