cgroup_change_cgroup_flags: if wanted group is template group then this patch 
enables cgroup_change_cgroup_flags procedure to create the control group on the 
fly. It uses
cgroup_create_cgroup procedure - will be changed in the next patch.
Template cgroups are control groups which contains % variable like %U.

e.g.
@students       devices         people/students/%U
will create a cgroup /people/students/john if user john from group students run 
a command and the people does not exist yet.

Changelog:
* not use cgroup_get_cgroup function (too robust for checking whether relevant 
cgroup exist or not)

Signed-off-by: Ivana Hutarova Varekova <varek...@redhat.com>
---

 src/api.c |  130 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 130 insertions(+), 0 deletions(-)

diff --git a/src/api.c b/src/api.c
index ea75cca..1d8219b 100644
--- a/src/api.c
+++ b/src/api.c
@@ -2635,6 +2635,129 @@ static struct cgroup_rule 
*cgroup_find_matching_rule(uid_t uid,
        return ret;
 }
 
+/* create control group based given template
+ * if the group already don't exist
+ * dest is template name with substitute variables
+ * tmp is used cgrules rule
+ */
+int cgroup_create_template_group(char *dest, struct cgroup_rule *tmp)
+{
+       char *prefix;   /* investigate cgroup */
+       char *suffix;   /* the rest of directory path not investigate yet */
+       struct cgroup *template_cgroup = NULL;
+       int ret = 0;
+       int i, j;
+       int exist;
+       int first;
+       struct cgroup_controller *controller;
+       char path[FILENAME_MAX];
+       DIR *dir;
+
+       prefix = (char *)malloc(sizeof(char *) * strlen(dest));
+       if (!prefix) {
+               last_errno = errno;
+               ret = ECGOTHER;
+               return ret;
+       }
+
+       /* nothing investigated yet */
+       suffix = dest;
+
+       while (suffix != NULL) {
+               /* prefix contains "not created/tested" directory cgroup which
+                * is closest to the root in given destination path
+                */
+
+               /* move suffix to deeper directory and prefix to path to it */
+               suffix = strchr(suffix, '/');
+               if (suffix != NULL) {
+                       suffix[0] = '\0';
+                       strcpy(prefix, dest);
+                       suffix[0] = '/';
+                       suffix++;
+               } else {
+                       strcpy(prefix, dest);
+               }
+
+               first = 0;
+               i = 0;
+               /* test for which controllers wanted group does not exist*/
+               while (tmp->controllers[i] != NULL) {
+
+                       /* test controller tmp->controllers[i] */
+                       pthread_rwlock_rdlock(&cg_mount_table_lock);
+                       exist = 1;
+                       for (j = 0; j < CG_CONTROLLER_MAX &&
+                               cg_mount_table[j].name[0] != '\0'; j++) {
+
+                               if (strcmp(cg_mount_table[j].name,
+                                       tmp->controllers[i])) {
+                                       /* it is not wanted controller */
+                                       continue;
+                               }
+
+                               if (!cg_build_path_locked(prefix, path,
+                                       cg_mount_table[j].name))
+                                       continue;
+
+                               dir = opendir(path);
+                               if (dir != NULL) {
+                                       /* cgroup in given subsystem
+                                        * does not exist
+                                        */
+                                       closedir(dir);
+                                       continue;
+                               }
+
+                               /* control group for wanted controller exists */
+                               exist = 0;
+                       }
+                       pthread_rwlock_unlock(&cg_mount_table_lock);
+
+                       /* controller have not relevant group */
+                       if (!exist) {
+                               /* it is the first controller without relevant
+                                  group the group have to be created*/
+                               if (first == 0) {
+                                       first = 1;
+                                       /* we investigate prefix group now.*/
+                                       template_cgroup =
+                                               cgroup_new_cgroup(prefix);
+                                       if (template_cgroup == NULL) {
+                                               ret = ECGFAIL;
+                                               goto finished;
+                                       }
+                               }
+
+                               /* and the controller have to be add */
+                               controller = cgroup_add_controller(
+                                       template_cgroup, tmp->controllers[i]);
+                               if (!controller) {
+                                       ret = ECGFAIL;
+                                       goto finished;
+                               }
+                       }
+                       i++;
+               }
+
+               if (first == 1) {
+                       /*  new group have to be created */
+                       ret = cgroup_create_cgroup(template_cgroup, 0);
+                       if (ret != 0) {
+                               cgroup_free(&template_cgroup);
+                               goto finished;
+                       }
+                       cgroup_dbg("Group %s created\n", prefix);
+
+                       cgroup_free(&template_cgroup);
+               }
+       }
+
+finished:
+       free(prefix);
+       return ret;
+}
+
 int cgroup_change_cgroup_flags(uid_t uid, gid_t gid,
                const char *procname, pid_t pid, int flags)
 {
@@ -2783,7 +2906,14 @@ int cgroup_change_cgroup_flags(uid_t uid, gid_t gid,
                                newdest[j] = tmp->destination[i];
                        }
                }
+
                newdest[j] = 0;
+               if (strcmp(newdest, tmp->destination) != 0) {
+                       /* destination tag contains templates */
+
+                       cgroup_dbg("control group %s is template\n", newdest);
+                       ret = cgroup_create_template_group(newdest, tmp);
+               }
 
                /* Apply the rule */
                ret = cgroup_change_cgroup_path(newdest,


------------------------------------------------------------------------------
Everyone hates slow websites. So do we.
Make your web apps faster with AppDynamics
Download AppDynamics Lite for free today:
http://ad.doubleclick.net/clk;258768047;13503038;j?
http://info.appdynamics.com/FreeJavaPerformanceDownload.html
_______________________________________________
Libcg-devel mailing list
Libcg-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/libcg-devel

Reply via email to