api-so: cleanup
[src/app-framework-binder.git] / src / afb-api-so.c
index d7609bb..da3c132 100644 (file)
@@ -38,6 +38,7 @@
 #include "afb-apis.h"
 #include "afb-api-so.h"
 #include "afb-sig-handler.h"
+#include "afb-thread.h"
 #include "afb-evt.h"
 #include "afb-svc.h"
 #include "verbose.h"
@@ -53,11 +54,6 @@ struct api_so_desc {
        struct afb_binding_interface interface; /* interface for the binding */
 };
 
-struct monitoring {
-       struct afb_req req;
-       void (*action)(struct afb_req);
-};
-
 static const char binding_register_function_v1[] = "afbBindingV1Register";
 static const char binding_service_init_function_v1[] = "afbBindingV1ServiceInit";
 static const char binding_service_event_function_v1[] = "afbBindingV1ServiceEvent";
@@ -67,6 +63,8 @@ static int api_timeout = 15;
 static struct afb_event afb_api_so_event_make_cb(void *closure, const char *name);
 static int afb_api_so_event_broadcast_cb(void *closure, const char *name, struct json_object *object);
 static void afb_api_so_vverbose_cb(void *closure, int level, const char *file, int line, const char *fmt, va_list args);
+static int afb_api_so_rootdir_get_fd(void *closure);
+static int afb_api_so_rootdir_open_locale(void *closure, const char *filename, int flags, const char *locale);
 
 static const struct afb_daemon_itf daemon_itf = {
        .event_broadcast = afb_api_so_event_broadcast_cb,
@@ -74,7 +72,9 @@ static const struct afb_daemon_itf daemon_itf = {
        .get_user_bus = afb_common_get_user_bus,
        .get_system_bus = afb_common_get_system_bus,
        .vverbose = afb_api_so_vverbose_cb,
-       .event_make = afb_api_so_event_make_cb
+       .event_make = afb_api_so_event_make_cb,
+       .rootdir_get_fd = afb_api_so_rootdir_get_fd,
+       .rootdir_open_locale = afb_api_so_rootdir_open_locale
 };
 
 static struct afb_event afb_api_so_event_make_cb(void *closure, const char *name)
@@ -125,33 +125,32 @@ static void afb_api_so_vverbose_cb(void *closure, int level, const char *file, i
        }
 }
 
-static void monitored_call(int signum, void *arg)
+static int afb_api_so_rootdir_get_fd(void *closure)
 {
-       struct monitoring *data = arg;
-       if (signum != 0)
-               afb_req_fail_f(data->req, "aborted", "signal %s(%d) caught", strsignal(signum), signum);
-       else
-               data->action(data->req);
+       return afb_common_rootdir_get_fd();
 }
 
-static void call_check(struct afb_req req, struct afb_context *context, const struct afb_verb_desc_v1 *verb)
+static int afb_api_so_rootdir_open_locale(void *closure, const char *filename, int flags, const char *locale)
 {
-       struct monitoring data;
+       return afb_common_rootdir_open_locale(filename, flags, locale);
+}
 
+static int call_check(struct afb_req req, struct afb_context *context, const struct afb_verb_desc_v1 *verb)
+{
        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;
+                       return 0;
                }
        }
 
        if ((stag & AFB_SESSION_CREATE) != 0) {
                if (afb_context_check_loa(context, 1)) {
                        afb_req_fail(req, "failed", "invalid creation state");
-                       return;
+                       return 0;
                }
                afb_context_change_loa(context, 1);
                afb_context_refresh(context);
@@ -169,7 +168,7 @@ static void call_check(struct afb_req req, struct afb_context *context, const st
                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;
+                       return 0;
                }
        }
 
@@ -177,27 +176,30 @@ static void call_check(struct afb_req req, struct afb_context *context, const st
                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;
+                       return 0;
                }
        }
-
-       data.req = req;
-       data.action = verb->callback;
-       afb_sig_monitor(monitored_call, &data, api_timeout);
+       return 1;
 }
 
-static void call_cb(void *closure, struct afb_req req, struct afb_context *context, const char *verb, size_t lenverb)
+static void call_cb(void *closure, struct afb_req req, struct afb_context *context, const char *strverb, size_t lenverb)
 {
-       const struct afb_verb_desc_v1 *v;
+       const struct afb_verb_desc_v1 *verb;
        struct api_so_desc *desc = closure;
 
-       v = desc->binding->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->binding->v1.prefix);
+       verb = desc->binding->v1.verbs;
+       while (verb->name && (strncasecmp(verb->name, strverb, lenverb) || verb->name[lenverb]))
+               verb++;
+       if (!verb->name)
+               afb_req_fail_f(req, "unknown-verb", "verb %.*s unknown within api %s", (int)lenverb, strverb, desc->binding->v1.prefix);
+       else if (call_check(req, context, verb)) {
+               if (0)
+                       /* not threaded */
+                       afb_sig_req_timeout(req, verb->callback, api_timeout);
+               else
+                       /* threaded */
+                       afb_thread_call(req, verb->callback, api_timeout, desc);
+       }
 }
 
 static int service_start_cb(void *closure, int share_session, int onneed)