From: millert Date: Sun, 16 Sep 2018 02:41:16 +0000 (+0000) Subject: Use user_from_uid(3), group_from_gid(3), uid_from_user(3) and X-Git-Url: http://artulab.com/gitweb/?a=commitdiff_plain;h=9cf561553267af28d67ab94def477e8575771dc9;p=openbsd Use user_from_uid(3), group_from_gid(3), uid_from_user(3) and gid_from_group(3) to avoid repeatedly looking up the same user/group. Also keep the passwd and group files open to avoid opening and closing them all the time. OK tb@ --- diff --git a/usr.sbin/mtree/create.c b/usr.sbin/mtree/create.c index e2a25e3d4dc..45feab40277 100644 --- a/usr.sbin/mtree/create.c +++ b/usr.sbin/mtree/create.c @@ -1,5 +1,5 @@ /* $NetBSD: create.c,v 1.11 1996/09/05 09:24:19 mycroft Exp $ */ -/* $OpenBSD: create.c,v 1.32 2016/08/16 16:41:46 krw Exp $ */ +/* $OpenBSD: create.c,v 1.33 2018/09/16 02:41:16 millert Exp $ */ /*- * Copyright (c) 1989, 1993 @@ -127,11 +127,10 @@ cwalk(void) static void statf(int indent, FTSENT *p) { - struct group *gr; - struct passwd *pw; u_int32_t len, val; int fd, offset; - char *name, *escaped_name; + const char *name; + char *escaped_name; size_t esc_len; esc_len = p->fts_namelen * 4 + 1; @@ -157,8 +156,9 @@ statf(int indent, FTSENT *p) output(indent, &offset, "type=%s", inotype(p->fts_statp->st_mode)); if (p->fts_statp->st_uid != uid) { if (keys & F_UNAME) { - if ((pw = getpwuid(p->fts_statp->st_uid)) != NULL) { - output(indent, &offset, "uname=%s", pw->pw_name); + name = user_from_uid(p->fts_statp->st_uid, 1); + if (name != NULL) { + output(indent, &offset, "uname=%s", name); } else { error("could not get uname for uid=%u", p->fts_statp->st_uid); @@ -169,8 +169,9 @@ statf(int indent, FTSENT *p) } if (p->fts_statp->st_gid != gid) { if (keys & F_GNAME) { - if ((gr = getgrgid(p->fts_statp->st_gid)) != NULL) { - output(indent, &offset, "gname=%s", gr->gr_name); + name = group_from_gid(p->fts_statp->st_gid, 1); + if (name != NULL) { + output(indent, &offset, "gname=%s", name); } else { error("could not get gname for gid=%u", p->fts_statp->st_gid); @@ -270,8 +271,6 @@ statd(FTS *t, FTSENT *parent, uid_t *puid, gid_t *pgid, mode_t *pmode) gid_t sgid; uid_t suid; mode_t smode; - struct group *gr; - struct passwd *pw; gid_t savegid = *pgid; uid_t saveuid = *puid; mode_t savemode = *pmode; @@ -281,6 +280,7 @@ statd(FTS *t, FTSENT *parent, uid_t *puid, gid_t *pgid, mode_t *pmode) gid_t g[MAXGID]; uid_t u[MAXUID]; mode_t m[MAXMODE]; + const char *name; static int first = 1; if ((p = fts_children(t, 0)) == NULL) { @@ -327,16 +327,16 @@ statd(FTS *t, FTSENT *parent, uid_t *puid, gid_t *pgid, mode_t *pmode) else (void)printf("/set type=file"); if (keys & F_UNAME) { - if ((pw = getpwuid(saveuid)) != NULL) - (void)printf(" uname=%s", pw->pw_name); + if ((name = user_from_uid(saveuid, 1)) != NULL) + (void)printf(" uname=%s", name); else error("could not get uname for uid=%u", saveuid); } if (keys & F_UID) (void)printf(" uid=%u", saveuid); if (keys & F_GNAME) { - if ((gr = getgrgid(savegid)) != NULL) - (void)printf(" gname=%s", gr->gr_name); + if ((name = group_from_gid(savegid, 1)) != NULL) + (void)printf(" gname=%s", name); else error("could not get gname for gid=%u", savegid); } diff --git a/usr.sbin/mtree/mtree.c b/usr.sbin/mtree/mtree.c index 19b231a7e87..b0344412f6b 100644 --- a/usr.sbin/mtree/mtree.c +++ b/usr.sbin/mtree/mtree.c @@ -1,4 +1,4 @@ -/* $OpenBSD: mtree.c,v 1.24 2015/12/20 19:53:24 benno Exp $ */ +/* $OpenBSD: mtree.c,v 1.25 2018/09/16 02:41:16 millert Exp $ */ /* $NetBSD: mtree.c,v 1.7 1996/09/05 23:29:22 thorpej Exp $ */ /*- @@ -155,6 +155,10 @@ main(int argc, char *argv[]) } } + /* Keep passwd and group files open for faster lookups. */ + setpassent(1); + setgroupent(1); + if (dir && chdir(dir)) error("%s: %s", dir, strerror(errno)); diff --git a/usr.sbin/mtree/spec.c b/usr.sbin/mtree/spec.c index de234b4ab46..34864b0a541 100644 --- a/usr.sbin/mtree/spec.c +++ b/usr.sbin/mtree/spec.c @@ -1,5 +1,5 @@ /* $NetBSD: spec.c,v 1.6 1995/03/07 21:12:12 cgd Exp $ */ -/* $OpenBSD: spec.c,v 1.28 2016/08/16 16:41:46 krw Exp $ */ +/* $OpenBSD: spec.c,v 1.29 2018/09/16 02:41:16 millert Exp $ */ /*- * Copyright (c) 1989, 1993 @@ -169,8 +169,6 @@ set(char *t, NODE *ip) { int type; char *kw, *val = NULL; - struct group *gr; - struct passwd *pw; void *m; int value; u_int32_t fset, fclr; @@ -207,9 +205,8 @@ set(char *t, NODE *ip) error("invalid gid %s", val); break; case F_GNAME: - if ((gr = getgrnam(val)) == NULL) + if (gid_from_group(val, &ip->st_gid) == -1) error("unknown group %s", val); - ip->st_gid = gr->gr_gid; break; case F_IGN: /* just set flag bit */ @@ -302,9 +299,8 @@ set(char *t, NODE *ip) error("invalid uid %s", val); break; case F_UNAME: - if ((pw = getpwnam(val)) == NULL) + if (uid_from_user(val, &ip->st_uid) == -1) error("unknown user %s", val); - ip->st_uid = pw->pw_uid; break; } }