+extern int io_timeout;
+extern int allowed_lull;
+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 int remove_sent_files;
+extern int preserve_hard_links;
+extern char *filesfrom_host;
+extern struct stats stats;
+extern struct file_list *the_file_list;
+
+const char phase_unknown[] = "unknown";
+int select_timeout = SELECT_TIMEOUT;
+int ignore_timeout = 0;
+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 an EOF error if non-zero. See whine_about_eof(). */
+int kluge_around_eof = 0;
+
+int msg_fd_in = -1;
+int msg_fd_out = -1;
+int sock_f_in = -1;
+int sock_f_out = -1;
+
+static int io_multiplexing_out;
+static int io_multiplexing_in;
+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 size_t contiguous_write_len = 0;
+
+static void read_loop(int fd, char *buf, size_t len);
+
+struct flist_ndx_item {
+ struct flist_ndx_item *next;
+ int ndx;
+};
+
+struct flist_ndx_list {
+ struct flist_ndx_item *head, *tail;
+};
+
+static struct flist_ndx_list redo_list, hlink_list;
+
+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 flist_ndx_push(struct flist_ndx_list *lp, int ndx)
+{
+ struct flist_ndx_item *item;
+
+ if (!(item = new(struct flist_ndx_item)))
+ out_of_memory("flist_ndx_push");
+ item->next = NULL;
+ item->ndx = ndx;
+ if (lp->tail)
+ lp->tail->next = item;
+ else
+ lp->head = item;
+ lp->tail = item;
+}
+
+static int flist_ndx_pop(struct flist_ndx_list *lp)
+{
+ struct flist_ndx_item *next;
+ int ndx;
+
+ if (!lp->head)
+ return -1;
+
+ ndx = lp->head->ndx;
+ next = lp->head->next;
+ free(lp->head);
+ lp->head = next;
+ if (!next)
+ lp->tail = NULL;
+
+ return ndx;
+}
+
+static void check_timeout(void)
+{
+ time_t t;
+
+ if (!io_timeout || ignore_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)))
+ out_of_memory("msg_list_add");
+ ml->next = NULL;
+ if (!(ml->buf = new_array(char, len+4)))
+ out_of_memory("msg_list_add");
+ 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)
+{
+ if (msg_fd_out < 0) {
+ io_multiplex_write(code, buf, len);
+ return;
+ }
+ 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 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);
+ }
+ flist_ndx_push(&redo_list, -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);
+ flist_ndx_push(&redo_list, IVAL(buf,0));
+ break;
+ case MSG_DELETED:
+ if (len >= (int)sizeof buf || !am_generator) {
+ rprintf(FERROR, "invalid message %d:%d\n", tag, len);
+ exit_cleanup(RERR_STREAMIO);
+ }
+ read_loop(fd, buf, len);
+ io_multiplex_write(MSG_DELETED, buf, len);
+ break;
+ case MSG_SUCCESS:
+ if (len != 4 || !am_generator) {
+ rprintf(FERROR, "invalid message %d:%d\n", tag, len);
+ exit_cleanup(RERR_STREAMIO);
+ }
+ read_loop(fd, buf, len);
+ if (remove_sent_files)
+ io_multiplex_write(MSG_SUCCESS, buf, len);
+ if (preserve_hard_links)
+ flist_ndx_push(&hlink_list, 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;
+}