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;
55 /** Ignore EOF errors while reading a module listing if the remote
56 version is 24 or less. */
57 int kludge_around_eof = False;
60 static int io_error_fd = -1;
62 static void read_loop(int fd, char *buf, size_t len);
64 static void check_timeout(void)
66 extern int am_server, am_daemon;
71 if (!io_timeout) return;
80 if (last_io && io_timeout && (t-last_io) >= io_timeout) {
81 if (!am_server && !am_daemon) {
82 rprintf(FERROR,"io timeout after %d seconds - exiting\n",
85 exit_cleanup(RERR_TIMEOUT);
89 /** Setup the fd used to propogate errors */
90 void io_set_error_fd(int fd)
95 /** Read some data from the error fd and write it to the write log code */
96 static void read_error_fd(void)
100 int fd = io_error_fd;
103 /* io_error_fd is temporarily disabled -- is this meant to
104 * prevent indefinite recursion? */
107 read_loop(fd, buf, 4);
110 len = tag & 0xFFFFFF;
116 if (n > (sizeof(buf)-1))
118 read_loop(fd, buf, n);
119 rwrite((enum logcode)tag, buf, n);
128 * It's almost always an error to get an EOF when we're trying to read
129 * from the network, because the protocol is self-terminating.
131 * However, there is one unfortunate cases where it is not, which is
132 * rsync <2.4.6 sending a list of modules on a server, since the list
133 * is terminated by closing the socket. So, for the section of the
134 * program where that is a problem (start_socket_client),
135 * kludge_around_eof is True and we just exit.
137 static void whine_about_eof (void)
139 if (kludge_around_eof)
143 "%s: connection unexpectedly closed "
144 "(%.0f bytes read so far)\n",
145 RSYNC_NAME, (double)stats.total_read);
147 exit_cleanup (RERR_STREAMIO);
152 static void die_from_readerr (int err)
154 /* this prevents us trying to write errors on a dead socket */
155 io_multiplexing_close();
157 rprintf(FERROR, "%s: read error: %s\n",
158 RSYNC_NAME, strerror (err));
159 exit_cleanup(RERR_STREAMIO);
164 * Read from a socket with IO timeout. return the number of bytes
165 * read. If no bytes can be read then exit, never return a number <= 0.
167 * TODO: If the remote shell connection fails, then current versions
168 * actually report an "unexpected EOF" error here. Since it's a
169 * fairly common mistake to try to use rsh when ssh is required, we
170 * should trap that: if we fail to read any data at all, we should
171 * give a better explanation. We can tell whether the connection has
172 * started by looking e.g. at whether the remote version is known yet.
174 static int read_timeout (int fd, char *buf, size_t len)
181 /* until we manage to read *something* */
189 if (io_error_fd != -1) {
190 FD_SET(io_error_fd, &fds);
191 if (io_error_fd > fd) fd_count = io_error_fd+1;
194 tv.tv_sec = io_timeout?io_timeout:SELECT_TIMEOUT;
199 count = select(fd_count, &fds, NULL, NULL, &tv);
206 if (errno == EBADF) {
207 exit_cleanup(RERR_SOCKETIO);
212 if (io_error_fd != -1 && FD_ISSET(io_error_fd, &fds)) {
216 if (!FD_ISSET(fd, &fds)) continue;
218 n = read(fd, buf, len);
225 last_io = time(NULL);
229 return -1; /* doesn't return */
230 } else if (n == -1) {
231 if (errno == EINTR || errno == EWOULDBLOCK ||
235 die_from_readerr (errno);
246 * Continue trying to read len bytes - don't return until len has been
249 static void read_loop (int fd, char *buf, size_t len)
252 int n = read_timeout(fd, buf, len);
261 * Read from the file descriptor handling multiplexing - return number
264 * Never returns <= 0.
266 static int read_unbuffered(int fd, char *buf, size_t len)
268 static size_t remaining;
272 if (!io_multiplexing_in || fd != multiplex_in_fd)
273 return read_timeout(fd, buf, len);
277 len = MIN(len, remaining);
278 read_loop(fd, buf, len);
284 read_loop(fd, line, 4);
287 remaining = tag & 0xFFFFFF;
290 if (tag == MPLEX_BASE)
295 if (tag != FERROR && tag != FINFO) {
296 rprintf(FERROR, "unexpected tag %d\n", tag);
297 exit_cleanup(RERR_STREAMIO);
300 if (remaining > sizeof(line) - 1) {
301 rprintf(FERROR, "multiplexing overflow %d\n\n",
303 exit_cleanup(RERR_STREAMIO);
306 read_loop(fd, line, remaining);
309 rprintf((enum logcode) tag, "%s", line);
319 * Do a buffered read from @p fd. Don't return until all @p n bytes
320 * have been read. If all @p n can't be read then exit with an
323 static void readfd (int fd, char *buffer, size_t N)
331 ret = read_unbuffered (fd, buffer + total, N-total);
335 stats.total_read += total;
339 int32 read_int(int f)
346 if (ret == (int32)0xffffffff) return -1;
350 int64 read_longint(int f)
352 extern int remote_version;
357 if ((int32)ret != (int32)0xffffffff) {
362 rprintf(FERROR,"Integer overflow - attempted 64 bit offset\n");
363 exit_cleanup(RERR_UNSUPPORTED);
365 if (remote_version >= 16) {
367 ret = IVAL(b,0) | (((int64)IVAL(b,4))<<32);
374 void read_buf(int f,char *buf,size_t len)
379 void read_sbuf(int f,char *buf,size_t len)
381 read_buf (f,buf,len);
385 unsigned char read_byte(int f)
388 read_buf (f, (char *)&c, 1);
394 * Write len bytes to the file descriptor @p fd.
396 * This function underlies the multiplexing system. The body of the
397 * application never calls this function directly.
399 static void writefd_unbuffered(int fd,char *buf,size_t len)
410 while (total < len) {
416 if (io_error_fd != -1) {
417 FD_SET(io_error_fd,&r_fds);
418 if (io_error_fd > fd_count)
419 fd_count = io_error_fd;
422 tv.tv_sec = io_timeout?io_timeout:SELECT_TIMEOUT;
427 count = select(fd_count+1,
428 io_error_fd != -1?&r_fds:NULL,
437 if (errno == EBADF) {
438 exit_cleanup(RERR_SOCKETIO);
443 if (io_error_fd != -1 && FD_ISSET(io_error_fd, &r_fds)) {
447 if (FD_ISSET(fd, &w_fds)) {
449 size_t n = len-total;
450 ret = write(fd,buf+total,n);
452 if (ret == -1 && errno == EINTR) {
457 (errno == EWOULDBLOCK || errno == EAGAIN)) {
463 /* Don't try to write errors back
464 * across the stream */
465 io_multiplexing_close();
466 rprintf(FERROR, RSYNC_NAME
467 ": writefd_unbuffered failed to write %ld bytes: %s\n",
470 exit_cleanup(RERR_STREAMIO);
473 /* Sleep after writing to limit I/O bandwidth */
477 tv.tv_usec = ret * 1000 / bwlimit;
478 while (tv.tv_usec > 1000000)
481 tv.tv_usec -= 1000000;
483 select(0, NULL, NULL, NULL, &tv);
489 last_io = time(NULL);
497 static char *io_buffer;
498 static int io_buffer_count;
500 void io_start_buffering(int fd)
502 if (io_buffer) return;
503 multiplex_out_fd = fd;
504 io_buffer = (char *)malloc(IO_BUFFER_SIZE);
505 if (!io_buffer) out_of_memory("writefd");
510 * Write an message to a multiplexed stream. If this fails then rsync
513 static void mplex_write(int fd, enum logcode code, char *buf, size_t len)
518 SIVAL(buffer, 0, ((MPLEX_BASE + (int)code)<<24) + len);
520 if (n > (sizeof(buffer)-4)) {
521 n = sizeof(buffer)-4;
524 memcpy(&buffer[4], buf, n);
525 writefd_unbuffered(fd, buffer, n+4);
531 writefd_unbuffered(fd, buf, len);
538 int fd = multiplex_out_fd;
542 if (!io_buffer_count || no_flush) return;
544 if (io_multiplexing_out) {
545 mplex_write(fd, FNONE, io_buffer, io_buffer_count);
547 writefd_unbuffered(fd, io_buffer, io_buffer_count);
553 void io_end_buffering(void)
556 if (!io_multiplexing_out) {
562 static void writefd(int fd,char *buf,size_t len)
564 stats.total_written += len;
568 if (!io_buffer || fd != multiplex_out_fd) {
569 writefd_unbuffered(fd, buf, len);
574 int n = MIN((int) len, IO_BUFFER_SIZE-io_buffer_count);
576 memcpy(io_buffer+io_buffer_count, buf, n);
579 io_buffer_count += n;
582 if (io_buffer_count == IO_BUFFER_SIZE) io_flush();
587 void write_int(int f,int32 x)
596 * Note: int64 may actually be a 32-bit type if ./configure couldn't find any
597 * 64-bit types on this platform.
599 void write_longint(int f, int64 x)
601 extern int remote_version;
604 if (remote_version < 16 || x <= 0x7FFFFFFF) {
605 write_int(f, (int)x);
609 write_int(f, (int32)0xFFFFFFFF);
610 SIVAL(b,0,(x&0xFFFFFFFF));
611 SIVAL(b,4,((x>>32)&0xFFFFFFFF));
616 void write_buf(int f,char *buf,size_t len)
621 /** Write a string to the connection */
622 static void write_sbuf(int f,char *buf)
624 write_buf(f, buf, strlen(buf));
628 void write_byte(int f,unsigned char c)
630 write_buf(f,(char *)&c,1);
636 * Read a line of up to @p maxlen characters into @p buf. Does not
637 * contain a trailing newline or carriage return.
639 * @return 1 for success; 0 for io error or truncation.
641 int read_line(int f, char *buf, size_t maxlen)
648 if (buf[0] == '\n') {
652 if (buf[0] != '\r') {
666 void io_printf(int fd, const char *format, ...)
672 va_start(ap, format);
673 len = vsnprintf(buf, sizeof(buf), format, ap);
676 if (len < 0) exit_cleanup(RERR_STREAMIO);
682 /** Setup for multiplexing an error stream with the data stream */
683 void io_start_multiplex_out(int fd)
685 multiplex_out_fd = fd;
687 io_start_buffering(fd);
688 io_multiplexing_out = 1;
691 /** Setup for multiplexing an error stream with the data stream */
692 void io_start_multiplex_in(int fd)
694 multiplex_in_fd = fd;
696 io_multiplexing_in = 1;
699 /** Write an message to the multiplexed error stream */
700 int io_multiplex_write(enum logcode code, char *buf, size_t len)
702 if (!io_multiplexing_out) return 0;
705 stats.total_written += (len+4);
706 mplex_write(multiplex_out_fd, code, buf, len);
710 /** Stop output multiplexing */
711 void io_multiplexing_close(void)
713 io_multiplexing_out = 0;