common : cgroup : add function opening eventfd 76/150576/6
authorKichan Kwon <k_c.kwon@samsung.com>
Mon, 18 Sep 2017 04:23:52 +0000 (13:23 +0900)
committerKichan Kwon <k_c.kwon@samsung.com>
Fri, 29 Sep 2017 07:34:58 +0000 (16:34 +0900)
Change-Id: I82cfb656ba482f5a3a74a36422cb336008b680a9
Signed-off-by: Kichan Kwon <k_c.kwon@samsung.com>
src/common/cgroup.c [new file with mode: 0644]
src/common/cgroup.h [new file with mode: 0644]

diff --git a/src/common/cgroup.c b/src/common/cgroup.c
new file mode 100644 (file)
index 0000000..4a558f4
--- /dev/null
@@ -0,0 +1,95 @@
+/*
+ * resourced-headless
+ *
+ * Copyright (c) 2017 Samsung Electronics Co., Ltd.
+ *
+ * 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 <errno.h>
+#include <fcntl.h>
+#include <sys/eventfd.h>
+#include <stdio.h>
+#include <unistd.h>
+
+#include "cgroup.h"
+#include "log.h"
+#include "macro.h"
+
+#define BUF_SIZE       1024
+
+API int cgroup_open_eventfd(const char *hierarchy, const char *notifier, const char *value)
+{
+       int ret;
+       int len;
+       int fd_event = -1;
+       int fd_notifier = -1;
+       int fd_event_interface = -1;
+       char buf[BUF_SIZE];
+
+       if (!hierarchy || !notifier || !value) {
+               _E("Invalid parameter");
+               return -EINVAL;
+       }
+
+       fd_event = eventfd(0, 0);
+       if (fd_event < 0) {
+               _E("Failed to create eventfd : %m");
+               return -errno;
+       }
+
+       ret = fcntl(fd_event, F_SETFL, O_NONBLOCK);
+       if (ret < 0) {
+               _E("Failed to manipulate eventfd : %m");
+               goto err;
+       }
+
+       len = snprintf(buf, BUF_SIZE, "%s/%s/%s", CGROUP_ROOT, hierarchy, notifier);
+       fd_notifier = open(buf, O_RDONLY);
+       if (fd_notifier < 0) {
+               _E("Failed to open %s : %m", buf);
+               goto err;
+       }
+
+       len = snprintf(buf, BUF_SIZE, "%s/%s/%s", CGROUP_ROOT, hierarchy, CGROUP_EVENT_INTERFACE);
+       fd_event_interface = open(buf, O_WRONLY);
+       if (fd_event_interface < 0) {
+               _E("Failed to open %s : %m", buf);
+               goto err;
+       }
+
+       len = snprintf(buf, BUF_SIZE, "%d %d %s", fd_event, fd_notifier, value);
+       ret = write(fd_event_interface, buf, len + 1);
+       if (ret < 0) {
+               _E("Failed to write in the event interface : %m");
+               goto err;
+       }
+
+       close(fd_event_interface);
+       close(fd_notifier);
+
+       return fd_event;
+
+err:
+
+       if (fd_event_interface >= 0)
+               close(fd_event_interface);
+
+       if (fd_notifier >= 0)
+               close(fd_notifier);
+
+       if (fd_event >= 0)
+               close(fd_event);
+
+       return -errno;
+}
diff --git a/src/common/cgroup.h b/src/common/cgroup.h
new file mode 100644 (file)
index 0000000..bbda13a
--- /dev/null
@@ -0,0 +1,41 @@
+/*
+ * resourced-headless
+ *
+ * Copyright (c) 2017 Samsung Electronics Co., Ltd.
+ *
+ * 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.
+ */
+
+/**
+ * @file  cgroup.h
+ * @brief CRUD for cgroup virtual filesystem
+ */
+
+#ifndef __RESOURCED_HEADLESS_GROUP_H__
+#define __RESOURCED_HEADLESS_GROUP_H__
+
+#define CGROUP_ROOT            "/sys/fs/cgroup"
+#define CGROUP_MEMORY  "memory"
+
+#define CGROUP_EVENT_INTERFACE "cgroup.event_control"
+
+/**
+ * @brief  Open eventfd linked with cgroup event interface
+ * @param[in] hierarchy  Cgroup hierarchy
+ * @param[in] filename  Notifier's name
+ * @param[in] value  The argument writen in the cgroup event interface
+ * @return  0 on success, otherwise a negative error value
+ */
+int cgroup_open_eventfd(const char *hierarchy, const char *filename, const char *value);
+
+#endif /* __RESOURCED_HEADLESS_CGROUP_H__ */