From: Kamalesh Babulal Date: Wed, 8 Feb 2023 09:56:17 +0000 (+0530) Subject: API: add cgroup_set_default_systemd_cgroup() X-Git-Tag: v3.1.0~213 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=23f9f03fd4ff5c7e48438813650b21b7cfbc11be;p=thirdparty%2Flibcgroup.git API: add cgroup_set_default_systemd_cgroup() Introduce an API cgroup_set_default_systemd_cgroup(), that reads the systemd_default_cgroup_file, if exists, and only sets the systemd_default_cgroup path if both of the following conditions are met: 1. The minimum length of the default slice/scope read is 15 characters, because at the minimum it should be .slice/.scope. 2. Checks if the /sys/fs/cgroup/ cgroup exists and if it doesn't, it also deletes the systemd_default_cgroup_file (/var/run/libcgroup/systemd) file. this will be used by the tools and can be used by any applications to set the default cgroup tree to systemd_default_cgroup after loading the cgconfig.conf file(s). Signed-off-by: Kamalesh Babulal Signed-off-by: Tom Hromatka --- diff --git a/src/config.c b/src/config.c index 1893b1d3..6b4fe067 100644 --- a/src/config.c +++ b/src/config.c @@ -2077,6 +2077,87 @@ static int config_create_slice_scope(char * const tmp_systemd_default_cgroup) err: return 0; } + +/* + * This function builds the canonical path for the systemd default cgroup + * based on the cgroup setup mode and if the cgroup doesn't exist removes + * the systemd default cgroup file. This function expects the caller to be + * holding the systemd_default_cgroup_lock. + */ +static bool systemd_default_cgroup_exists(void) +{ + char path[FILENAME_MAX] = "\0"; + char cgrp_procs_path[FILENAME_MAX + 13]; /* 13 = strlen(/cgroup.procs) */ + bool exists = false; + + switch (cgroup_setup_mode()) { + case CGROUP_MODE_HYBRID: + /* + * check for empty cgroup v2, the most common usage in + * the hybrid case. + */ + if (cg_build_path("", path, NULL)) + break; + case CGROUP_MODE_UNIFIED: + /* fallthrough */ + case CGROUP_MODE_LEGACY: + cg_build_path("", path, "cpu"); + /* fallthrough */ + case CGROUP_MODE_UNK: + break; + } + + if (!strlen(path)) { + cgroup_dbg("Unable to form canonical path for %s", systemd_default_cgroup); + goto err; + } + + snprintf(cgrp_procs_path, sizeof(cgrp_procs_path), "%s/cgroup.procs", path); + if (access(cgrp_procs_path, F_OK)) { + /* + * systemd has already removed the scope cgroup, hence delete + * the file too. + */ + cgroup_dbg("%s doesn't exists, deleting %s", path, systemd_default_cgroup_file); + unlink(systemd_default_cgroup_file); + goto err; + } + + exists = true; +err: + return exists; +} + +void cgroup_set_default_systemd_cgroup(void) +{ + FILE *systemd_def_cgrp_f; + size_t len; + + pthread_rwlock_wrlock(&systemd_default_cgroup_lock); + + systemd_def_cgrp_f = fopen(systemd_default_cgroup_file, "r"); + if (!systemd_def_cgrp_f) { + cgroup_dbg("Unable to read %s ", systemd_default_cgroup_file); + goto err; + } + + len = fread(systemd_default_cgroup, sizeof(char), FILENAME_MAX, systemd_def_cgrp_f); + fclose(systemd_def_cgrp_f); + /* at the minimum it should be .slice/.scope == 15 */ + if (len < 15) { + cgroup_dbg("Malformed systemd default cgroup %s", systemd_default_cgroup); + goto err; + } + + if (systemd_default_cgroup_exists()) { + pthread_rwlock_unlock(&systemd_default_cgroup_lock); + return; + } +err: + pthread_rwlock_unlock(&systemd_default_cgroup_lock); + cgroup_dbg(", continuing without systemd default cgroup.\n", systemd_default_cgroup); + systemd_default_cgroup[0] = '\0'; +} #else int cgroup_add_systemd_opts(const char * const config, const char * const value) { @@ -2089,4 +2170,6 @@ int cgroup_alloc_systemd_opts(const char * const config, const char * const valu } void cgroup_cleanup_systemd_opts(void) { } + +void cgroup_set_default_systemd_cgroup(void) { } #endif diff --git a/src/libcgroup.map b/src/libcgroup.map index 07bd7816..28ce7d40 100644 --- a/src/libcgroup.map +++ b/src/libcgroup.map @@ -155,4 +155,5 @@ CGROUP_3.0 { cgroup_get_controller_by_index; cgroup_get_controller_name; cgroup_create_scope2; + cgroup_set_default_systemd_cgroup; } CGROUP_2.0;