From 15498aca67bc96b1ac5dbd35be40d8c69a0e2d73 Mon Sep 17 00:00:00 2001 From: Arthur de Jong Date: Wed, 14 Mar 2012 20:31:38 +0000 Subject: 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 --- common/tio.c | 33 +++++++++++++++++++++++++++++++-- common/tio.h | 6 ++++-- 2 files changed, 35 insertions(+), 4 deletions(-) (limited to 'common') 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. */ -- cgit v1.2.3