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
42 static int io_multiplexing_out;
43 static int io_multiplexing_in;
44 static int multiplex_in_fd = -1;
45 static int multiplex_out_fd = -1;
46 static time_t last_io;
50 extern size_t bwlimit_writemax;
52 extern int io_timeout;
57 extern int checksum_seed;
58 extern int protocol_version;
59 extern char *remote_filesfrom_file;
60 extern struct stats stats;
62 const char phase_unknown[] = "unknown";
63 int select_timeout = SELECT_TIMEOUT;
67 * The connection might be dropped at some point; perhaps because the
68 * remote instance crashed. Just giving the offset on the stream is
69 * not very helpful. So instead we try to make io_phase_name point to
72 * For buffered/multiplexed I/O these names will be somewhat
73 * approximate; perhaps for ease of support we would rather make the
74 * buffer always flush when a single application-level I/O finishes.
76 * @todo Perhaps we want some simple stack functionality, but there's
77 * no need to overdo it.
79 const char *io_write_phase = phase_unknown;
80 const char *io_read_phase = phase_unknown;
82 /** Ignore EOF errors while reading a module listing if the remote
83 version is 24 or less. */
84 int kludge_around_eof = False;
89 static int write_batch_monitor_in = -1;
90 static int write_batch_monitor_out = -1;
92 static int io_filesfrom_f_in = -1;
93 static int io_filesfrom_f_out = -1;
94 static char io_filesfrom_buf[2048];
95 static char *io_filesfrom_bp;
96 static char io_filesfrom_lastchar;
97 static int io_filesfrom_buflen;
99 static void read_loop(int fd, char *buf, size_t len);
102 struct redo_list *next;
106 static struct redo_list *redo_list_head;
107 static struct redo_list *redo_list_tail;
110 struct msg_list *next;
115 static struct msg_list *msg_list_head;
116 static struct msg_list *msg_list_tail;
118 static void redo_list_add(int num)
120 struct redo_list *rl;
122 if (!(rl = new(struct redo_list)))
123 exit_cleanup(RERR_MALLOC);
127 redo_list_tail->next = rl;
133 static void check_timeout(void)
141 last_io = time(NULL);
147 if (last_io && io_timeout && (t-last_io) >= io_timeout) {
148 if (!am_server && !am_daemon) {
149 rprintf(FERROR, "io timeout after %d seconds - exiting\n",
152 exit_cleanup(RERR_TIMEOUT);
156 /** Setup the fd used to receive MSG_* messages. Only needed when
157 * we're the generator because the sender and receiver both use the
158 * multiplexed I/O setup. */
159 void set_msg_fd_in(int fd)
164 /** Setup the fd used to send our MSG_* messages. Only needed when
165 * we're the receiver because the generator and the sender both use
166 * the multiplexed I/O setup. */
167 void set_msg_fd_out(int fd)
170 set_nonblocking(msg_fd_out);
173 /* Add a message to the pending MSG_* list. */
174 static void msg_list_add(int code, char *buf, int len)
178 if (!(ml = new(struct msg_list)))
179 exit_cleanup(RERR_MALLOC);
181 if (!(ml->buf = new_array(char, len+4)))
182 exit_cleanup(RERR_MALLOC);
183 SIVAL(ml->buf, 0, ((code+MPLEX_BASE)<<24) | len);
184 memcpy(ml->buf+4, buf, len);
187 msg_list_tail->next = ml;
193 void send_msg(enum msgcode code, char *buf, int len)
195 msg_list_add(code, buf, len);
196 msg_list_push(NORMAL_FLUSH);
199 /** Read a message from the MSG_* fd and dispatch it. This is only
200 * called by the generator. */
201 static void read_msg_fd(void)
208 /* Temporarily disable msg_fd_in. This is needed to avoid looping back
209 * to this routine from read_timeout() and writefd_unbuffered(). */
212 read_loop(fd, buf, 4);
215 len = tag & 0xFFFFFF;
216 tag = (tag >> 24) - MPLEX_BASE;
221 rprintf(FERROR, "invalid message %d:%d\n", tag, len);
222 exit_cleanup(RERR_STREAMIO);
228 rprintf(FERROR, "invalid message %d:%d\n", tag, len);
229 exit_cleanup(RERR_STREAMIO);
231 read_loop(fd, buf, 4);
232 redo_list_add(IVAL(buf,0));
241 read_loop(fd, buf, n);
242 rwrite((enum logcode)tag, buf, n);
247 rprintf(FERROR, "unknown message %d:%d\n", tag, len);
248 exit_cleanup(RERR_STREAMIO);
254 /* Try to push messages off the list onto the wire. If we leave with more
255 * to do, return 0. On error, return -1. If everything flushed, return 1.
256 * This is only active in the receiver. */
257 int msg_list_push(int flush_it_all)
259 static int written = 0;
266 while (msg_list_head) {
267 struct msg_list *ml = msg_list_head;
268 int n = write(msg_fd_out, ml->buf + written, ml->len - written);
272 if (errno != EWOULDBLOCK && errno != EAGAIN)
277 FD_SET(msg_fd_out, &fds);
278 tv.tv_sec = select_timeout;
280 if (!select(msg_fd_out+1, NULL, &fds, NULL, &tv))
282 } else if ((written += n) == ml->len) {
284 msg_list_head = ml->next;
286 msg_list_tail = NULL;
294 int get_redo_num(void)
296 struct redo_list *next;
299 while (!redo_list_head)
302 num = redo_list_head->num;
303 next = redo_list_head->next;
304 free(redo_list_head);
305 redo_list_head = next;
307 redo_list_tail = NULL;
313 * When we're the receiver and we have a local --files-from list of names
314 * that needs to be sent over the socket to the sender, we have to do two
315 * things at the same time: send the sender a list of what files we're
316 * processing and read the incoming file+info list from the sender. We do
317 * this by augmenting the read_timeout() function to copy this data. It
318 * uses the io_filesfrom_buf to read a block of data from f_in (when it is
319 * ready, since it might be a pipe) and then blast it out f_out (when it
320 * is ready to receive more data).
322 void io_set_filesfrom_fds(int f_in, int f_out)
324 io_filesfrom_f_in = f_in;
325 io_filesfrom_f_out = f_out;
326 io_filesfrom_bp = io_filesfrom_buf;
327 io_filesfrom_lastchar = '\0';
328 io_filesfrom_buflen = 0;
332 * It's almost always an error to get an EOF when we're trying to read
333 * from the network, because the protocol is self-terminating.
335 * However, there is one unfortunate cases where it is not, which is
336 * rsync <2.4.6 sending a list of modules on a server, since the list
337 * is terminated by closing the socket. So, for the section of the
338 * program where that is a problem (start_socket_client),
339 * kludge_around_eof is True and we just exit.
341 static void whine_about_eof(void)
343 if (kludge_around_eof)
346 rprintf(FERROR, RSYNC_NAME ": connection unexpectedly closed "
347 "(%.0f bytes read so far)\n",
348 (double)stats.total_read);
350 exit_cleanup(RERR_STREAMIO);
354 static void die_from_readerr(int err)
356 /* this prevents us trying to write errors on a dead socket */
357 io_multiplexing_close();
359 rsyserr(FERROR, err, "read error");
360 exit_cleanup(RERR_STREAMIO);
365 * Read from a socket with I/O timeout. return the number of bytes
366 * read. If no bytes can be read then exit, never return a number <= 0.
368 * TODO: If the remote shell connection fails, then current versions
369 * actually report an "unexpected EOF" error here. Since it's a
370 * fairly common mistake to try to use rsh when ssh is required, we
371 * should trap that: if we fail to read any data at all, we should
372 * give a better explanation. We can tell whether the connection has
373 * started by looking e.g. at whether the remote version is known yet.
375 static int read_timeout(int fd, char *buf, size_t len)
379 io_flush(NORMAL_FLUSH);
382 /* until we manage to read *something* */
391 if (msg_fd_in >= 0) {
392 FD_SET(msg_fd_in, &r_fds);
393 if (msg_fd_in > maxfd)
395 } else if (msg_list_head) {
396 FD_SET(msg_fd_out, &w_fds);
397 if (msg_fd_out > maxfd)
400 if (io_filesfrom_f_out >= 0) {
402 if (io_filesfrom_buflen == 0) {
403 if (io_filesfrom_f_in >= 0) {
404 FD_SET(io_filesfrom_f_in, &r_fds);
405 new_fd = io_filesfrom_f_in;
407 io_filesfrom_f_out = -1;
411 FD_SET(io_filesfrom_f_out, &w_fds);
412 new_fd = io_filesfrom_f_out;
418 tv.tv_sec = select_timeout;
423 count = select(maxfd + 1, &r_fds, &w_fds, NULL, &tv);
427 exit_cleanup(RERR_SOCKETIO);
432 if (msg_fd_in >= 0 && FD_ISSET(msg_fd_in, &r_fds))
434 else if (msg_list_head && FD_ISSET(msg_fd_out, &w_fds))
435 msg_list_push(NORMAL_FLUSH);
437 if (io_filesfrom_f_out >= 0) {
438 if (io_filesfrom_buflen) {
439 if (FD_ISSET(io_filesfrom_f_out, &w_fds)) {
440 int l = write(io_filesfrom_f_out,
442 io_filesfrom_buflen);
444 if (!(io_filesfrom_buflen -= l))
445 io_filesfrom_bp = io_filesfrom_buf;
447 io_filesfrom_bp += l;
449 /* XXX should we complain? */
450 io_filesfrom_f_out = -1;
453 } else if (io_filesfrom_f_in >= 0) {
454 if (FD_ISSET(io_filesfrom_f_in, &r_fds)) {
455 int l = read(io_filesfrom_f_in,
457 sizeof io_filesfrom_buf);
459 /* Send end-of-file marker */
460 io_filesfrom_buf[0] = '\0';
461 io_filesfrom_buf[1] = '\0';
462 io_filesfrom_buflen = io_filesfrom_lastchar? 2 : 1;
463 io_filesfrom_f_in = -1;
466 char *s = io_filesfrom_buf + l;
467 /* Transform CR and/or LF into '\0' */
468 while (s-- > io_filesfrom_buf) {
469 if (*s == '\n' || *s == '\r')
473 if (!io_filesfrom_lastchar) {
474 /* Last buf ended with a '\0', so don't
475 * let this buf start with one. */
476 while (l && !*io_filesfrom_bp)
477 io_filesfrom_bp++, l--;
480 io_filesfrom_bp = io_filesfrom_buf;
482 char *f = io_filesfrom_bp;
485 /* Eliminate any multi-'\0' runs. */
487 if (!(*t++ = *f++)) {
488 while (f != eob && !*f)
492 io_filesfrom_lastchar = f[-1];
494 io_filesfrom_buflen = l;
500 if (!FD_ISSET(fd, &r_fds))
503 n = read(fd, buf, len);
507 whine_about_eof(); /* Doesn't return. */
508 if (errno == EINTR || errno == EWOULDBLOCK
511 die_from_readerr(errno); /* Doesn't return. */
518 last_io = time(NULL);
525 * Read a line into the "fname" buffer (which must be at least MAXPATHLEN
528 int read_filesfrom_line(int fd, char *fname)
530 char ch, *s, *eob = fname + MAXPATHLEN - 1;
532 int reading_remotely = remote_filesfrom_file != NULL;
533 int nulls = eol_nulls || reading_remotely;
538 cnt = read(fd, &ch, 1);
539 if (cnt < 0 && (errno == EWOULDBLOCK
540 || errno == EINTR || errno == EAGAIN)) {
545 tv.tv_sec = select_timeout;
547 if (!select(fd+1, &fds, NULL, NULL, &tv))
553 if (nulls? !ch : (ch == '\r' || ch == '\n')) {
554 /* Skip empty lines if reading locally. */
555 if (!reading_remotely && s == fname)
565 if (*fname == '#' || *fname == ';')
573 * Continue trying to read len bytes - don't return until len has been
576 static void read_loop(int fd, char *buf, size_t len)
579 int n = read_timeout(fd, buf, len);
588 * Read from the file descriptor handling multiplexing - return number
591 * Never returns <= 0.
593 static int readfd_unbuffered(int fd, char *buf, size_t len)
595 static size_t remaining;
599 static size_t bufferIdx = 0;
600 static size_t bufferSz;
602 if (fd != multiplex_in_fd)
603 return read_timeout(fd, buf, len);
605 if (!io_multiplexing_in && remaining == 0) {
607 bufferSz = 2 * IO_BUFFER_SIZE;
608 buffer = new_array(char, bufferSz);
610 out_of_memory("readfd_unbuffered");
612 remaining = read_timeout(fd, buffer, bufferSz);
618 len = MIN(len, remaining);
619 memcpy(buf, buffer + bufferIdx, len);
626 read_loop(fd, line, 4);
629 remaining = tag & 0xFFFFFF;
630 tag = (tag >> 24) - MPLEX_BASE;
634 if (!buffer || remaining > bufferSz) {
635 buffer = realloc_array(buffer, char, remaining);
637 out_of_memory("readfd_unbuffered");
638 bufferSz = remaining;
640 read_loop(fd, buffer, remaining);
645 if (remaining >= sizeof line) {
646 rprintf(FERROR, "multiplexing overflow %d:%ld\n\n",
647 tag, (long)remaining);
648 exit_cleanup(RERR_STREAMIO);
650 read_loop(fd, line, remaining);
651 rwrite((enum logcode)tag, line, remaining);
655 rprintf(FERROR, "unexpected tag %d\n", tag);
656 exit_cleanup(RERR_STREAMIO);
661 io_flush(NORMAL_FLUSH);
669 * Do a buffered read from @p fd. Don't return until all @p n bytes
670 * have been read. If all @p n can't be read then exit with an
673 static void readfd(int fd, char *buffer, size_t N)
679 ret = readfd_unbuffered(fd, buffer + total, N-total);
683 if (fd == write_batch_monitor_in) {
684 if ((size_t)write(batch_fd, buffer, total) != total)
685 exit_cleanup(RERR_FILEIO);
688 stats.total_read += total;
692 int32 read_int(int f)
699 if (ret == (int32)0xffffffff)
704 int64 read_longint(int f)
710 if ((int32)ret != (int32)0xffffffff)
714 rprintf(FERROR,"Integer overflow - attempted 64 bit offset\n");
715 exit_cleanup(RERR_UNSUPPORTED);
718 ret = IVAL(b,0) | (((int64)IVAL(b,4))<<32);
724 void read_buf(int f,char *buf,size_t len)
729 void read_sbuf(int f,char *buf,size_t len)
735 unsigned char read_byte(int f)
738 read_buf(f, (char *)&c, 1);
744 * Sleep after writing to limit I/O bandwidth usage.
746 * @todo Rather than sleeping after each write, it might be better to
747 * use some kind of averaging. The current algorithm seems to always
748 * use a bit less bandwidth than specified, because it doesn't make up
749 * for slow periods. But arguably this is a feature. In addition, we
750 * ought to take the time used to write the data into account.
752 * During some phases of big transfers (file FOO is uptodate) this is
753 * called with a small bytes_written every time. As the kernel has to
754 * round small waits up to guarantee that we actually wait at least the
755 * requested number of microseconds, this can become grossly inaccurate.
756 * We therefore keep track of the bytes we've written over time and only
757 * sleep when the accumulated delay is at least 1 tenth of a second.
759 static void sleep_for_bwlimit(int bytes_written)
761 static struct timeval prior_tv;
762 static long total_written = 0;
763 struct timeval tv, start_tv;
764 long elapsed_usec, sleep_usec;
766 #define ONE_SEC 1000000L /* # of microseconds in a second */
771 total_written += bytes_written;
773 gettimeofday(&start_tv, NULL);
774 if (prior_tv.tv_sec) {
775 elapsed_usec = (start_tv.tv_sec - prior_tv.tv_sec) * ONE_SEC
776 + (start_tv.tv_usec - prior_tv.tv_usec);
777 total_written -= elapsed_usec * bwlimit / (ONE_SEC/1024);
778 if (total_written < 0)
782 sleep_usec = total_written * (ONE_SEC/1024) / bwlimit;
783 if (sleep_usec < ONE_SEC / 10) {
788 tv.tv_sec = sleep_usec / ONE_SEC;
789 tv.tv_usec = sleep_usec % ONE_SEC;
790 select(0, NULL, NULL, NULL, &tv);
792 gettimeofday(&prior_tv, NULL);
793 elapsed_usec = (prior_tv.tv_sec - start_tv.tv_sec) * ONE_SEC
794 + (prior_tv.tv_usec - start_tv.tv_usec);
795 total_written = (sleep_usec - elapsed_usec) * bwlimit / (ONE_SEC/1024);
800 * Write len bytes to the file descriptor @p fd.
802 * This function underlies the multiplexing system. The body of the
803 * application never calls this function directly.
805 static void writefd_unbuffered(int fd,char *buf,size_t len)
809 int maxfd, count, ret;
812 if (fd == msg_fd_out) {
813 rprintf(FERROR, "Internal error: wrong write used in receiver.\n");
814 exit_cleanup(RERR_PROTOCOL);
819 while (total < len) {
824 if (msg_fd_in >= 0) {
826 FD_SET(msg_fd_in,&r_fds);
827 if (msg_fd_in > maxfd)
831 tv.tv_sec = select_timeout;
835 count = select(maxfd + 1, msg_fd_in >= 0 ? &r_fds : NULL,
839 if (count < 0 && errno == EBADF)
840 exit_cleanup(RERR_SOCKETIO);
845 if (msg_fd_in >= 0 && FD_ISSET(msg_fd_in, &r_fds))
848 if (!FD_ISSET(fd, &w_fds))
852 if (bwlimit && n > bwlimit_writemax)
853 n = bwlimit_writemax;
854 ret = write(fd, buf + total, n);
860 if (errno == EWOULDBLOCK || errno == EAGAIN) {
866 /* Don't try to write errors back across the stream. */
867 io_multiplexing_close();
868 rsyserr(FERROR, errno,
869 "writefd_unbuffered failed to write %ld bytes: phase \"%s\"",
870 (long)len, io_write_phase);
871 exit_cleanup(RERR_STREAMIO);
874 sleep_for_bwlimit(ret);
879 last_io = time(NULL);
886 static char *io_buffer;
887 static int io_buffer_count;
889 void io_start_buffering_out(int fd)
893 multiplex_out_fd = fd;
894 io_buffer = new_array(char, IO_BUFFER_SIZE);
896 out_of_memory("writefd");
900 void io_start_buffering_in(int fd)
902 multiplex_in_fd = fd;
906 * Write an message to a multiplexed stream. If this fails then rsync
909 static void mplex_write(int fd, enum msgcode code, char *buf, size_t len)
914 SIVAL(buffer, 0, ((MPLEX_BASE + (int)code)<<24) + len);
916 if (n > sizeof buffer - 4)
917 n = sizeof buffer - 4;
919 memcpy(&buffer[4], buf, n);
920 writefd_unbuffered(fd, buffer, n+4);
926 writefd_unbuffered(fd, buf, len);
930 void io_flush(int flush_it_all)
932 int fd = multiplex_out_fd;
934 msg_list_push(flush_it_all);
936 if (!io_buffer_count || no_flush)
939 if (io_multiplexing_out)
940 mplex_write(fd, MSG_DATA, io_buffer, io_buffer_count);
942 writefd_unbuffered(fd, io_buffer, io_buffer_count);
947 void io_end_buffering(void)
949 io_flush(NORMAL_FLUSH);
950 if (!io_multiplexing_out) {
956 static void writefd(int fd,char *buf,size_t len)
958 stats.total_written += len;
960 if (fd == msg_fd_out) {
961 rprintf(FERROR, "Internal error: wrong write used in receiver.\n");
962 exit_cleanup(RERR_PROTOCOL);
965 if (fd == write_batch_monitor_out) {
966 if ((size_t)write(batch_fd, buf, len) != len)
967 exit_cleanup(RERR_FILEIO);
970 if (!io_buffer || fd != multiplex_out_fd) {
971 writefd_unbuffered(fd, buf, len);
976 int n = MIN((int)len, IO_BUFFER_SIZE-io_buffer_count);
978 memcpy(io_buffer+io_buffer_count, buf, n);
981 io_buffer_count += n;
984 if (io_buffer_count == IO_BUFFER_SIZE)
985 io_flush(NORMAL_FLUSH);
990 void write_int(int f,int32 x)
998 void write_int_named(int f, int32 x, const char *phase)
1000 io_write_phase = phase;
1002 io_write_phase = phase_unknown;
1007 * Note: int64 may actually be a 32-bit type if ./configure couldn't find any
1008 * 64-bit types on this platform.
1010 void write_longint(int f, int64 x)
1014 if (x <= 0x7FFFFFFF) {
1015 write_int(f, (int)x);
1020 rprintf(FERROR,"Integer overflow - attempted 64 bit offset\n");
1021 exit_cleanup(RERR_UNSUPPORTED);
1023 write_int(f, (int32)0xFFFFFFFF);
1024 SIVAL(b,0,(x&0xFFFFFFFF));
1025 SIVAL(b,4,((x>>32)&0xFFFFFFFF));
1031 void write_buf(int f,char *buf,size_t len)
1036 /** Write a string to the connection */
1037 static void write_sbuf(int f,char *buf)
1039 write_buf(f, buf, strlen(buf));
1043 void write_byte(int f,unsigned char c)
1045 write_buf(f,(char *)&c,1);
1051 * Read a line of up to @p maxlen characters into @p buf (not counting
1052 * the trailing null). Strips the (required) trailing newline and all
1055 * @return 1 for success; 0 for I/O error or truncation.
1057 int read_line(int f, char *buf, size_t maxlen)
1061 read_buf(f, buf, 1);
1066 if (buf[0] != '\r') {
1076 void io_printf(int fd, const char *format, ...)
1082 va_start(ap, format);
1083 len = vsnprintf(buf, sizeof buf, format, ap);
1087 exit_cleanup(RERR_STREAMIO);
1089 write_sbuf(fd, buf);
1093 /** Setup for multiplexing a MSG_* stream with the data stream. */
1094 void io_start_multiplex_out(int fd)
1096 multiplex_out_fd = fd;
1097 io_flush(NORMAL_FLUSH);
1098 io_start_buffering_out(fd);
1099 io_multiplexing_out = 1;
1102 /** Setup for multiplexing a MSG_* stream with the data stream. */
1103 void io_start_multiplex_in(int fd)
1105 multiplex_in_fd = fd;
1106 io_flush(NORMAL_FLUSH);
1107 io_multiplexing_in = 1;
1110 /** Write an message to the multiplexed data stream. */
1111 int io_multiplex_write(enum msgcode code, char *buf, size_t len)
1113 if (!io_multiplexing_out)
1116 io_flush(NORMAL_FLUSH);
1117 stats.total_written += (len+4);
1118 mplex_write(multiplex_out_fd, code, buf, len);
1122 /** Stop output multiplexing. */
1123 void io_multiplexing_close(void)
1125 io_multiplexing_out = 0;
1128 void start_write_batch(int fd)
1130 /* Some communication has already taken place, but we don't
1131 * enable batch writing until here so that we can write a
1132 * canonical record of the communication even though the
1133 * actual communication so far depends on whether a daemon
1135 write_int(batch_fd, protocol_version);
1136 write_int(batch_fd, checksum_seed);
1137 stats.total_written -= sizeof (int) * 2;
1140 write_batch_monitor_out = fd;
1142 write_batch_monitor_in = fd;
1145 void stop_write_batch(void)
1147 write_batch_monitor_out = -1;
1148 write_batch_monitor_in = -1;