2 * Socket and pipe I/O utilities used in rsync.
4 * Copyright (C) 1996-2001 Andrew Tridgell
5 * Copyright (C) 1996 Paul Mackerras
6 * Copyright (C) 2001, 2002 Martin Pool <mbp@samba.org>
7 * Copyright (C) 2003-2020 Wayne Davison
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 3 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, visit the http://fsf.org website.
23 /* Rsync provides its own multiplexing system, which is used to send
24 * stderr and stdout over a single socket.
26 * For historical reasons this is off during the start of the
27 * connection, but it's switched on quite early using
28 * io_start_multiplex_out() and io_start_multiplex_in(). */
34 /** If no timeout is specified then use a 60 second select timeout */
35 #define SELECT_TIMEOUT 60
38 extern size_t bwlimit_writemax
;
39 extern int io_timeout
;
42 extern int am_receiver
;
43 extern int am_generator
;
44 extern int msgs2stderr
;
45 extern int inc_recurse
;
50 extern int file_total
;
51 extern int file_old_total
;
53 extern int read_batch
;
54 extern int compat_flags
;
55 extern int protect_args
;
56 extern int checksum_seed
;
57 extern int protocol_version
;
58 extern int remove_source_files
;
59 extern int preserve_hard_links
;
60 extern BOOL extra_flist_sending_enabled
;
61 extern BOOL flush_ok_after_signal
;
62 extern struct stats stats
;
63 extern struct file_list
*cur_flist
;
65 extern int filesfrom_convert
;
66 extern iconv_t ic_send
, ic_recv
;
69 int csum_length
= SHORT_SUM_LENGTH
; /* initial value */
72 int forward_flist_data
= 0;
73 BOOL flist_receiving_enabled
= False
;
75 /* Ignore an EOF error if non-zero. See whine_about_eof(). */
76 int kluge_around_eof
= 0;
77 int got_kill_signal
= -1; /* is set to 0 only after multiplexed I/O starts */
82 int64 total_data_read
= 0;
83 int64 total_data_written
= 0;
88 int out_fd
; /* Both "out" and "msg" go to this fd. */
90 unsigned out_empty_len
;
91 size_t raw_data_header_pos
; /* in the out xbuf */
92 size_t raw_flushing_ends_before
; /* in the out xbuf */
93 size_t raw_input_ends_before
; /* in the in xbuf */
94 } iobuf
= { .in_fd
= -1, .out_fd
= -1 };
96 static time_t last_io_in
;
97 static time_t last_io_out
;
99 static int write_batch_monitor_in
= -1;
100 static int write_batch_monitor_out
= -1;
102 static int ff_forward_fd
= -1;
103 static int ff_reenable_multiplex
= -1;
104 static char ff_lastchar
= '\0';
105 static xbuf ff_xb
= EMPTY_XBUF
;
107 static xbuf iconv_buf
= EMPTY_XBUF
;
109 static int select_timeout
= SELECT_TIMEOUT
;
110 static int active_filecnt
= 0;
111 static OFF_T active_bytecnt
= 0;
112 static int first_message
= 1;
114 static char int_byte_extra
[64] = {
115 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* (00 - 3F)/4 */
116 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* (40 - 7F)/4 */
117 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* (80 - BF)/4 */
118 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 5, 6, /* (C0 - FF)/4 */
121 /* Our I/O buffers are sized with no bits on in the lowest byte of the "size"
122 * (indeed, our rounding of sizes in 1024-byte units assures more than this).
123 * This allows the code that is storing bytes near the physical end of a
124 * circular buffer to temporarily reduce the buffer's size (in order to make
125 * some storing idioms easier), while also making it simple to restore the
126 * buffer's actual size when the buffer's "pos" wraps around to the start (we
127 * just round the buffer's size up again). */
129 #define IOBUF_WAS_REDUCED(siz) ((siz) & 0xFF)
130 #define IOBUF_RESTORE_SIZE(siz) (((siz) | 0xFF) + 1)
132 #define IN_MULTIPLEXED (iobuf.in_multiplexed != 0)
133 #define IN_MULTIPLEXED_AND_READY (iobuf.in_multiplexed > 0)
134 #define OUT_MULTIPLEXED (iobuf.out_empty_len != 0)
136 #define PIO_NEED_INPUT (1<<0) /* The *_NEED_* flags are mutually exclusive. */
137 #define PIO_NEED_OUTROOM (1<<1)
138 #define PIO_NEED_MSGROOM (1<<2)
140 #define PIO_CONSUME_INPUT (1<<4) /* Must becombined with PIO_NEED_INPUT. */
142 #define PIO_INPUT_AND_CONSUME (PIO_NEED_INPUT | PIO_CONSUME_INPUT)
143 #define PIO_NEED_FLAGS (PIO_NEED_INPUT | PIO_NEED_OUTROOM | PIO_NEED_MSGROOM)
145 #define REMOTE_OPTION_ERROR "rsync: on remote machine: -"
146 #define REMOTE_OPTION_ERROR2 ": unknown option"
148 #define FILESFROM_BUFLEN 2048
150 enum festatus
{ FES_SUCCESS
, FES_REDO
, FES_NO_SEND
};
152 static flist_ndx_list redo_list
, hlink_list
;
154 static void read_a_msg(void);
155 static void drain_multiplex_messages(void);
156 static void sleep_for_bwlimit(int bytes_written
);
158 static void check_timeout(BOOL allow_keepalive
, int keepalive_flags
)
162 /* On the receiving side, the generator is now the one that decides
163 * when a timeout has occurred. When it is sifting through a lot of
164 * files looking for work, it will be sending keep-alive messages to
165 * the sender, and even though the receiver won't be sending/receiving
166 * anything (not even keep-alive messages), the successful writes to
167 * the sender will keep things going. If the receiver is actively
168 * receiving data, it will ensure that the generator knows that it is
169 * not idle by sending the generator keep-alive messages (since the
170 * generator might be blocked trying to send checksums, it needs to
171 * know that the receiver is active). Thus, as long as one or the
172 * other is successfully doing work, the generator will not timeout. */
178 if (allow_keepalive
) {
179 /* This may put data into iobuf.msg w/o flushing. */
180 maybe_send_keepalive(t
, keepalive_flags
);
189 chk
= MAX(last_io_out
, last_io_in
);
190 if (t
- chk
>= io_timeout
) {
193 rprintf(FERROR
, "[%s] io timeout after %d seconds -- exiting\n",
194 who_am_i(), (int)(t
-chk
));
195 exit_cleanup(RERR_TIMEOUT
);
199 /* It's almost always an error to get an EOF when we're trying to read from the
200 * network, because the protocol is (for the most part) self-terminating.
202 * There is one case for the receiver when it is at the end of the transfer
203 * (hanging around reading any keep-alive packets that might come its way): if
204 * the sender dies before the generator's kill-signal comes through, we can end
205 * up here needing to loop until the kill-signal arrives. In this situation,
206 * kluge_around_eof will be < 0.
208 * There is another case for older protocol versions (< 24) where the module
209 * listing was not terminated, so we must ignore an EOF error in that case and
210 * exit. In this situation, kluge_around_eof will be > 0. */
211 static NORETURN
void whine_about_eof(BOOL allow_kluge
)
213 if (kluge_around_eof
&& allow_kluge
) {
215 if (kluge_around_eof
> 0)
217 /* If we're still here after 10 seconds, exit with an error. */
218 for (i
= 10*1000/20; i
--; )
222 rprintf(FERROR
, RSYNC_NAME
": connection unexpectedly closed "
223 "(%s bytes received so far) [%s]\n",
224 big_num(stats
.total_read
), who_am_i());
226 exit_cleanup(RERR_STREAMIO
);
229 /* Do a safe read, handling any needed looping and error handling.
230 * Returns the count of the bytes read, which will only be different
231 * from "len" if we encountered an EOF. This routine is not used on
232 * the socket except very early in the transfer. */
233 static size_t safe_read(int fd
, char *buf
, size_t len
)
237 assert(fd
!= iobuf
.in_fd
);
248 tv
.tv_sec
= select_timeout
;
251 cnt
= select(fd
+1, &r_fds
, NULL
, &e_fds
, &tv
);
253 if (cnt
< 0 && errno
== EBADF
) {
254 rsyserr(FERROR
, errno
, "safe_read select failed");
255 exit_cleanup(RERR_FILEIO
);
257 check_timeout(1, MSK_ALLOW_FLUSH
);
261 /*if (FD_ISSET(fd, &e_fds))
262 rprintf(FINFO, "select exception on fd %d\n", fd); */
264 if (FD_ISSET(fd
, &r_fds
)) {
265 int n
= read(fd
, buf
+ got
, len
- got
);
266 if (DEBUG_GTE(IO
, 2))
267 rprintf(FINFO
, "[%s] safe_read(%d)=%ld\n", who_am_i(), fd
, (long)n
);
273 rsyserr(FERROR
, errno
, "safe_read failed to read %ld bytes", (long)len
);
274 exit_cleanup(RERR_STREAMIO
);
276 if ((got
+= (size_t)n
) == len
)
284 static const char *what_fd_is(int fd
)
288 if (fd
== sock_f_out
)
290 else if (fd
== iobuf
.out_fd
)
292 else if (fd
== batch_fd
)
295 snprintf(buf
, sizeof buf
, "fd %d", fd
);
300 /* Do a safe write, handling any needed looping and error handling.
301 * Returns only if everything was successfully written. This routine
302 * is not used on the socket except very early in the transfer. */
303 static void safe_write(int fd
, const char *buf
, size_t len
)
307 assert(fd
!= iobuf
.out_fd
);
309 n
= write(fd
, buf
, len
);
310 if ((size_t)n
== len
)
313 if (errno
!= EINTR
&& errno
!= EWOULDBLOCK
&& errno
!= EAGAIN
) {
315 rsyserr(FERROR
, errno
,
316 "safe_write failed to write %ld bytes to %s",
317 (long)len
, what_fd_is(fd
));
318 exit_cleanup(RERR_STREAMIO
);
332 tv
.tv_sec
= select_timeout
;
335 cnt
= select(fd
+ 1, NULL
, &w_fds
, NULL
, &tv
);
337 if (cnt
< 0 && errno
== EBADF
) {
338 rsyserr(FERROR
, errno
, "safe_write select failed on %s", what_fd_is(fd
));
339 exit_cleanup(RERR_FILEIO
);
342 maybe_send_keepalive(time(NULL
), MSK_ALLOW_FLUSH
);
346 if (FD_ISSET(fd
, &w_fds
)) {
347 n
= write(fd
, buf
, len
);
359 /* This is only called when files-from data is known to be available. We read
360 * a chunk of data and put it into the output buffer. */
361 static void forward_filesfrom_data(void)
365 len
= read(ff_forward_fd
, ff_xb
.buf
+ ff_xb
.len
, ff_xb
.size
- ff_xb
.len
);
367 if (len
== 0 || errno
!= EINTR
) {
368 /* Send end-of-file marker */
370 write_buf(iobuf
.out_fd
, "\0\0", ff_lastchar
? 2 : 1);
372 if (ff_reenable_multiplex
>= 0)
373 io_start_multiplex_out(ff_reenable_multiplex
);
378 if (DEBUG_GTE(IO
, 2))
379 rprintf(FINFO
, "[%s] files-from read=%ld\n", who_am_i(), (long)len
);
386 char *s
= ff_xb
.buf
+ len
;
387 /* Transform CR and/or LF into '\0' */
388 while (s
-- > ff_xb
.buf
) {
389 if (*s
== '\n' || *s
== '\r')
398 /* Last buf ended with a '\0', so don't let this buf start with one. */
399 while (len
&& *s
== '\0')
401 ff_xb
.pos
= s
- ff_xb
.buf
;
405 if (filesfrom_convert
&& len
) {
406 char *sob
= ff_xb
.buf
+ ff_xb
.pos
, *s
= sob
;
407 char *eob
= sob
+ len
;
408 int flags
= ICB_INCLUDE_BAD
| ICB_INCLUDE_INCOMPLETE
| ICB_CIRCULAR_OUT
;
409 if (ff_lastchar
== '\0')
411 /* Convert/send each null-terminated string separately, skipping empties. */
414 ff_xb
.len
= s
- sob
- 1;
415 if (iconvbufs(ic_send
, &ff_xb
, &iobuf
.out
, flags
) < 0)
416 exit_cleanup(RERR_PROTOCOL
); /* impossible? */
417 write_buf(iobuf
.out_fd
, s
-1, 1); /* Send the '\0'. */
418 while (s
!= eob
&& *s
== '\0')
421 ff_xb
.pos
= sob
- ff_xb
.buf
;
426 if ((ff_xb
.len
= s
- sob
) == 0)
429 /* Handle a partial string specially, saving any incomplete chars. */
430 flags
&= ~ICB_INCLUDE_INCOMPLETE
;
431 if (iconvbufs(ic_send
, &ff_xb
, &iobuf
.out
, flags
) < 0) {
433 exit_cleanup(RERR_PROTOCOL
); /* impossible? */
435 memmove(ff_xb
.buf
, ff_xb
.buf
+ ff_xb
.pos
, ff_xb
.len
);
437 ff_lastchar
= 'x'; /* Anything non-zero. */
443 char *f
= ff_xb
.buf
+ ff_xb
.pos
;
446 /* Eliminate any multi-'\0' runs. */
448 if (!(*t
++ = *f
++)) {
449 while (f
!= eob
&& *f
== '\0')
454 if ((len
= t
- ff_xb
.buf
) != 0) {
455 /* This will not circle back to perform_io() because we only get
456 * called when there is plenty of room in the output buffer. */
457 write_buf(iobuf
.out_fd
, ff_xb
.buf
, len
);
462 void reduce_iobuf_size(xbuf
*out
, size_t new_size
)
464 if (new_size
< out
->size
) {
465 /* Avoid weird buffer interactions by only outputting this to stderr. */
466 if (msgs2stderr
&& DEBUG_GTE(IO
, 4)) {
467 const char *name
= out
== &iobuf
.out
? "iobuf.out"
468 : out
== &iobuf
.msg
? "iobuf.msg"
471 rprintf(FINFO
, "[%s] reduced size of %s (-%d)\n",
472 who_am_i(), name
, (int)(out
->size
- new_size
));
475 out
->size
= new_size
;
479 void restore_iobuf_size(xbuf
*out
)
481 if (IOBUF_WAS_REDUCED(out
->size
)) {
482 size_t new_size
= IOBUF_RESTORE_SIZE(out
->size
);
483 /* Avoid weird buffer interactions by only outputting this to stderr. */
484 if (msgs2stderr
&& DEBUG_GTE(IO
, 4)) {
485 const char *name
= out
== &iobuf
.out
? "iobuf.out"
486 : out
== &iobuf
.msg
? "iobuf.msg"
489 rprintf(FINFO
, "[%s] restored size of %s (+%d)\n",
490 who_am_i(), name
, (int)(new_size
- out
->size
));
493 out
->size
= new_size
;
497 static void handle_kill_signal(BOOL flush_ok
)
499 got_kill_signal
= -1;
500 flush_ok_after_signal
= flush_ok
;
501 exit_cleanup(RERR_SIGNAL
);
504 /* Perform buffered input and/or output until specified conditions are met.
505 * When given a "needed" read or write request, this returns without doing any
506 * I/O if the needed input bytes or write space is already available. Once I/O
507 * is needed, this will try to do whatever reading and/or writing is currently
508 * possible, up to the maximum buffer allowances, no matter if this is a read
509 * or write request. However, the I/O stops as soon as the required input
510 * bytes or output space is available. If this is not a read request, the
511 * routine may also do some advantageous reading of messages from a multiplexed
512 * input source (which ensures that we don't jam up with everyone in their
513 * "need to write" code and nobody reading the accumulated data that would make
516 * The iobuf.in, .out and .msg buffers are all circular. Callers need to be
517 * aware that some data copies will need to be split when the bytes wrap around
518 * from the end to the start. In order to help make writing into the output
519 * buffers easier for some operations (such as the use of SIVAL() into the
520 * buffer) a buffer may be temporarily shortened by a small amount, but the
521 * original size will be automatically restored when the .pos wraps to the
522 * start. See also the 3 raw_* iobuf vars that are used in the handling of
523 * MSG_DATA bytes as they are read-from/written-into the buffers.
525 * When writing, we flush data in the following priority order:
527 * 1. Finish writing any in-progress MSG_DATA sequence from iobuf.out.
529 * 2. Write out all the messages from the message buf (if iobuf.msg is active).
530 * Yes, this means that a PIO_NEED_OUTROOM call will completely flush any
531 * messages before getting to the iobuf.out flushing (except for rule 1).
533 * 3. Write out the raw data from iobuf.out, possibly filling in the multiplexed
534 * MSG_DATA header that was pre-allocated (when output is multiplexed).
536 * TODO: items for possible future work:
538 * - Make this routine able to read the generator-to-receiver batch flow?
540 * Unlike the old routines that this replaces, it is OK to read ahead as far as
541 * we can because the read_a_msg() routine now reads its bytes out of the input
542 * buffer. In the old days, only raw data was in the input buffer, and any
543 * unused raw data in the buf would prevent the reading of socket data. */
544 static char *perform_io(size_t needed
, int flags
)
546 fd_set r_fds
, e_fds
, w_fds
;
549 size_t empty_buf_len
= 0;
553 if (iobuf
.in
.len
== 0 && iobuf
.in
.pos
!= 0) {
554 if (iobuf
.raw_input_ends_before
)
555 iobuf
.raw_input_ends_before
-= iobuf
.in
.pos
;
559 switch (flags
& PIO_NEED_FLAGS
) {
561 /* We never resize the circular input buffer. */
562 if (iobuf
.in
.size
< needed
) {
563 rprintf(FERROR
, "need to read %ld bytes, iobuf.in.buf is only %ld bytes.\n",
564 (long)needed
, (long)iobuf
.in
.size
);
565 exit_cleanup(RERR_PROTOCOL
);
568 if (msgs2stderr
&& DEBUG_GTE(IO
, 3)) {
569 rprintf(FINFO
, "[%s] perform_io(%ld, %sinput)\n",
570 who_am_i(), (long)needed
, flags
& PIO_CONSUME_INPUT
? "consume&" : "");
574 case PIO_NEED_OUTROOM
:
575 /* We never resize the circular output buffer. */
576 if (iobuf
.out
.size
- iobuf
.out_empty_len
< needed
) {
577 fprintf(stderr
, "need to write %ld bytes, iobuf.out.buf is only %ld bytes.\n",
578 (long)needed
, (long)(iobuf
.out
.size
- iobuf
.out_empty_len
));
579 exit_cleanup(RERR_PROTOCOL
);
582 if (msgs2stderr
&& DEBUG_GTE(IO
, 3)) {
583 rprintf(FINFO
, "[%s] perform_io(%ld, outroom) needs to flush %ld\n",
584 who_am_i(), (long)needed
,
585 iobuf
.out
.len
+ needed
> iobuf
.out
.size
586 ? (long)(iobuf
.out
.len
+ needed
- iobuf
.out
.size
) : 0L);
590 case PIO_NEED_MSGROOM
:
591 /* We never resize the circular message buffer. */
592 if (iobuf
.msg
.size
< needed
) {
593 fprintf(stderr
, "need to write %ld bytes, iobuf.msg.buf is only %ld bytes.\n",
594 (long)needed
, (long)iobuf
.msg
.size
);
595 exit_cleanup(RERR_PROTOCOL
);
598 if (msgs2stderr
&& DEBUG_GTE(IO
, 3)) {
599 rprintf(FINFO
, "[%s] perform_io(%ld, msgroom) needs to flush %ld\n",
600 who_am_i(), (long)needed
,
601 iobuf
.msg
.len
+ needed
> iobuf
.msg
.size
602 ? (long)(iobuf
.msg
.len
+ needed
- iobuf
.msg
.size
) : 0L);
607 if (msgs2stderr
&& DEBUG_GTE(IO
, 3))
608 rprintf(FINFO
, "[%s] perform_io(%ld, %d)\n", who_am_i(), (long)needed
, flags
);
612 exit_cleanup(RERR_UNSUPPORTED
);
616 switch (flags
& PIO_NEED_FLAGS
) {
618 if (iobuf
.in
.len
>= needed
)
621 case PIO_NEED_OUTROOM
:
622 /* Note that iobuf.out_empty_len doesn't factor into this check
623 * because iobuf.out.len already holds any needed header len. */
624 if (iobuf
.out
.len
+ needed
<= iobuf
.out
.size
)
627 case PIO_NEED_MSGROOM
:
628 if (iobuf
.msg
.len
+ needed
<= iobuf
.msg
.size
)
637 if (iobuf
.in_fd
>= 0 && iobuf
.in
.size
- iobuf
.in
.len
) {
638 if (!read_batch
|| batch_fd
>= 0) {
639 FD_SET(iobuf
.in_fd
, &r_fds
);
640 FD_SET(iobuf
.in_fd
, &e_fds
);
642 if (iobuf
.in_fd
> max_fd
)
643 max_fd
= iobuf
.in_fd
;
646 /* Only do more filesfrom processing if there is enough room in the out buffer. */
647 if (ff_forward_fd
>= 0 && iobuf
.out
.size
- iobuf
.out
.len
> FILESFROM_BUFLEN
*2) {
648 FD_SET(ff_forward_fd
, &r_fds
);
649 if (ff_forward_fd
> max_fd
)
650 max_fd
= ff_forward_fd
;
654 if (iobuf
.out_fd
>= 0) {
655 if (iobuf
.raw_flushing_ends_before
656 || (!iobuf
.msg
.len
&& iobuf
.out
.len
> iobuf
.out_empty_len
&& !(flags
& PIO_NEED_MSGROOM
))) {
657 if (OUT_MULTIPLEXED
&& !iobuf
.raw_flushing_ends_before
) {
658 /* The iobuf.raw_flushing_ends_before value can point off the end
659 * of the iobuf.out buffer for a while, for easier subtracting. */
660 iobuf
.raw_flushing_ends_before
= iobuf
.out
.pos
+ iobuf
.out
.len
;
662 SIVAL(iobuf
.out
.buf
+ iobuf
.raw_data_header_pos
, 0,
663 ((MPLEX_BASE
+ (int)MSG_DATA
)<<24) + iobuf
.out
.len
- 4);
665 if (msgs2stderr
&& DEBUG_GTE(IO
, 1)) {
666 rprintf(FINFO
, "[%s] send_msg(%d, %ld)\n",
667 who_am_i(), (int)MSG_DATA
, (long)iobuf
.out
.len
- 4);
670 /* reserve room for the next MSG_DATA header */
671 iobuf
.raw_data_header_pos
= iobuf
.raw_flushing_ends_before
;
672 if (iobuf
.raw_data_header_pos
>= iobuf
.out
.size
)
673 iobuf
.raw_data_header_pos
-= iobuf
.out
.size
;
674 else if (iobuf
.raw_data_header_pos
+ 4 > iobuf
.out
.size
) {
675 /* The 4-byte header won't fit at the end of the buffer,
676 * so we'll temporarily reduce the output buffer's size
677 * and put the header at the start of the buffer. */
678 reduce_iobuf_size(&iobuf
.out
, iobuf
.raw_data_header_pos
);
679 iobuf
.raw_data_header_pos
= 0;
681 /* Yes, it is possible for this to make len > size for a while. */
685 empty_buf_len
= iobuf
.out_empty_len
;
687 } else if (iobuf
.msg
.len
) {
693 FD_SET(iobuf
.out_fd
, &w_fds
);
694 if (iobuf
.out_fd
> max_fd
)
695 max_fd
= iobuf
.out_fd
;
701 switch (flags
& PIO_NEED_FLAGS
) {
704 if (kluge_around_eof
== 2)
706 if (iobuf
.in_fd
== -2)
707 whine_about_eof(True
);
708 rprintf(FERROR
, "error in perform_io: no fd for input.\n");
709 exit_cleanup(RERR_PROTOCOL
);
710 case PIO_NEED_OUTROOM
:
711 case PIO_NEED_MSGROOM
:
713 drain_multiplex_messages();
714 if (iobuf
.out_fd
== -2)
715 whine_about_eof(True
);
716 rprintf(FERROR
, "error in perform_io: no fd for output.\n");
717 exit_cleanup(RERR_PROTOCOL
);
719 /* No stated needs, so I guess this is OK. */
725 if (got_kill_signal
> 0)
726 handle_kill_signal(True
);
728 if (extra_flist_sending_enabled
) {
729 if (file_total
- file_old_total
< MAX_FILECNT_LOOKAHEAD
&& IN_MULTIPLEXED_AND_READY
)
732 extra_flist_sending_enabled
= False
;
733 tv
.tv_sec
= select_timeout
;
736 tv
.tv_sec
= select_timeout
;
739 cnt
= select(max_fd
+ 1, &r_fds
, &w_fds
, &e_fds
, &tv
);
742 if (cnt
< 0 && errno
== EBADF
) {
744 exit_cleanup(RERR_SOCKETIO
);
746 if (extra_flist_sending_enabled
) {
747 extra_flist_sending_enabled
= False
;
748 send_extra_file_list(sock_f_out
, -1);
749 extra_flist_sending_enabled
= !flist_eof
;
751 check_timeout((flags
& PIO_NEED_INPUT
) != 0, 0);
752 FD_ZERO(&r_fds
); /* Just in case... */
756 if (iobuf
.in_fd
>= 0 && FD_ISSET(iobuf
.in_fd
, &r_fds
)) {
757 size_t len
, pos
= iobuf
.in
.pos
+ iobuf
.in
.len
;
759 if (pos
>= iobuf
.in
.size
) {
760 pos
-= iobuf
.in
.size
;
761 len
= iobuf
.in
.size
- iobuf
.in
.len
;
763 len
= iobuf
.in
.size
- pos
;
764 if ((n
= read(iobuf
.in_fd
, iobuf
.in
.buf
+ pos
, len
)) <= 0) {
766 /* Signal that input has become invalid. */
767 if (!read_batch
|| batch_fd
< 0 || am_generator
)
772 if (errno
== EINTR
|| errno
== EWOULDBLOCK
|| errno
== EAGAIN
)
775 /* Don't write errors on a dead socket. */
776 if (iobuf
.in_fd
== sock_f_in
) {
779 rsyserr(FERROR_SOCKET
, errno
, "read error");
781 rsyserr(FERROR
, errno
, "read error");
782 exit_cleanup(RERR_SOCKETIO
);
785 if (msgs2stderr
&& DEBUG_GTE(IO
, 2))
786 rprintf(FINFO
, "[%s] recv=%ld\n", who_am_i(), (long)n
);
789 last_io_in
= time(NULL
);
790 if (flags
& PIO_NEED_INPUT
)
791 maybe_send_keepalive(last_io_in
, 0);
793 stats
.total_read
+= n
;
798 if (out
&& FD_ISSET(iobuf
.out_fd
, &w_fds
)) {
799 size_t len
= iobuf
.raw_flushing_ends_before
? iobuf
.raw_flushing_ends_before
- out
->pos
: out
->len
;
802 if (bwlimit_writemax
&& len
> bwlimit_writemax
)
803 len
= bwlimit_writemax
;
805 if (out
->pos
+ len
> out
->size
)
806 len
= out
->size
- out
->pos
;
807 if ((n
= write(iobuf
.out_fd
, out
->buf
+ out
->pos
, len
)) <= 0) {
808 if (errno
== EINTR
|| errno
== EWOULDBLOCK
|| errno
== EAGAIN
)
811 /* Don't write errors on a dead socket. */
814 iobuf
.out
.len
= iobuf
.msg
.len
= iobuf
.raw_flushing_ends_before
= 0;
815 rsyserr(FERROR_SOCKET
, errno
, "write error");
816 drain_multiplex_messages();
817 exit_cleanup(RERR_SOCKETIO
);
820 if (msgs2stderr
&& DEBUG_GTE(IO
, 2)) {
821 rprintf(FINFO
, "[%s] %s sent=%ld\n",
822 who_am_i(), out
== &iobuf
.out
? "out" : "msg", (long)n
);
826 last_io_out
= time(NULL
);
827 stats
.total_written
+= n
;
829 if (bwlimit_writemax
)
830 sleep_for_bwlimit(n
);
832 if ((out
->pos
+= n
) == out
->size
) {
833 if (iobuf
.raw_flushing_ends_before
)
834 iobuf
.raw_flushing_ends_before
-= out
->size
;
836 restore_iobuf_size(out
);
837 } else if (out
->pos
== iobuf
.raw_flushing_ends_before
)
838 iobuf
.raw_flushing_ends_before
= 0;
839 if ((out
->len
-= n
) == empty_buf_len
) {
841 restore_iobuf_size(out
);
843 iobuf
.raw_data_header_pos
= 0;
847 if (got_kill_signal
> 0)
848 handle_kill_signal(True
);
850 /* We need to help prevent deadlock by doing what reading
851 * we can whenever we are here trying to write. */
852 if (IN_MULTIPLEXED_AND_READY
&& !(flags
& PIO_NEED_INPUT
)) {
853 while (!iobuf
.raw_input_ends_before
&& iobuf
.in
.len
> 512)
855 if (flist_receiving_enabled
&& iobuf
.in
.len
> 512)
856 wait_for_receiver(); /* generator only */
859 if (ff_forward_fd
>= 0 && FD_ISSET(ff_forward_fd
, &r_fds
)) {
860 /* This can potentially flush all output and enable
861 * multiplexed output, so keep this last in the loop
862 * and be sure to not cache anything that would break
864 forward_filesfrom_data();
869 if (got_kill_signal
> 0)
870 handle_kill_signal(True
);
872 data
= iobuf
.in
.buf
+ iobuf
.in
.pos
;
874 if (flags
& PIO_CONSUME_INPUT
) {
875 iobuf
.in
.len
-= needed
;
876 iobuf
.in
.pos
+= needed
;
877 if (iobuf
.in
.pos
== iobuf
.raw_input_ends_before
)
878 iobuf
.raw_input_ends_before
= 0;
879 if (iobuf
.in
.pos
>= iobuf
.in
.size
) {
880 iobuf
.in
.pos
-= iobuf
.in
.size
;
881 if (iobuf
.raw_input_ends_before
)
882 iobuf
.raw_input_ends_before
-= iobuf
.in
.size
;
889 static void raw_read_buf(char *buf
, size_t len
)
891 size_t pos
= iobuf
.in
.pos
;
892 char *data
= perform_io(len
, PIO_INPUT_AND_CONSUME
);
893 if (iobuf
.in
.pos
<= pos
&& len
) {
894 size_t siz
= len
- iobuf
.in
.pos
;
895 memcpy(buf
, data
, siz
);
896 memcpy(buf
+ siz
, iobuf
.in
.buf
, iobuf
.in
.pos
);
898 memcpy(buf
, data
, len
);
901 static int32
raw_read_int(void)
904 if (iobuf
.in
.size
- iobuf
.in
.pos
>= 4)
905 data
= perform_io(4, PIO_INPUT_AND_CONSUME
);
907 raw_read_buf(data
= buf
, 4);
908 return IVAL(data
, 0);
911 void noop_io_until_death(void)
915 if (!iobuf
.in
.buf
|| !iobuf
.out
.buf
|| iobuf
.in_fd
< 0 || iobuf
.out_fd
< 0 || kluge_around_eof
|| msgs2stderr
)
918 kluge_around_eof
= 2;
919 /* Setting an I/O timeout ensures that if something inexplicably weird
920 * happens, we won't hang around forever. */
925 read_buf(iobuf
.in_fd
, buf
, sizeof buf
);
928 /* Buffer a message for the multiplexed output stream. Is not used for (normal) MSG_DATA. */
929 int send_msg(enum msgcode code
, const char *buf
, size_t len
, int convert
)
933 BOOL want_debug
= DEBUG_GTE(IO
, 1) && convert
>= 0 && (msgs2stderr
|| code
!= MSG_INFO
);
935 if (!OUT_MULTIPLEXED
)
939 rprintf(FINFO
, "[%s] send_msg(%d, %ld)\n", who_am_i(), (int)code
, (long)len
);
941 /* When checking for enough free space for this message, we need to
942 * make sure that there is space for the 4-byte header, plus we'll
943 * assume that we may waste up to 3 bytes (if the header doesn't fit
944 * at the physical end of the buffer). */
946 if (convert
> 0 && ic_send
== (iconv_t
)-1)
949 /* Ensuring double-size room leaves space for maximal conversion expansion. */
950 needed
= len
*2 + 4 + 3;
953 needed
= len
+ 4 + 3;
954 if (iobuf
.msg
.len
+ needed
> iobuf
.msg
.size
) {
956 perform_io(needed
, PIO_NEED_MSGROOM
);
957 else { /* We allow the receiver to increase their iobuf.msg size to avoid a deadlock. */
958 size_t old_size
= iobuf
.msg
.size
;
959 restore_iobuf_size(&iobuf
.msg
);
960 realloc_xbuf(&iobuf
.msg
, iobuf
.msg
.size
* 2);
961 if (iobuf
.msg
.pos
+ iobuf
.msg
.len
> old_size
)
962 memcpy(iobuf
.msg
.buf
+ old_size
, iobuf
.msg
.buf
, iobuf
.msg
.pos
+ iobuf
.msg
.len
- old_size
);
966 pos
= iobuf
.msg
.pos
+ iobuf
.msg
.len
; /* Must be set after any flushing. */
967 if (pos
>= iobuf
.msg
.size
)
968 pos
-= iobuf
.msg
.size
;
969 else if (pos
+ 4 > iobuf
.msg
.size
) {
970 /* The 4-byte header won't fit at the end of the buffer,
971 * so we'll temporarily reduce the message buffer's size
972 * and put the header at the start of the buffer. */
973 reduce_iobuf_size(&iobuf
.msg
, pos
);
976 hdr
= iobuf
.msg
.buf
+ pos
;
978 iobuf
.msg
.len
+= 4; /* Allocate room for the coming header bytes. */
984 INIT_XBUF(inbuf
, (char*)buf
, len
, (size_t)-1);
987 iconvbufs(ic_send
, &inbuf
, &iobuf
.msg
,
988 ICB_INCLUDE_BAD
| ICB_INCLUDE_INCOMPLETE
| ICB_CIRCULAR_OUT
| ICB_INIT
);
990 rprintf(FERROR
, "overflowed iobuf.msg buffer in send_msg");
991 exit_cleanup(RERR_UNSUPPORTED
);
993 len
= iobuf
.msg
.len
- len
;
999 if ((pos
+= 4) == iobuf
.msg
.size
)
1002 /* Handle a split copy if we wrap around the end of the circular buffer. */
1003 if (pos
>= iobuf
.msg
.pos
&& (siz
= iobuf
.msg
.size
- pos
) < len
) {
1004 memcpy(iobuf
.msg
.buf
+ pos
, buf
, siz
);
1005 memcpy(iobuf
.msg
.buf
, buf
+ siz
, len
- siz
);
1007 memcpy(iobuf
.msg
.buf
+ pos
, buf
, len
);
1009 iobuf
.msg
.len
+= len
;
1012 SIVAL(hdr
, 0, ((MPLEX_BASE
+ (int)code
)<<24) + len
);
1014 if (want_debug
&& convert
> 0)
1015 rprintf(FINFO
, "[%s] converted msg len=%ld\n", who_am_i(), (long)len
);
1020 void send_msg_int(enum msgcode code
, int num
)
1024 if (DEBUG_GTE(IO
, 1))
1025 rprintf(FINFO
, "[%s] send_msg_int(%d, %d)\n", who_am_i(), (int)code
, num
);
1027 SIVAL(numbuf
, 0, num
);
1028 send_msg(code
, numbuf
, 4, -1);
1031 static void got_flist_entry_status(enum festatus status
, int ndx
)
1033 struct file_list
*flist
= flist_for_ndx(ndx
, "got_flist_entry_status");
1035 if (remove_source_files
) {
1037 active_bytecnt
-= F_LENGTH(flist
->files
[ndx
- flist
->ndx_start
]);
1041 flist
->in_progress
--;
1045 if (remove_source_files
)
1046 send_msg_int(MSG_SUCCESS
, ndx
);
1049 #ifdef SUPPORT_HARD_LINKS
1050 if (preserve_hard_links
) {
1051 struct file_struct
*file
= flist
->files
[ndx
- flist
->ndx_start
];
1052 if (F_IS_HLINKED(file
)) {
1053 if (status
== FES_NO_SEND
)
1054 flist_ndx_push(&hlink_list
, -2); /* indicates a failure follows */
1055 flist_ndx_push(&hlink_list
, ndx
);
1057 flist
->in_progress
++;
1065 flist
->in_progress
++;
1070 flist_ndx_push(&redo_list
, ndx
);
1075 /* Note the fds used for the main socket (which might really be a pipe
1076 * for a local transfer, but we can ignore that). */
1077 void io_set_sock_fds(int f_in
, int f_out
)
1083 void set_io_timeout(int secs
)
1086 allowed_lull
= (io_timeout
+ 1) / 2;
1088 if (!io_timeout
|| allowed_lull
> SELECT_TIMEOUT
)
1089 select_timeout
= SELECT_TIMEOUT
;
1091 select_timeout
= allowed_lull
;
1097 static void check_for_d_option_error(const char *msg
)
1099 static char rsync263_opts
[] = "BCDHIKLPRSTWabceghlnopqrtuvxz";
1104 || strncmp(msg
, REMOTE_OPTION_ERROR
, sizeof REMOTE_OPTION_ERROR
- 1) != 0)
1107 msg
+= sizeof REMOTE_OPTION_ERROR
- 1;
1108 if (*msg
== '-' || (colon
= strchr(msg
, ':')) == NULL
1109 || strncmp(colon
, REMOTE_OPTION_ERROR2
, sizeof REMOTE_OPTION_ERROR2
- 1) != 0)
1112 for ( ; *msg
!= ':'; msg
++) {
1115 else if (*msg
== 'e')
1117 else if (strchr(rsync263_opts
, *msg
) == NULL
)
1122 rprintf(FWARNING
, "*** Try using \"--old-d\" if remote rsync is <= 2.6.3 ***\n");
1126 /* This is used by the generator to limit how many file transfers can
1127 * be active at once when --remove-source-files is specified. Without
1128 * this, sender-side deletions were mostly happening at the end. */
1129 void increment_active_files(int ndx
, int itemizing
, enum logcode code
)
1132 /* TODO: tune these limits? */
1133 int limit
= active_bytecnt
>= 128*1024 ? 10 : 50;
1134 if (active_filecnt
< limit
)
1136 check_for_finished_files(itemizing
, code
, 0);
1137 if (active_filecnt
< limit
)
1139 wait_for_receiver();
1143 active_bytecnt
+= F_LENGTH(cur_flist
->files
[ndx
- cur_flist
->ndx_start
]);
1146 int get_redo_num(void)
1148 return flist_ndx_pop(&redo_list
);
1151 int get_hlink_num(void)
1153 return flist_ndx_pop(&hlink_list
);
1156 /* When we're the receiver and we have a local --files-from list of names
1157 * that needs to be sent over the socket to the sender, we have to do two
1158 * things at the same time: send the sender a list of what files we're
1159 * processing and read the incoming file+info list from the sender. We do
1160 * this by making recv_file_list() call forward_filesfrom_data(), which
1161 * will ensure that we forward data to the sender until we get some data
1162 * for recv_file_list() to use. */
1163 void start_filesfrom_forwarding(int fd
)
1165 if (protocol_version
< 31 && OUT_MULTIPLEXED
) {
1166 /* Older protocols send the files-from data w/o packaging
1167 * it in multiplexed I/O packets, so temporarily switch
1168 * to buffered I/O to match this behavior. */
1169 iobuf
.msg
.pos
= iobuf
.msg
.len
= 0; /* Be extra sure no messages go out. */
1170 ff_reenable_multiplex
= io_end_multiplex_out(MPLX_TO_BUFFERED
);
1174 alloc_xbuf(&ff_xb
, FILESFROM_BUFLEN
);
1177 /* Read a line into the "buf" buffer. */
1178 int read_line(int fd
, char *buf
, size_t bufsiz
, int flags
)
1183 if (flags
& RL_CONVERT
&& iconv_buf
.size
< bufsiz
)
1184 realloc_xbuf(&iconv_buf
, ROUND_UP_1024(bufsiz
) + 1024);
1189 s
= flags
& RL_CONVERT
? iconv_buf
.buf
: buf
;
1193 eob
= s
+ bufsiz
- 1;
1195 /* We avoid read_byte() for files because files can return an EOF. */
1196 if (fd
== iobuf
.in_fd
)
1198 else if (safe_read(fd
, &ch
, 1) == 0)
1200 if (flags
& RL_EOL_NULLS
? ch
== '\0' : (ch
== '\r' || ch
== '\n')) {
1201 /* Skip empty lines if dumping comments. */
1202 if (flags
& RL_DUMP_COMMENTS
&& s
== buf
)
1211 if (flags
& RL_DUMP_COMMENTS
&& (*buf
== '#' || *buf
== ';'))
1215 if (flags
& RL_CONVERT
) {
1217 INIT_XBUF(outbuf
, buf
, 0, bufsiz
);
1219 iconv_buf
.len
= s
- iconv_buf
.buf
;
1220 iconvbufs(ic_recv
, &iconv_buf
, &outbuf
,
1221 ICB_INCLUDE_BAD
| ICB_INCLUDE_INCOMPLETE
| ICB_INIT
);
1222 outbuf
.buf
[outbuf
.len
] = '\0';
1230 void read_args(int f_in
, char *mod_name
, char *buf
, size_t bufsiz
, int rl_nulls
,
1231 char ***argv_p
, int *argc_p
, char **request_p
)
1233 int maxargs
= MAX_ARGS
;
1234 int dot_pos
= 0, argc
= 0, request_len
= 0;
1236 int rl_flags
= (rl_nulls
? RL_EOL_NULLS
: 0);
1239 rl_flags
|= (protect_args
&& ic_recv
!= (iconv_t
)-1 ? RL_CONVERT
: 0);
1242 if (!(argv
= new_array(char *, maxargs
)))
1243 out_of_memory("read_args");
1244 if (mod_name
&& !protect_args
)
1245 argv
[argc
++] = "rsyncd";
1251 if (read_line(f_in
, buf
, bufsiz
, rl_flags
) == 0)
1254 if (argc
== maxargs
-1) {
1255 maxargs
+= MAX_ARGS
;
1256 if (!(argv
= realloc_array(argv
, char *, maxargs
)))
1257 out_of_memory("read_args");
1261 if (request_p
&& request_len
< 1024) {
1262 int len
= strlen(buf
);
1264 request_p
[0][request_len
++] = ' ';
1265 if (!(*request_p
= realloc_array(*request_p
, char, request_len
+ len
+ 1)))
1266 out_of_memory("read_args");
1267 memcpy(*request_p
+ request_len
, buf
, len
+ 1);
1271 glob_expand_module(mod_name
, buf
, &argv
, &argc
, &maxargs
);
1273 glob_expand(buf
, &argv
, &argc
, &maxargs
);
1275 if (!(p
= strdup(buf
)))
1276 out_of_memory("read_args");
1278 if (*p
== '.' && p
[1] == '\0')
1284 glob_expand(NULL
, NULL
, NULL
, NULL
);
1290 BOOL
io_start_buffering_out(int f_out
)
1292 if (msgs2stderr
&& DEBUG_GTE(IO
, 2))
1293 rprintf(FINFO
, "[%s] io_start_buffering_out(%d)\n", who_am_i(), f_out
);
1295 if (iobuf
.out
.buf
) {
1296 if (iobuf
.out_fd
== -1)
1297 iobuf
.out_fd
= f_out
;
1299 assert(f_out
== iobuf
.out_fd
);
1303 alloc_xbuf(&iobuf
.out
, ROUND_UP_1024(IO_BUFFER_SIZE
* 2));
1304 iobuf
.out_fd
= f_out
;
1309 BOOL
io_start_buffering_in(int f_in
)
1311 if (msgs2stderr
&& DEBUG_GTE(IO
, 2))
1312 rprintf(FINFO
, "[%s] io_start_buffering_in(%d)\n", who_am_i(), f_in
);
1315 if (iobuf
.in_fd
== -1)
1318 assert(f_in
== iobuf
.in_fd
);
1322 alloc_xbuf(&iobuf
.in
, ROUND_UP_1024(IO_BUFFER_SIZE
));
1328 void io_end_buffering_in(BOOL free_buffers
)
1330 if (msgs2stderr
&& DEBUG_GTE(IO
, 2)) {
1331 rprintf(FINFO
, "[%s] io_end_buffering_in(IOBUF_%s_BUFS)\n",
1332 who_am_i(), free_buffers
? "FREE" : "KEEP");
1336 free_xbuf(&iobuf
.in
);
1338 iobuf
.in
.pos
= iobuf
.in
.len
= 0;
1343 void io_end_buffering_out(BOOL free_buffers
)
1345 if (msgs2stderr
&& DEBUG_GTE(IO
, 2)) {
1346 rprintf(FINFO
, "[%s] io_end_buffering_out(IOBUF_%s_BUFS)\n",
1347 who_am_i(), free_buffers
? "FREE" : "KEEP");
1350 io_flush(FULL_FLUSH
);
1353 free_xbuf(&iobuf
.out
);
1354 free_xbuf(&iobuf
.msg
);
1360 void maybe_flush_socket(int important
)
1362 if (flist_eof
&& iobuf
.out
.buf
&& iobuf
.out
.len
> iobuf
.out_empty_len
1363 && (important
|| time(NULL
) - last_io_out
>= 5))
1364 io_flush(NORMAL_FLUSH
);
1367 /* Older rsync versions used to send either a MSG_NOOP (protocol 30) or a
1368 * raw-data-based keep-alive (protocol 29), both of which implied forwarding of
1369 * the message through the sender. Since the new timeout method does not need
1370 * any forwarding, we just send an empty MSG_DATA message, which works with all
1371 * rsync versions. This avoids any message forwarding, and leaves the raw-data
1372 * stream alone (since we can never be quite sure if that stream is in the
1373 * right state for a keep-alive message). */
1374 void maybe_send_keepalive(time_t now
, int flags
)
1376 if (flags
& MSK_ACTIVE_RECEIVER
)
1377 last_io_in
= now
; /* Fudge things when we're working hard on the files. */
1379 /* Early in the transfer (before the receiver forks) the receiving side doesn't
1380 * care if it hasn't sent data in a while as long as it is receiving data (in
1381 * fact, a pre-3.1.0 rsync would die if we tried to send it a keep alive during
1382 * this time). So, if we're an early-receiving proc, just return and let the
1383 * incoming data determine if we timeout. */
1384 if (!am_sender
&& !am_receiver
&& !am_generator
)
1387 if (now
- last_io_out
>= allowed_lull
) {
1388 /* The receiver is special: it only sends keep-alive messages if it is
1389 * actively receiving data. Otherwise, it lets the generator timeout. */
1390 if (am_receiver
&& now
- last_io_in
>= io_timeout
)
1393 if (!iobuf
.msg
.len
&& iobuf
.out
.len
== iobuf
.out_empty_len
)
1394 send_msg(MSG_DATA
, "", 0, 0);
1395 if (!(flags
& MSK_ALLOW_FLUSH
)) {
1396 /* Let the caller worry about writing out the data. */
1397 } else if (iobuf
.msg
.len
)
1398 perform_io(iobuf
.msg
.size
- iobuf
.msg
.len
+ 1, PIO_NEED_MSGROOM
);
1399 else if (iobuf
.out
.len
> iobuf
.out_empty_len
)
1400 io_flush(NORMAL_FLUSH
);
1404 void start_flist_forward(int ndx
)
1406 write_int(iobuf
.out_fd
, ndx
);
1407 forward_flist_data
= 1;
1410 void stop_flist_forward(void)
1412 forward_flist_data
= 0;
1415 /* Read a message from a multiplexed source. */
1416 static void read_a_msg(void)
1418 char data
[BIGPATHBUFLEN
];
1422 /* This ensures that perform_io() does not try to do any message reading
1423 * until we've read all of the data for this message. We should also
1424 * try to avoid calling things that will cause data to be written via
1425 * perform_io() prior to this being reset to 1. */
1426 iobuf
.in_multiplexed
= -1;
1428 tag
= raw_read_int();
1430 msg_bytes
= tag
& 0xFFFFFF;
1431 tag
= (tag
>> 24) - MPLEX_BASE
;
1433 if (DEBUG_GTE(IO
, 1) && msgs2stderr
)
1434 rprintf(FINFO
, "[%s] got msg=%d, len=%ld\n", who_am_i(), (int)tag
, (long)msg_bytes
);
1438 assert(iobuf
.raw_input_ends_before
== 0);
1439 /* Though this does not yet read the data, we do mark where in
1440 * the buffer the msg data will end once it is read. It is
1441 * possible that this points off the end of the buffer, in
1442 * which case the gradual reading of the input stream will
1443 * cause this value to wrap around and eventually become real. */
1445 iobuf
.raw_input_ends_before
= iobuf
.in
.pos
+ msg_bytes
;
1446 iobuf
.in_multiplexed
= 1;
1449 if (msg_bytes
!= sizeof stats
.total_read
|| !am_generator
)
1451 raw_read_buf((char*)&stats
.total_read
, sizeof stats
.total_read
);
1452 iobuf
.in_multiplexed
= 1;
1455 if (msg_bytes
!= 4 || !am_generator
)
1457 val
= raw_read_int();
1458 iobuf
.in_multiplexed
= 1;
1459 got_flist_entry_status(FES_REDO
, val
);
1464 val
= raw_read_int();
1465 iobuf
.in_multiplexed
= 1;
1468 send_msg_int(MSG_IO_ERROR
, val
);
1470 case MSG_IO_TIMEOUT
:
1471 if (msg_bytes
!= 4 || am_server
|| am_generator
)
1473 val
= raw_read_int();
1474 iobuf
.in_multiplexed
= 1;
1475 if (!io_timeout
|| io_timeout
> val
) {
1476 if (INFO_GTE(MISC
, 2))
1477 rprintf(FINFO
, "Setting --timeout=%d to match server\n", val
);
1478 set_io_timeout(val
);
1482 /* Support protocol-30 keep-alive method. */
1485 iobuf
.in_multiplexed
= 1;
1487 maybe_send_keepalive(time(NULL
), MSK_ALLOW_FLUSH
);
1490 if (msg_bytes
>= sizeof data
)
1493 raw_read_buf(data
, msg_bytes
);
1494 iobuf
.in_multiplexed
= 1;
1495 send_msg(MSG_DELETED
, data
, msg_bytes
, 1);
1499 if (ic_recv
!= (iconv_t
)-1) {
1503 int flags
= ICB_INCLUDE_BAD
| ICB_INIT
;
1505 INIT_CONST_XBUF(outbuf
, data
);
1506 INIT_XBUF(inbuf
, ibuf
, 0, (size_t)-1);
1509 size_t len
= msg_bytes
> sizeof ibuf
- inbuf
.len
? sizeof ibuf
- inbuf
.len
: msg_bytes
;
1510 raw_read_buf(ibuf
+ inbuf
.len
, len
);
1513 if (!(msg_bytes
-= len
) && !ibuf
[inbuf
.len
-1])
1514 inbuf
.len
--, add_null
= 1;
1515 if (iconvbufs(ic_send
, &inbuf
, &outbuf
, flags
) < 0) {
1518 /* Buffer ended with an incomplete char, so move the
1519 * bytes to the start of the buffer and continue. */
1520 memmove(ibuf
, ibuf
+ inbuf
.pos
, inbuf
.len
);
1525 if (outbuf
.len
== outbuf
.size
)
1527 outbuf
.buf
[outbuf
.len
++] = '\0';
1529 msg_bytes
= outbuf
.len
;
1532 raw_read_buf(data
, msg_bytes
);
1533 iobuf
.in_multiplexed
= 1;
1534 /* A directory name was sent with the trailing null */
1535 if (msg_bytes
> 0 && !data
[msg_bytes
-1])
1536 log_delete(data
, S_IFDIR
);
1538 data
[msg_bytes
] = '\0';
1539 log_delete(data
, S_IFREG
);
1543 if (msg_bytes
!= 4) {
1545 rprintf(FERROR
, "invalid multi-message %d:%lu [%s%s]\n",
1546 tag
, (unsigned long)msg_bytes
, who_am_i(),
1547 inc_recurse
? "/inc" : "");
1548 exit_cleanup(RERR_STREAMIO
);
1550 val
= raw_read_int();
1551 iobuf
.in_multiplexed
= 1;
1553 got_flist_entry_status(FES_SUCCESS
, val
);
1555 successful_send(val
);
1560 val
= raw_read_int();
1561 iobuf
.in_multiplexed
= 1;
1563 got_flist_entry_status(FES_NO_SEND
, val
);
1565 send_msg_int(MSG_NO_SEND
, val
);
1567 case MSG_ERROR_SOCKET
:
1568 case MSG_ERROR_UTF8
:
1573 if (tag
== MSG_ERROR_SOCKET
)
1578 case MSG_ERROR_XFER
:
1580 if (msg_bytes
>= sizeof data
) {
1583 "multiplexing overflow %d:%lu [%s%s]\n",
1584 tag
, (unsigned long)msg_bytes
, who_am_i(),
1585 inc_recurse
? "/inc" : "");
1586 exit_cleanup(RERR_STREAMIO
);
1588 raw_read_buf(data
, msg_bytes
);
1589 /* We don't set in_multiplexed value back to 1 before writing this message
1590 * because the write might loop back and read yet another message, over and
1591 * over again, while waiting for room to put the message in the msg buffer. */
1592 rwrite((enum logcode
)tag
, data
, msg_bytes
, !am_generator
);
1593 iobuf
.in_multiplexed
= 1;
1594 if (first_message
) {
1595 if (list_only
&& !am_sender
&& tag
== 1 && msg_bytes
< sizeof data
) {
1596 data
[msg_bytes
] = '\0';
1597 check_for_d_option_error(data
);
1602 case MSG_ERROR_EXIT
:
1604 val
= raw_read_int();
1605 else if (msg_bytes
== 0)
1609 iobuf
.in_multiplexed
= 1;
1610 if (DEBUG_GTE(EXIT
, 3))
1611 rprintf(FINFO
, "[%s] got MSG_ERROR_EXIT with %ld bytes\n", who_am_i(), (long)msg_bytes
);
1612 if (msg_bytes
== 0) {
1613 if (!am_sender
&& !am_generator
) {
1614 if (DEBUG_GTE(EXIT
, 3)) {
1615 rprintf(FINFO
, "[%s] sending MSG_ERROR_EXIT (len 0)\n",
1618 send_msg(MSG_ERROR_EXIT
, "", 0, 0);
1619 io_flush(FULL_FLUSH
);
1621 } else if (protocol_version
>= 31) {
1622 if (am_generator
|| am_receiver
) {
1623 if (DEBUG_GTE(EXIT
, 3)) {
1624 rprintf(FINFO
, "[%s] sending MSG_ERROR_EXIT with exit_code %d\n",
1627 send_msg_int(MSG_ERROR_EXIT
, val
);
1629 if (DEBUG_GTE(EXIT
, 3)) {
1630 rprintf(FINFO
, "[%s] sending MSG_ERROR_EXIT (len 0)\n",
1633 send_msg(MSG_ERROR_EXIT
, "", 0, 0);
1636 /* Send a negative linenum so that we don't end up
1637 * with a duplicate exit message. */
1638 _exit_cleanup(val
, __FILE__
, 0 - __LINE__
);
1640 rprintf(FERROR
, "unexpected tag %d [%s%s]\n",
1641 tag
, who_am_i(), inc_recurse
? "/inc" : "");
1642 exit_cleanup(RERR_STREAMIO
);
1645 assert(iobuf
.in_multiplexed
> 0);
1648 static void drain_multiplex_messages(void)
1650 while (IN_MULTIPLEXED_AND_READY
&& iobuf
.in
.len
) {
1651 if (iobuf
.raw_input_ends_before
) {
1652 size_t raw_len
= iobuf
.raw_input_ends_before
- iobuf
.in
.pos
;
1653 iobuf
.raw_input_ends_before
= 0;
1654 if (raw_len
>= iobuf
.in
.len
) {
1658 iobuf
.in
.len
-= raw_len
;
1659 if ((iobuf
.in
.pos
+= raw_len
) >= iobuf
.in
.size
)
1660 iobuf
.in
.pos
-= iobuf
.in
.size
;
1666 void wait_for_receiver(void)
1668 if (!iobuf
.raw_input_ends_before
)
1671 if (iobuf
.raw_input_ends_before
) {
1672 int ndx
= read_int(iobuf
.in_fd
);
1677 if (DEBUG_GTE(FLIST
, 3))
1678 rprintf(FINFO
, "[%s] flist_eof=1\n", who_am_i());
1684 exit_cleanup(RERR_STREAMIO
);
1687 struct file_list
*flist
;
1688 flist_receiving_enabled
= False
;
1689 if (DEBUG_GTE(FLIST
, 2)) {
1690 rprintf(FINFO
, "[%s] receiving flist for dir %d\n",
1693 flist
= recv_file_list(iobuf
.in_fd
, ndx
);
1694 flist
->parent_ndx
= ndx
;
1695 #ifdef SUPPORT_HARD_LINKS
1696 if (preserve_hard_links
)
1697 match_hard_links(flist
);
1699 flist_receiving_enabled
= True
;
1704 unsigned short read_shortint(int f
)
1708 return (UVAL(b
, 1) << 8) + UVAL(b
, 0);
1711 int32
read_int(int f
)
1718 #if SIZEOF_INT32 > 4
1719 if (num
& (int32
)0x80000000)
1720 num
|= ~(int32
)0xffffffff;
1725 int32
read_varint(int f
)
1736 extra
= int_byte_extra
[ch
/ 4];
1738 uchar bit
= ((uchar
)1<<(8-extra
));
1739 if (extra
>= (int)sizeof u
.b
) {
1740 rprintf(FERROR
, "Overflow in read_varint()\n");
1741 exit_cleanup(RERR_STREAMIO
);
1743 read_buf(f
, u
.b
, extra
);
1744 u
.b
[extra
] = ch
& (bit
-1);
1747 #if CAREFUL_ALIGNMENT
1750 #if SIZEOF_INT32 > 4
1751 if (u
.x
& (int32
)0x80000000)
1752 u
.x
|= ~(int32
)0xffffffff;
1757 int64
read_varlong(int f
, uchar min_bytes
)
1766 #if SIZEOF_INT64 < 8
1771 read_buf(f
, b2
, min_bytes
);
1772 memcpy(u
.b
, b2
+1, min_bytes
-1);
1773 extra
= int_byte_extra
[CVAL(b2
, 0) / 4];
1775 uchar bit
= ((uchar
)1<<(8-extra
));
1776 if (min_bytes
+ extra
> (int)sizeof u
.b
) {
1777 rprintf(FERROR
, "Overflow in read_varlong()\n");
1778 exit_cleanup(RERR_STREAMIO
);
1780 read_buf(f
, u
.b
+ min_bytes
- 1, extra
);
1781 u
.b
[min_bytes
+ extra
- 1] = CVAL(b2
, 0) & (bit
-1);
1782 #if SIZEOF_INT64 < 8
1783 if (min_bytes
+ extra
> 5 || u
.b
[4] || CVAL(u
.b
,3) & 0x80) {
1784 rprintf(FERROR
, "Integer overflow: attempted 64-bit offset\n");
1785 exit_cleanup(RERR_UNSUPPORTED
);
1789 u
.b
[min_bytes
+ extra
- 1] = CVAL(b2
, 0);
1790 #if SIZEOF_INT64 < 8
1792 #elif CAREFUL_ALIGNMENT
1793 u
.x
= IVAL64(u
.b
,0);
1798 int64
read_longint(int f
)
1800 #if SIZEOF_INT64 >= 8
1803 int32 num
= read_int(f
);
1805 if (num
!= (int32
)0xffffffff)
1808 #if SIZEOF_INT64 < 8
1809 rprintf(FERROR
, "Integer overflow: attempted 64-bit offset\n");
1810 exit_cleanup(RERR_UNSUPPORTED
);
1813 return IVAL(b
,0) | (((int64
)IVAL(b
,4))<<32);
1817 void read_buf(int f
, char *buf
, size_t len
)
1819 if (f
!= iobuf
.in_fd
) {
1820 if (safe_read(f
, buf
, len
) != len
)
1821 whine_about_eof(False
); /* Doesn't return. */
1825 if (!IN_MULTIPLEXED
) {
1826 raw_read_buf(buf
, len
);
1827 total_data_read
+= len
;
1828 if (forward_flist_data
)
1829 write_buf(iobuf
.out_fd
, buf
, len
);
1831 if (f
== write_batch_monitor_in
)
1832 safe_write(batch_fd
, buf
, len
);
1839 while (!iobuf
.raw_input_ends_before
)
1842 siz
= MIN(len
, iobuf
.raw_input_ends_before
- iobuf
.in
.pos
);
1843 if (siz
>= iobuf
.in
.size
)
1844 siz
= iobuf
.in
.size
;
1845 raw_read_buf(buf
, siz
);
1846 total_data_read
+= siz
;
1848 if (forward_flist_data
)
1849 write_buf(iobuf
.out_fd
, buf
, siz
);
1851 if (f
== write_batch_monitor_in
)
1852 safe_write(batch_fd
, buf
, siz
);
1854 if ((len
-= siz
) == 0)
1860 void read_sbuf(int f
, char *buf
, size_t len
)
1862 read_buf(f
, buf
, len
);
1866 uchar
read_byte(int f
)
1869 read_buf(f
, (char*)&c
, 1);
1873 int read_vstring(int f
, char *buf
, int bufsize
)
1875 int len
= read_byte(f
);
1878 len
= (len
& ~0x80) * 0x100 + read_byte(f
);
1880 if (len
>= bufsize
) {
1881 rprintf(FERROR
, "over-long vstring received (%d > %d)\n",
1887 read_buf(f
, buf
, len
);
1892 /* Populate a sum_struct with values from the socket. This is
1893 * called by both the sender and the receiver. */
1894 void read_sum_head(int f
, struct sum_struct
*sum
)
1896 int32 max_blength
= protocol_version
< 30 ? OLD_MAX_BLOCK_SIZE
: MAX_BLOCK_SIZE
;
1897 sum
->count
= read_int(f
);
1898 if (sum
->count
< 0) {
1899 rprintf(FERROR
, "Invalid checksum count %ld [%s]\n",
1900 (long)sum
->count
, who_am_i());
1901 exit_cleanup(RERR_PROTOCOL
);
1903 sum
->blength
= read_int(f
);
1904 if (sum
->blength
< 0 || sum
->blength
> max_blength
) {
1905 rprintf(FERROR
, "Invalid block length %ld [%s]\n",
1906 (long)sum
->blength
, who_am_i());
1907 exit_cleanup(RERR_PROTOCOL
);
1909 sum
->s2length
= protocol_version
< 27 ? csum_length
: (int)read_int(f
);
1910 if (sum
->s2length
< 0 || sum
->s2length
> MAX_DIGEST_LEN
) {
1911 rprintf(FERROR
, "Invalid checksum length %d [%s]\n",
1912 sum
->s2length
, who_am_i());
1913 exit_cleanup(RERR_PROTOCOL
);
1915 sum
->remainder
= read_int(f
);
1916 if (sum
->remainder
< 0 || sum
->remainder
> sum
->blength
) {
1917 rprintf(FERROR
, "Invalid remainder length %ld [%s]\n",
1918 (long)sum
->remainder
, who_am_i());
1919 exit_cleanup(RERR_PROTOCOL
);
1923 /* Send the values from a sum_struct over the socket. Set sum to
1924 * NULL if there are no checksums to send. This is called by both
1925 * the generator and the sender. */
1926 void write_sum_head(int f
, struct sum_struct
*sum
)
1928 static struct sum_struct null_sum
;
1933 write_int(f
, sum
->count
);
1934 write_int(f
, sum
->blength
);
1935 if (protocol_version
>= 27)
1936 write_int(f
, sum
->s2length
);
1937 write_int(f
, sum
->remainder
);
1940 /* Sleep after writing to limit I/O bandwidth usage.
1942 * @todo Rather than sleeping after each write, it might be better to
1943 * use some kind of averaging. The current algorithm seems to always
1944 * use a bit less bandwidth than specified, because it doesn't make up
1945 * for slow periods. But arguably this is a feature. In addition, we
1946 * ought to take the time used to write the data into account.
1948 * During some phases of big transfers (file FOO is uptodate) this is
1949 * called with a small bytes_written every time. As the kernel has to
1950 * round small waits up to guarantee that we actually wait at least the
1951 * requested number of microseconds, this can become grossly inaccurate.
1952 * We therefore keep track of the bytes we've written over time and only
1953 * sleep when the accumulated delay is at least 1 tenth of a second. */
1954 static void sleep_for_bwlimit(int bytes_written
)
1956 static struct timeval prior_tv
;
1957 static long total_written
= 0;
1958 struct timeval tv
, start_tv
;
1959 long elapsed_usec
, sleep_usec
;
1961 #define ONE_SEC 1000000L /* # of microseconds in a second */
1963 total_written
+= bytes_written
;
1965 gettimeofday(&start_tv
, NULL
);
1966 if (prior_tv
.tv_sec
) {
1967 elapsed_usec
= (start_tv
.tv_sec
- prior_tv
.tv_sec
) * ONE_SEC
1968 + (start_tv
.tv_usec
- prior_tv
.tv_usec
);
1969 total_written
-= (int64
)elapsed_usec
* bwlimit
/ (ONE_SEC
/1024);
1970 if (total_written
< 0)
1974 sleep_usec
= total_written
* (ONE_SEC
/1024) / bwlimit
;
1975 if (sleep_usec
< ONE_SEC
/ 10) {
1976 prior_tv
= start_tv
;
1980 tv
.tv_sec
= sleep_usec
/ ONE_SEC
;
1981 tv
.tv_usec
= sleep_usec
% ONE_SEC
;
1982 select(0, NULL
, NULL
, NULL
, &tv
);
1984 gettimeofday(&prior_tv
, NULL
);
1985 elapsed_usec
= (prior_tv
.tv_sec
- start_tv
.tv_sec
) * ONE_SEC
1986 + (prior_tv
.tv_usec
- start_tv
.tv_usec
);
1987 total_written
= (sleep_usec
- elapsed_usec
) * bwlimit
/ (ONE_SEC
/1024);
1990 void io_flush(int flush_type
)
1992 if (iobuf
.out
.len
> iobuf
.out_empty_len
) {
1993 if (flush_type
== FULL_FLUSH
) /* flush everything in the output buffers */
1994 perform_io(iobuf
.out
.size
- iobuf
.out_empty_len
, PIO_NEED_OUTROOM
);
1995 else if (flush_type
== NORMAL_FLUSH
) /* flush at least 1 byte */
1996 perform_io(iobuf
.out
.size
- iobuf
.out
.len
+ 1, PIO_NEED_OUTROOM
);
1997 /* MSG_FLUSH: flush iobuf.msg only */
2000 perform_io(iobuf
.msg
.size
, PIO_NEED_MSGROOM
);
2003 void write_shortint(int f
, unsigned short x
)
2007 b
[1] = (char)(x
>> 8);
2011 void write_int(int f
, int32 x
)
2018 void write_varint(int f
, int32 x
)
2026 for (cnt
= 4; cnt
> 1 && b
[cnt
] == 0; cnt
--) {}
2027 bit
= ((uchar
)1<<(7-cnt
+1));
2029 if (CVAL(b
, cnt
) >= bit
) {
2033 *b
= b
[cnt
] | ~(bit
*2-1);
2037 write_buf(f
, b
, cnt
);
2040 void write_varlong(int f
, int64 x
, uchar min_bytes
)
2046 #if SIZEOF_INT64 >= 8
2050 if (x
<= 0x7FFFFFFF && x
>= 0)
2051 memset(b
+ 5, 0, 4);
2053 rprintf(FERROR
, "Integer overflow: attempted 64-bit offset\n");
2054 exit_cleanup(RERR_UNSUPPORTED
);
2058 while (cnt
> min_bytes
&& b
[cnt
] == 0)
2060 bit
= ((uchar
)1<<(7-cnt
+min_bytes
));
2061 if (CVAL(b
, cnt
) >= bit
) {
2064 } else if (cnt
> min_bytes
)
2065 *b
= b
[cnt
] | ~(bit
*2-1);
2069 write_buf(f
, b
, cnt
);
2073 * Note: int64 may actually be a 32-bit type if ./configure couldn't find any
2074 * 64-bit types on this platform.
2076 void write_longint(int f
, int64 x
)
2078 char b
[12], * const s
= b
+4;
2081 if (x
<= 0x7FFFFFFF && x
>= 0) {
2086 #if SIZEOF_INT64 < 8
2087 rprintf(FERROR
, "Integer overflow: attempted 64-bit offset\n");
2088 exit_cleanup(RERR_UNSUPPORTED
);
2091 SIVAL(s
, 4, x
>> 32);
2092 write_buf(f
, b
, 12);
2096 void write_bigbuf(int f
, const char *buf
, size_t len
)
2098 size_t half_max
= (iobuf
.out
.size
- iobuf
.out_empty_len
) / 2;
2100 while (len
> half_max
+ 1024) {
2101 write_buf(f
, buf
, half_max
);
2106 write_buf(f
, buf
, len
);
2109 void write_buf(int f
, const char *buf
, size_t len
)
2113 if (f
!= iobuf
.out_fd
) {
2114 safe_write(f
, buf
, len
);
2118 if (iobuf
.out
.len
+ len
> iobuf
.out
.size
)
2119 perform_io(len
, PIO_NEED_OUTROOM
);
2121 pos
= iobuf
.out
.pos
+ iobuf
.out
.len
; /* Must be set after any flushing. */
2122 if (pos
>= iobuf
.out
.size
)
2123 pos
-= iobuf
.out
.size
;
2125 /* Handle a split copy if we wrap around the end of the circular buffer. */
2126 if (pos
>= iobuf
.out
.pos
&& (siz
= iobuf
.out
.size
- pos
) < len
) {
2127 memcpy(iobuf
.out
.buf
+ pos
, buf
, siz
);
2128 memcpy(iobuf
.out
.buf
, buf
+ siz
, len
- siz
);
2130 memcpy(iobuf
.out
.buf
+ pos
, buf
, len
);
2132 iobuf
.out
.len
+= len
;
2133 total_data_written
+= len
;
2136 if (f
== write_batch_monitor_out
)
2137 safe_write(batch_fd
, buf
, len
);
2140 /* Write a string to the connection */
2141 void write_sbuf(int f
, const char *buf
)
2143 write_buf(f
, buf
, strlen(buf
));
2146 void write_byte(int f
, uchar c
)
2148 write_buf(f
, (char *)&c
, 1);
2151 void write_vstring(int f
, const char *str
, int len
)
2153 uchar lenbuf
[3], *lb
= lenbuf
;
2158 "attempting to send over-long vstring (%d > %d)\n",
2160 exit_cleanup(RERR_PROTOCOL
);
2162 *lb
++ = len
/ 0x100 + 0x80;
2166 write_buf(f
, (char*)lenbuf
, lb
- lenbuf
+ 1);
2168 write_buf(f
, str
, len
);
2171 /* Send a file-list index using a byte-reduction method. */
2172 void write_ndx(int f
, int32 ndx
)
2174 static int32 prev_positive
= -1, prev_negative
= 1;
2175 int32 diff
, cnt
= 0;
2178 if (protocol_version
< 30 || read_batch
) {
2183 /* Send NDX_DONE as a single-byte 0 with no side effects. Send
2184 * negative nums as a positive after sending a leading 0xFF. */
2186 diff
= ndx
- prev_positive
;
2187 prev_positive
= ndx
;
2188 } else if (ndx
== NDX_DONE
) {
2193 b
[cnt
++] = (char)0xFF;
2195 diff
= ndx
- prev_negative
;
2196 prev_negative
= ndx
;
2199 /* A diff of 1 - 253 is sent as a one-byte diff; a diff of 254 - 32767
2200 * or 0 is sent as a 0xFE + a two-byte diff; otherwise we send 0xFE
2201 * & all 4 bytes of the (non-negative) num with the high-bit set. */
2202 if (diff
< 0xFE && diff
> 0)
2203 b
[cnt
++] = (char)diff
;
2204 else if (diff
< 0 || diff
> 0x7FFF) {
2205 b
[cnt
++] = (char)0xFE;
2206 b
[cnt
++] = (char)((ndx
>> 24) | 0x80);
2207 b
[cnt
++] = (char)ndx
;
2208 b
[cnt
++] = (char)(ndx
>> 8);
2209 b
[cnt
++] = (char)(ndx
>> 16);
2211 b
[cnt
++] = (char)0xFE;
2212 b
[cnt
++] = (char)(diff
>> 8);
2213 b
[cnt
++] = (char)diff
;
2215 write_buf(f
, b
, cnt
);
2218 /* Receive a file-list index using a byte-reduction method. */
2219 int32
read_ndx(int f
)
2221 static int32 prev_positive
= -1, prev_negative
= 1;
2222 int32
*prev_ptr
, num
;
2225 if (protocol_version
< 30)
2229 if (CVAL(b
, 0) == 0xFF) {
2231 prev_ptr
= &prev_negative
;
2232 } else if (CVAL(b
, 0) == 0)
2235 prev_ptr
= &prev_positive
;
2236 if (CVAL(b
, 0) == 0xFE) {
2238 if (CVAL(b
, 0) & 0x80) {
2239 b
[3] = CVAL(b
, 0) & ~0x80;
2241 read_buf(f
, b
+1, 2);
2244 num
= (UVAL(b
,0)<<8) + UVAL(b
,1) + *prev_ptr
;
2246 num
= UVAL(b
, 0) + *prev_ptr
;
2248 if (prev_ptr
== &prev_negative
)
2253 /* Read a line of up to bufsiz-1 characters into buf. Strips
2254 * the (required) trailing newline and all carriage returns.
2255 * Returns 1 for success; 0 for I/O error or truncation. */
2256 int read_line_old(int fd
, char *buf
, size_t bufsiz
, int eof_ok
)
2258 assert(fd
!= iobuf
.in_fd
);
2259 bufsiz
--; /* leave room for the null */
2260 while (bufsiz
> 0) {
2261 if (safe_read(fd
, buf
, 1) == 0) {
2279 void io_printf(int fd
, const char *format
, ...)
2282 char buf
[BIGPATHBUFLEN
];
2285 va_start(ap
, format
);
2286 len
= vsnprintf(buf
, sizeof buf
, format
, ap
);
2290 exit_cleanup(RERR_PROTOCOL
);
2292 if (len
>= (int)sizeof buf
) {
2293 rprintf(FERROR
, "io_printf() was too long for the buffer.\n");
2294 exit_cleanup(RERR_PROTOCOL
);
2297 write_sbuf(fd
, buf
);
2300 /* Setup for multiplexing a MSG_* stream with the data stream. */
2301 void io_start_multiplex_out(int fd
)
2303 io_flush(FULL_FLUSH
);
2305 if (msgs2stderr
&& DEBUG_GTE(IO
, 2))
2306 rprintf(FINFO
, "[%s] io_start_multiplex_out(%d)\n", who_am_i(), fd
);
2309 alloc_xbuf(&iobuf
.msg
, ROUND_UP_1024(IO_BUFFER_SIZE
));
2311 iobuf
.out_empty_len
= 4; /* See also OUT_MULTIPLEXED */
2312 io_start_buffering_out(fd
);
2313 got_kill_signal
= 0;
2315 iobuf
.raw_data_header_pos
= iobuf
.out
.pos
+ iobuf
.out
.len
;
2319 /* Setup for multiplexing a MSG_* stream with the data stream. */
2320 void io_start_multiplex_in(int fd
)
2322 if (msgs2stderr
&& DEBUG_GTE(IO
, 2))
2323 rprintf(FINFO
, "[%s] io_start_multiplex_in(%d)\n", who_am_i(), fd
);
2325 iobuf
.in_multiplexed
= 1; /* See also IN_MULTIPLEXED */
2326 io_start_buffering_in(fd
);
2329 int io_end_multiplex_in(int mode
)
2331 int ret
= iobuf
.in_multiplexed
? iobuf
.in_fd
: -1;
2333 if (msgs2stderr
&& DEBUG_GTE(IO
, 2))
2334 rprintf(FINFO
, "[%s] io_end_multiplex_in(mode=%d)\n", who_am_i(), mode
);
2336 iobuf
.in_multiplexed
= 0;
2337 if (mode
== MPLX_SWITCHING
)
2338 iobuf
.raw_input_ends_before
= 0;
2340 assert(iobuf
.raw_input_ends_before
== 0);
2341 if (mode
!= MPLX_TO_BUFFERED
)
2342 io_end_buffering_in(mode
);
2347 int io_end_multiplex_out(int mode
)
2349 int ret
= iobuf
.out_empty_len
? iobuf
.out_fd
: -1;
2351 if (msgs2stderr
&& DEBUG_GTE(IO
, 2))
2352 rprintf(FINFO
, "[%s] io_end_multiplex_out(mode=%d)\n", who_am_i(), mode
);
2354 if (mode
!= MPLX_TO_BUFFERED
)
2355 io_end_buffering_out(mode
);
2357 io_flush(FULL_FLUSH
);
2360 iobuf
.out_empty_len
= 0;
2361 if (got_kill_signal
> 0) /* Just in case... */
2362 handle_kill_signal(False
);
2363 got_kill_signal
= -1;
2368 void start_write_batch(int fd
)
2370 /* Some communication has already taken place, but we don't
2371 * enable batch writing until here so that we can write a
2372 * canonical record of the communication even though the
2373 * actual communication so far depends on whether a daemon
2375 write_int(batch_fd
, protocol_version
);
2376 if (protocol_version
>= 30)
2377 write_varint(batch_fd
, compat_flags
);
2378 write_int(batch_fd
, checksum_seed
);
2381 write_batch_monitor_out
= fd
;
2383 write_batch_monitor_in
= fd
;
2386 void stop_write_batch(void)
2388 write_batch_monitor_out
= -1;
2389 write_batch_monitor_in
= -1;