X-Git-Url: https://gerrit.automotivelinux.org/gerrit/gitweb?a=blobdiff_plain;f=src%2Fafb-xreq.c;h=47f2ec6702a3ec95388f488a4255522420527c50;hb=0891ef4826e347d5554c630b5c0ce73c68f76c9c;hp=b091bd1e8fe3ec83721421111c04195655b911a8;hpb=39a955678ac5f16646e5841a720d1d358600dabb;p=src%2Fapp-framework-binder.git diff --git a/src/afb-xreq.c b/src/afb-xreq.c index b091bd1e..47f2ec67 100644 --- a/src/afb-xreq.c +++ b/src/afb-xreq.c @@ -34,6 +34,7 @@ #include "afb-cred.h" #include "afb-hook.h" #include "afb-api.h" +#include "afb-api-dyn.h" #include "afb-apiset.h" #include "afb-auth.h" #include "jobs.h" @@ -97,7 +98,8 @@ struct subcall struct { union { void (*callback)(void*, int, struct json_object*); - void (*callback2)(void*, int, struct json_object*, struct afb_req); + void (*callback_req)(void*, int, struct json_object*, struct afb_req); + void (*callback_request)(void*, int, struct json_object*, struct afb_request*); }; void *closure; }; @@ -171,8 +173,8 @@ static struct subcall *subcall_alloc( afb_context_subinit(&subcall->xreq.context, &caller->context); subcall->xreq.cred = afb_cred_addref(caller->cred); subcall->xreq.json = args; - subcall->xreq.api = api; - subcall->xreq.verb = verb; + subcall->xreq.request.api = api; + subcall->xreq.request.verb = verb; subcall->xreq.caller = caller; afb_xreq_unhooked_addref(caller); } @@ -187,7 +189,12 @@ static void subcall_on_reply(struct subcall *subcall, int status, struct json_ob static void subcall_req_on_reply(struct subcall *subcall, int status, struct json_object *result) { - subcall->callback2(subcall->closure, status, result, to_req(subcall->xreq.caller)); + subcall->callback_req(subcall->closure, status, result, to_req(subcall->xreq.caller)); +} + +static void subcall_request_on_reply(struct subcall *subcall, int status, struct json_object *result) +{ + subcall->callback_request(subcall->closure, status, result, to_request(subcall->xreq.caller)); } static void subcall_hooked_on_reply(struct subcall *subcall, int status, struct json_object *result) @@ -202,6 +209,12 @@ static void subcall_req_hooked_on_reply(struct subcall *subcall, int status, str subcall_req_on_reply(subcall, status, result); } +static void subcall_request_hooked_on_reply(struct subcall *subcall, int status, struct json_object *result) +{ + afb_hook_xreq_subcall_result(subcall->xreq.caller, status, result); + subcall_request_on_reply(subcall, status, result); +} + static void subcall_reply_direct_cb(void *closure, int status, struct json_object *result) { struct afb_xreq *xreq = closure; @@ -220,7 +233,7 @@ static void subcall_process(struct subcall *subcall, void (*completion)(struct s subcall->completion = completion; if (subcall->xreq.caller->queryitf->subcall) { subcall->xreq.caller->queryitf->subcall( - subcall->xreq.caller, subcall->xreq.api, subcall->xreq.verb, + subcall->xreq.caller, subcall->xreq.request.api, subcall->xreq.request.verb, subcall->xreq.json, subcall_reply_direct_cb, &subcall->xreq); } else { afb_xreq_unhooked_addref(&subcall->xreq); @@ -237,11 +250,18 @@ static void subcall(struct subcall *subcall, void (*callback)(void*, int, struct static void subcall_req(struct subcall *subcall, void (*callback)(void*, int, struct json_object*, struct afb_req), void *cb_closure) { - subcall->callback2 = callback; + subcall->callback_req = callback; subcall->closure = cb_closure; subcall_process(subcall, subcall_req_on_reply); } +static void subcall_request(struct subcall *subcall, void (*callback)(void*, int, struct json_object*, struct afb_request*), void *cb_closure) +{ + subcall->callback_request = callback; + subcall->closure = cb_closure; + subcall_process(subcall, subcall_request_on_reply); +} + static void subcall_hooked(struct subcall *subcall, void (*callback)(void*, int, struct json_object*), void *cb_closure) { subcall->callback = callback; @@ -251,11 +271,18 @@ static void subcall_hooked(struct subcall *subcall, void (*callback)(void*, int, static void subcall_req_hooked(struct subcall *subcall, void (*callback)(void*, int, struct json_object*, struct afb_req), void *cb_closure) { - subcall->callback2 = callback; + subcall->callback_req = callback; subcall->closure = cb_closure; subcall_process(subcall, subcall_req_hooked_on_reply); } +static void subcall_request_hooked(struct subcall *subcall, void (*callback)(void*, int, struct json_object*, struct afb_request*), void *cb_closure) +{ + subcall->callback_request = callback; + subcall->closure = cb_closure; + subcall_process(subcall, subcall_request_hooked_on_reply); +} + static void subcall_sync_leave(struct subcall *subcall) { struct jobloop *jobloop = __atomic_exchange_n(&subcall->jobloop, NULL, __ATOMIC_RELAXED); @@ -424,7 +451,7 @@ static int xreq_session_set_LOA_cb(struct afb_request *closure, unsigned level) static int xreq_subscribe_eventid_cb(struct afb_request *closure, struct afb_eventid *eventid); static int xreq_subscribe_cb(struct afb_request *closure, struct afb_event event) { - return xreq_subscribe_eventid_cb(closure, event.closure); + return xreq_subscribe_eventid_cb(closure, afb_event_to_eventid(event)); } static int xreq_subscribe_eventid_cb(struct afb_request *closure, struct afb_eventid *eventid) @@ -436,7 +463,7 @@ static int xreq_subscribe_eventid_cb(struct afb_request *closure, struct afb_eve int afb_xreq_subscribe(struct afb_xreq *xreq, struct afb_eventid *eventid) { if (xreq->listener) - return afb_evt_add_watch(xreq->listener, eventid); + return afb_evt_eventid_add_watch(xreq->listener, eventid); if (xreq->queryitf->subscribe) return xreq->queryitf->subscribe(xreq, eventid); ERROR("no event listener, subscription impossible"); @@ -447,7 +474,7 @@ int afb_xreq_subscribe(struct afb_xreq *xreq, struct afb_eventid *eventid) static int xreq_unsubscribe_eventid_cb(struct afb_request *closure, struct afb_eventid *eventid); static int xreq_unsubscribe_cb(struct afb_request *closure, struct afb_event event) { - return xreq_unsubscribe_eventid_cb(closure, event.closure); + return xreq_unsubscribe_eventid_cb(closure, afb_event_to_eventid(event)); } static int xreq_unsubscribe_eventid_cb(struct afb_request *closure, struct afb_eventid *eventid) @@ -459,7 +486,7 @@ static int xreq_unsubscribe_eventid_cb(struct afb_request *closure, struct afb_e int afb_xreq_unsubscribe(struct afb_xreq *xreq, struct afb_eventid *eventid) { if (xreq->listener) - return afb_evt_remove_watch(xreq->listener, eventid); + return afb_evt_eventid_remove_watch(xreq->listener, eventid); if (xreq->queryitf->unsubscribe) return xreq->queryitf->unsubscribe(xreq, eventid); ERROR("no event listener, unsubscription impossible"); @@ -497,6 +524,21 @@ static void xreq_subcall_req_cb(struct afb_request *closure, const char *api, co } } +static void xreq_subcall_request_cb(struct afb_request *closure, const char *api, const char *verb, struct json_object *args, void (*callback)(void*, int, struct json_object*, struct afb_request*), void *cb_closure) +{ + struct afb_xreq *xreq = from_request(closure); + struct subcall *sc; + + sc = subcall_alloc(xreq, api, verb, args); + if (sc == NULL) { + if (callback) + callback(cb_closure, 1, afb_msg_json_internal_error(), to_request(xreq)); + json_object_put(args); + } else { + subcall_request(sc, callback, cb_closure); + } +} + static int xreq_subcallsync_cb(struct afb_request *closure, const char *api, const char *verb, struct json_object *args, struct json_object **result) { @@ -528,7 +570,7 @@ static void xreq_vverbose_cb(struct afb_request *closure, int level, const char if (!fmt || vasprintf(&p, fmt, args) < 0) vverbose(level, file, line, func, fmt, args); else { - verbose(level, file, line, func, "[REQ/API %s] %s", xreq->api, p); + verbose(level, file, line, func, "[REQ/API %s] %s", xreq->request.api, p); free(p); } } @@ -557,6 +599,12 @@ static void *xreq_context_make_cb(struct afb_request *closure, int replace, void return afb_context_make(&xreq->context, replace, create_value, free_value, create_closure); } +static int xreq_get_uid_cb(struct afb_request *closure) +{ + struct afb_xreq *xreq = from_request(closure); + return xreq->cred && xreq->cred->id ? (int)xreq->cred->uid : -1; +} + /******************************************************************************/ static struct json_object *xreq_hooked_json_cb(struct afb_request *closure) @@ -639,18 +687,30 @@ static int xreq_hooked_session_set_LOA_cb(struct afb_request *closure, unsigned return afb_hook_xreq_session_set_LOA(xreq, level, r); } +static int xreq_hooked_subscribe_eventid_cb(struct afb_request *closure, struct afb_eventid *eventid); static int xreq_hooked_subscribe_cb(struct afb_request *closure, struct afb_event event) { - int r = xreq_subscribe_cb(closure, event); + return xreq_hooked_subscribe_eventid_cb(closure, afb_event_to_eventid(event)); +} + +static int xreq_hooked_subscribe_eventid_cb(struct afb_request *closure, struct afb_eventid *eventid) +{ + int r = xreq_subscribe_eventid_cb(closure, eventid); struct afb_xreq *xreq = from_request(closure); - return afb_hook_xreq_subscribe(xreq, event.closure, r); + return afb_hook_xreq_subscribe(xreq, eventid, r); } +static int xreq_hooked_unsubscribe_eventid_cb(struct afb_request *closure, struct afb_eventid *eventid); static int xreq_hooked_unsubscribe_cb(struct afb_request *closure, struct afb_event event) { - int r = xreq_unsubscribe_cb(closure, event); + return xreq_hooked_unsubscribe_eventid_cb(closure, afb_event_to_eventid(event)); +} + +static int xreq_hooked_unsubscribe_eventid_cb(struct afb_request *closure, struct afb_eventid *eventid) +{ + int r = xreq_unsubscribe_eventid_cb(closure, eventid); struct afb_xreq *xreq = from_request(closure); - return afb_hook_xreq_unsubscribe(xreq, event.closure, r); + return afb_hook_xreq_unsubscribe(xreq, eventid, r); } static void xreq_hooked_subcall_cb(struct afb_request *closure, const char *api, const char *verb, struct json_object *args, void (*callback)(void*, int, struct json_object*), void *cb_closure) @@ -685,6 +745,22 @@ static void xreq_hooked_subcall_req_cb(struct afb_request *closure, const char * } } +static void xreq_hooked_subcall_request_cb(struct afb_request *closure, const char *api, const char *verb, struct json_object *args, void (*callback)(void*, int, struct json_object*, struct afb_request *), void *cb_closure) +{ + struct afb_xreq *xreq = from_request(closure); + struct subcall *sc; + + afb_hook_xreq_subcall(xreq, api, verb, args); + sc = subcall_alloc(xreq, api, verb, args); + if (sc == NULL) { + if (callback) + callback(cb_closure, 1, afb_msg_json_internal_error(), to_request(xreq)); + json_object_put(args); + } else { + subcall_request_hooked(sc, callback, cb_closure); + } +} + static int xreq_hooked_subcallsync_cb(struct afb_request *closure, const char *api, const char *verb, struct json_object *args, struct json_object **result) { int r; @@ -733,6 +809,13 @@ static void *xreq_hooked_context_make_cb(struct afb_request *closure, int replac return afb_hook_xreq_context_make(xreq, replace, create_value, free_value, create_closure, result); } +static int xreq_hooked_get_uid_cb(struct afb_request *closure) +{ + struct afb_xreq *xreq = from_request(closure); + int r = xreq_get_uid_cb(closure); + return afb_hook_xreq_get_uid(xreq, r); +} + /******************************************************************************/ const struct afb_request_itf xreq_itf = { @@ -757,7 +840,11 @@ const struct afb_request_itf xreq_itf = { .subcall_req = xreq_subcall_req_cb, .has_permission = xreq_has_permission_cb, .get_application_id = xreq_get_application_id_cb, - .context_make = xreq_context_make_cb + .context_make = xreq_context_make_cb, + .subscribe_eventid = xreq_subscribe_eventid_cb, + .unsubscribe_eventid = xreq_unsubscribe_eventid_cb, + .subcall_request = xreq_subcall_request_cb, + .get_uid = xreq_get_uid_cb, }; const struct afb_request_itf xreq_hooked_itf = { @@ -782,7 +869,11 @@ const struct afb_request_itf xreq_hooked_itf = { .subcall_req = xreq_hooked_subcall_req_cb, .has_permission = xreq_hooked_has_permission_cb, .get_application_id = xreq_hooked_get_application_id_cb, - .context_make = xreq_hooked_context_make_cb + .context_make = xreq_hooked_context_make_cb, + .subscribe_eventid = xreq_hooked_subscribe_eventid_cb, + .unsubscribe_eventid = xreq_hooked_unsubscribe_eventid_cb, + .subcall_request = xreq_hooked_subcall_request_cb, + .get_uid = xreq_hooked_get_uid_cb, }; /******************************************************************************/ @@ -848,12 +939,12 @@ void afb_xreq_unref(struct afb_xreq *xreq) afb_request_unref(to_request(xreq)); } -void afb_xreq_unhooked_subcall(struct afb_xreq *xreq, const char *api, const char *verb, struct json_object *args, void (*callback)(void*, int, struct json_object*), void *cb_closure) +void afb_xreq_unhooked_subcall(struct afb_xreq *xreq, const char *api, const char *verb, struct json_object *args, void (*callback)(void*, int, struct json_object*, struct afb_request *), void *cb_closure) { - xreq_subcall_cb(to_request(xreq), api, verb, args, callback, cb_closure); + xreq_subcall_request_cb(to_request(xreq), api, verb, args, callback, cb_closure); } -void afb_xreq_subcall(struct afb_xreq *xreq, const char *api, const char *verb, struct json_object *args, void (*callback)(void*, int, struct json_object*), void *cb_closure) +void afb_xreq_subcall(struct afb_xreq *xreq, const char *api, const char *verb, struct json_object *args, void (*callback)(void*, int, struct json_object*, struct afb_request *), void *cb_closure) { afb_request_subcall(to_request(xreq), api, verb, args, callback, cb_closure); } @@ -964,6 +1055,15 @@ void afb_xreq_call_verb_v2(struct afb_xreq *xreq, const struct afb_verb_v2 *verb verb->callback(to_req(xreq)); } +void afb_xreq_call_verb_vdyn(struct afb_xreq *xreq, const struct afb_api_dyn_verb *verb) +{ + if (!verb) + afb_xreq_fail_unknown_verb(xreq); + else + if (xreq_session_check_apply_v2(xreq, verb->session, verb->auth) >= 0) + verb->callback(to_request(xreq)); +} + void afb_xreq_init(struct afb_xreq *xreq, const struct afb_xreq_query_itf *queryitf) { memset(xreq, 0, sizeof *xreq); @@ -974,12 +1074,12 @@ void afb_xreq_init(struct afb_xreq *xreq, const struct afb_xreq_query_itf *query void afb_xreq_fail_unknown_api(struct afb_xreq *xreq) { - afb_xreq_fail_f(xreq, "unknown-api", "api %s not found (for verb %s)", xreq->api, xreq->verb); + afb_xreq_fail_f(xreq, "unknown-api", "api %s not found (for verb %s)", xreq->request.api, xreq->request.verb); } void afb_xreq_fail_unknown_verb(struct afb_xreq *xreq) { - afb_xreq_fail_f(xreq, "unknown-verb", "verb %s unknown within api %s", xreq->verb, xreq->api); + afb_xreq_fail_f(xreq, "unknown-verb", "verb %s unknown within api %s", xreq->request.verb, xreq->request.api); } static void init_hooking(struct afb_xreq *xreq) @@ -1043,12 +1143,12 @@ void afb_xreq_process(struct afb_xreq *xreq, struct afb_apiset *apiset) /* lookup at the api */ xreq->apiset = apiset; - api = afb_apiset_lookup_started(apiset, xreq->api, 1); + api = afb_apiset_lookup_started(apiset, xreq->request.api, 1); if (!api) { if (errno == ENOENT) - early_failure(xreq, "unknown-api", "api %s not found (for verb %s)", xreq->api, xreq->verb); + early_failure(xreq, "unknown-api", "api %s not found (for verb %s)", xreq->request.api, xreq->request.verb); else - early_failure(xreq, "bad-api-state", "api %s not started correctly: %m", xreq->api); + early_failure(xreq, "bad-api-state", "api %s not started correctly: %m", xreq->request.api); goto end; } xreq->context.api_key = api; @@ -1059,8 +1159,8 @@ void afb_xreq_process(struct afb_xreq *xreq, struct afb_apiset *apiset) while (caller) { if (((const struct afb_api *)caller->context.api_key)->group == api->group) { /* noconcurrency lock detected */ - ERROR("self-lock detected in call stack for API %s", xreq->api); - early_failure(xreq, "self-locked", "recursive self lock, API %s", xreq->api); + ERROR("self-lock detected in call stack for API %s", xreq->request.api); + early_failure(xreq, "self-locked", "recursive self lock, API %s", xreq->request.api); goto end; } caller = caller->caller;