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);
107 /* read from a socket with IO timeout. return the number of
108 bytes read. If no bytes can be read then exit, never return
110 static int read_timeout(int fd, char *buf, int len)
123 if (io_error_fd != -1) {
124 FD_SET(io_error_fd, &fds);
125 if (io_error_fd > fd) fd_count = io_error_fd+1;
128 tv.tv_sec = io_timeout?io_timeout:SELECT_TIMEOUT;
133 if (select(fd_count, &fds, NULL, NULL, &tv) < 1) {
134 if (errno == EBADF) {
135 exit_cleanup(RERR_SOCKETIO);
141 if (io_error_fd != -1 && FD_ISSET(io_error_fd, &fds)) {
145 if (!FD_ISSET(fd, &fds)) continue;
147 n = read(fd, buf, len);
154 last_io = time(NULL);
158 if (n == -1 && errno == EINTR) {
163 (errno == EWOULDBLOCK || errno == EAGAIN)) {
170 rprintf(FERROR,"unexpected EOF in read_timeout\n");
172 exit_cleanup(RERR_STREAMIO);
175 /* this prevents us trying to write errors on a dead socket */
176 io_multiplexing_close();
178 rprintf(FERROR,"read error: %s\n", strerror(errno));
179 exit_cleanup(RERR_STREAMIO);
185 /* continue trying to read len bytes - don't return until len
187 static void read_loop(int fd, char *buf, int len)
190 int n = read_timeout(fd, buf, len);
197 /* read from the file descriptor handling multiplexing -
198 return number of bytes read
200 static int read_unbuffered(int fd, char *buf, int len)
202 static int remaining;
206 if (!io_multiplexing_in || fd != multiplex_in_fd)
207 return read_timeout(fd, buf, len);
211 len = MIN(len, remaining);
212 read_loop(fd, buf, len);
218 read_loop(fd, line, 4);
221 remaining = tag & 0xFFFFFF;
224 if (tag == MPLEX_BASE) continue;
228 if (tag != FERROR && tag != FINFO) {
229 rprintf(FERROR,"unexpected tag %d\n", tag);
230 exit_cleanup(RERR_STREAMIO);
233 if (remaining > sizeof(line)-1) {
234 rprintf(FERROR,"multiplexing overflow %d\n\n",
236 exit_cleanup(RERR_STREAMIO);
239 read_loop(fd, line, remaining);
242 rprintf((enum logcode)tag,"%s", line);
250 /* do a buffered read from fd. don't return until all N bytes
251 have been read. If all N can't be read then exit with an error */
252 static void readfd(int fd,char *buffer,int N)
260 ret = read_unbuffered(fd,buffer + total,N-total);
264 stats.total_read += total;
268 int32 read_int(int f)
275 if (ret == (int32)0xffffffff) return -1;
279 int64 read_longint(int f)
281 extern int remote_version;
286 if ((int32)ret != (int32)0xffffffff) {
291 rprintf(FERROR,"Integer overflow - attempted 64 bit offset\n");
292 exit_cleanup(RERR_UNSUPPORTED);
294 if (remote_version >= 16) {
296 ret = IVAL(b,0) | (((int64)IVAL(b,4))<<32);
303 void read_buf(int f,char *buf,int len)
308 void read_sbuf(int f,char *buf,int len)
314 unsigned char read_byte(int f)
317 read_buf(f,(char *)&c,1);
321 /* write len bytes to fd */
322 static void writefd_unbuffered(int fd,char *buf,int len)
331 while (total < len) {
337 if (io_error_fd != -1) {
338 FD_SET(io_error_fd,&r_fds);
339 if (io_error_fd > fd_count)
340 fd_count = io_error_fd;
343 tv.tv_sec = io_timeout?io_timeout:SELECT_TIMEOUT;
348 count = select(fd_count+1,
349 io_error_fd != -1?&r_fds:NULL,
354 if (errno == EBADF) {
355 exit_cleanup(RERR_SOCKETIO);
361 if (io_error_fd != -1 && FD_ISSET(io_error_fd, &r_fds)) {
365 if (FD_ISSET(fd, &w_fds)) {
366 int ret, n = len-total;
368 ret = write(fd,buf+total,n);
370 if (ret == -1 && errno == EINTR) {
375 (errno == EWOULDBLOCK || errno == EAGAIN)) {
381 rprintf(FERROR,"erroring writing %d bytes - exiting\n", len);
382 exit_cleanup(RERR_STREAMIO);
385 /* Sleep after writing to limit I/O bandwidth */
389 tv.tv_usec = ret * 1000 / bwlimit;
390 while (tv.tv_usec > 1000000)
393 tv.tv_usec -= 1000000;
395 select(0, NULL, NULL, NULL, &tv);
401 last_io = time(NULL);
409 static char *io_buffer;
410 static int io_buffer_count;
412 void io_start_buffering(int fd)
414 if (io_buffer) return;
415 multiplex_out_fd = fd;
416 io_buffer = (char *)malloc(IO_BUFFER_SIZE);
417 if (!io_buffer) out_of_memory("writefd");
421 /* write an message to a multiplexed stream. If this fails then rsync
423 static void mplex_write(int fd, enum logcode code, char *buf, int len)
428 SIVAL(buffer, 0, ((MPLEX_BASE + (int)code)<<24) + len);
430 if (n > (sizeof(buffer)-4)) {
431 n = sizeof(buffer)-4;
434 memcpy(&buffer[4], buf, n);
435 writefd_unbuffered(fd, buffer, n+4);
441 writefd_unbuffered(fd, buf, len);
448 int fd = multiplex_out_fd;
449 if (!io_buffer_count || no_flush) return;
451 if (io_multiplexing_out) {
452 mplex_write(fd, FNONE, io_buffer, io_buffer_count);
454 writefd_unbuffered(fd, io_buffer, io_buffer_count);
460 /* XXX: fd is ignored, which seems a little strange. */
461 void io_end_buffering(int fd)
464 if (!io_multiplexing_out) {
470 /* some OSes have a bug where an exit causes the pending writes on
471 a socket to be flushed. Do an explicit shutdown to try to prevent this */
472 void io_shutdown(void)
474 if (multiplex_out_fd != -1) close(multiplex_out_fd);
475 if (io_error_fd != -1) close(io_error_fd);
476 multiplex_out_fd = -1;
481 static void writefd(int fd,char *buf,int len)
483 stats.total_written += len;
485 if (!io_buffer || fd != multiplex_out_fd) {
486 writefd_unbuffered(fd, buf, len);
491 int n = MIN(len, IO_BUFFER_SIZE-io_buffer_count);
493 memcpy(io_buffer+io_buffer_count, buf, n);
496 io_buffer_count += n;
499 if (io_buffer_count == IO_BUFFER_SIZE) io_flush();
504 void write_int(int f,int32 x)
513 * Note: int64 may actually be a 32-bit type if ./configure couldn't find any
514 * 64-bit types on this platform.
516 void write_longint(int f, int64 x)
518 extern int remote_version;
521 if (remote_version < 16 || x <= 0x7FFFFFFF) {
522 write_int(f, (int)x);
526 write_int(f, (int32)0xFFFFFFFF);
527 SIVAL(b,0,(x&0xFFFFFFFF));
528 SIVAL(b,4,((x>>32)&0xFFFFFFFF));
533 void write_buf(int f,char *buf,int len)
538 /* write a string to the connection */
539 static void write_sbuf(int f,char *buf)
541 write_buf(f, buf, strlen(buf));
545 void write_byte(int f,unsigned char c)
547 write_buf(f,(char *)&c,1);
550 int read_line(int f, char *buf, int maxlen)
557 if (buf[0] == 0) return 0;
558 if (buf[0] == '\n') {
562 if (buf[0] != '\r') {
578 void io_printf(int fd, const char *format, ...)
584 va_start(ap, format);
585 len = vslprintf(buf, sizeof(buf), format, ap);
588 if (len < 0) exit_cleanup(RERR_STREAMIO);
594 /* setup for multiplexing an error stream with the data stream */
595 void io_start_multiplex_out(int fd)
597 multiplex_out_fd = fd;
599 io_start_buffering(fd);
600 io_multiplexing_out = 1;
603 /* setup for multiplexing an error stream with the data stream */
604 void io_start_multiplex_in(int fd)
606 multiplex_in_fd = fd;
608 io_multiplexing_in = 1;
611 /* write an message to the multiplexed error stream */
612 int io_multiplex_write(enum logcode code, char *buf, int len)
614 if (!io_multiplexing_out) return 0;
617 stats.total_written += (len+4);
618 mplex_write(multiplex_out_fd, code, buf, len);
622 /* write a message to the special error fd */
623 int io_error_write(int f, enum logcode code, char *buf, int len)
625 if (f == -1) return 0;
626 mplex_write(f, code, buf, len);
630 /* stop output multiplexing */
631 void io_multiplexing_close(void)
633 io_multiplexing_out = 0;