When multi subsystems mounted on one place like this:

$ lssubsys -m
cpu,cpuacct /cgroup/cpu

$ lscgroup
cpu,cpuacct:/
cpu,cpuacct:/test

if we delete the cgroup with the cgdelete -g, and specifying multi
controllers like this:

$ cgdelete -g cpu,cpuacct:test

or

$ cgdelete -g cpu:test -g cpuacct:test

it will report error:
cgdelete: cannot remove group 'test': No such file or directory

this patch fix the problem.

v1 -> v2
 - make cgdelete -g cpu:/test -g cpu:test failed.
v2 -> v3
 - make cgdelete -g cpu:test -g cpu:test1 ok.
v3 -> v4
 - make cgdelete -g cpuacct:test -g cpu:test -g cpuacct:test failed.
 - add some comments
 - fix the uninitialized warning

Signed-off-by: Weng Meiling <wengmeiling.w...@huawei.com>
---
 src/tools/cgdelete.c | 87 ++++++++++++++++++++++++++++++++++++++++++++++------
 1 file changed, 78 insertions(+), 9 deletions(-)

diff --git a/src/tools/cgdelete.c b/src/tools/cgdelete.c
index b955753..29e3113 100644
--- a/src/tools/cgdelete.c
+++ b/src/tools/cgdelete.c
@@ -52,6 +52,52 @@ static void usage(int status, const char *program_name)
        }
 }

+/*
+ * Skip adding controller which points to the same group when delete
+ * group with specifying multi controllers. Just skip controller which
+ * cgroup and hierarchy number is same.
+ *     @param counter Count for the current groups
+ *     @param skip The skip result
+ *     @param h_list Record for controllers' hierarchy
+ *     @param cg_name Record for groups' name
+ *     @param cont_name Record for controllers' name
+ */
+static int skip_add_controller(int counter, int *skip, int *h_list,
+               char cg_name[][FILENAME_MAX], char cont_name[][FILENAME_MAX])
+{
+       int k, ret = 0;
+       struct controller_data info;
+       void *handle;
+
+       ret = cgroup_get_all_controller_begin(&handle, &info);
+       while (ret == 0) {
+               if (!strcmp(info.name, cont_name[counter])) {
+                       h_list[counter] = info.hierarchy;
+                       for (k = 0; k < counter; k++)
+                               if (h_list[k] == info.hierarchy &&
+                                       !strcmp(cg_name[k],
+                                               cg_name[counter])) {
+                                       if (strcmp(cont_name[k],
+                                               cont_name[counter]))
+                                               *skip = 1;
+                                       break;
+                               }
+                       break;
+               }
+               ret = cgroup_get_all_controller_next(&handle, &info);
+       }
+       cgroup_get_all_controller_end(&handle);
+
+       if (ret == ECGEOF)
+               ret = 0;
+       if (ret) {
+               fprintf(stderr,
+               "cgroup_get_controller_begin/next failed(%s)\n",
+               cgroup_strerror(ret));
+       }
+       return ret;
+}
+

 int main(int argc, char *argv[])
 {
@@ -60,6 +106,10 @@ int main(int argc, char *argv[])
        int c;
        int flags = 0;
        int final_ret = 0;
+       int counter = 0;
+       int h_list[CG_CONTROLLER_MAX] = {-1};
+       char cg_name[CG_CONTROLLER_MAX][FILENAME_MAX];
+       char cont_name[CG_CONTROLLER_MAX][FILENAME_MAX];

        struct cgroup_group_spec **cgroup_list = NULL;
        struct cgroup *cgroup;
@@ -139,17 +189,36 @@ int main(int argc, char *argv[])
                /* add controllers to the cgroup */
                j = 0;
                while (cgroup_list[i]->controllers[j]) {
-                       cgc = cgroup_add_controller(cgroup,
-                               cgroup_list[i]->controllers[j]);
-                       if (!cgc) {
-                               ret = ECGFAIL;
-                               fprintf(stderr, "%s: "
-                                       "controller %s can't be added\n",
-                                       argv[0],
-                                       cgroup_list[i]->controllers[j]);
-                               cgroup_free(&cgroup);
+                       int skip_add_cont = 0;
+                       /*
+                        * save controller name, cgroup name and hierarchy 
number to
+                        * determine whether we should skip addng the 
controller.
+                        */
+                       strncpy(cont_name[counter],
+                               cgroup_list[i]->controllers[j],
+                               FILENAME_MAX);
+                       cont_name[counter][FILENAME_MAX - 1] = '\0';
+                       strncpy(cg_name[counter], cgroup_list[i]->path,
+                               FILENAME_MAX);
+                       cg_name[counter][FILENAME_MAX - 1] = '\0';
+                       ret = skip_add_controller(counter, &skip_add_cont,
+                                               h_list, cg_name, cont_name);
+                       if (ret)
                                goto err;
+                       if (!skip_add_cont) {
+                               cgc = cgroup_add_controller(cgroup,
+                                       cgroup_list[i]->controllers[j]);
+                               if (!cgc) {
+                                       ret = ECGFAIL;
+                                       fprintf(stderr, "%s: "
+                                               "controller %s can't be 
added\n",
+                                               argv[0],
+                                               cgroup_list[i]->controllers[j]);
+                                       cgroup_free(&cgroup);
+                                       goto err;
+                               }
                        }
+                       counter++;
                        j++;
                }

-- 
1.8.3




------------------------------------------------------------------------------
Open source business process management suite built on Java and Eclipse
Turn processes into business applications with Bonita BPM Community Edition
Quickly connect people, data, and systems into organized workflows
Winner of BOSSIE, CODIE, OW2 and Gartner awards
http://p.sf.net/sfu/Bonitasoft
_______________________________________________
Libcg-devel mailing list
Libcg-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/libcg-devel

Reply via email to