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