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 void setup_readbuffer(int f_in)
48 static void check_timeout(void)
52 if (!io_timeout) return;
61 if (last_io && io_timeout && (t-last_io) >= io_timeout) {
62 rprintf(FERROR,"io timeout after %d second - exiting\n",
64 exit_cleanup(RERR_TIMEOUT);
68 /* setup the fd used to propogate errors */
69 void io_set_error_fd(int fd)
74 /* read some data from the error fd and write it to FERROR */
75 static void read_error_fd(void)
82 n = read(fd, buf, sizeof(buf)-1);
84 rwrite(FERROR, buf, n);
93 /* read from a socket with IO timeout. return the number of
94 bytes read. If no bytes can be read then exit, never return
96 static int read_timeout(int fd, char *buf, int len)
109 if (io_error_fd != -1) {
110 FD_SET(io_error_fd, &fds);
111 if (io_error_fd > fd) fd_count = io_error_fd+1;
114 tv.tv_sec = io_timeout?io_timeout:SELECT_TIMEOUT;
119 if (select(fd_count, &fds, NULL, NULL, &tv) < 1) {
120 if (errno == EBADF) {
121 exit_cleanup(RERR_SOCKETIO);
127 if (io_error_fd != -1 && FD_ISSET(io_error_fd, &fds)) {
131 if (!FD_ISSET(fd, &fds)) continue;
133 n = read(fd, buf, len);
140 last_io = time(NULL);
144 if (n == -1 && errno == EINTR) {
151 rprintf(FERROR,"unexpected EOF in read_timeout\n");
153 exit_cleanup(RERR_STREAMIO);
156 /* this prevents us trying to write errors on a dead socket */
157 io_multiplexing_close();
159 rprintf(FERROR,"read error: %s\n", strerror(errno));
160 exit_cleanup(RERR_STREAMIO);
166 /* continue trying to read len bytes - don't return until len
168 static void read_loop(int fd, char *buf, int len)
171 int n = read_timeout(fd, buf, len);
178 /* read from the file descriptor handling multiplexing -
179 return number of bytes read
181 static int read_unbuffered(int fd, char *buf, int len)
183 static int remaining;
188 if (!io_multiplexing_in || fd != multiplex_in_fd)
189 return read_timeout(fd, buf, len);
193 len = MIN(len, remaining);
194 read_loop(fd, buf, len);
200 read_loop(fd, ibuf, 4);
203 remaining = tag & 0xFFFFFF;
206 if (tag == MPLEX_BASE) continue;
210 if (tag != FERROR && tag != FINFO) {
211 rprintf(FERROR,"unexpected tag %d\n", tag);
212 exit_cleanup(RERR_STREAMIO);
215 if (remaining > sizeof(line)-1) {
216 rprintf(FERROR,"multiplexing overflow %d\n\n",
218 exit_cleanup(RERR_STREAMIO);
221 read_loop(fd, line, remaining);
224 rprintf(tag,"%s", line);
232 /* do a buffered read from fd. don't return until all N bytes
233 have been read. If all N can't be read then exit with an error */
234 static void readfd(int fd,char *buffer,int N)
242 ret = read_unbuffered(fd,buffer + total,N-total);
246 stats.total_read += total;
250 int32 read_int(int f)
257 if (ret == (int32)0xffffffff) return -1;
261 int64 read_longint(int f)
263 extern int remote_version;
268 if ((int32)ret != (int32)0xffffffff) {
273 rprintf(FERROR,"Integer overflow - attempted 64 bit offset\n");
274 exit_cleanup(RERR_UNSUPPORTED);
276 if (remote_version >= 16) {
278 ret = IVAL(b,0) | (((int64)IVAL(b,4))<<32);
285 void read_buf(int f,char *buf,int len)
290 void read_sbuf(int f,char *buf,int len)
296 unsigned char read_byte(int f)
299 read_buf(f,(char *)&c,1);
305 /* write len bytes to fd, possibly reading from buffer_f_in if set
306 in order to unclog the pipe. don't return until all len
307 bytes have been written */
308 static void writefd_unbuffered(int fd,char *buf,int len)
317 while (total < len) {
323 if (io_error_fd != -1) {
324 FD_SET(io_error_fd,&r_fds);
325 if (io_error_fd > fd_count)
326 fd_count = io_error_fd;
329 tv.tv_sec = io_timeout?io_timeout:SELECT_TIMEOUT;
334 count = select(fd_count+1,
335 io_error_fd != -1?&r_fds:NULL,
340 if (errno == EBADF) {
341 exit_cleanup(RERR_SOCKETIO);
347 if (io_error_fd != -1 && FD_ISSET(io_error_fd, &r_fds)) {
351 if (FD_ISSET(fd, &w_fds)) {
352 int ret, n = len-total;
354 if (n > PIPE_BUF) n = PIPE_BUF;
356 ret = write(fd,buf+total,n?n:1);
358 if (ret == -1 && errno == EINTR) {
363 rprintf(FERROR,"erroring writing %d bytes - exiting\n", len);
364 exit_cleanup(RERR_STREAMIO);
370 last_io = time(NULL);
378 static char *io_buffer;
379 static int io_buffer_count;
381 void io_start_buffering(int fd)
383 if (io_buffer) return;
384 multiplex_out_fd = fd;
385 io_buffer = (char *)malloc(IO_BUFFER_SIZE+4);
386 if (!io_buffer) out_of_memory("writefd");
389 /* leave room for the multiplex header in case it's needed */
395 int fd = multiplex_out_fd;
396 if (!io_buffer_count || no_flush) return;
398 if (io_multiplexing_out) {
399 SIVAL(io_buffer-4, 0, (MPLEX_BASE<<24) + io_buffer_count);
400 writefd_unbuffered(fd, io_buffer-4, io_buffer_count+4);
402 writefd_unbuffered(fd, io_buffer, io_buffer_count);
407 void io_end_buffering(int fd)
410 if (!io_multiplexing_out) {
416 static void writefd(int fd,char *buf,int len)
418 stats.total_written += len;
420 if (!io_buffer || fd != multiplex_out_fd) {
421 writefd_unbuffered(fd, buf, len);
426 int n = MIN(len, IO_BUFFER_SIZE-io_buffer_count);
428 memcpy(io_buffer+io_buffer_count, buf, n);
431 io_buffer_count += n;
434 if (io_buffer_count == IO_BUFFER_SIZE) io_flush();
439 void write_int(int f,int32 x)
446 void write_longint(int f, int64 x)
448 extern int remote_version;
451 if (remote_version < 16 || x <= 0x7FFFFFFF) {
452 write_int(f, (int)x);
456 write_int(f, (int32)0xFFFFFFFF);
457 SIVAL(b,0,(x&0xFFFFFFFF));
458 SIVAL(b,4,((x>>32)&0xFFFFFFFF));
463 void write_buf(int f,char *buf,int len)
468 /* write a string to the connection */
469 static void write_sbuf(int f,char *buf)
471 write_buf(f, buf, strlen(buf));
475 void write_byte(int f,unsigned char c)
477 write_buf(f,(char *)&c,1);
480 int read_line(int f, char *buf, int maxlen)
487 if (buf[0] == 0) return 0;
488 if (buf[0] == '\n') {
492 if (buf[0] != '\r') {
508 void io_printf(int fd, const char *format, ...)
514 va_start(ap, format);
515 len = vslprintf(buf, sizeof(buf), format, ap);
518 if (len < 0) exit_cleanup(RERR_STREAMIO);
524 /* setup for multiplexing an error stream with the data stream */
525 void io_start_multiplex_out(int fd)
527 multiplex_out_fd = fd;
529 io_start_buffering(fd);
530 io_multiplexing_out = 1;
533 /* setup for multiplexing an error stream with the data stream */
534 void io_start_multiplex_in(int fd)
536 multiplex_in_fd = fd;
538 io_multiplexing_in = 1;
541 /* write an message to the multiplexed error stream */
542 int io_multiplex_write(int f, char *buf, int len)
544 if (!io_multiplexing_out) return 0;
548 SIVAL(io_buffer-4, 0, ((MPLEX_BASE + f)<<24) + len);
549 memcpy(io_buffer, buf, len);
551 stats.total_written += (len+4);
553 writefd_unbuffered(multiplex_out_fd, io_buffer-4, len+4);
557 /* write a message to the special error fd */
558 int io_error_write(int f, char *buf, int len)
560 if (f == -1) return 0;
561 writefd_unbuffered(f, buf, len);
565 /* stop output multiplexing */
566 void io_multiplexing_close(void)
568 io_multiplexing_out = 0;
571 void io_close_input(int fd)