+extern int io_timeout;
+extern int am_server;
+extern int am_daemon;
+extern int am_sender;
+extern int am_generator;
+extern int eol_nulls;
+extern int csum_length;
+extern int checksum_seed;
+extern int protocol_version;
+extern char *remote_filesfrom_file;
+extern struct stats stats;
+
+const char phase_unknown[] = "unknown";
+int select_timeout = SELECT_TIMEOUT;
+int batch_fd = -1;
+int batch_gen_fd = -1;
+
+/**
+ * The connection might be dropped at some point; perhaps because the
+ * remote instance crashed. Just giving the offset on the stream is
+ * not very helpful. So instead we try to make io_phase_name point to
+ * something useful.
+ *
+ * For buffered/multiplexed I/O these names will be somewhat
+ * approximate; perhaps for ease of support we would rather make the
+ * buffer always flush when a single application-level I/O finishes.
+ *
+ * @todo Perhaps we want some simple stack functionality, but there's
+ * no need to overdo it.
+ **/
+const char *io_write_phase = phase_unknown;
+const char *io_read_phase = phase_unknown;
+
+/** Ignore EOF errors while reading a module listing if the remote
+ version is 24 or less. */
+int kludge_around_eof = False;
+
+int msg_fd_in = -1;
+int msg_fd_out = -1;
+
+static int io_multiplexing_out;
+static int io_multiplexing_in;
+static int sock_f_in = -1;
+static int sock_f_out = -1;
+static time_t last_io;
+static int no_flush;
+
+static int write_batch_monitor_in = -1;
+static int write_batch_monitor_out = -1;
+
+static int io_filesfrom_f_in = -1;
+static int io_filesfrom_f_out = -1;
+static char io_filesfrom_buf[2048];
+static char *io_filesfrom_bp;
+static char io_filesfrom_lastchar;
+static int io_filesfrom_buflen;
+
+static void read_loop(int fd, char *buf, size_t len);
+
+struct redo_list {
+ struct redo_list *next;
+ int num;
+};
+
+static struct redo_list *redo_list_head;
+static struct redo_list *redo_list_tail;
+
+struct msg_list {
+ struct msg_list *next;
+ char *buf;
+ int len;
+};
+
+static struct msg_list *msg_list_head;
+static struct msg_list *msg_list_tail;
+
+static void redo_list_add(int num)
+{
+ struct redo_list *rl;
+
+ if (!(rl = new(struct redo_list)))
+ exit_cleanup(RERR_MALLOC);
+ rl->next = NULL;
+ rl->num = num;
+ if (redo_list_tail)
+ redo_list_tail->next = rl;
+ else
+ redo_list_head = rl;
+ redo_list_tail = rl;
+}
+
+static void check_timeout(void)
+{
+ time_t t;
+
+ if (!io_timeout)
+ return;
+
+ if (!last_io) {
+ last_io = time(NULL);
+ return;
+ }
+
+ t = time(NULL);
+
+ if (t - last_io >= io_timeout) {
+ if (!am_server && !am_daemon) {
+ rprintf(FERROR, "io timeout after %d seconds - exiting\n",
+ (int)(t-last_io));
+ }
+ exit_cleanup(RERR_TIMEOUT);
+ }
+}
+
+/* Note the fds used for the main socket (which might really be a pipe
+ * for a local transfer, but we can ignore that). */
+void io_set_sock_fds(int f_in, int f_out)
+{
+ sock_f_in = f_in;
+ sock_f_out = f_out;
+}
+
+/* Setup the fd used to receive MSG_* messages. Only needed during the
+ * early stages of being a local sender (up through the sending of the
+ * file list) or when we're the generator (to fetch the messages from
+ * the receiver). */
+void set_msg_fd_in(int fd)
+{
+ msg_fd_in = fd;
+}
+
+/* Setup the fd used to send our MSG_* messages. Only needed when
+ * we're the receiver (to send our messages to the generator). */
+void set_msg_fd_out(int fd)
+{
+ msg_fd_out = fd;
+ set_nonblocking(msg_fd_out);
+}
+
+/* Add a message to the pending MSG_* list. */
+static void msg_list_add(int code, char *buf, int len)
+{
+ struct msg_list *ml;
+
+ if (!(ml = new(struct msg_list)))
+ exit_cleanup(RERR_MALLOC);
+ ml->next = NULL;
+ if (!(ml->buf = new_array(char, len+4)))
+ exit_cleanup(RERR_MALLOC);
+ SIVAL(ml->buf, 0, ((code+MPLEX_BASE)<<24) | len);
+ memcpy(ml->buf+4, buf, len);
+ ml->len = len+4;
+ if (msg_list_tail)
+ msg_list_tail->next = ml;
+ else
+ msg_list_head = ml;
+ msg_list_tail = ml;
+}
+
+void send_msg(enum msgcode code, char *buf, int len)
+{
+ msg_list_add(code, buf, len);
+ msg_list_push(NORMAL_FLUSH);
+}
+
+/* Read a message from the MSG_* fd and handle it. This is called either
+ * during the early stages of being a local sender (up through the sending
+ * of the file list) or when we're the generator (to fetch the messages
+ * from the receiver). */
+static void read_msg_fd(void)
+{
+ char buf[2048];
+ size_t n;
+ int fd = msg_fd_in;
+ int tag, len;
+
+ /* Temporarily disable msg_fd_in. This is needed to avoid looping back
+ * to this routine from read_timeout() and writefd_unbuffered(). */
+ msg_fd_in = -1;
+
+ read_loop(fd, buf, 4);
+ tag = IVAL(buf, 0);
+
+ len = tag & 0xFFFFFF;
+ tag = (tag >> 24) - MPLEX_BASE;
+
+ switch (tag) {
+ case MSG_DONE:
+ if (len != 0 || !am_generator) {
+ rprintf(FERROR, "invalid message %d:%d\n", tag, len);
+ exit_cleanup(RERR_STREAMIO);
+ }
+ redo_list_add(-1);
+ break;
+ case MSG_REDO:
+ if (len != 4 || !am_generator) {
+ rprintf(FERROR, "invalid message %d:%d\n", tag, len);
+ exit_cleanup(RERR_STREAMIO);
+ }
+ read_loop(fd, buf, 4);
+ redo_list_add(IVAL(buf,0));
+ break;
+ case MSG_INFO:
+ case MSG_ERROR:
+ case MSG_LOG:
+ while (len) {
+ n = len;
+ if (n >= sizeof buf)
+ n = sizeof buf - 1;
+ read_loop(fd, buf, n);
+ rwrite((enum logcode)tag, buf, n);
+ len -= n;
+ }
+ break;
+ default:
+ rprintf(FERROR, "unknown message %d:%d\n", tag, len);
+ exit_cleanup(RERR_STREAMIO);
+ }
+
+ msg_fd_in = fd;
+}
+
+/* Try to push messages off the list onto the wire. If we leave with more
+ * to do, return 0. On error, return -1. If everything flushed, return 1.
+ * This is only active in the receiver. */
+int msg_list_push(int flush_it_all)
+{
+ static int written = 0;
+ struct timeval tv;
+ fd_set fds;
+
+ if (msg_fd_out < 0)
+ return -1;
+
+ while (msg_list_head) {
+ struct msg_list *ml = msg_list_head;
+ int n = write(msg_fd_out, ml->buf + written, ml->len - written);
+ if (n < 0) {
+ if (errno == EINTR)
+ continue;
+ if (errno != EWOULDBLOCK && errno != EAGAIN)
+ return -1;
+ if (!flush_it_all)
+ return 0;
+ FD_ZERO(&fds);
+ FD_SET(msg_fd_out, &fds);
+ tv.tv_sec = select_timeout;
+ tv.tv_usec = 0;
+ if (!select(msg_fd_out+1, NULL, &fds, NULL, &tv))
+ check_timeout();
+ } else if ((written += n) == ml->len) {
+ free(ml->buf);
+ msg_list_head = ml->next;
+ if (!msg_list_head)
+ msg_list_tail = NULL;
+ free(ml);
+ written = 0;
+ }
+ }
+ return 1;
+}
+
+int get_redo_num(void)
+{
+ struct redo_list *next;
+ int num;
+
+ while (!redo_list_head)
+ read_msg_fd();
+
+ num = redo_list_head->num;
+ next = redo_list_head->next;
+ free(redo_list_head);
+ redo_list_head = next;
+ if (!next)
+ redo_list_tail = NULL;
+
+ return num;
+}
+
+/**
+ * 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 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(int fd)
+{
+ if (kludge_around_eof && fd == sock_f_in)
+ exit_cleanup(0);
+
+ 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, ret = 0;
+
+ io_flush(NORMAL_FLUSH);
+
+ while (ret == 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_fd_in >= 0) {
+ FD_SET(msg_fd_in, &r_fds);
+ if (msg_fd_in > maxfd)
+ maxfd = msg_fd_in;
+ } else 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_fd_in >= 0 && FD_ISSET(msg_fd_in, &r_fds))
+ read_msg_fd();
+ else if (msg_list_head && FD_ISSET(msg_fd_out, &w_fds))
+ msg_list_push(NORMAL_FLUSH);