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