dbus: add dbus signal event decision maker 76/202376/5
authorMaciej Slodczyk <m.slodczyk2@partner.samsung.com>
Wed, 27 Mar 2019 15:45:58 +0000 (16:45 +0100)
committerMaciej Slodczyk <m.slodczyk2@partner.samsung.com>
Thu, 4 Apr 2019 13:13:44 +0000 (15:13 +0200)
Change-Id: I95c457972bf46ba720d5015647cf868755d164a0
Signed-off-by: Maciej Slodczyk <m.slodczyk2@partner.samsung.com>
include/action.h
modules.conf.d/activation_eh.conf.d/activation_eh.conf
src/core/action.c
src/decision_makers/activation_dm.c
src/util/json-config.c

index 098578a0d1a4a823d22d8874a6cf0c350b2d0e92..0d571d35b0fe1daed4f2d20a53cadce3ede197db 100644 (file)
 #define EPC_AD_UNIT_NAME "UnitName"
 #define EPC_AD_UNIT_ACTION "UnitAction"
 
-int epc_fill_for_srv_restart(struct epc_object *obj, char *service_path);
+int epc_fill_for_srv_restart(struct epc_object *obj, const char *service_path);
 
-int epc_fill_for_srv_recover(struct epc_object *obj, char *service_path, char *recovery);
+int epc_fill_for_srv_recover(struct epc_object *obj, const char *service_path, const char *recovery);
 
-int epc_fill_for_unit_start(struct epc_object *obj, char *unit_path);
-int epc_fill_for_unit_action(struct epc_object *obj, char *action);
+int epc_fill_for_unit_start(struct epc_object *obj, const char *unit_path);
+int epc_fill_for_unit_action(struct epc_object *obj, const char *action);
 
 #define epc_fill_for_reboot(o)                                 \
        epc_object_fill_empty(o)
index 9d55a9a98e80bb3088685298044d5727cdf6412c..0a09d737c5f451f8a70b11ea4dee60909165f04e 100644 (file)
@@ -1,5 +1,8 @@
 {
        "rules":[
-       {"event":"vconf_key_changed", "key_name":"db/test/key3", "action":"StartUnit", "target":"foo.service"}
+       {"event":"vconf_key_changed", "key_name":"db/test/key3", "action":"StartUnit", "target":"foo.service"},
+       {"event":"dbus_signal", "id":"example_signal", "action":"StartUnit", "target":"bar.service", "match":
+               {"arg1":"hello", "arg2":35}
+       }
        ]
 }
index eb455e587c83e066ee57bc5fcf09c91b490bdfab..379e91b74f0dc9369f53894794fdf0d0e5dccdc9 100644 (file)
@@ -22,7 +22,7 @@
 #include "log.h"
 #include "action.h"
 
-int epc_fill_for_srv_restart(struct epc_object *obj, char *service_path)
+int epc_fill_for_srv_restart(struct epc_object *obj, const char *service_path)
 {
        int ret;
 
@@ -36,7 +36,7 @@ int epc_fill_for_srv_restart(struct epc_object *obj, char *service_path)
        return 0;
 }
 
-int epc_fill_for_srv_recover(struct epc_object *obj, char *service_path, char *recovery)
+int epc_fill_for_srv_recover(struct epc_object *obj, const char *service_path, const char *recovery)
 {
        int ret;
 
@@ -59,7 +59,7 @@ int epc_fill_for_srv_recover(struct epc_object *obj, char *service_path, char *r
        return 0;
 }
 
-int epc_fill_for_unit_start(struct epc_object *obj, char *unit_path)
+int epc_fill_for_unit_start(struct epc_object *obj, const char *unit_path)
 {
        int ret;
 
@@ -73,7 +73,7 @@ int epc_fill_for_unit_start(struct epc_object *obj, char *unit_path)
        return 0;
 }
 
-int epc_fill_for_unit_action(struct epc_object *obj, char *unit_action)
+int epc_fill_for_unit_action(struct epc_object *obj, const char *unit_action)
 {
        int ret;
 
index 1d2327f9fde8ef008b538d51739abee187f7bb97..5d40cb2181892c4bee6887f357e0da0d7155ea8c 100644 (file)
@@ -28,6 +28,7 @@
 #include "json-config.h"
 
 #include "vconf_key_changed_event.h"
+#include "dbus_signal_event.h"
 
 #define MODULE_NAME "activation_decision_maker"
 
@@ -36,14 +37,21 @@ struct activation_decision_maker {
        struct list_head rules;
 };
 
+enum rule_type {
+       RULE_TYPE_VCONF,
+       RULE_TYPE_DBUS
+};
+
 struct rule {
        const char *event;
+       enum rule_type type;
        void *ev_data;
        bool match_oldval;
        bool match_newval;
        const char *action;
        const char *target;
 
+       struct json_object *match;
        struct list_head node;
 };
 
@@ -66,7 +74,8 @@ static const char *vkc_type_to_string(enum vkc_type t)
 static int activation_event_match(struct epc_event_handler *handler,
                struct epc_event *ev)
 {
-       if (strcmp(ev->type->name, VCONF_KEY_CHANGED_EVENT_ID) == 0)
+       if (strcmp(ev->type->name, VCONF_KEY_CHANGED_EVENT_ID) == 0 ||
+                       strcmp (ev->type->name, DBUS_SIGNAL_EVENT_ID) == 0)
                        return 1;
 
        return 0;
@@ -87,6 +96,7 @@ static int vconf_field_cmp(struct vkc_value *a, struct vkc_value *b)
        case VKC_STRING:
                return strcmp(a->value.s, b->value.s) == 0;
        }
+       return 0;
 }
 
 static int vconf_rule_match(struct rule *r, struct epc_event *event)
@@ -127,6 +137,21 @@ static int vconf_rule_match(struct rule *r, struct epc_event *event)
        return 1;
 }
 
+static int dbus_rule_match(struct rule *r, struct epc_event *event)
+{
+       struct dbus_signal_event *ev = to_dbus_signal_event(event);
+       struct dbus_signal_event_data *ev_data = r->ev_data;
+
+       if (ev_data->event_id && strcmp(ev_data->event_id, ev->event_id))
+               return 0;
+
+       if (ev->params && r->match && !epc_object_match_pattern(ev->params, r->match))
+               return 0;
+
+       return 1;
+}
+
+
 static int execute_rule(struct rule *r, struct epc_event *ev)
 {
        struct dm_event_data ev_data = {
@@ -180,8 +205,10 @@ static int activation_make_decision(struct epc_event_handler *handler)
                if (strcmp(r->event, ev->type->name))
                        continue;
 
-               if (!strcmp(ev->type->name, VCONF_KEY_CHANGED_EVENT_ID) == 0 ||
-                               !vconf_rule_match(r, ev))
+               if ((!strcmp(ev->type->name, VCONF_KEY_CHANGED_EVENT_ID) == 0 ||
+                               (r->type == RULE_TYPE_VCONF && !vconf_rule_match(r, ev))) &&
+                       (!strcmp(ev->type->name, DBUS_SIGNAL_EVENT_ID) == 0 ||
+                               (r->type == RULE_TYPE_DBUS && !dbus_rule_match(r, ev))))
                        continue;
 
                ret = execute_rule(r, ev);
@@ -197,7 +224,7 @@ static int parse_vconf_field(json_object *root, const char *key, struct vkc_valu
 {
        struct json_object *node = NULL;
        enum json_type type;
-       char *str;
+       const char *str;
 
        if (!json_object_object_get_ex(root, key, &node)) {
                log_debug("Config does not contain %s parameter", key);
@@ -259,7 +286,25 @@ static int parse_vconf_data(json_object *root, struct rule *r)
        r->match_newval = ret >= 0;
 
        r->ev_data = evd;
+       r->type = RULE_TYPE_VCONF;
+       return 0;
+}
+
+static int parse_dbus_data(json_object *root, struct rule *r)
+{
+       struct dbus_signal_event_data *evd;
+
+       evd = calloc(1, sizeof(*evd));
+       if (!evd)
+               return -ENOMEM;
+
+       r->match = NULL;
 
+       (void) get_config_field(root, "id", &evd->event_id, json_type_string);
+       (void) get_config_field(root, "match", &r->match, json_type_object);
+
+       r->ev_data = evd;
+       r->type = RULE_TYPE_DBUS;
        return 0;
 }
 
@@ -283,7 +328,6 @@ static int add_rule(struct activation_decision_maker *dm, json_object *root)
 {
        int ret;
        struct rule *r;
-       char *str;
 
        r = calloc(1, sizeof(*r));
        if (!r) {
@@ -299,6 +343,8 @@ static int add_rule(struct activation_decision_maker *dm, json_object *root)
 
        if (strcmp(r->event, VCONF_KEY_CHANGED_EVENT_ID) == 0) {
                parse_vconf_data(root, r);
+       } else if (strcmp(r->event, DBUS_SIGNAL_EVENT_ID) == 0) {
+               parse_dbus_data(root, r);
        } else {
                log_warning("Event type %s is not supported by activationd module\n", r->event);
                goto out;
@@ -369,6 +415,7 @@ static int activation_cleanup(struct epc_event_handler *handler)
                        struct activation_decision_maker, eh);
 
        clean_rules(dm);
+       return 0;
 }
 
 static struct activation_decision_maker activation_dm = {
index 9bbf2e1a237a0e3cce7019a025141a76233ab61d..d5a5dd58e8bed02b6c635582fed3c395eac24479 100644 (file)
@@ -344,6 +344,11 @@ int get_config_field(struct json_object *root, const char *key, void *value, int
        }
 
        switch (type) {
+       case json_type_object: {
+               *(struct json_object **) value = node;
+               break;
+       }
+
        case json_type_int: {
                int node_value;