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"
44 #include "afb-poll-itf.h"
49 #include "utils-upoll.h"
52 struct AFB_plugin *plugin; /* descriptor */
55 void *handle; /* context of dlopen */
56 struct AFB_interface *interface;
59 static int api_timeout = 15;
60 static struct api_desc *apis_array = NULL;
61 static int apis_count = 0;
63 static const char plugin_register_function[] = "pluginRegister";
65 static const struct afb_poll_itf upoll_itf = {
66 .on_readable = (void*)upoll_on_readable,
67 .on_writable = (void*)upoll_on_writable,
68 .on_hangup = (void*)upoll_on_hangup,
69 .close = (void*)upoll_close
78 void afb_apis_free_context(int apiidx, void *context)
82 assert(0 <= apiidx && apiidx < apis_count);
83 cb = apis_array[apiidx].plugin->freeCtxCB;
90 static struct afb_poll itf_poll_open(int fd, void *closure)
92 struct afb_poll result;
93 result.data = upoll_open(fd, closure);
94 result.itf = result.data ? &upoll_itf : NULL;
99 int afb_apis_add_plugin(const char *path)
101 struct api_desc *apis;
102 struct AFB_plugin *plugin;
103 struct AFB_plugin *(*pluginRegisterFct) (const struct AFB_interface *interface);
104 struct AFB_interface *interface;
108 // This is a loadable library let's check if it's a plugin
109 handle = dlopen(path, RTLD_NOW | RTLD_LOCAL);
110 if (handle == NULL) {
111 fprintf(stderr, "[%s] not loadable, continuing...\n", path);
115 /* retrieves the register function */
116 pluginRegisterFct = dlsym(handle, plugin_register_function);
117 if (!pluginRegisterFct) {
118 fprintf(stderr, "[%s] not an AFB plugin, continuing...\n", path);
122 fprintf(stderr, "[%s] is a valid AFB plugin\n", path);
124 /* allocates enough memory */
125 apis = realloc(apis_array, ((unsigned)apis_count + 1) * sizeof * apis);
127 fprintf(stderr, "ERROR: plugin [%s] memory missing. continuing...\n", path);
132 /* allocates the interface */
133 interface = calloc(1, sizeof *interface);
134 if (interface == NULL) {
135 fprintf(stderr, "ERROR: plugin [%s] memory missing. continuing...\n", path);
138 interface->verbosity = 0;
139 interface->mode = AFB_MODE_LOCAL;
140 interface->poll_open = itf_poll_open;
142 /* init the plugin */
143 plugin = pluginRegisterFct(interface);
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 for (i = 0 ; i < apis_count ; i++) {
169 if (!strcasecmp(apis_array[i].prefix, plugin->prefix)) {
170 fprintf(stderr, "ERROR: plugin [%s] prefix %s duplicated...\n", path, plugin->prefix);
175 /* record the plugin */
177 fprintf(stderr, "Loading plugin[%lu] prefix=[%s] info=%s\n", (unsigned long)apis_count, plugin->prefix, plugin->info);
178 apis = &apis_array[apis_count];
179 apis->plugin = plugin;
180 apis->prefixlen = strlen(plugin->prefix);
181 apis->prefix = plugin->prefix;
182 apis->handle = handle;
183 apis->interface = interface;
196 static int adddirs(char path[PATH_MAX], size_t end)
200 struct dirent ent, *result;
203 /* open the DIR now */
206 fprintf(stderr, "ERROR in scanning plugin directory %s, %m\n", path);
210 fprintf(stderr, "Scanning dir=[%s] for plugins\n", path);
212 /* scan each entry */
216 readdir_r(dir, &ent, &result);
220 len = strlen(ent.d_name);
221 if (len + end >= PATH_MAX) {
222 fprintf(stderr, "path too long for %s\n", ent.d_name);
225 memcpy(&path[end], ent.d_name, len+1);
226 if (ent.d_type == DT_DIR) {
227 /* case of directories */
228 if (ent.d_name[0] == '.') {
231 if (ent.d_name[1] == '.' && len == 2)
234 rc = adddirs(path, end+len);;
235 } else if (ent.d_type == DT_REG) {
237 if (!strstr(ent.d_name, ".so"))
239 rc = afb_apis_add_plugin(path);
246 int afb_apis_add_directory(const char *path)
249 char buffer[PATH_MAX];
251 length = strlen(path);
252 if (length >= sizeof(buffer)) {
253 fprintf(stderr, "path too long %lu [%.99s...]\n", (unsigned long)length, path);
257 memcpy(buffer, path, length + 1);
258 return adddirs(buffer, length);
261 int afb_apis_add_path(const char *path)
266 rc = stat(path, &st);
268 fprintf(stderr, "Invalid plugin path [%s]: %m\n", path);
269 else if (S_ISDIR(st.st_mode))
270 rc = afb_apis_add_directory(path);
272 rc = afb_apis_add_plugin(path);
276 int afb_apis_add_pathset(const char *pathset)
278 static char sep[] = ":";
282 ps = strdupa(pathset);
284 p = strsep(&ps, sep);
287 rc = afb_apis_add_path(p);
291 // Check of apiurl is declare in this plugin and call it
292 extern __thread sigjmp_buf *error_handler;
293 static void trapping_handle(struct afb_req req, void(*cb)(struct afb_req))
295 volatile int signum, timerset;
297 sigjmp_buf jmpbuf, *older;
298 struct sigevent sevp;
299 struct itimerspec its;
301 // save context before calling the API
303 older = error_handler;
304 signum = setjmp(jmpbuf);
306 afb_req_fail_f(req, "aborted", "signal %d caught", signum);
309 error_handler = &jmpbuf;
310 if (api_timeout > 0) {
311 timerset = 1; /* TODO: check statuses */
312 sevp.sigev_notify = SIGEV_THREAD_ID;
313 sevp.sigev_signo = SIGALRM;
314 sevp.sigev_value.sival_ptr = NULL;
315 #if defined(sigev_notify_thread_id)
316 sevp.sigev_notify_thread_id = (pid_t)syscall(SYS_gettid);
318 sevp._sigev_un._tid = (pid_t)syscall(SYS_gettid);
320 timer_create(CLOCK_THREAD_CPUTIME_ID, &sevp, &timerid);
321 its.it_interval.tv_sec = 0;
322 its.it_interval.tv_nsec = 0;
323 its.it_value.tv_sec = api_timeout;
324 its.it_value.tv_nsec = 0;
325 timer_settime(timerid, 0, &its, NULL);
331 timer_delete(timerid);
332 error_handler = older;
335 static void handle(struct afb_req req, const struct AFB_restapi *verb)
337 switch(verb->session) {
338 case AFB_SESSION_CREATE:
339 if (!afb_req_session_create(req))
342 case AFB_SESSION_RENEW:
343 if (!afb_req_session_check(req, 1))
346 case AFB_SESSION_CLOSE:
347 case AFB_SESSION_CHECK:
348 if (!afb_req_session_check(req, 0))
351 case AFB_SESSION_NONE:
355 trapping_handle(req, verb->callback);
357 if (verb->session == AFB_SESSION_CLOSE)
358 afb_req_session_close(req);
361 int afb_apis_handle(struct afb_req req, struct AFB_clientCtx *context, const char *api, size_t lenapi, const char *verb, size_t lenverb)
364 const struct api_desc *a;
365 const struct AFB_restapi *v;
368 for (i = 0 ; i < apis_count ; i++, a++) {
369 if (a->prefixlen == lenapi && !strncasecmp(a->prefix, api, lenapi)) {
371 for (j = 0 ; v->name ; j++, v++) {
372 if (!strncasecmp(v->name, verb, lenverb) && !v->name[lenverb]) {
373 req.context = &context->contexts[i];
378 afb_req_fail_f(req, "unknown-verb", "verb %.*s unknown within api %s", (int)lenverb, verb, a->prefix);