From dee3187183d4449f0a83e69d8a155ed2cc345fdd Mon Sep 17 00:00:00 2001 From: millert Date: Sun, 16 Sep 2018 02:43:11 +0000 Subject: [PATCH] Use user_from_uid(3) and group_from_gid(3) in a few more places that do repeated lookups. OK tb@ --- sbin/fsck_ext2fs/inode.c | 13 +++++-------- sbin/fsck_ffs/inode.c | 11 ++++------- sbin/fsdb/fsdbutil.c | 13 ++++++------- usr.bin/fstat/fuser.c | 12 ++++++------ usr.sbin/vmctl/vmctl.c | 16 ++++++++-------- 5 files changed, 29 insertions(+), 36 deletions(-) diff --git a/sbin/fsck_ext2fs/inode.c b/sbin/fsck_ext2fs/inode.c index 88338eed528..d77160fa11c 100644 --- a/sbin/fsck_ext2fs/inode.c +++ b/sbin/fsck_ext2fs/inode.c @@ -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()); diff --git a/sbin/fsck_ffs/inode.c b/sbin/fsck_ffs/inode.c index 39695b54625..bdac6546e33 100644 --- a/sbin/fsck_ffs/inode.c +++ b/sbin/fsck_ffs/inode.c @@ -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)); diff --git a/sbin/fsdb/fsdbutil.c b/sbin/fsdb/fsdbutil.c index b0c6fe2c80a..32760e72cc5 100644 --- a/sbin/fsdb/fsdbutil.c +++ b/sbin/fsdb/fsdbutil.c @@ -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)); diff --git a/usr.bin/fstat/fuser.c b/usr.bin/fstat/fuser.c index 96212e6ece3..745d65a9478 100644 --- a/usr.bin/fstat/fuser.c +++ b/usr.bin/fstat/fuser.c @@ -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 @@ -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(' '); diff --git a/usr.sbin/vmctl/vmctl.c b/usr.sbin/vmctl/vmctl.c index 0ef71a55890..4fffa110469 100644 --- a/usr.sbin/vmctl/vmctl.c +++ b/usr.sbin/vmctl/vmctl.c @@ -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 @@ -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)); } -- 2.20.1