+ if (iobuf_out_cnt)
+ io_flush(NORMAL_FLUSH);
+ else
+ read_msg_fd();
+}
+
+int get_redo_num(void)
+{
+ return flist_ndx_pop(&redo_list);
+}
+
+int get_hlink_num(void)
+{
+ return flist_ndx_pop(&hlink_list);
+}
+
+/**
+ * When we're the receiver and we have a local --files-from list of names
+ * that needs to be sent over the socket to the sender, we have to do two
+ * things at the same time: send the sender a list of what files we're
+ * processing and read the incoming file+info list from the sender. We do
+ * this by augmenting the read_timeout() function to copy this data. It
+ * uses ff_buf to read a block of data from f_in (when it is ready, since
+ * it might be a pipe) and then blast it out f_out (when it is ready to
+ * receive more data).
+ */
+void io_set_filesfrom_fds(int f_in, int f_out)
+{
+ io_filesfrom_f_in = f_in;
+ io_filesfrom_f_out = f_out;
+ alloc_xbuf(&ff_buf, 2048);
+#ifdef ICONV_OPTION
+ if (protect_args)
+ alloc_xbuf(&iconv_buf, 1024);
+#endif
+}
+
+/* It's almost always an error to get an EOF when we're trying to read from the
+ * network, because the protocol is (for the most part) self-terminating.
+ *
+ * There is one case for the receiver when it is at the end of the transfer
+ * (hanging around reading any keep-alive packets that might come its way): if
+ * the sender dies before the generator's kill-signal comes through, we can end
+ * up here needing to loop until the kill-signal arrives. In this situation,
+ * kluge_around_eof will be < 0.
+ *
+ * There is another case for older protocol versions (< 24) where the module
+ * listing was not terminated, so we must ignore an EOF error in that case and
+ * exit. In this situation, kluge_around_eof will be > 0. */
+static void whine_about_eof(int fd)
+{
+ if (kluge_around_eof && fd == sock_f_in) {
+ int i;
+ if (kluge_around_eof > 0)
+ exit_cleanup(0);
+ /* If we're still here after 10 seconds, exit with an error. */
+ for (i = 10*1000/20; i--; )
+ msleep(20);
+ }
+
+ rprintf(FERROR, RSYNC_NAME ": connection unexpectedly closed "
+ "(%.0f bytes received so far) [%s]\n",
+ (double)stats.total_read, who_am_i());
+
+ exit_cleanup(RERR_STREAMIO);
+}
+
+/**
+ * Read from a socket with I/O 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, cnt = 0;
+
+ io_flush(FULL_FLUSH);
+
+ while (cnt == 0) {
+ /* until we manage to read *something* */
+ fd_set r_fds, w_fds;
+ struct timeval tv;
+ int maxfd = fd;
+ int count;
+
+ FD_ZERO(&r_fds);
+ FD_ZERO(&w_fds);
+ FD_SET(fd, &r_fds);
+ if (io_filesfrom_f_out >= 0) {
+ int new_fd;
+ if (ff_buf.len == 0) {
+ if (io_filesfrom_f_in >= 0) {
+ FD_SET(io_filesfrom_f_in, &r_fds);
+ new_fd = io_filesfrom_f_in;
+ } else {
+ io_filesfrom_f_out = -1;
+ new_fd = -1;
+ }
+ } else {
+ FD_SET(io_filesfrom_f_out, &w_fds);
+ new_fd = io_filesfrom_f_out;
+ }
+ if (new_fd > maxfd)
+ maxfd = new_fd;
+ }
+
+ tv.tv_sec = select_timeout;
+ tv.tv_usec = 0;
+
+ errno = 0;
+
+ count = select(maxfd + 1, &r_fds, &w_fds, NULL, &tv);
+
+ if (count <= 0) {
+ if (errno == EBADF) {
+ defer_forwarding_messages = 0;
+ exit_cleanup(RERR_SOCKETIO);
+ }
+ check_timeout();
+ continue;
+ }
+
+ if (io_filesfrom_f_out >= 0) {
+ if (ff_buf.len) {
+ if (FD_ISSET(io_filesfrom_f_out, &w_fds)) {
+ int l = write(io_filesfrom_f_out,
+ ff_buf.buf + ff_buf.pos,
+ ff_buf.len);
+ if (l > 0) {
+ if (!(ff_buf.len -= l))
+ ff_buf.pos = 0;
+ else
+ ff_buf.pos += l;
+ } else if (errno != EINTR) {
+ /* XXX should we complain? */
+ io_filesfrom_f_out = -1;
+ }
+ }
+ } else if (io_filesfrom_f_in >= 0) {
+ if (FD_ISSET(io_filesfrom_f_in, &r_fds)) {
+#ifdef ICONV_OPTION
+ xbuf *ibuf = filesfrom_convert ? &iconv_buf : &ff_buf;
+#else
+ xbuf *ibuf = &ff_buf;
+#endif
+ int l = read(io_filesfrom_f_in, ibuf->buf, ibuf->size);
+ if (l <= 0) {
+ if (l == 0 || errno != EINTR) {
+ /* Send end-of-file marker */
+ memcpy(ff_buf.buf, "\0\0", 2);
+ ff_buf.len = ff_lastchar? 2 : 1;
+ ff_buf.pos = 0;
+ io_filesfrom_f_in = -1;
+ }
+ } else {
+#ifdef ICONV_OPTION
+ if (filesfrom_convert) {
+ iconv_buf.pos = 0;
+ iconv_buf.len = l;
+ iconvbufs(ic_send, &iconv_buf, &ff_buf,
+ ICB_EXPAND_OUT|ICB_INCLUDE_BAD|ICB_INCLUDE_INCOMPLETE);
+ l = ff_buf.len;
+ }
+#endif
+ if (!eol_nulls) {
+ char *s = ff_buf.buf + l;
+ /* Transform CR and/or LF into '\0' */
+ while (s-- > ff_buf.buf) {
+ if (*s == '\n' || *s == '\r')
+ *s = '\0';
+ }
+ }
+ if (!ff_lastchar) {
+ /* Last buf ended with a '\0', so don't
+ * let this buf start with one. */
+ while (l && ff_buf.buf[ff_buf.pos] == '\0')
+ ff_buf.pos++, l--;
+ }
+ if (!l)
+ ff_buf.pos = 0;
+ else {
+ char *f = ff_buf.buf + ff_buf.pos;
+ char *t = f;
+ char *eob = f + l;
+ /* Eliminate any multi-'\0' runs. */
+ while (f != eob) {
+ if (!(*t++ = *f++)) {
+ while (f != eob && !*f)
+ f++, l--;
+ }
+ }
+ ff_lastchar = f[-1];
+ }
+ ff_buf.len = l;
+ }
+ }
+ }
+ }
+
+ if (!FD_ISSET(fd, &r_fds))
+ continue;
+
+ n = read(fd, buf, len);
+
+ if (n <= 0) {
+ if (n == 0)
+ whine_about_eof(fd); /* Doesn't return. */
+ if (errno == EINTR || errno == EWOULDBLOCK
+ || errno == EAGAIN)
+ continue;
+
+ /* Don't write errors on a dead socket. */
+ if (fd == sock_f_in) {
+ io_end_multiplex_out();
+ rsyserr(FERROR_SOCKET, errno, "read error");
+ } else
+ rsyserr(FERROR, errno, "read error");
+ exit_cleanup(RERR_STREAMIO);
+ }