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
30 static int io_multiplexing_out;
31 static int io_multiplexing_in;
32 static int multiplex_in_fd;
33 static int multiplex_out_fd;
34 static time_t last_io;
35 static int eof_error=1;
37 extern int io_timeout;
38 extern struct stats stats;
40 static int buffer_f_in = -1;
41 static int io_error_fd = -1;
43 static void read_loop(int fd, char *buf, int len);
45 void setup_readbuffer(int f_in)
50 static void check_timeout(void)
54 if (!io_timeout) return;
63 if (last_io && io_timeout && (t-last_io) >= io_timeout) {
64 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) {
165 rprintf(FERROR,"unexpected EOF in read_timeout\n");
167 exit_cleanup(RERR_STREAMIO);
170 /* this prevents us trying to write errors on a dead socket */
171 io_multiplexing_close();
173 rprintf(FERROR,"read error: %s\n", strerror(errno));
174 exit_cleanup(RERR_STREAMIO);
180 /* continue trying to read len bytes - don't return until len
182 static void read_loop(int fd, char *buf, int len)
185 int n = read_timeout(fd, buf, len);
192 /* read from the file descriptor handling multiplexing -
193 return number of bytes read
195 static int read_unbuffered(int fd, char *buf, int len)
197 static int remaining;
201 if (!io_multiplexing_in || fd != multiplex_in_fd)
202 return read_timeout(fd, buf, len);
206 len = MIN(len, remaining);
207 read_loop(fd, buf, len);
213 read_loop(fd, line, 4);
216 remaining = tag & 0xFFFFFF;
219 if (tag == MPLEX_BASE) continue;
223 if (tag != FERROR && tag != FINFO) {
224 rprintf(FERROR,"unexpected tag %d\n", tag);
225 exit_cleanup(RERR_STREAMIO);
228 if (remaining > sizeof(line)-1) {
229 rprintf(FERROR,"multiplexing overflow %d\n\n",
231 exit_cleanup(RERR_STREAMIO);
234 read_loop(fd, line, remaining);
237 rprintf((enum logcode)tag,"%s", line);
245 /* do a buffered read from fd. don't return until all N bytes
246 have been read. If all N can't be read then exit with an error */
247 static void readfd(int fd,char *buffer,int N)
255 ret = read_unbuffered(fd,buffer + total,N-total);
259 stats.total_read += total;
263 int32 read_int(int f)
270 if (ret == (int32)0xffffffff) return -1;
274 int64 read_longint(int f)
276 extern int remote_version;
281 if ((int32)ret != (int32)0xffffffff) {
286 rprintf(FERROR,"Integer overflow - attempted 64 bit offset\n");
287 exit_cleanup(RERR_UNSUPPORTED);
289 if (remote_version >= 16) {
291 ret = IVAL(b,0) | (((int64)IVAL(b,4))<<32);
298 void read_buf(int f,char *buf,int len)
303 void read_sbuf(int f,char *buf,int len)
309 unsigned char read_byte(int f)
312 read_buf(f,(char *)&c,1);
318 /* write len bytes to fd, possibly reading from buffer_f_in if set
319 in order to unclog the pipe. don't return until all len
320 bytes have been written */
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 if (n > PIPE_BUF) n = PIPE_BUF;
369 ret = write(fd,buf+total,n?n:1);
371 if (ret == -1 && errno == EINTR) {
376 rprintf(FERROR,"erroring writing %d bytes - exiting\n", len);
377 exit_cleanup(RERR_STREAMIO);
383 last_io = time(NULL);
391 static char *io_buffer;
392 static int io_buffer_count;
394 void io_start_buffering(int fd)
396 if (io_buffer) return;
397 multiplex_out_fd = fd;
398 io_buffer = (char *)malloc(IO_BUFFER_SIZE);
399 if (!io_buffer) out_of_memory("writefd");
403 /* write an message to a multiplexed stream. If this fails then rsync
405 static void mplex_write(int fd, enum logcode code, char *buf, int len)
410 SIVAL(buffer, 0, ((MPLEX_BASE + (int)code)<<24) + len);
412 if (n > (sizeof(buf)-4)) {
416 memcpy(&buffer[4], buf, n);
417 writefd_unbuffered(fd, buffer, n+4);
422 writefd_unbuffered(fd, buf, len);
428 int fd = multiplex_out_fd;
429 if (!io_buffer_count || no_flush) return;
431 if (io_multiplexing_out) {
432 mplex_write(fd, 0, io_buffer, io_buffer_count);
434 writefd_unbuffered(fd, io_buffer, io_buffer_count);
439 void io_end_buffering(int fd)
442 if (!io_multiplexing_out) {
448 static void writefd(int fd,char *buf,int len)
450 stats.total_written += len;
452 if (!io_buffer || fd != multiplex_out_fd) {
453 writefd_unbuffered(fd, buf, len);
458 int n = MIN(len, IO_BUFFER_SIZE-io_buffer_count);
460 memcpy(io_buffer+io_buffer_count, buf, n);
463 io_buffer_count += n;
466 if (io_buffer_count == IO_BUFFER_SIZE) io_flush();
471 void write_int(int f,int32 x)
478 void write_longint(int f, int64 x)
480 extern int remote_version;
483 if (remote_version < 16 || x <= 0x7FFFFFFF) {
484 write_int(f, (int)x);
488 write_int(f, (int32)0xFFFFFFFF);
489 SIVAL(b,0,(x&0xFFFFFFFF));
490 SIVAL(b,4,((x>>32)&0xFFFFFFFF));
495 void write_buf(int f,char *buf,int len)
500 /* write a string to the connection */
501 static void write_sbuf(int f,char *buf)
503 write_buf(f, buf, strlen(buf));
507 void write_byte(int f,unsigned char c)
509 write_buf(f,(char *)&c,1);
512 int read_line(int f, char *buf, int maxlen)
519 if (buf[0] == 0) return 0;
520 if (buf[0] == '\n') {
524 if (buf[0] != '\r') {
540 void io_printf(int fd, const char *format, ...)
546 va_start(ap, format);
547 len = vslprintf(buf, sizeof(buf), format, ap);
550 if (len < 0) exit_cleanup(RERR_STREAMIO);
556 /* setup for multiplexing an error stream with the data stream */
557 void io_start_multiplex_out(int fd)
559 multiplex_out_fd = fd;
561 io_start_buffering(fd);
562 io_multiplexing_out = 1;
565 /* setup for multiplexing an error stream with the data stream */
566 void io_start_multiplex_in(int fd)
568 multiplex_in_fd = fd;
570 io_multiplexing_in = 1;
573 /* write an message to the multiplexed error stream */
574 int io_multiplex_write(enum logcode code, char *buf, int len)
576 if (!io_multiplexing_out) return 0;
579 stats.total_written += (len+4);
580 mplex_write(multiplex_out_fd, code, buf, len);
584 /* write a message to the special error fd */
585 int io_error_write(int f, enum logcode code, char *buf, int len)
587 if (f == -1) return 0;
588 mplex_write(f, code, buf, len);
592 /* stop output multiplexing */
593 void io_multiplexing_close(void)
595 io_multiplexing_out = 0;
598 void io_close_input(int fd)