-/*
- * Description of a plugin
- */
-struct api_so_desc {
- struct AFB_plugin *plugin; /* descriptor */
- size_t apilength; /* length of the API name */
- void *handle; /* context of dlopen */
- struct afb_svc *service; /* handler for service started */
- struct AFB_interface interface; /* interface for the plugin */
-};
-
-struct monitoring {
- struct afb_req req;
- void (*action)(struct afb_req);
-};
-
-static const char plugin_register_function_v1[] = "pluginAfbV1Register";
-static const char plugin_service_init_function_v1[] = "pluginAfbV1ServiceInit";
-static const char plugin_service_event_function_v1[] = "pluginAfbV1ServiceEvent";
-
-static int api_timeout = 15;
-
-static struct afb_event afb_api_so_event_make(struct api_so_desc *desc, const char *name);
-static int afb_api_so_event_broadcast(struct api_so_desc *desc, const char *name, struct json_object *object);
-static void afb_api_so_vverbose(struct api_so_desc *desc, int level, const char *file, int line, const char *fmt, va_list args);
-
-static const struct afb_daemon_itf daemon_itf = {
- .event_broadcast = (void*)afb_api_so_event_broadcast,
- .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,
- .vverbose = (void*)afb_api_so_vverbose,
- .event_make = (void*)afb_api_so_event_make
-};
-
-static struct afb_event afb_api_so_event_make(struct api_so_desc *desc, const char *name)
-{
- size_t length;
- char *event;
-
- /* makes the event name */
- assert(desc->plugin != NULL);
- length = strlen(name);
- event = alloca(length + 2 + desc->apilength);
- memcpy(event, desc->plugin->v1.prefix, desc->apilength);
- event[desc->apilength] = '/';
- memcpy(event + desc->apilength + 1, name, length + 1);
-
- /* crate the event */
- return afb_evt_create_event(event);
-}
-
-static int afb_api_so_event_broadcast(struct api_so_desc *desc, const char *name, struct json_object *object)
-{
- size_t length;
- char *event;
-
- /* makes the event name */
- assert(desc->plugin != NULL);
- length = strlen(name);
- event = alloca(length + 2 + desc->apilength);
- memcpy(event, desc->plugin->v1.prefix, desc->apilength);
- event[desc->apilength] = '/';
- memcpy(event + desc->apilength + 1, name, length + 1);
-
- return afb_evt_broadcast(event, object);
-}
-
-static void afb_api_so_vverbose(struct api_so_desc *desc, int level, const char *file, int line, const char *fmt, va_list args)
-{
- char *p;
-
- if (vasprintf(&p, fmt, args) < 0)
- vverbose(level, file, line, fmt, args);
- else {
- verbose(level, file, line, "%s {plugin %s}", p, desc->plugin->v1.prefix);
- free(p);
- }
-}
-
-static void monitored_call(int signum, struct monitoring *data)
-{
- if (signum != 0)
- afb_req_fail_f(data->req, "aborted", "signal %s(%d) caught", strsignal(signum), signum);
- else
- data->action(data->req);
-}
-
-static void call_check(struct afb_req req, struct afb_context *context, const struct AFB_verb_desc_v1 *verb)
-{
- struct monitoring data;
-
- int stag = (int)verb->session;
-
- if ((stag & (AFB_SESSION_CREATE|AFB_SESSION_CLOSE|AFB_SESSION_RENEW|AFB_SESSION_CHECK|AFB_SESSION_LOA_EQ)) != 0) {
- 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_check_loa(context, 1)) {
- afb_req_fail(req, "failed", "invalid creation state");
- return;
- }
- afb_context_change_loa(context, 1);
- afb_context_refresh(context);
- }
-
- if ((stag & (AFB_SESSION_CREATE | AFB_SESSION_RENEW)) != 0)
- afb_context_refresh(context);
-
- if ((stag & AFB_SESSION_CLOSE) != 0) {
- afb_context_change_loa(context, 0);
- afb_context_close(context);
- }
-
- if ((stag & AFB_SESSION_LOA_GE) != 0) {
- int loa = (stag >> AFB_SESSION_LOA_SHIFT) & AFB_SESSION_LOA_MASK;
- if (!afb_context_check_loa(context, loa)) {
- afb_req_fail(req, "failed", "invalid LOA");
- return;
- }
- }
-
- if ((stag & AFB_SESSION_LOA_LE) != 0) {
- int loa = (stag >> AFB_SESSION_LOA_SHIFT) & AFB_SESSION_LOA_MASK;
- if (afb_context_check_loa(context, loa + 1)) {
- afb_req_fail(req, "failed", "invalid LOA");
- return;
- }
- }
-
- data.req = req;
- data.action = verb->callback;
- afb_sig_monitor((void*)monitored_call, &data, api_timeout);
-}
-
-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_verb_desc_v1 *v;
-
- v = desc->plugin->v1.verbs;
- 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->v1.prefix);
-}
-
-static int service_start(struct api_so_desc *desc, int share_session, int onneed)
-{
- int (*init)(struct afb_service service);
- void (*onevent)(const char *event, struct json_object *object);
-
- /* check state */
- if (desc->service != NULL) {
- /* not an error when onneed */
- if (onneed != 0)
- return 0;
-
- /* already started: it is an error */
- ERROR("Service %s already started", desc->plugin->v1.prefix);
- return -1;
- }
-
- /* get the initialisation */
- init = dlsym(desc->handle, plugin_service_init_function_v1);
- if (init == NULL) {
- /* not an error when onneed */
- if (onneed != 0)
- return 0;
-
- /* no initialisation method */
- ERROR("Binding %s is not a service", desc->plugin->v1.prefix);
- return -1;
- }
-
- /* get the event handler if any */
- onevent = dlsym(desc->handle, plugin_service_event_function_v1);
- desc->service = afb_svc_create(share_session, init, onevent);
- if (desc->service == NULL) {
- /* starting error */
- ERROR("Starting service %s failed", desc->plugin->v1.prefix);
- return -1;
- }
-
- return 0;
-}
-
-void afb_api_so_set_timeout(int to)
-{
- api_timeout = to;
-}
-
-int afb_api_so_add_plugin(const char *path)