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