diff options
author | Dhaval Giani <dhaval@linux.vnet.ibm.com> | 2008-07-01 14:05:09 +0000 |
---|---|---|
committer | Dhaval Giani <dhaval@linux.vnet.ibm.com> | 2008-07-01 14:05:09 +0000 |
commit | fd13e9f6d4f315018fc88258ba965c32e12601da (patch) | |
tree | cf50097167492756530eb25c89904c4d550f2475 /tests/libcgrouptest01.c | |
parent | 4f1301efe29a3a37470d10a4bb6a497f2e532be5 (diff) | |
download | libcg-fd13e9f6d4f315018fc88258ba965c32e12601da.tar.gz libcg-fd13e9f6d4f315018fc88258ba965c32e12601da.tar.xz libcg-fd13e9f6d4f315018fc88258ba965c32e12601da.zip |
From: Sudhir Kumar <skumar@linux.vnet.ibm.com>
libcgroup: put common code in a function
This patch puts the common code for creating cgroup structure into
a function struct cgroup *new_cgroup(). The function uses few global
variables which are changed before the function call and takes few
variables as arguments.
Signed-off-by: Sudhir Kumar <skumar@linux.vnet.ibm.com>
Signed-off-by: Dhaval Giani <dhaval@linux.vnet.ibm.com>
git-svn-id: https://libcg.svn.sourceforge.net/svnroot/libcg/trunk@93 4f4bb910-9a46-0410-90c8-c897d4f1cd53
Diffstat (limited to 'tests/libcgrouptest01.c')
-rw-r--r-- | tests/libcgrouptest01.c | 141 |
1 files changed, 77 insertions, 64 deletions
diff --git a/tests/libcgrouptest01.c b/tests/libcgrouptest01.c index 788ecbf..03cb51c 100644 --- a/tests/libcgrouptest01.c +++ b/tests/libcgrouptest01.c @@ -18,13 +18,11 @@ int main(int argc, char *argv[]) { - int fs_mounted, retval, i = 0, pass = 0; + int fs_mounted, retval, pass = 0; pid_t curr_tid, tid; struct cgroup *cgroup1, *cgroup2, *nullcgroup = NULL; - struct cgroup_controller *controller1, *controller2; char controller_name[FILENAME_MAX], control_file[FILENAME_MAX], - control_val[FILENAME_MAX], path_group[FILENAME_MAX], - path_control_file[FILENAME_MAX]; + path_group[FILENAME_MAX], path_control_file[FILENAME_MAX]; FILE *file; char mountpoint[FILENAME_MAX], tasksfile[FILENAME_MAX], group[FILENAME_MAX]; @@ -49,6 +47,12 @@ int main(int argc, char *argv[]) exit(1); } + /* Set default control permissions */ + control_uid = 0; + control_gid = 0; + tasks_uid = 0; + tasks_gid = 0; + /* * Testsets: Testcases are broadly devided into 3 categories based on * filesystem(fs) mount scenario. fs not mounted, fs mounted, fs multi @@ -90,27 +94,14 @@ int main(int argc, char *argv[]) */ strncpy(group, "group1", sizeof(group)); - retval = set_controller(MEMORY, controller_name, - control_file, control_val, "40960000"); + retval = set_controller(MEMORY, controller_name, control_file); + strncpy(val_string, "40960000", sizeof(val_string)); + if (retval) fprintf(stderr, "Setting controller failled\n"); - cgroup1 = cgroup_new_cgroup(group, 0, 0, 0, 0); - if (cgroup1) { - controller1 = cgroup_add_controller(cgroup1, controller_name); - if (controller1) { - retval = cgroup_add_value_string(controller1, - control_file, control_val); - if (!retval) - printf("Test[0:%2d]\tPASS: cgroup_new_cgroup() success\n", ++i); - else - printf("Test[0:%2d]\tFAIL: cgroup_add_value_string()\n", ++i); - } - else - printf("Test[0:%2d]\tFAIL: cgroup_add_controller()\n", ++i); - } - else - printf("Test[0:%2d]\tFAIL: cgroup_new_cgroup() fails\n", ++i); + cgroup1 = new_cgroup(group, controller_name, + control_file, STRING); /* * Test04: Then Call cgroup_create_cgroup() with this valid group @@ -239,27 +230,14 @@ int main(int argc, char *argv[]) * Exp outcome: no error. 0 return value */ strncpy(group, "group1", sizeof(group)); - retval = set_controller(MEMORY, controller_name, - control_file, control_val, "40960000"); + retval = set_controller(MEMORY, controller_name, control_file); + strncpy(val_string, "40960000", sizeof(val_string)); + if (retval) fprintf(stderr, "Setting controller failled\n"); - cgroup1 = cgroup_new_cgroup(group, 0, 0, 0, 0); - if (cgroup1) { - controller1 = cgroup_add_controller(cgroup1, controller_name); - if (controller1) { - retval = cgroup_add_value_string(controller1, - control_file, control_val); - if (!retval) - printf("Test[1:%2d]\tPASS: cgroup_new_cgroup() success\n", ++i); - else - printf("Test[1:%2d]\tFAIL: cgroup_add_value_string()\n", ++i); - } - else - printf("Test[1:%2d]\tFAIL: cgroup_add_controller()\n", ++i); - } - else - printf("Test[1:%2d]\tFAIL: cgroup_new_cgroup() fails\n", ++i); + cgroup1 = new_cgroup(group, controller_name, + control_file, STRING); /* * Test06: Then Call cgroup_create_cgroup() with this valid group @@ -281,28 +259,14 @@ int main(int argc, char *argv[]) * Exp outcome: no error. 0 return value */ strncpy(group, "group1", sizeof(group)); - retval = set_controller(MEMORY, controller_name, - control_file, control_val, "81920000"); + retval = set_controller(MEMORY, controller_name, control_file); + strncpy(val_string, "81920000", sizeof(val_string)); + if (retval) fprintf(stderr, "Setting controller failled\n"); - - cgroup2 = cgroup_new_cgroup(group, 0, 0, 0, 0); - if (cgroup2) { - controller2 = cgroup_add_controller(cgroup2, controller_name); - if (controller2) { - retval = cgroup_add_value_string(controller2, - control_file, control_val); - if (!retval) - printf("Test[1:%2d]\tPASS: cgroup_new_cgroup() success\n", ++i); - else - printf("Test[1:%2d]\tFAIL: cgroup_add_value_string()\n", ++i); - } - else - printf("Test[1:%2d]\tFAIL: cgroup_add_controller()\n", ++i); - } - else - printf("Test[1:%2d]\tFAIL: cgroup_new_cgroup() fails\n", ++i); + cgroup2 = new_cgroup(group, controller_name, + control_file, STRING); /* * Test07: modify cgroup @@ -313,8 +277,6 @@ int main(int argc, char *argv[]) strncat(path_control_file, "/", sizeof("/")); strncat(path_control_file, control_file, sizeof(control_file)); - strncpy(val_string, "81920000", sizeof(val_string)); - retval = cgroup_modify_cgroup(cgroup2); /* Check if the values are changed */ if (!retval && !group_modified(path_control_file, STRING)) @@ -421,7 +383,7 @@ static int group_exist(char *path_group) } static int set_controller(int controller, char *controller_name, - char *control_file, char *control_val, char *value) + char *control_file) { switch (controller) { case MEMORY: @@ -430,7 +392,6 @@ static int set_controller(int controller, char *controller_name, strncpy(controller_name, "memory", FILENAME_MAX); strncpy(control_file, "memory.limit_in_bytes", FILENAME_MAX); - strncpy(control_val, value, FILENAME_MAX); return 0; break; @@ -440,7 +401,6 @@ static int set_controller(int controller, char *controller_name, strncpy(controller_name, "cpu", FILENAME_MAX); strncpy(control_file, "cpu.shares", FILENAME_MAX); - strncpy(control_val, value, FILENAME_MAX); return 0; break; /* Future controllers can be added here */ @@ -496,3 +456,56 @@ static int group_modified(char *path_control_file, int value_type) } return 1; } + +struct cgroup *new_cgroup(char *group, char *controller_name, + char *control_file, int value_type) +{ + int retval; + struct cgroup *newcgroup; + struct cgroup_controller *newcontroller; + newcgroup = cgroup_new_cgroup(group, tasks_uid, tasks_gid, + control_uid, control_gid); + + if (newcgroup) { + newcontroller = cgroup_add_controller(newcgroup, controller_name); + if (newcontroller) { + switch (value_type) { + + case BOOL: + retval = cgroup_add_value_bool(newcontroller, + control_file, val_bool); + break; + case INT64: + retval = cgroup_add_value_int64(newcontroller, + control_file, val_int64); + break; + case UINT64: + retval = cgroup_add_value_uint64(newcontroller, + control_file, val_uint64); + break; + case STRING: + retval = cgroup_add_value_string(newcontroller, + control_file, val_string); + break; + default: + printf("ERROR: wrong value in new_cgroup()\n"); + return NULL; + break; + } + + if (!retval) { + printf("Test[1:%2d]\tPASS: cgroup_new_cgroup() success\n", ++i); + } else { + printf("Test[1:%2d]\tFAIL: cgroup_add_value_string()\n", ++i); + return NULL; + } + } else { + printf("Test[1:%2d]\tFAIL: cgroup_add_controller()\n", ++i); + return NULL; + } + } else { + printf("Test[1:%2d]\tFAIL: cgroup_new_cgroup() fails\n", ++i); + return NULL; + } + return newcgroup; +} |