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