Use the new "the_file_list" global.
[rsync/rsync.git] / io.c
CommitLineData
7a24c346 1/* -*- c-file-style: "linux" -*-
d62bcc17
WD
2 *
3 * Copyright (C) 1996-2001 by Andrew Tridgell
880da007
MP
4 * Copyright (C) Paul Mackerras 1996
5 * Copyright (C) 2001, 2002 by Martin Pool <mbp@samba.org>
d62bcc17 6 *
880da007
MP
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
d62bcc17 11 *
880da007
MP
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
d62bcc17 16 *
880da007
MP
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 */
720b47f2 21
87ee2481 22/**
87ee2481
MP
23 * @file io.c
24 *
6ed6d7f5 25 * Socket and pipe I/O utilities used in rsync.
87ee2481
MP
26 *
27 * rsync provides its own multiplexing system, which is used to send
28 * stderr and stdout over a single socket. We need this because
29 * stdout normally carries the binary data stream, and stderr all our
30 * error messages.
31 *
32 * For historical reasons this is off during the start of the
33 * connection, but it's switched on quite early using
34 * io_start_multiplex_out() and io_start_multiplex_in().
35 **/
720b47f2 36
720b47f2
AT
37#include "rsync.h"
38
880da007 39/** If no timeout is specified then use a 60 second select timeout */
8cd9fd4e
AT
40#define SELECT_TIMEOUT 60
41
7a55d06e 42extern int bwlimit;
71e58630 43extern size_t bwlimit_writemax;
720b47f2 44extern int verbose;
6ba9279f 45extern int io_timeout;
d17e1dd2
WD
46extern int am_server;
47extern int am_daemon;
48extern int am_sender;
98f8c9a5 49extern int am_generator;
e626b29e 50extern int eol_nulls;
188fed95 51extern int csum_length;
b9f592fb
WD
52extern int checksum_seed;
53extern int protocol_version;
d19320fd 54extern char *filesfrom_host;
a800434a 55extern struct stats stats;
720b47f2 56
a86179f4 57const char phase_unknown[] = "unknown";
e626b29e 58int select_timeout = SELECT_TIMEOUT;
9e2409ab 59int ignore_timeout = 0;
b9f592fb 60int batch_fd = -1;
b0ad5429 61int batch_gen_fd = -1;
805edf9d 62
98b332ed
MP
63/**
64 * The connection might be dropped at some point; perhaps because the
65 * remote instance crashed. Just giving the offset on the stream is
66 * not very helpful. So instead we try to make io_phase_name point to
67 * something useful.
eca2adb4 68 *
6ed6d7f5 69 * For buffered/multiplexed I/O these names will be somewhat
805edf9d 70 * approximate; perhaps for ease of support we would rather make the
6ed6d7f5 71 * buffer always flush when a single application-level I/O finishes.
805edf9d 72 *
eca2adb4
MP
73 * @todo Perhaps we want some simple stack functionality, but there's
74 * no need to overdo it.
98b332ed 75 **/
805edf9d
MP
76const char *io_write_phase = phase_unknown;
77const char *io_read_phase = phase_unknown;
98b332ed 78
cb2e1f18 79/* Ignore an EOF error if non-zero. See whine_about_eof(). */
574c2500 80int kluge_around_eof = 0;
7a55d06e 81
d17e1dd2
WD
82int msg_fd_in = -1;
83int msg_fd_out = -1;
7a55d06e 84
1f75bb10
WD
85static int io_multiplexing_out;
86static int io_multiplexing_in;
87static int sock_f_in = -1;
88static int sock_f_out = -1;
89static time_t last_io;
90static int no_flush;
91
b9f592fb
WD
92static int write_batch_monitor_in = -1;
93static int write_batch_monitor_out = -1;
94
56014c8c
WD
95static int io_filesfrom_f_in = -1;
96static int io_filesfrom_f_out = -1;
97static char io_filesfrom_buf[2048];
98static char *io_filesfrom_bp;
99static char io_filesfrom_lastchar;
100static int io_filesfrom_buflen;
720b47f2 101
9dd891bb 102static void read_loop(int fd, char *buf, size_t len);
ff41a59f 103
d17e1dd2
WD
104struct redo_list {
105 struct redo_list *next;
106 int num;
107};
108
109static struct redo_list *redo_list_head;
110static struct redo_list *redo_list_tail;
111
112struct msg_list {
113 struct msg_list *next;
114 char *buf;
115 int len;
116};
117
118static struct msg_list *msg_list_head;
119static struct msg_list *msg_list_tail;
120
121static void redo_list_add(int num)
122{
123 struct redo_list *rl;
124
125 if (!(rl = new(struct redo_list)))
126 exit_cleanup(RERR_MALLOC);
127 rl->next = NULL;
128 rl->num = num;
129 if (redo_list_tail)
130 redo_list_tail->next = rl;
131 else
132 redo_list_head = rl;
133 redo_list_tail = rl;
134}
135
8d9dc9f9
AT
136static void check_timeout(void)
137{
138 time_t t;
90ba34e2 139
9e2409ab 140 if (!io_timeout || ignore_timeout)
d17e1dd2 141 return;
8d9dc9f9
AT
142
143 if (!last_io) {
144 last_io = time(NULL);
145 return;
146 }
147
148 t = time(NULL);
149
1f75bb10 150 if (t - last_io >= io_timeout) {
0adb99b9 151 if (!am_server && !am_daemon) {
4ccfd96c 152 rprintf(FERROR, "io timeout after %d seconds -- exiting\n",
0adb99b9
AT
153 (int)(t-last_io));
154 }
65417579 155 exit_cleanup(RERR_TIMEOUT);
8d9dc9f9
AT
156 }
157}
158
1f75bb10
WD
159/* Note the fds used for the main socket (which might really be a pipe
160 * for a local transfer, but we can ignore that). */
161void io_set_sock_fds(int f_in, int f_out)
162{
163 sock_f_in = f_in;
164 sock_f_out = f_out;
165}
166
98f8c9a5
WD
167/* Setup the fd used to receive MSG_* messages. Only needed during the
168 * early stages of being a local sender (up through the sending of the
169 * file list) or when we're the generator (to fetch the messages from
170 * the receiver). */
d17e1dd2
WD
171void set_msg_fd_in(int fd)
172{
173 msg_fd_in = fd;
174}
175
98f8c9a5
WD
176/* Setup the fd used to send our MSG_* messages. Only needed when
177 * we're the receiver (to send our messages to the generator). */
d17e1dd2
WD
178void set_msg_fd_out(int fd)
179{
180 msg_fd_out = fd;
181 set_nonblocking(msg_fd_out);
182}
183
184/* Add a message to the pending MSG_* list. */
185static void msg_list_add(int code, char *buf, int len)
554e0a8d 186{
d17e1dd2
WD
187 struct msg_list *ml;
188
189 if (!(ml = new(struct msg_list)))
190 exit_cleanup(RERR_MALLOC);
191 ml->next = NULL;
192 if (!(ml->buf = new_array(char, len+4)))
193 exit_cleanup(RERR_MALLOC);
194 SIVAL(ml->buf, 0, ((code+MPLEX_BASE)<<24) | len);
195 memcpy(ml->buf+4, buf, len);
196 ml->len = len+4;
197 if (msg_list_tail)
198 msg_list_tail->next = ml;
199 else
200 msg_list_head = ml;
201 msg_list_tail = ml;
554e0a8d
AT
202}
203
d17e1dd2
WD
204void send_msg(enum msgcode code, char *buf, int len)
205{
0d67e00a
WD
206 if (msg_fd_out < 0) {
207 io_multiplex_write(code, buf, len);
208 return;
209 }
d17e1dd2
WD
210 msg_list_add(code, buf, len);
211 msg_list_push(NORMAL_FLUSH);
212}
213
98f8c9a5
WD
214/* Read a message from the MSG_* fd and handle it. This is called either
215 * during the early stages of being a local sender (up through the sending
216 * of the file list) or when we're the generator (to fetch the messages
217 * from the receiver). */
d17e1dd2 218static void read_msg_fd(void)
554e0a8d 219{
f9c6b3e7 220 char buf[2048];
06ce139f 221 size_t n;
d17e1dd2 222 int fd = msg_fd_in;
ff41a59f
AT
223 int tag, len;
224
00bdf899
WD
225 /* Temporarily disable msg_fd_in. This is needed to avoid looping back
226 * to this routine from read_timeout() and writefd_unbuffered(). */
d17e1dd2 227 msg_fd_in = -1;
554e0a8d 228
ff41a59f
AT
229 read_loop(fd, buf, 4);
230 tag = IVAL(buf, 0);
231
232 len = tag & 0xFFFFFF;
d17e1dd2 233 tag = (tag >> 24) - MPLEX_BASE;
ff41a59f 234
d17e1dd2
WD
235 switch (tag) {
236 case MSG_DONE:
98f8c9a5 237 if (len != 0 || !am_generator) {
13c7bcbb 238 rprintf(FERROR, "invalid message %d:%d\n", tag, len);
d17e1dd2 239 exit_cleanup(RERR_STREAMIO);
13c7bcbb 240 }
d17e1dd2
WD
241 redo_list_add(-1);
242 break;
243 case MSG_REDO:
98f8c9a5 244 if (len != 4 || !am_generator) {
13c7bcbb 245 rprintf(FERROR, "invalid message %d:%d\n", tag, len);
d17e1dd2 246 exit_cleanup(RERR_STREAMIO);
13c7bcbb 247 }
d17e1dd2
WD
248 read_loop(fd, buf, 4);
249 redo_list_add(IVAL(buf,0));
250 break;
0d67e00a
WD
251 case MSG_DELETED:
252 if (len >= (int)sizeof buf || !am_generator) {
253 rprintf(FERROR, "invalid message %d:%d\n", tag, len);
254 exit_cleanup(RERR_STREAMIO);
255 }
256 read_loop(fd, buf, len);
257 io_multiplex_write(MSG_DELETED, buf, len);
258 break;
9981c27e
WD
259 case MSG_SUCCESS:
260 if (len != 4 || !am_generator) {
261 rprintf(FERROR, "invalid message %d:%d\n", tag, len);
262 exit_cleanup(RERR_STREAMIO);
263 }
264 read_loop(fd, buf, len);
265 io_multiplex_write(MSG_SUCCESS, buf, len);
266 break;
d17e1dd2
WD
267 case MSG_INFO:
268 case MSG_ERROR:
269 case MSG_LOG:
270 while (len) {
271 n = len;
272 if (n >= sizeof buf)
273 n = sizeof buf - 1;
274 read_loop(fd, buf, n);
275 rwrite((enum logcode)tag, buf, n);
276 len -= n;
277 }
278 break;
279 default:
13c7bcbb 280 rprintf(FERROR, "unknown message %d:%d\n", tag, len);
d17e1dd2
WD
281 exit_cleanup(RERR_STREAMIO);
282 }
283
284 msg_fd_in = fd;
285}
286
287/* Try to push messages off the list onto the wire. If we leave with more
288 * to do, return 0. On error, return -1. If everything flushed, return 1.
f9c6b3e7 289 * This is only active in the receiver. */
d17e1dd2
WD
290int msg_list_push(int flush_it_all)
291{
292 static int written = 0;
293 struct timeval tv;
294 fd_set fds;
295
296 if (msg_fd_out < 0)
297 return -1;
298
299 while (msg_list_head) {
300 struct msg_list *ml = msg_list_head;
301 int n = write(msg_fd_out, ml->buf + written, ml->len - written);
302 if (n < 0) {
303 if (errno == EINTR)
304 continue;
305 if (errno != EWOULDBLOCK && errno != EAGAIN)
306 return -1;
307 if (!flush_it_all)
308 return 0;
309 FD_ZERO(&fds);
310 FD_SET(msg_fd_out, &fds);
e626b29e 311 tv.tv_sec = select_timeout;
d17e1dd2
WD
312 tv.tv_usec = 0;
313 if (!select(msg_fd_out+1, NULL, &fds, NULL, &tv))
314 check_timeout();
315 } else if ((written += n) == ml->len) {
316 free(ml->buf);
317 msg_list_head = ml->next;
318 if (!msg_list_head)
319 msg_list_tail = NULL;
320 free(ml);
321 written = 0;
322 }
554e0a8d 323 }
d17e1dd2
WD
324 return 1;
325}
326
327int get_redo_num(void)
328{
329 struct redo_list *next;
330 int num;
331
332 while (!redo_list_head)
333 read_msg_fd();
554e0a8d 334
d17e1dd2
WD
335 num = redo_list_head->num;
336 next = redo_list_head->next;
337 free(redo_list_head);
338 redo_list_head = next;
339 if (!next)
340 redo_list_tail = NULL;
341
342 return num;
554e0a8d
AT
343}
344
56014c8c
WD
345/**
346 * When we're the receiver and we have a local --files-from list of names
347 * that needs to be sent over the socket to the sender, we have to do two
348 * things at the same time: send the sender a list of what files we're
349 * processing and read the incoming file+info list from the sender. We do
350 * this by augmenting the read_timeout() function to copy this data. It
351 * uses the io_filesfrom_buf to read a block of data from f_in (when it is
352 * ready, since it might be a pipe) and then blast it out f_out (when it
353 * is ready to receive more data).
354 */
355void io_set_filesfrom_fds(int f_in, int f_out)
356{
357 io_filesfrom_f_in = f_in;
358 io_filesfrom_f_out = f_out;
359 io_filesfrom_bp = io_filesfrom_buf;
360 io_filesfrom_lastchar = '\0';
361 io_filesfrom_buflen = 0;
362}
720b47f2 363
cb2e1f18
WD
364/* It's almost always an error to get an EOF when we're trying to read from the
365 * network, because the protocol is (for the most part) self-terminating.
880da007 366 *
cb2e1f18
WD
367 * There is one case for the receiver when it is at the end of the transfer
368 * (hanging around reading any keep-alive packets that might come its way): if
369 * the sender dies before the generator's kill-signal comes through, we can end
370 * up here needing to loop until the kill-signal arrives. In this situation,
371 * kluge_around_eof will be < 0.
372 *
373 * There is another case for older protocol versions (< 24) where the module
374 * listing was not terminated, so we must ignore an EOF error in that case and
375 * exit. In this situation, kluge_around_eof will be > 0. */
1f75bb10 376static void whine_about_eof(int fd)
7a55d06e 377{
574c2500 378 if (kluge_around_eof && fd == sock_f_in) {
55bb7fff 379 int i;
574c2500
WD
380 if (kluge_around_eof > 0)
381 exit_cleanup(0);
55bb7fff
WD
382 /* If we're still here after 10 seconds, exit with an error. */
383 for (i = 10*1000/20; i--; )
574c2500
WD
384 msleep(20);
385 }
3151cbae 386
00bdf899 387 rprintf(FERROR, RSYNC_NAME ": connection unexpectedly closed "
dca68b0a
WD
388 "(%.0f bytes received so far) [%s]\n",
389 (double)stats.total_read, who_am_i());
00bdf899
WD
390
391 exit_cleanup(RERR_STREAMIO);
7a55d06e 392}
720b47f2 393
7a55d06e 394
880da007 395/**
6ed6d7f5 396 * Read from a socket with I/O timeout. return the number of bytes
c3563c46
MP
397 * read. If no bytes can be read then exit, never return a number <= 0.
398 *
8886f8d0
MP
399 * TODO: If the remote shell connection fails, then current versions
400 * actually report an "unexpected EOF" error here. Since it's a
401 * fairly common mistake to try to use rsh when ssh is required, we
402 * should trap that: if we fail to read any data at all, we should
403 * give a better explanation. We can tell whether the connection has
404 * started by looking e.g. at whether the remote version is known yet.
c3563c46 405 */
3151cbae 406static int read_timeout(int fd, char *buf, size_t len)
8d9dc9f9 407{
d62bcc17 408 int n, ret = 0;
4c36ddbe 409
d17e1dd2 410 io_flush(NORMAL_FLUSH);
ea2111d1 411
4c36ddbe 412 while (ret == 0) {
7a55d06e 413 /* until we manage to read *something* */
56014c8c 414 fd_set r_fds, w_fds;
4c36ddbe 415 struct timeval tv;
1ea087a7 416 int maxfd = fd;
a57873b7 417 int count;
4c36ddbe 418
56014c8c 419 FD_ZERO(&r_fds);
a7026ba9 420 FD_ZERO(&w_fds);
56014c8c 421 FD_SET(fd, &r_fds);
d17e1dd2
WD
422 if (msg_fd_in >= 0) {
423 FD_SET(msg_fd_in, &r_fds);
1ea087a7
WD
424 if (msg_fd_in > maxfd)
425 maxfd = msg_fd_in;
4033b80b
WD
426 } else if (msg_list_head) {
427 FD_SET(msg_fd_out, &w_fds);
1ea087a7
WD
428 if (msg_fd_out > maxfd)
429 maxfd = msg_fd_out;
56014c8c 430 }
3309507d 431 if (io_filesfrom_f_out >= 0) {
56014c8c
WD
432 int new_fd;
433 if (io_filesfrom_buflen == 0) {
3309507d 434 if (io_filesfrom_f_in >= 0) {
56014c8c
WD
435 FD_SET(io_filesfrom_f_in, &r_fds);
436 new_fd = io_filesfrom_f_in;
437 } else {
438 io_filesfrom_f_out = -1;
439 new_fd = -1;
440 }
441 } else {
56014c8c
WD
442 FD_SET(io_filesfrom_f_out, &w_fds);
443 new_fd = io_filesfrom_f_out;
444 }
1ea087a7
WD
445 if (new_fd > maxfd)
446 maxfd = new_fd;
554e0a8d
AT
447 }
448
e626b29e 449 tv.tv_sec = select_timeout;
4c36ddbe
AT
450 tv.tv_usec = 0;
451
554e0a8d
AT
452 errno = 0;
453
a7026ba9 454 count = select(maxfd + 1, &r_fds, &w_fds, NULL, &tv);
a57873b7 455
a57873b7 456 if (count <= 0) {
f89b9368 457 if (errno == EBADF)
554e0a8d 458 exit_cleanup(RERR_SOCKETIO);
bd717af8 459 check_timeout();
4c36ddbe
AT
460 continue;
461 }
462
d17e1dd2
WD
463 if (msg_fd_in >= 0 && FD_ISSET(msg_fd_in, &r_fds))
464 read_msg_fd();
4033b80b
WD
465 else if (msg_list_head && FD_ISSET(msg_fd_out, &w_fds))
466 msg_list_push(NORMAL_FLUSH);
554e0a8d 467
3309507d 468 if (io_filesfrom_f_out >= 0) {
56014c8c
WD
469 if (io_filesfrom_buflen) {
470 if (FD_ISSET(io_filesfrom_f_out, &w_fds)) {
471 int l = write(io_filesfrom_f_out,
472 io_filesfrom_bp,
473 io_filesfrom_buflen);
474 if (l > 0) {
475 if (!(io_filesfrom_buflen -= l))
476 io_filesfrom_bp = io_filesfrom_buf;
477 else
478 io_filesfrom_bp += l;
479 } else {
480 /* XXX should we complain? */
481 io_filesfrom_f_out = -1;
482 }
483 }
3309507d 484 } else if (io_filesfrom_f_in >= 0) {
56014c8c
WD
485 if (FD_ISSET(io_filesfrom_f_in, &r_fds)) {
486 int l = read(io_filesfrom_f_in,
487 io_filesfrom_buf,
488 sizeof io_filesfrom_buf);
489 if (l <= 0) {
490 /* Send end-of-file marker */
491 io_filesfrom_buf[0] = '\0';
492 io_filesfrom_buf[1] = '\0';
493 io_filesfrom_buflen = io_filesfrom_lastchar? 2 : 1;
494 io_filesfrom_f_in = -1;
495 } else {
56014c8c
WD
496 if (!eol_nulls) {
497 char *s = io_filesfrom_buf + l;
498 /* Transform CR and/or LF into '\0' */
499 while (s-- > io_filesfrom_buf) {
500 if (*s == '\n' || *s == '\r')
501 *s = '\0';
502 }
503 }
504 if (!io_filesfrom_lastchar) {
505 /* Last buf ended with a '\0', so don't
506 * let this buf start with one. */
507 while (l && !*io_filesfrom_bp)
508 io_filesfrom_bp++, l--;
509 }
510 if (!l)
511 io_filesfrom_bp = io_filesfrom_buf;
512 else {
513 char *f = io_filesfrom_bp;
514 char *t = f;
515 char *eob = f + l;
516 /* Eliminate any multi-'\0' runs. */
517 while (f != eob) {
518 if (!(*t++ = *f++)) {
519 while (f != eob && !*f)
520 f++, l--;
521 }
522 }
523 io_filesfrom_lastchar = f[-1];
524 }
525 io_filesfrom_buflen = l;
526 }
527 }
528 }
529 }
530
f89b9368
WD
531 if (!FD_ISSET(fd, &r_fds))
532 continue;
554e0a8d 533
4c36ddbe
AT
534 n = read(fd, buf, len);
535
1ea087a7
WD
536 if (n <= 0) {
537 if (n == 0)
1f75bb10 538 whine_about_eof(fd); /* Doesn't return. */
d62bcc17
WD
539 if (errno == EINTR || errno == EWOULDBLOCK
540 || errno == EAGAIN)
7a55d06e 541 continue;
1f75bb10
WD
542
543 /* Don't write errors on a dead socket. */
544 if (fd == sock_f_in)
7f459268 545 close_multiplexing_out();
1f75bb10
WD
546 rsyserr(FERROR, errno, "read error");
547 exit_cleanup(RERR_STREAMIO);
8d9dc9f9 548 }
00bdf899
WD
549
550 buf += n;
551 len -= n;
552 ret += n;
1f75bb10
WD
553
554 if (io_timeout && fd == sock_f_in)
00bdf899 555 last_io = time(NULL);
4c36ddbe 556 }
8d9dc9f9 557
4c36ddbe
AT
558 return ret;
559}
8d9dc9f9 560
56014c8c
WD
561/**
562 * Read a line into the "fname" buffer (which must be at least MAXPATHLEN
563 * characters long).
564 */
565int read_filesfrom_line(int fd, char *fname)
566{
567 char ch, *s, *eob = fname + MAXPATHLEN - 1;
568 int cnt;
d19320fd 569 int reading_remotely = filesfrom_host != NULL;
56014c8c
WD
570 int nulls = eol_nulls || reading_remotely;
571
572 start:
573 s = fname;
574 while (1) {
575 cnt = read(fd, &ch, 1);
576 if (cnt < 0 && (errno == EWOULDBLOCK
577 || errno == EINTR || errno == EAGAIN)) {
578 struct timeval tv;
579 fd_set fds;
580 FD_ZERO(&fds);
581 FD_SET(fd, &fds);
e626b29e 582 tv.tv_sec = select_timeout;
56014c8c
WD
583 tv.tv_usec = 0;
584 if (!select(fd+1, &fds, NULL, NULL, &tv))
585 check_timeout();
586 continue;
587 }
588 if (cnt != 1)
589 break;
590 if (nulls? !ch : (ch == '\r' || ch == '\n')) {
591 /* Skip empty lines if reading locally. */
592 if (!reading_remotely && s == fname)
593 continue;
594 break;
595 }
596 if (s < eob)
597 *s++ = ch;
598 }
599 *s = '\0';
7a55d06e 600
6b45fcf1
WD
601 /* Dump comments. */
602 if (*fname == '#' || *fname == ';')
56014c8c
WD
603 goto start;
604
605 return s - fname;
606}
7a55d06e
MP
607
608
1f75bb10
WD
609static char *iobuf_out;
610static int iobuf_out_cnt;
611
612void io_start_buffering_out(void)
613{
614 if (iobuf_out)
615 return;
616 if (!(iobuf_out = new_array(char, IO_BUFFER_SIZE)))
617 out_of_memory("io_start_buffering_out");
618 iobuf_out_cnt = 0;
619}
620
621
622static char *iobuf_in;
623static size_t iobuf_in_siz;
624
625void io_start_buffering_in(void)
626{
627 if (iobuf_in)
628 return;
629 iobuf_in_siz = 2 * IO_BUFFER_SIZE;
630 if (!(iobuf_in = new_array(char, iobuf_in_siz)))
631 out_of_memory("io_start_buffering_in");
632}
633
634
635void io_end_buffering(void)
636{
637 io_flush(NORMAL_FLUSH);
638 if (!io_multiplexing_out) {
639 free(iobuf_out);
640 iobuf_out = NULL;
641 }
642}
643
644
9e2409ab
WD
645void maybe_send_keepalive(int allowed_lull, int ndx)
646{
647 if (time(NULL) - last_io >= allowed_lull) {
648 if (!iobuf_out || !iobuf_out_cnt) {
3221f451
WD
649 if (protocol_version < 29)
650 return; /* there's nothing we can do */
9e2409ab
WD
651 write_int(sock_f_out, ndx);
652 write_shortint(sock_f_out, ITEM_IS_NEW);
653 }
654 if (iobuf_out)
655 io_flush(NORMAL_FLUSH);
656 }
657}
658
659
880da007
MP
660/**
661 * Continue trying to read len bytes - don't return until len has been
662 * read.
663 **/
3151cbae 664static void read_loop(int fd, char *buf, size_t len)
4c36ddbe
AT
665{
666 while (len) {
667 int n = read_timeout(fd, buf, len);
668
669 buf += n;
670 len -= n;
8d9dc9f9
AT
671 }
672}
673
7a55d06e
MP
674
675/**
676 * Read from the file descriptor handling multiplexing - return number
677 * of bytes read.
d62bcc17
WD
678 *
679 * Never returns <= 0.
7a55d06e 680 */
c399d22a 681static int readfd_unbuffered(int fd, char *buf, size_t len)
8d9dc9f9 682{
6fe25398 683 static size_t remaining;
1f75bb10 684 static size_t iobuf_in_ndx;
909ce14f 685 int tag, ret = 0;
0d67e00a 686 char line[MAXPATHLEN+1];
8d9dc9f9 687
1f75bb10 688 if (!iobuf_in || fd != sock_f_in)
4c36ddbe 689 return read_timeout(fd, buf, len);
8d9dc9f9 690
76c21947 691 if (!io_multiplexing_in && remaining == 0) {
1f75bb10
WD
692 remaining = read_timeout(fd, iobuf_in, iobuf_in_siz);
693 iobuf_in_ndx = 0;
76c21947
WD
694 }
695
8d9dc9f9
AT
696 while (ret == 0) {
697 if (remaining) {
698 len = MIN(len, remaining);
1f75bb10
WD
699 memcpy(buf, iobuf_in + iobuf_in_ndx, len);
700 iobuf_in_ndx += len;
8d9dc9f9
AT
701 remaining -= len;
702 ret = len;
76c21947 703 break;
8d9dc9f9
AT
704 }
705
909ce14f 706 read_loop(fd, line, 4);
ff41a59f 707 tag = IVAL(line, 0);
679e7657 708
8d9dc9f9 709 remaining = tag & 0xFFFFFF;
d17e1dd2 710 tag = (tag >> 24) - MPLEX_BASE;
8d9dc9f9 711
d17e1dd2
WD
712 switch (tag) {
713 case MSG_DATA:
1f75bb10
WD
714 if (remaining > iobuf_in_siz) {
715 if (!(iobuf_in = realloc_array(iobuf_in, char,
716 remaining)))
c399d22a 717 out_of_memory("readfd_unbuffered");
1f75bb10 718 iobuf_in_siz = remaining;
76c21947 719 }
1f75bb10
WD
720 read_loop(fd, iobuf_in, remaining);
721 iobuf_in_ndx = 0;
d17e1dd2 722 break;
0d67e00a
WD
723 case MSG_DELETED:
724 if (remaining >= sizeof line) {
725 rprintf(FERROR, "invalid multi-message %d:%ld\n",
726 tag, (long)remaining);
727 exit_cleanup(RERR_STREAMIO);
728 }
729 read_loop(fd, line, remaining);
730 line[remaining] = '\0';
731 /* A directory name was sent with the trailing null */
732 if (remaining > 0 && !line[remaining-1])
733 log_delete(line, S_IFDIR);
734 else
735 log_delete(line, S_IFREG);
736 remaining = 0;
737 break;
9981c27e
WD
738 case MSG_SUCCESS:
739 if (remaining != 4) {
740 rprintf(FERROR, "invalid multi-message %d:%ld\n",
741 tag, (long)remaining);
742 exit_cleanup(RERR_STREAMIO);
743 }
744 read_loop(fd, line, remaining);
745 successful_send(IVAL(line, 0));
746 remaining = 0;
747 break;
d17e1dd2
WD
748 case MSG_INFO:
749 case MSG_ERROR:
750 if (remaining >= sizeof line) {
bd9fca47
WD
751 rprintf(FERROR,
752 "[%s] multiplexing overflow %d:%ld\n\n",
753 who_am_i(), tag, (long)remaining);
d17e1dd2
WD
754 exit_cleanup(RERR_STREAMIO);
755 }
756 read_loop(fd, line, remaining);
757 rwrite((enum logcode)tag, line, remaining);
758 remaining = 0;
759 break;
760 default:
bd9fca47
WD
761 rprintf(FERROR, "[%s] unexpected tag %d\n",
762 who_am_i(), tag);
65417579 763 exit_cleanup(RERR_STREAMIO);
8d9dc9f9 764 }
8d9dc9f9
AT
765 }
766
76c21947 767 if (remaining == 0)
d17e1dd2 768 io_flush(NORMAL_FLUSH);
76c21947 769
8d9dc9f9
AT
770 return ret;
771}
772
773
909ce14f 774
880da007
MP
775/**
776 * Do a buffered read from @p fd. Don't return until all @p n bytes
777 * have been read. If all @p n can't be read then exit with an
778 * error.
779 **/
3151cbae 780static void readfd(int fd, char *buffer, size_t N)
720b47f2 781{
6ba9279f 782 int ret;
d62bcc17 783 size_t total = 0;
3151cbae 784
6ba9279f 785 while (total < N) {
c399d22a 786 ret = readfd_unbuffered(fd, buffer + total, N-total);
6ba9279f 787 total += ret;
7f28dbee 788 }
1b7c47cb 789
b9f592fb
WD
790 if (fd == write_batch_monitor_in) {
791 if ((size_t)write(batch_fd, buffer, total) != total)
792 exit_cleanup(RERR_FILEIO);
793 }
1f75bb10
WD
794
795 if (fd == sock_f_in)
796 stats.total_read += total;
720b47f2
AT
797}
798
799
0d67e00a 800int read_shortint(int f)
9361f839
WD
801{
802 uchar b[2];
803 readfd(f, (char *)b, 2);
804 return (b[1] << 8) + b[0];
805}
806
807
b7922338 808int32 read_int(int f)
720b47f2 809{
4c36ddbe 810 char b[4];
d730b113
AT
811 int32 ret;
812
4c36ddbe 813 readfd(f,b,4);
d730b113 814 ret = IVAL(b,0);
f89b9368
WD
815 if (ret == (int32)0xffffffff)
816 return -1;
d730b113 817 return ret;
720b47f2
AT
818}
819
71c46176 820int64 read_longint(int f)
3a6a366f 821{
71c46176 822 int64 ret;
3a6a366f
AT
823 char b[8];
824 ret = read_int(f);
71c46176 825
f89b9368 826 if ((int32)ret != (int32)0xffffffff)
8de330a3 827 return ret;
71c46176 828
031fa9ad
WD
829#if SIZEOF_INT64 < 8
830 rprintf(FERROR, "Integer overflow: attempted 64-bit offset\n");
831 exit_cleanup(RERR_UNSUPPORTED);
832#else
91c4da3f
S
833 readfd(f,b,8);
834 ret = IVAL(b,0) | (((int64)IVAL(b,4))<<32);
031fa9ad 835#endif
71c46176 836
3a6a366f
AT
837 return ret;
838}
839
9dd891bb 840void read_buf(int f,char *buf,size_t len)
720b47f2 841{
4c36ddbe 842 readfd(f,buf,len);
720b47f2
AT
843}
844
9dd891bb 845void read_sbuf(int f,char *buf,size_t len)
575f2fca 846{
93095cbe 847 readfd(f, buf, len);
575f2fca
AT
848 buf[len] = 0;
849}
850
9361f839 851uchar read_byte(int f)
182dca5c 852{
9361f839 853 uchar c;
93095cbe 854 readfd(f, (char *)&c, 1);
4c36ddbe 855 return c;
182dca5c 856}
720b47f2 857
188fed95
WD
858/* Populate a sum_struct with values from the socket. This is
859 * called by both the sender and the receiver. */
860void read_sum_head(int f, struct sum_struct *sum)
861{
862 sum->count = read_int(f);
863 sum->blength = read_int(f);
864 if (sum->blength < 0 || sum->blength > MAX_BLOCK_SIZE) {
67dde161
WD
865 rprintf(FERROR, "[%s] Invalid block length %ld\n",
866 who_am_i(), (long)sum->blength);
188fed95
WD
867 exit_cleanup(RERR_PROTOCOL);
868 }
869 sum->s2length = protocol_version < 27 ? csum_length : (int)read_int(f);
870 if (sum->s2length < 0 || sum->s2length > MD4_SUM_LENGTH) {
67dde161
WD
871 rprintf(FERROR, "[%s] Invalid checksum length %d\n",
872 who_am_i(), sum->s2length);
188fed95
WD
873 exit_cleanup(RERR_PROTOCOL);
874 }
875 sum->remainder = read_int(f);
876 if (sum->remainder < 0 || sum->remainder > sum->blength) {
67dde161
WD
877 rprintf(FERROR, "[%s] Invalid remainder length %ld\n",
878 who_am_i(), (long)sum->remainder);
188fed95
WD
879 exit_cleanup(RERR_PROTOCOL);
880 }
881}
882
c207d7ec
WD
883/* Send the values from a sum_struct over the socket. Set sum to
884 * NULL if there are no checksums to send. This is called by both
885 * the generator and the sender. */
886void write_sum_head(int f, struct sum_struct *sum)
887{
888 static struct sum_struct null_sum;
889
890 if (sum == NULL)
891 sum = &null_sum;
892
893 write_int(f, sum->count);
894 write_int(f, sum->blength);
895 if (protocol_version >= 27)
896 write_int(f, sum->s2length);
897 write_int(f, sum->remainder);
898}
899
880da007 900
08571358
MP
901/**
902 * Sleep after writing to limit I/O bandwidth usage.
903 *
904 * @todo Rather than sleeping after each write, it might be better to
905 * use some kind of averaging. The current algorithm seems to always
906 * use a bit less bandwidth than specified, because it doesn't make up
907 * for slow periods. But arguably this is a feature. In addition, we
908 * ought to take the time used to write the data into account.
71e58630
WD
909 *
910 * During some phases of big transfers (file FOO is uptodate) this is
911 * called with a small bytes_written every time. As the kernel has to
912 * round small waits up to guarantee that we actually wait at least the
913 * requested number of microseconds, this can become grossly inaccurate.
914 * We therefore keep track of the bytes we've written over time and only
915 * sleep when the accumulated delay is at least 1 tenth of a second.
08571358
MP
916 **/
917static void sleep_for_bwlimit(int bytes_written)
918{
71e58630
WD
919 static struct timeval prior_tv;
920 static long total_written = 0;
921 struct timeval tv, start_tv;
922 long elapsed_usec, sleep_usec;
923
924#define ONE_SEC 1000000L /* # of microseconds in a second */
08571358
MP
925
926 if (!bwlimit)
927 return;
e681e820 928
71e58630
WD
929 total_written += bytes_written;
930
931 gettimeofday(&start_tv, NULL);
932 if (prior_tv.tv_sec) {
933 elapsed_usec = (start_tv.tv_sec - prior_tv.tv_sec) * ONE_SEC
934 + (start_tv.tv_usec - prior_tv.tv_usec);
935 total_written -= elapsed_usec * bwlimit / (ONE_SEC/1024);
936 if (total_written < 0)
937 total_written = 0;
938 }
3151cbae 939
71e58630
WD
940 sleep_usec = total_written * (ONE_SEC/1024) / bwlimit;
941 if (sleep_usec < ONE_SEC / 10) {
942 prior_tv = start_tv;
943 return;
944 }
08571358 945
71e58630
WD
946 tv.tv_sec = sleep_usec / ONE_SEC;
947 tv.tv_usec = sleep_usec % ONE_SEC;
98b332ed 948 select(0, NULL, NULL, NULL, &tv);
71e58630
WD
949
950 gettimeofday(&prior_tv, NULL);
951 elapsed_usec = (prior_tv.tv_sec - start_tv.tv_sec) * ONE_SEC
952 + (prior_tv.tv_usec - start_tv.tv_usec);
953 total_written = (sleep_usec - elapsed_usec) * bwlimit / (ONE_SEC/1024);
08571358
MP
954}
955
956
1f75bb10
WD
957/* Write len bytes to the file descriptor fd, looping as necessary to get
958 * the job done and also (in the generator) reading any data on msg_fd_in
959 * (to avoid deadlock).
880da007
MP
960 *
961 * This function underlies the multiplexing system. The body of the
1f75bb10 962 * application never calls this function directly. */
9dd891bb 963static void writefd_unbuffered(int fd,char *buf,size_t len)
720b47f2 964{
1ea087a7 965 size_t n, total = 0;
8d9dc9f9 966 fd_set w_fds, r_fds;
1ea087a7 967 int maxfd, count, ret;
8d9dc9f9 968 struct timeval tv;
720b47f2 969
e44f9a12
AT
970 no_flush++;
971
4c36ddbe 972 while (total < len) {
8d9dc9f9 973 FD_ZERO(&w_fds);
8d9dc9f9 974 FD_SET(fd,&w_fds);
1ea087a7 975 maxfd = fd;
4c36ddbe 976
d17e1dd2 977 if (msg_fd_in >= 0) {
56014c8c 978 FD_ZERO(&r_fds);
d17e1dd2 979 FD_SET(msg_fd_in,&r_fds);
1ea087a7
WD
980 if (msg_fd_in > maxfd)
981 maxfd = msg_fd_in;
8d9dc9f9 982 }
41cfde6b
WD
983 if (fd != sock_f_out && iobuf_out_cnt && no_flush == 1) {
984 FD_SET(sock_f_out, &w_fds);
985 if (sock_f_out > maxfd)
986 maxfd = sock_f_out;
987 }
8d9dc9f9 988
e626b29e 989 tv.tv_sec = select_timeout;
8d9dc9f9 990 tv.tv_usec = 0;
4c36ddbe 991
554e0a8d 992 errno = 0;
1ea087a7 993 count = select(maxfd + 1, msg_fd_in >= 0 ? &r_fds : NULL,
d17e1dd2 994 &w_fds, NULL, &tv);
4c36ddbe
AT
995
996 if (count <= 0) {
1ea087a7 997 if (count < 0 && errno == EBADF)
554e0a8d 998 exit_cleanup(RERR_SOCKETIO);
bd717af8 999 check_timeout();
8d9dc9f9
AT
1000 continue;
1001 }
4c36ddbe 1002
d17e1dd2
WD
1003 if (msg_fd_in >= 0 && FD_ISSET(msg_fd_in, &r_fds))
1004 read_msg_fd();
554e0a8d 1005
41cfde6b
WD
1006 if (!FD_ISSET(fd, &w_fds)) {
1007 if (fd != sock_f_out && iobuf_out_cnt) {
1008 no_flush--;
1009 io_flush(NORMAL_FLUSH);
1010 no_flush++;
1011 }
1ea087a7 1012 continue;
41cfde6b 1013 }
4c36ddbe 1014
1ea087a7
WD
1015 n = len - total;
1016 if (bwlimit && n > bwlimit_writemax)
1017 n = bwlimit_writemax;
1018 ret = write(fd, buf + total, n);
1019
1020 if (ret <= 0) {
3309507d
WD
1021 if (ret < 0) {
1022 if (errno == EINTR)
1023 continue;
1024 if (errno == EWOULDBLOCK || errno == EAGAIN) {
1025 msleep(1);
1026 continue;
1027 }
f0359dd0
AT
1028 }
1029
1ea087a7 1030 /* Don't try to write errors back across the stream. */
1f75bb10 1031 if (fd == sock_f_out)
7f459268 1032 close_multiplexing_out();
1ea087a7 1033 rsyserr(FERROR, errno,
dca68b0a
WD
1034 "writefd_unbuffered failed to write %ld bytes: phase \"%s\" [%s]",
1035 (long)len, io_write_phase, who_am_i());
d1b31da7
WD
1036 /* If the other side is sending us error messages, try
1037 * to grab any messages they sent before they died. */
7f459268 1038 while (fd == sock_f_out && io_multiplexing_in) {
ef0c03ff 1039 io_timeout = select_timeout = 30;
9e2409ab 1040 ignore_timeout = 0;
d1b31da7
WD
1041 readfd_unbuffered(sock_f_in, io_filesfrom_buf,
1042 sizeof io_filesfrom_buf);
1043 }
1ea087a7
WD
1044 exit_cleanup(RERR_STREAMIO);
1045 }
4c36ddbe 1046
1ea087a7 1047 total += ret;
a800434a 1048
1f75bb10
WD
1049 if (fd == sock_f_out) {
1050 if (io_timeout)
1051 last_io = time(NULL);
1052 sleep_for_bwlimit(ret);
1053 }
4c36ddbe 1054 }
e44f9a12
AT
1055
1056 no_flush--;
720b47f2
AT
1057}
1058
8d9dc9f9 1059
880da007
MP
1060/**
1061 * Write an message to a multiplexed stream. If this fails then rsync
1062 * exits.
1063 **/
1f75bb10 1064static void mplex_write(enum msgcode code, char *buf, size_t len)
ff41a59f
AT
1065{
1066 char buffer[4096];
06ce139f 1067 size_t n = len;
8d9dc9f9 1068
ff41a59f
AT
1069 SIVAL(buffer, 0, ((MPLEX_BASE + (int)code)<<24) + len);
1070
c399d22a 1071 if (n > sizeof buffer - 4)
3151cbae 1072 n = sizeof buffer - 4;
ff41a59f
AT
1073
1074 memcpy(&buffer[4], buf, n);
1f75bb10 1075 writefd_unbuffered(sock_f_out, buffer, n+4);
ff41a59f
AT
1076
1077 len -= n;
1078 buf += n;
1079
1ea087a7 1080 if (len)
1f75bb10 1081 writefd_unbuffered(sock_f_out, buf, len);
d6dead6b
AT
1082}
1083
ff41a59f 1084
d17e1dd2 1085void io_flush(int flush_it_all)
d6dead6b 1086{
d17e1dd2 1087 msg_list_push(flush_it_all);
90ba34e2 1088
1f75bb10 1089 if (!iobuf_out_cnt || no_flush)
d17e1dd2 1090 return;
8d9dc9f9 1091
d17e1dd2 1092 if (io_multiplexing_out)
1f75bb10 1093 mplex_write(MSG_DATA, iobuf_out, iobuf_out_cnt);
d17e1dd2 1094 else
1f75bb10
WD
1095 writefd_unbuffered(sock_f_out, iobuf_out, iobuf_out_cnt);
1096 iobuf_out_cnt = 0;
8d9dc9f9
AT
1097}
1098
0ba48136 1099
9dd891bb 1100static void writefd(int fd,char *buf,size_t len)
d6dead6b 1101{
4033b80b
WD
1102 if (fd == msg_fd_out) {
1103 rprintf(FERROR, "Internal error: wrong write used in receiver.\n");
1104 exit_cleanup(RERR_PROTOCOL);
1105 }
90ba34e2 1106
1f75bb10
WD
1107 if (fd == sock_f_out)
1108 stats.total_written += len;
1109
b9f592fb
WD
1110 if (fd == write_batch_monitor_out) {
1111 if ((size_t)write(batch_fd, buf, len) != len)
1112 exit_cleanup(RERR_FILEIO);
1113 }
1114
1f75bb10 1115 if (!iobuf_out || fd != sock_f_out) {
4c36ddbe
AT
1116 writefd_unbuffered(fd, buf, len);
1117 return;
1118 }
d6dead6b
AT
1119
1120 while (len) {
1f75bb10 1121 int n = MIN((int)len, IO_BUFFER_SIZE - iobuf_out_cnt);
d6dead6b 1122 if (n > 0) {
1f75bb10 1123 memcpy(iobuf_out+iobuf_out_cnt, buf, n);
d6dead6b
AT
1124 buf += n;
1125 len -= n;
1f75bb10 1126 iobuf_out_cnt += n;
d6dead6b 1127 }
3151cbae 1128
1f75bb10 1129 if (iobuf_out_cnt == IO_BUFFER_SIZE)
d17e1dd2 1130 io_flush(NORMAL_FLUSH);
d6dead6b 1131 }
d6dead6b 1132}
720b47f2
AT
1133
1134
0d67e00a 1135void write_shortint(int f, int x)
9361f839
WD
1136{
1137 uchar b[2];
1138 b[0] = x;
1139 b[1] = x >> 8;
1140 writefd(f, (char *)b, 2);
1141}
1142
1143
b7922338 1144void write_int(int f,int32 x)
720b47f2 1145{
8d9dc9f9
AT
1146 char b[4];
1147 SIVAL(b,0,x);
4c36ddbe 1148 writefd(f,b,4);
720b47f2
AT
1149}
1150
7a24c346 1151
805edf9d
MP
1152void write_int_named(int f, int32 x, const char *phase)
1153{
1154 io_write_phase = phase;
1155 write_int(f, x);
1156 io_write_phase = phase_unknown;
1157}
1158
1159
7a24c346
MP
1160/*
1161 * Note: int64 may actually be a 32-bit type if ./configure couldn't find any
1162 * 64-bit types on this platform.
1163 */
71c46176 1164void write_longint(int f, int64 x)
3a6a366f 1165{
3a6a366f 1166 char b[8];
3a6a366f 1167
91c4da3f 1168 if (x <= 0x7FFFFFFF) {
3a6a366f
AT
1169 write_int(f, (int)x);
1170 return;
1171 }
1172
031fa9ad
WD
1173#if SIZEOF_INT64 < 8
1174 rprintf(FERROR, "Integer overflow: attempted 64-bit offset\n");
1175 exit_cleanup(RERR_UNSUPPORTED);
1176#else
8de330a3 1177 write_int(f, (int32)0xFFFFFFFF);
3a6a366f
AT
1178 SIVAL(b,0,(x&0xFFFFFFFF));
1179 SIVAL(b,4,((x>>32)&0xFFFFFFFF));
1180
4c36ddbe 1181 writefd(f,b,8);
031fa9ad 1182#endif
3a6a366f
AT
1183}
1184
9dd891bb 1185void write_buf(int f,char *buf,size_t len)
720b47f2 1186{
4c36ddbe 1187 writefd(f,buf,len);
720b47f2
AT
1188}
1189
9361f839 1190
880da007 1191/** Write a string to the connection */
cf338ab1 1192void write_sbuf(int f, char *buf)
f0fca04e 1193{
93095cbe 1194 writefd(f, buf, strlen(buf));
f0fca04e
AT
1195}
1196
9361f839
WD
1197
1198void write_byte(int f, uchar c)
182dca5c 1199{
93095cbe 1200 writefd(f, (char *)&c, 1);
182dca5c
AT
1201}
1202
7a55d06e 1203
914cc65c 1204/**
6ed6d7f5
WD
1205 * Read a line of up to @p maxlen characters into @p buf (not counting
1206 * the trailing null). Strips the (required) trailing newline and all
1207 * carriage returns.
914cc65c 1208 *
6ed6d7f5 1209 * @return 1 for success; 0 for I/O error or truncation.
914cc65c 1210 **/
9dd891bb 1211int read_line(int f, char *buf, size_t maxlen)
f0fca04e
AT
1212{
1213 while (maxlen) {
528bfcd7 1214 buf[0] = 0;
f0fca04e 1215 read_buf(f, buf, 1);
914cc65c
MP
1216 if (buf[0] == 0)
1217 return 0;
6ed6d7f5 1218 if (buf[0] == '\n')
f0fca04e 1219 break;
f0fca04e
AT
1220 if (buf[0] != '\r') {
1221 buf++;
1222 maxlen--;
1223 }
1224 }
6ed6d7f5
WD
1225 *buf = '\0';
1226 return maxlen > 0;
f0fca04e
AT
1227}
1228
1229
1230void io_printf(int fd, const char *format, ...)
1231{
d62bcc17 1232 va_list ap;
f0fca04e
AT
1233 char buf[1024];
1234 int len;
3151cbae 1235
f0fca04e 1236 va_start(ap, format);
3151cbae 1237 len = vsnprintf(buf, sizeof buf, format, ap);
f0fca04e
AT
1238 va_end(ap);
1239
f89b9368
WD
1240 if (len < 0)
1241 exit_cleanup(RERR_STREAMIO);
f0fca04e
AT
1242
1243 write_sbuf(fd, buf);
1244}
8d9dc9f9
AT
1245
1246
d17e1dd2 1247/** Setup for multiplexing a MSG_* stream with the data stream. */
1f75bb10 1248void io_start_multiplex_out(void)
8d9dc9f9 1249{
d17e1dd2 1250 io_flush(NORMAL_FLUSH);
1f75bb10 1251 io_start_buffering_out();
8d9dc9f9
AT
1252 io_multiplexing_out = 1;
1253}
1254
d17e1dd2 1255/** Setup for multiplexing a MSG_* stream with the data stream. */
1f75bb10 1256void io_start_multiplex_in(void)
8d9dc9f9 1257{
d17e1dd2 1258 io_flush(NORMAL_FLUSH);
1f75bb10 1259 io_start_buffering_in();
8d9dc9f9
AT
1260 io_multiplexing_in = 1;
1261}
1262
d17e1dd2
WD
1263/** Write an message to the multiplexed data stream. */
1264int io_multiplex_write(enum msgcode code, char *buf, size_t len)
8d9dc9f9 1265{
f89b9368
WD
1266 if (!io_multiplexing_out)
1267 return 0;
8d9dc9f9 1268
d17e1dd2 1269 io_flush(NORMAL_FLUSH);
1b7c47cb 1270 stats.total_written += (len+4);
1f75bb10 1271 mplex_write(code, buf, len);
8d9dc9f9
AT
1272 return 1;
1273}
1274
7f459268
WD
1275void close_multiplexing_in(void)
1276{
1277 io_multiplexing_in = 0;
1278}
1279
d17e1dd2 1280/** Stop output multiplexing. */
7f459268 1281void close_multiplexing_out(void)
554e0a8d
AT
1282{
1283 io_multiplexing_out = 0;
1284}
1285
b9f592fb
WD
1286void start_write_batch(int fd)
1287{
741d6544
WD
1288 write_stream_flags(batch_fd);
1289
b9f592fb
WD
1290 /* Some communication has already taken place, but we don't
1291 * enable batch writing until here so that we can write a
1292 * canonical record of the communication even though the
1293 * actual communication so far depends on whether a daemon
1294 * is involved. */
1295 write_int(batch_fd, protocol_version);
1296 write_int(batch_fd, checksum_seed);
b9f592fb
WD
1297
1298 if (am_sender)
1299 write_batch_monitor_out = fd;
1300 else
1301 write_batch_monitor_in = fd;
1302}
1303
1304void stop_write_batch(void)
1305{
1306 write_batch_monitor_out = -1;
1307 write_batch_monitor_in = -1;
1308}