Change a variable name in read_a_msg().
[rsync/rsync.git] / io.c
CommitLineData
0f78b815
WD
1/*
2 * Socket and pipe I/O utilities used in rsync.
d62bcc17 3 *
0f78b815
WD
4 * Copyright (C) 1996-2001 Andrew Tridgell
5 * Copyright (C) 1996 Paul Mackerras
6 * Copyright (C) 2001, 2002 Martin Pool <mbp@samba.org>
b3bf9b9d 7 * Copyright (C) 2003-2009 Wayne Davison
d62bcc17 8 *
880da007 9 * This program is free software; you can redistribute it and/or modify
8e41b68e
WD
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.
d62bcc17 13 *
880da007
MP
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.
d62bcc17 18 *
e7c67065 19 * You should have received a copy of the GNU General Public License along
4fd842f9 20 * with this program; if not, visit the http://fsf.org website.
880da007 21 */
720b47f2 22
0f78b815
WD
23/* Rsync provides its own multiplexing system, which is used to send
24 * stderr and stdout over a single socket.
87ee2481
MP
25 *
26 * For historical reasons this is off during the start of the
27 * connection, but it's switched on quite early using
0f78b815 28 * io_start_multiplex_out() and io_start_multiplex_in(). */
720b47f2 29
720b47f2 30#include "rsync.h"
1b42f628 31#include "ifuncs.h"
5dd14f0c 32#include "inums.h"
720b47f2 33
880da007 34/** If no timeout is specified then use a 60 second select timeout */
8cd9fd4e
AT
35#define SELECT_TIMEOUT 60
36
7a55d06e 37extern int bwlimit;
71e58630 38extern size_t bwlimit_writemax;
6ba9279f 39extern int io_timeout;
d17e1dd2
WD
40extern int am_server;
41extern int am_daemon;
42extern int am_sender;
98f8c9a5 43extern int am_generator;
20caffd2 44extern int msgs2stderr;
3ea6e0e7 45extern int inc_recurse;
8ef246e0 46extern int io_error;
e626b29e 47extern int eol_nulls;
8ef246e0 48extern int flist_eof;
ce827c3e
WD
49extern int file_total;
50extern int file_old_total;
7f9bf6b7 51extern int list_only;
3b0a30eb 52extern int read_batch;
ba525f77 53extern int protect_args;
b9f592fb
WD
54extern int checksum_seed;
55extern int protocol_version;
47c11975 56extern int remove_source_files;
cdf236aa 57extern int preserve_hard_links;
7a7810aa 58extern BOOL extra_flist_sending_enabled;
a800434a 59extern struct stats stats;
19531e1f 60extern struct file_list *cur_flist;
332cf6df 61#ifdef ICONV_OPTION
ba525f77 62extern int filesfrom_convert;
332cf6df
WD
63extern iconv_t ic_send, ic_recv;
64#endif
720b47f2 65
93465f51
WD
66int csum_length = SHORT_SUM_LENGTH; /* initial value */
67int allowed_lull = 0;
9e2409ab 68int ignore_timeout = 0;
b9f592fb 69int batch_fd = -1;
04c722d5 70int msgdone_cnt = 0;
20caffd2 71int forward_flist_data = 0;
2885270b 72BOOL flist_receiving_enabled = False;
805edf9d 73
cb2e1f18 74/* Ignore an EOF error if non-zero. See whine_about_eof(). */
574c2500 75int kluge_around_eof = 0;
7a55d06e 76
cdf236aa
WD
77int sock_f_in = -1;
78int sock_f_out = -1;
7a55d06e 79
4dde3347
WD
80int64 total_data_read = 0;
81int64 total_data_written = 0;
82
20caffd2
WD
83static struct {
84 xbuf in, out, msg;
85 int in_fd;
86 int out_fd; /* Both "out" and "msg" go to this fd. */
87 BOOL in_multiplexed;
88 unsigned out_empty_len;
89 size_t raw_data_header_pos; /* in the out xbuf */
90 size_t raw_flushing_ends_before; /* in the out xbuf */
91 size_t raw_input_ends_before; /* in the in xbuf */
92} iobuf = { .in_fd = -1, .out_fd = -1 };
8ef246e0 93
3b0a30eb
WD
94static time_t last_io_in;
95static time_t last_io_out;
1f75bb10 96
b9f592fb
WD
97static int write_batch_monitor_in = -1;
98static int write_batch_monitor_out = -1;
99
20caffd2 100static int ff_forward_fd = -1;
8e6b4ddb
WD
101static int ff_reenable_multiplex = -1;
102static char ff_lastchar = '\0';
cbc63a09 103static xbuf ff_xb = EMPTY_XBUF;
332cf6df 104#ifdef ICONV_OPTION
ba525f77 105static xbuf iconv_buf = EMPTY_XBUF;
332cf6df 106#endif
3b0a30eb 107static int select_timeout = SELECT_TIMEOUT;
d6081c82
WD
108static int active_filecnt = 0;
109static OFF_T active_bytecnt = 0;
7f9bf6b7 110static int first_message = 1;
720b47f2 111
351e23ad
WD
112static char int_byte_extra[64] = {
113 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* (00 - 3F)/4 */
114 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* (40 - 7F)/4 */
115 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* (80 - BF)/4 */
116 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 5, 6, /* (C0 - FF)/4 */
482f48cc
WD
117};
118
907e6a32
WD
119/* Our I/O buffers are sized with no bits on in the lowest byte of the "size"
120 * (indeed, our rounding of sizes in 1024-byte units assures more than this).
121 * This allows the code that is storing bytes near the physical end of a
122 * circular buffer to temporarily reduce the buffer's size (in order to make
123 * some storing idioms easier), while also making it simple to restore the
124 * buffer's actual size when the buffer's "pos" wraps around to the start (we
125 * just round the buffer's size up again). */
126
127#define IOBUF_WAS_REDUCED(siz) ((siz) & 0xFF)
128#define IOBUF_RESTORE_SIZE(siz) (((siz) | 0xFF) + 1)
129
20caffd2
WD
130#define IN_MULTIPLEXED (iobuf.in_multiplexed)
131#define OUT_MULTIPLEXED (iobuf.out_empty_len != 0)
29349024 132
20caffd2
WD
133#define PIO_NEED_INPUT (1<<0) /* The *_NEED_* flags are mutually exclusive. */
134#define PIO_NEED_OUTROOM (1<<1)
135#define PIO_NEED_MSGROOM (1<<2)
ff41a59f 136
20caffd2 137#define PIO_CONSUME_INPUT (1<<4) /* Must becombined with PIO_NEED_INPUT. */
d17e1dd2 138
20caffd2
WD
139#define PIO_INPUT_AND_CONSUME (PIO_NEED_INPUT | PIO_CONSUME_INPUT)
140#define PIO_NEED_FLAGS (PIO_NEED_INPUT | PIO_NEED_OUTROOM | PIO_NEED_MSGROOM)
d17e1dd2 141
20caffd2
WD
142#define REMOTE_OPTION_ERROR "rsync: on remote machine: -"
143#define REMOTE_OPTION_ERROR2 ": unknown option"
08c88178 144
20caffd2 145#define FILESFROM_BUFLEN 2048
d17e1dd2 146
20caffd2 147enum festatus { FES_SUCCESS, FES_REDO, FES_NO_SEND };
29349024 148
20caffd2 149static flist_ndx_list redo_list, hlink_list;
29349024 150
2885270b 151static void read_a_msg(void);
ae598f38 152static void drain_multiplex_messages(void);
20caffd2 153static void sleep_for_bwlimit(int bytes_written);
29349024 154
8d9dc9f9
AT
155static void check_timeout(void)
156{
157 time_t t;
90ba34e2 158
9e2409ab 159 if (!io_timeout || ignore_timeout)
d17e1dd2 160 return;
8d9dc9f9 161
3b0a30eb
WD
162 if (!last_io_in) {
163 last_io_in = time(NULL);
8d9dc9f9
AT
164 return;
165 }
166
167 t = time(NULL);
168
3b0a30eb 169 if (t - last_io_in >= io_timeout) {
0adb99b9 170 if (!am_server && !am_daemon) {
4ccfd96c 171 rprintf(FERROR, "io timeout after %d seconds -- exiting\n",
3b0a30eb 172 (int)(t-last_io_in));
0adb99b9 173 }
65417579 174 exit_cleanup(RERR_TIMEOUT);
8d9dc9f9
AT
175 }
176}
177
20caffd2
WD
178/* It's almost always an error to get an EOF when we're trying to read from the
179 * network, because the protocol is (for the most part) self-terminating.
180 *
181 * There is one case for the receiver when it is at the end of the transfer
182 * (hanging around reading any keep-alive packets that might come its way): if
183 * the sender dies before the generator's kill-signal comes through, we can end
184 * up here needing to loop until the kill-signal arrives. In this situation,
185 * kluge_around_eof will be < 0.
186 *
187 * There is another case for older protocol versions (< 24) where the module
188 * listing was not terminated, so we must ignore an EOF error in that case and
189 * exit. In this situation, kluge_around_eof will be > 0. */
ae598f38 190static NORETURN void whine_about_eof(BOOL allow_kluge)
d17e1dd2 191{
ae598f38 192 if (kluge_around_eof && allow_kluge) {
20caffd2
WD
193 int i;
194 if (kluge_around_eof > 0)
195 exit_cleanup(0);
196 /* If we're still here after 10 seconds, exit with an error. */
197 for (i = 10*1000/20; i--; )
198 msleep(20);
199 }
d17e1dd2 200
20caffd2
WD
201 rprintf(FERROR, RSYNC_NAME ": connection unexpectedly closed "
202 "(%s bytes received so far) [%s]\n",
203 big_num(stats.total_read), who_am_i());
d17e1dd2 204
20caffd2 205 exit_cleanup(RERR_STREAMIO);
554e0a8d
AT
206}
207
20caffd2
WD
208/* Do a safe read, handling any needed looping and error handling.
209 * Returns the count of the bytes read, which will only be different
210 * from "len" if we encountered an EOF. This routine is not used on
211 * the socket except very early in the transfer. */
212static size_t safe_read(int fd, char *buf, size_t len)
0b789446 213{
20caffd2
WD
214 size_t got;
215 int n;
0b789446 216
20caffd2 217 assert(fd != iobuf.in_fd);
0b789446 218
20caffd2
WD
219 n = read(fd, buf, len);
220 if ((size_t)n == len || n == 0) {
221 if (DEBUG_GTE(IO, 2))
222 rprintf(FINFO, "[%s] safe_read(%d)=%ld\n", who_am_i(), fd, (long)n);
223 return n;
3be97bf9 224 }
20caffd2
WD
225 if (n < 0) {
226 if (errno != EINTR && errno != EWOULDBLOCK && errno != EAGAIN) {
227 read_failed:
228 rsyserr(FERROR, errno, "safe_read failed to read %ld bytes [%s]",
229 (long)len, who_am_i());
230 exit_cleanup(RERR_STREAMIO);
231 }
232 got = 0;
233 } else
234 got = n;
3be97bf9 235
20caffd2
WD
236 while (1) {
237 struct timeval tv;
238 fd_set r_fds, e_fds;
239 int cnt;
7f9bf6b7 240
20caffd2
WD
241 FD_ZERO(&r_fds);
242 FD_SET(fd, &r_fds);
243 FD_ZERO(&e_fds);
244 FD_SET(fd, &e_fds);
245 tv.tv_sec = select_timeout;
246 tv.tv_usec = 0;
7f9bf6b7 247
20caffd2
WD
248 cnt = select(fd+1, &r_fds, NULL, &e_fds, &tv);
249 if (cnt <= 0) {
250 if (cnt < 0 && errno == EBADF) {
251 rsyserr(FERROR, errno, "safe_read select failed [%s]",
252 who_am_i());
253 exit_cleanup(RERR_FILEIO);
254 }
255 check_timeout();
256 continue;
257 }
7f9bf6b7 258
20caffd2
WD
259 /*if (FD_ISSET(fd, &e_fds))
260 rprintf(FINFO, "select exception on fd %d\n", fd); */
7f9bf6b7 261
20caffd2
WD
262 if (FD_ISSET(fd, &r_fds)) {
263 n = read(fd, buf + got, len - got);
264 if (DEBUG_GTE(IO, 2))
265 rprintf(FINFO, "[%s] safe_read(%d)=%ld\n", who_am_i(), fd, (long)n);
266 if (n == 0)
267 break;
268 if (n < 0) {
269 if (errno == EINTR)
270 continue;
271 goto read_failed;
272 }
273 if ((got += (size_t)n) == len)
274 break;
275 }
7f9bf6b7 276 }
20caffd2
WD
277
278 return got;
7f9bf6b7
WD
279}
280
20caffd2 281static const char *what_fd_is(int fd)
d6081c82 282{
20caffd2 283 static char buf[20];
d6081c82 284
20caffd2
WD
285 if (fd == sock_f_out)
286 return "socket";
287 else if (fd == iobuf.out_fd)
288 return "message fd";
289 else if (fd == batch_fd)
290 return "batch file";
291 else {
292 snprintf(buf, sizeof buf, "fd %d", fd);
293 return buf;
294 }
d6081c82
WD
295}
296
20caffd2
WD
297/* Do a safe write, handling any needed looping and error handling.
298 * Returns only if everything was successfully written. This routine
299 * is not used on the socket except very early in the transfer. */
300static void safe_write(int fd, const char *buf, size_t len)
d17e1dd2 301{
20caffd2 302 int n;
ee6e80c7 303
20caffd2 304 assert(fd != iobuf.out_fd);
332cf6df 305
20caffd2
WD
306 n = write(fd, buf, len);
307 if ((size_t)n == len)
308 return;
309 if (n < 0) {
310 if (errno != EINTR && errno != EWOULDBLOCK && errno != EAGAIN) {
311 write_failed:
312 rsyserr(FERROR, errno,
313 "safe_write failed to write %ld bytes to %s [%s]",
314 (long)len, what_fd_is(fd), who_am_i());
315 exit_cleanup(RERR_STREAMIO);
ee6e80c7 316 }
20caffd2
WD
317 } else {
318 buf += n;
319 len -= n;
320 }
ee6e80c7 321
20caffd2
WD
322 while (len) {
323 struct timeval tv;
324 fd_set w_fds;
325 int cnt;
3be97bf9 326
20caffd2
WD
327 FD_ZERO(&w_fds);
328 FD_SET(fd, &w_fds);
329 tv.tv_sec = select_timeout;
330 tv.tv_usec = 0;
17a4977b 331
20caffd2
WD
332 cnt = select(fd + 1, NULL, &w_fds, NULL, &tv);
333 if (cnt <= 0) {
334 if (cnt < 0 && errno == EBADF) {
335 rsyserr(FERROR, errno, "safe_write select failed on %s [%s]",
336 what_fd_is(fd), who_am_i());
337 exit_cleanup(RERR_FILEIO);
338 }
339 check_timeout();
340 continue;
341 }
d17e1dd2 342
20caffd2
WD
343 if (FD_ISSET(fd, &w_fds)) {
344 n = write(fd, buf, len);
345 if (n < 0) {
346 if (errno == EINTR)
347 continue;
348 goto write_failed;
349 }
350 buf += n;
351 len -= n;
352 }
37f35d89 353 }
37f35d89
WD
354}
355
20caffd2
WD
356/* This is only called when files-from data is known to be available. We read
357 * a chunk of data and put it into the output buffer. */
358static void forward_filesfrom_data(void)
155d9206 359{
20caffd2 360 int len;
20caffd2 361
cbc63a09 362 len = read(ff_forward_fd, ff_xb.buf + ff_xb.len, ff_xb.size - ff_xb.len);
20caffd2
WD
363 if (len <= 0) {
364 if (len == 0 || errno != EINTR) {
365 /* Send end-of-file marker */
20caffd2 366 ff_forward_fd = -1;
cbc63a09
WD
367 write_buf(iobuf.out_fd, "\0\0", ff_lastchar ? 2 : 1);
368 free_xbuf(&ff_xb);
8e6b4ddb
WD
369 if (ff_reenable_multiplex >= 0)
370 io_start_multiplex_out(ff_reenable_multiplex);
20caffd2 371 }
0b789446 372 return;
20caffd2 373 }
554e0a8d 374
20caffd2
WD
375 if (DEBUG_GTE(IO, 2))
376 rprintf(FINFO, "[%s] files-from read=%ld\n", who_am_i(), (long)len);
554e0a8d 377
cbc63a09
WD
378#ifdef ICONV_OPTION
379 len += ff_xb.len;
380#endif
381
20caffd2 382 if (!eol_nulls) {
cbc63a09 383 char *s = ff_xb.buf + len;
20caffd2 384 /* Transform CR and/or LF into '\0' */
cbc63a09 385 while (s-- > ff_xb.buf) {
20caffd2
WD
386 if (*s == '\n' || *s == '\r')
387 *s = '\0';
388 }
389 }
cbc63a09 390
20caffd2 391 if (ff_lastchar)
cbc63a09 392 ff_xb.pos = 0;
20caffd2 393 else {
cbc63a09 394 char *s = ff_xb.buf;
20caffd2
WD
395 /* Last buf ended with a '\0', so don't let this buf start with one. */
396 while (len && *s == '\0')
397 s++, len--;
cbc63a09 398 ff_xb.pos = s - ff_xb.buf;
20caffd2 399 }
cbc63a09
WD
400
401#ifdef ICONV_OPTION
402 if (filesfrom_convert && len) {
403 char *sob = ff_xb.buf + ff_xb.pos, *s = sob;
404 char *eob = sob + len;
405 int flags = ICB_INCLUDE_BAD | ICB_INCLUDE_INCOMPLETE | ICB_CIRCULAR_OUT;
406 if (ff_lastchar == '\0')
407 flags |= ICB_INIT;
408 /* Convert/send each null-terminated string separately, skipping empties. */
409 while (s != eob) {
410 if (*s++ == '\0') {
411 ff_xb.len = s - sob - 1;
412 if (iconvbufs(ic_send, &ff_xb, &iobuf.out, flags) < 0)
413 exit_cleanup(RERR_PROTOCOL); /* impossible? */
414 write_buf(iobuf.out_fd, s-1, 1); /* Send the '\0'. */
415 while (s != eob && *s == '\0')
416 s++;
417 sob = s;
418 ff_xb.pos = sob - ff_xb.buf;
419 flags |= ICB_INIT;
420 }
421 }
422
423 if ((ff_xb.len = s - sob) == 0)
424 ff_lastchar = '\0';
425 else {
426 /* Handle a partial string specially, saving any incomplete chars. */
427 flags &= ~ICB_INCLUDE_INCOMPLETE;
428 if (iconvbufs(ic_send, &ff_xb, &iobuf.out, flags) < 0) {
429 if (errno == E2BIG)
430 exit_cleanup(RERR_PROTOCOL); /* impossible? */
431 if (ff_xb.pos)
432 memmove(ff_xb.buf, ff_xb.buf + ff_xb.pos, ff_xb.len);
433 }
434 ff_lastchar = 'x'; /* Anything non-zero. */
435 }
436 } else
437#endif
438
20caffd2 439 if (len) {
cbc63a09
WD
440 char *f = ff_xb.buf + ff_xb.pos;
441 char *t = ff_xb.buf;
20caffd2
WD
442 char *eob = f + len;
443 /* Eliminate any multi-'\0' runs. */
444 while (f != eob) {
445 if (!(*t++ = *f++)) {
cbc63a09
WD
446 while (f != eob && *f == '\0')
447 f++;
20caffd2
WD
448 }
449 }
450 ff_lastchar = f[-1];
cbc63a09
WD
451 if ((len = t - ff_xb.buf) != 0) {
452 /* This will not circle back to perform_io() because we only get
453 * called when there is plenty of room in the output buffer. */
454 write_buf(iobuf.out_fd, ff_xb.buf, len);
455 }
20caffd2 456 }
56014c8c 457}
720b47f2 458
907e6a32
WD
459void reduce_iobuf_size(xbuf *out, size_t new_size)
460{
461 if (new_size < out->size) {
462 if (DEBUG_GTE(IO, 4)) {
463 const char *name = out == &iobuf.out ? "iobuf.out"
464 : out == &iobuf.msg ? "iobuf.msg"
465 : NULL;
466 if (name) {
467 rprintf(FINFO, "[%s] reduced size of %s (-%d)\n",
468 who_am_i(), name, (int)(out->size - new_size));
469 }
470 }
471 out->size = new_size;
472 }
473}
474
475void restore_iobuf_size(xbuf *out)
476{
477 if (IOBUF_WAS_REDUCED(out->size)) {
478 size_t new_size = IOBUF_RESTORE_SIZE(out->size);
479 if (DEBUG_GTE(IO, 4)) {
480 const char *name = out == &iobuf.out ? "iobuf.out"
481 : out == &iobuf.msg ? "iobuf.msg"
482 : NULL;
483 if (name) {
484 rprintf(FINFO, "[%s] restored size of %s (+%d)\n",
485 who_am_i(), name, (int)(new_size - out->size));
486 }
487 }
488 out->size = new_size;
489 }
490}
491
20caffd2
WD
492/* Perform buffered input and output until specified conditions are met. When
493 * given a "needed" read requirement, we'll return without doing any I/O if the
494 * iobuf.in bytes are already available. When reading, we'll read as many
495 * bytes as we can into the buffer, and return as soon as we meet the minimum
496 * read requirement. When given a "needed" write requirement, we'll return
497 * without doing any I/O if that many bytes will fit in the output buffer (we
498 * check either iobuf.out or iobuf.msg, depending on the flags). When writing,
499 * we write out as much as we can, and return as soon as the given free-space
500 * requirement is available.
880da007 501 *
20caffd2
WD
502 * The iobuf.out and iobuf.msg buffers are circular, so some writes into them
503 * will need to be split when the data needs to wrap around to the start. In
504 * order to help make this easier for some operations (such as the use of
763880ba
WD
505 * SIVAL() into the buffer) a buffer may be temporarily shortened, but the
506 * original size will be automatically restored. The iobuf.in buffer is also
507 * circular, so callers may need to split their reading of the data if it spans
508 * the end. See also the 3 raw_* iobuf vars that are used in the handling of
509 * MSG_DATA bytes as they are read-from/written-into the buffers.
cb2e1f18 510 *
20caffd2
WD
511 * When writing, we flush data in the following priority order:
512 *
513 * 1. Finish writing any in-progress MSG_DATA sequence from iobuf.out.
514 *
515 * 2. Write out all the messages from the message buf (if iobuf.msg is active).
516 * Yes, this means that a PIO_NEED_OUTROOM call will completely flush any
517 * messages before getting to the iobuf.out flushing (except for rule 1).
518 *
519 * 3. Write out the raw data from iobuf.out, possibly filling in the multiplexed
520 * MSG_DATA header that was pre-allocated (when output is multiplexed).
521 *
522 * TODO: items for possible future work:
523 *
524 * - Make this routine able to read the generator-to-receiver batch flow?
525 *
20caffd2
WD
526 * Unlike the old routines that this replaces, it is OK to read ahead as far as
527 * we can because the read_a_msg() routine now reads its bytes out of the input
528 * buffer. In the old days, only raw data was in the input buffer, and any
529 * unused raw data in the buf would prevent the reading of socket data. */
530static char *perform_io(size_t needed, int flags)
7a55d06e 531{
20caffd2
WD
532 fd_set r_fds, e_fds, w_fds;
533 struct timeval tv;
534 int cnt, max_fd;
535 size_t empty_buf_len = 0;
536 xbuf *out;
537 char *data;
538
539 if (iobuf.in.len == 0 && iobuf.in.pos != 0) {
540 if (iobuf.raw_input_ends_before)
541 iobuf.raw_input_ends_before -= iobuf.in.pos;
542 iobuf.in.pos = 0;
574c2500 543 }
3151cbae 544
20caffd2
WD
545 switch (flags & PIO_NEED_FLAGS) {
546 case PIO_NEED_INPUT:
763880ba
WD
547 /* We never resize the circular input buffer. */
548 if (iobuf.in.size < needed) {
549 rprintf(FERROR, "need to read %ld bytes, iobuf.in.buf is only %ld bytes.\n",
550 (long)needed, (long)iobuf.in.size);
551 exit_cleanup(RERR_PROTOCOL);
552 }
553
20caffd2
WD
554 if (DEBUG_GTE(IO, 3)) {
555 rprintf(FINFO, "[%s] perform_io(%ld, %sinput)\n",
556 who_am_i(), (long)needed, flags & PIO_CONSUME_INPUT ? "consume&" : "");
557 }
20caffd2 558 break;
720b47f2 559
20caffd2
WD
560 case PIO_NEED_OUTROOM:
561 /* We never resize the circular output buffer. */
562 if (iobuf.out.size - iobuf.out_empty_len < needed) {
563 fprintf(stderr, "need to write %ld bytes, iobuf.out.buf is only %ld bytes.\n",
564 (long)needed, (long)(iobuf.out.size - iobuf.out_empty_len));
565 exit_cleanup(RERR_PROTOCOL);
566 }
4c36ddbe 567
20caffd2
WD
568 if (DEBUG_GTE(IO, 3)) {
569 rprintf(FINFO, "[%s] perform_io(%ld, outroom) needs to flush %ld\n",
570 who_am_i(), (long)needed,
b82d8c9d
WD
571 iobuf.out.len + needed > iobuf.out.size
572 ? (long)(iobuf.out.len + needed - iobuf.out.size) : 0L);
20caffd2
WD
573 }
574 break;
ea2111d1 575
20caffd2
WD
576 case PIO_NEED_MSGROOM:
577 /* We never resize the circular message buffer. */
578 if (iobuf.msg.size < needed) {
579 fprintf(stderr, "need to write %ld bytes, iobuf.msg.buf is only %ld bytes.\n",
580 (long)needed, (long)iobuf.msg.size);
581 exit_cleanup(RERR_PROTOCOL);
582 }
583
584 if (DEBUG_GTE(IO, 3)) {
585 rprintf(FINFO, "[%s] perform_io(%ld, msgroom) needs to flush %ld\n",
586 who_am_i(), (long)needed,
b82d8c9d
WD
587 iobuf.msg.len + needed > iobuf.msg.size
588 ? (long)(iobuf.msg.len + needed - iobuf.msg.size) : 0L);
20caffd2
WD
589 }
590 break;
591
592 case 0:
593 if (DEBUG_GTE(IO, 3))
594 rprintf(FINFO, "[%s] perform_io(%ld, %d)\n", who_am_i(), (long)needed, flags);
595 break;
596
597 default:
598 exit_cleanup(RERR_UNSUPPORTED);
599 }
600
601 while (1) {
602 switch (flags & PIO_NEED_FLAGS) {
603 case PIO_NEED_INPUT:
604 if (iobuf.in.len >= needed)
605 goto double_break;
606 break;
607 case PIO_NEED_OUTROOM:
b82d8c9d
WD
608 /* Note that iobuf.out_empty_len doesn't factor into this check
609 * because iobuf.out.len already holds any needed header len. */
610 if (iobuf.out.len + needed <= iobuf.out.size)
20caffd2
WD
611 goto double_break;
612 break;
613 case PIO_NEED_MSGROOM:
b82d8c9d 614 if (iobuf.msg.len + needed <= iobuf.msg.size)
20caffd2
WD
615 goto double_break;
616 break;
617 }
618
619 max_fd = -1;
4c36ddbe 620
56014c8c 621 FD_ZERO(&r_fds);
20caffd2 622 FD_ZERO(&e_fds);
763880ba 623 if (iobuf.in_fd >= 0 && iobuf.in.size - iobuf.in.len) {
20caffd2
WD
624 if (!read_batch || batch_fd >= 0) {
625 FD_SET(iobuf.in_fd, &r_fds);
626 FD_SET(iobuf.in_fd, &e_fds);
627 }
628 if (iobuf.in_fd > max_fd)
629 max_fd = iobuf.in_fd;
630 }
631
632 /* Only do more filesfrom processing if there is enough room in the out buffer. */
633 if (ff_forward_fd >= 0 && iobuf.out.size - iobuf.out.len > FILESFROM_BUFLEN*2) {
634 FD_SET(ff_forward_fd, &r_fds);
635 if (ff_forward_fd > max_fd)
636 max_fd = ff_forward_fd;
637 }
638
a7026ba9 639 FD_ZERO(&w_fds);
20caffd2
WD
640 if (iobuf.out_fd >= 0) {
641 if (iobuf.raw_flushing_ends_before
642 || (!iobuf.msg.len && iobuf.out.len > iobuf.out_empty_len && !(flags & PIO_NEED_MSGROOM))) {
643 if (OUT_MULTIPLEXED && !iobuf.raw_flushing_ends_before) {
20caffd2
WD
644 /* The iobuf.raw_flushing_ends_before value can point off the end
645 * of the iobuf.out buffer for a while, for easier subtracting. */
646 iobuf.raw_flushing_ends_before = iobuf.out.pos + iobuf.out.len;
647
648 SIVAL(iobuf.out.buf + iobuf.raw_data_header_pos, 0,
649 ((MPLEX_BASE + (int)MSG_DATA)<<24) + iobuf.out.len - 4);
20caffd2
WD
650
651 if (DEBUG_GTE(IO, 1)) {
652 rprintf(FINFO, "[%s] send_msg(%d, %ld)\n",
653 who_am_i(), (int)MSG_DATA, (long)iobuf.out.len - 4);
654 }
655
656 /* reserve room for the next MSG_DATA header */
657 iobuf.raw_data_header_pos = iobuf.raw_flushing_ends_before;
658 if (iobuf.raw_data_header_pos >= iobuf.out.size)
659 iobuf.raw_data_header_pos -= iobuf.out.size;
907e6a32
WD
660 else if (iobuf.raw_data_header_pos + 4 > iobuf.out.size) {
661 /* The 4-byte header won't fit at the end of the buffer,
662 * so we'll temporarily reduce the output buffer's size
663 * and put the header at the start of the buffer. */
664 reduce_iobuf_size(&iobuf.out, iobuf.raw_data_header_pos);
665 iobuf.raw_data_header_pos = 0;
666 }
75ea8459 667 /* Yes, it is possible for this to make len > size for a while. */
20caffd2 668 iobuf.out.len += 4;
56014c8c 669 }
20caffd2
WD
670
671 empty_buf_len = iobuf.out_empty_len;
672 out = &iobuf.out;
673 } else if (iobuf.msg.len) {
674 empty_buf_len = 0;
675 out = &iobuf.msg;
676 } else
677 out = NULL;
678 if (out) {
679 FD_SET(iobuf.out_fd, &w_fds);
680 if (iobuf.out_fd > max_fd)
681 max_fd = iobuf.out_fd;
682 }
683 } else
684 out = NULL;
685
686 if (max_fd < 0) {
687 switch (flags & PIO_NEED_FLAGS) {
688 case PIO_NEED_INPUT:
ae598f38
WD
689 iobuf.in.len = 0;
690 if (kluge_around_eof == 2)
691 exit_cleanup(0);
692 if (iobuf.in_fd == -2)
693 whine_about_eof(True);
20caffd2
WD
694 rprintf(FERROR, "error in perform_io: no fd for input.\n");
695 exit_cleanup(RERR_PROTOCOL);
696 case PIO_NEED_OUTROOM:
697 case PIO_NEED_MSGROOM:
698 msgs2stderr = 1;
ae598f38
WD
699 drain_multiplex_messages();
700 if (iobuf.out_fd == -2)
701 whine_about_eof(True);
20caffd2
WD
702 rprintf(FERROR, "error in perform_io: no fd for output.\n");
703 exit_cleanup(RERR_PROTOCOL);
704 default:
705 /* No stated needs, so I guess this is OK. */
706 break;
56014c8c 707 }
20caffd2 708 break;
554e0a8d
AT
709 }
710
20caffd2 711 if (extra_flist_sending_enabled) {
56926577 712 if (file_total - file_old_total < MAX_FILECNT_LOOKAHEAD)
7a7810aa
WD
713 tv.tv_sec = 0;
714 else {
715 extra_flist_sending_enabled = False;
716 tv.tv_sec = select_timeout;
717 }
718 } else
ce827c3e 719 tv.tv_sec = select_timeout;
4c36ddbe
AT
720 tv.tv_usec = 0;
721
20caffd2 722 cnt = select(max_fd + 1, &r_fds, &w_fds, &e_fds, &tv);
554e0a8d 723
20caffd2
WD
724 if (cnt <= 0) {
725 if (cnt < 0 && errno == EBADF) {
726 msgs2stderr = 1;
554e0a8d 727 exit_cleanup(RERR_SOCKETIO);
2a5df862 728 }
20caffd2 729 if (extra_flist_sending_enabled) {
7a7810aa 730 extra_flist_sending_enabled = False;
ce827c3e 731 send_extra_file_list(sock_f_out, -1);
7a7810aa
WD
732 extra_flist_sending_enabled = !flist_eof;
733 } else
ce827c3e 734 check_timeout();
20caffd2
WD
735 FD_ZERO(&r_fds); /* Just in case... */
736 FD_ZERO(&w_fds);
4c36ddbe
AT
737 }
738
20caffd2 739 if (iobuf.in_fd >= 0 && FD_ISSET(iobuf.in_fd, &r_fds)) {
763880ba 740 size_t len, pos = iobuf.in.pos + iobuf.in.len;
20caffd2 741 int n;
763880ba
WD
742 if (pos >= iobuf.in.size) {
743 pos -= iobuf.in.size;
744 len = iobuf.in.size - iobuf.in.len;
745 } else
746 len = iobuf.in.size - pos;
20caffd2
WD
747 if ((n = read(iobuf.in_fd, iobuf.in.buf + pos, len)) <= 0) {
748 if (n == 0) {
ae598f38 749 /* Signal that input has become invalid. */
20caffd2 750 if (!read_batch || batch_fd < 0 || am_generator)
ae598f38 751 iobuf.in_fd = -2;
20caffd2
WD
752 batch_fd = -1;
753 continue;
754 }
755 if (errno == EINTR || errno == EWOULDBLOCK || errno == EAGAIN)
756 n = 0;
757 else {
758 /* Don't write errors on a dead socket. */
759 if (iobuf.in_fd == sock_f_in) {
760 if (am_sender)
761 msgs2stderr = 1;
762 rsyserr(FERROR_SOCKET, errno, "read error");
763 } else
764 rsyserr(FERROR, errno, "read error");
e4c598c8 765 exit_cleanup(RERR_SOCKETIO);
56014c8c 766 }
20caffd2
WD
767 }
768 if (msgs2stderr && DEBUG_GTE(IO, 2))
769 rprintf(FINFO, "[%s] recv=%ld\n", who_am_i(), (long)n);
770
771 if (io_timeout)
772 last_io_in = time(NULL);
773 stats.total_read += n;
774
775 iobuf.in.len += n;
776 }
777
778 if (iobuf.out_fd >= 0 && FD_ISSET(iobuf.out_fd, &w_fds)) {
779 size_t len = iobuf.raw_flushing_ends_before ? iobuf.raw_flushing_ends_before - out->pos : out->len;
780 int n;
781
782 if (bwlimit_writemax && len > bwlimit_writemax)
783 len = bwlimit_writemax;
784
785 if (out->pos + len > out->size)
786 len = out->size - out->pos;
787 if ((n = write(iobuf.out_fd, out->buf + out->pos, len)) <= 0) {
788 if (errno == EINTR || errno == EWOULDBLOCK || errno == EAGAIN)
789 n = 0;
790 else {
791 /* Don't write errors on a dead socket. */
792 msgs2stderr = 1;
ae598f38
WD
793 iobuf.out_fd = -2;
794 iobuf.out.len = iobuf.msg.len = iobuf.raw_flushing_ends_before = 0;
f9185203 795 rsyserr(FERROR_SOCKET, errno, "[%s] write error", who_am_i());
ae598f38 796 drain_multiplex_messages();
e4c598c8 797 exit_cleanup(RERR_SOCKETIO);
20caffd2
WD
798 }
799 }
800 if (msgs2stderr && DEBUG_GTE(IO, 2)) {
801 rprintf(FINFO, "[%s] %s sent=%ld\n",
802 who_am_i(), out == &iobuf.out ? "out" : "msg", (long)n);
803 }
804
805 if (io_timeout)
806 last_io_out = time(NULL);
807 stats.total_written += n;
808
809 if (bwlimit_writemax)
810 sleep_for_bwlimit(n);
811
812 if ((out->pos += n) == out->size) {
813 if (iobuf.raw_flushing_ends_before)
814 iobuf.raw_flushing_ends_before -= out->size;
815 out->pos = 0;
907e6a32 816 restore_iobuf_size(out);
d620b907 817 } else if (out->pos == iobuf.raw_flushing_ends_before)
20caffd2
WD
818 iobuf.raw_flushing_ends_before = 0;
819 if ((out->len -= n) == empty_buf_len) {
820 out->pos = 0;
907e6a32 821 restore_iobuf_size(out);
20caffd2
WD
822 if (empty_buf_len)
823 iobuf.raw_data_header_pos = 0;
824 }
825 }
826
2885270b
WD
827 /* We need to help prevent deadlock by doing what reading
828 * we can whenever we are here trying to write. */
829 if (IN_MULTIPLEXED && !(flags & PIO_NEED_INPUT)) {
830 while (!iobuf.raw_input_ends_before && iobuf.in.len > 512)
831 read_a_msg();
832 if (flist_receiving_enabled && iobuf.in.len > 512)
833 wait_for_receiver(); /* generator only */
834 }
835
20caffd2
WD
836 if (ff_forward_fd >= 0 && FD_ISSET(ff_forward_fd, &r_fds)) {
837 /* This can potentially flush all output and enable
838 * multiplexed output, so keep this last in the loop
839 * and be sure to not cache anything that would break
840 * such a change. */
841 forward_filesfrom_data();
842 }
843 }
844 double_break:
845
846 data = iobuf.in.buf + iobuf.in.pos;
847
848 if (flags & PIO_CONSUME_INPUT) {
849 iobuf.in.len -= needed;
850 iobuf.in.pos += needed;
763880ba
WD
851 if (iobuf.in.pos == iobuf.raw_input_ends_before)
852 iobuf.raw_input_ends_before = 0;
853 if (iobuf.in.pos >= iobuf.in.size) {
854 iobuf.in.pos -= iobuf.in.size;
855 if (iobuf.raw_input_ends_before)
856 iobuf.raw_input_ends_before -= iobuf.in.size;
857 }
20caffd2
WD
858 }
859
860 return data;
861}
862
763880ba
WD
863static void raw_read_buf(char *buf, size_t len)
864{
865 size_t pos = iobuf.in.pos;
866 char *data = perform_io(len, PIO_INPUT_AND_CONSUME);
867 if (iobuf.in.pos <= pos && len) {
868 size_t siz = len - iobuf.in.pos;
869 memcpy(buf, data, siz);
870 memcpy(buf + siz, iobuf.in.buf, iobuf.in.pos);
871 } else
872 memcpy(buf, data, len);
873}
874
875static int32 raw_read_int(void)
876{
877 char *data, buf[4];
878 if (iobuf.in.size - iobuf.in.pos >= 4)
879 data = perform_io(4, PIO_INPUT_AND_CONSUME);
880 else
881 raw_read_buf(data = buf, 4);
882 return IVAL(data, 0);
883}
884
f9185203
WD
885void noop_io_until_death(void)
886{
887 char buf[1024];
888
ae598f38 889 kluge_around_eof = 2;
e4c598c8
WD
890 /* Setting an I/O timeout ensures that if something inexplicably weird
891 * happens, we won't hang around forever. */
892 if (!io_timeout)
893 set_io_timeout(60);
f9185203
WD
894
895 while (1)
896 read_buf(iobuf.in_fd, buf, sizeof buf);
897}
898
20caffd2
WD
899/* Buffer a message for the multiplexed output stream. Is never used for MSG_DATA. */
900int send_msg(enum msgcode code, const char *buf, size_t len, int convert)
901{
902 char *hdr;
907e6a32 903 size_t needed, pos;
20caffd2
WD
904 BOOL want_debug = DEBUG_GTE(IO, 1) && convert >= 0 && (msgs2stderr || code != MSG_INFO);
905
906 if (!OUT_MULTIPLEXED)
907 return 0;
908
909 if (want_debug)
910 rprintf(FINFO, "[%s] send_msg(%d, %ld)\n", who_am_i(), (int)code, (long)len);
911
907e6a32
WD
912 /* When checking for enough free space for this message, we need to
913 * make sure that there is space for the 4-byte header, plus we'll
914 * assume that we may waste up to 3 bytes (if the header doesn't fit
915 * at the physical end of the buffer). */
22955408 916#ifdef ICONV_OPTION
20caffd2
WD
917 if (convert > 0 && ic_send == (iconv_t)-1)
918 convert = 0;
919 if (convert > 0) {
d620b907 920 /* Ensuring double-size room leaves space for maximal conversion expansion. */
907e6a32 921 needed = len*2 + 4 + 3;
20caffd2 922 } else
22955408 923#endif
907e6a32
WD
924 needed = len + 4 + 3;
925 if (iobuf.msg.len + needed > iobuf.msg.size)
926 perform_io(needed, PIO_NEED_MSGROOM);
20caffd2
WD
927
928 pos = iobuf.msg.pos + iobuf.msg.len; /* Must be set after any flushing. */
929 if (pos >= iobuf.msg.size)
930 pos -= iobuf.msg.size;
907e6a32
WD
931 else if (pos + 4 > iobuf.msg.size) {
932 /* The 4-byte header won't fit at the end of the buffer,
933 * so we'll temporarily reduce the message buffer's size
934 * and put the header at the start of the buffer. */
935 reduce_iobuf_size(&iobuf.msg, pos);
936 pos = 0;
937 }
20caffd2
WD
938 hdr = iobuf.msg.buf + pos;
939
d620b907 940 iobuf.msg.len += 4; /* Allocate room for the coming header bytes. */
20caffd2 941
22955408 942#ifdef ICONV_OPTION
20caffd2
WD
943 if (convert > 0) {
944 xbuf inbuf;
945
946 INIT_XBUF(inbuf, (char*)buf, len, (size_t)-1);
947
948 len = iobuf.msg.len;
949 iconvbufs(ic_send, &inbuf, &iobuf.msg,
d8a7290f 950 ICB_INCLUDE_BAD | ICB_INCLUDE_INCOMPLETE | ICB_CIRCULAR_OUT | ICB_INIT);
20caffd2
WD
951 if (inbuf.len > 0) {
952 rprintf(FERROR, "overflowed iobuf.msg buffer in send_msg");
953 exit_cleanup(RERR_UNSUPPORTED);
954 }
955 len = iobuf.msg.len - len;
956 } else
22955408 957#endif
20caffd2
WD
958 {
959 size_t siz;
960
961 if ((pos += 4) >= iobuf.msg.size)
962 pos -= iobuf.msg.size;
963
964 /* Handle a split copy if we wrap around the end of the circular buffer. */
965 if (pos >= iobuf.msg.pos && (siz = iobuf.msg.size - pos) < len) {
966 memcpy(iobuf.msg.buf + pos, buf, siz);
967 memcpy(iobuf.msg.buf, buf + siz, len - siz);
968 } else
969 memcpy(iobuf.msg.buf + pos, buf, len);
970
971 iobuf.msg.len += len;
972 }
973
974 SIVAL(hdr, 0, ((MPLEX_BASE + (int)code)<<24) + len);
20caffd2
WD
975
976 if (want_debug && convert > 0)
977 rprintf(FINFO, "[%s] converted msg len=%ld\n", who_am_i(), (long)len);
978
979 return 1;
980}
981
982void send_msg_int(enum msgcode code, int num)
983{
984 char numbuf[4];
985
986 if (DEBUG_GTE(IO, 1))
987 rprintf(FINFO, "[%s] send_msg_int(%d, %d)\n", who_am_i(), (int)code, num);
988
989 SIVAL(numbuf, 0, num);
990 send_msg(code, numbuf, 4, -1);
991}
992
993static void got_flist_entry_status(enum festatus status, int ndx)
994{
995 struct file_list *flist = flist_for_ndx(ndx, "got_flist_entry_status");
996
997 if (remove_source_files) {
998 active_filecnt--;
999 active_bytecnt -= F_LENGTH(flist->files[ndx - flist->ndx_start]);
1000 }
1001
1002 if (inc_recurse)
1003 flist->in_progress--;
1004
1005 switch (status) {
1006 case FES_SUCCESS:
1007 if (remove_source_files)
1008 send_msg_int(MSG_SUCCESS, ndx);
1009 if (preserve_hard_links) {
1010 struct file_struct *file = flist->files[ndx - flist->ndx_start];
1011 if (F_IS_HLINKED(file)) {
1012 flist_ndx_push(&hlink_list, ndx);
1013 flist->in_progress++;
56014c8c
WD
1014 }
1015 }
20caffd2
WD
1016 break;
1017 case FES_REDO:
1018 if (read_batch) {
1019 if (inc_recurse)
1020 flist->in_progress++;
1021 break;
1022 }
1023 if (inc_recurse)
1024 flist->to_redo++;
1025 flist_ndx_push(&redo_list, ndx);
1026 break;
1027 case FES_NO_SEND:
1028 break;
1029 }
1030}
1031
1032/* Note the fds used for the main socket (which might really be a pipe
1033 * for a local transfer, but we can ignore that). */
1034void io_set_sock_fds(int f_in, int f_out)
1035{
1036 sock_f_in = f_in;
1037 sock_f_out = f_out;
1038}
1039
1040void set_io_timeout(int secs)
1041{
1042 io_timeout = secs;
1043
1044 if (!io_timeout || io_timeout > SELECT_TIMEOUT)
1045 select_timeout = SELECT_TIMEOUT;
1046 else
1047 select_timeout = io_timeout;
1048
1049 allowed_lull = read_batch ? 0 : (io_timeout + 1) / 2;
1050}
1051
1052static void check_for_d_option_error(const char *msg)
1053{
1054 static char rsync263_opts[] = "BCDHIKLPRSTWabceghlnopqrtuvxz";
1055 char *colon;
1056 int saw_d = 0;
1057
1058 if (*msg != 'r'
1059 || strncmp(msg, REMOTE_OPTION_ERROR, sizeof REMOTE_OPTION_ERROR - 1) != 0)
1060 return;
56014c8c 1061
20caffd2
WD
1062 msg += sizeof REMOTE_OPTION_ERROR - 1;
1063 if (*msg == '-' || (colon = strchr(msg, ':')) == NULL
1064 || strncmp(colon, REMOTE_OPTION_ERROR2, sizeof REMOTE_OPTION_ERROR2 - 1) != 0)
1065 return;
554e0a8d 1066
20caffd2
WD
1067 for ( ; *msg != ':'; msg++) {
1068 if (*msg == 'd')
1069 saw_d = 1;
1070 else if (*msg == 'e')
1071 break;
1072 else if (strchr(rsync263_opts, *msg) == NULL)
1073 return;
1074 }
4c36ddbe 1075
20caffd2
WD
1076 if (saw_d) {
1077 rprintf(FWARNING,
1078 "*** Try using \"--old-d\" if remote rsync is <= 2.6.3 ***\n");
1079 }
1080}
1f75bb10 1081
20caffd2
WD
1082/* This is used by the generator to limit how many file transfers can
1083 * be active at once when --remove-source-files is specified. Without
1084 * this, sender-side deletions were mostly happening at the end. */
1085void increment_active_files(int ndx, int itemizing, enum logcode code)
1086{
1087 while (1) {
1088 /* TODO: tune these limits? */
1089 int limit = active_bytecnt >= 128*1024 ? 10 : 50;
1090 if (active_filecnt < limit)
1091 break;
1092 check_for_finished_files(itemizing, code, 0);
1093 if (active_filecnt < limit)
1094 break;
1095 wait_for_receiver();
1096 }
00bdf899 1097
20caffd2
WD
1098 active_filecnt++;
1099 active_bytecnt += F_LENGTH(cur_flist->files[ndx - cur_flist->ndx_start]);
1100}
1f75bb10 1101
20caffd2
WD
1102int get_redo_num(void)
1103{
1104 return flist_ndx_pop(&redo_list);
1105}
1106
1107int get_hlink_num(void)
1108{
1109 return flist_ndx_pop(&hlink_list);
1110}
8d9dc9f9 1111
20caffd2
WD
1112/* When we're the receiver and we have a local --files-from list of names
1113 * that needs to be sent over the socket to the sender, we have to do two
1114 * things at the same time: send the sender a list of what files we're
1115 * processing and read the incoming file+info list from the sender. We do
1116 * this by making recv_file_list() call forward_filesfrom_data(), which
1117 * will ensure that we forward data to the sender until we get some data
1118 * for recv_file_list() to use. */
1119void start_filesfrom_forwarding(int fd)
1120{
8e6b4ddb
WD
1121 if (protocol_version < 31 && OUT_MULTIPLEXED) {
1122 /* Older protocols send the files-from data w/o packaging
1123 * it in multiplexed I/O packets, so temporarily switch
705132bc
WD
1124 * to buffered I/O to match this behavior. */
1125 iobuf.msg.pos = iobuf.msg.len = 0; /* Be extra sure no messages go out. */
8e6b4ddb 1126 ff_reenable_multiplex = io_end_multiplex_out(MPLX_TO_BUFFERED);
20caffd2 1127 }
705132bc 1128 ff_forward_fd = fd;
cbc63a09
WD
1129
1130 alloc_xbuf(&ff_xb, FILESFROM_BUFLEN);
4c36ddbe 1131}
8d9dc9f9 1132
67b8f3df 1133/* Read a line into the "buf" buffer. */
ba525f77 1134int read_line(int fd, char *buf, size_t bufsiz, int flags)
56014c8c 1135{
ba525f77 1136 char ch, *s, *eob;
56014c8c 1137
ba525f77
WD
1138#ifdef ICONV_OPTION
1139 if (flags & RL_CONVERT && iconv_buf.size < bufsiz)
1140 realloc_xbuf(&iconv_buf, bufsiz + 1024);
1141#endif
1142
56014c8c 1143 start:
22955408 1144#ifdef ICONV_OPTION
ba525f77 1145 s = flags & RL_CONVERT ? iconv_buf.buf : buf;
22955408
WD
1146#else
1147 s = buf;
1148#endif
ba525f77 1149 eob = s + bufsiz - 1;
56014c8c 1150 while (1) {
20caffd2
WD
1151 /* We avoid read_byte() for files because files can return an EOF. */
1152 if (fd == iobuf.in_fd)
1153 ch = read_byte(fd);
1154 else if (safe_read(fd, &ch, 1) == 0)
56014c8c 1155 break;
ba525f77 1156 if (flags & RL_EOL_NULLS ? ch == '\0' : (ch == '\r' || ch == '\n')) {
67b8f3df 1157 /* Skip empty lines if dumping comments. */
ba525f77 1158 if (flags & RL_DUMP_COMMENTS && s == buf)
56014c8c
WD
1159 continue;
1160 break;
1161 }
1162 if (s < eob)
1163 *s++ = ch;
1164 }
1165 *s = '\0';
7a55d06e 1166
ba525f77 1167 if (flags & RL_DUMP_COMMENTS && (*buf == '#' || *buf == ';'))
56014c8c
WD
1168 goto start;
1169
ba525f77
WD
1170#ifdef ICONV_OPTION
1171 if (flags & RL_CONVERT) {
1172 xbuf outbuf;
1173 INIT_XBUF(outbuf, buf, 0, bufsiz);
1174 iconv_buf.pos = 0;
1175 iconv_buf.len = s - iconv_buf.buf;
1176 iconvbufs(ic_recv, &iconv_buf, &outbuf,
d8a7290f 1177 ICB_INCLUDE_BAD | ICB_INCLUDE_INCOMPLETE | ICB_INIT);
ba525f77
WD
1178 outbuf.buf[outbuf.len] = '\0';
1179 return outbuf.len;
1180 }
1181#endif
1182
67b8f3df
WD
1183 return s - buf;
1184}
1185
53936ef9
WD
1186void read_args(int f_in, char *mod_name, char *buf, size_t bufsiz, int rl_nulls,
1187 char ***argv_p, int *argc_p, char **request_p)
67b8f3df
WD
1188{
1189 int maxargs = MAX_ARGS;
1190 int dot_pos = 0;
1191 int argc = 0;
1192 char **argv, *p;
22955408
WD
1193 int rl_flags = (rl_nulls ? RL_EOL_NULLS : 0);
1194
1195#ifdef ICONV_OPTION
1196 rl_flags |= (protect_args && ic_recv != (iconv_t)-1 ? RL_CONVERT : 0);
1197#endif
67b8f3df
WD
1198
1199 if (!(argv = new_array(char *, maxargs)))
1200 out_of_memory("read_args");
53936ef9 1201 if (mod_name && !protect_args)
67b8f3df
WD
1202 argv[argc++] = "rsyncd";
1203
1204 while (1) {
ba525f77 1205 if (read_line(f_in, buf, bufsiz, rl_flags) == 0)
67b8f3df
WD
1206 break;
1207
53936ef9 1208 if (argc == maxargs-1) {
67b8f3df
WD
1209 maxargs += MAX_ARGS;
1210 if (!(argv = realloc_array(argv, char *, maxargs)))
1211 out_of_memory("read_args");
1212 }
1213
1214 if (dot_pos) {
1215 if (request_p) {
1216 *request_p = strdup(buf);
1217 request_p = NULL;
1218 }
1219 if (mod_name)
1220 glob_expand_module(mod_name, buf, &argv, &argc, &maxargs);
1221 else
1222 glob_expand(buf, &argv, &argc, &maxargs);
1223 } else {
1224 if (!(p = strdup(buf)))
1225 out_of_memory("read_args");
1226 argv[argc++] = p;
1227 if (*p == '.' && p[1] == '\0')
1228 dot_pos = argc;
1229 }
1230 }
53936ef9 1231 argv[argc] = NULL;
67b8f3df 1232
987a5467
WD
1233 glob_expand(NULL, NULL, NULL, NULL);
1234
67b8f3df
WD
1235 *argc_p = argc;
1236 *argv_p = argv;
56014c8c 1237}
7a55d06e 1238
8e6b4ddb 1239BOOL io_start_buffering_out(int f_out)
1f75bb10 1240{
20caffd2
WD
1241 if (msgs2stderr && DEBUG_GTE(IO, 2))
1242 rprintf(FINFO, "[%s] io_start_buffering_out(%d)\n", who_am_i(), f_out);
1243
20caffd2
WD
1244 if (iobuf.out.buf) {
1245 if (iobuf.out_fd == -1)
1246 iobuf.out_fd = f_out;
d620b907
WD
1247 else
1248 assert(f_out == iobuf.out_fd);
8e6b4ddb 1249 return False;
8ef246e0 1250 }
20caffd2 1251
907e6a32 1252 alloc_xbuf(&iobuf.out, ROUND_UP_1024(IO_BUFFER_SIZE * 2));
20caffd2
WD
1253 iobuf.out_fd = f_out;
1254
8e6b4ddb 1255 return True;
1f75bb10
WD
1256}
1257
8e6b4ddb 1258BOOL io_start_buffering_in(int f_in)
1f75bb10 1259{
20caffd2
WD
1260 if (msgs2stderr && DEBUG_GTE(IO, 2))
1261 rprintf(FINFO, "[%s] io_start_buffering_in(%d)\n", who_am_i(), f_in);
1262
1263 if (iobuf.in.buf) {
1264 if (iobuf.in_fd == -1)
1265 iobuf.in_fd = f_in;
d620b907
WD
1266 else
1267 assert(f_in == iobuf.in_fd);
8e6b4ddb 1268 return False;
8ef246e0 1269 }
20caffd2 1270
907e6a32 1271 alloc_xbuf(&iobuf.in, ROUND_UP_1024(IO_BUFFER_SIZE));
20caffd2
WD
1272 iobuf.in_fd = f_in;
1273
8e6b4ddb 1274 return True;
1f75bb10
WD
1275}
1276
20caffd2 1277void io_end_buffering_in(BOOL free_buffers)
8ef246e0 1278{
705132bc
WD
1279 if (msgs2stderr && DEBUG_GTE(IO, 2)) {
1280 rprintf(FINFO, "[%s] io_end_buffering_in(IOBUF_%s_BUFS)\n",
1281 who_am_i(), free_buffers ? "FREE" : "KEEP");
20caffd2
WD
1282 }
1283
1284 if (free_buffers)
1285 free_xbuf(&iobuf.in);
1286 else
1287 iobuf.in.pos = iobuf.in.len = 0;
1288
1289 iobuf.in_fd = -1;
1290}
1291
1292void io_end_buffering_out(BOOL free_buffers)
1293{
705132bc
WD
1294 if (msgs2stderr && DEBUG_GTE(IO, 2)) {
1295 rprintf(FINFO, "[%s] io_end_buffering_out(IOBUF_%s_BUFS)\n",
1296 who_am_i(), free_buffers ? "FREE" : "KEEP");
20caffd2
WD
1297 }
1298
8ef246e0 1299 io_flush(FULL_FLUSH);
20caffd2
WD
1300
1301 if (free_buffers) {
1302 free_xbuf(&iobuf.out);
1303 free_xbuf(&iobuf.msg);
20caffd2
WD
1304 }
1305
1306 iobuf.out_fd = -1;
1f75bb10
WD
1307}
1308
be91bd81 1309void maybe_flush_socket(int important)
626bec8e 1310{
1ec57e4d
WD
1311 if (flist_eof && iobuf.out.buf && iobuf.out.len > iobuf.out_empty_len
1312 && (important || time(NULL) - last_io_out >= 5))
626bec8e
WD
1313 io_flush(NORMAL_FLUSH);
1314}
1315
cdf236aa 1316void maybe_send_keepalive(void)
9e2409ab 1317{
3b0a30eb 1318 if (time(NULL) - last_io_out >= allowed_lull) {
20caffd2 1319 if (!iobuf.msg.len && iobuf.out.len == iobuf.out_empty_len) {
3221f451
WD
1320 if (protocol_version < 29)
1321 return; /* there's nothing we can do */
8ef246e0 1322 if (protocol_version >= 30)
332cf6df 1323 send_msg(MSG_NOOP, "", 0, 0);
8ef246e0 1324 else {
20caffd2
WD
1325 write_int(iobuf.out_fd, cur_flist->used);
1326 write_shortint(iobuf.out_fd, ITEM_IS_NEW);
8ef246e0 1327 }
9e2409ab 1328 }
20caffd2
WD
1329 if (iobuf.msg.len)
1330 perform_io(iobuf.msg.size - iobuf.msg.len + 1, PIO_NEED_MSGROOM);
1331 else if (iobuf.out.len > iobuf.out_empty_len)
9e2409ab
WD
1332 io_flush(NORMAL_FLUSH);
1333 }
1334}
1335
20caffd2 1336void start_flist_forward(int ndx)
8ef246e0 1337{
20caffd2
WD
1338 write_int(iobuf.out_fd, ndx);
1339 forward_flist_data = 1;
8ef246e0
WD
1340}
1341
20caffd2 1342void stop_flist_forward(void)
4c36ddbe 1343{
20caffd2 1344 forward_flist_data = 0;
8d9dc9f9
AT
1345}
1346
d8587b46 1347/* Read a message from a multiplexed source. */
20caffd2 1348static void read_a_msg(void)
8d9dc9f9 1349{
6d952fdb 1350 char data[BIGPATHBUFLEN];
a0f1ed55 1351 int tag, val;
d8587b46 1352 size_t msg_bytes;
d8587b46 1353
763880ba 1354 tag = raw_read_int();
d8587b46
WD
1355
1356 msg_bytes = tag & 0xFFFFFF;
1357 tag = (tag >> 24) - MPLEX_BASE;
1358
20caffd2
WD
1359 if (DEBUG_GTE(IO, 1) && (msgs2stderr || tag != MSG_INFO))
1360 rprintf(FINFO, "[%s] got msg=%d, len=%ld\n", who_am_i(), (int)tag, (long)msg_bytes);
d8587b46
WD
1361
1362 switch (tag) {
1363 case MSG_DATA:
20caffd2
WD
1364 assert(iobuf.raw_input_ends_before == 0);
1365 /* Though this does not yet read the data, we do mark where in
1366 * the buffer the msg data will end once it is read. It is
1367 * possible that this points off the end of the buffer, in
1368 * which case the gradual reading of the input stream will
1369 * cause this value to decrease and eventually become real. */
1370 iobuf.raw_input_ends_before = iobuf.in.pos + msg_bytes;
d8587b46 1371 break;
20caffd2
WD
1372 case MSG_STATS:
1373 if (msg_bytes != sizeof stats.total_read || !am_generator)
d8587b46 1374 goto invalid_msg;
763880ba 1375 raw_read_buf((char*)&stats.total_read, sizeof stats.total_read);
d8587b46
WD
1376 break;
1377 case MSG_REDO:
1378 if (msg_bytes != 4 || !am_generator)
1379 goto invalid_msg;
763880ba 1380 got_flist_entry_status(FES_REDO, raw_read_int());
d8587b46
WD
1381 break;
1382 case MSG_IO_ERROR:
1383 if (msg_bytes != 4 || am_sender)
1384 goto invalid_msg;
763880ba 1385 val = raw_read_int();
4e2a7e59 1386 io_error |= val;
d8587b46 1387 if (!am_generator)
4e2a7e59 1388 send_msg_int(MSG_IO_ERROR, val);
d8587b46 1389 break;
a0f1ed55 1390 case MSG_IO_TIMEOUT:
84c11e85 1391 if (msg_bytes != 4 || am_server || am_generator)
a0f1ed55 1392 goto invalid_msg;
763880ba 1393 val = raw_read_int();
a0f1ed55
WD
1394 if (!io_timeout || io_timeout > val) {
1395 if (INFO_GTE(MISC, 2))
1396 rprintf(FINFO, "Setting --timeout=%d to match server\n", val);
4655dcf2 1397 set_io_timeout(val);
a0f1ed55
WD
1398 }
1399 break;
d8587b46
WD
1400 case MSG_NOOP:
1401 if (am_sender)
1402 maybe_send_keepalive();
1403 break;
d8587b46 1404 case MSG_DELETED:
6d952fdb 1405 if (msg_bytes >= sizeof data)
d8587b46
WD
1406 goto overflow;
1407 if (am_generator) {
6d952fdb
WD
1408 raw_read_buf(data, msg_bytes);
1409 send_msg(MSG_DELETED, data, msg_bytes, 1);
d8587b46
WD
1410 break;
1411 }
1412#ifdef ICONV_OPTION
1413 if (ic_recv != (iconv_t)-1) {
1414 xbuf outbuf, inbuf;
1415 char ibuf[512];
1416 int add_null = 0;
cbc63a09 1417 int flags = ICB_INCLUDE_BAD | ICB_INIT;
d8587b46 1418
6d952fdb 1419 INIT_CONST_XBUF(outbuf, data);
d8587b46
WD
1420 INIT_XBUF(inbuf, ibuf, 0, (size_t)-1);
1421
1422 while (msg_bytes) {
cbc63a09 1423 size_t len = msg_bytes > sizeof ibuf - inbuf.len ? sizeof ibuf - inbuf.len : msg_bytes;
763880ba 1424 raw_read_buf(ibuf + inbuf.len, len);
d8a7290f 1425 inbuf.pos = 0;
cbc63a09
WD
1426 inbuf.len += len;
1427 if (!(msg_bytes -= len) && !ibuf[inbuf.len-1])
d8587b46 1428 inbuf.len--, add_null = 1;
cbc63a09
WD
1429 if (iconvbufs(ic_send, &inbuf, &outbuf, flags) < 0) {
1430 if (errno == E2BIG)
1431 goto overflow;
1432 /* Buffer ended with an incomplete char, so move the
1433 * bytes to the start of the buffer and continue. */
1434 memmove(ibuf, ibuf + inbuf.pos, inbuf.len);
1435 }
1436 flags &= ~ICB_INIT;
d8587b46
WD
1437 }
1438 if (add_null) {
1439 if (outbuf.len == outbuf.size)
1440 goto overflow;
1441 outbuf.buf[outbuf.len++] = '\0';
1442 }
1443 msg_bytes = outbuf.len;
1444 } else
1445#endif
6d952fdb 1446 raw_read_buf(data, msg_bytes);
d8587b46 1447 /* A directory name was sent with the trailing null */
6d952fdb
WD
1448 if (msg_bytes > 0 && !data[msg_bytes-1])
1449 log_delete(data, S_IFDIR);
d8587b46 1450 else {
6d952fdb
WD
1451 data[msg_bytes] = '\0';
1452 log_delete(data, S_IFREG);
d8587b46
WD
1453 }
1454 break;
1455 case MSG_SUCCESS:
1456 if (msg_bytes != 4) {
1457 invalid_msg:
1458 rprintf(FERROR, "invalid multi-message %d:%lu [%s%s]\n",
1459 tag, (unsigned long)msg_bytes, who_am_i(),
1460 inc_recurse ? "/inc" : "");
1461 exit_cleanup(RERR_STREAMIO);
1462 }
763880ba 1463 val = raw_read_int();
d8587b46 1464 if (am_generator)
4e2a7e59 1465 got_flist_entry_status(FES_SUCCESS, val);
d8587b46 1466 else
4e2a7e59 1467 successful_send(val);
d8587b46
WD
1468 break;
1469 case MSG_NO_SEND:
1470 if (msg_bytes != 4)
1471 goto invalid_msg;
763880ba 1472 val = raw_read_int();
d8587b46 1473 if (am_generator)
4e2a7e59 1474 got_flist_entry_status(FES_NO_SEND, val);
d8587b46 1475 else
4e2a7e59 1476 send_msg_int(MSG_NO_SEND, val);
d8587b46
WD
1477 break;
1478 case MSG_ERROR_SOCKET:
1479 case MSG_ERROR_UTF8:
1480 case MSG_CLIENT:
1481 case MSG_LOG:
1482 if (!am_generator)
1483 goto invalid_msg;
1484 if (tag == MSG_ERROR_SOCKET)
20caffd2 1485 msgs2stderr = 1;
d8587b46
WD
1486 /* FALL THROUGH */
1487 case MSG_INFO:
1488 case MSG_ERROR:
1489 case MSG_ERROR_XFER:
1490 case MSG_WARNING:
6d952fdb 1491 if (msg_bytes >= sizeof data) {
d8587b46
WD
1492 overflow:
1493 rprintf(FERROR,
1494 "multiplexing overflow %d:%lu [%s%s]\n",
1495 tag, (unsigned long)msg_bytes, who_am_i(),
1496 inc_recurse ? "/inc" : "");
1497 exit_cleanup(RERR_STREAMIO);
1498 }
6d952fdb
WD
1499 raw_read_buf(data, msg_bytes);
1500 rwrite((enum logcode)tag, data, msg_bytes, !am_generator);
d8587b46 1501 if (first_message) {
6d952fdb
WD
1502 if (list_only && !am_sender && tag == 1 && msg_bytes < sizeof data) {
1503 data[msg_bytes] = '\0';
1504 check_for_d_option_error(data);
d8587b46
WD
1505 }
1506 first_message = 0;
1507 }
1508 break;
f9185203 1509 case MSG_ERROR_EXIT:
0c2e8f93
WD
1510 if (DEBUG_GTE(EXIT, 3))
1511 rprintf(FINFO, "[%s] got MSG_ERROR_EXIT with %d bytes\n", who_am_i(), msg_bytes);
304d7b58
WD
1512 if (msg_bytes == 0) {
1513 if (!am_sender && !am_generator) {
0c2e8f93
WD
1514 if (DEBUG_GTE(EXIT, 3)) {
1515 rprintf(FINFO, "[%s] sending MSG_ERROR_EXIT (len 0)\n",
1516 who_am_i());
1517 }
304d7b58
WD
1518 send_msg(MSG_ERROR_EXIT, "", 0, 0);
1519 io_flush(FULL_FLUSH);
1520 }
1521 val = 0;
1522 } else if (msg_bytes == 4) {
763880ba 1523 val = raw_read_int();
304d7b58 1524 if (protocol_version >= 31) {
0c2e8f93
WD
1525 if (am_generator) {
1526 if (DEBUG_GTE(EXIT, 3)) {
1527 rprintf(FINFO, "[%s] sending MSG_ERROR_EXIT with exit_code %d\n",
1528 who_am_i(), val);
1529 }
304d7b58 1530 send_msg_int(MSG_ERROR_EXIT, val);
0c2e8f93
WD
1531 } else {
1532 if (DEBUG_GTE(EXIT, 3)) {
1533 rprintf(FINFO, "[%s] sending MSG_ERROR_EXIT (len 0)\n",
1534 who_am_i());
1535 }
304d7b58 1536 send_msg(MSG_ERROR_EXIT, "", 0, 0);
0c2e8f93 1537 }
304d7b58
WD
1538 }
1539 } else
f9185203 1540 goto invalid_msg;
304d7b58
WD
1541 /* Send a negative linenum so that we don't end up
1542 * with a duplicate exit message. */
1543 _exit_cleanup(val, __FILE__, 0 - __LINE__);
d8587b46
WD
1544 default:
1545 rprintf(FERROR, "unexpected tag %d [%s%s]\n",
1546 tag, who_am_i(), inc_recurse ? "/inc" : "");
1547 exit_cleanup(RERR_STREAMIO);
1548 }
d8587b46
WD
1549}
1550
ae598f38
WD
1551static void drain_multiplex_messages(void)
1552{
1553 while (IN_MULTIPLEXED && iobuf.in.len) {
1554 if (iobuf.raw_input_ends_before) {
1555 size_t raw_len = iobuf.raw_input_ends_before - iobuf.in.pos;
1556 iobuf.raw_input_ends_before = 0;
1557 if (raw_len >= iobuf.in.len) {
1558 iobuf.in.len = 0;
1559 break;
1560 }
ae598f38 1561 iobuf.in.len -= raw_len;
763880ba
WD
1562 if ((iobuf.in.pos += raw_len) >= iobuf.in.size)
1563 iobuf.in.pos -= iobuf.in.size;
ae598f38
WD
1564 }
1565 read_a_msg();
1566 }
1567}
1568
20caffd2 1569void wait_for_receiver(void)
d8587b46 1570{
20caffd2
WD
1571 if (!iobuf.raw_input_ends_before)
1572 read_a_msg();
1573
1574 if (iobuf.raw_input_ends_before) {
1575 int ndx = read_int(iobuf.in_fd);
1576 if (ndx < 0) {
1577 switch (ndx) {
1578 case NDX_FLIST_EOF:
1579 flist_eof = 1;
1580 if (DEBUG_GTE(FLIST, 3))
1581 rprintf(FINFO, "[%s] flist_eof=1\n", who_am_i());
1582 break;
1583 case NDX_DONE:
1584 msgdone_cnt++;
1585 break;
1586 default:
1587 exit_cleanup(RERR_STREAMIO);
1588 }
1589 } else {
1590 struct file_list *flist;
2885270b 1591 flist_receiving_enabled = False;
20caffd2
WD
1592 if (DEBUG_GTE(FLIST, 2)) {
1593 rprintf(FINFO, "[%s] receiving flist for dir %d\n",
1594 who_am_i(), ndx);
1595 }
1596 flist = recv_file_list(iobuf.in_fd);
1597 flist->parent_ndx = ndx;
1598#ifdef SUPPORT_HARD_LINKS
1599 if (preserve_hard_links)
1600 match_hard_links(flist);
1601#endif
2885270b 1602 flist_receiving_enabled = True;
8d9dc9f9 1603 }
b9f592fb 1604 }
720b47f2
AT
1605}
1606
3a993aa4 1607unsigned short read_shortint(int f)
9361f839 1608{
482f48cc 1609 char b[2];
20caffd2 1610 read_buf(f, b, 2);
482f48cc 1611 return (UVAL(b, 1) << 8) + UVAL(b, 0);
9361f839
WD
1612}
1613
b7922338 1614int32 read_int(int f)
720b47f2 1615{
4c36ddbe 1616 char b[4];
d8aeda1e 1617 int32 num;
d730b113 1618
20caffd2 1619 read_buf(f, b, 4);
482f48cc
WD
1620 num = IVAL(b, 0);
1621#if SIZEOF_INT32 > 4
1622 if (num & (int32)0x80000000)
1623 num |= ~(int32)0xffffffff;
1624#endif
d8aeda1e 1625 return num;
720b47f2
AT
1626}
1627
351e23ad 1628int32 read_varint(int f)
3a6a366f 1629{
351e23ad 1630 union {
20caffd2
WD
1631 char b[5];
1632 int32 x;
351e23ad
WD
1633 } u;
1634 uchar ch;
1635 int extra;
1636
1637 u.x = 0;
20caffd2 1638 ch = read_byte(f);
351e23ad
WD
1639 extra = int_byte_extra[ch / 4];
1640 if (extra) {
1641 uchar bit = ((uchar)1<<(8-extra));
1642 if (extra >= (int)sizeof u.b) {
1643 rprintf(FERROR, "Overflow in read_varint()\n");
1644 exit_cleanup(RERR_STREAMIO);
1645 }
20caffd2 1646 read_buf(f, u.b, extra);
351e23ad
WD
1647 u.b[extra] = ch & (bit-1);
1648 } else
1649 u.b[0] = ch;
1650#if CAREFUL_ALIGNMENT
1651 u.x = IVAL(u.b,0);
1652#endif
1653#if SIZEOF_INT32 > 4
1654 if (u.x & (int32)0x80000000)
1655 u.x |= ~(int32)0xffffffff;
1656#endif
1657 return u.x;
1658}
71c46176 1659
351e23ad
WD
1660int64 read_varlong(int f, uchar min_bytes)
1661{
1662 union {
20caffd2
WD
1663 char b[9];
1664 int64 x;
351e23ad
WD
1665 } u;
1666 char b2[8];
1667 int extra;
71c46176 1668
031fa9ad 1669#if SIZEOF_INT64 < 8
351e23ad 1670 memset(u.b, 0, 8);
031fa9ad 1671#else
351e23ad 1672 u.x = 0;
031fa9ad 1673#endif
20caffd2 1674 read_buf(f, b2, min_bytes);
351e23ad
WD
1675 memcpy(u.b, b2+1, min_bytes-1);
1676 extra = int_byte_extra[CVAL(b2, 0) / 4];
1677 if (extra) {
1678 uchar bit = ((uchar)1<<(8-extra));
1679 if (min_bytes + extra > (int)sizeof u.b) {
1680 rprintf(FERROR, "Overflow in read_varlong()\n");
1681 exit_cleanup(RERR_STREAMIO);
1682 }
20caffd2 1683 read_buf(f, u.b + min_bytes - 1, extra);
351e23ad 1684 u.b[min_bytes + extra - 1] = CVAL(b2, 0) & (bit-1);
4ea4acf1 1685#if SIZEOF_INT64 < 8
351e23ad 1686 if (min_bytes + extra > 5 || u.b[4] || CVAL(u.b,3) & 0x80) {
4ea4acf1
WD
1687 rprintf(FERROR, "Integer overflow: attempted 64-bit offset\n");
1688 exit_cleanup(RERR_UNSUPPORTED);
1689 }
1690#endif
351e23ad
WD
1691 } else
1692 u.b[min_bytes + extra - 1] = CVAL(b2, 0);
1693#if SIZEOF_INT64 < 8
1694 u.x = IVAL(u.b,0);
1695#elif CAREFUL_ALIGNMENT
1696 u.x = IVAL(u.b,0) | (((int64)IVAL(u.b,4))<<32);
1697#endif
1698 return u.x;
1699}
1700
1701int64 read_longint(int f)
1702{
4ea4acf1 1703#if SIZEOF_INT64 >= 8
351e23ad 1704 char b[9];
4ea4acf1 1705#endif
351e23ad 1706 int32 num = read_int(f);
71c46176 1707
351e23ad
WD
1708 if (num != (int32)0xffffffff)
1709 return num;
1710
1711#if SIZEOF_INT64 < 8
1712 rprintf(FERROR, "Integer overflow: attempted 64-bit offset\n");
1713 exit_cleanup(RERR_UNSUPPORTED);
1714#else
20caffd2 1715 read_buf(f, b, 8);
351e23ad
WD
1716 return IVAL(b,0) | (((int64)IVAL(b,4))<<32);
1717#endif
3a6a366f
AT
1718}
1719
4a19c3b2 1720void read_buf(int f, char *buf, size_t len)
720b47f2 1721{
20caffd2
WD
1722 if (f != iobuf.in_fd) {
1723 if (safe_read(f, buf, len) != len)
ae598f38 1724 whine_about_eof(False); /* Doesn't return. */
20caffd2
WD
1725 goto batch_copy;
1726 }
1727
1728 if (!IN_MULTIPLEXED) {
763880ba 1729 raw_read_buf(buf, len);
4dde3347 1730 total_data_read += len;
20caffd2
WD
1731 if (forward_flist_data)
1732 write_buf(iobuf.out_fd, buf, len);
1733 batch_copy:
1734 if (f == write_batch_monitor_in)
1735 safe_write(batch_fd, buf, len);
1736 return;
1737 }
1738
1739 while (1) {
20caffd2
WD
1740 size_t siz;
1741
1742 while (!iobuf.raw_input_ends_before)
1743 read_a_msg();
1744
1745 siz = MIN(len, iobuf.raw_input_ends_before - iobuf.in.pos);
763880ba
WD
1746 if (siz >= iobuf.in.size)
1747 siz = iobuf.in.size;
1748 raw_read_buf(buf, siz);
4dde3347 1749 total_data_read += siz;
20caffd2
WD
1750
1751 if (forward_flist_data)
1752 write_buf(iobuf.out_fd, buf, siz);
1753
1754 if (f == write_batch_monitor_in)
1755 safe_write(batch_fd, buf, siz);
1756
1757 if ((len -= siz) == 0)
1758 break;
1759 buf += siz;
1760 }
720b47f2
AT
1761}
1762
4a19c3b2 1763void read_sbuf(int f, char *buf, size_t len)
575f2fca 1764{
20caffd2 1765 read_buf(f, buf, len);
af436313 1766 buf[len] = '\0';
575f2fca
AT
1767}
1768
9361f839 1769uchar read_byte(int f)
182dca5c 1770{
9361f839 1771 uchar c;
20caffd2 1772 read_buf(f, (char*)&c, 1);
4c36ddbe 1773 return c;
182dca5c 1774}
720b47f2 1775
46e99b09
WD
1776int read_vstring(int f, char *buf, int bufsize)
1777{
1778 int len = read_byte(f);
1779
1780 if (len & 0x80)
1781 len = (len & ~0x80) * 0x100 + read_byte(f);
1782
1783 if (len >= bufsize) {
1784 rprintf(FERROR, "over-long vstring received (%d > %d)\n",
1785 len, bufsize - 1);
9a6ed83f 1786 return -1;
46e99b09
WD
1787 }
1788
1789 if (len)
20caffd2 1790 read_buf(f, buf, len);
46e99b09
WD
1791 buf[len] = '\0';
1792 return len;
1793}
1794
188fed95
WD
1795/* Populate a sum_struct with values from the socket. This is
1796 * called by both the sender and the receiver. */
1797void read_sum_head(int f, struct sum_struct *sum)
1798{
8bd77e70 1799 int32 max_blength = protocol_version < 30 ? OLD_MAX_BLOCK_SIZE : MAX_BLOCK_SIZE;
188fed95 1800 sum->count = read_int(f);
c638caa6
WD
1801 if (sum->count < 0) {
1802 rprintf(FERROR, "Invalid checksum count %ld [%s]\n",
1803 (long)sum->count, who_am_i());
1804 exit_cleanup(RERR_PROTOCOL);
1805 }
188fed95 1806 sum->blength = read_int(f);
8bd77e70 1807 if (sum->blength < 0 || sum->blength > max_blength) {
cdf236aa
WD
1808 rprintf(FERROR, "Invalid block length %ld [%s]\n",
1809 (long)sum->blength, who_am_i());
188fed95
WD
1810 exit_cleanup(RERR_PROTOCOL);
1811 }
1812 sum->s2length = protocol_version < 27 ? csum_length : (int)read_int(f);
a0456b9c 1813 if (sum->s2length < 0 || sum->s2length > MAX_DIGEST_LEN) {
cdf236aa
WD
1814 rprintf(FERROR, "Invalid checksum length %d [%s]\n",
1815 sum->s2length, who_am_i());
188fed95
WD
1816 exit_cleanup(RERR_PROTOCOL);
1817 }
1818 sum->remainder = read_int(f);
1819 if (sum->remainder < 0 || sum->remainder > sum->blength) {
cdf236aa
WD
1820 rprintf(FERROR, "Invalid remainder length %ld [%s]\n",
1821 (long)sum->remainder, who_am_i());
188fed95
WD
1822 exit_cleanup(RERR_PROTOCOL);
1823 }
1824}
1825
c207d7ec
WD
1826/* Send the values from a sum_struct over the socket. Set sum to
1827 * NULL if there are no checksums to send. This is called by both
1828 * the generator and the sender. */
1829void write_sum_head(int f, struct sum_struct *sum)
1830{
1831 static struct sum_struct null_sum;
1832
1833 if (sum == NULL)
1834 sum = &null_sum;
1835
1836 write_int(f, sum->count);
1837 write_int(f, sum->blength);
1838 if (protocol_version >= 27)
1839 write_int(f, sum->s2length);
1840 write_int(f, sum->remainder);
1841}
1842
20caffd2 1843/* Sleep after writing to limit I/O bandwidth usage.
08571358
MP
1844 *
1845 * @todo Rather than sleeping after each write, it might be better to
1846 * use some kind of averaging. The current algorithm seems to always
1847 * use a bit less bandwidth than specified, because it doesn't make up
1848 * for slow periods. But arguably this is a feature. In addition, we
1849 * ought to take the time used to write the data into account.
71e58630
WD
1850 *
1851 * During some phases of big transfers (file FOO is uptodate) this is
1852 * called with a small bytes_written every time. As the kernel has to
1853 * round small waits up to guarantee that we actually wait at least the
1854 * requested number of microseconds, this can become grossly inaccurate.
1855 * We therefore keep track of the bytes we've written over time and only
20caffd2 1856 * sleep when the accumulated delay is at least 1 tenth of a second. */
08571358
MP
1857static void sleep_for_bwlimit(int bytes_written)
1858{
71e58630 1859 static struct timeval prior_tv;
0f78b815 1860 static long total_written = 0;
71e58630
WD
1861 struct timeval tv, start_tv;
1862 long elapsed_usec, sleep_usec;
1863
1864#define ONE_SEC 1000000L /* # of microseconds in a second */
08571358 1865
0f78b815 1866 total_written += bytes_written;
71e58630
WD
1867
1868 gettimeofday(&start_tv, NULL);
1869 if (prior_tv.tv_sec) {
1870 elapsed_usec = (start_tv.tv_sec - prior_tv.tv_sec) * ONE_SEC
1871 + (start_tv.tv_usec - prior_tv.tv_usec);
1872 total_written -= elapsed_usec * bwlimit / (ONE_SEC/1024);
1873 if (total_written < 0)
1874 total_written = 0;
1875 }
3151cbae 1876
71e58630
WD
1877 sleep_usec = total_written * (ONE_SEC/1024) / bwlimit;
1878 if (sleep_usec < ONE_SEC / 10) {
1879 prior_tv = start_tv;
1880 return;
1881 }
08571358 1882
71e58630
WD
1883 tv.tv_sec = sleep_usec / ONE_SEC;
1884 tv.tv_usec = sleep_usec % ONE_SEC;
98b332ed 1885 select(0, NULL, NULL, NULL, &tv);
71e58630
WD
1886
1887 gettimeofday(&prior_tv, NULL);
1888 elapsed_usec = (prior_tv.tv_sec - start_tv.tv_sec) * ONE_SEC
1889 + (prior_tv.tv_usec - start_tv.tv_usec);
1890 total_written = (sleep_usec - elapsed_usec) * bwlimit / (ONE_SEC/1024);
08571358
MP
1891}
1892
20caffd2 1893void io_flush(int flush_it_all)
d6dead6b 1894{
20caffd2
WD
1895 if (iobuf.out.len > iobuf.out_empty_len) {
1896 if (flush_it_all) /* FULL_FLUSH: flush everything in the output buffers */
1897 perform_io(iobuf.out.size - iobuf.out_empty_len, PIO_NEED_OUTROOM);
1898 else /* NORMAL_FLUSH: flush at least 1 byte */
1899 perform_io(iobuf.out.size - iobuf.out.len + 1, PIO_NEED_OUTROOM);
d6dead6b 1900 }
20caffd2
WD
1901 if (iobuf.msg.len)
1902 perform_io(iobuf.msg.size, PIO_NEED_MSGROOM);
d6dead6b 1903}
720b47f2 1904
3a993aa4 1905void write_shortint(int f, unsigned short x)
9361f839 1906{
3a993aa4
WD
1907 char b[2];
1908 b[0] = (char)x;
1909 b[1] = (char)(x >> 8);
20caffd2 1910 write_buf(f, b, 2);
9361f839
WD
1911}
1912
351e23ad
WD
1913void write_int(int f, int32 x)
1914{
1915 char b[4];
1916 SIVAL(b, 0, x);
20caffd2 1917 write_buf(f, b, 4);
351e23ad
WD
1918}
1919
f31514ad 1920void write_varint(int f, int32 x)
1c3344a1
WD
1921{
1922 char b[5];
351e23ad
WD
1923 uchar bit;
1924 int cnt = 4;
1925
1926 SIVAL(b, 1, x);
1927
1928 while (cnt > 1 && b[cnt] == 0)
1929 cnt--;
1930 bit = ((uchar)1<<(7-cnt+1));
1931 if (CVAL(b, cnt) >= bit) {
1932 cnt++;
1933 *b = ~(bit-1);
1934 } else if (cnt > 1)
1935 *b = b[cnt] | ~(bit*2-1);
1936 else
1937 *b = b[cnt];
1938
20caffd2 1939 write_buf(f, b, cnt);
1c3344a1
WD
1940}
1941
351e23ad 1942void write_varlong(int f, int64 x, uchar min_bytes)
720b47f2 1943{
351e23ad
WD
1944 char b[9];
1945 uchar bit;
1946 int cnt = 8;
1947
1948 SIVAL(b, 1, x);
1949#if SIZEOF_INT64 >= 8
1950 SIVAL(b, 5, x >> 32);
1951#else
1952 if (x <= 0x7FFFFFFF && x >= 0)
1953 memset(b + 5, 0, 4);
1954 else {
1955 rprintf(FERROR, "Integer overflow: attempted 64-bit offset\n");
1956 exit_cleanup(RERR_UNSUPPORTED);
1957 }
1958#endif
1959
1960 while (cnt > min_bytes && b[cnt] == 0)
1961 cnt--;
1962 bit = ((uchar)1<<(7-cnt+min_bytes));
1963 if (CVAL(b, cnt) >= bit) {
1964 cnt++;
1965 *b = ~(bit-1);
1966 } else if (cnt > min_bytes)
1967 *b = b[cnt] | ~(bit*2-1);
1968 else
1969 *b = b[cnt];
1970
20caffd2 1971 write_buf(f, b, cnt);
720b47f2
AT
1972}
1973
7a24c346
MP
1974/*
1975 * Note: int64 may actually be a 32-bit type if ./configure couldn't find any
1976 * 64-bit types on this platform.
1977 */
71c46176 1978void write_longint(int f, int64 x)
3a6a366f 1979{
351e23ad 1980 char b[12], * const s = b+4;
3a6a366f 1981
351e23ad
WD
1982 SIVAL(s, 0, x);
1983 if (x <= 0x7FFFFFFF && x >= 0) {
20caffd2 1984 write_buf(f, s, 4);
351e23ad
WD
1985 return;
1986 }
3a6a366f 1987
4ea4acf1 1988#if SIZEOF_INT64 < 8
351e23ad
WD
1989 rprintf(FERROR, "Integer overflow: attempted 64-bit offset\n");
1990 exit_cleanup(RERR_UNSUPPORTED);
4ea4acf1 1991#else
351e23ad
WD
1992 memset(b, 0xFF, 4);
1993 SIVAL(s, 4, x >> 32);
20caffd2 1994 write_buf(f, b, 12);
4ea4acf1 1995#endif
3a6a366f
AT
1996}
1997
4a19c3b2 1998void write_buf(int f, const char *buf, size_t len)
720b47f2 1999{
20caffd2
WD
2000 size_t pos, siz;
2001
2002 if (f != iobuf.out_fd) {
2003 safe_write(f, buf, len);
2004 goto batch_copy;
2005 }
2006
75ea8459 2007 if (iobuf.out.len + len > iobuf.out.size)
20caffd2
WD
2008 perform_io(len, PIO_NEED_OUTROOM);
2009
2010 pos = iobuf.out.pos + iobuf.out.len; /* Must be set after any flushing. */
2011 if (pos >= iobuf.out.size)
2012 pos -= iobuf.out.size;
2013
2014 /* Handle a split copy if we wrap around the end of the circular buffer. */
2015 if (pos >= iobuf.out.pos && (siz = iobuf.out.size - pos) < len) {
2016 memcpy(iobuf.out.buf + pos, buf, siz);
2017 memcpy(iobuf.out.buf, buf + siz, len - siz);
2018 } else
2019 memcpy(iobuf.out.buf + pos, buf, len);
2020
2021 iobuf.out.len += len;
4dde3347 2022 total_data_written += len;
20caffd2
WD
2023
2024 batch_copy:
2025 if (f == write_batch_monitor_out)
2026 safe_write(batch_fd, buf, len);
720b47f2
AT
2027}
2028
20caffd2 2029/* Write a string to the connection */
4a19c3b2 2030void write_sbuf(int f, const char *buf)
f0fca04e 2031{
20caffd2 2032 write_buf(f, buf, strlen(buf));
f0fca04e
AT
2033}
2034
9361f839 2035void write_byte(int f, uchar c)
182dca5c 2036{
20caffd2 2037 write_buf(f, (char *)&c, 1);
182dca5c
AT
2038}
2039
4a19c3b2 2040void write_vstring(int f, const char *str, int len)
46e99b09
WD
2041{
2042 uchar lenbuf[3], *lb = lenbuf;
2043
2044 if (len > 0x7F) {
2045 if (len > 0x7FFF) {
2046 rprintf(FERROR,
2047 "attempting to send over-long vstring (%d > %d)\n",
2048 len, 0x7FFF);
2049 exit_cleanup(RERR_PROTOCOL);
2050 }
2051 *lb++ = len / 0x100 + 0x80;
2052 }
2053 *lb = len;
2054
20caffd2 2055 write_buf(f, (char*)lenbuf, lb - lenbuf + 1);
46e99b09 2056 if (len)
20caffd2 2057 write_buf(f, str, len);
46e99b09
WD
2058}
2059
8a65e0ce
WD
2060/* Send a file-list index using a byte-reduction method. */
2061void write_ndx(int f, int32 ndx)
2062{
2063 static int32 prev_positive = -1, prev_negative = 1;
2064 int32 diff, cnt = 0;
2065 char b[6];
2066
2067 if (protocol_version < 30 || read_batch) {
2068 write_int(f, ndx);
2069 return;
2070 }
2071
2072 /* Send NDX_DONE as a single-byte 0 with no side effects. Send
2073 * negative nums as a positive after sending a leading 0xFF. */
2074 if (ndx >= 0) {
2075 diff = ndx - prev_positive;
2076 prev_positive = ndx;
2077 } else if (ndx == NDX_DONE) {
2078 *b = 0;
20caffd2 2079 write_buf(f, b, 1);
8a65e0ce
WD
2080 return;
2081 } else {
2082 b[cnt++] = (char)0xFF;
2083 ndx = -ndx;
2084 diff = ndx - prev_negative;
2085 prev_negative = ndx;
2086 }
2087
2088 /* A diff of 1 - 253 is sent as a one-byte diff; a diff of 254 - 32767
2089 * or 0 is sent as a 0xFE + a two-byte diff; otherwise we send 0xFE
2090 * & all 4 bytes of the (non-negative) num with the high-bit set. */
2091 if (diff < 0xFE && diff > 0)
2092 b[cnt++] = (char)diff;
2093 else if (diff < 0 || diff > 0x7FFF) {
2094 b[cnt++] = (char)0xFE;
2095 b[cnt++] = (char)((ndx >> 24) | 0x80);
8a65e0ce 2096 b[cnt++] = (char)ndx;
351e23ad
WD
2097 b[cnt++] = (char)(ndx >> 8);
2098 b[cnt++] = (char)(ndx >> 16);
8a65e0ce
WD
2099 } else {
2100 b[cnt++] = (char)0xFE;
2101 b[cnt++] = (char)(diff >> 8);
2102 b[cnt++] = (char)diff;
2103 }
20caffd2 2104 write_buf(f, b, cnt);
8a65e0ce
WD
2105}
2106
2107/* Receive a file-list index using a byte-reduction method. */
2108int32 read_ndx(int f)
2109{
2110 static int32 prev_positive = -1, prev_negative = 1;
2111 int32 *prev_ptr, num;
2112 char b[4];
2113
2114 if (protocol_version < 30)
2115 return read_int(f);
2116
20caffd2 2117 read_buf(f, b, 1);
8a65e0ce 2118 if (CVAL(b, 0) == 0xFF) {
20caffd2 2119 read_buf(f, b, 1);
8a65e0ce
WD
2120 prev_ptr = &prev_negative;
2121 } else if (CVAL(b, 0) == 0)
2122 return NDX_DONE;
2123 else
2124 prev_ptr = &prev_positive;
2125 if (CVAL(b, 0) == 0xFE) {
20caffd2 2126 read_buf(f, b, 2);
8a65e0ce 2127 if (CVAL(b, 0) & 0x80) {
351e23ad
WD
2128 b[3] = CVAL(b, 0) & ~0x80;
2129 b[0] = b[1];
20caffd2 2130 read_buf(f, b+1, 2);
351e23ad 2131 num = IVAL(b, 0);
8a65e0ce 2132 } else
351e23ad 2133 num = (UVAL(b,0)<<8) + UVAL(b,1) + *prev_ptr;
8a65e0ce 2134 } else
351e23ad 2135 num = UVAL(b, 0) + *prev_ptr;
8a65e0ce
WD
2136 *prev_ptr = num;
2137 if (prev_ptr == &prev_negative)
2138 num = -num;
2139 return num;
2140}
2141
67b8f3df
WD
2142/* Read a line of up to bufsiz-1 characters into buf. Strips
2143 * the (required) trailing newline and all carriage returns.
2144 * Returns 1 for success; 0 for I/O error or truncation. */
20caffd2 2145int read_line_old(int fd, char *buf, size_t bufsiz)
f0fca04e 2146{
67b8f3df
WD
2147 bufsiz--; /* leave room for the null */
2148 while (bufsiz > 0) {
20caffd2
WD
2149 assert(fd != iobuf.in_fd);
2150 if (safe_read(fd, buf, 1) == 0)
2151 return 0;
2152 if (*buf == '\0')
914cc65c 2153 return 0;
20caffd2 2154 if (*buf == '\n')
f0fca04e 2155 break;
20caffd2 2156 if (*buf != '\r') {
f0fca04e 2157 buf++;
67b8f3df 2158 bufsiz--;
f0fca04e
AT
2159 }
2160 }
6ed6d7f5 2161 *buf = '\0';
67b8f3df 2162 return bufsiz > 0;
f0fca04e
AT
2163}
2164
f0fca04e
AT
2165void io_printf(int fd, const char *format, ...)
2166{
d62bcc17 2167 va_list ap;
33544bf4 2168 char buf[BIGPATHBUFLEN];
f0fca04e 2169 int len;
3151cbae 2170
f0fca04e 2171 va_start(ap, format);
3151cbae 2172 len = vsnprintf(buf, sizeof buf, format, ap);
f0fca04e
AT
2173 va_end(ap);
2174
f89b9368 2175 if (len < 0)
e4c598c8 2176 exit_cleanup(RERR_PROTOCOL);
f0fca04e 2177
33544bf4
WD
2178 if (len > (int)sizeof buf) {
2179 rprintf(FERROR, "io_printf() was too long for the buffer.\n");
e4c598c8 2180 exit_cleanup(RERR_PROTOCOL);
33544bf4
WD
2181 }
2182
f0fca04e
AT
2183 write_sbuf(fd, buf);
2184}
8d9dc9f9 2185
20caffd2
WD
2186/* Setup for multiplexing a MSG_* stream with the data stream. */
2187void io_start_multiplex_out(int fd)
8d9dc9f9 2188{
705132bc
WD
2189 io_flush(FULL_FLUSH);
2190
20caffd2
WD
2191 if (msgs2stderr && DEBUG_GTE(IO, 2))
2192 rprintf(FINFO, "[%s] io_start_multiplex_out(%d)\n", who_am_i(), fd);
8d9dc9f9 2193
907e6a32
WD
2194 if (!iobuf.msg.buf)
2195 alloc_xbuf(&iobuf.msg, ROUND_UP_1024(IO_BUFFER_SIZE));
2196
20caffd2
WD
2197 iobuf.out_empty_len = 4; /* See also OUT_MULTIPLEXED */
2198 io_start_buffering_out(fd);
2199
2200 iobuf.raw_data_header_pos = iobuf.out.pos + iobuf.out.len;
2201 iobuf.out.len += 4;
8d9dc9f9
AT
2202}
2203
20caffd2
WD
2204/* Setup for multiplexing a MSG_* stream with the data stream. */
2205void io_start_multiplex_in(int fd)
8d9dc9f9 2206{
20caffd2
WD
2207 if (msgs2stderr && DEBUG_GTE(IO, 2))
2208 rprintf(FINFO, "[%s] io_start_multiplex_in(%d)\n", who_am_i(), fd);
2209
2210 iobuf.in_multiplexed = True; /* See also IN_MULTIPLEXED */
2211 io_start_buffering_in(fd);
8d9dc9f9
AT
2212}
2213
8e6b4ddb 2214int io_end_multiplex_in(int mode)
7f459268 2215{
8e6b4ddb
WD
2216 int ret = iobuf.in_multiplexed ? iobuf.in_fd : -1;
2217
705132bc
WD
2218 if (msgs2stderr && DEBUG_GTE(IO, 2))
2219 rprintf(FINFO, "[%s] io_end_multiplex_in(mode=%d)\n", who_am_i(), mode);
20caffd2
WD
2220
2221 iobuf.in_multiplexed = False;
705132bc
WD
2222 if (mode == MPLX_SWITCHING)
2223 iobuf.raw_input_ends_before = 0;
2224 else
2225 assert(iobuf.raw_input_ends_before == 0);
2226 if (mode != MPLX_TO_BUFFERED)
2227 io_end_buffering_in(mode);
8e6b4ddb
WD
2228
2229 return ret;
7f459268
WD
2230}
2231
8e6b4ddb 2232int io_end_multiplex_out(int mode)
554e0a8d 2233{
8e6b4ddb
WD
2234 int ret = iobuf.out_empty_len ? iobuf.out_fd : -1;
2235
705132bc
WD
2236 if (msgs2stderr && DEBUG_GTE(IO, 2))
2237 rprintf(FINFO, "[%s] io_end_multiplex_out(mode=%d)\n", who_am_i(), mode);
2238
2239 if (mode != MPLX_TO_BUFFERED)
2240 io_end_buffering_out(mode);
2241 else
2242 io_flush(FULL_FLUSH);
20caffd2 2243
705132bc 2244 iobuf.out.len = 0;
20caffd2 2245 iobuf.out_empty_len = 0;
8e6b4ddb
WD
2246
2247 return ret;
554e0a8d
AT
2248}
2249
b9f592fb
WD
2250void start_write_batch(int fd)
2251{
2252 /* Some communication has already taken place, but we don't
2253 * enable batch writing until here so that we can write a
2254 * canonical record of the communication even though the
2255 * actual communication so far depends on whether a daemon
2256 * is involved. */
2257 write_int(batch_fd, protocol_version);
71456d30
WD
2258 if (protocol_version >= 30)
2259 write_byte(batch_fd, inc_recurse);
b9f592fb 2260 write_int(batch_fd, checksum_seed);
b9f592fb
WD
2261
2262 if (am_sender)
2263 write_batch_monitor_out = fd;
2264 else
2265 write_batch_monitor_in = fd;
2266}
2267
2268void stop_write_batch(void)
2269{
2270 write_batch_monitor_out = -1;
2271 write_batch_monitor_in = -1;
2272}