instance: add instances into unified cgroup hierarchy
authorDaniel Golle <daniel@makrotopia.org>
Wed, 5 Aug 2020 13:36:44 +0000 (14:36 +0100)
committerDaniel Golle <daniel@makrotopia.org>
Thu, 6 Aug 2020 14:18:34 +0000 (15:18 +0100)
Signed-off-by: Daniel Golle <daniel@makrotopia.org>
service/instance.c

index 4f13237c7b65ccb293f88171618328b7bcfc472b..218bdec513c176fad988f30ec6117c18eaaf6207 100644 (file)
@@ -37,6 +37,7 @@
 #include "instance.h"
 
 #define UJAIL_BIN_PATH "/sbin/ujail"
+#define CGROUP_BASEDIR "/sys/fs/cgroup/services"
 
 enum {
        INSTANCE_ATTR_COMMAND,
@@ -458,6 +459,32 @@ instance_run(struct service_instance *in, int _stdout, int _stderr)
        exit(127);
 }
 
+static void
+instance_add_cgroup(const char *service, const char *instance)
+{
+       struct stat sb;
+       char cgnamebuf[256];
+       int fd;
+
+       if (stat("/sys/fs/cgroup/cgroup.subtree_control", &sb))
+               return;
+
+       mkdir(CGROUP_BASEDIR, 0700);
+
+       snprintf(cgnamebuf, sizeof(cgnamebuf), "%s/%s", CGROUP_BASEDIR, service);
+       mkdir(cgnamebuf, 0700);
+       snprintf(cgnamebuf, sizeof(cgnamebuf), "%s/%s/%s", CGROUP_BASEDIR, service, instance);
+       mkdir(cgnamebuf, 0700);
+       strcat(cgnamebuf, "/cgroup.procs");
+
+       fd = open(cgnamebuf, O_WRONLY);
+       if (fd == -1)
+               return;
+
+       dprintf(fd, "%d", getpid());
+       close(fd);
+}
+
 static void
 instance_free_stdio(struct service_instance *in)
 {
@@ -538,6 +565,7 @@ instance_start(struct service_instance *in)
                uloop_done();
                closefd(opipe[0]);
                closefd(epipe[0]);
+               instance_add_cgroup(in->srv->name, in->name);
                instance_run(in, opipe[1], epipe[1]);
                return;
        }