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