1 /* -*- c-file-style: "linux" -*-
3 Copyright (C) 1996-2001 by Andrew Tridgell
4 Copyright (C) Paul Mackerras 1996
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 socket and pipe IO utilities used in rsync
28 /* if no timeout is specified then use a 60 second select timeout */
29 #define SELECT_TIMEOUT 60
33 static int io_multiplexing_out;
34 static int io_multiplexing_in;
35 static int multiplex_in_fd;
36 static int multiplex_out_fd;
37 static time_t last_io;
38 static int eof_error=1;
40 extern int io_timeout;
41 extern struct stats stats;
43 static int io_error_fd = -1;
45 static void read_loop(int fd, char *buf, int len);
47 static void check_timeout(void)
49 extern int am_server, am_daemon;
52 if (!io_timeout) return;
61 if (last_io && io_timeout && (t-last_io) >= io_timeout) {
62 if (!am_server && !am_daemon) {
63 rprintf(FERROR,"io timeout after %d seconds - exiting\n",
66 exit_cleanup(RERR_TIMEOUT);
70 /* setup the fd used to propogate errors */
71 void io_set_error_fd(int fd)
76 /* read some data from the error fd and write it to the write log code */
77 static void read_error_fd(void)
84 /* io_error_fd is temporarily disabled -- is this meant to
85 * prevent indefinite recursion? */
88 read_loop(fd, buf, 4);
97 if (n > (sizeof(buf)-1)) n = sizeof(buf)-1;
98 read_loop(fd, buf, n);
99 rwrite((enum logcode)tag, buf, n);
110 * Read from a socket with IO timeout. return the number of bytes
111 * read. If no bytes can be read then exit, never return a number <= 0.
113 * TODO: If the remote shell connection fails, then current versions
114 * actually report an "unexpected EOF" error here. Since it's a
115 * fairly common mistake to try to use rsh when ssh is required, we
116 * should trap that: if we fail to read any data at all, we should
117 * give a better explanation. We can tell whether the connection has
118 * started by looking e.g. at whether the remote version is known yet.
120 static int read_timeout(int fd, char *buf, int len)
133 if (io_error_fd != -1) {
134 FD_SET(io_error_fd, &fds);
135 if (io_error_fd > fd) fd_count = io_error_fd+1;
138 tv.tv_sec = io_timeout?io_timeout:SELECT_TIMEOUT;
143 if (select(fd_count, &fds, NULL, NULL, &tv) < 1) {
144 if (errno == EBADF) {
145 exit_cleanup(RERR_SOCKETIO);
151 if (io_error_fd != -1 && FD_ISSET(io_error_fd, &fds)) {
155 if (!FD_ISSET(fd, &fds)) continue;
157 n = read(fd, buf, len);
164 last_io = time(NULL);
168 if (n == -1 && errno == EINTR) {
173 (errno == EWOULDBLOCK || errno == EAGAIN)) {
181 "%s: connection to server unexpectedly closed"
182 " (%ld bytes read so far)\n",
183 RSYNC_NAME, stats.total_read);
185 exit_cleanup(RERR_STREAMIO);
188 /* this prevents us trying to write errors on a dead socket */
189 io_multiplexing_close();
191 rprintf(FERROR,"read error: %s\n", strerror(errno));
192 exit_cleanup(RERR_STREAMIO);
198 /* continue trying to read len bytes - don't return until len
200 static void read_loop(int fd, char *buf, int len)
203 int n = read_timeout(fd, buf, len);
210 /* read from the file descriptor handling multiplexing -
211 return number of bytes read
213 static int read_unbuffered(int fd, char *buf, int len)
215 static int remaining;
219 if (!io_multiplexing_in || fd != multiplex_in_fd)
220 return read_timeout(fd, buf, len);
224 len = MIN(len, remaining);
225 read_loop(fd, buf, len);
231 read_loop(fd, line, 4);
234 remaining = tag & 0xFFFFFF;
237 if (tag == MPLEX_BASE) continue;
241 if (tag != FERROR && tag != FINFO) {
242 rprintf(FERROR,"unexpected tag %d\n", tag);
243 exit_cleanup(RERR_STREAMIO);
246 if (remaining > sizeof(line)-1) {
247 rprintf(FERROR,"multiplexing overflow %d\n\n",
249 exit_cleanup(RERR_STREAMIO);
252 read_loop(fd, line, remaining);
255 rprintf((enum logcode)tag,"%s", line);
263 /* do a buffered read from fd. don't return until all N bytes
264 have been read. If all N can't be read then exit with an error */
265 static void readfd(int fd,char *buffer,int N)
273 ret = read_unbuffered(fd,buffer + total,N-total);
277 stats.total_read += total;
281 int32 read_int(int f)
288 if (ret == (int32)0xffffffff) return -1;
292 int64 read_longint(int f)
294 extern int remote_version;
299 if ((int32)ret != (int32)0xffffffff) {
304 rprintf(FERROR,"Integer overflow - attempted 64 bit offset\n");
305 exit_cleanup(RERR_UNSUPPORTED);
307 if (remote_version >= 16) {
309 ret = IVAL(b,0) | (((int64)IVAL(b,4))<<32);
316 void read_buf(int f,char *buf,int len)
321 void read_sbuf(int f,char *buf,int len)
327 unsigned char read_byte(int f)
330 read_buf(f,(char *)&c,1);
334 /* write len bytes to fd */
335 static void writefd_unbuffered(int fd,char *buf,int len)
344 while (total < len) {
350 if (io_error_fd != -1) {
351 FD_SET(io_error_fd,&r_fds);
352 if (io_error_fd > fd_count)
353 fd_count = io_error_fd;
356 tv.tv_sec = io_timeout?io_timeout:SELECT_TIMEOUT;
361 count = select(fd_count+1,
362 io_error_fd != -1?&r_fds:NULL,
367 if (errno == EBADF) {
368 exit_cleanup(RERR_SOCKETIO);
374 if (io_error_fd != -1 && FD_ISSET(io_error_fd, &r_fds)) {
378 if (FD_ISSET(fd, &w_fds)) {
379 int ret, n = len-total;
381 ret = write(fd,buf+total,n);
383 if (ret == -1 && errno == EINTR) {
388 (errno == EWOULDBLOCK || errno == EAGAIN)) {
395 "error writing %d unbuffered bytes"
396 " - exiting: %s\n", len,
398 exit_cleanup(RERR_STREAMIO);
401 /* Sleep after writing to limit I/O bandwidth */
405 tv.tv_usec = ret * 1000 / bwlimit;
406 while (tv.tv_usec > 1000000)
409 tv.tv_usec -= 1000000;
411 select(0, NULL, NULL, NULL, &tv);
417 last_io = time(NULL);
425 static char *io_buffer;
426 static int io_buffer_count;
428 void io_start_buffering(int fd)
430 if (io_buffer) return;
431 multiplex_out_fd = fd;
432 io_buffer = (char *)malloc(IO_BUFFER_SIZE);
433 if (!io_buffer) out_of_memory("writefd");
437 /* write an message to a multiplexed stream. If this fails then rsync
439 static void mplex_write(int fd, enum logcode code, char *buf, int len)
444 SIVAL(buffer, 0, ((MPLEX_BASE + (int)code)<<24) + len);
446 if (n > (sizeof(buffer)-4)) {
447 n = sizeof(buffer)-4;
450 memcpy(&buffer[4], buf, n);
451 writefd_unbuffered(fd, buffer, n+4);
457 writefd_unbuffered(fd, buf, len);
464 int fd = multiplex_out_fd;
465 if (!io_buffer_count || no_flush) return;
467 if (io_multiplexing_out) {
468 mplex_write(fd, FNONE, io_buffer, io_buffer_count);
470 writefd_unbuffered(fd, io_buffer, io_buffer_count);
476 /* XXX: fd is ignored, which seems a little strange. */
477 void io_end_buffering(int fd)
480 if (!io_multiplexing_out) {
486 /* some OSes have a bug where an exit causes the pending writes on
487 a socket to be flushed. Do an explicit shutdown to try to prevent this */
488 void io_shutdown(void)
490 if (multiplex_out_fd != -1) close(multiplex_out_fd);
491 if (io_error_fd != -1) close(io_error_fd);
492 multiplex_out_fd = -1;
497 static void writefd(int fd,char *buf,int len)
499 stats.total_written += len;
501 if (!io_buffer || fd != multiplex_out_fd) {
502 writefd_unbuffered(fd, buf, len);
507 int n = MIN(len, IO_BUFFER_SIZE-io_buffer_count);
509 memcpy(io_buffer+io_buffer_count, buf, n);
512 io_buffer_count += n;
515 if (io_buffer_count == IO_BUFFER_SIZE) io_flush();
520 void write_int(int f,int32 x)
529 * Note: int64 may actually be a 32-bit type if ./configure couldn't find any
530 * 64-bit types on this platform.
532 void write_longint(int f, int64 x)
534 extern int remote_version;
537 if (remote_version < 16 || x <= 0x7FFFFFFF) {
538 write_int(f, (int)x);
542 write_int(f, (int32)0xFFFFFFFF);
543 SIVAL(b,0,(x&0xFFFFFFFF));
544 SIVAL(b,4,((x>>32)&0xFFFFFFFF));
549 void write_buf(int f,char *buf,int len)
554 /* write a string to the connection */
555 static void write_sbuf(int f,char *buf)
557 write_buf(f, buf, strlen(buf));
561 void write_byte(int f,unsigned char c)
563 write_buf(f,(char *)&c,1);
566 int read_line(int f, char *buf, int maxlen)
573 if (buf[0] == 0) return 0;
574 if (buf[0] == '\n') {
578 if (buf[0] != '\r') {
594 void io_printf(int fd, const char *format, ...)
600 va_start(ap, format);
601 len = vslprintf(buf, sizeof(buf), format, ap);
604 if (len < 0) exit_cleanup(RERR_STREAMIO);
610 /* setup for multiplexing an error stream with the data stream */
611 void io_start_multiplex_out(int fd)
613 multiplex_out_fd = fd;
615 io_start_buffering(fd);
616 io_multiplexing_out = 1;
619 /* setup for multiplexing an error stream with the data stream */
620 void io_start_multiplex_in(int fd)
622 multiplex_in_fd = fd;
624 io_multiplexing_in = 1;
627 /* write an message to the multiplexed error stream */
628 int io_multiplex_write(enum logcode code, char *buf, int len)
630 if (!io_multiplexing_out) return 0;
633 stats.total_written += (len+4);
634 mplex_write(multiplex_out_fd, code, buf, len);
638 /* write a message to the special error fd */
639 int io_error_write(int f, enum logcode code, char *buf, int len)
641 if (f == -1) return 0;
642 mplex_write(f, code, buf, len);
646 /* stop output multiplexing */
647 void io_multiplexing_close(void)
649 io_multiplexing_out = 0;