X-Git-Url: https://gerrit.automotivelinux.org/gerrit/gitweb?a=blobdiff_plain;f=src%2Fafb-api-dbus.c;h=0ac4f4995bd354d1f9581196fd2f3384d7c08549;hb=eaf5670e9d10b5d7c066043e7563706cf1e01bd5;hp=a2c9b086b4a4b4dceacacf424b9f804398597a4a;hpb=12ec841c28f8f795b49466cc377e64db3146430d;p=src%2Fapp-framework-binder.git diff --git a/src/afb-api-dbus.c b/src/afb-api-dbus.c index a2c9b086..0ac4f499 100644 --- a/src/afb-api-dbus.c +++ b/src/afb-api-dbus.c @@ -1,5 +1,5 @@ /* - * Copyright (C) 2015-2018 "IoT.bzh" + * Copyright (C) 2015-2019 "IoT.bzh" * Author José Bollo * * Licensed under the Apache License, Version 2.0 (the "License"); @@ -15,7 +15,7 @@ * limitations under the License. */ -#if defined(WITH_DBUS_TRANSPARENCY) +#if WITH_DBUS_TRANSPARENCY #define _GNU_SOURCE @@ -27,11 +27,12 @@ #include #include +#if !defined(JSON_C_TO_STRING_NOSLASHESCAPE) +#define JSON_C_TO_STRING_NOSLASHESCAPE 0 +#endif #include -#include "afb-systemd.h" - #include "afb-session.h" #include "afb-msg-json.h" #include "afb-api.h" @@ -41,7 +42,10 @@ #include "afb-cred.h" #include "afb-evt.h" #include "afb-xreq.h" + #include "verbose.h" +#include "systemd.h" +#include "jobs.h" static const char DEFAULT_PATH_PREFIX[] = "/org/agl/afb/api/"; @@ -126,7 +130,8 @@ static struct api_dbus *make_api_dbus_3(int system, const char *path, size_t pat } /* choose the bus */ - sdbus = (system ? afb_systemd_get_system_bus : afb_systemd_get_user_bus)(); + jobs_acquire_event_manager(); + sdbus = (system ? systemd_get_system_bus : systemd_get_user_bus)(); if (sdbus == NULL) goto error2; @@ -282,6 +287,8 @@ static int api_dbus_client_on_reply(sd_bus_message *message, void *userdata, sd_ int rc; struct dbus_memo *memo; const char *json, *error, *info; + struct json_object *object; + enum json_tokener_error jerr; /* retrieve the recorded data */ memo = userdata; @@ -293,7 +300,14 @@ static int api_dbus_client_on_reply(sd_bus_message *message, void *userdata, sd_ afb_xreq_reply(memo->xreq, NULL, "error", "dbus error"); } else { /* report the answer */ - afb_xreq_reply(memo->xreq, *json ? json_tokener_parse(json) : NULL, *error ? error : NULL, *info ? info : NULL); + if (!*json) + object = NULL; + else { + object = json_tokener_parse_verbose(json, &jerr); + if (jerr != json_tokener_success) + object = json_object_new_string(json); + } + afb_xreq_reply(memo->xreq, object, *error ? error : NULL, *info ? info : NULL); } api_dbus_client_memo_destroy(memo); return 1; @@ -354,12 +368,19 @@ static int api_dbus_client_on_broadcast_event(sd_bus_message *m, void *userdata, { struct json_object *object; const char *event, *data; - int rc = sd_bus_message_read(m, "ss", &event, &data); + const unsigned char *uuid; + size_t szuuid; + uint8_t hop; + enum json_tokener_error jerr; + + int rc = sd_bus_message_read(m, "ssayy", &event, &data, &uuid, &szuuid, &hop); if (rc < 0) ERROR("unreadable broadcasted event"); else { - object = json_tokener_parse(data); - afb_evt_broadcast(event, object); + object = json_tokener_parse_verbose(data, &jerr); + if (jerr != json_tokener_success) + object = json_object_new_string(data); + afb_evt_rebroadcast(event, object, uuid, hop); } return 1; } @@ -437,6 +458,7 @@ static void api_dbus_client_event_push(struct api_dbus *api, int id, const char { struct json_object *object; struct dbus_event *ev; + enum json_tokener_error jerr; /* retrieves the event */ ev = api_dbus_client_event_search(api, id, name); @@ -446,7 +468,9 @@ static void api_dbus_client_event_push(struct api_dbus *api, int id, const char } /* destroys the event */ - object = json_tokener_parse(data); + object = json_tokener_parse_verbose(data, &jerr); + if (jerr != json_tokener_success) + object = json_object_new_string(data); afb_evt_event_x2_push(ev->event, object); } @@ -612,10 +636,10 @@ error: /******************* event structures for server part **********************************/ -static void afb_api_dbus_server_event_add(void *closure, const char *event, int eventid); -static void afb_api_dbus_server_event_remove(void *closure, const char *event, int eventid); -static void afb_api_dbus_server_event_push(void *closure, const char *event, int eventid, struct json_object *object); -static void afb_api_dbus_server_event_broadcast(void *closure, const char *event, int eventid, struct json_object *object); +static void afb_api_dbus_server_event_add(void *closure, const char *event, uint16_t eventid); +static void afb_api_dbus_server_event_remove(void *closure, const char *event, uint16_t eventid); +static void afb_api_dbus_server_event_push(void *closure, const char *event, uint16_t eventid, struct json_object *object); +static void afb_api_dbus_server_event_broadcast(void *closure, const char *event, struct json_object *object, const uuid_binary_t uuid, uint8_t hop); /* the interface for events broadcasting */ static const struct afb_evt_itf evt_broadcast_itf = { @@ -646,7 +670,7 @@ struct origin struct afb_cred *cred; /* the origin */ - char name[1]; + char name[]; }; /* get the credentials for the message */ @@ -690,7 +714,7 @@ static struct origin *afb_api_dbus_server_origin_get(struct api_dbus *api, const } /* not found, create it */ - origin = malloc(strlen(sender) + sizeof *origin); + origin = malloc(strlen(sender) + 1 + sizeof *origin); if (origin == NULL) errno = ENOMEM; else { @@ -810,7 +834,7 @@ void dbus_req_raw_reply(struct afb_xreq *xreq, struct json_object *obj, const ch int rc; rc = sd_bus_reply_method_return(dreq->message, "sss", - obj ? json_object_to_json_string_ext(obj, JSON_C_TO_STRING_PLAIN) : "", + obj ? json_object_to_json_string_ext(obj, JSON_C_TO_STRING_PLAIN|JSON_C_TO_STRING_NOSLASHESCAPE) : "", error ? : "", info ? : ""); if (rc < 0) @@ -880,31 +904,32 @@ end: sd_bus_message_unref(msg); } -static void afb_api_dbus_server_event_add(void *closure, const char *event, int eventid) +static void afb_api_dbus_server_event_add(void *closure, const char *event, uint16_t eventid) { afb_api_dbus_server_event_send(closure, '+', event, eventid, "", 0); } -static void afb_api_dbus_server_event_remove(void *closure, const char *event, int eventid) +static void afb_api_dbus_server_event_remove(void *closure, const char *event, uint16_t eventid) { afb_api_dbus_server_event_send(closure, '-', event, eventid, "", 0); } -static void afb_api_dbus_server_event_push(void *closure, const char *event, int eventid, struct json_object *object) +static void afb_api_dbus_server_event_push(void *closure, const char *event, uint16_t eventid, struct json_object *object) { - const char *data = json_object_to_json_string_ext(object, JSON_C_TO_STRING_PLAIN); + const char *data = json_object_to_json_string_ext(object, JSON_C_TO_STRING_PLAIN|JSON_C_TO_STRING_NOSLASHESCAPE); afb_api_dbus_server_event_send(closure, '!', event, eventid, data, 0); json_object_put(object); } -static void afb_api_dbus_server_event_broadcast(void *closure, const char *event, int eventid, struct json_object *object) +static void afb_api_dbus_server_event_broadcast(void *closure, const char *event, struct json_object *object, const uuid_binary_t uuid, uint8_t hop) { int rc; struct api_dbus *api; api = closure; rc = sd_bus_emit_signal(api->sdbus, api->path, api->name, "broadcast", - "ss", event, json_object_to_json_string_ext(object, JSON_C_TO_STRING_PLAIN)); + "ssayy", event, json_object_to_json_string_ext(object, JSON_C_TO_STRING_PLAIN|JSON_C_TO_STRING_NOSLASHESCAPE), + uuid, UUID_BINARY_LENGTH, hop); if (rc < 0) ERROR("error while broadcasting event %s", event); json_object_put(object); @@ -922,6 +947,7 @@ static int api_dbus_server_on_object_called(sd_bus_message *message, void *userd uint32_t flags; struct afb_session *session; struct listener *listener; + enum json_tokener_error jerr; /* check the interface */ if (strcmp(sd_bus_message_get_interface(message), api->name) != 0) @@ -955,10 +981,10 @@ static int api_dbus_server_on_object_called(sd_bus_message *message, void *userd /* fulfill the request and emit it */ dreq->xreq.context.flags = flags; - dreq->xreq.cred = afb_cred_mixed_on_behalf_import(listener->origin->cred, uuid, creds && creds[0] ? creds : NULL); + dreq->xreq.cred = afb_cred_mixed_on_behalf_import(listener->origin->cred, &dreq->xreq.context, creds && creds[0] ? creds : NULL); dreq->message = sd_bus_message_ref(message); - dreq->json = json_tokener_parse(dreq->request); - if (dreq->json == NULL && strcmp(dreq->request, "null")) { + dreq->json = json_tokener_parse_verbose(dreq->request, &jerr); + if (jerr != json_tokener_success) { /* lazy error detection of json request. Is it to improve? */ dreq->json = json_object_new_string(dreq->request); }