]> git.ipfire.org Git - thirdparty/libcgroup.git/commitdiff
tools/cgxset: add cgroup_set_cgroup_values() helper
authorKamalesh Babulal <kamalesh.babulal@oracle.com>
Sat, 29 Jun 2024 00:06:38 +0000 (05:36 +0530)
committerTom Hromatka <tom.hromatka@oracle.com>
Wed, 17 Jul 2024 21:14:33 +0000 (15:14 -0600)
Move the cgroup controller value setting logic to a helper function,
cgroup_set_cgroup_values() and also introduce program_name variable, to
be used instead of argv[0], when printing the info()/err() messages.

Signed-off-by: Kamalesh Babulal <kamalesh.babulal@oracle.com>
Signed-off-by: Tom Hromatka <tom.hromatka@oracle.com>
src/tools/cgxset.c

index f22482c0f2f15367df0b6dd8527b687cfb024f0c..a718c4cd4b48a167d38848ae3cba2bf31170595b 100644 (file)
@@ -37,6 +37,8 @@ static const struct option long_options[] = {
 
 int flags; /* used input method */
 
+static char *program_name;
+
 static struct cgroup *copy_name_value_from_cgroup(char src_cg_path[FILENAME_MAX])
 {
        struct cgroup *src_cgroup;
@@ -64,7 +66,70 @@ scgroup_err:
        return NULL;
 }
 
-static void usage(int status, const char *program_name)
+static int cgroup_set_cgroup_values(struct cgroup *src_cgrp, const char * const new_cgrp,
+                                   bool ignore_unmappable, enum cg_version_t src_version)
+{
+       struct cgroup *cgrp, *converted_src_cgrp;
+       int ret = 0;
+
+       /* create new cgroup */
+       cgrp = cgroup_new_cgroup(new_cgrp);
+       if (!cgrp) {
+               ret = ECGFAIL;
+               err("%s: can't add new cgroup: %s\n", program_name, cgroup_strerror(ret));
+               return ret;
+       }
+
+       /* copy the values from the source cgroup to new one */
+       ret = cgroup_copy_cgroup(cgrp, src_cgrp);
+       if (ret != 0) {
+               err("%s: cgroup %s error: %s\n", program_name, src_cgrp->name,
+                   cgroup_strerror(ret));
+               goto err;
+       }
+
+       converted_src_cgrp = cgroup_new_cgroup(cgrp->name);
+       if (converted_src_cgrp == NULL) {
+               ret = ECGCONTROLLERCREATEFAILED;
+               goto err;
+       }
+
+       /*
+        * If a failure occurs between this comment and the "End of above comment"
+        * below, then we need to manually free converted_src_cgroup.
+        */
+
+       ret = cgroup_convert_cgroup(converted_src_cgrp, CGROUP_DISK, src_cgrp, src_version);
+       if ((ret && ret != ECGNOVERSIONCONVERT) ||
+           (ret == ECGNOVERSIONCONVERT && !ignore_unmappable)) {
+               /*
+                * If the user not has specified that we ignore any errors
+                * due to being unable to map from v1 to v2 or vice versa,
+                * return error, else ignore the error and continue.
+                */
+               cgroup_free(&converted_src_cgrp);
+               goto err;
+       }
+
+       /*
+        * End of above comment about converted_src_cgroup needing to be manually freed.
+        */
+       cgroup_free(&cgrp);
+       cgrp = converted_src_cgrp;
+
+       /* modify cgroup based on values of the new one */
+       ret = cgroup_modify_cgroup(cgrp);
+       if (ret) {
+               err("%s: cgroup modify error: %s\n", program_name, cgroup_strerror(ret));
+               goto err;
+       }
+
+err:
+       cgroup_free(&cgrp);
+       return ret;
+}
+
+static void usage(int status)
 {
        if (status != 0) {
                err("Wrong input parameters, ");
@@ -153,19 +218,19 @@ int main(int argc, char *argv[])
        int nv_number = 0;
        int nv_max = 0;
 
-       struct cgroup *converted_src_cgroup = NULL;
        char src_cg_path[FILENAME_MAX] = "\0";
        struct cgroup *src_cgroup = NULL;
-       struct cgroup *cgroup = NULL;
 
        enum cg_version_t src_version = CGROUP_UNK;
        bool ignore_unmappable = false;
        int ret = 0;
        int c;
 
+       program_name = argv[0];
+
        /* no parameter on input */
        if (argc < 2) {
-               err("Usage is %s -r <name=value> <relative path to cgroup>\n", argv[0]);
+               err("Usage is %s -r <name=value> <relative path to cgroup>\n", program_name);
                return -1;
        }
 
@@ -181,12 +246,12 @@ int main(int argc, char *argv[])
                switch (c) {
 #endif
                case 'h':
-                       usage(0, argv[0]);
+                       usage(0);
                        ret = 0;
                        goto err;
                case 'r':
                        if ((flags &  FL_COPY) != 0) {
-                               usage(1, argv[0]);
+                               usage(1);
                                ret = EXIT_BADARGS;
                                goto err;
                        }
@@ -198,13 +263,13 @@ int main(int argc, char *argv[])
                                name_value = (struct control_value *)
                                        realloc(name_value, nv_max * sizeof(struct control_value));
                                if (!name_value) {
-                                       err("%s: not enough memory\n", argv[0]);
+                                       err("%s: not enough memory\n", program_name);
                                        ret = -1;
                                        goto err;
                                }
                        }
 
-                       ret = parse_r_flag(argv[0], optarg, &name_value[nv_number]);
+                       ret = parse_r_flag(program_name, optarg, &name_value[nv_number]);
                        if (ret)
                                goto err;
 
@@ -212,7 +277,7 @@ int main(int argc, char *argv[])
                        break;
                case COPY_FROM_OPTION:
                        if (flags != 0) {
-                               usage(1, argv[0]);
+                               usage(1);
                                ret = EXIT_BADARGS;
                                goto err;
                        }
@@ -230,7 +295,7 @@ int main(int argc, char *argv[])
                        ignore_unmappable = true;
                        break;
                default:
-                       usage(1, argv[0]);
+                       usage(1);
                        ret = EXIT_BADARGS;
                        goto err;
                }
@@ -238,13 +303,13 @@ int main(int argc, char *argv[])
 
        /* no cgroup name */
        if (!argv[optind]) {
-               err("%s: no cgroup specified\n", argv[0]);
+               err("%s: no cgroup specified\n", program_name);
                ret = EXIT_BADARGS;
                goto err;
        }
 
        if (flags == 0) {
-               err("%s: no name-value pair was set\n", argv[0]);
+               err("%s: no name-value pair was set\n", program_name);
                ret = EXIT_BADARGS;
                goto err;
        }
@@ -252,7 +317,7 @@ int main(int argc, char *argv[])
        /* initialize libcgroup */
        ret = cgroup_init();
        if (ret) {
-               err("%s: libcgroup initialization failed: %s\n", argv[0],
+               err("%s: libcgroup initialization failed: %s\n", program_name,
                    cgroup_strerror(ret));
                goto err;
        }
@@ -277,69 +342,17 @@ int main(int argc, char *argv[])
        }
 
        while (optind < argc) {
-               /* create new cgroup */
-               cgroup = cgroup_new_cgroup(argv[optind]);
-               if (!cgroup) {
-                       ret = ECGFAIL;
-                       err("%s: can't add new cgroup: %s\n", argv[0], cgroup_strerror(ret));
-                       goto err;
-               }
-
-               /* copy the values from the source cgroup to new one */
-               ret = cgroup_copy_cgroup(cgroup, src_cgroup);
-               if (ret != 0) {
-                       err("%s: cgroup %s error: %s\n", argv[0], src_cg_path,
-                           cgroup_strerror(ret));
-                       goto err;
-               }
-
-               converted_src_cgroup = cgroup_new_cgroup(cgroup->name);
-               if (converted_src_cgroup == NULL) {
-                       ret = ECGCONTROLLERCREATEFAILED;
-                       goto err;
-               }
-
-               /*
-                * If a failure occurs between this comment and the "End of above comment"
-                * below, then we need to manually free converted_src_cgroup.
-                */
-
-               ret = cgroup_convert_cgroup(converted_src_cgroup, CGROUP_DISK, src_cgroup,
-                                           src_version);
-               if ((ret && ret != ECGNOVERSIONCONVERT) ||
-                   (ret == ECGNOVERSIONCONVERT && !ignore_unmappable)) {
-                       /*
-                        * If the user not has specified that we ignore any errors
-                        * due to being unable to map from v1 to v2 or vice versa,
-                        * return error, else ignore the error and continue.
-                        */
-                       free(converted_src_cgroup);
-                       goto err;
-               }
-
-               /*
-                * End of above comment about converted_src_cgroup needing to be manually freed.
-                */
 
-               cgroup_free(&cgroup);
-               cgroup = converted_src_cgroup;
-
-               /* modify cgroup based on values of the new one */
-               ret = cgroup_modify_cgroup(cgroup);
-               if (ret) {
-                       err("%s: cgroup modify error: %s\n", argv[0], cgroup_strerror(ret));
+               ret = cgroup_set_cgroup_values(src_cgroup, argv[optind], ignore_unmappable,
+                                              src_version);
+               if (ret)
                        goto err;
-               }
 
                optind++;
-               cgroup_free(&cgroup);
        }
 
 err:
-       if (cgroup)
-               cgroup_free(&cgroup);
-       if (src_cgroup)
-               cgroup_free(&src_cgroup);
+       cgroup_free(&src_cgroup);
        free(name_value);
 
        return ret;