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"
46 #include "afb-api-so.h"
48 #include "utils-upoll.h"
51 struct AFB_plugin *plugin; /* descriptor */
52 void *handle; /* context of dlopen */
53 struct AFB_interface interface;
56 static int api_timeout = 15;
58 static const char plugin_register_function[] = "pluginRegister";
60 static const struct afb_poll_itf upoll_itf = {
61 .on_readable = (void*)upoll_on_readable,
62 .on_writable = (void*)upoll_on_writable,
63 .on_hangup = (void*)upoll_on_hangup,
64 .close = (void*)upoll_close
67 static struct afb_poll itf_poll_open(int fd, void *closure)
69 struct afb_poll result;
70 result.data = upoll_open(fd, closure);
71 result.itf = result.data ? &upoll_itf : NULL;
75 static void free_context(struct api_so_desc *desc, void *context)
79 cb = desc->plugin->freeCtxCB;
87 // Check of apiurl is declare in this plugin and call it
88 extern __thread sigjmp_buf *error_handler;
89 static void trapping_call(struct afb_req req, void(*cb)(struct afb_req))
91 volatile int signum, timerset;
93 sigjmp_buf jmpbuf, *older;
95 struct itimerspec its;
97 // save context before calling the API
99 older = error_handler;
100 signum = setjmp(jmpbuf);
102 afb_req_fail_f(req, "aborted", "signal %d caught", signum);
105 error_handler = &jmpbuf;
106 if (api_timeout > 0) {
107 timerset = 1; /* TODO: check statuses */
108 sevp.sigev_notify = SIGEV_THREAD_ID;
109 sevp.sigev_signo = SIGALRM;
110 sevp.sigev_value.sival_ptr = NULL;
111 #if defined(sigev_notify_thread_id)
112 sevp.sigev_notify_thread_id = (pid_t)syscall(SYS_gettid);
114 sevp._sigev_un._tid = (pid_t)syscall(SYS_gettid);
116 timer_create(CLOCK_THREAD_CPUTIME_ID, &sevp, &timerid);
117 its.it_interval.tv_sec = 0;
118 its.it_interval.tv_nsec = 0;
119 its.it_value.tv_sec = api_timeout;
120 its.it_value.tv_nsec = 0;
121 timer_settime(timerid, 0, &its, NULL);
127 timer_delete(timerid);
128 error_handler = older;
131 static void call_check(struct afb_req req, const struct AFB_restapi *verb)
133 switch(verb->session) {
134 case AFB_SESSION_CREATE:
135 if (!afb_req_session_create(req))
138 case AFB_SESSION_RENEW:
139 if (!afb_req_session_check(req, 1))
142 case AFB_SESSION_CLOSE:
143 case AFB_SESSION_CHECK:
144 if (!afb_req_session_check(req, 0))
147 case AFB_SESSION_NONE:
151 trapping_call(req, verb->callback);
153 if (verb->session == AFB_SESSION_CLOSE)
154 afb_req_session_close(req);
157 static void call(struct api_so_desc *desc, struct afb_req req, const char *verb, size_t lenverb)
159 const struct AFB_restapi *v;
161 v = desc->plugin->apis;
162 while (v->name && (strncasecmp(v->name, verb, lenverb) || v->name[lenverb]))
167 afb_req_fail_f(req, "unknown-verb", "verb %.*s unknown within api %s", (int)lenverb, verb, desc->plugin->prefix);
172 int afb_api_so_add_plugin(const char *path)
174 struct api_so_desc *desc;
175 struct AFB_plugin *(*pluginRegisterFct) (const struct AFB_interface *interface);
177 desc = calloc(1, sizeof *desc);
179 fprintf(stderr, "[%s] out of memory\n", path);
183 // This is a loadable library let's check if it's a plugin
184 desc->handle = dlopen(path, RTLD_NOW | RTLD_LOCAL);
185 if (desc->handle == NULL) {
186 fprintf(stderr, "[%s] not loadable, continuing...\n", path);
190 /* retrieves the register function */
191 pluginRegisterFct = dlsym(desc->handle, plugin_register_function);
192 if (!pluginRegisterFct) {
193 fprintf(stderr, "[%s] not an AFB plugin, continuing...\n", path);
197 fprintf(stderr, "[%s] is a valid AFB plugin\n", path);
199 /* init the interface */
200 desc->interface.verbosity = 0;
201 desc->interface.mode = AFB_MODE_LOCAL;
202 desc->interface.poll_open = itf_poll_open;
204 /* init the plugin */
205 desc->plugin = pluginRegisterFct(&desc->interface);
206 if (desc->plugin == NULL) {
207 fprintf(stderr, "ERROR: plugin [%s] register function failed. continuing...\n", path);
211 /* check the returned structure */
212 if (desc->plugin->type != AFB_PLUGIN_JSON) {
213 fprintf(stderr, "ERROR: plugin [%s] invalid type %d...\n", path, desc->plugin->type);
216 if (desc->plugin->prefix == NULL || *desc->plugin->prefix == 0) {
217 fprintf(stderr, "ERROR: plugin [%s] bad prefix...\n", path);
220 if (desc->plugin->info == NULL || *desc->plugin->info == 0) {
221 fprintf(stderr, "ERROR: plugin [%s] bad description...\n", path);
224 if (desc->plugin->apis == NULL) {
225 fprintf(stderr, "ERROR: plugin [%s] no APIs...\n", path);
229 /* records the plugin */
230 if (afb_apis_add(desc->plugin->prefix, (struct afb_api){
233 .free_context = (void*)free_context}) < 0) {
234 fprintf(stderr, "ERROR: plugin [%s] can't be registered...\n", path);
241 dlclose(desc->handle);
248 static int adddirs(char path[PATH_MAX], size_t end)
251 struct dirent ent, *result;
254 /* open the DIR now */
257 fprintf(stderr, "ERROR in scanning plugin directory %s, %m\n", path);
261 fprintf(stderr, "Scanning dir=[%s] for plugins\n", path);
263 /* scan each entry */
267 readdir_r(dir, &ent, &result);
271 len = strlen(ent.d_name);
272 if (len + end >= PATH_MAX) {
273 fprintf(stderr, "path too long for %s\n", ent.d_name);
276 memcpy(&path[end], ent.d_name, len+1);
277 if (ent.d_type == DT_DIR) {
278 /* case of directories */
279 if (ent.d_name[0] == '.') {
282 if (ent.d_name[1] == '.' && len == 2)
285 adddirs(path, end+len);;
286 } else if (ent.d_type == DT_REG) {
288 if (!strstr(ent.d_name, ".so"))
290 afb_api_so_add_plugin(path);
297 int afb_api_so_add_directory(const char *path)
300 char buffer[PATH_MAX];
302 length = strlen(path);
303 if (length >= sizeof(buffer)) {
304 fprintf(stderr, "path too long %lu [%.99s...]\n", (unsigned long)length, path);
308 memcpy(buffer, path, length + 1);
309 return adddirs(buffer, length);
312 int afb_api_so_add_path(const char *path)
317 rc = stat(path, &st);
319 fprintf(stderr, "Invalid plugin path [%s]: %m\n", path);
320 else if (S_ISDIR(st.st_mode))
321 rc = afb_api_so_add_directory(path);
323 rc = afb_api_so_add_plugin(path);
327 int afb_api_so_add_pathset(const char *pathset)
329 static char sep[] = ":";
332 ps = strdupa(pathset);
334 p = strsep(&ps, sep);
337 afb_api_so_add_path(p);