This API unloads the cgroupfs filesystem in memory. With this API
we can now replace service cgconfig stop with a cgconfigparser -u

Signed-off-by: Dhaval Giani <[email protected]>
---
 include/libcgroup.h  |    1 
 src/config.c         |  117 +++++++++++++++++++++++++++++++++++++++++++++++++++
 src/libcgroup.map    |    1 
 src/tools/cgconfig.c |    7 ++-
 4 files changed, 125 insertions(+), 1 deletion(-)

Index: libcg/src/config.c
===================================================================
--- libcg.orig/src/config.c     2009-06-15 16:08:10.000000000 +0530
+++ libcg/src/config.c  2009-06-15 20:39:26.000000000 +0530
@@ -494,3 +494,150 @@
        fclose(yyin);
        return error;
 }
+
+int cgroup_config_unload_config(void)
+{
+       int error = 0;
+       char *ctrl_name, *ctrl_path;
+       void *ctrl_handle;
+       int ret = 0;
+       char *curr_path = NULL;
+       char *root_path, *rel_path;
+
+       error = cgroup_init();
+
+       if (error) {
+               ret = error;
+               goto out_error;
+       }
+
+       error = cgroup_get_controller_begin(&ctrl_handle, &ctrl_name,
+                                                       &ctrl_path);
+
+
+       if (error && error !=ECGEOF) {
+               ret = error;
+               goto out_error;
+       }
+
+       while (error != ECGEOF) {
+               struct cgroup_file_info info;
+               void *tree_handle;
+               int lvl;
+
+
+               if (curr_path) {
+                       if (strcmp(ctrl_path, curr_path) == 0) {
+                               error = 
cgroup_get_controller_next(&ctrl_handle, &ctrl_name, &ctrl_path);
+
+                               if (error) {
+                                       ret = error;
+                                       free(curr_path);
+                                       goto out_error;
+                               }
+                               continue;
+                       }
+                       else {
+                               free(curr_path);
+                               curr_path = strdup(ctrl_path);
+
+                               if (!curr_path) {
+                                       last_errno = errno;
+                                       ret = ECGOTHER;
+                                       goto ctrl_error;
+                               }
+                       }
+               } else {
+                       curr_path = strdup(ctrl_path);
+                       if (!curr_path) {
+                               free(ctrl_path);
+                               ctrl_path = NULL;
+                               free(ctrl_name);
+                               ctrl_name = NULL;
+                       }
+               }
+
+               ret = cgroup_walk_tree_begin(ctrl_name, "/", 0, &tree_handle,
+                                                               &info, &lvl);
+
+               if (ret)
+                       goto ctrl_error;
+
+               root_path = strdup(info.full_path);
+
+               if (!root_path) {
+                       last_errno = ECGOTHER;
+                       ret = ECGOTHER;
+                       goto ctrl_error;
+               }
+
+               error = cgroup_walk_tree_set_flags(&tree_handle, 
CGROUP_WALK_TYPE_POST_DIR);
+
+               if (error) {
+                       ret = error;
+                       goto ctrl_error;
+               }
+
+               while (error != ECGEOF) {
+                       void *task_handle;
+                       pid_t pid;
+
+                       if (info.type != CGROUP_FILE_TYPE_DIR) {
+                               error = cgroup_walk_tree_next(0, &tree_handle, 
&info, lvl);
+                               continue;
+                       }
+
+                       rel_path = info.full_path + strlen(root_path) - 1;
+
+                       if (!strncmp(rel_path, "/", strlen(rel_path))) {
+                               error = cgroup_walk_tree_next(0, &tree_handle, 
&info, lvl);
+                               continue;
+                       }
+
+                       error = cgroup_get_task_begin(rel_path, ctrl_name,
+                                                       &task_handle, &pid);
+
+                       while (error != ECGEOF) {
+                               ret = cgroup_attach_task_pid(NULL, pid);
+                               error = cgroup_get_task_next(&task_handle, 
&pid);
+                       }
+
+                       cgroup_get_task_end(&task_handle);
+
+                       error = rmdir(info.full_path);
+                       if (error) {
+                               last_errno = errno;
+                               ret = ECGOTHER;
+                               goto ctrl_error;
+                       }
+
+                       error = cgroup_walk_tree_next(0, &tree_handle, &info, 
lvl);
+               }
+               cgroup_walk_tree_end(&tree_handle);
+               error = umount(ctrl_path);
+
+               if (error) {
+                       last_errno = errno;
+                       ret = ECGOTHER;
+                       goto ctrl_error;
+               }
+               free(ctrl_name);
+               ctrl_name = NULL;
+               free(ctrl_path);
+               ctrl_path = NULL;
+
+               error = cgroup_get_controller_next(&ctrl_handle, &ctrl_name, 
&ctrl_path);
+       }
+
+ctrl_error:
+       if(curr_path)
+               free(curr_path);
+       if(ctrl_path)
+               free(ctrl_path);
+       if(ctrl_name)
+               free(ctrl_name);
+out_error:
+       cgroup_get_controller_end(&ctrl_handle);
+       free(root_path);
+       return ret;
+}
Index: libcg/src/tools/cgconfig.c
===================================================================
--- libcg.orig/src/tools/cgconfig.c     2009-05-18 15:34:44.000000000 +0530
+++ libcg/src/tools/cgconfig.c  2009-06-15 20:34:25.000000000 +0530
@@ -33,6 +33,7 @@
        printf("\n");
        printf("  -h, --help            Display this help\n");
        printf("  -l, --load=FILE       Parse and load the cgroups 
configuration file\n");
+       printf("  -u, --unload          Unload the cgroups filesystem\n");
        exit(2);
 }
 
@@ -44,13 +45,14 @@
        static struct option options[] = {
                {"help", 0, 0, 'h'},
                {"load", 1, 0, 'l'},
+               {"unload", 0, 0, 'u'},
                {0, 0, 0, 0}
        };
 
        if (argc < 2)
                usage(argv[0]); /* usage() exits */
 
-       while ((c = getopt_long(argc, argv, "hl:", options, NULL)) > 0) {
+       while ((c = getopt_long(argc, argv, "hl:u", options, NULL)) > 0) {
                switch (c) {
                case 'h':
                        usage(argv[0]);
@@ -65,6 +67,9 @@
                                exit(3);
                        }
                        return 0;
+               case 'u':
+                       ret = cgroup_config_unload_config();
+                       return 0;
                default:
                        usage(argv[0]);
                        break;
Index: libcg/include/libcgroup.h
===================================================================
--- libcg.orig/include/libcgroup.h      2009-06-15 16:51:42.000000000 +0530
+++ libcg/include/libcgroup.h   2009-06-15 16:52:15.000000000 +0530
@@ -371,6 +371,7 @@
  * Config related stuff
  */
 int cgroup_config_load_config(const char *pathname);
+int cgroup_config_unload_config(void);
 
 __END_DECLS
 
Index: libcg/src/libcgroup.map
===================================================================
--- libcg.orig/src/libcgroup.map        2009-06-15 16:31:21.000000000 +0530
+++ libcg/src/libcgroup.map     2009-06-15 16:52:15.000000000 +0530
@@ -66,6 +66,7 @@
        cgroup_get_controller_end;
        cgroup_get_controller_next;
        cgroup_get_controller_begin;
+       cgroup_config_unload_config;
        cgroup_get_controller;
        cgroup_get_uid_gid_from_procfs;
 } CGROUP_0.33;



------------------------------------------------------------------------------
Crystal Reports - New Free Runtime and 30 Day Trial
Check out the new simplified licensing option that enables unlimited
royalty-free distribution of the report engine for externally facing 
server and web deployment.
http://p.sf.net/sfu/businessobjects
_______________________________________________
Libcg-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/libcg-devel

Reply via email to