- /* support for POLLHUP. An hung up descriptor does not
- increase the return value! */
- if (recv (pfd[i].fd, data, 64, MSG_PEEK) == -1)
+ 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;
+#else
+ char data[64];
+ r = recv (pfd[i].fd, data, 64, MSG_PEEK);
+ if (r == -1)