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