Add entity to represent service failure
authorKrzysztof Opasiak <k.opasiak@samsung.com>
Tue, 9 May 2017 09:44:02 +0000 (11:44 +0200)
committerKrzysztof Opasiak <k.opasiak@samsung.com>
Wed, 10 May 2017 18:49:58 +0000 (20:49 +0200)
This type of event should be generated by a listener
when it detects that some service entered a failed state.

Signed-off-by: Krzysztof Opasiak <k.opasiak@samsung.com>
Makefile.am
src/event_types/service_failed_event.c [new file with mode: 0644]
src/event_types/service_failed_event.h [new file with mode: 0644]

index 6f953d8fadffa9abf1d45e03accf53e396711172..22c8b692d0aa1f277c59d08400b68e9535f722a8 100644 (file)
@@ -41,6 +41,7 @@ faultd_SOURCES = \
     src/decision_makers/rv_dm.c \
     src/event_types/decision_made_event.c \
     src/event_types/resource_violation_event.c \
+    src/event_types/service_failed_event.c \
     src/faultd.c \
     src/listeners/audit.c \
     src/util/log.c \
diff --git a/src/event_types/service_failed_event.c b/src/event_types/service_failed_event.c
new file mode 100644 (file)
index 0000000..189d7aa
--- /dev/null
@@ -0,0 +1,92 @@
+/*
+ * 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 <stdio.h>
+#include <errno.h>
+#include <malloc.h>
+
+#include "service_failed_event.h"
+
+static int allocate_sf_event(struct faultd_event_type *type,
+                             void *data, struct faultd_event **ev)
+{
+       struct service_failed_event *sf_ev;
+       struct sf_event_data *sf_ev_data = data;
+       int ret;
+
+       sf_ev = malloc(sizeof(*sf_ev));
+       if (!sf_ev)
+               return -ENOMEM;
+
+       ret = faultd_event_init(type, &sf_ev->event);
+       if (ret)
+               goto free_rv_ev;
+
+       /* TODO */
+       sf_ev->service = sf_ev_data->service;
+       sf_ev->detection_time = sf_ev_data->detection_time;
+       /* /TODO */
+
+       *ev = &sf_ev->event;
+       return 0;
+free_rv_ev:
+       free(sf_ev);
+
+       return ret;
+}
+
+static void sf_event_release(struct faultd_event *ev)
+{
+       struct service_failed_event *sf_ev =
+               to_service_failed_event(ev);
+
+       systemd_service_cleanup(&sf_ev->service);
+       faultd_event_cleanup(&sf_ev->event);
+       free(sf_ev);
+}
+
+static char *sf_event_to_string(struct faultd_event *ev)
+{
+       struct service_failed_event *sf_ev =
+               to_service_failed_event(ev);
+       char *str;
+       int ret;
+
+       /* TODO print other fields */
+       ret = asprintf(&str, "Service Failed Event:"
+                                  " Time: %lld.%.9ld"
+                                  " Service: name %s",
+                                  (long long)sf_ev->event.timestamp.tv_sec,
+                                  sf_ev->event.timestamp.tv_nsec,
+                                  sf_ev->service.name);
+
+       return ret > 0 ? str : NULL;
+}
+
+static struct faultd_event_type service_failed_event_type = {
+       .name = SERVICE_FAILED_EVENT_ID,
+       .default_ops = {
+               .to_string = sf_event_to_string,
+               .release = sf_event_release,
+       },
+       .allocate_event = allocate_sf_event,
+       .node = LIST_HEAD_INIT(service_failed_event_type.node),
+};
+
+FAULTD_EVENT_TYPE_REGISTER(service_failed_event_type, service_failed_et)
diff --git a/src/event_types/service_failed_event.h b/src/event_types/service_failed_event.h
new file mode 100644 (file)
index 0000000..6a53a44
--- /dev/null
@@ -0,0 +1,44 @@
+/*
+ * 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.
+ */
+
+#ifndef _FAULTD_SERVICE_FAILED_EVENT_H
+#define _FAULTD_SERVICE_FAILED_EVENT_H
+
+#include <time.h>
+
+#include "event.h"
+#include "common.h"
+#include "service.h"
+
+#define SERVICE_FAILED_EVENT_ID "service_failed"
+
+struct service_failed_event {
+       struct faultd_event event;
+       struct systemd_service service;
+       time_t detection_time;
+};
+
+struct sf_event_data {
+       struct systemd_service service;
+       time_t detection_time;
+};
+
+#define to_service_failed_event(EVENT)                                         \
+       container_of(EVENT, struct service_failed_event, event)
+
+#endif /* _FAULTD_SERVICE_FAILED_EVENT_H */