+void setup_readbuffer(int f_in)
+{
+ buffer_f_in = f_in;
+}
+
+static void check_timeout(void)
+{
+ time_t t;
+
+ if (!io_timeout) return;
+
+ if (!last_io) {
+ last_io = time(NULL);
+ return;
+ }
+
+ t = time(NULL);
+
+ if (last_io && io_timeout && (t-last_io)>io_timeout) {
+ rprintf(FERROR,"read timeout after %d second - exiting\n",
+ (int)(t-last_io));
+ exit_cleanup(1);
+ }
+}
+
+
+static char *read_buffer;
+static char *read_buffer_p;
+static int read_buffer_len;
+static int read_buffer_size;
+
+/* 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;
+
+ io_flush();
+
+ while (ret == 0) {
+ fd_set fds;
+ struct timeval tv;
+
+ FD_ZERO(&fds);
+ FD_SET(fd, &fds);
+ tv.tv_sec = io_timeout;
+ tv.tv_usec = 0;
+
+ if (select(fd+1, &fds, NULL, NULL,
+ io_timeout?&tv:NULL) != 1) {
+ check_timeout();
+ 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) {
+ rprintf(FERROR,"EOF in read_timeout\n");
+ exit_cleanup(1);
+ }
+
+ rprintf(FERROR,"read error: %s\n", strerror(errno));
+ exit_cleanup(1);
+ }
+
+ 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)