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