From: Ivana Hutarova Varekova Date: Thu, 31 Jul 2014 09:37:26 +0000 (+0200) Subject: api.c: add cgroup_add_all_controllers function X-Git-Tag: v0.42.rc1~44 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=713a2d63296c24e8a6bf546ac24efcdd32f47e3f;p=thirdparty%2Flibcgroup.git api.c: add cgroup_add_all_controllers function cgroup_add_all_controllers function attach all mounted controllers to a given cgroup. This function just modifies internal libcgroup structure, not the kernel control group. input parameter: cgroup output parameter: zero or error number Signed-off-by: Ivana Hutarova Varekova Reviewed-by: Jan Chaloupka --- diff --git a/include/libcgroup/groups.h b/include/libcgroup/groups.h index d5c87aa5..201558f6 100644 --- a/include/libcgroup/groups.h +++ b/include/libcgroup/groups.h @@ -149,6 +149,16 @@ struct cgroup *cgroup_new_cgroup(const char *name); struct cgroup_controller *cgroup_add_controller(struct cgroup *cgroup, const char *name); +/** + * Attach all mounted controllers to given cgroup. This function just modifies + * internal libcgroup structure, not the kernel control group. + * + * @param cgroup + * @return zero or error number + */ +int cgroup_add_all_controllers(struct cgroup *cgroup); + + /** * Return appropriate controller from given group. * The controller must be added before using cgroup_add_controller() or loaded diff --git a/src/libcgroup.map b/src/libcgroup.map index f8b0fb96..8fe19905 100644 --- a/src/libcgroup.map +++ b/src/libcgroup.map @@ -122,3 +122,10 @@ CGROUP_0.40 { cgroup_templates_cache_set_source_files; cgroup_load_templates_cache_from_files; } CGROUP_0.39; + +CGROUP_0.41 { +} CGROUP_0.40; + +CGROUP_0.42 { + cgroup_add_all_controllers; +} CGROUP_0.41; diff --git a/src/wrapper.c b/src/wrapper.c index c03472aa..3a9331fe 100644 --- a/src/wrapper.c +++ b/src/wrapper.c @@ -92,6 +92,56 @@ struct cgroup_controller *cgroup_add_controller(struct cgroup *cgroup, return controller; } +int cgroup_add_all_controllers(struct cgroup *cgroup) +{ + int ret; + void *handle; + struct controller_data info; + struct cgroup_controller *cgc; + + /* go through the controller list */ + ret = cgroup_get_all_controller_begin(&handle, &info); + if ((ret != 0) && (ret != ECGEOF)) { + fprintf(stderr, "cannot read controller data: %s\n", + cgroup_strerror(ret)); + return ret; + } + + while (ret == 0) { + if (info.hierarchy == 0) { + /* the controller is not attached to any hierarchy + skip it */ + goto next; + } + + /* add mounted controller to cgroup structure */ + cgc = cgroup_add_controller(cgroup, info.name); + if (!cgc) { + ret = ECGINVAL; + fprintf(stderr, "controller %s can't be add\n", + info.name); + } + +next: + ret = cgroup_get_all_controller_next(&handle, &info); + if (ret && ret != ECGEOF) + goto end; + } + +end: + cgroup_get_all_controller_end(&handle); + + if (ret == ECGEOF) + ret = 0; + + if (ret) + fprintf(stderr, + "cgroup_get_controller_begin/next failed (%s)\n", + cgroup_strerror(ret)); + + return ret; +} + void cgroup_free_controllers(struct cgroup *cgroup) { int i, j;