+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 the io_filesfrom_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;
+ io_filesfrom_bp = io_filesfrom_buf;
+ io_filesfrom_lastchar = '\0';
+ io_filesfrom_buflen = 0;
+}
+
+/* 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(NORMAL_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 (msg_list.head) {
+ FD_SET(msg_fd_out, &w_fds);
+ if (msg_fd_out > maxfd)
+ maxfd = msg_fd_out;
+ }
+ if (io_filesfrom_f_out >= 0) {
+ int new_fd;
+ if (io_filesfrom_buflen == 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)
+ exit_cleanup(RERR_SOCKETIO);
+ check_timeout();
+ continue;
+ }
+
+ if (msg_list.head && FD_ISSET(msg_fd_out, &w_fds))
+ msg_list_flush(NORMAL_FLUSH);
+
+ if (io_filesfrom_f_out >= 0) {
+ if (io_filesfrom_buflen) {
+ if (FD_ISSET(io_filesfrom_f_out, &w_fds)) {
+ int l = write(io_filesfrom_f_out,
+ io_filesfrom_bp,
+ io_filesfrom_buflen);
+ if (l > 0) {
+ if (!(io_filesfrom_buflen -= l))
+ io_filesfrom_bp = io_filesfrom_buf;
+ else
+ io_filesfrom_bp += l;
+ } else {
+ /* 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)) {
+ int l = read(io_filesfrom_f_in,
+ io_filesfrom_buf,
+ sizeof io_filesfrom_buf);
+ if (l <= 0) {
+ /* Send end-of-file marker */
+ io_filesfrom_buf[0] = '\0';
+ io_filesfrom_buf[1] = '\0';
+ io_filesfrom_buflen = io_filesfrom_lastchar? 2 : 1;
+ io_filesfrom_f_in = -1;
+ } else {
+ if (!eol_nulls) {
+ char *s = io_filesfrom_buf + l;
+ /* Transform CR and/or LF into '\0' */
+ while (s-- > io_filesfrom_buf) {
+ if (*s == '\n' || *s == '\r')
+ *s = '\0';
+ }
+ }
+ if (!io_filesfrom_lastchar) {
+ /* Last buf ended with a '\0', so don't
+ * let this buf start with one. */
+ while (l && !*io_filesfrom_bp)
+ io_filesfrom_bp++, l--;
+ }
+ if (!l)
+ io_filesfrom_bp = io_filesfrom_buf;
+ else {
+ char *f = io_filesfrom_bp;
+ char *t = f;
+ char *eob = f + l;
+ /* Eliminate any multi-'\0' runs. */
+ while (f != eob) {
+ if (!(*t++ = *f++)) {
+ while (f != eob && !*f)
+ f++, l--;
+ }
+ }
+ io_filesfrom_lastchar = f[-1];
+ }
+ io_filesfrom_buflen = 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) {
+ close_multiplexing_out();
+ rsyserr(FSOCKERR, errno, "read error");
+ } else
+ rsyserr(FERROR, errno, "read error");
+ exit_cleanup(RERR_STREAMIO);
+ }
+
+ buf += n;
+ len -= n;
+ cnt += n;
+
+ if (fd == sock_f_in && io_timeout)
+ last_io_in = time(NULL);
+ }
+
+ return cnt;
+}
+
+/**
+ * Read a line into the "fname" buffer (which must be at least MAXPATHLEN
+ * characters long).
+ */
+int read_filesfrom_line(int fd, char *fname)
+{
+ char ch, *s, *eob = fname + MAXPATHLEN - 1;
+ int cnt;
+ int reading_remotely = filesfrom_host != NULL;
+ int nulls = eol_nulls || reading_remotely;
+
+ start:
+ s = fname;
+ while (1) {
+ cnt = read(fd, &ch, 1);
+ if (cnt < 0 && (errno == EWOULDBLOCK
+ || errno == EINTR || errno == EAGAIN)) {
+ struct timeval tv;
+ fd_set fds;
+ FD_ZERO(&fds);
+ FD_SET(fd, &fds);
+ tv.tv_sec = select_timeout;
+ tv.tv_usec = 0;
+ if (!select(fd+1, &fds, NULL, NULL, &tv))
+ check_timeout();
+ continue;
+ }
+ if (cnt != 1)
+ break;
+ if (nulls? !ch : (ch == '\r' || ch == '\n')) {
+ /* Skip empty lines if reading locally. */
+ if (!reading_remotely && s == fname)
+ continue;
+ break;
+ }
+ if (s < eob)
+ *s++ = ch;
+ }
+ *s = '\0';
+
+ /* Dump comments. */
+ if (*fname == '#' || *fname == ';')
+ goto start;
+
+ return s - fname;
+}
+
+static char *iobuf_out;
+static int iobuf_out_cnt;
+
+void io_start_buffering_out(void)
+{
+ if (iobuf_out)
+ return;
+ if (!(iobuf_out = new_array(char, IO_BUFFER_SIZE)))
+ out_of_memory("io_start_buffering_out");
+ iobuf_out_cnt = 0;
+}
+
+static char *iobuf_in;
+static size_t iobuf_in_siz;
+
+void io_start_buffering_in(void)
+{
+ if (iobuf_in)
+ return;
+ iobuf_in_siz = 2 * IO_BUFFER_SIZE;
+ if (!(iobuf_in = new_array(char, iobuf_in_siz)))
+ out_of_memory("io_start_buffering_in");
+}
+
+void io_end_buffering(void)
+{
+ io_flush(NORMAL_FLUSH);
+ if (!io_multiplexing_out) {
+ free(iobuf_out);
+ iobuf_out = NULL;
+ }
+}
+
+void maybe_flush_socket(void)
+{
+ if (iobuf_out && iobuf_out_cnt && time(NULL) - last_io_out >= 5)
+ io_flush(NORMAL_FLUSH);
+}
+
+void maybe_send_keepalive(void)
+{
+ if (time(NULL) - last_io_out >= allowed_lull) {
+ if (!iobuf_out || !iobuf_out_cnt) {
+ if (protocol_version < 29)
+ return; /* there's nothing we can do */
+ write_int(sock_f_out, the_file_list->count);
+ write_shortint(sock_f_out, ITEM_IS_NEW);
+ }
+ if (iobuf_out)
+ io_flush(NORMAL_FLUSH);
+ }
+}
+
+/**
+ * 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 readfd_unbuffered(int fd, char *buf, size_t len)
+{
+ static size_t remaining;
+ static size_t iobuf_in_ndx;
+ size_t msg_bytes;
+ int tag, cnt = 0;
+ char line[BIGPATHBUFLEN];
+
+ if (!iobuf_in || fd != sock_f_in)
+ return read_timeout(fd, buf, len);
+
+ if (!io_multiplexing_in && remaining == 0) {
+ remaining = read_timeout(fd, iobuf_in, iobuf_in_siz);
+ iobuf_in_ndx = 0;
+ }
+
+ while (cnt == 0) {
+ if (remaining) {
+ len = MIN(len, remaining);
+ memcpy(buf, iobuf_in + iobuf_in_ndx, len);
+ iobuf_in_ndx += len;
+ remaining -= len;
+ cnt = len;
+ break;
+ }
+
+ read_loop(fd, line, 4);
+ tag = IVAL(line, 0);
+
+ msg_bytes = tag & 0xFFFFFF;
+ tag = (tag >> 24) - MPLEX_BASE;
+
+ switch (tag) {
+ case MSG_DATA:
+ if (msg_bytes > iobuf_in_siz) {
+ if (!(iobuf_in = realloc_array(iobuf_in, char,
+ msg_bytes)))
+ out_of_memory("readfd_unbuffered");
+ iobuf_in_siz = msg_bytes;
+ }
+ read_loop(fd, iobuf_in, msg_bytes);
+ remaining = msg_bytes;
+ iobuf_in_ndx = 0;
+ break;
+ case MSG_DELETED:
+ if (msg_bytes >= sizeof line)
+ goto overflow;
+ read_loop(fd, line, msg_bytes);
+ line[msg_bytes] = '\0';
+ /* A directory name was sent with the trailing null */
+ if (msg_bytes > 0 && !line[msg_bytes-1])
+ log_delete(line, S_IFDIR);
+ else
+ log_delete(line, S_IFREG);
+ break;
+ case MSG_SUCCESS:
+ if (msg_bytes != 4) {
+ rprintf(FERROR, "invalid multi-message %d:%ld [%s]\n",
+ tag, (long)msg_bytes, who_am_i());
+ exit_cleanup(RERR_STREAMIO);
+ }
+ read_loop(fd, line, msg_bytes);
+ successful_send(IVAL(line, 0));
+ break;
+ case MSG_INFO:
+ case MSG_ERROR:
+ if (msg_bytes >= sizeof line) {
+ overflow:
+ rprintf(FERROR,
+ "multiplexing overflow %d:%ld [%s]\n",
+ tag, (long)msg_bytes, who_am_i());
+ exit_cleanup(RERR_STREAMIO);
+ }
+ read_loop(fd, line, msg_bytes);
+ rwrite((enum logcode)tag, line, msg_bytes);
+ break;
+ default:
+ rprintf(FERROR, "unexpected tag %d [%s]\n",
+ tag, who_am_i());
+ exit_cleanup(RERR_STREAMIO);
+ }
+ }