Add decision made event
authorKrzysztof Opasiak <k.opasiak@samsung.com>
Tue, 9 May 2017 11:33:48 +0000 (13:33 +0200)
committerKrzysztof Opasiak <k.opasiak@samsung.com>
Wed, 10 May 2017 18:49:58 +0000 (20:49 +0200)
This event is generated when some decision maker
made a decision that some action should be executed.

Signed-off-by: Krzysztof Opasiak <k.opasiak@samsung.com>
Makefile.am
src/event_types/decision_made_event.c [new file with mode: 0644]
src/event_types/decision_made_event.h [new file with mode: 0644]

index 8f1964b8e3f241c2c3c4fd4279be6873595f8469..5e01537cac73f791e4fe1e89d91defb0488987cd 100644 (file)
@@ -37,6 +37,7 @@ faultd_SOURCES = \
     src/core/event_processor.c \
     src/core/service.c \
     src/core/module.c \
+    src/event_types/decision_made_event.c \
     src/event_types/resource_violation_event.c \
     src/faultd.c \
     src/listeners/audit.c \
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..40cc664
--- /dev/null
@@ -0,0 +1,109 @@
+/*
+ * 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 1
+#include <stdio.h>
+#include <errno.h>
+#include <malloc.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_ref(dm_ev->reason);
+
+       *ev = &dm_ev->event;
+       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(dm_ev->action);
+       if (dm_ev->action_data_release)
+               dm_ev->action_data_release(dm_ev->action_data);
+       faultd_event_unref(dm_ev->reason);
+       faultd_event_cleanup(&dm_ev->event);
+       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;
+       int ret;
+
+       /* 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, decision_made_et)
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..ee15bec
--- /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 decision_made_event, event)
+
+#endif /* FAULTD_DECISION_MADE_EVENT_H */