diff options
author | Dhaval Giani <dhaval@linux.vnet.ibm.com> | 2008-08-12 08:19:24 +0000 |
---|---|---|
committer | Dhaval Giani <dhaval@linux.vnet.ibm.com> | 2008-08-12 08:19:24 +0000 |
commit | 9788476671e04b3d488a367169a3a51c03233c23 (patch) | |
tree | 8a71711634157cabc6b8bd52e24fc63f489dde5f /wrapper.c | |
parent | 2e920dd947b8f6a58d89a93bd0326e513b335487 (diff) | |
download | libcg-9788476671e04b3d488a367169a3a51c03233c23.tar.gz libcg-9788476671e04b3d488a367169a3a51c03233c23.tar.xz libcg-9788476671e04b3d488a367169a3a51c03233c23.zip |
libcgroup: Add cgroup_?et_value_* APIs
Adding in APIs which allow you to set and get values from a cgroup
controller. Use cgroup_get_controller to get a controller, and then
use cgroup_?et_value_* API for your requirement.
Signed-off-by: Dhaval Giani <dhaval@linux.vnet.ibm.com>
git-svn-id: https://libcg.svn.sourceforge.net/svnroot/libcg/trunk@126 4f4bb910-9a46-0410-90c8-c897d4f1cd53
Diffstat (limited to 'wrapper.c')
-rw-r--r-- | wrapper.c | 194 |
1 files changed, 194 insertions, 0 deletions
@@ -17,6 +17,7 @@ #include <libcgroup.h> #include <libcgroup-internal.h> +#include <inttypes.h> #include <stdio.h> #include <stdlib.h> #include <string.h> @@ -313,3 +314,196 @@ int cgroup_get_uid_gid(struct cgroup *cgroup, uid_t *tasks_uid, return 0; } + +struct cgroup_controller *cgroup_get_controller(struct cgroup *cgroup, + const char *name) +{ + int i; + struct cgroup_controller *cgc; + + for (i = 0; i < cgroup->index; i++) { + cgc = cgroup->controller[i]; + + if (!strcmp(cgc->name, name)) + return cgc; + } + + return NULL; +} + +int cgroup_get_value_string(struct cgroup_controller *controller, + const char *name, char **value) +{ + int i; + + for (i = 0; i < controller->index; i++) { + struct control_value *val = controller->values[i]; + + if (!strcmp(val->name, name)) { + *value = strdup(val->value); + + if (!*value) + return ECGOTHER; + + return 0; + } + } + + return ECGROUPVALUENOTEXIST; + +} + +int cgroup_set_value_string(struct cgroup_controller *controller, + const char *name, const char *value) +{ + int i; + + for (i = 0; i < controller->index; i++) { + struct control_value *val = controller->values[i]; + if (!strcmp(val->name, name)) { + strncpy(val->value, value, CG_VALUE_MAX); + return 0; + } + } + + return cgroup_add_value_string(controller, name, value); +} + +int cgroup_get_value_int64(struct cgroup_controller *controller, + const char *name, int64_t *value) +{ + int i; + + for (i = 0; i < controller->index; i++) { + struct control_value *val = controller->values[i]; + + if (!strcmp(val->name, name)) { + + if (sscanf(val->value, "%ld", value) != 1) + return ECGINVAL; + + return 0; + } + } + + return ECGROUPVALUENOTEXIST; +} + +int cgroup_set_value_int64(struct cgroup_controller *controller, + const char *name, int64_t value) +{ + int i; + unsigned ret; + + for (i = 0; i < controller->index; i++) { + struct control_value *val = controller->values[i]; + + if (!strcmp(val->name, name)) { + ret = snprintf(val->value, + sizeof(val->value), "%lu", value); + + if (ret >= sizeof(val->value) || ret < 0) + return ECGINVAL; + + return 0; + } + } + + return cgroup_add_value_int64(controller, name, value); +} + +int cgroup_get_value_uint64(struct cgroup_controller *controller, + const char *name, u_int64_t *value) +{ + int i; + + for (i = 0; i < controller->index; i++) { + struct control_value *val = controller->values[i]; + if (!strcmp(val->name, name)) { + + if (sscanf(val->value, "%lu", value) != 1) + return ECGINVAL; + + return 0; + } + } + + return ECGROUPVALUENOTEXIST; +} + +int cgroup_set_value_uint64(struct cgroup_controller *controller, + const char *name, u_int64_t value) +{ + int i; + unsigned ret; + + for (i = 0; i < controller->index; i++) { + struct control_value *val = controller->values[i]; + + if (!strcmp(val->name, name)) { + ret = snprintf(val->value, + sizeof(val->value), "%lu", value); + + if (ret >= sizeof(val->value) || ret < 0) + return ECGINVAL; + + return 0; + } + } + + return cgroup_add_value_uint64(controller, name, value); +} + +int cgroup_get_value_bool(struct cgroup_controller *controller, + const char *name, bool *value) +{ + int i; + + for (i = 0; i < controller->index; i++) { + struct control_value *val = controller->values[i]; + + if (!strcmp(val->name, name)) { + int cgc_val; + + if (sscanf(val->value, "%d", &cgc_val) != 1) + return ECGINVAL; + + if (cgc_val) + *value = true; + else + *value = false; + + return 0; + } + } + return ECGROUPVALUENOTEXIST; +} + +int cgroup_set_value_bool(struct cgroup_controller *controller, + const char *name, bool value) +{ + int i; + unsigned ret; + + for (i = 0; i < controller->index; i++) { + struct control_value *val = controller->values[i]; + + if (!strcmp(val->name, name)) { + if (value) { + ret = snprintf(val->value, + sizeof(val->value), "1"); + } else { + ret = snprintf(val->value, + sizeof(val->value), "0"); + } + + if (ret >= sizeof(val->value) || ret < 0) + return ECGINVAL; + + return 0; + + } + } + + return cgroup_add_value_bool(controller, name, value); +} |