changelog v1 - incorporate Jan's feedback (thanks) adds (fp == NULL) test to cgroup_read_value_end function
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 <[email protected]> --- src/api.c | 79 +++++++++++++++++++++++++++++++++++++++++++++++++++++ src/libcgroup.map | 3 ++ 2 files changed, 82 insertions(+), 0 deletions(-) diff --git a/include/libcgroup/iterators.h b/include/libcgroup/iterators.h index f572bec..ed4e6de 100644 --- a/include/libcgroup/iterators.h +++ b/include/libcgroup/iterators.h @@ -174,6 +174,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); + +/** * @} * * @name Read group stats diff --git a/src/api.c b/src/api.c index 4bd6b46..0fe1871 100644 --- a/src/api.c +++ b/src/api.c @@ -2864,6 +2864,84 @@ 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; + if (fp == NULL) + return 0; + + 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; @@ -2875,6 +2953,7 @@ int cgroup_read_stats_end(void **handle) return ECGINVAL; fp = (FILE *)*handle; + fclose(fp); return 0; } diff --git a/src/libcgroup.map b/src/libcgroup.map index 130fc76..60970ca 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; ------------------------------------------------------------------------------ This SF.net Dev2Dev email is sponsored by: Show off your parallel programming skills. Enter the Intel(R) Threading Challenge 2010. http://p.sf.net/sfu/intel-thread-sfd _______________________________________________ Libcg-devel mailing list [email protected] https://lists.sourceforge.net/lists/listinfo/libcg-devel
