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 checksum_seed;
52 extern int protocol_version;
53 extern char *remote_filesfrom_file;
54 extern struct stats stats;
56 const char phase_unknown[] = "unknown";
57 int select_timeout = SELECT_TIMEOUT;
59 int batch_gen_fd = -1;
62 * The connection might be dropped at some point; perhaps because the
63 * remote instance crashed. Just giving the offset on the stream is
64 * not very helpful. So instead we try to make io_phase_name point to
67 * For buffered/multiplexed I/O these names will be somewhat
68 * approximate; perhaps for ease of support we would rather make the
69 * buffer always flush when a single application-level I/O finishes.
71 * @todo Perhaps we want some simple stack functionality, but there's
72 * no need to overdo it.
74 const char *io_write_phase = phase_unknown;
75 const char *io_read_phase = phase_unknown;
77 /** Ignore EOF errors while reading a module listing if the remote
78 version is 24 or less. */
79 int kludge_around_eof = False;
84 static int io_multiplexing_out;
85 static int io_multiplexing_in;
86 static int sock_f_in = -1;
87 static int sock_f_out = -1;
88 static time_t last_io;
91 static int write_batch_monitor_in = -1;
92 static int write_batch_monitor_out = -1;
94 static int io_filesfrom_f_in = -1;
95 static int io_filesfrom_f_out = -1;
96 static char io_filesfrom_buf[2048];
97 static char *io_filesfrom_bp;
98 static char io_filesfrom_lastchar;
99 static int io_filesfrom_buflen;
101 static void read_loop(int fd, char *buf, size_t len);
104 struct redo_list *next;
108 static struct redo_list *redo_list_head;
109 static struct redo_list *redo_list_tail;
112 struct msg_list *next;
117 static struct msg_list *msg_list_head;
118 static struct msg_list *msg_list_tail;
120 static void redo_list_add(int num)
122 struct redo_list *rl;
124 if (!(rl = new(struct redo_list)))
125 exit_cleanup(RERR_MALLOC);
129 redo_list_tail->next = rl;
135 static void check_timeout(void)
143 last_io = time(NULL);
149 if (t - last_io >= io_timeout) {
150 if (!am_server && !am_daemon) {
151 rprintf(FERROR, "io timeout after %d seconds - exiting\n",
154 exit_cleanup(RERR_TIMEOUT);
158 /* Note the fds used for the main socket (which might really be a pipe
159 * for a local transfer, but we can ignore that). */
160 void io_set_sock_fds(int f_in, int f_out)
166 /* Setup the fd used to receive MSG_* messages. Only needed during the
167 * early stages of being a local sender (up through the sending of the
168 * file list) or when we're the generator (to fetch the messages from
170 void set_msg_fd_in(int fd)
175 /* Setup the fd used to send our MSG_* messages. Only needed when
176 * we're the receiver (to send our messages to the generator). */
177 void set_msg_fd_out(int fd)
180 set_nonblocking(msg_fd_out);
183 /* Add a message to the pending MSG_* list. */
184 static void msg_list_add(int code, char *buf, int len)
188 if (!(ml = new(struct msg_list)))
189 exit_cleanup(RERR_MALLOC);
191 if (!(ml->buf = new_array(char, len+4)))
192 exit_cleanup(RERR_MALLOC);
193 SIVAL(ml->buf, 0, ((code+MPLEX_BASE)<<24) | len);
194 memcpy(ml->buf+4, buf, len);
197 msg_list_tail->next = ml;
203 void send_msg(enum msgcode code, char *buf, int len)
205 msg_list_add(code, buf, len);
206 msg_list_push(NORMAL_FLUSH);
209 /* Read a message from the MSG_* fd and handle it. This is called either
210 * during the early stages of being a local sender (up through the sending
211 * of the file list) or when we're the generator (to fetch the messages
212 * from the receiver). */
213 static void read_msg_fd(void)
220 /* Temporarily disable msg_fd_in. This is needed to avoid looping back
221 * to this routine from read_timeout() and writefd_unbuffered(). */
224 read_loop(fd, buf, 4);
227 len = tag & 0xFFFFFF;
228 tag = (tag >> 24) - MPLEX_BASE;
232 if (len != 0 || !am_generator) {
233 rprintf(FERROR, "invalid message %d:%d\n", tag, len);
234 exit_cleanup(RERR_STREAMIO);
239 if (len != 4 || !am_generator) {
240 rprintf(FERROR, "invalid message %d:%d\n", tag, len);
241 exit_cleanup(RERR_STREAMIO);
243 read_loop(fd, buf, 4);
244 redo_list_add(IVAL(buf,0));
253 read_loop(fd, buf, n);
254 rwrite((enum logcode)tag, buf, n);
259 rprintf(FERROR, "unknown message %d:%d\n", tag, len);
260 exit_cleanup(RERR_STREAMIO);
266 /* Try to push messages off the list onto the wire. If we leave with more
267 * to do, return 0. On error, return -1. If everything flushed, return 1.
268 * This is only active in the receiver. */
269 int msg_list_push(int flush_it_all)
271 static int written = 0;
278 while (msg_list_head) {
279 struct msg_list *ml = msg_list_head;
280 int n = write(msg_fd_out, ml->buf + written, ml->len - written);
284 if (errno != EWOULDBLOCK && errno != EAGAIN)
289 FD_SET(msg_fd_out, &fds);
290 tv.tv_sec = select_timeout;
292 if (!select(msg_fd_out+1, NULL, &fds, NULL, &tv))
294 } else if ((written += n) == ml->len) {
296 msg_list_head = ml->next;
298 msg_list_tail = NULL;
306 int get_redo_num(void)
308 struct redo_list *next;
311 while (!redo_list_head)
314 num = redo_list_head->num;
315 next = redo_list_head->next;
316 free(redo_list_head);
317 redo_list_head = next;
319 redo_list_tail = NULL;
325 * When we're the receiver and we have a local --files-from list of names
326 * that needs to be sent over the socket to the sender, we have to do two
327 * things at the same time: send the sender a list of what files we're
328 * processing and read the incoming file+info list from the sender. We do
329 * this by augmenting the read_timeout() function to copy this data. It
330 * uses the io_filesfrom_buf to read a block of data from f_in (when it is
331 * ready, since it might be a pipe) and then blast it out f_out (when it
332 * is ready to receive more data).
334 void io_set_filesfrom_fds(int f_in, int f_out)
336 io_filesfrom_f_in = f_in;
337 io_filesfrom_f_out = f_out;
338 io_filesfrom_bp = io_filesfrom_buf;
339 io_filesfrom_lastchar = '\0';
340 io_filesfrom_buflen = 0;
344 * It's almost always an error to get an EOF when we're trying to read
345 * from the network, because the protocol is self-terminating.
347 * However, there is one unfortunate cases where it is not, which is
348 * rsync <2.4.6 sending a list of modules on a server, since the list
349 * is terminated by closing the socket. So, for the section of the
350 * program where that is a problem (start_socket_client),
351 * kludge_around_eof is True and we just exit.
353 static void whine_about_eof(int fd)
355 if (kludge_around_eof && fd == sock_f_in)
358 rprintf(FERROR, RSYNC_NAME ": connection unexpectedly closed "
359 "(%.0f bytes received so far) [%s]\n",
360 (double)stats.total_read, who_am_i());
362 exit_cleanup(RERR_STREAMIO);
367 * Read from a socket with I/O timeout. return the number of bytes
368 * read. If no bytes can be read then exit, never return a number <= 0.
370 * TODO: If the remote shell connection fails, then current versions
371 * actually report an "unexpected EOF" error here. Since it's a
372 * fairly common mistake to try to use rsh when ssh is required, we
373 * should trap that: if we fail to read any data at all, we should
374 * give a better explanation. We can tell whether the connection has
375 * started by looking e.g. at whether the remote version is known yet.
377 static int read_timeout(int fd, char *buf, size_t len)
381 io_flush(NORMAL_FLUSH);
384 /* until we manage to read *something* */
393 if (msg_fd_in >= 0) {
394 FD_SET(msg_fd_in, &r_fds);
395 if (msg_fd_in > maxfd)
397 } else if (msg_list_head) {
398 FD_SET(msg_fd_out, &w_fds);
399 if (msg_fd_out > maxfd)
402 if (io_filesfrom_f_out >= 0) {
404 if (io_filesfrom_buflen == 0) {
405 if (io_filesfrom_f_in >= 0) {
406 FD_SET(io_filesfrom_f_in, &r_fds);
407 new_fd = io_filesfrom_f_in;
409 io_filesfrom_f_out = -1;
413 FD_SET(io_filesfrom_f_out, &w_fds);
414 new_fd = io_filesfrom_f_out;
420 tv.tv_sec = select_timeout;
425 count = select(maxfd + 1, &r_fds, &w_fds, NULL, &tv);
429 exit_cleanup(RERR_SOCKETIO);
434 if (msg_fd_in >= 0 && FD_ISSET(msg_fd_in, &r_fds))
436 else if (msg_list_head && FD_ISSET(msg_fd_out, &w_fds))
437 msg_list_push(NORMAL_FLUSH);
439 if (io_filesfrom_f_out >= 0) {
440 if (io_filesfrom_buflen) {
441 if (FD_ISSET(io_filesfrom_f_out, &w_fds)) {
442 int l = write(io_filesfrom_f_out,
444 io_filesfrom_buflen);
446 if (!(io_filesfrom_buflen -= l))
447 io_filesfrom_bp = io_filesfrom_buf;
449 io_filesfrom_bp += l;
451 /* XXX should we complain? */
452 io_filesfrom_f_out = -1;
455 } else if (io_filesfrom_f_in >= 0) {
456 if (FD_ISSET(io_filesfrom_f_in, &r_fds)) {
457 int l = read(io_filesfrom_f_in,
459 sizeof io_filesfrom_buf);
461 /* Send end-of-file marker */
462 io_filesfrom_buf[0] = '\0';
463 io_filesfrom_buf[1] = '\0';
464 io_filesfrom_buflen = io_filesfrom_lastchar? 2 : 1;
465 io_filesfrom_f_in = -1;
468 char *s = io_filesfrom_buf + l;
469 /* Transform CR and/or LF into '\0' */
470 while (s-- > io_filesfrom_buf) {
471 if (*s == '\n' || *s == '\r')
475 if (!io_filesfrom_lastchar) {
476 /* Last buf ended with a '\0', so don't
477 * let this buf start with one. */
478 while (l && !*io_filesfrom_bp)
479 io_filesfrom_bp++, l--;
482 io_filesfrom_bp = io_filesfrom_buf;
484 char *f = io_filesfrom_bp;
487 /* Eliminate any multi-'\0' runs. */
489 if (!(*t++ = *f++)) {
490 while (f != eob && !*f)
494 io_filesfrom_lastchar = f[-1];
496 io_filesfrom_buflen = l;
502 if (!FD_ISSET(fd, &r_fds))
505 n = read(fd, buf, len);
509 whine_about_eof(fd); /* Doesn't return. */
510 if (errno == EINTR || errno == EWOULDBLOCK
514 /* Don't write errors on a dead socket. */
516 close_multiplexing_out();
517 rsyserr(FERROR, errno, "read error");
518 exit_cleanup(RERR_STREAMIO);
525 if (io_timeout && fd == sock_f_in)
526 last_io = time(NULL);
533 * Read a line into the "fname" buffer (which must be at least MAXPATHLEN
536 int read_filesfrom_line(int fd, char *fname)
538 char ch, *s, *eob = fname + MAXPATHLEN - 1;
540 int reading_remotely = remote_filesfrom_file != NULL;
541 int nulls = eol_nulls || reading_remotely;
546 cnt = read(fd, &ch, 1);
547 if (cnt < 0 && (errno == EWOULDBLOCK
548 || errno == EINTR || errno == EAGAIN)) {
553 tv.tv_sec = select_timeout;
555 if (!select(fd+1, &fds, NULL, NULL, &tv))
561 if (nulls? !ch : (ch == '\r' || ch == '\n')) {
562 /* Skip empty lines if reading locally. */
563 if (!reading_remotely && s == fname)
573 if (*fname == '#' || *fname == ';')
580 static char *iobuf_out;
581 static int iobuf_out_cnt;
583 void io_start_buffering_out(void)
587 if (!(iobuf_out = new_array(char, IO_BUFFER_SIZE)))
588 out_of_memory("io_start_buffering_out");
593 static char *iobuf_in;
594 static size_t iobuf_in_siz;
596 void io_start_buffering_in(void)
600 iobuf_in_siz = 2 * IO_BUFFER_SIZE;
601 if (!(iobuf_in = new_array(char, iobuf_in_siz)))
602 out_of_memory("io_start_buffering_in");
606 void io_end_buffering(void)
608 io_flush(NORMAL_FLUSH);
609 if (!io_multiplexing_out) {
617 * Continue trying to read len bytes - don't return until len has been
620 static void read_loop(int fd, char *buf, size_t len)
623 int n = read_timeout(fd, buf, len);
632 * Read from the file descriptor handling multiplexing - return number
635 * Never returns <= 0.
637 static int readfd_unbuffered(int fd, char *buf, size_t len)
639 static size_t remaining;
640 static size_t iobuf_in_ndx;
644 if (!iobuf_in || fd != sock_f_in)
645 return read_timeout(fd, buf, len);
647 if (!io_multiplexing_in && remaining == 0) {
648 remaining = read_timeout(fd, iobuf_in, iobuf_in_siz);
654 len = MIN(len, remaining);
655 memcpy(buf, iobuf_in + iobuf_in_ndx, len);
662 read_loop(fd, line, 4);
665 remaining = tag & 0xFFFFFF;
666 tag = (tag >> 24) - MPLEX_BASE;
670 if (remaining > iobuf_in_siz) {
671 if (!(iobuf_in = realloc_array(iobuf_in, char,
673 out_of_memory("readfd_unbuffered");
674 iobuf_in_siz = remaining;
676 read_loop(fd, iobuf_in, remaining);
681 if (remaining >= sizeof line) {
682 rprintf(FERROR, "multiplexing overflow %d:%ld\n\n",
683 tag, (long)remaining);
684 exit_cleanup(RERR_STREAMIO);
686 read_loop(fd, line, remaining);
687 rwrite((enum logcode)tag, line, remaining);
691 rprintf(FERROR, "unexpected tag %d\n", tag);
692 exit_cleanup(RERR_STREAMIO);
697 io_flush(NORMAL_FLUSH);
705 * Do a buffered read from @p fd. Don't return until all @p n bytes
706 * have been read. If all @p n can't be read then exit with an
709 static void readfd(int fd, char *buffer, size_t N)
715 ret = readfd_unbuffered(fd, buffer + total, N-total);
719 if (fd == write_batch_monitor_in) {
720 if ((size_t)write(batch_fd, buffer, total) != total)
721 exit_cleanup(RERR_FILEIO);
725 stats.total_read += total;
729 int32 read_int(int f)
736 if (ret == (int32)0xffffffff)
741 int64 read_longint(int f)
747 if ((int32)ret != (int32)0xffffffff)
750 #ifdef INT64_IS_OFF_T
751 if (sizeof (int64) < 8) {
752 rprintf(FERROR, "Integer overflow: attempted 64-bit offset\n");
753 exit_cleanup(RERR_UNSUPPORTED);
757 ret = IVAL(b,0) | (((int64)IVAL(b,4))<<32);
762 void read_buf(int f,char *buf,size_t len)
767 void read_sbuf(int f,char *buf,size_t len)
773 unsigned char read_byte(int f)
776 readfd(f, (char *)&c, 1);
782 * Sleep after writing to limit I/O bandwidth usage.
784 * @todo Rather than sleeping after each write, it might be better to
785 * use some kind of averaging. The current algorithm seems to always
786 * use a bit less bandwidth than specified, because it doesn't make up
787 * for slow periods. But arguably this is a feature. In addition, we
788 * ought to take the time used to write the data into account.
790 * During some phases of big transfers (file FOO is uptodate) this is
791 * called with a small bytes_written every time. As the kernel has to
792 * round small waits up to guarantee that we actually wait at least the
793 * requested number of microseconds, this can become grossly inaccurate.
794 * We therefore keep track of the bytes we've written over time and only
795 * sleep when the accumulated delay is at least 1 tenth of a second.
797 static void sleep_for_bwlimit(int bytes_written)
799 static struct timeval prior_tv;
800 static long total_written = 0;
801 struct timeval tv, start_tv;
802 long elapsed_usec, sleep_usec;
804 #define ONE_SEC 1000000L /* # of microseconds in a second */
809 total_written += bytes_written;
811 gettimeofday(&start_tv, NULL);
812 if (prior_tv.tv_sec) {
813 elapsed_usec = (start_tv.tv_sec - prior_tv.tv_sec) * ONE_SEC
814 + (start_tv.tv_usec - prior_tv.tv_usec);
815 total_written -= elapsed_usec * bwlimit / (ONE_SEC/1024);
816 if (total_written < 0)
820 sleep_usec = total_written * (ONE_SEC/1024) / bwlimit;
821 if (sleep_usec < ONE_SEC / 10) {
826 tv.tv_sec = sleep_usec / ONE_SEC;
827 tv.tv_usec = sleep_usec % ONE_SEC;
828 select(0, NULL, NULL, NULL, &tv);
830 gettimeofday(&prior_tv, NULL);
831 elapsed_usec = (prior_tv.tv_sec - start_tv.tv_sec) * ONE_SEC
832 + (prior_tv.tv_usec - start_tv.tv_usec);
833 total_written = (sleep_usec - elapsed_usec) * bwlimit / (ONE_SEC/1024);
837 /* Write len bytes to the file descriptor fd, looping as necessary to get
838 * the job done and also (in the generator) reading any data on msg_fd_in
839 * (to avoid deadlock).
841 * This function underlies the multiplexing system. The body of the
842 * application never calls this function directly. */
843 static void writefd_unbuffered(int fd,char *buf,size_t len)
847 int maxfd, count, ret;
852 while (total < len) {
857 if (msg_fd_in >= 0) {
859 FD_SET(msg_fd_in,&r_fds);
860 if (msg_fd_in > maxfd)
863 if (fd != sock_f_out && iobuf_out_cnt && no_flush == 1) {
864 FD_SET(sock_f_out, &w_fds);
865 if (sock_f_out > maxfd)
869 tv.tv_sec = select_timeout;
873 count = select(maxfd + 1, msg_fd_in >= 0 ? &r_fds : NULL,
877 if (count < 0 && errno == EBADF)
878 exit_cleanup(RERR_SOCKETIO);
883 if (msg_fd_in >= 0 && FD_ISSET(msg_fd_in, &r_fds))
886 if (!FD_ISSET(fd, &w_fds)) {
887 if (fd != sock_f_out && iobuf_out_cnt) {
889 io_flush(NORMAL_FLUSH);
896 if (bwlimit && n > bwlimit_writemax)
897 n = bwlimit_writemax;
898 ret = write(fd, buf + total, n);
904 if (errno == EWOULDBLOCK || errno == EAGAIN) {
910 /* Don't try to write errors back across the stream. */
911 if (fd == sock_f_out)
912 close_multiplexing_out();
913 rsyserr(FERROR, errno,
914 "writefd_unbuffered failed to write %ld bytes: phase \"%s\" [%s]",
915 (long)len, io_write_phase, who_am_i());
916 /* If the other side is sending us error messages, try
917 * to grab any messages they sent before they died. */
918 while (fd == sock_f_out && io_multiplexing_in) {
920 readfd_unbuffered(sock_f_in, io_filesfrom_buf,
921 sizeof io_filesfrom_buf);
923 exit_cleanup(RERR_STREAMIO);
928 if (fd == sock_f_out) {
930 last_io = time(NULL);
931 sleep_for_bwlimit(ret);
940 * Write an message to a multiplexed stream. If this fails then rsync
943 static void mplex_write(enum msgcode code, char *buf, size_t len)
948 SIVAL(buffer, 0, ((MPLEX_BASE + (int)code)<<24) + len);
950 if (n > sizeof buffer - 4)
951 n = sizeof buffer - 4;
953 memcpy(&buffer[4], buf, n);
954 writefd_unbuffered(sock_f_out, buffer, n+4);
960 writefd_unbuffered(sock_f_out, buf, len);
964 void io_flush(int flush_it_all)
966 msg_list_push(flush_it_all);
968 if (!iobuf_out_cnt || no_flush)
971 if (io_multiplexing_out)
972 mplex_write(MSG_DATA, iobuf_out, iobuf_out_cnt);
974 writefd_unbuffered(sock_f_out, iobuf_out, iobuf_out_cnt);
979 static void writefd(int fd,char *buf,size_t len)
981 if (fd == msg_fd_out) {
982 rprintf(FERROR, "Internal error: wrong write used in receiver.\n");
983 exit_cleanup(RERR_PROTOCOL);
986 if (fd == sock_f_out)
987 stats.total_written += len;
989 if (fd == write_batch_monitor_out) {
990 if ((size_t)write(batch_fd, buf, len) != len)
991 exit_cleanup(RERR_FILEIO);
994 if (!iobuf_out || fd != sock_f_out) {
995 writefd_unbuffered(fd, buf, len);
1000 int n = MIN((int)len, IO_BUFFER_SIZE - iobuf_out_cnt);
1002 memcpy(iobuf_out+iobuf_out_cnt, buf, n);
1008 if (iobuf_out_cnt == IO_BUFFER_SIZE)
1009 io_flush(NORMAL_FLUSH);
1014 void write_int(int f,int32 x)
1022 void write_int_named(int f, int32 x, const char *phase)
1024 io_write_phase = phase;
1026 io_write_phase = phase_unknown;
1031 * Note: int64 may actually be a 32-bit type if ./configure couldn't find any
1032 * 64-bit types on this platform.
1034 void write_longint(int f, int64 x)
1038 if (x <= 0x7FFFFFFF) {
1039 write_int(f, (int)x);
1043 #ifdef INT64_IS_OFF_T
1044 if (sizeof (int64) < 8) {
1045 rprintf(FERROR, "Integer overflow: attempted 64-bit offset\n");
1046 exit_cleanup(RERR_UNSUPPORTED);
1050 write_int(f, (int32)0xFFFFFFFF);
1051 SIVAL(b,0,(x&0xFFFFFFFF));
1052 SIVAL(b,4,((x>>32)&0xFFFFFFFF));
1057 void write_buf(int f,char *buf,size_t len)
1062 /** Write a string to the connection */
1063 void write_sbuf(int f, char *buf)
1065 writefd(f, buf, strlen(buf));
1068 void write_byte(int f,unsigned char c)
1070 writefd(f, (char *)&c, 1);
1076 * Read a line of up to @p maxlen characters into @p buf (not counting
1077 * the trailing null). Strips the (required) trailing newline and all
1080 * @return 1 for success; 0 for I/O error or truncation.
1082 int read_line(int f, char *buf, size_t maxlen)
1086 read_buf(f, buf, 1);
1091 if (buf[0] != '\r') {
1101 void io_printf(int fd, const char *format, ...)
1107 va_start(ap, format);
1108 len = vsnprintf(buf, sizeof buf, format, ap);
1112 exit_cleanup(RERR_STREAMIO);
1114 write_sbuf(fd, buf);
1118 /** Setup for multiplexing a MSG_* stream with the data stream. */
1119 void io_start_multiplex_out(void)
1121 io_flush(NORMAL_FLUSH);
1122 io_start_buffering_out();
1123 io_multiplexing_out = 1;
1126 /** Setup for multiplexing a MSG_* stream with the data stream. */
1127 void io_start_multiplex_in(void)
1129 io_flush(NORMAL_FLUSH);
1130 io_start_buffering_in();
1131 io_multiplexing_in = 1;
1134 /** Write an message to the multiplexed data stream. */
1135 int io_multiplex_write(enum msgcode code, char *buf, size_t len)
1137 if (!io_multiplexing_out)
1140 io_flush(NORMAL_FLUSH);
1141 stats.total_written += (len+4);
1142 mplex_write(code, buf, len);
1146 void close_multiplexing_in(void)
1148 io_multiplexing_in = 0;
1151 /** Stop output multiplexing. */
1152 void close_multiplexing_out(void)
1154 io_multiplexing_out = 0;
1157 void start_write_batch(int fd)
1159 write_stream_flags(batch_fd);
1161 /* Some communication has already taken place, but we don't
1162 * enable batch writing until here so that we can write a
1163 * canonical record of the communication even though the
1164 * actual communication so far depends on whether a daemon
1166 write_int(batch_fd, protocol_version);
1167 write_int(batch_fd, checksum_seed);
1170 write_batch_monitor_out = fd;
1172 write_batch_monitor_in = fd;
1175 void stop_write_batch(void)
1177 write_batch_monitor_out = -1;
1178 write_batch_monitor_in = -1;