- int rc;
- DIR *dir;
- struct dirent ent, *result;
- size_t len;
-
- /* open the DIR now */
- dir = opendir(path);
- if (dir == NULL) {
- fprintf(stderr, "ERROR in scanning plugin directory %s, %m\n", path);
- return -1;
- }
- if (verbose)
- fprintf(stderr, "Scanning dir=[%s] for plugins\n", path);
-
- /* scan each entry */
- if (end)
- path[end++] = '/';
- for (;;) {
- readdir_r(dir, &ent, &result);
- if (result == NULL)
- break;
-
- len = strlen(ent.d_name);
- if (len + end >= PATH_MAX) {
- fprintf(stderr, "path too long for %s\n", ent.d_name);
- continue;
- }
- memcpy(&path[end], ent.d_name, len+1);
- if (ent.d_type == DT_DIR) {
- /* case of directories */
- if (ent.d_name[0] == '.') {
- if (len == 1)
- continue;
- if (ent.d_name[1] == '.' && len == 2)
- continue;
- }
- rc = adddirs(path, end+len);;
- } else if (ent.d_type == DT_REG) {
- /* case of files */
- if (!strstr(ent.d_name, ".so"))
- continue;
- rc = afb_apis_add_plugin(path);
- }
- }
- closedir(dir);
- return 0;
-}
-
-int afb_apis_add_directory(const char *path)
-{
- size_t length;
- char buffer[PATH_MAX];
-
- length = strlen(path);
- if (length >= sizeof(buffer)) {
- fprintf(stderr, "path too long %lu [%.99s...]\n", (unsigned long)length, path);
- return -1;
- }
-
- memcpy(buffer, path, length + 1);
- return adddirs(buffer, length);
-}
-
-int afb_apis_add_path(const char *path)
-{
- struct stat st;
- int rc;
-
- rc = stat(path, &st);
- if (rc < 0)
- fprintf(stderr, "Invalid plugin path [%s]: %m\n", path);
- else if (S_ISDIR(st.st_mode))
- rc = afb_apis_add_directory(path);
- else
- rc = afb_apis_add_plugin(path);
- return rc;
-}
-
-int afb_apis_add_pathset(const char *pathset)
-{
- static char sep[] = ":";
- char *ps, *p;
- int rc;
-
- ps = strdupa(pathset);
- for (;;) {
- p = strsep(&ps, sep);
- if (!p)
- return 0;
- rc = afb_apis_add_path(p);
- };
-}
-
-// Check of apiurl is declare in this plugin and call it
-extern __thread sigjmp_buf *error_handler;
-static void trapping_handle(AFB_request * request, struct json_object *(*cb)(AFB_request *,void*))
-{
- volatile int signum, timerset;
- timer_t timerid;
- sigjmp_buf jmpbuf, *older;
- struct sigevent sevp;
- struct itimerspec its;
-
- // save context before calling the API
- timerset = 0;
- older = error_handler;
- signum = setjmp(jmpbuf);
- if (signum != 0) {
- afb_req_fail_f(*request->areq, "aborted", "signal %d caught", signum);
- }
- else {
- error_handler = &jmpbuf;
- if (api_timeout > 0) {
- timerset = 1; /* TODO: check statuses */
- sevp.sigev_notify = SIGEV_THREAD_ID;
- sevp.sigev_signo = SIGALRM;
-#if defined(sigev_notify_thread_id)
- sevp.sigev_notify_thread_id = (pid_t)syscall(SYS_gettid);
-#else
- sevp._sigev_un._tid = (pid_t)syscall(SYS_gettid);
-#endif
- timer_create(CLOCK_THREAD_CPUTIME_ID, &sevp, &timerid);
- its.it_interval.tv_sec = 0;
- its.it_interval.tv_nsec = 0;
- its.it_value.tv_sec = api_timeout;
- its.it_value.tv_nsec = 0;
- timer_settime(timerid, 0, &its, NULL);
- }
-
- cb(request, NULL);
- }
- if (timerset)
- timer_delete(timerid);
- error_handler = older;
-}
-
-static void handle(struct afb_req req, const struct api_desc *api, const struct AFB_restapi *verb)
-{
- AFB_request request;
-
- request.uuid = request.url = "fake";
- request.prefix = api->prefix;
- request.method = verb->name;
- request.context = NULL;
- request.restfull = 0;
- request.errcode = 0;
- request.config = NULL;
- request.areq = &req;
-
- switch(verb->session) {
- case AFB_SESSION_CREATE:
- case AFB_SESSION_RENEW:
- /*if (check) new*/
- break;
- case AFB_SESSION_CLOSE:
- case AFB_SESSION_CHECK:
- /*check*/
- break;
- case AFB_SESSION_NONE:
- default:
- break;
- }
- trapping_handle(&request, verb->callback);
-
- if (verb->session == AFB_SESSION_CLOSE)
- /*close*/;