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 | 61 +++++++++++++++++++++++++++++++++------------------ config.c | 19 ++++++++++++++- libcgroup-internal.h | 4 +-- 3 files changed, 59 insertions(+), 25 deletions(-) Index: trunk/libcgroup-internal.h =================================================================== --- trunk.orig/libcgroup-internal.h 2009-01-02 17:43:39.000000000 +0530 +++ trunk/libcgroup-internal.h 2009-01-02 17:44:44.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 2009-01-02 17:43:39.000000000 +0530 +++ trunk/api.c 2009-01-02 17:46:37.000000000 +0530 @@ -159,7 +159,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); @@ -605,18 +605,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++; } } } @@ -624,13 +632,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; @@ -698,7 +706,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) @@ -754,11 +762,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); @@ -800,7 +813,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; @@ -1426,8 +1439,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; @@ -1457,14 +1468,22 @@ 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; error = rmdir(path); + free(path); + path = NULL; if (error < 0 && errno == ENOENT) error = 0; } @@ -1626,7 +1645,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 @@ -1761,7 +1780,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 2009-01-02 17:43:39.000000000 +0530 +++ trunk/config.c 2009-01-02 17:44:44.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); ------------------------------------------------------------------------------ _______________________________________________ Libcg-devel mailing list [email protected] https://lists.sourceforge.net/lists/listinfo/libcg-devel
