On 12/15/2012 07:01 PM, Ivana Hutarova Varekova wrote: > 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 > > CHANGELOG: > reload template rules if it is necessary, don't init them > create a control group in all controllers which have no relevant > template > return the template name when it is changed - Jan's feedback > > Signed-off-by: Ivana Hutarova Varekova <varek...@redhat.com> Acked-by: Jan Safranek<jsafr...@redhat.com>
> --- > > include/libcgroup/config.h | 20 ++++++++ > include/libcgroup/tasks.h | 2 + > src/config.c | 108 > ++++++++++++++++++++++++++++++++++++++++++++ > src/libcgroup.map | 3 + > 4 files changed, 132 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 6955546..549a7ef 100644 > --- a/src/config.c > +++ b/src/config.c > @@ -1503,3 +1503,111 @@ 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; > + char buffer[FILENAME_MAX]; > + struct cgroup *aux_cgroup; > + struct cgroup_controller *cgc; > + > + /* > + * 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)) { > + if (template_table_index == 0) > + /* the rules cache is empty */ > + ret = cgroup_init_templates_cache(CGCONFIG_CONF_FILE); > + else > + /* cache is not empty */ > + ret = cgroup_reload_cached_templates( > + CGCONFIG_CONF_FILE); > + if (ret != 0) { > + cgroup_dbg("Failed initialize templates cache.\n"); > + return ret; > + } > + } > + > + for (i = 0; cgroup->controller[i] != NULL; i++) { > + /* for each controller we have to add to cgroup structure > + * either template cgroup or empty controller */ > + > + /* look for relevant template - test name x controller pair */ > + for (j = 0; j < template_table_index; j++) { > + > + t_cgroup = &template_table[j]; > + if (strcmp(t_cgroup->name, template_name) != 0) { > + /* template 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) { > + /* controller name does not match */ > + continue; > + } > + > + /* name and controller match template found */ > + /* variables substituted in template */ > + strncpy(buffer, t_cgroup->name, > + FILENAME_MAX); > + strncpy(t_cgroup->name, cgroup->name, > + FILENAME_MAX); > + > + ret = cgroup_create_cgroup(t_cgroup, flags); > + > + strncpy(t_cgroup->name, buffer, > + FILENAME_MAX); > + if (ret) { > + cgroup_dbg("creating group %s, error > %d\n", > + cgroup->name, ret); > + goto end; > + } else { > + /* go to new controller */ > + j = template_table_index; > + continue; > + } > + > + } > + } > + > + /* no template is present for given name x controller pair > + * add controller to result cgroup */ > + aux_cgroup = cgroup_new_cgroup(cgroup->name); > + if (ret) { > + ret = ECGINVAL; > + fprintf(stderr, "cgroup %s can't be created\n", > + cgroup->name); > + goto end; > + } > + cgc = cgroup_add_controller(aux_cgroup, > + (cgroup->controller[i])->name); > + if (cgc == NULL) { > + ret = ECGINVAL; > + fprintf(stderr, "cgroup %s can't be created\n", > + cgroup->name); > + goto end; > + } > + ret = cgroup_create_cgroup(aux_cgroup, flags); > + if (ret) { > + ret = ECGINVAL; > + fprintf(stderr, "cgroup %s can't be created\n", > + cgroup->name); > + goto end; > + } > + } > + > +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; > > > ------------------------------------------------------------------------------ > LogMeIn Rescue: Anywhere, Anytime Remote support for IT. Free Trial > Remotely access PCs and mobile devices and provide instant support > Improve your efficiency, and focus on delivering more value-add services > Discover what IT Professionals Know. Rescue delivers > http://p.sf.net/sfu/logmein_12329d2d > _______________________________________________ > Libcg-devel mailing list > Libcg-devel@lists.sourceforge.net > https://lists.sourceforge.net/lists/listinfo/libcg-devel > ------------------------------------------------------------------------------ LogMeIn Rescue: Anywhere, Anytime Remote support for IT. Free Trial Remotely access PCs and mobile devices and provide instant support Improve your efficiency, and focus on delivering more value-add services Discover what IT Professionals Know. Rescue delivers http://p.sf.net/sfu/logmein_12329d2d _______________________________________________ Libcg-devel mailing list Libcg-devel@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/libcg-devel