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 \
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;
}
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;
}
#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;
}
#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 */
#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);
}
#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_ */
* limitations under the License.
*/
-#include <ejdb/bson.h>
#include <errno.h>
#include <systemd/sd-id128.h>
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;
}
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;
}
#ifndef FAULTD_EVENT_H
#define FAULTD_EVENT_H
-#include <ejdb/bson.h>
#include <string.h>
#include <systemd/sd-id128.h>
#include <time.h>
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 {
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;
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);
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);
}
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 */
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);
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;
if (dbus_path)
ret = 0;
-finish:
+
return ret;
cleanup:
#ifndef FAULTD_SERVICE_H
#define FAULTD_SERVICE_H
-#include <ejdb/bson.h>
#include <sys/types.h>
#include <unistd.h>
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)
--- /dev/null
+/*
+ * 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);
--- /dev/null
+/*
+ * 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);
};
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);
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;
}
.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);
goto unref_new_event;
}
+ faultd_object_unref(ev_data.action_data);
return 0;
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;
}
};
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) {
goto unref_new_event;
}
+ faultd_object_unref(ev_data.action_data);
return 0;
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;
* limitations under the License.
*/
-#include <ejdb/bson.h>
#include <stdio.h>
#include <errno.h>
#include <malloc.h>
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;
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);
}
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 = {
struct faultd_event *reason;
char *action;
char *action_impl;
- bson action_log;
+ struct faultd_object *action_log;
int result;
};
char *action;
char *action_impl;
/* Action log is shallow copied */
- bson action_log;
+ struct faultd_object *action_log;
int result;
};
* limitations under the License.
*/
-#include <ejdb/bson.h>
#include <stdio.h>
#include <errno.h>
#include <malloc.h>
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);
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);
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 = {
#define FAULTD_DECISION_MADE_EVENT_H
#include <time.h>
-#include <ejdb/bson.h>
#include "action.h"
#include "event.h"
struct faultd_event *reason;
char *who_made;
char *action;
- bson action_data;
+ struct faultd_object *action_data;
/* TODO: what more do we need? */
};
char *who_made;
char *action;
- bson action_data;
+ struct faultd_object *action_data;
};
#define to_decision_made_event(EVENT) \
-#include <ejdb/bson.h>
#include <errno.h>
#include <systemd/sd-id128.h>
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);
}
* limitations under the License.
*/
-#include <ejdb/bson.h>
#include <stdio.h>
#include <errno.h>
#include <malloc.h>
}
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;
}
}
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 = {
* limitations under the License.
*/
-#include <ejdb/bson.h>
#include <stdio.h>
#include <errno.h>
#include <malloc.h>
}
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;
}
}
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 = {
* limitations under the License.
*/
-#include <ejdb/bson.h>
#include <stdio.h>
#include <errno.h>
#include <malloc.h>
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 = {
#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;
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,