Make idev, hlink and file_struct + strings use allocation
[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:
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 {
3151cbae
WD
337 rprintf(FERROR,
338 "%s: connection unexpectedly closed "
339 "(%.0f bytes read so far)\n",
340 RSYNC_NAME, (double)stats.total_read);
341
342 exit_cleanup(RERR_STREAMIO);
7a55d06e
MP
343 }
344}
720b47f2 345
7a55d06e 346
3151cbae 347static void die_from_readerr(int err)
7a55d06e
MP
348{
349 /* this prevents us trying to write errors on a dead socket */
350 io_multiplexing_close();
3151cbae 351
7a55d06e 352 rprintf(FERROR, "%s: read error: %s\n",
3151cbae 353 RSYNC_NAME, strerror(err));
7a55d06e
MP
354 exit_cleanup(RERR_STREAMIO);
355}
356
357
880da007 358/**
c3563c46
MP
359 * Read from a socket with IO timeout. return the number of bytes
360 * read. If no bytes can be read then exit, never return a number <= 0.
361 *
8886f8d0
MP
362 * TODO: If the remote shell connection fails, then current versions
363 * actually report an "unexpected EOF" error here. Since it's a
364 * fairly common mistake to try to use rsh when ssh is required, we
365 * should trap that: if we fail to read any data at all, we should
366 * give a better explanation. We can tell whether the connection has
367 * started by looking e.g. at whether the remote version is known yet.
c3563c46 368 */
3151cbae 369static int read_timeout(int fd, char *buf, size_t len)
8d9dc9f9 370{
4c36ddbe
AT
371 int n, ret=0;
372
d17e1dd2 373 io_flush(NORMAL_FLUSH);
ea2111d1 374
4c36ddbe 375 while (ret == 0) {
7a55d06e 376 /* until we manage to read *something* */
56014c8c 377 fd_set r_fds, w_fds;
4c36ddbe 378 struct timeval tv;
554e0a8d 379 int fd_count = fd+1;
a57873b7 380 int count;
4c36ddbe 381
56014c8c
WD
382 FD_ZERO(&r_fds);
383 FD_SET(fd, &r_fds);
d17e1dd2
WD
384 if (msg_fd_in >= 0) {
385 FD_SET(msg_fd_in, &r_fds);
386 if (msg_fd_in >= fd_count)
387 fd_count = msg_fd_in+1;
56014c8c 388 }
3309507d 389 if (io_filesfrom_f_out >= 0) {
56014c8c
WD
390 int new_fd;
391 if (io_filesfrom_buflen == 0) {
3309507d 392 if (io_filesfrom_f_in >= 0) {
56014c8c
WD
393 FD_SET(io_filesfrom_f_in, &r_fds);
394 new_fd = io_filesfrom_f_in;
395 } else {
396 io_filesfrom_f_out = -1;
397 new_fd = -1;
398 }
399 } else {
400 FD_ZERO(&w_fds);
401 FD_SET(io_filesfrom_f_out, &w_fds);
402 new_fd = io_filesfrom_f_out;
403 }
d17e1dd2
WD
404 if (new_fd >= fd_count)
405 fd_count = new_fd+1;
554e0a8d
AT
406 }
407
8cd9fd4e 408 tv.tv_sec = io_timeout?io_timeout:SELECT_TIMEOUT;
4c36ddbe
AT
409 tv.tv_usec = 0;
410
554e0a8d
AT
411 errno = 0;
412
56014c8c
WD
413 count = select(fd_count, &r_fds,
414 io_filesfrom_buflen? &w_fds : NULL,
415 NULL, &tv);
a57873b7
AT
416
417 if (count == 0) {
d17e1dd2 418 msg_list_push(NORMAL_FLUSH);
a57873b7
AT
419 check_timeout();
420 }
421
422 if (count <= 0) {
554e0a8d
AT
423 if (errno == EBADF) {
424 exit_cleanup(RERR_SOCKETIO);
425 }
4c36ddbe
AT
426 continue;
427 }
428
d17e1dd2
WD
429 if (msg_fd_in >= 0 && FD_ISSET(msg_fd_in, &r_fds))
430 read_msg_fd();
554e0a8d 431
3309507d 432 if (io_filesfrom_f_out >= 0) {
56014c8c
WD
433 if (io_filesfrom_buflen) {
434 if (FD_ISSET(io_filesfrom_f_out, &w_fds)) {
435 int l = write(io_filesfrom_f_out,
436 io_filesfrom_bp,
437 io_filesfrom_buflen);
438 if (l > 0) {
439 if (!(io_filesfrom_buflen -= l))
440 io_filesfrom_bp = io_filesfrom_buf;
441 else
442 io_filesfrom_bp += l;
443 } else {
444 /* XXX should we complain? */
445 io_filesfrom_f_out = -1;
446 }
447 }
3309507d 448 } else if (io_filesfrom_f_in >= 0) {
56014c8c
WD
449 if (FD_ISSET(io_filesfrom_f_in, &r_fds)) {
450 int l = read(io_filesfrom_f_in,
451 io_filesfrom_buf,
452 sizeof io_filesfrom_buf);
453 if (l <= 0) {
454 /* Send end-of-file marker */
455 io_filesfrom_buf[0] = '\0';
456 io_filesfrom_buf[1] = '\0';
457 io_filesfrom_buflen = io_filesfrom_lastchar? 2 : 1;
458 io_filesfrom_f_in = -1;
459 } else {
460 extern int eol_nulls;
461 if (!eol_nulls) {
462 char *s = io_filesfrom_buf + l;
463 /* Transform CR and/or LF into '\0' */
464 while (s-- > io_filesfrom_buf) {
465 if (*s == '\n' || *s == '\r')
466 *s = '\0';
467 }
468 }
469 if (!io_filesfrom_lastchar) {
470 /* Last buf ended with a '\0', so don't
471 * let this buf start with one. */
472 while (l && !*io_filesfrom_bp)
473 io_filesfrom_bp++, l--;
474 }
475 if (!l)
476 io_filesfrom_bp = io_filesfrom_buf;
477 else {
478 char *f = io_filesfrom_bp;
479 char *t = f;
480 char *eob = f + l;
481 /* Eliminate any multi-'\0' runs. */
482 while (f != eob) {
483 if (!(*t++ = *f++)) {
484 while (f != eob && !*f)
485 f++, l--;
486 }
487 }
488 io_filesfrom_lastchar = f[-1];
489 }
490 io_filesfrom_buflen = l;
491 }
492 }
493 }
494 }
495
496 if (!FD_ISSET(fd, &r_fds)) continue;
554e0a8d 497
4c36ddbe
AT
498 n = read(fd, buf, len);
499
8d9dc9f9
AT
500 if (n > 0) {
501 buf += n;
502 len -= n;
4c36ddbe
AT
503 ret += n;
504 if (io_timeout)
505 last_io = time(NULL);
506 continue;
7a55d06e 507 } else if (n == 0) {
3151cbae 508 whine_about_eof();
7a55d06e 509 return -1; /* doesn't return */
3309507d 510 } else if (n < 0) {
7a55d06e
MP
511 if (errno == EINTR || errno == EWOULDBLOCK ||
512 errno == EAGAIN)
513 continue;
3151cbae 514 die_from_readerr(errno);
8d9dc9f9 515 }
4c36ddbe 516 }
8d9dc9f9 517
4c36ddbe
AT
518 return ret;
519}
8d9dc9f9 520
56014c8c
WD
521/**
522 * Read a line into the "fname" buffer (which must be at least MAXPATHLEN
523 * characters long).
524 */
525int read_filesfrom_line(int fd, char *fname)
526{
527 char ch, *s, *eob = fname + MAXPATHLEN - 1;
528 int cnt;
529 extern int io_timeout;
530 extern int eol_nulls;
531 extern char *remote_filesfrom_file;
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);
545 tv.tv_sec = io_timeout? io_timeout : SELECT_TIMEOUT;
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.
590 *
591 * Never returns <= 0.
592 */
9dd891bb 593static int read_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);
609 if (!buffer) out_of_memory("read_unbuffered");
610 }
611 remaining = read_timeout(fd, buffer, bufferSz);
612 bufferIdx = 0;
613 }
614
8d9dc9f9
AT
615 while (ret == 0) {
616 if (remaining) {
617 len = MIN(len, remaining);
76c21947
WD
618 memcpy(buf, buffer + bufferIdx, len);
619 bufferIdx += len;
8d9dc9f9
AT
620 remaining -= len;
621 ret = len;
76c21947 622 break;
8d9dc9f9
AT
623 }
624
909ce14f 625 read_loop(fd, line, 4);
ff41a59f 626 tag = IVAL(line, 0);
679e7657 627
8d9dc9f9 628 remaining = tag & 0xFFFFFF;
d17e1dd2 629 tag = (tag >> 24) - MPLEX_BASE;
8d9dc9f9 630
d17e1dd2
WD
631 switch (tag) {
632 case MSG_DATA:
76c21947
WD
633 if (!buffer || remaining > bufferSz) {
634 buffer = realloc_array(buffer, char, remaining);
635 if (!buffer) out_of_memory("read_unbuffered");
636 bufferSz = remaining;
637 }
638 read_loop(fd, buffer, remaining);
639 bufferIdx = 0;
d17e1dd2
WD
640 break;
641 case MSG_INFO:
642 case MSG_ERROR:
643 if (remaining >= sizeof line) {
644 rprintf(FERROR, "multiplexing overflow %d:%ld\n\n",
645 tag, (long)remaining);
646 exit_cleanup(RERR_STREAMIO);
647 }
648 read_loop(fd, line, remaining);
649 rwrite((enum logcode)tag, line, remaining);
650 remaining = 0;
651 break;
652 default:
909ce14f 653 rprintf(FERROR, "unexpected tag %d\n", tag);
65417579 654 exit_cleanup(RERR_STREAMIO);
8d9dc9f9 655 }
8d9dc9f9
AT
656 }
657
76c21947 658 if (remaining == 0)
d17e1dd2 659 io_flush(NORMAL_FLUSH);
76c21947 660
8d9dc9f9
AT
661 return ret;
662}
663
664
909ce14f 665
880da007
MP
666/**
667 * Do a buffered read from @p fd. Don't return until all @p n bytes
668 * have been read. If all @p n can't be read then exit with an
669 * error.
670 **/
3151cbae 671static void readfd(int fd, char *buffer, size_t N)
720b47f2 672{
6ba9279f 673 int ret;
06ce139f 674 size_t total=0;
3151cbae 675
6ba9279f 676 while (total < N) {
3151cbae 677 ret = read_unbuffered(fd, buffer + total, N-total);
6ba9279f 678 total += ret;
7f28dbee 679 }
1b7c47cb
AT
680
681 stats.total_read += total;
720b47f2
AT
682}
683
684
b7922338 685int32 read_int(int f)
720b47f2 686{
4c36ddbe 687 char b[4];
d730b113
AT
688 int32 ret;
689
4c36ddbe 690 readfd(f,b,4);
d730b113
AT
691 ret = IVAL(b,0);
692 if (ret == (int32)0xffffffff) return -1;
693 return ret;
720b47f2
AT
694}
695
71c46176 696int64 read_longint(int f)
3a6a366f 697{
71c46176 698 int64 ret;
3a6a366f
AT
699 char b[8];
700 ret = read_int(f);
71c46176 701
8de330a3
AT
702 if ((int32)ret != (int32)0xffffffff) {
703 return ret;
704 }
71c46176 705
3bee6733 706#ifdef NO_INT64
9486289c 707 rprintf(FERROR,"Integer overflow - attempted 64 bit offset\n");
65417579 708 exit_cleanup(RERR_UNSUPPORTED);
71c46176 709#else
91c4da3f
S
710 readfd(f,b,8);
711 ret = IVAL(b,0) | (((int64)IVAL(b,4))<<32);
71c46176
AT
712#endif
713
3a6a366f
AT
714 return ret;
715}
716
9dd891bb 717void read_buf(int f,char *buf,size_t len)
720b47f2 718{
4c36ddbe 719 readfd(f,buf,len);
720b47f2
AT
720}
721
9dd891bb 722void read_sbuf(int f,char *buf,size_t len)
575f2fca 723{
3151cbae 724 read_buf(f,buf,len);
575f2fca
AT
725 buf[len] = 0;
726}
727
182dca5c
AT
728unsigned char read_byte(int f)
729{
4c36ddbe 730 unsigned char c;
3151cbae 731 read_buf(f, (char *)&c, 1);
4c36ddbe 732 return c;
182dca5c 733}
720b47f2 734
880da007 735
08571358
MP
736/**
737 * Sleep after writing to limit I/O bandwidth usage.
738 *
739 * @todo Rather than sleeping after each write, it might be better to
740 * use some kind of averaging. The current algorithm seems to always
741 * use a bit less bandwidth than specified, because it doesn't make up
742 * for slow periods. But arguably this is a feature. In addition, we
743 * ought to take the time used to write the data into account.
744 **/
745static void sleep_for_bwlimit(int bytes_written)
746{
747 struct timeval tv;
748
749 if (!bwlimit)
750 return;
e681e820
MP
751
752 assert(bytes_written > 0);
753 assert(bwlimit > 0);
3151cbae 754
08571358 755 tv.tv_usec = bytes_written * 1000 / bwlimit;
e681e820
MP
756 tv.tv_sec = tv.tv_usec / 1000000;
757 tv.tv_usec = tv.tv_usec % 1000000;
08571358 758
98b332ed 759 select(0, NULL, NULL, NULL, &tv);
08571358
MP
760}
761
762
880da007
MP
763/**
764 * Write len bytes to the file descriptor @p fd.
765 *
766 * This function underlies the multiplexing system. The body of the
767 * application never calls this function directly.
768 **/
9dd891bb 769static void writefd_unbuffered(int fd,char *buf,size_t len)
720b47f2 770{
06ce139f 771 size_t total = 0;
8d9dc9f9 772 fd_set w_fds, r_fds;
4c36ddbe 773 int fd_count, count;
8d9dc9f9 774 struct timeval tv;
720b47f2 775
d17e1dd2 776 msg_list_push(NORMAL_FLUSH);
90ba34e2 777
e44f9a12
AT
778 no_flush++;
779
4c36ddbe 780 while (total < len) {
8d9dc9f9 781 FD_ZERO(&w_fds);
8d9dc9f9 782 FD_SET(fd,&w_fds);
554e0a8d 783 fd_count = fd;
4c36ddbe 784
d17e1dd2 785 if (msg_fd_in >= 0) {
56014c8c 786 FD_ZERO(&r_fds);
d17e1dd2
WD
787 FD_SET(msg_fd_in,&r_fds);
788 if (msg_fd_in > fd_count)
789 fd_count = msg_fd_in;
8d9dc9f9
AT
790 }
791
8cd9fd4e 792 tv.tv_sec = io_timeout?io_timeout:SELECT_TIMEOUT;
8d9dc9f9 793 tv.tv_usec = 0;
4c36ddbe 794
554e0a8d 795 errno = 0;
d17e1dd2
WD
796 count = select(fd_count+1, msg_fd_in >= 0 ? &r_fds : NULL,
797 &w_fds, NULL, &tv);
4c36ddbe 798
a57873b7 799 if (count == 0) {
d17e1dd2 800 msg_list_push(NORMAL_FLUSH);
a57873b7
AT
801 check_timeout();
802 }
803
4c36ddbe 804 if (count <= 0) {
554e0a8d
AT
805 if (errno == EBADF) {
806 exit_cleanup(RERR_SOCKETIO);
807 }
8d9dc9f9
AT
808 continue;
809 }
4c36ddbe 810
d17e1dd2
WD
811 if (msg_fd_in >= 0 && FD_ISSET(msg_fd_in, &r_fds))
812 read_msg_fd();
554e0a8d 813
8d9dc9f9 814 if (FD_ISSET(fd, &w_fds)) {
06ce139f
MP
815 int ret;
816 size_t n = len-total;
f0359dd0 817 ret = write(fd,buf+total,n);
4c36ddbe 818
3309507d
WD
819 if (ret < 0) {
820 if (errno == EINTR)
821 continue;
822 if (errno == EWOULDBLOCK || errno == EAGAIN) {
823 msleep(1);
824 continue;
825 }
f0359dd0
AT
826 }
827
4c36ddbe 828 if (ret <= 0) {
befbfe61
MP
829 /* Don't try to write errors back
830 * across the stream */
831 io_multiplexing_close();
3ce0f9a6 832 rprintf(FERROR, RSYNC_NAME
98b332ed 833 ": writefd_unbuffered failed to write %ld bytes: phase \"%s\": %s\n",
eca2adb4 834 (long) len, io_write_phase,
ce6c7c63 835 strerror(errno));
65417579 836 exit_cleanup(RERR_STREAMIO);
4c36ddbe
AT
837 }
838
08571358 839 sleep_for_bwlimit(ret);
ef5d23eb 840
4c36ddbe 841 total += ret;
a800434a 842
4c36ddbe
AT
843 if (io_timeout)
844 last_io = time(NULL);
8d9dc9f9 845 }
4c36ddbe 846 }
e44f9a12
AT
847
848 no_flush--;
720b47f2
AT
849}
850
8d9dc9f9 851
d6dead6b
AT
852static char *io_buffer;
853static int io_buffer_count;
854
76c21947 855void io_start_buffering_out(int fd)
d6dead6b 856{
8d9dc9f9 857 if (io_buffer) return;
679e7657 858 multiplex_out_fd = fd;
58cadc86 859 io_buffer = new_array(char, IO_BUFFER_SIZE);
d6dead6b
AT
860 if (!io_buffer) out_of_memory("writefd");
861 io_buffer_count = 0;
ff41a59f
AT
862}
863
76c21947
WD
864void io_start_buffering_in(int fd)
865{
866 multiplex_in_fd = fd;
867}
868
880da007
MP
869/**
870 * Write an message to a multiplexed stream. If this fails then rsync
871 * exits.
872 **/
d17e1dd2 873static void mplex_write(int fd, enum msgcode code, char *buf, size_t len)
ff41a59f
AT
874{
875 char buffer[4096];
06ce139f 876 size_t n = len;
8d9dc9f9 877
ff41a59f
AT
878 SIVAL(buffer, 0, ((MPLEX_BASE + (int)code)<<24) + len);
879
3151cbae
WD
880 if (n > (sizeof buffer - 4)) {
881 n = sizeof buffer - 4;
ff41a59f
AT
882 }
883
884 memcpy(&buffer[4], buf, n);
885 writefd_unbuffered(fd, buffer, n+4);
886
887 len -= n;
888 buf += n;
889
6d7b6081
AT
890 if (len) {
891 writefd_unbuffered(fd, buf, len);
892 }
d6dead6b
AT
893}
894
ff41a59f 895
d17e1dd2 896void io_flush(int flush_it_all)
d6dead6b 897{
679e7657 898 int fd = multiplex_out_fd;
d17e1dd2
WD
899
900 msg_list_push(flush_it_all);
90ba34e2 901
d17e1dd2
WD
902 if (!io_buffer_count || no_flush)
903 return;
8d9dc9f9 904
d17e1dd2
WD
905 if (io_multiplexing_out)
906 mplex_write(fd, MSG_DATA, io_buffer, io_buffer_count);
907 else
4c36ddbe 908 writefd_unbuffered(fd, io_buffer, io_buffer_count);
8d9dc9f9
AT
909 io_buffer_count = 0;
910}
911
0ba48136 912
7b5c3eb0 913void io_end_buffering(void)
8d9dc9f9 914{
d17e1dd2 915 io_flush(NORMAL_FLUSH);
8d9dc9f9 916 if (!io_multiplexing_out) {
ff41a59f 917 free(io_buffer);
8d9dc9f9
AT
918 io_buffer = NULL;
919 }
d6dead6b
AT
920}
921
9dd891bb 922static void writefd(int fd,char *buf,size_t len)
d6dead6b 923{
1b7c47cb
AT
924 stats.total_written += len;
925
d17e1dd2 926 msg_list_push(NORMAL_FLUSH);
90ba34e2 927
554e0a8d 928 if (!io_buffer || fd != multiplex_out_fd) {
4c36ddbe
AT
929 writefd_unbuffered(fd, buf, len);
930 return;
931 }
d6dead6b
AT
932
933 while (len) {
7b5c3eb0 934 int n = MIN((int) len, IO_BUFFER_SIZE-io_buffer_count);
d6dead6b
AT
935 if (n > 0) {
936 memcpy(io_buffer+io_buffer_count, buf, n);
937 buf += n;
938 len -= n;
939 io_buffer_count += n;
940 }
3151cbae 941
d17e1dd2
WD
942 if (io_buffer_count == IO_BUFFER_SIZE)
943 io_flush(NORMAL_FLUSH);
d6dead6b 944 }
d6dead6b 945}
720b47f2
AT
946
947
b7922338 948void write_int(int f,int32 x)
720b47f2 949{
8d9dc9f9
AT
950 char b[4];
951 SIVAL(b,0,x);
4c36ddbe 952 writefd(f,b,4);
720b47f2
AT
953}
954
7a24c346 955
805edf9d
MP
956void write_int_named(int f, int32 x, const char *phase)
957{
958 io_write_phase = phase;
959 write_int(f, x);
960 io_write_phase = phase_unknown;
961}
962
963
7a24c346
MP
964/*
965 * Note: int64 may actually be a 32-bit type if ./configure couldn't find any
966 * 64-bit types on this platform.
967 */
71c46176 968void write_longint(int f, int64 x)
3a6a366f 969{
3a6a366f 970 char b[8];
3a6a366f 971
91c4da3f 972 if (x <= 0x7FFFFFFF) {
3a6a366f
AT
973 write_int(f, (int)x);
974 return;
975 }
976
67863f46
S
977#ifdef NO_INT64
978 rprintf(FERROR,"Integer overflow - attempted 64 bit offset\n");
979 exit_cleanup(RERR_UNSUPPORTED);
980#else
8de330a3 981 write_int(f, (int32)0xFFFFFFFF);
3a6a366f
AT
982 SIVAL(b,0,(x&0xFFFFFFFF));
983 SIVAL(b,4,((x>>32)&0xFFFFFFFF));
984
4c36ddbe 985 writefd(f,b,8);
67863f46 986#endif
3a6a366f
AT
987}
988
9dd891bb 989void write_buf(int f,char *buf,size_t len)
720b47f2 990{
4c36ddbe 991 writefd(f,buf,len);
720b47f2
AT
992}
993
880da007 994/** Write a string to the connection */
6e4fb64e 995static void write_sbuf(int f,char *buf)
f0fca04e
AT
996{
997 write_buf(f, buf, strlen(buf));
998}
999
720b47f2 1000
182dca5c
AT
1001void write_byte(int f,unsigned char c)
1002{
f0fca04e 1003 write_buf(f,(char *)&c,1);
182dca5c
AT
1004}
1005
7a55d06e
MP
1006
1007
914cc65c
MP
1008/**
1009 * Read a line of up to @p maxlen characters into @p buf. Does not
1010 * contain a trailing newline or carriage return.
1011 *
1012 * @return 1 for success; 0 for io error or truncation.
1013 **/
9dd891bb 1014int read_line(int f, char *buf, size_t maxlen)
f0fca04e
AT
1015{
1016 while (maxlen) {
528bfcd7 1017 buf[0] = 0;
f0fca04e 1018 read_buf(f, buf, 1);
914cc65c
MP
1019 if (buf[0] == 0)
1020 return 0;
f0fca04e
AT
1021 if (buf[0] == '\n') {
1022 buf[0] = 0;
1023 break;
1024 }
1025 if (buf[0] != '\r') {
1026 buf++;
1027 maxlen--;
1028 }
1029 }
1030 if (maxlen == 0) {
1031 *buf = 0;
1032 return 0;
1033 }
528bfcd7 1034
f0fca04e
AT
1035 return 1;
1036}
1037
1038
1039void io_printf(int fd, const char *format, ...)
1040{
1041 va_list ap;
1042 char buf[1024];
1043 int len;
3151cbae 1044
f0fca04e 1045 va_start(ap, format);
3151cbae 1046 len = vsnprintf(buf, sizeof buf, format, ap);
f0fca04e
AT
1047 va_end(ap);
1048
65417579 1049 if (len < 0) exit_cleanup(RERR_STREAMIO);
f0fca04e
AT
1050
1051 write_sbuf(fd, buf);
1052}
8d9dc9f9
AT
1053
1054
d17e1dd2 1055/** Setup for multiplexing a MSG_* stream with the data stream. */
8d9dc9f9
AT
1056void io_start_multiplex_out(int fd)
1057{
679e7657 1058 multiplex_out_fd = fd;
d17e1dd2 1059 io_flush(NORMAL_FLUSH);
76c21947 1060 io_start_buffering_out(fd);
8d9dc9f9
AT
1061 io_multiplexing_out = 1;
1062}
1063
d17e1dd2 1064/** Setup for multiplexing a MSG_* stream with the data stream. */
8d9dc9f9
AT
1065void io_start_multiplex_in(int fd)
1066{
679e7657 1067 multiplex_in_fd = fd;
d17e1dd2 1068 io_flush(NORMAL_FLUSH);
8d9dc9f9
AT
1069 io_multiplexing_in = 1;
1070}
1071
d17e1dd2
WD
1072/** Write an message to the multiplexed data stream. */
1073int io_multiplex_write(enum msgcode code, char *buf, size_t len)
8d9dc9f9
AT
1074{
1075 if (!io_multiplexing_out) return 0;
1076
d17e1dd2 1077 io_flush(NORMAL_FLUSH);
1b7c47cb 1078 stats.total_written += (len+4);
ff41a59f 1079 mplex_write(multiplex_out_fd, code, buf, len);
8d9dc9f9
AT
1080 return 1;
1081}
1082
d17e1dd2 1083/** Stop output multiplexing. */
554e0a8d
AT
1084void io_multiplexing_close(void)
1085{
1086 io_multiplexing_out = 0;
1087}
1088