about summary refs log tree commit diff
diff options
context:
space:
mode:
-rw-r--r--ChangeLog11
-rw-r--r--nscd/grpcache.c42
-rw-r--r--nscd/hstcache.c78
-rw-r--r--nscd/nscd_getgr_r.c13
-rw-r--r--nscd/nscd_gethst_r.c28
-rw-r--r--nscd/nscd_getpw_r.c23
-rw-r--r--nscd/pwdcache.c42
7 files changed, 89 insertions, 148 deletions
diff --git a/ChangeLog b/ChangeLog
index 8d625e9321..fed258d0e1 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,16 @@
 2003-01-15  Ulrich Drepper  <drepper@redhat.com>
 
+	* nscd/nscd_getpw_r.c (nscd_getpw_r): Consolidate writing of the
+	request with one writev call.  Protect all read calls with
+	TEMP_FAILURE_RETRY.
+	* nscd/nscd_getgr_r.c (nscd_getgr_r): Likewise.
+	* nscd/nscd_gethst_r.c (nscd_gethst_r): Likewise.
+
+	* nscd/hstcache.c: Use extend_alloca to reallocate alloca'd buffer.
+	Protect writev calls with TEMP_FAILURE_RETRY.
+	* nscd/grpcache.c: Likewise.
+	* nscd/pwdcache.c: Likewise.
+
 	* nscd/hstcache.c (addhstbynamev6): Don't interpret hostname as
 	IPv6 address in debug output.
 
diff --git a/nscd/grpcache.c b/nscd/grpcache.c
index 41a36dc674..a8b33cab1a 100644
--- a/nscd/grpcache.c
+++ b/nscd/grpcache.c
@@ -1,5 +1,5 @@
 /* Cache handling for group lookup.
-   Copyright (C) 1998, 1999, 2000, 2001, 2002 Free Software Foundation, Inc.
+   Copyright (C) 1998-2002, 2003 Free Software Foundation, Inc.
    This file is part of the GNU C Library.
    Contributed by Ulrich Drepper <drepper@cygnus.com>, 1998.
 
@@ -18,6 +18,7 @@
    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
    02111-1307 USA.  */
 
+#include <alloca.h>
 #include <errno.h>
 #include <error.h>
 #include <grp.h>
@@ -95,7 +96,7 @@ cache_addgr (struct database *db, int fd, request_header *req, void *key,
 
       total = sizeof (notfound);
 
-      written = writev (fd, &iov_notfound, 1);
+      written = TEMP_FAILURE_RETRY (writev (fd, &iov_notfound, 1));
 
       copy = malloc (req->key_len);
       if (copy == NULL)
@@ -227,10 +228,11 @@ addgrbyname (struct database *db, int fd, request_header *req,
     {
       char *old_buffer = buffer;
       errno = 0;
-      buflen += 1024;
+#define INCR 1024
 
       if (__builtin_expect (buflen > 32768, 0))
 	{
+	  buflen += INCR;
 	  buffer = (char *) realloc (use_malloc ? buffer : NULL, buflen);
 	  if (buffer == NULL)
 	    {
@@ -244,19 +246,9 @@ addgrbyname (struct database *db, int fd, request_header *req,
 	  use_malloc = true;
 	}
       else
-	{
-	  buffer = (char *) alloca (buflen);
-#if _STACK_GROWS_DOWN
-	  if (buffer + buflen == old_buffer)
-	    buflen = 2 * buflen - 1024;
-#elif _STACK_GROWS_UP
-	  if (old_buffer + buflen - 1024 == buffer)
-	    {
-	      buffer = old_buffer;
-	      buflen = 2 * buflen - 1024;
-	    }
-#endif
-	}
+	/* Allocate a new buffer on the stack.  If possible combine it
+	   with the previously allocated buffer.  */
+	buffer = (char *) extend_alloca (buffer, buflen, buflen + INCR);
     }
 
   if (secure[grpdb])
@@ -309,10 +301,10 @@ addgrbygid (struct database *db, int fd, request_header *req,
     {
       char *old_buffer = buffer;
       errno = 0;
-      buflen += 1024;
 
       if (__builtin_expect (buflen > 32768, 0))
 	{
+	  buflen += INCR;
 	  buffer = (char *) realloc (use_malloc ? buffer : NULL, buflen);
 	  if (buffer == NULL)
 	    {
@@ -326,19 +318,9 @@ addgrbygid (struct database *db, int fd, request_header *req,
 	  use_malloc = true;
 	}
       else
-	{
-	  buffer = (char *) alloca (buflen);
-#if _STACK_GROWS_DOWN
-	  if (buffer + buflen == old_buffer)
-	    buflen = 2 * buflen - 1024;
-#elif _STACK_GROWS_UP
-	  if (old_buffer + buflen - 1024 == buffer)
-	    {
-	      buffer = old_buffer;
-	      buflen = 2 * buflen - 1024;
-	    }
-#endif
-	}
+	/* Allocate a new buffer on the stack.  If possible combine it
+	   with the previously allocated buffer.  */
+	buffer = (char *) extend_alloca (buffer, buflen, buflen + INCR);
     }
 
   if (secure[grpdb])
diff --git a/nscd/hstcache.c b/nscd/hstcache.c
index 19a85b9409..08d11e4586 100644
--- a/nscd/hstcache.c
+++ b/nscd/hstcache.c
@@ -18,6 +18,7 @@
    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
    02111-1307 USA.  */
 
+#include <alloca.h>
 #include <assert.h>
 #include <errno.h>
 #include <error.h>
@@ -103,7 +104,7 @@ cache_addhst (struct database *db, int fd, request_header *req, void *key,
 
       total = sizeof (notfound);
 
-      written = writev (fd, &iov_notfound, 1);
+      written = TEMP_FAILURE_RETRY (writev (fd, &iov_notfound, 1));
 
       copy = malloc (req->key_len);
       if (copy == NULL)
@@ -315,10 +316,11 @@ addhstbyname (struct database *db, int fd, request_header *req,
     {
       char *old_buffer = buffer;
       errno = 0;
-      buflen += 1024;
+#define INCR 1024
 
       if (__builtin_expect (buflen > 32768, 0))
 	{
+	  buflen += INCR;
 	  buffer = (char *) realloc (use_malloc ? buffer : NULL, buflen);
 	  if (buffer == NULL)
 	    {
@@ -332,19 +334,9 @@ addhstbyname (struct database *db, int fd, request_header *req,
 	  use_malloc = true;
 	}
       else
-	{
-	  buffer = (char *) alloca (buflen);
-#if _STACK_GROWS_DOWN
-	  if (buffer + buflen == old_buffer)
-	    buflen = 2 * buflen - 1024;
-#elif _STACK_GROWS_UP
-	  if (old_buffer + buflen - 1024 == buffer)
-	    {
-	      buffer = old_buffer;
-	      buflen = 2 * buflen - 1024;
-	    }
-#endif
-	}
+	/* Allocate a new buffer on the stack.  If possible combine it
+	   with the previously allocated buffer.  */
+	buffer = (char *) extend_alloca (buffer, buflen, buflen + INCR);
     }
 
   if (secure[hstdb])
@@ -392,10 +384,10 @@ addhstbyaddr (struct database *db, int fd, request_header *req,
     {
       char *old_buffer = buffer;
       errno = 0;
-      buflen += 1024;
 
       if (__builtin_expect (buflen > 32768, 0))
 	{
+	  buflen += INCR;
 	  buffer = (char *) realloc (use_malloc ? buffer : NULL, buflen);
 	  if (buffer == NULL)
 	    {
@@ -409,19 +401,9 @@ addhstbyaddr (struct database *db, int fd, request_header *req,
 	  use_malloc = true;
 	}
       else
-	{
-	  buffer = (char *) alloca (buflen);
-#if _STACK_GROWS_DOWN
-	  if (buffer + buflen == old_buffer)
-	    buflen = 2 * buflen - 1024;
-#elif _STACK_GROWS_UP
-	  if (old_buffer + buflen - 1024 == buffer)
-	    {
-	      buffer = old_buffer;
-	      buflen = 2 * buflen - 1024;
-	    }
-#endif
-	}
+	/* Allocate a new buffer on the stack.  If possible combine it
+	   with the previously allocated buffer.  */
+	buffer = (char *) extend_alloca (buffer, buflen, buflen + INCR);
     }
 
   if (secure[hstdb])
@@ -450,7 +432,7 @@ addhstbynamev6 (struct database *db, int fd, request_header *req,
   bool use_malloc = false;
 
   if (__builtin_expect (debug_level > 0, 0))
-    dbg_log (_("Haven't found \"%s\" in hosts cache!"), key);
+    dbg_log (_("Haven't found \"%s\" in hosts cache!"), (char *) key);
 
   if (secure[hstdb])
     {
@@ -465,10 +447,10 @@ addhstbynamev6 (struct database *db, int fd, request_header *req,
     {
       char *old_buffer = buffer;
       errno = 0;
-      buflen += 1024;
 
       if (__builtin_expect (buflen > 32768, 0))
 	{
+	  buflen += INCR;
 	  buffer = (char *) realloc (use_malloc ? buffer : NULL, buflen);
 	  if (buffer == NULL)
 	    {
@@ -482,19 +464,9 @@ addhstbynamev6 (struct database *db, int fd, request_header *req,
 	  use_malloc = true;
 	}
       else
-	{
-	  buffer = (char *) alloca (buflen);
-#if _STACK_GROWS_DOWN
-	  if (buffer + buflen == old_buffer)
-	    buflen = 2 * buflen - 1024;
-#elif _STACK_GROWS_UP
-	  if (old_buffer + buflen - 1024 == buffer)
-	    {
-	      buffer = old_buffer;
-	      buflen = 2 * buflen - 1024;
-	    }
-#endif
-	}
+	/* Allocate a new buffer on the stack.  If possible combine it
+	   with the previously allocated buffer.  */
+	buffer = (char *) extend_alloca (buffer, buflen, buflen + INCR);
     }
 
   if (secure[hstdb])
@@ -542,10 +514,10 @@ addhstbyaddrv6 (struct database *db, int fd, request_header *req,
     {
       char *old_buffer = buffer;
       errno = 0;
-      buflen += 1024;
 
       if (__builtin_expect (buflen > 32768, 0))
 	{
+	  buflen += INCR;
 	  buffer = (char *) realloc (use_malloc ? buffer : NULL, buflen);
 	  if (buffer == NULL)
 	    {
@@ -559,19 +531,9 @@ addhstbyaddrv6 (struct database *db, int fd, request_header *req,
 	  use_malloc = true;
 	}
       else
-	{
-	  buffer = (char *) alloca (buflen);
-#if _STACK_GROWS_DOWN
-	  if (buffer + buflen == old_buffer)
-	    buflen = 2 * buflen - 1024;
-#elif _STACK_GROWS_UP
-	  if (old_buffer + buflen - 1024 == buffer)
-	    {
-	      buffer = old_buffer;
-	      buflen = 2 * buflen - 1024;
-	    }
-#endif
-	}
+	/* Allocate a new buffer on the stack.  If possible combine it
+	   with the previously allocated buffer.  */
+	buffer = (char *) extend_alloca (buffer, buflen, buflen + INCR);
     }
 
   if (secure[hstdb])
diff --git a/nscd/nscd_getgr_r.c b/nscd/nscd_getgr_r.c
index ab9eef4e24..49f4b51304 100644
--- a/nscd/nscd_getgr_r.c
+++ b/nscd/nscd_getgr_r.c
@@ -1,4 +1,4 @@
-/* Copyright (C) 1998, 1999, 2000, 2002 Free Software Foundation, Inc.
+/* Copyright (C) 1998, 1999, 2000, 2002, 2003 Free Software Foundation, Inc.
    This file is part of the GNU C Library.
    Contributed by Thorsten Kukuk <kukuk@uni-paderborn.de>, 1998.
 
@@ -114,13 +114,15 @@ nscd_getgr_r (const char *key, size_t keylen, request_type type,
   vec[1].iov_base = (void *) key;
   vec[1].iov_len = keylen;
 
-  if ((size_t) __writev (sock, vec, 2) != sizeof (request_header) + keylen)
+  nbytes = (size_t) TEMP_FAILURE_RETRY (__writev (sock, vec, 2));
+  if (nbytes != sizeof (request_header) + keylen)
     {
       __close (sock);
       return -1;
     }
 
-  nbytes = __read (sock, &gr_resp, sizeof (gr_response_header));
+  nbytes = TEMP_FAILURE_RETRY (__read (sock, &gr_resp,
+				       sizeof (gr_response_header)));
   if (nbytes != sizeof (gr_response_header))
     {
       __close (sock);
@@ -182,7 +184,7 @@ nscd_getgr_r (const char *key, size_t keylen, request_type type,
       total_len += gr_resp.gr_name_len + gr_resp.gr_passwd_len;
 
       /* Get this data.  */
-      if ((size_t) __readv (sock, vec, 2) != total_len)
+      if ((size_t) TEMP_FAILURE_RETRY (__readv (sock, vec, 2)) != total_len)
 	{
 	  __close (sock);
 	  return -1;
@@ -203,7 +205,8 @@ nscd_getgr_r (const char *key, size_t keylen, request_type type,
       if (total_len > buflen)
 	goto no_room;
 
-      if (__read (sock, resultbuf->gr_mem[0], total_len) != total_len)
+      if ((size_t) TEMP_FAILURE_RETRY (__read (sock, resultbuf->gr_mem[0],
+					       total_len)) != total_len)
 	{
 	  __close (sock);
 	  /* The `errno' to some value != ERANGE.  */
diff --git a/nscd/nscd_gethst_r.c b/nscd/nscd_gethst_r.c
index f51cceb928..bd0be682d8 100644
--- a/nscd/nscd_gethst_r.c
+++ b/nscd/nscd_gethst_r.c
@@ -1,4 +1,4 @@
-/* Copyright (C) 1998, 1999, 2000, 2001, 2002 Free Software Foundation, Inc.
+/* Copyright (C) 1998-2002, 2003 Free Software Foundation, Inc.
    This file is part of the GNU C Library.
    Contributed by Ulrich Drepper <drepper@cygnus.com>, 1998.
 
@@ -123,6 +123,7 @@ nscd_gethst_r (const char *key, size_t keylen, request_type type,
   hst_response_header hst_resp;
   request_header req;
   ssize_t nbytes;
+  struct iovec vec[4];
 
   if (sock == -1)
     {
@@ -133,21 +134,21 @@ nscd_gethst_r (const char *key, size_t keylen, request_type type,
   req.version = NSCD_VERSION;
   req.type = type;
   req.key_len = keylen;
-  nbytes = __write (sock, &req, sizeof (request_header));
-  if (nbytes != sizeof (request_header))
-    {
-      __close (sock);
-      return -1;
-    }
 
-  nbytes = __write (sock, key, req.key_len);
-  if (nbytes != req.key_len)
+  vec[0].iov_base = &req;
+  vec[0].iov_len = sizeof (request_header);
+  vec[1].iov_base = (void *) key;
+  vec[1].iov_len = req.key_len;
+
+  nbytes = TEMP_FAILURE_RETRY (__writev (sock, vec, 2));
+  if ((size_t) nbytes != sizeof (request_header) + req.key_len)
     {
       __close (sock);
       return -1;
     }
 
-  nbytes = __read (sock, &hst_resp, sizeof (hst_response_header));
+  nbytes = TEMP_FAILURE_RETRY (__read (sock, &hst_resp,
+				       sizeof (hst_response_header)));
   if (nbytes != sizeof (hst_response_header))
     {
       __close (sock);
@@ -164,7 +165,6 @@ nscd_gethst_r (const char *key, size_t keylen, request_type type,
 
   if (hst_resp.found == 1)
     {
-      struct iovec vec[4];
       uint32_t *aliases_len;
       char *cp = buffer;
       uintptr_t align1;
@@ -263,7 +263,7 @@ nscd_gethst_r (const char *key, size_t keylen, request_type type,
 	}
       resultbuf->h_addr_list[cnt] = NULL;
 
-      if ((size_t) __readv (sock, vec, n) != total_len)
+      if ((size_t) TEMP_FAILURE_RETRY (__readv (sock, vec, n)) != total_len)
 	{
 	  __close (sock);
 	  return -1;
@@ -284,8 +284,8 @@ nscd_gethst_r (const char *key, size_t keylen, request_type type,
 	goto no_room;
 
       /* And finally read the aliases.  */
-      if ((size_t) __read (sock, resultbuf->h_aliases[0], total_len)
-	  != total_len)
+      if ((size_t) TEMP_FAILURE_RETRY (__read (sock, resultbuf->h_aliases[0],
+					       total_len)) != total_len)
 	{
 	  __close (sock);
 	  return -1;
diff --git a/nscd/nscd_getpw_r.c b/nscd/nscd_getpw_r.c
index 06f41efe5e..f4fa75fa6f 100644
--- a/nscd/nscd_getpw_r.c
+++ b/nscd/nscd_getpw_r.c
@@ -1,4 +1,4 @@
-/* Copyright (C) 1998, 1999 Free Software Foundation, Inc.
+/* Copyright (C) 1998, 1999, 2003 Free Software Foundation, Inc.
    This file is part of the GNU C Library.
    Contributed by Thorsten Kukuk <kukuk@uni-paderborn.de>, 1998.
 
@@ -96,6 +96,7 @@ nscd_getpw_r (const char *key, size_t keylen, request_type type,
   request_header req;
   pw_response_header pw_resp;
   ssize_t nbytes;
+  struct iovec vec[2];
 
   if (sock == -1)
     {
@@ -106,21 +107,21 @@ nscd_getpw_r (const char *key, size_t keylen, request_type type,
   req.version = NSCD_VERSION;
   req.type = type;
   req.key_len = keylen;
-  nbytes = __write (sock, &req, sizeof (request_header));
-  if (nbytes != sizeof (request_header))
-    {
-      __close (sock);
-      return -1;
-    }
 
-  nbytes = __write (sock, key, keylen);
-  if (nbytes != keylen)
+  vec[0].iov_base = &req;
+  vec[0].iov_len = sizeof (request_header);
+  vec[1].iov_base = (void *) key;
+  vec[1].iov_len = keylen;
+
+  nbytes = (size_t) TEMP_FAILURE_RETRY (__writev (sock, vec, 2));
+  if (nbytes != sizeof (request_header) + keylen)
     {
       __close (sock);
       return -1;
     }
 
-  nbytes = __read (sock, &pw_resp, sizeof (pw_response_header));
+  nbytes = TEMP_FAILURE_RETRY (__read (sock, &pw_resp,
+				       sizeof (pw_response_header)));
   if (nbytes != sizeof (pw_response_header))
     {
       __close (sock);
@@ -168,7 +169,7 @@ nscd_getpw_r (const char *key, size_t keylen, request_type type,
       /* get pw_pshell */
       resultbuf->pw_shell = p;
 
-      nbytes = __read (sock, buffer, total);
+      nbytes = TEMP_FAILURE_RETRY (__read (sock, buffer, total));
 
       __close (sock);
 
diff --git a/nscd/pwdcache.c b/nscd/pwdcache.c
index a52c7e3355..2f78884d88 100644
--- a/nscd/pwdcache.c
+++ b/nscd/pwdcache.c
@@ -1,5 +1,5 @@
 /* Cache handling for passwd lookup.
-   Copyright (C) 1998, 1999, 2000, 2001, 2002 Free Software Foundation, Inc.
+   Copyright (C) 1998-2002, 2003 Free Software Foundation, Inc.
    This file is part of the GNU C Library.
    Contributed by Ulrich Drepper <drepper@cygnus.com>, 1998.
 
@@ -18,6 +18,7 @@
    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
    02111-1307 USA.  */
 
+#include <alloca.h>
 #include <errno.h>
 #include <error.h>
 #include <pwd.h>
@@ -101,7 +102,7 @@ cache_addpw (struct database *db, int fd, request_header *req, void *key,
 
       total = sizeof (notfound);
 
-      written = writev (fd, &iov_notfound, 1);
+      written = TEMP_FAILURE_RETRY (writev (fd, &iov_notfound, 1));
 
       copy = malloc (req->key_len);
       if (copy == NULL)
@@ -223,10 +224,11 @@ addpwbyname (struct database *db, int fd, request_header *req,
     {
       char *old_buffer = buffer;
       errno = 0;
-      buflen += 1024;
+#define INCR 1024
 
       if (__builtin_expect (buflen > 32768, 0))
 	{
+	  buflen += INCR;
 	  buffer = (char *) realloc (use_malloc ? buffer : NULL, buflen);
 	  if (buffer == NULL)
 	    {
@@ -240,19 +242,9 @@ addpwbyname (struct database *db, int fd, request_header *req,
 	  use_malloc = true;
 	}
       else
-	{
-	  buffer = (char *) alloca (buflen);
-#if _STACK_GROWS_DOWN
-	  if (buffer + buflen == old_buffer)
-	    buflen = 2 * buflen - 1024;
-#elif _STACK_GROWS_UP
-	  if (old_buffer + buflen - 1024 == buffer)
-	    {
-	      buffer = old_buffer;
-	      buflen = 2 * buflen - 1024;
-	    }
-#endif
-	}
+	/* Allocate a new buffer on the stack.  If possible combine it
+	   with the previously allocated buffer.  */
+	buffer = (char *) extend_alloca (buffer, buflen, buflen + INCR);
     }
 
   if (secure[pwddb])
@@ -305,10 +297,10 @@ addpwbyuid (struct database *db, int fd, request_header *req,
     {
       char *old_buffer = buffer;
       errno = 0;
-      buflen += 1024;
 
       if (__builtin_expect (buflen > 32768, 0))
 	{
+	  buflen += 1024;
 	  buffer = (char *) realloc (use_malloc ? buffer : NULL, buflen);
 	  if (buffer == NULL)
 	    {
@@ -322,19 +314,9 @@ addpwbyuid (struct database *db, int fd, request_header *req,
 	  use_malloc = true;
 	}
       else
-	{
-	  buffer = (char *) alloca (buflen);
-#if _STACK_GROWS_DOWN
-	  if (buffer + buflen == old_buffer)
-	    buflen = 2 * buflen - 1024;
-#elif _STACK_GROWS_UP
-	  if (old_buffer + buflen - 1024 == buffer)
-	    {
-	      buffer = old_buffer;
-	      buflen = 2 * buflen - 1024;
-	    }
-#endif
-	}
+	/* Allocate a new buffer on the stack.  If possible combine it
+	   with the previously allocated buffer.  */
+	buffer = (char *) extend_alloca (buffer, buflen, buflen + INCR);
     }
 
   if (secure[pwddb])