lists.arthurdejong.org
RSS feed

nss-pam-ldapd commit: r1659 - in nss-pam-ldapd: common nss

[Date Prev][Date Next] [Thread Prev][Thread Next]

nss-pam-ldapd commit: r1659 - in nss-pam-ldapd: common nss



Author: arthur
Date: Thu Apr 26 21:42:22 2012
New Revision: 1659
URL: http://arthurdejong.org/viewvc/nss-pam-ldapd?revision=1659&view=revision

Log:
split the functionality to read everything from the stream into a separate 
function and don't assume we use non-blocking IO (fix r1637)

Modified:
   nss-pam-ldapd/common/tio.c
   nss-pam-ldapd/common/tio.h
   nss-pam-ldapd/nss/common.h

Modified: nss-pam-ldapd/common/tio.c
==============================================================================
--- nss-pam-ldapd/common/tio.c  Thu Apr 26 21:39:57 2012        (r1658)
+++ nss-pam-ldapd/common/tio.c  Thu Apr 26 21:42:22 2012        (r1659)
@@ -317,18 +317,19 @@
   }
 }
 
-/* Read and discard the specified number of bytes from the stream.
-   If count is 0 reads and discards any data that can be read and empties
-   the read buffer. */
+/* Read and discard the specified number of bytes from the stream. */
 int tio_skip(TFILE *fp, size_t count)
 {
+  return tio_read(fp,NULL,count);
+}
+
+/* Read all available data from the stream and empty the read buffer. */
+int tio_skipall(TFILE *fp)
+{
+  struct timeval tv;
+  fd_set fdset;
   int rv;
   size_t len;
-  /* for simple cases just read */
-  if (count>0)
-  {
-    return tio_read(fp,NULL,count);
-  }
   /* clear the read buffer */
   fp->readbuffer.start=0;
   fp->readbuffer.len=0;
@@ -341,8 +342,22 @@
 #endif /* SSIZE_MAX */
   while (1)
   {
+    /* prepare our file descriptor set */
+    FD_ZERO(&fdset);
+    FD_SET(fp->fd,&fdset);
+    /* prepare the time to wait */
+    tv.tv_sec=0;
+    tv.tv_usec=0;
+    /* see if any data is available */
+    rv=select(FD_SETSIZE,&fdset,NULL,NULL,&tv);
+    if (rv==0)
+      return 0; /* no file descriptor ready */
+    if ((rv<0)&&((errno==EINTR)||(errno==EAGAIN)))
+      continue; /* interrupted, try again */
+    if (rv<0)
+      return -1; /* something went wrong */
+    /* read data from the stream */
     rv=read(fp->fd,fp->readbuffer.buffer,len);
-    /* check for errors */
     if (rv==0)
       return 0; /* end-of-file */
     if ((rv<0)&&(errno==EWOULDBLOCK))

Modified: nss-pam-ldapd/common/tio.h
==============================================================================
--- nss-pam-ldapd/common/tio.h  Thu Apr 26 21:39:57 2012        (r1658)
+++ nss-pam-ldapd/common/tio.h  Thu Apr 26 21:42:22 2012        (r1659)
@@ -56,11 +56,12 @@
 /* Read the specified number of bytes from the stream. */
 int tio_read(TFILE *fp,void *buf,size_t count);
 
-/* Read and discard the specified number of bytes from the stream.
-   If count is 0 reads and discards any data that can be read and empties
-   the read buffer. */
+/* Read and discard the specified number of bytes from the stream. */
 int tio_skip(TFILE *fp,size_t count);
 
+/* Read all available data from the stream and empty the read buffer. */
+int tio_skipall(TFILE *fp);
+
 /* Write the specified buffer to the stream. */
 int tio_write(TFILE *fp,const void *buf,size_t count);
 

Modified: nss-pam-ldapd/nss/common.h
==============================================================================
--- nss-pam-ldapd/nss/common.h  Thu Apr 26 21:39:57 2012        (r1658)
+++ nss-pam-ldapd/nss/common.h  Thu Apr 26 21:42:22 2012        (r1659)
@@ -148,7 +148,7 @@
   /* close socket and we're done */ \
   if ((retv==NSS_STATUS_SUCCESS)||(retv==NSS_STATUS_TRYAGAIN)) \
   { \
-    (void)tio_skip(fp,0); /* read any buffered data */ \
+    (void)tio_skipall(fp); \
     (void)tio_close(fp); \
   } \
   return retv;
@@ -224,7 +224,7 @@
   NSS_AVAILCHECK; \
   if (fp!=NULL) \
   { \
-    (void)tio_skip(fp,0); /* read any buffered data */ \
+    (void)tio_skipall(fp); \
     (void)tio_close(fp); \
     fp=NULL; \
   } \
-- 
To unsubscribe send an email to
nss-pam-ldapd-commits-unsubscribe@lists.arthurdejong.org or see
http://lists.arthurdejong.org/nss-pam-ldapd-commits/