Provide serialising function for struct timespec 58/130258/2
authorŁukasz Stelmach <l.stelmach@samsung.com>
Mon, 15 May 2017 08:44:58 +0000 (10:44 +0200)
committerŁukasz Stelmach <l.stelmach@samsung.com>
Mon, 22 May 2017 10:46:33 +0000 (12:46 +0200)
Provide serialising function for struct timespec as well as macro for
generating such functions.

Change-Id: Ib27477b2be0ab9e6f38f5d1fede477426acb7e80
Signed-off-by: Łukasz Stelmach <l.stelmach@samsung.com>
Makefile.am
src/util/common.c [new file with mode: 0644]
src/util/common.h

index 2aee6d3caf230e6986038cd6776b2c1b8107a7f2..d332a39f0cc05ace05248898f30d9ba166357b3b 100644 (file)
@@ -43,8 +43,8 @@ faultd_SOURCES = \
     src/action/system_reboot_to_recovery.c \
     src/core/event.c \
     src/core/event_processor.c \
-    src/core/service.c \
     src/core/module.c \
+    src/core/service.c \
     src/decision_makers/rv_dm.c \
     src/event_types/decision_made_event.c \
     src/event_types/resource_violation_event.c \
@@ -53,6 +53,7 @@ faultd_SOURCES = \
     src/listeners/audit.c \
     src/listeners/systemd.c \
     src/util/log.c \
+    src/util/common.c \
     src/util/notify_queue.c \
     src/util/systemd_dbus.c
 faultd_LDADD = $(LIBSYSTEMD_LIBS) $(AUDIT_LIBS) $(LIBEJDB_LIBS)
diff --git a/src/util/common.c b/src/util/common.c
new file mode 100644 (file)
index 0000000..096b0f9
--- /dev/null
@@ -0,0 +1,29 @@
+/*
+ * This file is a part of faultd.
+ *
+ * Copyright © 2017 Samsung Electronics
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#define _GNU_SOURCE 1
+
+#include <ejdb/bson.h>
+#include <time.h>
+
+#include "common.h"
+
+GENERATE_BSON_APPEND_STRUCT(timespec,
+                                                       bson_append_long(out, "sec", data->tv_sec);
+                                                       bson_append_long(out, "nsec", data->tv_nsec);
+                                                       )
index a8b7f058639783389c43b79852eb10a5a526b196..f0ba42cfe232afbde75cbf158bbd6e3a7ac6d6c1 100644 (file)
@@ -19,7 +19,9 @@
 #ifndef FAULTD_COMMON_H
 #define FAULTD_COMMON_H
 
+#include <ejdb/bson.h>
 #include <stdlib.h>
+#include <time.h>
 
 #define ARRAY_SIZE(a) (sizeof(a)/sizeof(*a))
 
@@ -42,4 +44,18 @@ static inline void freep(void *p)
 }
 #define _cleanup_free_ _cleanup_(freep)
 
+#define DECLARE_BSON_APPEND_STRUCT(type) \
+       void faultd_bson_append_##type(bson* out, const char *key, struct type *data)
+
+/* FIXME: bson_append_XXX can faile, check return codes */
+#define GENERATE_BSON_APPEND_STRUCT(type, code)        \
+       DECLARE_BSON_APPEND_STRUCT(type)                        \
+       {                                                                                       \
+               bson_append_start_object(out, key);             \
+               do { code } while(0);                                   \
+               bson_append_finish_object(out);                 \
+       }
+
+DECLARE_BSON_APPEND_STRUCT(timespec);
+
 #endif /* FAULTD_COMMON_H */