add a function: int cgroup_config_create_template_group(const char *pathname, struct cgroup *cgroup, char *template_name, int ignore_ownership);
Physically create a new control group in kernel, based on given control group template and configuration file. If given template is not set in configuration file, then the procedure works create the control group using cgroup_create_cgroup function input parameters are: pathname .. name of template configuration file (/etc/cgconfig.conf by default) cgroup .. control group name and subsystems to which it should belong to template_name .. name of the template we want to use flags .. Bit flags to change the behavior return 0 on success Signed-off-by: Ivana Hutarova Varekova <varek...@redhat.com> --- include/libcgroup/config.h | 20 ++++++++++++++ include/libcgroup/tasks.h | 2 + src/config.c | 65 ++++++++++++++++++++++++++++++++++++++++++++ src/libcgroup.map | 3 +- 4 files changed, 89 insertions(+), 1 deletions(-) diff --git a/include/libcgroup/config.h b/include/libcgroup/config.h index d18634e..43568e1 100644 --- a/include/libcgroup/config.h +++ b/include/libcgroup/config.h @@ -84,6 +84,26 @@ int cgroup_init_templates_cache(char *pathname); int cgroup_reload_cached_templates(char *pathname); /** + * Physically create a new control group in kernel, based on given control + * group template and configuration file. If given template is not set in + * configuration file, then the procedure works create the control group + * using cgroup_create_cgroup() function + * + * The flags can alter the behavior of this function: + * CGFLAG_USE_TEMPLATE_CACHE: Use cached templates instead of + * parsing the config file + * + * @param pathname Name of the configuration file with template definitions + * @param cgroup Wanted control group - contains substitute name and wanted + * controllers. + * @param template_name Template name used for cgroup setting + * @param flags Bit flags to change the behavior + */ +int cgroup_config_create_template_group( + struct cgroup *cgroup, char *template_name, + int flags); + +/** * @} * @} */ diff --git a/include/libcgroup/tasks.h b/include/libcgroup/tasks.h index fb728f4..7e5089c 100644 --- a/include/libcgroup/tasks.h +++ b/include/libcgroup/tasks.h @@ -18,6 +18,8 @@ __BEGIN_DECLS enum cgflags { /** Use cached rules, do not read rules from disk. */ CGFLAG_USECACHE = 0x01, + /** Use cached templates, do not read templates from disk. */ + CGFLAG_USE_TEMPLATE_CACHE = 0x02, }; /** Flags for cgroup_register_unchanged_process(). */ diff --git a/src/config.c b/src/config.c index 6a91239..c70972e 100644 --- a/src/config.c +++ b/src/config.c @@ -1498,3 +1498,68 @@ int cgroup_init_templates_cache(char *pathname) } + +/* + * Create a given cgroup, based on template configuration if it is present + * if the template is not present cgroup is creted using cgroup_create_cgroup + */ +int cgroup_config_create_template_group(struct cgroup *cgroup, + char *template_name, int flags) +{ + int ret = 0; + int i, j, k; + struct cgroup *t_cgroup; + + /* + * If the user did not ask for cached rules, we must parse the + * configuration file and prepare template structures now. We + * use CGCONFIG_CONF_FILE by default + */ + if (!(flags & CGFLAG_USE_TEMPLATE_CACHE)) { + ret = cgroup_init_templates_cache(CGCONFIG_CONF_FILE); + if (ret != 0) { + cgroup_dbg("Failed initialize templates cache.\n"); + return ret; + } + } + + for (i = 0; cgroup->controller[i] != NULL; i++) { +new_controller: + for (j = 0; j < template_table_index; j++) { + + t_cgroup = &template_table[j]; + if (strcmp(t_cgroup->name, template_name) != 0) { + /* tempate name does not match skip template */ + continue; + } + + /* template name match */ + for (k = 0; t_cgroup->controller[k] != NULL; k++) { + if (strcmp((cgroup->controller[i])->name, + (t_cgroup->controller[k])->name) != 0) { + continue; + } + /* name and controller match template found */ + + /* variables substituted in template */ + strncpy(t_cgroup->name, cgroup->name, + FILENAME_MAX); + + ret = cgroup_create_cgroup(t_cgroup, + 0); + if (ret) { + cgroup_dbg("creating group %s, error %d\n", + cgroup->name, ret); + goto end; + } else { + goto new_controller; + } + } + } + } + + /* the template does not exist */ + cgroup_create_cgroup(cgroup, flags); +end: + return ret; +} diff --git a/src/libcgroup.map b/src/libcgroup.map index e29f887..b550a58 100644 --- a/src/libcgroup.map +++ b/src/libcgroup.map @@ -109,4 +109,5 @@ CGROUP_0.38 { CGROUP_0.39 { cgroup_reload_cached_templates; cgroup_init_templates_cache; -} CGROUP_0.38; \ No newline at end of file + cgroup_config_create_template_group; +} CGROUP_0.38; ------------------------------------------------------------------------------ Monitor your physical, virtual and cloud infrastructure from a single web console. Get in-depth insight into apps, servers, databases, vmware, SAP, cloud infrastructure, etc. Download 30-day Free Trial. Pricing starts from $795 for 25 servers or applications! http://p.sf.net/sfu/zoho_dev2dev_nov _______________________________________________ Libcg-devel mailing list Libcg-devel@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/libcg-devel