X-Git-Url: https://gerrit.automotivelinux.org/gerrit/gitweb?a=blobdiff_plain;ds=sidebyside;f=src%2Fmain.c;h=6684ed5e98928ff2dde58e96150fbd3e64c75635;hb=a138fda9841c46e28de93e32aee62956e26556b1;hp=18f5e5ef97564737fe14a4e0a157b2036b00daf8;hpb=5c87873a2134045dcd013d8e55acc4b5ca841131;p=src%2Fapp-framework-binder.git diff --git a/src/main.c b/src/main.c index 18f5e5ef..6684ed5e 100644 --- a/src/main.c +++ b/src/main.c @@ -27,6 +27,8 @@ #include #include +#include + #include #include @@ -41,6 +43,8 @@ #include "afb-hsrv.h" #include "afb-context.h" #include "afb-hreq.h" +#include "afb-xreq.h" +#include "afb-cred.h" #include "jobs.h" #include "afb-session.h" #include "verbose.h" @@ -430,6 +434,101 @@ static int execute_command() return -1; } +/*--------------------------------------------------------- + | startup calls + +--------------------------------------------------------- */ + +struct startup_req +{ + struct afb_xreq xreq; + char *api; + char *verb; + struct afb_config_list *current; + struct afb_session *session; +}; + +static void startup_call_reply(struct afb_xreq *xreq, int iserror, struct json_object *obj) +{ + struct startup_req *sreq = CONTAINER_OF_XREQ(struct startup_req, xreq); + + if (!iserror) + NOTICE("startup call %s returned %s", sreq->current->value, json_object_get_string(obj)); + else { + ERROR("startup call %s ERROR! %s", sreq->current->value, json_object_get_string(obj)); + exit(1); + } +} + +static void startup_call_current(struct startup_req *sreq); + +static void startup_call_unref(struct afb_xreq *xreq) +{ + struct startup_req *sreq = CONTAINER_OF_XREQ(struct startup_req, xreq); + + free(sreq->api); + free(sreq->verb); + json_object_put(sreq->xreq.json); + afb_cred_unref(sreq->xreq.cred); + sreq->current = sreq->current->next; + if (sreq->current) + startup_call_current(sreq); + else { + afb_session_close(sreq->session); + afb_session_unref(sreq->session); + free(sreq); + } +} + +static struct afb_xreq_query_itf startup_xreq_itf = +{ + .reply = startup_call_reply, + .unref = startup_call_unref +}; + +static void startup_call_current(struct startup_req *sreq) +{ + char *api, *verb, *json; + + api = sreq->current->value; + verb = strchr(api, '/'); + if (verb) { + json = strchr(verb, ':'); + if (json) { + memset(&sreq->xreq, 0, sizeof sreq->xreq); + afb_xreq_init(&sreq->xreq, &startup_xreq_itf); + afb_context_init(&sreq->xreq.context, sreq->session, NULL); + sreq->xreq.context.validated = 1; + sreq->xreq.cred = afb_cred_current(); + sreq->api = strndup(api, verb - api); + sreq->verb = strndup(verb + 1, json - verb - 1); + sreq->xreq.api = sreq->api; + sreq->xreq.verb = sreq->verb; + sreq->xreq.json = json_tokener_parse(json + 1); + if (sreq->api && sreq->verb && sreq->xreq.json) { + afb_apis_call(&sreq->xreq); + afb_xreq_unref(&sreq->xreq); + return; + } + } + } + ERROR("Bad call specification %s", sreq->current->value); + exit(1); +} + +static void run_startup_calls() +{ + struct afb_config_list *list; + struct startup_req *sreq; + + list = config->calls; + if (list) { + sreq = calloc(1, sizeof *sreq); + sreq->session = afb_session_create("startup", 3600); + sreq->current = list; + startup_call_current(sreq); + } +} + /*--------------------------------------------------------- | job for starting the daemon +--------------------------------------------------------- */ @@ -497,6 +596,9 @@ static void start() /* ready */ sd_notify(1, "READY=1"); + + /* run the startup calls */ + run_startup_calls(); return; error: exit(1);