Add entity to represent resource violation event
authorKrzysztof Opasiak <k.opasiak@samsung.com>
Tue, 9 May 2017 11:31:47 +0000 (13:31 +0200)
committerKrzysztof Opasiak <k.opasiak@samsung.com>
Wed, 10 May 2017 18:49:57 +0000 (20:49 +0200)
Resource violation means that some process is using
more resources than it should.

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

index e7e04c42652c429b01b78f6826a7295000a71599..11261eb68ddb2b60661974f9944d24744693ac72 100644 (file)
@@ -8,6 +8,7 @@ AM_CPPFLAGS = \
        -DSYSCONFDIR=\""$(sysconfdir)"\" \
        -I${top_srcdir}/src \
         -I${top_srcdir}/src/core \
+        -I${top_srcdir}/src/event_types \
         -I${top_srcdir}/src/util
 
 
@@ -35,6 +36,7 @@ faultd_SOURCES = \
     src/core/event_processor.c \
     src/core/service.c \
     src/core/module.c \
+    src/event_types/resource_violation_event.c \
     src/faultd.c \
     src/util/log.c \
     src/util/notify_queue.c
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..0b3c86c
--- /dev/null
@@ -0,0 +1,94 @@
+/*
+ * 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 "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_time;
+       rv_ev->resource_type = rv_ev_data->resource_type;
+
+       *ev = &rv_ev->event;
+       return 0;
+free_rv_ev:
+       free(rv_ev);
+
+       return ret;
+}
+
+static void rv_event_release(struct faultd_event *ev)
+{
+       struct resource_violation_event *rv_ev =
+               to_resource_violation_event(ev);
+
+       systemd_service_cleanup(&rv_ev->service);
+       faultd_event_cleanup(&rv_ev->event);
+       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;
+       int ret;
+
+       /* 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->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, resource_violation_et)
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..472c69a
--- /dev/null
@@ -0,0 +1,54 @@
+/*
+ * 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"
+#include "service.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 systemd_service service;
+       time_t detection_time;
+       enum resource_type resource_type;
+       /* TODO: what more do we need? */
+       pid_t pid;
+};
+
+struct rv_event_data {
+       struct systemd_service service;
+       time_t detection_time;
+       enum resource_type resource_type;
+       pid_t pid;
+};
+
+#define to_resource_violation_event(EVENT)                                             \
+       container_of(EVENT, struct resource_violation_event, event)
+
+#endif /* FAULTD_RESOURCE_VIOLATION_EVENT_H */