1 /* -*- c-file-style: "linux" -*-
3 * Copyright (C) 1996-2001 by Andrew Tridgell
4 * Copyright (C) Paul Mackerras 1996
5 * Copyright (C) 2001, 2002 by Martin Pool <mbp@samba.org>
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.
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.
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.
25 * Socket and pipe I/O utilities used in rsync.
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
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().
39 /** If no timeout is specified then use a 60 second select timeout */
40 #define SELECT_TIMEOUT 60
43 extern size_t bwlimit_writemax;
45 extern int io_timeout;
49 extern int am_generator;
51 extern int csum_length;
52 extern int checksum_seed;
53 extern int protocol_version;
54 extern char *remote_filesfrom_file;
55 extern struct stats stats;
57 const char phase_unknown[] = "unknown";
58 int select_timeout = SELECT_TIMEOUT;
60 int batch_gen_fd = -1;
63 * The connection might be dropped at some point; perhaps because the
64 * remote instance crashed. Just giving the offset on the stream is
65 * not very helpful. So instead we try to make io_phase_name point to
68 * For buffered/multiplexed I/O these names will be somewhat
69 * approximate; perhaps for ease of support we would rather make the
70 * buffer always flush when a single application-level I/O finishes.
72 * @todo Perhaps we want some simple stack functionality, but there's
73 * no need to overdo it.
75 const char *io_write_phase = phase_unknown;
76 const char *io_read_phase = phase_unknown;
78 /** Ignore EOF errors while reading a module listing if the remote
79 version is 24 or less. */
80 int kludge_around_eof = False;
85 static int io_multiplexing_out;
86 static int io_multiplexing_in;
87 static int sock_f_in = -1;
88 static int sock_f_out = -1;
89 static time_t last_io;
92 static int write_batch_monitor_in = -1;
93 static int write_batch_monitor_out = -1;
95 static int io_filesfrom_f_in = -1;
96 static int io_filesfrom_f_out = -1;
97 static char io_filesfrom_buf[2048];
98 static char *io_filesfrom_bp;
99 static char io_filesfrom_lastchar;
100 static int io_filesfrom_buflen;
102 static void read_loop(int fd, char *buf, size_t len);
105 struct redo_list *next;
109 static struct redo_list *redo_list_head;
110 static struct redo_list *redo_list_tail;
113 struct msg_list *next;
118 static struct msg_list *msg_list_head;
119 static struct msg_list *msg_list_tail;
121 static void redo_list_add(int num)
123 struct redo_list *rl;
125 if (!(rl = new(struct redo_list)))
126 exit_cleanup(RERR_MALLOC);
130 redo_list_tail->next = rl;
136 static void check_timeout(void)
144 last_io = time(NULL);
150 if (t - last_io >= io_timeout) {
151 if (!am_server && !am_daemon) {
152 rprintf(FERROR, "io timeout after %d seconds - exiting\n",
155 exit_cleanup(RERR_TIMEOUT);
159 /* Note the fds used for the main socket (which might really be a pipe
160 * for a local transfer, but we can ignore that). */
161 void io_set_sock_fds(int f_in, int f_out)
167 /* Setup the fd used to receive MSG_* messages. Only needed during the
168 * early stages of being a local sender (up through the sending of the
169 * file list) or when we're the generator (to fetch the messages from
171 void set_msg_fd_in(int fd)
176 /* Setup the fd used to send our MSG_* messages. Only needed when
177 * we're the receiver (to send our messages to the generator). */
178 void set_msg_fd_out(int fd)
181 set_nonblocking(msg_fd_out);
184 /* Add a message to the pending MSG_* list. */
185 static void msg_list_add(int code, char *buf, int len)
189 if (!(ml = new(struct msg_list)))
190 exit_cleanup(RERR_MALLOC);
192 if (!(ml->buf = new_array(char, len+4)))
193 exit_cleanup(RERR_MALLOC);
194 SIVAL(ml->buf, 0, ((code+MPLEX_BASE)<<24) | len);
195 memcpy(ml->buf+4, buf, len);
198 msg_list_tail->next = ml;
204 void send_msg(enum msgcode code, char *buf, int len)
206 msg_list_add(code, buf, len);
207 msg_list_push(NORMAL_FLUSH);
210 /* Read a message from the MSG_* fd and handle it. This is called either
211 * during the early stages of being a local sender (up through the sending
212 * of the file list) or when we're the generator (to fetch the messages
213 * from the receiver). */
214 static void read_msg_fd(void)
221 /* Temporarily disable msg_fd_in. This is needed to avoid looping back
222 * to this routine from read_timeout() and writefd_unbuffered(). */
225 read_loop(fd, buf, 4);
228 len = tag & 0xFFFFFF;
229 tag = (tag >> 24) - MPLEX_BASE;
233 if (len != 0 || !am_generator) {
234 rprintf(FERROR, "invalid message %d:%d\n", tag, len);
235 exit_cleanup(RERR_STREAMIO);
240 if (len != 4 || !am_generator) {
241 rprintf(FERROR, "invalid message %d:%d\n", tag, len);
242 exit_cleanup(RERR_STREAMIO);
244 read_loop(fd, buf, 4);
245 redo_list_add(IVAL(buf,0));
254 read_loop(fd, buf, n);
255 rwrite((enum logcode)tag, buf, n);
260 rprintf(FERROR, "unknown message %d:%d\n", tag, len);
261 exit_cleanup(RERR_STREAMIO);
267 /* Try to push messages off the list onto the wire. If we leave with more
268 * to do, return 0. On error, return -1. If everything flushed, return 1.
269 * This is only active in the receiver. */
270 int msg_list_push(int flush_it_all)
272 static int written = 0;
279 while (msg_list_head) {
280 struct msg_list *ml = msg_list_head;
281 int n = write(msg_fd_out, ml->buf + written, ml->len - written);
285 if (errno != EWOULDBLOCK && errno != EAGAIN)
290 FD_SET(msg_fd_out, &fds);
291 tv.tv_sec = select_timeout;
293 if (!select(msg_fd_out+1, NULL, &fds, NULL, &tv))
295 } else if ((written += n) == ml->len) {
297 msg_list_head = ml->next;
299 msg_list_tail = NULL;
307 int get_redo_num(void)
309 struct redo_list *next;
312 while (!redo_list_head)
315 num = redo_list_head->num;
316 next = redo_list_head->next;
317 free(redo_list_head);
318 redo_list_head = next;
320 redo_list_tail = NULL;
326 * When we're the receiver and we have a local --files-from list of names
327 * that needs to be sent over the socket to the sender, we have to do two
328 * things at the same time: send the sender a list of what files we're
329 * processing and read the incoming file+info list from the sender. We do
330 * this by augmenting the read_timeout() function to copy this data. It
331 * uses the io_filesfrom_buf to read a block of data from f_in (when it is
332 * ready, since it might be a pipe) and then blast it out f_out (when it
333 * is ready to receive more data).
335 void io_set_filesfrom_fds(int f_in, int f_out)
337 io_filesfrom_f_in = f_in;
338 io_filesfrom_f_out = f_out;
339 io_filesfrom_bp = io_filesfrom_buf;
340 io_filesfrom_lastchar = '\0';
341 io_filesfrom_buflen = 0;
345 * It's almost always an error to get an EOF when we're trying to read
346 * from the network, because the protocol is self-terminating.
348 * However, there is one unfortunate cases where it is not, which is
349 * rsync <2.4.6 sending a list of modules on a server, since the list
350 * is terminated by closing the socket. So, for the section of the
351 * program where that is a problem (start_socket_client),
352 * kludge_around_eof is True and we just exit.
354 static void whine_about_eof(int fd)
356 if (kludge_around_eof && fd == sock_f_in)
359 rprintf(FERROR, RSYNC_NAME ": connection unexpectedly closed "
360 "(%.0f bytes received so far) [%s]\n",
361 (double)stats.total_read, who_am_i());
363 exit_cleanup(RERR_STREAMIO);
368 * Read from a socket with I/O timeout. return the number of bytes
369 * read. If no bytes can be read then exit, never return a number <= 0.
371 * TODO: If the remote shell connection fails, then current versions
372 * actually report an "unexpected EOF" error here. Since it's a
373 * fairly common mistake to try to use rsh when ssh is required, we
374 * should trap that: if we fail to read any data at all, we should
375 * give a better explanation. We can tell whether the connection has
376 * started by looking e.g. at whether the remote version is known yet.
378 static int read_timeout(int fd, char *buf, size_t len)
382 io_flush(NORMAL_FLUSH);
385 /* until we manage to read *something* */
394 if (msg_fd_in >= 0) {
395 FD_SET(msg_fd_in, &r_fds);
396 if (msg_fd_in > maxfd)
398 } else if (msg_list_head) {
399 FD_SET(msg_fd_out, &w_fds);
400 if (msg_fd_out > maxfd)
403 if (io_filesfrom_f_out >= 0) {
405 if (io_filesfrom_buflen == 0) {
406 if (io_filesfrom_f_in >= 0) {
407 FD_SET(io_filesfrom_f_in, &r_fds);
408 new_fd = io_filesfrom_f_in;
410 io_filesfrom_f_out = -1;
414 FD_SET(io_filesfrom_f_out, &w_fds);
415 new_fd = io_filesfrom_f_out;
421 tv.tv_sec = select_timeout;
426 count = select(maxfd + 1, &r_fds, &w_fds, NULL, &tv);
430 exit_cleanup(RERR_SOCKETIO);
435 if (msg_fd_in >= 0 && FD_ISSET(msg_fd_in, &r_fds))
437 else if (msg_list_head && FD_ISSET(msg_fd_out, &w_fds))
438 msg_list_push(NORMAL_FLUSH);
440 if (io_filesfrom_f_out >= 0) {
441 if (io_filesfrom_buflen) {
442 if (FD_ISSET(io_filesfrom_f_out, &w_fds)) {
443 int l = write(io_filesfrom_f_out,
445 io_filesfrom_buflen);
447 if (!(io_filesfrom_buflen -= l))
448 io_filesfrom_bp = io_filesfrom_buf;
450 io_filesfrom_bp += l;
452 /* XXX should we complain? */
453 io_filesfrom_f_out = -1;
456 } else if (io_filesfrom_f_in >= 0) {
457 if (FD_ISSET(io_filesfrom_f_in, &r_fds)) {
458 int l = read(io_filesfrom_f_in,
460 sizeof io_filesfrom_buf);
462 /* Send end-of-file marker */
463 io_filesfrom_buf[0] = '\0';
464 io_filesfrom_buf[1] = '\0';
465 io_filesfrom_buflen = io_filesfrom_lastchar? 2 : 1;
466 io_filesfrom_f_in = -1;
469 char *s = io_filesfrom_buf + l;
470 /* Transform CR and/or LF into '\0' */
471 while (s-- > io_filesfrom_buf) {
472 if (*s == '\n' || *s == '\r')
476 if (!io_filesfrom_lastchar) {
477 /* Last buf ended with a '\0', so don't
478 * let this buf start with one. */
479 while (l && !*io_filesfrom_bp)
480 io_filesfrom_bp++, l--;
483 io_filesfrom_bp = io_filesfrom_buf;
485 char *f = io_filesfrom_bp;
488 /* Eliminate any multi-'\0' runs. */
490 if (!(*t++ = *f++)) {
491 while (f != eob && !*f)
495 io_filesfrom_lastchar = f[-1];
497 io_filesfrom_buflen = l;
503 if (!FD_ISSET(fd, &r_fds))
506 n = read(fd, buf, len);
510 whine_about_eof(fd); /* Doesn't return. */
511 if (errno == EINTR || errno == EWOULDBLOCK
515 /* Don't write errors on a dead socket. */
517 close_multiplexing_out();
518 rsyserr(FERROR, errno, "read error");
519 exit_cleanup(RERR_STREAMIO);
526 if (io_timeout && fd == sock_f_in)
527 last_io = time(NULL);
534 * Read a line into the "fname" buffer (which must be at least MAXPATHLEN
537 int read_filesfrom_line(int fd, char *fname)
539 char ch, *s, *eob = fname + MAXPATHLEN - 1;
541 int reading_remotely = remote_filesfrom_file != NULL;
542 int nulls = eol_nulls || reading_remotely;
547 cnt = read(fd, &ch, 1);
548 if (cnt < 0 && (errno == EWOULDBLOCK
549 || errno == EINTR || errno == EAGAIN)) {
554 tv.tv_sec = select_timeout;
556 if (!select(fd+1, &fds, NULL, NULL, &tv))
562 if (nulls? !ch : (ch == '\r' || ch == '\n')) {
563 /* Skip empty lines if reading locally. */
564 if (!reading_remotely && s == fname)
574 if (*fname == '#' || *fname == ';')
581 static char *iobuf_out;
582 static int iobuf_out_cnt;
584 void io_start_buffering_out(void)
588 if (!(iobuf_out = new_array(char, IO_BUFFER_SIZE)))
589 out_of_memory("io_start_buffering_out");
594 static char *iobuf_in;
595 static size_t iobuf_in_siz;
597 void io_start_buffering_in(void)
601 iobuf_in_siz = 2 * IO_BUFFER_SIZE;
602 if (!(iobuf_in = new_array(char, iobuf_in_siz)))
603 out_of_memory("io_start_buffering_in");
607 void io_end_buffering(void)
609 io_flush(NORMAL_FLUSH);
610 if (!io_multiplexing_out) {
618 * Continue trying to read len bytes - don't return until len has been
621 static void read_loop(int fd, char *buf, size_t len)
624 int n = read_timeout(fd, buf, len);
633 * Read from the file descriptor handling multiplexing - return number
636 * Never returns <= 0.
638 static int readfd_unbuffered(int fd, char *buf, size_t len)
640 static size_t remaining;
641 static size_t iobuf_in_ndx;
645 if (!iobuf_in || fd != sock_f_in)
646 return read_timeout(fd, buf, len);
648 if (!io_multiplexing_in && remaining == 0) {
649 remaining = read_timeout(fd, iobuf_in, iobuf_in_siz);
655 len = MIN(len, remaining);
656 memcpy(buf, iobuf_in + iobuf_in_ndx, len);
663 read_loop(fd, line, 4);
666 remaining = tag & 0xFFFFFF;
667 tag = (tag >> 24) - MPLEX_BASE;
671 if (remaining > iobuf_in_siz) {
672 if (!(iobuf_in = realloc_array(iobuf_in, char,
674 out_of_memory("readfd_unbuffered");
675 iobuf_in_siz = remaining;
677 read_loop(fd, iobuf_in, remaining);
682 if (remaining >= sizeof line) {
684 "[%s] multiplexing overflow %d:%ld\n\n",
685 who_am_i(), tag, (long)remaining);
686 exit_cleanup(RERR_STREAMIO);
688 read_loop(fd, line, remaining);
689 rwrite((enum logcode)tag, line, remaining);
693 rprintf(FERROR, "[%s] unexpected tag %d\n",
695 exit_cleanup(RERR_STREAMIO);
700 io_flush(NORMAL_FLUSH);
708 * Do a buffered read from @p fd. Don't return until all @p n bytes
709 * have been read. If all @p n can't be read then exit with an
712 static void readfd(int fd, char *buffer, size_t N)
718 ret = readfd_unbuffered(fd, buffer + total, N-total);
722 if (fd == write_batch_monitor_in) {
723 if ((size_t)write(batch_fd, buffer, total) != total)
724 exit_cleanup(RERR_FILEIO);
728 stats.total_read += total;
732 int32 read_int(int f)
739 if (ret == (int32)0xffffffff)
744 int64 read_longint(int f)
750 if ((int32)ret != (int32)0xffffffff)
753 #ifdef INT64_IS_OFF_T
754 if (sizeof (int64) < 8) {
755 rprintf(FERROR, "Integer overflow: attempted 64-bit offset\n");
756 exit_cleanup(RERR_UNSUPPORTED);
760 ret = IVAL(b,0) | (((int64)IVAL(b,4))<<32);
765 void read_buf(int f,char *buf,size_t len)
770 void read_sbuf(int f,char *buf,size_t len)
776 unsigned char read_byte(int f)
779 readfd(f, (char *)&c, 1);
783 /* Populate a sum_struct with values from the socket. This is
784 * called by both the sender and the receiver. */
785 void read_sum_head(int f, struct sum_struct *sum)
787 sum->count = read_int(f);
788 sum->blength = read_int(f);
789 if (sum->blength < 0 || sum->blength > MAX_BLOCK_SIZE) {
790 rprintf(FERROR, "Invalid block length %ld\n",
792 exit_cleanup(RERR_PROTOCOL);
794 sum->s2length = protocol_version < 27 ? csum_length : (int)read_int(f);
795 if (sum->s2length < 0 || sum->s2length > MD4_SUM_LENGTH) {
796 rprintf(FERROR, "Invalid checksum length %d\n", sum->s2length);
797 exit_cleanup(RERR_PROTOCOL);
799 sum->remainder = read_int(f);
800 if (sum->remainder < 0 || sum->remainder > sum->blength) {
801 rprintf(FERROR, "Invalid remainder length %ld\n",
802 (long)sum->remainder);
803 exit_cleanup(RERR_PROTOCOL);
807 /* Send the values from a sum_struct over the socket. Set sum to
808 * NULL if there are no checksums to send. This is called by both
809 * the generator and the sender. */
810 void write_sum_head(int f, struct sum_struct *sum)
812 static struct sum_struct null_sum;
817 write_int(f, sum->count);
818 write_int(f, sum->blength);
819 if (protocol_version >= 27)
820 write_int(f, sum->s2length);
821 write_int(f, sum->remainder);
826 * Sleep after writing to limit I/O bandwidth usage.
828 * @todo Rather than sleeping after each write, it might be better to
829 * use some kind of averaging. The current algorithm seems to always
830 * use a bit less bandwidth than specified, because it doesn't make up
831 * for slow periods. But arguably this is a feature. In addition, we
832 * ought to take the time used to write the data into account.
834 * During some phases of big transfers (file FOO is uptodate) this is
835 * called with a small bytes_written every time. As the kernel has to
836 * round small waits up to guarantee that we actually wait at least the
837 * requested number of microseconds, this can become grossly inaccurate.
838 * We therefore keep track of the bytes we've written over time and only
839 * sleep when the accumulated delay is at least 1 tenth of a second.
841 static void sleep_for_bwlimit(int bytes_written)
843 static struct timeval prior_tv;
844 static long total_written = 0;
845 struct timeval tv, start_tv;
846 long elapsed_usec, sleep_usec;
848 #define ONE_SEC 1000000L /* # of microseconds in a second */
853 total_written += bytes_written;
855 gettimeofday(&start_tv, NULL);
856 if (prior_tv.tv_sec) {
857 elapsed_usec = (start_tv.tv_sec - prior_tv.tv_sec) * ONE_SEC
858 + (start_tv.tv_usec - prior_tv.tv_usec);
859 total_written -= elapsed_usec * bwlimit / (ONE_SEC/1024);
860 if (total_written < 0)
864 sleep_usec = total_written * (ONE_SEC/1024) / bwlimit;
865 if (sleep_usec < ONE_SEC / 10) {
870 tv.tv_sec = sleep_usec / ONE_SEC;
871 tv.tv_usec = sleep_usec % ONE_SEC;
872 select(0, NULL, NULL, NULL, &tv);
874 gettimeofday(&prior_tv, NULL);
875 elapsed_usec = (prior_tv.tv_sec - start_tv.tv_sec) * ONE_SEC
876 + (prior_tv.tv_usec - start_tv.tv_usec);
877 total_written = (sleep_usec - elapsed_usec) * bwlimit / (ONE_SEC/1024);
881 /* Write len bytes to the file descriptor fd, looping as necessary to get
882 * the job done and also (in the generator) reading any data on msg_fd_in
883 * (to avoid deadlock).
885 * This function underlies the multiplexing system. The body of the
886 * application never calls this function directly. */
887 static void writefd_unbuffered(int fd,char *buf,size_t len)
891 int maxfd, count, ret;
896 while (total < len) {
901 if (msg_fd_in >= 0) {
903 FD_SET(msg_fd_in,&r_fds);
904 if (msg_fd_in > maxfd)
907 if (fd != sock_f_out && iobuf_out_cnt && no_flush == 1) {
908 FD_SET(sock_f_out, &w_fds);
909 if (sock_f_out > maxfd)
913 tv.tv_sec = select_timeout;
917 count = select(maxfd + 1, msg_fd_in >= 0 ? &r_fds : NULL,
921 if (count < 0 && errno == EBADF)
922 exit_cleanup(RERR_SOCKETIO);
927 if (msg_fd_in >= 0 && FD_ISSET(msg_fd_in, &r_fds))
930 if (!FD_ISSET(fd, &w_fds)) {
931 if (fd != sock_f_out && iobuf_out_cnt) {
933 io_flush(NORMAL_FLUSH);
940 if (bwlimit && n > bwlimit_writemax)
941 n = bwlimit_writemax;
942 ret = write(fd, buf + total, n);
948 if (errno == EWOULDBLOCK || errno == EAGAIN) {
954 /* Don't try to write errors back across the stream. */
955 if (fd == sock_f_out)
956 close_multiplexing_out();
957 rsyserr(FERROR, errno,
958 "writefd_unbuffered failed to write %ld bytes: phase \"%s\" [%s]",
959 (long)len, io_write_phase, who_am_i());
960 /* If the other side is sending us error messages, try
961 * to grab any messages they sent before they died. */
962 while (fd == sock_f_out && io_multiplexing_in) {
963 io_timeout = select_timeout = 30;
964 readfd_unbuffered(sock_f_in, io_filesfrom_buf,
965 sizeof io_filesfrom_buf);
967 exit_cleanup(RERR_STREAMIO);
972 if (fd == sock_f_out) {
974 last_io = time(NULL);
975 sleep_for_bwlimit(ret);
984 * Write an message to a multiplexed stream. If this fails then rsync
987 static void mplex_write(enum msgcode code, char *buf, size_t len)
992 SIVAL(buffer, 0, ((MPLEX_BASE + (int)code)<<24) + len);
994 if (n > sizeof buffer - 4)
995 n = sizeof buffer - 4;
997 memcpy(&buffer[4], buf, n);
998 writefd_unbuffered(sock_f_out, buffer, n+4);
1004 writefd_unbuffered(sock_f_out, buf, len);
1008 void io_flush(int flush_it_all)
1010 msg_list_push(flush_it_all);
1012 if (!iobuf_out_cnt || no_flush)
1015 if (io_multiplexing_out)
1016 mplex_write(MSG_DATA, iobuf_out, iobuf_out_cnt);
1018 writefd_unbuffered(sock_f_out, iobuf_out, iobuf_out_cnt);
1023 static void writefd(int fd,char *buf,size_t len)
1025 if (fd == msg_fd_out) {
1026 rprintf(FERROR, "Internal error: wrong write used in receiver.\n");
1027 exit_cleanup(RERR_PROTOCOL);
1030 if (fd == sock_f_out)
1031 stats.total_written += len;
1033 if (fd == write_batch_monitor_out) {
1034 if ((size_t)write(batch_fd, buf, len) != len)
1035 exit_cleanup(RERR_FILEIO);
1038 if (!iobuf_out || fd != sock_f_out) {
1039 writefd_unbuffered(fd, buf, len);
1044 int n = MIN((int)len, IO_BUFFER_SIZE - iobuf_out_cnt);
1046 memcpy(iobuf_out+iobuf_out_cnt, buf, n);
1052 if (iobuf_out_cnt == IO_BUFFER_SIZE)
1053 io_flush(NORMAL_FLUSH);
1058 void write_int(int f,int32 x)
1066 void write_int_named(int f, int32 x, const char *phase)
1068 io_write_phase = phase;
1070 io_write_phase = phase_unknown;
1075 * Note: int64 may actually be a 32-bit type if ./configure couldn't find any
1076 * 64-bit types on this platform.
1078 void write_longint(int f, int64 x)
1082 if (x <= 0x7FFFFFFF) {
1083 write_int(f, (int)x);
1087 #ifdef INT64_IS_OFF_T
1088 if (sizeof (int64) < 8) {
1089 rprintf(FERROR, "Integer overflow: attempted 64-bit offset\n");
1090 exit_cleanup(RERR_UNSUPPORTED);
1094 write_int(f, (int32)0xFFFFFFFF);
1095 SIVAL(b,0,(x&0xFFFFFFFF));
1096 SIVAL(b,4,((x>>32)&0xFFFFFFFF));
1101 void write_buf(int f,char *buf,size_t len)
1106 /** Write a string to the connection */
1107 void write_sbuf(int f, char *buf)
1109 writefd(f, buf, strlen(buf));
1112 void write_byte(int f,unsigned char c)
1114 writefd(f, (char *)&c, 1);
1120 * Read a line of up to @p maxlen characters into @p buf (not counting
1121 * the trailing null). Strips the (required) trailing newline and all
1124 * @return 1 for success; 0 for I/O error or truncation.
1126 int read_line(int f, char *buf, size_t maxlen)
1130 read_buf(f, buf, 1);
1135 if (buf[0] != '\r') {
1145 void io_printf(int fd, const char *format, ...)
1151 va_start(ap, format);
1152 len = vsnprintf(buf, sizeof buf, format, ap);
1156 exit_cleanup(RERR_STREAMIO);
1158 write_sbuf(fd, buf);
1162 /** Setup for multiplexing a MSG_* stream with the data stream. */
1163 void io_start_multiplex_out(void)
1165 io_flush(NORMAL_FLUSH);
1166 io_start_buffering_out();
1167 io_multiplexing_out = 1;
1170 /** Setup for multiplexing a MSG_* stream with the data stream. */
1171 void io_start_multiplex_in(void)
1173 io_flush(NORMAL_FLUSH);
1174 io_start_buffering_in();
1175 io_multiplexing_in = 1;
1178 /** Write an message to the multiplexed data stream. */
1179 int io_multiplex_write(enum msgcode code, char *buf, size_t len)
1181 if (!io_multiplexing_out)
1184 io_flush(NORMAL_FLUSH);
1185 stats.total_written += (len+4);
1186 mplex_write(code, buf, len);
1190 void close_multiplexing_in(void)
1192 io_multiplexing_in = 0;
1195 /** Stop output multiplexing. */
1196 void close_multiplexing_out(void)
1198 io_multiplexing_out = 0;
1201 void start_write_batch(int fd)
1203 write_stream_flags(batch_fd);
1205 /* Some communication has already taken place, but we don't
1206 * enable batch writing until here so that we can write a
1207 * canonical record of the communication even though the
1208 * actual communication so far depends on whether a daemon
1210 write_int(batch_fd, protocol_version);
1211 write_int(batch_fd, checksum_seed);
1214 write_batch_monitor_out = fd;
1216 write_batch_monitor_in = fd;
1219 void stop_write_batch(void)
1221 write_batch_monitor_out = -1;
1222 write_batch_monitor_in = -1;