Follow @Openwall on Twitter for new release announcements and other news
[<prev] [next>] [<thread-prev] [day] [month] [year] [list]
Message-Id: <1423614776-25579-2-git-send-email-josiahw@gmail.com>
Date: Tue, 10 Feb 2015 18:32:56 -0600
From: Josiah Worcester <josiahw@...il.com>
To: musl@...ts.openwall.com
Cc: Josiah Worcester <josiahw@...il.com>
Subject: [PATCH 2/2] Refactor group functions.

---
 src/passwd/fgetgrent.c  |  4 +++-
 src/passwd/getgr_a.c    | 30 ++++++++++++++++++++++++++++++
 src/passwd/getgr_r.c    | 39 +++++++++++++--------------------------
 src/passwd/getgrent.c   | 32 +++++++++++++-------------------
 src/passwd/getgrent_a.c |  9 +++++++--
 src/passwd/pwf.h        |  3 ++-
 6 files changed, 68 insertions(+), 49 deletions(-)
 create mode 100644 src/passwd/getgr_a.c

diff --git a/src/passwd/fgetgrent.c b/src/passwd/fgetgrent.c
index d8d1c77..49b45c8 100644
--- a/src/passwd/fgetgrent.c
+++ b/src/passwd/fgetgrent.c
@@ -5,6 +5,8 @@ struct group *fgetgrent(FILE *f)
 {
 	static char *line, **mem;
 	static struct group gr;
+	struct passwd *res;
 	size_t size=0, nmem=0;
-	return __getgrent_a(f, &gr, &line, &size, &mem, &nmem);
+	__getgrent_a(f, &gr, &line, &size, &mem, &nmem, &res);
+	return res;
 }
diff --git a/src/passwd/getgr_a.c b/src/passwd/getgr_a.c
new file mode 100644
index 0000000..7760d4a
--- /dev/null
+++ b/src/passwd/getgr_a.c
@@ -0,0 +1,30 @@
+#include <pthread.h>
+#include "pwf.h"
+
+int getgr_a(const char *name, gid_t gid, struct group *gr, char **buf, size_t *size, char ***mem, size_t *nmem, struct group **res)
+{
+	FILE *f;
+	int rv = 0;
+	int cs;
+
+	pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &cs);
+
+	f = fopen("/etc/group", "rbe");
+	if (!f) {
+		rv = errno;
+		goto done;
+	}
+
+	while (!(rv = __getgrent_a(f, gr, buf, size, mem, nmem, res)) && *res) {
+		if (name && !strcmp(name, (*res)->gr_name)
+		|| !name && (*res)->gr_gid == gid) {
+			break;
+		}
+	}
+	fclose(f);
+
+done:
+	pthread_setcancelstate(cs, 0);
+	if (rv) errno = rv;
+	return rv;
+}
diff --git a/src/passwd/getgr_r.c b/src/passwd/getgr_r.c
index 3fe2e2b..c9b71b7 100644
--- a/src/passwd/getgr_r.c
+++ b/src/passwd/getgr_r.c
@@ -16,32 +16,19 @@ static int getgr_r(const char *name, gid_t gid, struct group *gr, char *buf, siz
 
 	pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &cs);
 
-	f = fopen("/etc/group", "rbe");
-	if (!f) {
-		rv = errno;
-		goto done;
-	}
-
-	*res = 0;
-	while (__getgrent_a(f, gr, &line, &len, &mem, &nmem)) {
-		if (name && !strcmp(name, gr->gr_name)
-		|| !name && gr->gr_gid == gid) {
-			if (size < len + (nmem+1)*sizeof(char *) + 32) {
-				rv = ERANGE;
-				break;
-			}
-			*res = gr;
-			buf += (16-(uintptr_t)buf)%16;
-			gr->gr_mem = (void *)buf;
-			buf += (nmem+1)*sizeof(char *);
-			memcpy(buf, line, len);
-			FIX(name);
-			FIX(passwd);
-			for (i=0; mem[i]; i++)
-				gr->gr_mem[i] = mem[i]-line+buf;
-			gr->gr_mem[i] = 0;
-			break;
-		}
+	rv = __getgr_a(name, gid, gr, &line, &len, &mem, &nmem, res);
+	if (!rv && size < len + (nmem+1)*sizeof(char *) + 32)
+		rv = ERANGE;
+	if (!rv) {
+		buf += (16-(uintptr_t)buf)%16;
+		gr->gr_mem = (void *)buf;
+		buf += (nmem+1)*sizeof(char *);
+		memcpy(buf, line, len);
+		FIX(name);
+		FIX(passwd);
+		for (i=0; mem[i]; i++)
+			gr->gr_mem[i] = mem[i]-line+buf;
+		gr->gr_mem[i] = 0;
 	}
  	free(mem);
  	free(line);
diff --git a/src/passwd/getgrent.c b/src/passwd/getgrent.c
index 429a3e5..835b9ab 100644
--- a/src/passwd/getgrent.c
+++ b/src/passwd/getgrent.c
@@ -1,6 +1,8 @@
 #include "pwf.h"
 
 static FILE *f;
+static char *line, **mem;
+static struct group gr;
 
 void setgrent()
 {
@@ -12,34 +14,26 @@ weak_alias(setgrent, endgrent);
 
 struct group *getgrent()
 {
-	static char *line, **mem;
-	static struct group gr;
+	struct group *res;
 	size_t size=0, nmem=0;
 	if (!f) f = fopen("/etc/group", "rbe");
 	if (!f) return 0;
-	return __getgrent_a(f, &gr, &line, &size, &mem, &nmem);
+	__getgrent_a(f, &gr, &line, &size, &mem, &nmem, &res);
+	return res;
 }
 
 struct group *getgrgid(gid_t gid)
 {
-	struct group *gr;
-	int errno_saved;
-	setgrent();
-	while ((gr=getgrent()) && gr->gr_gid != gid);
-	errno_saved = errno;
-	endgrent();
-	errno = errno_saved;
-	return gr;
+	struct group *res;
+	size_t size=0, nmem=0;
+	__getgr_a(0, gid, &gr, &line, &size, &mem, &nmem, &res);
+	return res;
 }
 
 struct group *getgrnam(const char *name)
 {
-	struct group *gr;
-	int errno_saved;
-	setgrent();
-	while ((gr=getgrent()) && strcmp(gr->gr_name, name));
-	errno_saved = errno;
-	endgrent();
-	errno = errno_saved;
-	return gr;
+	struct group *res;
+	size_t size=0, nmem=0;
+	__getgr_a(name, 0, &gr, &line, &size, &mem, &nmem, &res);
+	return res;
 }
diff --git a/src/passwd/getgrent_a.c b/src/passwd/getgrent_a.c
index 2cb8521..f94cc34 100644
--- a/src/passwd/getgrent_a.c
+++ b/src/passwd/getgrent_a.c
@@ -8,15 +8,17 @@ static unsigned atou(char **s)
 	return x;
 }
 
-struct group *__getgrent_a(FILE *f, struct group *gr, char **line, size_t *size, char ***mem, size_t *nmem)
+int __getgrent_a(FILE *f, struct group *gr, char **line, size_t *size, char ***mem, size_t *nmem, struct group **res)
 {
 	ssize_t l;
 	char *s, *mems;
 	size_t i;
+	int rv;
 	int cs;
 	pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &cs);
 	for (;;) {
 		if ((l=getline(line, size, f)) < 0) {
+			rv = errno;
 			free(*line);
 			*line = 0;
 			gr = 0;
@@ -43,6 +45,7 @@ struct group *__getgrent_a(FILE *f, struct group *gr, char **line, size_t *size,
 	free(*mem);
 	*mem = calloc(sizeof(char *), *nmem+1);
 	if (!*mem) {
+		rv = errno;
 		free(*line);
 		*line = 0;
 		return 0;
@@ -58,5 +61,7 @@ struct group *__getgrent_a(FILE *f, struct group *gr, char **line, size_t *size,
 	gr->gr_mem = *mem;
 end:
 	pthread_setcancelstate(cs, 0);
-	return gr;
+	*res = gr;
+	if(rv) errno = rv;
+	return rv;
 }
diff --git a/src/passwd/pwf.h b/src/passwd/pwf.h
index 806ffd3..fc63db2 100644
--- a/src/passwd/pwf.h
+++ b/src/passwd/pwf.h
@@ -10,5 +10,6 @@
 
 int __getpwent_a(FILE *f, struct passwd *pw, char **line, size_t *size, struct passwd **res);
 int __getpw_a(const char *name, uid_t uid, struct passwd *pw, char **buf, size_t *size, struct passwd **res);
-struct group *__getgrent_a(FILE *f, struct group *gr, char **line, size_t *size, char ***mem, size_t *nmem);
+int __getgrent_a(FILE *f, struct group *gr, char **line, size_t *size, char ***mem, size_t *nmem, struct group **res);
+int __getgr_a(const char *name, gid_t gid, struct group *gr, char **buf, size_t *size, char ***mem, size_t *nmem, struct group **res);
 int __parsespent(char *s, struct spwd *sp);
-- 
2.1.4

Powered by blists - more mailing lists

Confused about mailing lists and their use? Read about mailing lists on Wikipedia and check out these guidelines on proper formatting of your messages.