From feb92219d95b1b8e5400cf04c8a1e37f6aeb4d68 Mon Sep 17 00:00:00 2001 From: lukem Date: Sun, 24 Oct 2004 14:52:46 +0000 Subject: [PATCH] Protect access to all public functions via a mutex as some of the backends are not reentrant (such as compat getgr*_r, because it uses the non reentrant _{dns,nis}_getgrent() backends to implement `+group'). --- lib/libc/gen/getgrent.c | 27 ++++++++++++++++++++++++--- 1 file changed, 24 insertions(+), 3 deletions(-) diff --git a/lib/libc/gen/getgrent.c b/lib/libc/gen/getgrent.c index d2028de6dc95..7b08cda00598 100644 --- a/lib/libc/gen/getgrent.c +++ b/lib/libc/gen/getgrent.c @@ -1,4 +1,4 @@ -/* $NetBSD: getgrent.c,v 1.49 2004/10/04 04:11:33 lukem Exp $ */ +/* $NetBSD: getgrent.c,v 1.50 2004/10/24 14:52:46 lukem Exp $ */ /*- * Copyright (c) 1999, 2000, 2004 The NetBSD Foundation, Inc. @@ -95,7 +95,7 @@ #if 0 static char sccsid[] = "@(#)getgrent.c 8.2 (Berkeley) 3/21/94"; #else -__RCSID("$NetBSD: getgrent.c,v 1.49 2004/10/04 04:11:33 lukem Exp $"); +__RCSID("$NetBSD: getgrent.c,v 1.50 2004/10/24 14:52:46 lukem Exp $"); #endif #endif /* LIBC_SCCS and not lint */ @@ -108,12 +108,13 @@ __RCSID("$NetBSD: getgrent.c,v 1.49 2004/10/04 04:11:33 lukem Exp $"); #include #include #include +#include #include #include #include #include +#include -#include #ifdef HESIOD #include @@ -139,6 +140,10 @@ __weak_alias(getgrnam_r,_getgrnam_r) __weak_alias(setgrent,_setgrent) __weak_alias(setgroupent,_setgroupent) #endif + +#ifdef _REENTRANT +static mutex_t _grmutex = MUTEX_INITIALIZER; +#endif static const ns_src defaultcompat[] = { { NSSRC_COMPAT, NS_SUCCESS }, @@ -1695,8 +1700,10 @@ getgrent(void) { 0 } }; + mutex_lock(&_grmutex); rv = nsdispatch(NULL, dtab, NSDB_GROUP, "getgrent", defaultcompat, &retval); + mutex_unlock(&_grmutex); return (rv == NS_SUCCESS) ? retval : NULL; } @@ -1714,8 +1721,10 @@ getgrgid(gid_t gid) { 0 } }; + mutex_lock(&_grmutex); rv = nsdispatch(NULL, dtab, NSDB_GROUP, "getgrgid", defaultcompat, &retval, gid); + mutex_unlock(&_grmutex); return (rv == NS_SUCCESS) ? retval : NULL; } @@ -1739,8 +1748,10 @@ getgrgid_r(gid_t gid, struct group *grp, char *buffer, size_t buflen, *result = NULL; retval = 0; + mutex_lock(&_grmutex); rv = nsdispatch(NULL, dtab, NSDB_GROUP, "getgrgid_r", defaultcompat, &retval, gid, grp, buffer, buflen, result); + mutex_unlock(&_grmutex); return (rv == NS_SUCCESS) ? 0 : retval ? retval : ENOENT; } @@ -1758,8 +1769,10 @@ getgrnam(const char *name) { 0 } }; + mutex_lock(&_grmutex); rv = nsdispatch(NULL, dtab, NSDB_GROUP, "getgrnam", defaultcompat, &retval, name); + mutex_unlock(&_grmutex); return (rv == NS_SUCCESS) ? retval : NULL; } @@ -1784,8 +1797,10 @@ getgrnam_r(const char *name, struct group *grp, char *buffer, size_t buflen, *result = NULL; retval = 0; + mutex_lock(&_grmutex); rv = nsdispatch(NULL, dtab, NSDB_GROUP, "getgrnam_r", defaultcompat, &retval, name, grp, buffer, buflen, result); + mutex_unlock(&_grmutex); return (rv == NS_SUCCESS) ? 0 : retval ? retval : ENOENT; } @@ -1800,9 +1815,11 @@ endgrent(void) { 0 } }; + mutex_lock(&_grmutex); /* force all endgrent() methods */ (void) nsdispatch(NULL, dtab, NSDB_GROUP, "endgrent", defaultcompat_forceall); + mutex_unlock(&_grmutex); } int @@ -1817,9 +1834,11 @@ setgroupent(int stayopen) }; int rv, retval; + mutex_lock(&_grmutex); /* force all setgroupent() methods */ rv = nsdispatch(NULL, dtab, NSDB_GROUP, "setgroupent", defaultcompat_forceall, &retval, stayopen); + mutex_unlock(&_grmutex); return (rv == NS_SUCCESS) ? retval : 0; } @@ -1834,7 +1853,9 @@ setgrent(void) { 0 } }; + mutex_lock(&_grmutex); /* force all setgrent() methods */ (void) nsdispatch(NULL, dtab, NSDB_GROUP, "setgrent", defaultcompat_forceall); + mutex_unlock(&_grmutex); }