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 * @todo Perhaps we want some simple stack functionality, but there's
62 * no need to overdo it.
64 const char *io_write_phase = "unknown";
65 const char *io_read_phase = "unknown";
68 /** Ignore EOF errors while reading a module listing if the remote
69 version is 24 or less. */
70 int kludge_around_eof = False;
73 static int io_error_fd = -1;
75 static void read_loop(int fd, char *buf, size_t len);
77 static void check_timeout(void)
79 extern int am_server, am_daemon;
84 if (!io_timeout) return;
93 if (last_io && io_timeout && (t-last_io) >= io_timeout) {
94 if (!am_server && !am_daemon) {
95 rprintf(FERROR,"io timeout after %d seconds - exiting\n",
98 exit_cleanup(RERR_TIMEOUT);
102 /** Setup the fd used to propogate errors */
103 void io_set_error_fd(int fd)
108 /** Read some data from the error fd and write it to the write log code */
109 static void read_error_fd(void)
113 int fd = io_error_fd;
116 /* io_error_fd is temporarily disabled -- is this meant to
117 * prevent indefinite recursion? */
120 read_loop(fd, buf, 4);
123 len = tag & 0xFFFFFF;
129 if (n > (sizeof(buf)-1))
131 read_loop(fd, buf, n);
132 rwrite((enum logcode)tag, buf, n);
141 * It's almost always an error to get an EOF when we're trying to read
142 * from the network, because the protocol is self-terminating.
144 * However, there is one unfortunate cases where it is not, which is
145 * rsync <2.4.6 sending a list of modules on a server, since the list
146 * is terminated by closing the socket. So, for the section of the
147 * program where that is a problem (start_socket_client),
148 * kludge_around_eof is True and we just exit.
150 static void whine_about_eof (void)
152 if (kludge_around_eof)
156 "%s: connection unexpectedly closed "
157 "(%.0f bytes read so far)\n",
158 RSYNC_NAME, (double)stats.total_read);
160 exit_cleanup (RERR_STREAMIO);
165 static void die_from_readerr (int err)
167 /* this prevents us trying to write errors on a dead socket */
168 io_multiplexing_close();
170 rprintf(FERROR, "%s: read error: %s\n",
171 RSYNC_NAME, strerror (err));
172 exit_cleanup(RERR_STREAMIO);
177 * Read from a socket with IO timeout. return the number of bytes
178 * read. If no bytes can be read then exit, never return a number <= 0.
180 * TODO: If the remote shell connection fails, then current versions
181 * actually report an "unexpected EOF" error here. Since it's a
182 * fairly common mistake to try to use rsh when ssh is required, we
183 * should trap that: if we fail to read any data at all, we should
184 * give a better explanation. We can tell whether the connection has
185 * started by looking e.g. at whether the remote version is known yet.
187 static int read_timeout (int fd, char *buf, size_t len)
194 /* until we manage to read *something* */
202 if (io_error_fd != -1) {
203 FD_SET(io_error_fd, &fds);
204 if (io_error_fd > fd) fd_count = io_error_fd+1;
207 tv.tv_sec = io_timeout?io_timeout:SELECT_TIMEOUT;
212 count = select(fd_count, &fds, NULL, NULL, &tv);
219 if (errno == EBADF) {
220 exit_cleanup(RERR_SOCKETIO);
225 if (io_error_fd != -1 && FD_ISSET(io_error_fd, &fds)) {
229 if (!FD_ISSET(fd, &fds)) continue;
231 n = read(fd, buf, len);
238 last_io = time(NULL);
242 return -1; /* doesn't return */
243 } else if (n == -1) {
244 if (errno == EINTR || errno == EWOULDBLOCK ||
248 die_from_readerr (errno);
259 * Continue trying to read len bytes - don't return until len has been
262 static void read_loop (int fd, char *buf, size_t len)
265 int n = read_timeout(fd, buf, len);
274 * Read from the file descriptor handling multiplexing - return number
277 * Never returns <= 0.
279 static int read_unbuffered(int fd, char *buf, size_t len)
281 static size_t remaining;
285 if (!io_multiplexing_in || fd != multiplex_in_fd)
286 return read_timeout(fd, buf, len);
290 len = MIN(len, remaining);
291 read_loop(fd, buf, len);
297 read_loop(fd, line, 4);
300 remaining = tag & 0xFFFFFF;
303 if (tag == MPLEX_BASE)
308 if (tag != FERROR && tag != FINFO) {
309 rprintf(FERROR, "unexpected tag %d\n", tag);
310 exit_cleanup(RERR_STREAMIO);
313 if (remaining > sizeof(line) - 1) {
314 rprintf(FERROR, "multiplexing overflow %d\n\n",
316 exit_cleanup(RERR_STREAMIO);
319 read_loop(fd, line, remaining);
322 rprintf((enum logcode) tag, "%s", line);
332 * Do a buffered read from @p fd. Don't return until all @p n bytes
333 * have been read. If all @p n can't be read then exit with an
336 static void readfd (int fd, char *buffer, size_t N)
344 ret = read_unbuffered (fd, buffer + total, N-total);
348 stats.total_read += total;
352 int32 read_int(int f)
359 if (ret == (int32)0xffffffff) return -1;
363 int64 read_longint(int f)
365 extern int remote_version;
370 if ((int32)ret != (int32)0xffffffff) {
375 rprintf(FERROR,"Integer overflow - attempted 64 bit offset\n");
376 exit_cleanup(RERR_UNSUPPORTED);
378 if (remote_version >= 16) {
380 ret = IVAL(b,0) | (((int64)IVAL(b,4))<<32);
387 void read_buf(int f,char *buf,size_t len)
392 void read_sbuf(int f,char *buf,size_t len)
394 read_buf (f,buf,len);
398 unsigned char read_byte(int f)
401 read_buf (f, (char *)&c, 1);
407 * Sleep after writing to limit I/O bandwidth usage.
409 * @todo Rather than sleeping after each write, it might be better to
410 * use some kind of averaging. The current algorithm seems to always
411 * use a bit less bandwidth than specified, because it doesn't make up
412 * for slow periods. But arguably this is a feature. In addition, we
413 * ought to take the time used to write the data into account.
415 static void sleep_for_bwlimit(int bytes_written)
422 assert(bytes_written > 0);
425 tv.tv_usec = bytes_written * 1000 / bwlimit;
426 tv.tv_sec = tv.tv_usec / 1000000;
427 tv.tv_usec = tv.tv_usec % 1000000;
429 select(0, NULL, NULL, NULL, &tv);
434 * Write len bytes to the file descriptor @p fd.
436 * This function underlies the multiplexing system. The body of the
437 * application never calls this function directly.
439 static void writefd_unbuffered(int fd,char *buf,size_t len)
450 while (total < len) {
456 if (io_error_fd != -1) {
457 FD_SET(io_error_fd,&r_fds);
458 if (io_error_fd > fd_count)
459 fd_count = io_error_fd;
462 tv.tv_sec = io_timeout?io_timeout:SELECT_TIMEOUT;
467 count = select(fd_count+1,
468 io_error_fd != -1?&r_fds:NULL,
477 if (errno == EBADF) {
478 exit_cleanup(RERR_SOCKETIO);
483 if (io_error_fd != -1 && FD_ISSET(io_error_fd, &r_fds)) {
487 if (FD_ISSET(fd, &w_fds)) {
489 size_t n = len-total;
490 ret = write(fd,buf+total,n);
492 if (ret == -1 && errno == EINTR) {
497 (errno == EWOULDBLOCK || errno == EAGAIN)) {
503 /* Don't try to write errors back
504 * across the stream */
505 io_multiplexing_close();
506 rprintf(FERROR, RSYNC_NAME
507 ": writefd_unbuffered failed to write %ld bytes: phase \"%s\": %s\n",
508 (long) len, io_write_phase,
510 exit_cleanup(RERR_STREAMIO);
513 sleep_for_bwlimit(ret);
518 last_io = time(NULL);
526 static char *io_buffer;
527 static int io_buffer_count;
529 void io_start_buffering(int fd)
531 if (io_buffer) return;
532 multiplex_out_fd = fd;
533 io_buffer = (char *)malloc(IO_BUFFER_SIZE);
534 if (!io_buffer) out_of_memory("writefd");
539 * Write an message to a multiplexed stream. If this fails then rsync
542 static void mplex_write(int fd, enum logcode code, char *buf, size_t len)
547 SIVAL(buffer, 0, ((MPLEX_BASE + (int)code)<<24) + len);
549 if (n > (sizeof(buffer)-4)) {
550 n = sizeof(buffer)-4;
553 memcpy(&buffer[4], buf, n);
554 writefd_unbuffered(fd, buffer, n+4);
560 writefd_unbuffered(fd, buf, len);
567 int fd = multiplex_out_fd;
571 if (!io_buffer_count || no_flush) return;
573 if (io_multiplexing_out) {
574 mplex_write(fd, FNONE, io_buffer, io_buffer_count);
576 writefd_unbuffered(fd, io_buffer, io_buffer_count);
582 void io_end_buffering(void)
585 if (!io_multiplexing_out) {
591 static void writefd(int fd,char *buf,size_t len)
593 stats.total_written += len;
597 if (!io_buffer || fd != multiplex_out_fd) {
598 writefd_unbuffered(fd, buf, len);
603 int n = MIN((int) len, IO_BUFFER_SIZE-io_buffer_count);
605 memcpy(io_buffer+io_buffer_count, buf, n);
608 io_buffer_count += n;
611 if (io_buffer_count == IO_BUFFER_SIZE) io_flush();
616 void write_int(int f,int32 x)
625 * Note: int64 may actually be a 32-bit type if ./configure couldn't find any
626 * 64-bit types on this platform.
628 void write_longint(int f, int64 x)
630 extern int remote_version;
633 if (remote_version < 16 || x <= 0x7FFFFFFF) {
634 write_int(f, (int)x);
638 write_int(f, (int32)0xFFFFFFFF);
639 SIVAL(b,0,(x&0xFFFFFFFF));
640 SIVAL(b,4,((x>>32)&0xFFFFFFFF));
645 void write_buf(int f,char *buf,size_t len)
650 /** Write a string to the connection */
651 static void write_sbuf(int f,char *buf)
653 write_buf(f, buf, strlen(buf));
657 void write_byte(int f,unsigned char c)
659 write_buf(f,(char *)&c,1);
665 * Read a line of up to @p maxlen characters into @p buf. Does not
666 * contain a trailing newline or carriage return.
668 * @return 1 for success; 0 for io error or truncation.
670 int read_line(int f, char *buf, size_t maxlen)
677 if (buf[0] == '\n') {
681 if (buf[0] != '\r') {
695 void io_printf(int fd, const char *format, ...)
701 va_start(ap, format);
702 len = vsnprintf(buf, sizeof(buf), format, ap);
705 if (len < 0) exit_cleanup(RERR_STREAMIO);
711 /** Setup for multiplexing an error stream with the data stream */
712 void io_start_multiplex_out(int fd)
714 multiplex_out_fd = fd;
716 io_start_buffering(fd);
717 io_multiplexing_out = 1;
720 /** Setup for multiplexing an error stream with the data stream */
721 void io_start_multiplex_in(int fd)
723 multiplex_in_fd = fd;
725 io_multiplexing_in = 1;
728 /** Write an message to the multiplexed error stream */
729 int io_multiplex_write(enum logcode code, char *buf, size_t len)
731 if (!io_multiplexing_out) return 0;
734 stats.total_written += (len+4);
735 mplex_write(multiplex_out_fd, code, buf, len);
739 /** Stop output multiplexing */
740 void io_multiplexing_close(void)
742 io_multiplexing_out = 0;