]> git.ipfire.org Git - thirdparty/libcgroup.git/commitdiff
cgget.c: fix checkpatch.pl warnings
authorKamalesh Babulal <kamalesh.babulal@oracle.com>
Wed, 16 Mar 2022 15:32:16 +0000 (21:02 +0530)
committerTom Hromatka <tom.hromatka@oracle.com>
Wed, 16 Mar 2022 21:38:20 +0000 (15:38 -0600)
Fix all of the warnings/errors reported by Linux Kernel's checkpatch.pl,
except SPDX_LICENSE_TAG, USE_NEGATIVE_ERRNO and NEW_TYPEDEFS types.  It
also introduces reverse xmas tree local variable declarations and header
file reordering.

In summary, this patch fixes the following checkpatch.pl
recommendations:
total: 2 errors, 13 warnings, 772 lines checked

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

index 460e312b406fdeba13b6076fb940983f29a7c57a..ee8f540715099aa6dbe08e5f0f01416bf3c663b2 100644 (file)
@@ -1,55 +1,54 @@
 // SPDX-License-Identifier: LGPL-2.1-only
+#include "tools-common.h"
+
 #include <libcgroup.h>
 #include <libcgroup-internal.h>
 
 #include <dirent.h>
 #include <getopt.h>
 #include <pthread.h>
-#include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
 #include <unistd.h>
-
-#include "tools-common.h"
+#include <stdio.h>
 
 #define MODE_SHOW_HEADERS              1
 #define MODE_SHOW_NAMES                        2
 
 #define LL_MAX                         100
 
-static struct option const long_options[] =
-{
-       {"variable", required_argument, NULL, 'r'},
-       {"help", no_argument, NULL, 'h'},
-       {"all",  no_argument, NULL, 'a'},
-       {"values-only", no_argument, NULL, 'v'},
+static const struct option long_options[] = {
+       {"variable",    required_argument, NULL, 'r'},
+       {"help",              no_argument, NULL, 'h'},
+       {"all",               no_argument, NULL, 'a'},
+       {"values-only",       no_argument, NULL, 'v'},
        {NULL, 0, NULL, 0}
 };
 
 static void usage(int status, const char *program_name)
 {
        if (status != 0) {
-               fprintf(stderr, "Wrong input parameters,"
-                       " try %s -h' for more information.\n",
+               fprintf(stderr, "Wrong input parameters,");
+               fprintf(stderr, " try %s -h' for more information.\n",
                        program_name);
                return;
        }
-       printf("Usage: %s [-nv] [-r <name>] [-g <controllers>] "\
-               "[-a] <path> ...\n"\
-               "   or: %s [-nv] [-r <name>] -g <controllers>:<path> ...\n",
-               program_name, program_name);
+       printf("Usage: %s [-nv] [-r <name>] [-g <controllers>] ", program_name);
+       printf("[-a] <path> ...\n");
+       printf("   or: %s [-nv] [-r <name>] -g <controllers>:<path> ...\n",
+              program_name);
        printf("Print parameter(s) of given group(s).\n");
-       printf("  -a, --all                     Print info about all relevant "\
-               "controllers\n");
-       printf("  -g <controllers>              Controller which info should "\
-               "be displayed\n");
-       printf("  -g <controllers>:<path>       Control group which info "\
-               "should be displayed\n");
+       printf("  -a, --all                     Print info about all relevant ");
+       printf("controllers\n");
+       printf("  -g <controllers>              Controller which info should ");
+       printf("be displayed\n");
+       printf("  -g <controllers>:<path>       Control group which info ");
+       printf("should be displayed\n");
        printf("  -h, --help                    Display this help\n");
        printf("  -n                            Do not print headers\n");
        printf("  -r, --variable  <name>        Define parameter to display\n");
-       printf("  -v, --values-only             Print only values, not "\
-               "parameter names\n");
+       printf("  -v, --values-only             Print only values, not ");
+       printf("parameter names\n");
 }
 
 static int get_controller_from_name(const char * const name,
@@ -63,8 +62,8 @@ static int get_controller_from_name(const char * const name,
 
        dot = strchr(*controller, '.');
        if (dot == NULL) {
-               fprintf(stderr, "cgget: error parsing parameter name\n" \
-                       " '%s'", name);
+               fprintf(stderr, "cgget: error parsing parameter name\n");
+               fprintf(stderr, " '%s'", name);
                return ECGINVAL;
        }
 
@@ -75,9 +74,10 @@ static int get_controller_from_name(const char * const name,
 static int create_cg(struct cgroup **cg_list[], int * const cg_list_len)
 {
        *cg_list = realloc(*cg_list,
-                       ((*cg_list_len) + 1) * sizeof(struct cgroup *));
+                          ((*cg_list_len) + 1) * sizeof(struct cgroup *));
        if ((*cg_list) == NULL)
                return ECGCONTROLLERCREATEFAILED;
+
        memset(&(*cg_list)[*cg_list_len], 0, sizeof(struct cgroup *));
 
        (*cg_list)[*cg_list_len] = cgroup_new_cgroup("");
@@ -102,7 +102,8 @@ static int parse_a_flag(struct cgroup **cg_list[], int * const cg_list_len)
                        goto out;
        }
 
-       /* if "-r" was provided, then we know that the cgroup(s) will be an
+       /*
+        * if "-r" was provided, then we know that the cgroup(s) will be an
         * optarg at the end with no flag.  Let's temporarily populate the
         * first cgroup with the requested control values.
         */
@@ -124,7 +125,8 @@ static int parse_a_flag(struct cgroup **cg_list[], int * const cg_list_len)
                ret = cgroup_get_controller_next(&handle, &controller);
        }
        if (ret == ECGEOF)
-               /* we successfully reached the end of the controller list;
+               /*
+                * we successfully reached the end of the controller list;
                 * this is not an error
                 */
                ret = 0;
@@ -132,7 +134,6 @@ static int parse_a_flag(struct cgroup **cg_list[], int * const cg_list_len)
        cgroup_get_controller_end(&handle);
 
        return ret;
-
 out:
        cgroup_get_controller_end(&handle);
        return ret;
@@ -152,7 +153,8 @@ static int parse_r_flag(struct cgroup **cg_list[], int * const cg_list_len,
                        goto out;
        }
 
-       /* if "-r" was provided, then we know that the cgroup(s) will be an
+       /*
+        * if "-r" was provided, then we know that the cgroup(s) will be an
         * optarg at the end with no flag.  Let's temporarily populate the
         * first cgroup with the requested control values.
         */
@@ -182,7 +184,8 @@ out:
 }
 
 static int parse_g_flag_no_colon(struct cgroup **cg_list[],
-               int * const cg_list_len, const char * const ctrl_str)
+                                int * const cg_list_len,
+                                const char * const ctrl_str)
 {
        struct cgroup_controller *cgc;
        struct cgroup *cg = NULL;
@@ -199,7 +202,8 @@ static int parse_g_flag_no_colon(struct cgroup **cg_list[],
                        goto out;
        }
 
-       /* if "-g <controller>" was provided, then we know that the cgroup(s)
+       /*
+        * if "-g <controller>" was provided, then we know that the cgroup(s)
         * will be an optarg at the end with no flag.  Let's temporarily
         * populate the first cgroup with the requested control values.
         */
@@ -240,8 +244,8 @@ static int split_controllers(const char * const in,
                             char **ctrl[], int * const ctrl_len)
 {
        char *copy, *tok, *colon, *saveptr = NULL;
-       char **tmp;
        int ret = 0;
+       char **tmp;
 
        copy = strdup(in);
        if (!copy)
@@ -276,7 +280,8 @@ out:
 }
 
 static int parse_g_flag_with_colon(struct cgroup **cg_list[],
-               int * const cg_list_len, const char * const ctrl_str)
+                                  int * const cg_list_len,
+                                  const char * const ctrl_str)
 {
        struct cgroup_controller *cgc;
        struct cgroup *cg = NULL;
@@ -323,20 +328,23 @@ static int parse_opt_args(int argc, char *argv[], struct cgroup **cg_list[],
        struct cgroup *cg = NULL;
        int ret = 0;
 
-       /* The first cgroup was temporarily populated and requires
-        * the user to provide a cgroup name as an opt */
+       /*
+        * The first cgroup was temporarily populated and requires
+        * the user to provide a cgroup name as an opt
+        */
        if (argv[optind] == NULL && first_cg_is_dummy) {
                usage(1, argv[0]);
                exit(-1);
        }
 
-       /* The user has provided a cgroup and controller via the
+       /*
+        * The user has provided a cgroup and controller via the
         * -g <controller>:<cgroup> flag and has also provided a cgroup via
         * the optind.  This was not supported by the previous cgget
         * implementation.  Continue that approach.
         *
         * Example of a command that will hit this code:
-        *      $ cgget -g cpu:mycgroup mycgroup
+        *      $ cgget -g cpu:mycgroup mycgroup
         */
        if (argv[optind] != NULL && (*cg_list_len) > 0 &&
            strcmp((*cg_list)[0]->name, "") != 0) {
@@ -351,7 +359,8 @@ static int parse_opt_args(int argc, char *argv[], struct cgroup **cg_list[],
                        cg = NULL;
 
                if ((*cg_list_len) == 0) {
-                       /* The user didn't provide a '-r' or '-g' flag.
+                       /*
+                        * The user didn't provide a '-r' or '-g' flag.
                         * The parse_a_flag() function can be reused here
                         * because we both have the same use case - gather
                         * all the data about this particular cgroup.
@@ -364,7 +373,8 @@ static int parse_opt_args(int argc, char *argv[], struct cgroup **cg_list[],
                                argv[optind],
                                sizeof((*cg_list)[(*cg_list_len) - 1]->name) - 1);
                } else if (cg != NULL && strlen(cg->name) == 0) {
-                       /* this cgroup was created based upon control/value
+                       /*
+                        * this cgroup was created based upon control/value
                         * pairs or with a -g <controller> option.  we'll
                         * populate it with the parameter provided by the
                         * user
@@ -396,14 +406,14 @@ static int parse_opts(int argc, char *argv[], struct cgroup **cg_list[],
                      int * const cg_list_len, int * const mode)
 {
        bool do_not_fill_controller = false;
-       bool fill_controller = false;
        bool first_cgroup_is_dummy = false;
+       bool fill_controller = false;
        int ret = 0;
        int c;
 
        /* Parse arguments. */
-       while ((c = getopt_long(argc, argv, "r:hnvg:a", long_options, NULL))
-               > 0) {
+       while ((c = getopt_long(argc, argv, "r:hnvg:a", long_options,
+                               NULL)) > 0) {
                switch (c) {
                case 'h':
                        usage(0, argv[0]);
@@ -475,25 +485,28 @@ static int get_cv_value(struct control_value * const cv,
        int ret;
 
        ret = cgroup_read_value_begin(controller_name, cg_name, cv->name,
-                       &handle, tmp_line, LL_MAX);
+                                     &handle, tmp_line, LL_MAX);
        if (ret == ECGEOF)
                goto read_end;
        if (ret != 0) {
                if (ret == ECGOTHER) {
                        int tmp_ret;
 
-                       /* to maintain compatibility with an earlier version
+                       /*
+                        * to maintain compatibility with an earlier version
                         * of cgget, try to determine if the failure was due
                         * to an invalid controller
                         */
                        tmp_ret = cgroup_test_subsys_mounted(controller_name);
-                       if (tmp_ret == 0)
-                               fprintf(stderr, "cgget: cannot find controller " \
-                                       "'%s' in group '%s'\n", controller_name,
-                                       cg_name);
-                       else
+                       if (tmp_ret == 0) {
+                               fprintf(stderr, "cgget: cannot find ");
+                               fprintf(stderr, "controller '%s' in group ",
+                                       controller_name);
+                               fprintf(stderr, "'%s'\n", cg_name);
+                       } else {
                                fprintf(stderr, "variable file read failed %s\n",
                                        cgroup_strerror(ret));
+                       }
                }
 
                goto end;
@@ -507,7 +520,7 @@ static int get_cv_value(struct control_value * const cv,
        if (cv->multiline_value == NULL)
                goto end;
 
-       while((ret = cgroup_read_value_next(&handle, tmp_line, LL_MAX)) == 0) {
+       while ((ret = cgroup_read_value_next(&handle, tmp_line, LL_MAX)) == 0) {
                if (ret == 0) {
                        is_multiline = true;
                        cv->value[0] = '\0';
@@ -564,15 +577,15 @@ static int fill_empty_controller(struct cgroup * const cg,
                                 struct cgroup_controller * const cgc)
 {
        struct dirent *ctrl_dir = NULL;
-       char path[FILENAME_MAX];
        bool found_mount = false;
        int i, path_len, ret = 0;
+       char path[FILENAME_MAX];
        DIR *dir = NULL;
 
        pthread_rwlock_rdlock(&cg_mount_table_lock);
 
-       for (i = 0; i < CG_CONTROLLER_MAX &&
-                       cg_mount_table[i].name[0] != '\0'; i++) {
+       for (i = 0; i < CG_CONTROLLER_MAX && cg_mount_table[i].name[0] != '\0';
+            i++) {
                if (strlen(cgc->name) == strlen(cg_mount_table[i].name) &&
                    strncmp(cgc->name, cg_mount_table[i].name,
                            strlen(cgc->name)) == 0) {
@@ -607,9 +620,7 @@ static int fill_empty_controller(struct cgroup * const cg,
        }
 
        while ((ctrl_dir = readdir(dir)) != NULL) {
-               /*
-                * Skip over non regular files
-                */
+               /* Skip over non regular files */
                if (ctrl_dir->d_type != DT_REG)
                        continue;
 
@@ -622,7 +633,8 @@ static int fill_empty_controller(struct cgroup * const cg,
                if (cgc->index > 0) {
                        cgc->values[cgc->index - 1]->dirty = false;
 
-                       /* previous versions of cgget indented the second and
+                       /*
+                        * previous versions of cgget indented the second and
                         * all subsequent lines.  continue that behavior
                         */
                        if (strchr(cgc->values[cgc->index - 1]->value, '\n')) {
@@ -707,9 +719,8 @@ void print_controller(const struct cgroup_controller * const cgc, int mode)
 {
        int i;
 
-       for (i = 0; i < cgc->index; i++) {
+       for (i = 0; i < cgc->index; i++)
                print_control_values(cgc->values[i], mode);
-       }
 }
 
 static void print_cgroup(const struct cgroup * const cg, int mode)
@@ -730,17 +741,16 @@ static void print_cgroups(struct cgroup *cg_list[], int cg_list_len, int mode)
 {
        int i;
 
-       for (i = 0; i < cg_list_len; i++) {
+       for (i = 0; i < cg_list_len; i++)
                print_cgroup(cg_list[i], mode);
-       }
 }
 
 int main(int argc, char *argv[])
 {
+       int mode = MODE_SHOW_NAMES | MODE_SHOW_HEADERS;
        struct cgroup **cg_list = NULL;
        int cg_list_len = 0;
        int ret = 0, i;
-       int mode = MODE_SHOW_NAMES | MODE_SHOW_HEADERS;
 
        /* No parameter on input? */
        if (argc < 2) {