1 /* -*- c-file-style: "linux" -*-
3 * Copyright (C) 1996-2001 by Andrew Tridgell
4 * Copyright (C) Paul Mackerras 1996
5 * Copyright (C) 2001, 2002 by Martin Pool <mbp@samba.org>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25 * Socket and pipe I/O utilities used in rsync.
27 * rsync provides its own multiplexing system, which is used to send
28 * stderr and stdout over a single socket. We need this because
29 * stdout normally carries the binary data stream, and stderr all our
32 * For historical reasons this is off during the start of the
33 * connection, but it's switched on quite early using
34 * io_start_multiplex_out() and io_start_multiplex_in().
39 /** If no timeout is specified then use a 60 second select timeout */
40 #define SELECT_TIMEOUT 60
43 extern size_t bwlimit_writemax;
45 extern int io_timeout;
49 extern int am_generator;
51 extern int csum_length;
52 extern int checksum_seed;
53 extern int protocol_version;
54 extern char *filesfrom_host;
55 extern struct stats stats;
57 const char phase_unknown[] = "unknown";
58 int select_timeout = SELECT_TIMEOUT;
59 int ignore_timeout = 0;
61 int batch_gen_fd = -1;
64 * The connection might be dropped at some point; perhaps because the
65 * remote instance crashed. Just giving the offset on the stream is
66 * not very helpful. So instead we try to make io_phase_name point to
69 * For buffered/multiplexed I/O these names will be somewhat
70 * approximate; perhaps for ease of support we would rather make the
71 * buffer always flush when a single application-level I/O finishes.
73 * @todo Perhaps we want some simple stack functionality, but there's
74 * no need to overdo it.
76 const char *io_write_phase = phase_unknown;
77 const char *io_read_phase = phase_unknown;
79 /* Ignore an EOF error if non-zero. See whine_about_eof(). */
80 int kluge_around_eof = 0;
85 static int io_multiplexing_out;
86 static int io_multiplexing_in;
87 static int sock_f_in = -1;
88 static int sock_f_out = -1;
89 static time_t last_io;
92 static int write_batch_monitor_in = -1;
93 static int write_batch_monitor_out = -1;
95 static int io_filesfrom_f_in = -1;
96 static int io_filesfrom_f_out = -1;
97 static char io_filesfrom_buf[2048];
98 static char *io_filesfrom_bp;
99 static char io_filesfrom_lastchar;
100 static int io_filesfrom_buflen;
102 static void read_loop(int fd, char *buf, size_t len);
105 struct redo_list *next;
109 static struct redo_list *redo_list_head;
110 static struct redo_list *redo_list_tail;
113 struct msg_list *next;
118 static struct msg_list *msg_list_head;
119 static struct msg_list *msg_list_tail;
121 static void redo_list_add(int num)
123 struct redo_list *rl;
125 if (!(rl = new(struct redo_list)))
126 exit_cleanup(RERR_MALLOC);
130 redo_list_tail->next = rl;
136 static void check_timeout(void)
140 if (!io_timeout || ignore_timeout)
144 last_io = time(NULL);
150 if (t - last_io >= io_timeout) {
151 if (!am_server && !am_daemon) {
152 rprintf(FERROR, "io timeout after %d seconds -- exiting\n",
155 exit_cleanup(RERR_TIMEOUT);
159 /* Note the fds used for the main socket (which might really be a pipe
160 * for a local transfer, but we can ignore that). */
161 void io_set_sock_fds(int f_in, int f_out)
167 /* Setup the fd used to receive MSG_* messages. Only needed during the
168 * early stages of being a local sender (up through the sending of the
169 * file list) or when we're the generator (to fetch the messages from
171 void set_msg_fd_in(int fd)
176 /* Setup the fd used to send our MSG_* messages. Only needed when
177 * we're the receiver (to send our messages to the generator). */
178 void set_msg_fd_out(int fd)
181 set_nonblocking(msg_fd_out);
184 /* Add a message to the pending MSG_* list. */
185 static void msg_list_add(int code, char *buf, int len)
189 if (!(ml = new(struct msg_list)))
190 exit_cleanup(RERR_MALLOC);
192 if (!(ml->buf = new_array(char, len+4)))
193 exit_cleanup(RERR_MALLOC);
194 SIVAL(ml->buf, 0, ((code+MPLEX_BASE)<<24) | len);
195 memcpy(ml->buf+4, buf, len);
198 msg_list_tail->next = ml;
204 void send_msg(enum msgcode code, char *buf, int len)
206 if (msg_fd_out < 0) {
207 io_multiplex_write(code, buf, len);
210 msg_list_add(code, buf, len);
211 msg_list_push(NORMAL_FLUSH);
214 /* Read a message from the MSG_* fd and handle it. This is called either
215 * during the early stages of being a local sender (up through the sending
216 * of the file list) or when we're the generator (to fetch the messages
217 * from the receiver). */
218 static void read_msg_fd(void)
225 /* Temporarily disable msg_fd_in. This is needed to avoid looping back
226 * to this routine from read_timeout() and writefd_unbuffered(). */
229 read_loop(fd, buf, 4);
232 len = tag & 0xFFFFFF;
233 tag = (tag >> 24) - MPLEX_BASE;
237 if (len != 0 || !am_generator) {
238 rprintf(FERROR, "invalid message %d:%d\n", tag, len);
239 exit_cleanup(RERR_STREAMIO);
244 if (len != 4 || !am_generator) {
245 rprintf(FERROR, "invalid message %d:%d\n", tag, len);
246 exit_cleanup(RERR_STREAMIO);
248 read_loop(fd, buf, 4);
249 redo_list_add(IVAL(buf,0));
252 if (len >= (int)sizeof buf || !am_generator) {
253 rprintf(FERROR, "invalid message %d:%d\n", tag, len);
254 exit_cleanup(RERR_STREAMIO);
256 read_loop(fd, buf, len);
257 io_multiplex_write(MSG_DELETED, buf, len);
260 if (len != 4 || !am_generator) {
261 rprintf(FERROR, "invalid message %d:%d\n", tag, len);
262 exit_cleanup(RERR_STREAMIO);
264 read_loop(fd, buf, len);
265 io_multiplex_write(MSG_SUCCESS, buf, len);
274 read_loop(fd, buf, n);
275 rwrite((enum logcode)tag, buf, n);
280 rprintf(FERROR, "unknown message %d:%d\n", tag, len);
281 exit_cleanup(RERR_STREAMIO);
287 /* Try to push messages off the list onto the wire. If we leave with more
288 * to do, return 0. On error, return -1. If everything flushed, return 1.
289 * This is only active in the receiver. */
290 int msg_list_push(int flush_it_all)
292 static int written = 0;
299 while (msg_list_head) {
300 struct msg_list *ml = msg_list_head;
301 int n = write(msg_fd_out, ml->buf + written, ml->len - written);
305 if (errno != EWOULDBLOCK && errno != EAGAIN)
310 FD_SET(msg_fd_out, &fds);
311 tv.tv_sec = select_timeout;
313 if (!select(msg_fd_out+1, NULL, &fds, NULL, &tv))
315 } else if ((written += n) == ml->len) {
317 msg_list_head = ml->next;
319 msg_list_tail = NULL;
327 int get_redo_num(void)
329 struct redo_list *next;
332 while (!redo_list_head)
335 num = redo_list_head->num;
336 next = redo_list_head->next;
337 free(redo_list_head);
338 redo_list_head = next;
340 redo_list_tail = NULL;
346 * When we're the receiver and we have a local --files-from list of names
347 * that needs to be sent over the socket to the sender, we have to do two
348 * things at the same time: send the sender a list of what files we're
349 * processing and read the incoming file+info list from the sender. We do
350 * this by augmenting the read_timeout() function to copy this data. It
351 * uses the io_filesfrom_buf to read a block of data from f_in (when it is
352 * ready, since it might be a pipe) and then blast it out f_out (when it
353 * is ready to receive more data).
355 void io_set_filesfrom_fds(int f_in, int f_out)
357 io_filesfrom_f_in = f_in;
358 io_filesfrom_f_out = f_out;
359 io_filesfrom_bp = io_filesfrom_buf;
360 io_filesfrom_lastchar = '\0';
361 io_filesfrom_buflen = 0;
364 /* It's almost always an error to get an EOF when we're trying to read from the
365 * network, because the protocol is (for the most part) self-terminating.
367 * There is one case for the receiver when it is at the end of the transfer
368 * (hanging around reading any keep-alive packets that might come its way): if
369 * the sender dies before the generator's kill-signal comes through, we can end
370 * up here needing to loop until the kill-signal arrives. In this situation,
371 * kluge_around_eof will be < 0.
373 * There is another case for older protocol versions (< 24) where the module
374 * listing was not terminated, so we must ignore an EOF error in that case and
375 * exit. In this situation, kluge_around_eof will be > 0. */
376 static void whine_about_eof(int fd)
378 if (kluge_around_eof && fd == sock_f_in) {
380 if (kluge_around_eof > 0)
382 /* If we're still here after 10 seconds, exit with an error. */
383 for (i = 10*1000/20; i--; )
387 rprintf(FERROR, RSYNC_NAME ": connection unexpectedly closed "
388 "(%.0f bytes received so far) [%s]\n",
389 (double)stats.total_read, who_am_i());
391 exit_cleanup(RERR_STREAMIO);
396 * Read from a socket with I/O timeout. return the number of bytes
397 * read. If no bytes can be read then exit, never return a number <= 0.
399 * TODO: If the remote shell connection fails, then current versions
400 * actually report an "unexpected EOF" error here. Since it's a
401 * fairly common mistake to try to use rsh when ssh is required, we
402 * should trap that: if we fail to read any data at all, we should
403 * give a better explanation. We can tell whether the connection has
404 * started by looking e.g. at whether the remote version is known yet.
406 static int read_timeout(int fd, char *buf, size_t len)
410 io_flush(NORMAL_FLUSH);
413 /* until we manage to read *something* */
422 if (msg_fd_in >= 0) {
423 FD_SET(msg_fd_in, &r_fds);
424 if (msg_fd_in > maxfd)
426 } else if (msg_list_head) {
427 FD_SET(msg_fd_out, &w_fds);
428 if (msg_fd_out > maxfd)
431 if (io_filesfrom_f_out >= 0) {
433 if (io_filesfrom_buflen == 0) {
434 if (io_filesfrom_f_in >= 0) {
435 FD_SET(io_filesfrom_f_in, &r_fds);
436 new_fd = io_filesfrom_f_in;
438 io_filesfrom_f_out = -1;
442 FD_SET(io_filesfrom_f_out, &w_fds);
443 new_fd = io_filesfrom_f_out;
449 tv.tv_sec = select_timeout;
454 count = select(maxfd + 1, &r_fds, &w_fds, NULL, &tv);
458 exit_cleanup(RERR_SOCKETIO);
463 if (msg_fd_in >= 0 && FD_ISSET(msg_fd_in, &r_fds))
465 else if (msg_list_head && FD_ISSET(msg_fd_out, &w_fds))
466 msg_list_push(NORMAL_FLUSH);
468 if (io_filesfrom_f_out >= 0) {
469 if (io_filesfrom_buflen) {
470 if (FD_ISSET(io_filesfrom_f_out, &w_fds)) {
471 int l = write(io_filesfrom_f_out,
473 io_filesfrom_buflen);
475 if (!(io_filesfrom_buflen -= l))
476 io_filesfrom_bp = io_filesfrom_buf;
478 io_filesfrom_bp += l;
480 /* XXX should we complain? */
481 io_filesfrom_f_out = -1;
484 } else if (io_filesfrom_f_in >= 0) {
485 if (FD_ISSET(io_filesfrom_f_in, &r_fds)) {
486 int l = read(io_filesfrom_f_in,
488 sizeof io_filesfrom_buf);
490 /* Send end-of-file marker */
491 io_filesfrom_buf[0] = '\0';
492 io_filesfrom_buf[1] = '\0';
493 io_filesfrom_buflen = io_filesfrom_lastchar? 2 : 1;
494 io_filesfrom_f_in = -1;
497 char *s = io_filesfrom_buf + l;
498 /* Transform CR and/or LF into '\0' */
499 while (s-- > io_filesfrom_buf) {
500 if (*s == '\n' || *s == '\r')
504 if (!io_filesfrom_lastchar) {
505 /* Last buf ended with a '\0', so don't
506 * let this buf start with one. */
507 while (l && !*io_filesfrom_bp)
508 io_filesfrom_bp++, l--;
511 io_filesfrom_bp = io_filesfrom_buf;
513 char *f = io_filesfrom_bp;
516 /* Eliminate any multi-'\0' runs. */
518 if (!(*t++ = *f++)) {
519 while (f != eob && !*f)
523 io_filesfrom_lastchar = f[-1];
525 io_filesfrom_buflen = l;
531 if (!FD_ISSET(fd, &r_fds))
534 n = read(fd, buf, len);
538 whine_about_eof(fd); /* Doesn't return. */
539 if (errno == EINTR || errno == EWOULDBLOCK
543 /* Don't write errors on a dead socket. */
545 close_multiplexing_out();
546 rsyserr(FERROR, errno, "read error");
547 exit_cleanup(RERR_STREAMIO);
554 if (io_timeout && fd == sock_f_in)
555 last_io = time(NULL);
562 * Read a line into the "fname" buffer (which must be at least MAXPATHLEN
565 int read_filesfrom_line(int fd, char *fname)
567 char ch, *s, *eob = fname + MAXPATHLEN - 1;
569 int reading_remotely = filesfrom_host != NULL;
570 int nulls = eol_nulls || reading_remotely;
575 cnt = read(fd, &ch, 1);
576 if (cnt < 0 && (errno == EWOULDBLOCK
577 || errno == EINTR || errno == EAGAIN)) {
582 tv.tv_sec = select_timeout;
584 if (!select(fd+1, &fds, NULL, NULL, &tv))
590 if (nulls? !ch : (ch == '\r' || ch == '\n')) {
591 /* Skip empty lines if reading locally. */
592 if (!reading_remotely && s == fname)
602 if (*fname == '#' || *fname == ';')
609 static char *iobuf_out;
610 static int iobuf_out_cnt;
612 void io_start_buffering_out(void)
616 if (!(iobuf_out = new_array(char, IO_BUFFER_SIZE)))
617 out_of_memory("io_start_buffering_out");
622 static char *iobuf_in;
623 static size_t iobuf_in_siz;
625 void io_start_buffering_in(void)
629 iobuf_in_siz = 2 * IO_BUFFER_SIZE;
630 if (!(iobuf_in = new_array(char, iobuf_in_siz)))
631 out_of_memory("io_start_buffering_in");
635 void io_end_buffering(void)
637 io_flush(NORMAL_FLUSH);
638 if (!io_multiplexing_out) {
645 void maybe_send_keepalive(int allowed_lull, int ndx)
647 if (time(NULL) - last_io >= allowed_lull) {
648 if (!iobuf_out || !iobuf_out_cnt) {
649 if (protocol_version < 29)
650 return; /* there's nothing we can do */
651 write_int(sock_f_out, ndx);
652 write_shortint(sock_f_out, ITEM_IS_NEW);
655 io_flush(NORMAL_FLUSH);
661 * Continue trying to read len bytes - don't return until len has been
664 static void read_loop(int fd, char *buf, size_t len)
667 int n = read_timeout(fd, buf, len);
676 * Read from the file descriptor handling multiplexing - return number
679 * Never returns <= 0.
681 static int readfd_unbuffered(int fd, char *buf, size_t len)
683 static size_t remaining;
684 static size_t iobuf_in_ndx;
686 char line[MAXPATHLEN+1];
688 if (!iobuf_in || fd != sock_f_in)
689 return read_timeout(fd, buf, len);
691 if (!io_multiplexing_in && remaining == 0) {
692 remaining = read_timeout(fd, iobuf_in, iobuf_in_siz);
698 len = MIN(len, remaining);
699 memcpy(buf, iobuf_in + iobuf_in_ndx, len);
706 read_loop(fd, line, 4);
709 remaining = tag & 0xFFFFFF;
710 tag = (tag >> 24) - MPLEX_BASE;
714 if (remaining > iobuf_in_siz) {
715 if (!(iobuf_in = realloc_array(iobuf_in, char,
717 out_of_memory("readfd_unbuffered");
718 iobuf_in_siz = remaining;
720 read_loop(fd, iobuf_in, remaining);
724 if (remaining >= sizeof line) {
725 rprintf(FERROR, "invalid multi-message %d:%ld\n",
726 tag, (long)remaining);
727 exit_cleanup(RERR_STREAMIO);
729 read_loop(fd, line, remaining);
730 line[remaining] = '\0';
731 /* A directory name was sent with the trailing null */
732 if (remaining > 0 && !line[remaining-1])
733 log_delete(line, S_IFDIR);
735 log_delete(line, S_IFREG);
739 if (remaining != 4) {
740 rprintf(FERROR, "invalid multi-message %d:%ld\n",
741 tag, (long)remaining);
742 exit_cleanup(RERR_STREAMIO);
744 read_loop(fd, line, remaining);
745 successful_send(IVAL(line, 0));
750 if (remaining >= sizeof line) {
752 "[%s] multiplexing overflow %d:%ld\n\n",
753 who_am_i(), tag, (long)remaining);
754 exit_cleanup(RERR_STREAMIO);
756 read_loop(fd, line, remaining);
757 rwrite((enum logcode)tag, line, remaining);
761 rprintf(FERROR, "[%s] unexpected tag %d\n",
763 exit_cleanup(RERR_STREAMIO);
768 io_flush(NORMAL_FLUSH);
776 * Do a buffered read from @p fd. Don't return until all @p n bytes
777 * have been read. If all @p n can't be read then exit with an
780 static void readfd(int fd, char *buffer, size_t N)
786 ret = readfd_unbuffered(fd, buffer + total, N-total);
790 if (fd == write_batch_monitor_in) {
791 if ((size_t)write(batch_fd, buffer, total) != total)
792 exit_cleanup(RERR_FILEIO);
796 stats.total_read += total;
800 int read_shortint(int f)
803 readfd(f, (char *)b, 2);
804 return (b[1] << 8) + b[0];
808 int32 read_int(int f)
815 if (ret == (int32)0xffffffff)
820 int64 read_longint(int f)
826 if ((int32)ret != (int32)0xffffffff)
830 rprintf(FERROR, "Integer overflow: attempted 64-bit offset\n");
831 exit_cleanup(RERR_UNSUPPORTED);
834 ret = IVAL(b,0) | (((int64)IVAL(b,4))<<32);
840 void read_buf(int f,char *buf,size_t len)
845 void read_sbuf(int f,char *buf,size_t len)
851 uchar read_byte(int f)
854 readfd(f, (char *)&c, 1);
858 /* Populate a sum_struct with values from the socket. This is
859 * called by both the sender and the receiver. */
860 void read_sum_head(int f, struct sum_struct *sum)
862 sum->count = read_int(f);
863 sum->blength = read_int(f);
864 if (sum->blength < 0 || sum->blength > MAX_BLOCK_SIZE) {
865 rprintf(FERROR, "[%s] Invalid block length %ld\n",
866 who_am_i(), (long)sum->blength);
867 exit_cleanup(RERR_PROTOCOL);
869 sum->s2length = protocol_version < 27 ? csum_length : (int)read_int(f);
870 if (sum->s2length < 0 || sum->s2length > MD4_SUM_LENGTH) {
871 rprintf(FERROR, "[%s] Invalid checksum length %d\n",
872 who_am_i(), sum->s2length);
873 exit_cleanup(RERR_PROTOCOL);
875 sum->remainder = read_int(f);
876 if (sum->remainder < 0 || sum->remainder > sum->blength) {
877 rprintf(FERROR, "[%s] Invalid remainder length %ld\n",
878 who_am_i(), (long)sum->remainder);
879 exit_cleanup(RERR_PROTOCOL);
883 /* Send the values from a sum_struct over the socket. Set sum to
884 * NULL if there are no checksums to send. This is called by both
885 * the generator and the sender. */
886 void write_sum_head(int f, struct sum_struct *sum)
888 static struct sum_struct null_sum;
893 write_int(f, sum->count);
894 write_int(f, sum->blength);
895 if (protocol_version >= 27)
896 write_int(f, sum->s2length);
897 write_int(f, sum->remainder);
902 * Sleep after writing to limit I/O bandwidth usage.
904 * @todo Rather than sleeping after each write, it might be better to
905 * use some kind of averaging. The current algorithm seems to always
906 * use a bit less bandwidth than specified, because it doesn't make up
907 * for slow periods. But arguably this is a feature. In addition, we
908 * ought to take the time used to write the data into account.
910 * During some phases of big transfers (file FOO is uptodate) this is
911 * called with a small bytes_written every time. As the kernel has to
912 * round small waits up to guarantee that we actually wait at least the
913 * requested number of microseconds, this can become grossly inaccurate.
914 * We therefore keep track of the bytes we've written over time and only
915 * sleep when the accumulated delay is at least 1 tenth of a second.
917 static void sleep_for_bwlimit(int bytes_written)
919 static struct timeval prior_tv;
920 static long total_written = 0;
921 struct timeval tv, start_tv;
922 long elapsed_usec, sleep_usec;
924 #define ONE_SEC 1000000L /* # of microseconds in a second */
929 total_written += bytes_written;
931 gettimeofday(&start_tv, NULL);
932 if (prior_tv.tv_sec) {
933 elapsed_usec = (start_tv.tv_sec - prior_tv.tv_sec) * ONE_SEC
934 + (start_tv.tv_usec - prior_tv.tv_usec);
935 total_written -= elapsed_usec * bwlimit / (ONE_SEC/1024);
936 if (total_written < 0)
940 sleep_usec = total_written * (ONE_SEC/1024) / bwlimit;
941 if (sleep_usec < ONE_SEC / 10) {
946 tv.tv_sec = sleep_usec / ONE_SEC;
947 tv.tv_usec = sleep_usec % ONE_SEC;
948 select(0, NULL, NULL, NULL, &tv);
950 gettimeofday(&prior_tv, NULL);
951 elapsed_usec = (prior_tv.tv_sec - start_tv.tv_sec) * ONE_SEC
952 + (prior_tv.tv_usec - start_tv.tv_usec);
953 total_written = (sleep_usec - elapsed_usec) * bwlimit / (ONE_SEC/1024);
957 /* Write len bytes to the file descriptor fd, looping as necessary to get
958 * the job done and also (in the generator) reading any data on msg_fd_in
959 * (to avoid deadlock).
961 * This function underlies the multiplexing system. The body of the
962 * application never calls this function directly. */
963 static void writefd_unbuffered(int fd,char *buf,size_t len)
967 int maxfd, count, ret;
972 while (total < len) {
977 if (msg_fd_in >= 0) {
979 FD_SET(msg_fd_in,&r_fds);
980 if (msg_fd_in > maxfd)
983 if (fd != sock_f_out && iobuf_out_cnt && no_flush == 1) {
984 FD_SET(sock_f_out, &w_fds);
985 if (sock_f_out > maxfd)
989 tv.tv_sec = select_timeout;
993 count = select(maxfd + 1, msg_fd_in >= 0 ? &r_fds : NULL,
997 if (count < 0 && errno == EBADF)
998 exit_cleanup(RERR_SOCKETIO);
1003 if (msg_fd_in >= 0 && FD_ISSET(msg_fd_in, &r_fds))
1006 if (!FD_ISSET(fd, &w_fds)) {
1007 if (fd != sock_f_out && iobuf_out_cnt) {
1009 io_flush(NORMAL_FLUSH);
1016 if (bwlimit && n > bwlimit_writemax)
1017 n = bwlimit_writemax;
1018 ret = write(fd, buf + total, n);
1024 if (errno == EWOULDBLOCK || errno == EAGAIN) {
1030 /* Don't try to write errors back across the stream. */
1031 if (fd == sock_f_out)
1032 close_multiplexing_out();
1033 rsyserr(FERROR, errno,
1034 "writefd_unbuffered failed to write %ld bytes: phase \"%s\" [%s]",
1035 (long)len, io_write_phase, who_am_i());
1036 /* If the other side is sending us error messages, try
1037 * to grab any messages they sent before they died. */
1038 while (fd == sock_f_out && io_multiplexing_in) {
1039 io_timeout = select_timeout = 30;
1041 readfd_unbuffered(sock_f_in, io_filesfrom_buf,
1042 sizeof io_filesfrom_buf);
1044 exit_cleanup(RERR_STREAMIO);
1049 if (fd == sock_f_out) {
1051 last_io = time(NULL);
1052 sleep_for_bwlimit(ret);
1061 * Write an message to a multiplexed stream. If this fails then rsync
1064 static void mplex_write(enum msgcode code, char *buf, size_t len)
1069 SIVAL(buffer, 0, ((MPLEX_BASE + (int)code)<<24) + len);
1071 if (n > sizeof buffer - 4)
1072 n = sizeof buffer - 4;
1074 memcpy(&buffer[4], buf, n);
1075 writefd_unbuffered(sock_f_out, buffer, n+4);
1081 writefd_unbuffered(sock_f_out, buf, len);
1085 void io_flush(int flush_it_all)
1087 msg_list_push(flush_it_all);
1089 if (!iobuf_out_cnt || no_flush)
1092 if (io_multiplexing_out)
1093 mplex_write(MSG_DATA, iobuf_out, iobuf_out_cnt);
1095 writefd_unbuffered(sock_f_out, iobuf_out, iobuf_out_cnt);
1100 static void writefd(int fd,char *buf,size_t len)
1102 if (fd == msg_fd_out) {
1103 rprintf(FERROR, "Internal error: wrong write used in receiver.\n");
1104 exit_cleanup(RERR_PROTOCOL);
1107 if (fd == sock_f_out)
1108 stats.total_written += len;
1110 if (fd == write_batch_monitor_out) {
1111 if ((size_t)write(batch_fd, buf, len) != len)
1112 exit_cleanup(RERR_FILEIO);
1115 if (!iobuf_out || fd != sock_f_out) {
1116 writefd_unbuffered(fd, buf, len);
1121 int n = MIN((int)len, IO_BUFFER_SIZE - iobuf_out_cnt);
1123 memcpy(iobuf_out+iobuf_out_cnt, buf, n);
1129 if (iobuf_out_cnt == IO_BUFFER_SIZE)
1130 io_flush(NORMAL_FLUSH);
1135 void write_shortint(int f, int x)
1140 writefd(f, (char *)b, 2);
1144 void write_int(int f,int32 x)
1152 void write_int_named(int f, int32 x, const char *phase)
1154 io_write_phase = phase;
1156 io_write_phase = phase_unknown;
1161 * Note: int64 may actually be a 32-bit type if ./configure couldn't find any
1162 * 64-bit types on this platform.
1164 void write_longint(int f, int64 x)
1168 if (x <= 0x7FFFFFFF) {
1169 write_int(f, (int)x);
1173 #if SIZEOF_INT64 < 8
1174 rprintf(FERROR, "Integer overflow: attempted 64-bit offset\n");
1175 exit_cleanup(RERR_UNSUPPORTED);
1177 write_int(f, (int32)0xFFFFFFFF);
1178 SIVAL(b,0,(x&0xFFFFFFFF));
1179 SIVAL(b,4,((x>>32)&0xFFFFFFFF));
1185 void write_buf(int f,char *buf,size_t len)
1191 /** Write a string to the connection */
1192 void write_sbuf(int f, char *buf)
1194 writefd(f, buf, strlen(buf));
1198 void write_byte(int f, uchar c)
1200 writefd(f, (char *)&c, 1);
1205 * Read a line of up to @p maxlen characters into @p buf (not counting
1206 * the trailing null). Strips the (required) trailing newline and all
1209 * @return 1 for success; 0 for I/O error or truncation.
1211 int read_line(int f, char *buf, size_t maxlen)
1215 read_buf(f, buf, 1);
1220 if (buf[0] != '\r') {
1230 void io_printf(int fd, const char *format, ...)
1236 va_start(ap, format);
1237 len = vsnprintf(buf, sizeof buf, format, ap);
1241 exit_cleanup(RERR_STREAMIO);
1243 write_sbuf(fd, buf);
1247 /** Setup for multiplexing a MSG_* stream with the data stream. */
1248 void io_start_multiplex_out(void)
1250 io_flush(NORMAL_FLUSH);
1251 io_start_buffering_out();
1252 io_multiplexing_out = 1;
1255 /** Setup for multiplexing a MSG_* stream with the data stream. */
1256 void io_start_multiplex_in(void)
1258 io_flush(NORMAL_FLUSH);
1259 io_start_buffering_in();
1260 io_multiplexing_in = 1;
1263 /** Write an message to the multiplexed data stream. */
1264 int io_multiplex_write(enum msgcode code, char *buf, size_t len)
1266 if (!io_multiplexing_out)
1269 io_flush(NORMAL_FLUSH);
1270 stats.total_written += (len+4);
1271 mplex_write(code, buf, len);
1275 void close_multiplexing_in(void)
1277 io_multiplexing_in = 0;
1280 /** Stop output multiplexing. */
1281 void close_multiplexing_out(void)
1283 io_multiplexing_out = 0;
1286 void start_write_batch(int fd)
1288 write_stream_flags(batch_fd);
1290 /* Some communication has already taken place, but we don't
1291 * enable batch writing until here so that we can write a
1292 * canonical record of the communication even though the
1293 * actual communication so far depends on whether a daemon
1295 write_int(batch_fd, protocol_version);
1296 write_int(batch_fd, checksum_seed);
1299 write_batch_monitor_out = fd;
1301 write_batch_monitor_in = fd;
1304 void stop_write_batch(void)
1306 write_batch_monitor_out = -1;
1307 write_batch_monitor_in = -1;