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-req-itf.h"
46 AFB_plugin *plugin; /* descriptor */
49 void *handle; /* context of dlopen */
52 static int api_timeout = 15;
53 static struct api_desc *apis_array = NULL;
54 static int apis_count = 0;
56 static const char plugin_register_function[] = "pluginRegister";
63 void afb_apis_free_context(int apiidx, void *context)
67 assert(0 <= apiidx && apiidx < apis_count);
68 cb = apis_array[apiidx].plugin->freeCtxCB;
75 const struct AFB_restapi *afb_apis_get(int apiidx, int verbidx)
77 assert(0 <= apiidx && apiidx < apis_count);
78 return &apis_array[apiidx].plugin->apis[verbidx];
81 int afb_apis_get_verbidx(int apiidx, const char *name)
83 const struct AFB_restapi *apis;
86 assert(0 <= apiidx && apiidx < apis_count);
87 apis = apis_array[apiidx].plugin->apis;
88 for (idx = 0 ; apis[idx].name ; idx++)
89 if (!strcasecmp(apis[idx].name, name))
94 int afb_apis_get_apiidx(const char *prefix, size_t length)
97 const struct api_desc *a;
100 length = strlen(prefix);
102 for (i = 0 ; i < apis_count ; i++) {
104 if (a->prefixlen == length && !strcasecmp(a->prefix, prefix))
110 int afb_apis_add_plugin(const char *path)
112 struct api_desc *apis;
114 AFB_plugin *(*pluginRegisterFct) (void);
118 // This is a loadable library let's check if it's a plugin
119 handle = dlopen(path, RTLD_NOW | RTLD_LOCAL);
120 if (handle == NULL) {
121 fprintf(stderr, "[%s] not loadable, continuing...\n", path);
125 /* retrieves the register function */
126 pluginRegisterFct = dlsym(handle, plugin_register_function);
127 if (!pluginRegisterFct) {
128 fprintf(stderr, "[%s] not an AFB plugin, continuing...\n", path);
132 fprintf(stderr, "[%s] is a valid AFB plugin\n", path);
134 /* allocates enough memory */
135 apis = realloc(apis_array, ((unsigned)apis_count + 1) * sizeof * apis);
137 fprintf(stderr, "ERROR: plugin [%s] memory missing. continuing...\n", path);
142 /* init the plugin */
143 plugin = pluginRegisterFct();
144 if (plugin == NULL) {
145 fprintf(stderr, "ERROR: plugin [%s] register function failed. continuing...\n", path);
149 /* check the returned structure */
150 if (plugin->type != AFB_PLUGIN_JSON) {
151 fprintf(stderr, "ERROR: plugin [%s] invalid type %d...\n", path, plugin->type);
154 if (plugin->prefix == NULL || *plugin->prefix == 0) {
155 fprintf(stderr, "ERROR: plugin [%s] bad prefix...\n", path);
158 if (plugin->info == NULL || *plugin->info == 0) {
159 fprintf(stderr, "ERROR: plugin [%s] bad description...\n", path);
162 if (plugin->apis == NULL) {
163 fprintf(stderr, "ERROR: plugin [%s] no APIs...\n", path);
167 /* check previously existing plugin */
168 len = strlen(plugin->prefix);
169 if (afb_apis_get_apiidx(plugin->prefix, len) >= 0) {
170 fprintf(stderr, "ERROR: plugin [%s] prefix %s duplicated...\n", path, plugin->prefix);
174 /* record the plugin */
176 fprintf(stderr, "Loading plugin[%lu] prefix=[%s] info=%s\n", (unsigned long)apis_count, plugin->prefix, plugin->info);
177 apis = &apis_array[apis_count];
178 apis->plugin = plugin;
179 apis->prefixlen = len;
180 apis->prefix = plugin->prefix;
181 apis->handle = handle;
192 static int adddirs(char path[PATH_MAX], size_t end)
196 struct dirent ent, *result;
199 /* open the DIR now */
202 fprintf(stderr, "ERROR in scanning plugin directory %s, %m\n", path);
206 fprintf(stderr, "Scanning dir=[%s] for plugins\n", path);
208 /* scan each entry */
212 readdir_r(dir, &ent, &result);
216 len = strlen(ent.d_name);
217 if (len + end >= PATH_MAX) {
218 fprintf(stderr, "path too long for %s\n", ent.d_name);
221 memcpy(&path[end], ent.d_name, len+1);
222 if (ent.d_type == DT_DIR) {
223 /* case of directories */
224 if (ent.d_name[0] == '.') {
227 if (ent.d_name[1] == '.' && len == 2)
230 rc = adddirs(path, end+len);;
231 } else if (ent.d_type == DT_REG) {
233 if (!strstr(ent.d_name, ".so"))
235 rc = afb_apis_add_plugin(path);
242 int afb_apis_add_directory(const char *path)
245 char buffer[PATH_MAX];
247 length = strlen(path);
248 if (length >= sizeof(buffer)) {
249 fprintf(stderr, "path too long %lu [%.99s...]\n", (unsigned long)length, path);
253 memcpy(buffer, path, length + 1);
254 return adddirs(buffer, length);
257 int afb_apis_add_path(const char *path)
262 rc = stat(path, &st);
264 fprintf(stderr, "Invalid plugin path [%s]: %m\n", path);
265 else if (S_ISDIR(st.st_mode))
266 rc = afb_apis_add_directory(path);
268 rc = afb_apis_add_plugin(path);
272 int afb_apis_add_pathset(const char *pathset)
274 static char sep[] = ":";
278 ps = strdupa(pathset);
280 p = strsep(&ps, sep);
283 rc = afb_apis_add_path(p);
287 // Check of apiurl is declare in this plugin and call it
288 extern __thread sigjmp_buf *error_handler;
289 static void trapping_handle(AFB_request * request, struct json_object *(*cb)(AFB_request *,void*))
291 volatile int signum, timerset;
293 sigjmp_buf jmpbuf, *older;
294 struct sigevent sevp;
295 struct itimerspec its;
297 // save context before calling the API
299 older = error_handler;
300 signum = setjmp(jmpbuf);
302 afb_req_fail_f(*request->areq, "aborted", "signal %d caught", signum);
305 error_handler = &jmpbuf;
306 if (api_timeout > 0) {
307 timerset = 1; /* TODO: check statuses */
308 sevp.sigev_notify = SIGEV_THREAD_ID;
309 sevp.sigev_signo = SIGALRM;
310 #if defined(sigev_notify_thread_id)
311 sevp.sigev_notify_thread_id = (pid_t)syscall(SYS_gettid);
313 sevp._sigev_un._tid = (pid_t)syscall(SYS_gettid);
315 timer_create(CLOCK_THREAD_CPUTIME_ID, &sevp, &timerid);
316 its.it_interval.tv_sec = 0;
317 its.it_interval.tv_nsec = 0;
318 its.it_value.tv_sec = api_timeout;
319 its.it_value.tv_nsec = 0;
320 timer_settime(timerid, 0, &its, NULL);
326 timer_delete(timerid);
327 error_handler = older;
330 static void handle(struct afb_req req, const struct api_desc *api, const struct AFB_restapi *verb)
334 request.uuid = request.url = "fake";
335 request.prefix = api->prefix;
336 request.method = verb->name;
337 request.context = NULL;
338 request.restfull = 0;
340 request.config = NULL;
343 switch(verb->session) {
344 case AFB_SESSION_CREATE:
345 case AFB_SESSION_RENEW:
348 case AFB_SESSION_CLOSE:
349 case AFB_SESSION_CHECK:
352 case AFB_SESSION_NONE:
356 trapping_handle(&request, verb->callback);
358 if (verb->session == AFB_SESSION_CLOSE)
362 int afb_apis_handle(struct afb_req req, const char *api, size_t lenapi, const char *verb, size_t lenverb)
365 const struct api_desc *a;
366 const struct AFB_restapi *v;
369 for (i = 0 ; i < apis_count ; i++, a++) {
370 if (a->prefixlen == lenapi && !strncasecmp(a->prefix, api, lenapi)) {
372 for (j = 0 ; v->name ; j++, v++) {
373 if (!strncasecmp(v->name, verb, lenverb) && !v->name[lenverb]) {
378 afb_req_fail_f(req, "unknown-verb", "verb %.*s unknown within api %s", (int)lenverb, verb, a->prefix);