Fix #188: support without cgroups v2 support

This patch adds support for detecting if the Linux kernel Fint runs on
has support for cgroups v2.  The probe is done very early, in runlevel
S, when attempting to mount /sys/fs/cgroup.  Meaning, the log message
may be lost on systems that do not run sysklogd[1].

Here's the relevant logs from a system running myLinux on a kernel
that has cgroups v2 support disabled:

    Jan 10 20:19:40 anarchy finit[1]: myLinux 2021.11-696-g8fddaf36ed, entering runlevel S
    Jan 10 20:19:40 anarchy finit[1]: Kernel does not support cgroups v2, disabling.
    Jan 10 20:19:41 anarchy finit[1]: myLinux 2021.11-696-g8fddaf36ed, entering runlevel 2

All code paths that attempt to set or query kernel cgroups config has
been short circuited when this happens.  However, this does not apply
to any cgroup related config in the Finit .conf files -- which may of
course be omitted.  Yet, if configuration directives exist, they will
be parsed and may cause error.

Also, the initctl tool checks if /sys/fs/cgroup is mounted at runtime
and disables all status output and commands that rely on the feature.

Signed-off-by: Joachim Wiberg <troglobit@gmail.com>
This commit is contained in:
Joachim Wiberg committed 2022-01-10 21:21:35 +01:00
1 parent d331b72dfd
commit a37037ded4
4 files changed
+108 -53

No files matched your search

+30 -1
View File
@@ -56,6 +56,7 @@ static char controllers[256];
static struct iwatch iw_cgroup;
static uev_t cgw;
static int avail;
static void cgset(const char *path, char *ctrl, char *prop)
@@ -164,6 +165,9 @@ static int cgroup_leaf_init(char *group, char *name, int pid, const char *cfg)
int cgroup_user(char *name, int pid)
{
if (!avail)
return 0;
return cgroup_leaf_init("user", name, pid, NULL);
}
@@ -171,6 +175,9 @@ int cgroup_service(char *name, int pid, struct cgroup *cg)
{
char *group = "system";
if (!avail)
return 0;
if (cg && cg->name[0]) {
char path[256];
@@ -307,6 +314,9 @@ void cgroup_mark_all(void)
{
struct cg *cg;
if (!avail)
return;
TAILQ_FOREACH(cg, &cgroups, link) {
if (cg->is_protected)
continue;
@@ -323,6 +333,9 @@ void cgroup_cleanup(void)
struct cg *cg, *tmp;
char path[256];
if (!avail)
return;
TAILQ_FOREACH_SAFE(cg, &cgroups, link, tmp) {
if (cg->active)
continue;
@@ -339,6 +352,9 @@ int cgroup_add(char *name, char *cfg, int is_protected)
{
struct cg *cg;
if (!avail)
return 0;
if (!name)
return -1;
if (!cfg)
@@ -383,6 +399,9 @@ int cgroup_del(char *dir)
struct cg *cg;
char path[256];
if (!avail)
return 0;
TAILQ_FOREACH(cg, &cgroups, link) {
snprintf(path, sizeof(path), FINIT_CGPATH "/%s", cg->name);
if (strcmp(path, dir))
@@ -414,6 +433,9 @@ void cgroup_config(void)
{
struct cg *cg;
if (!avail)
return;
TAILQ_FOREACH(cg, &cgroups, link) {
char path[256];
int leaf = 0;
@@ -442,14 +464,21 @@ void cgroup_init(uev_ctx_t *ctx)
int fd;
if (mount("none", FINIT_CGPATH, "cgroup2", opts, NULL)) {
_pe("Failed mounting cgroup v2");
if (errno == ENOENT)
logit(LOG_NOTICE, "Kernel does not support cgroups v2, disabling.");
else
_pe("Failed mounting cgroup v2, disabling.");
avail = 0;
return;
}
avail = 1;
/* Find available controllers */
fp = fopen(FINIT_CGPATH "/cgroup.controllers", "r");
if (!fp) {
_pe("Failed opening %s", FINIT_CGPATH "/cgroup.controllers");
umount(FINIT_CGPATH);
avail = 0;
return;
}