As part of the cleanups let's move to a dynamic arrays.

TODO:
1. Convert the configuration subsystem.
2. More testing

This has been tested by the test cases available till now. Also run
under valgrind. But needs more stringent testing.

Signed-off-by: Dhaval Giani <[EMAIL PROTECTED]>

---
 api.c                |   63 ++++++++++++++++++++++++++++++++++-----------------
 config.c             |   19 +++++++++++++--
 libcgroup-internal.h |    4 +--
 3 files changed, 61 insertions(+), 25 deletions(-)

Index: trunk/libcgroup-internal.h
===================================================================
--- trunk.orig/libcgroup-internal.h     2008-11-22 17:31:48.000000000 +0530
+++ trunk/libcgroup-internal.h  2008-11-29 11:35:22.000000000 +0530
@@ -49,8 +49,8 @@ struct cgroup {
 
 
 struct cg_mount_table_s {
-       char name[FILENAME_MAX];
-       char path[FILENAME_MAX];
+       char *name;
+       char *path;
        int index;
 };
 
Index: trunk/api.c
===================================================================
--- trunk.orig/api.c    2008-11-29 11:24:42.000000000 +0530
+++ trunk/api.c 2008-11-29 11:35:22.000000000 +0530
@@ -122,7 +122,7 @@ static int cgroup_test_subsys_mounted(co
 
        pthread_rwlock_rdlock(&cg_mount_table_lock);
 
-       for (i = 0; cg_mount_table[i].name[0] != '\0'; i++) {
+       for (i = 0; cg_mount_table[i].name; i++) {
                if (strncmp(cg_mount_table[i].name, name,
                                sizeof(cg_mount_table[i].name)) == 0) {
                        pthread_rwlock_unlock(&cg_mount_table_lock);
@@ -568,18 +568,26 @@ int cgroup_init()
 
                                mntopt = strtok_r(mntopt, ",", &strtok_buffer);
 
-                               if (strcmp(mntopt, controllers[i]) == 0) {
-                                       dbg("matched %s:%s\n", mntopt,
-                                               controllers[i]);
-                                       strcpy(cg_mount_table[found_mnt].name,
-                                               controllers[i]);
-                                       strcpy(cg_mount_table[found_mnt].path,
-                                               ent->mnt_dir);
-                                       dbg("Found cgroup option %s, "
-                                               " count %d\n",
-                                               ent->mnt_opts, found_mnt);
-                                       found_mnt++;
+                               if (strcmp(mntopt, controllers[i]))
+                                       continue;
+
+                               dbg("matched %s:%s\n", mntopt, controllers[i]);
+                               cg_mount_table[found_mnt].name =
+                                                       strdup(controllers[i]);
+                               if (!cg_mount_table[found_mnt].name) {
+                                       ret = ECGOTHER;
+                                       goto unlock_exit;
                                }
+                               cg_mount_table[found_mnt].path =
+                                                       strdup(ent->mnt_dir);
+
+                               if (!cg_mount_table[found_mnt].path) {
+                                       ret = ECGOTHER;
+                                       goto unlock_exit;
+                               }
+                               dbg("Found cgroup option %s, count %d\n",
+                                       ent->mnt_opts, found_mnt);
+                               found_mnt++;
                        }
                }
        }
@@ -587,13 +595,13 @@ int cgroup_init()
        free(temp_ent);
 
        if (!found_mnt) {
-               cg_mount_table[0].name[0] = '\0';
+               cg_mount_table[0].name = NULL;
                ret = ECGROUPNOTMOUNTED;
                goto unlock_exit;
        }
 
        found_mnt++;
-       cg_mount_table[found_mnt].name[0] = '\0';
+       cg_mount_table[found_mnt].name = NULL;
 
        fclose(proc_mount);
        cgroup_initialized = 1;
@@ -661,7 +669,7 @@ static char *cg_build_path_var(char *nam
        bool found = false;
        va_list args;
 
-       for (i = 0; cg_mount_table[i].name[0] != '\0'; i++) {
+       for (i = 0; cg_mount_table[i].name; i++) {
                if (strcmp(cg_mount_table[i].name, type) == 0) {
                        found = true;
                        if (name)
@@ -717,11 +725,16 @@ err:
 static char *cg_build_path_locked(char *name, char *path, char *type)
 {
        int i;
-       for (i = 0; cg_mount_table[i].name[0] != '\0'; i++) {
+       for (i = 0; cg_mount_table[i].name; i++) {
                /*
                 * XX: Change to snprintf once you figure what n should be
                 */
                if (strcmp(cg_mount_table[i].name, type) == 0) {
+                       if (!path) {
+                               path = calloc(1, FILENAME_MAX);
+                               if (!path)
+                                       return NULL;
+                       }
                        sprintf(path, "%s/", cg_mount_table[i].path);
                        if (name) {
                                sprintf(path, "%s%s/", path, name);
@@ -736,6 +749,7 @@ char *cg_build_path(char *name, char *pa
 {
        pthread_rwlock_rdlock(&cg_mount_table_lock);
        path = cg_build_path_locked(name, path, type);
+       printf("build_path_locked: path is %s\n", path);
        pthread_rwlock_unlock(&cg_mount_table_lock);
 
        return path;
@@ -763,7 +777,7 @@ int cgroup_attach_task_pid(struct cgroup
        {
                pthread_rwlock_rdlock(&cg_mount_table_lock);
                for(i = 0; i < CG_CONTROLLER_MAX &&
-                               cg_mount_table[i].name[0]!='\0'; i++) {
+                               cg_mount_table[i].name; i++) {
                        if (!cg_build_path_locked(NULL, path,
                                                cg_mount_table[i].name))
                                continue;
@@ -1389,8 +1403,6 @@ int cgroup_delete_cgroup(struct cgroup *
                        continue;
 
                delete_tasks = fopen(path, "r");
-               free(path);
-               path = NULL;
                if (!delete_tasks) {
                        fclose(base_tasks);
                        goto open_err;
@@ -1420,14 +1432,23 @@ int cgroup_delete_cgroup(struct cgroup *
                                        cgroup->controller[i]->name))
                        continue;
                error = rmdir(path);
+               free(path);
+               path = NULL;
        }
 open_err:
        if (ignore_migration) {
                for (i = 0; i < cgroup->index; i++) {
+                       if (path) {
+                               free(path);
+                               path = NULL;
+                       }
                        if (!cg_build_path(cgroup->name, path,
                                                cgroup->controller[i]->name))
                                continue;
+                       printf("delete: path is %s\n", path);
                        error = rmdir(path);
+                       free(path);
+                       path = NULL;
                        if (error < 0 && errno == ENOENT)
                                error = 0;
                }
@@ -1592,7 +1613,7 @@ int cgroup_get_cgroup(struct cgroup *cgr
 
        pthread_rwlock_rdlock(&cg_mount_table_lock);
        for (i = 0; i < CG_CONTROLLER_MAX &&
-                       cg_mount_table[i].name[0] != '\0'; i++) {
+                       cg_mount_table[i].name; i++) {
                /*
                 * cgc will not leak, since it has to be freed using
                 * cgroup_free_cgroup
@@ -1727,7 +1748,7 @@ static int cg_prepare_cgroup(struct cgro
                if (strcmp(controller, "*") == 0) {
                        pthread_rwlock_rdlock(&cg_mount_table_lock);
                        for (i = 0; i < CG_CONTROLLER_MAX &&
-                               cg_mount_table[i].name[0] != '\0'; i++) {
+                               cg_mount_table[i].name; i++) {
                                dbg("Adding controller %s\n",
                                        cg_mount_table[i].name);
                                cptr = cgroup_add_controller(cgroup,
Index: trunk/config.c
===================================================================
--- trunk.orig/config.c 2008-11-10 19:20:23.000000000 +0530
+++ trunk/config.c      2008-11-29 11:55:11.000000000 +0530
@@ -288,6 +288,7 @@ admin_error:
 int cgroup_config_insert_into_mount_table(char *name, char *mount_point)
 {
        int i;
+       int ret;
 
        if (config_table_index >= CG_CONTROLLER_MAX)
                return 0;
@@ -307,8 +308,22 @@ int cgroup_config_insert_into_mount_tabl
                }
        }
 
-       strcpy(config_mount_table[config_table_index].name, name);
-       strcpy(config_mount_table[config_table_index].path, mount_point);
+       ret = asprintf(&config_mount_table[config_table_index].name, "%s",
+                                                               name);
+
+       if (ret < 0) {
+               fprintf(stderr, "Failed to allocate mount table name\n");
+               return 0;
+       }
+
+       ret = asprintf(&config_mount_table[config_table_index].path, "%s",
+                                                               mount_point);
+
+       if (ret < 0) {
+               fprintf(stderr, "Failed to allocated ount table path\n");
+               return 0;
+       }
+
        config_table_index++;
 done:
        pthread_rwlock_unlock(&config_table_lock);

--
regards,
Dhaval


-------------------------------------------------------------------------
This SF.Net email is sponsored by the Moblin Your Move Developer's challenge
Build the coolest Linux based applications with Moblin SDK & win great prizes
Grand prize is a trip for two to an Open Source event anywhere in the world
http://moblin-contest.org/redirect.php?banner_id=100&url=/
_______________________________________________
Libcg-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/libcg-devel

Reply via email to