]> git.ipfire.org Git - thirdparty/libcgroup.git/commitdiff
api: add function for creating template control groups
authorIvana Hutarova Varekova <varekova@redhat.com>
Mon, 28 Jan 2013 13:42:25 +0000 (14:42 +0100)
committerIvana Hutarova Varekova <varekova@redhat.com>
Mon, 28 Jan 2013 13:42:25 +0000 (14:42 +0100)
    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 <varekova@redhat.com>
Acked-by: Jan Safranek<jsafrane@redhat.com>
include/libcgroup/config.h
include/libcgroup/tasks.h
src/config.c
src/libcgroup.map

index d18634ef0d219e59c87c80e34ae491149890369e..43568e1cf8d06b2d6ad81dc21effac3cd0ce60f5 100644 (file)
@@ -83,6 +83,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);
+
 /**
  * @}
  * @}
index fb728f444fad90f1603c35634d357dbc798f0241..7e5089cf0a59a58c24fd096688168bf99b471bd9 100644 (file)
@@ -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(). */
index 72755daecd79b1cb5cc4d36ec4a91afb7fe80c74..db298386cb071bed7729aabb36a013dd68014d71 100644 (file)
@@ -1501,3 +1501,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;
+}
index e29f88796ffbdce64b8b4f4b2af6d6c5586fecf6..b550a58ff5ea9fc23dd9ffc204b8e4385177675e 100644 (file)
@@ -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;