]> git.ipfire.org Git - thirdparty/libcgroup.git/commitdiff
libcgroup: Fix segmentation fault in APIs.
authorDhaval Giani <dhaval@linux.vnet.ibm.com>
Wed, 11 Jun 2008 08:20:54 +0000 (08:20 +0000)
committerDhaval Giani <dhaval@linux.vnet.ibm.com>
Wed, 11 Jun 2008 08:20:54 +0000 (08:20 +0000)
Dan Smith reported some segmentation faults. It turns out that we did
not NULL terminate all the structures when we had moved to the wrapper
based approach. With this in mind, the loop checks the index instead
of a NULL structure.

Also remove unwanted NULL terminations.

Thanks to Dan for excellent debugging.

Signed-off-by: Dhaval Giani <dhaval@linux.vnet.ibm.com>
git-svn-id: https://libcg.svn.sourceforge.net/svnroot/libcg/tags/v0.1c@77 4f4bb910-9a46-0410-90c8-c897d4f1cd53

api.c
wrapper.c

diff --git a/api.c b/api.c
index 6250cebcc003e940634bf3a01b059d6885491b3d..0adeeb551856a0d69649aefc0f745618047e65ff 100644 (file)
--- a/api.c
+++ b/api.c
@@ -279,13 +279,11 @@ int cgroup_attach_task_pid(struct cgroup *cgroup, pid_t tid)
                        fclose(tasks);
                }
        } else {
-               for (i = 0; i <= CG_CONTROLLER_MAX &&
-                               cgroup->controller[i] != NULL; i++) {
+               for (i = 0; i < cgroup->index; i++) {
                        if (!cgroup_test_subsys_mounted(cgroup->controller[i]->name))
                                return ECGROUPSUBSYSNOTMOUNTED;
                }
-               for (i = 0; i <= CG_CONTROLLER_MAX &&
-                               cgroup->controller[i] != NULL ; i++) {
+               for (i = 0; i <= cgroup->index; i++) {
                        if (!cg_build_path(cgroup->name, path,
                                        cgroup->controller[i]->name))
                                continue;
@@ -412,21 +410,18 @@ int cgroup_modify_cgroup(struct cgroup *cgroup)
        if (!cgroup_initialized)
                return ECGROUPNOTINITALIZED;
 
-       for (i = 0; i <= CG_CONTROLLER_MAX && cgroup->controller[i] != NULL;
-                                                                       i++) {
+       for (i = 0; i < cgroup->index; i++) {
                if (!cgroup_test_subsys_mounted(cgroup->controller[i]->name))
                        return ECGROUPSUBSYSNOTMOUNTED;
        }
 
-       for (i = 0; i < CG_CONTROLLER_MAX && cgroup->controller[i];
-                                               i++, strcpy(path, base)) {
+       for (i = 0; i < cgroup->index; i++, strcpy(path, base)) {
                int j;
                if (!cg_build_path(cgroup->name, base,
                        cgroup->controller[i]->name))
                        continue;
-               for(j = 0; j < CG_NV_MAX &&
-                       cgroup->controller[i]->values[j];
-                       j++, strcpy(path, base)) {
+               for (j = 0; j < cgroup->controller[i]->index; j++,
+                                               strcpy(path, base)) {
                        strcat(path, cgroup->controller[i]->values[j]->name);
                        error = cg_set_control_value(path,
                                cgroup->controller[i]->values[j]->value);
@@ -455,8 +450,7 @@ int cgroup_create_cgroup(struct cgroup *cgroup, int ignore_ownership)
        if (!cgroup_initialized)
                return ECGROUPNOTINITALIZED;
 
-       for (i = 0; i <= CG_CONTROLLER_MAX && cgroup->controller[i] != NULL;
-                                                                       i++) {
+       for (i = 0; i < cgroup->index;  i++) {
                if (!cgroup_test_subsys_mounted(cgroup->controller[i]->name))
                        return ECGROUPSUBSYSNOTMOUNTED;
        }
@@ -472,7 +466,7 @@ int cgroup_create_cgroup(struct cgroup *cgroup, int ignore_ownership)
         * subsystems mounted at one point, all of them *have* be on the cgroup
         * data structure. If not, we fail.
         */
-       for (k = 0; k < CG_CONTROLLER_MAX && cgroup->controller[k]; k++) {
+       for (k = 0; k < cgroup->index; k++) {
                path[0] = '\0';
 
                if (!cg_build_path(cgroup->name, path,
@@ -493,8 +487,8 @@ int cgroup_create_cgroup(struct cgroup *cgroup, int ignore_ownership)
                if (error)
                        goto err;
 
-               for (j = 0; j < CG_NV_MAX && cgroup->controller[k]->values[j];
-                                       j++, strcpy(path, base)) {
+               for (j = 0; j < cgroup->controller[k]->index; j++,
+                                                       strcpy(path, base)) {
                        strcat(path, cgroup->controller[k]->values[j]->name);
                        error = cg_set_control_value(path,
                                cgroup->controller[k]->values[j]->value);
@@ -538,13 +532,12 @@ int cgroup_delete_cgroup(struct cgroup *cgroup, int ignore_migration)
        if (!cgroup_initialized)
                return ECGROUPNOTINITALIZED;
 
-       for (i = 0; i <= CG_CONTROLLER_MAX && cgroup->controller[i] != NULL;
-                                                                       i++) {
+       for (i = 0; i < cgroup->index; i++) {
                if (!cgroup_test_subsys_mounted(cgroup->controller[i]->name))
                        return ECGROUPSUBSYSNOTMOUNTED;
        }
 
-       for (i = 0; i < CG_CONTROLLER_MAX && cgroup->controller; i++) {
+       for (i = 0; i < cgroup->index; i++) {
                if (!cg_build_path(cgroup->name, path,
                                        cgroup->controller[i]->name))
                        continue;
@@ -585,7 +578,7 @@ del_open_err:
                fclose(base_tasks);
 base_open_err:
        if (ignore_migration) {
-               for (i = 0; cgroup->controller[i] != NULL; i++) {
+               for (i = 0; i < cgroup->index; i++) {
                        if (!cg_build_path(cgroup->name, path,
                                                cgroup->controller[i]->name))
                                continue;
index 916710ffb3f9d3be223ba753761b111284302a79..350319413460868ada1e1d4f306164627c38f031 100644 (file)
--- a/wrapper.c
+++ b/wrapper.c
@@ -37,7 +37,6 @@ struct cgroup *cgroup_new_cgroup(const char *name, uid_t tasks_uid,
        cgroup->control_uid = control_uid;
        cgroup->control_gid = control_gid;
        cgroup->index = 0;
-       cgroup->controller[cgroup->index] = NULL;
 
        return cgroup;
 }
@@ -71,7 +70,6 @@ struct cgroup_controller *cgroup_add_controller(struct cgroup *cgroup,
 
        strncpy(controller->name, name, sizeof(controller->name));
        controller->index = 0;
-       controller->values[controller->index] = NULL;
 
        cgroup->controller[cgroup->index] = controller;
        cgroup->index++;