Add RERR_OK == 0
[rsync/rsync.git] / io.c
CommitLineData
7a24c346 1/* -*- c-file-style: "linux" -*-
880da007
MP
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 */
720b47f2 21
87ee2481 22/**
87ee2481
MP
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 **/
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
8d9dc9f9
AT
42static int io_multiplexing_out;
43static int io_multiplexing_in;
679e7657
AT
44static int multiplex_in_fd;
45static int multiplex_out_fd;
8d9dc9f9 46static time_t last_io;
7a55d06e
MP
47static int no_flush;
48
49extern int bwlimit;
720b47f2 50extern int verbose;
6ba9279f 51extern int io_timeout;
a800434a 52extern struct stats stats;
720b47f2 53
7a55d06e 54
805edf9d
MP
55const char * const phase_unknown = "unknown";
56
98b332ed
MP
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.
eca2adb4 62 *
805edf9d
MP
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 *
eca2adb4
MP
67 * @todo Perhaps we want some simple stack functionality, but there's
68 * no need to overdo it.
98b332ed 69 **/
805edf9d
MP
70const char *io_write_phase = phase_unknown;
71const char *io_read_phase = phase_unknown;
98b332ed 72
7a55d06e
MP
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
554e0a8d 78static int io_error_fd = -1;
720b47f2 79
9dd891bb 80static void read_loop(int fd, char *buf, size_t len);
ff41a59f 81
8d9dc9f9
AT
82static void check_timeout(void)
83{
0adb99b9 84 extern int am_server, am_daemon;
8d9dc9f9 85 time_t t;
90ba34e2
AT
86
87 err_list_push();
8d9dc9f9
AT
88
89 if (!io_timeout) return;
90
91 if (!last_io) {
92 last_io = time(NULL);
93 return;
94 }
95
96 t = time(NULL);
97
86ffe37f 98 if (last_io && io_timeout && (t-last_io) >= io_timeout) {
0adb99b9 99 if (!am_server && !am_daemon) {
ce6c7c63 100 rprintf(FERROR,"io timeout after %d seconds - exiting\n",
0adb99b9
AT
101 (int)(t-last_io));
102 }
65417579 103 exit_cleanup(RERR_TIMEOUT);
8d9dc9f9
AT
104 }
105}
106
880da007 107/** Setup the fd used to propogate errors */
554e0a8d
AT
108void io_set_error_fd(int fd)
109{
110 io_error_fd = fd;
111}
112
880da007 113/** Read some data from the error fd and write it to the write log code */
554e0a8d
AT
114static void read_error_fd(void)
115{
116 char buf[200];
06ce139f 117 size_t n;
554e0a8d 118 int fd = io_error_fd;
ff41a59f
AT
119 int tag, len;
120
8886f8d0
MP
121 /* io_error_fd is temporarily disabled -- is this meant to
122 * prevent indefinite recursion? */
554e0a8d
AT
123 io_error_fd = -1;
124
ff41a59f
AT
125 read_loop(fd, buf, 4);
126 tag = IVAL(buf, 0);
127
128 len = tag & 0xFFFFFF;
129 tag = tag >> 24;
130 tag -= MPLEX_BASE;
131
132 while (len) {
133 n = len;
06ce139f
MP
134 if (n > (sizeof(buf)-1))
135 n = sizeof(buf)-1;
ff41a59f
AT
136 read_loop(fd, buf, n);
137 rwrite((enum logcode)tag, buf, n);
138 len -= n;
554e0a8d
AT
139 }
140
141 io_error_fd = fd;
142}
143
720b47f2 144
880da007
MP
145/**
146 * It's almost always an error to get an EOF when we're trying to read
147 * from the network, because the protocol is self-terminating.
148 *
149 * However, there is one unfortunate cases where it is not, which is
150 * rsync <2.4.6 sending a list of modules on a server, since the list
151 * is terminated by closing the socket. So, for the section of the
152 * program where that is a problem (start_socket_client),
153 * kludge_around_eof is True and we just exit.
154 */
7a55d06e
MP
155static void whine_about_eof (void)
156{
7a55d06e
MP
157 if (kludge_around_eof)
158 exit_cleanup (0);
159 else {
160 rprintf (FERROR,
161 "%s: connection unexpectedly closed "
162 "(%.0f bytes read so far)\n",
163 RSYNC_NAME, (double)stats.total_read);
164
165 exit_cleanup (RERR_STREAMIO);
166 }
167}
720b47f2 168
7a55d06e
MP
169
170static void die_from_readerr (int err)
171{
172 /* this prevents us trying to write errors on a dead socket */
173 io_multiplexing_close();
174
175 rprintf(FERROR, "%s: read error: %s\n",
176 RSYNC_NAME, strerror (err));
177 exit_cleanup(RERR_STREAMIO);
178}
179
180
880da007 181/**
c3563c46
MP
182 * Read from a socket with IO timeout. return the number of bytes
183 * read. If no bytes can be read then exit, never return a number <= 0.
184 *
8886f8d0
MP
185 * TODO: If the remote shell connection fails, then current versions
186 * actually report an "unexpected EOF" error here. Since it's a
187 * fairly common mistake to try to use rsh when ssh is required, we
188 * should trap that: if we fail to read any data at all, we should
189 * give a better explanation. We can tell whether the connection has
190 * started by looking e.g. at whether the remote version is known yet.
c3563c46 191 */
9dd891bb 192static int read_timeout (int fd, char *buf, size_t len)
8d9dc9f9 193{
4c36ddbe
AT
194 int n, ret=0;
195
ea2111d1
AT
196 io_flush();
197
4c36ddbe 198 while (ret == 0) {
7a55d06e 199 /* until we manage to read *something* */
4c36ddbe
AT
200 fd_set fds;
201 struct timeval tv;
554e0a8d 202 int fd_count = fd+1;
a57873b7 203 int count;
4c36ddbe
AT
204
205 FD_ZERO(&fds);
206 FD_SET(fd, &fds);
554e0a8d
AT
207 if (io_error_fd != -1) {
208 FD_SET(io_error_fd, &fds);
209 if (io_error_fd > fd) fd_count = io_error_fd+1;
210 }
211
8cd9fd4e 212 tv.tv_sec = io_timeout?io_timeout:SELECT_TIMEOUT;
4c36ddbe
AT
213 tv.tv_usec = 0;
214
554e0a8d
AT
215 errno = 0;
216
a57873b7
AT
217 count = select(fd_count, &fds, NULL, NULL, &tv);
218
219 if (count == 0) {
220 check_timeout();
221 }
222
223 if (count <= 0) {
554e0a8d
AT
224 if (errno == EBADF) {
225 exit_cleanup(RERR_SOCKETIO);
226 }
4c36ddbe
AT
227 continue;
228 }
229
554e0a8d
AT
230 if (io_error_fd != -1 && FD_ISSET(io_error_fd, &fds)) {
231 read_error_fd();
232 }
233
234 if (!FD_ISSET(fd, &fds)) continue;
235
4c36ddbe
AT
236 n = read(fd, buf, len);
237
8d9dc9f9
AT
238 if (n > 0) {
239 buf += n;
240 len -= n;
4c36ddbe
AT
241 ret += n;
242 if (io_timeout)
243 last_io = time(NULL);
244 continue;
7a55d06e
MP
245 } else if (n == 0) {
246 whine_about_eof ();
247 return -1; /* doesn't return */
248 } else if (n == -1) {
249 if (errno == EINTR || errno == EWOULDBLOCK ||
250 errno == EAGAIN)
251 continue;
252 else
253 die_from_readerr (errno);
8d9dc9f9 254 }
4c36ddbe 255 }
8d9dc9f9 256
4c36ddbe
AT
257 return ret;
258}
8d9dc9f9 259
7a55d06e
MP
260
261
262
880da007
MP
263/**
264 * Continue trying to read len bytes - don't return until len has been
265 * read.
266 **/
9dd891bb 267static void read_loop (int fd, char *buf, size_t len)
4c36ddbe
AT
268{
269 while (len) {
270 int n = read_timeout(fd, buf, len);
271
272 buf += n;
273 len -= n;
8d9dc9f9
AT
274 }
275}
276
7a55d06e
MP
277
278/**
279 * Read from the file descriptor handling multiplexing - return number
280 * of bytes read.
281 *
282 * Never returns <= 0.
283 */
9dd891bb 284static int read_unbuffered(int fd, char *buf, size_t len)
8d9dc9f9 285{
6fe25398 286 static size_t remaining;
909ce14f 287 int tag, ret = 0;
8d9dc9f9
AT
288 char line[1024];
289
7a55d06e 290 if (!io_multiplexing_in || fd != multiplex_in_fd)
4c36ddbe 291 return read_timeout(fd, buf, len);
8d9dc9f9
AT
292
293 while (ret == 0) {
294 if (remaining) {
295 len = MIN(len, remaining);
296 read_loop(fd, buf, len);
297 remaining -= len;
298 ret = len;
299 continue;
300 }
301
909ce14f 302 read_loop(fd, line, 4);
ff41a59f 303 tag = IVAL(line, 0);
679e7657 304
8d9dc9f9
AT
305 remaining = tag & 0xFFFFFF;
306 tag = tag >> 24;
307
909ce14f
MP
308 if (tag == MPLEX_BASE)
309 continue;
8d9dc9f9
AT
310
311 tag -= MPLEX_BASE;
312
313 if (tag != FERROR && tag != FINFO) {
909ce14f 314 rprintf(FERROR, "unexpected tag %d\n", tag);
65417579 315 exit_cleanup(RERR_STREAMIO);
8d9dc9f9
AT
316 }
317
909ce14f
MP
318 if (remaining > sizeof(line) - 1) {
319 rprintf(FERROR, "multiplexing overflow %d\n\n",
8d9dc9f9 320 remaining);
65417579 321 exit_cleanup(RERR_STREAMIO);
8d9dc9f9
AT
322 }
323
324 read_loop(fd, line, remaining);
325 line[remaining] = 0;
326
909ce14f 327 rprintf((enum logcode) tag, "%s", line);
8d9dc9f9
AT
328 remaining = 0;
329 }
330
331 return ret;
332}
333
334
909ce14f 335
880da007
MP
336/**
337 * Do a buffered read from @p fd. Don't return until all @p n bytes
338 * have been read. If all @p n can't be read then exit with an
339 * error.
340 **/
9dd891bb 341static void readfd (int fd, char *buffer, size_t N)
720b47f2 342{
6ba9279f 343 int ret;
06ce139f 344 size_t total=0;
6ba9279f 345
6ba9279f 346 while (total < N) {
8d9dc9f9
AT
347 io_flush();
348
7a55d06e 349 ret = read_unbuffered (fd, buffer + total, N-total);
6ba9279f 350 total += ret;
7f28dbee 351 }
1b7c47cb
AT
352
353 stats.total_read += total;
720b47f2
AT
354}
355
356
b7922338 357int32 read_int(int f)
720b47f2 358{
4c36ddbe 359 char b[4];
d730b113
AT
360 int32 ret;
361
4c36ddbe 362 readfd(f,b,4);
d730b113
AT
363 ret = IVAL(b,0);
364 if (ret == (int32)0xffffffff) return -1;
365 return ret;
720b47f2
AT
366}
367
71c46176 368int64 read_longint(int f)
3a6a366f
AT
369{
370 extern int remote_version;
71c46176 371 int64 ret;
3a6a366f
AT
372 char b[8];
373 ret = read_int(f);
71c46176 374
8de330a3
AT
375 if ((int32)ret != (int32)0xffffffff) {
376 return ret;
377 }
71c46176 378
3bee6733 379#ifdef NO_INT64
9486289c 380 rprintf(FERROR,"Integer overflow - attempted 64 bit offset\n");
65417579 381 exit_cleanup(RERR_UNSUPPORTED);
71c46176
AT
382#else
383 if (remote_version >= 16) {
4c36ddbe 384 readfd(f,b,8);
71c46176 385 ret = IVAL(b,0) | (((int64)IVAL(b,4))<<32);
3a6a366f 386 }
71c46176
AT
387#endif
388
3a6a366f
AT
389 return ret;
390}
391
9dd891bb 392void read_buf(int f,char *buf,size_t len)
720b47f2 393{
4c36ddbe 394 readfd(f,buf,len);
720b47f2
AT
395}
396
9dd891bb 397void read_sbuf(int f,char *buf,size_t len)
575f2fca 398{
7a55d06e 399 read_buf (f,buf,len);
575f2fca
AT
400 buf[len] = 0;
401}
402
182dca5c
AT
403unsigned char read_byte(int f)
404{
4c36ddbe 405 unsigned char c;
7a55d06e 406 read_buf (f, (char *)&c, 1);
4c36ddbe 407 return c;
182dca5c 408}
720b47f2 409
880da007 410
08571358
MP
411/**
412 * Sleep after writing to limit I/O bandwidth usage.
413 *
414 * @todo Rather than sleeping after each write, it might be better to
415 * use some kind of averaging. The current algorithm seems to always
416 * use a bit less bandwidth than specified, because it doesn't make up
417 * for slow periods. But arguably this is a feature. In addition, we
418 * ought to take the time used to write the data into account.
419 **/
420static void sleep_for_bwlimit(int bytes_written)
421{
422 struct timeval tv;
423
424 if (!bwlimit)
425 return;
e681e820
MP
426
427 assert(bytes_written > 0);
428 assert(bwlimit > 0);
08571358 429
08571358 430 tv.tv_usec = bytes_written * 1000 / bwlimit;
e681e820
MP
431 tv.tv_sec = tv.tv_usec / 1000000;
432 tv.tv_usec = tv.tv_usec % 1000000;
08571358 433
98b332ed 434 select(0, NULL, NULL, NULL, &tv);
08571358
MP
435}
436
437
880da007
MP
438/**
439 * Write len bytes to the file descriptor @p fd.
440 *
441 * This function underlies the multiplexing system. The body of the
442 * application never calls this function directly.
443 **/
9dd891bb 444static void writefd_unbuffered(int fd,char *buf,size_t len)
720b47f2 445{
06ce139f 446 size_t total = 0;
8d9dc9f9 447 fd_set w_fds, r_fds;
4c36ddbe 448 int fd_count, count;
8d9dc9f9 449 struct timeval tv;
720b47f2 450
90ba34e2
AT
451 err_list_push();
452
e44f9a12
AT
453 no_flush++;
454
4c36ddbe 455 while (total < len) {
8d9dc9f9
AT
456 FD_ZERO(&w_fds);
457 FD_ZERO(&r_fds);
458 FD_SET(fd,&w_fds);
554e0a8d 459 fd_count = fd;
4c36ddbe 460
554e0a8d
AT
461 if (io_error_fd != -1) {
462 FD_SET(io_error_fd,&r_fds);
463 if (io_error_fd > fd_count)
464 fd_count = io_error_fd;
8d9dc9f9
AT
465 }
466
8cd9fd4e 467 tv.tv_sec = io_timeout?io_timeout:SELECT_TIMEOUT;
8d9dc9f9 468 tv.tv_usec = 0;
4c36ddbe 469
554e0a8d
AT
470 errno = 0;
471
472 count = select(fd_count+1,
08f15335 473 io_error_fd != -1?&r_fds:NULL,
4c36ddbe 474 &w_fds,NULL,
8cd9fd4e 475 &tv);
4c36ddbe 476
a57873b7
AT
477 if (count == 0) {
478 check_timeout();
479 }
480
4c36ddbe 481 if (count <= 0) {
554e0a8d
AT
482 if (errno == EBADF) {
483 exit_cleanup(RERR_SOCKETIO);
484 }
8d9dc9f9
AT
485 continue;
486 }
4c36ddbe 487
554e0a8d
AT
488 if (io_error_fd != -1 && FD_ISSET(io_error_fd, &r_fds)) {
489 read_error_fd();
490 }
491
8d9dc9f9 492 if (FD_ISSET(fd, &w_fds)) {
06ce139f
MP
493 int ret;
494 size_t n = len-total;
f0359dd0 495 ret = write(fd,buf+total,n);
4c36ddbe
AT
496
497 if (ret == -1 && errno == EINTR) {
498 continue;
499 }
500
f0359dd0
AT
501 if (ret == -1 &&
502 (errno == EWOULDBLOCK || errno == EAGAIN)) {
e92ee128 503 msleep(1);
f0359dd0
AT
504 continue;
505 }
506
4c36ddbe 507 if (ret <= 0) {
befbfe61
MP
508 /* Don't try to write errors back
509 * across the stream */
510 io_multiplexing_close();
3ce0f9a6 511 rprintf(FERROR, RSYNC_NAME
98b332ed 512 ": writefd_unbuffered failed to write %ld bytes: phase \"%s\": %s\n",
eca2adb4 513 (long) len, io_write_phase,
ce6c7c63 514 strerror(errno));
65417579 515 exit_cleanup(RERR_STREAMIO);
4c36ddbe
AT
516 }
517
08571358 518 sleep_for_bwlimit(ret);
ef5d23eb 519
4c36ddbe 520 total += ret;
a800434a 521
4c36ddbe
AT
522 if (io_timeout)
523 last_io = time(NULL);
8d9dc9f9 524 }
4c36ddbe 525 }
e44f9a12
AT
526
527 no_flush--;
720b47f2
AT
528}
529
8d9dc9f9 530
d6dead6b
AT
531static char *io_buffer;
532static int io_buffer_count;
533
534void io_start_buffering(int fd)
535{
8d9dc9f9 536 if (io_buffer) return;
679e7657 537 multiplex_out_fd = fd;
ff41a59f 538 io_buffer = (char *)malloc(IO_BUFFER_SIZE);
d6dead6b
AT
539 if (!io_buffer) out_of_memory("writefd");
540 io_buffer_count = 0;
ff41a59f
AT
541}
542
880da007
MP
543/**
544 * Write an message to a multiplexed stream. If this fails then rsync
545 * exits.
546 **/
9dd891bb 547static void mplex_write(int fd, enum logcode code, char *buf, size_t len)
ff41a59f
AT
548{
549 char buffer[4096];
06ce139f 550 size_t n = len;
8d9dc9f9 551
ff41a59f
AT
552 SIVAL(buffer, 0, ((MPLEX_BASE + (int)code)<<24) + len);
553
6d7b6081
AT
554 if (n > (sizeof(buffer)-4)) {
555 n = sizeof(buffer)-4;
ff41a59f
AT
556 }
557
558 memcpy(&buffer[4], buf, n);
559 writefd_unbuffered(fd, buffer, n+4);
560
561 len -= n;
562 buf += n;
563
6d7b6081
AT
564 if (len) {
565 writefd_unbuffered(fd, buf, len);
566 }
d6dead6b
AT
567}
568
ff41a59f 569
8d9dc9f9 570void io_flush(void)
d6dead6b 571{
679e7657 572 int fd = multiplex_out_fd;
90ba34e2
AT
573
574 err_list_push();
575
e44f9a12 576 if (!io_buffer_count || no_flush) return;
8d9dc9f9
AT
577
578 if (io_multiplexing_out) {
0f3203c3 579 mplex_write(fd, FNONE, io_buffer, io_buffer_count);
8d9dc9f9 580 } else {
4c36ddbe 581 writefd_unbuffered(fd, io_buffer, io_buffer_count);
d6dead6b 582 }
8d9dc9f9
AT
583 io_buffer_count = 0;
584}
585
0ba48136 586
7b5c3eb0 587void io_end_buffering(void)
8d9dc9f9
AT
588{
589 io_flush();
590 if (!io_multiplexing_out) {
ff41a59f 591 free(io_buffer);
8d9dc9f9
AT
592 io_buffer = NULL;
593 }
d6dead6b
AT
594}
595
9dd891bb 596static void writefd(int fd,char *buf,size_t len)
d6dead6b 597{
1b7c47cb
AT
598 stats.total_written += len;
599
90ba34e2
AT
600 err_list_push();
601
554e0a8d 602 if (!io_buffer || fd != multiplex_out_fd) {
4c36ddbe
AT
603 writefd_unbuffered(fd, buf, len);
604 return;
605 }
d6dead6b
AT
606
607 while (len) {
7b5c3eb0 608 int n = MIN((int) len, IO_BUFFER_SIZE-io_buffer_count);
d6dead6b
AT
609 if (n > 0) {
610 memcpy(io_buffer+io_buffer_count, buf, n);
611 buf += n;
612 len -= n;
613 io_buffer_count += n;
614 }
615
8d9dc9f9 616 if (io_buffer_count == IO_BUFFER_SIZE) io_flush();
d6dead6b 617 }
d6dead6b 618}
720b47f2
AT
619
620
b7922338 621void write_int(int f,int32 x)
720b47f2 622{
8d9dc9f9
AT
623 char b[4];
624 SIVAL(b,0,x);
4c36ddbe 625 writefd(f,b,4);
720b47f2
AT
626}
627
7a24c346 628
805edf9d
MP
629void write_int_named(int f, int32 x, const char *phase)
630{
631 io_write_phase = phase;
632 write_int(f, x);
633 io_write_phase = phase_unknown;
634}
635
636
7a24c346
MP
637/*
638 * Note: int64 may actually be a 32-bit type if ./configure couldn't find any
639 * 64-bit types on this platform.
640 */
71c46176 641void write_longint(int f, int64 x)
3a6a366f
AT
642{
643 extern int remote_version;
644 char b[8];
3a6a366f
AT
645
646 if (remote_version < 16 || x <= 0x7FFFFFFF) {
647 write_int(f, (int)x);
648 return;
649 }
650
8de330a3 651 write_int(f, (int32)0xFFFFFFFF);
3a6a366f
AT
652 SIVAL(b,0,(x&0xFFFFFFFF));
653 SIVAL(b,4,((x>>32)&0xFFFFFFFF));
654
4c36ddbe 655 writefd(f,b,8);
3a6a366f
AT
656}
657
9dd891bb 658void write_buf(int f,char *buf,size_t len)
720b47f2 659{
4c36ddbe 660 writefd(f,buf,len);
720b47f2
AT
661}
662
880da007 663/** Write a string to the connection */
6e4fb64e 664static void write_sbuf(int f,char *buf)
f0fca04e
AT
665{
666 write_buf(f, buf, strlen(buf));
667}
668
720b47f2 669
182dca5c
AT
670void write_byte(int f,unsigned char c)
671{
f0fca04e 672 write_buf(f,(char *)&c,1);
182dca5c
AT
673}
674
7a55d06e
MP
675
676
914cc65c
MP
677/**
678 * Read a line of up to @p maxlen characters into @p buf. Does not
679 * contain a trailing newline or carriage return.
680 *
681 * @return 1 for success; 0 for io error or truncation.
682 **/
9dd891bb 683int read_line(int f, char *buf, size_t maxlen)
f0fca04e
AT
684{
685 while (maxlen) {
528bfcd7 686 buf[0] = 0;
f0fca04e 687 read_buf(f, buf, 1);
914cc65c
MP
688 if (buf[0] == 0)
689 return 0;
f0fca04e
AT
690 if (buf[0] == '\n') {
691 buf[0] = 0;
692 break;
693 }
694 if (buf[0] != '\r') {
695 buf++;
696 maxlen--;
697 }
698 }
699 if (maxlen == 0) {
700 *buf = 0;
701 return 0;
702 }
528bfcd7 703
f0fca04e
AT
704 return 1;
705}
706
707
708void io_printf(int fd, const char *format, ...)
709{
710 va_list ap;
711 char buf[1024];
712 int len;
713
714 va_start(ap, format);
8950ac03 715 len = vsnprintf(buf, sizeof(buf), format, ap);
f0fca04e
AT
716 va_end(ap);
717
65417579 718 if (len < 0) exit_cleanup(RERR_STREAMIO);
f0fca04e
AT
719
720 write_sbuf(fd, buf);
721}
8d9dc9f9
AT
722
723
880da007 724/** Setup for multiplexing an error stream with the data stream */
8d9dc9f9
AT
725void io_start_multiplex_out(int fd)
726{
679e7657
AT
727 multiplex_out_fd = fd;
728 io_flush();
8d9dc9f9
AT
729 io_start_buffering(fd);
730 io_multiplexing_out = 1;
731}
732
880da007 733/** Setup for multiplexing an error stream with the data stream */
8d9dc9f9
AT
734void io_start_multiplex_in(int fd)
735{
679e7657
AT
736 multiplex_in_fd = fd;
737 io_flush();
8d9dc9f9
AT
738 io_multiplexing_in = 1;
739}
740
880da007 741/** Write an message to the multiplexed error stream */
9dd891bb 742int io_multiplex_write(enum logcode code, char *buf, size_t len)
8d9dc9f9
AT
743{
744 if (!io_multiplexing_out) return 0;
745
746 io_flush();
1b7c47cb 747 stats.total_written += (len+4);
ff41a59f 748 mplex_write(multiplex_out_fd, code, buf, len);
8d9dc9f9
AT
749 return 1;
750}
751
880da007 752/** Stop output multiplexing */
554e0a8d
AT
753void io_multiplexing_close(void)
754{
755 io_multiplexing_out = 0;
756}
757