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