Commit | Line | Data |
---|---|---|
720b47f2 AT |
1 | /* |
2 | Copyright (C) Andrew Tridgell 1996 | |
3 | Copyright (C) Paul Mackerras 1996 | |
4 | ||
5 | This program is free software; you can redistribute it and/or modify | |
6 | it under the terms of the GNU General Public License as published by | |
7 | the Free Software Foundation; either version 2 of the License, or | |
8 | (at your option) any later version. | |
9 | ||
10 | This program is distributed in the hope that it will be useful, | |
11 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
13 | GNU General Public License for more details. | |
14 | ||
15 | You should have received a copy of the GNU General Public License | |
16 | along with this program; if not, write to the Free Software | |
17 | Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. | |
18 | */ | |
19 | ||
20 | /* | |
08f15335 | 21 | socket and pipe IO utilities used in rsync |
720b47f2 AT |
22 | |
23 | tridge, June 1996 | |
24 | */ | |
25 | #include "rsync.h" | |
26 | ||
8cd9fd4e AT |
27 | /* if no timeout is specified then use a 60 second select timeout */ |
28 | #define SELECT_TIMEOUT 60 | |
29 | ||
ef5d23eb DD |
30 | extern int bwlimit; |
31 | ||
8d9dc9f9 AT |
32 | static int io_multiplexing_out; |
33 | static int io_multiplexing_in; | |
679e7657 AT |
34 | static int multiplex_in_fd; |
35 | static int multiplex_out_fd; | |
8d9dc9f9 | 36 | static time_t last_io; |
528bfcd7 | 37 | static int eof_error=1; |
720b47f2 | 38 | extern int verbose; |
6ba9279f | 39 | extern int io_timeout; |
a800434a | 40 | extern struct stats stats; |
720b47f2 | 41 | |
554e0a8d | 42 | static int io_error_fd = -1; |
720b47f2 | 43 | |
ff41a59f AT |
44 | static void read_loop(int fd, char *buf, int len); |
45 | ||
8d9dc9f9 AT |
46 | static void check_timeout(void) |
47 | { | |
0adb99b9 | 48 | extern int am_server, am_daemon; |
8d9dc9f9 AT |
49 | time_t t; |
50 | ||
51 | if (!io_timeout) return; | |
52 | ||
53 | if (!last_io) { | |
54 | last_io = time(NULL); | |
55 | return; | |
56 | } | |
57 | ||
58 | t = time(NULL); | |
59 | ||
86ffe37f | 60 | if (last_io && io_timeout && (t-last_io) >= io_timeout) { |
0adb99b9 AT |
61 | if (!am_server && !am_daemon) { |
62 | rprintf(FERROR,"io timeout after %d second - exiting\n", | |
63 | (int)(t-last_io)); | |
64 | } | |
65417579 | 65 | exit_cleanup(RERR_TIMEOUT); |
8d9dc9f9 AT |
66 | } |
67 | } | |
68 | ||
554e0a8d AT |
69 | /* setup the fd used to propogate errors */ |
70 | void io_set_error_fd(int fd) | |
71 | { | |
72 | io_error_fd = fd; | |
73 | } | |
74 | ||
ff41a59f | 75 | /* read some data from the error fd and write it to the write log code */ |
554e0a8d AT |
76 | static void read_error_fd(void) |
77 | { | |
78 | char buf[200]; | |
79 | int n; | |
80 | int fd = io_error_fd; | |
ff41a59f AT |
81 | int tag, len; |
82 | ||
554e0a8d AT |
83 | io_error_fd = -1; |
84 | ||
ff41a59f AT |
85 | read_loop(fd, buf, 4); |
86 | tag = IVAL(buf, 0); | |
87 | ||
88 | len = tag & 0xFFFFFF; | |
89 | tag = tag >> 24; | |
90 | tag -= MPLEX_BASE; | |
91 | ||
92 | while (len) { | |
93 | n = len; | |
94 | if (n > (sizeof(buf)-1)) n = sizeof(buf)-1; | |
95 | read_loop(fd, buf, n); | |
96 | rwrite((enum logcode)tag, buf, n); | |
97 | len -= n; | |
554e0a8d AT |
98 | } |
99 | ||
100 | io_error_fd = fd; | |
101 | } | |
102 | ||
720b47f2 | 103 | |
e44f9a12 | 104 | static int no_flush; |
720b47f2 | 105 | |
4c36ddbe AT |
106 | /* read from a socket with IO timeout. return the number of |
107 | bytes read. If no bytes can be read then exit, never return | |
108 | a number <= 0 */ | |
109 | static int read_timeout(int fd, char *buf, int len) | |
8d9dc9f9 | 110 | { |
4c36ddbe AT |
111 | int n, ret=0; |
112 | ||
ea2111d1 AT |
113 | io_flush(); |
114 | ||
4c36ddbe AT |
115 | while (ret == 0) { |
116 | fd_set fds; | |
117 | struct timeval tv; | |
554e0a8d | 118 | int fd_count = fd+1; |
4c36ddbe AT |
119 | |
120 | FD_ZERO(&fds); | |
121 | FD_SET(fd, &fds); | |
554e0a8d AT |
122 | if (io_error_fd != -1) { |
123 | FD_SET(io_error_fd, &fds); | |
124 | if (io_error_fd > fd) fd_count = io_error_fd+1; | |
125 | } | |
126 | ||
8cd9fd4e | 127 | tv.tv_sec = io_timeout?io_timeout:SELECT_TIMEOUT; |
4c36ddbe AT |
128 | tv.tv_usec = 0; |
129 | ||
554e0a8d AT |
130 | errno = 0; |
131 | ||
132 | if (select(fd_count, &fds, NULL, NULL, &tv) < 1) { | |
133 | if (errno == EBADF) { | |
134 | exit_cleanup(RERR_SOCKETIO); | |
135 | } | |
4c36ddbe AT |
136 | check_timeout(); |
137 | continue; | |
138 | } | |
139 | ||
554e0a8d AT |
140 | if (io_error_fd != -1 && FD_ISSET(io_error_fd, &fds)) { |
141 | read_error_fd(); | |
142 | } | |
143 | ||
144 | if (!FD_ISSET(fd, &fds)) continue; | |
145 | ||
4c36ddbe AT |
146 | n = read(fd, buf, len); |
147 | ||
8d9dc9f9 AT |
148 | if (n > 0) { |
149 | buf += n; | |
150 | len -= n; | |
4c36ddbe AT |
151 | ret += n; |
152 | if (io_timeout) | |
153 | last_io = time(NULL); | |
154 | continue; | |
8d9dc9f9 | 155 | } |
4c36ddbe AT |
156 | |
157 | if (n == -1 && errno == EINTR) { | |
158 | continue; | |
159 | } | |
160 | ||
f0359dd0 AT |
161 | if (n == -1 && |
162 | (errno == EWOULDBLOCK || errno == EAGAIN)) { | |
163 | continue; | |
164 | } | |
165 | ||
5d1e1dcf | 166 | |
8d9dc9f9 | 167 | if (n == 0) { |
528bfcd7 | 168 | if (eof_error) { |
ca8e9694 | 169 | rprintf(FERROR,"unexpected EOF in read_timeout\n"); |
528bfcd7 | 170 | } |
65417579 | 171 | exit_cleanup(RERR_STREAMIO); |
8d9dc9f9 | 172 | } |
8d9dc9f9 | 173 | |
5d1e1dcf | 174 | /* this prevents us trying to write errors on a dead socket */ |
554e0a8d | 175 | io_multiplexing_close(); |
5d1e1dcf | 176 | |
4c36ddbe | 177 | rprintf(FERROR,"read error: %s\n", strerror(errno)); |
65417579 | 178 | exit_cleanup(RERR_STREAMIO); |
4c36ddbe | 179 | } |
8d9dc9f9 | 180 | |
4c36ddbe AT |
181 | return ret; |
182 | } | |
8d9dc9f9 | 183 | |
4c36ddbe AT |
184 | /* continue trying to read len bytes - don't return until len |
185 | has been read */ | |
186 | static void read_loop(int fd, char *buf, int len) | |
187 | { | |
188 | while (len) { | |
189 | int n = read_timeout(fd, buf, len); | |
190 | ||
191 | buf += n; | |
192 | len -= n; | |
8d9dc9f9 AT |
193 | } |
194 | } | |
195 | ||
cad2bba7 | 196 | /* read from the file descriptor handling multiplexing - |
4c36ddbe AT |
197 | return number of bytes read |
198 | never return <= 0 */ | |
8d9dc9f9 AT |
199 | static int read_unbuffered(int fd, char *buf, int len) |
200 | { | |
201 | static int remaining; | |
8d9dc9f9 AT |
202 | int tag, ret=0; |
203 | char line[1024]; | |
204 | ||
679e7657 | 205 | if (!io_multiplexing_in || fd != multiplex_in_fd) |
4c36ddbe | 206 | return read_timeout(fd, buf, len); |
8d9dc9f9 AT |
207 | |
208 | while (ret == 0) { | |
209 | if (remaining) { | |
210 | len = MIN(len, remaining); | |
211 | read_loop(fd, buf, len); | |
212 | remaining -= len; | |
213 | ret = len; | |
214 | continue; | |
215 | } | |
216 | ||
ff41a59f AT |
217 | read_loop(fd, line, 4); |
218 | tag = IVAL(line, 0); | |
679e7657 | 219 | |
8d9dc9f9 AT |
220 | remaining = tag & 0xFFFFFF; |
221 | tag = tag >> 24; | |
222 | ||
223 | if (tag == MPLEX_BASE) continue; | |
224 | ||
225 | tag -= MPLEX_BASE; | |
226 | ||
227 | if (tag != FERROR && tag != FINFO) { | |
228 | rprintf(FERROR,"unexpected tag %d\n", tag); | |
65417579 | 229 | exit_cleanup(RERR_STREAMIO); |
8d9dc9f9 AT |
230 | } |
231 | ||
232 | if (remaining > sizeof(line)-1) { | |
233 | rprintf(FERROR,"multiplexing overflow %d\n\n", | |
234 | remaining); | |
65417579 | 235 | exit_cleanup(RERR_STREAMIO); |
8d9dc9f9 AT |
236 | } |
237 | ||
238 | read_loop(fd, line, remaining); | |
239 | line[remaining] = 0; | |
240 | ||
ff41a59f | 241 | rprintf((enum logcode)tag,"%s", line); |
8d9dc9f9 AT |
242 | remaining = 0; |
243 | } | |
244 | ||
245 | return ret; | |
246 | } | |
247 | ||
248 | ||
4c36ddbe AT |
249 | /* do a buffered read from fd. don't return until all N bytes |
250 | have been read. If all N can't be read then exit with an error */ | |
251 | static void readfd(int fd,char *buffer,int N) | |
720b47f2 | 252 | { |
6ba9279f AT |
253 | int ret; |
254 | int total=0; | |
6ba9279f | 255 | |
6ba9279f | 256 | while (total < N) { |
8d9dc9f9 AT |
257 | io_flush(); |
258 | ||
4c36ddbe | 259 | ret = read_unbuffered(fd,buffer + total,N-total); |
6ba9279f | 260 | total += ret; |
7f28dbee | 261 | } |
1b7c47cb AT |
262 | |
263 | stats.total_read += total; | |
720b47f2 AT |
264 | } |
265 | ||
266 | ||
b7922338 | 267 | int32 read_int(int f) |
720b47f2 | 268 | { |
4c36ddbe | 269 | char b[4]; |
d730b113 AT |
270 | int32 ret; |
271 | ||
4c36ddbe | 272 | readfd(f,b,4); |
d730b113 AT |
273 | ret = IVAL(b,0); |
274 | if (ret == (int32)0xffffffff) return -1; | |
275 | return ret; | |
720b47f2 AT |
276 | } |
277 | ||
71c46176 | 278 | int64 read_longint(int f) |
3a6a366f AT |
279 | { |
280 | extern int remote_version; | |
71c46176 | 281 | int64 ret; |
3a6a366f AT |
282 | char b[8]; |
283 | ret = read_int(f); | |
71c46176 | 284 | |
8de330a3 AT |
285 | if ((int32)ret != (int32)0xffffffff) { |
286 | return ret; | |
287 | } | |
71c46176 | 288 | |
3bee6733 | 289 | #ifdef NO_INT64 |
9486289c | 290 | rprintf(FERROR,"Integer overflow - attempted 64 bit offset\n"); |
65417579 | 291 | exit_cleanup(RERR_UNSUPPORTED); |
71c46176 AT |
292 | #else |
293 | if (remote_version >= 16) { | |
4c36ddbe | 294 | readfd(f,b,8); |
71c46176 | 295 | ret = IVAL(b,0) | (((int64)IVAL(b,4))<<32); |
3a6a366f | 296 | } |
71c46176 AT |
297 | #endif |
298 | ||
3a6a366f AT |
299 | return ret; |
300 | } | |
301 | ||
720b47f2 AT |
302 | void read_buf(int f,char *buf,int len) |
303 | { | |
4c36ddbe | 304 | readfd(f,buf,len); |
720b47f2 AT |
305 | } |
306 | ||
575f2fca AT |
307 | void read_sbuf(int f,char *buf,int len) |
308 | { | |
309 | read_buf(f,buf,len); | |
310 | buf[len] = 0; | |
311 | } | |
312 | ||
182dca5c AT |
313 | unsigned char read_byte(int f) |
314 | { | |
4c36ddbe AT |
315 | unsigned char c; |
316 | read_buf(f,(char *)&c,1); | |
317 | return c; | |
182dca5c | 318 | } |
720b47f2 | 319 | |
ae682c3e | 320 | /* write len bytes to fd */ |
4c36ddbe | 321 | static void writefd_unbuffered(int fd,char *buf,int len) |
720b47f2 | 322 | { |
8d9dc9f9 AT |
323 | int total = 0; |
324 | fd_set w_fds, r_fds; | |
4c36ddbe | 325 | int fd_count, count; |
8d9dc9f9 | 326 | struct timeval tv; |
720b47f2 | 327 | |
e44f9a12 AT |
328 | no_flush++; |
329 | ||
4c36ddbe | 330 | while (total < len) { |
8d9dc9f9 AT |
331 | FD_ZERO(&w_fds); |
332 | FD_ZERO(&r_fds); | |
333 | FD_SET(fd,&w_fds); | |
554e0a8d | 334 | fd_count = fd; |
4c36ddbe | 335 | |
554e0a8d AT |
336 | if (io_error_fd != -1) { |
337 | FD_SET(io_error_fd,&r_fds); | |
338 | if (io_error_fd > fd_count) | |
339 | fd_count = io_error_fd; | |
8d9dc9f9 AT |
340 | } |
341 | ||
8cd9fd4e | 342 | tv.tv_sec = io_timeout?io_timeout:SELECT_TIMEOUT; |
8d9dc9f9 | 343 | tv.tv_usec = 0; |
4c36ddbe | 344 | |
554e0a8d AT |
345 | errno = 0; |
346 | ||
347 | count = select(fd_count+1, | |
08f15335 | 348 | io_error_fd != -1?&r_fds:NULL, |
4c36ddbe | 349 | &w_fds,NULL, |
8cd9fd4e | 350 | &tv); |
4c36ddbe AT |
351 | |
352 | if (count <= 0) { | |
554e0a8d AT |
353 | if (errno == EBADF) { |
354 | exit_cleanup(RERR_SOCKETIO); | |
355 | } | |
8d9dc9f9 AT |
356 | check_timeout(); |
357 | continue; | |
358 | } | |
4c36ddbe | 359 | |
554e0a8d AT |
360 | if (io_error_fd != -1 && FD_ISSET(io_error_fd, &r_fds)) { |
361 | read_error_fd(); | |
362 | } | |
363 | ||
8d9dc9f9 | 364 | if (FD_ISSET(fd, &w_fds)) { |
07b7c86c AT |
365 | int ret, n = len-total; |
366 | ||
f0359dd0 | 367 | ret = write(fd,buf+total,n); |
4c36ddbe AT |
368 | |
369 | if (ret == -1 && errno == EINTR) { | |
370 | continue; | |
371 | } | |
372 | ||
f0359dd0 AT |
373 | if (ret == -1 && |
374 | (errno == EWOULDBLOCK || errno == EAGAIN)) { | |
375 | continue; | |
376 | } | |
377 | ||
4c36ddbe AT |
378 | if (ret <= 0) { |
379 | rprintf(FERROR,"erroring writing %d bytes - exiting\n", len); | |
65417579 | 380 | exit_cleanup(RERR_STREAMIO); |
4c36ddbe AT |
381 | } |
382 | ||
ef5d23eb DD |
383 | /* Sleep after writing to limit I/O bandwidth */ |
384 | if (bwlimit) | |
385 | { | |
386 | tv.tv_sec = 0; | |
387 | tv.tv_usec = ret * 1000 / bwlimit; | |
388 | while (tv.tv_usec > 1000000) | |
389 | { | |
390 | tv.tv_sec++; | |
391 | tv.tv_usec -= 1000000; | |
392 | } | |
393 | select(0, NULL, NULL, NULL, &tv); | |
394 | } | |
395 | ||
4c36ddbe | 396 | total += ret; |
a800434a | 397 | |
4c36ddbe AT |
398 | if (io_timeout) |
399 | last_io = time(NULL); | |
8d9dc9f9 | 400 | } |
4c36ddbe | 401 | } |
e44f9a12 AT |
402 | |
403 | no_flush--; | |
720b47f2 AT |
404 | } |
405 | ||
8d9dc9f9 | 406 | |
d6dead6b AT |
407 | static char *io_buffer; |
408 | static int io_buffer_count; | |
409 | ||
410 | void io_start_buffering(int fd) | |
411 | { | |
8d9dc9f9 | 412 | if (io_buffer) return; |
679e7657 | 413 | multiplex_out_fd = fd; |
ff41a59f | 414 | io_buffer = (char *)malloc(IO_BUFFER_SIZE); |
d6dead6b AT |
415 | if (!io_buffer) out_of_memory("writefd"); |
416 | io_buffer_count = 0; | |
ff41a59f AT |
417 | } |
418 | ||
419 | /* write an message to a multiplexed stream. If this fails then rsync | |
420 | exits */ | |
421 | static void mplex_write(int fd, enum logcode code, char *buf, int len) | |
422 | { | |
423 | char buffer[4096]; | |
424 | int n = len; | |
8d9dc9f9 | 425 | |
ff41a59f AT |
426 | SIVAL(buffer, 0, ((MPLEX_BASE + (int)code)<<24) + len); |
427 | ||
6d7b6081 AT |
428 | if (n > (sizeof(buffer)-4)) { |
429 | n = sizeof(buffer)-4; | |
ff41a59f AT |
430 | } |
431 | ||
432 | memcpy(&buffer[4], buf, n); | |
433 | writefd_unbuffered(fd, buffer, n+4); | |
434 | ||
435 | len -= n; | |
436 | buf += n; | |
437 | ||
6d7b6081 AT |
438 | if (len) { |
439 | writefd_unbuffered(fd, buf, len); | |
440 | } | |
d6dead6b AT |
441 | } |
442 | ||
ff41a59f | 443 | |
8d9dc9f9 | 444 | void io_flush(void) |
d6dead6b | 445 | { |
679e7657 | 446 | int fd = multiplex_out_fd; |
e44f9a12 | 447 | if (!io_buffer_count || no_flush) return; |
8d9dc9f9 AT |
448 | |
449 | if (io_multiplexing_out) { | |
0f3203c3 | 450 | mplex_write(fd, FNONE, io_buffer, io_buffer_count); |
8d9dc9f9 | 451 | } else { |
4c36ddbe | 452 | writefd_unbuffered(fd, io_buffer, io_buffer_count); |
d6dead6b | 453 | } |
8d9dc9f9 AT |
454 | io_buffer_count = 0; |
455 | } | |
456 | ||
457 | void io_end_buffering(int fd) | |
458 | { | |
459 | io_flush(); | |
460 | if (!io_multiplexing_out) { | |
ff41a59f | 461 | free(io_buffer); |
8d9dc9f9 AT |
462 | io_buffer = NULL; |
463 | } | |
d6dead6b AT |
464 | } |
465 | ||
4c36ddbe | 466 | static void writefd(int fd,char *buf,int len) |
d6dead6b | 467 | { |
1b7c47cb AT |
468 | stats.total_written += len; |
469 | ||
554e0a8d | 470 | if (!io_buffer || fd != multiplex_out_fd) { |
4c36ddbe AT |
471 | writefd_unbuffered(fd, buf, len); |
472 | return; | |
473 | } | |
d6dead6b AT |
474 | |
475 | while (len) { | |
476 | int n = MIN(len, IO_BUFFER_SIZE-io_buffer_count); | |
477 | if (n > 0) { | |
478 | memcpy(io_buffer+io_buffer_count, buf, n); | |
479 | buf += n; | |
480 | len -= n; | |
481 | io_buffer_count += n; | |
482 | } | |
483 | ||
8d9dc9f9 | 484 | if (io_buffer_count == IO_BUFFER_SIZE) io_flush(); |
d6dead6b | 485 | } |
d6dead6b | 486 | } |
720b47f2 AT |
487 | |
488 | ||
b7922338 | 489 | void write_int(int f,int32 x) |
720b47f2 | 490 | { |
8d9dc9f9 AT |
491 | char b[4]; |
492 | SIVAL(b,0,x); | |
4c36ddbe | 493 | writefd(f,b,4); |
720b47f2 AT |
494 | } |
495 | ||
71c46176 | 496 | void write_longint(int f, int64 x) |
3a6a366f AT |
497 | { |
498 | extern int remote_version; | |
499 | char b[8]; | |
3a6a366f AT |
500 | |
501 | if (remote_version < 16 || x <= 0x7FFFFFFF) { | |
502 | write_int(f, (int)x); | |
503 | return; | |
504 | } | |
505 | ||
8de330a3 | 506 | write_int(f, (int32)0xFFFFFFFF); |
3a6a366f AT |
507 | SIVAL(b,0,(x&0xFFFFFFFF)); |
508 | SIVAL(b,4,((x>>32)&0xFFFFFFFF)); | |
509 | ||
4c36ddbe | 510 | writefd(f,b,8); |
3a6a366f AT |
511 | } |
512 | ||
720b47f2 AT |
513 | void write_buf(int f,char *buf,int len) |
514 | { | |
4c36ddbe | 515 | writefd(f,buf,len); |
720b47f2 AT |
516 | } |
517 | ||
f0fca04e | 518 | /* write a string to the connection */ |
6e4fb64e | 519 | static void write_sbuf(int f,char *buf) |
f0fca04e AT |
520 | { |
521 | write_buf(f, buf, strlen(buf)); | |
522 | } | |
523 | ||
720b47f2 | 524 | |
182dca5c AT |
525 | void write_byte(int f,unsigned char c) |
526 | { | |
f0fca04e | 527 | write_buf(f,(char *)&c,1); |
182dca5c AT |
528 | } |
529 | ||
f0fca04e AT |
530 | int read_line(int f, char *buf, int maxlen) |
531 | { | |
528bfcd7 AT |
532 | eof_error = 0; |
533 | ||
f0fca04e | 534 | while (maxlen) { |
528bfcd7 | 535 | buf[0] = 0; |
f0fca04e | 536 | read_buf(f, buf, 1); |
528bfcd7 | 537 | if (buf[0] == 0) return 0; |
f0fca04e AT |
538 | if (buf[0] == '\n') { |
539 | buf[0] = 0; | |
540 | break; | |
541 | } | |
542 | if (buf[0] != '\r') { | |
543 | buf++; | |
544 | maxlen--; | |
545 | } | |
546 | } | |
547 | if (maxlen == 0) { | |
548 | *buf = 0; | |
549 | return 0; | |
550 | } | |
528bfcd7 AT |
551 | |
552 | eof_error = 1; | |
553 | ||
f0fca04e AT |
554 | return 1; |
555 | } | |
556 | ||
557 | ||
558 | void io_printf(int fd, const char *format, ...) | |
559 | { | |
560 | va_list ap; | |
561 | char buf[1024]; | |
562 | int len; | |
563 | ||
564 | va_start(ap, format); | |
37f9805d | 565 | len = vslprintf(buf, sizeof(buf), format, ap); |
f0fca04e AT |
566 | va_end(ap); |
567 | ||
65417579 | 568 | if (len < 0) exit_cleanup(RERR_STREAMIO); |
f0fca04e AT |
569 | |
570 | write_sbuf(fd, buf); | |
571 | } | |
8d9dc9f9 AT |
572 | |
573 | ||
574 | /* setup for multiplexing an error stream with the data stream */ | |
575 | void io_start_multiplex_out(int fd) | |
576 | { | |
679e7657 AT |
577 | multiplex_out_fd = fd; |
578 | io_flush(); | |
8d9dc9f9 AT |
579 | io_start_buffering(fd); |
580 | io_multiplexing_out = 1; | |
581 | } | |
582 | ||
583 | /* setup for multiplexing an error stream with the data stream */ | |
584 | void io_start_multiplex_in(int fd) | |
585 | { | |
679e7657 AT |
586 | multiplex_in_fd = fd; |
587 | io_flush(); | |
8d9dc9f9 AT |
588 | io_multiplexing_in = 1; |
589 | } | |
590 | ||
554e0a8d | 591 | /* write an message to the multiplexed error stream */ |
ff41a59f | 592 | int io_multiplex_write(enum logcode code, char *buf, int len) |
8d9dc9f9 AT |
593 | { |
594 | if (!io_multiplexing_out) return 0; | |
595 | ||
596 | io_flush(); | |
1b7c47cb | 597 | stats.total_written += (len+4); |
ff41a59f | 598 | mplex_write(multiplex_out_fd, code, buf, len); |
8d9dc9f9 AT |
599 | return 1; |
600 | } | |
601 | ||
554e0a8d | 602 | /* write a message to the special error fd */ |
ff41a59f | 603 | int io_error_write(int f, enum logcode code, char *buf, int len) |
554e0a8d AT |
604 | { |
605 | if (f == -1) return 0; | |
ff41a59f | 606 | mplex_write(f, code, buf, len); |
554e0a8d AT |
607 | return 1; |
608 | } | |
609 | ||
610 | /* stop output multiplexing */ | |
611 | void io_multiplexing_close(void) | |
612 | { | |
613 | io_multiplexing_out = 0; | |
614 | } | |
615 |