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