From 83053b6a59e087087f35a4834a61a30ec64f61ba Mon Sep 17 00:00:00 2001 From: Daniel Golle Date: Wed, 5 Aug 2020 14:36:44 +0100 Subject: [PATCH] instance: add instances into unified cgroup hierarchy Signed-off-by: Daniel Golle --- service/instance.c | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/service/instance.c b/service/instance.c index 4f13237..218bdec 100644 --- a/service/instance.c +++ b/service/instance.c @@ -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; } -- 2.30.2