Add decision made event
authorKrzysztof Opasiak <k.opasiak@samsung.com>
Tue, 25 Apr 2017 19:04:26 +0000 (21:04 +0200)
committerKrzysztof Opasiak <k.opasiak@samsung.com>
Tue, 25 Apr 2017 19:04:26 +0000 (21:04 +0200)
Signed-off-by: Krzysztof Opasiak <k.opasiak@samsung.com>
src/event_types/decision_made_event.c [new file with mode: 0644]
src/event_types/decision_made_event.h [new file with mode: 0644]

diff --git a/src/event_types/decision_made_event.c b/src/event_types/decision_made_event.c
new file mode 100644 (file)
index 0000000..2057a93
--- /dev/null
@@ -0,0 +1,106 @@
+/*
+ * faultd
+ *
+ * Copyright © 2017 Samsung Electronics
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#define _GNU_SOURCE
+#include <stdio.h>
+#include <errno.h>
+
+#include "decision_made_event.h"
+
+static int allocate_dm_event(struct faultd_event_type *type,
+                             void *data, struct faultd_event **ev)
+{
+        struct decision_made_event *dm_ev;
+        struct dm_event_data *dm_ev_data = data;
+        int ret;
+
+        dm_ev = malloc(sizeof(*dm_ev));
+        if (!dm_ev)
+                return -ENOMEM;
+
+        ret = faultd_event_init(type, &dm_ev->event);
+        if (ret)
+                goto free_dm_ev;
+
+        dm_ev->who_made = strdup(dm_ev_data->who_made);
+        dm_ev->action = strdup(dm_ev_data->action);
+
+        if (!dm_ev->who_made || !dm_ev->action) {
+                ret = -ENOMEM;
+                goto cleanup_strs;
+        }
+
+        dm_ev->action_data = dm_ev_data->action_data;
+        dm_ev->action_data_release = dm_ev_data->action_data_release;
+
+        dm_ev->reason = dm_ev_data->reason;
+        faultd_event_get(dm_ev->reason);
+
+        return 0;
+cleanup_strs:
+        free(dm_ev->who_made);
+        free(dm_ev->action);
+
+        faultd_event_cleanup(&dm_ev->event);
+free_dm_ev:
+        free(dm_ev);
+
+        return ret;
+}
+
+static void dm_event_release(struct faultd_event *ev)
+{
+        struct decision_made_event *dm_ev =
+                to_decision_made_event(ev);
+
+        free(dm_ev->who_made);
+        free(action);
+        if (action_data_release)
+                action_data_release(dm_ev->action_data);
+        faultd_event_put(dm_ev->reason);
+        faultd_event_cleanup(dm_ev->ev);
+        free(dm_ev);
+}
+
+static char *dm_event_to_string(struct faultd_event *ev)
+{
+        struct decision_made_event *dm_ev =
+                to_decision_made_event(ev);
+        char *str;
+
+        /* TODO print action data? */
+        ret = asprintf(&str, "Decision Made Event:"
+                       " Who made: %s"
+                       " Action: %s"
+                       dm_ev->who_made,
+                       dm_ev->action);
+
+        return ret > 0 ? str : NULL;
+}
+
+static struct faultd_event_type decision_made_event_type = {
+        .name = DECISION_MADE_EVENT_ID,
+        .default_ops = {
+                .to_string = dm_event_to_string,
+                .release = dm_event_release,
+        },
+        .allocate_event = allocate_dm_event,
+        .node = LIST_HEAD_INIT(decision_made_event_type.node),
+};
+
+FAULTD_EVENT_TYPE_REGISTER(decision_made_event_type)
diff --git a/src/event_types/decision_made_event.h b/src/event_types/decision_made_event.h
new file mode 100644 (file)
index 0000000..3a35d97
--- /dev/null
@@ -0,0 +1,54 @@
+/*
+ * faultd
+ *
+ * Copyright © 2017 Samsung Electronics
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef FAULTD_DECISION_MADE_EVENT_H
+#define FAULTD_DECISION_MADE_EVENT_H
+
+#include <time.h>
+
+#include "action.h"
+#include "event.h"
+#include "common.h"
+
+#define DECISION_MADE_EVENT_ID "decision_made"
+
+struct decision_made_event {
+        struct faultd_event event;
+        struct faultd_event *reason;
+        char *who_made;
+        char *action;
+        void *action_data;
+        void (*action_data_release)(void *);
+        /* TODO: what more do we need? */
+};
+
+struct dm_event_data {
+        /* This is referenced by dm event */
+        struct faultd_event *reason;
+        /* Strings are deep copied */
+        char *who_made;
+        char *action;
+        /* Action data is shallow copied */
+        void *action_data
+        void (*action_data_release)(void *);        
+};
+        
+#define to_decision_made_event(EVENT)      \
+        container_of(EVENT, struct resource_decision_made_event, event)
+
+#endif /* FAULTD_DECISION_MADE_EVENT_H */