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