diff options
author | Arthur de Jong <arthur@arthurdejong.org> | 2012-03-14 20:31:38 +0000 |
---|---|---|
committer | Arthur de Jong <arthur@arthurdejong.org> | 2012-03-14 20:31:38 +0000 |
commit | 15498aca67bc96b1ac5dbd35be40d8c69a0e2d73 (patch) | |
tree | 8b6f423140943cddd0a808a39c4e8cf20eb72a57 | |
parent | c4d9aa9908c7551968660b34791f48cdbf6acb1f (diff) |
read any remaining available data from the stream when closing the connection in a normal way to prevent Broken pipe messages in nslcd
git-svn-id: http://arthurdejong.org/svn/nss-pam-ldapd/nss-pam-ldapd@1637 ef36b2f9-881f-0410-afb5-c4e39611909c
-rw-r--r-- | common/tio.c | 33 | ||||
-rw-r--r-- | common/tio.h | 6 | ||||
-rw-r--r-- | nss/common.h | 6 |
3 files changed, 39 insertions, 6 deletions
diff --git a/common/tio.c b/common/tio.c index 34002d4..67caed8 100644 --- a/common/tio.c +++ b/common/tio.c @@ -317,10 +317,39 @@ int tio_read(TFILE *fp, void *buf, size_t count) } } -/* Read and discard the specified number of bytes from the stream. */ +/* 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. */ int tio_skip(TFILE *fp, size_t count) { - return tio_read(fp,NULL,count); + 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; + fp->read_resettable=0; + /* read until we can't read no more */ + len=fp->readbuffer.size; +#ifdef SSIZE_MAX + if (len>SSIZE_MAX) + len=SSIZE_MAX; +#endif /* SSIZE_MAX */ + while (1) + { + rv=read(fp->fd,fp->readbuffer.buffer,len); + /* check for errors */ + if (rv==0) + return 0; /* end-of-file */ + if ((rv<0)&&(errno==EWOULDBLOCK)) + return 0; /* we've ready everything we can without blocking */ + if ((rv<0)&&(errno!=EINTR)&&(errno!=EAGAIN)) + return -1; /* something went wrong with the read */ + } } /* the caller has assured us that we can write to the file descriptor diff --git a/common/tio.h b/common/tio.h index 22c099c..832367b 100644 --- a/common/tio.h +++ b/common/tio.h @@ -2,7 +2,7 @@ tio.h - timed io functions This file is part of the nss-pam-ldapd library. - Copyright (C) 2007, 2008 Arthur de Jong + Copyright (C) 2007, 2008, 2010, 2012 Arthur de Jong This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public @@ -56,7 +56,9 @@ TFILE *tio_fdopen(int fd,struct timeval *readtimeout,struct timeval *writetimeou /* 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. */ +/* 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. */ int tio_skip(TFILE *fp,size_t count); /* Write the specified buffer to the stream. */ diff --git a/nss/common.h b/nss/common.h index 8d8f3a8..57587d0 100644 --- a/nss/common.h +++ b/nss/common.h @@ -2,7 +2,7 @@ common.h - common functions for NSS lookups Copyright (C) 2006 West Consulting - Copyright (C) 2006, 2007, 2008, 2009, 2010, 2011 Arthur de Jong + Copyright (C) 2006, 2007, 2008, 2009, 2010, 2011, 2012 Arthur de Jong This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public @@ -147,6 +147,7 @@ nss_status_t nss_ldap_destructor(nss_backend_t *be,void UNUSED(*args)); retv=readfn; \ /* 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_close(fp); \ return retv; @@ -213,12 +214,13 @@ nss_status_t nss_ldap_destructor(nss_backend_t *be,void UNUSED(*args)); fp=NULL; /* file should be closed by now */ \ return retv; -/* This macro generates a endent() function body. This just closes +/* This macro generates an endent() function body. This just closes the stream. */ #define NSS_ENDENT(fp) \ NSS_AVAILCHECK; \ if (fp!=NULL) \ { \ + (void)tio_skip(fp,0); /* read any buffered data */ \ (void)tio_close(fp); \ fp=NULL; \ } \ |