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 *remote_filesfrom_file;
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 EOF errors while reading a module listing if the remote
80 * version is 24 or less. Also used by the receiver when it is
81 * reading a potential end-of-transfer keep-alive message that
83 int kludge_around_eof = False;
88 static int io_multiplexing_out;
89 static int io_multiplexing_in;
90 static int sock_f_in = -1;
91 static int sock_f_out = -1;
92 static time_t last_io;
95 static int write_batch_monitor_in = -1;
96 static int write_batch_monitor_out = -1;
98 static int io_filesfrom_f_in = -1;
99 static int io_filesfrom_f_out = -1;
100 static char io_filesfrom_buf[2048];
101 static char *io_filesfrom_bp;
102 static char io_filesfrom_lastchar;
103 static int io_filesfrom_buflen;
105 static void read_loop(int fd, char *buf, size_t len);
108 struct redo_list *next;
112 static struct redo_list *redo_list_head;
113 static struct redo_list *redo_list_tail;
116 struct msg_list *next;
121 static struct msg_list *msg_list_head;
122 static struct msg_list *msg_list_tail;
124 static void redo_list_add(int num)
126 struct redo_list *rl;
128 if (!(rl = new(struct redo_list)))
129 exit_cleanup(RERR_MALLOC);
133 redo_list_tail->next = rl;
139 static void check_timeout(void)
143 if (!io_timeout || ignore_timeout)
147 last_io = time(NULL);
153 if (t - last_io >= io_timeout) {
154 if (!am_server && !am_daemon) {
155 rprintf(FERROR, "io timeout after %d seconds -- exiting\n",
158 exit_cleanup(RERR_TIMEOUT);
162 /* Note the fds used for the main socket (which might really be a pipe
163 * for a local transfer, but we can ignore that). */
164 void io_set_sock_fds(int f_in, int f_out)
170 /* Setup the fd used to receive MSG_* messages. Only needed during the
171 * early stages of being a local sender (up through the sending of the
172 * file list) or when we're the generator (to fetch the messages from
174 void set_msg_fd_in(int fd)
179 /* Setup the fd used to send our MSG_* messages. Only needed when
180 * we're the receiver (to send our messages to the generator). */
181 void set_msg_fd_out(int fd)
184 set_nonblocking(msg_fd_out);
187 /* Add a message to the pending MSG_* list. */
188 static void msg_list_add(int code, char *buf, int len)
192 if (!(ml = new(struct msg_list)))
193 exit_cleanup(RERR_MALLOC);
195 if (!(ml->buf = new_array(char, len+4)))
196 exit_cleanup(RERR_MALLOC);
197 SIVAL(ml->buf, 0, ((code+MPLEX_BASE)<<24) | len);
198 memcpy(ml->buf+4, buf, len);
201 msg_list_tail->next = ml;
207 void send_msg(enum msgcode code, char *buf, int len)
209 if (msg_fd_out < 0) {
210 io_multiplex_write(code, buf, len);
213 msg_list_add(code, buf, len);
214 msg_list_push(NORMAL_FLUSH);
217 /* Read a message from the MSG_* fd and handle it. This is called either
218 * during the early stages of being a local sender (up through the sending
219 * of the file list) or when we're the generator (to fetch the messages
220 * from the receiver). */
221 static void read_msg_fd(void)
228 /* Temporarily disable msg_fd_in. This is needed to avoid looping back
229 * to this routine from read_timeout() and writefd_unbuffered(). */
232 read_loop(fd, buf, 4);
235 len = tag & 0xFFFFFF;
236 tag = (tag >> 24) - MPLEX_BASE;
240 if (len != 0 || !am_generator) {
241 rprintf(FERROR, "invalid message %d:%d\n", tag, len);
242 exit_cleanup(RERR_STREAMIO);
247 if (len != 4 || !am_generator) {
248 rprintf(FERROR, "invalid message %d:%d\n", tag, len);
249 exit_cleanup(RERR_STREAMIO);
251 read_loop(fd, buf, 4);
252 redo_list_add(IVAL(buf,0));
255 if (len >= (int)sizeof buf || !am_generator) {
256 rprintf(FERROR, "invalid message %d:%d\n", tag, len);
257 exit_cleanup(RERR_STREAMIO);
259 read_loop(fd, buf, len);
260 io_multiplex_write(MSG_DELETED, buf, len);
263 if (len != 4 || !am_generator) {
264 rprintf(FERROR, "invalid message %d:%d\n", tag, len);
265 exit_cleanup(RERR_STREAMIO);
267 read_loop(fd, buf, len);
268 io_multiplex_write(MSG_SUCCESS, buf, len);
277 read_loop(fd, buf, n);
278 rwrite((enum logcode)tag, buf, n);
283 rprintf(FERROR, "unknown message %d:%d\n", tag, len);
284 exit_cleanup(RERR_STREAMIO);
290 /* Try to push messages off the list onto the wire. If we leave with more
291 * to do, return 0. On error, return -1. If everything flushed, return 1.
292 * This is only active in the receiver. */
293 int msg_list_push(int flush_it_all)
295 static int written = 0;
302 while (msg_list_head) {
303 struct msg_list *ml = msg_list_head;
304 int n = write(msg_fd_out, ml->buf + written, ml->len - written);
308 if (errno != EWOULDBLOCK && errno != EAGAIN)
313 FD_SET(msg_fd_out, &fds);
314 tv.tv_sec = select_timeout;
316 if (!select(msg_fd_out+1, NULL, &fds, NULL, &tv))
318 } else if ((written += n) == ml->len) {
320 msg_list_head = ml->next;
322 msg_list_tail = NULL;
330 int get_redo_num(void)
332 struct redo_list *next;
335 while (!redo_list_head)
338 num = redo_list_head->num;
339 next = redo_list_head->next;
340 free(redo_list_head);
341 redo_list_head = next;
343 redo_list_tail = NULL;
349 * When we're the receiver and we have a local --files-from list of names
350 * that needs to be sent over the socket to the sender, we have to do two
351 * things at the same time: send the sender a list of what files we're
352 * processing and read the incoming file+info list from the sender. We do
353 * this by augmenting the read_timeout() function to copy this data. It
354 * uses the io_filesfrom_buf to read a block of data from f_in (when it is
355 * ready, since it might be a pipe) and then blast it out f_out (when it
356 * is ready to receive more data).
358 void io_set_filesfrom_fds(int f_in, int f_out)
360 io_filesfrom_f_in = f_in;
361 io_filesfrom_f_out = f_out;
362 io_filesfrom_bp = io_filesfrom_buf;
363 io_filesfrom_lastchar = '\0';
364 io_filesfrom_buflen = 0;
368 * It's almost always an error to get an EOF when we're trying to read
369 * from the network, because the protocol is self-terminating.
371 * However, there is one unfortunate cases where it is not, which is
372 * rsync <2.4.6 sending a list of modules on a server, since the list
373 * is terminated by closing the socket. So, for the section of the
374 * program where that is a problem (start_socket_client),
375 * kludge_around_eof is True and we just exit.
377 static void whine_about_eof(int fd)
379 if (kludge_around_eof && fd == sock_f_in)
382 rprintf(FERROR, RSYNC_NAME ": connection unexpectedly closed "
383 "(%.0f bytes received so far) [%s]\n",
384 (double)stats.total_read, who_am_i());
386 exit_cleanup(RERR_STREAMIO);
391 * Read from a socket with I/O timeout. return the number of bytes
392 * read. If no bytes can be read then exit, never return a number <= 0.
394 * TODO: If the remote shell connection fails, then current versions
395 * actually report an "unexpected EOF" error here. Since it's a
396 * fairly common mistake to try to use rsh when ssh is required, we
397 * should trap that: if we fail to read any data at all, we should
398 * give a better explanation. We can tell whether the connection has
399 * started by looking e.g. at whether the remote version is known yet.
401 static int read_timeout(int fd, char *buf, size_t len)
405 io_flush(NORMAL_FLUSH);
408 /* until we manage to read *something* */
417 if (msg_fd_in >= 0) {
418 FD_SET(msg_fd_in, &r_fds);
419 if (msg_fd_in > maxfd)
421 } else if (msg_list_head) {
422 FD_SET(msg_fd_out, &w_fds);
423 if (msg_fd_out > maxfd)
426 if (io_filesfrom_f_out >= 0) {
428 if (io_filesfrom_buflen == 0) {
429 if (io_filesfrom_f_in >= 0) {
430 FD_SET(io_filesfrom_f_in, &r_fds);
431 new_fd = io_filesfrom_f_in;
433 io_filesfrom_f_out = -1;
437 FD_SET(io_filesfrom_f_out, &w_fds);
438 new_fd = io_filesfrom_f_out;
444 tv.tv_sec = select_timeout;
449 count = select(maxfd + 1, &r_fds, &w_fds, NULL, &tv);
453 exit_cleanup(RERR_SOCKETIO);
458 if (msg_fd_in >= 0 && FD_ISSET(msg_fd_in, &r_fds))
460 else if (msg_list_head && FD_ISSET(msg_fd_out, &w_fds))
461 msg_list_push(NORMAL_FLUSH);
463 if (io_filesfrom_f_out >= 0) {
464 if (io_filesfrom_buflen) {
465 if (FD_ISSET(io_filesfrom_f_out, &w_fds)) {
466 int l = write(io_filesfrom_f_out,
468 io_filesfrom_buflen);
470 if (!(io_filesfrom_buflen -= l))
471 io_filesfrom_bp = io_filesfrom_buf;
473 io_filesfrom_bp += l;
475 /* XXX should we complain? */
476 io_filesfrom_f_out = -1;
479 } else if (io_filesfrom_f_in >= 0) {
480 if (FD_ISSET(io_filesfrom_f_in, &r_fds)) {
481 int l = read(io_filesfrom_f_in,
483 sizeof io_filesfrom_buf);
485 /* Send end-of-file marker */
486 io_filesfrom_buf[0] = '\0';
487 io_filesfrom_buf[1] = '\0';
488 io_filesfrom_buflen = io_filesfrom_lastchar? 2 : 1;
489 io_filesfrom_f_in = -1;
492 char *s = io_filesfrom_buf + l;
493 /* Transform CR and/or LF into '\0' */
494 while (s-- > io_filesfrom_buf) {
495 if (*s == '\n' || *s == '\r')
499 if (!io_filesfrom_lastchar) {
500 /* Last buf ended with a '\0', so don't
501 * let this buf start with one. */
502 while (l && !*io_filesfrom_bp)
503 io_filesfrom_bp++, l--;
506 io_filesfrom_bp = io_filesfrom_buf;
508 char *f = io_filesfrom_bp;
511 /* Eliminate any multi-'\0' runs. */
513 if (!(*t++ = *f++)) {
514 while (f != eob && !*f)
518 io_filesfrom_lastchar = f[-1];
520 io_filesfrom_buflen = l;
526 if (!FD_ISSET(fd, &r_fds))
529 n = read(fd, buf, len);
533 whine_about_eof(fd); /* Doesn't return. */
534 if (errno == EINTR || errno == EWOULDBLOCK
538 /* Don't write errors on a dead socket. */
540 close_multiplexing_out();
541 rsyserr(FERROR, errno, "read error");
542 exit_cleanup(RERR_STREAMIO);
549 if (io_timeout && fd == sock_f_in)
550 last_io = time(NULL);
557 * Read a line into the "fname" buffer (which must be at least MAXPATHLEN
560 int read_filesfrom_line(int fd, char *fname)
562 char ch, *s, *eob = fname + MAXPATHLEN - 1;
564 int reading_remotely = remote_filesfrom_file != NULL;
565 int nulls = eol_nulls || reading_remotely;
570 cnt = read(fd, &ch, 1);
571 if (cnt < 0 && (errno == EWOULDBLOCK
572 || errno == EINTR || errno == EAGAIN)) {
577 tv.tv_sec = select_timeout;
579 if (!select(fd+1, &fds, NULL, NULL, &tv))
585 if (nulls? !ch : (ch == '\r' || ch == '\n')) {
586 /* Skip empty lines if reading locally. */
587 if (!reading_remotely && s == fname)
597 if (*fname == '#' || *fname == ';')
604 static char *iobuf_out;
605 static int iobuf_out_cnt;
607 void io_start_buffering_out(void)
611 if (!(iobuf_out = new_array(char, IO_BUFFER_SIZE)))
612 out_of_memory("io_start_buffering_out");
617 static char *iobuf_in;
618 static size_t iobuf_in_siz;
620 void io_start_buffering_in(void)
624 iobuf_in_siz = 2 * IO_BUFFER_SIZE;
625 if (!(iobuf_in = new_array(char, iobuf_in_siz)))
626 out_of_memory("io_start_buffering_in");
630 void io_end_buffering(void)
632 io_flush(NORMAL_FLUSH);
633 if (!io_multiplexing_out) {
640 void maybe_send_keepalive(int allowed_lull, int ndx)
642 if (time(NULL) - last_io >= allowed_lull) {
643 if (!iobuf_out || !iobuf_out_cnt) {
644 if (protocol_version < 29)
645 return; /* there's nothing we can do */
646 write_int(sock_f_out, ndx);
647 write_shortint(sock_f_out, ITEM_IS_NEW);
650 io_flush(NORMAL_FLUSH);
656 * Continue trying to read len bytes - don't return until len has been
659 static void read_loop(int fd, char *buf, size_t len)
662 int n = read_timeout(fd, buf, len);
671 * Read from the file descriptor handling multiplexing - return number
674 * Never returns <= 0.
676 static int readfd_unbuffered(int fd, char *buf, size_t len)
678 static size_t remaining;
679 static size_t iobuf_in_ndx;
681 char line[MAXPATHLEN+1];
683 if (!iobuf_in || fd != sock_f_in)
684 return read_timeout(fd, buf, len);
686 if (!io_multiplexing_in && remaining == 0) {
687 remaining = read_timeout(fd, iobuf_in, iobuf_in_siz);
693 len = MIN(len, remaining);
694 memcpy(buf, iobuf_in + iobuf_in_ndx, len);
701 read_loop(fd, line, 4);
704 remaining = tag & 0xFFFFFF;
705 tag = (tag >> 24) - MPLEX_BASE;
709 if (remaining > iobuf_in_siz) {
710 if (!(iobuf_in = realloc_array(iobuf_in, char,
712 out_of_memory("readfd_unbuffered");
713 iobuf_in_siz = remaining;
715 read_loop(fd, iobuf_in, remaining);
719 if (remaining >= sizeof line) {
720 rprintf(FERROR, "invalid multi-message %d:%ld\n",
721 tag, (long)remaining);
722 exit_cleanup(RERR_STREAMIO);
724 read_loop(fd, line, remaining);
725 line[remaining] = '\0';
726 /* A directory name was sent with the trailing null */
727 if (remaining > 0 && !line[remaining-1])
728 log_delete(line, S_IFDIR);
730 log_delete(line, S_IFREG);
734 if (remaining != 4) {
735 rprintf(FERROR, "invalid multi-message %d:%ld\n",
736 tag, (long)remaining);
737 exit_cleanup(RERR_STREAMIO);
739 read_loop(fd, line, remaining);
740 successful_send(IVAL(line, 0));
745 if (remaining >= sizeof line) {
747 "[%s] multiplexing overflow %d:%ld\n\n",
748 who_am_i(), tag, (long)remaining);
749 exit_cleanup(RERR_STREAMIO);
751 read_loop(fd, line, remaining);
752 rwrite((enum logcode)tag, line, remaining);
756 rprintf(FERROR, "[%s] unexpected tag %d\n",
758 exit_cleanup(RERR_STREAMIO);
763 io_flush(NORMAL_FLUSH);
771 * Do a buffered read from @p fd. Don't return until all @p n bytes
772 * have been read. If all @p n can't be read then exit with an
775 static void readfd(int fd, char *buffer, size_t N)
781 ret = readfd_unbuffered(fd, buffer + total, N-total);
785 if (fd == write_batch_monitor_in) {
786 if ((size_t)write(batch_fd, buffer, total) != total)
787 exit_cleanup(RERR_FILEIO);
791 stats.total_read += total;
795 int read_shortint(int f)
798 readfd(f, (char *)b, 2);
799 return (b[1] << 8) + b[0];
803 int32 read_int(int f)
810 if (ret == (int32)0xffffffff)
815 int64 read_longint(int f)
821 if ((int32)ret != (int32)0xffffffff)
825 rprintf(FERROR, "Integer overflow: attempted 64-bit offset\n");
826 exit_cleanup(RERR_UNSUPPORTED);
829 ret = IVAL(b,0) | (((int64)IVAL(b,4))<<32);
835 void read_buf(int f,char *buf,size_t len)
840 void read_sbuf(int f,char *buf,size_t len)
846 uchar read_byte(int f)
849 readfd(f, (char *)&c, 1);
853 /* Populate a sum_struct with values from the socket. This is
854 * called by both the sender and the receiver. */
855 void read_sum_head(int f, struct sum_struct *sum)
857 sum->count = read_int(f);
858 sum->blength = read_int(f);
859 if (sum->blength < 0 || sum->blength > MAX_BLOCK_SIZE) {
860 rprintf(FERROR, "[%s] Invalid block length %ld\n",
861 who_am_i(), (long)sum->blength);
862 exit_cleanup(RERR_PROTOCOL);
864 sum->s2length = protocol_version < 27 ? csum_length : (int)read_int(f);
865 if (sum->s2length < 0 || sum->s2length > MD4_SUM_LENGTH) {
866 rprintf(FERROR, "[%s] Invalid checksum length %d\n",
867 who_am_i(), sum->s2length);
868 exit_cleanup(RERR_PROTOCOL);
870 sum->remainder = read_int(f);
871 if (sum->remainder < 0 || sum->remainder > sum->blength) {
872 rprintf(FERROR, "[%s] Invalid remainder length %ld\n",
873 who_am_i(), (long)sum->remainder);
874 exit_cleanup(RERR_PROTOCOL);
878 /* Send the values from a sum_struct over the socket. Set sum to
879 * NULL if there are no checksums to send. This is called by both
880 * the generator and the sender. */
881 void write_sum_head(int f, struct sum_struct *sum)
883 static struct sum_struct null_sum;
888 write_int(f, sum->count);
889 write_int(f, sum->blength);
890 if (protocol_version >= 27)
891 write_int(f, sum->s2length);
892 write_int(f, sum->remainder);
897 * Sleep after writing to limit I/O bandwidth usage.
899 * @todo Rather than sleeping after each write, it might be better to
900 * use some kind of averaging. The current algorithm seems to always
901 * use a bit less bandwidth than specified, because it doesn't make up
902 * for slow periods. But arguably this is a feature. In addition, we
903 * ought to take the time used to write the data into account.
905 * During some phases of big transfers (file FOO is uptodate) this is
906 * called with a small bytes_written every time. As the kernel has to
907 * round small waits up to guarantee that we actually wait at least the
908 * requested number of microseconds, this can become grossly inaccurate.
909 * We therefore keep track of the bytes we've written over time and only
910 * sleep when the accumulated delay is at least 1 tenth of a second.
912 static void sleep_for_bwlimit(int bytes_written)
914 static struct timeval prior_tv;
915 static long total_written = 0;
916 struct timeval tv, start_tv;
917 long elapsed_usec, sleep_usec;
919 #define ONE_SEC 1000000L /* # of microseconds in a second */
924 total_written += bytes_written;
926 gettimeofday(&start_tv, NULL);
927 if (prior_tv.tv_sec) {
928 elapsed_usec = (start_tv.tv_sec - prior_tv.tv_sec) * ONE_SEC
929 + (start_tv.tv_usec - prior_tv.tv_usec);
930 total_written -= elapsed_usec * bwlimit / (ONE_SEC/1024);
931 if (total_written < 0)
935 sleep_usec = total_written * (ONE_SEC/1024) / bwlimit;
936 if (sleep_usec < ONE_SEC / 10) {
941 tv.tv_sec = sleep_usec / ONE_SEC;
942 tv.tv_usec = sleep_usec % ONE_SEC;
943 select(0, NULL, NULL, NULL, &tv);
945 gettimeofday(&prior_tv, NULL);
946 elapsed_usec = (prior_tv.tv_sec - start_tv.tv_sec) * ONE_SEC
947 + (prior_tv.tv_usec - start_tv.tv_usec);
948 total_written = (sleep_usec - elapsed_usec) * bwlimit / (ONE_SEC/1024);
952 /* Write len bytes to the file descriptor fd, looping as necessary to get
953 * the job done and also (in the generator) reading any data on msg_fd_in
954 * (to avoid deadlock).
956 * This function underlies the multiplexing system. The body of the
957 * application never calls this function directly. */
958 static void writefd_unbuffered(int fd,char *buf,size_t len)
962 int maxfd, count, ret;
967 while (total < len) {
972 if (msg_fd_in >= 0) {
974 FD_SET(msg_fd_in,&r_fds);
975 if (msg_fd_in > maxfd)
978 if (fd != sock_f_out && iobuf_out_cnt && no_flush == 1) {
979 FD_SET(sock_f_out, &w_fds);
980 if (sock_f_out > maxfd)
984 tv.tv_sec = select_timeout;
988 count = select(maxfd + 1, msg_fd_in >= 0 ? &r_fds : NULL,
992 if (count < 0 && errno == EBADF)
993 exit_cleanup(RERR_SOCKETIO);
998 if (msg_fd_in >= 0 && FD_ISSET(msg_fd_in, &r_fds))
1001 if (!FD_ISSET(fd, &w_fds)) {
1002 if (fd != sock_f_out && iobuf_out_cnt) {
1004 io_flush(NORMAL_FLUSH);
1011 if (bwlimit && n > bwlimit_writemax)
1012 n = bwlimit_writemax;
1013 ret = write(fd, buf + total, n);
1019 if (errno == EWOULDBLOCK || errno == EAGAIN) {
1025 /* Don't try to write errors back across the stream. */
1026 if (fd == sock_f_out)
1027 close_multiplexing_out();
1028 rsyserr(FERROR, errno,
1029 "writefd_unbuffered failed to write %ld bytes: phase \"%s\" [%s]",
1030 (long)len, io_write_phase, who_am_i());
1031 /* If the other side is sending us error messages, try
1032 * to grab any messages they sent before they died. */
1033 while (fd == sock_f_out && io_multiplexing_in) {
1034 io_timeout = select_timeout = 30;
1036 readfd_unbuffered(sock_f_in, io_filesfrom_buf,
1037 sizeof io_filesfrom_buf);
1039 exit_cleanup(RERR_STREAMIO);
1044 if (fd == sock_f_out) {
1046 last_io = time(NULL);
1047 sleep_for_bwlimit(ret);
1056 * Write an message to a multiplexed stream. If this fails then rsync
1059 static void mplex_write(enum msgcode code, char *buf, size_t len)
1064 SIVAL(buffer, 0, ((MPLEX_BASE + (int)code)<<24) + len);
1066 if (n > sizeof buffer - 4)
1067 n = sizeof buffer - 4;
1069 memcpy(&buffer[4], buf, n);
1070 writefd_unbuffered(sock_f_out, buffer, n+4);
1076 writefd_unbuffered(sock_f_out, buf, len);
1080 void io_flush(int flush_it_all)
1082 msg_list_push(flush_it_all);
1084 if (!iobuf_out_cnt || no_flush)
1087 if (io_multiplexing_out)
1088 mplex_write(MSG_DATA, iobuf_out, iobuf_out_cnt);
1090 writefd_unbuffered(sock_f_out, iobuf_out, iobuf_out_cnt);
1095 static void writefd(int fd,char *buf,size_t len)
1097 if (fd == msg_fd_out) {
1098 rprintf(FERROR, "Internal error: wrong write used in receiver.\n");
1099 exit_cleanup(RERR_PROTOCOL);
1102 if (fd == sock_f_out)
1103 stats.total_written += len;
1105 if (fd == write_batch_monitor_out) {
1106 if ((size_t)write(batch_fd, buf, len) != len)
1107 exit_cleanup(RERR_FILEIO);
1110 if (!iobuf_out || fd != sock_f_out) {
1111 writefd_unbuffered(fd, buf, len);
1116 int n = MIN((int)len, IO_BUFFER_SIZE - iobuf_out_cnt);
1118 memcpy(iobuf_out+iobuf_out_cnt, buf, n);
1124 if (iobuf_out_cnt == IO_BUFFER_SIZE)
1125 io_flush(NORMAL_FLUSH);
1130 void write_shortint(int f, int x)
1135 writefd(f, (char *)b, 2);
1139 void write_int(int f,int32 x)
1147 void write_int_named(int f, int32 x, const char *phase)
1149 io_write_phase = phase;
1151 io_write_phase = phase_unknown;
1156 * Note: int64 may actually be a 32-bit type if ./configure couldn't find any
1157 * 64-bit types on this platform.
1159 void write_longint(int f, int64 x)
1163 if (x <= 0x7FFFFFFF) {
1164 write_int(f, (int)x);
1168 #if SIZEOF_INT64 < 8
1169 rprintf(FERROR, "Integer overflow: attempted 64-bit offset\n");
1170 exit_cleanup(RERR_UNSUPPORTED);
1172 write_int(f, (int32)0xFFFFFFFF);
1173 SIVAL(b,0,(x&0xFFFFFFFF));
1174 SIVAL(b,4,((x>>32)&0xFFFFFFFF));
1180 void write_buf(int f,char *buf,size_t len)
1186 /** Write a string to the connection */
1187 void write_sbuf(int f, char *buf)
1189 writefd(f, buf, strlen(buf));
1193 void write_byte(int f, uchar c)
1195 writefd(f, (char *)&c, 1);
1200 * Read a line of up to @p maxlen characters into @p buf (not counting
1201 * the trailing null). Strips the (required) trailing newline and all
1204 * @return 1 for success; 0 for I/O error or truncation.
1206 int read_line(int f, char *buf, size_t maxlen)
1210 read_buf(f, buf, 1);
1215 if (buf[0] != '\r') {
1225 void io_printf(int fd, const char *format, ...)
1231 va_start(ap, format);
1232 len = vsnprintf(buf, sizeof buf, format, ap);
1236 exit_cleanup(RERR_STREAMIO);
1238 write_sbuf(fd, buf);
1242 /** Setup for multiplexing a MSG_* stream with the data stream. */
1243 void io_start_multiplex_out(void)
1245 io_flush(NORMAL_FLUSH);
1246 io_start_buffering_out();
1247 io_multiplexing_out = 1;
1250 /** Setup for multiplexing a MSG_* stream with the data stream. */
1251 void io_start_multiplex_in(void)
1253 io_flush(NORMAL_FLUSH);
1254 io_start_buffering_in();
1255 io_multiplexing_in = 1;
1258 /** Write an message to the multiplexed data stream. */
1259 int io_multiplex_write(enum msgcode code, char *buf, size_t len)
1261 if (!io_multiplexing_out)
1264 io_flush(NORMAL_FLUSH);
1265 stats.total_written += (len+4);
1266 mplex_write(code, buf, len);
1270 void close_multiplexing_in(void)
1272 io_multiplexing_in = 0;
1275 /** Stop output multiplexing. */
1276 void close_multiplexing_out(void)
1278 io_multiplexing_out = 0;
1281 void start_write_batch(int fd)
1283 write_stream_flags(batch_fd);
1285 /* Some communication has already taken place, but we don't
1286 * enable batch writing until here so that we can write a
1287 * canonical record of the communication even though the
1288 * actual communication so far depends on whether a daemon
1290 write_int(batch_fd, protocol_version);
1291 write_int(batch_fd, checksum_seed);
1294 write_batch_monitor_out = fd;
1296 write_batch_monitor_in = fd;
1299 void stop_write_batch(void)
1301 write_batch_monitor_out = -1;
1302 write_batch_monitor_in = -1;