Added reminder to update rsync.yo manpage when adding to
[rsync/rsync.git] / io.c
... / ...
CommitLineData
1/* -*- c-file-style: "linux" -*-
2 *
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>
6 *
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.
11 *
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.
16 *
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 */
21
22/**
23 * @file io.c
24 *
25 * Socket and pipe IO utilities used in rsync.
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 **/
36
37#include "rsync.h"
38
39/** If no timeout is specified then use a 60 second select timeout */
40#define SELECT_TIMEOUT 60
41
42static int io_multiplexing_out;
43static int io_multiplexing_in;
44static int multiplex_in_fd;
45static int multiplex_out_fd;
46static time_t last_io;
47static int no_flush;
48
49extern int bwlimit;
50extern int verbose;
51extern int io_timeout;
52extern struct stats stats;
53
54
55const char phase_unknown[] = "unknown";
56
57/**
58 * The connection might be dropped at some point; perhaps because the
59 * remote instance crashed. Just giving the offset on the stream is
60 * not very helpful. So instead we try to make io_phase_name point to
61 * something useful.
62 *
63 * For buffered/multiplexed IO these names will be somewhat
64 * approximate; perhaps for ease of support we would rather make the
65 * buffer always flush when a single application-level IO finishes.
66 *
67 * @todo Perhaps we want some simple stack functionality, but there's
68 * no need to overdo it.
69 **/
70const char *io_write_phase = phase_unknown;
71const char *io_read_phase = phase_unknown;
72
73/** Ignore EOF errors while reading a module listing if the remote
74 version is 24 or less. */
75int kludge_around_eof = False;
76
77
78static int io_error_fd = -1;
79static int io_filesfrom_f_in = -1;
80static int io_filesfrom_f_out = -1;
81static char io_filesfrom_buf[2048];
82static char *io_filesfrom_bp;
83static char io_filesfrom_lastchar;
84static int io_filesfrom_buflen;
85
86static void read_loop(int fd, char *buf, size_t len);
87
88static void check_timeout(void)
89{
90 extern int am_server, am_daemon;
91 time_t t;
92
93 err_list_push();
94
95 if (!io_timeout) return;
96
97 if (!last_io) {
98 last_io = time(NULL);
99 return;
100 }
101
102 t = time(NULL);
103
104 if (last_io && io_timeout && (t-last_io) >= io_timeout) {
105 if (!am_server && !am_daemon) {
106 rprintf(FERROR,"io timeout after %d seconds - exiting\n",
107 (int)(t-last_io));
108 }
109 exit_cleanup(RERR_TIMEOUT);
110 }
111}
112
113/** Setup the fd used to propagate errors */
114void io_set_error_fd(int fd)
115{
116 io_error_fd = fd;
117}
118
119/** Read some data from the error fd and write it to the write log code */
120static void read_error_fd(void)
121{
122 char buf[200];
123 size_t n;
124 int fd = io_error_fd;
125 int tag, len;
126
127 /* io_error_fd is temporarily disabled -- is this meant to
128 * prevent indefinite recursion? */
129 io_error_fd = -1;
130
131 read_loop(fd, buf, 4);
132 tag = IVAL(buf, 0);
133
134 len = tag & 0xFFFFFF;
135 tag = tag >> 24;
136 tag -= MPLEX_BASE;
137
138 while (len) {
139 n = len;
140 if (n > (sizeof buf - 1))
141 n = sizeof buf - 1;
142 read_loop(fd, buf, n);
143 rwrite((enum logcode)tag, buf, n);
144 len -= n;
145 }
146
147 io_error_fd = fd;
148}
149
150/**
151 * When we're the receiver and we have a local --files-from list of names
152 * that needs to be sent over the socket to the sender, we have to do two
153 * things at the same time: send the sender a list of what files we're
154 * processing and read the incoming file+info list from the sender. We do
155 * this by augmenting the read_timeout() function to copy this data. It
156 * uses the io_filesfrom_buf to read a block of data from f_in (when it is
157 * ready, since it might be a pipe) and then blast it out f_out (when it
158 * is ready to receive more data).
159 */
160void io_set_filesfrom_fds(int f_in, int f_out)
161{
162 io_filesfrom_f_in = f_in;
163 io_filesfrom_f_out = f_out;
164 io_filesfrom_bp = io_filesfrom_buf;
165 io_filesfrom_lastchar = '\0';
166 io_filesfrom_buflen = 0;
167}
168
169/**
170 * It's almost always an error to get an EOF when we're trying to read
171 * from the network, because the protocol is self-terminating.
172 *
173 * However, there is one unfortunate cases where it is not, which is
174 * rsync <2.4.6 sending a list of modules on a server, since the list
175 * is terminated by closing the socket. So, for the section of the
176 * program where that is a problem (start_socket_client),
177 * kludge_around_eof is True and we just exit.
178 */
179static void whine_about_eof(void)
180{
181 if (kludge_around_eof)
182 exit_cleanup(0);
183 else {
184 rprintf(FERROR,
185 "%s: connection unexpectedly closed "
186 "(%.0f bytes read so far)\n",
187 RSYNC_NAME, (double)stats.total_read);
188
189 exit_cleanup(RERR_STREAMIO);
190 }
191}
192
193
194static void die_from_readerr(int err)
195{
196 /* this prevents us trying to write errors on a dead socket */
197 io_multiplexing_close();
198
199 rprintf(FERROR, "%s: read error: %s\n",
200 RSYNC_NAME, strerror(err));
201 exit_cleanup(RERR_STREAMIO);
202}
203
204
205/**
206 * Read from a socket with IO timeout. return the number of bytes
207 * read. If no bytes can be read then exit, never return a number <= 0.
208 *
209 * TODO: If the remote shell connection fails, then current versions
210 * actually report an "unexpected EOF" error here. Since it's a
211 * fairly common mistake to try to use rsh when ssh is required, we
212 * should trap that: if we fail to read any data at all, we should
213 * give a better explanation. We can tell whether the connection has
214 * started by looking e.g. at whether the remote version is known yet.
215 */
216static int read_timeout(int fd, char *buf, size_t len)
217{
218 int n, ret=0;
219
220 io_flush();
221
222 while (ret == 0) {
223 /* until we manage to read *something* */
224 fd_set r_fds, w_fds;
225 struct timeval tv;
226 int fd_count = fd+1;
227 int count;
228
229 FD_ZERO(&r_fds);
230 FD_SET(fd, &r_fds);
231 if (io_error_fd != -1) {
232 FD_SET(io_error_fd, &r_fds);
233 if (io_error_fd >= fd_count) fd_count = io_error_fd+1;
234 }
235 if (io_filesfrom_f_out != -1) {
236 int new_fd;
237 if (io_filesfrom_buflen == 0) {
238 if (io_filesfrom_f_in != -1) {
239 FD_SET(io_filesfrom_f_in, &r_fds);
240 new_fd = io_filesfrom_f_in;
241 } else {
242 io_filesfrom_f_out = -1;
243 new_fd = -1;
244 }
245 } else {
246 FD_ZERO(&w_fds);
247 FD_SET(io_filesfrom_f_out, &w_fds);
248 new_fd = io_filesfrom_f_out;
249 }
250 if (new_fd >= fd_count) fd_count = new_fd+1;
251 }
252
253 tv.tv_sec = io_timeout?io_timeout:SELECT_TIMEOUT;
254 tv.tv_usec = 0;
255
256 errno = 0;
257
258 count = select(fd_count, &r_fds,
259 io_filesfrom_buflen? &w_fds : NULL,
260 NULL, &tv);
261
262 if (count == 0) {
263 check_timeout();
264 }
265
266 if (count <= 0) {
267 if (errno == EBADF) {
268 exit_cleanup(RERR_SOCKETIO);
269 }
270 continue;
271 }
272
273
274 if (io_error_fd != -1 && FD_ISSET(io_error_fd, &r_fds)) {
275 read_error_fd();
276 }
277
278 if (io_filesfrom_f_out != -1) {
279 if (io_filesfrom_buflen) {
280 if (FD_ISSET(io_filesfrom_f_out, &w_fds)) {
281 int l = write(io_filesfrom_f_out,
282 io_filesfrom_bp,
283 io_filesfrom_buflen);
284 if (l > 0) {
285 if (!(io_filesfrom_buflen -= l))
286 io_filesfrom_bp = io_filesfrom_buf;
287 else
288 io_filesfrom_bp += l;
289 } else {
290 /* XXX should we complain? */
291 io_filesfrom_f_out = -1;
292 }
293 }
294 } else if (io_filesfrom_f_in != -1) {
295 if (FD_ISSET(io_filesfrom_f_in, &r_fds)) {
296 int l = read(io_filesfrom_f_in,
297 io_filesfrom_buf,
298 sizeof io_filesfrom_buf);
299 if (l <= 0) {
300 /* Send end-of-file marker */
301 io_filesfrom_buf[0] = '\0';
302 io_filesfrom_buf[1] = '\0';
303 io_filesfrom_buflen = io_filesfrom_lastchar? 2 : 1;
304 io_filesfrom_f_in = -1;
305 } else {
306 extern int eol_nulls;
307 if (!eol_nulls) {
308 char *s = io_filesfrom_buf + l;
309 /* Transform CR and/or LF into '\0' */
310 while (s-- > io_filesfrom_buf) {
311 if (*s == '\n' || *s == '\r')
312 *s = '\0';
313 }
314 }
315 if (!io_filesfrom_lastchar) {
316 /* Last buf ended with a '\0', so don't
317 * let this buf start with one. */
318 while (l && !*io_filesfrom_bp)
319 io_filesfrom_bp++, l--;
320 }
321 if (!l)
322 io_filesfrom_bp = io_filesfrom_buf;
323 else {
324 char *f = io_filesfrom_bp;
325 char *t = f;
326 char *eob = f + l;
327 /* Eliminate any multi-'\0' runs. */
328 while (f != eob) {
329 if (!(*t++ = *f++)) {
330 while (f != eob && !*f)
331 f++, l--;
332 }
333 }
334 io_filesfrom_lastchar = f[-1];
335 }
336 io_filesfrom_buflen = l;
337 }
338 }
339 }
340 }
341
342 if (!FD_ISSET(fd, &r_fds)) continue;
343
344 n = read(fd, buf, len);
345
346 if (n > 0) {
347 buf += n;
348 len -= n;
349 ret += n;
350 if (io_timeout)
351 last_io = time(NULL);
352 continue;
353 } else if (n == 0) {
354 whine_about_eof();
355 return -1; /* doesn't return */
356 } else if (n == -1) {
357 if (errno == EINTR || errno == EWOULDBLOCK ||
358 errno == EAGAIN)
359 continue;
360 die_from_readerr(errno);
361 }
362 }
363
364 return ret;
365}
366
367/**
368 * Read a line into the "fname" buffer (which must be at least MAXPATHLEN
369 * characters long).
370 */
371int read_filesfrom_line(int fd, char *fname)
372{
373 char ch, *s, *eob = fname + MAXPATHLEN - 1;
374 int cnt;
375 extern int io_timeout;
376 extern int eol_nulls;
377 extern char *remote_filesfrom_file;
378 extern int am_server;
379 int reading_remotely = remote_filesfrom_file || (am_server && fd == 0);
380 int nulls = eol_nulls || reading_remotely;
381
382 start:
383 s = fname;
384 while (1) {
385 cnt = read(fd, &ch, 1);
386 if (cnt < 0 && (errno == EWOULDBLOCK
387 || errno == EINTR || errno == EAGAIN)) {
388 struct timeval tv;
389 fd_set fds;
390 FD_ZERO(&fds);
391 FD_SET(fd, &fds);
392 tv.tv_sec = io_timeout? io_timeout : SELECT_TIMEOUT;
393 tv.tv_usec = 0;
394 if (!select(fd+1, &fds, NULL, NULL, &tv))
395 check_timeout();
396 continue;
397 }
398 if (cnt != 1)
399 break;
400 if (nulls? !ch : (ch == '\r' || ch == '\n')) {
401 /* Skip empty lines if reading locally. */
402 if (!reading_remotely && s == fname)
403 continue;
404 break;
405 }
406 if (s < eob)
407 *s++ = ch;
408 }
409 *s = '\0';
410
411 /* Dump comments. */
412 if (*fname == '#' || *fname == ';')
413 goto start;
414
415 return s - fname;
416}
417
418
419/**
420 * Continue trying to read len bytes - don't return until len has been
421 * read.
422 **/
423static void read_loop(int fd, char *buf, size_t len)
424{
425 while (len) {
426 int n = read_timeout(fd, buf, len);
427
428 buf += n;
429 len -= n;
430 }
431}
432
433
434/**
435 * Read from the file descriptor handling multiplexing - return number
436 * of bytes read.
437 *
438 * Never returns <= 0.
439 */
440static int read_unbuffered(int fd, char *buf, size_t len)
441{
442 static size_t remaining;
443 int tag, ret = 0;
444 char line[1024];
445
446 if (!io_multiplexing_in || fd != multiplex_in_fd)
447 return read_timeout(fd, buf, len);
448
449 while (ret == 0) {
450 if (remaining) {
451 len = MIN(len, remaining);
452 read_loop(fd, buf, len);
453 remaining -= len;
454 ret = len;
455 continue;
456 }
457
458 read_loop(fd, line, 4);
459 tag = IVAL(line, 0);
460
461 remaining = tag & 0xFFFFFF;
462 tag = tag >> 24;
463
464 if (tag == MPLEX_BASE)
465 continue;
466
467 tag -= MPLEX_BASE;
468
469 if (tag != FERROR && tag != FINFO) {
470 rprintf(FERROR, "unexpected tag %d\n", tag);
471 exit_cleanup(RERR_STREAMIO);
472 }
473
474 if (remaining > sizeof line - 1) {
475 rprintf(FERROR, "multiplexing overflow %ld\n\n",
476 (long)remaining);
477 exit_cleanup(RERR_STREAMIO);
478 }
479
480 read_loop(fd, line, remaining);
481 line[remaining] = 0;
482
483 rprintf((enum logcode) tag, "%s", line);
484 remaining = 0;
485 }
486
487 return ret;
488}
489
490
491
492/**
493 * Do a buffered read from @p fd. Don't return until all @p n bytes
494 * have been read. If all @p n can't be read then exit with an
495 * error.
496 **/
497static void readfd(int fd, char *buffer, size_t N)
498{
499 int ret;
500 size_t total=0;
501
502 while (total < N) {
503 io_flush();
504
505 ret = read_unbuffered(fd, buffer + total, N-total);
506 total += ret;
507 }
508
509 stats.total_read += total;
510}
511
512
513int32 read_int(int f)
514{
515 char b[4];
516 int32 ret;
517
518 readfd(f,b,4);
519 ret = IVAL(b,0);
520 if (ret == (int32)0xffffffff) return -1;
521 return ret;
522}
523
524int64 read_longint(int f)
525{
526 int64 ret;
527 char b[8];
528 ret = read_int(f);
529
530 if ((int32)ret != (int32)0xffffffff) {
531 return ret;
532 }
533
534#ifdef NO_INT64
535 rprintf(FERROR,"Integer overflow - attempted 64 bit offset\n");
536 exit_cleanup(RERR_UNSUPPORTED);
537#else
538 readfd(f,b,8);
539 ret = IVAL(b,0) | (((int64)IVAL(b,4))<<32);
540#endif
541
542 return ret;
543}
544
545void read_buf(int f,char *buf,size_t len)
546{
547 readfd(f,buf,len);
548}
549
550void read_sbuf(int f,char *buf,size_t len)
551{
552 read_buf(f,buf,len);
553 buf[len] = 0;
554}
555
556unsigned char read_byte(int f)
557{
558 unsigned char c;
559 read_buf(f, (char *)&c, 1);
560 return c;
561}
562
563
564/**
565 * Sleep after writing to limit I/O bandwidth usage.
566 *
567 * @todo Rather than sleeping after each write, it might be better to
568 * use some kind of averaging. The current algorithm seems to always
569 * use a bit less bandwidth than specified, because it doesn't make up
570 * for slow periods. But arguably this is a feature. In addition, we
571 * ought to take the time used to write the data into account.
572 **/
573static void sleep_for_bwlimit(int bytes_written)
574{
575 struct timeval tv;
576
577 if (!bwlimit)
578 return;
579
580 assert(bytes_written > 0);
581 assert(bwlimit > 0);
582
583 tv.tv_usec = bytes_written * 1000 / bwlimit;
584 tv.tv_sec = tv.tv_usec / 1000000;
585 tv.tv_usec = tv.tv_usec % 1000000;
586
587 select(0, NULL, NULL, NULL, &tv);
588}
589
590
591/**
592 * Write len bytes to the file descriptor @p fd.
593 *
594 * This function underlies the multiplexing system. The body of the
595 * application never calls this function directly.
596 **/
597static void writefd_unbuffered(int fd,char *buf,size_t len)
598{
599 size_t total = 0;
600 fd_set w_fds, r_fds;
601 int fd_count, count;
602 struct timeval tv;
603
604 err_list_push();
605
606 no_flush++;
607
608 while (total < len) {
609 FD_ZERO(&w_fds);
610 FD_SET(fd,&w_fds);
611 fd_count = fd;
612
613 if (io_error_fd != -1) {
614 FD_ZERO(&r_fds);
615 FD_SET(io_error_fd,&r_fds);
616 if (io_error_fd > fd_count)
617 fd_count = io_error_fd;
618 }
619
620 tv.tv_sec = io_timeout?io_timeout:SELECT_TIMEOUT;
621 tv.tv_usec = 0;
622
623 errno = 0;
624
625 count = select(fd_count+1,
626 io_error_fd != -1?&r_fds:NULL,
627 &w_fds,NULL,
628 &tv);
629
630 if (count == 0) {
631 check_timeout();
632 }
633
634 if (count <= 0) {
635 if (errno == EBADF) {
636 exit_cleanup(RERR_SOCKETIO);
637 }
638 continue;
639 }
640
641 if (io_error_fd != -1 && FD_ISSET(io_error_fd, &r_fds)) {
642 read_error_fd();
643 }
644
645 if (FD_ISSET(fd, &w_fds)) {
646 int ret;
647 size_t n = len-total;
648 ret = write(fd,buf+total,n);
649
650 if (ret == -1 && errno == EINTR) {
651 continue;
652 }
653
654 if (ret == -1 &&
655 (errno == EWOULDBLOCK || errno == EAGAIN)) {
656 msleep(1);
657 continue;
658 }
659
660 if (ret <= 0) {
661 /* Don't try to write errors back
662 * across the stream */
663 io_multiplexing_close();
664 rprintf(FERROR, RSYNC_NAME
665 ": writefd_unbuffered failed to write %ld bytes: phase \"%s\": %s\n",
666 (long) len, io_write_phase,
667 strerror(errno));
668 exit_cleanup(RERR_STREAMIO);
669 }
670
671 sleep_for_bwlimit(ret);
672
673 total += ret;
674
675 if (io_timeout)
676 last_io = time(NULL);
677 }
678 }
679
680 no_flush--;
681}
682
683
684static char *io_buffer;
685static int io_buffer_count;
686
687void io_start_buffering(int fd)
688{
689 if (io_buffer) return;
690 multiplex_out_fd = fd;
691 io_buffer = (char *)malloc(IO_BUFFER_SIZE);
692 if (!io_buffer) out_of_memory("writefd");
693 io_buffer_count = 0;
694}
695
696/**
697 * Write an message to a multiplexed stream. If this fails then rsync
698 * exits.
699 **/
700static void mplex_write(int fd, enum logcode code, char *buf, size_t len)
701{
702 char buffer[4096];
703 size_t n = len;
704
705 SIVAL(buffer, 0, ((MPLEX_BASE + (int)code)<<24) + len);
706
707 if (n > (sizeof buffer - 4)) {
708 n = sizeof buffer - 4;
709 }
710
711 memcpy(&buffer[4], buf, n);
712 writefd_unbuffered(fd, buffer, n+4);
713
714 len -= n;
715 buf += n;
716
717 if (len) {
718 writefd_unbuffered(fd, buf, len);
719 }
720}
721
722
723void io_flush(void)
724{
725 int fd = multiplex_out_fd;
726
727 err_list_push();
728
729 if (!io_buffer_count || no_flush) return;
730
731 if (io_multiplexing_out) {
732 mplex_write(fd, FNONE, io_buffer, io_buffer_count);
733 } else {
734 writefd_unbuffered(fd, io_buffer, io_buffer_count);
735 }
736 io_buffer_count = 0;
737}
738
739
740void io_end_buffering(void)
741{
742 io_flush();
743 if (!io_multiplexing_out) {
744 free(io_buffer);
745 io_buffer = NULL;
746 }
747}
748
749static void writefd(int fd,char *buf,size_t len)
750{
751 stats.total_written += len;
752
753 err_list_push();
754
755 if (!io_buffer || fd != multiplex_out_fd) {
756 writefd_unbuffered(fd, buf, len);
757 return;
758 }
759
760 while (len) {
761 int n = MIN((int) len, IO_BUFFER_SIZE-io_buffer_count);
762 if (n > 0) {
763 memcpy(io_buffer+io_buffer_count, buf, n);
764 buf += n;
765 len -= n;
766 io_buffer_count += n;
767 }
768
769 if (io_buffer_count == IO_BUFFER_SIZE) io_flush();
770 }
771}
772
773
774void write_int(int f,int32 x)
775{
776 char b[4];
777 SIVAL(b,0,x);
778 writefd(f,b,4);
779}
780
781
782void write_int_named(int f, int32 x, const char *phase)
783{
784 io_write_phase = phase;
785 write_int(f, x);
786 io_write_phase = phase_unknown;
787}
788
789
790/*
791 * Note: int64 may actually be a 32-bit type if ./configure couldn't find any
792 * 64-bit types on this platform.
793 */
794void write_longint(int f, int64 x)
795{
796 char b[8];
797
798 if (x <= 0x7FFFFFFF) {
799 write_int(f, (int)x);
800 return;
801 }
802
803#ifdef NO_INT64
804 rprintf(FERROR,"Integer overflow - attempted 64 bit offset\n");
805 exit_cleanup(RERR_UNSUPPORTED);
806#else
807 write_int(f, (int32)0xFFFFFFFF);
808 SIVAL(b,0,(x&0xFFFFFFFF));
809 SIVAL(b,4,((x>>32)&0xFFFFFFFF));
810
811 writefd(f,b,8);
812#endif
813}
814
815void write_buf(int f,char *buf,size_t len)
816{
817 writefd(f,buf,len);
818}
819
820/** Write a string to the connection */
821static void write_sbuf(int f,char *buf)
822{
823 write_buf(f, buf, strlen(buf));
824}
825
826
827void write_byte(int f,unsigned char c)
828{
829 write_buf(f,(char *)&c,1);
830}
831
832
833
834/**
835 * Read a line of up to @p maxlen characters into @p buf. Does not
836 * contain a trailing newline or carriage return.
837 *
838 * @return 1 for success; 0 for io error or truncation.
839 **/
840int read_line(int f, char *buf, size_t maxlen)
841{
842 while (maxlen) {
843 buf[0] = 0;
844 read_buf(f, buf, 1);
845 if (buf[0] == 0)
846 return 0;
847 if (buf[0] == '\n') {
848 buf[0] = 0;
849 break;
850 }
851 if (buf[0] != '\r') {
852 buf++;
853 maxlen--;
854 }
855 }
856 if (maxlen == 0) {
857 *buf = 0;
858 return 0;
859 }
860
861 return 1;
862}
863
864
865void io_printf(int fd, const char *format, ...)
866{
867 va_list ap;
868 char buf[1024];
869 int len;
870
871 va_start(ap, format);
872 len = vsnprintf(buf, sizeof buf, format, ap);
873 va_end(ap);
874
875 if (len < 0) exit_cleanup(RERR_STREAMIO);
876
877 write_sbuf(fd, buf);
878}
879
880
881/** Setup for multiplexing an error stream with the data stream */
882void io_start_multiplex_out(int fd)
883{
884 multiplex_out_fd = fd;
885 io_flush();
886 io_start_buffering(fd);
887 io_multiplexing_out = 1;
888}
889
890/** Setup for multiplexing an error stream with the data stream */
891void io_start_multiplex_in(int fd)
892{
893 multiplex_in_fd = fd;
894 io_flush();
895 io_multiplexing_in = 1;
896}
897
898/** Write an message to the multiplexed error stream */
899int io_multiplex_write(enum logcode code, char *buf, size_t len)
900{
901 if (!io_multiplexing_out) return 0;
902
903 io_flush();
904 stats.total_written += (len+4);
905 mplex_write(multiplex_out_fd, code, buf, len);
906 return 1;
907}
908
909/** Stop output multiplexing */
910void io_multiplexing_close(void)
911{
912 io_multiplexing_out = 0;
913}
914