summaryrefslogtreecommitdiffstats
path: root/wrapper.c
diff options
context:
space:
mode:
authorBalbir Singh <balbir@linux.vnet.ibm.com>2008-05-24 11:08:57 +0000
committerBalbir Singh <balbir@linux.vnet.ibm.com>2008-05-24 11:08:57 +0000
commit6b0384f40a1eb8ff70b27b6dfed3553883c9141f (patch)
tree95d650f507446f69071f3d0cd728392468027d5c /wrapper.c
parentc74b7e334e27928cbfeee489adcc7c25d2efb369 (diff)
downloadlibcg-6b0384f40a1eb8ff70b27b6dfed3553883c9141f.tar.gz
libcg-6b0384f40a1eb8ff70b27b6dfed3553883c9141f.tar.xz
libcg-6b0384f40a1eb8ff70b27b6dfed3553883c9141f.zip
Add v0.1b tag
git-svn-id: https://libcg.svn.sourceforge.net/svnroot/libcg/tags/v0.1b@49 4f4bb910-9a46-0410-90c8-c897d4f1cd53
Diffstat (limited to 'wrapper.c')
-rw-r--r--wrapper.c236
1 files changed, 236 insertions, 0 deletions
diff --git a/wrapper.c b/wrapper.c
new file mode 100644
index 0000000..916710f
--- /dev/null
+++ b/wrapper.c
@@ -0,0 +1,236 @@
+/*
+ * Copyright IBM Corporation. 2008
+ *
+ * Author: Dhaval Giani <dhaval@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.
+ *
+ * Code initiated and designed by Dhaval Giani. All faults are most likely
+ * his mistake.
+ */
+
+#include <libcgroup.h>
+#include <libcgroup-internal.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+
+struct cgroup *cgroup_new_cgroup(const char *name, uid_t tasks_uid,
+ gid_t tasks_gid, uid_t control_uid, gid_t control_gid)
+{
+ struct cgroup *cgroup = (struct cgroup *)
+ malloc(sizeof(struct cgroup));
+
+ if (!cgroup)
+ return NULL;
+
+ strncpy(cgroup->name, name, sizeof(cgroup->name));
+ cgroup->tasks_uid = tasks_uid;
+ cgroup->tasks_gid = tasks_gid;
+ cgroup->control_uid = control_uid;
+ cgroup->control_gid = control_gid;
+ cgroup->index = 0;
+ cgroup->controller[cgroup->index] = NULL;
+
+ return cgroup;
+}
+
+struct cgroup_controller *cgroup_add_controller(struct cgroup *cgroup,
+ const char *name)
+{
+ int i;
+ struct cgroup_controller *controller;
+
+ /*
+ * Still not sure how to handle the failure here.
+ */
+ if (cgroup->index >= CG_CONTROLLER_MAX)
+ return NULL;
+
+ /*
+ * Still not sure how to handle the failure here.
+ */
+ for (i = 0; i < cgroup->index; i++) {
+ if (strncmp(name, cgroup->controller[i]->name,
+ sizeof(cgroup->controller[i]->name)) == 0)
+ return NULL;
+ }
+
+ controller = (struct cgroup_controller *)
+ malloc(sizeof(struct cgroup_controller));
+
+ if (!controller)
+ return NULL;
+
+ strncpy(controller->name, name, sizeof(controller->name));
+ controller->index = 0;
+ controller->values[controller->index] = NULL;
+
+ cgroup->controller[cgroup->index] = controller;
+ cgroup->index++;
+
+ return controller;
+}
+
+void cgroup_free(struct cgroup **cgroup)
+{
+ int i, j;
+ struct cgroup *cg = *cgroup;
+
+ /*
+ * Passing NULL pointers is OK. We just return.
+ */
+ if (!cg)
+ return;
+
+ for (i = 0; i < cg->index; i++) {
+ for (j = 0; j < cg->controller[i]->index; j++)
+ free(cg->controller[i]->values[j]);
+ free(cg->controller[i]);
+ }
+
+ free(cg);
+ *cgroup = NULL;
+}
+
+int cgroup_add_value_string(struct cgroup_controller *controller,
+ const char *name, const char *value)
+{
+ int i;
+ struct control_value *cntl_value = (struct control_value *)
+ malloc(sizeof(struct control_value));
+
+ if (!cntl_value)
+ return ECGCONTROLLERCREATEFAILED;
+
+ if (controller->index >= CG_VALUE_MAX)
+ return ECGMAXVALUESEXCEEDED;
+
+ for (i = 0; i < controller->index && i < CG_VALUE_MAX; i++) {
+ if (strncmp(controller->values[controller->index]->name, name,
+ sizeof(controller->values[controller->index]->name)) == 0)
+ return ECGVALUEEXISTS;
+ }
+
+
+ strncpy(cntl_value->name, name, sizeof(cntl_value->name));
+ strncpy(cntl_value->value, value, sizeof(cntl_value->value));
+ controller->values[controller->index] = cntl_value;
+ controller->index++;
+
+ return 0;
+}
+
+int cgroup_add_value_int64(struct cgroup_controller *controller,
+ const char *name, int64_t value)
+{
+ int i;
+ unsigned ret;
+ struct control_value *cntl_value = (struct control_value *)
+ malloc(sizeof(struct control_value));
+
+ if (!cntl_value)
+ return ECGCONTROLLERCREATEFAILED;
+
+
+ if (controller->index >= CG_VALUE_MAX)
+ return ECGMAXVALUESEXCEEDED;
+
+ for (i = 0; i < controller->index && i < CG_VALUE_MAX; i++) {
+ if (strncmp(controller->values[controller->index]->name, name,
+ sizeof(controller->values[controller->index]->name)) == 0)
+ return ECGVALUEEXISTS;
+ }
+
+ strncpy(cntl_value->name, name,
+ sizeof(cntl_value->name));
+ ret = snprintf(cntl_value->value,
+ sizeof(cntl_value->value), "%lld", value);
+
+ if (ret >= sizeof(cntl_value->value))
+ return ECGINVAL;
+
+ controller->values[controller->index] = cntl_value;
+ controller->index++;
+
+ return 0;
+
+}
+
+int cgroup_add_value_uint64(struct cgroup_controller *controller,
+ const char *name, u_int64_t value)
+{
+ int i;
+ unsigned ret;
+ struct control_value *cntl_value = (struct control_value *)
+ malloc(sizeof(struct control_value));
+
+ if (!cntl_value)
+ return ECGCONTROLLERCREATEFAILED;
+
+
+ if (controller->index >= CG_VALUE_MAX)
+ return ECGMAXVALUESEXCEEDED;
+
+ for (i = 0; i < controller->index && i < CG_VALUE_MAX; i++) {
+ if (strncmp(controller->values[controller->index]->name, name,
+ sizeof(controller->values[controller->index]->name)) == 0)
+ return ECGVALUEEXISTS;
+ }
+
+ strncpy(cntl_value->name, name, sizeof(cntl_value->name));
+ ret = snprintf(cntl_value->value, sizeof(cntl_value->value), "%llu", value);
+
+ if (ret >= sizeof(cntl_value->value))
+ return ECGINVAL;
+
+ controller->values[controller->index] = cntl_value;
+ controller->index++;
+
+ return 0;
+
+}
+
+int cgroup_add_value_bool(struct cgroup_controller *controller,
+ const char *name, bool value)
+{
+ int i;
+ unsigned ret;
+ struct control_value *cntl_value = (struct control_value *)
+ malloc(sizeof(struct control_value));
+
+ if (!cntl_value)
+ return ECGCONTROLLERCREATEFAILED;
+
+
+ if (controller->index >= CG_VALUE_MAX)
+ return ECGMAXVALUESEXCEEDED;
+
+ for (i = 0; i < controller->index && i < CG_VALUE_MAX; i++) {
+ if (strncmp(controller->values[controller->index]->name, name,
+ sizeof(controller->values[controller->index]->name)) == 0)
+ return ECGVALUEEXISTS;
+ }
+
+ strncpy(cntl_value->name, name, sizeof(cntl_value->name));
+
+ if (value)
+ ret = snprintf(cntl_value->value, sizeof(cntl_value->value), "1");
+ else
+ ret = snprintf(cntl_value->value, sizeof(cntl_value->value), "0");
+
+ if (ret >= sizeof(cntl_value->value))
+ return ECGINVAL;
+
+ controller->values[controller->index] = cntl_value;
+ controller->index++;
+
+ return 0;
+}