]> git.ipfire.org Git - thirdparty/libcgroup.git/commitdiff
add api for generating the list of all controllers (not only mount ones)
authorIvana Hutarova Varekova <varekova@redhat.com>
Fri, 17 Jul 2009 07:41:12 +0000 (09:41 +0200)
committerDhaval Giani <dhaval@linux.vnet.ibm.com>
Thu, 6 Aug 2009 06:29:15 +0000 (11:59 +0530)
this patch add api for generating the list of all controllers (not only mount ones)
- it uses three functions:
cgroup_get_all_controller_begin
cgroup_get_all_controller_next
cgroup_get_all_controller_end

and structure:
struct controller_data {
char name[FILENAME_MAX];
int hierarchy;
int num_cgroups;
int enabled;
};

the data are read from /proc/cgroups file

Signed-off-by: Ivana Hutarova Varekova <varekova@redhat.com>
Reviewed-by: Jan Safranek <jsafrane@redhat.com>
Signed-off-by: Dhaval Giani <dhaval@linux.vnet.ibm.com>
include/libcgroup.h
src/api.c
src/libcgroup.map

index d3fad976a7cec0fb2feb3daf4c4952f70b9c114e..b8a3a77364bf854545f1a5517ffe1eb5f81fbd63 100644 (file)
@@ -155,6 +155,25 @@ struct cgroup_mount_point {
        char path[FILENAME_MAX];
 };
 
+/*
+ * Detailed information about available controller.
+ */
+
+struct controller_data {
+/** Controller name. */
+       char name[FILENAME_MAX];
+/**
+ * Hierarchy ID. Controllers with the same hierarchy ID
+ * are mounted together as one hierarchy. Controllers with
+ * ID 0 are not currently used.
+ */
+       int hierarchy;
+/** Number of groups. */
+       int num_cgroups;
+/** Enabled flag */
+       int enabled;
+};
+
 /* Functions and structures that can be used by the application*/
 struct cgroup;
 struct cgroup_controller;
@@ -386,6 +405,20 @@ int cgroup_get_controller_begin(void **handle, struct cgroup_mount_point *info);
 int cgroup_get_controller_next(void **handle, struct cgroup_mount_point *info);
 int cgroup_get_controller_end(void **handle);
 
+/**
+ * Read the list of controllers from /proc/cgroups (not mounted included)
+ * @param handle: Handle to be used for iteration.
+ * @param info: The structure which contains all controller data
+ */
+int cgroup_get_all_controller_begin(void **handle,
+       struct controller_data *info);
+/*
+ * While walking through the mount table, the controllers will be
+ * returned in the same order as is in /proc/cgroups file
+ */
+int cgroup_get_all_controller_next(void **handle, struct controller_data *info);
+int cgroup_get_all_controller_end(void **handle);
+
 /*
  * Reads the mount to table to give the mount point of a controller
  * @controller: Name of the controller
index 727f59416faeaf4c2c04d3fe5d8be89a4672c2a3..e2399ab8f848e0e35c0f0f8ce610d0ff64db39ea 100644 (file)
--- a/src/api.c
+++ b/src/api.c
@@ -3232,3 +3232,79 @@ out_exit:
        pthread_rwlock_unlock(&cg_mount_table_lock);
        return ret;
 }
+
+
+int cgroup_get_all_controller_end(void **handle)
+{
+       FILE *proc_cgroup = (FILE *) *handle;
+
+       if (!cgroup_initialized)
+               return ECGROUPNOTINITIALIZED;
+
+       if (!proc_cgroup)
+               return ECGINVAL;
+
+       fclose(proc_cgroup);
+       *handle = NULL;
+
+       return 0;
+}
+
+
+int cgroup_get_all_controller_next(void **handle, struct controller_data *info)
+{
+       FILE *proc_cgroup = (FILE *) *handle;
+       int err = 0;
+       int hierarchy, num_cgroups, enabled;
+       char subsys_name[FILENAME_MAX];
+
+       if (!cgroup_initialized)
+               return ECGROUPNOTINITIALIZED;
+
+       if (!proc_cgroup)
+               return ECGINVAL;
+
+       if (!info)
+               return ECGINVAL;
+
+       err = fscanf(proc_cgroup, "%s %d %d %d\n", subsys_name,
+                       &hierarchy, &num_cgroups, &enabled);
+
+       if (err != 4)
+               return ECGEOF;
+
+       strncpy(info->name, subsys_name, FILENAME_MAX);
+       info->name[FILENAME_MAX-1] = '\0';
+       info->hierarchy = hierarchy;
+       info->num_cgroups = num_cgroups;
+       info->enabled = enabled;
+
+       return 0;
+}
+
+
+int cgroup_get_all_controller_begin(void **handle, struct controller_data *info)
+{
+       FILE *proc_cgroup = NULL;
+       char buf[FILENAME_MAX];
+
+       if (!cgroup_initialized)
+               return ECGROUPNOTINITIALIZED;
+
+       if (!info)
+               return ECGINVAL;
+
+       proc_cgroup = fopen("/proc/cgroups", "r");
+       if (!proc_cgroup) {
+               last_errno = errno;
+               return ECGOTHER;
+       }
+
+       if (!fgets(buf, FILENAME_MAX, proc_cgroup)) {
+               last_errno = errno;
+               return ECGOTHER;
+       }
+       *handle = proc_cgroup;
+
+       return cgroup_get_all_controller_next(handle, info);
+}
index 66201f2973043e478c76fb53392d074e9edfb53b..87d8a5144d5308d5b328589f25d8b82e38002c55 100644 (file)
@@ -79,4 +79,7 @@ CGROUP_0.35 {
 global:
        create_cgroup_from_name_value_pairs;
        cgroup_delete_cgroup_ext;
+       cgroup_get_all_controller_begin;
+       cgroup_get_all_controller_next;
+       cgroup_get_all_controller_end;
 } CGROUP_0.34;