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