1 /* -*- c-file-style: "linux" -*-
3 Copyright (C) 1996-2001 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
;
54 if (!io_timeout
) return;
63 if (last_io
&& io_timeout
&& (t
-last_io
) >= io_timeout
) {
64 if (!am_server
&& !am_daemon
) {
65 rprintf(FERROR
,"io timeout after %d seconds - exiting\n",
68 exit_cleanup(RERR_TIMEOUT
);
72 /* setup the fd used to propogate errors */
73 void io_set_error_fd(int fd
)
78 /* read some data from the error fd and write it to the write log code */
79 static void read_error_fd(void)
86 /* io_error_fd is temporarily disabled -- is this meant to
87 * prevent indefinite recursion? */
90 read_loop(fd
, buf
, 4);
99 if (n
> (sizeof(buf
)-1)) n
= sizeof(buf
)-1;
100 read_loop(fd
, buf
, n
);
101 rwrite((enum logcode
)tag
, buf
, n
);
112 * Read from a socket with IO timeout. return the number of bytes
113 * read. If no bytes can be read then exit, never return a number <= 0.
115 * TODO: If the remote shell connection fails, then current versions
116 * actually report an "unexpected EOF" error here. Since it's a
117 * fairly common mistake to try to use rsh when ssh is required, we
118 * should trap that: if we fail to read any data at all, we should
119 * give a better explanation. We can tell whether the connection has
120 * started by looking e.g. at whether the remote version is known yet.
122 static int read_timeout(int fd
, char *buf
, int len
)
135 if (io_error_fd
!= -1) {
136 FD_SET(io_error_fd
, &fds
);
137 if (io_error_fd
> fd
) fd_count
= io_error_fd
+1;
140 tv
.tv_sec
= io_timeout
?io_timeout
:SELECT_TIMEOUT
;
145 if (select(fd_count
, &fds
, NULL
, NULL
, &tv
) < 1) {
146 if (errno
== EBADF
) {
147 exit_cleanup(RERR_SOCKETIO
);
153 if (io_error_fd
!= -1 && FD_ISSET(io_error_fd
, &fds
)) {
157 if (!FD_ISSET(fd
, &fds
)) continue;
159 n
= read(fd
, buf
, len
);
166 last_io
= time(NULL
);
170 if (n
== -1 && errno
== EINTR
) {
175 (errno
== EWOULDBLOCK
|| errno
== EAGAIN
)) {
183 "%s: connection to server unexpectedly closed"
184 " (%.0f bytes read so far)\n",
185 RSYNC_NAME
, (double)stats
.total_read
);
187 exit_cleanup(RERR_STREAMIO
);
190 /* this prevents us trying to write errors on a dead socket */
191 io_multiplexing_close();
193 rprintf(FERROR
,"read error: %s\n", strerror(errno
));
194 exit_cleanup(RERR_STREAMIO
);
200 /* continue trying to read len bytes - don't return until len
202 static void read_loop(int fd
, char *buf
, int len
)
205 int n
= read_timeout(fd
, buf
, len
);
212 /* read from the file descriptor handling multiplexing -
213 return number of bytes read
215 static int read_unbuffered(int fd
, char *buf
, int len
)
217 static int remaining
;
221 if (!io_multiplexing_in
|| fd
!= multiplex_in_fd
)
222 return read_timeout(fd
, buf
, len
);
226 len
= MIN(len
, remaining
);
227 read_loop(fd
, buf
, len
);
233 read_loop(fd
, line
, 4);
236 remaining
= tag
& 0xFFFFFF;
239 if (tag
== MPLEX_BASE
) continue;
243 if (tag
!= FERROR
&& tag
!= FINFO
) {
244 rprintf(FERROR
,"unexpected tag %d\n", tag
);
245 exit_cleanup(RERR_STREAMIO
);
248 if (remaining
> sizeof(line
)-1) {
249 rprintf(FERROR
,"multiplexing overflow %d\n\n",
251 exit_cleanup(RERR_STREAMIO
);
254 read_loop(fd
, line
, remaining
);
257 rprintf((enum logcode
)tag
,"%s", line
);
265 /* do a buffered read from fd. don't return until all N bytes
266 have been read. If all N can't be read then exit with an error */
267 static void readfd(int fd
,char *buffer
,int N
)
275 ret
= read_unbuffered(fd
,buffer
+ total
,N
-total
);
279 stats
.total_read
+= total
;
283 int32
read_int(int f
)
290 if (ret
== (int32
)0xffffffff) return -1;
294 int64
read_longint(int f
)
296 extern int remote_version
;
301 if ((int32
)ret
!= (int32
)0xffffffff) {
306 rprintf(FERROR
,"Integer overflow - attempted 64 bit offset\n");
307 exit_cleanup(RERR_UNSUPPORTED
);
309 if (remote_version
>= 16) {
311 ret
= IVAL(b
,0) | (((int64
)IVAL(b
,4))<<32);
318 void read_buf(int f
,char *buf
,int len
)
323 void read_sbuf(int f
,char *buf
,int len
)
329 unsigned char read_byte(int f
)
332 read_buf(f
,(char *)&c
,1);
336 /* write len bytes to fd */
337 static void writefd_unbuffered(int fd
,char *buf
,int len
)
348 while (total
< len
) {
354 if (io_error_fd
!= -1) {
355 FD_SET(io_error_fd
,&r_fds
);
356 if (io_error_fd
> fd_count
)
357 fd_count
= io_error_fd
;
360 tv
.tv_sec
= io_timeout
?io_timeout
:SELECT_TIMEOUT
;
365 count
= select(fd_count
+1,
366 io_error_fd
!= -1?&r_fds
:NULL
,
371 if (errno
== EBADF
) {
372 exit_cleanup(RERR_SOCKETIO
);
378 if (io_error_fd
!= -1 && FD_ISSET(io_error_fd
, &r_fds
)) {
382 if (FD_ISSET(fd
, &w_fds
)) {
383 int ret
, n
= len
-total
;
384 ret
= write(fd
,buf
+total
,n
);
386 if (ret
== -1 && errno
== EINTR
) {
391 (errno
== EWOULDBLOCK
|| errno
== EAGAIN
)) {
398 "error writing %d unbuffered bytes"
399 " - exiting: %s\n", len
,
401 exit_cleanup(RERR_STREAMIO
);
404 /* Sleep after writing to limit I/O bandwidth */
408 tv
.tv_usec
= ret
* 1000 / bwlimit
;
409 while (tv
.tv_usec
> 1000000)
412 tv
.tv_usec
-= 1000000;
414 select(0, NULL
, NULL
, NULL
, &tv
);
420 last_io
= time(NULL
);
428 static char *io_buffer
;
429 static int io_buffer_count
;
431 void io_start_buffering(int fd
)
433 if (io_buffer
) return;
434 multiplex_out_fd
= fd
;
435 io_buffer
= (char *)malloc(IO_BUFFER_SIZE
);
436 if (!io_buffer
) out_of_memory("writefd");
440 /* write an message to a multiplexed stream. If this fails then rsync
442 static void mplex_write(int fd
, enum logcode code
, char *buf
, int len
)
447 SIVAL(buffer
, 0, ((MPLEX_BASE
+ (int)code
)<<24) + len
);
449 if (n
> (sizeof(buffer
)-4)) {
450 n
= sizeof(buffer
)-4;
453 memcpy(&buffer
[4], buf
, n
);
454 writefd_unbuffered(fd
, buffer
, n
+4);
460 writefd_unbuffered(fd
, buf
, len
);
467 int fd
= multiplex_out_fd
;
471 if (!io_buffer_count
|| no_flush
) return;
473 if (io_multiplexing_out
) {
474 mplex_write(fd
, FNONE
, io_buffer
, io_buffer_count
);
476 writefd_unbuffered(fd
, io_buffer
, io_buffer_count
);
482 /* XXX: fd is ignored, which seems a little strange. */
483 void io_end_buffering(int fd
)
486 if (!io_multiplexing_out
) {
492 static void writefd(int fd
,char *buf
,int len
)
494 stats
.total_written
+= len
;
498 if (!io_buffer
|| fd
!= multiplex_out_fd
) {
499 writefd_unbuffered(fd
, buf
, len
);
504 int n
= MIN(len
, IO_BUFFER_SIZE
-io_buffer_count
);
506 memcpy(io_buffer
+io_buffer_count
, buf
, n
);
509 io_buffer_count
+= n
;
512 if (io_buffer_count
== IO_BUFFER_SIZE
) io_flush();
517 void write_int(int f
,int32 x
)
526 * Note: int64 may actually be a 32-bit type if ./configure couldn't find any
527 * 64-bit types on this platform.
529 void write_longint(int f
, int64 x
)
531 extern int remote_version
;
534 if (remote_version
< 16 || x
<= 0x7FFFFFFF) {
535 write_int(f
, (int)x
);
539 write_int(f
, (int32
)0xFFFFFFFF);
540 SIVAL(b
,0,(x
&0xFFFFFFFF));
541 SIVAL(b
,4,((x
>>32)&0xFFFFFFFF));
546 void write_buf(int f
,char *buf
,int len
)
551 /* write a string to the connection */
552 static void write_sbuf(int f
,char *buf
)
554 write_buf(f
, buf
, strlen(buf
));
558 void write_byte(int f
,unsigned char c
)
560 write_buf(f
,(char *)&c
,1);
563 int read_line(int f
, char *buf
, int maxlen
)
570 if (buf
[0] == 0) return 0;
571 if (buf
[0] == '\n') {
575 if (buf
[0] != '\r') {
591 void io_printf(int fd
, const char *format
, ...)
597 va_start(ap
, format
);
598 len
= vsnprintf(buf
, sizeof(buf
), format
, ap
);
601 if (len
< 0) exit_cleanup(RERR_STREAMIO
);
607 /* setup for multiplexing an error stream with the data stream */
608 void io_start_multiplex_out(int fd
)
610 multiplex_out_fd
= fd
;
612 io_start_buffering(fd
);
613 io_multiplexing_out
= 1;
616 /* setup for multiplexing an error stream with the data stream */
617 void io_start_multiplex_in(int fd
)
619 multiplex_in_fd
= fd
;
621 io_multiplexing_in
= 1;
624 /* write an message to the multiplexed error stream */
625 int io_multiplex_write(enum logcode code
, char *buf
, int len
)
627 if (!io_multiplexing_out
) return 0;
630 stats
.total_written
+= (len
+4);
631 mplex_write(multiplex_out_fd
, code
, buf
, len
);
635 /* stop output multiplexing */
636 void io_multiplexing_close(void)
638 io_multiplexing_out
= 0;