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