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 MP |
392 | |
393 | /** | |
394 | * Write len bytes to the file descriptor @p fd. | |
395 | * | |
396 | * This function underlies the multiplexing system. The body of the | |
397 | * application never calls this function directly. | |
398 | **/ | |
9dd891bb | 399 | static void writefd_unbuffered(int fd,char *buf,size_t len) |
720b47f2 | 400 | { |
06ce139f | 401 | size_t total = 0; |
8d9dc9f9 | 402 | fd_set w_fds, r_fds; |
4c36ddbe | 403 | int fd_count, count; |
8d9dc9f9 | 404 | struct timeval tv; |
720b47f2 | 405 | |
90ba34e2 AT |
406 | err_list_push(); |
407 | ||
e44f9a12 AT |
408 | no_flush++; |
409 | ||
4c36ddbe | 410 | while (total < len) { |
8d9dc9f9 AT |
411 | FD_ZERO(&w_fds); |
412 | FD_ZERO(&r_fds); | |
413 | FD_SET(fd,&w_fds); | |
554e0a8d | 414 | fd_count = fd; |
4c36ddbe | 415 | |
554e0a8d AT |
416 | if (io_error_fd != -1) { |
417 | FD_SET(io_error_fd,&r_fds); | |
418 | if (io_error_fd > fd_count) | |
419 | fd_count = io_error_fd; | |
8d9dc9f9 AT |
420 | } |
421 | ||
8cd9fd4e | 422 | tv.tv_sec = io_timeout?io_timeout:SELECT_TIMEOUT; |
8d9dc9f9 | 423 | tv.tv_usec = 0; |
4c36ddbe | 424 | |
554e0a8d AT |
425 | errno = 0; |
426 | ||
427 | count = select(fd_count+1, | |
08f15335 | 428 | io_error_fd != -1?&r_fds:NULL, |
4c36ddbe | 429 | &w_fds,NULL, |
8cd9fd4e | 430 | &tv); |
4c36ddbe | 431 | |
a57873b7 AT |
432 | if (count == 0) { |
433 | check_timeout(); | |
434 | } | |
435 | ||
4c36ddbe | 436 | if (count <= 0) { |
554e0a8d AT |
437 | if (errno == EBADF) { |
438 | exit_cleanup(RERR_SOCKETIO); | |
439 | } | |
8d9dc9f9 AT |
440 | continue; |
441 | } | |
4c36ddbe | 442 | |
554e0a8d AT |
443 | if (io_error_fd != -1 && FD_ISSET(io_error_fd, &r_fds)) { |
444 | read_error_fd(); | |
445 | } | |
446 | ||
8d9dc9f9 | 447 | if (FD_ISSET(fd, &w_fds)) { |
06ce139f MP |
448 | int ret; |
449 | size_t n = len-total; | |
f0359dd0 | 450 | ret = write(fd,buf+total,n); |
4c36ddbe AT |
451 | |
452 | if (ret == -1 && errno == EINTR) { | |
453 | continue; | |
454 | } | |
455 | ||
f0359dd0 AT |
456 | if (ret == -1 && |
457 | (errno == EWOULDBLOCK || errno == EAGAIN)) { | |
e92ee128 | 458 | msleep(1); |
f0359dd0 AT |
459 | continue; |
460 | } | |
461 | ||
4c36ddbe | 462 | if (ret <= 0) { |
befbfe61 MP |
463 | /* Don't try to write errors back |
464 | * across the stream */ | |
465 | io_multiplexing_close(); | |
3ce0f9a6 | 466 | rprintf(FERROR, RSYNC_NAME |
8901a07f MP |
467 | ": writefd_unbuffered failed to write %ld bytes: %s\n", |
468 | (long) len, | |
ce6c7c63 | 469 | strerror(errno)); |
65417579 | 470 | exit_cleanup(RERR_STREAMIO); |
4c36ddbe AT |
471 | } |
472 | ||
ef5d23eb DD |
473 | /* Sleep after writing to limit I/O bandwidth */ |
474 | if (bwlimit) | |
475 | { | |
476 | tv.tv_sec = 0; | |
477 | tv.tv_usec = ret * 1000 / bwlimit; | |
478 | while (tv.tv_usec > 1000000) | |
479 | { | |
480 | tv.tv_sec++; | |
481 | tv.tv_usec -= 1000000; | |
482 | } | |
483 | select(0, NULL, NULL, NULL, &tv); | |
484 | } | |
485 | ||
4c36ddbe | 486 | total += ret; |
a800434a | 487 | |
4c36ddbe AT |
488 | if (io_timeout) |
489 | last_io = time(NULL); | |
8d9dc9f9 | 490 | } |
4c36ddbe | 491 | } |
e44f9a12 AT |
492 | |
493 | no_flush--; | |
720b47f2 AT |
494 | } |
495 | ||
8d9dc9f9 | 496 | |
d6dead6b AT |
497 | static char *io_buffer; |
498 | static int io_buffer_count; | |
499 | ||
500 | void io_start_buffering(int fd) | |
501 | { | |
8d9dc9f9 | 502 | if (io_buffer) return; |
679e7657 | 503 | multiplex_out_fd = fd; |
ff41a59f | 504 | io_buffer = (char *)malloc(IO_BUFFER_SIZE); |
d6dead6b AT |
505 | if (!io_buffer) out_of_memory("writefd"); |
506 | io_buffer_count = 0; | |
ff41a59f AT |
507 | } |
508 | ||
880da007 MP |
509 | /** |
510 | * Write an message to a multiplexed stream. If this fails then rsync | |
511 | * exits. | |
512 | **/ | |
9dd891bb | 513 | static void mplex_write(int fd, enum logcode code, char *buf, size_t len) |
ff41a59f AT |
514 | { |
515 | char buffer[4096]; | |
06ce139f | 516 | size_t n = len; |
8d9dc9f9 | 517 | |
ff41a59f AT |
518 | SIVAL(buffer, 0, ((MPLEX_BASE + (int)code)<<24) + len); |
519 | ||
6d7b6081 AT |
520 | if (n > (sizeof(buffer)-4)) { |
521 | n = sizeof(buffer)-4; | |
ff41a59f AT |
522 | } |
523 | ||
524 | memcpy(&buffer[4], buf, n); | |
525 | writefd_unbuffered(fd, buffer, n+4); | |
526 | ||
527 | len -= n; | |
528 | buf += n; | |
529 | ||
6d7b6081 AT |
530 | if (len) { |
531 | writefd_unbuffered(fd, buf, len); | |
532 | } | |
d6dead6b AT |
533 | } |
534 | ||
ff41a59f | 535 | |
8d9dc9f9 | 536 | void io_flush(void) |
d6dead6b | 537 | { |
679e7657 | 538 | int fd = multiplex_out_fd; |
90ba34e2 AT |
539 | |
540 | err_list_push(); | |
541 | ||
e44f9a12 | 542 | if (!io_buffer_count || no_flush) return; |
8d9dc9f9 AT |
543 | |
544 | if (io_multiplexing_out) { | |
0f3203c3 | 545 | mplex_write(fd, FNONE, io_buffer, io_buffer_count); |
8d9dc9f9 | 546 | } else { |
4c36ddbe | 547 | writefd_unbuffered(fd, io_buffer, io_buffer_count); |
d6dead6b | 548 | } |
8d9dc9f9 AT |
549 | io_buffer_count = 0; |
550 | } | |
551 | ||
0ba48136 | 552 | |
7b5c3eb0 | 553 | void io_end_buffering(void) |
8d9dc9f9 AT |
554 | { |
555 | io_flush(); | |
556 | if (!io_multiplexing_out) { | |
ff41a59f | 557 | free(io_buffer); |
8d9dc9f9 AT |
558 | io_buffer = NULL; |
559 | } | |
d6dead6b AT |
560 | } |
561 | ||
9dd891bb | 562 | static void writefd(int fd,char *buf,size_t len) |
d6dead6b | 563 | { |
1b7c47cb AT |
564 | stats.total_written += len; |
565 | ||
90ba34e2 AT |
566 | err_list_push(); |
567 | ||
554e0a8d | 568 | if (!io_buffer || fd != multiplex_out_fd) { |
4c36ddbe AT |
569 | writefd_unbuffered(fd, buf, len); |
570 | return; | |
571 | } | |
d6dead6b AT |
572 | |
573 | while (len) { | |
7b5c3eb0 | 574 | int n = MIN((int) len, IO_BUFFER_SIZE-io_buffer_count); |
d6dead6b AT |
575 | if (n > 0) { |
576 | memcpy(io_buffer+io_buffer_count, buf, n); | |
577 | buf += n; | |
578 | len -= n; | |
579 | io_buffer_count += n; | |
580 | } | |
581 | ||
8d9dc9f9 | 582 | if (io_buffer_count == IO_BUFFER_SIZE) io_flush(); |
d6dead6b | 583 | } |
d6dead6b | 584 | } |
720b47f2 AT |
585 | |
586 | ||
b7922338 | 587 | void write_int(int f,int32 x) |
720b47f2 | 588 | { |
8d9dc9f9 AT |
589 | char b[4]; |
590 | SIVAL(b,0,x); | |
4c36ddbe | 591 | writefd(f,b,4); |
720b47f2 AT |
592 | } |
593 | ||
7a24c346 MP |
594 | |
595 | /* | |
596 | * Note: int64 may actually be a 32-bit type if ./configure couldn't find any | |
597 | * 64-bit types on this platform. | |
598 | */ | |
71c46176 | 599 | void write_longint(int f, int64 x) |
3a6a366f AT |
600 | { |
601 | extern int remote_version; | |
602 | char b[8]; | |
3a6a366f AT |
603 | |
604 | if (remote_version < 16 || x <= 0x7FFFFFFF) { | |
605 | write_int(f, (int)x); | |
606 | return; | |
607 | } | |
608 | ||
8de330a3 | 609 | write_int(f, (int32)0xFFFFFFFF); |
3a6a366f AT |
610 | SIVAL(b,0,(x&0xFFFFFFFF)); |
611 | SIVAL(b,4,((x>>32)&0xFFFFFFFF)); | |
612 | ||
4c36ddbe | 613 | writefd(f,b,8); |
3a6a366f AT |
614 | } |
615 | ||
9dd891bb | 616 | void write_buf(int f,char *buf,size_t len) |
720b47f2 | 617 | { |
4c36ddbe | 618 | writefd(f,buf,len); |
720b47f2 AT |
619 | } |
620 | ||
880da007 | 621 | /** Write a string to the connection */ |
6e4fb64e | 622 | static void write_sbuf(int f,char *buf) |
f0fca04e AT |
623 | { |
624 | write_buf(f, buf, strlen(buf)); | |
625 | } | |
626 | ||
720b47f2 | 627 | |
182dca5c AT |
628 | void write_byte(int f,unsigned char c) |
629 | { | |
f0fca04e | 630 | write_buf(f,(char *)&c,1); |
182dca5c AT |
631 | } |
632 | ||
7a55d06e MP |
633 | |
634 | ||
914cc65c MP |
635 | /** |
636 | * Read a line of up to @p maxlen characters into @p buf. Does not | |
637 | * contain a trailing newline or carriage return. | |
638 | * | |
639 | * @return 1 for success; 0 for io error or truncation. | |
640 | **/ | |
9dd891bb | 641 | int read_line(int f, char *buf, size_t maxlen) |
f0fca04e AT |
642 | { |
643 | while (maxlen) { | |
528bfcd7 | 644 | buf[0] = 0; |
f0fca04e | 645 | read_buf(f, buf, 1); |
914cc65c MP |
646 | if (buf[0] == 0) |
647 | return 0; | |
f0fca04e AT |
648 | if (buf[0] == '\n') { |
649 | buf[0] = 0; | |
650 | break; | |
651 | } | |
652 | if (buf[0] != '\r') { | |
653 | buf++; | |
654 | maxlen--; | |
655 | } | |
656 | } | |
657 | if (maxlen == 0) { | |
658 | *buf = 0; | |
659 | return 0; | |
660 | } | |
528bfcd7 | 661 | |
f0fca04e AT |
662 | return 1; |
663 | } | |
664 | ||
665 | ||
666 | void io_printf(int fd, const char *format, ...) | |
667 | { | |
668 | va_list ap; | |
669 | char buf[1024]; | |
670 | int len; | |
671 | ||
672 | va_start(ap, format); | |
8950ac03 | 673 | len = vsnprintf(buf, sizeof(buf), format, ap); |
f0fca04e AT |
674 | va_end(ap); |
675 | ||
65417579 | 676 | if (len < 0) exit_cleanup(RERR_STREAMIO); |
f0fca04e AT |
677 | |
678 | write_sbuf(fd, buf); | |
679 | } | |
8d9dc9f9 AT |
680 | |
681 | ||
880da007 | 682 | /** Setup for multiplexing an error stream with the data stream */ |
8d9dc9f9 AT |
683 | void io_start_multiplex_out(int fd) |
684 | { | |
679e7657 AT |
685 | multiplex_out_fd = fd; |
686 | io_flush(); | |
8d9dc9f9 AT |
687 | io_start_buffering(fd); |
688 | io_multiplexing_out = 1; | |
689 | } | |
690 | ||
880da007 | 691 | /** Setup for multiplexing an error stream with the data stream */ |
8d9dc9f9 AT |
692 | void io_start_multiplex_in(int fd) |
693 | { | |
679e7657 AT |
694 | multiplex_in_fd = fd; |
695 | io_flush(); | |
8d9dc9f9 AT |
696 | io_multiplexing_in = 1; |
697 | } | |
698 | ||
880da007 | 699 | /** Write an message to the multiplexed error stream */ |
9dd891bb | 700 | int io_multiplex_write(enum logcode code, char *buf, size_t len) |
8d9dc9f9 AT |
701 | { |
702 | if (!io_multiplexing_out) return 0; | |
703 | ||
704 | io_flush(); | |
1b7c47cb | 705 | stats.total_written += (len+4); |
ff41a59f | 706 | mplex_write(multiplex_out_fd, code, buf, len); |
8d9dc9f9 AT |
707 | return 1; |
708 | } | |
709 | ||
880da007 | 710 | /** Stop output multiplexing */ |
554e0a8d AT |
711 | void io_multiplexing_close(void) |
712 | { | |
713 | io_multiplexing_out = 0; | |
714 | } | |
715 |