From: Daniel Wagner <[email protected]>

When starting ConnMan we create under /sys/fs/cgroup a new
directory called connman where we mount for the time beeing
the net_cls controller.
---
 src/cgroup.c | 92 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 92 insertions(+)

diff --git a/src/cgroup.c b/src/cgroup.c
index 712274a..febb895 100644
--- a/src/cgroup.c
+++ b/src/cgroup.c
@@ -23,16 +23,108 @@
 #include <config.h>
 #endif
 
+#include <sys/mount.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <unistd.h>
+#include <errno.h>
+
 #include "connman.h"
 
+#define CGROUP_PATH "/sys/fs/cgroup/connman"
+
+static connman_bool_t enabled;
+
+static int check_mount_point(const char *path)
+{
+       struct stat st;
+       char *parent, *end;
+       int l, err;
+
+       err = lstat(path, &st);
+       if (err < 0) {
+               if (errno == ENOENT)
+                       return 0;
+               return err;
+       }
+
+       end = rindex(path, '/');
+       l = end - path;
+       parent = g_strndup(path, l);
+
+       err = lstat(parent, &st);
+       g_free(parent);
+
+       return err;
+}
+
+static int cgroup_create(const char *path, const char *controller)
+{
+       int err;
+
+       err = mkdir(path, S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH);
+       if (err < 0 && errno != EEXIST) {
+               err = -errno;
+               DBG("Could not create %s: %s", path, strerror(errno));
+               return err;
+       }
+
+       err = mount("cgroup", path, "cgroup",
+               MS_NOSUID|MS_NOEXEC|MS_NODEV, controller);
+       if (err < 0) {
+               err = -errno;
+               DBG("Could not mount %s: %s", path, strerror(errno));
+               unlink(path);
+       }
+
+       return err;
+}
+
+static int cgroup_setup(void)
+{
+       int err;
+
+       err = check_mount_point(CGROUP_PATH);
+       if (err < 0)
+               return err;
+
+       err = cgroup_create(CGROUP_PATH, "net_cls");
+       if (err < 0)
+               return err;
+
+       return 0;
+
+}
+
+static void cgroup_cleanup(void)
+{
+       umount(CGROUP_PATH);
+       rmdir(CGROUP_PATH);
+}
+
 int __connman_cgroup_init(void)
 {
+       int err;
+
        DBG("");
 
+       err = cgroup_setup();
+       if (err < 0) {
+               connman_error("Could not create ConnMan cgroup");
+               return err;
+       }
+
+       enabled = TRUE;
+
        return 0;
 }
 
 void __connman_cgroup_cleanup(void)
 {
        DBG("");
+
+       if (enabled == FALSE)
+               return;
+
+       cgroup_cleanup();
 }
-- 
1.7.12.rc1.16.g05a20c8

_______________________________________________
connman mailing list
[email protected]
http://lists.connman.net/listinfo/connman

Reply via email to