svn commit: trunk/busybox/libpwdgrp

aldot at busybox.net aldot at busybox.net
Fri Oct 28 11:21:43 UTC 2005


Author: aldot
Date: 2005-10-28 04:21:40 -0700 (Fri, 28 Oct 2005)
New Revision: 11952

Log:
- move GETXXKEY_R_FUNC into an internal helper file to allow for compiling
  with IMA.


Added:
   trunk/busybox/libpwdgrp/pwd_grp_internal.c

Modified:
   trunk/busybox/libpwdgrp/pwd_grp.c


Changeset:
Modified: trunk/busybox/libpwdgrp/pwd_grp.c
===================================================================
--- trunk/busybox/libpwdgrp/pwd_grp.c	2005-10-28 10:55:11 UTC (rev 11951)
+++ trunk/busybox/libpwdgrp/pwd_grp.c	2005-10-28 11:21:40 UTC (rev 11952)
@@ -74,6 +74,7 @@
  *   Doing so is analogous to having fgetc() set errno on EOF.
  */
 /**********************************************************************/
+
 #ifdef L_fgetpwent_r
 
 int fgetpwent_r(FILE *__restrict stream, struct passwd *__restrict resultbuf,
@@ -224,6 +225,7 @@
 #define GETXXKEY_R_TEST(ENT)	(!strcmp((ENT)->pw_name, key))
 #define DO_GETXXKEY_R_KEYTYPE	const char *__restrict
 #define DO_GETXXKEY_R_PATHNAME  _PATH_PASSWD
+#include "pwd_grp_internal.c"
 #endif
 
 #ifdef L_getgrnam_r
@@ -233,6 +235,7 @@
 #define GETXXKEY_R_TEST(ENT)	(!strcmp((ENT)->gr_name, key))
 #define DO_GETXXKEY_R_KEYTYPE	const char *__restrict
 #define DO_GETXXKEY_R_PATHNAME  _PATH_GROUP
+#include "pwd_grp_internal.c"
 #endif
 
 #ifdef L_getspnam_r
@@ -242,6 +245,7 @@
 #define GETXXKEY_R_TEST(ENT)	(!strcmp((ENT)->sp_namp, key))
 #define DO_GETXXKEY_R_KEYTYPE	const char *__restrict
 #define DO_GETXXKEY_R_PATHNAME  _PATH_SHADOW
+#include "pwd_grp_internal.c"
 #endif
 
 #ifdef L_getpwuid_r
@@ -251,6 +255,7 @@
 #define GETXXKEY_R_TEST(ENT)	((ENT)->pw_uid == key)
 #define DO_GETXXKEY_R_KEYTYPE	uid_t
 #define DO_GETXXKEY_R_PATHNAME  _PATH_PASSWD
+#include "pwd_grp_internal.c"
 #endif
 
 #ifdef L_getgrgid_r
@@ -260,47 +265,10 @@
 #define GETXXKEY_R_TEST(ENT)	((ENT)->gr_gid == key)
 #define DO_GETXXKEY_R_KEYTYPE	gid_t
 #define DO_GETXXKEY_R_PATHNAME  _PATH_GROUP
+#include "pwd_grp_internal.c"
 #endif
 
 /**********************************************************************/
-#ifdef GETXXKEY_R_FUNC
-
-int GETXXKEY_R_FUNC(DO_GETXXKEY_R_KEYTYPE key,
-					GETXXKEY_R_ENTTYPE *__restrict resultbuf,
-					char *__restrict buffer, size_t buflen,
-					GETXXKEY_R_ENTTYPE **__restrict result)
-{
-	FILE *stream;
-	int rv;
-
-	*result = NULL;
-
-	if (!(stream = fopen(DO_GETXXKEY_R_PATHNAME, "r"))) {
-		rv = errno;
-	} else {
-		do {
-			if (!(rv = __pgsreader(GETXXKEY_R_PARSER, resultbuf,
-								   buffer, buflen, stream))
-				) {
-				if (GETXXKEY_R_TEST(resultbuf)) { /* Found key? */
-					*result = resultbuf;
-					break;
-				}
-			} else {
-				if (rv == ENOENT) {	/* end-of-file encountered. */
-					rv = 0;
-				}
-				break;
-			}
-		} while (1);
-		fclose(stream);
-	}
-
-	return rv;
-}
-
-#endif
-/**********************************************************************/
 #ifdef L_getpwuid
 
 struct passwd *getpwuid(uid_t uid)
@@ -440,22 +408,37 @@
 
 #endif
 /**********************************************************************/
-#ifdef L_getpwent_r
 
+#if defined(L_getpwent_r) || defined(L_getgrent_r) || defined(L_getspent_r)
+#if defined CONFIG_USE_BB_THREADSAFE_SHADOW && defined PTHREAD_MUTEX_INITIALIZER
+static pthread_mutex_t mylock = PTHREAD_MUTEX_INITIALIZER;
+# define LOCK		pthread_mutex_lock(&mylock)
+# define UNLOCK		pthread_mutex_unlock(&mylock);
+#else
+# define LOCK		((void) 0)
+# define UNLOCK		((void) 0)
+#endif
+#endif
+
+#ifdef L_getpwent_r
 static FILE *pwf /*= NULL*/;
 void setpwent(void)
 {
+	LOCK;
 	if (pwf) {
 		rewind(pwf);
 	}
+	UNLOCK;
 }
 
 void endpwent(void)
 {
+	LOCK;
 	if (pwf) {
 		fclose(pwf);
 		pwf = NULL;
 	}
+	UNLOCK;
 }
 
 
@@ -465,6 +448,7 @@
 {
 	int rv;
 
+	LOCK;
 	*result = NULL;				/* In case of error... */
 
 	if (!pwf) {
@@ -480,6 +464,7 @@
 	}
 
  ERR:
+	UNLOCK;
 	return rv;
 }
 
@@ -490,17 +475,21 @@
 static FILE *grf /*= NULL*/;
 void setgrent(void)
 {
+	LOCK;
 	if (grf) {
 		rewind(grf);
 	}
+	UNLOCK;
 }
 
 void endgrent(void)
 {
+	LOCK;
 	if (grf) {
 		fclose(grf);
 		grf = NULL;
 	}
+	UNLOCK;
 }
 
 int getgrent_r(struct group *__restrict resultbuf,
@@ -509,6 +498,7 @@
 {
 	int rv;
 
+	LOCK;
 	*result = NULL;				/* In case of error... */
 
 	if (!grf) {
@@ -524,6 +514,7 @@
 	}
 
  ERR:
+	UNLOCK;
 	return rv;
 }
 
@@ -534,17 +525,21 @@
 static FILE *spf /*= NULL*/;
 void setspent(void)
 {
+	LOCK;
 	if (spf) {
 		rewind(spf);
 	}
+	UNLOCK;
 }
 
 void endspent(void)
 {
+	LOCK;
 	if (spf) {
 		fclose(spf);
 		spf = NULL;
 	}
+	UNLOCK;
 }
 
 int getspent_r(struct spwd *resultbuf, char *buffer, 
@@ -552,6 +547,7 @@
 {
 	int rv;
 
+	LOCK;
 	*result = NULL;				/* In case of error... */
 
 	if (!spf) {
@@ -567,6 +563,7 @@
 	}
 
  ERR:
+	UNLOCK;
 	return rv;
 }
 
@@ -632,7 +629,7 @@
 
 int initgroups(const char *user, gid_t gid)
 {
-	FILE *grf;
+	FILE *grfile;
 	gid_t *group_list;
 	int num_groups, rv;
 	char **m;
@@ -643,13 +640,13 @@
 
 	/* We alloc space for 8 gids at a time. */
 	if (((group_list = (gid_t *) malloc(8*sizeof(gid_t *))) != NULL)
-		&& ((grf = fopen(_PATH_GROUP, "r")) != NULL)
+		&& ((grfile = fopen(_PATH_GROUP, "r")) != NULL)
 		) {
 
 		*group_list = gid;
 		num_groups = 1;
 
-		while (!__pgsreader(__parsegrent, &group, buff, sizeof(buff), grf)) {
+		while (!__pgsreader(__parsegrent, &group, buff, sizeof(buff), grfile)) {
 			assert(group.gr_mem); /* Must have at least a NULL terminator. */
 			if (group.gr_gid != gid) {
 				for (m=group.gr_mem ; *m ; m++) {
@@ -673,7 +670,7 @@
 
 		rv = setgroups(num_groups, group_list);
 	DO_CLOSE:
-		fclose(grf);
+		fclose(grfile);
 	}
 
 	/* group_list will be NULL if initial malloc failed, which may trigger
@@ -756,7 +753,7 @@
 /**********************************************************************/
 #ifdef L_putspent
 
-static const unsigned char sp_off[] = {
+static const unsigned char _sp_off[] = {
 	offsetof(struct spwd, sp_lstchg),	/* 2 - not a char ptr */
 	offsetof(struct spwd, sp_min), 		/* 3 - not a char ptr */
 	offsetof(struct spwd, sp_max),		/* 4 - not a char ptr */
@@ -780,9 +777,9 @@
 		goto DO_UNLOCK;
 	}
 
-	for (i=0 ; i < sizeof(sp_off) ; i++) {
+	for (i=0 ; i < sizeof(_sp_off) ; i++) {
 		f = ld_format;
-		if ((x = *(const long int *)(((const char *) p) + sp_off[i])) == -1) {
+		if ((x = *(const long int *)(((const char *) p) + _sp_off[i])) == -1) {
 			f += 3;
 		}
 		if (fprintf(stream, f, x) < 0) {

Added: trunk/busybox/libpwdgrp/pwd_grp_internal.c
===================================================================
--- trunk/busybox/libpwdgrp/pwd_grp_internal.c	2005-10-28 10:55:11 UTC (rev 11951)
+++ trunk/busybox/libpwdgrp/pwd_grp_internal.c	2005-10-28 11:21:40 UTC (rev 11952)
@@ -0,0 +1,111 @@
+/*  Copyright (C) 2003     Manuel Novoa III
+ *
+ *  Licensed under GPL v2, or later.  See file LICENSE in this tarball.
+ */
+
+/*  Nov 6, 2003  Initial version.
+ *
+ *  NOTE: This implementation is quite strict about requiring all
+ *    field seperators.  It also does not allow leading whitespace
+ *    except when processing the numeric fields.  glibc is more
+ *    lenient.  See the various glibc difference comments below.
+ *
+ *  TODO:
+ *    Move to dynamic allocation of (currently staticly allocated)
+ *      buffers; especially for the group-related functions since
+ *      large group member lists will cause error returns.
+ *
+ */
+
+#include <features.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <stdint.h>
+#include <string.h>
+#include <stddef.h>
+#include <errno.h>
+#include <assert.h>
+#include <ctype.h>
+#include "busybox.h"
+#include "pwd_.h"
+#include "grp_.h"
+#include "shadow_.h"
+
+#ifndef _PATH_SHADOW
+#define	_PATH_SHADOW	"/etc/shadow"
+#endif
+#ifndef _PATH_PASSWD
+#define	_PATH_PASSWD	"/etc/passwd"
+#endif
+#ifndef _PATH_GROUP
+#define	_PATH_GROUP	"/etc/group"
+#endif
+
+/**********************************************************************/
+/* Sizes for staticly allocated buffers. */
+
+/* If you change these values, also change _SC_GETPW_R_SIZE_MAX and
+ * _SC_GETGR_R_SIZE_MAX in libc/unistd/sysconf.c to match */
+#define PWD_BUFFER_SIZE 256
+#define GRP_BUFFER_SIZE 256
+
+/**********************************************************************/
+/* Prototypes for internal functions. */
+
+extern int __parsepwent(void *pw, char *line);
+extern int __parsegrent(void *gr, char *line);
+extern int __parsespent(void *sp, char *line);
+
+extern int __pgsreader(int (*__parserfunc)(void *d, char *line), void *data,
+					   char *__restrict line_buff, size_t buflen, FILE *f);
+
+
+#ifndef GETXXKEY_R_FUNC
+#error GETXXKEY_R_FUNC is not defined!
+#endif
+/**********************************************************************/
+#ifdef GETXXKEY_R_FUNC
+
+int GETXXKEY_R_FUNC(DO_GETXXKEY_R_KEYTYPE key,
+					GETXXKEY_R_ENTTYPE *__restrict resultbuf,
+					char *__restrict buffer, size_t buflen,
+					GETXXKEY_R_ENTTYPE **__restrict result)
+{
+	FILE *stream;
+	int rv;
+
+	*result = NULL;
+
+	if (!(stream = fopen(DO_GETXXKEY_R_PATHNAME, "r"))) {
+		rv = errno;
+	} else {
+		do {
+			if (!(rv = __pgsreader(GETXXKEY_R_PARSER, resultbuf,
+								   buffer, buflen, stream))
+				) {
+				if (GETXXKEY_R_TEST(resultbuf)) { /* Found key? */
+					*result = resultbuf;
+					break;
+				}
+			} else {
+				if (rv == ENOENT) {	/* end-of-file encountered. */
+					rv = 0;
+				}
+				break;
+			}
+		} while (1);
+		fclose(stream);
+	}
+
+	return rv;
+}
+
+#endif
+/**********************************************************************/
+#undef GETXXKEY_R_FUNC
+#undef GETXXKEY_R_PARSER
+#undef GETXXKEY_R_ENTTYPE
+#undef GETXXKEY_R_TEST
+#undef DO_GETXXKEY_R_KEYTYPE
+#undef DO_GETXXKEY_R_PATHNAME
+




More information about the busybox-cvs mailing list