From b9b46ba71cd73222c81d6d7653c784e3ddb1c854 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Stelmach?= Date: Mon, 22 May 2017 12:57:32 +0200 Subject: core: Create database adapter module MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Change-Id: I5a77674ae87defab22a8925b49f4db7f54d97241 Signed-off-by: Łukasz Stelmach --- .dir-locals.el | 1 + Makefile.am | 1 + src/core/database.c | 141 ++++++++++++++++++++++++++++++++++++++++++++++++++++ src/core/database.h | 28 +++++++++++ src/core/module.h | 1 + 5 files changed, 172 insertions(+) create mode 100644 src/core/database.c create mode 100644 src/core/database.h 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 +#include +#include +#include + +#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 + +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, -- cgit v1.2.3