2 * Copyright (C) 2016 "IoT.bzh"
3 * Author "Fulup Ar Foll"
4 * Author José Bollo <jose.bollo@iot.bzh>
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 * Contain all generic part to handle REST/API
21 * https://www.gnu.org/software/libmicrohttpd/tutorial.html [search 'largepost.c']
33 #include <sys/types.h>
37 #include <sys/syscall.h>
40 #include "local-def.h"
42 #include "afb-plugin.h"
43 #include "afb-req-itf.h"
47 AFB_plugin *plugin; /* descriptor */
50 void *handle; /* context of dlopen */
53 static int api_timeout = 15;
54 static struct api_desc *apis_array = NULL;
55 static int apis_count = 0;
57 static const char plugin_register_function[] = "pluginRegister";
64 void afb_apis_free_context(int apiidx, void *context)
68 assert(0 <= apiidx && apiidx < apis_count);
69 cb = apis_array[apiidx].plugin->freeCtxCB;
77 const struct AFB_restapi *afb_apis_get(int apiidx, int verbidx)
79 assert(0 <= apiidx && apiidx < apis_count);
80 return &apis_array[apiidx].plugin->apis[verbidx];
83 int afb_apis_get_verbidx(int apiidx, const char *name)
85 const struct AFB_restapi *apis;
88 assert(0 <= apiidx && apiidx < apis_count);
89 apis = apis_array[apiidx].plugin->apis;
90 for (idx = 0 ; apis[idx].name ; idx++)
91 if (!strcasecmp(apis[idx].name, name))
97 int afb_apis_get_apiidx(const char *prefix, size_t length)
100 const struct api_desc *a;
103 length = strlen(prefix);
105 for (i = 0 ; i < apis_count ; i++) {
107 if (a->prefixlen == length && !strcasecmp(a->prefix, prefix))
113 int afb_apis_add_plugin(const char *path)
115 struct api_desc *apis;
117 AFB_plugin *(*pluginRegisterFct) (void);
121 // This is a loadable library let's check if it's a plugin
122 handle = dlopen(path, RTLD_NOW | RTLD_LOCAL);
123 if (handle == NULL) {
124 fprintf(stderr, "[%s] not loadable, continuing...\n", path);
128 /* retrieves the register function */
129 pluginRegisterFct = dlsym(handle, plugin_register_function);
130 if (!pluginRegisterFct) {
131 fprintf(stderr, "[%s] not an AFB plugin, continuing...\n", path);
135 fprintf(stderr, "[%s] is a valid AFB plugin\n", path);
137 /* allocates enough memory */
138 apis = realloc(apis_array, ((unsigned)apis_count + 1) * sizeof * apis);
140 fprintf(stderr, "ERROR: plugin [%s] memory missing. continuing...\n", path);
145 /* init the plugin */
146 plugin = pluginRegisterFct();
147 if (plugin == NULL) {
148 fprintf(stderr, "ERROR: plugin [%s] register function failed. continuing...\n", path);
152 /* check the returned structure */
153 if (plugin->type != AFB_PLUGIN_JSON) {
154 fprintf(stderr, "ERROR: plugin [%s] invalid type %d...\n", path, plugin->type);
157 if (plugin->prefix == NULL || *plugin->prefix == 0) {
158 fprintf(stderr, "ERROR: plugin [%s] bad prefix...\n", path);
161 if (plugin->info == NULL || *plugin->info == 0) {
162 fprintf(stderr, "ERROR: plugin [%s] bad description...\n", path);
165 if (plugin->apis == NULL) {
166 fprintf(stderr, "ERROR: plugin [%s] no APIs...\n", path);
170 /* check previously existing plugin */
171 len = strlen(plugin->prefix);
172 if (afb_apis_get_apiidx(plugin->prefix, len) >= 0) {
173 fprintf(stderr, "ERROR: plugin [%s] prefix %s duplicated...\n", path, plugin->prefix);
177 /* record the plugin */
179 fprintf(stderr, "Loading plugin[%lu] prefix=[%s] info=%s\n", (unsigned long)apis_count, plugin->prefix, plugin->info);
180 apis = &apis_array[apis_count];
181 apis->plugin = plugin;
182 apis->prefixlen = len;
183 apis->prefix = plugin->prefix;
184 apis->handle = handle;
195 static int adddirs(char path[PATH_MAX], size_t end)
199 struct dirent ent, *result;
202 /* open the DIR now */
205 fprintf(stderr, "ERROR in scanning plugin directory %s, %m\n", path);
209 fprintf(stderr, "Scanning dir=[%s] for plugins\n", path);
211 /* scan each entry */
215 readdir_r(dir, &ent, &result);
219 len = strlen(ent.d_name);
220 if (len + end >= PATH_MAX) {
221 fprintf(stderr, "path too long for %s\n", ent.d_name);
224 memcpy(&path[end], ent.d_name, len+1);
225 if (ent.d_type == DT_DIR) {
226 /* case of directories */
227 if (ent.d_name[0] == '.') {
230 if (ent.d_name[1] == '.' && len == 2)
233 rc = adddirs(path, end+len);;
234 } else if (ent.d_type == DT_REG) {
236 if (!strstr(ent.d_name, ".so"))
238 rc = afb_apis_add_plugin(path);
245 int afb_apis_add_directory(const char *path)
248 char buffer[PATH_MAX];
250 length = strlen(path);
251 if (length >= sizeof(buffer)) {
252 fprintf(stderr, "path too long %lu [%.99s...]\n", (unsigned long)length, path);
256 memcpy(buffer, path, length + 1);
257 return adddirs(buffer, length);
260 int afb_apis_add_path(const char *path)
265 rc = stat(path, &st);
267 fprintf(stderr, "Invalid plugin path [%s]: %m\n", path);
268 else if (S_ISDIR(st.st_mode))
269 rc = afb_apis_add_directory(path);
271 rc = afb_apis_add_plugin(path);
275 int afb_apis_add_pathset(const char *pathset)
277 static char sep[] = ":";
281 ps = strdupa(pathset);
283 p = strsep(&ps, sep);
286 rc = afb_apis_add_path(p);
290 // Check of apiurl is declare in this plugin and call it
291 extern __thread sigjmp_buf *error_handler;
292 static void trapping_handle(struct afb_req req, void(*cb)(struct afb_req))
294 volatile int signum, timerset;
296 sigjmp_buf jmpbuf, *older;
297 struct sigevent sevp;
298 struct itimerspec its;
300 // save context before calling the API
302 older = error_handler;
303 signum = setjmp(jmpbuf);
305 afb_req_fail_f(req, "aborted", "signal %d caught", signum);
308 error_handler = &jmpbuf;
309 if (api_timeout > 0) {
310 timerset = 1; /* TODO: check statuses */
311 sevp.sigev_notify = SIGEV_THREAD_ID;
312 sevp.sigev_signo = SIGALRM;
313 #if defined(sigev_notify_thread_id)
314 sevp.sigev_notify_thread_id = (pid_t)syscall(SYS_gettid);
316 sevp._sigev_un._tid = (pid_t)syscall(SYS_gettid);
318 timer_create(CLOCK_THREAD_CPUTIME_ID, &sevp, &timerid);
319 its.it_interval.tv_sec = 0;
320 its.it_interval.tv_nsec = 0;
321 its.it_value.tv_sec = api_timeout;
322 its.it_value.tv_nsec = 0;
323 timer_settime(timerid, 0, &its, NULL);
329 timer_delete(timerid);
330 error_handler = older;
333 static void handle(struct afb_req req, int idxapi, const struct AFB_restapi *verb)
335 switch(verb->session) {
336 case AFB_SESSION_CREATE:
338 req.context = afb_req_session_create(req, idxapi);
339 if (req.context == NULL)
343 case AFB_SESSION_RENEW:
345 req.context = afb_req_session_check(req, idxapi, 1);
346 if (req.context == NULL)
350 case AFB_SESSION_CLOSE:
351 case AFB_SESSION_CHECK:
353 req.context = afb_req_session_check(req, idxapi, 1);
354 if (req.context == NULL)
358 case AFB_SESSION_NONE:
363 trapping_handle(req, verb->callback);
365 if (verb->session == AFB_SESSION_CLOSE) {
367 afb_req_session_close(req);
372 int afb_apis_handle(struct afb_req req, const char *api, size_t lenapi, const char *verb, size_t lenverb)
375 const struct api_desc *a;
376 const struct AFB_restapi *v;
379 for (i = 0 ; i < apis_count ; i++, a++) {
380 if (a->prefixlen == lenapi && !strncasecmp(a->prefix, api, lenapi)) {
382 for (j = 0 ; v->name ; j++, v++) {
383 if (!strncasecmp(v->name, verb, lenverb) && !v->name[lenverb]) {
388 afb_req_fail_f(req, "unknown-verb", "verb %.*s unknown within api %s", (int)lenverb, verb, a->prefix);