Le mardi 19 décembre 2006 à 09:52 -0800, Paul Eggert a écrit :
> Paolo Bonzini <[EMAIL PROTECTED]> writes:
>
> > I don't remember if it was Paul or Bruno who suggested that POLLHUP
> > does not increase the return value. What do you think?
>
> It wasn't me, so I guess it was Bruno. I don't have any opinion on
> this one, other than that if it seems good to you and Yoann, that's
> good enough for me.
Attached is an improved version that should conform more to GnuLib
coding standard.
Regards,
--
Yoann Vandoorselaere | Responsable R&D / CTO | PreludeIDS Technologies
Tel: +33 (0)8 70 70 21 58 Fax: +33(0)4 78 42 21 58
http://www.prelude-ids.com
Index: lib/poll.c
===================================================================
RCS file: /cvsroot/gnulib/gnulib/lib/poll.c,v
retrieving revision 1.5
diff -u -r1.5 poll.c
--- lib/poll.c 28 Sep 2006 19:58:33 -0000 1.5
+++ lib/poll.c 19 Dec 2006 19:22:40 -0000
@@ -28,6 +28,7 @@
#include <sys/socket.h>
#include <sys/select.h>
#include <unistd.h>
+#include <string.h>
#ifdef HAVE_SYS_IOCTL_H
#include <sys/ioctl.h>
@@ -158,40 +159,33 @@
if (FD_ISSET (pfd[i].fd, &rfds))
{
int r;
- long avail = -1;
- /* support for POLLHUP. */
+
#if defined __MACH__ && defined __APPLE__
/* There is a bug in Mac OS X that causes it to ignore MSG_PEEK for
- some kinds of descriptors. Use FIONREAD to emulate POLLHUP.
- It is still not completely POSIX compliant (it does not fully
- work on TTYs), but at least it does not delete data! For other
- platforms, we still use MSG_PEEK because it was proved to be
- reliable, and I a leery of changing it. */
- do
- r = ioctl (pfd[i].fd, FIONREAD, &avail);
- while (r == -1 && (errno == EAGAIN || errno == EINTR));
- if (avail < 0)
- avail = 0;
+ some kinds of descriptors. Use a length of 0. */
+ r = recv (pfd[i].fd, NULL, 0, MSG_PEEK);
+ if (r == 0)
+ happened = POLLIN | POLLRDNORM;
#else
char data[64];
- r = recv (pfd[i].fd, data, 64, MSG_PEEK);
- if (r == -1)
+
+ r = recv (pfd[i].fd, data, sizeof (data), MSG_PEEK);
+ if (r == 0)
+ happened = POLLHUP;
+#endif
+ else if (r > 0)
+ happened = POLLIN | POLLRDNORM;
+
+ else if (r < 0)
{
- avail = (errno == ESHUTDOWN || errno == ECONNRESET ||
- errno == ECONNABORTED || errno == ENETRESET) ? 0 : -1;
+ if (errno == ENOTCONN)
+ happened = POLLIN | POLLRDNORM; /* Event happening on an unconnected server socket. */
+ else
+ happened = (errno == ESHUTDOWN || errno == ECONNRESET ||
+ errno == ECONNABORTED
+ || errno == ENETRESET) ? POLLHUP : POLLERR;
errno = 0;
}
- else
- avail = r;
-#endif
-
- /* An hung up descriptor does not increase the return value! */
- if (avail == 0)
- pfd[i].revents |= POLLHUP;
- else if (avail == -1)
- pfd[i].revents |= POLLERR;
- else
- happened |= POLLIN | POLLRDNORM;
}
if (FD_ISSET (pfd[i].fd, &wfds))
@@ -200,7 +194,7 @@
if (FD_ISSET (pfd[i].fd, &efds))
happened |= POLLPRI | POLLRDBAND;
- pfd[i].revents |= pfd[i].events & happened;
+ pfd[i].revents |= happened;
rc += (happened > 0);
}
}