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