+static char *read_buffer;
+static char *read_buffer_p;
+static int read_buffer_len;
+static int read_buffer_size;
+static int no_flush;
+static int no_flush_read;
+
+/* 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 */
+static int read_timeout(int fd, char *buf, int len)
+{
+ int n, ret=0;
+
+ no_flush_read++;
+ io_flush();
+ no_flush_read--;
+
+ while (ret == 0) {
+ fd_set fds;
+ struct timeval tv;
+ int fd_count = fd+1;
+
+ 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;
+
+ if (select(fd_count, &fds, NULL, NULL, &tv) < 1) {
+ if (errno == EBADF) {
+ exit_cleanup(RERR_SOCKETIO);
+ }
+ check_timeout();
+ 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;
+ }
+
+ if (n == -1 && errno == EINTR) {
+ continue;
+ }
+
+
+ if (n == 0) {
+ if (eof_error) {
+ rprintf(FERROR,"unexpected EOF in read_timeout\n");
+ }
+ exit_cleanup(RERR_STREAMIO);
+ }
+
+ /* this prevents us trying to write errors on a dead socket */
+ io_multiplexing_close();
+
+ rprintf(FERROR,"read error: %s\n", strerror(errno));
+ exit_cleanup(RERR_STREAMIO);
+ }
+
+ return ret;
+}
+
+/* continue trying to read len bytes - don't return until len
+ has been read */
+static void read_loop(int fd, char *buf, int 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 return <= 0 */
+static int read_unbuffered(int fd, char *buf, int len)
+{
+ static int remaining;
+ char ibuf[4];
+ 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, ibuf, 4);
+ tag = IVAL(ibuf, 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(tag,"%s", line);
+ remaining = 0;
+
+ if (in_read_check) break;
+ }
+
+ return ret;
+}
+