+ io_error_fd = fd;
+}
+
+/** Read some data from the error fd and write it to the write log code */
+static void read_error_fd(void)
+{
+ char buf[200];
+ size_t n;
+ int fd = io_error_fd;
+ int tag, len;
+
+ /* io_error_fd is temporarily disabled -- is this meant to
+ * prevent indefinite recursion? */
+ io_error_fd = -1;
+
+ read_loop(fd, buf, 4);
+ tag = IVAL(buf, 0);
+
+ len = tag & 0xFFFFFF;
+ tag = tag >> 24;
+ tag -= MPLEX_BASE;
+
+ while (len) {
+ n = len;
+ if (n > (sizeof(buf)-1))
+ n = sizeof(buf)-1;
+ read_loop(fd, buf, n);
+ rwrite((enum logcode)tag, buf, n);
+ len -= n;
+ }
+
+ io_error_fd = fd;
+}
+
+
+/**
+ * It's almost always an error to get an EOF when we're trying to read
+ * from the network, because the protocol is self-terminating.
+ *
+ * However, there is one unfortunate cases where it is not, which is
+ * rsync <2.4.6 sending a list of modules on a server, since the list
+ * is terminated by closing the socket. So, for the section of the
+ * program where that is a problem (start_socket_client),
+ * kludge_around_eof is True and we just exit.
+ */
+static void whine_about_eof (void)
+{
+ if (kludge_around_eof)
+ exit_cleanup (0);
+ else {
+ rprintf (FERROR,
+ "%s: connection unexpectedly closed "
+ "(%.0f bytes read so far)\n",
+ RSYNC_NAME, (double)stats.total_read);
+
+ exit_cleanup (RERR_STREAMIO);
+ }
+}
+
+
+static void die_from_readerr (int err)
+{
+ /* this prevents us trying to write errors on a dead socket */
+ io_multiplexing_close();
+
+ rprintf(FERROR, "%s: read error: %s\n",
+ RSYNC_NAME, strerror (err));
+ exit_cleanup(RERR_STREAMIO);
+}
+
+
+/**
+ * Read from a socket with IO timeout. return the number of bytes
+ * read. If no bytes can be read then exit, never return a number <= 0.
+ *
+ * TODO: If the remote shell connection fails, then current versions
+ * actually report an "unexpected EOF" error here. Since it's a
+ * fairly common mistake to try to use rsh when ssh is required, we
+ * should trap that: if we fail to read any data at all, we should
+ * give a better explanation. We can tell whether the connection has
+ * started by looking e.g. at whether the remote version is known yet.
+ */
+static int read_timeout (int fd, char *buf, size_t len)
+{
+ int n, ret=0;
+
+ io_flush();
+
+ while (ret == 0) {
+ /* until we manage to read *something* */
+ fd_set fds;
+ struct timeval tv;
+ int fd_count = fd+1;
+ int count;
+
+ FD_ZERO(&fds);
+ FD_SET(fd, &fds);
+ if (io_error_fd != -1) {
+ FD_SET(io_error_fd, &fds);
+ if (io_error_fd > fd) fd_count = io_error_fd+1;
+ }
+
+ tv.tv_sec = io_timeout?io_timeout:SELECT_TIMEOUT;
+ tv.tv_usec = 0;
+
+ errno = 0;
+
+ count = select(fd_count, &fds, NULL, NULL, &tv);
+
+ if (count == 0) {
+ check_timeout();
+ }
+
+ if (count <= 0) {
+ if (errno == EBADF) {
+ exit_cleanup(RERR_SOCKETIO);
+ }
+ continue;
+ }
+
+ if (io_error_fd != -1 && FD_ISSET(io_error_fd, &fds)) {
+ read_error_fd();
+ }
+
+ if (!FD_ISSET(fd, &fds)) continue;
+
+ n = read(fd, buf, len);
+
+ if (n > 0) {
+ buf += n;
+ len -= n;
+ ret += n;
+ if (io_timeout)
+ last_io = time(NULL);
+ continue;
+ } else if (n == 0) {
+ whine_about_eof ();
+ return -1; /* doesn't return */
+ } else if (n == -1) {
+ if (errno == EINTR || errno == EWOULDBLOCK ||
+ errno == EAGAIN)
+ continue;
+ else
+ die_from_readerr (errno);
+ }
+ }
+
+ return ret;
+}
+
+
+
+
+/**
+ * Continue trying to read len bytes - don't return until len has been
+ * read.
+ **/
+static void read_loop (int fd, char *buf, size_t len)
+{
+ while (len) {
+ int n = read_timeout(fd, buf, len);
+
+ buf += n;
+ len -= n;
+ }
+}
+
+
+/**
+ * Read from the file descriptor handling multiplexing - return number
+ * of bytes read.
+ *
+ * Never returns <= 0.
+ */
+static int read_unbuffered(int fd, char *buf, size_t len)
+{
+ static size_t remaining;
+ int tag, ret = 0;
+ char line[1024];
+
+ if (!io_multiplexing_in || fd != multiplex_in_fd)
+ return read_timeout(fd, buf, len);
+
+ while (ret == 0) {
+ if (remaining) {
+ len = MIN(len, remaining);
+ read_loop(fd, buf, len);
+ remaining -= len;
+ ret = len;
+ continue;
+ }
+
+ read_loop(fd, line, 4);
+ tag = IVAL(line, 0);
+
+ remaining = tag & 0xFFFFFF;
+ tag = tag >> 24;
+
+ if (tag == MPLEX_BASE)
+ continue;
+
+ tag -= MPLEX_BASE;
+
+ if (tag != FERROR && tag != FINFO) {
+ rprintf(FERROR, "unexpected tag %d\n", tag);
+ exit_cleanup(RERR_STREAMIO);
+ }
+
+ if (remaining > sizeof(line) - 1) {
+ rprintf(FERROR, "multiplexing overflow %d\n\n",
+ remaining);
+ exit_cleanup(RERR_STREAMIO);
+ }
+
+ read_loop(fd, line, remaining);
+ line[remaining] = 0;
+
+ rprintf((enum logcode) tag, "%s", line);
+ remaining = 0;
+ }
+
+ return ret;
+}
+
+
+
+/**
+ * Do a buffered read from @p fd. Don't return until all @p n bytes
+ * have been read. If all @p n can't be read then exit with an
+ * error.
+ **/
+static void readfd (int fd, char *buffer, size_t N)
+{
+ int ret;
+ size_t total=0;
+
+ while (total < N) {
+ io_flush();
+
+ ret = read_unbuffered (fd, buffer + total, N-total);
+ total += ret;
+ }
+
+ stats.total_read += total;