2 Copyright (C) Andrew Tridgell 1996
3 Copyright (C) Paul Mackerras 1996
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 socket and pipe IO utilities used in rsync
27 /* if no timeout is specified then use a 60 second select timeout */
28 #define SELECT_TIMEOUT 60
32 static int io_multiplexing_out;
33 static int io_multiplexing_in;
34 static int multiplex_in_fd;
35 static int multiplex_out_fd;
36 static time_t last_io;
37 static int eof_error=1;
39 extern int io_timeout;
40 extern struct stats stats;
42 static int io_error_fd = -1;
44 static void read_loop(int fd, char *buf, int len);
46 static void check_timeout(void)
48 extern int am_server, am_daemon;
51 if (!io_timeout) return;
60 if (last_io && io_timeout && (t-last_io) >= io_timeout) {
61 if (!am_server && !am_daemon) {
62 rprintf(FERROR,"io timeout after %d second - exiting\n",
65 exit_cleanup(RERR_TIMEOUT);
69 /* setup the fd used to propogate errors */
70 void io_set_error_fd(int fd)
75 /* read some data from the error fd and write it to the write log code */
76 static void read_error_fd(void)
85 read_loop(fd, buf, 4);
94 if (n > (sizeof(buf)-1)) n = sizeof(buf)-1;
95 read_loop(fd, buf, n);
96 rwrite((enum logcode)tag, buf, n);
106 /* read from a socket with IO timeout. return the number of
107 bytes read. If no bytes can be read then exit, never return
109 static int read_timeout(int fd, char *buf, int len)
122 if (io_error_fd != -1) {
123 FD_SET(io_error_fd, &fds);
124 if (io_error_fd > fd) fd_count = io_error_fd+1;
127 tv.tv_sec = io_timeout?io_timeout:SELECT_TIMEOUT;
132 if (select(fd_count, &fds, NULL, NULL, &tv) < 1) {
133 if (errno == EBADF) {
134 exit_cleanup(RERR_SOCKETIO);
140 if (io_error_fd != -1 && FD_ISSET(io_error_fd, &fds)) {
144 if (!FD_ISSET(fd, &fds)) continue;
146 n = read(fd, buf, len);
153 last_io = time(NULL);
157 if (n == -1 && errno == EINTR) {
162 (errno == EWOULDBLOCK || errno == EAGAIN)) {
169 rprintf(FERROR,"unexpected EOF in read_timeout\n");
171 exit_cleanup(RERR_STREAMIO);
174 /* this prevents us trying to write errors on a dead socket */
175 io_multiplexing_close();
177 rprintf(FERROR,"read error: %s\n", strerror(errno));
178 exit_cleanup(RERR_STREAMIO);
184 /* continue trying to read len bytes - don't return until len
186 static void read_loop(int fd, char *buf, int len)
189 int n = read_timeout(fd, buf, len);
196 /* read from the file descriptor handling multiplexing -
197 return number of bytes read
199 static int read_unbuffered(int fd, char *buf, int len)
201 static int remaining;
205 if (!io_multiplexing_in || fd != multiplex_in_fd)
206 return read_timeout(fd, buf, len);
210 len = MIN(len, remaining);
211 read_loop(fd, buf, len);
217 read_loop(fd, line, 4);
220 remaining = tag & 0xFFFFFF;
223 if (tag == MPLEX_BASE) continue;
227 if (tag != FERROR && tag != FINFO) {
228 rprintf(FERROR,"unexpected tag %d\n", tag);
229 exit_cleanup(RERR_STREAMIO);
232 if (remaining > sizeof(line)-1) {
233 rprintf(FERROR,"multiplexing overflow %d\n\n",
235 exit_cleanup(RERR_STREAMIO);
238 read_loop(fd, line, remaining);
241 rprintf((enum logcode)tag,"%s", line);
249 /* do a buffered read from fd. don't return until all N bytes
250 have been read. If all N can't be read then exit with an error */
251 static void readfd(int fd,char *buffer,int N)
259 ret = read_unbuffered(fd,buffer + total,N-total);
263 stats.total_read += total;
267 int32 read_int(int f)
274 if (ret == (int32)0xffffffff) return -1;
278 int64 read_longint(int f)
280 extern int remote_version;
285 if ((int32)ret != (int32)0xffffffff) {
290 rprintf(FERROR,"Integer overflow - attempted 64 bit offset\n");
291 exit_cleanup(RERR_UNSUPPORTED);
293 if (remote_version >= 16) {
295 ret = IVAL(b,0) | (((int64)IVAL(b,4))<<32);
302 void read_buf(int f,char *buf,int len)
307 void read_sbuf(int f,char *buf,int len)
313 unsigned char read_byte(int f)
316 read_buf(f,(char *)&c,1);
320 /* write len bytes to fd */
321 static void writefd_unbuffered(int fd,char *buf,int len)
330 while (total < len) {
336 if (io_error_fd != -1) {
337 FD_SET(io_error_fd,&r_fds);
338 if (io_error_fd > fd_count)
339 fd_count = io_error_fd;
342 tv.tv_sec = io_timeout?io_timeout:SELECT_TIMEOUT;
347 count = select(fd_count+1,
348 io_error_fd != -1?&r_fds:NULL,
353 if (errno == EBADF) {
354 exit_cleanup(RERR_SOCKETIO);
360 if (io_error_fd != -1 && FD_ISSET(io_error_fd, &r_fds)) {
364 if (FD_ISSET(fd, &w_fds)) {
365 int ret, n = len-total;
367 ret = write(fd,buf+total,n);
369 if (ret == -1 && errno == EINTR) {
374 (errno == EWOULDBLOCK || errno == EAGAIN)) {
379 rprintf(FERROR,"erroring writing %d bytes - exiting\n", len);
380 exit_cleanup(RERR_STREAMIO);
383 /* Sleep after writing to limit I/O bandwidth */
387 tv.tv_usec = ret * 1000 / bwlimit;
388 while (tv.tv_usec > 1000000)
391 tv.tv_usec -= 1000000;
393 select(0, NULL, NULL, NULL, &tv);
399 last_io = time(NULL);
407 static char *io_buffer;
408 static int io_buffer_count;
410 void io_start_buffering(int fd)
412 if (io_buffer) return;
413 multiplex_out_fd = fd;
414 io_buffer = (char *)malloc(IO_BUFFER_SIZE);
415 if (!io_buffer) out_of_memory("writefd");
419 /* write an message to a multiplexed stream. If this fails then rsync
421 static void mplex_write(int fd, enum logcode code, char *buf, int len)
426 SIVAL(buffer, 0, ((MPLEX_BASE + (int)code)<<24) + len);
428 if (n > (sizeof(buffer)-4)) {
429 n = sizeof(buffer)-4;
432 memcpy(&buffer[4], buf, n);
433 writefd_unbuffered(fd, buffer, n+4);
439 writefd_unbuffered(fd, buf, len);
446 int fd = multiplex_out_fd;
447 if (!io_buffer_count || no_flush) return;
449 if (io_multiplexing_out) {
450 mplex_write(fd, FNONE, io_buffer, io_buffer_count);
452 writefd_unbuffered(fd, io_buffer, io_buffer_count);
457 void io_end_buffering(int fd)
460 if (!io_multiplexing_out) {
466 static void writefd(int fd,char *buf,int len)
468 stats.total_written += len;
470 if (!io_buffer || fd != multiplex_out_fd) {
471 writefd_unbuffered(fd, buf, len);
476 int n = MIN(len, IO_BUFFER_SIZE-io_buffer_count);
478 memcpy(io_buffer+io_buffer_count, buf, n);
481 io_buffer_count += n;
484 if (io_buffer_count == IO_BUFFER_SIZE) io_flush();
489 void write_int(int f,int32 x)
496 void write_longint(int f, int64 x)
498 extern int remote_version;
501 if (remote_version < 16 || x <= 0x7FFFFFFF) {
502 write_int(f, (int)x);
506 write_int(f, (int32)0xFFFFFFFF);
507 SIVAL(b,0,(x&0xFFFFFFFF));
508 SIVAL(b,4,((x>>32)&0xFFFFFFFF));
513 void write_buf(int f,char *buf,int len)
518 /* write a string to the connection */
519 static void write_sbuf(int f,char *buf)
521 write_buf(f, buf, strlen(buf));
525 void write_byte(int f,unsigned char c)
527 write_buf(f,(char *)&c,1);
530 int read_line(int f, char *buf, int maxlen)
537 if (buf[0] == 0) return 0;
538 if (buf[0] == '\n') {
542 if (buf[0] != '\r') {
558 void io_printf(int fd, const char *format, ...)
564 va_start(ap, format);
565 len = vslprintf(buf, sizeof(buf), format, ap);
568 if (len < 0) exit_cleanup(RERR_STREAMIO);
574 /* setup for multiplexing an error stream with the data stream */
575 void io_start_multiplex_out(int fd)
577 multiplex_out_fd = fd;
579 io_start_buffering(fd);
580 io_multiplexing_out = 1;
583 /* setup for multiplexing an error stream with the data stream */
584 void io_start_multiplex_in(int fd)
586 multiplex_in_fd = fd;
588 io_multiplexing_in = 1;
591 /* write an message to the multiplexed error stream */
592 int io_multiplex_write(enum logcode code, char *buf, int len)
594 if (!io_multiplexing_out) return 0;
597 stats.total_written += (len+4);
598 mplex_write(multiplex_out_fd, code, buf, len);
602 /* write a message to the special error fd */
603 int io_error_write(int f, enum logcode code, char *buf, int len)
605 if (f == -1) return 0;
606 mplex_write(f, code, buf, len);
610 /* stop output multiplexing */
611 void io_multiplexing_close(void)
613 io_multiplexing_out = 0;