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