1 /* -*- c-file-style: "linux" -*-
3 * Copyright (C) 1996-2001 by Andrew Tridgell
4 * Copyright (C) Paul Mackerras 1996
5 * Copyright (C) 2001, 2002 by Martin Pool <mbp@samba.org>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25 * Socket and pipe IO utilities used in rsync.
27 * rsync provides its own multiplexing system, which is used to send
28 * stderr and stdout over a single socket. We need this because
29 * stdout normally carries the binary data stream, and stderr all our
32 * For historical reasons this is off during the start of the
33 * connection, but it's switched on quite early using
34 * io_start_multiplex_out() and io_start_multiplex_in().
39 /** If no timeout is specified then use a 60 second select timeout */
40 #define SELECT_TIMEOUT 60
42 static int io_multiplexing_out
;
43 static int io_multiplexing_in
;
44 static int multiplex_in_fd
;
45 static int multiplex_out_fd
;
46 static time_t last_io
;
51 extern int io_timeout
;
52 extern struct stats stats
;
55 const char phase_unknown
[] = "unknown";
58 * The connection might be dropped at some point; perhaps because the
59 * remote instance crashed. Just giving the offset on the stream is
60 * not very helpful. So instead we try to make io_phase_name point to
63 * For buffered/multiplexed IO these names will be somewhat
64 * approximate; perhaps for ease of support we would rather make the
65 * buffer always flush when a single application-level IO finishes.
67 * @todo Perhaps we want some simple stack functionality, but there's
68 * no need to overdo it.
70 const char *io_write_phase
= phase_unknown
;
71 const char *io_read_phase
= phase_unknown
;
73 /** Ignore EOF errors while reading a module listing if the remote
74 version is 24 or less. */
75 int kludge_around_eof
= False
;
78 static int io_error_fd
= -1;
80 static void read_loop(int fd
, char *buf
, size_t len
);
82 static void check_timeout(void)
84 extern int am_server
, am_daemon
;
89 if (!io_timeout
) return;
98 if (last_io
&& io_timeout
&& (t
-last_io
) >= io_timeout
) {
99 if (!am_server
&& !am_daemon
) {
100 rprintf(FERROR
,"io timeout after %d seconds - exiting\n",
103 exit_cleanup(RERR_TIMEOUT
);
107 /** Setup the fd used to propagate errors */
108 void io_set_error_fd(int fd
)
113 /** Read some data from the error fd and write it to the write log code */
114 static void read_error_fd(void)
118 int fd
= io_error_fd
;
121 /* io_error_fd is temporarily disabled -- is this meant to
122 * prevent indefinite recursion? */
125 read_loop(fd
, buf
, 4);
128 len
= tag
& 0xFFFFFF;
134 if (n
> (sizeof(buf
)-1))
136 read_loop(fd
, buf
, n
);
137 rwrite((enum logcode
)tag
, buf
, n
);
146 * It's almost always an error to get an EOF when we're trying to read
147 * from the network, because the protocol is self-terminating.
149 * However, there is one unfortunate cases where it is not, which is
150 * rsync <2.4.6 sending a list of modules on a server, since the list
151 * is terminated by closing the socket. So, for the section of the
152 * program where that is a problem (start_socket_client),
153 * kludge_around_eof is True and we just exit.
155 static void whine_about_eof (void)
157 if (kludge_around_eof
)
161 "%s: connection unexpectedly closed "
162 "(%.0f bytes read so far)\n",
163 RSYNC_NAME
, (double)stats
.total_read
);
165 exit_cleanup (RERR_STREAMIO
);
170 static void die_from_readerr (int err
)
172 /* this prevents us trying to write errors on a dead socket */
173 io_multiplexing_close();
175 rprintf(FERROR
, "%s: read error: %s\n",
176 RSYNC_NAME
, strerror (err
));
177 exit_cleanup(RERR_STREAMIO
);
182 * Read from a socket with IO timeout. return the number of bytes
183 * read. If no bytes can be read then exit, never return a number <= 0.
185 * TODO: If the remote shell connection fails, then current versions
186 * actually report an "unexpected EOF" error here. Since it's a
187 * fairly common mistake to try to use rsh when ssh is required, we
188 * should trap that: if we fail to read any data at all, we should
189 * give a better explanation. We can tell whether the connection has
190 * started by looking e.g. at whether the remote version is known yet.
192 static int read_timeout (int fd
, char *buf
, size_t len
)
199 /* until we manage to read *something* */
207 if (io_error_fd
!= -1) {
208 FD_SET(io_error_fd
, &fds
);
209 if (io_error_fd
> fd
) fd_count
= io_error_fd
+1;
212 tv
.tv_sec
= io_timeout
?io_timeout
:SELECT_TIMEOUT
;
217 count
= select(fd_count
, &fds
, NULL
, NULL
, &tv
);
224 if (errno
== EBADF
) {
225 exit_cleanup(RERR_SOCKETIO
);
230 if (io_error_fd
!= -1 && FD_ISSET(io_error_fd
, &fds
)) {
234 if (!FD_ISSET(fd
, &fds
)) continue;
236 n
= read(fd
, buf
, len
);
243 last_io
= time(NULL
);
247 return -1; /* doesn't return */
248 } else if (n
== -1) {
249 if (errno
== EINTR
|| errno
== EWOULDBLOCK
||
253 die_from_readerr (errno
);
264 * Continue trying to read len bytes - don't return until len has been
267 static void read_loop (int fd
, char *buf
, size_t len
)
270 int n
= read_timeout(fd
, buf
, len
);
279 * Read from the file descriptor handling multiplexing - return number
282 * Never returns <= 0.
284 static int read_unbuffered(int fd
, char *buf
, size_t len
)
286 static size_t remaining
;
290 if (!io_multiplexing_in
|| fd
!= multiplex_in_fd
)
291 return read_timeout(fd
, buf
, len
);
295 len
= MIN(len
, remaining
);
296 read_loop(fd
, buf
, len
);
302 read_loop(fd
, line
, 4);
305 remaining
= tag
& 0xFFFFFF;
308 if (tag
== MPLEX_BASE
)
313 if (tag
!= FERROR
&& tag
!= FINFO
) {
314 rprintf(FERROR
, "unexpected tag %d\n", tag
);
315 exit_cleanup(RERR_STREAMIO
);
318 if (remaining
> sizeof(line
) - 1) {
319 rprintf(FERROR
, "multiplexing overflow %d\n\n",
321 exit_cleanup(RERR_STREAMIO
);
324 read_loop(fd
, line
, remaining
);
327 rprintf((enum logcode
) tag
, "%s", line
);
337 * Do a buffered read from @p fd. Don't return until all @p n bytes
338 * have been read. If all @p n can't be read then exit with an
341 static void readfd (int fd
, char *buffer
, size_t N
)
349 ret
= read_unbuffered (fd
, buffer
+ total
, N
-total
);
353 stats
.total_read
+= total
;
357 int32
read_int(int f
)
364 if (ret
== (int32
)0xffffffff) return -1;
368 int64
read_longint(int f
)
370 extern int remote_version
;
375 if ((int32
)ret
!= (int32
)0xffffffff) {
380 rprintf(FERROR
,"Integer overflow - attempted 64 bit offset\n");
381 exit_cleanup(RERR_UNSUPPORTED
);
383 if (remote_version
>= 16) {
385 ret
= IVAL(b
,0) | (((int64
)IVAL(b
,4))<<32);
392 void read_buf(int f
,char *buf
,size_t len
)
397 void read_sbuf(int f
,char *buf
,size_t len
)
399 read_buf (f
,buf
,len
);
403 unsigned char read_byte(int f
)
406 read_buf (f
, (char *)&c
, 1);
412 * Sleep after writing to limit I/O bandwidth usage.
414 * @todo Rather than sleeping after each write, it might be better to
415 * use some kind of averaging. The current algorithm seems to always
416 * use a bit less bandwidth than specified, because it doesn't make up
417 * for slow periods. But arguably this is a feature. In addition, we
418 * ought to take the time used to write the data into account.
420 static void sleep_for_bwlimit(int bytes_written
)
427 assert(bytes_written
> 0);
430 tv
.tv_usec
= bytes_written
* 1000 / bwlimit
;
431 tv
.tv_sec
= tv
.tv_usec
/ 1000000;
432 tv
.tv_usec
= tv
.tv_usec
% 1000000;
434 select(0, NULL
, NULL
, NULL
, &tv
);
439 * Write len bytes to the file descriptor @p fd.
441 * This function underlies the multiplexing system. The body of the
442 * application never calls this function directly.
444 static void writefd_unbuffered(int fd
,char *buf
,size_t len
)
455 while (total
< len
) {
461 if (io_error_fd
!= -1) {
462 FD_SET(io_error_fd
,&r_fds
);
463 if (io_error_fd
> fd_count
)
464 fd_count
= io_error_fd
;
467 tv
.tv_sec
= io_timeout
?io_timeout
:SELECT_TIMEOUT
;
472 count
= select(fd_count
+1,
473 io_error_fd
!= -1?&r_fds
:NULL
,
482 if (errno
== EBADF
) {
483 exit_cleanup(RERR_SOCKETIO
);
488 if (io_error_fd
!= -1 && FD_ISSET(io_error_fd
, &r_fds
)) {
492 if (FD_ISSET(fd
, &w_fds
)) {
494 size_t n
= len
-total
;
495 ret
= write(fd
,buf
+total
,n
);
497 if (ret
== -1 && errno
== EINTR
) {
502 (errno
== EWOULDBLOCK
|| errno
== EAGAIN
)) {
508 /* Don't try to write errors back
509 * across the stream */
510 io_multiplexing_close();
511 rprintf(FERROR
, RSYNC_NAME
512 ": writefd_unbuffered failed to write %ld bytes: phase \"%s\": %s\n",
513 (long) len
, io_write_phase
,
515 exit_cleanup(RERR_STREAMIO
);
518 sleep_for_bwlimit(ret
);
523 last_io
= time(NULL
);
531 static char *io_buffer
;
532 static int io_buffer_count
;
534 void io_start_buffering(int fd
)
536 if (io_buffer
) return;
537 multiplex_out_fd
= fd
;
538 io_buffer
= (char *)malloc(IO_BUFFER_SIZE
);
539 if (!io_buffer
) out_of_memory("writefd");
544 * Write an message to a multiplexed stream. If this fails then rsync
547 static void mplex_write(int fd
, enum logcode code
, char *buf
, size_t len
)
552 SIVAL(buffer
, 0, ((MPLEX_BASE
+ (int)code
)<<24) + len
);
554 if (n
> (sizeof(buffer
)-4)) {
555 n
= sizeof(buffer
)-4;
558 memcpy(&buffer
[4], buf
, n
);
559 writefd_unbuffered(fd
, buffer
, n
+4);
565 writefd_unbuffered(fd
, buf
, len
);
572 int fd
= multiplex_out_fd
;
576 if (!io_buffer_count
|| no_flush
) return;
578 if (io_multiplexing_out
) {
579 mplex_write(fd
, FNONE
, io_buffer
, io_buffer_count
);
581 writefd_unbuffered(fd
, io_buffer
, io_buffer_count
);
587 void io_end_buffering(void)
590 if (!io_multiplexing_out
) {
596 static void writefd(int fd
,char *buf
,size_t len
)
598 stats
.total_written
+= len
;
602 if (!io_buffer
|| fd
!= multiplex_out_fd
) {
603 writefd_unbuffered(fd
, buf
, len
);
608 int n
= MIN((int) len
, IO_BUFFER_SIZE
-io_buffer_count
);
610 memcpy(io_buffer
+io_buffer_count
, buf
, n
);
613 io_buffer_count
+= n
;
616 if (io_buffer_count
== IO_BUFFER_SIZE
) io_flush();
621 void write_int(int f
,int32 x
)
629 void write_int_named(int f
, int32 x
, const char *phase
)
631 io_write_phase
= phase
;
633 io_write_phase
= phase_unknown
;
638 * Note: int64 may actually be a 32-bit type if ./configure couldn't find any
639 * 64-bit types on this platform.
641 void write_longint(int f
, int64 x
)
643 extern int remote_version
;
646 if (remote_version
< 16 || x
<= 0x7FFFFFFF) {
647 write_int(f
, (int)x
);
651 write_int(f
, (int32
)0xFFFFFFFF);
652 SIVAL(b
,0,(x
&0xFFFFFFFF));
653 SIVAL(b
,4,((x
>>32)&0xFFFFFFFF));
658 void write_buf(int f
,char *buf
,size_t len
)
663 /** Write a string to the connection */
664 static void write_sbuf(int f
,char *buf
)
666 write_buf(f
, buf
, strlen(buf
));
670 void write_byte(int f
,unsigned char c
)
672 write_buf(f
,(char *)&c
,1);
678 * Read a line of up to @p maxlen characters into @p buf. Does not
679 * contain a trailing newline or carriage return.
681 * @return 1 for success; 0 for io error or truncation.
683 int read_line(int f
, char *buf
, size_t maxlen
)
690 if (buf
[0] == '\n') {
694 if (buf
[0] != '\r') {
708 void io_printf(int fd
, const char *format
, ...)
714 va_start(ap
, format
);
715 len
= vsnprintf(buf
, sizeof(buf
), format
, ap
);
718 if (len
< 0) exit_cleanup(RERR_STREAMIO
);
724 /** Setup for multiplexing an error stream with the data stream */
725 void io_start_multiplex_out(int fd
)
727 multiplex_out_fd
= fd
;
729 io_start_buffering(fd
);
730 io_multiplexing_out
= 1;
733 /** Setup for multiplexing an error stream with the data stream */
734 void io_start_multiplex_in(int fd
)
736 multiplex_in_fd
= fd
;
738 io_multiplexing_in
= 1;
741 /** Write an message to the multiplexed error stream */
742 int io_multiplex_write(enum logcode code
, char *buf
, size_t len
)
744 if (!io_multiplexing_out
) return 0;
747 stats
.total_written
+= (len
+4);
748 mplex_write(multiplex_out_fd
, code
, buf
, len
);
752 /** Stop output multiplexing */
753 void io_multiplexing_close(void)
755 io_multiplexing_out
= 0;