]> git.ipfire.org Git - thirdparty/libcgroup.git/commitdiff
new iterator api for reading variables
authorIvana Hutarova Varekova <varekova@redhat.com>
Thu, 2 Sep 2010 10:17:14 +0000 (12:17 +0200)
committerJan Safranek <jsafrane@redhat.com>
Fri, 17 Sep 2010 08:07:08 +0000 (10:07 +0200)
This patch adds three iterators api function
cgroup_read_vars_begin
cgroup_read_vars_next
cgroup_read_vars_end

 They read the values of the given variable for the specified
 controller and control group. The string with the maximal
 length max is returned line is returned per
 cgroup_read_stats_begin() and cgroup_read_stats_next() call.

(there already are _stat_ versions of function which can read only stat
variables)

Signed-off-by: Ivana Hutarova Varekova <varekova@redhat.com>
Signed-off-by: Jan Safranek <jsafrane@redhat.com>
include/libcgroup/iterators.h
src/api.c
src/libcgroup.map

index f572becabc1767f2373bdf09193f31751a697f20..ed4e6de22d152df27fb186029d5e98a0ef5d3994 100644 (file)
@@ -173,6 +173,44 @@ int cgroup_walk_tree_end(void **handle);
  */
 int cgroup_walk_tree_set_flags(void **handle, int flags);
 
+/**
+ * Read the value of the given variable for the specified
+ * controller and control group.
+ * The value is read up to newline character or at most max-1 characters,
+ * whichever comes first (i.e. similar to fgets()).
+ * @param controller Name of the controller for which stats are requested.
+ * @param path Path to control group, relative to hierarchy root.
+ * @param name is variable name.
+ * @param handle Handle to be used during iteration.
+ * @param buffer Buffer to read the value into.
+ * The buffer is always zero-terminated.
+ * @param max Maximal lenght of the buffer
+ * @return #ECGEOF when the stats file is empty.
+ */
+
+int cgroup_read_value_begin(const char *controller, const char *path,
+       char *name, void **handle, char *buffer, int max);
+
+/**
+  * Read the next string from the given variable handle
+  * which is generated by cgroup_read_stats_begin() function.
+  * the value is read up to newline character or at most max-1 characters,
+  * whichever comes first (i.e. similar to fgets()) per
+  * cgroup_read_stats_next() call
+  * @param handle Handle to be used during iteration.
+  * @param data returned the string.
+  * @param buffer Buffer to read the value into.
+  * The buffer is always zero-terminated.
+  * @param max Maximal lenght of the buffer
+  * @return #ECGEOF when the iterator finishes getting the list of stats.
+  */
+int cgroup_read_value_next(void **handle, char *buffer, int max);
+
+/**
+ * Release the iterator.
+ */
+int cgroup_read_value_end(void **handle);
+
 /**
  * @}
  *
index b9a906f179e4ac27185a8b7541e7a71a545a9719..1e6e3c3ce21ec016d4ec2c8e0137556168bf971a 100644 (file)
--- a/src/api.c
+++ b/src/api.c
@@ -2830,6 +2830,81 @@ out_free:
        return ret;
 }
 
+
+int cgroup_read_value_end(void **handle)
+{
+       FILE *fp;
+
+       if (!cgroup_initialized)
+               return ECGROUPNOTINITIALIZED;
+
+       if (!handle)
+               return ECGINVAL;
+
+       fp = (FILE *)*handle;
+       fclose(fp);
+
+       return 0;
+}
+
+int cgroup_read_value_next(void **handle, char *buffer, int max)
+{
+       int ret = 0;
+       char *ret_c;
+       FILE *fp;
+
+       if (!cgroup_initialized)
+               return ECGROUPNOTINITIALIZED;
+
+       if (!buffer || !handle)
+               return ECGINVAL;
+
+       fp = (FILE *)*handle;
+       ret_c = fgets(buffer, max, fp);
+       if (ret_c == NULL)
+               ret = ECGEOF;
+
+       return ret;
+}
+
+int cgroup_read_value_begin(const char *controller, const char *path,
+       char *name, void **handle, char *buffer, int max)
+{
+       int ret = 0;
+       char *ret_c = NULL;
+       char stat_file[FILENAME_MAX];
+       char stat_path[FILENAME_MAX];
+       FILE *fp;
+
+       if (!cgroup_initialized)
+               return ECGROUPNOTINITIALIZED;
+
+       if (!buffer || !handle)
+               return ECGINVAL;
+
+       if (!cg_build_path(path, stat_path, controller))
+               return ECGOTHER;
+
+       snprintf(stat_file, sizeof(stat_file), "%s/%s", stat_path,
+               name);
+       fp = fopen(stat_file, "re");
+       if (!fp) {
+               cgroup_dbg("fopen failed\n");
+               last_errno = errno;
+               *handle = NULL;
+               return ECGOTHER;
+       }
+
+       ret_c = fgets(buffer, max, fp);
+       if (ret_c == NULL)
+               ret = ECGEOF;
+
+       *handle = fp;
+       return 0;
+}
+
+
+
 int cgroup_read_stats_end(void **handle)
 {
        FILE *fp;
index 130fc768216c84edf72f988da87a8f868052ac35..60970cacb512f6880bcd5ad1949cac585cb51b8f 100644 (file)
@@ -91,4 +91,7 @@ CGROUP_0.36 {
 
 CGROUP_0.37 {
        cgroup_get_procs;
+       cgroup_read_value_begin;
+       cgroup_read_value_next;
+       cgroup_read_value_end;
 } CGROUP_0.36;