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