1 /* -*- c-file-style: "linux" -*-
3 Copyright (C) 1996-2000 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 second - 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)
86 read_loop(fd, buf, 4);
95 if (n > (sizeof(buf)-1)) n = sizeof(buf)-1;
96 read_loop(fd, buf, n);
97 rwrite((enum logcode)tag, buf, n);
108 * Read from a socket with IO timeout. return the number of bytes
109 * read. If no bytes can be read then exit, never return a number <= 0.
111 * TODO: If the remote shell connection fails, then current versions actually
112 * report an "unexpected EOF" error here. Since it's a fairly common mistake
113 * to try to use rsh when ssh is required, we should trap that: if we fail
114 * to read any data at all, we should give a better explanation.
116 static int read_timeout(int fd, char *buf, int len)
129 if (io_error_fd != -1) {
130 FD_SET(io_error_fd, &fds);
131 if (io_error_fd > fd) fd_count = io_error_fd+1;
134 tv.tv_sec = io_timeout?io_timeout:SELECT_TIMEOUT;
139 if (select(fd_count, &fds, NULL, NULL, &tv) < 1) {
140 if (errno == EBADF) {
141 exit_cleanup(RERR_SOCKETIO);
147 if (io_error_fd != -1 && FD_ISSET(io_error_fd, &fds)) {
151 if (!FD_ISSET(fd, &fds)) continue;
153 n = read(fd, buf, len);
160 last_io = time(NULL);
164 if (n == -1 && errno == EINTR) {
169 (errno == EWOULDBLOCK || errno == EAGAIN)) {
176 rprintf(FERROR,"unexpected EOF in read_timeout\n");
178 exit_cleanup(RERR_STREAMIO);
181 /* this prevents us trying to write errors on a dead socket */
182 io_multiplexing_close();
184 rprintf(FERROR,"read error: %s\n", strerror(errno));
185 exit_cleanup(RERR_STREAMIO);
191 /* continue trying to read len bytes - don't return until len
193 static void read_loop(int fd, char *buf, int len)
196 int n = read_timeout(fd, buf, len);
203 /* read from the file descriptor handling multiplexing -
204 return number of bytes read
206 static int read_unbuffered(int fd, char *buf, int len)
208 static int remaining;
212 if (!io_multiplexing_in || fd != multiplex_in_fd)
213 return read_timeout(fd, buf, len);
217 len = MIN(len, remaining);
218 read_loop(fd, buf, len);
224 read_loop(fd, line, 4);
227 remaining = tag & 0xFFFFFF;
230 if (tag == MPLEX_BASE) continue;
234 if (tag != FERROR && tag != FINFO) {
235 rprintf(FERROR,"unexpected tag %d\n", tag);
236 exit_cleanup(RERR_STREAMIO);
239 if (remaining > sizeof(line)-1) {
240 rprintf(FERROR,"multiplexing overflow %d\n\n",
242 exit_cleanup(RERR_STREAMIO);
245 read_loop(fd, line, remaining);
248 rprintf((enum logcode)tag,"%s", line);
256 /* do a buffered read from fd. don't return until all N bytes
257 have been read. If all N can't be read then exit with an error */
258 static void readfd(int fd,char *buffer,int N)
266 ret = read_unbuffered(fd,buffer + total,N-total);
270 stats.total_read += total;
274 int32 read_int(int f)
281 if (ret == (int32)0xffffffff) return -1;
285 int64 read_longint(int f)
287 extern int remote_version;
292 if ((int32)ret != (int32)0xffffffff) {
297 rprintf(FERROR,"Integer overflow - attempted 64 bit offset\n");
298 exit_cleanup(RERR_UNSUPPORTED);
300 if (remote_version >= 16) {
302 ret = IVAL(b,0) | (((int64)IVAL(b,4))<<32);
309 void read_buf(int f,char *buf,int len)
314 void read_sbuf(int f,char *buf,int len)
320 unsigned char read_byte(int f)
323 read_buf(f,(char *)&c,1);
327 /* write len bytes to fd */
328 static void writefd_unbuffered(int fd,char *buf,int len)
337 while (total < len) {
343 if (io_error_fd != -1) {
344 FD_SET(io_error_fd,&r_fds);
345 if (io_error_fd > fd_count)
346 fd_count = io_error_fd;
349 tv.tv_sec = io_timeout?io_timeout:SELECT_TIMEOUT;
354 count = select(fd_count+1,
355 io_error_fd != -1?&r_fds:NULL,
360 if (errno == EBADF) {
361 exit_cleanup(RERR_SOCKETIO);
367 if (io_error_fd != -1 && FD_ISSET(io_error_fd, &r_fds)) {
371 if (FD_ISSET(fd, &w_fds)) {
372 int ret, n = len-total;
374 ret = write(fd,buf+total,n);
376 if (ret == -1 && errno == EINTR) {
381 (errno == EWOULDBLOCK || errno == EAGAIN)) {
387 rprintf(FERROR,"erroring writing %d bytes - exiting\n", len);
388 exit_cleanup(RERR_STREAMIO);
391 /* Sleep after writing to limit I/O bandwidth */
395 tv.tv_usec = ret * 1000 / bwlimit;
396 while (tv.tv_usec > 1000000)
399 tv.tv_usec -= 1000000;
401 select(0, NULL, NULL, NULL, &tv);
407 last_io = time(NULL);
415 static char *io_buffer;
416 static int io_buffer_count;
418 void io_start_buffering(int fd)
420 if (io_buffer) return;
421 multiplex_out_fd = fd;
422 io_buffer = (char *)malloc(IO_BUFFER_SIZE);
423 if (!io_buffer) out_of_memory("writefd");
427 /* write an message to a multiplexed stream. If this fails then rsync
429 static void mplex_write(int fd, enum logcode code, char *buf, int len)
434 SIVAL(buffer, 0, ((MPLEX_BASE + (int)code)<<24) + len);
436 if (n > (sizeof(buffer)-4)) {
437 n = sizeof(buffer)-4;
440 memcpy(&buffer[4], buf, n);
441 writefd_unbuffered(fd, buffer, n+4);
447 writefd_unbuffered(fd, buf, len);
454 int fd = multiplex_out_fd;
455 if (!io_buffer_count || no_flush) return;
457 if (io_multiplexing_out) {
458 mplex_write(fd, FNONE, io_buffer, io_buffer_count);
460 writefd_unbuffered(fd, io_buffer, io_buffer_count);
466 /* XXX: fd is ignored, which seems a little strange. */
467 void io_end_buffering(int fd)
470 if (!io_multiplexing_out) {
476 /* some OSes have a bug where an exit causes the pending writes on
477 a socket to be flushed. Do an explicit shutdown to try to prevent this */
478 void io_shutdown(void)
480 if (multiplex_out_fd != -1) close(multiplex_out_fd);
481 if (io_error_fd != -1) close(io_error_fd);
482 multiplex_out_fd = -1;
487 static void writefd(int fd,char *buf,int len)
489 stats.total_written += len;
491 if (!io_buffer || fd != multiplex_out_fd) {
492 writefd_unbuffered(fd, buf, len);
497 int n = MIN(len, IO_BUFFER_SIZE-io_buffer_count);
499 memcpy(io_buffer+io_buffer_count, buf, n);
502 io_buffer_count += n;
505 if (io_buffer_count == IO_BUFFER_SIZE) io_flush();
510 void write_int(int f,int32 x)
519 * Note: int64 may actually be a 32-bit type if ./configure couldn't find any
520 * 64-bit types on this platform.
522 void write_longint(int f, int64 x)
524 extern int remote_version;
527 if (remote_version < 16 || x <= 0x7FFFFFFF) {
528 write_int(f, (int)x);
532 write_int(f, (int32)0xFFFFFFFF);
533 SIVAL(b,0,(x&0xFFFFFFFF));
534 SIVAL(b,4,((x>>32)&0xFFFFFFFF));
539 void write_buf(int f,char *buf,int len)
544 /* write a string to the connection */
545 static void write_sbuf(int f,char *buf)
547 write_buf(f, buf, strlen(buf));
551 void write_byte(int f,unsigned char c)
553 write_buf(f,(char *)&c,1);
556 int read_line(int f, char *buf, int maxlen)
563 if (buf[0] == 0) return 0;
564 if (buf[0] == '\n') {
568 if (buf[0] != '\r') {
584 void io_printf(int fd, const char *format, ...)
590 va_start(ap, format);
591 len = vslprintf(buf, sizeof(buf), format, ap);
594 if (len < 0) exit_cleanup(RERR_STREAMIO);
600 /* setup for multiplexing an error stream with the data stream */
601 void io_start_multiplex_out(int fd)
603 multiplex_out_fd = fd;
605 io_start_buffering(fd);
606 io_multiplexing_out = 1;
609 /* setup for multiplexing an error stream with the data stream */
610 void io_start_multiplex_in(int fd)
612 multiplex_in_fd = fd;
614 io_multiplexing_in = 1;
617 /* write an message to the multiplexed error stream */
618 int io_multiplex_write(enum logcode code, char *buf, int len)
620 if (!io_multiplexing_out) return 0;
623 stats.total_written += (len+4);
624 mplex_write(multiplex_out_fd, code, buf, len);
628 /* write a message to the special error fd */
629 int io_error_write(int f, enum logcode code, char *buf, int len)
631 if (f == -1) return 0;
632 mplex_write(f, code, buf, len);
636 /* stop output multiplexing */
637 void io_multiplexing_close(void)
639 io_multiplexing_out = 0;