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"
48 struct AFB_plugin *plugin; /* descriptor */
51 void *handle; /* context of dlopen */
52 struct AFB_interface *interface;
55 static int api_timeout = 15;
56 static struct api_desc *apis_array = NULL;
57 static int apis_count = 0;
59 static const char plugin_register_function[] = "pluginRegister";
66 void afb_apis_free_context(int apiidx, void *context)
70 assert(0 <= apiidx && apiidx < apis_count);
71 cb = apis_array[apiidx].plugin->freeCtxCB;
78 int afb_apis_add_plugin(const char *path)
80 struct api_desc *apis;
81 struct AFB_plugin *plugin;
82 struct AFB_plugin *(*pluginRegisterFct) (const struct AFB_interface *interface);
83 struct AFB_interface *interface;
87 // This is a loadable library let's check if it's a plugin
88 handle = dlopen(path, RTLD_NOW | RTLD_LOCAL);
90 fprintf(stderr, "[%s] not loadable, continuing...\n", path);
94 /* retrieves the register function */
95 pluginRegisterFct = dlsym(handle, plugin_register_function);
96 if (!pluginRegisterFct) {
97 fprintf(stderr, "[%s] not an AFB plugin, continuing...\n", path);
101 fprintf(stderr, "[%s] is a valid AFB plugin\n", path);
103 /* allocates enough memory */
104 apis = realloc(apis_array, ((unsigned)apis_count + 1) * sizeof * apis);
106 fprintf(stderr, "ERROR: plugin [%s] memory missing. continuing...\n", path);
111 /* allocates the interface */
112 interface = calloc(1, sizeof *interface);
113 if (interface == NULL) {
114 fprintf(stderr, "ERROR: plugin [%s] memory missing. continuing...\n", path);
117 interface->verbose = 0;
118 interface->mode = AFB_MODE_LOCAL;
120 /* init the plugin */
121 plugin = pluginRegisterFct(interface);
122 if (plugin == NULL) {
123 fprintf(stderr, "ERROR: plugin [%s] register function failed. continuing...\n", path);
127 /* check the returned structure */
128 if (plugin->type != AFB_PLUGIN_JSON) {
129 fprintf(stderr, "ERROR: plugin [%s] invalid type %d...\n", path, plugin->type);
132 if (plugin->prefix == NULL || *plugin->prefix == 0) {
133 fprintf(stderr, "ERROR: plugin [%s] bad prefix...\n", path);
136 if (plugin->info == NULL || *plugin->info == 0) {
137 fprintf(stderr, "ERROR: plugin [%s] bad description...\n", path);
140 if (plugin->apis == NULL) {
141 fprintf(stderr, "ERROR: plugin [%s] no APIs...\n", path);
145 /* check previously existing plugin */
146 for (i = 0 ; i < apis_count ; i++) {
147 if (!strcasecmp(apis_array[i].prefix, plugin->prefix)) {
148 fprintf(stderr, "ERROR: plugin [%s] prefix %s duplicated...\n", path, plugin->prefix);
153 /* record the plugin */
155 fprintf(stderr, "Loading plugin[%lu] prefix=[%s] info=%s\n", (unsigned long)apis_count, plugin->prefix, plugin->info);
156 apis = &apis_array[apis_count];
157 apis->plugin = plugin;
158 apis->prefixlen = strlen(plugin->prefix);
159 apis->prefix = plugin->prefix;
160 apis->handle = handle;
161 apis->interface = interface;
174 static int adddirs(char path[PATH_MAX], size_t end)
178 struct dirent ent, *result;
181 /* open the DIR now */
184 fprintf(stderr, "ERROR in scanning plugin directory %s, %m\n", path);
188 fprintf(stderr, "Scanning dir=[%s] for plugins\n", path);
190 /* scan each entry */
194 readdir_r(dir, &ent, &result);
198 len = strlen(ent.d_name);
199 if (len + end >= PATH_MAX) {
200 fprintf(stderr, "path too long for %s\n", ent.d_name);
203 memcpy(&path[end], ent.d_name, len+1);
204 if (ent.d_type == DT_DIR) {
205 /* case of directories */
206 if (ent.d_name[0] == '.') {
209 if (ent.d_name[1] == '.' && len == 2)
212 rc = adddirs(path, end+len);;
213 } else if (ent.d_type == DT_REG) {
215 if (!strstr(ent.d_name, ".so"))
217 rc = afb_apis_add_plugin(path);
224 int afb_apis_add_directory(const char *path)
227 char buffer[PATH_MAX];
229 length = strlen(path);
230 if (length >= sizeof(buffer)) {
231 fprintf(stderr, "path too long %lu [%.99s...]\n", (unsigned long)length, path);
235 memcpy(buffer, path, length + 1);
236 return adddirs(buffer, length);
239 int afb_apis_add_path(const char *path)
244 rc = stat(path, &st);
246 fprintf(stderr, "Invalid plugin path [%s]: %m\n", path);
247 else if (S_ISDIR(st.st_mode))
248 rc = afb_apis_add_directory(path);
250 rc = afb_apis_add_plugin(path);
254 int afb_apis_add_pathset(const char *pathset)
256 static char sep[] = ":";
260 ps = strdupa(pathset);
262 p = strsep(&ps, sep);
265 rc = afb_apis_add_path(p);
269 // Check of apiurl is declare in this plugin and call it
270 extern __thread sigjmp_buf *error_handler;
271 static void trapping_handle(struct afb_req req, void(*cb)(struct afb_req))
273 volatile int signum, timerset;
275 sigjmp_buf jmpbuf, *older;
276 struct sigevent sevp;
277 struct itimerspec its;
279 // save context before calling the API
281 older = error_handler;
282 signum = setjmp(jmpbuf);
284 afb_req_fail_f(req, "aborted", "signal %d caught", signum);
287 error_handler = &jmpbuf;
288 if (api_timeout > 0) {
289 timerset = 1; /* TODO: check statuses */
290 sevp.sigev_notify = SIGEV_THREAD_ID;
291 sevp.sigev_signo = SIGALRM;
292 #if defined(sigev_notify_thread_id)
293 sevp.sigev_notify_thread_id = (pid_t)syscall(SYS_gettid);
295 sevp._sigev_un._tid = (pid_t)syscall(SYS_gettid);
297 timer_create(CLOCK_THREAD_CPUTIME_ID, &sevp, &timerid);
298 its.it_interval.tv_sec = 0;
299 its.it_interval.tv_nsec = 0;
300 its.it_value.tv_sec = api_timeout;
301 its.it_value.tv_nsec = 0;
302 timer_settime(timerid, 0, &its, NULL);
308 timer_delete(timerid);
309 error_handler = older;
312 static void handle(struct afb_req req, const struct AFB_restapi *verb)
314 switch(verb->session) {
315 case AFB_SESSION_CREATE:
316 if (!afb_req_session_create(req))
319 case AFB_SESSION_RENEW:
320 if (!afb_req_session_check(req, 1))
323 case AFB_SESSION_CLOSE:
324 case AFB_SESSION_CHECK:
325 if (!afb_req_session_check(req, 0))
328 case AFB_SESSION_NONE:
332 trapping_handle(req, verb->callback);
334 if (verb->session == AFB_SESSION_CLOSE)
335 afb_req_session_close(req);
338 int afb_apis_handle(struct afb_req req, struct AFB_clientCtx *context, const char *api, size_t lenapi, const char *verb, size_t lenverb)
341 const struct api_desc *a;
342 const struct AFB_restapi *v;
345 for (i = 0 ; i < apis_count ; i++, a++) {
346 if (a->prefixlen == lenapi && !strncasecmp(a->prefix, api, lenapi)) {
348 for (j = 0 ; v->name ; j++, v++) {
349 if (!strncasecmp(v->name, verb, lenverb) && !v->name[lenverb]) {
350 req.context = context->contexts[i];
352 context->contexts[i] = req.context;
356 afb_req_fail_f(req, "unknown-verb", "verb %.*s unknown within api %s", (int)lenverb, verb, a->prefix);