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