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/getgrnam.c | 63 ++++++++++++++++++++++++++++++++----------------- 1 file changed, 42 insertions(+), 21 deletions(-) (limited to 'libc/pwd_grp/getgrnam.c') diff --git a/libc/pwd_grp/getgrnam.c b/libc/pwd_grp/getgrnam.c index 2c8d030e0..27d714407 100644 --- a/libc/pwd_grp/getgrnam.c +++ b/libc/pwd_grp/getgrnam.c @@ -1,6 +1,8 @@ +/* vi: set sw=4 ts=4: */ /* * getgrnam.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 @@ -18,6 +20,7 @@ * */ +#include #include #include #include @@ -27,39 +30,57 @@ #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); -#else +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; +#endif - -struct group *getgrnam(const char *name) +int getgrnam_r (const char *name, struct group *group, + char *buff, size_t buflen, struct group **result) { - int grp_fd; - struct group *group; + int ret; + int group_fd; + + *result = NULL; if (name == NULL) { - __set_errno(EINVAL); - return NULL; + return EINVAL; } - if ((grp_fd = open(_PATH_GROUP, O_RDONLY)) < 0) - return NULL; + if ((group_fd = open(_PATH_GROUP, O_RDONLY)) < 0) { + return ENOENT; + } - LOCK; - while ((group = __getgrent(grp_fd, line_buff, members)) != NULL) + while ((ret=__getgrent_r(group, buff, buflen, group_fd)) == 0) { if (!strcmp(group->gr_name, name)) { - close(grp_fd); - UNLOCK; - return group; + close(group_fd); + *result = group; + return 0; } + } + + close(group_fd); + return ret; +} - close(grp_fd); +struct group *getgrnam(const char *name) +{ + int ret; + static char line_buff[PWD_BUFFER_SIZE]; + static struct group grp; + struct group *result; + + LOCK; + if ((ret=getgrnam_r(name, &grp, line_buff, sizeof(line_buff), &result)) == 0) { + UNLOCK; + return &grp; + } + __set_errno(ret); UNLOCK; return NULL; } + + -- cgit v1.2.3