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"
41 #include "afb-thread.h"
47 * Description of a binding
50 struct afb_binding *binding; /* descriptor */
51 size_t apilength; /* length of the API name */
52 void *handle; /* context of dlopen */
53 struct afb_svc *service; /* handler for service started */
54 struct afb_binding_interface interface; /* interface for the binding */
57 static const char binding_register_function_v1[] = "afbBindingV1Register";
58 static const char binding_service_init_function_v1[] = "afbBindingV1ServiceInit";
59 static const char binding_service_event_function_v1[] = "afbBindingV1ServiceEvent";
61 static int api_timeout = 15;
63 static struct afb_event afb_api_so_event_make_cb(void *closure, const char *name);
64 static int afb_api_so_event_broadcast_cb(void *closure, const char *name, struct json_object *object);
65 static void afb_api_so_vverbose_cb(void *closure, int level, const char *file, int line, const char *fmt, va_list args);
66 static int afb_api_so_rootdir_get_fd(void *closure);
67 static int afb_api_so_rootdir_open_locale(void *closure, const char *filename, int flags, const char *locale);
69 static const struct afb_daemon_itf daemon_itf = {
70 .event_broadcast = afb_api_so_event_broadcast_cb,
71 .get_event_loop = afb_common_get_event_loop,
72 .get_user_bus = afb_common_get_user_bus,
73 .get_system_bus = afb_common_get_system_bus,
74 .vverbose = afb_api_so_vverbose_cb,
75 .event_make = afb_api_so_event_make_cb,
76 .rootdir_get_fd = afb_api_so_rootdir_get_fd,
77 .rootdir_open_locale = afb_api_so_rootdir_open_locale
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 int afb_api_so_rootdir_get_fd(void *closure)
130 return afb_common_rootdir_get_fd();
133 static int afb_api_so_rootdir_open_locale(void *closure, const char *filename, int flags, const char *locale)
135 return afb_common_rootdir_open_locale(filename, flags, locale);
138 static int call_check(struct afb_req req, struct afb_context *context, const struct afb_verb_desc_v1 *verb)
140 int stag = (int)verb->session;
142 if ((stag & (AFB_SESSION_CREATE|AFB_SESSION_CLOSE|AFB_SESSION_RENEW|AFB_SESSION_CHECK|AFB_SESSION_LOA_EQ)) != 0) {
143 if (!afb_context_check(context)) {
144 afb_context_close(context);
145 afb_req_fail(req, "failed", "invalid token's identity");
150 if ((stag & AFB_SESSION_CREATE) != 0) {
151 if (afb_context_check_loa(context, 1)) {
152 afb_req_fail(req, "failed", "invalid creation state");
155 afb_context_change_loa(context, 1);
156 afb_context_refresh(context);
159 if ((stag & (AFB_SESSION_CREATE | AFB_SESSION_RENEW)) != 0)
160 afb_context_refresh(context);
162 if ((stag & AFB_SESSION_CLOSE) != 0) {
163 afb_context_change_loa(context, 0);
164 afb_context_close(context);
167 if ((stag & AFB_SESSION_LOA_GE) != 0) {
168 int loa = (stag >> AFB_SESSION_LOA_SHIFT) & AFB_SESSION_LOA_MASK;
169 if (!afb_context_check_loa(context, loa)) {
170 afb_req_fail(req, "failed", "invalid LOA");
175 if ((stag & AFB_SESSION_LOA_LE) != 0) {
176 int loa = (stag >> AFB_SESSION_LOA_SHIFT) & AFB_SESSION_LOA_MASK;
177 if (afb_context_check_loa(context, loa + 1)) {
178 afb_req_fail(req, "failed", "invalid LOA");
185 static void call_cb(void *closure, struct afb_req req, struct afb_context *context, const char *strverb, size_t lenverb)
187 const struct afb_verb_desc_v1 *verb;
188 struct api_so_desc *desc = closure;
190 verb = desc->binding->v1.verbs;
191 while (verb->name && (strncasecmp(verb->name, strverb, lenverb) || verb->name[lenverb]))
194 afb_req_fail_f(req, "unknown-verb", "verb %.*s unknown within api %s", (int)lenverb, strverb, desc->binding->v1.prefix);
195 else if (call_check(req, context, verb)) {
198 afb_sig_req_timeout(req, verb->callback, api_timeout);
201 afb_thread_call(req, verb->callback, api_timeout, desc);
205 static int service_start_cb(void *closure, int share_session, int onneed)
207 int (*init)(struct afb_service service);
208 void (*onevent)(const char *event, struct json_object *object);
210 struct api_so_desc *desc = closure;
213 if (desc->service != NULL) {
214 /* not an error when onneed */
218 /* already started: it is an error */
219 ERROR("Service %s already started", desc->binding->v1.prefix);
223 /* get the initialisation */
224 init = dlsym(desc->handle, binding_service_init_function_v1);
226 /* not an error when onneed */
230 /* no initialisation method */
231 ERROR("Binding %s is not a service", desc->binding->v1.prefix);
235 /* get the event handler if any */
236 onevent = dlsym(desc->handle, binding_service_event_function_v1);
237 desc->service = afb_svc_create(share_session, init, onevent);
238 if (desc->service == NULL) {
240 ERROR("Starting service %s failed", desc->binding->v1.prefix);
247 void afb_api_so_set_timeout(int to)
252 int afb_api_so_add_binding(const char *path)
256 struct api_so_desc *desc;
257 struct afb_binding *(*register_function) (const struct afb_binding_interface *interface);
258 struct afb_verb_desc_v1 fake_verb;
259 struct afb_binding fake_binding;
261 // This is a loadable library let's check if it's a binding
263 handle = dlopen(path, RTLD_NOW | RTLD_LOCAL);
264 if (handle == NULL) {
265 ERROR("binding [%s] not loadable: %s", path, dlerror());
269 /* retrieves the register function */
270 register_function = dlsym(handle, binding_register_function_v1);
271 if (!register_function) {
272 ERROR("binding [%s] is not an AFB binding", path);
275 INFO("binding [%s] is a valid AFB binding", path);
278 /* allocates the description */
279 desc = calloc(1, sizeof *desc);
281 ERROR("out of memory");
284 desc->handle = handle;
286 /* init the interface */
287 desc->interface.verbosity = verbosity;
288 desc->interface.mode = AFB_MODE_LOCAL;
289 desc->interface.daemon.itf = &daemon_itf;
290 desc->interface.daemon.closure = desc;
292 /* for log purpose, a fake binding is needed here */
293 desc->binding = &fake_binding;
294 fake_binding.type = AFB_BINDING_VERSION_1;
295 fake_binding.v1.info = path;
296 fake_binding.v1.prefix = path;
297 fake_binding.v1.verbs = &fake_verb;
298 fake_verb.name = NULL;
300 /* init the binding */
301 NOTICE("binding [%s] calling registering function %s", path, binding_register_function_v1);
302 desc->binding = register_function(&desc->interface);
303 if (desc->binding == NULL) {
304 ERROR("binding [%s] register function failed. continuing...", path);
308 /* check the returned structure */
309 if (desc->binding->type != AFB_BINDING_VERSION_1) {
310 ERROR("binding [%s] invalid type %d...", path, desc->binding->type);
313 if (desc->binding->v1.prefix == NULL || *desc->binding->v1.prefix == 0) {
314 ERROR("binding [%s] bad prefix...", path);
317 if (!afb_apis_is_valid_api_name(desc->binding->v1.prefix)) {
318 ERROR("binding [%s] invalid prefix...", path);
321 if (desc->binding->v1.info == NULL || *desc->binding->v1.info == 0) {
322 ERROR("binding [%s] bad description...", path);
325 if (desc->binding->v1.verbs == NULL) {
326 ERROR("binding [%s] no APIs...", path);
330 /* records the binding */
331 desc->apilength = strlen(desc->binding->v1.prefix);
332 if (afb_apis_add(desc->binding->v1.prefix, (struct afb_api){
335 .service_start = service_start_cb }) < 0) {
336 ERROR("binding [%s] can't be registered...", path);
339 NOTICE("binding %s loaded with API prefix %s", path, desc->binding->v1.prefix);
350 static int adddirs(char path[PATH_MAX], size_t end)
353 struct dirent ent, *result;
356 /* open the DIR now */
359 ERROR("can't scan binding directory %s, %m", path);
362 INFO("Scanning dir=[%s] for bindings", path);
364 /* scan each entry */
368 readdir_r(dir, &ent, &result);
372 len = strlen(ent.d_name);
373 if (len + end >= PATH_MAX) {
374 ERROR("path too long while scanning bindings for %s", ent.d_name);
377 memcpy(&path[end], ent.d_name, len+1);
378 if (ent.d_type == DT_DIR) {
379 /* case of directories */
380 if (ent.d_name[0] == '.') {
383 if (ent.d_name[1] == '.' && len == 2)
386 adddirs(path, end+len);;
387 } else if (ent.d_type == DT_REG) {
389 if (!strstr(ent.d_name, ".so"))
391 if (afb_api_so_add_binding(path) < 0)
399 int afb_api_so_add_directory(const char *path)
402 char buffer[PATH_MAX];
404 length = strlen(path);
405 if (length >= sizeof(buffer)) {
406 ERROR("path too long %lu [%.99s...]", (unsigned long)length, path);
410 memcpy(buffer, path, length + 1);
411 return adddirs(buffer, length);
414 int afb_api_so_add_path(const char *path)
419 rc = stat(path, &st);
421 ERROR("Invalid binding path [%s]: %m", path);
422 else if (S_ISDIR(st.st_mode))
423 rc = afb_api_so_add_directory(path);
424 else if (strstr(path, ".so"))
425 rc = afb_api_so_add_binding(path);
427 INFO("not a binding [%s], skipped", path);
431 int afb_api_so_add_pathset(const char *pathset)
433 static char sep[] = ":";
436 ps = strdupa(pathset);
438 p = strsep(&ps, sep);
441 if (afb_api_so_add_path(p) < 0)