From 56104838733c81ba410ac41f767ac72267049489 Mon Sep 17 00:00:00 2001 From: Eric Andersen Date: Fri, 10 Oct 2003 07:34:27 +0000 Subject: Implement getgrgid_r and getgrnam_r. Rework group handling code to be fully reentrant, since there was still a shared static value. indent stuff, --- libc/pwd_grp/getgrgid.c | 60 ++++++++++++++++++++++++++++--------------------- 1 file changed, 35 insertions(+), 25 deletions(-) (limited to 'libc/pwd_grp/getgrgid.c') diff --git a/libc/pwd_grp/getgrgid.c b/libc/pwd_grp/getgrgid.c index 3575a749b..b8f2a29f5 100644 --- a/libc/pwd_grp/getgrgid.c +++ b/libc/pwd_grp/getgrgid.c @@ -1,6 +1,8 @@ +/* vi: set sw=4 ts=4: */ /* * getgrgid.c - This file is part of the libc-8086/grp package for ELKS, * Copyright (C) 1995, 1996 Nat Friedman . + * Copyright (C) 2001-2003 Erik Andersen * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Library General Public @@ -22,48 +24,56 @@ #include #include #include +#include #include "config.h" #ifdef __UCLIBC_HAS_THREADS__ #include -extern pthread_mutex_t __getgrent_lock; -# define LOCK pthread_mutex_lock(&__getgrent_lock) -# define UNLOCK pthread_mutex_unlock(&__getgrent_lock); +static pthread_mutex_t mylock = PTHREAD_MUTEX_INITIALIZER; +# define LOCK pthread_mutex_lock(&mylock) +# define UNLOCK pthread_mutex_unlock(&mylock); #else # define LOCK # define UNLOCK #endif -static char *line_buff = NULL; -static char **members = NULL; -struct group *getgrgid(const gid_t gid) +/* Search for an entry with a matching group ID. */ +int getgrgid_r (gid_t gid, struct group *group, char *buffer, + size_t buflen, struct group **result) { - struct group *group; - int grp_fd; + int grp_fd; + if ((grp_fd = open(_PATH_GROUP, O_RDONLY)) < 0) + return errno; - if ((grp_fd = open(_PATH_GROUP, O_RDONLY)) < 0) - return NULL; - - LOCK; - while ((group = __getgrent(grp_fd, line_buff, members)) != NULL) - if (group->gr_gid == gid) { - close(grp_fd); - UNLOCK; - return group; + *result = NULL; + while (__getgrent_r(group, buffer, buflen, grp_fd) == 0) { + if (group->gr_gid == gid) { + close(grp_fd); + *result = group; + return 0; + } } - close(grp_fd); - UNLOCK; - return NULL; + close(grp_fd); + return EINVAL; } -#if 0 -/* Search for an entry with a matching group ID. */ -int getgrgid_r (gid_t gid, struct group *resultbuf, char *buffer, - size_t buflen, struct group **result) +struct group *getgrgid(const gid_t gid) { + int ret; + struct group *result; + static struct group grp; + static char line_buff[GRP_BUFFER_SIZE]; + LOCK; + if ((ret=getgrgid_r(gid, &grp, line_buff, sizeof(line_buff), &result)) == 0) { + UNLOCK; + return &grp; + } + UNLOCK; + __set_errno(ret); + return NULL; } -#endif + -- cgit v1.2.3