X-Git-Url: https://gerrit.automotivelinux.org/gerrit/gitweb?a=blobdiff_plain;f=src%2Fafb-apis.c;h=7dcb101ade2bec61ae7d4bcd4c330e3fda1849ea;hb=70558f02b1c030c4290a0146313e3052fc0ea715;hp=ca42743748292e650a3c48570dd567a093a64f8b;hpb=1205c90cccd3144bab24b4b5fd8dcbf0d0e6b570;p=src%2Fapp-framework-binder.git diff --git a/src/afb-apis.c b/src/afb-apis.c index ca427437..7dcb101a 100644 --- a/src/afb-apis.c +++ b/src/afb-apis.c @@ -32,8 +32,12 @@ #include #include #include +#include +#include +#include +#include -#include "../include/local-def.h" +#include "local-def.h" #include "afb-req-itf.h" #include "afb-apis.h" @@ -45,6 +49,7 @@ struct api_desc { void *handle; /* context of dlopen */ }; +static int api_timeout = 15; static struct api_desc *apis_array = NULL; static int apis_count = 0; @@ -279,47 +284,51 @@ int afb_apis_add_pathset(const char *pathset) }; } -/* // Check of apiurl is declare in this plugin and call it extern __thread sigjmp_buf *error_handler; -static int callPluginApi(AFB_request * request) +static void trapping_handle(struct afb_req req, void(*cb)(struct afb_req)) { + volatile int signum, timerset; + timer_t timerid; sigjmp_buf jmpbuf, *older; + struct sigevent sevp; + struct itimerspec its; // save context before calling the API - status = setjmp(jmpbuf); - if (status != 0) { - return 0; + timerset = 0; + older = error_handler; + signum = setjmp(jmpbuf); + if (signum != 0) { + afb_req_fail_f(req, "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); + } - // Trigger a timer to protect from unacceptable long time execution - if (request->config->apiTimeout > 0) - alarm((unsigned)request->config->apiTimeout); - - older = error_handler; - error_handler = &jmpbuf; - doCallPluginApi(request, apiidx, verbidx, context); + cb(req); + } + if (timerset) + timer_delete(timerid); error_handler = older; - - // cancel timeout and plugin signal handle before next call - alarm(0); - return 1; } -*/ 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: @@ -331,12 +340,13 @@ static void handle(struct afb_req req, const struct api_desc *api, const struct break; case AFB_SESSION_NONE: default: + req.context = NULL; break; } - verb->callback(&request, NULL); + trapping_handle(req, verb->callback); if (verb->session == AFB_SESSION_CLOSE) - /*del*/; + /*close*/; } int afb_apis_handle(struct afb_req req, const char *api, size_t lenapi, const char *verb, size_t lenverb) @@ -347,7 +357,7 @@ int afb_apis_handle(struct afb_req req, const char *api, size_t lenapi, const ch a = apis_array; for (i = 0 ; i < apis_count ; i++, a++) { - if (a->prefixlen == lenapi && !strcasecmp(a->prefix, api)) { + if (a->prefixlen == lenapi && !strncasecmp(a->prefix, api, lenapi)) { v = a->plugin->apis; for (j = 0 ; v->name ; j++, v++) { if (!strncasecmp(v->name, verb, lenverb) && !v->name[lenverb]) { @@ -355,7 +365,8 @@ int afb_apis_handle(struct afb_req req, const char *api, size_t lenapi, const ch return 1; } } - break; + afb_req_fail_f(req, "unknown-verb", "verb %.*s unknown within api %s", (int)lenverb, verb, a->prefix); + return 1; } } return 0;