From: Ivana Hutarova Varekova Date: Thu, 2 Sep 2010 10:17:14 +0000 (+0200) Subject: new iterator api for reading variables X-Git-Tag: v0.37.1~43 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=e388d1c10187f9c30ff1f22314647bb9a42b0601;p=thirdparty%2Flibcgroup.git new iterator api for reading variables 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 Signed-off-by: Jan Safranek --- diff --git a/include/libcgroup/iterators.h b/include/libcgroup/iterators.h index f572beca..ed4e6de2 100644 --- a/include/libcgroup/iterators.h +++ b/include/libcgroup/iterators.h @@ -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); + /** * @} * diff --git a/src/api.c b/src/api.c index b9a906f1..1e6e3c3c 100644 --- 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; diff --git a/src/libcgroup.map b/src/libcgroup.map index 130fc768..60970cac 100644 --- a/src/libcgroup.map +++ b/src/libcgroup.map @@ -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;