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.
27 #include <sys/types.h>
31 #include <sys/syscall.h>
34 #include "afb-plugin.h"
35 #include "afb-req-itf.h"
36 #include "afb-evmgr-itf.h"
39 #include "afb-common.h"
41 #include "afb-api-so.h"
44 extern __thread sigjmp_buf *error_handler;
47 struct AFB_plugin *plugin; /* descriptor */
49 void *handle; /* context of dlopen */
50 struct AFB_interface interface; /* interface */
53 static int api_timeout = 15;
55 static const char plugin_register_function[] = "pluginRegister";
57 static void afb_api_so_evmgr_push(struct api_so_desc *desc, const char *name, struct json_object *object)
62 assert(desc->plugin != NULL);
63 length = strlen(name);
64 event = alloca(length + 2 + desc->apilength);
65 memcpy(event, desc->plugin->prefix, desc->apilength);
66 event[desc->apilength] = '/';
67 memcpy(event + desc->apilength + 1, name, length + 1);
68 ctxClientEventSend(NULL, event, object);
71 static const struct afb_evmgr_itf evmgr_itf = {
72 .push = (void*)afb_api_so_evmgr_push
75 static struct afb_evmgr afb_api_so_get_evmgr(struct api_so_desc *desc)
77 return (struct afb_evmgr){ .itf = &evmgr_itf, .closure = desc };
80 static const struct afb_daemon_itf daemon_itf = {
81 .get_evmgr = (void*)afb_api_so_get_evmgr,
82 .get_event_loop = (void*)afb_common_get_event_loop,
83 .get_user_bus = (void*)afb_common_get_user_bus,
84 .get_system_bus = (void*)afb_common_get_system_bus
88 static void trapping_call(struct afb_req req, void(*cb)(struct afb_req))
90 volatile int signum, timerset;
92 sigjmp_buf jmpbuf, *older;
94 struct itimerspec its;
97 older = error_handler;
98 signum = setjmp(jmpbuf);
100 afb_req_fail_f(req, "aborted", "signal %d caught", signum);
103 error_handler = &jmpbuf;
104 if (api_timeout > 0) {
105 timerset = 1; /* TODO: check statuses */
106 sevp.sigev_notify = SIGEV_THREAD_ID;
107 sevp.sigev_signo = SIGALRM;
108 sevp.sigev_value.sival_ptr = NULL;
109 #if defined(sigev_notify_thread_id)
110 sevp.sigev_notify_thread_id = (pid_t)syscall(SYS_gettid);
112 sevp._sigev_un._tid = (pid_t)syscall(SYS_gettid);
114 timer_create(CLOCK_THREAD_CPUTIME_ID, &sevp, &timerid);
115 its.it_interval.tv_sec = 0;
116 its.it_interval.tv_nsec = 0;
117 its.it_value.tv_sec = api_timeout;
118 its.it_value.tv_nsec = 0;
119 timer_settime(timerid, 0, &its, NULL);
125 timer_delete(timerid);
126 error_handler = older;
129 static void call_check(struct afb_req req, const struct AFB_restapi *verb)
131 switch(verb->session) {
132 case AFB_SESSION_CREATE:
133 if (!afb_req_session_create(req))
136 case AFB_SESSION_RENEW:
137 if (!afb_req_session_check(req, 1))
140 case AFB_SESSION_CLOSE:
141 case AFB_SESSION_CHECK:
142 if (!afb_req_session_check(req, 0))
145 case AFB_SESSION_NONE:
149 trapping_call(req, verb->callback);
150 if (verb->session == AFB_SESSION_CLOSE)
151 afb_req_session_close(req);
154 static void call(struct api_so_desc *desc, struct afb_req req, const char *verb, size_t lenverb)
156 const struct AFB_restapi *v;
158 v = desc->plugin->apis;
159 while (v->name && (strncasecmp(v->name, verb, lenverb) || v->name[lenverb]))
164 afb_req_fail_f(req, "unknown-verb", "verb %.*s unknown within api %s", (int)lenverb, verb, desc->plugin->prefix);
167 int afb_api_so_add_plugin(const char *path)
169 struct api_so_desc *desc;
170 struct AFB_plugin *(*pluginRegisterFct) (const struct AFB_interface *interface);
172 desc = calloc(1, sizeof *desc);
174 fprintf(stderr, "[%s] out of memory\n", path);
178 // This is a loadable library let's check if it's a plugin
179 desc->handle = dlopen(path, RTLD_NOW | RTLD_LOCAL);
180 if (desc->handle == NULL) {
181 fprintf(stderr, "[%s] not loadable, continuing...\n", path);
185 /* retrieves the register function */
186 pluginRegisterFct = dlsym(desc->handle, plugin_register_function);
187 if (!pluginRegisterFct) {
188 fprintf(stderr, "[%s] not an AFB plugin, continuing...\n", path);
192 fprintf(stderr, "[%s] is a valid AFB plugin\n", path);
194 /* init the interface */
195 desc->interface.verbosity = 0;
196 desc->interface.mode = AFB_MODE_LOCAL;
197 desc->interface.daemon.itf = &daemon_itf;
198 desc->interface.daemon.closure = desc;
200 /* init the plugin */
201 desc->plugin = pluginRegisterFct(&desc->interface);
202 if (desc->plugin == NULL) {
203 fprintf(stderr, "ERROR: plugin [%s] register function failed. continuing...\n", path);
207 /* check the returned structure */
208 if (desc->plugin->type != AFB_PLUGIN_JSON) {
209 fprintf(stderr, "ERROR: plugin [%s] invalid type %d...\n", path, desc->plugin->type);
212 if (desc->plugin->prefix == NULL || *desc->plugin->prefix == 0) {
213 fprintf(stderr, "ERROR: plugin [%s] bad prefix...\n", path);
216 if (desc->plugin->info == NULL || *desc->plugin->info == 0) {
217 fprintf(stderr, "ERROR: plugin [%s] bad description...\n", path);
220 if (desc->plugin->apis == NULL) {
221 fprintf(stderr, "ERROR: plugin [%s] no APIs...\n", path);
225 /* records the plugin */
226 desc->apilength = strlen(desc->plugin->prefix);
227 if (afb_apis_add(desc->plugin->prefix, (struct afb_api){
229 .call = (void*)call}) < 0) {
230 fprintf(stderr, "ERROR: plugin [%s] can't be registered...\n", path);
237 dlclose(desc->handle);
244 static int adddirs(char path[PATH_MAX], size_t end)
247 struct dirent ent, *result;
250 /* open the DIR now */
253 fprintf(stderr, "ERROR in scanning plugin directory %s, %m\n", path);
257 fprintf(stderr, "Scanning dir=[%s] for plugins\n", path);
259 /* scan each entry */
263 readdir_r(dir, &ent, &result);
267 len = strlen(ent.d_name);
268 if (len + end >= PATH_MAX) {
269 fprintf(stderr, "path too long for %s\n", ent.d_name);
272 memcpy(&path[end], ent.d_name, len+1);
273 if (ent.d_type == DT_DIR) {
274 /* case of directories */
275 if (ent.d_name[0] == '.') {
278 if (ent.d_name[1] == '.' && len == 2)
281 adddirs(path, end+len);;
282 } else if (ent.d_type == DT_REG) {
284 if (!strstr(ent.d_name, ".so"))
286 afb_api_so_add_plugin(path);
293 int afb_api_so_add_directory(const char *path)
296 char buffer[PATH_MAX];
298 length = strlen(path);
299 if (length >= sizeof(buffer)) {
300 fprintf(stderr, "path too long %lu [%.99s...]\n", (unsigned long)length, path);
304 memcpy(buffer, path, length + 1);
305 return adddirs(buffer, length);
308 int afb_api_so_add_path(const char *path)
313 rc = stat(path, &st);
315 fprintf(stderr, "Invalid plugin path [%s]: %m\n", path);
316 else if (S_ISDIR(st.st_mode))
317 rc = afb_api_so_add_directory(path);
319 rc = afb_api_so_add_plugin(path);
323 int afb_api_so_add_pathset(const char *pathset)
325 static char sep[] = ":";
328 ps = strdupa(pathset);
330 p = strsep(&ps, sep);
333 afb_api_so_add_path(p);