2 * Copyright (C) 2016 "IoT.bzh"
3 * Author José Bollo <jose.bollo@iot.bzh>
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 * Contain all generic part to handle REST/API
20 * https://www.gnu.org/software/libmicrohttpd/tutorial.html [search 'largepost.c']
32 #include <sys/types.h>
36 #include <sys/syscall.h>
39 #include "afb-plugin.h"
40 #include "afb-req-itf.h"
41 #include "afb-poll-itf.h"
45 #include "afb-api-so.h"
47 #include "utils-upoll.h"
50 struct AFB_plugin *plugin; /* descriptor */
51 void *handle; /* context of dlopen */
52 struct AFB_interface interface;
55 static int api_timeout = 15;
57 static const char plugin_register_function[] = "pluginRegister";
59 static const struct afb_poll_itf upoll_itf = {
60 .on_readable = (void*)upoll_on_readable,
61 .on_writable = (void*)upoll_on_writable,
62 .on_hangup = (void*)upoll_on_hangup,
63 .close = (void*)upoll_close
66 static struct afb_poll itf_poll_open(int fd, void *closure)
68 struct afb_poll result;
69 result.data = upoll_open(fd, closure);
70 result.itf = result.data ? &upoll_itf : NULL;
74 static void free_context(struct api_so_desc *desc, void *context)
78 cb = desc->plugin->freeCtxCB;
86 // Check of apiurl is declare in this plugin and call it
87 extern __thread sigjmp_buf *error_handler;
88 static void trapping_call(struct afb_req req, void(*cb)(struct afb_req))
90 volatile int signum, timerset;
92 sigjmp_buf jmpbuf, *older;
94 struct itimerspec its;
96 // save context before calling the API
98 older = error_handler;
99 signum = setjmp(jmpbuf);
101 afb_req_fail_f(req, "aborted", "signal %d caught", signum);
104 error_handler = &jmpbuf;
105 if (api_timeout > 0) {
106 timerset = 1; /* TODO: check statuses */
107 sevp.sigev_notify = SIGEV_THREAD_ID;
108 sevp.sigev_signo = SIGALRM;
109 sevp.sigev_value.sival_ptr = NULL;
110 #if defined(sigev_notify_thread_id)
111 sevp.sigev_notify_thread_id = (pid_t)syscall(SYS_gettid);
113 sevp._sigev_un._tid = (pid_t)syscall(SYS_gettid);
115 timer_create(CLOCK_THREAD_CPUTIME_ID, &sevp, &timerid);
116 its.it_interval.tv_sec = 0;
117 its.it_interval.tv_nsec = 0;
118 its.it_value.tv_sec = api_timeout;
119 its.it_value.tv_nsec = 0;
120 timer_settime(timerid, 0, &its, NULL);
126 timer_delete(timerid);
127 error_handler = older;
130 static void call_check(struct afb_req req, const struct AFB_restapi *verb)
132 switch(verb->session) {
133 case AFB_SESSION_CREATE:
134 if (!afb_req_session_create(req))
137 case AFB_SESSION_RENEW:
138 if (!afb_req_session_check(req, 1))
141 case AFB_SESSION_CLOSE:
142 case AFB_SESSION_CHECK:
143 if (!afb_req_session_check(req, 0))
146 case AFB_SESSION_NONE:
150 trapping_call(req, verb->callback);
152 if (verb->session == AFB_SESSION_CLOSE)
153 afb_req_session_close(req);
156 static void call(struct api_so_desc *desc, struct afb_req req, const char *verb, size_t lenverb)
158 const struct AFB_restapi *v;
160 v = desc->plugin->apis;
161 while (v->name && (strncasecmp(v->name, verb, lenverb) || v->name[lenverb]))
166 afb_req_fail_f(req, "unknown-verb", "verb %.*s unknown within api %s", (int)lenverb, verb, desc->plugin->prefix);
171 int afb_api_so_add_plugin(const char *path)
173 struct api_so_desc *desc;
174 struct AFB_plugin *(*pluginRegisterFct) (const struct AFB_interface *interface);
176 desc = calloc(1, sizeof *desc);
178 fprintf(stderr, "[%s] out of memory\n", path);
182 // This is a loadable library let's check if it's a plugin
183 desc->handle = dlopen(path, RTLD_NOW | RTLD_LOCAL);
184 if (desc->handle == NULL) {
185 fprintf(stderr, "[%s] not loadable, continuing...\n", path);
189 /* retrieves the register function */
190 pluginRegisterFct = dlsym(desc->handle, plugin_register_function);
191 if (!pluginRegisterFct) {
192 fprintf(stderr, "[%s] not an AFB plugin, continuing...\n", path);
196 fprintf(stderr, "[%s] is a valid AFB plugin\n", path);
198 /* init the interface */
199 desc->interface.verbosity = 0;
200 desc->interface.mode = AFB_MODE_LOCAL;
201 desc->interface.poll_open = itf_poll_open;
203 /* init the plugin */
204 desc->plugin = pluginRegisterFct(&desc->interface);
205 if (desc->plugin == NULL) {
206 fprintf(stderr, "ERROR: plugin [%s] register function failed. continuing...\n", path);
210 /* check the returned structure */
211 if (desc->plugin->type != AFB_PLUGIN_JSON) {
212 fprintf(stderr, "ERROR: plugin [%s] invalid type %d...\n", path, desc->plugin->type);
215 if (desc->plugin->prefix == NULL || *desc->plugin->prefix == 0) {
216 fprintf(stderr, "ERROR: plugin [%s] bad prefix...\n", path);
219 if (desc->plugin->info == NULL || *desc->plugin->info == 0) {
220 fprintf(stderr, "ERROR: plugin [%s] bad description...\n", path);
223 if (desc->plugin->apis == NULL) {
224 fprintf(stderr, "ERROR: plugin [%s] no APIs...\n", path);
228 /* records the plugin */
229 if (afb_apis_add(desc->plugin->prefix, (struct afb_api){
232 .free_context = (void*)free_context}) < 0) {
233 fprintf(stderr, "ERROR: plugin [%s] can't be registered...\n", path);
240 dlclose(desc->handle);
247 static int adddirs(char path[PATH_MAX], size_t end)
250 struct dirent ent, *result;
253 /* open the DIR now */
256 fprintf(stderr, "ERROR in scanning plugin directory %s, %m\n", path);
260 fprintf(stderr, "Scanning dir=[%s] for plugins\n", path);
262 /* scan each entry */
266 readdir_r(dir, &ent, &result);
270 len = strlen(ent.d_name);
271 if (len + end >= PATH_MAX) {
272 fprintf(stderr, "path too long for %s\n", ent.d_name);
275 memcpy(&path[end], ent.d_name, len+1);
276 if (ent.d_type == DT_DIR) {
277 /* case of directories */
278 if (ent.d_name[0] == '.') {
281 if (ent.d_name[1] == '.' && len == 2)
284 adddirs(path, end+len);;
285 } else if (ent.d_type == DT_REG) {
287 if (!strstr(ent.d_name, ".so"))
289 afb_api_so_add_plugin(path);
296 int afb_api_so_add_directory(const char *path)
299 char buffer[PATH_MAX];
301 length = strlen(path);
302 if (length >= sizeof(buffer)) {
303 fprintf(stderr, "path too long %lu [%.99s...]\n", (unsigned long)length, path);
307 memcpy(buffer, path, length + 1);
308 return adddirs(buffer, length);
311 int afb_api_so_add_path(const char *path)
316 rc = stat(path, &st);
318 fprintf(stderr, "Invalid plugin path [%s]: %m\n", path);
319 else if (S_ISDIR(st.st_mode))
320 rc = afb_api_so_add_directory(path);
322 rc = afb_api_so_add_plugin(path);
326 int afb_api_so_add_pathset(const char *pathset)
328 static char sep[] = ":";
331 ps = strdupa(pathset);
333 p = strsep(&ps, sep);
336 afb_api_so_add_path(p);