From fabd08e8543c01b41f277eb2eb9f731c7b424c36 Mon Sep 17 00:00:00 2001 From: Eric Andersen Date: Sat, 1 Nov 2003 04:40:10 +0000 Subject: Fix things (properly) to open /etc/passd and /etc/group if they have not yet been opened. My last try was completely and embarrasingly broken. -Erik --- libc/pwd_grp/grent.c | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) (limited to 'libc/pwd_grp/grent.c') diff --git a/libc/pwd_grp/grent.c b/libc/pwd_grp/grent.c index 128520e8c..e05459c2d 100644 --- a/libc/pwd_grp/grent.c +++ b/libc/pwd_grp/grent.c @@ -26,6 +26,7 @@ * in together. */ +#define _GNU_SOURCE #include #include #include @@ -35,7 +36,7 @@ #ifdef __UCLIBC_HAS_THREADS__ #include -static pthread_mutex_t mylock = PTHREAD_MUTEX_INITIALIZER; +static pthread_mutex_t mylock = PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP; # define LOCK pthread_mutex_lock(&mylock) # define UNLOCK pthread_mutex_unlock(&mylock); #else @@ -43,12 +44,13 @@ static pthread_mutex_t mylock = PTHREAD_MUTEX_INITIALIZER; # define UNLOCK #endif -static int grp_fd = -1; +/* file descriptor for the group file currently open */ +static int grp_fd = -9; void setgrent(void) { LOCK; - if (grp_fd != -1) + if (grp_fd > -1) close(grp_fd); grp_fd = open(_PATH_GROUP, O_RDONLY); UNLOCK; @@ -57,7 +59,7 @@ void setgrent(void) void endgrent(void) { LOCK; - if (grp_fd != -1) + if (grp_fd > -1) close(grp_fd); grp_fd = -1; UNLOCK; @@ -70,6 +72,10 @@ struct group *getgrent(void) static char line_buff[PWD_BUFFER_SIZE]; LOCK; + /* Open /etc/group if it has never been opened */ + if (grp_fd == -9) { + setgrent(); + } if (grp_fd == -1) { UNLOCK; return NULL; -- cgit v1.2.3