Add event factory
authorKrzysztof Opasiak <k.opasiak@samsung.com>
Tue, 25 Apr 2017 10:48:11 +0000 (12:48 +0200)
committerKrzysztof Opasiak <k.opasiak@samsung.com>
Tue, 25 Apr 2017 10:48:11 +0000 (12:48 +0200)
Signed-off-by: Krzysztof Opasiak <k.opasiak@samsung.com>
Makefile.am
src/event.c [new file with mode: 0644]
src/event.h [new file with mode: 0644]

index 4e8148d834939b733f322f6f64be783531efb5f7..fd2e6029587d0aa2feea91c2ddba16ac6fa4d3ce 100644 (file)
@@ -34,5 +34,6 @@ faultd_SOURCES = \
     src/core/module.c \
     src/core/notify_queue.c \
     src/listeners/systemd.c \
-    src/listeners/audit.c
+    src/listeners/audit.c \
+    src/event.c
 faultd_LDADD = $(LIBSYSTEMD_LIBS) ${AUDIT_LIBS}
diff --git a/src/event.c b/src/event.c
new file mode 100644 (file)
index 0000000..1c52e9b
--- /dev/null
@@ -0,0 +1,119 @@
+/*
+ * 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.
+ */
+
+#include "event.h"
+#include "module.h"
+#include "common.h"
+
+struct event_factory {
+        struct faultd_module module;
+        struct list_head types;
+};
+
+#define to_event_factory(MOD) \
+        container_of(MOD, struct event_factory, module)
+
+static int init_event_factory(struct faultd_module *module,
+                              sd_event *event_loop)
+{
+        return 0;
+}
+
+static void cleanup_event_factory(struct faultd_module *module)
+{
+        struct event_factory *efactory = to_event_factory(module);
+
+        assert(list_is_empty(&efactory->types));
+}
+
+static struct event_factory event_factory = {
+        .module = {
+                .name = "event_factory",
+                .type = FAULTD_MODULE_TYPE_CORE,
+
+                .init = init_event_factory,
+                .cleanup = cleanup_event_factory,
+                .node = LIST_HEAD_INIT(event_factory.module.node),
+        },
+        .types = LIST_HEAD_INIT(event_factory.types),
+};
+
+FAULTD_MODULE_REGISTER(&event_factory.module);
+
+int faultd_event_type_register(struct faultd_event_type *new)
+{
+        if (!new->name || !new->allocate_event)
+                return -EINVAL;
+
+        list_add_tail(&new->node, &event_factory.types);
+}
+
+void faultd_event_type_unregister(struct faultd_event_type *type)
+{
+        if (list_is_empty(&type->node))
+                return;
+
+        list_del_init(type->node);
+}
+
+int faultd_event_create(const char *type, void *data, struct faultd_event **ev)
+{
+        struct faultd_event_type *ev_type;
+        struct faultd_event *ev;
+        int ret;
+
+        if (!type)
+                return -EINVAL;
+
+        list_for_each_entry(ev_type, &event_factory.types, node)
+                if (strcmp(ev_type, type) == 0)
+                        break;
+        if (&ev_type->node == &event_factory.types)
+                return -ENOENT;
+
+        ret = ev_type->allocate_event(ev_type, data, ev);
+
+        return ret;
+}
+
+static void release_faultd_event(struct uref *uref)
+{
+        struct faultd_event *ev = container_of(uref, struct faultd_event, uref);
+
+        if (ev->ops->release)
+                ev->ops->release(ev);
+}
+
+int faultd_event_init(struct faultd_event_type *ev_type, pid_t pid,
+                      char *service_name, struct faultd_event *ev)
+{
+        int ret;
+
+        ret = clock_gettime(CLOCK_MONOTONIC, &ev->timestamp);
+        if (ret)
+                return ret;
+
+        ev->type = type;
+        ev->ops = type->default_ops;
+
+        uref_init(&ev->uref, release_faultd_event);
+        INIT_LIST_HEAD(&ev->node);
+        INIT_NQUEUE_NODE(&ev->nq_node);
+
+        return 0;
+}
diff --git a/src/event.h b/src/event.h
new file mode 100644 (file)
index 0000000..ec84e82
--- /dev/null
@@ -0,0 +1,88 @@
+/*
+ * 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_EVENT_H
+#define FAULTD_EVENT_H
+
+#include <time.h>
+
+#include "uref.h"
+#include "list.h"
+
+struct faultd_event;
+
+struct faultd_event_ops {
+        char *(*to_string)(struct faultd_event *);
+        void release(struct faultd_event *);
+};
+
+struct faultd_event_type {
+        char *name;
+
+        struct faultd_event_ops default_ops;
+
+        /* Add function to allocate event from DB */
+
+        /* Allocate event based on passed data */
+        int (*allocate_event)(struct faultd_event_type *type,
+                              void *data, struct faultd_event **);
+
+        /* To be used by event factory */
+        struct list_node node;
+};
+
+struct faultd_event {
+        struct timespec timestamp;
+
+        /* TODO: add here some id field */
+        
+        struct faultd_event_type *type;
+        struct faultd_event_ops ops;
+
+        struct uref uref;
+        /* To be used by event holder */
+        struct list_head node;
+        /* To be used by event processig FW */
+        struct nqueue_node nq_node;
+};
+
+int faultd_event_type_register(struct faultd_event_type *type);
+void faultd_event_type_unregister(struct faultd_event_type *type);
+
+int faultd_event_create(const char *type, void *data, struct faultd_event **ev);
+
+static inline void faultd_event_get(struct faultd_event *ev)
+{
+        uref_get(&ev->uref);
+}
+
+static inline void faultd_event_put(struct faultd_event *ev)
+{
+        uref_put(&ev->uref);
+}
+
+static inline int faultd_event_is_of_type(struct faultd_event *ev,
+                                          const char *type)
+{
+        return strcmp(ev->type->name, type) == 0;
+}
+
+int faultd_event_init(struct faultd_event_type *ev_type,
+                      struct faultd_event *ev);
+
+#endif /* FAULTD_EVENT_H */