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