]> git.ipfire.org Git - thirdparty/libcgroup.git/commitdiff
libcgroup: Don't fail cgroup_get_cgroup if fscanf fails
authorDhaval Giani <dhaval@linux.vnet.ibm.com>
Mon, 29 Dec 2008 07:05:54 +0000 (07:05 +0000)
committerDhaval Giani <dhaval@linux.vnet.ibm.com>
Mon, 29 Dec 2008 07:05:54 +0000 (07:05 +0000)
cgroup_get_cgroup was failing with the memory controller enabled. It was
failing on memory.force_empty. The file has read permissions, but there
is no read routine associated with it inside the kernel. fscanf failed
and so cgroup_get_cgroup also failed. This was unexpected,
cgroup_get_cgroup should have just skipped the file. In order to fix
this, change cg_rd_ctrl_file to return more descriptive errors.

Signed-off-by: Dhaval Giani <dhaval@linux.vnet.ibm.com>
Acked-by: Balbir Singh <balbir@linux.vnet.ibm.com>
git-svn-id: https://libcg.svn.sourceforge.net/svnroot/libcg/trunk@293 4f4bb910-9a46-0410-90c8-c897d4f1cd53

api.c

diff --git a/api.c b/api.c
index 0a65270f5dfd5333bcd3be42e6415f49445eeb0f..8867b442de53bbafb5094283f55e79ef04f3b788 100644 (file)
--- a/api.c
+++ b/api.c
@@ -1371,38 +1371,37 @@ open_err:
  * will assume that the callers have taken care of everything.
  * Including the locking.
  */
-static char *cg_rd_ctrl_file(char *subsys, char *cgroup, char *file)
+static int cg_rd_ctrl_file(char *subsys, char *cgroup, char *file, char **value)
 {
-       char *value;
        char path[FILENAME_MAX];
        FILE *ctrl_file;
        int ret;
 
        if (!cg_build_path_locked(cgroup, path, subsys))
-               return NULL;
+               return ECGFAIL;
 
        strcat(path, file);
        ctrl_file = fopen(path, "r");
        if (!ctrl_file)
-               return NULL;
+               return ECGROUPVALUENOTEXIST;
 
-       value = malloc(CG_VALUE_MAX);
-       if (!value)
-               return NULL;
+       *value = malloc(CG_VALUE_MAX);
+       if (!*value)
+               return ECGOTHER;
 
        /*
         * using %as crashes when we try to read from files like
         * memory.stat
         */
-       ret = fscanf(ctrl_file, "%s", value);
+       ret = fscanf(ctrl_file, "%s", *value);
        if (ret == 0 || ret == EOF) {
-               free(value);
-               value = NULL;
+               free(*value);
+               *value = NULL;
        }
 
        fclose(ctrl_file);
 
-       return value;
+       return 0;
 }
 
 /*
@@ -1461,12 +1460,10 @@ static int cgroup_fill_cgc(struct dirent *ctrl_dir, struct cgroup *cgroup,
        }
 
        if (strcmp(ctrl_name, cg_mount_table[index].name) == 0) {
-               ctrl_value = cg_rd_ctrl_file(cg_mount_table[index].name,
-                               cgroup->name, ctrl_dir->d_name);
-               if (!ctrl_value) {
-                       error = ECGFAIL;
+               error = cg_rd_ctrl_file(cg_mount_table[index].name,
+                               cgroup->name, ctrl_dir->d_name, &ctrl_value);
+               if (error || !ctrl_value)
                        goto fill_error;
-               }
 
                if (cgroup_add_value_string(cgc, ctrl_dir->d_name,
                                ctrl_value)) {