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