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 "afb-plugin.h"
41 #include "afb-req-itf.h"
42 #include "afb-poll-itf.h"
47 #include "utils-upoll.h"
50 struct AFB_plugin *plugin; /* descriptor */
53 void *handle; /* context of dlopen */
54 struct AFB_interface *interface;
57 static int api_timeout = 15;
58 static struct api_desc *apis_array = NULL;
59 static int apis_count = 0;
61 static const char plugin_register_function[] = "pluginRegister";
63 static const struct afb_poll_itf upoll_itf = {
64 .on_readable = (void*)upoll_on_readable,
65 .on_writable = (void*)upoll_on_writable,
66 .on_hangup = (void*)upoll_on_hangup,
67 .close = (void*)upoll_close
76 void afb_apis_free_context(int apiidx, void *context)
80 assert(0 <= apiidx && apiidx < apis_count);
81 cb = apis_array[apiidx].plugin->freeCtxCB;
88 static struct afb_poll itf_poll_open(int fd, void *closure)
90 struct afb_poll result;
91 result.data = upoll_open(fd, closure);
92 result.itf = result.data ? &upoll_itf : NULL;
97 int afb_apis_add_plugin(const char *path)
99 struct api_desc *apis;
100 struct AFB_plugin *plugin;
101 struct AFB_plugin *(*pluginRegisterFct) (const struct AFB_interface *interface);
102 struct AFB_interface *interface;
106 // This is a loadable library let's check if it's a plugin
107 handle = dlopen(path, RTLD_NOW | RTLD_LOCAL);
108 if (handle == NULL) {
109 fprintf(stderr, "[%s] not loadable, continuing...\n", path);
113 /* retrieves the register function */
114 pluginRegisterFct = dlsym(handle, plugin_register_function);
115 if (!pluginRegisterFct) {
116 fprintf(stderr, "[%s] not an AFB plugin, continuing...\n", path);
120 fprintf(stderr, "[%s] is a valid AFB plugin\n", path);
122 /* allocates enough memory */
123 apis = realloc(apis_array, ((unsigned)apis_count + 1) * sizeof * apis);
125 fprintf(stderr, "ERROR: plugin [%s] memory missing. continuing...\n", path);
130 /* allocates the interface */
131 interface = calloc(1, sizeof *interface);
132 if (interface == NULL) {
133 fprintf(stderr, "ERROR: plugin [%s] memory missing. continuing...\n", path);
136 interface->verbosity = 0;
137 interface->mode = AFB_MODE_LOCAL;
138 interface->poll_open = itf_poll_open;
140 /* init the plugin */
141 plugin = pluginRegisterFct(interface);
142 if (plugin == NULL) {
143 fprintf(stderr, "ERROR: plugin [%s] register function failed. continuing...\n", path);
147 /* check the returned structure */
148 if (plugin->type != AFB_PLUGIN_JSON) {
149 fprintf(stderr, "ERROR: plugin [%s] invalid type %d...\n", path, plugin->type);
152 if (plugin->prefix == NULL || *plugin->prefix == 0) {
153 fprintf(stderr, "ERROR: plugin [%s] bad prefix...\n", path);
156 if (plugin->info == NULL || *plugin->info == 0) {
157 fprintf(stderr, "ERROR: plugin [%s] bad description...\n", path);
160 if (plugin->apis == NULL) {
161 fprintf(stderr, "ERROR: plugin [%s] no APIs...\n", path);
165 /* check previously existing plugin */
166 for (i = 0 ; i < apis_count ; i++) {
167 if (!strcasecmp(apis_array[i].prefix, plugin->prefix)) {
168 fprintf(stderr, "ERROR: plugin [%s] prefix %s duplicated...\n", path, plugin->prefix);
173 /* record the plugin */
175 fprintf(stderr, "Loading plugin[%lu] prefix=[%s] info=%s\n", (unsigned long)apis_count, plugin->prefix, plugin->info);
176 apis = &apis_array[apis_count];
177 apis->plugin = plugin;
178 apis->prefixlen = strlen(plugin->prefix);
179 apis->prefix = plugin->prefix;
180 apis->handle = handle;
181 apis->interface = interface;
194 static int adddirs(char path[PATH_MAX], size_t end)
198 struct dirent ent, *result;
201 /* open the DIR now */
204 fprintf(stderr, "ERROR in scanning plugin directory %s, %m\n", path);
208 fprintf(stderr, "Scanning dir=[%s] for plugins\n", path);
210 /* scan each entry */
214 readdir_r(dir, &ent, &result);
218 len = strlen(ent.d_name);
219 if (len + end >= PATH_MAX) {
220 fprintf(stderr, "path too long for %s\n", ent.d_name);
223 memcpy(&path[end], ent.d_name, len+1);
224 if (ent.d_type == DT_DIR) {
225 /* case of directories */
226 if (ent.d_name[0] == '.') {
229 if (ent.d_name[1] == '.' && len == 2)
232 rc = adddirs(path, end+len);;
233 } else if (ent.d_type == DT_REG) {
235 if (!strstr(ent.d_name, ".so"))
237 rc = afb_apis_add_plugin(path);
244 int afb_apis_add_directory(const char *path)
247 char buffer[PATH_MAX];
249 length = strlen(path);
250 if (length >= sizeof(buffer)) {
251 fprintf(stderr, "path too long %lu [%.99s...]\n", (unsigned long)length, path);
255 memcpy(buffer, path, length + 1);
256 return adddirs(buffer, length);
259 int afb_apis_add_path(const char *path)
264 rc = stat(path, &st);
266 fprintf(stderr, "Invalid plugin path [%s]: %m\n", path);
267 else if (S_ISDIR(st.st_mode))
268 rc = afb_apis_add_directory(path);
270 rc = afb_apis_add_plugin(path);
274 int afb_apis_add_pathset(const char *pathset)
276 static char sep[] = ":";
280 ps = strdupa(pathset);
282 p = strsep(&ps, sep);
285 rc = afb_apis_add_path(p);
289 // Check of apiurl is declare in this plugin and call it
290 extern __thread sigjmp_buf *error_handler;
291 static void trapping_handle(struct afb_req req, void(*cb)(struct afb_req))
293 volatile int signum, timerset;
295 sigjmp_buf jmpbuf, *older;
296 struct sigevent sevp;
297 struct itimerspec its;
299 // save context before calling the API
301 older = error_handler;
302 signum = setjmp(jmpbuf);
304 afb_req_fail_f(req, "aborted", "signal %d caught", signum);
307 error_handler = &jmpbuf;
308 if (api_timeout > 0) {
309 timerset = 1; /* TODO: check statuses */
310 sevp.sigev_notify = SIGEV_THREAD_ID;
311 sevp.sigev_signo = SIGALRM;
312 sevp.sigev_value.sival_ptr = NULL;
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, const struct AFB_restapi *verb)
335 switch(verb->session) {
336 case AFB_SESSION_CREATE:
337 if (!afb_req_session_create(req))
340 case AFB_SESSION_RENEW:
341 if (!afb_req_session_check(req, 1))
344 case AFB_SESSION_CLOSE:
345 case AFB_SESSION_CHECK:
346 if (!afb_req_session_check(req, 0))
349 case AFB_SESSION_NONE:
353 trapping_handle(req, verb->callback);
355 if (verb->session == AFB_SESSION_CLOSE)
356 afb_req_session_close(req);
359 int afb_apis_handle(struct afb_req req, struct AFB_clientCtx *context, const char *api, size_t lenapi, const char *verb, size_t lenverb)
362 const struct api_desc *a;
363 const struct AFB_restapi *v;
366 for (i = 0 ; i < apis_count ; i++, a++) {
367 if (a->prefixlen == lenapi && !strncasecmp(a->prefix, api, lenapi)) {
369 for (j = 0 ; v->name ; j++, v++) {
370 if (!strncasecmp(v->name, verb, lenverb) && !v->name[lenverb]) {
371 req.context = &context->contexts[i];
376 afb_req_fail_f(req, "unknown-verb", "verb %.*s unknown within api %s", (int)lenverb, verb, a->prefix);