]> git.ipfire.org Git - thirdparty/libcgroup.git/commitdiff
libcgroup: Some libcgroup API cleanups
authorDhaval Giani <dhaval@linux.vnet.ibm.com>
Fri, 15 Aug 2008 05:06:14 +0000 (05:06 +0000)
committerDhaval Giani <dhaval@linux.vnet.ibm.com>
Fri, 15 Aug 2008 05:06:14 +0000 (05:06 +0000)
Various libcgroup api cleanups as suggested by balbir during code review

Signed-off-by: Vivek Goyal <vgoyal@redhat.com>
Signed-off-by: Dhaval Giani <dhaval@linux.vnet.ibm.com>
git-svn-id: https://libcg.svn.sourceforge.net/svnroot/libcg/trunk@159 4f4bb910-9a46-0410-90c8-c897d4f1cd53

api.c
libcgroup-internal.h
libcgroup.h

diff --git a/api.c b/api.c
index 0edb55bbc2beecea24100f3e05a7d507fc192c99..2902ad192df7b1ccc2d35a668ed8706a2d86b1e2 100644 (file)
--- a/api.c
+++ b/api.c
@@ -993,11 +993,9 @@ static int cg_prepare_controller_array(char *cstr, char *controllers[])
                        temp = strtok_r(NULL, ",", &saveptr);
 
                if (temp) {
-                       controllers[j] = (char *) malloc(strlen(temp) + 1);
+                       controllers[j] = strdup(temp);
                        if (!controllers[j])
                                return ECGOTHER;
-                       else
-                               strcpy(controllers[j], temp);
                }
                j++;
        } while (temp);
@@ -1096,15 +1094,14 @@ static int cg_parse_rules_config_file(struct cgroup_rules_data *cgrldp,
                        return 0;
                }
 
-               if (i == 3) {
+               if (i == CGRULES_MAX_FIELDS_PER_LINE) {
                        /* a complete line */
                        if (((strcmp(cgrldp->pw->pw_name, user) == 0) ||
                                (strcmp(user, "*") == 0)) ||
                                (match_uid && !strcmp(user, "%"))) {
                                match_uid = 1;
 
-                               cgroup = (struct cgroup *)
-                                       calloc(1, sizeof(struct cgroup));
+                               cgroup = calloc(1, sizeof(struct cgroup));
                                if (!cgroup) {
                                        ret = ECGOTHER;
                                        goto out;
@@ -1137,8 +1134,8 @@ static int cg_parse_rules_config_file(struct cgroup_rules_data *cgrldp,
                                        (match_gid && !strcmp(user, "%"))) {
                                        match_gid = 1;
 
-                                       cgroup = (struct cgroup *)
-                                               calloc(1, sizeof(struct cgroup));
+                                       cgroup = calloc(1,
+                                                       sizeof(struct cgroup));
                                        if (!cgroup) {
                                                ret = ECGOTHER;
                                                goto out;
@@ -1185,14 +1182,14 @@ int cgroup_change_cgroup_uid_gid(uid_t uid, gid_t gid, pid_t pid)
        int ret = 0, i;
        struct passwd *pw;
        struct cgroup_rules_data cgrld, *cgrldp = &cgrld;
-       struct cgroup *cgroups[CG_CONTROLLER_MAX];
+       struct cgroup *cgroups[CG_HIER_MAX];
 
        if (!cgroup_initialized) {
                dbg("libcgroup is not initialized\n");
                return ECGROUPNOTINITIALIZED;
        }
        memset(cgrldp, 0, sizeof(struct cgroup_rules_data));
-       memset(cgroups, 0, CG_CONTROLLER_MAX);
+       memset(cgroups, 0, CG_HIER_MAX);
 
        pw = getpwuid(uid);
        if (!pw) {
@@ -1212,7 +1209,7 @@ int cgroup_change_cgroup_uid_gid(uid_t uid, gid_t gid, pid_t pid)
        }
 
        /* Add task to cgroups */
-       for (i = 0; (i < CG_CONTROLLER_MAX) && cgroups[i]; i++) {
+       for (i = 0; (i < CG_HIER_MAX) && cgroups[i]; i++) {
                ret = cgroup_attach_task_pid(cgroups[i], cgrldp->pid);
                if (ret) {
                        dbg("cgroup_attach_task_pid failed:%d\n", ret);
@@ -1221,7 +1218,7 @@ int cgroup_change_cgroup_uid_gid(uid_t uid, gid_t gid, pid_t pid)
        }
 out:
        /* Free the cgroups */
-       for (i = 0; (i < CG_CONTROLLER_MAX) && cgroups[i]; i++)
+       for (i = 0; (i < CG_HIER_MAX) && cgroups[i]; i++)
                cgroup_free(&cgroups[i]);
        return ret;
 }
index c01dfa90c9893ec0ffc3f78bf6834ebf03d09a16..f422cc5a6e9ccf9fdb463debea9db5854115f35a 100644 (file)
@@ -22,6 +22,7 @@ __BEGIN_DECLS
 #include <limits.h>
 
 #define CGRULES_CONF_FILE       "/etc/cgrules.conf"
+#define CGRULES_MAX_FIELDS_PER_LINE            3
 
 struct control_value {
        char name[FILENAME_MAX];
index ae73a2215291e8f167cf8d251e1b866443020716..8eeb1a79262e2b627bbae8e0af60f10288ccf692 100644 (file)
@@ -156,6 +156,11 @@ void cg_unload_current_config(void);
 #define CG_NV_MAX 100
 #define CG_CONTROLLER_MAX 100
 #define CG_VALUE_MAX 100
+/* Max number of mounted hierarchies. Event if one controller is mounted per
+ * hier, it can not exceed CG_CONTROLLER_MAX
+ */
+#define CG_HIER_MAX  CG_CONTROLLER_MAX
+
 /* Functions and structures that can be used by the application*/
 struct cgroup;
 struct cgroup_controller;