Use user_from_uid(3) and group_from_gid(3) in a few more places
authormillert <millert@openbsd.org>
Sun, 16 Sep 2018 02:43:11 +0000 (02:43 +0000)
committermillert <millert@openbsd.org>
Sun, 16 Sep 2018 02:43:11 +0000 (02:43 +0000)
that do repeated lookups.  OK tb@

sbin/fsck_ext2fs/inode.c
sbin/fsck_ffs/inode.c
sbin/fsdb/fsdbutil.c
usr.bin/fstat/fuser.c
usr.sbin/vmctl/vmctl.c

index 88338ee..d77160f 100644 (file)
@@ -1,4 +1,4 @@
-/*     $OpenBSD: inode.c,v 1.27 2016/12/16 17:44:59 krw Exp $  */
+/*     $OpenBSD: inode.c,v 1.28 2018/09/16 02:43:11 millert Exp $      */
 /*     $NetBSD: inode.c,v 1.8 2000/01/28 16:01:46 bouyer Exp $ */
 
 /*
@@ -559,10 +559,7 @@ void
 pinode(ino_t ino)
 {
        struct ext2fs_dinode *dp;
-       char *p;
-#ifndef SMALL
-       struct passwd *pw;
-#endif
+       const char *p;
        time_t t;
        u_int32_t uid;
 
@@ -573,11 +570,11 @@ pinode(ino_t ino)
        printf(" OWNER=");
        uid = letoh16(dp->e2di_uid_low) | (letoh16(dp->e2di_uid_high) << 16);
 #ifndef SMALL
-       if ((pw = getpwuid((int)uid)) != 0)
-               printf("%s ", pw->pw_name);
+       if ((p = user_from_uid(uid, 1)) != NULL)
+               printf("%s ", p);
        else
 #endif
-               printf("%u ", (unsigned)uid);
+               printf("%u ", uid);
        printf("MODE=%o\n", letoh16(dp->e2di_mode));
        if (preen)
                printf("%s: ", cdevname());
index 39695b5..bdac654 100644 (file)
@@ -1,4 +1,4 @@
-/*     $OpenBSD: inode.c,v 1.48 2016/12/16 17:44:59 krw Exp $  */
+/*     $OpenBSD: inode.c,v 1.49 2018/09/16 02:43:11 millert Exp $      */
 /*     $NetBSD: inode.c,v 1.23 1996/10/11 20:15:47 thorpej Exp $       */
 
 /*
@@ -525,10 +525,7 @@ void
 pinode(ino_t ino)
 {
        union dinode *dp;
-       char *p;
-#ifndef SMALL
-       struct passwd *pw;
-#endif
+       const char *p;
        time_t t;
 
        printf(" I=%llu ", (unsigned long long)ino);
@@ -537,8 +534,8 @@ pinode(ino_t ino)
        dp = ginode(ino);
        printf(" OWNER=");
 #ifndef SMALL
-       if ((pw = getpwuid(DIP(dp, di_uid))) != 0)
-               printf("%s ", pw->pw_name);
+       if ((p = user_from_uid(DIP(dp, di_uid), 1)) != NULL)
+               printf("%s ", p);
        else
 #endif
                printf("%u ", (unsigned)DIP(dp, di_uid));
index b0c6fe2..32760e7 100644 (file)
@@ -1,4 +1,4 @@
-/*     $OpenBSD: fsdbutil.c,v 1.18 2017/07/29 21:14:56 fcambus Exp $   */
+/*     $OpenBSD: fsdbutil.c,v 1.19 2018/09/16 02:43:11 millert Exp $   */
 /*     $NetBSD: fsdbutil.c,v 1.5 1996/09/28 19:30:37 christos Exp $    */
 
 /*-
@@ -83,8 +83,7 @@ argcount(struct cmdtable *cmdp, int argc, char *argv[])
 void
 printstat(const char *cp, ino_t inum, union dinode *dp)
 {
-       struct group *grp;
-       struct passwd *pw;
+       const char *name;
        time_t t;
        char *p;
 
@@ -139,12 +138,12 @@ printstat(const char *cp, ino_t inum, union dinode *dp)
        printf("\n\tATIME=%15.15s %4.4s [%d nsec]\n", &p[4], &p[20],
            DIP(dp, di_atimensec));
 
-       if ((pw = getpwuid(DIP(dp, di_uid))))
-               printf("OWNER=%s ", pw->pw_name);
+       if ((name = user_from_uid(DIP(dp, di_uid), 1)) != NULL)
+               printf("OWNER=%s ", name);
        else
                printf("OWNUID=%u ", DIP(dp, di_uid));
-       if ((grp = getgrgid(DIP(dp, di_gid))))
-               printf("GRP=%s ", grp->gr_name);
+       if ((name = group_from_gid(DIP(dp, di_gid), 1)) != NULL)
+               printf("GRP=%s ", name);
        else
                printf("GID=%u ", DIP(dp, di_gid));
 
index 96212e6..745d65a 100644 (file)
@@ -1,4 +1,4 @@
-/*     $OpenBSD: fuser.c,v 1.6 2015/01/16 06:40:08 deraadt Exp $       */
+/*     $OpenBSD: fuser.c,v 1.7 2018/09/16 02:43:11 millert Exp $       */
 
 /*
  * Copyright (c) 2009 Todd C. Miller <Todd.Miller@courtesan.com>
@@ -133,7 +133,7 @@ fuser_check(struct kinfo_file *kf)
 static void
 printfu(struct fuser *fu)
 {
-       struct passwd *pwd;
+       const char *name;
 
        printf("%d", fu->pid);
        fflush(stdout);
@@ -148,11 +148,11 @@ printfu(struct fuser *fu)
                fprintf(stderr, "t");
 
        if (uflg) {
-               pwd = getpwuid(fu->uid);
-               if (pwd != NULL)
-                       fprintf(stderr, "(%s)", pwd->pw_name);
+               name = user_from_uid(fu->uid, 1);
+               if (name != NULL)
+                       fprintf(stderr, "(%s)", name);
                else
-                       fprintf(stderr, "(%d)", fu->uid);
+                       fprintf(stderr, "(%u)", fu->uid);
        }
 
        putchar(' ');
index 0ef71a5..4fffa11 100644 (file)
@@ -1,4 +1,4 @@
-/*     $OpenBSD: vmctl.c,v 1.57 2018/09/11 04:03:16 ccardenas Exp $    */
+/*     $OpenBSD: vmctl.c,v 1.58 2018/09/16 02:43:11 millert Exp $      */
 
 /*
  * Copyright (c) 2014 Mike Larkin <mlarkin@openbsd.org>
@@ -631,8 +631,7 @@ print_vm_info(struct vmop_info_result *list, size_t ct)
        char curmem[FMT_SCALED_STRSIZE];
        char maxmem[FMT_SCALED_STRSIZE];
        char user[16], group[16];
-       struct passwd *pw;
-       struct group *gr;
+       const char *name;
 
        printf("%5s %5s %5s %7s %7s %7s %12s %s\n", "ID", "PID", "VCPUS",
            "MAXMEM", "CURMEM", "TTY", "OWNER", "NAME");
@@ -642,22 +641,23 @@ print_vm_info(struct vmop_info_result *list, size_t ct)
                vir = &vmi->vir_info;
                if (check_info_id(vir->vir_name, vir->vir_id)) {
                        /* get user name */
-                       if ((pw = getpwuid(vmi->vir_uid)) == NULL)
+                       name = user_from_uid(vmi->vir_uid, 1);
+                       if (name == NULL)
                                (void)snprintf(user, sizeof(user),
                                    "%d", vmi->vir_uid);
                        else
-                               (void)strlcpy(user, pw->pw_name,
-                                   sizeof(user));
+                               (void)strlcpy(user, name, sizeof(user));
                        /* get group name */
                        if (vmi->vir_gid != -1) {
                                if (vmi->vir_uid == 0)
                                        *user = '\0';
-                               if ((gr = getgrgid(vmi->vir_gid)) == NULL)
+                               name = group_from_gid(vmi->vir_gid, 1);
+                               if (name == NULL)
                                        (void)snprintf(group, sizeof(group),
                                            ":%lld", vmi->vir_gid);
                                else
                                        (void)snprintf(group, sizeof(group),
-                                           ":%s", gr->gr_name);
+                                           ":%s", name);
                                (void)strlcat(user, group, sizeof(user));
                        }