summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorŁukasz Stelmach <l.stelmach@samsung.com>2017-05-22 12:57:32 +0200
committerŁukasz Stelmach <l.stelmach@samsung.com>2017-05-31 12:46:07 +0200
commitb9b46ba71cd73222c81d6d7653c784e3ddb1c854 (patch)
treea0ca269ef16df86485d1533d7b1d5955b058a2ab
parentb50d87e8b43c03125225c6354ff4e1307ad2e89c (diff)
downloadfaultd-b9b46ba71cd73222c81d6d7653c784e3ddb1c854.tar.gz
faultd-b9b46ba71cd73222c81d6d7653c784e3ddb1c854.tar.bz2
faultd-b9b46ba71cd73222c81d6d7653c784e3ddb1c854.zip
core: Create database adapter module
Change-Id: I5a77674ae87defab22a8925b49f4db7f54d97241 Signed-off-by: Łukasz Stelmach <l.stelmach@samsung.com>
-rw-r--r--.dir-locals.el1
-rw-r--r--Makefile.am1
-rw-r--r--src/core/database.c141
-rw-r--r--src/core/database.h28
-rw-r--r--src/core/module.h1
5 files changed, 172 insertions, 0 deletions
diff --git a/.dir-locals.el b/.dir-locals.el
index 18bfe66..85e552f 100644
--- a/.dir-locals.el
+++ b/.dir-locals.el
@@ -3,6 +3,7 @@
;; (major-mode . ((var1 . val1) (var2 . val2)))
;; If major-mode is nil values apply for all modes.
((nil . ((indent-tabs-mode . nil)))
+ (makefile-mode . ((indent-tabs-mode . t)))
(c-mode . ((indent-tabs-mode . t)
(c-basic-offset . 4)
(tab-width . 4))))
diff --git a/Makefile.am b/Makefile.am
index f9a25a9..fcf5cb5 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -42,6 +42,7 @@ faultd_SOURCES = \
src/action/service_restart.c \
src/action/system_reboot.c \
src/action/system_reboot_to_recovery.c \
+ src/core/database.c \
src/core/event.c \
src/core/event_processor.c \
src/core/module.c \
diff --git a/src/core/database.c b/src/core/database.c
new file mode 100644
index 0000000..2824cab
--- /dev/null
+++ b/src/core/database.c
@@ -0,0 +1,141 @@
+/*
+ * This file is a part of fauld.
+ *
+ * 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 <assert.h>
+#include <ejdb/ejdb.h>
+#include <errno.h>
+#include <systemd/sd-id128.h>
+
+#include "database.h"
+#include "log.h"
+#include "module.h"
+
+struct database_adapter {
+ struct faultd_module module;
+ EJDB *db;
+ EJCOLL *coll;
+};
+
+#define to_database_adapter(MOD) \
+ container_of(MOD, struct database_adapter, module)
+
+static void cleanup_database_adapter(struct faultd_module *module);
+
+static int init_database_adapter(struct faultd_module *module,
+ struct faultd_config *config,
+ sd_event *event_loop)
+{
+ struct database_adapter *da = \
+ to_database_adapter(module);
+
+ da->db = ejdbnew();
+ if (da->db == NULL) {
+ log_error("ejdbnew has failed: %m");
+ goto error;
+ }
+
+ if (!ejdbopen(da->db, "faultdb", JBOWRITER | JBOCREAT | JBOTSYNC)) {
+ log_error("ejdbopen has failed: %s", ejdberrmsg(ejdbecode(da->db)));
+ goto error;
+ }
+
+ da->coll = ejdbcreatecoll(da->db, "log", NULL);
+ if (da->coll == NULL) {
+ log_error("ejdbcreatecoll has failed: %s", ejdberrmsg(ejdbecode(da->db)));
+ goto error;
+ }
+ return 0;
+
+error:
+ cleanup_database_adapter(module);
+ return -1;
+}
+
+static void cleanup_database_adapter(struct faultd_module *module)
+{
+ struct database_adapter *da = \
+ to_database_adapter(module);
+
+ if (da->db == NULL)
+ return;
+
+ da->coll = NULL;
+
+ if (!ejdbclose(da->db))
+ log_error("ejdbclose has failed: %s", ejdberrmsg(ejdbecode(da->db)));
+
+ ejdbdel(da->db);
+ da->db = NULL;
+}
+
+static struct database_adapter database_adapter = {
+ .module = {
+ .name = "database_adapter",
+ .type = FAULTD_MODULE_TYPE_DBADAPTER,
+ .init = init_database_adapter,
+ .cleanup = cleanup_database_adapter,
+ .node = LIST_HEAD_INIT(database_adapter.module.node),
+ },
+};
+
+FAULTD_MODULE_REGISTER(&database_adapter.module);
+
+int database_store(bson *b, bson_oid_t *oid)
+{
+ EJDB *db = database_adapter.db;
+ EJCOLL *coll = database_adapter.coll;
+
+ if (!ejdbtranbegin(coll)) {
+ log_error("ejdbtransbegin failed: %s", ejdberrmsg(ejdbecode(db)));
+ return -1;
+ }
+
+ if (!ejdbsavebson(coll, b, oid)) {
+ log_error("ejdbsavebson failed: %s", ejdberrmsg(ejdbecode(db)));
+ goto abort;
+ }
+
+ if (!ejdbtrancommit(coll)) {
+ log_error("ejdbtrancommit failed: %s", ejdberrmsg(ejdbecode(db)));
+ goto abort;
+ }
+
+ return 0;
+abort:
+ if (!ejdbtranabort(coll))
+ /* That's BAD! */
+ log_error("ejdbtranabort failed: %s", ejdberrmsg(ejdbecode(db)));
+ return -1;
+}
+
+int database_load(bson *q, bson *b)
+{
+ return -ENOTSUP;
+}
+
+int database_new_oid(bson_oid_t *oid)
+{
+ sd_id128_t uuid;
+
+ assert(oid != NULL);
+
+ sd_id128_randomize(&uuid);
+ memcpy(oid, uuid.bytes, sizeof(*oid));
+ return 0;
+}
diff --git a/src/core/database.h b/src/core/database.h
new file mode 100644
index 0000000..96cfd7c
--- /dev/null
+++ b/src/core/database.h
@@ -0,0 +1,28 @@
+/*
+ * This file is a part of fauld.
+ *
+ * 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 _DATABASE_H_
+#define _DATABASE_H_
+
+#include <ejdb/bson.h>
+
+int database_store(bson *b, bson_oid_t *oid);
+int database_load(bson *q, bson *b);
+int database_new_oid(bson_oid_t *oid);
+
+#endif /* _DATABASE_H_ */
diff --git a/src/core/module.h b/src/core/module.h
index 95990c9..f95c300 100644
--- a/src/core/module.h
+++ b/src/core/module.h
@@ -30,6 +30,7 @@ enum faultd_module_type {
FAULTD_MODULE_TYPE_MIN = 0,
FAULTD_MODULE_TYPE_UNCATEGORIZED = FAULTD_MODULE_TYPE_MIN,
FAULTD_MODULE_TYPE_CORE,
+ FAULTD_MODULE_TYPE_DBADAPTER,
FAULTD_MODULE_TYPE_EVENT,
FAULTD_MODULE_TYPE_ACTION_EXECUTOR,
FAULTD_MODULE_TYPE_ACTION,