From f1bce06975c0c4d6b1e348bbd1cbc26694e57f82 Mon Sep 17 00:00:00 2001 From: Dhaval Giani Date: Tue, 10 Jun 2008 19:17:26 +0000 Subject: Some bugs were missed in v0.1b. Fixing those bugs and tagging v0.1c. Signed-off-by: Dhaval Giani git-svn-id: https://libcg.svn.sourceforge.net/svnroot/libcg/tags/v0.1c@76 4f4bb910-9a46-0410-90c8-c897d4f1cd53 --- api.c | 66 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 60 insertions(+), 6 deletions(-) (limited to 'api.c') diff --git a/api.c b/api.c index a279631..6250ceb 100644 --- a/api.c +++ b/api.c @@ -48,6 +48,9 @@ const static char cg_version[] = VERSION(PACKAGE_VERSION); struct cg_mount_table_s cg_mount_table[CG_CONTROLLER_MAX]; +/* Check if cgroup_init has been called or not. */ +static int cgroup_initialized; + static int cg_chown_file(FTS *fts, FTSENT *ent, uid_t owner, gid_t group) { int ret = 0; @@ -93,6 +96,19 @@ static int cg_chown_recursive(char **path, uid_t owner, gid_t group) return ret; } +static int cgroup_test_subsys_mounted(const char *name) +{ + int i; + + for (i = 0; cg_mount_table[i].name[0] != '\0'; i++) { + if (strncmp(cg_mount_table[i].name, name, + sizeof(cg_mount_table[i].name)) == 0) { + return 1; + } + } + return 0; +} + /** * cgroup_init(), initializes the MOUNT_POINT. * This code is not currently thread safe (hint: getmntent is not thread safe). @@ -152,6 +168,7 @@ int cgroup_init() if (!strncmp(ent->mnt_type, "cgroup", strlen("cgroup"))) { for (i = 0; controllers[i] != NULL; i++) { mntopt = hasmntopt(ent, controllers[i]); + mntopt = strtok(mntopt, ","); if (mntopt && strcmp(mntopt, controllers[i]) == 0) { dbg("matched %s:%s\n", mntopt, @@ -177,8 +194,8 @@ int cgroup_init() found_mnt++; cg_mount_table[found_mnt].name[0] = '\0'; - fclose(proc_mount); + cgroup_initialized = 1; return ret; } @@ -214,8 +231,10 @@ static char* cg_build_path(char *name, char *path, char *type) if (strcmp(cg_mount_table[i].name, type) == 0) { strcpy(path, cg_mount_table[i].path); strcat(path, "/"); - strcat(path, name); - strcat(path, "/"); + if (name) { + strcat(path, name); + strcat(path, "/"); + } return path; } } @@ -236,11 +255,14 @@ int cgroup_attach_task_pid(struct cgroup *cgroup, pid_t tid) FILE *tasks; int i; + if (!cgroup_initialized) + return ECGROUPNOTINITALIZED; + if(!cgroup) { for(i = 0; i < CG_CONTROLLER_MAX && cg_mount_table[i].name[0]!='\0'; i++) { - if (!cg_build_path(cgroup->name, path, NULL)) + if (!cg_build_path(NULL, path, cg_mount_table[i].name)) continue; strcat(path, "/tasks"); @@ -257,7 +279,12 @@ int cgroup_attach_task_pid(struct cgroup *cgroup, pid_t tid) fclose(tasks); } } else { - for( i = 0; i <= CG_CONTROLLER_MAX && + for (i = 0; i <= CG_CONTROLLER_MAX && + cgroup->controller[i] != NULL; i++) { + if (!cgroup_test_subsys_mounted(cgroup->controller[i]->name)) + return ECGROUPSUBSYSNOTMOUNTED; + } + for (i = 0; i <= CG_CONTROLLER_MAX && cgroup->controller[i] != NULL ; i++) { if (!cg_build_path(cgroup->name, path, cgroup->controller[i]->name)) @@ -382,6 +409,15 @@ int cgroup_modify_cgroup(struct cgroup *cgroup) int i; int error; + if (!cgroup_initialized) + return ECGROUPNOTINITALIZED; + + for (i = 0; i <= CG_CONTROLLER_MAX && cgroup->controller[i] != NULL; + 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)) { int j; @@ -413,9 +449,18 @@ err: int cgroup_create_cgroup(struct cgroup *cgroup, int ignore_ownership) { char *fts_path[2], base[FILENAME_MAX], *path; - int j, k; + int i, j, k; int error = 0; + if (!cgroup_initialized) + return ECGROUPNOTINITALIZED; + + for (i = 0; i <= CG_CONTROLLER_MAX && cgroup->controller[i] != NULL; + i++) { + if (!cgroup_test_subsys_mounted(cgroup->controller[i]->name)) + return ECGROUPSUBSYSNOTMOUNTED; + } + fts_path[0] = (char *)malloc(FILENAME_MAX); if (!fts_path[0]) return ENOMEM; @@ -490,6 +535,15 @@ int cgroup_delete_cgroup(struct cgroup *cgroup, int ignore_migration) int error = ECGROUPNOTALLOWED; int i, ret; + if (!cgroup_initialized) + return ECGROUPNOTINITALIZED; + + for (i = 0; i <= CG_CONTROLLER_MAX && cgroup->controller[i] != NULL; + i++) { + if (!cgroup_test_subsys_mounted(cgroup->controller[i]->name)) + return ECGROUPSUBSYSNOTMOUNTED; + } + for (i = 0; i < CG_CONTROLLER_MAX && cgroup->controller; i++) { if (!cg_build_path(cgroup->name, path, cgroup->controller[i]->name)) -- cgit