-extern __thread sigjmp_buf *error_handler;
-
-struct api_so_desc {
- struct AFB_plugin *plugin; /* descriptor */
- size_t apilength;
- void *handle; /* context of dlopen */
- struct AFB_interface interface; /* interface */
-};
-
-static int api_timeout = 15;
-
-static const char plugin_register_function[] = "pluginRegister";
-
-static void afb_api_so_evmgr_push(struct api_so_desc *desc, const char *name, struct json_object *object)
-{
- size_t length;
- char *event;
-
- assert(desc->plugin != NULL);
- length = strlen(name);
- event = alloca(length + 2 + desc->apilength);
- memcpy(event, desc->plugin->prefix, desc->apilength);
- event[desc->apilength] = '/';
- memcpy(event + desc->apilength + 1, name, length + 1);
- ctxClientEventSend(NULL, event, object);
-}
-
-static const struct afb_evmgr_itf evmgr_itf = {
- .push = (void*)afb_api_so_evmgr_push
-};
-
-static struct afb_evmgr afb_api_so_get_evmgr(struct api_so_desc *desc)
-{
- return (struct afb_evmgr){ .itf = &evmgr_itf, .closure = desc };
-}
-
-static const struct afb_daemon_itf daemon_itf = {
- .get_evmgr = (void*)afb_api_so_get_evmgr,
- .get_event_loop = (void*)afb_common_get_event_loop,
- .get_user_bus = (void*)afb_common_get_user_bus,
- .get_system_bus = (void*)afb_common_get_system_bus
-};
-
-
-static void trapping_call(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;
-
- 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);
- }
-
- cb(req);
- }
- if (timerset)
- timer_delete(timerid);
- error_handler = older;
-}
-
-static void call_check(struct afb_req req, struct afb_context *context, const struct AFB_restapi *verb)
-{
- int stag = (int)(verb->session & AFB_SESSION_MASK);
-
- if (stag != AFB_SESSION_NONE) {
- if (!afb_context_check(context)) {
- afb_context_close(context);
- afb_req_fail(req, "failed", "invalid token's identity");
- return;
- }
- }
-
- if ((stag & AFB_SESSION_CREATE) != 0) {
- if (!afb_context_create(context)) {
- afb_context_close(context);
- afb_req_fail(req, "failed", "invalid creation state");
- return;
- }
- }
-
- if ((stag & (AFB_SESSION_CREATE | AFB_SESSION_RENEW)) != 0)
- afb_context_refresh(context);
-
- if ((stag & AFB_SESSION_CLOSE) != 0)
- afb_context_close(context);
-
- trapping_call(req, verb->callback);
-}
-
-static void call(struct api_so_desc *desc, struct afb_req req, struct afb_context *context, const char *verb, size_t lenverb)
-{
- const struct AFB_restapi *v;
-
- v = desc->plugin->apis;
- while (v->name && (strncasecmp(v->name, verb, lenverb) || v->name[lenverb]))
- v++;
- if (v->name)
- call_check(req, context, v);
- else
- afb_req_fail_f(req, "unknown-verb", "verb %.*s unknown within api %s", (int)lenverb, verb, desc->plugin->prefix);
-}
-
-int afb_api_so_add_plugin(const char *path)