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