X-Git-Url: https://gerrit.automotivelinux.org/gerrit/gitweb?a=blobdiff_plain;f=src%2Fafb-apis.c;h=0e257883503b5cb0d0ba3a329d16f7a975e12f42;hb=ede362db9ea82b85a531849c21582f1692bf0d4d;hp=0a04ed73e3eb5b1b3e502a59f837daf2a7ec808b;hpb=b8d4c81cc8175ce49c77d41e572a9f1a2e367cdc;p=src%2Fapp-framework-binder.git diff --git a/src/afb-apis.c b/src/afb-apis.c index 0a04ed73..0e257883 100644 --- a/src/afb-apis.c +++ b/src/afb-apis.c @@ -1,394 +1,300 @@ /* - * Copyright (C) 2016 "IoT.bzh" + * Copyright (C) 2016, 2017 "IoT.bzh" * Author "Fulup Ar Foll" * Author José Bollo * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. + * http://www.apache.org/licenses/LICENSE-2.0 * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - * - * Contain all generic part to handle REST/API - * - * https://www.gnu.org/software/libmicrohttpd/tutorial.html [search 'largepost.c'] + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. */ #define _GNU_SOURCE +#include #include -#include #include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -#include "local-def.h" - -#include "afb-plugin.h" -#include "afb-req-itf.h" +#include + +#include "afb-session.h" +#include "verbose.h" #include "afb-apis.h" +#include "afb-context.h" +#include "afb-hook.h" +#include "afb-xreq.h" +#include "jobs.h" + +#include +/** + * Internal description of an api + */ struct api_desc { - AFB_plugin *plugin; /* descriptor */ - size_t prefixlen; - const char *prefix; - void *handle; /* context of dlopen */ + const char *name; /**< name of the api */ + struct afb_api api; /**< handler of the api */ }; -static int api_timeout = 15; static struct api_desc *apis_array = NULL; static int apis_count = 0; +static int apis_timeout = 15; -static const char plugin_register_function[] = "pluginRegister"; - -int afb_apis_count() -{ - return apis_count; -} - -void afb_apis_free_context(int apiidx, void *context) -{ - void (*cb)(void*); - - assert(0 <= apiidx && apiidx < apis_count); - cb = apis_array[apiidx].plugin->freeCtxCB; - if (cb) - cb(context); - else - free(context); -} - -/* -const struct AFB_restapi *afb_apis_get(int apiidx, int verbidx) -{ - assert(0 <= apiidx && apiidx < apis_count); - return &apis_array[apiidx].plugin->apis[verbidx]; -} - -int afb_apis_get_verbidx(int apiidx, const char *name) +/** + * Set the API timeout + * @param to the timeout in seconds + */ +void afb_apis_set_timeout(int to) { - 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; + apis_timeout = to; } -*/ -int afb_apis_get_apiidx(const char *prefix, size_t length) +/** + * Checks wether 'name' is a valid API name. + * @return 1 if valid, 0 otherwise + */ +int afb_apis_is_valid_api_name(const char *name) { - 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; + unsigned char c; + + c = (unsigned char)*name; + if (c == 0) + /* empty names aren't valid */ + return 0; + + do { + if (c < (unsigned char)'\x80') { + switch(c) { + default: + if (c > ' ') + break; + case '"': + case '#': + case '%': + case '&': + case '\'': + case '/': + case '?': + case '`': + case '\\': + case '\x7f': + return 0; + } + } + c = (unsigned char)*++name; + } while(c != 0); + return 1; } -int afb_apis_add_plugin(const char *path) +/** + * Adds the api of 'name' described by 'api'. + * @param name the name of the api to add (have to survive, not copied!) + * @param api the api + * @returns 0 in case of success or -1 in case + * of error with errno set: + * - EINVAL if name isn't valid + * - EEXIST if name already registered + * - ENOMEM when out of memory + */ +int afb_apis_add(const char *name, struct afb_api api) { struct api_desc *apis; - AFB_plugin *plugin; - AFB_plugin *(*pluginRegisterFct) (void); - void *handle; - size_t len; - - // This is a loadable library let's check if it's a plugin - handle = dlopen(path, RTLD_NOW | RTLD_LOCAL); - if (handle == NULL) { - fprintf(stderr, "[%s] not loadable, continuing...\n", path); + int i, c; + + /* Checks the api name */ + if (!afb_apis_is_valid_api_name(name)) { + ERROR("invalid api name forbidden (name is '%s')", name); + errno = EINVAL; goto error; } - /* retrieves the register function */ - pluginRegisterFct = dlsym(handle, plugin_register_function); - if (!pluginRegisterFct) { - fprintf(stderr, "[%s] not an AFB plugin, continuing...\n", path); - goto error2; + /* check previously existing plugin */ + for (i = 0 ; i < apis_count ; i++) { + c = strcasecmp(apis_array[i].name, name); + if (c == 0) { + ERROR("api of name %s already exists", name); + errno = EEXIST; + goto error; + } + if (c > 0) + break; } - if (verbose) - fprintf(stderr, "[%s] is a valid AFB plugin\n", path); /* allocates enough memory */ apis = realloc(apis_array, ((unsigned)apis_count + 1) * sizeof * apis); if (apis == NULL) { - fprintf(stderr, "ERROR: plugin [%s] memory missing. continuing...\n", path); - goto error2; + ERROR("out of memory"); + errno = ENOMEM; + goto error; } apis_array = apis; - /* init the plugin */ - plugin = pluginRegisterFct(); - if (plugin == NULL) { - fprintf(stderr, "ERROR: plugin [%s] register function failed. continuing...\n", path); - goto error2; - } - - /* check the returned structure */ - if (plugin->type != AFB_PLUGIN_JSON) { - fprintf(stderr, "ERROR: plugin [%s] invalid type %d...\n", path, plugin->type); - goto error2; - } - if (plugin->prefix == NULL || *plugin->prefix == 0) { - fprintf(stderr, "ERROR: plugin [%s] bad prefix...\n", path); - goto error2; - } - if (plugin->info == NULL || *plugin->info == 0) { - fprintf(stderr, "ERROR: plugin [%s] bad description...\n", path); - goto error2; - } - if (plugin->apis == NULL) { - fprintf(stderr, "ERROR: plugin [%s] no APIs...\n", path); - goto error2; - } - - /* 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; + /* copy higher part of the array */ + c = apis_count; + while (c > i) { + apis_array[c] = apis_array[c - 1]; + c--; } /* record the plugin */ - if (verbose) - 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->prefix = plugin->prefix; - apis->handle = handle; + apis = &apis_array[i]; + apis->api = api; + apis->name = name; apis_count++; + NOTICE("API %s added", name); + return 0; -error2: - dlclose(handle); error: return -1; } -static int adddirs(char path[PATH_MAX], size_t end) +/** + * Search the 'api'. + * @param api the api of the verb + * @return the descriptor if found or NULL otherwise + */ +static const struct api_desc *search(const char *api) { - 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); + int i, c, up, lo; + const struct api_desc *a; - /* scan each entry */ - if (end) - path[end++] = '/'; + /* dichotomic search of the api */ + /* initial slice */ + lo = 0; + up = apis_count; 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; + /* check remaining slice */ + if (lo >= up) { + /* not found */ + return NULL; } - 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); + /* check the mid of the slice */ + i = (lo + up) >> 1; + a = &apis_array[i]; + c = strcasecmp(a->name, api); + if (c == 0) { + /* found */ + return a; } + /* update the slice */ + if (c < 0) + lo = i + 1; + else + up = i; } - closedir(dir); - return 0; } -int afb_apis_add_directory(const char *path) +/** + * Starts a service by its 'api' name. + * @param api name of the service to start + * @param share_session if true start the servic"e in a shared session + * if false start it in its own session + * @param onneed if true start the service if possible, if false the api + * must be a service + * @return a positive number on success + */ +int afb_apis_start_service(const char *api, int share_session, int onneed) { - size_t length; - char buffer[PATH_MAX]; + int i; - length = strlen(path); - if (length >= sizeof(buffer)) { - fprintf(stderr, "path too long %lu [%.99s...]\n", (unsigned long)length, path); - return -1; + for (i = 0 ; i < apis_count ; i++) { + if (!strcasecmp(apis_array[i].name, api)) + return apis_array[i].api.service_start(apis_array[i].api.closure, share_session, onneed); } - - memcpy(buffer, path, length + 1); - return adddirs(buffer, length); + ERROR("can't find service %s", api); + errno = ENOENT; + return -1; } -int afb_apis_add_path(const char *path) +/** + * Starts all possible services but stops at first error. + * @param share_session if true start the servic"e in a shared session + * if false start it in its own session + * @return 0 on success or a negative number when an error is found + */ +int afb_apis_start_all_services(int share_session) { - struct stat st; - int rc; + int i, 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; + for (i = 0 ; i < apis_count ; i++) { + rc = apis_array[i].api.service_start(apis_array[i].api.closure, share_session, 1); + if (rc < 0) + return rc; + } + return 0; } -int afb_apis_add_pathset(const char *pathset) +/** + * Internal direct dispatch of the request 'xreq' + * @param xreq the request to dispatch + */ +static void do_call_direct(struct afb_xreq *xreq) { - static char sep[] = ":"; - char *ps, *p; - int rc; + const struct api_desc *a; - ps = strdupa(pathset); - for (;;) { - p = strsep(&ps, sep); - if (!p) - return 0; - rc = afb_apis_add_path(p); - }; + /* search the api */ + a = search(xreq->api); + if (!a) + afb_xreq_fail_f(xreq, "unknown-api", "api %s not found", xreq->api); + else { + xreq->context.api_key = a->api.closure; + a->api.call(a->api.closure, xreq); + } } -// Check of apiurl is declare in this plugin and call it -extern __thread sigjmp_buf *error_handler; -static void trapping_handle(struct afb_req req, void(*cb)(struct afb_req)) +/** + * Asynchronous dispatch callback for the request 'xreq' + * @param signum 0 on normal flow or the signal number that interupted the normal flow + */ +static void do_call_async(int signum, void *arg) { - 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(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); - } + struct afb_xreq *xreq = arg; - cb(req); + if (signum != 0) + afb_xreq_fail_f(xreq, "aborted", "signal %s(%d) caught", strsignal(signum), signum); + else { + do_call_direct(xreq); } - if (timerset) - timer_delete(timerid); - error_handler = older; + afb_xreq_unref(xreq); } -static void handle(struct afb_req req, int idxapi, const struct AFB_restapi *verb) +/** + * Dispatch the request 'xreq' synchronously and directly. + * @param xreq the request to dispatch + */ +void afb_apis_call_direct(struct afb_xreq *xreq) { - switch(verb->session) { - case AFB_SESSION_CREATE: - /* - req.context = afb_req_session_create(req, idxapi); - if (req.context == NULL) - return; - break; - */ - case AFB_SESSION_RENEW: - /* - req.context = afb_req_session_check(req, idxapi, 1); - if (req.context == NULL) - return; - */ - break; - case AFB_SESSION_CLOSE: - case AFB_SESSION_CHECK: - /* - req.context = afb_req_session_check(req, idxapi, 1); - if (req.context == NULL) - return; - */ - break; - case AFB_SESSION_NONE: - default: - req.context = NULL; - break; - } - trapping_handle(req, verb->callback); + /* init hooking the request */ + // TODO req = afb_hook_req_call(req, context, api, verb); - if (verb->session == AFB_SESSION_CLOSE) { - /* - afb_req_session_close(req); - */ - } + do_call_direct(xreq); } -int afb_apis_handle(struct afb_req req, const char *api, size_t lenapi, const char *verb, size_t lenverb) +/** + * Dispatch the request 'xreq' asynchronously. + * @param xreq the request to dispatch + */ +void afb_apis_call(struct afb_xreq *xreq) { - int i, j; - const struct api_desc *a; - const struct AFB_restapi *v; - - a = apis_array; - for (i = 0 ; i < apis_count ; i++, a++) { - 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, i, v); - return 1; - } - } - afb_req_fail_f(req, "unknown-verb", "verb %.*s unknown within api %s", (int)lenverb, verb, a->prefix); - return 1; - } + int rc; + + /* init hooking the request */ + // TODO req = afb_hook_req_call(req, context, api, verb); + + afb_xreq_addref(xreq); + rc = jobs_queue(NULL, apis_timeout, do_call_async, xreq); + if (rc < 0) { + /* TODO: allows or not to proccess it directly as when no threading? (see above) */ + ERROR("can't process job with threads: %m"); + afb_xreq_fail_f(xreq, "cancelled", "not able to create a job for the task"); + afb_xreq_unref(xreq); } - return 0; }