Whamcloud - gitweb
LU-6142 utils: Fix style issues for l_getidentity.c 37/34437/3
authorArshad Hussain <arshad.super@gmail.com>
Sat, 9 Mar 2019 16:35:49 +0000 (22:05 +0530)
committerOleg Drokin <green@whamcloud.com>
Mon, 1 Apr 2019 07:22:17 +0000 (07:22 +0000)
This patch fixes issues reported by checkpatch
for file lustre/utils/l_getidentity.c

Change-Id: If49272725e663c1e3ddb75acd0eadc58b79be35a
Signed-off-by: Arshad Hussain <arshad.super@gmail.com>
Reviewed-on: https://review.whamcloud.com/34437
Reviewed-by: Andreas Dilger <adilger@whamcloud.com>
Tested-by: Jenkins
Tested-by: Maloo <maloo@whamcloud.com>
Reviewed-by: James Simmons <uja.ornl@yahoo.com>
lustre/utils/l_getidentity.c

index ff7bb26..df80258 100644 (file)
@@ -81,7 +81,7 @@ static void usage(void)
 
 static int compare_u32(const void *v1, const void *v2)
 {
-        return (*(__u32 *)v1 - *(__u32 *)v2);
+       return *(__u32 *)v1 - *(__u32 *)v2;
 }
 
 static void errlog(const char *fmt, ...)
@@ -117,12 +117,14 @@ int get_groups_local(struct identity_downcall_data *data,
 
        groups = data->idd_groups;
 
-       /* Allocate array of size maxgroups instead of handling two
-        * consecutive and potentially racy getgrouplist() calls. */
+       /*
+        * Allocate array of size maxgroups instead of handling two
+        * consecutive and potentially racy getgrouplist() calls.
+        */
        groups_tmp = malloc(maxgroups * sizeof(gid_t));
-       if (groups_tmp == NULL) {
+       if (!groups_tmp) {
                data->idd_err = errno ? errno : ENOMEM;
-               errlog("malloc error=%u\n",data->idd_err);
+               errlog("malloc error=%u\n", data->idd_err);
                return -1;
        }
 
@@ -132,7 +134,7 @@ int get_groups_local(struct identity_downcall_data *data,
                free(groups_tmp);
                data->idd_err = errno ? errno : EIDRM;
                errlog("getgrouplist() error for uid %u: error=%u\n",
-                       data->idd_uid, data->idd_err);
+                      data->idd_uid, data->idd_err);
                return -1;
        }
 
@@ -151,32 +153,33 @@ int get_groups_local(struct identity_downcall_data *data,
 
 static inline int comment_line(char *line)
 {
-        char *p = line;
+       char *p = line;
 
-        while (*p && (*p == ' ' || *p == '\t')) p++;
+       while (*p && (*p == ' ' || *p == '\t'))
+               p++;
 
-        if (!*p || *p == '\n' || *p == '#')
-                return 1;
-        return 0;
+       if (!*p || *p == '\n' || *p == '#')
+               return 1;
+       return 0;
 }
 
 static inline int match_uid(uid_t uid, const char *str)
 {
-        char *end;
-        uid_t uid2;
+       char *end;
+       uid_t uid2;
 
-        if(!strcmp(str, "*"))
-                return -1;
+       if (!strcmp(str, "*"))
+               return -1;
 
-        uid2 = strtoul(str, &end, 0);
-        if (*end)
-                return 0;
-        return (uid == uid2);
+       uid2 = strtoul(str, &end, 0);
+       if (*end)
+               return 0;
+       return (uid == uid2);
 }
 
 typedef struct {
-        char   *name;
-        __u32   bit;
+       char *name;
+       __u32 bit;
 } perm_type_t;
 
 static perm_type_t perm_types[] = {
@@ -199,18 +202,19 @@ static perm_type_t noperm_types[] = {
 
 int parse_perm(__u32 *perm, __u32 *noperm, char *str)
 {
-        char *start, *end;
-        char name[64];
-        perm_type_t *pt;
-
-        *perm = 0;
-        *noperm = 0;
-        start = str;
-        while (1) {
+       char *start, *end;
+       char name[64];
+       perm_type_t *pt;
+
+       *perm = 0;
+       *noperm = 0;
+       start = str;
+       while (1) {
                size_t len;
+
                memset(name, 0, sizeof(name));
                end = strchr(start, ',');
-               if (end == NULL)
+               if (!end)
                        end = str + strlen(str);
                if (start >= end)
                        break;
@@ -219,30 +223,30 @@ int parse_perm(__u32 *perm, __u32 *noperm, char *str)
                        return -E2BIG;
                strncpy(name, start, len);
                name[len] = '\0';
-                for (pt = perm_types; pt->name; pt++) {
-                        if (!strcasecmp(name, pt->name)) {
-                                *perm |= pt->bit;
-                                break;
-                        }
-                }
-
-                if (!pt->name) {
-                        for (pt = noperm_types; pt->name; pt++) {
-                                if (!strcasecmp(name, pt->name)) {
-                                        *noperm |= pt->bit;
-                                        break;
-                                }
-                        }
-
-                        if (!pt->name) {
-                                printf("unkown type: %s\n", name);
-                                return -1;
-                        }
-                }
-
-                start = end + 1;
-        }
-        return 0;
+               for (pt = perm_types; pt->name; pt++) {
+                       if (!strcasecmp(name, pt->name)) {
+                               *perm |= pt->bit;
+                               break;
+                       }
+               }
+
+               if (!pt->name) {
+                       for (pt = noperm_types; pt->name; pt++) {
+                               if (!strcasecmp(name, pt->name)) {
+                                       *noperm |= pt->bit;
+                                       break;
+                               }
+                       }
+
+                       if (!pt->name) {
+                               printf("unkown type: %s\n", name);
+                               return -1;
+                       }
+               }
+
+               start = end + 1;
+       }
+       return 0;
 }
 
 static int
@@ -251,92 +255,93 @@ parse_perm_line(struct identity_downcall_data *data, char *line, size_t size)
        char uid_str[size];
        char nid_str[size];
        char perm_str[size];
-        lnet_nid_t nid;
-        __u32 perm, noperm;
-        int rc, i;
+       lnet_nid_t nid;
+       __u32 perm, noperm;
+       int rc, i;
 
-        if (data->idd_nperms >= N_PERMS_MAX) {
-                errlog("permission count %d > max %d\n",
-                        data->idd_nperms, N_PERMS_MAX);
-                return -1;
-        }
+       if (data->idd_nperms >= N_PERMS_MAX) {
+               errlog("permission count %d > max %d\n",
+                      data->idd_nperms, N_PERMS_MAX);
+               return -1;
+       }
 
        rc = sscanf(line, "%s %s %s", nid_str, uid_str, perm_str);
-        if (rc != 3) {
-                errlog("can't parse line %s\n", line);
-                return -1;
-        }
-
-        if (!match_uid(data->idd_uid, uid_str))
-                return 0;
-
-        if (!strcmp(nid_str, "*")) {
-                nid = LNET_NID_ANY;
-        } else {
-                nid = libcfs_str2nid(nid_str);
-                if (nid == LNET_NID_ANY) {
-                        errlog("can't parse nid %s\n", nid_str);
-                        return -1;
-                }
-        }
-
-        if (parse_perm(&perm, &noperm, perm_str)) {
-                errlog("invalid perm %s\n", perm_str);
-                return -1;
-        }
-
-        /* merge the perms with the same nid.
-         *
-         * If there is LNET_NID_ANY in data->idd_perms[i].pdd_nid,
-         * it must be data->idd_perms[0].pdd_nid, and act as default perm.
-         */
-        if (nid != LNET_NID_ANY) {
-                int found = 0;
-
-                /* search for the same nid */
-                for (i = data->idd_nperms - 1; i >= 0; i--) {
-                        if (data->idd_perms[i].pdd_nid == nid) {
-                                data->idd_perms[i].pdd_perm =
-                                        (data->idd_perms[i].pdd_perm | perm) &
-                                        ~noperm;
-                                found = 1;
-                                break;
-                        }
-                }
-
-                /* NOT found, add to tail */
-                if (!found) {
-                        data->idd_perms[data->idd_nperms].pdd_nid = nid;
-                        data->idd_perms[data->idd_nperms].pdd_perm =
-                                perm & ~noperm;
-                        data->idd_nperms++;
-                }
-        } else {
-                if (data->idd_nperms > 0) {
-                        /* the first one isn't LNET_NID_ANY, need exchange */
-                        if (data->idd_perms[0].pdd_nid != LNET_NID_ANY) {
-                                data->idd_perms[data->idd_nperms].pdd_nid =
-                                        data->idd_perms[0].pdd_nid;
-                                data->idd_perms[data->idd_nperms].pdd_perm =
-                                        data->idd_perms[0].pdd_perm;
-                                data->idd_perms[0].pdd_nid = LNET_NID_ANY;
-                                data->idd_perms[0].pdd_perm = perm & ~noperm;
-                                data->idd_nperms++;
-                        } else {
-                                /* only fix LNET_NID_ANY item */
-                                data->idd_perms[0].pdd_perm =
-                                        (data->idd_perms[0].pdd_perm | perm) &
-                                        ~noperm;
-                        }
-                } else {
-                        /* it is the first one, only add to head */
-                        data->idd_perms[0].pdd_nid = LNET_NID_ANY;
-                        data->idd_perms[0].pdd_perm = perm & ~noperm;
-                        data->idd_nperms = 1;
-                }
-        }
-
-        return 0;
+       if (rc != 3) {
+               errlog("can't parse line %s\n", line);
+               return -1;
+       }
+
+       if (!match_uid(data->idd_uid, uid_str))
+               return 0;
+
+       if (!strcmp(nid_str, "*")) {
+               nid = LNET_NID_ANY;
+       } else {
+               nid = libcfs_str2nid(nid_str);
+               if (nid == LNET_NID_ANY) {
+                       errlog("can't parse nid %s\n", nid_str);
+                       return -1;
+               }
+       }
+
+       if (parse_perm(&perm, &noperm, perm_str)) {
+               errlog("invalid perm %s\n", perm_str);
+               return -1;
+       }
+
+       /*
+        * merge the perms with the same nid.
+        *
+        * If there is LNET_NID_ANY in data->idd_perms[i].pdd_nid,
+        * it must be data->idd_perms[0].pdd_nid, and act as default perm.
+        */
+       if (nid != LNET_NID_ANY) {
+               int found = 0;
+
+               /* search for the same nid */
+               for (i = data->idd_nperms - 1; i >= 0; i--) {
+                       if (data->idd_perms[i].pdd_nid == nid) {
+                               data->idd_perms[i].pdd_perm =
+                                       (data->idd_perms[i].pdd_perm | perm) &
+                                       ~noperm;
+                               found = 1;
+                               break;
+                       }
+               }
+
+               /* NOT found, add to tail */
+               if (!found) {
+                       data->idd_perms[data->idd_nperms].pdd_nid = nid;
+                       data->idd_perms[data->idd_nperms].pdd_perm =
+                               perm & ~noperm;
+                       data->idd_nperms++;
+               }
+       } else {
+               if (data->idd_nperms > 0) {
+                       /* the first one isn't LNET_NID_ANY, need exchange */
+                       if (data->idd_perms[0].pdd_nid != LNET_NID_ANY) {
+                               data->idd_perms[data->idd_nperms].pdd_nid =
+                                       data->idd_perms[0].pdd_nid;
+                               data->idd_perms[data->idd_nperms].pdd_perm =
+                                       data->idd_perms[0].pdd_perm;
+                               data->idd_perms[0].pdd_nid = LNET_NID_ANY;
+                               data->idd_perms[0].pdd_perm = perm & ~noperm;
+                               data->idd_nperms++;
+                       } else {
+                               /* only fix LNET_NID_ANY item */
+                               data->idd_perms[0].pdd_perm =
+                                       (data->idd_perms[0].pdd_perm | perm) &
+                                       ~noperm;
+                       }
+               } else {
+                       /* it is the first one, only add to head */
+                       data->idd_perms[0].pdd_nid = LNET_NID_ANY;
+                       data->idd_perms[0].pdd_perm = perm & ~noperm;
+                       data->idd_nperms = 1;
+               }
+       }
+
+       return 0;
 }
 
 int get_perms(struct identity_downcall_data *data)
@@ -344,59 +349,57 @@ int get_perms(struct identity_downcall_data *data)
        FILE *fp;
        char line[PATH_MAX];
 
-        fp = fopen(PERM_PATHNAME, "r");
-        if (fp == NULL) {
-                if (errno == ENOENT) {
-                        return 0;
-                } else {
-                        errlog("open %s failed: %s\n",
-                               PERM_PATHNAME, strerror(errno));
-                        data->idd_err = errno;
-                        return -1;
-                }
-        }
+       fp = fopen(PERM_PATHNAME, "r");
+       if (!fp) {
+               if (errno == ENOENT)
+                       return 0;
+               errlog("open %s failed: %s\n",
+                      PERM_PATHNAME, strerror(errno));
+               data->idd_err = errno;
+               return -1;
+       }
 
        while (fgets(line, sizeof(line), fp)) {
-                if (comment_line(line))
-                        continue;
+               if (comment_line(line))
+                       continue;
 
                if (parse_perm_line(data, line, sizeof(line))) {
-                        errlog("parse line %s failed!\n", line);
-                        data->idd_err = EINVAL;
-                        fclose(fp);
-                        return -1;
-                }
-        }
-
-        fclose(fp);
-        return 0;
+                       errlog("parse line %s failed!\n", line);
+                       data->idd_err = EINVAL;
+                       fclose(fp);
+                       return -1;
+               }
+       }
+
+       fclose(fp);
+       return 0;
 }
 
 static void show_result(struct identity_downcall_data *data)
 {
-        int i;
+       int i;
 
-        if (data->idd_err) {
-                errlog("failed to get identity for uid %d: %s\n",
-                       data->idd_uid, strerror(data->idd_err));
-                return;
-        }
+       if (data->idd_err) {
+               errlog("failed to get identity for uid %d: %s\n",
+                      data->idd_uid, strerror(data->idd_err));
+               return;
+       }
 
-        printf("uid=%d gid=%d", data->idd_uid, data->idd_gid);
-        for (i = 0; i < data->idd_ngroups; i++)
-                printf(",%u", data->idd_groups[i]);
-        printf("\n");
-        printf("permissions:\n"
-               "  nid\t\t\tperm\n");
-        for (i = 0; i < data->idd_nperms; i++) {
-                struct perm_downcall_data *pdd;
+       printf("uid=%d gid=%d", data->idd_uid, data->idd_gid);
+       for (i = 0; i < data->idd_ngroups; i++)
+               printf(",%u", data->idd_groups[i]);
+       printf("\n");
+       printf("permissions:\n"
+              "  nid\t\t\tperm\n");
+       for (i = 0; i < data->idd_nperms; i++) {
+               struct perm_downcall_data *pdd;
 
-                pdd = &data->idd_perms[i];
+               pdd = &data->idd_perms[i];
 
                printf("  %#jx\t0x%x\n", (uintmax_t)pdd->pdd_nid,
                       pdd->pdd_perm);
-        }
-        printf("\n");
+       }
+       printf("\n");
 }
 
 int main(int argc, char **argv)
@@ -407,46 +410,46 @@ int main(int argc, char **argv)
        unsigned long uid;
        int fd, rc = -EINVAL, size, maxgroups;
 
-        progname = basename(argv[0]);
-        if (argc != 3) {
-                usage();
-                goto out;
-        }
-
-        uid = strtoul(argv[2], &end, 0);
-        if (*end) {
-                errlog("%s: invalid uid '%s'\n", progname, argv[2]);
-                goto out;
-        }
-
-        maxgroups = sysconf(_SC_NGROUPS_MAX);
-        if (maxgroups > NGROUPS_MAX)
-                maxgroups = NGROUPS_MAX;
+       progname = basename(argv[0]);
+       if (argc != 3) {
+               usage();
+               goto out;
+       }
+
+       uid = strtoul(argv[2], &end, 0);
+       if (*end) {
+               errlog("%s: invalid uid '%s'\n", progname, argv[2]);
+               goto out;
+       }
+
+       maxgroups = sysconf(_SC_NGROUPS_MAX);
+       if (maxgroups > NGROUPS_MAX)
+               maxgroups = NGROUPS_MAX;
        if (maxgroups == -1) {
                rc = -EINVAL;
                goto out;
        }
 
-        size = offsetof(struct identity_downcall_data, idd_groups[maxgroups]);
-        data = malloc(size);
-        if (!data) {
-                errlog("malloc identity downcall data(%d) failed!\n", size);
-                rc = -ENOMEM;
-                goto out;
-        }
-
-        memset(data, 0, size);
-        data->idd_magic = IDENTITY_DOWNCALL_MAGIC;
-        data->idd_uid = uid;
-        /* get groups for uid */
-        rc = get_groups_local(data, maxgroups);
-        if (rc)
-                goto downcall;
-
-        size = offsetof(struct identity_downcall_data,
-                        idd_groups[data->idd_ngroups]);
-        /* read permission database */
-        rc = get_perms(data);
+       size = offsetof(struct identity_downcall_data, idd_groups[maxgroups]);
+       data = malloc(size);
+       if (!data) {
+               errlog("malloc identity downcall data(%d) failed!\n", size);
+               rc = -ENOMEM;
+               goto out;
+       }
+
+       memset(data, 0, size);
+       data->idd_magic = IDENTITY_DOWNCALL_MAGIC;
+       data->idd_uid = uid;
+       /* get groups for uid */
+       rc = get_groups_local(data, maxgroups);
+       if (rc)
+               goto downcall;
+
+       size = offsetof(struct identity_downcall_data,
+                       idd_groups[data->idd_ngroups]);
+       /* read permission database */
+       rc = get_perms(data);
 
 downcall:
        if (strcmp(argv[1], "-d") == 0 || getenv("L_GETIDENTITY_TEST")) {
@@ -481,7 +484,7 @@ downcall:
 out_params:
        cfs_free_param_data(&path);
 out:
-       if (data != NULL)
+       if (data)
                free(data);
        return rc;
 }