summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJan Safranek <jsafrane@redhat.com>2009-12-22 09:16:48 +0100
committerJan Safranek <jsafrane@redhat.com>2009-12-22 09:16:48 +0100
commit902795e9a894bd6d5836c0e473bdafe5669a7dc1 (patch)
treeb73b4c683886be8b5fde0545ffb1b44fa6206579
parenta875ed35ec110a68c02b95762547f1989e2c6fb6 (diff)
downloadlibcg-902795e9a894bd6d5836c0e473bdafe5669a7dc1.tar.gz
libcg-902795e9a894bd6d5836c0e473bdafe5669a7dc1.tar.xz
libcg-902795e9a894bd6d5836c0e473bdafe5669a7dc1.zip
Fix reading of empty parameter values
When a group parameter has empty value (like uninitialized cpuset.cpus), libcgroup does not return this parameter value - it returns ECGROUPVALUENOTEXIST instead. I think reading whole parameter file instead of just '%s' is the right thing to do - it helps also with multiline values, like cpuacct.stat. Signed-off-by: Jan Safranek <jsafrane@redhat.com>
-rw-r--r--src/api.c10
1 files changed, 7 insertions, 3 deletions
diff --git a/src/api.c b/src/api.c
index 8cb3dcc..863df33 100644
--- a/src/api.c
+++ b/src/api.c
@@ -1775,7 +1775,7 @@ static int cg_rd_ctrl_file(char *subsys, char *cgroup, char *file, char **value)
if (!ctrl_file)
return ECGROUPVALUENOTEXIST;
- *value = malloc(CG_VALUE_MAX);
+ *value = calloc(CG_VALUE_MAX, 1);
if (!*value) {
last_errno = errno;
return ECGOTHER;
@@ -1785,10 +1785,14 @@ static int cg_rd_ctrl_file(char *subsys, char *cgroup, char *file, char **value)
* using %as crashes when we try to read from files like
* memory.stat
*/
- ret = fscanf(ctrl_file, "%s", *value);
- if (ret == 0 || ret == EOF) {
+ ret = fread(*value, 1, CG_VALUE_MAX-1, ctrl_file);
+ if (ret < 0) {
free(*value);
*value = NULL;
+ } else {
+ /* remove trailing \n */
+ if (ret > 0 && (*value)[ret-1] == '\n')
+ (*value)[ret-1] = '\0';
}
fclose(ctrl_file);