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