summaryrefslogtreecommitdiffstats
path: root/api.c
diff options
context:
space:
mode:
authorDhaval Giani <dhaval@linux.vnet.ibm.com>2008-06-10 19:17:26 +0000
committerDhaval Giani <dhaval@linux.vnet.ibm.com>2008-06-10 19:17:26 +0000
commitf1bce06975c0c4d6b1e348bbd1cbc26694e57f82 (patch)
tree1c08928a71070164304e94662137d79f3c86cf07 /api.c
parentab944729e147ed15188905a9e196a82db88c7eff (diff)
downloadlibcg-f1bce06975c0c4d6b1e348bbd1cbc26694e57f82.tar.gz
libcg-f1bce06975c0c4d6b1e348bbd1cbc26694e57f82.tar.xz
libcg-f1bce06975c0c4d6b1e348bbd1cbc26694e57f82.zip
Some bugs were missed in v0.1b. Fixing those bugs and tagging
v0.1c. Signed-off-by: Dhaval Giani <dhaval@linux.vnet.ibm.com> git-svn-id: https://libcg.svn.sourceforge.net/svnroot/libcg/tags/v0.1c@76 4f4bb910-9a46-0410-90c8-c897d4f1cd53
Diffstat (limited to 'api.c')
-rw-r--r--api.c66
1 files changed, 60 insertions, 6 deletions
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))