X-Git-Url: https://gerrit.automotivelinux.org/gerrit/gitweb?a=blobdiff_plain;f=src%2Fafb-apis.c;h=9b2df3f30f1176d4a9e2fc89afa91ddce4b346d1;hb=c0453c34a58aac8150300ab829149a0ca4d9e5ee;hp=ca42743748292e650a3c48570dd567a093a64f8b;hpb=1205c90cccd3144bab24b4b5fd8dcbf0d0e6b570;p=src%2Fapp-framework-binder.git diff --git a/src/afb-apis.c b/src/afb-apis.c index ca427437..9b2df3f3 100644 --- a/src/afb-apis.c +++ b/src/afb-apis.c @@ -32,24 +32,44 @@ #include #include #include +#include +#include +#include +#include -#include "../include/local-def.h" +#include "local-def.h" +#include "afb-plugin.h" #include "afb-req-itf.h" +#include "afb-poll-itf.h" + +#include "session.h" #include "afb-apis.h" +#include "verbose.h" +#include "utils-upoll.h" struct api_desc { - AFB_plugin *plugin; /* descriptor */ + struct AFB_plugin *plugin; /* descriptor */ size_t prefixlen; const char *prefix; void *handle; /* context of dlopen */ + struct AFB_interface *interface; }; +static int api_timeout = 15; static struct api_desc *apis_array = NULL; static int apis_count = 0; static const char plugin_register_function[] = "pluginRegister"; +static const struct afb_poll_itf upoll_itf = { + .on_readable = (void*)upoll_on_readable, + .on_writable = (void*)upoll_on_writable, + .on_hangup = (void*)upoll_on_hangup, + .close = (void*)upoll_close +}; + + int afb_apis_count() { return apis_count; @@ -67,48 +87,23 @@ void afb_apis_free_context(int apiidx, void *context) free(context); } -const struct AFB_restapi *afb_apis_get(int apiidx, int verbidx) +static struct afb_poll itf_poll_open(int fd, void *closure) { - assert(0 <= apiidx && apiidx < apis_count); - return &apis_array[apiidx].plugin->apis[verbidx]; + struct afb_poll result; + result.data = upoll_open(fd, closure); + result.itf = result.data ? &upoll_itf : NULL; + return result; } -int afb_apis_get_verbidx(int apiidx, const char *name) -{ - const struct AFB_restapi *apis; - int idx; - - assert(0 <= apiidx && apiidx < apis_count); - apis = apis_array[apiidx].plugin->apis; - for (idx = 0 ; apis[idx].name ; idx++) - if (!strcasecmp(apis[idx].name, name)) - return idx; - return -1; -} - -int afb_apis_get_apiidx(const char *prefix, size_t length) -{ - int i; - const struct api_desc *a; - - if (!length) - length = strlen(prefix); - - for (i = 0 ; i < apis_count ; i++) { - a = &apis_array[i]; - if (a->prefixlen == length && !strcasecmp(a->prefix, prefix)) - return i; - } - return -1; -} int afb_apis_add_plugin(const char *path) { struct api_desc *apis; - AFB_plugin *plugin; - AFB_plugin *(*pluginRegisterFct) (void); + struct AFB_plugin *plugin; + struct AFB_plugin *(*pluginRegisterFct) (const struct AFB_interface *interface); + struct AFB_interface *interface; void *handle; - size_t len; + int i; // This is a loadable library let's check if it's a plugin handle = dlopen(path, RTLD_NOW | RTLD_LOCAL); @@ -123,7 +118,7 @@ int afb_apis_add_plugin(const char *path) fprintf(stderr, "[%s] not an AFB plugin, continuing...\n", path); goto error2; } - if (verbose) + if (verbosity) fprintf(stderr, "[%s] is a valid AFB plugin\n", path); /* allocates enough memory */ @@ -134,50 +129,64 @@ int afb_apis_add_plugin(const char *path) } apis_array = apis; + /* allocates the interface */ + interface = calloc(1, sizeof *interface); + if (interface == NULL) { + fprintf(stderr, "ERROR: plugin [%s] memory missing. continuing...\n", path); + goto error2; + } + interface->verbosity = 0; + interface->mode = AFB_MODE_LOCAL; + interface->poll_open = itf_poll_open; + /* init the plugin */ - plugin = pluginRegisterFct(); + plugin = pluginRegisterFct(interface); if (plugin == NULL) { fprintf(stderr, "ERROR: plugin [%s] register function failed. continuing...\n", path); - goto error2; + goto error3; } /* check the returned structure */ if (plugin->type != AFB_PLUGIN_JSON) { fprintf(stderr, "ERROR: plugin [%s] invalid type %d...\n", path, plugin->type); - goto error2; + goto error3; } if (plugin->prefix == NULL || *plugin->prefix == 0) { fprintf(stderr, "ERROR: plugin [%s] bad prefix...\n", path); - goto error2; + goto error3; } if (plugin->info == NULL || *plugin->info == 0) { fprintf(stderr, "ERROR: plugin [%s] bad description...\n", path); - goto error2; + goto error3; } if (plugin->apis == NULL) { fprintf(stderr, "ERROR: plugin [%s] no APIs...\n", path); - goto error2; + goto error3; } /* check previously existing plugin */ - len = strlen(plugin->prefix); - if (afb_apis_get_apiidx(plugin->prefix, len) >= 0) { - fprintf(stderr, "ERROR: plugin [%s] prefix %s duplicated...\n", path, plugin->prefix); - goto error2; + for (i = 0 ; i < apis_count ; i++) { + if (!strcasecmp(apis_array[i].prefix, plugin->prefix)) { + fprintf(stderr, "ERROR: plugin [%s] prefix %s duplicated...\n", path, plugin->prefix); + goto error2; + } } /* record the plugin */ - if (verbose) + if (verbosity) fprintf(stderr, "Loading plugin[%lu] prefix=[%s] info=%s\n", (unsigned long)apis_count, plugin->prefix, plugin->info); apis = &apis_array[apis_count]; apis->plugin = plugin; - apis->prefixlen = len; + apis->prefixlen = strlen(plugin->prefix); apis->prefix = plugin->prefix; apis->handle = handle; + apis->interface = interface; apis_count++; return 0; +error3: + free(interface); error2: dlclose(handle); error: @@ -197,7 +206,7 @@ static int adddirs(char path[PATH_MAX], size_t end) fprintf(stderr, "ERROR in scanning plugin directory %s, %m\n", path); return -1; } - if (verbose) + if (verbosity) fprintf(stderr, "Scanning dir=[%s] for plugins\n", path); /* scan each entry */ @@ -279,67 +288,77 @@ 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; + sevp.sigev_value.sival_ptr = NULL; +#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) +static void handle(struct afb_req req, 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: + if (!afb_req_session_create(req)) + return; + break; case AFB_SESSION_RENEW: - /*if (check) new*/ + if (!afb_req_session_check(req, 1)) + return; break; case AFB_SESSION_CLOSE: case AFB_SESSION_CHECK: - /*check*/ + if (!afb_req_session_check(req, 0)) + return; break; case AFB_SESSION_NONE: default: break; } - verb->callback(&request, NULL); + trapping_handle(req, verb->callback); if (verb->session == AFB_SESSION_CLOSE) - /*del*/; + afb_req_session_close(req); } -int afb_apis_handle(struct afb_req req, const char *api, size_t lenapi, const char *verb, size_t lenverb) +int afb_apis_handle(struct afb_req req, struct AFB_clientCtx *context, const char *api, size_t lenapi, const char *verb, size_t lenverb) { int i, j; const struct api_desc *a; @@ -347,15 +366,17 @@ 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]) { - handle(req, a, v); + req.context = &context->contexts[i]; + handle(req, v); 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;