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-pollmgr-itf.h"
37 #include "afb-evmgr-itf.h"
41 #include "afb-api-so.h"
43 #include "utils-upoll.h"
45 extern __thread sigjmp_buf *error_handler;
48 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 const struct afb_pollmgr_itf pollmgr_itf = {
58 .wait = (void*)upoll_wait,
59 .open = (void*)upoll_open,
60 .on_readable = (void*)upoll_on_readable,
61 .on_writable = (void*)upoll_on_writable,
62 .on_hangup = (void*)upoll_on_hangup,
63 .close = (void*)upoll_close
66 static void afb_api_so_evmgr_push(struct api_so_desc *desc, const char *name, struct json_object *object)
70 static const struct afb_evmgr_itf evmgr_itf = {
71 .push = (void*)afb_api_so_evmgr_push
74 static struct afb_evmgr afb_api_so_get_evmgr(struct api_so_desc *desc)
76 return (struct afb_evmgr){ .itf = &evmgr_itf, .closure = desc };
79 static struct afb_pollmgr afb_api_so_get_pollmgr(struct api_so_desc *desc)
81 return (struct afb_pollmgr){ .itf = &pollmgr_itf, .closure = NULL };
84 static const struct afb_daemon_itf daemon_itf = {
85 .get_evmgr = (void*)afb_api_so_get_evmgr,
86 .get_pollmgr = (void*)afb_api_so_get_pollmgr
89 static void free_context(struct api_so_desc *desc, void *context)
93 cb = desc->plugin->freeCtxCB;
100 static void trapping_call(struct afb_req req, void(*cb)(struct afb_req))
102 volatile int signum, timerset;
104 sigjmp_buf jmpbuf, *older;
105 struct sigevent sevp;
106 struct itimerspec its;
109 older = error_handler;
110 signum = setjmp(jmpbuf);
112 afb_req_fail_f(req, "aborted", "signal %d caught", signum);
115 error_handler = &jmpbuf;
116 if (api_timeout > 0) {
117 timerset = 1; /* TODO: check statuses */
118 sevp.sigev_notify = SIGEV_THREAD_ID;
119 sevp.sigev_signo = SIGALRM;
120 sevp.sigev_value.sival_ptr = NULL;
121 #if defined(sigev_notify_thread_id)
122 sevp.sigev_notify_thread_id = (pid_t)syscall(SYS_gettid);
124 sevp._sigev_un._tid = (pid_t)syscall(SYS_gettid);
126 timer_create(CLOCK_THREAD_CPUTIME_ID, &sevp, &timerid);
127 its.it_interval.tv_sec = 0;
128 its.it_interval.tv_nsec = 0;
129 its.it_value.tv_sec = api_timeout;
130 its.it_value.tv_nsec = 0;
131 timer_settime(timerid, 0, &its, NULL);
137 timer_delete(timerid);
138 error_handler = older;
141 static void call_check(struct afb_req req, const struct AFB_restapi *verb)
143 switch(verb->session) {
144 case AFB_SESSION_CREATE:
145 if (!afb_req_session_create(req))
148 case AFB_SESSION_RENEW:
149 if (!afb_req_session_check(req, 1))
152 case AFB_SESSION_CLOSE:
153 case AFB_SESSION_CHECK:
154 if (!afb_req_session_check(req, 0))
157 case AFB_SESSION_NONE:
161 trapping_call(req, verb->callback);
163 if (verb->session == AFB_SESSION_CLOSE)
164 afb_req_session_close(req);
167 static void call(struct api_so_desc *desc, struct afb_req req, const char *verb, size_t lenverb)
169 const struct AFB_restapi *v;
171 v = desc->plugin->apis;
172 while (v->name && (strncasecmp(v->name, verb, lenverb) || v->name[lenverb]))
177 afb_req_fail_f(req, "unknown-verb", "verb %.*s unknown within api %s", (int)lenverb, verb, desc->plugin->prefix);
180 int afb_api_so_add_plugin(const char *path)
182 struct api_so_desc *desc;
183 struct AFB_plugin *(*pluginRegisterFct) (const struct AFB_interface *interface);
185 desc = calloc(1, sizeof *desc);
187 fprintf(stderr, "[%s] out of memory\n", path);
191 // This is a loadable library let's check if it's a plugin
192 desc->handle = dlopen(path, RTLD_NOW | RTLD_LOCAL);
193 if (desc->handle == NULL) {
194 fprintf(stderr, "[%s] not loadable, continuing...\n", path);
198 /* retrieves the register function */
199 pluginRegisterFct = dlsym(desc->handle, plugin_register_function);
200 if (!pluginRegisterFct) {
201 fprintf(stderr, "[%s] not an AFB plugin, continuing...\n", path);
205 fprintf(stderr, "[%s] is a valid AFB plugin\n", path);
207 /* init the interface */
208 desc->interface.verbosity = 0;
209 desc->interface.mode = AFB_MODE_LOCAL;
210 desc->interface.daemon.itf = &daemon_itf;
211 desc->interface.daemon.closure = desc;
213 /* init the plugin */
214 desc->plugin = pluginRegisterFct(&desc->interface);
215 if (desc->plugin == NULL) {
216 fprintf(stderr, "ERROR: plugin [%s] register function failed. continuing...\n", path);
220 /* check the returned structure */
221 if (desc->plugin->type != AFB_PLUGIN_JSON) {
222 fprintf(stderr, "ERROR: plugin [%s] invalid type %d...\n", path, desc->plugin->type);
225 if (desc->plugin->prefix == NULL || *desc->plugin->prefix == 0) {
226 fprintf(stderr, "ERROR: plugin [%s] bad prefix...\n", path);
229 if (desc->plugin->info == NULL || *desc->plugin->info == 0) {
230 fprintf(stderr, "ERROR: plugin [%s] bad description...\n", path);
233 if (desc->plugin->apis == NULL) {
234 fprintf(stderr, "ERROR: plugin [%s] no APIs...\n", path);
238 /* records the plugin */
239 if (afb_apis_add(desc->plugin->prefix, (struct afb_api){
242 .free_context = (void*)free_context}) < 0) {
243 fprintf(stderr, "ERROR: plugin [%s] can't be registered...\n", path);
250 dlclose(desc->handle);
257 static int adddirs(char path[PATH_MAX], size_t end)
260 struct dirent ent, *result;
263 /* open the DIR now */
266 fprintf(stderr, "ERROR in scanning plugin directory %s, %m\n", path);
270 fprintf(stderr, "Scanning dir=[%s] for plugins\n", path);
272 /* scan each entry */
276 readdir_r(dir, &ent, &result);
280 len = strlen(ent.d_name);
281 if (len + end >= PATH_MAX) {
282 fprintf(stderr, "path too long for %s\n", ent.d_name);
285 memcpy(&path[end], ent.d_name, len+1);
286 if (ent.d_type == DT_DIR) {
287 /* case of directories */
288 if (ent.d_name[0] == '.') {
291 if (ent.d_name[1] == '.' && len == 2)
294 adddirs(path, end+len);;
295 } else if (ent.d_type == DT_REG) {
297 if (!strstr(ent.d_name, ".so"))
299 afb_api_so_add_plugin(path);
306 int afb_api_so_add_directory(const char *path)
309 char buffer[PATH_MAX];
311 length = strlen(path);
312 if (length >= sizeof(buffer)) {
313 fprintf(stderr, "path too long %lu [%.99s...]\n", (unsigned long)length, path);
317 memcpy(buffer, path, length + 1);
318 return adddirs(buffer, length);
321 int afb_api_so_add_path(const char *path)
326 rc = stat(path, &st);
328 fprintf(stderr, "Invalid plugin path [%s]: %m\n", path);
329 else if (S_ISDIR(st.st_mode))
330 rc = afb_api_so_add_directory(path);
332 rc = afb_api_so_add_plugin(path);
336 int afb_api_so_add_pathset(const char *pathset)
338 static char sep[] = ":";
341 ps = strdupa(pathset);
343 p = strsep(&ps, sep);
346 afb_api_so_add_path(p);