summaryrefslogtreecommitdiffstats
path: root/tests/libcg_ba.cpp
diff options
context:
space:
mode:
authorBalbir Singh <balbir@linux.vnet.ibm.com>2008-04-11 19:38:06 +0000
committerBalbir Singh <balbir@linux.vnet.ibm.com>2008-04-11 19:38:06 +0000
commit2be6ec85fb430e17afcf16b0ab84e4f966920472 (patch)
treeb0faddbaf23aa2a2fe68042ebac80e0c9b4a3833 /tests/libcg_ba.cpp
parent31f116d4e2842da4929e7bb7e6187c3d9259422c (diff)
downloadlibcg-2be6ec85fb430e17afcf16b0ab84e4f966920472.tar.gz
libcg-2be6ec85fb430e17afcf16b0ab84e4f966920472.tar.xz
libcg-2be6ec85fb430e17afcf16b0ab84e4f966920472.zip
Several bug fixes, some API enhancements.
The first basic acceptance test is under development and first prototype is released api.c | 161 +++++++++++++++++++++++++++++++++++++++++++---------- libcg.h | 14 +--- tests/Makefile | 9 ++ tests/libcg_ba.cpp | 127 +++++++++++++++++++++++++++++++++++++++++ 4 files changed, 274 insertions(+), 37 deletions(-) Signed-off-by: Balbir Singh <balbir@linux.vnet.ibm.com> git-svn-id: https://libcg.svn.sourceforge.net/svnroot/libcg/branches/balbir@12 4f4bb910-9a46-0410-90c8-c897d4f1cd53
Diffstat (limited to 'tests/libcg_ba.cpp')
-rw-r--r--tests/libcg_ba.cpp127
1 files changed, 127 insertions, 0 deletions
diff --git a/tests/libcg_ba.cpp b/tests/libcg_ba.cpp
new file mode 100644
index 0000000..873f12f
--- /dev/null
+++ b/tests/libcg_ba.cpp
@@ -0,0 +1,127 @@
+/*
+ * Copyright IBM Corporation. 2007
+ *
+ * Author: Balbir Singh <balbir@linux.vnet.ibm.com>
+ *
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms of version 2.1 of the GNU Lesser General Public License
+ * as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it would be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
+ *
+ * Basic acceptance test for libcg - Written one late night by Balbir Singh
+ */
+using namespace std;
+
+#include <string>
+#include <stdexcept>
+#include <iostream>
+#include <libcg.h>
+#include <sys/types.h>
+#include <pwd.h>
+#include <errno.h>
+
+namespace cgtest {
+
+class cg {
+private:
+public:
+ cg();
+ ~cg()
+ { }
+ struct cgroup *makenode(const string &name, const string &task_uid,
+ const string &task_gid, const string &control_uid,
+ const string &control_gid);
+};
+
+cg::cg(void)
+{
+ int ret;
+
+ ret = cg_init();
+ if (ret)
+ throw logic_error("Control Group Initialization failed..."
+ "Please check that cgroups are mounted and\n"
+ "at a single place");
+}
+
+struct cgroup *cg::makenode(const string &name, const string &task_uid,
+ const string &task_gid, const string &control_uid,
+ const string &control_gid)
+{
+ uid_t tuid, cuid;
+ gid_t tgid, cgid;
+ struct cgroup *ccg;
+ struct passwd *passwd;
+ int ret;
+
+ ccg = (struct cgroup *)malloc(sizeof(*ccg));
+
+ passwd = getpwnam(task_uid.c_str());
+ if (!passwd)
+ return NULL;
+ tuid = passwd->pw_uid;
+
+ passwd = getpwnam(task_gid.c_str());
+ if (!passwd)
+ return NULL;
+ tgid = passwd->pw_gid;
+
+ passwd = getpwnam(control_uid.c_str());
+ if (!passwd)
+ return NULL;
+ cuid = passwd->pw_uid;
+
+ passwd = getpwnam(control_gid.c_str());
+ if (!passwd)
+ return NULL;
+ cgid = passwd->pw_gid;
+
+ dbg("tuid %d, tgid %d, cuid %d, cgid %d\n", tuid, tgid, cuid, cgid);
+
+ ccg->name = (char *)malloc(strlen(name.c_str()) + 1);
+ strcpy(ccg->name, name.c_str());
+ ccg->controller[0] = (struct controller *)
+ calloc(1, sizeof(struct controller));
+ ccg->controller[0]->name = (char *)malloc(strlen("cpu") + 1);
+ strcpy(ccg->controller[0]->name,"cpu");
+
+ ccg->controller[0]->values[0] = (struct control_value *)
+ calloc(1, sizeof(struct control_value));
+ strcpy(ccg->controller[0]->values[0]->name,"cpu.shares");
+ ccg->controller[0]->values[0]->value = (char *)malloc(strlen("100") + 1);
+ strcpy(ccg->controller[0]->values[0]->value, "100");
+ ccg->tasks_uid = tuid;
+ ccg->tasks_gid = tgid;
+ ccg->control_uid = cuid;
+ ccg->control_gid = cgid;
+
+ ret = cg_create_cgroup(ccg);
+ if (ret) {
+ cout << "cg create group failed " << errno << endl;
+ ret = cg_delete_cgroup(ccg, 1);
+ if (ret)
+ cout << "cg delete group failed " << errno << endl;
+ }
+ return ccg;
+}
+
+} // namespace
+
+using namespace cgtest;
+int main(int argc, char *argv[])
+{
+ try {
+ cg *app = new cg();
+ struct cgroup *ccg;
+ ccg = app->makenode("database", "root", "root", "balbir",
+ "balbir");
+ delete app;
+ } catch (exception &e) {
+ cout << e.what() << endl;
+ exit(1);
+ }
+ return 0;
+}