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 IO 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;
45 static int multiplex_out_fd;
46 static time_t last_io;
51 extern int io_timeout;
52 extern struct stats stats;
56 * The connection might be dropped at some point; perhaps because the
57 * remote instance crashed. Just giving the offset on the stream is
58 * not very helpful. So instead we try to make io_phase_name point to
61 const char *io_phase_name = "unknown";
64 /** Ignore EOF errors while reading a module listing if the remote
65 version is 24 or less. */
66 int kludge_around_eof = False;
69 static int io_error_fd = -1;
71 static void read_loop(int fd, char *buf, size_t len);
73 static void check_timeout(void)
75 extern int am_server, am_daemon;
80 if (!io_timeout) return;
89 if (last_io && io_timeout && (t-last_io) >= io_timeout) {
90 if (!am_server && !am_daemon) {
91 rprintf(FERROR,"io timeout after %d seconds - exiting\n",
94 exit_cleanup(RERR_TIMEOUT);
98 /** Setup the fd used to propogate errors */
99 void io_set_error_fd(int fd)
104 /** Read some data from the error fd and write it to the write log code */
105 static void read_error_fd(void)
109 int fd = io_error_fd;
112 /* io_error_fd is temporarily disabled -- is this meant to
113 * prevent indefinite recursion? */
116 read_loop(fd, buf, 4);
119 len = tag & 0xFFFFFF;
125 if (n > (sizeof(buf)-1))
127 read_loop(fd, buf, n);
128 rwrite((enum logcode)tag, buf, n);
137 * It's almost always an error to get an EOF when we're trying to read
138 * from the network, because the protocol is self-terminating.
140 * However, there is one unfortunate cases where it is not, which is
141 * rsync <2.4.6 sending a list of modules on a server, since the list
142 * is terminated by closing the socket. So, for the section of the
143 * program where that is a problem (start_socket_client),
144 * kludge_around_eof is True and we just exit.
146 static void whine_about_eof (void)
148 if (kludge_around_eof)
152 "%s: connection unexpectedly closed "
153 "(%.0f bytes read so far)\n",
154 RSYNC_NAME, (double)stats.total_read);
156 exit_cleanup (RERR_STREAMIO);
161 static void die_from_readerr (int err)
163 /* this prevents us trying to write errors on a dead socket */
164 io_multiplexing_close();
166 rprintf(FERROR, "%s: read error: %s\n",
167 RSYNC_NAME, strerror (err));
168 exit_cleanup(RERR_STREAMIO);
173 * Read from a socket with IO timeout. return the number of bytes
174 * read. If no bytes can be read then exit, never return a number <= 0.
176 * TODO: If the remote shell connection fails, then current versions
177 * actually report an "unexpected EOF" error here. Since it's a
178 * fairly common mistake to try to use rsh when ssh is required, we
179 * should trap that: if we fail to read any data at all, we should
180 * give a better explanation. We can tell whether the connection has
181 * started by looking e.g. at whether the remote version is known yet.
183 static int read_timeout (int fd, char *buf, size_t len)
190 /* until we manage to read *something* */
198 if (io_error_fd != -1) {
199 FD_SET(io_error_fd, &fds);
200 if (io_error_fd > fd) fd_count = io_error_fd+1;
203 tv.tv_sec = io_timeout?io_timeout:SELECT_TIMEOUT;
208 count = select(fd_count, &fds, NULL, NULL, &tv);
215 if (errno == EBADF) {
216 exit_cleanup(RERR_SOCKETIO);
221 if (io_error_fd != -1 && FD_ISSET(io_error_fd, &fds)) {
225 if (!FD_ISSET(fd, &fds)) continue;
227 n = read(fd, buf, len);
234 last_io = time(NULL);
238 return -1; /* doesn't return */
239 } else if (n == -1) {
240 if (errno == EINTR || errno == EWOULDBLOCK ||
244 die_from_readerr (errno);
255 * Continue trying to read len bytes - don't return until len has been
258 static void read_loop (int fd, char *buf, size_t len)
261 int n = read_timeout(fd, buf, len);
270 * Read from the file descriptor handling multiplexing - return number
273 * Never returns <= 0.
275 static int read_unbuffered(int fd, char *buf, size_t len)
277 static size_t remaining;
281 if (!io_multiplexing_in || fd != multiplex_in_fd)
282 return read_timeout(fd, buf, len);
286 len = MIN(len, remaining);
287 read_loop(fd, buf, len);
293 read_loop(fd, line, 4);
296 remaining = tag & 0xFFFFFF;
299 if (tag == MPLEX_BASE)
304 if (tag != FERROR && tag != FINFO) {
305 rprintf(FERROR, "unexpected tag %d\n", tag);
306 exit_cleanup(RERR_STREAMIO);
309 if (remaining > sizeof(line) - 1) {
310 rprintf(FERROR, "multiplexing overflow %d\n\n",
312 exit_cleanup(RERR_STREAMIO);
315 read_loop(fd, line, remaining);
318 rprintf((enum logcode) tag, "%s", line);
328 * Do a buffered read from @p fd. Don't return until all @p n bytes
329 * have been read. If all @p n can't be read then exit with an
332 static void readfd (int fd, char *buffer, size_t N)
340 ret = read_unbuffered (fd, buffer + total, N-total);
344 stats.total_read += total;
348 int32 read_int(int f)
355 if (ret == (int32)0xffffffff) return -1;
359 int64 read_longint(int f)
361 extern int remote_version;
366 if ((int32)ret != (int32)0xffffffff) {
371 rprintf(FERROR,"Integer overflow - attempted 64 bit offset\n");
372 exit_cleanup(RERR_UNSUPPORTED);
374 if (remote_version >= 16) {
376 ret = IVAL(b,0) | (((int64)IVAL(b,4))<<32);
383 void read_buf(int f,char *buf,size_t len)
388 void read_sbuf(int f,char *buf,size_t len)
390 read_buf (f,buf,len);
394 unsigned char read_byte(int f)
397 read_buf (f, (char *)&c, 1);
403 * Sleep after writing to limit I/O bandwidth usage.
405 * @todo Rather than sleeping after each write, it might be better to
406 * use some kind of averaging. The current algorithm seems to always
407 * use a bit less bandwidth than specified, because it doesn't make up
408 * for slow periods. But arguably this is a feature. In addition, we
409 * ought to take the time used to write the data into account.
411 static void sleep_for_bwlimit(int bytes_written)
418 assert(bytes_written > 0);
421 tv.tv_usec = bytes_written * 1000 / bwlimit;
422 tv.tv_sec = tv.tv_usec / 1000000;
423 tv.tv_usec = tv.tv_usec % 1000000;
425 select(0, NULL, NULL, NULL, &tv);
430 * Write len bytes to the file descriptor @p fd.
432 * This function underlies the multiplexing system. The body of the
433 * application never calls this function directly.
435 static void writefd_unbuffered(int fd,char *buf,size_t len)
446 while (total < len) {
452 if (io_error_fd != -1) {
453 FD_SET(io_error_fd,&r_fds);
454 if (io_error_fd > fd_count)
455 fd_count = io_error_fd;
458 tv.tv_sec = io_timeout?io_timeout:SELECT_TIMEOUT;
463 count = select(fd_count+1,
464 io_error_fd != -1?&r_fds:NULL,
473 if (errno == EBADF) {
474 exit_cleanup(RERR_SOCKETIO);
479 if (io_error_fd != -1 && FD_ISSET(io_error_fd, &r_fds)) {
483 if (FD_ISSET(fd, &w_fds)) {
485 size_t n = len-total;
486 ret = write(fd,buf+total,n);
488 if (ret == -1 && errno == EINTR) {
493 (errno == EWOULDBLOCK || errno == EAGAIN)) {
499 /* Don't try to write errors back
500 * across the stream */
501 io_multiplexing_close();
502 rprintf(FERROR, RSYNC_NAME
503 ": writefd_unbuffered failed to write %ld bytes: phase \"%s\": %s\n",
504 (long) len, io_phase_name,
506 exit_cleanup(RERR_STREAMIO);
509 sleep_for_bwlimit(ret);
514 last_io = time(NULL);
522 static char *io_buffer;
523 static int io_buffer_count;
525 void io_start_buffering(int fd)
527 if (io_buffer) return;
528 multiplex_out_fd = fd;
529 io_buffer = (char *)malloc(IO_BUFFER_SIZE);
530 if (!io_buffer) out_of_memory("writefd");
535 * Write an message to a multiplexed stream. If this fails then rsync
538 static void mplex_write(int fd, enum logcode code, char *buf, size_t len)
543 SIVAL(buffer, 0, ((MPLEX_BASE + (int)code)<<24) + len);
545 if (n > (sizeof(buffer)-4)) {
546 n = sizeof(buffer)-4;
549 memcpy(&buffer[4], buf, n);
550 writefd_unbuffered(fd, buffer, n+4);
556 writefd_unbuffered(fd, buf, len);
563 int fd = multiplex_out_fd;
567 if (!io_buffer_count || no_flush) return;
569 if (io_multiplexing_out) {
570 mplex_write(fd, FNONE, io_buffer, io_buffer_count);
572 writefd_unbuffered(fd, io_buffer, io_buffer_count);
578 void io_end_buffering(void)
581 if (!io_multiplexing_out) {
587 static void writefd(int fd,char *buf,size_t len)
589 stats.total_written += len;
593 if (!io_buffer || fd != multiplex_out_fd) {
594 writefd_unbuffered(fd, buf, len);
599 int n = MIN((int) len, IO_BUFFER_SIZE-io_buffer_count);
601 memcpy(io_buffer+io_buffer_count, buf, n);
604 io_buffer_count += n;
607 if (io_buffer_count == IO_BUFFER_SIZE) io_flush();
612 void write_int(int f,int32 x)
621 * Note: int64 may actually be a 32-bit type if ./configure couldn't find any
622 * 64-bit types on this platform.
624 void write_longint(int f, int64 x)
626 extern int remote_version;
629 if (remote_version < 16 || x <= 0x7FFFFFFF) {
630 write_int(f, (int)x);
634 write_int(f, (int32)0xFFFFFFFF);
635 SIVAL(b,0,(x&0xFFFFFFFF));
636 SIVAL(b,4,((x>>32)&0xFFFFFFFF));
641 void write_buf(int f,char *buf,size_t len)
646 /** Write a string to the connection */
647 static void write_sbuf(int f,char *buf)
649 write_buf(f, buf, strlen(buf));
653 void write_byte(int f,unsigned char c)
655 write_buf(f,(char *)&c,1);
661 * Read a line of up to @p maxlen characters into @p buf. Does not
662 * contain a trailing newline or carriage return.
664 * @return 1 for success; 0 for io error or truncation.
666 int read_line(int f, char *buf, size_t maxlen)
673 if (buf[0] == '\n') {
677 if (buf[0] != '\r') {
691 void io_printf(int fd, const char *format, ...)
697 va_start(ap, format);
698 len = vsnprintf(buf, sizeof(buf), format, ap);
701 if (len < 0) exit_cleanup(RERR_STREAMIO);
707 /** Setup for multiplexing an error stream with the data stream */
708 void io_start_multiplex_out(int fd)
710 multiplex_out_fd = fd;
712 io_start_buffering(fd);
713 io_multiplexing_out = 1;
716 /** Setup for multiplexing an error stream with the data stream */
717 void io_start_multiplex_in(int fd)
719 multiplex_in_fd = fd;
721 io_multiplexing_in = 1;
724 /** Write an message to the multiplexed error stream */
725 int io_multiplex_write(enum logcode code, char *buf, size_t len)
727 if (!io_multiplexing_out) return 0;
730 stats.total_written += (len+4);
731 mplex_write(multiplex_out_fd, code, buf, len);
735 /** Stop output multiplexing */
736 void io_multiplexing_close(void)
738 io_multiplexing_out = 0;