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;
50 extern int checksum_seed;
51 extern int protocol_version;
52 extern char *remote_filesfrom_file;
53 extern struct stats stats;
55 const char phase_unknown[] = "unknown";
56 int select_timeout = SELECT_TIMEOUT;
58 int batch_gen_fd = -1;
61 * The connection might be dropped at some point; perhaps because the
62 * remote instance crashed. Just giving the offset on the stream is
63 * not very helpful. So instead we try to make io_phase_name point to
66 * For buffered/multiplexed I/O these names will be somewhat
67 * approximate; perhaps for ease of support we would rather make the
68 * buffer always flush when a single application-level I/O finishes.
70 * @todo Perhaps we want some simple stack functionality, but there's
71 * no need to overdo it.
73 const char *io_write_phase = phase_unknown;
74 const char *io_read_phase = phase_unknown;
76 /** Ignore EOF errors while reading a module listing if the remote
77 version is 24 or less. */
78 int kludge_around_eof = False;
83 static int io_multiplexing_out;
84 static int io_multiplexing_in;
85 static int sock_f_in = -1;
86 static int sock_f_out = -1;
87 static time_t last_io;
90 static int write_batch_monitor_in = -1;
91 static int write_batch_monitor_out = -1;
93 static int io_filesfrom_f_in = -1;
94 static int io_filesfrom_f_out = -1;
95 static char io_filesfrom_buf[2048];
96 static char *io_filesfrom_bp;
97 static char io_filesfrom_lastchar;
98 static int io_filesfrom_buflen;
100 static void read_loop(int fd, char *buf, size_t len);
103 struct redo_list *next;
107 static struct redo_list *redo_list_head;
108 static struct redo_list *redo_list_tail;
111 struct msg_list *next;
116 static struct msg_list *msg_list_head;
117 static struct msg_list *msg_list_tail;
119 static void redo_list_add(int num)
121 struct redo_list *rl;
123 if (!(rl = new(struct redo_list)))
124 exit_cleanup(RERR_MALLOC);
128 redo_list_tail->next = rl;
134 static void check_timeout(void)
142 last_io = time(NULL);
148 if (t - last_io >= io_timeout) {
149 if (!am_server && !am_daemon) {
150 rprintf(FERROR, "io timeout after %d seconds - exiting\n",
153 exit_cleanup(RERR_TIMEOUT);
157 /* Note the fds used for the main socket (which might really be a pipe
158 * for a local transfer, but we can ignore that). */
159 void io_set_sock_fds(int f_in, int f_out)
165 /** Setup the fd used to receive MSG_* messages. Only needed when
166 * we're the generator because the sender and receiver both use the
167 * multiplexed I/O setup. */
168 void set_msg_fd_in(int fd)
173 /** Setup the fd used to send our MSG_* messages. Only needed when
174 * we're the receiver because the generator and the sender both use
175 * the multiplexed I/O setup. */
176 void set_msg_fd_out(int fd)
179 set_nonblocking(msg_fd_out);
182 /* Add a message to the pending MSG_* list. */
183 static void msg_list_add(int code, char *buf, int len)
187 if (!(ml = new(struct msg_list)))
188 exit_cleanup(RERR_MALLOC);
190 if (!(ml->buf = new_array(char, len+4)))
191 exit_cleanup(RERR_MALLOC);
192 SIVAL(ml->buf, 0, ((code+MPLEX_BASE)<<24) | len);
193 memcpy(ml->buf+4, buf, len);
196 msg_list_tail->next = ml;
202 void send_msg(enum msgcode code, char *buf, int len)
204 msg_list_add(code, buf, len);
205 msg_list_push(NORMAL_FLUSH);
208 /** Read a message from the MSG_* fd and dispatch it. This is only
209 * called by the generator. */
210 static void read_msg_fd(void)
217 /* Temporarily disable msg_fd_in. This is needed to avoid looping back
218 * to this routine from read_timeout() and writefd_unbuffered(). */
221 read_loop(fd, buf, 4);
224 len = tag & 0xFFFFFF;
225 tag = (tag >> 24) - MPLEX_BASE;
230 rprintf(FERROR, "invalid message %d:%d\n", tag, len);
231 exit_cleanup(RERR_STREAMIO);
237 rprintf(FERROR, "invalid message %d:%d\n", tag, len);
238 exit_cleanup(RERR_STREAMIO);
240 read_loop(fd, buf, 4);
241 redo_list_add(IVAL(buf,0));
250 read_loop(fd, buf, n);
251 rwrite((enum logcode)tag, buf, n);
256 rprintf(FERROR, "unknown message %d:%d\n", tag, len);
257 exit_cleanup(RERR_STREAMIO);
263 /* Try to push messages off the list onto the wire. If we leave with more
264 * to do, return 0. On error, return -1. If everything flushed, return 1.
265 * This is only active in the receiver. */
266 int msg_list_push(int flush_it_all)
268 static int written = 0;
275 while (msg_list_head) {
276 struct msg_list *ml = msg_list_head;
277 int n = write(msg_fd_out, ml->buf + written, ml->len - written);
281 if (errno != EWOULDBLOCK && errno != EAGAIN)
286 FD_SET(msg_fd_out, &fds);
287 tv.tv_sec = select_timeout;
289 if (!select(msg_fd_out+1, NULL, &fds, NULL, &tv))
291 } else if ((written += n) == ml->len) {
293 msg_list_head = ml->next;
295 msg_list_tail = NULL;
303 int get_redo_num(void)
305 struct redo_list *next;
308 while (!redo_list_head)
311 num = redo_list_head->num;
312 next = redo_list_head->next;
313 free(redo_list_head);
314 redo_list_head = next;
316 redo_list_tail = NULL;
322 * When we're the receiver and we have a local --files-from list of names
323 * that needs to be sent over the socket to the sender, we have to do two
324 * things at the same time: send the sender a list of what files we're
325 * processing and read the incoming file+info list from the sender. We do
326 * this by augmenting the read_timeout() function to copy this data. It
327 * uses the io_filesfrom_buf to read a block of data from f_in (when it is
328 * ready, since it might be a pipe) and then blast it out f_out (when it
329 * is ready to receive more data).
331 void io_set_filesfrom_fds(int f_in, int f_out)
333 io_filesfrom_f_in = f_in;
334 io_filesfrom_f_out = f_out;
335 io_filesfrom_bp = io_filesfrom_buf;
336 io_filesfrom_lastchar = '\0';
337 io_filesfrom_buflen = 0;
341 * It's almost always an error to get an EOF when we're trying to read
342 * from the network, because the protocol is self-terminating.
344 * However, there is one unfortunate cases where it is not, which is
345 * rsync <2.4.6 sending a list of modules on a server, since the list
346 * is terminated by closing the socket. So, for the section of the
347 * program where that is a problem (start_socket_client),
348 * kludge_around_eof is True and we just exit.
350 static void whine_about_eof(int fd)
352 if (kludge_around_eof && fd == sock_f_in)
355 rprintf(FERROR, RSYNC_NAME ": connection unexpectedly closed "
356 "(%.0f bytes received so far) [%s]\n",
357 (double)stats.total_read, who_am_i());
359 exit_cleanup(RERR_STREAMIO);
364 * Read from a socket with I/O timeout. return the number of bytes
365 * read. If no bytes can be read then exit, never return a number <= 0.
367 * TODO: If the remote shell connection fails, then current versions
368 * actually report an "unexpected EOF" error here. Since it's a
369 * fairly common mistake to try to use rsh when ssh is required, we
370 * should trap that: if we fail to read any data at all, we should
371 * give a better explanation. We can tell whether the connection has
372 * started by looking e.g. at whether the remote version is known yet.
374 static int read_timeout(int fd, char *buf, size_t len)
378 io_flush(NORMAL_FLUSH);
381 /* until we manage to read *something* */
390 if (msg_fd_in >= 0) {
391 FD_SET(msg_fd_in, &r_fds);
392 if (msg_fd_in > maxfd)
394 } else if (msg_list_head) {
395 FD_SET(msg_fd_out, &w_fds);
396 if (msg_fd_out > maxfd)
399 if (io_filesfrom_f_out >= 0) {
401 if (io_filesfrom_buflen == 0) {
402 if (io_filesfrom_f_in >= 0) {
403 FD_SET(io_filesfrom_f_in, &r_fds);
404 new_fd = io_filesfrom_f_in;
406 io_filesfrom_f_out = -1;
410 FD_SET(io_filesfrom_f_out, &w_fds);
411 new_fd = io_filesfrom_f_out;
417 tv.tv_sec = select_timeout;
422 count = select(maxfd + 1, &r_fds, &w_fds, NULL, &tv);
426 exit_cleanup(RERR_SOCKETIO);
431 if (msg_fd_in >= 0 && FD_ISSET(msg_fd_in, &r_fds))
433 else if (msg_list_head && FD_ISSET(msg_fd_out, &w_fds))
434 msg_list_push(NORMAL_FLUSH);
436 if (io_filesfrom_f_out >= 0) {
437 if (io_filesfrom_buflen) {
438 if (FD_ISSET(io_filesfrom_f_out, &w_fds)) {
439 int l = write(io_filesfrom_f_out,
441 io_filesfrom_buflen);
443 if (!(io_filesfrom_buflen -= l))
444 io_filesfrom_bp = io_filesfrom_buf;
446 io_filesfrom_bp += l;
448 /* XXX should we complain? */
449 io_filesfrom_f_out = -1;
452 } else if (io_filesfrom_f_in >= 0) {
453 if (FD_ISSET(io_filesfrom_f_in, &r_fds)) {
454 int l = read(io_filesfrom_f_in,
456 sizeof io_filesfrom_buf);
458 /* Send end-of-file marker */
459 io_filesfrom_buf[0] = '\0';
460 io_filesfrom_buf[1] = '\0';
461 io_filesfrom_buflen = io_filesfrom_lastchar? 2 : 1;
462 io_filesfrom_f_in = -1;
465 char *s = io_filesfrom_buf + l;
466 /* Transform CR and/or LF into '\0' */
467 while (s-- > io_filesfrom_buf) {
468 if (*s == '\n' || *s == '\r')
472 if (!io_filesfrom_lastchar) {
473 /* Last buf ended with a '\0', so don't
474 * let this buf start with one. */
475 while (l && !*io_filesfrom_bp)
476 io_filesfrom_bp++, l--;
479 io_filesfrom_bp = io_filesfrom_buf;
481 char *f = io_filesfrom_bp;
484 /* Eliminate any multi-'\0' runs. */
486 if (!(*t++ = *f++)) {
487 while (f != eob && !*f)
491 io_filesfrom_lastchar = f[-1];
493 io_filesfrom_buflen = l;
499 if (!FD_ISSET(fd, &r_fds))
502 n = read(fd, buf, len);
506 whine_about_eof(fd); /* Doesn't return. */
507 if (errno == EINTR || errno == EWOULDBLOCK
511 /* Don't write errors on a dead socket. */
513 close_multiplexing_out();
514 rsyserr(FERROR, errno, "read error");
515 exit_cleanup(RERR_STREAMIO);
522 if (io_timeout && fd == sock_f_in)
523 last_io = time(NULL);
530 * Read a line into the "fname" buffer (which must be at least MAXPATHLEN
533 int read_filesfrom_line(int fd, char *fname)
535 char ch, *s, *eob = fname + MAXPATHLEN - 1;
537 int reading_remotely = remote_filesfrom_file != NULL;
538 int nulls = eol_nulls || reading_remotely;
543 cnt = read(fd, &ch, 1);
544 if (cnt < 0 && (errno == EWOULDBLOCK
545 || errno == EINTR || errno == EAGAIN)) {
550 tv.tv_sec = select_timeout;
552 if (!select(fd+1, &fds, NULL, NULL, &tv))
558 if (nulls? !ch : (ch == '\r' || ch == '\n')) {
559 /* Skip empty lines if reading locally. */
560 if (!reading_remotely && s == fname)
570 if (*fname == '#' || *fname == ';')
577 static char *iobuf_out;
578 static int iobuf_out_cnt;
580 void io_start_buffering_out(void)
584 if (!(iobuf_out = new_array(char, IO_BUFFER_SIZE)))
585 out_of_memory("io_start_buffering_out");
590 static char *iobuf_in;
591 static size_t iobuf_in_siz;
593 void io_start_buffering_in(void)
597 iobuf_in_siz = 2 * IO_BUFFER_SIZE;
598 if (!(iobuf_in = new_array(char, iobuf_in_siz)))
599 out_of_memory("io_start_buffering_in");
603 void io_end_buffering(void)
605 io_flush(NORMAL_FLUSH);
606 if (!io_multiplexing_out) {
614 * Continue trying to read len bytes - don't return until len has been
617 static void read_loop(int fd, char *buf, size_t len)
620 int n = read_timeout(fd, buf, len);
629 * Read from the file descriptor handling multiplexing - return number
632 * Never returns <= 0.
634 static int readfd_unbuffered(int fd, char *buf, size_t len)
636 static size_t remaining;
637 static size_t iobuf_in_ndx;
641 if (!iobuf_in || fd != sock_f_in)
642 return read_timeout(fd, buf, len);
644 if (!io_multiplexing_in && remaining == 0) {
645 remaining = read_timeout(fd, iobuf_in, iobuf_in_siz);
651 len = MIN(len, remaining);
652 memcpy(buf, iobuf_in + iobuf_in_ndx, len);
659 read_loop(fd, line, 4);
662 remaining = tag & 0xFFFFFF;
663 tag = (tag >> 24) - MPLEX_BASE;
667 if (remaining > iobuf_in_siz) {
668 if (!(iobuf_in = realloc_array(iobuf_in, char,
670 out_of_memory("readfd_unbuffered");
671 iobuf_in_siz = remaining;
673 read_loop(fd, iobuf_in, remaining);
678 if (remaining >= sizeof line) {
679 rprintf(FERROR, "multiplexing overflow %d:%ld\n\n",
680 tag, (long)remaining);
681 exit_cleanup(RERR_STREAMIO);
683 read_loop(fd, line, remaining);
684 rwrite((enum logcode)tag, line, remaining);
688 rprintf(FERROR, "unexpected tag %d\n", tag);
689 exit_cleanup(RERR_STREAMIO);
694 io_flush(NORMAL_FLUSH);
702 * Do a buffered read from @p fd. Don't return until all @p n bytes
703 * have been read. If all @p n can't be read then exit with an
706 static void readfd(int fd, char *buffer, size_t N)
712 ret = readfd_unbuffered(fd, buffer + total, N-total);
716 if (fd == write_batch_monitor_in) {
717 if ((size_t)write(batch_fd, buffer, total) != total)
718 exit_cleanup(RERR_FILEIO);
722 stats.total_read += total;
726 int32 read_int(int f)
733 if (ret == (int32)0xffffffff)
738 int64 read_longint(int f)
744 if ((int32)ret != (int32)0xffffffff)
747 #ifdef INT64_IS_OFF_T
748 if (sizeof (int64) < 8) {
749 rprintf(FERROR, "Integer overflow: attempted 64-bit offset\n");
750 exit_cleanup(RERR_UNSUPPORTED);
754 ret = IVAL(b,0) | (((int64)IVAL(b,4))<<32);
759 void read_buf(int f,char *buf,size_t len)
764 void read_sbuf(int f,char *buf,size_t len)
770 unsigned char read_byte(int f)
773 readfd(f, (char *)&c, 1);
779 * Sleep after writing to limit I/O bandwidth usage.
781 * @todo Rather than sleeping after each write, it might be better to
782 * use some kind of averaging. The current algorithm seems to always
783 * use a bit less bandwidth than specified, because it doesn't make up
784 * for slow periods. But arguably this is a feature. In addition, we
785 * ought to take the time used to write the data into account.
787 * During some phases of big transfers (file FOO is uptodate) this is
788 * called with a small bytes_written every time. As the kernel has to
789 * round small waits up to guarantee that we actually wait at least the
790 * requested number of microseconds, this can become grossly inaccurate.
791 * We therefore keep track of the bytes we've written over time and only
792 * sleep when the accumulated delay is at least 1 tenth of a second.
794 static void sleep_for_bwlimit(int bytes_written)
796 static struct timeval prior_tv;
797 static long total_written = 0;
798 struct timeval tv, start_tv;
799 long elapsed_usec, sleep_usec;
801 #define ONE_SEC 1000000L /* # of microseconds in a second */
806 total_written += bytes_written;
808 gettimeofday(&start_tv, NULL);
809 if (prior_tv.tv_sec) {
810 elapsed_usec = (start_tv.tv_sec - prior_tv.tv_sec) * ONE_SEC
811 + (start_tv.tv_usec - prior_tv.tv_usec);
812 total_written -= elapsed_usec * bwlimit / (ONE_SEC/1024);
813 if (total_written < 0)
817 sleep_usec = total_written * (ONE_SEC/1024) / bwlimit;
818 if (sleep_usec < ONE_SEC / 10) {
823 tv.tv_sec = sleep_usec / ONE_SEC;
824 tv.tv_usec = sleep_usec % ONE_SEC;
825 select(0, NULL, NULL, NULL, &tv);
827 gettimeofday(&prior_tv, NULL);
828 elapsed_usec = (prior_tv.tv_sec - start_tv.tv_sec) * ONE_SEC
829 + (prior_tv.tv_usec - start_tv.tv_usec);
830 total_written = (sleep_usec - elapsed_usec) * bwlimit / (ONE_SEC/1024);
834 /* Write len bytes to the file descriptor fd, looping as necessary to get
835 * the job done and also (in the generator) reading any data on msg_fd_in
836 * (to avoid deadlock).
838 * This function underlies the multiplexing system. The body of the
839 * application never calls this function directly. */
840 static void writefd_unbuffered(int fd,char *buf,size_t len)
844 int maxfd, count, ret;
849 while (total < len) {
854 if (msg_fd_in >= 0) {
856 FD_SET(msg_fd_in,&r_fds);
857 if (msg_fd_in > maxfd)
860 if (fd != sock_f_out && iobuf_out_cnt && no_flush == 1) {
861 FD_SET(sock_f_out, &w_fds);
862 if (sock_f_out > maxfd)
866 tv.tv_sec = select_timeout;
870 count = select(maxfd + 1, msg_fd_in >= 0 ? &r_fds : NULL,
874 if (count < 0 && errno == EBADF)
875 exit_cleanup(RERR_SOCKETIO);
880 if (msg_fd_in >= 0 && FD_ISSET(msg_fd_in, &r_fds))
883 if (!FD_ISSET(fd, &w_fds)) {
884 if (fd != sock_f_out && iobuf_out_cnt) {
886 io_flush(NORMAL_FLUSH);
893 if (bwlimit && n > bwlimit_writemax)
894 n = bwlimit_writemax;
895 ret = write(fd, buf + total, n);
901 if (errno == EWOULDBLOCK || errno == EAGAIN) {
907 /* Don't try to write errors back across the stream. */
908 if (fd == sock_f_out)
909 close_multiplexing_out();
910 rsyserr(FERROR, errno,
911 "writefd_unbuffered failed to write %ld bytes: phase \"%s\" [%s]",
912 (long)len, io_write_phase, who_am_i());
913 /* If the other side is sending us error messages, try
914 * to grab any messages they sent before they died. */
915 while (fd == sock_f_out && io_multiplexing_in) {
917 readfd_unbuffered(sock_f_in, io_filesfrom_buf,
918 sizeof io_filesfrom_buf);
920 exit_cleanup(RERR_STREAMIO);
925 if (fd == sock_f_out) {
927 last_io = time(NULL);
928 sleep_for_bwlimit(ret);
937 * Write an message to a multiplexed stream. If this fails then rsync
940 static void mplex_write(enum msgcode code, char *buf, size_t len)
945 SIVAL(buffer, 0, ((MPLEX_BASE + (int)code)<<24) + len);
947 if (n > sizeof buffer - 4)
948 n = sizeof buffer - 4;
950 memcpy(&buffer[4], buf, n);
951 writefd_unbuffered(sock_f_out, buffer, n+4);
957 writefd_unbuffered(sock_f_out, buf, len);
961 void io_flush(int flush_it_all)
963 msg_list_push(flush_it_all);
965 if (!iobuf_out_cnt || no_flush)
968 if (io_multiplexing_out)
969 mplex_write(MSG_DATA, iobuf_out, iobuf_out_cnt);
971 writefd_unbuffered(sock_f_out, iobuf_out, iobuf_out_cnt);
976 static void writefd(int fd,char *buf,size_t len)
978 if (fd == msg_fd_out) {
979 rprintf(FERROR, "Internal error: wrong write used in receiver.\n");
980 exit_cleanup(RERR_PROTOCOL);
983 if (fd == sock_f_out)
984 stats.total_written += len;
986 if (fd == write_batch_monitor_out) {
987 if ((size_t)write(batch_fd, buf, len) != len)
988 exit_cleanup(RERR_FILEIO);
991 if (!iobuf_out || fd != sock_f_out) {
992 writefd_unbuffered(fd, buf, len);
997 int n = MIN((int)len, IO_BUFFER_SIZE - iobuf_out_cnt);
999 memcpy(iobuf_out+iobuf_out_cnt, buf, n);
1005 if (iobuf_out_cnt == IO_BUFFER_SIZE)
1006 io_flush(NORMAL_FLUSH);
1011 void write_int(int f,int32 x)
1019 void write_int_named(int f, int32 x, const char *phase)
1021 io_write_phase = phase;
1023 io_write_phase = phase_unknown;
1028 * Note: int64 may actually be a 32-bit type if ./configure couldn't find any
1029 * 64-bit types on this platform.
1031 void write_longint(int f, int64 x)
1035 if (x <= 0x7FFFFFFF) {
1036 write_int(f, (int)x);
1040 #ifdef INT64_IS_OFF_T
1041 if (sizeof (int64) < 8) {
1042 rprintf(FERROR, "Integer overflow: attempted 64-bit offset\n");
1043 exit_cleanup(RERR_UNSUPPORTED);
1047 write_int(f, (int32)0xFFFFFFFF);
1048 SIVAL(b,0,(x&0xFFFFFFFF));
1049 SIVAL(b,4,((x>>32)&0xFFFFFFFF));
1054 void write_buf(int f,char *buf,size_t len)
1059 /** Write a string to the connection */
1060 void write_sbuf(int f, char *buf)
1062 writefd(f, buf, strlen(buf));
1065 void write_byte(int f,unsigned char c)
1067 writefd(f, (char *)&c, 1);
1073 * Read a line of up to @p maxlen characters into @p buf (not counting
1074 * the trailing null). Strips the (required) trailing newline and all
1077 * @return 1 for success; 0 for I/O error or truncation.
1079 int read_line(int f, char *buf, size_t maxlen)
1083 read_buf(f, buf, 1);
1088 if (buf[0] != '\r') {
1098 void io_printf(int fd, const char *format, ...)
1104 va_start(ap, format);
1105 len = vsnprintf(buf, sizeof buf, format, ap);
1109 exit_cleanup(RERR_STREAMIO);
1111 write_sbuf(fd, buf);
1115 /** Setup for multiplexing a MSG_* stream with the data stream. */
1116 void io_start_multiplex_out(void)
1118 io_flush(NORMAL_FLUSH);
1119 io_start_buffering_out();
1120 io_multiplexing_out = 1;
1123 /** Setup for multiplexing a MSG_* stream with the data stream. */
1124 void io_start_multiplex_in(void)
1126 io_flush(NORMAL_FLUSH);
1127 io_start_buffering_in();
1128 io_multiplexing_in = 1;
1131 /** Write an message to the multiplexed data stream. */
1132 int io_multiplex_write(enum msgcode code, char *buf, size_t len)
1134 if (!io_multiplexing_out)
1137 io_flush(NORMAL_FLUSH);
1138 stats.total_written += (len+4);
1139 mplex_write(code, buf, len);
1143 void close_multiplexing_in(void)
1145 io_multiplexing_in = 0;
1148 /** Stop output multiplexing. */
1149 void close_multiplexing_out(void)
1151 io_multiplexing_out = 0;
1154 void start_write_batch(int fd)
1156 write_stream_flags(batch_fd);
1158 /* Some communication has already taken place, but we don't
1159 * enable batch writing until here so that we can write a
1160 * canonical record of the communication even though the
1161 * actual communication so far depends on whether a daemon
1163 write_int(batch_fd, protocol_version);
1164 write_int(batch_fd, checksum_seed);
1167 write_batch_monitor_out = fd;
1169 write_batch_monitor_in = fd;
1172 void stop_write_batch(void)
1174 write_batch_monitor_out = -1;
1175 write_batch_monitor_in = -1;