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; | |
76c21947 WD |
44 | static int multiplex_in_fd = -1; |
45 | static int multiplex_out_fd = -1; | |
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 | |
a86179f4 | 55 | const char phase_unknown[] = "unknown"; |
805edf9d | 56 | |
98b332ed MP |
57 | /** |
58 | * The connection might be dropped at some point; perhaps because the | |
59 | * remote instance crashed. Just giving the offset on the stream is | |
60 | * not very helpful. So instead we try to make io_phase_name point to | |
61 | * something useful. | |
eca2adb4 | 62 | * |
805edf9d MP |
63 | * For buffered/multiplexed IO these names will be somewhat |
64 | * approximate; perhaps for ease of support we would rather make the | |
65 | * buffer always flush when a single application-level IO finishes. | |
66 | * | |
eca2adb4 MP |
67 | * @todo Perhaps we want some simple stack functionality, but there's |
68 | * no need to overdo it. | |
98b332ed | 69 | **/ |
805edf9d MP |
70 | const char *io_write_phase = phase_unknown; |
71 | const char *io_read_phase = phase_unknown; | |
98b332ed | 72 | |
7a55d06e MP |
73 | /** Ignore EOF errors while reading a module listing if the remote |
74 | version is 24 or less. */ | |
75 | int kludge_around_eof = False; | |
76 | ||
77 | ||
554e0a8d | 78 | static int io_error_fd = -1; |
56014c8c WD |
79 | static int io_filesfrom_f_in = -1; |
80 | static int io_filesfrom_f_out = -1; | |
81 | static char io_filesfrom_buf[2048]; | |
82 | static char *io_filesfrom_bp; | |
83 | static char io_filesfrom_lastchar; | |
84 | static int io_filesfrom_buflen; | |
720b47f2 | 85 | |
9dd891bb | 86 | static void read_loop(int fd, char *buf, size_t len); |
ff41a59f | 87 | |
8d9dc9f9 AT |
88 | static void check_timeout(void) |
89 | { | |
0adb99b9 | 90 | extern int am_server, am_daemon; |
8d9dc9f9 | 91 | time_t t; |
90ba34e2 AT |
92 | |
93 | err_list_push(); | |
3151cbae | 94 | |
8d9dc9f9 AT |
95 | if (!io_timeout) return; |
96 | ||
97 | if (!last_io) { | |
98 | last_io = time(NULL); | |
99 | return; | |
100 | } | |
101 | ||
102 | t = time(NULL); | |
103 | ||
86ffe37f | 104 | if (last_io && io_timeout && (t-last_io) >= io_timeout) { |
0adb99b9 | 105 | if (!am_server && !am_daemon) { |
ce6c7c63 | 106 | rprintf(FERROR,"io timeout after %d seconds - exiting\n", |
0adb99b9 AT |
107 | (int)(t-last_io)); |
108 | } | |
65417579 | 109 | exit_cleanup(RERR_TIMEOUT); |
8d9dc9f9 AT |
110 | } |
111 | } | |
112 | ||
c979dad5 | 113 | /** Setup the fd used to propagate errors */ |
554e0a8d AT |
114 | void io_set_error_fd(int fd) |
115 | { | |
116 | io_error_fd = fd; | |
117 | } | |
118 | ||
880da007 | 119 | /** Read some data from the error fd and write it to the write log code */ |
554e0a8d AT |
120 | static void read_error_fd(void) |
121 | { | |
122 | char buf[200]; | |
06ce139f | 123 | size_t n; |
554e0a8d | 124 | int fd = io_error_fd; |
ff41a59f AT |
125 | int tag, len; |
126 | ||
56014c8c WD |
127 | /* io_error_fd is temporarily disabled -- is this meant to |
128 | * prevent indefinite recursion? */ | |
554e0a8d AT |
129 | io_error_fd = -1; |
130 | ||
ff41a59f AT |
131 | read_loop(fd, buf, 4); |
132 | tag = IVAL(buf, 0); | |
133 | ||
134 | len = tag & 0xFFFFFF; | |
135 | tag = tag >> 24; | |
136 | tag -= MPLEX_BASE; | |
137 | ||
138 | while (len) { | |
139 | n = len; | |
3151cbae WD |
140 | if (n > (sizeof buf - 1)) |
141 | n = sizeof buf - 1; | |
ff41a59f AT |
142 | read_loop(fd, buf, n); |
143 | rwrite((enum logcode)tag, buf, n); | |
144 | len -= n; | |
554e0a8d AT |
145 | } |
146 | ||
147 | io_error_fd = fd; | |
148 | } | |
149 | ||
56014c8c WD |
150 | /** |
151 | * When we're the receiver and we have a local --files-from list of names | |
152 | * that needs to be sent over the socket to the sender, we have to do two | |
153 | * things at the same time: send the sender a list of what files we're | |
154 | * processing and read the incoming file+info list from the sender. We do | |
155 | * this by augmenting the read_timeout() function to copy this data. It | |
156 | * uses the io_filesfrom_buf to read a block of data from f_in (when it is | |
157 | * ready, since it might be a pipe) and then blast it out f_out (when it | |
158 | * is ready to receive more data). | |
159 | */ | |
160 | void io_set_filesfrom_fds(int f_in, int f_out) | |
161 | { | |
162 | io_filesfrom_f_in = f_in; | |
163 | io_filesfrom_f_out = f_out; | |
164 | io_filesfrom_bp = io_filesfrom_buf; | |
165 | io_filesfrom_lastchar = '\0'; | |
166 | io_filesfrom_buflen = 0; | |
167 | } | |
720b47f2 | 168 | |
880da007 MP |
169 | /** |
170 | * It's almost always an error to get an EOF when we're trying to read | |
171 | * from the network, because the protocol is self-terminating. | |
172 | * | |
173 | * However, there is one unfortunate cases where it is not, which is | |
174 | * rsync <2.4.6 sending a list of modules on a server, since the list | |
175 | * is terminated by closing the socket. So, for the section of the | |
176 | * program where that is a problem (start_socket_client), | |
177 | * kludge_around_eof is True and we just exit. | |
178 | */ | |
3151cbae | 179 | static void whine_about_eof(void) |
7a55d06e | 180 | { |
7a55d06e | 181 | if (kludge_around_eof) |
3151cbae | 182 | exit_cleanup(0); |
7a55d06e | 183 | else { |
3151cbae WD |
184 | rprintf(FERROR, |
185 | "%s: connection unexpectedly closed " | |
186 | "(%.0f bytes read so far)\n", | |
187 | RSYNC_NAME, (double)stats.total_read); | |
188 | ||
189 | exit_cleanup(RERR_STREAMIO); | |
7a55d06e MP |
190 | } |
191 | } | |
720b47f2 | 192 | |
7a55d06e | 193 | |
3151cbae | 194 | static void die_from_readerr(int err) |
7a55d06e MP |
195 | { |
196 | /* this prevents us trying to write errors on a dead socket */ | |
197 | io_multiplexing_close(); | |
3151cbae | 198 | |
7a55d06e | 199 | rprintf(FERROR, "%s: read error: %s\n", |
3151cbae | 200 | RSYNC_NAME, strerror(err)); |
7a55d06e MP |
201 | exit_cleanup(RERR_STREAMIO); |
202 | } | |
203 | ||
204 | ||
880da007 | 205 | /** |
c3563c46 MP |
206 | * Read from a socket with IO timeout. return the number of bytes |
207 | * read. If no bytes can be read then exit, never return a number <= 0. | |
208 | * | |
8886f8d0 MP |
209 | * TODO: If the remote shell connection fails, then current versions |
210 | * actually report an "unexpected EOF" error here. Since it's a | |
211 | * fairly common mistake to try to use rsh when ssh is required, we | |
212 | * should trap that: if we fail to read any data at all, we should | |
213 | * give a better explanation. We can tell whether the connection has | |
214 | * started by looking e.g. at whether the remote version is known yet. | |
c3563c46 | 215 | */ |
3151cbae | 216 | static int read_timeout(int fd, char *buf, size_t len) |
8d9dc9f9 | 217 | { |
4c36ddbe AT |
218 | int n, ret=0; |
219 | ||
ea2111d1 AT |
220 | io_flush(); |
221 | ||
4c36ddbe | 222 | while (ret == 0) { |
7a55d06e | 223 | /* until we manage to read *something* */ |
56014c8c | 224 | fd_set r_fds, w_fds; |
4c36ddbe | 225 | struct timeval tv; |
554e0a8d | 226 | int fd_count = fd+1; |
a57873b7 | 227 | int count; |
4c36ddbe | 228 | |
56014c8c WD |
229 | FD_ZERO(&r_fds); |
230 | FD_SET(fd, &r_fds); | |
3309507d | 231 | if (io_error_fd >= 0) { |
56014c8c WD |
232 | FD_SET(io_error_fd, &r_fds); |
233 | if (io_error_fd >= fd_count) fd_count = io_error_fd+1; | |
234 | } | |
3309507d | 235 | if (io_filesfrom_f_out >= 0) { |
56014c8c WD |
236 | int new_fd; |
237 | if (io_filesfrom_buflen == 0) { | |
3309507d | 238 | if (io_filesfrom_f_in >= 0) { |
56014c8c WD |
239 | FD_SET(io_filesfrom_f_in, &r_fds); |
240 | new_fd = io_filesfrom_f_in; | |
241 | } else { | |
242 | io_filesfrom_f_out = -1; | |
243 | new_fd = -1; | |
244 | } | |
245 | } else { | |
246 | FD_ZERO(&w_fds); | |
247 | FD_SET(io_filesfrom_f_out, &w_fds); | |
248 | new_fd = io_filesfrom_f_out; | |
249 | } | |
250 | if (new_fd >= fd_count) fd_count = new_fd+1; | |
554e0a8d AT |
251 | } |
252 | ||
8cd9fd4e | 253 | tv.tv_sec = io_timeout?io_timeout:SELECT_TIMEOUT; |
4c36ddbe AT |
254 | tv.tv_usec = 0; |
255 | ||
554e0a8d AT |
256 | errno = 0; |
257 | ||
56014c8c WD |
258 | count = select(fd_count, &r_fds, |
259 | io_filesfrom_buflen? &w_fds : NULL, | |
260 | NULL, &tv); | |
a57873b7 AT |
261 | |
262 | if (count == 0) { | |
263 | check_timeout(); | |
264 | } | |
265 | ||
266 | if (count <= 0) { | |
554e0a8d AT |
267 | if (errno == EBADF) { |
268 | exit_cleanup(RERR_SOCKETIO); | |
269 | } | |
4c36ddbe AT |
270 | continue; |
271 | } | |
272 | ||
3309507d | 273 | if (io_error_fd >= 0 && FD_ISSET(io_error_fd, &r_fds)) { |
554e0a8d AT |
274 | read_error_fd(); |
275 | } | |
276 | ||
3309507d | 277 | if (io_filesfrom_f_out >= 0) { |
56014c8c WD |
278 | if (io_filesfrom_buflen) { |
279 | if (FD_ISSET(io_filesfrom_f_out, &w_fds)) { | |
280 | int l = write(io_filesfrom_f_out, | |
281 | io_filesfrom_bp, | |
282 | io_filesfrom_buflen); | |
283 | if (l > 0) { | |
284 | if (!(io_filesfrom_buflen -= l)) | |
285 | io_filesfrom_bp = io_filesfrom_buf; | |
286 | else | |
287 | io_filesfrom_bp += l; | |
288 | } else { | |
289 | /* XXX should we complain? */ | |
290 | io_filesfrom_f_out = -1; | |
291 | } | |
292 | } | |
3309507d | 293 | } else if (io_filesfrom_f_in >= 0) { |
56014c8c WD |
294 | if (FD_ISSET(io_filesfrom_f_in, &r_fds)) { |
295 | int l = read(io_filesfrom_f_in, | |
296 | io_filesfrom_buf, | |
297 | sizeof io_filesfrom_buf); | |
298 | if (l <= 0) { | |
299 | /* Send end-of-file marker */ | |
300 | io_filesfrom_buf[0] = '\0'; | |
301 | io_filesfrom_buf[1] = '\0'; | |
302 | io_filesfrom_buflen = io_filesfrom_lastchar? 2 : 1; | |
303 | io_filesfrom_f_in = -1; | |
304 | } else { | |
305 | extern int eol_nulls; | |
306 | if (!eol_nulls) { | |
307 | char *s = io_filesfrom_buf + l; | |
308 | /* Transform CR and/or LF into '\0' */ | |
309 | while (s-- > io_filesfrom_buf) { | |
310 | if (*s == '\n' || *s == '\r') | |
311 | *s = '\0'; | |
312 | } | |
313 | } | |
314 | if (!io_filesfrom_lastchar) { | |
315 | /* Last buf ended with a '\0', so don't | |
316 | * let this buf start with one. */ | |
317 | while (l && !*io_filesfrom_bp) | |
318 | io_filesfrom_bp++, l--; | |
319 | } | |
320 | if (!l) | |
321 | io_filesfrom_bp = io_filesfrom_buf; | |
322 | else { | |
323 | char *f = io_filesfrom_bp; | |
324 | char *t = f; | |
325 | char *eob = f + l; | |
326 | /* Eliminate any multi-'\0' runs. */ | |
327 | while (f != eob) { | |
328 | if (!(*t++ = *f++)) { | |
329 | while (f != eob && !*f) | |
330 | f++, l--; | |
331 | } | |
332 | } | |
333 | io_filesfrom_lastchar = f[-1]; | |
334 | } | |
335 | io_filesfrom_buflen = l; | |
336 | } | |
337 | } | |
338 | } | |
339 | } | |
340 | ||
341 | if (!FD_ISSET(fd, &r_fds)) continue; | |
554e0a8d | 342 | |
4c36ddbe AT |
343 | n = read(fd, buf, len); |
344 | ||
8d9dc9f9 AT |
345 | if (n > 0) { |
346 | buf += n; | |
347 | len -= n; | |
4c36ddbe AT |
348 | ret += n; |
349 | if (io_timeout) | |
350 | last_io = time(NULL); | |
351 | continue; | |
7a55d06e | 352 | } else if (n == 0) { |
3151cbae | 353 | whine_about_eof(); |
7a55d06e | 354 | return -1; /* doesn't return */ |
3309507d | 355 | } else if (n < 0) { |
7a55d06e MP |
356 | if (errno == EINTR || errno == EWOULDBLOCK || |
357 | errno == EAGAIN) | |
358 | continue; | |
3151cbae | 359 | die_from_readerr(errno); |
8d9dc9f9 | 360 | } |
4c36ddbe | 361 | } |
8d9dc9f9 | 362 | |
4c36ddbe AT |
363 | return ret; |
364 | } | |
8d9dc9f9 | 365 | |
56014c8c WD |
366 | /** |
367 | * Read a line into the "fname" buffer (which must be at least MAXPATHLEN | |
368 | * characters long). | |
369 | */ | |
370 | int read_filesfrom_line(int fd, char *fname) | |
371 | { | |
372 | char ch, *s, *eob = fname + MAXPATHLEN - 1; | |
373 | int cnt; | |
374 | extern int io_timeout; | |
375 | extern int eol_nulls; | |
376 | extern char *remote_filesfrom_file; | |
55d5937d | 377 | int reading_remotely = remote_filesfrom_file != NULL; |
56014c8c WD |
378 | int nulls = eol_nulls || reading_remotely; |
379 | ||
380 | start: | |
381 | s = fname; | |
382 | while (1) { | |
383 | cnt = read(fd, &ch, 1); | |
384 | if (cnt < 0 && (errno == EWOULDBLOCK | |
385 | || errno == EINTR || errno == EAGAIN)) { | |
386 | struct timeval tv; | |
387 | fd_set fds; | |
388 | FD_ZERO(&fds); | |
389 | FD_SET(fd, &fds); | |
390 | tv.tv_sec = io_timeout? io_timeout : SELECT_TIMEOUT; | |
391 | tv.tv_usec = 0; | |
392 | if (!select(fd+1, &fds, NULL, NULL, &tv)) | |
393 | check_timeout(); | |
394 | continue; | |
395 | } | |
396 | if (cnt != 1) | |
397 | break; | |
398 | if (nulls? !ch : (ch == '\r' || ch == '\n')) { | |
399 | /* Skip empty lines if reading locally. */ | |
400 | if (!reading_remotely && s == fname) | |
401 | continue; | |
402 | break; | |
403 | } | |
404 | if (s < eob) | |
405 | *s++ = ch; | |
406 | } | |
407 | *s = '\0'; | |
7a55d06e | 408 | |
6b45fcf1 WD |
409 | /* Dump comments. */ |
410 | if (*fname == '#' || *fname == ';') | |
56014c8c WD |
411 | goto start; |
412 | ||
413 | return s - fname; | |
414 | } | |
7a55d06e MP |
415 | |
416 | ||
880da007 MP |
417 | /** |
418 | * Continue trying to read len bytes - don't return until len has been | |
419 | * read. | |
420 | **/ | |
3151cbae | 421 | static void read_loop(int fd, char *buf, size_t len) |
4c36ddbe AT |
422 | { |
423 | while (len) { | |
424 | int n = read_timeout(fd, buf, len); | |
425 | ||
426 | buf += n; | |
427 | len -= n; | |
8d9dc9f9 AT |
428 | } |
429 | } | |
430 | ||
7a55d06e MP |
431 | |
432 | /** | |
433 | * Read from the file descriptor handling multiplexing - return number | |
434 | * of bytes read. | |
435 | * | |
436 | * Never returns <= 0. | |
437 | */ | |
9dd891bb | 438 | static int read_unbuffered(int fd, char *buf, size_t len) |
8d9dc9f9 | 439 | { |
6fe25398 | 440 | static size_t remaining; |
909ce14f | 441 | int tag, ret = 0; |
8d9dc9f9 | 442 | char line[1024]; |
76c21947 WD |
443 | static char *buffer; |
444 | static size_t bufferIdx = 0; | |
445 | static size_t bufferSz; | |
8d9dc9f9 | 446 | |
76c21947 | 447 | if (fd != multiplex_in_fd) |
4c36ddbe | 448 | return read_timeout(fd, buf, len); |
8d9dc9f9 | 449 | |
76c21947 WD |
450 | if (!io_multiplexing_in && remaining == 0) { |
451 | if (!buffer) { | |
452 | bufferSz = 2 * IO_BUFFER_SIZE; | |
453 | buffer = new_array(char, bufferSz); | |
454 | if (!buffer) out_of_memory("read_unbuffered"); | |
455 | } | |
456 | remaining = read_timeout(fd, buffer, bufferSz); | |
457 | bufferIdx = 0; | |
458 | } | |
459 | ||
8d9dc9f9 AT |
460 | while (ret == 0) { |
461 | if (remaining) { | |
462 | len = MIN(len, remaining); | |
76c21947 WD |
463 | memcpy(buf, buffer + bufferIdx, len); |
464 | bufferIdx += len; | |
8d9dc9f9 AT |
465 | remaining -= len; |
466 | ret = len; | |
76c21947 | 467 | break; |
8d9dc9f9 AT |
468 | } |
469 | ||
909ce14f | 470 | read_loop(fd, line, 4); |
ff41a59f | 471 | tag = IVAL(line, 0); |
679e7657 | 472 | |
8d9dc9f9 AT |
473 | remaining = tag & 0xFFFFFF; |
474 | tag = tag >> 24; | |
475 | ||
76c21947 WD |
476 | if (tag == MPLEX_BASE) { |
477 | if (!buffer || remaining > bufferSz) { | |
478 | buffer = realloc_array(buffer, char, remaining); | |
479 | if (!buffer) out_of_memory("read_unbuffered"); | |
480 | bufferSz = remaining; | |
481 | } | |
482 | read_loop(fd, buffer, remaining); | |
483 | bufferIdx = 0; | |
909ce14f | 484 | continue; |
76c21947 | 485 | } |
8d9dc9f9 AT |
486 | |
487 | tag -= MPLEX_BASE; | |
488 | ||
489 | if (tag != FERROR && tag != FINFO) { | |
909ce14f | 490 | rprintf(FERROR, "unexpected tag %d\n", tag); |
65417579 | 491 | exit_cleanup(RERR_STREAMIO); |
8d9dc9f9 AT |
492 | } |
493 | ||
3151cbae | 494 | if (remaining > sizeof line - 1) { |
8801138b WD |
495 | rprintf(FERROR, "multiplexing overflow %ld\n\n", |
496 | (long)remaining); | |
65417579 | 497 | exit_cleanup(RERR_STREAMIO); |
8d9dc9f9 AT |
498 | } |
499 | ||
500 | read_loop(fd, line, remaining); | |
501 | line[remaining] = 0; | |
502 | ||
909ce14f | 503 | rprintf((enum logcode) tag, "%s", line); |
8d9dc9f9 AT |
504 | remaining = 0; |
505 | } | |
506 | ||
76c21947 WD |
507 | if (remaining == 0) |
508 | io_flush(); | |
509 | ||
8d9dc9f9 AT |
510 | return ret; |
511 | } | |
512 | ||
513 | ||
909ce14f | 514 | |
880da007 MP |
515 | /** |
516 | * Do a buffered read from @p fd. Don't return until all @p n bytes | |
517 | * have been read. If all @p n can't be read then exit with an | |
518 | * error. | |
519 | **/ | |
3151cbae | 520 | static void readfd(int fd, char *buffer, size_t N) |
720b47f2 | 521 | { |
6ba9279f | 522 | int ret; |
06ce139f | 523 | size_t total=0; |
3151cbae | 524 | |
6ba9279f | 525 | while (total < N) { |
3151cbae | 526 | ret = read_unbuffered(fd, buffer + total, N-total); |
6ba9279f | 527 | total += ret; |
7f28dbee | 528 | } |
1b7c47cb AT |
529 | |
530 | stats.total_read += total; | |
720b47f2 AT |
531 | } |
532 | ||
533 | ||
b7922338 | 534 | int32 read_int(int f) |
720b47f2 | 535 | { |
4c36ddbe | 536 | char b[4]; |
d730b113 AT |
537 | int32 ret; |
538 | ||
4c36ddbe | 539 | readfd(f,b,4); |
d730b113 AT |
540 | ret = IVAL(b,0); |
541 | if (ret == (int32)0xffffffff) return -1; | |
542 | return ret; | |
720b47f2 AT |
543 | } |
544 | ||
71c46176 | 545 | int64 read_longint(int f) |
3a6a366f | 546 | { |
71c46176 | 547 | int64 ret; |
3a6a366f AT |
548 | char b[8]; |
549 | ret = read_int(f); | |
71c46176 | 550 | |
8de330a3 AT |
551 | if ((int32)ret != (int32)0xffffffff) { |
552 | return ret; | |
553 | } | |
71c46176 | 554 | |
3bee6733 | 555 | #ifdef NO_INT64 |
9486289c | 556 | rprintf(FERROR,"Integer overflow - attempted 64 bit offset\n"); |
65417579 | 557 | exit_cleanup(RERR_UNSUPPORTED); |
71c46176 | 558 | #else |
91c4da3f S |
559 | readfd(f,b,8); |
560 | ret = IVAL(b,0) | (((int64)IVAL(b,4))<<32); | |
71c46176 AT |
561 | #endif |
562 | ||
3a6a366f AT |
563 | return ret; |
564 | } | |
565 | ||
9dd891bb | 566 | void read_buf(int f,char *buf,size_t len) |
720b47f2 | 567 | { |
4c36ddbe | 568 | readfd(f,buf,len); |
720b47f2 AT |
569 | } |
570 | ||
9dd891bb | 571 | void read_sbuf(int f,char *buf,size_t len) |
575f2fca | 572 | { |
3151cbae | 573 | read_buf(f,buf,len); |
575f2fca AT |
574 | buf[len] = 0; |
575 | } | |
576 | ||
182dca5c AT |
577 | unsigned char read_byte(int f) |
578 | { | |
4c36ddbe | 579 | unsigned char c; |
3151cbae | 580 | read_buf(f, (char *)&c, 1); |
4c36ddbe | 581 | return c; |
182dca5c | 582 | } |
720b47f2 | 583 | |
880da007 | 584 | |
08571358 MP |
585 | /** |
586 | * Sleep after writing to limit I/O bandwidth usage. | |
587 | * | |
588 | * @todo Rather than sleeping after each write, it might be better to | |
589 | * use some kind of averaging. The current algorithm seems to always | |
590 | * use a bit less bandwidth than specified, because it doesn't make up | |
591 | * for slow periods. But arguably this is a feature. In addition, we | |
592 | * ought to take the time used to write the data into account. | |
593 | **/ | |
594 | static void sleep_for_bwlimit(int bytes_written) | |
595 | { | |
596 | struct timeval tv; | |
597 | ||
598 | if (!bwlimit) | |
599 | return; | |
e681e820 MP |
600 | |
601 | assert(bytes_written > 0); | |
602 | assert(bwlimit > 0); | |
3151cbae | 603 | |
08571358 | 604 | tv.tv_usec = bytes_written * 1000 / bwlimit; |
e681e820 MP |
605 | tv.tv_sec = tv.tv_usec / 1000000; |
606 | tv.tv_usec = tv.tv_usec % 1000000; | |
08571358 | 607 | |
98b332ed | 608 | select(0, NULL, NULL, NULL, &tv); |
08571358 MP |
609 | } |
610 | ||
611 | ||
880da007 MP |
612 | /** |
613 | * Write len bytes to the file descriptor @p fd. | |
614 | * | |
615 | * This function underlies the multiplexing system. The body of the | |
616 | * application never calls this function directly. | |
617 | **/ | |
9dd891bb | 618 | static void writefd_unbuffered(int fd,char *buf,size_t len) |
720b47f2 | 619 | { |
06ce139f | 620 | size_t total = 0; |
8d9dc9f9 | 621 | fd_set w_fds, r_fds; |
4c36ddbe | 622 | int fd_count, count; |
8d9dc9f9 | 623 | struct timeval tv; |
720b47f2 | 624 | |
90ba34e2 AT |
625 | err_list_push(); |
626 | ||
e44f9a12 AT |
627 | no_flush++; |
628 | ||
4c36ddbe | 629 | while (total < len) { |
8d9dc9f9 | 630 | FD_ZERO(&w_fds); |
8d9dc9f9 | 631 | FD_SET(fd,&w_fds); |
554e0a8d | 632 | fd_count = fd; |
4c36ddbe | 633 | |
3309507d | 634 | if (io_error_fd >= 0) { |
56014c8c | 635 | FD_ZERO(&r_fds); |
554e0a8d AT |
636 | FD_SET(io_error_fd,&r_fds); |
637 | if (io_error_fd > fd_count) | |
638 | fd_count = io_error_fd; | |
8d9dc9f9 AT |
639 | } |
640 | ||
8cd9fd4e | 641 | tv.tv_sec = io_timeout?io_timeout:SELECT_TIMEOUT; |
8d9dc9f9 | 642 | tv.tv_usec = 0; |
4c36ddbe | 643 | |
554e0a8d AT |
644 | errno = 0; |
645 | ||
646 | count = select(fd_count+1, | |
3309507d | 647 | io_error_fd >= 0?&r_fds:NULL, |
4c36ddbe | 648 | &w_fds,NULL, |
8cd9fd4e | 649 | &tv); |
4c36ddbe | 650 | |
a57873b7 AT |
651 | if (count == 0) { |
652 | check_timeout(); | |
653 | } | |
654 | ||
4c36ddbe | 655 | if (count <= 0) { |
554e0a8d AT |
656 | if (errno == EBADF) { |
657 | exit_cleanup(RERR_SOCKETIO); | |
658 | } | |
8d9dc9f9 AT |
659 | continue; |
660 | } | |
4c36ddbe | 661 | |
3309507d | 662 | if (io_error_fd >= 0 && FD_ISSET(io_error_fd, &r_fds)) { |
554e0a8d AT |
663 | read_error_fd(); |
664 | } | |
665 | ||
8d9dc9f9 | 666 | if (FD_ISSET(fd, &w_fds)) { |
06ce139f MP |
667 | int ret; |
668 | size_t n = len-total; | |
f0359dd0 | 669 | ret = write(fd,buf+total,n); |
4c36ddbe | 670 | |
3309507d WD |
671 | if (ret < 0) { |
672 | if (errno == EINTR) | |
673 | continue; | |
674 | if (errno == EWOULDBLOCK || errno == EAGAIN) { | |
675 | msleep(1); | |
676 | continue; | |
677 | } | |
f0359dd0 AT |
678 | } |
679 | ||
4c36ddbe | 680 | if (ret <= 0) { |
befbfe61 MP |
681 | /* Don't try to write errors back |
682 | * across the stream */ | |
683 | io_multiplexing_close(); | |
3ce0f9a6 | 684 | rprintf(FERROR, RSYNC_NAME |
98b332ed | 685 | ": writefd_unbuffered failed to write %ld bytes: phase \"%s\": %s\n", |
eca2adb4 | 686 | (long) len, io_write_phase, |
ce6c7c63 | 687 | strerror(errno)); |
65417579 | 688 | exit_cleanup(RERR_STREAMIO); |
4c36ddbe AT |
689 | } |
690 | ||
08571358 | 691 | sleep_for_bwlimit(ret); |
ef5d23eb | 692 | |
4c36ddbe | 693 | total += ret; |
a800434a | 694 | |
4c36ddbe AT |
695 | if (io_timeout) |
696 | last_io = time(NULL); | |
8d9dc9f9 | 697 | } |
4c36ddbe | 698 | } |
e44f9a12 AT |
699 | |
700 | no_flush--; | |
720b47f2 AT |
701 | } |
702 | ||
8d9dc9f9 | 703 | |
d6dead6b AT |
704 | static char *io_buffer; |
705 | static int io_buffer_count; | |
706 | ||
76c21947 | 707 | void io_start_buffering_out(int fd) |
d6dead6b | 708 | { |
8d9dc9f9 | 709 | if (io_buffer) return; |
679e7657 | 710 | multiplex_out_fd = fd; |
58cadc86 | 711 | io_buffer = new_array(char, IO_BUFFER_SIZE); |
d6dead6b AT |
712 | if (!io_buffer) out_of_memory("writefd"); |
713 | io_buffer_count = 0; | |
ff41a59f AT |
714 | } |
715 | ||
76c21947 WD |
716 | void io_start_buffering_in(int fd) |
717 | { | |
718 | multiplex_in_fd = fd; | |
719 | } | |
720 | ||
880da007 MP |
721 | /** |
722 | * Write an message to a multiplexed stream. If this fails then rsync | |
723 | * exits. | |
724 | **/ | |
9dd891bb | 725 | static void mplex_write(int fd, enum logcode code, char *buf, size_t len) |
ff41a59f AT |
726 | { |
727 | char buffer[4096]; | |
06ce139f | 728 | size_t n = len; |
8d9dc9f9 | 729 | |
ff41a59f AT |
730 | SIVAL(buffer, 0, ((MPLEX_BASE + (int)code)<<24) + len); |
731 | ||
3151cbae WD |
732 | if (n > (sizeof buffer - 4)) { |
733 | n = sizeof buffer - 4; | |
ff41a59f AT |
734 | } |
735 | ||
736 | memcpy(&buffer[4], buf, n); | |
737 | writefd_unbuffered(fd, buffer, n+4); | |
738 | ||
739 | len -= n; | |
740 | buf += n; | |
741 | ||
6d7b6081 AT |
742 | if (len) { |
743 | writefd_unbuffered(fd, buf, len); | |
744 | } | |
d6dead6b AT |
745 | } |
746 | ||
ff41a59f | 747 | |
8d9dc9f9 | 748 | void io_flush(void) |
d6dead6b | 749 | { |
679e7657 | 750 | int fd = multiplex_out_fd; |
90ba34e2 AT |
751 | |
752 | err_list_push(); | |
753 | ||
e44f9a12 | 754 | if (!io_buffer_count || no_flush) return; |
8d9dc9f9 AT |
755 | |
756 | if (io_multiplexing_out) { | |
0f3203c3 | 757 | mplex_write(fd, FNONE, io_buffer, io_buffer_count); |
8d9dc9f9 | 758 | } else { |
4c36ddbe | 759 | writefd_unbuffered(fd, io_buffer, io_buffer_count); |
d6dead6b | 760 | } |
8d9dc9f9 AT |
761 | io_buffer_count = 0; |
762 | } | |
763 | ||
0ba48136 | 764 | |
7b5c3eb0 | 765 | void io_end_buffering(void) |
8d9dc9f9 AT |
766 | { |
767 | io_flush(); | |
768 | if (!io_multiplexing_out) { | |
ff41a59f | 769 | free(io_buffer); |
8d9dc9f9 AT |
770 | io_buffer = NULL; |
771 | } | |
d6dead6b AT |
772 | } |
773 | ||
9dd891bb | 774 | static void writefd(int fd,char *buf,size_t len) |
d6dead6b | 775 | { |
1b7c47cb AT |
776 | stats.total_written += len; |
777 | ||
90ba34e2 AT |
778 | err_list_push(); |
779 | ||
554e0a8d | 780 | if (!io_buffer || fd != multiplex_out_fd) { |
4c36ddbe AT |
781 | writefd_unbuffered(fd, buf, len); |
782 | return; | |
783 | } | |
d6dead6b AT |
784 | |
785 | while (len) { | |
7b5c3eb0 | 786 | int n = MIN((int) len, IO_BUFFER_SIZE-io_buffer_count); |
d6dead6b AT |
787 | if (n > 0) { |
788 | memcpy(io_buffer+io_buffer_count, buf, n); | |
789 | buf += n; | |
790 | len -= n; | |
791 | io_buffer_count += n; | |
792 | } | |
3151cbae | 793 | |
8d9dc9f9 | 794 | if (io_buffer_count == IO_BUFFER_SIZE) io_flush(); |
d6dead6b | 795 | } |
d6dead6b | 796 | } |
720b47f2 AT |
797 | |
798 | ||
b7922338 | 799 | void write_int(int f,int32 x) |
720b47f2 | 800 | { |
8d9dc9f9 AT |
801 | char b[4]; |
802 | SIVAL(b,0,x); | |
4c36ddbe | 803 | writefd(f,b,4); |
720b47f2 AT |
804 | } |
805 | ||
7a24c346 | 806 | |
805edf9d MP |
807 | void write_int_named(int f, int32 x, const char *phase) |
808 | { | |
809 | io_write_phase = phase; | |
810 | write_int(f, x); | |
811 | io_write_phase = phase_unknown; | |
812 | } | |
813 | ||
814 | ||
7a24c346 MP |
815 | /* |
816 | * Note: int64 may actually be a 32-bit type if ./configure couldn't find any | |
817 | * 64-bit types on this platform. | |
818 | */ | |
71c46176 | 819 | void write_longint(int f, int64 x) |
3a6a366f | 820 | { |
3a6a366f | 821 | char b[8]; |
3a6a366f | 822 | |
91c4da3f | 823 | if (x <= 0x7FFFFFFF) { |
3a6a366f AT |
824 | write_int(f, (int)x); |
825 | return; | |
826 | } | |
827 | ||
67863f46 S |
828 | #ifdef NO_INT64 |
829 | rprintf(FERROR,"Integer overflow - attempted 64 bit offset\n"); | |
830 | exit_cleanup(RERR_UNSUPPORTED); | |
831 | #else | |
8de330a3 | 832 | write_int(f, (int32)0xFFFFFFFF); |
3a6a366f AT |
833 | SIVAL(b,0,(x&0xFFFFFFFF)); |
834 | SIVAL(b,4,((x>>32)&0xFFFFFFFF)); | |
835 | ||
4c36ddbe | 836 | writefd(f,b,8); |
67863f46 | 837 | #endif |
3a6a366f AT |
838 | } |
839 | ||
9dd891bb | 840 | void write_buf(int f,char *buf,size_t len) |
720b47f2 | 841 | { |
4c36ddbe | 842 | writefd(f,buf,len); |
720b47f2 AT |
843 | } |
844 | ||
880da007 | 845 | /** Write a string to the connection */ |
6e4fb64e | 846 | static void write_sbuf(int f,char *buf) |
f0fca04e AT |
847 | { |
848 | write_buf(f, buf, strlen(buf)); | |
849 | } | |
850 | ||
720b47f2 | 851 | |
182dca5c AT |
852 | void write_byte(int f,unsigned char c) |
853 | { | |
f0fca04e | 854 | write_buf(f,(char *)&c,1); |
182dca5c AT |
855 | } |
856 | ||
7a55d06e MP |
857 | |
858 | ||
914cc65c MP |
859 | /** |
860 | * Read a line of up to @p maxlen characters into @p buf. Does not | |
861 | * contain a trailing newline or carriage return. | |
862 | * | |
863 | * @return 1 for success; 0 for io error or truncation. | |
864 | **/ | |
9dd891bb | 865 | int read_line(int f, char *buf, size_t maxlen) |
f0fca04e AT |
866 | { |
867 | while (maxlen) { | |
528bfcd7 | 868 | buf[0] = 0; |
f0fca04e | 869 | read_buf(f, buf, 1); |
914cc65c MP |
870 | if (buf[0] == 0) |
871 | return 0; | |
f0fca04e AT |
872 | if (buf[0] == '\n') { |
873 | buf[0] = 0; | |
874 | break; | |
875 | } | |
876 | if (buf[0] != '\r') { | |
877 | buf++; | |
878 | maxlen--; | |
879 | } | |
880 | } | |
881 | if (maxlen == 0) { | |
882 | *buf = 0; | |
883 | return 0; | |
884 | } | |
528bfcd7 | 885 | |
f0fca04e AT |
886 | return 1; |
887 | } | |
888 | ||
889 | ||
890 | void io_printf(int fd, const char *format, ...) | |
891 | { | |
892 | va_list ap; | |
893 | char buf[1024]; | |
894 | int len; | |
3151cbae | 895 | |
f0fca04e | 896 | va_start(ap, format); |
3151cbae | 897 | len = vsnprintf(buf, sizeof buf, format, ap); |
f0fca04e AT |
898 | va_end(ap); |
899 | ||
65417579 | 900 | if (len < 0) exit_cleanup(RERR_STREAMIO); |
f0fca04e AT |
901 | |
902 | write_sbuf(fd, buf); | |
903 | } | |
8d9dc9f9 AT |
904 | |
905 | ||
880da007 | 906 | /** Setup for multiplexing an error stream with the data stream */ |
8d9dc9f9 AT |
907 | void io_start_multiplex_out(int fd) |
908 | { | |
679e7657 AT |
909 | multiplex_out_fd = fd; |
910 | io_flush(); | |
76c21947 | 911 | io_start_buffering_out(fd); |
8d9dc9f9 AT |
912 | io_multiplexing_out = 1; |
913 | } | |
914 | ||
880da007 | 915 | /** Setup for multiplexing an error stream with the data stream */ |
8d9dc9f9 AT |
916 | void io_start_multiplex_in(int fd) |
917 | { | |
679e7657 AT |
918 | multiplex_in_fd = fd; |
919 | io_flush(); | |
8d9dc9f9 AT |
920 | io_multiplexing_in = 1; |
921 | } | |
922 | ||
880da007 | 923 | /** Write an message to the multiplexed error stream */ |
9dd891bb | 924 | int io_multiplex_write(enum logcode code, char *buf, size_t len) |
8d9dc9f9 AT |
925 | { |
926 | if (!io_multiplexing_out) return 0; | |
927 | ||
928 | io_flush(); | |
1b7c47cb | 929 | stats.total_written += (len+4); |
ff41a59f | 930 | mplex_write(multiplex_out_fd, code, buf, len); |
8d9dc9f9 AT |
931 | return 1; |
932 | } | |
933 | ||
880da007 | 934 | /** Stop output multiplexing */ |
554e0a8d AT |
935 | void io_multiplexing_close(void) |
936 | { | |
937 | io_multiplexing_out = 0; | |
938 | } | |
939 |