Add resource violation event
authorKrzysztof Opasiak <k.opasiak@samsung.com>
Tue, 25 Apr 2017 16:02:46 +0000 (18:02 +0200)
committerKrzysztof Opasiak <k.opasiak@samsung.com>
Tue, 25 Apr 2017 16:02:46 +0000 (18:02 +0200)
Signed-off-by: Krzysztof Opasiak <k.opasiak@samsung.com>
src/event_types/resource_violation_event.c [new file with mode: 0644]
src/event_types/resource_violation_event.h [new file with mode: 0644]

diff --git a/src/event_types/resource_violation_event.c b/src/event_types/resource_violation_event.c
new file mode 100644 (file)
index 0000000..fca58c7
--- /dev/null
@@ -0,0 +1,92 @@
+/*
+ * 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
+#include <stdio.h>
+#include <errno.h>
+
+#include "resource_violation_event.h"
+
+static int allocate_rv_event(struct faultd_event_type *type,
+                             void *data, struct faultd_event **ev)
+{
+        struct resource_violation_event *rv_ev;
+        struct rv_event_data rv_ev_data = data;
+        int ret;
+
+        rv_ev = malloc(sizeof(*rv_ev));
+        if (!rv_ev)
+                return -ENOMEM;
+
+        ret = faultd_event_init(type, &rv_ev->event);
+        if (ret)
+                goto free_rv_ev;
+
+        rv_ev->service = rv_ev_data->service;
+        rv_ev->detection_time = rv_ev_data->detection_type;
+        rv_ev->resource_type = rv_ev_data->resource_type;
+
+        return 0;
+free_rv_ev:
+        free(rv_ev);
+
+        return ret;
+}
+
+void rv_event_release(struct faultd_event *ev)
+{
+        struct resource_violation_event *rv_ev =
+                to_resource_violation_event(ev);
+
+        system_service_cleanup(rv_ev->service);
+        faultd_event_cleanup(rv_ev->ev);
+        free(rv_ev);
+}
+
+static char *rv_event_to_string(struct faultd_event *ev)
+{
+        struct resource_violation_event *rv_ev =
+                to_resource_violation_event(ev);
+        char *str;
+
+        /* TODO print other fields */
+        ret = asprintf(&str, "Resource Violation Event:"
+                       " Time: %lld.%.9ld"
+                       " Service: pid: %d, name %s"
+                       " Resource type: %d",
+                       (long long)rv_ev->event->timestamp.tv_sec,
+                       rv_ev->event->timestamp.tv_nsec,
+                       rv_ev->service.pid,
+                       rv_ev->service.name,
+                       rv_ev->resource_type);
+
+        return ret > 0 ? str : NULL;
+}
+        
+
+static struct faultd_event_type resource_violation_event_type = {
+        .name = RESOURCE_VIOLATION_EVENT_ID,
+        .default_ops = {
+                .to_string = rv_event_to_string,
+                .release = rv_event_release,
+        },
+        .allocate_event = allocate_rv_event,
+        .node = LIST_HEAD_INIT(resource_violation_event_type.node),
+};
+
+FAULTD_EVENT_TYPE_REGISTER(resource_violation_event_type)
diff --git a/src/event_types/resource_violation_event.h b/src/event_types/resource_violation_event.h
new file mode 100644 (file)
index 0000000..9a3c7ce
--- /dev/null
@@ -0,0 +1,51 @@
+/*
+ * 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_RESOURCE_VIOLATION_EVENT_H
+#define FAULTD_RESOURCE_VIOLATION_EVENT_H
+
+#include <time.h>
+
+#include "event.h"
+#include "common.h"
+
+#define RESOURCE_VIOLATION_EVENT_ID "resource_violation"
+
+enum resource_type {
+        FAULTD_RESOURCE_FD,
+        FAULTD_RESOURCE_MEM
+};
+
+struct resource_violation_event {
+        struct faultd_event event;
+        struct system_service service;
+        struct time_t detection_time;
+        enum resource_type resource_type;
+        /* TODO: what more do we need? */
+};
+
+struct rv_event_data {
+        struct system_service service;
+        struct time_t detection_time;
+        enum resource_type resource_type;
+};
+        
+#define to_resource_violation_event(EVENT)      \
+        container_of(EVENT, struct resource_violation_event, event)
+
+#endif /* FAULTD_RESOURCE_VIOLATION_EVENT_H */