Make database abstraction layer not depend on ejdb 35/139335/12
authorPaweł Szewczyk <p.szewczyk@samsung.com>
Tue, 1 Aug 2017 17:28:52 +0000 (19:28 +0200)
committerKonrad Kuchciak <k.kuchciak@samsung.com>
Thu, 3 Aug 2017 13:30:31 +0000 (15:30 +0200)
This commit introduces new abstraction layer for databases. Database
logic is separated into independent modules, leaving in core only
generic code.

Change-Id: I6291628b31f96df8d8acae77d659a9da733f2f36
Signed-off-by: Paweł Szewczyk <p.szewczyk@samsung.com>
[Minor fixes]
Signed-off-by: Krzysztof Opasiak <k.opasiak@samsung.com>
26 files changed:
Makefile.am
src/action/service_recover.c
src/action/service_restart.c
src/core/action.c
src/core/action.h
src/core/database.c
src/core/database.h
src/core/event.c
src/core/event.h
src/core/event_processor.c
src/core/service.c
src/core/service.h
src/database/database_nop.c [new file with mode: 0644]
src/database/ejdb.c [new file with mode: 0644]
src/decision_makers/rv_dm.c
src/decision_makers/standard_fault_dm.c
src/decision_makers/vip_fault_dm.c
src/event_types/action_executed_event.c
src/event_types/action_executed_event.h
src/event_types/decision_made_event.c
src/event_types/decision_made_event.h
src/event_types/faultd_started_event.c
src/event_types/resource_violation_event.c
src/event_types/service_failed_event.c
src/event_types/system_booted_event.c
src/listeners/startup.c

index e61b455bfb56e3e5656723d67aee448063ea3a9a..f3e35b3ec2888be5d91f002593844dc61f8ebfdd 100644 (file)
@@ -61,6 +61,8 @@ faultd_SOURCES = \
     src/core/event_processor.c \
     src/core/module.c \
     src/core/service.c \
+    src/database/database_nop.c \
+    src/database/ejdb.c \
     src/core/faultd-config.c \
     src/event_types/action_executed_event.c \
     src/event_types/decision_made_event.c \
index 4292c9a521943e0dbef33d8627d631e46f001ea1..a34d15e595076ecb99baaef4781f3e92ac668948 100644 (file)
@@ -28,11 +28,11 @@ static int recover_service(struct faultd_action *action)
        struct faultd_event *ev = nqueue_pop(&action->execution_queue,
                                                                                 struct faultd_event, nq_node);
        struct decision_made_event *dm_ev = to_decision_made_event(ev);
-       char *service_path;
+       char *service_path = NULL;
        int ret;
 
-       service_path = bson_get_string(&dm_ev->action_data, FAULTD_AD_SERVICE_NAME);
-       if (!service_path) {
+       ret = faultd_object_get_string(dm_ev->action_data, FAULTD_AD_SERVICE_NAME, &service_path);
+       if (ret < 0) {
                log_error("Service path has not been provided");
                goto unref_old_event;
        }
index ba7bedef98de41b579f22b1ef851ca24e8f7e739..5d284fb58452163f73c7be16d7c60061c0ffbe54 100644 (file)
@@ -28,11 +28,11 @@ static int restart_service(struct faultd_action *action)
        struct faultd_event *ev = nqueue_pop(&action->execution_queue,
                                                                                 struct faultd_event, nq_node);
        struct decision_made_event *dm_ev = to_decision_made_event(ev);
-       char *service_path;
+       char *service_path = NULL;
        int ret;
 
-       service_path = bson_get_string(&dm_ev->action_data, FAULTD_AD_SERVICE_NAME);
-       if (!service_path) {
+       ret = faultd_object_get_string(dm_ev->action_data, FAULTD_AD_SERVICE_NAME, &service_path);
+       if (ret < 0) {
                log_error("Service path has not been provided");
                goto unref_old_event;
        }
index 123568a0caf2afa928a559c837b3d2701dab6cf2..517d3d64082a6cadd38f1bf3f86c81f2ed7cd53e 100644 (file)
 #include "log.h"
 #include "action.h"
 
-int bson_fill_for_srv_restart(bson *b, char *service_path)
+int faultd_fill_for_srv_restart(struct faultd_object *obj, char *service_path)
 {
        int ret;
 
-       ret = faultd_bson_safe_init(b);
-       if (ret != BSON_OK) {
-               log_error("Unable to create bson");
-               return -ENOMEM;
-       }
-
-       ret = bson_append_string(b, FAULTD_AD_SERVICE_NAME, service_path);
-       if (ret != BSON_OK) {
+       ret = faultd_object_append_string(obj, FAULTD_AD_SERVICE_NAME, service_path);
+       if (ret < 0) {
                log_error("Unable to append %s:%s",
                                  FAULTD_AD_SERVICE_NAME, service_path);
-               goto del_bson;
-       }
-
-       ret = bson_finish(b);
-       if (ret != BSON_OK) {
-               log_error("Unable to finish bson");
-               goto del_bson;
+               return -ENOMEM;
        }
 
        return 0;
-
-del_bson:
-       bson_destroy(b);
-       return -ENOMEM;
 }
 
-int bson_fill_for_srv_recover(bson *b, char *service_path, char *recovery)
+int faultd_fill_for_srv_recover(struct faultd_object *obj, char *service_path, char *recovery)
 {
        int ret;
 
-       ret = faultd_bson_safe_init(b);
-       if (ret != BSON_OK) {
-               log_error("Unable to create bson");
-               return -ENOMEM;
-       }
-
-       ret = bson_append_string(b, FAULTD_AD_SERVICE_NAME, service_path);
-       if (ret != BSON_OK) {
+       ret = faultd_object_append_string(obj, FAULTD_AD_SERVICE_NAME, service_path);
+       if (ret < 0) {
                log_error("Unable to append %s:%s",
                                  FAULTD_AD_SERVICE_NAME, service_path);
-               goto del_bson;
+               return -ENOMEM;
        }
 
        if (recovery) {
-               ret = bson_append_string(b, FAULTD_AD_CLEANUP_UNIT, recovery);
-               if (ret != BSON_OK) {
+               ret = faultd_object_append_string(obj, FAULTD_AD_CLEANUP_UNIT, recovery);
+               if (ret < 0) {
                        log_error("Unable to append %s:%s",
                                          FAULTD_AD_CLEANUP_UNIT, recovery);
-                       goto del_bson;
+                       return -ENOMEM;
                }
        }
 
-       ret = bson_finish(b);
-       if (ret != BSON_OK) {
-               log_error("Unable to finish bson");
-               goto del_bson;
-       }
-
        return 0;
-
-del_bson:
-       bson_destroy(b);
-       return -ENOMEM;
 }
index 4a727d4c59eb380a3e57264f2eaadcd6acf68276..13785fcfb29b929a1c3d0dad22be923894e16ad9 100644 (file)
@@ -19,8 +19,6 @@
 #ifndef FAULTD_ACTION_H
 #define FAULTD_ACTION_H
 
-#include <ejdb/bson.h>
-
 #include "common.h"
 
 #define FAULTD_ACTION_SERVICE_RESTART_ID "org.tizen.faultd.action.SERVICE_RESTART"
 #define FAULTD_AD_SERVICE_NAME "ServiceName"
 #define FAULTD_AD_CLEANUP_UNIT "RecoveryUnit"
 
-int bson_fill_for_srv_restart(bson *b, char *service_path);
+int faultd_fill_for_srv_restart(struct faultd_object *obj, char *service_path);
 
-int bson_fill_for_srv_recover(bson *b, char *service_path, char *recovery);
+int faultd_fill_for_srv_recover(struct faultd_object *obj, char *service_path, char *recovery);
 
-#define bson_fill_for_reboot(b)                                        \
-       bson_fill_empty(b)
+#define faultd_fill_for_reboot(o)                                      \
+       faultd_object_fill_empty(o)
 
-#define bson_fill_for_recovery_reboot(b)               \
-       bson_fill_empty(b)
+#define faultd_fill_for_recovery_reboot(o)             \
+       faultd_object_fill_empty(o)
 
 #endif /* FAULTD_ACTION_H */
index c4332ce96e7d2f0852c0945cfe3b0d388e7dc203..d90fa9030d977ec5e5c6f60e382c15925eb3348a 100644 (file)
 #include "log.h"
 #include "module.h"
 
-struct database_adapter {
-       struct faultd_module module;
-       EJDB *db;
-       EJCOLL *coll;
-};
+static struct faultd_database_adapter *database_adapter = NULL;
 
-#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)
+void faultd_set_database_adapter(struct faultd_database_adapter *adapter)
 {
-       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, DBDIR "/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;
+       database_adapter = adapter;
 }
 
-static void cleanup_database_adapter(struct faultd_module *module)
+int database_store(struct faultd_object *obj, faultd_oid_t *oid)
 {
-       struct database_adapter *da = \
-               to_database_adapter(module);
-
-       if (da->db == NULL)
-               return;
-
-       da->coll = NULL;
+       if (!database_adapter)
+               return -ENOSYS;
 
-       if (!ejdbclose(da->db))
-               log_error("ejdbclose has failed: %s", ejdberrmsg(ejdbecode(da->db)));
-
-       ejdbdel(da->db);
-       da->db = NULL;
+       return database_adapter->store(database_adapter, obj, oid);
 }
 
-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)
+int database_load(struct faultd_object *query, struct faultd_object *result, uint32_t *nr)
 {
-       EJDB *db = database_adapter.db;
-       EJCOLL *coll = database_adapter.coll;
-
-       if (!ejdbtranbegin(coll)) {
-               log_error("ejdbtransbegin failed: %s", ejdberrmsg(ejdbecode(db)));
-               return -1;
-       }
+       if (!database_adapter)
+               return -ENOSYS;
 
-       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;
+       return database_adapter->load(database_adapter, query, result, nr);
 }
 
-int database_load(bson *bq, bson *br, uint32_t *nr)
+int database_new_oid(faultd_oid_t *oid)
 {
-       EJDB *db = database_adapter.db;
-       EJCOLL *coll = database_adapter.coll;
-       EJQ *q = NULL;
-       TCLIST *r = NULL;
-       uint32_t count = 0;
-       int ret = -EINVAL;
-       int qflags = 0;
-
-       if (br == NULL)
-               qflags |= JBQRYCOUNT;
-
-       q = ejdbcreatequery(db, bq, NULL, 0, NULL);
-       if (q == NULL) {
-               log_error("ejdbcreatequery failed: %s", ejdberrmsg(ejdbecode(db)));
-               goto finish;
-       }
-
-       r = ejdbqryexecute(coll, q, &count, qflags, NULL);
-       if ((r == NULL) && (ejdbecode(db) != TCESUCCESS)) {
-               log_error("ejdbqryexecute failed: %s", ejdberrmsg(ejdbecode(db)));
-               goto finish;
-       }
-
-       log_debug("ejdbqueryexecute: %u records", count);
-
-       if (br != NULL)
-               bson_init(br);
-
-       if (r == NULL) {
-               ret = 0;
-               goto finish;
-       }
-
-       for (uint32_t i = 0; i < count; ++i) {
-               void *bsdata = TCLISTVALPTR(r, i);
-               bson val;
-               char key[11]; /* strlen(itoa(1U<<31, key, 10)) => 10 */
-
-               bson_init_finished_data(&val, bsdata);
-               snprintf(key, sizeof(key), "%u", i);
-               bson_append_bson(br, key, &val);
-       }
-
-       ret = 0;
-finish:
-       if (r != NULL)
-               tclistdel(r);
-
-       if (q != NULL)
-               ejdbquerydel(q);
-
-       if (ret == 0) {
-               if (nr != NULL)
-                       *nr = count;
-
-               if (br != NULL)
-                       bson_finish(br);
-       }
-
-       return ret;
-}
-
-int database_new_oid(bson_oid_t *oid)
-{
-       sd_id128_t uuid;
-
-       assert(oid != NULL);
+       if (!database_adapter)
+               return -ENOSYS;
 
-       sd_id128_randomize(&uuid);
-       memcpy(oid, uuid.bytes, sizeof(*oid));
-       return 0;
+       return database_adapter->new_oid(oid);
 }
 
-bson *database_get_by_oid(bson_oid_t *oid)
+int database_get_by_oid(faultd_oid_t *oid, struct faultd_object *result)
 {
-       EJCOLL *coll = database_adapter.coll;
+       if (!database_adapter)
+               return -ENOSYS;
 
-       return ejdbloadbson(coll, oid);
+       return database_adapter->get_by_oid(database_adapter, oid, result);
 }
index 0b66eaed7a8d4048acdee1ca8f465e81eedec837..0ad07705f5f179b4566acb3f92f9e02a222de082 100644 (file)
 
 #include <ejdb/bson.h>
 
-int database_store(bson *b, bson_oid_t *oid);
-bson *database_get_by_oid(bson_oid_t *oid);
-int database_load(bson *q, bson *b, uint32_t *nr);
-int database_new_oid(bson_oid_t *oid);
+#include "common.h"
+#include "module.h"
+
+struct faultd_database_adapter {
+       struct faultd_module module;
+       char *name;
+
+       int (*store)(struct faultd_database_adapter *adapter,
+                                struct faultd_object *obj, faultd_oid_t *oid);
+
+       int (*get_by_oid)(struct faultd_database_adapter *adapter,
+                                         faultd_oid_t *oid, struct faultd_object *result);
+
+       int (*load)(struct faultd_database_adapter *adapter,
+                               struct faultd_object *query, struct faultd_object *result,
+                               uint32_t *nr);
+
+       int (*new_oid)(faultd_oid_t *oid);
+};
+
+#define to_database_adapter(MOD)                               \
+       container_of(MOD, struct faultd_database_adapter, module)
+
+void faultd_set_database_adapter(struct faultd_database_adapter *adapter);
+int database_store(struct faultd_object *obj, faultd_oid_t *oid);
+int database_get_by_oid(faultd_oid_t *oid, struct faultd_object *result);
+int database_load(struct faultd_object *query, struct faultd_object *result, uint32_t *nr);
+int database_new_oid(faultd_oid_t *oid);
 
 #endif /* _DATABASE_H_ */
index ff8f08b7e9c98a607db7589b78fe12e432763a4e..c71e35d4093b3cda1fa9432ed91a2ae76909fb46 100644 (file)
@@ -16,7 +16,6 @@
  * limitations under the License.
  */
 
-#include <ejdb/bson.h>
 #include <errno.h>
 #include <systemd/sd-id128.h>
 
@@ -90,10 +89,10 @@ int faultd_event_create(const char *type, void *data, struct faultd_event **ev)
 
        from_bson = (strcmp(type, BSON_EVENT_ID) == 0);
        if (from_bson) {
-               bson *d = data;
+               struct faultd_object *d = data;
                /* get event type name from bson */
-               type = bson_get_string(d, EV_TYPE_NAME);
-               if (type == NULL)
+               ret = faultd_object_get_string(d, EV_TYPE_NAME, (char **)&type);
+               if (ret < 0)
                        return ret;
        }
 
@@ -151,40 +150,28 @@ int faultd_event_init_internal(struct faultd_event_type *ev_type,
        return 0;
 }
 
-void faultd_event_serialize_internal(struct faultd_event *ev, bson *out)
+void faultd_event_serialize_internal(struct faultd_event *ev, struct faultd_object *out)
 {
-       /* FIXME: check return values of bson_append_XXX */
-       bson_append_binary(out, EV_BOOT_ID, BSON_BIN_UUID, (char *)&ev->boot_id, sizeof(ev->boot_id));
+       /* FIXME: check return values of faultd_object_append_XXX */
+       faultd_object_append_uuid(out, EV_BOOT_ID, &ev->boot_id);
        /* FIXME: serialise event_type structure (?) */
-       bson_append_string(out, EV_TYPE_NAME, ev->type->name);
-       faultd_bson_append_timespec(out, EV_TIMESTAMP, &ev->timestamp);
+       faultd_object_append_string(out, EV_TYPE_NAME, ev->type->name);
+       faultd_object_append_timespec(out, EV_TIMESTAMP, &ev->timestamp);
 }
 
-int faultd_event_deserialize_internal(const bson* in, struct faultd_event_type *ev_type, struct faultd_event *ev)
+int faultd_event_deserialize_internal(const struct faultd_object* in, struct faultd_event_type *ev_type, struct faultd_event *ev)
 {
-       bson_iterator it;
-       bson_type bt;
-       int ret = -EINVAL;
+       struct faultd_object *obj;
 
        ev->type = ev_type;
        ev->ops = ev_type->default_ops;
 
-       BSON_ITERATOR_INIT(&it, in);
-
-       while ((bt = bson_iterator_next(&it)) != BSON_EOO) {
-               if ((bt == BSON_BINDATA) &&
-                       (bson_iterator_bin_type(&it) == BSON_BIN_UUID) &&
-                       (strcmp(EV_BOOT_ID, bson_iterator_key(&it)) == 0))
-
-                       memcpy(&ev->boot_id, bson_iterator_bin_data(&it), sizeof(ev->boot_id));
-
-               else if ((bt == BSON_OBJECT) &&
-                                  (strcmp(EV_TIMESTAMP, bson_iterator_key(&it)) == 0)) {
-
-                       faultd_bson_iterator_timespec(&it, &ev->timestamp);
-               }
+       list_for_each_entry(obj, &in->val.children, node) {
+               if (obj->type == TYPE_UUID && strcmp(EV_BOOT_ID, obj->key) == 0)
+                       memcpy(&ev->boot_id, &obj->val.uuid, sizeof(ev->boot_id));
+               else if (obj->type == TYPE_TIMESPEC && strcmp(EV_TIMESTAMP, obj->key) == 0)
+                       memcpy(&ev->timestamp, &obj->val.ts, sizeof(ev->timestamp));
        }
-       ret = 0;
-finish:
-       return ret;
+
+       return 0;
 }
index 3bf08fcd021405e163c8bc4ba96e7e3f32f9ac7d..bf40306933dceec6ccda4974705a633a7fc3a522 100644 (file)
@@ -19,7 +19,6 @@
 #ifndef FAULTD_EVENT_H
 #define FAULTD_EVENT_H
 
-#include <ejdb/bson.h>
 #include <string.h>
 #include <systemd/sd-id128.h>
 #include <time.h>
@@ -40,7 +39,7 @@ struct faultd_event;
 struct faultd_event_ops {
        char *(*to_string)(struct faultd_event *);
        void (*release)(struct faultd_event *);
-       void (*serialize)(struct faultd_event*, bson*);
+       void (*serialize)(struct faultd_event*, struct faultd_object*);
 };
 
 struct faultd_event_type {
@@ -55,7 +54,7 @@ struct faultd_event_type {
                                                  void *data, struct faultd_event **ev);
 
        int (*deserialize_event)(struct faultd_event_type *type,
-                                                        bson *data, struct faultd_event **ev);
+                                                        struct faultd_object *data, struct faultd_event **ev);
 
        /* To be used by event factory */
        struct list_head node;
@@ -75,7 +74,7 @@ struct faultd_event {
        struct list_head node;
        /* To be used by event processig FW */
        struct nqueue_node nq_node;
-       bson_oid_t oid;
+       faultd_oid_t oid;
 };
 
 int faultd_event_type_register(struct faultd_event_type *type);
@@ -127,7 +126,7 @@ static inline char *faultd_event_to_string(struct faultd_event *ev)
        return ev->ops.to_string(ev);
 }
 
-static inline void faultd_event_serialize(struct faultd_event *ev, bson *out)
+static inline void faultd_event_serialize(struct faultd_event *ev, struct faultd_object *out)
 {
        ev->ops.serialize(ev, out);
 }
@@ -137,8 +136,8 @@ int faultd_event_init_internal(struct faultd_event_type *ev_type,
 
 static inline void faultd_event_cleanup_internal(struct faultd_event *ev) {}
 
-void faultd_event_serialize_internal(struct faultd_event *ev, bson *out);
+void faultd_event_serialize_internal(struct faultd_event *ev, struct faultd_object *out);
 
-int faultd_event_deserialize_internal(const bson* in, struct faultd_event_type *ev_type, struct faultd_event *ev);
+int faultd_event_deserialize_internal(const struct faultd_object *in, struct faultd_event_type *ev_type, struct faultd_event *ev);
 
 #endif /* FAULTD_EVENT_H */
index 8c437afdff7fcfb66bae635894b95ffac9b1def8..0b86b0e89384ffe336da0c59f3150c91d9c2a81e 100644 (file)
@@ -133,18 +133,17 @@ FAULTD_MODULE_REGISTER(&event_processor.module);
 
 int event_processor_report_event(struct faultd_event *ev)
 {
-       bson b;
-       bson_oid_t oid;
+       struct faultd_object *obj;
+       faultd_oid_t oid;
 
-       bson_init(&b);
+       faultd_object_new(&obj);
        database_new_oid(&oid);
-       bson_append_oid(&b, JDBIDKEYNAME, &oid);
-       faultd_event_serialize(ev, &b);
-       bson_finish(&b);
+       faultd_object_append_oid(obj, JDBIDKEYNAME, &oid);
+       faultd_event_serialize(ev, obj);
 
-       database_store(&b, &ev->oid);
+       database_store(obj, &ev->oid);
 
-       bson_destroy(&b);
+       faultd_object_unref(obj);
 
        return nqueue_append(&event_processor.pending_events,
                                                 &ev->nq_node);
index c2e03890f41df0ca81abac4380aa0d82f4bf5699..98fea5f2f5597e9bc2c5f58a72da4b3403e47abb 100644 (file)
@@ -228,46 +228,53 @@ int systemd_get_unit_by_pid(pid_t pid, const char **name)
        return 0;
 }
 
-void systemd_service_serialize(struct systemd_service *s, bson* out)
+void systemd_service_serialize(struct systemd_service *s, struct faultd_object* out)
 {
-       bson_append_start_object(out, SYSD_SERVICE);
-       bson_append_string(out, SYSD_SERVICE_DBUS_PATH, s->dbus_path);
+       struct faultd_object *child;
+       int ret;
+
+       ret = faultd_object_new(&child);
+       if (ret < 0)
+               return;
+
+       faultd_object_append_string(child, SYSD_SERVICE_DBUS_PATH, s->dbus_path);
        if (s->service_type != NULL)
-               bson_append_string(out, SYSD_SERVICE_SERVICE_TYPE, s->service_type);
+               faultd_object_append_string(child, SYSD_SERVICE_SERVICE_TYPE, s->service_type);
        if (s->recovery_unit != NULL)
-               bson_append_string(out, SYSD_SERVICE_RECOVERY_UNIT, s->recovery_unit);
-       bson_append_finish_object(out);
+               faultd_object_append_string(child, SYSD_SERVICE_RECOVERY_UNIT, s->recovery_unit);
+
+       faultd_object_append_object(out, SYSD_SERVICE, child);
+
+       faultd_object_unref(child);
 }
 
-int systemd_service_deserialize(const bson* in, struct systemd_service *s)
+int systemd_service_deserialize(const struct faultd_object* in, struct systemd_service *s)
 {
-       bson_iterator it;
-       bson_type bt;
+       struct faultd_object *obj;
        int ret = -EINVAL;
        bool dbus_path = false;
 
        memset(s, 0, sizeof(*s));
-       BSON_ITERATOR_INIT(&it, in);
 
-       while ((bt = bson_iterator_next(&it)) != BSON_EOO) {
-               if ((bt == BSON_STRING) &&
-                       (strcmp(SYSD_SERVICE_DBUS_PATH, bson_iterator_key(&it)) == 0)) {
-                       s->dbus_path = strdup(bson_iterator_string(&it));
+       list_for_each_entry(obj, &in->val.children, node) {
+               if ((obj->type == TYPE_STRING) &&
+                       (strcmp(SYSD_SERVICE_DBUS_PATH, obj->key) == 0)) {
+                       s->dbus_path = strdup(obj->val.s);
                        if (s->dbus_path == NULL) {
                                ret = -errno;
                                goto cleanup;
                        }
                        dbus_path = true;
-               } else if ((bt == BSON_STRING) &&
-                                  (strcmp(SYSD_SERVICE_SERVICE_TYPE, bson_iterator_key(&it)) == 0)) {
-                       s->service_type = strdup(bson_iterator_string(&it));
+               } else if ((obj->type == TYPE_STRING) &&
+                                  (strcmp(SYSD_SERVICE_SERVICE_TYPE, obj->key) == 0)) {
+                       s->service_type = strdup(obj->val.s);
                        if (s->service_type == NULL) {
                                ret = -errno;
                                goto cleanup;
                        }
-               } else if ((bt == BSON_STRING) &&
-                                  (strcmp(SYSD_SERVICE_RECOVERY_UNIT, bson_iterator_key(&it)) == 0)) {
-                       s->recovery_unit = strdup(bson_iterator_string(&it));
+               } else if ((obj->type == TYPE_STRING) &&
+                                  (strcmp(SYSD_SERVICE_RECOVERY_UNIT, obj->key) == 0)) {
+                       s->recovery_unit = strdup(obj->val.s);
                        if (s->recovery_unit == NULL) {
                                ret = -errno;
                                goto cleanup;
@@ -277,7 +284,7 @@ int systemd_service_deserialize(const bson* in, struct systemd_service *s)
 
        if (dbus_path)
                ret = 0;
-finish:
+
        return ret;
 
 cleanup:
index c5563a8e743e7da5aee1dd304a3f63565c42e97e..ef9c393df20fb56272b9e411592b1ea01c31e1a1 100644 (file)
@@ -19,7 +19,6 @@
 #ifndef FAULTD_SERVICE_H
 #define FAULTD_SERVICE_H
 
-#include <ejdb/bson.h>
 #include <sys/types.h>
 #include <unistd.h>
 
@@ -43,8 +42,8 @@ int systemd_service_init_by_pid(pid_t pid, struct systemd_service *s);
 int systemd_service_init(const char *dbus_path, struct systemd_service *s);
 void systemd_service_cleanup(struct systemd_service *s);
 int systemd_get_unit_by_pid(pid_t pid, const char **name);
-void systemd_service_serialize(struct systemd_service *s, bson *out);
-int systemd_service_deserialize(const bson *out, struct systemd_service *s);
+void systemd_service_serialize(struct systemd_service *s, struct faultd_object *out);
+int systemd_service_deserialize(const struct faultd_object *out, struct systemd_service *s);
 
 static inline int systemd_service_is_of_type(struct systemd_service *s,
                                                                                         char *type)
diff --git a/src/database/database_nop.c b/src/database/database_nop.c
new file mode 100644 (file)
index 0000000..a5a86cf
--- /dev/null
@@ -0,0 +1,82 @@
+/*
+ * 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 "database.h"
+
+struct nop_adapter {
+       struct faultd_database_adapter database_adapter;
+};
+
+#define to_nop_adapter(MOD)                            \
+       container_of(MOD, struct nop_adapter, database_adapter)
+
+
+static int init_nop_adapter(struct faultd_module *module,
+                                                       struct faultd_config *config,
+                                                       sd_event *event_loop)
+{
+       faultd_set_database_adapter(to_database_adapter(module));
+       return 0;
+}
+
+static void cleanup_nop_adapter(struct faultd_module *module)
+{
+}
+
+static int nop_store(struct faultd_database_adapter *adapter,
+                                        struct faultd_object *obj, faultd_oid_t *oid)
+{
+       return 0;
+}
+
+static int nop_get_by_oid(struct faultd_database_adapter *adapter,
+                                                 faultd_oid_t *oid, struct faultd_object *result)
+{
+       return -ENOENT;
+}
+
+static int nop_load(struct faultd_database_adapter *adapter,
+                                       struct faultd_object *query,
+                                       struct faultd_object *result, uint32_t *nr)
+{
+       *nr = 0;
+       return 0;
+}
+
+static int nop_new_oid(faultd_oid_t *oid)
+{
+       return 0;
+}
+
+static struct nop_adapter nop_adapter = {
+       .database_adapter = {
+               .module = {
+                       .name = "nop_dbadapter",
+                       .type = FAULTD_MODULE_TYPE_DBADAPTER,
+                       .init = init_nop_adapter,
+                       .cleanup = cleanup_nop_adapter,
+                       .node = LIST_HEAD_INIT(nop_adapter.database_adapter.module.node),
+               },
+               .store = nop_store,
+               .get_by_oid = nop_get_by_oid,
+               .load = nop_load,
+               .new_oid = nop_new_oid,
+       }
+};
+
+FAULTD_MODULE_REGISTER(&nop_adapter.database_adapter.module);
diff --git a/src/database/ejdb.c b/src/database/ejdb.c
new file mode 100644 (file)
index 0000000..d8b6c89
--- /dev/null
@@ -0,0 +1,332 @@
+/*
+ * 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 ejdb_adapter {
+       struct faultd_database_adapter database_adapter;
+       EJDB *db;
+       EJCOLL *coll;
+};
+
+#define to_ejdb_adapter(MOD)                           \
+       container_of(MOD, struct ejdb_adapter, database_adapter)
+
+static void cleanup_ejdb_adapter(struct faultd_module *module)
+{
+       struct faultd_database_adapter *_da = to_database_adapter(module);
+       struct ejdb_adapter *da = to_ejdb_adapter(_da);
+
+       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 int init_ejdb_adapter(struct faultd_module *module,
+                                                                struct faultd_config *config,
+                                                                sd_event *event_loop)
+{
+       struct faultd_database_adapter *_da = to_database_adapter(module);
+       struct ejdb_adapter *da = to_ejdb_adapter(_da);
+
+       faultd_set_database_adapter(_da);
+
+       da->db = ejdbnew();
+       if (da->db == NULL) {
+               log_error("ejdbnew has failed: %m");
+               goto error;
+       }
+
+       if (!ejdbopen(da->db, DBDIR "/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_ejdb_adapter(module);
+       return -1;
+}
+
+static int faultd_object_to_bson(struct faultd_object *obj, bson *out)
+{
+       struct faultd_object *child;
+
+       if (obj->type != TYPE_OBJECT)
+               return -EINVAL;
+
+       bson_init(out);
+       list_for_each_entry(child, &obj->val.children, node) {
+               switch (child->type) {
+               case TYPE_OID:
+                       bson_append_oid(out, child->key, &child->val.oid.bson);
+                       break;
+               case TYPE_STRING:
+                       bson_append_string(out, child->key, child->val.s);
+                       break;
+               case TYPE_INT:
+                       bson_append_int(out, child->key, child->val.i);
+                       break;
+               case TYPE_TIMESPEC:
+                       /* TODO */
+                       break;
+               case TYPE_TIME_T:
+                       bson_append_time_t(out, child->key, child->val.time);
+                       break;
+               case TYPE_UUID:
+                       bson_append_binary(out, child->key, BSON_BIN_UUID,
+                                       (char *)(&child->val.uuid), sizeof(child->val.uuid));
+                       break;
+               case TYPE_OBJECT:
+                       {
+                               bson b;
+
+                               faultd_object_to_bson(child, &b);
+                               bson_append_bson(out, child->key, &b);
+                               break;
+                       }
+               }
+       }
+
+       bson_finish(out);
+       return 0;
+}
+
+static int bson_to_faultd_object(bson *b, struct faultd_object *out)
+{
+       bson_type bt;
+       bson_iterator it;
+       int ret;
+
+       BSON_ITERATOR_INIT(&it, b);
+
+       while ((bt = bson_iterator_next(&it)) != BSON_EOO) {
+               const char *key = bson_iterator_key(&it);
+               faultd_oid_t oid;
+
+               switch (bt) {
+               case BSON_OID:
+                       oid.bson = *bson_iterator_oid(&it);
+                       ret = faultd_object_append_oid(out, key, &oid);
+                       break;
+               case BSON_STRING:
+                       ret = faultd_object_append_string(out, key, bson_iterator_string(&it));
+                       break;
+               case BSON_INT:
+                       ret = faultd_object_append_int(out, key, bson_iterator_int(&it));
+                       break;
+               case BSON_TIMESTAMP:
+                       /* TODO */
+                       ret = 0;
+                       break;
+               case BSON_LONG:
+                       ret = faultd_object_append_time_t(out, key, bson_iterator_time_t(&it));
+                       break;
+               case BSON_BINDATA:
+                       /* FIXME: find information about subtype */
+                       /* we assume every bindata is uuid */
+                       ret = faultd_object_append_uuid(out, key, (sd_id128_t *)bson_iterator_bin_data(&it));
+                       break;
+               case BSON_OBJECT:
+                       /* TODO */
+                       ret = -ENOTSUP;
+                       break;
+               default:
+                       ret = -ENOTSUP;
+               }
+
+               if (ret < 0)
+                       return ret;
+       }
+
+       return 0;
+}
+
+static int ejdb_store(struct faultd_database_adapter *adapter, struct faultd_object *obj, faultd_oid_t *oid)
+{
+       struct ejdb_adapter *da = to_ejdb_adapter(adapter);
+       EJDB *db = da->db;
+       EJCOLL *coll = da->coll;
+       bson b;
+
+       if (!ejdbtranbegin(coll)) {
+               log_error("ejdbtransbegin failed: %s", ejdberrmsg(ejdbecode(db)));
+               return -1;
+       }
+
+       if (faultd_object_to_bson(obj, &b) < 0)
+               goto abort;
+
+       if (!ejdbsavebson(coll, &b, &oid->bson)) {
+               log_error("ejdbsavebson failed: %s", ejdberrmsg(ejdbecode(db)));
+               goto destroy_bson;
+       }
+
+       if (!ejdbtrancommit(coll)) {
+               log_error("ejdbtrancommit failed: %s", ejdberrmsg(ejdbecode(db)));
+               goto destroy_bson;
+       }
+
+       bson_destroy(&b);
+
+       return 0;
+
+destroy_bson:
+       bson_destroy(&b);
+
+abort:
+       if (!ejdbtranabort(coll))
+               /* That's BAD! */
+               log_error("ejdbtranabort failed: %s", ejdberrmsg(ejdbecode(db)));
+       return -1;
+}
+
+static int ejdb_get_by_oid(struct faultd_database_adapter *adapter, faultd_oid_t *oid, struct faultd_object *result)
+{
+       struct ejdb_adapter *da = to_ejdb_adapter(adapter);
+       EJCOLL *coll = da->coll;
+       bson *b;
+
+       b = ejdbloadbson(coll, &oid->bson);
+       if (b == NULL)
+               return -ENOENT;
+
+       return bson_to_faultd_object(b, result);
+}
+
+static int ejdb_load(struct faultd_database_adapter *adapter, struct faultd_object *query, struct faultd_object *result, uint32_t *nr)
+{
+       struct ejdb_adapter *da = to_ejdb_adapter(adapter);
+       EJDB *db = da->db;
+       EJCOLL *coll = da->coll;
+       EJQ *q = NULL;
+       TCLIST *r = NULL;
+       uint32_t count = 0;
+       int ret = -EINVAL;
+       int qflags = 0;
+       bson bq;
+
+       if (result == NULL)
+               qflags |= JBQRYCOUNT;
+
+       ret = faultd_object_to_bson(query, &bq);
+       if (ret < 0)
+               goto finish;
+
+       q = ejdbcreatequery(db, &bq, NULL, 0, NULL);
+       if (q == NULL) {
+               log_error("ejdbcreatequery failed: %s", ejdberrmsg(ejdbecode(db)));
+               goto finish;
+       }
+
+       r = ejdbqryexecute(coll, q, &count, qflags, NULL);
+       if ((r == NULL) && (ejdbecode(db) != TCESUCCESS)) {
+               log_error("ejdbqryexecute failed: %s", ejdberrmsg(ejdbecode(db)));
+               goto finish;
+       }
+
+       log_debug("ejdbqueryexecute: %u records", count);
+
+       if (r == NULL) {
+               ret = 0;
+               goto finish;
+       }
+
+       for (uint32_t i = 0; i < count; ++i) {
+               void *bsdata = TCLISTVALPTR(r, i);
+               bson val;
+               struct faultd_object *obj;
+               char key[11]; /* strlen(itoa(1U<<31, key, 10)) => 10 */
+
+               bson_init_finished_data(&val, bsdata);
+               snprintf(key, sizeof(key), "%u", i);
+               ret = faultd_object_new(&obj);
+               if (ret < 0)
+                       goto finish;
+
+               bson_to_faultd_object(&val, obj);
+               /* TODO Check if binary fields are matching if there are any*/
+               faultd_object_append_object(result, key, obj);
+       }
+
+       ret = 0;
+finish:
+       if (r != NULL)
+               tclistdel(r);
+
+       if (q != NULL)
+               ejdbquerydel(q);
+
+       if (ret == 0) {
+               if (nr != NULL)
+                       *nr = count;
+       }
+
+       return ret;
+}
+
+static int ejdb_new_oid(faultd_oid_t *oid)
+{
+       sd_id128_t uuid;
+
+       assert(oid != NULL);
+
+       sd_id128_randomize(&uuid);
+       memcpy(&oid->bson, uuid.bytes, sizeof(*oid));
+       return 0;
+}
+
+
+static struct ejdb_adapter ejdb_adapter = {
+       .database_adapter = {
+               .module = {
+                       .name = "ejdb_dbadapter",
+                       .type = FAULTD_MODULE_TYPE_DBADAPTER,
+                       .init = init_ejdb_adapter,
+                       .cleanup = cleanup_ejdb_adapter,
+                       .node = LIST_HEAD_INIT(ejdb_adapter.database_adapter.module.node),
+               },
+               .store = ejdb_store,
+               .get_by_oid = ejdb_get_by_oid,
+               .load = ejdb_load,
+               .new_oid = ejdb_new_oid,
+       }
+};
+
+FAULTD_MODULE_REGISTER(&ejdb_adapter.database_adapter.module);
index 4de21f857c5b8507c598be3c729c0ac186f0917b..151c832381b6822c5992476f5c4d7b94079e030b 100644 (file)
@@ -54,17 +54,18 @@ static int rv_make_decision(struct faultd_event_handler *handler)
        };
        int ret;
 
+       faultd_object_new(&ev_data.action_data);
        if (systemd_service_is_of_type(&rv_ev->service, FAULTD_SERVICE_TYPE_VIP)) {
                ev_data.action = FAULTD_ACTION_REBOOT_ID;
-               ret = bson_fill_for_reboot(&ev_data.action_data);
+               ret = faultd_fill_for_reboot(ev_data.action_data);
        } else {
-               ret = bson_fill_for_srv_restart(&ev_data.action_data,
+               ret = faultd_fill_for_srv_restart(ev_data.action_data,
                                                                          rv_ev->service.dbus_path);
        }
 
        if (ret) {
                log_error("Unable to create action data");
-               goto unref_old_event;
+               goto del_action_data;
        }
 
        ret = faultd_event_create(DECISION_MADE_EVENT_ID, &ev_data, &new_ev);
@@ -81,15 +82,13 @@ static int rv_make_decision(struct faultd_event_handler *handler)
                goto unref_new_event;
        }
 
+       faultd_object_unref(ev_data.action_data);
        return 0;
 
 unref_new_event:
        faultd_event_unref(new_ev);
-       return 0;
-
 del_action_data:
-       bson_destroy(&ev_data.action_data);
-unref_old_event:
+       faultd_object_unref(ev_data.action_data);
        faultd_event_unref(ev);
        return 0;
 }
index 1a644c5d93111e501372b1d8e865cafedb76bd05..dc4ec21fe291a38b35028f9e67506e0581d57d96 100644 (file)
@@ -50,70 +50,55 @@ static int sf_make_decision(struct faultd_event_handler *handler)
                .who_made = MODULE_NAME,
        };
        int ret;
-       bson query, b;
+       struct faultd_object *query, *service_obj;
        sd_id128_t boot_id;
-       int nr;
-       bson_type bt;
-       bson_iterator it;
+       uint32_t nr;
 
        ret = sd_id128_get_boot(&boot_id);
        if (ret < 0)
                return ret;
 
-       bson_init_as_query(&query);
-
-       /* FIXME: query by boot id
-        * bson_append_binary(&query, EV_BOOT_ID, BSON_BIN_UUID, (char *)&boot_id, sizeof(boot_id)); */
-       bson_append_string(&query, EV_TYPE_NAME, ev->type->name);
-       bson_append_start_object(&query, SYSD_SERVICE);
-       bson_append_string(&query, SYSD_SERVICE_DBUS_PATH, sf_ev->service.dbus_path);
-       bson_append_finish_object(&query);
-       bson_finish(&query);
-
-       bson_init(&b);
-       ret = database_load(&query, &b, NULL);
+       ret = faultd_object_new(&query);
        if (ret < 0)
                return ret;
 
-       nr = 0;
-       BSON_ITERATOR_INIT(&it, &b);
-       while ((bt = bson_iterator_next(&it)) != BSON_EOO) {
-               if (bt == BSON_OBJECT) {
-                       bson it_b;
-                       struct faultd_event *it_ev;
-
-                       bson_init_finished_data(&it_b, bson_iterator_value(&it));
-                       ret = faultd_event_create(BSON_EVENT_ID, &it_b, &it_ev);
-                       if (ret < 0) {
-                               log_error_errno(ret, "Could not create event from bson");
-                               return ret;
-                       }
-
-                       log_debug(" BootId: " SD_ID128_FORMAT_STR
-                               " BootId: " SD_ID128_FORMAT_STR,
-                               SD_ID128_FORMAT_VAL(it_ev->boot_id),
-                               SD_ID128_FORMAT_VAL(boot_id));
-
-                       if (sd_id128_equal(it_ev->boot_id, boot_id))
-                               ++nr;
-
-                       faultd_event_unref(it_ev);
-               }
+       faultd_object_append_uuid(query, EV_BOOT_ID, &boot_id);
+       faultd_object_append_string(query, EV_TYPE_NAME, ev->type->name);
+
+       ret = faultd_object_new(&service_obj);
+       if (ret < 0) {
+               faultd_object_unref(query);
+               return ret;
        }
 
+       faultd_object_append_string(service_obj, SYSD_SERVICE_DBUS_PATH, sf_ev->service.dbus_path);
+       faultd_object_append_object(query, SYSD_SERVICE, service_obj);
+       faultd_object_unref(service_obj);
+
+       ret = database_load(query, NULL, &nr);
+       faultd_object_unref(query);
+       if (ret < 0)
+               return ret;
+
        log_debug("service failed %d times during current boot", nr);
 
-       if (nr < 3)
+       ret = faultd_object_new(&ev_data.action_data);
+       if (ret < 0)
+               return ret;
+
+       if (nr < 3) {
                ev_data.action = FAULTD_ACTION_SERVICE_RECOVER_ID;
-       else
+               ret = faultd_fill_for_reboot(ev_data.action_data);
+       } else {
                ev_data.action = FAULTD_ACTION_REBOOT_ID;
-
-       ret = bson_fill_for_srv_recover(&ev_data.action_data,
+               ret = faultd_fill_for_srv_recover(ev_data.action_data,
                                                                  sf_ev->service.dbus_path,
                                                                  sf_ev->service.recovery_unit);
+       }
+
        if (ret) {
                log_error("Unable to create action data");
-               goto unref_old_event;
+               goto del_action_data;
        }
 
        ret = faultd_event_create(DECISION_MADE_EVENT_ID, &ev_data, &new_ev);
@@ -130,6 +115,7 @@ static int sf_make_decision(struct faultd_event_handler *handler)
                goto unref_new_event;
        }
 
+       faultd_object_unref(ev_data.action_data);
        return 0;
 
 unref_new_event:
@@ -137,8 +123,7 @@ unref_new_event:
        return 0;
 
 del_action_data:
-       bson_destroy(&ev_data.action_data);
-unref_old_event:
+       faultd_object_unref(ev_data.action_data);
        faultd_event_unref(ev);
        return 0;
 }
index 5e8ea0d79770c07aa26b8c25cd36718dac6c5e7a..3d11f5729cb296ab598786ba494067b54b69720c 100644 (file)
@@ -49,9 +49,13 @@ static int vf_make_decision(struct faultd_event_handler *handler)
        };
        int ret;
 
-       ret = bson_fill_for_reboot(&ev_data.action_data);
+       ret = faultd_object_new(&ev_data.action_data);
+       if (ret < 0)
+               return ret;
+
+       ret = faultd_fill_for_reboot(ev_data.action_data);
        if (ret)
-               goto unref_old_event;
+               goto del_action_data;
 
        ret = faultd_event_create(DECISION_MADE_EVENT_ID, &ev_data, &new_ev);
        if (ret) {
@@ -67,6 +71,7 @@ static int vf_make_decision(struct faultd_event_handler *handler)
                goto unref_new_event;
        }
 
+       faultd_object_unref(ev_data.action_data);
        return 0;
 
 unref_new_event:
@@ -74,8 +79,7 @@ unref_new_event:
        return 0;
 
 del_action_data:
-       bson_destroy(&ev_data.action_data);
-unref_old_event:
+       faultd_object_unref(ev_data.action_data);
        faultd_event_unref(ev);
        return 0;
 
index 9d60c16c7a3c75ca05558357470e41900297ec67..7a958124fbb618c94d6879d3a35513848392f9ea 100644 (file)
@@ -16,7 +16,6 @@
  * limitations under the License.
  */
 
-#include <ejdb/bson.h>
 #include <stdio.h>
 #include <errno.h>
 #include <malloc.h>
@@ -47,7 +46,7 @@ static int allocate_ae_event(struct faultd_event_type *type,
                goto cleanup_strs;
        }
 
-       ae_ev->action_log = ae_ev_data->action_log;
+       ae_ev->action_log = faultd_object_ref(ae_ev_data->action_log);
        ae_ev->result  = ae_ev_data->result;
 
        ae_ev->reason = ae_ev_data->reason;
@@ -76,7 +75,7 @@ static void ae_event_release(struct faultd_event *ev)
                faultd_event_unref(ae_ev->reason);
        free(ae_ev->action);
        free(ae_ev->action_impl);
-       bson_destroy(&ae_ev->action_log);
+       faultd_object_unref(ae_ev->action_log);
        faultd_event_cleanup_internal(&ae_ev->event);
        free(ae_ev);
 }
@@ -86,40 +85,35 @@ static char *ae_event_to_string(struct faultd_event *ev)
        struct action_executed_event *ae_ev =
                to_action_executed_event(ev);
        char *str;
-       char *buf;
-       int len;
        int ret;
 
-       bson2json(ae_ev->action_log.data, &buf, &len);
+       /* TODO: add serialization? 
+       bson2json(ae_ev->action_log.data, &buf, &len); */
 
        ret = asprintf(&str, "Action Executed Event:"
                                   " Action: %s"
                                   " Impl: %s"
-                                  " Log:  %s"
                                   " Result: %d",
                                   ae_ev->action,
                                   ae_ev->action_impl,
-                                  buf,
                                   ae_ev->result);
 
-       free(buf);
-
        return ret > 0 ? str : NULL;
 }
 
-static void ae_event_serialize(struct faultd_event *ev, bson *out)
+static void ae_event_serialize(struct faultd_event *ev, struct faultd_object *out)
 {
        struct action_executed_event *ae_ev =
                to_action_executed_event(ev);
 
-       assert(!BSON_OID_IS_ZERO(ae_ev->reason->oid));
+       assert(!FAULTD_OID_IS_ZERO(ae_ev->reason->oid));
 
        faultd_event_serialize_internal(ev, out);
-       bson_append_oid(out, AE_EV_REASON, &ae_ev->reason->oid);
-       bson_append_string(out, AE_EV_ACTION, ae_ev->action);
-       bson_append_string(out, AE_EV_ACTION_IMPL, ae_ev->action_impl);
-       bson_append_bson(out, AE_EV_ACTION_LOG, &ae_ev->action_log);
-       bson_append_int(out, AE_EV_RESULT, ae_ev->result);
+       faultd_object_append_oid(out, AE_EV_REASON, &ae_ev->reason->oid);
+       faultd_object_append_string(out, AE_EV_ACTION, ae_ev->action);
+       faultd_object_append_string(out, AE_EV_ACTION_IMPL, ae_ev->action_impl);
+       faultd_object_append_object(out, AE_EV_ACTION_LOG, ae_ev->action_log);
+       faultd_object_append_int(out, AE_EV_RESULT, ae_ev->result);
 }
 
 static struct faultd_event_type action_executed_event_type = {
index 6dd38a698e5ea02a852464d623d9b909f1083e77..b82b8bf43e217c32d39771ba1012f41e53af3fda 100644 (file)
@@ -37,7 +37,7 @@ struct action_executed_event {
        struct faultd_event *reason;
        char *action;
        char *action_impl;
-       bson action_log;
+       struct faultd_object *action_log;
        int result;
 };
 
@@ -48,7 +48,7 @@ struct ae_event_data {
        char *action;
        char *action_impl;
        /* Action log is shallow copied */
-       bson action_log;
+       struct faultd_object *action_log;
        int result;
 };
 
index ebc4c6ae70420968ede75736dfe2eab221816ef1..083f2f33a378e91cd6206f28295d7b23a26800d9 100644 (file)
@@ -16,7 +16,6 @@
  * limitations under the License.
  */
 
-#include <ejdb/bson.h>
 #include <stdio.h>
 #include <errno.h>
 #include <malloc.h>
@@ -47,7 +46,7 @@ static int allocate_dm_event(struct faultd_event_type *type,
                goto cleanup_strs;
        }
 
-       dm_ev->action_data = dm_ev_data->action_data;
+       dm_ev->action_data = faultd_object_ref(dm_ev_data->action_data);
 
        dm_ev->reason = dm_ev_data->reason;
        faultd_event_ref(dm_ev->reason);
@@ -72,7 +71,7 @@ static void dm_event_release(struct faultd_event *ev)
 
        free(dm_ev->who_made);
        free(dm_ev->action);
-       bson_destroy(&dm_ev->action_data);
+       faultd_object_unref(dm_ev->action_data);
        faultd_event_unref(dm_ev->reason);
        faultd_event_cleanup_internal(&dm_ev->event);
        free(dm_ev);
@@ -95,18 +94,17 @@ static char *dm_event_to_string(struct faultd_event *ev)
        return ret > 0 ? str : NULL;
 }
 
-static void dm_event_serialize(struct faultd_event *ev, bson *out)
+static void dm_event_serialize(struct faultd_event *ev, struct faultd_object *out)
 {
        struct decision_made_event *dm_ev =
                to_decision_made_event(ev);
 
-       assert(!BSON_OID_IS_ZERO(dm_ev->reason->oid));
+       assert(!FAULTD_OID_IS_ZERO(dm_ev->reason->oid));
 
        faultd_event_serialize_internal(ev, out);
-       bson_append_string(out, DM_EV_WHO, dm_ev->who_made);
-       bson_append_string(out, DM_EV_ACTION, dm_ev->action);
-       bson_append_oid(out, DM_EV_REASON, &dm_ev->reason->oid);
-       bson_append_bson(out, DM_EV_ACTION_DATA, &dm_ev->action_data);
+       faultd_object_append_string(out, DM_EV_WHO, dm_ev->who_made);
+       faultd_object_append_string(out, DM_EV_ACTION, dm_ev->action);
+       faultd_object_append_object(out, DM_EV_ACTION_DATA, dm_ev->action_data);
 }
 
 static struct faultd_event_type decision_made_event_type = {
index 36e4824e7b3449663b356d6923fa597287505756..85e0af2fbefbc98c582e5c523500cfa23a514400 100644 (file)
@@ -20,7 +20,6 @@
 #define FAULTD_DECISION_MADE_EVENT_H
 
 #include <time.h>
-#include <ejdb/bson.h>
 
 #include "action.h"
 #include "event.h"
@@ -37,7 +36,7 @@ struct decision_made_event {
        struct faultd_event *reason;
        char *who_made;
        char *action;
-       bson action_data;
+       struct faultd_object *action_data;
        /* TODO: what more do we need? */
 };
 
@@ -48,7 +47,7 @@ struct dm_event_data {
        char *who_made;
        char *action;
 
-       bson action_data;
+       struct faultd_object *action_data;
 };
 
 #define to_decision_made_event(EVENT)                                          \
index 3a56271cc0e3c13f82316a577668ca2273f8711e..9ed21b1837b3d45d365385142b87814ca830fe0c 100644 (file)
@@ -1,4 +1,3 @@
-#include <ejdb/bson.h>
 #include <errno.h>
 #include <systemd/sd-id128.h>
 
@@ -51,7 +50,7 @@ static char *fs_event_to_string(struct faultd_event *ev)
        return ret > 0 ? str : NULL;
 }
 
-static void fs_event_serialize(struct faultd_event *ev, bson *out)
+static void fs_event_serialize(struct faultd_event *ev, struct faultd_object *out)
 {
        faultd_event_serialize_internal(ev, out);
 }
index feec5823a996867522a14f03827b88588ce84fb3..138d7b8006f530820dc9ade4e8720255248b95d3 100644 (file)
@@ -16,7 +16,6 @@
  * limitations under the License.
  */
 
-#include <ejdb/bson.h>
 #include <stdio.h>
 #include <errno.h>
 #include <malloc.h>
@@ -51,41 +50,36 @@ free_rv_ev:
 }
 
 static int deserialize_rv_event(struct faultd_event_type *type,
-                                                               bson *data, struct faultd_event **ev)
+                                                               struct faultd_object *data, struct faultd_event **ev)
 {
        int ret = -EINVAL;
        struct rv_event_data rv_ev_data;
-       bson_type bt;
-       bson_iterator it;
+       struct faultd_object *obj;
 
        memset(&rv_ev_data, 0, sizeof(rv_ev_data));
-       BSON_ITERATOR_INIT(&it, data);
 
-       while ((bt = bson_iterator_next(&it)) != BSON_EOO) {
+       list_for_each_entry(obj, &data->val.children, node) {
+               if ((obj->type == TYPE_OBJECT) &&
+                       (strcmp(SYSD_SERVICE, obj->key) == 0)) {
 
-               if ((bt == BSON_OBJECT) &&
-                       (strcmp(SYSD_SERVICE, bson_iterator_key(&it)) == 0)) {
-                       bson svc;
-
-                       bson_init_finished_data(&svc, bson_iterator_value(&it));
-                       ret = systemd_service_deserialize(&svc, &rv_ev_data.service);
+                       ret = systemd_service_deserialize(obj, &rv_ev_data.service);
                        if (ret < 0)
                                goto finish;
 
-               } else if ((bt == BSON_TIMESTAMP) &&
-                                  (strcmp(RV_EV_DTIME, bson_iterator_key(&it)))) {
+               } else if ((obj->type == TYPE_TIMESPEC) &&
+                                  (strcmp(RV_EV_DTIME, obj->key))) {
 
-                       rv_ev_data.detection_time = bson_iterator_time_t(&it);
+                       rv_ev_data.detection_time = obj->val.time;
 
-               } else if ((bt == BSON_INT) &&
-                                  (strcmp(RV_EV_RES_TYPE, bson_iterator_key(&it)))) {
+               } else if ((obj->type == TYPE_INT) &&
+                                  (strcmp(RV_EV_RES_TYPE, obj->key))) {
 
-                       rv_ev_data.resource_type = bson_iterator_int(&it);
+                       rv_ev_data.resource_type = obj->val.i;
 
-               } else if ((bt == BSON_INT) &&
-                                  (strcmp(RV_EV_PID, bson_iterator_key(&it)))) {
+               } else if ((obj->type == TYPE_INT) &&
+                                  (strcmp(RV_EV_PID, obj->key))) {
 
-                       rv_ev_data.pid = bson_iterator_int(&it);
+                       rv_ev_data.pid = obj->val.i;
 
                }
        }
@@ -142,15 +136,14 @@ static char *rv_event_to_string(struct faultd_event *ev)
        return ret > 0 ? str : NULL;
 }
 
-static void rv_event_serialize(struct faultd_event *ev, bson *out)
+static void rv_event_serialize(struct faultd_event *ev, struct faultd_object *out)
 {
        struct resource_violation_event *rv_ev =
                to_resource_violation_event(ev);
        faultd_event_serialize_internal(ev, out);
        systemd_service_serialize(&rv_ev->service, out);
-       bson_append_time_t(out, RV_EV_DTIME, rv_ev->detection_time);
-       bson_append_int(out, RV_EV_PID, rv_ev->pid);
-       bson_append_int(out, RV_EV_RES_TYPE, rv_ev->resource_type);
+       faultd_object_append_time_t(out, RV_EV_DTIME, rv_ev->detection_time);
+       faultd_object_append_int(out, RV_EV_PID, rv_ev->pid);
 }
 
 static struct faultd_event_type resource_violation_event_type = {
index 929337923b349ca377ae20a8fccd5a85815256b3..f0c73b032d29cd8b96f2e55a24f451301e4f362e 100644 (file)
@@ -16,7 +16,6 @@
  * limitations under the License.
  */
 
-#include <ejdb/bson.h>
 #include <stdio.h>
 #include <errno.h>
 #include <malloc.h>
@@ -52,28 +51,24 @@ free_sf_ev:
 }
 
 static int deserialize_sf_event(struct faultd_event_type *type,
-                                                               bson *data, struct faultd_event **ev)
+                                                               struct faultd_object *data, struct faultd_event **ev)
 {
        int ret = -EINVAL;
        struct sf_event_data sf_ev_data;
-       bson_type bt;
-       bson_iterator it;
+       struct faultd_object *obj;
 
        memset(&sf_ev_data, 0, sizeof(sf_ev_data));
-       BSON_ITERATOR_INIT(&it, data);
 
-       while ((bt = bson_iterator_next(&it)) != BSON_EOO) {
-               if ((bt == BSON_OBJECT) &&
-                       (strcmp(SYSD_SERVICE, bson_iterator_key(&it)) == 0)) {
-                       bson svc;
+       list_for_each_entry(obj, &data->val.children, node) {
+               if ((obj->type == TYPE_OBJECT) &&
+                       (strcmp(SYSD_SERVICE, obj->key) == 0)) {
 
-                       bson_init_finished_data(&svc, bson_iterator_value(&it));
-                       ret = systemd_service_deserialize(&svc, &sf_ev_data.service);
+                       ret = systemd_service_deserialize(obj, &sf_ev_data.service);
                        if (ret < 0)
                                goto finish;
-               } else if ((bt == BSON_TIMESTAMP) &&
-                                  (strcmp(SF_EV_DTIME, bson_iterator_key(&it)))) {
-                       sf_ev_data.detection_time = bson_iterator_time_t(&it);
+               } else if ((obj->type == TYPE_TIME_T) &&
+                                  (strcmp(SF_EV_DTIME, obj->key) == 0)) {
+                       sf_ev_data.detection_time = obj->val.time;
                }
        }
 
@@ -126,13 +121,13 @@ static char *sf_event_to_string(struct faultd_event *ev)
        return ret > 0 ? str : NULL;
 }
 
-static void sf_event_serialize(struct faultd_event *ev, bson *out)
+static void sf_event_serialize(struct faultd_event *ev, struct faultd_object *out)
 {
        struct service_failed_event *sf_ev =
                to_service_failed_event(ev);
        faultd_event_serialize_internal(ev, out);
        systemd_service_serialize(&sf_ev->service, out);
-       bson_append_time_t(out, SF_EV_DTIME, sf_ev->detection_time);
+       faultd_object_append_time_t(out, SF_EV_DTIME, sf_ev->detection_time);
 }
 
 static struct faultd_event_type service_failed_event_type = {
index 637c2be35304e4d5534d26f2755e1957bd6c3128..901b815a173f66a1b0f28eb068671e1995840e00 100644 (file)
@@ -16,7 +16,6 @@
  * limitations under the License.
  */
 
-#include <ejdb/bson.h>
 #include <stdio.h>
 #include <errno.h>
 #include <malloc.h>
@@ -77,14 +76,13 @@ static char *sb_event_to_string(struct faultd_event *ev)
        return ret > 0 ? str : NULL;
 }
 
-static void sb_event_serialize(struct faultd_event *ev, bson *out)
+static void sb_event_serialize(struct faultd_event *ev, struct faultd_object *out)
 {
        struct system_booted_event *sb_ev=
                to_system_booted_event(ev);
 
        faultd_event_serialize_internal(ev, out);
-       bson_append_binary(out, "prev_boot_id", BSON_BIN_UUID,
-                       (char *)&sb_ev->prev_boot_id, sizeof(sb_ev->prev_boot_id));
+       faultd_object_append_uuid(out, "prev_boot_id", &sb_ev->prev_boot_id);
 }
 
 static struct faultd_event_type system_booted_event_type = {
index d0e457abbcb8e26f6bf5f3a0c890f0a18ca33066..2920b72ea714c6c71b276c05a587c123283470da 100644 (file)
@@ -24,7 +24,7 @@
 #include "system_booted_event.h"
 #include "database.h"
 
-#define LAST_BOOT_ID_OID "32a4ca4f96ddfcd8aa1f5026"
+static const char LAST_BOOT_ID_OID[33] = "32a4ca4f96ddfcd8a1f50269014ff320";
 
 struct startup_listener {
        struct faultd_module module;
@@ -32,44 +32,52 @@ struct startup_listener {
 
 static int retrieve_last_boot_id(sd_id128_t *boot_id)
 {
-       bson *b;
-       bson_oid_t oid;
-       bson_iterator i;
-       bson_type t;
-
-       bson_oid_from_string(&oid, LAST_BOOT_ID_OID);
-       b = database_get_by_oid(&oid);
-       if (!b) {
-               *boot_id = SD_ID128_NULL;
-               return 0;
+       faultd_oid_t oid;
+       int ret;
+       struct faultd_object *root;
+
+       ret = faultd_oid_from_string(&oid, LAST_BOOT_ID_OID);
+       if (ret < 0) {
+               log_error("Could not build oid from string %s", LAST_BOOT_ID_OID);
+               return ret;
        }
 
-       bson_iterator_init(&i, b);
-       t = bson_find(&i, b, "boot_id");
-       if (t == BSON_EOO) {
-               log_error("Could not find boot_id key");
-               return -1;
+       faultd_object_new(&root);
+       ret = database_get_by_oid(&oid, root);
+       if (ret < 0) {
+               log_debug("No boot event found");
+               *boot_id = SD_ID128_NULL;
+               ret = 0;
+               goto out;
        }
 
-       *boot_id = *(sd_id128_t *)bson_iterator_bin_data(&i);
+       ret = faultd_object_get_uuid(root, "boot_id", boot_id);
+       if (ret < 0) {
+               log_error("could not get boot_id object");
+               goto out;
+       }
 
-       return 0;
+out:
+       faultd_object_unref(root);
+       return ret;
 }
 
 static void store_boot_id(sd_id128_t *boot_id)
 {
-       bson b;
-       bson_oid_t oid;
+       faultd_oid_t oid;
+       struct faultd_object *obj;
+       int ret;
 
-       bson_oid_from_string(&oid, LAST_BOOT_ID_OID);
+       faultd_oid_from_string(&oid, LAST_BOOT_ID_OID);
 
-       bson_init(&b);
-       bson_append_oid(&b, JDBIDKEYNAME, &oid);
-       bson_append_binary(&b, "boot_id", BSON_BIN_UUID, (char *)boot_id, sizeof(*boot_id));
-       bson_finish(&b);
+       faultd_object_new(&obj);
+       faultd_object_append_oid(obj, JDBIDKEYNAME, &oid);
+       faultd_object_append_uuid(obj, "boot_id", boot_id);
 
-       database_store(&b, &oid);
-       bson_destroy(&b);
+       ret = database_store(obj, &oid);
+       if (ret < 0)
+               log_error_errno(ret, "Could not store object in database");
+       faultd_object_unref(obj);
 }
 
 static int startup_listener_init(struct faultd_module *module,