[uClibc-cvs] uClibc/libc/pwd_grp __getgrent_r.c, 1.2, 1.3 __getpwent_r.c, 1.6, 1.7 grent.c, 1.10, 1.11 pwent.c, 1.14, 1.15

Erik Andersen andersen at uclibc.org
Sat Nov 1 04:40:13 UTC 2003


Update of /var/cvs/uClibc/libc/pwd_grp
In directory winder:/tmp/cvs-serv16895

Modified Files:
	__getgrent_r.c __getpwent_r.c grent.c pwent.c 
Log Message:
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


Index: pwent.c
===================================================================
RCS file: /var/cvs/uClibc/libc/pwd_grp/pwent.c,v
retrieving revision 1.14
retrieving revision 1.15
diff -u -d -r1.14 -r1.15
--- pwent.c	31 Oct 2003 20:17:01 -0000	1.14
+++ pwent.c	1 Nov 2003 04:40:10 -0000	1.15
@@ -20,6 +20,13 @@
  *
  */
 
+/*
+ * setpwent(), endpwent(), and getpwent() are included in the same object
+ * file, since one cannot be used without the other two, so it makes sense to
+ * link them all in together.
+ */
+
+#define _GNU_SOURCE
 #include <features.h>
 #include <unistd.h>
 #include <stdlib.h>
@@ -30,7 +37,7 @@
 
 #ifdef __UCLIBC_HAS_THREADS__
 #include <pthread.h>
-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       
@@ -38,21 +45,14 @@
 # define UNLOCK
 #endif      
 
-/*
- * setpwent(), endpwent(), and getpwent() are included in the same object
- * file, since one cannot be used without the other two, so it makes sense to
- * link them all in together.
- */
-
 /* file descriptor for the password file currently open */
-static int pw_fd = -1;
+static int pw_fd = -9;
 
 void setpwent(void)
 {
 	LOCK;
-	if (pw_fd != -1)
+	if (pw_fd > -1)
 		close(pw_fd);
-
 	pw_fd = open(_PATH_PASSWD, O_RDONLY);
 	UNLOCK;
 }
@@ -60,7 +60,7 @@
 void endpwent(void)
 {
 	LOCK;
-	if (pw_fd != -1)
+	if (pw_fd > -1)
 		close(pw_fd);
 	pw_fd = -1;
 	UNLOCK;
@@ -86,13 +86,25 @@
 struct passwd *getpwent(void)
 {
 	int ret;
-	static char line_buff[PWD_BUFFER_SIZE];
-	static struct passwd pwd;
 	struct passwd *result;
+	static struct passwd pwd;
+	static char line_buff[PWD_BUFFER_SIZE];
 
-	if ((ret=getpwent_r(&pwd, line_buff, sizeof(line_buff), &result)) == 0) {
+	LOCK;
+	/* Open /etc/passwd if not yet opened */
+	if (pw_fd == -9) {
+		setpwent();
+	}
+	if (pw_fd == -1) {
+		UNLOCK;
+		return NULL;
+	}
+	ret=getpwent_r(&pwd, line_buff, sizeof(line_buff), &result);
+	if (ret == 0) {
+		UNLOCK;
 		return &pwd;
 	}
+	UNLOCK;
 	__set_errno(ret);
 	return NULL;
 }

Index: grent.c
===================================================================
RCS file: /var/cvs/uClibc/libc/pwd_grp/grent.c,v
retrieving revision 1.10
retrieving revision 1.11
diff -u -d -r1.10 -r1.11
--- grent.c	10 Oct 2003 07:34:27 -0000	1.10
+++ grent.c	1 Nov 2003 04:40:10 -0000	1.11
@@ -26,6 +26,7 @@
  * in together.
  */
 
+#define _GNU_SOURCE
 #include <features.h>
 #include <unistd.h>
 #include <fcntl.h>
@@ -35,7 +36,7 @@
 
 #ifdef __UCLIBC_HAS_THREADS__
 #include <pthread.h>
-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 @@
 # 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 endgrent(void)
 {
 	LOCK;
-	if (grp_fd != -1)
+	if (grp_fd > -1)
 		close(grp_fd);
 	grp_fd = -1;
 	UNLOCK;
@@ -70,6 +72,10 @@
 	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;

Index: __getpwent_r.c
===================================================================
RCS file: /var/cvs/uClibc/libc/pwd_grp/__getpwent_r.c,v
retrieving revision 1.6
retrieving revision 1.7
diff -u -d -r1.6 -r1.7
--- __getpwent_r.c	31 Oct 2003 23:50:25 -0000	1.6
+++ __getpwent_r.c	1 Nov 2003 04:40:10 -0000	1.7
@@ -47,9 +47,6 @@
 		return ERANGE;
 	}
 
-	if (pwd_fd == -1)
-		setpwent();
-
 	/* We use the restart label to handle malformatted lines */
 restart:
 	/* Read the passwd line into the buffer for processing */

Index: __getgrent_r.c
===================================================================
RCS file: /var/cvs/uClibc/libc/pwd_grp/__getgrent_r.c,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -d -r1.2 -r1.3
--- __getgrent_r.c	31 Oct 2003 20:17:01 -0000	1.2
+++ __getgrent_r.c	1 Nov 2003 04:40:10 -0000	1.3
@@ -44,9 +44,6 @@
 		return ERANGE;
 	}
 
-	if (grp_fd==-1)
-		setgrent();
-
 	/* We use the restart label to handle malformatted lines */
 restart:
 	/* Read the group line into the buffer for processing */



More information about the uClibc-cvs mailing list