Commit | Line | Data |
---|---|---|
0f78b815 WD |
1 | /* |
2 | * Socket and pipe I/O utilities used in rsync. | |
d62bcc17 | 3 | * |
0f78b815 WD |
4 | * Copyright (C) 1996-2001 Andrew Tridgell |
5 | * Copyright (C) 1996 Paul Mackerras | |
6 | * Copyright (C) 2001, 2002 Martin Pool <mbp@samba.org> | |
b3bf9b9d | 7 | * Copyright (C) 2003-2009 Wayne Davison |
d62bcc17 | 8 | * |
880da007 | 9 | * This program is free software; you can redistribute it and/or modify |
8e41b68e WD |
10 | * it under the terms of the GNU General Public License as published by |
11 | * the Free Software Foundation; either version 3 of the License, or | |
12 | * (at your option) any later version. | |
d62bcc17 | 13 | * |
880da007 MP |
14 | * This program is distributed in the hope that it will be useful, |
15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
17 | * GNU General Public License for more details. | |
d62bcc17 | 18 | * |
e7c67065 | 19 | * You should have received a copy of the GNU General Public License along |
4fd842f9 | 20 | * with this program; if not, visit the http://fsf.org website. |
880da007 | 21 | */ |
720b47f2 | 22 | |
0f78b815 WD |
23 | /* Rsync provides its own multiplexing system, which is used to send |
24 | * stderr and stdout over a single socket. | |
87ee2481 MP |
25 | * |
26 | * For historical reasons this is off during the start of the | |
27 | * connection, but it's switched on quite early using | |
0f78b815 | 28 | * io_start_multiplex_out() and io_start_multiplex_in(). */ |
720b47f2 | 29 | |
720b47f2 | 30 | #include "rsync.h" |
1b42f628 | 31 | #include "ifuncs.h" |
5dd14f0c | 32 | #include "inums.h" |
720b47f2 | 33 | |
880da007 | 34 | /** If no timeout is specified then use a 60 second select timeout */ |
8cd9fd4e AT |
35 | #define SELECT_TIMEOUT 60 |
36 | ||
7a55d06e | 37 | extern int bwlimit; |
71e58630 | 38 | extern size_t bwlimit_writemax; |
6ba9279f | 39 | extern int io_timeout; |
d17e1dd2 WD |
40 | extern int am_server; |
41 | extern int am_daemon; | |
42 | extern int am_sender; | |
98f8c9a5 | 43 | extern int am_generator; |
20caffd2 | 44 | extern int msgs2stderr; |
3ea6e0e7 | 45 | extern int inc_recurse; |
8ef246e0 | 46 | extern int io_error; |
e626b29e | 47 | extern int eol_nulls; |
8ef246e0 | 48 | extern int flist_eof; |
ce827c3e WD |
49 | extern int file_total; |
50 | extern int file_old_total; | |
7f9bf6b7 | 51 | extern int list_only; |
3b0a30eb | 52 | extern int read_batch; |
ba525f77 | 53 | extern int protect_args; |
b9f592fb WD |
54 | extern int checksum_seed; |
55 | extern int protocol_version; | |
47c11975 | 56 | extern int remove_source_files; |
cdf236aa | 57 | extern int preserve_hard_links; |
7a7810aa | 58 | extern BOOL extra_flist_sending_enabled; |
a800434a | 59 | extern struct stats stats; |
19531e1f | 60 | extern struct file_list *cur_flist; |
332cf6df | 61 | #ifdef ICONV_OPTION |
ba525f77 | 62 | extern int filesfrom_convert; |
332cf6df WD |
63 | extern iconv_t ic_send, ic_recv; |
64 | #endif | |
720b47f2 | 65 | |
93465f51 WD |
66 | int csum_length = SHORT_SUM_LENGTH; /* initial value */ |
67 | int allowed_lull = 0; | |
9e2409ab | 68 | int ignore_timeout = 0; |
b9f592fb | 69 | int batch_fd = -1; |
04c722d5 | 70 | int msgdone_cnt = 0; |
20caffd2 | 71 | int forward_flist_data = 0; |
805edf9d | 72 | |
cb2e1f18 | 73 | /* Ignore an EOF error if non-zero. See whine_about_eof(). */ |
574c2500 | 74 | int kluge_around_eof = 0; |
7a55d06e | 75 | |
cdf236aa WD |
76 | int sock_f_in = -1; |
77 | int sock_f_out = -1; | |
7a55d06e | 78 | |
4dde3347 WD |
79 | int64 total_data_read = 0; |
80 | int64 total_data_written = 0; | |
81 | ||
20caffd2 WD |
82 | static struct { |
83 | xbuf in, out, msg; | |
84 | int in_fd; | |
85 | int out_fd; /* Both "out" and "msg" go to this fd. */ | |
86 | BOOL in_multiplexed; | |
87 | unsigned out_empty_len; | |
88 | size_t raw_data_header_pos; /* in the out xbuf */ | |
89 | size_t raw_flushing_ends_before; /* in the out xbuf */ | |
90 | size_t raw_input_ends_before; /* in the in xbuf */ | |
91 | } iobuf = { .in_fd = -1, .out_fd = -1 }; | |
8ef246e0 | 92 | |
3b0a30eb WD |
93 | static time_t last_io_in; |
94 | static time_t last_io_out; | |
1f75bb10 | 95 | |
b9f592fb WD |
96 | static int write_batch_monitor_in = -1; |
97 | static int write_batch_monitor_out = -1; | |
98 | ||
20caffd2 | 99 | static int ff_forward_fd = -1; |
8e6b4ddb WD |
100 | static int ff_reenable_multiplex = -1; |
101 | static char ff_lastchar = '\0'; | |
cbc63a09 | 102 | static xbuf ff_xb = EMPTY_XBUF; |
332cf6df | 103 | #ifdef ICONV_OPTION |
ba525f77 | 104 | static xbuf iconv_buf = EMPTY_XBUF; |
332cf6df | 105 | #endif |
3b0a30eb | 106 | static int select_timeout = SELECT_TIMEOUT; |
d6081c82 WD |
107 | static int active_filecnt = 0; |
108 | static OFF_T active_bytecnt = 0; | |
7f9bf6b7 | 109 | static int first_message = 1; |
720b47f2 | 110 | |
351e23ad WD |
111 | static char int_byte_extra[64] = { |
112 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* (00 - 3F)/4 */ | |
113 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* (40 - 7F)/4 */ | |
114 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* (80 - BF)/4 */ | |
115 | 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 5, 6, /* (C0 - FF)/4 */ | |
482f48cc WD |
116 | }; |
117 | ||
20caffd2 WD |
118 | #define IN_MULTIPLEXED (iobuf.in_multiplexed) |
119 | #define OUT_MULTIPLEXED (iobuf.out_empty_len != 0) | |
29349024 | 120 | |
20caffd2 WD |
121 | #define PIO_NEED_INPUT (1<<0) /* The *_NEED_* flags are mutually exclusive. */ |
122 | #define PIO_NEED_OUTROOM (1<<1) | |
123 | #define PIO_NEED_MSGROOM (1<<2) | |
ff41a59f | 124 | |
20caffd2 | 125 | #define PIO_CONSUME_INPUT (1<<4) /* Must becombined with PIO_NEED_INPUT. */ |
d17e1dd2 | 126 | |
20caffd2 WD |
127 | #define PIO_INPUT_AND_CONSUME (PIO_NEED_INPUT | PIO_CONSUME_INPUT) |
128 | #define PIO_NEED_FLAGS (PIO_NEED_INPUT | PIO_NEED_OUTROOM | PIO_NEED_MSGROOM) | |
d17e1dd2 | 129 | |
20caffd2 WD |
130 | #define REMOTE_OPTION_ERROR "rsync: on remote machine: -" |
131 | #define REMOTE_OPTION_ERROR2 ": unknown option" | |
08c88178 | 132 | |
20caffd2 | 133 | #define FILESFROM_BUFLEN 2048 |
d17e1dd2 | 134 | |
20caffd2 | 135 | enum festatus { FES_SUCCESS, FES_REDO, FES_NO_SEND }; |
29349024 | 136 | |
20caffd2 | 137 | static flist_ndx_list redo_list, hlink_list; |
29349024 | 138 | |
20caffd2 | 139 | static void sleep_for_bwlimit(int bytes_written); |
29349024 | 140 | |
8d9dc9f9 AT |
141 | static void check_timeout(void) |
142 | { | |
143 | time_t t; | |
90ba34e2 | 144 | |
9e2409ab | 145 | if (!io_timeout || ignore_timeout) |
d17e1dd2 | 146 | return; |
8d9dc9f9 | 147 | |
3b0a30eb WD |
148 | if (!last_io_in) { |
149 | last_io_in = time(NULL); | |
8d9dc9f9 AT |
150 | return; |
151 | } | |
152 | ||
153 | t = time(NULL); | |
154 | ||
3b0a30eb | 155 | if (t - last_io_in >= io_timeout) { |
0adb99b9 | 156 | if (!am_server && !am_daemon) { |
4ccfd96c | 157 | rprintf(FERROR, "io timeout after %d seconds -- exiting\n", |
3b0a30eb | 158 | (int)(t-last_io_in)); |
0adb99b9 | 159 | } |
65417579 | 160 | exit_cleanup(RERR_TIMEOUT); |
8d9dc9f9 AT |
161 | } |
162 | } | |
163 | ||
20caffd2 WD |
164 | /* It's almost always an error to get an EOF when we're trying to read from the |
165 | * network, because the protocol is (for the most part) self-terminating. | |
166 | * | |
167 | * There is one case for the receiver when it is at the end of the transfer | |
168 | * (hanging around reading any keep-alive packets that might come its way): if | |
169 | * the sender dies before the generator's kill-signal comes through, we can end | |
170 | * up here needing to loop until the kill-signal arrives. In this situation, | |
171 | * kluge_around_eof will be < 0. | |
172 | * | |
173 | * There is another case for older protocol versions (< 24) where the module | |
174 | * listing was not terminated, so we must ignore an EOF error in that case and | |
175 | * exit. In this situation, kluge_around_eof will be > 0. */ | |
176 | static NORETURN void whine_about_eof(int fd) | |
d17e1dd2 | 177 | { |
20caffd2 WD |
178 | if (kluge_around_eof && fd == sock_f_in) { |
179 | int i; | |
180 | if (kluge_around_eof > 0) | |
181 | exit_cleanup(0); | |
182 | /* If we're still here after 10 seconds, exit with an error. */ | |
183 | for (i = 10*1000/20; i--; ) | |
184 | msleep(20); | |
185 | } | |
d17e1dd2 | 186 | |
20caffd2 WD |
187 | rprintf(FERROR, RSYNC_NAME ": connection unexpectedly closed " |
188 | "(%s bytes received so far) [%s]\n", | |
189 | big_num(stats.total_read), who_am_i()); | |
d17e1dd2 | 190 | |
20caffd2 | 191 | exit_cleanup(RERR_STREAMIO); |
554e0a8d AT |
192 | } |
193 | ||
20caffd2 WD |
194 | /* Do a safe read, handling any needed looping and error handling. |
195 | * Returns the count of the bytes read, which will only be different | |
196 | * from "len" if we encountered an EOF. This routine is not used on | |
197 | * the socket except very early in the transfer. */ | |
198 | static size_t safe_read(int fd, char *buf, size_t len) | |
0b789446 | 199 | { |
20caffd2 WD |
200 | size_t got; |
201 | int n; | |
0b789446 | 202 | |
20caffd2 | 203 | assert(fd != iobuf.in_fd); |
0b789446 | 204 | |
20caffd2 WD |
205 | n = read(fd, buf, len); |
206 | if ((size_t)n == len || n == 0) { | |
207 | if (DEBUG_GTE(IO, 2)) | |
208 | rprintf(FINFO, "[%s] safe_read(%d)=%ld\n", who_am_i(), fd, (long)n); | |
209 | return n; | |
3be97bf9 | 210 | } |
20caffd2 WD |
211 | if (n < 0) { |
212 | if (errno != EINTR && errno != EWOULDBLOCK && errno != EAGAIN) { | |
213 | read_failed: | |
214 | rsyserr(FERROR, errno, "safe_read failed to read %ld bytes [%s]", | |
215 | (long)len, who_am_i()); | |
216 | exit_cleanup(RERR_STREAMIO); | |
217 | } | |
218 | got = 0; | |
219 | } else | |
220 | got = n; | |
3be97bf9 | 221 | |
20caffd2 WD |
222 | while (1) { |
223 | struct timeval tv; | |
224 | fd_set r_fds, e_fds; | |
225 | int cnt; | |
7f9bf6b7 | 226 | |
20caffd2 WD |
227 | FD_ZERO(&r_fds); |
228 | FD_SET(fd, &r_fds); | |
229 | FD_ZERO(&e_fds); | |
230 | FD_SET(fd, &e_fds); | |
231 | tv.tv_sec = select_timeout; | |
232 | tv.tv_usec = 0; | |
7f9bf6b7 | 233 | |
20caffd2 WD |
234 | cnt = select(fd+1, &r_fds, NULL, &e_fds, &tv); |
235 | if (cnt <= 0) { | |
236 | if (cnt < 0 && errno == EBADF) { | |
237 | rsyserr(FERROR, errno, "safe_read select failed [%s]", | |
238 | who_am_i()); | |
239 | exit_cleanup(RERR_FILEIO); | |
240 | } | |
241 | check_timeout(); | |
242 | continue; | |
243 | } | |
7f9bf6b7 | 244 | |
20caffd2 WD |
245 | /*if (FD_ISSET(fd, &e_fds)) |
246 | rprintf(FINFO, "select exception on fd %d\n", fd); */ | |
7f9bf6b7 | 247 | |
20caffd2 WD |
248 | if (FD_ISSET(fd, &r_fds)) { |
249 | n = read(fd, buf + got, len - got); | |
250 | if (DEBUG_GTE(IO, 2)) | |
251 | rprintf(FINFO, "[%s] safe_read(%d)=%ld\n", who_am_i(), fd, (long)n); | |
252 | if (n == 0) | |
253 | break; | |
254 | if (n < 0) { | |
255 | if (errno == EINTR) | |
256 | continue; | |
257 | goto read_failed; | |
258 | } | |
259 | if ((got += (size_t)n) == len) | |
260 | break; | |
261 | } | |
7f9bf6b7 | 262 | } |
20caffd2 WD |
263 | |
264 | return got; | |
7f9bf6b7 WD |
265 | } |
266 | ||
20caffd2 | 267 | static const char *what_fd_is(int fd) |
d6081c82 | 268 | { |
20caffd2 | 269 | static char buf[20]; |
d6081c82 | 270 | |
20caffd2 WD |
271 | if (fd == sock_f_out) |
272 | return "socket"; | |
273 | else if (fd == iobuf.out_fd) | |
274 | return "message fd"; | |
275 | else if (fd == batch_fd) | |
276 | return "batch file"; | |
277 | else { | |
278 | snprintf(buf, sizeof buf, "fd %d", fd); | |
279 | return buf; | |
280 | } | |
d6081c82 WD |
281 | } |
282 | ||
20caffd2 WD |
283 | /* Do a safe write, handling any needed looping and error handling. |
284 | * Returns only if everything was successfully written. This routine | |
285 | * is not used on the socket except very early in the transfer. */ | |
286 | static void safe_write(int fd, const char *buf, size_t len) | |
d17e1dd2 | 287 | { |
20caffd2 | 288 | int n; |
ee6e80c7 | 289 | |
20caffd2 | 290 | assert(fd != iobuf.out_fd); |
332cf6df | 291 | |
20caffd2 WD |
292 | n = write(fd, buf, len); |
293 | if ((size_t)n == len) | |
294 | return; | |
295 | if (n < 0) { | |
296 | if (errno != EINTR && errno != EWOULDBLOCK && errno != EAGAIN) { | |
297 | write_failed: | |
298 | rsyserr(FERROR, errno, | |
299 | "safe_write failed to write %ld bytes to %s [%s]", | |
300 | (long)len, what_fd_is(fd), who_am_i()); | |
301 | exit_cleanup(RERR_STREAMIO); | |
ee6e80c7 | 302 | } |
20caffd2 WD |
303 | } else { |
304 | buf += n; | |
305 | len -= n; | |
306 | } | |
ee6e80c7 | 307 | |
20caffd2 WD |
308 | while (len) { |
309 | struct timeval tv; | |
310 | fd_set w_fds; | |
311 | int cnt; | |
3be97bf9 | 312 | |
20caffd2 WD |
313 | FD_ZERO(&w_fds); |
314 | FD_SET(fd, &w_fds); | |
315 | tv.tv_sec = select_timeout; | |
316 | tv.tv_usec = 0; | |
17a4977b | 317 | |
20caffd2 WD |
318 | cnt = select(fd + 1, NULL, &w_fds, NULL, &tv); |
319 | if (cnt <= 0) { | |
320 | if (cnt < 0 && errno == EBADF) { | |
321 | rsyserr(FERROR, errno, "safe_write select failed on %s [%s]", | |
322 | what_fd_is(fd), who_am_i()); | |
323 | exit_cleanup(RERR_FILEIO); | |
324 | } | |
325 | check_timeout(); | |
326 | continue; | |
327 | } | |
d17e1dd2 | 328 | |
20caffd2 WD |
329 | if (FD_ISSET(fd, &w_fds)) { |
330 | n = write(fd, buf, len); | |
331 | if (n < 0) { | |
332 | if (errno == EINTR) | |
333 | continue; | |
334 | goto write_failed; | |
335 | } | |
336 | buf += n; | |
337 | len -= n; | |
338 | } | |
37f35d89 | 339 | } |
37f35d89 WD |
340 | } |
341 | ||
20caffd2 WD |
342 | /* This is only called when files-from data is known to be available. We read |
343 | * a chunk of data and put it into the output buffer. */ | |
344 | static void forward_filesfrom_data(void) | |
155d9206 | 345 | { |
20caffd2 | 346 | int len; |
20caffd2 | 347 | |
cbc63a09 | 348 | len = read(ff_forward_fd, ff_xb.buf + ff_xb.len, ff_xb.size - ff_xb.len); |
20caffd2 WD |
349 | if (len <= 0) { |
350 | if (len == 0 || errno != EINTR) { | |
351 | /* Send end-of-file marker */ | |
20caffd2 | 352 | ff_forward_fd = -1; |
cbc63a09 WD |
353 | write_buf(iobuf.out_fd, "\0\0", ff_lastchar ? 2 : 1); |
354 | free_xbuf(&ff_xb); | |
8e6b4ddb WD |
355 | if (ff_reenable_multiplex >= 0) |
356 | io_start_multiplex_out(ff_reenable_multiplex); | |
20caffd2 | 357 | } |
0b789446 | 358 | return; |
20caffd2 | 359 | } |
554e0a8d | 360 | |
20caffd2 WD |
361 | if (DEBUG_GTE(IO, 2)) |
362 | rprintf(FINFO, "[%s] files-from read=%ld\n", who_am_i(), (long)len); | |
554e0a8d | 363 | |
cbc63a09 WD |
364 | #ifdef ICONV_OPTION |
365 | len += ff_xb.len; | |
366 | #endif | |
367 | ||
20caffd2 | 368 | if (!eol_nulls) { |
cbc63a09 | 369 | char *s = ff_xb.buf + len; |
20caffd2 | 370 | /* Transform CR and/or LF into '\0' */ |
cbc63a09 | 371 | while (s-- > ff_xb.buf) { |
20caffd2 WD |
372 | if (*s == '\n' || *s == '\r') |
373 | *s = '\0'; | |
374 | } | |
375 | } | |
cbc63a09 | 376 | |
20caffd2 | 377 | if (ff_lastchar) |
cbc63a09 | 378 | ff_xb.pos = 0; |
20caffd2 | 379 | else { |
cbc63a09 | 380 | char *s = ff_xb.buf; |
20caffd2 WD |
381 | /* Last buf ended with a '\0', so don't let this buf start with one. */ |
382 | while (len && *s == '\0') | |
383 | s++, len--; | |
cbc63a09 | 384 | ff_xb.pos = s - ff_xb.buf; |
20caffd2 | 385 | } |
cbc63a09 WD |
386 | |
387 | #ifdef ICONV_OPTION | |
388 | if (filesfrom_convert && len) { | |
389 | char *sob = ff_xb.buf + ff_xb.pos, *s = sob; | |
390 | char *eob = sob + len; | |
391 | int flags = ICB_INCLUDE_BAD | ICB_INCLUDE_INCOMPLETE | ICB_CIRCULAR_OUT; | |
392 | if (ff_lastchar == '\0') | |
393 | flags |= ICB_INIT; | |
394 | /* Convert/send each null-terminated string separately, skipping empties. */ | |
395 | while (s != eob) { | |
396 | if (*s++ == '\0') { | |
397 | ff_xb.len = s - sob - 1; | |
398 | if (iconvbufs(ic_send, &ff_xb, &iobuf.out, flags) < 0) | |
399 | exit_cleanup(RERR_PROTOCOL); /* impossible? */ | |
400 | write_buf(iobuf.out_fd, s-1, 1); /* Send the '\0'. */ | |
401 | while (s != eob && *s == '\0') | |
402 | s++; | |
403 | sob = s; | |
404 | ff_xb.pos = sob - ff_xb.buf; | |
405 | flags |= ICB_INIT; | |
406 | } | |
407 | } | |
408 | ||
409 | if ((ff_xb.len = s - sob) == 0) | |
410 | ff_lastchar = '\0'; | |
411 | else { | |
412 | /* Handle a partial string specially, saving any incomplete chars. */ | |
413 | flags &= ~ICB_INCLUDE_INCOMPLETE; | |
414 | if (iconvbufs(ic_send, &ff_xb, &iobuf.out, flags) < 0) { | |
415 | if (errno == E2BIG) | |
416 | exit_cleanup(RERR_PROTOCOL); /* impossible? */ | |
417 | if (ff_xb.pos) | |
418 | memmove(ff_xb.buf, ff_xb.buf + ff_xb.pos, ff_xb.len); | |
419 | } | |
420 | ff_lastchar = 'x'; /* Anything non-zero. */ | |
421 | } | |
422 | } else | |
423 | #endif | |
424 | ||
20caffd2 | 425 | if (len) { |
cbc63a09 WD |
426 | char *f = ff_xb.buf + ff_xb.pos; |
427 | char *t = ff_xb.buf; | |
20caffd2 WD |
428 | char *eob = f + len; |
429 | /* Eliminate any multi-'\0' runs. */ | |
430 | while (f != eob) { | |
431 | if (!(*t++ = *f++)) { | |
cbc63a09 WD |
432 | while (f != eob && *f == '\0') |
433 | f++; | |
20caffd2 WD |
434 | } |
435 | } | |
436 | ff_lastchar = f[-1]; | |
cbc63a09 WD |
437 | if ((len = t - ff_xb.buf) != 0) { |
438 | /* This will not circle back to perform_io() because we only get | |
439 | * called when there is plenty of room in the output buffer. */ | |
440 | write_buf(iobuf.out_fd, ff_xb.buf, len); | |
441 | } | |
20caffd2 | 442 | } |
56014c8c | 443 | } |
720b47f2 | 444 | |
20caffd2 WD |
445 | /* Perform buffered input and output until specified conditions are met. When |
446 | * given a "needed" read requirement, we'll return without doing any I/O if the | |
447 | * iobuf.in bytes are already available. When reading, we'll read as many | |
448 | * bytes as we can into the buffer, and return as soon as we meet the minimum | |
449 | * read requirement. When given a "needed" write requirement, we'll return | |
450 | * without doing any I/O if that many bytes will fit in the output buffer (we | |
451 | * check either iobuf.out or iobuf.msg, depending on the flags). When writing, | |
452 | * we write out as much as we can, and return as soon as the given free-space | |
453 | * requirement is available. | |
880da007 | 454 | * |
20caffd2 WD |
455 | * The iobuf.out and iobuf.msg buffers are circular, so some writes into them |
456 | * will need to be split when the data needs to wrap around to the start. In | |
457 | * order to help make this easier for some operations (such as the use of | |
458 | * SIVAL() into the buffer) the buffers MUST have 4 bytes of overflow space at | |
459 | * the end that is not not counted in the "size". The iobuf.in buffer is not | |
460 | * (currently) circular. To facilitate the handling of MSG_DATA bytes as they | |
461 | * are read-from/written-into the buffers, see the three raw_* iobuf vars. | |
cb2e1f18 | 462 | * |
20caffd2 WD |
463 | * When writing, we flush data in the following priority order: |
464 | * | |
465 | * 1. Finish writing any in-progress MSG_DATA sequence from iobuf.out. | |
466 | * | |
467 | * 2. Write out all the messages from the message buf (if iobuf.msg is active). | |
468 | * Yes, this means that a PIO_NEED_OUTROOM call will completely flush any | |
469 | * messages before getting to the iobuf.out flushing (except for rule 1). | |
470 | * | |
471 | * 3. Write out the raw data from iobuf.out, possibly filling in the multiplexed | |
472 | * MSG_DATA header that was pre-allocated (when output is multiplexed). | |
473 | * | |
474 | * TODO: items for possible future work: | |
475 | * | |
476 | * - Make this routine able to read the generator-to-receiver batch flow? | |
477 | * | |
478 | * - Make the input buffer circular? | |
479 | * | |
480 | * Unlike the old routines that this replaces, it is OK to read ahead as far as | |
481 | * we can because the read_a_msg() routine now reads its bytes out of the input | |
482 | * buffer. In the old days, only raw data was in the input buffer, and any | |
483 | * unused raw data in the buf would prevent the reading of socket data. */ | |
484 | static char *perform_io(size_t needed, int flags) | |
7a55d06e | 485 | { |
20caffd2 WD |
486 | fd_set r_fds, e_fds, w_fds; |
487 | struct timeval tv; | |
488 | int cnt, max_fd; | |
489 | size_t empty_buf_len = 0; | |
490 | xbuf *out; | |
491 | char *data; | |
492 | ||
493 | if (iobuf.in.len == 0 && iobuf.in.pos != 0) { | |
494 | if (iobuf.raw_input_ends_before) | |
495 | iobuf.raw_input_ends_before -= iobuf.in.pos; | |
496 | iobuf.in.pos = 0; | |
574c2500 | 497 | } |
3151cbae | 498 | |
20caffd2 WD |
499 | switch (flags & PIO_NEED_FLAGS) { |
500 | case PIO_NEED_INPUT: | |
501 | if (DEBUG_GTE(IO, 3)) { | |
502 | rprintf(FINFO, "[%s] perform_io(%ld, %sinput)\n", | |
503 | who_am_i(), (long)needed, flags & PIO_CONSUME_INPUT ? "consume&" : ""); | |
504 | } | |
00bdf899 | 505 | |
20caffd2 WD |
506 | /* Make sure the input buffer is big enough to hold "needed" bytes. |
507 | * Also make sure it will fit in the free space at the end, or | |
508 | * else we need to shift some bytes. */ | |
509 | if (needed && iobuf.in.size < needed) { | |
510 | if (!(iobuf.in.buf = realloc_array(iobuf.in.buf, char, needed))) | |
511 | out_of_memory("perform_io"); | |
512 | if (DEBUG_GTE(IO, 4)) { | |
0a56ef12 WD |
513 | rprintf(FINFO, "[%s] resized input buffer from %ld to %ld bytes.\n", |
514 | who_am_i(), (long)iobuf.in.size, (long)needed); | |
20caffd2 WD |
515 | } |
516 | iobuf.in.size = needed; | |
517 | } | |
518 | if (iobuf.in.size - iobuf.in.pos < needed | |
519 | || (iobuf.in.len < needed && iobuf.in.len < 1024 | |
520 | && iobuf.in.size - (iobuf.in.pos + iobuf.in.len) < 1024)) { | |
521 | memmove(iobuf.in.buf, iobuf.in.buf + iobuf.in.pos, iobuf.in.len); | |
522 | if (DEBUG_GTE(IO, 4)) { | |
523 | rprintf(FINFO, | |
0a56ef12 WD |
524 | "[%s] moved %ld bytes from %ld to 0 in the input buffer (size=%ld, needed=%ld).\n", |
525 | who_am_i(), (long)iobuf.in.len, (long)iobuf.in.pos, (long)iobuf.in.size, (long)needed); | |
20caffd2 WD |
526 | } |
527 | if (iobuf.raw_input_ends_before) | |
528 | iobuf.raw_input_ends_before -= iobuf.in.pos; | |
529 | iobuf.in.pos = 0; | |
530 | } | |
531 | break; | |
720b47f2 | 532 | |
20caffd2 WD |
533 | case PIO_NEED_OUTROOM: |
534 | /* We never resize the circular output buffer. */ | |
535 | if (iobuf.out.size - iobuf.out_empty_len < needed) { | |
536 | fprintf(stderr, "need to write %ld bytes, iobuf.out.buf is only %ld bytes.\n", | |
537 | (long)needed, (long)(iobuf.out.size - iobuf.out_empty_len)); | |
538 | exit_cleanup(RERR_PROTOCOL); | |
539 | } | |
4c36ddbe | 540 | |
20caffd2 WD |
541 | if (DEBUG_GTE(IO, 3)) { |
542 | rprintf(FINFO, "[%s] perform_io(%ld, outroom) needs to flush %ld\n", | |
543 | who_am_i(), (long)needed, | |
544 | iobuf.out.len > iobuf.out.size - needed | |
545 | ? (long)iobuf.out.len - (iobuf.out.size - needed) : 0L); | |
546 | } | |
547 | break; | |
ea2111d1 | 548 | |
20caffd2 WD |
549 | case PIO_NEED_MSGROOM: |
550 | /* We never resize the circular message buffer. */ | |
551 | if (iobuf.msg.size < needed) { | |
552 | fprintf(stderr, "need to write %ld bytes, iobuf.msg.buf is only %ld bytes.\n", | |
553 | (long)needed, (long)iobuf.msg.size); | |
554 | exit_cleanup(RERR_PROTOCOL); | |
555 | } | |
556 | ||
557 | if (DEBUG_GTE(IO, 3)) { | |
558 | rprintf(FINFO, "[%s] perform_io(%ld, msgroom) needs to flush %ld\n", | |
559 | who_am_i(), (long)needed, | |
560 | iobuf.out.len > iobuf.msg.size - needed | |
561 | ? (long)iobuf.out.len - (iobuf.msg.size - needed) : 0L); | |
562 | } | |
563 | break; | |
564 | ||
565 | case 0: | |
566 | if (DEBUG_GTE(IO, 3)) | |
567 | rprintf(FINFO, "[%s] perform_io(%ld, %d)\n", who_am_i(), (long)needed, flags); | |
568 | break; | |
569 | ||
570 | default: | |
571 | exit_cleanup(RERR_UNSUPPORTED); | |
572 | } | |
573 | ||
574 | while (1) { | |
575 | switch (flags & PIO_NEED_FLAGS) { | |
576 | case PIO_NEED_INPUT: | |
577 | if (iobuf.in.len >= needed) | |
578 | goto double_break; | |
579 | break; | |
580 | case PIO_NEED_OUTROOM: | |
581 | if (iobuf.out.len <= iobuf.out.size - needed) | |
582 | goto double_break; | |
583 | break; | |
584 | case PIO_NEED_MSGROOM: | |
585 | if (iobuf.msg.len <= iobuf.msg.size - needed) | |
586 | goto double_break; | |
587 | break; | |
588 | } | |
589 | ||
590 | max_fd = -1; | |
4c36ddbe | 591 | |
56014c8c | 592 | FD_ZERO(&r_fds); |
20caffd2 WD |
593 | FD_ZERO(&e_fds); |
594 | if (iobuf.in_fd >= 0 && iobuf.in.size - (iobuf.in.pos + iobuf.in.len)) { | |
595 | if (!read_batch || batch_fd >= 0) { | |
596 | FD_SET(iobuf.in_fd, &r_fds); | |
597 | FD_SET(iobuf.in_fd, &e_fds); | |
598 | } | |
599 | if (iobuf.in_fd > max_fd) | |
600 | max_fd = iobuf.in_fd; | |
601 | } | |
602 | ||
603 | /* Only do more filesfrom processing if there is enough room in the out buffer. */ | |
604 | if (ff_forward_fd >= 0 && iobuf.out.size - iobuf.out.len > FILESFROM_BUFLEN*2) { | |
605 | FD_SET(ff_forward_fd, &r_fds); | |
606 | if (ff_forward_fd > max_fd) | |
607 | max_fd = ff_forward_fd; | |
608 | } | |
609 | ||
a7026ba9 | 610 | FD_ZERO(&w_fds); |
20caffd2 WD |
611 | if (iobuf.out_fd >= 0) { |
612 | if (iobuf.raw_flushing_ends_before | |
613 | || (!iobuf.msg.len && iobuf.out.len > iobuf.out_empty_len && !(flags & PIO_NEED_MSGROOM))) { | |
614 | if (OUT_MULTIPLEXED && !iobuf.raw_flushing_ends_before) { | |
615 | size_t val; | |
616 | ||
617 | /* The iobuf.raw_flushing_ends_before value can point off the end | |
618 | * of the iobuf.out buffer for a while, for easier subtracting. */ | |
619 | iobuf.raw_flushing_ends_before = iobuf.out.pos + iobuf.out.len; | |
620 | ||
621 | SIVAL(iobuf.out.buf + iobuf.raw_data_header_pos, 0, | |
622 | ((MPLEX_BASE + (int)MSG_DATA)<<24) + iobuf.out.len - 4); | |
623 | if ((val = iobuf.out.size - iobuf.raw_data_header_pos) < 4) { | |
624 | /* We used some of the overflow bytes, so move them. */ | |
625 | if (DEBUG_GTE(IO, 4)) { | |
626 | rprintf(FINFO, "[%s] wrap-bytes moved: %d (perform_io)\n", | |
627 | who_am_i(), (int)val); | |
628 | } | |
629 | memcpy(iobuf.out.buf, iobuf.out.buf + iobuf.out.size, 4 - val); | |
630 | } | |
631 | ||
632 | if (DEBUG_GTE(IO, 1)) { | |
633 | rprintf(FINFO, "[%s] send_msg(%d, %ld)\n", | |
634 | who_am_i(), (int)MSG_DATA, (long)iobuf.out.len - 4); | |
635 | } | |
636 | ||
637 | /* reserve room for the next MSG_DATA header */ | |
638 | iobuf.raw_data_header_pos = iobuf.raw_flushing_ends_before; | |
639 | if (iobuf.raw_data_header_pos >= iobuf.out.size) | |
640 | iobuf.raw_data_header_pos -= iobuf.out.size; | |
641 | iobuf.out.len += 4; | |
56014c8c | 642 | } |
20caffd2 WD |
643 | |
644 | empty_buf_len = iobuf.out_empty_len; | |
645 | out = &iobuf.out; | |
646 | } else if (iobuf.msg.len) { | |
647 | empty_buf_len = 0; | |
648 | out = &iobuf.msg; | |
649 | } else | |
650 | out = NULL; | |
651 | if (out) { | |
652 | FD_SET(iobuf.out_fd, &w_fds); | |
653 | if (iobuf.out_fd > max_fd) | |
654 | max_fd = iobuf.out_fd; | |
655 | } | |
656 | } else | |
657 | out = NULL; | |
658 | ||
659 | if (max_fd < 0) { | |
660 | switch (flags & PIO_NEED_FLAGS) { | |
661 | case PIO_NEED_INPUT: | |
662 | rprintf(FERROR, "error in perform_io: no fd for input.\n"); | |
663 | exit_cleanup(RERR_PROTOCOL); | |
664 | case PIO_NEED_OUTROOM: | |
665 | case PIO_NEED_MSGROOM: | |
666 | msgs2stderr = 1; | |
667 | rprintf(FERROR, "error in perform_io: no fd for output.\n"); | |
668 | exit_cleanup(RERR_PROTOCOL); | |
669 | default: | |
670 | /* No stated needs, so I guess this is OK. */ | |
671 | break; | |
56014c8c | 672 | } |
20caffd2 | 673 | break; |
554e0a8d AT |
674 | } |
675 | ||
20caffd2 | 676 | if (extra_flist_sending_enabled) { |
7a7810aa WD |
677 | if (file_total - file_old_total < MAX_FILECNT_LOOKAHEAD |
678 | && file_total - file_old_total >= MIN_FILECNT_LOOKAHEAD) | |
679 | tv.tv_sec = 0; | |
680 | else { | |
681 | extra_flist_sending_enabled = False; | |
682 | tv.tv_sec = select_timeout; | |
683 | } | |
684 | } else | |
ce827c3e | 685 | tv.tv_sec = select_timeout; |
4c36ddbe AT |
686 | tv.tv_usec = 0; |
687 | ||
20caffd2 | 688 | cnt = select(max_fd + 1, &r_fds, &w_fds, &e_fds, &tv); |
554e0a8d | 689 | |
20caffd2 WD |
690 | if (cnt <= 0) { |
691 | if (cnt < 0 && errno == EBADF) { | |
692 | msgs2stderr = 1; | |
554e0a8d | 693 | exit_cleanup(RERR_SOCKETIO); |
2a5df862 | 694 | } |
20caffd2 | 695 | if (extra_flist_sending_enabled) { |
7a7810aa | 696 | extra_flist_sending_enabled = False; |
ce827c3e | 697 | send_extra_file_list(sock_f_out, -1); |
7a7810aa WD |
698 | extra_flist_sending_enabled = !flist_eof; |
699 | } else | |
ce827c3e | 700 | check_timeout(); |
20caffd2 WD |
701 | FD_ZERO(&r_fds); /* Just in case... */ |
702 | FD_ZERO(&w_fds); | |
4c36ddbe AT |
703 | } |
704 | ||
20caffd2 WD |
705 | if (iobuf.in_fd >= 0 && FD_ISSET(iobuf.in_fd, &r_fds)) { |
706 | size_t pos = iobuf.in.pos + iobuf.in.len; | |
707 | size_t len = iobuf.in.size - pos; | |
708 | int n; | |
709 | if ((n = read(iobuf.in_fd, iobuf.in.buf + pos, len)) <= 0) { | |
710 | if (n == 0) { | |
711 | if (!read_batch || batch_fd < 0 || am_generator) | |
712 | whine_about_eof(iobuf.in_fd); /* Doesn't return. */ | |
713 | batch_fd = -1; | |
714 | continue; | |
715 | } | |
716 | if (errno == EINTR || errno == EWOULDBLOCK || errno == EAGAIN) | |
717 | n = 0; | |
718 | else { | |
719 | /* Don't write errors on a dead socket. */ | |
720 | if (iobuf.in_fd == sock_f_in) { | |
721 | if (am_sender) | |
722 | msgs2stderr = 1; | |
723 | rsyserr(FERROR_SOCKET, errno, "read error"); | |
724 | } else | |
725 | rsyserr(FERROR, errno, "read error"); | |
726 | exit_cleanup(RERR_STREAMIO); | |
56014c8c | 727 | } |
20caffd2 WD |
728 | } |
729 | if (msgs2stderr && DEBUG_GTE(IO, 2)) | |
730 | rprintf(FINFO, "[%s] recv=%ld\n", who_am_i(), (long)n); | |
731 | ||
732 | if (io_timeout) | |
733 | last_io_in = time(NULL); | |
734 | stats.total_read += n; | |
735 | ||
736 | iobuf.in.len += n; | |
737 | } | |
738 | ||
739 | if (iobuf.out_fd >= 0 && FD_ISSET(iobuf.out_fd, &w_fds)) { | |
740 | size_t len = iobuf.raw_flushing_ends_before ? iobuf.raw_flushing_ends_before - out->pos : out->len; | |
741 | int n; | |
742 | ||
743 | if (bwlimit_writemax && len > bwlimit_writemax) | |
744 | len = bwlimit_writemax; | |
745 | ||
746 | if (out->pos + len > out->size) | |
747 | len = out->size - out->pos; | |
748 | if ((n = write(iobuf.out_fd, out->buf + out->pos, len)) <= 0) { | |
749 | if (errno == EINTR || errno == EWOULDBLOCK || errno == EAGAIN) | |
750 | n = 0; | |
751 | else { | |
752 | /* Don't write errors on a dead socket. */ | |
753 | msgs2stderr = 1; | |
754 | out->len = iobuf.raw_flushing_ends_before = out->pos = 0; | |
755 | rsyserr(FERROR_SOCKET, errno, "write error"); | |
756 | exit_cleanup(RERR_STREAMIO); | |
757 | } | |
758 | } | |
759 | if (msgs2stderr && DEBUG_GTE(IO, 2)) { | |
760 | rprintf(FINFO, "[%s] %s sent=%ld\n", | |
761 | who_am_i(), out == &iobuf.out ? "out" : "msg", (long)n); | |
762 | } | |
763 | ||
764 | if (io_timeout) | |
765 | last_io_out = time(NULL); | |
766 | stats.total_written += n; | |
767 | ||
768 | if (bwlimit_writemax) | |
769 | sleep_for_bwlimit(n); | |
770 | ||
771 | if ((out->pos += n) == out->size) { | |
772 | if (iobuf.raw_flushing_ends_before) | |
773 | iobuf.raw_flushing_ends_before -= out->size; | |
774 | out->pos = 0; | |
775 | } | |
776 | if (out->pos == iobuf.raw_flushing_ends_before) | |
777 | iobuf.raw_flushing_ends_before = 0; | |
778 | if ((out->len -= n) == empty_buf_len) { | |
779 | out->pos = 0; | |
780 | if (empty_buf_len) | |
781 | iobuf.raw_data_header_pos = 0; | |
782 | } | |
783 | } | |
784 | ||
785 | if (ff_forward_fd >= 0 && FD_ISSET(ff_forward_fd, &r_fds)) { | |
786 | /* This can potentially flush all output and enable | |
787 | * multiplexed output, so keep this last in the loop | |
788 | * and be sure to not cache anything that would break | |
789 | * such a change. */ | |
790 | forward_filesfrom_data(); | |
791 | } | |
792 | } | |
793 | double_break: | |
794 | ||
795 | data = iobuf.in.buf + iobuf.in.pos; | |
796 | ||
797 | if (flags & PIO_CONSUME_INPUT) { | |
798 | iobuf.in.len -= needed; | |
799 | iobuf.in.pos += needed; | |
800 | } | |
801 | ||
802 | return data; | |
803 | } | |
804 | ||
805 | /* Buffer a message for the multiplexed output stream. Is never used for MSG_DATA. */ | |
806 | int send_msg(enum msgcode code, const char *buf, size_t len, int convert) | |
807 | { | |
808 | char *hdr; | |
809 | size_t pos; | |
810 | BOOL want_debug = DEBUG_GTE(IO, 1) && convert >= 0 && (msgs2stderr || code != MSG_INFO); | |
811 | ||
812 | if (!OUT_MULTIPLEXED) | |
813 | return 0; | |
814 | ||
815 | if (want_debug) | |
816 | rprintf(FINFO, "[%s] send_msg(%d, %ld)\n", who_am_i(), (int)code, (long)len); | |
817 | ||
22955408 | 818 | #ifdef ICONV_OPTION |
20caffd2 WD |
819 | if (convert > 0 && ic_send == (iconv_t)-1) |
820 | convert = 0; | |
821 | if (convert > 0) { | |
822 | /* Ensuring double-size room leaves space for a potential conversion. */ | |
823 | if (iobuf.msg.len + len*2 + 4 > iobuf.msg.size) | |
824 | perform_io(len*2 + 4, PIO_NEED_MSGROOM); | |
825 | } else | |
22955408 | 826 | #endif |
20caffd2 WD |
827 | if (iobuf.msg.len + len + 4 > iobuf.msg.size) |
828 | perform_io(len + 4, PIO_NEED_MSGROOM); | |
829 | ||
830 | pos = iobuf.msg.pos + iobuf.msg.len; /* Must be set after any flushing. */ | |
831 | if (pos >= iobuf.msg.size) | |
832 | pos -= iobuf.msg.size; | |
833 | hdr = iobuf.msg.buf + pos; | |
834 | ||
835 | iobuf.msg.len += 4; /* Leave room for the coming header bytes. */ | |
836 | ||
22955408 | 837 | #ifdef ICONV_OPTION |
20caffd2 WD |
838 | if (convert > 0) { |
839 | xbuf inbuf; | |
840 | ||
841 | INIT_XBUF(inbuf, (char*)buf, len, (size_t)-1); | |
842 | ||
843 | len = iobuf.msg.len; | |
844 | iconvbufs(ic_send, &inbuf, &iobuf.msg, | |
d8a7290f | 845 | ICB_INCLUDE_BAD | ICB_INCLUDE_INCOMPLETE | ICB_CIRCULAR_OUT | ICB_INIT); |
20caffd2 WD |
846 | if (inbuf.len > 0) { |
847 | rprintf(FERROR, "overflowed iobuf.msg buffer in send_msg"); | |
848 | exit_cleanup(RERR_UNSUPPORTED); | |
849 | } | |
850 | len = iobuf.msg.len - len; | |
851 | } else | |
22955408 | 852 | #endif |
20caffd2 WD |
853 | { |
854 | size_t siz; | |
855 | ||
856 | if ((pos += 4) >= iobuf.msg.size) | |
857 | pos -= iobuf.msg.size; | |
858 | ||
859 | /* Handle a split copy if we wrap around the end of the circular buffer. */ | |
860 | if (pos >= iobuf.msg.pos && (siz = iobuf.msg.size - pos) < len) { | |
861 | memcpy(iobuf.msg.buf + pos, buf, siz); | |
862 | memcpy(iobuf.msg.buf, buf + siz, len - siz); | |
863 | } else | |
864 | memcpy(iobuf.msg.buf + pos, buf, len); | |
865 | ||
866 | iobuf.msg.len += len; | |
867 | } | |
868 | ||
869 | SIVAL(hdr, 0, ((MPLEX_BASE + (int)code)<<24) + len); | |
870 | /* If the header used any overflow bytes, move them to the start. */ | |
871 | if ((pos = hdr+4 - iobuf.msg.buf) > iobuf.msg.size) { | |
872 | size_t siz = pos - iobuf.msg.size; | |
873 | if (DEBUG_GTE(IO, 4)) | |
874 | rprintf(FINFO, "[%s] wrap-bytes moved: %d (send_msg)\n", who_am_i(), (int)siz); | |
875 | memcpy(iobuf.msg.buf, hdr+4 - siz, siz); | |
876 | } | |
877 | ||
878 | if (want_debug && convert > 0) | |
879 | rprintf(FINFO, "[%s] converted msg len=%ld\n", who_am_i(), (long)len); | |
880 | ||
881 | return 1; | |
882 | } | |
883 | ||
884 | void send_msg_int(enum msgcode code, int num) | |
885 | { | |
886 | char numbuf[4]; | |
887 | ||
888 | if (DEBUG_GTE(IO, 1)) | |
889 | rprintf(FINFO, "[%s] send_msg_int(%d, %d)\n", who_am_i(), (int)code, num); | |
890 | ||
891 | SIVAL(numbuf, 0, num); | |
892 | send_msg(code, numbuf, 4, -1); | |
893 | } | |
894 | ||
895 | static void got_flist_entry_status(enum festatus status, int ndx) | |
896 | { | |
897 | struct file_list *flist = flist_for_ndx(ndx, "got_flist_entry_status"); | |
898 | ||
899 | if (remove_source_files) { | |
900 | active_filecnt--; | |
901 | active_bytecnt -= F_LENGTH(flist->files[ndx - flist->ndx_start]); | |
902 | } | |
903 | ||
904 | if (inc_recurse) | |
905 | flist->in_progress--; | |
906 | ||
907 | switch (status) { | |
908 | case FES_SUCCESS: | |
909 | if (remove_source_files) | |
910 | send_msg_int(MSG_SUCCESS, ndx); | |
911 | if (preserve_hard_links) { | |
912 | struct file_struct *file = flist->files[ndx - flist->ndx_start]; | |
913 | if (F_IS_HLINKED(file)) { | |
914 | flist_ndx_push(&hlink_list, ndx); | |
915 | flist->in_progress++; | |
56014c8c WD |
916 | } |
917 | } | |
20caffd2 WD |
918 | break; |
919 | case FES_REDO: | |
920 | if (read_batch) { | |
921 | if (inc_recurse) | |
922 | flist->in_progress++; | |
923 | break; | |
924 | } | |
925 | if (inc_recurse) | |
926 | flist->to_redo++; | |
927 | flist_ndx_push(&redo_list, ndx); | |
928 | break; | |
929 | case FES_NO_SEND: | |
930 | break; | |
931 | } | |
932 | } | |
933 | ||
934 | /* Note the fds used for the main socket (which might really be a pipe | |
935 | * for a local transfer, but we can ignore that). */ | |
936 | void io_set_sock_fds(int f_in, int f_out) | |
937 | { | |
938 | sock_f_in = f_in; | |
939 | sock_f_out = f_out; | |
940 | } | |
941 | ||
942 | void set_io_timeout(int secs) | |
943 | { | |
944 | io_timeout = secs; | |
945 | ||
946 | if (!io_timeout || io_timeout > SELECT_TIMEOUT) | |
947 | select_timeout = SELECT_TIMEOUT; | |
948 | else | |
949 | select_timeout = io_timeout; | |
950 | ||
951 | allowed_lull = read_batch ? 0 : (io_timeout + 1) / 2; | |
952 | } | |
953 | ||
954 | static void check_for_d_option_error(const char *msg) | |
955 | { | |
956 | static char rsync263_opts[] = "BCDHIKLPRSTWabceghlnopqrtuvxz"; | |
957 | char *colon; | |
958 | int saw_d = 0; | |
959 | ||
960 | if (*msg != 'r' | |
961 | || strncmp(msg, REMOTE_OPTION_ERROR, sizeof REMOTE_OPTION_ERROR - 1) != 0) | |
962 | return; | |
56014c8c | 963 | |
20caffd2 WD |
964 | msg += sizeof REMOTE_OPTION_ERROR - 1; |
965 | if (*msg == '-' || (colon = strchr(msg, ':')) == NULL | |
966 | || strncmp(colon, REMOTE_OPTION_ERROR2, sizeof REMOTE_OPTION_ERROR2 - 1) != 0) | |
967 | return; | |
554e0a8d | 968 | |
20caffd2 WD |
969 | for ( ; *msg != ':'; msg++) { |
970 | if (*msg == 'd') | |
971 | saw_d = 1; | |
972 | else if (*msg == 'e') | |
973 | break; | |
974 | else if (strchr(rsync263_opts, *msg) == NULL) | |
975 | return; | |
976 | } | |
4c36ddbe | 977 | |
20caffd2 WD |
978 | if (saw_d) { |
979 | rprintf(FWARNING, | |
980 | "*** Try using \"--old-d\" if remote rsync is <= 2.6.3 ***\n"); | |
981 | } | |
982 | } | |
1f75bb10 | 983 | |
20caffd2 WD |
984 | /* This is used by the generator to limit how many file transfers can |
985 | * be active at once when --remove-source-files is specified. Without | |
986 | * this, sender-side deletions were mostly happening at the end. */ | |
987 | void increment_active_files(int ndx, int itemizing, enum logcode code) | |
988 | { | |
989 | while (1) { | |
990 | /* TODO: tune these limits? */ | |
991 | int limit = active_bytecnt >= 128*1024 ? 10 : 50; | |
992 | if (active_filecnt < limit) | |
993 | break; | |
994 | check_for_finished_files(itemizing, code, 0); | |
995 | if (active_filecnt < limit) | |
996 | break; | |
997 | wait_for_receiver(); | |
998 | } | |
00bdf899 | 999 | |
20caffd2 WD |
1000 | active_filecnt++; |
1001 | active_bytecnt += F_LENGTH(cur_flist->files[ndx - cur_flist->ndx_start]); | |
1002 | } | |
1f75bb10 | 1003 | |
20caffd2 WD |
1004 | int get_redo_num(void) |
1005 | { | |
1006 | return flist_ndx_pop(&redo_list); | |
1007 | } | |
1008 | ||
1009 | int get_hlink_num(void) | |
1010 | { | |
1011 | return flist_ndx_pop(&hlink_list); | |
1012 | } | |
8d9dc9f9 | 1013 | |
20caffd2 WD |
1014 | /* When we're the receiver and we have a local --files-from list of names |
1015 | * that needs to be sent over the socket to the sender, we have to do two | |
1016 | * things at the same time: send the sender a list of what files we're | |
1017 | * processing and read the incoming file+info list from the sender. We do | |
1018 | * this by making recv_file_list() call forward_filesfrom_data(), which | |
1019 | * will ensure that we forward data to the sender until we get some data | |
1020 | * for recv_file_list() to use. */ | |
1021 | void start_filesfrom_forwarding(int fd) | |
1022 | { | |
8e6b4ddb WD |
1023 | if (protocol_version < 31 && OUT_MULTIPLEXED) { |
1024 | /* Older protocols send the files-from data w/o packaging | |
1025 | * it in multiplexed I/O packets, so temporarily switch | |
705132bc WD |
1026 | * to buffered I/O to match this behavior. */ |
1027 | iobuf.msg.pos = iobuf.msg.len = 0; /* Be extra sure no messages go out. */ | |
8e6b4ddb | 1028 | ff_reenable_multiplex = io_end_multiplex_out(MPLX_TO_BUFFERED); |
20caffd2 | 1029 | } |
705132bc | 1030 | ff_forward_fd = fd; |
cbc63a09 WD |
1031 | |
1032 | alloc_xbuf(&ff_xb, FILESFROM_BUFLEN); | |
4c36ddbe | 1033 | } |
8d9dc9f9 | 1034 | |
67b8f3df | 1035 | /* Read a line into the "buf" buffer. */ |
ba525f77 | 1036 | int read_line(int fd, char *buf, size_t bufsiz, int flags) |
56014c8c | 1037 | { |
ba525f77 | 1038 | char ch, *s, *eob; |
56014c8c | 1039 | |
ba525f77 WD |
1040 | #ifdef ICONV_OPTION |
1041 | if (flags & RL_CONVERT && iconv_buf.size < bufsiz) | |
1042 | realloc_xbuf(&iconv_buf, bufsiz + 1024); | |
1043 | #endif | |
1044 | ||
56014c8c | 1045 | start: |
22955408 | 1046 | #ifdef ICONV_OPTION |
ba525f77 | 1047 | s = flags & RL_CONVERT ? iconv_buf.buf : buf; |
22955408 WD |
1048 | #else |
1049 | s = buf; | |
1050 | #endif | |
ba525f77 | 1051 | eob = s + bufsiz - 1; |
56014c8c | 1052 | while (1) { |
20caffd2 WD |
1053 | /* We avoid read_byte() for files because files can return an EOF. */ |
1054 | if (fd == iobuf.in_fd) | |
1055 | ch = read_byte(fd); | |
1056 | else if (safe_read(fd, &ch, 1) == 0) | |
56014c8c | 1057 | break; |
ba525f77 | 1058 | if (flags & RL_EOL_NULLS ? ch == '\0' : (ch == '\r' || ch == '\n')) { |
67b8f3df | 1059 | /* Skip empty lines if dumping comments. */ |
ba525f77 | 1060 | if (flags & RL_DUMP_COMMENTS && s == buf) |
56014c8c WD |
1061 | continue; |
1062 | break; | |
1063 | } | |
1064 | if (s < eob) | |
1065 | *s++ = ch; | |
1066 | } | |
1067 | *s = '\0'; | |
7a55d06e | 1068 | |
ba525f77 | 1069 | if (flags & RL_DUMP_COMMENTS && (*buf == '#' || *buf == ';')) |
56014c8c WD |
1070 | goto start; |
1071 | ||
ba525f77 WD |
1072 | #ifdef ICONV_OPTION |
1073 | if (flags & RL_CONVERT) { | |
1074 | xbuf outbuf; | |
1075 | INIT_XBUF(outbuf, buf, 0, bufsiz); | |
1076 | iconv_buf.pos = 0; | |
1077 | iconv_buf.len = s - iconv_buf.buf; | |
1078 | iconvbufs(ic_recv, &iconv_buf, &outbuf, | |
d8a7290f | 1079 | ICB_INCLUDE_BAD | ICB_INCLUDE_INCOMPLETE | ICB_INIT); |
ba525f77 WD |
1080 | outbuf.buf[outbuf.len] = '\0'; |
1081 | return outbuf.len; | |
1082 | } | |
1083 | #endif | |
1084 | ||
67b8f3df WD |
1085 | return s - buf; |
1086 | } | |
1087 | ||
53936ef9 WD |
1088 | void read_args(int f_in, char *mod_name, char *buf, size_t bufsiz, int rl_nulls, |
1089 | char ***argv_p, int *argc_p, char **request_p) | |
67b8f3df WD |
1090 | { |
1091 | int maxargs = MAX_ARGS; | |
1092 | int dot_pos = 0; | |
1093 | int argc = 0; | |
1094 | char **argv, *p; | |
22955408 WD |
1095 | int rl_flags = (rl_nulls ? RL_EOL_NULLS : 0); |
1096 | ||
1097 | #ifdef ICONV_OPTION | |
1098 | rl_flags |= (protect_args && ic_recv != (iconv_t)-1 ? RL_CONVERT : 0); | |
1099 | #endif | |
67b8f3df WD |
1100 | |
1101 | if (!(argv = new_array(char *, maxargs))) | |
1102 | out_of_memory("read_args"); | |
53936ef9 | 1103 | if (mod_name && !protect_args) |
67b8f3df WD |
1104 | argv[argc++] = "rsyncd"; |
1105 | ||
1106 | while (1) { | |
ba525f77 | 1107 | if (read_line(f_in, buf, bufsiz, rl_flags) == 0) |
67b8f3df WD |
1108 | break; |
1109 | ||
53936ef9 | 1110 | if (argc == maxargs-1) { |
67b8f3df WD |
1111 | maxargs += MAX_ARGS; |
1112 | if (!(argv = realloc_array(argv, char *, maxargs))) | |
1113 | out_of_memory("read_args"); | |
1114 | } | |
1115 | ||
1116 | if (dot_pos) { | |
1117 | if (request_p) { | |
1118 | *request_p = strdup(buf); | |
1119 | request_p = NULL; | |
1120 | } | |
1121 | if (mod_name) | |
1122 | glob_expand_module(mod_name, buf, &argv, &argc, &maxargs); | |
1123 | else | |
1124 | glob_expand(buf, &argv, &argc, &maxargs); | |
1125 | } else { | |
1126 | if (!(p = strdup(buf))) | |
1127 | out_of_memory("read_args"); | |
1128 | argv[argc++] = p; | |
1129 | if (*p == '.' && p[1] == '\0') | |
1130 | dot_pos = argc; | |
1131 | } | |
1132 | } | |
53936ef9 | 1133 | argv[argc] = NULL; |
67b8f3df | 1134 | |
987a5467 WD |
1135 | glob_expand(NULL, NULL, NULL, NULL); |
1136 | ||
67b8f3df WD |
1137 | *argc_p = argc; |
1138 | *argv_p = argv; | |
56014c8c | 1139 | } |
7a55d06e | 1140 | |
8e6b4ddb | 1141 | BOOL io_start_buffering_out(int f_out) |
1f75bb10 | 1142 | { |
20caffd2 WD |
1143 | if (msgs2stderr && DEBUG_GTE(IO, 2)) |
1144 | rprintf(FINFO, "[%s] io_start_buffering_out(%d)\n", who_am_i(), f_out); | |
1145 | ||
1146 | if (OUT_MULTIPLEXED && !iobuf.msg.buf) { | |
1147 | iobuf.msg.size = IO_BUFFER_SIZE - 4; | |
1148 | if (!(iobuf.msg.buf = new_array(char, iobuf.msg.size + 4))) | |
1149 | out_of_memory("io_start_buffering_out"); | |
1150 | iobuf.msg.pos = iobuf.msg.len = 0; | |
1151 | } | |
1152 | ||
1153 | if (iobuf.out.buf) { | |
1154 | if (iobuf.out_fd == -1) | |
1155 | iobuf.out_fd = f_out; | |
1156 | assert(f_out == iobuf.out_fd); | |
8e6b4ddb | 1157 | return False; |
8ef246e0 | 1158 | } |
20caffd2 WD |
1159 | |
1160 | iobuf.out.size = IO_BUFFER_SIZE * 2 - 4; | |
1161 | /* The 4 overflow bytes makes some circular-buffer wrapping operations easier. */ | |
1162 | if (!(iobuf.out.buf = new_array(char, iobuf.out.size + 4))) | |
1f75bb10 | 1163 | out_of_memory("io_start_buffering_out"); |
20caffd2 WD |
1164 | iobuf.out.pos = iobuf.out.len = 0; |
1165 | iobuf.out_fd = f_out; | |
1166 | ||
8e6b4ddb | 1167 | return True; |
1f75bb10 WD |
1168 | } |
1169 | ||
8e6b4ddb | 1170 | BOOL io_start_buffering_in(int f_in) |
1f75bb10 | 1171 | { |
20caffd2 WD |
1172 | if (msgs2stderr && DEBUG_GTE(IO, 2)) |
1173 | rprintf(FINFO, "[%s] io_start_buffering_in(%d)\n", who_am_i(), f_in); | |
1174 | ||
1175 | if (iobuf.in.buf) { | |
1176 | if (iobuf.in_fd == -1) | |
1177 | iobuf.in_fd = f_in; | |
1178 | assert(f_in == iobuf.in_fd); | |
8e6b4ddb | 1179 | return False; |
8ef246e0 | 1180 | } |
20caffd2 WD |
1181 | |
1182 | iobuf.in.size = IO_BUFFER_SIZE; | |
1183 | if (!(iobuf.in.buf = new_array(char, iobuf.in.size))) | |
1f75bb10 | 1184 | out_of_memory("io_start_buffering_in"); |
20caffd2 WD |
1185 | |
1186 | iobuf.in.pos = iobuf.in.len = 0; | |
1187 | ||
1188 | iobuf.in_fd = f_in; | |
1189 | ||
8e6b4ddb | 1190 | return True; |
1f75bb10 WD |
1191 | } |
1192 | ||
20caffd2 | 1193 | void io_end_buffering_in(BOOL free_buffers) |
8ef246e0 | 1194 | { |
705132bc WD |
1195 | if (msgs2stderr && DEBUG_GTE(IO, 2)) { |
1196 | rprintf(FINFO, "[%s] io_end_buffering_in(IOBUF_%s_BUFS)\n", | |
1197 | who_am_i(), free_buffers ? "FREE" : "KEEP"); | |
20caffd2 WD |
1198 | } |
1199 | ||
1200 | if (free_buffers) | |
1201 | free_xbuf(&iobuf.in); | |
1202 | else | |
1203 | iobuf.in.pos = iobuf.in.len = 0; | |
1204 | ||
1205 | iobuf.in_fd = -1; | |
1206 | } | |
1207 | ||
1208 | void io_end_buffering_out(BOOL free_buffers) | |
1209 | { | |
705132bc WD |
1210 | if (msgs2stderr && DEBUG_GTE(IO, 2)) { |
1211 | rprintf(FINFO, "[%s] io_end_buffering_out(IOBUF_%s_BUFS)\n", | |
1212 | who_am_i(), free_buffers ? "FREE" : "KEEP"); | |
20caffd2 WD |
1213 | } |
1214 | ||
8ef246e0 | 1215 | io_flush(FULL_FLUSH); |
20caffd2 WD |
1216 | |
1217 | if (free_buffers) { | |
1218 | free_xbuf(&iobuf.out); | |
1219 | free_xbuf(&iobuf.msg); | |
20caffd2 WD |
1220 | } |
1221 | ||
1222 | iobuf.out_fd = -1; | |
1f75bb10 WD |
1223 | } |
1224 | ||
be91bd81 | 1225 | void maybe_flush_socket(int important) |
626bec8e | 1226 | { |
1ec57e4d WD |
1227 | if (flist_eof && iobuf.out.buf && iobuf.out.len > iobuf.out_empty_len |
1228 | && (important || time(NULL) - last_io_out >= 5)) | |
626bec8e WD |
1229 | io_flush(NORMAL_FLUSH); |
1230 | } | |
1231 | ||
cdf236aa | 1232 | void maybe_send_keepalive(void) |
9e2409ab | 1233 | { |
3b0a30eb | 1234 | if (time(NULL) - last_io_out >= allowed_lull) { |
20caffd2 | 1235 | if (!iobuf.msg.len && iobuf.out.len == iobuf.out_empty_len) { |
3221f451 WD |
1236 | if (protocol_version < 29) |
1237 | return; /* there's nothing we can do */ | |
8ef246e0 | 1238 | if (protocol_version >= 30) |
332cf6df | 1239 | send_msg(MSG_NOOP, "", 0, 0); |
8ef246e0 | 1240 | else { |
20caffd2 WD |
1241 | write_int(iobuf.out_fd, cur_flist->used); |
1242 | write_shortint(iobuf.out_fd, ITEM_IS_NEW); | |
8ef246e0 | 1243 | } |
9e2409ab | 1244 | } |
20caffd2 WD |
1245 | if (iobuf.msg.len) |
1246 | perform_io(iobuf.msg.size - iobuf.msg.len + 1, PIO_NEED_MSGROOM); | |
1247 | else if (iobuf.out.len > iobuf.out_empty_len) | |
9e2409ab WD |
1248 | io_flush(NORMAL_FLUSH); |
1249 | } | |
1250 | } | |
1251 | ||
20caffd2 | 1252 | void start_flist_forward(int ndx) |
8ef246e0 | 1253 | { |
20caffd2 WD |
1254 | write_int(iobuf.out_fd, ndx); |
1255 | forward_flist_data = 1; | |
8ef246e0 WD |
1256 | } |
1257 | ||
20caffd2 | 1258 | void stop_flist_forward(void) |
4c36ddbe | 1259 | { |
20caffd2 | 1260 | forward_flist_data = 0; |
8d9dc9f9 AT |
1261 | } |
1262 | ||
d8587b46 | 1263 | /* Read a message from a multiplexed source. */ |
20caffd2 | 1264 | static void read_a_msg(void) |
8d9dc9f9 | 1265 | { |
20caffd2 | 1266 | char *data, line[BIGPATHBUFLEN]; |
a0f1ed55 | 1267 | int tag, val; |
d8587b46 | 1268 | size_t msg_bytes; |
d8587b46 | 1269 | |
20caffd2 WD |
1270 | data = perform_io(4, PIO_INPUT_AND_CONSUME); |
1271 | tag = IVAL(data, 0); | |
d8587b46 WD |
1272 | |
1273 | msg_bytes = tag & 0xFFFFFF; | |
1274 | tag = (tag >> 24) - MPLEX_BASE; | |
1275 | ||
20caffd2 WD |
1276 | if (DEBUG_GTE(IO, 1) && (msgs2stderr || tag != MSG_INFO)) |
1277 | rprintf(FINFO, "[%s] got msg=%d, len=%ld\n", who_am_i(), (int)tag, (long)msg_bytes); | |
d8587b46 WD |
1278 | |
1279 | switch (tag) { | |
1280 | case MSG_DATA: | |
20caffd2 WD |
1281 | assert(iobuf.raw_input_ends_before == 0); |
1282 | /* Though this does not yet read the data, we do mark where in | |
1283 | * the buffer the msg data will end once it is read. It is | |
1284 | * possible that this points off the end of the buffer, in | |
1285 | * which case the gradual reading of the input stream will | |
1286 | * cause this value to decrease and eventually become real. */ | |
1287 | iobuf.raw_input_ends_before = iobuf.in.pos + msg_bytes; | |
d8587b46 | 1288 | break; |
20caffd2 WD |
1289 | case MSG_STATS: |
1290 | if (msg_bytes != sizeof stats.total_read || !am_generator) | |
d8587b46 | 1291 | goto invalid_msg; |
20caffd2 WD |
1292 | data = perform_io(sizeof stats.total_read, PIO_INPUT_AND_CONSUME); |
1293 | memcpy((char*)&stats.total_read, data, sizeof stats.total_read); | |
d8587b46 WD |
1294 | break; |
1295 | case MSG_REDO: | |
1296 | if (msg_bytes != 4 || !am_generator) | |
1297 | goto invalid_msg; | |
20caffd2 WD |
1298 | data = perform_io(4, PIO_INPUT_AND_CONSUME); |
1299 | got_flist_entry_status(FES_REDO, IVAL(data, 0)); | |
d8587b46 WD |
1300 | break; |
1301 | case MSG_IO_ERROR: | |
1302 | if (msg_bytes != 4 || am_sender) | |
1303 | goto invalid_msg; | |
20caffd2 WD |
1304 | data = perform_io(4, PIO_INPUT_AND_CONSUME); |
1305 | io_error |= IVAL(data, 0); | |
d8587b46 | 1306 | if (!am_generator) |
20caffd2 | 1307 | send_msg(MSG_IO_ERROR, data, 4, 0); |
d8587b46 | 1308 | break; |
a0f1ed55 WD |
1309 | case MSG_IO_TIMEOUT: |
1310 | if (msg_bytes != 4 || am_sender || am_generator) | |
1311 | goto invalid_msg; | |
1312 | data = perform_io(4, PIO_INPUT_AND_CONSUME); | |
1313 | val = IVAL(data, 0); | |
1314 | if (!io_timeout || io_timeout > val) { | |
1315 | if (INFO_GTE(MISC, 2)) | |
1316 | rprintf(FINFO, "Setting --timeout=%d to match server\n", val); | |
1317 | io_timeout = val; | |
1318 | } | |
1319 | break; | |
d8587b46 WD |
1320 | case MSG_NOOP: |
1321 | if (am_sender) | |
1322 | maybe_send_keepalive(); | |
1323 | break; | |
d8587b46 WD |
1324 | case MSG_DELETED: |
1325 | if (msg_bytes >= sizeof line) | |
1326 | goto overflow; | |
1327 | if (am_generator) { | |
20caffd2 | 1328 | memcpy(line, perform_io(msg_bytes, PIO_INPUT_AND_CONSUME), msg_bytes); |
d8587b46 WD |
1329 | send_msg(MSG_DELETED, line, msg_bytes, 1); |
1330 | break; | |
1331 | } | |
1332 | #ifdef ICONV_OPTION | |
1333 | if (ic_recv != (iconv_t)-1) { | |
1334 | xbuf outbuf, inbuf; | |
1335 | char ibuf[512]; | |
1336 | int add_null = 0; | |
cbc63a09 | 1337 | int flags = ICB_INCLUDE_BAD | ICB_INIT; |
d8587b46 WD |
1338 | |
1339 | INIT_CONST_XBUF(outbuf, line); | |
1340 | INIT_XBUF(inbuf, ibuf, 0, (size_t)-1); | |
1341 | ||
1342 | while (msg_bytes) { | |
cbc63a09 WD |
1343 | size_t len = msg_bytes > sizeof ibuf - inbuf.len ? sizeof ibuf - inbuf.len : msg_bytes; |
1344 | memcpy(ibuf + inbuf.len, perform_io(len, PIO_INPUT_AND_CONSUME), len); | |
d8a7290f | 1345 | inbuf.pos = 0; |
cbc63a09 WD |
1346 | inbuf.len += len; |
1347 | if (!(msg_bytes -= len) && !ibuf[inbuf.len-1]) | |
d8587b46 | 1348 | inbuf.len--, add_null = 1; |
cbc63a09 WD |
1349 | if (iconvbufs(ic_send, &inbuf, &outbuf, flags) < 0) { |
1350 | if (errno == E2BIG) | |
1351 | goto overflow; | |
1352 | /* Buffer ended with an incomplete char, so move the | |
1353 | * bytes to the start of the buffer and continue. */ | |
1354 | memmove(ibuf, ibuf + inbuf.pos, inbuf.len); | |
1355 | } | |
1356 | flags &= ~ICB_INIT; | |
d8587b46 WD |
1357 | } |
1358 | if (add_null) { | |
1359 | if (outbuf.len == outbuf.size) | |
1360 | goto overflow; | |
1361 | outbuf.buf[outbuf.len++] = '\0'; | |
1362 | } | |
1363 | msg_bytes = outbuf.len; | |
1364 | } else | |
1365 | #endif | |
20caffd2 | 1366 | memcpy(line, perform_io(msg_bytes, PIO_INPUT_AND_CONSUME), msg_bytes); |
d8587b46 WD |
1367 | /* A directory name was sent with the trailing null */ |
1368 | if (msg_bytes > 0 && !line[msg_bytes-1]) | |
1369 | log_delete(line, S_IFDIR); | |
1370 | else { | |
1371 | line[msg_bytes] = '\0'; | |
1372 | log_delete(line, S_IFREG); | |
1373 | } | |
1374 | break; | |
1375 | case MSG_SUCCESS: | |
1376 | if (msg_bytes != 4) { | |
1377 | invalid_msg: | |
1378 | rprintf(FERROR, "invalid multi-message %d:%lu [%s%s]\n", | |
1379 | tag, (unsigned long)msg_bytes, who_am_i(), | |
1380 | inc_recurse ? "/inc" : ""); | |
1381 | exit_cleanup(RERR_STREAMIO); | |
1382 | } | |
20caffd2 | 1383 | data = perform_io(4, PIO_INPUT_AND_CONSUME); |
d8587b46 | 1384 | if (am_generator) |
20caffd2 | 1385 | got_flist_entry_status(FES_SUCCESS, IVAL(data, 0)); |
d8587b46 | 1386 | else |
20caffd2 | 1387 | successful_send(IVAL(data, 0)); |
d8587b46 WD |
1388 | break; |
1389 | case MSG_NO_SEND: | |
1390 | if (msg_bytes != 4) | |
1391 | goto invalid_msg; | |
20caffd2 | 1392 | data = perform_io(4, PIO_INPUT_AND_CONSUME); |
d8587b46 | 1393 | if (am_generator) |
20caffd2 | 1394 | got_flist_entry_status(FES_NO_SEND, IVAL(data, 0)); |
d8587b46 | 1395 | else |
20caffd2 | 1396 | send_msg(MSG_NO_SEND, data, 4, 0); |
d8587b46 WD |
1397 | break; |
1398 | case MSG_ERROR_SOCKET: | |
1399 | case MSG_ERROR_UTF8: | |
1400 | case MSG_CLIENT: | |
1401 | case MSG_LOG: | |
1402 | if (!am_generator) | |
1403 | goto invalid_msg; | |
1404 | if (tag == MSG_ERROR_SOCKET) | |
20caffd2 | 1405 | msgs2stderr = 1; |
d8587b46 WD |
1406 | /* FALL THROUGH */ |
1407 | case MSG_INFO: | |
1408 | case MSG_ERROR: | |
1409 | case MSG_ERROR_XFER: | |
1410 | case MSG_WARNING: | |
1411 | if (msg_bytes >= sizeof line) { | |
1412 | overflow: | |
1413 | rprintf(FERROR, | |
1414 | "multiplexing overflow %d:%lu [%s%s]\n", | |
1415 | tag, (unsigned long)msg_bytes, who_am_i(), | |
1416 | inc_recurse ? "/inc" : ""); | |
1417 | exit_cleanup(RERR_STREAMIO); | |
1418 | } | |
20caffd2 | 1419 | memcpy(line, perform_io(msg_bytes, PIO_INPUT_AND_CONSUME), msg_bytes); |
d8587b46 WD |
1420 | rwrite((enum logcode)tag, line, msg_bytes, !am_generator); |
1421 | if (first_message) { | |
20caffd2 | 1422 | if (list_only && !am_sender && tag == 1 && msg_bytes < sizeof line) { |
d8587b46 WD |
1423 | line[msg_bytes] = '\0'; |
1424 | check_for_d_option_error(line); | |
1425 | } | |
1426 | first_message = 0; | |
1427 | } | |
1428 | break; | |
1429 | default: | |
1430 | rprintf(FERROR, "unexpected tag %d [%s%s]\n", | |
1431 | tag, who_am_i(), inc_recurse ? "/inc" : ""); | |
1432 | exit_cleanup(RERR_STREAMIO); | |
1433 | } | |
d8587b46 WD |
1434 | } |
1435 | ||
20caffd2 | 1436 | void wait_for_receiver(void) |
d8587b46 | 1437 | { |
20caffd2 WD |
1438 | if (!iobuf.raw_input_ends_before) |
1439 | read_a_msg(); | |
1440 | ||
1441 | if (iobuf.raw_input_ends_before) { | |
1442 | int ndx = read_int(iobuf.in_fd); | |
1443 | if (ndx < 0) { | |
1444 | switch (ndx) { | |
1445 | case NDX_FLIST_EOF: | |
1446 | flist_eof = 1; | |
1447 | if (DEBUG_GTE(FLIST, 3)) | |
1448 | rprintf(FINFO, "[%s] flist_eof=1\n", who_am_i()); | |
1449 | break; | |
1450 | case NDX_DONE: | |
1451 | msgdone_cnt++; | |
1452 | break; | |
1453 | default: | |
1454 | exit_cleanup(RERR_STREAMIO); | |
1455 | } | |
1456 | } else { | |
1457 | struct file_list *flist; | |
1458 | if (DEBUG_GTE(FLIST, 2)) { | |
1459 | rprintf(FINFO, "[%s] receiving flist for dir %d\n", | |
1460 | who_am_i(), ndx); | |
1461 | } | |
1462 | flist = recv_file_list(iobuf.in_fd); | |
1463 | flist->parent_ndx = ndx; | |
1464 | #ifdef SUPPORT_HARD_LINKS | |
1465 | if (preserve_hard_links) | |
1466 | match_hard_links(flist); | |
1467 | #endif | |
8d9dc9f9 | 1468 | } |
b9f592fb | 1469 | } |
720b47f2 AT |
1470 | } |
1471 | ||
3a993aa4 | 1472 | unsigned short read_shortint(int f) |
9361f839 | 1473 | { |
482f48cc | 1474 | char b[2]; |
20caffd2 | 1475 | read_buf(f, b, 2); |
482f48cc | 1476 | return (UVAL(b, 1) << 8) + UVAL(b, 0); |
9361f839 WD |
1477 | } |
1478 | ||
b7922338 | 1479 | int32 read_int(int f) |
720b47f2 | 1480 | { |
4c36ddbe | 1481 | char b[4]; |
d8aeda1e | 1482 | int32 num; |
d730b113 | 1483 | |
20caffd2 | 1484 | read_buf(f, b, 4); |
482f48cc WD |
1485 | num = IVAL(b, 0); |
1486 | #if SIZEOF_INT32 > 4 | |
1487 | if (num & (int32)0x80000000) | |
1488 | num |= ~(int32)0xffffffff; | |
1489 | #endif | |
d8aeda1e | 1490 | return num; |
720b47f2 AT |
1491 | } |
1492 | ||
351e23ad | 1493 | int32 read_varint(int f) |
3a6a366f | 1494 | { |
351e23ad | 1495 | union { |
20caffd2 WD |
1496 | char b[5]; |
1497 | int32 x; | |
351e23ad WD |
1498 | } u; |
1499 | uchar ch; | |
1500 | int extra; | |
1501 | ||
1502 | u.x = 0; | |
20caffd2 | 1503 | ch = read_byte(f); |
351e23ad WD |
1504 | extra = int_byte_extra[ch / 4]; |
1505 | if (extra) { | |
1506 | uchar bit = ((uchar)1<<(8-extra)); | |
1507 | if (extra >= (int)sizeof u.b) { | |
1508 | rprintf(FERROR, "Overflow in read_varint()\n"); | |
1509 | exit_cleanup(RERR_STREAMIO); | |
1510 | } | |
20caffd2 | 1511 | read_buf(f, u.b, extra); |
351e23ad WD |
1512 | u.b[extra] = ch & (bit-1); |
1513 | } else | |
1514 | u.b[0] = ch; | |
1515 | #if CAREFUL_ALIGNMENT | |
1516 | u.x = IVAL(u.b,0); | |
1517 | #endif | |
1518 | #if SIZEOF_INT32 > 4 | |
1519 | if (u.x & (int32)0x80000000) | |
1520 | u.x |= ~(int32)0xffffffff; | |
1521 | #endif | |
1522 | return u.x; | |
1523 | } | |
71c46176 | 1524 | |
351e23ad WD |
1525 | int64 read_varlong(int f, uchar min_bytes) |
1526 | { | |
1527 | union { | |
20caffd2 WD |
1528 | char b[9]; |
1529 | int64 x; | |
351e23ad WD |
1530 | } u; |
1531 | char b2[8]; | |
1532 | int extra; | |
71c46176 | 1533 | |
031fa9ad | 1534 | #if SIZEOF_INT64 < 8 |
351e23ad | 1535 | memset(u.b, 0, 8); |
031fa9ad | 1536 | #else |
351e23ad | 1537 | u.x = 0; |
031fa9ad | 1538 | #endif |
20caffd2 | 1539 | read_buf(f, b2, min_bytes); |
351e23ad WD |
1540 | memcpy(u.b, b2+1, min_bytes-1); |
1541 | extra = int_byte_extra[CVAL(b2, 0) / 4]; | |
1542 | if (extra) { | |
1543 | uchar bit = ((uchar)1<<(8-extra)); | |
1544 | if (min_bytes + extra > (int)sizeof u.b) { | |
1545 | rprintf(FERROR, "Overflow in read_varlong()\n"); | |
1546 | exit_cleanup(RERR_STREAMIO); | |
1547 | } | |
20caffd2 | 1548 | read_buf(f, u.b + min_bytes - 1, extra); |
351e23ad | 1549 | u.b[min_bytes + extra - 1] = CVAL(b2, 0) & (bit-1); |
4ea4acf1 | 1550 | #if SIZEOF_INT64 < 8 |
351e23ad | 1551 | if (min_bytes + extra > 5 || u.b[4] || CVAL(u.b,3) & 0x80) { |
4ea4acf1 WD |
1552 | rprintf(FERROR, "Integer overflow: attempted 64-bit offset\n"); |
1553 | exit_cleanup(RERR_UNSUPPORTED); | |
1554 | } | |
1555 | #endif | |
351e23ad WD |
1556 | } else |
1557 | u.b[min_bytes + extra - 1] = CVAL(b2, 0); | |
1558 | #if SIZEOF_INT64 < 8 | |
1559 | u.x = IVAL(u.b,0); | |
1560 | #elif CAREFUL_ALIGNMENT | |
1561 | u.x = IVAL(u.b,0) | (((int64)IVAL(u.b,4))<<32); | |
1562 | #endif | |
1563 | return u.x; | |
1564 | } | |
1565 | ||
1566 | int64 read_longint(int f) | |
1567 | { | |
4ea4acf1 | 1568 | #if SIZEOF_INT64 >= 8 |
351e23ad | 1569 | char b[9]; |
4ea4acf1 | 1570 | #endif |
351e23ad | 1571 | int32 num = read_int(f); |
71c46176 | 1572 | |
351e23ad WD |
1573 | if (num != (int32)0xffffffff) |
1574 | return num; | |
1575 | ||
1576 | #if SIZEOF_INT64 < 8 | |
1577 | rprintf(FERROR, "Integer overflow: attempted 64-bit offset\n"); | |
1578 | exit_cleanup(RERR_UNSUPPORTED); | |
1579 | #else | |
20caffd2 | 1580 | read_buf(f, b, 8); |
351e23ad WD |
1581 | return IVAL(b,0) | (((int64)IVAL(b,4))<<32); |
1582 | #endif | |
3a6a366f AT |
1583 | } |
1584 | ||
4a19c3b2 | 1585 | void read_buf(int f, char *buf, size_t len) |
720b47f2 | 1586 | { |
20caffd2 WD |
1587 | if (f != iobuf.in_fd) { |
1588 | if (safe_read(f, buf, len) != len) | |
1589 | whine_about_eof(f); /* Doesn't return. */ | |
1590 | goto batch_copy; | |
1591 | } | |
1592 | ||
1593 | if (!IN_MULTIPLEXED) { | |
1594 | memcpy(buf, perform_io(len, PIO_INPUT_AND_CONSUME), len); | |
4dde3347 | 1595 | total_data_read += len; |
20caffd2 WD |
1596 | if (forward_flist_data) |
1597 | write_buf(iobuf.out_fd, buf, len); | |
1598 | batch_copy: | |
1599 | if (f == write_batch_monitor_in) | |
1600 | safe_write(batch_fd, buf, len); | |
1601 | return; | |
1602 | } | |
1603 | ||
1604 | while (1) { | |
1605 | char *data; | |
1606 | size_t siz; | |
1607 | ||
1608 | while (!iobuf.raw_input_ends_before) | |
1609 | read_a_msg(); | |
1610 | ||
1611 | siz = MIN(len, iobuf.raw_input_ends_before - iobuf.in.pos); | |
1612 | data = perform_io(siz, PIO_INPUT_AND_CONSUME); | |
1613 | if (iobuf.in.pos == iobuf.raw_input_ends_before) | |
1614 | iobuf.raw_input_ends_before = 0; | |
1615 | ||
1616 | /* The bytes at the "data" pointer will survive long | |
1617 | * enough to make a copy, but not past future I/O. */ | |
1618 | memcpy(buf, data, siz); | |
4dde3347 | 1619 | total_data_read += siz; |
20caffd2 WD |
1620 | |
1621 | if (forward_flist_data) | |
1622 | write_buf(iobuf.out_fd, buf, siz); | |
1623 | ||
1624 | if (f == write_batch_monitor_in) | |
1625 | safe_write(batch_fd, buf, siz); | |
1626 | ||
1627 | if ((len -= siz) == 0) | |
1628 | break; | |
1629 | buf += siz; | |
1630 | } | |
720b47f2 AT |
1631 | } |
1632 | ||
4a19c3b2 | 1633 | void read_sbuf(int f, char *buf, size_t len) |
575f2fca | 1634 | { |
20caffd2 | 1635 | read_buf(f, buf, len); |
af436313 | 1636 | buf[len] = '\0'; |
575f2fca AT |
1637 | } |
1638 | ||
9361f839 | 1639 | uchar read_byte(int f) |
182dca5c | 1640 | { |
9361f839 | 1641 | uchar c; |
20caffd2 | 1642 | read_buf(f, (char*)&c, 1); |
4c36ddbe | 1643 | return c; |
182dca5c | 1644 | } |
720b47f2 | 1645 | |
46e99b09 WD |
1646 | int read_vstring(int f, char *buf, int bufsize) |
1647 | { | |
1648 | int len = read_byte(f); | |
1649 | ||
1650 | if (len & 0x80) | |
1651 | len = (len & ~0x80) * 0x100 + read_byte(f); | |
1652 | ||
1653 | if (len >= bufsize) { | |
1654 | rprintf(FERROR, "over-long vstring received (%d > %d)\n", | |
1655 | len, bufsize - 1); | |
9a6ed83f | 1656 | return -1; |
46e99b09 WD |
1657 | } |
1658 | ||
1659 | if (len) | |
20caffd2 | 1660 | read_buf(f, buf, len); |
46e99b09 WD |
1661 | buf[len] = '\0'; |
1662 | return len; | |
1663 | } | |
1664 | ||
188fed95 WD |
1665 | /* Populate a sum_struct with values from the socket. This is |
1666 | * called by both the sender and the receiver. */ | |
1667 | void read_sum_head(int f, struct sum_struct *sum) | |
1668 | { | |
8bd77e70 | 1669 | int32 max_blength = protocol_version < 30 ? OLD_MAX_BLOCK_SIZE : MAX_BLOCK_SIZE; |
188fed95 | 1670 | sum->count = read_int(f); |
c638caa6 WD |
1671 | if (sum->count < 0) { |
1672 | rprintf(FERROR, "Invalid checksum count %ld [%s]\n", | |
1673 | (long)sum->count, who_am_i()); | |
1674 | exit_cleanup(RERR_PROTOCOL); | |
1675 | } | |
188fed95 | 1676 | sum->blength = read_int(f); |
8bd77e70 | 1677 | if (sum->blength < 0 || sum->blength > max_blength) { |
cdf236aa WD |
1678 | rprintf(FERROR, "Invalid block length %ld [%s]\n", |
1679 | (long)sum->blength, who_am_i()); | |
188fed95 WD |
1680 | exit_cleanup(RERR_PROTOCOL); |
1681 | } | |
1682 | sum->s2length = protocol_version < 27 ? csum_length : (int)read_int(f); | |
a0456b9c | 1683 | if (sum->s2length < 0 || sum->s2length > MAX_DIGEST_LEN) { |
cdf236aa WD |
1684 | rprintf(FERROR, "Invalid checksum length %d [%s]\n", |
1685 | sum->s2length, who_am_i()); | |
188fed95 WD |
1686 | exit_cleanup(RERR_PROTOCOL); |
1687 | } | |
1688 | sum->remainder = read_int(f); | |
1689 | if (sum->remainder < 0 || sum->remainder > sum->blength) { | |
cdf236aa WD |
1690 | rprintf(FERROR, "Invalid remainder length %ld [%s]\n", |
1691 | (long)sum->remainder, who_am_i()); | |
188fed95 WD |
1692 | exit_cleanup(RERR_PROTOCOL); |
1693 | } | |
1694 | } | |
1695 | ||
c207d7ec WD |
1696 | /* Send the values from a sum_struct over the socket. Set sum to |
1697 | * NULL if there are no checksums to send. This is called by both | |
1698 | * the generator and the sender. */ | |
1699 | void write_sum_head(int f, struct sum_struct *sum) | |
1700 | { | |
1701 | static struct sum_struct null_sum; | |
1702 | ||
1703 | if (sum == NULL) | |
1704 | sum = &null_sum; | |
1705 | ||
1706 | write_int(f, sum->count); | |
1707 | write_int(f, sum->blength); | |
1708 | if (protocol_version >= 27) | |
1709 | write_int(f, sum->s2length); | |
1710 | write_int(f, sum->remainder); | |
1711 | } | |
1712 | ||
20caffd2 | 1713 | /* Sleep after writing to limit I/O bandwidth usage. |
08571358 MP |
1714 | * |
1715 | * @todo Rather than sleeping after each write, it might be better to | |
1716 | * use some kind of averaging. The current algorithm seems to always | |
1717 | * use a bit less bandwidth than specified, because it doesn't make up | |
1718 | * for slow periods. But arguably this is a feature. In addition, we | |
1719 | * ought to take the time used to write the data into account. | |
71e58630 WD |
1720 | * |
1721 | * During some phases of big transfers (file FOO is uptodate) this is | |
1722 | * called with a small bytes_written every time. As the kernel has to | |
1723 | * round small waits up to guarantee that we actually wait at least the | |
1724 | * requested number of microseconds, this can become grossly inaccurate. | |
1725 | * We therefore keep track of the bytes we've written over time and only | |
20caffd2 | 1726 | * sleep when the accumulated delay is at least 1 tenth of a second. */ |
08571358 MP |
1727 | static void sleep_for_bwlimit(int bytes_written) |
1728 | { | |
71e58630 | 1729 | static struct timeval prior_tv; |
0f78b815 | 1730 | static long total_written = 0; |
71e58630 WD |
1731 | struct timeval tv, start_tv; |
1732 | long elapsed_usec, sleep_usec; | |
1733 | ||
1734 | #define ONE_SEC 1000000L /* # of microseconds in a second */ | |
08571358 | 1735 | |
0f78b815 | 1736 | total_written += bytes_written; |
71e58630 WD |
1737 | |
1738 | gettimeofday(&start_tv, NULL); | |
1739 | if (prior_tv.tv_sec) { | |
1740 | elapsed_usec = (start_tv.tv_sec - prior_tv.tv_sec) * ONE_SEC | |
1741 | + (start_tv.tv_usec - prior_tv.tv_usec); | |
1742 | total_written -= elapsed_usec * bwlimit / (ONE_SEC/1024); | |
1743 | if (total_written < 0) | |
1744 | total_written = 0; | |
1745 | } | |
3151cbae | 1746 | |
71e58630 WD |
1747 | sleep_usec = total_written * (ONE_SEC/1024) / bwlimit; |
1748 | if (sleep_usec < ONE_SEC / 10) { | |
1749 | prior_tv = start_tv; | |
1750 | return; | |
1751 | } | |
08571358 | 1752 | |
71e58630 WD |
1753 | tv.tv_sec = sleep_usec / ONE_SEC; |
1754 | tv.tv_usec = sleep_usec % ONE_SEC; | |
98b332ed | 1755 | select(0, NULL, NULL, NULL, &tv); |
71e58630 WD |
1756 | |
1757 | gettimeofday(&prior_tv, NULL); | |
1758 | elapsed_usec = (prior_tv.tv_sec - start_tv.tv_sec) * ONE_SEC | |
1759 | + (prior_tv.tv_usec - start_tv.tv_usec); | |
1760 | total_written = (sleep_usec - elapsed_usec) * bwlimit / (ONE_SEC/1024); | |
08571358 MP |
1761 | } |
1762 | ||
20caffd2 | 1763 | void io_flush(int flush_it_all) |
d6dead6b | 1764 | { |
20caffd2 WD |
1765 | if (iobuf.out.len > iobuf.out_empty_len) { |
1766 | if (flush_it_all) /* FULL_FLUSH: flush everything in the output buffers */ | |
1767 | perform_io(iobuf.out.size - iobuf.out_empty_len, PIO_NEED_OUTROOM); | |
1768 | else /* NORMAL_FLUSH: flush at least 1 byte */ | |
1769 | perform_io(iobuf.out.size - iobuf.out.len + 1, PIO_NEED_OUTROOM); | |
d6dead6b | 1770 | } |
20caffd2 WD |
1771 | if (iobuf.msg.len) |
1772 | perform_io(iobuf.msg.size, PIO_NEED_MSGROOM); | |
d6dead6b | 1773 | } |
720b47f2 | 1774 | |
3a993aa4 | 1775 | void write_shortint(int f, unsigned short x) |
9361f839 | 1776 | { |
3a993aa4 WD |
1777 | char b[2]; |
1778 | b[0] = (char)x; | |
1779 | b[1] = (char)(x >> 8); | |
20caffd2 | 1780 | write_buf(f, b, 2); |
9361f839 WD |
1781 | } |
1782 | ||
351e23ad WD |
1783 | void write_int(int f, int32 x) |
1784 | { | |
1785 | char b[4]; | |
1786 | SIVAL(b, 0, x); | |
20caffd2 | 1787 | write_buf(f, b, 4); |
351e23ad WD |
1788 | } |
1789 | ||
f31514ad | 1790 | void write_varint(int f, int32 x) |
1c3344a1 WD |
1791 | { |
1792 | char b[5]; | |
351e23ad WD |
1793 | uchar bit; |
1794 | int cnt = 4; | |
1795 | ||
1796 | SIVAL(b, 1, x); | |
1797 | ||
1798 | while (cnt > 1 && b[cnt] == 0) | |
1799 | cnt--; | |
1800 | bit = ((uchar)1<<(7-cnt+1)); | |
1801 | if (CVAL(b, cnt) >= bit) { | |
1802 | cnt++; | |
1803 | *b = ~(bit-1); | |
1804 | } else if (cnt > 1) | |
1805 | *b = b[cnt] | ~(bit*2-1); | |
1806 | else | |
1807 | *b = b[cnt]; | |
1808 | ||
20caffd2 | 1809 | write_buf(f, b, cnt); |
1c3344a1 WD |
1810 | } |
1811 | ||
351e23ad | 1812 | void write_varlong(int f, int64 x, uchar min_bytes) |
720b47f2 | 1813 | { |
351e23ad WD |
1814 | char b[9]; |
1815 | uchar bit; | |
1816 | int cnt = 8; | |
1817 | ||
1818 | SIVAL(b, 1, x); | |
1819 | #if SIZEOF_INT64 >= 8 | |
1820 | SIVAL(b, 5, x >> 32); | |
1821 | #else | |
1822 | if (x <= 0x7FFFFFFF && x >= 0) | |
1823 | memset(b + 5, 0, 4); | |
1824 | else { | |
1825 | rprintf(FERROR, "Integer overflow: attempted 64-bit offset\n"); | |
1826 | exit_cleanup(RERR_UNSUPPORTED); | |
1827 | } | |
1828 | #endif | |
1829 | ||
1830 | while (cnt > min_bytes && b[cnt] == 0) | |
1831 | cnt--; | |
1832 | bit = ((uchar)1<<(7-cnt+min_bytes)); | |
1833 | if (CVAL(b, cnt) >= bit) { | |
1834 | cnt++; | |
1835 | *b = ~(bit-1); | |
1836 | } else if (cnt > min_bytes) | |
1837 | *b = b[cnt] | ~(bit*2-1); | |
1838 | else | |
1839 | *b = b[cnt]; | |
1840 | ||
20caffd2 | 1841 | write_buf(f, b, cnt); |
720b47f2 AT |
1842 | } |
1843 | ||
7a24c346 MP |
1844 | /* |
1845 | * Note: int64 may actually be a 32-bit type if ./configure couldn't find any | |
1846 | * 64-bit types on this platform. | |
1847 | */ | |
71c46176 | 1848 | void write_longint(int f, int64 x) |
3a6a366f | 1849 | { |
351e23ad | 1850 | char b[12], * const s = b+4; |
3a6a366f | 1851 | |
351e23ad WD |
1852 | SIVAL(s, 0, x); |
1853 | if (x <= 0x7FFFFFFF && x >= 0) { | |
20caffd2 | 1854 | write_buf(f, s, 4); |
351e23ad WD |
1855 | return; |
1856 | } | |
3a6a366f | 1857 | |
4ea4acf1 | 1858 | #if SIZEOF_INT64 < 8 |
351e23ad WD |
1859 | rprintf(FERROR, "Integer overflow: attempted 64-bit offset\n"); |
1860 | exit_cleanup(RERR_UNSUPPORTED); | |
4ea4acf1 | 1861 | #else |
351e23ad WD |
1862 | memset(b, 0xFF, 4); |
1863 | SIVAL(s, 4, x >> 32); | |
20caffd2 | 1864 | write_buf(f, b, 12); |
4ea4acf1 | 1865 | #endif |
3a6a366f AT |
1866 | } |
1867 | ||
4a19c3b2 | 1868 | void write_buf(int f, const char *buf, size_t len) |
720b47f2 | 1869 | { |
20caffd2 WD |
1870 | size_t pos, siz; |
1871 | ||
1872 | if (f != iobuf.out_fd) { | |
1873 | safe_write(f, buf, len); | |
1874 | goto batch_copy; | |
1875 | } | |
1876 | ||
1877 | if (iobuf.out.size - iobuf.out.len < len) | |
1878 | perform_io(len, PIO_NEED_OUTROOM); | |
1879 | ||
1880 | pos = iobuf.out.pos + iobuf.out.len; /* Must be set after any flushing. */ | |
1881 | if (pos >= iobuf.out.size) | |
1882 | pos -= iobuf.out.size; | |
1883 | ||
1884 | /* Handle a split copy if we wrap around the end of the circular buffer. */ | |
1885 | if (pos >= iobuf.out.pos && (siz = iobuf.out.size - pos) < len) { | |
1886 | memcpy(iobuf.out.buf + pos, buf, siz); | |
1887 | memcpy(iobuf.out.buf, buf + siz, len - siz); | |
1888 | } else | |
1889 | memcpy(iobuf.out.buf + pos, buf, len); | |
1890 | ||
1891 | iobuf.out.len += len; | |
4dde3347 | 1892 | total_data_written += len; |
20caffd2 WD |
1893 | |
1894 | batch_copy: | |
1895 | if (f == write_batch_monitor_out) | |
1896 | safe_write(batch_fd, buf, len); | |
720b47f2 AT |
1897 | } |
1898 | ||
20caffd2 | 1899 | /* Write a string to the connection */ |
4a19c3b2 | 1900 | void write_sbuf(int f, const char *buf) |
f0fca04e | 1901 | { |
20caffd2 | 1902 | write_buf(f, buf, strlen(buf)); |
f0fca04e AT |
1903 | } |
1904 | ||
9361f839 | 1905 | void write_byte(int f, uchar c) |
182dca5c | 1906 | { |
20caffd2 | 1907 | write_buf(f, (char *)&c, 1); |
182dca5c AT |
1908 | } |
1909 | ||
4a19c3b2 | 1910 | void write_vstring(int f, const char *str, int len) |
46e99b09 WD |
1911 | { |
1912 | uchar lenbuf[3], *lb = lenbuf; | |
1913 | ||
1914 | if (len > 0x7F) { | |
1915 | if (len > 0x7FFF) { | |
1916 | rprintf(FERROR, | |
1917 | "attempting to send over-long vstring (%d > %d)\n", | |
1918 | len, 0x7FFF); | |
1919 | exit_cleanup(RERR_PROTOCOL); | |
1920 | } | |
1921 | *lb++ = len / 0x100 + 0x80; | |
1922 | } | |
1923 | *lb = len; | |
1924 | ||
20caffd2 | 1925 | write_buf(f, (char*)lenbuf, lb - lenbuf + 1); |
46e99b09 | 1926 | if (len) |
20caffd2 | 1927 | write_buf(f, str, len); |
46e99b09 WD |
1928 | } |
1929 | ||
8a65e0ce WD |
1930 | /* Send a file-list index using a byte-reduction method. */ |
1931 | void write_ndx(int f, int32 ndx) | |
1932 | { | |
1933 | static int32 prev_positive = -1, prev_negative = 1; | |
1934 | int32 diff, cnt = 0; | |
1935 | char b[6]; | |
1936 | ||
1937 | if (protocol_version < 30 || read_batch) { | |
1938 | write_int(f, ndx); | |
1939 | return; | |
1940 | } | |
1941 | ||
1942 | /* Send NDX_DONE as a single-byte 0 with no side effects. Send | |
1943 | * negative nums as a positive after sending a leading 0xFF. */ | |
1944 | if (ndx >= 0) { | |
1945 | diff = ndx - prev_positive; | |
1946 | prev_positive = ndx; | |
1947 | } else if (ndx == NDX_DONE) { | |
1948 | *b = 0; | |
20caffd2 | 1949 | write_buf(f, b, 1); |
8a65e0ce WD |
1950 | return; |
1951 | } else { | |
1952 | b[cnt++] = (char)0xFF; | |
1953 | ndx = -ndx; | |
1954 | diff = ndx - prev_negative; | |
1955 | prev_negative = ndx; | |
1956 | } | |
1957 | ||
1958 | /* A diff of 1 - 253 is sent as a one-byte diff; a diff of 254 - 32767 | |
1959 | * or 0 is sent as a 0xFE + a two-byte diff; otherwise we send 0xFE | |
1960 | * & all 4 bytes of the (non-negative) num with the high-bit set. */ | |
1961 | if (diff < 0xFE && diff > 0) | |
1962 | b[cnt++] = (char)diff; | |
1963 | else if (diff < 0 || diff > 0x7FFF) { | |
1964 | b[cnt++] = (char)0xFE; | |
1965 | b[cnt++] = (char)((ndx >> 24) | 0x80); | |
8a65e0ce | 1966 | b[cnt++] = (char)ndx; |
351e23ad WD |
1967 | b[cnt++] = (char)(ndx >> 8); |
1968 | b[cnt++] = (char)(ndx >> 16); | |
8a65e0ce WD |
1969 | } else { |
1970 | b[cnt++] = (char)0xFE; | |
1971 | b[cnt++] = (char)(diff >> 8); | |
1972 | b[cnt++] = (char)diff; | |
1973 | } | |
20caffd2 | 1974 | write_buf(f, b, cnt); |
8a65e0ce WD |
1975 | } |
1976 | ||
1977 | /* Receive a file-list index using a byte-reduction method. */ | |
1978 | int32 read_ndx(int f) | |
1979 | { | |
1980 | static int32 prev_positive = -1, prev_negative = 1; | |
1981 | int32 *prev_ptr, num; | |
1982 | char b[4]; | |
1983 | ||
1984 | if (protocol_version < 30) | |
1985 | return read_int(f); | |
1986 | ||
20caffd2 | 1987 | read_buf(f, b, 1); |
8a65e0ce | 1988 | if (CVAL(b, 0) == 0xFF) { |
20caffd2 | 1989 | read_buf(f, b, 1); |
8a65e0ce WD |
1990 | prev_ptr = &prev_negative; |
1991 | } else if (CVAL(b, 0) == 0) | |
1992 | return NDX_DONE; | |
1993 | else | |
1994 | prev_ptr = &prev_positive; | |
1995 | if (CVAL(b, 0) == 0xFE) { | |
20caffd2 | 1996 | read_buf(f, b, 2); |
8a65e0ce | 1997 | if (CVAL(b, 0) & 0x80) { |
351e23ad WD |
1998 | b[3] = CVAL(b, 0) & ~0x80; |
1999 | b[0] = b[1]; | |
20caffd2 | 2000 | read_buf(f, b+1, 2); |
351e23ad | 2001 | num = IVAL(b, 0); |
8a65e0ce | 2002 | } else |
351e23ad | 2003 | num = (UVAL(b,0)<<8) + UVAL(b,1) + *prev_ptr; |
8a65e0ce | 2004 | } else |
351e23ad | 2005 | num = UVAL(b, 0) + *prev_ptr; |
8a65e0ce WD |
2006 | *prev_ptr = num; |
2007 | if (prev_ptr == &prev_negative) | |
2008 | num = -num; | |
2009 | return num; | |
2010 | } | |
2011 | ||
67b8f3df WD |
2012 | /* Read a line of up to bufsiz-1 characters into buf. Strips |
2013 | * the (required) trailing newline and all carriage returns. | |
2014 | * Returns 1 for success; 0 for I/O error or truncation. */ | |
20caffd2 | 2015 | int read_line_old(int fd, char *buf, size_t bufsiz) |
f0fca04e | 2016 | { |
67b8f3df WD |
2017 | bufsiz--; /* leave room for the null */ |
2018 | while (bufsiz > 0) { | |
20caffd2 WD |
2019 | assert(fd != iobuf.in_fd); |
2020 | if (safe_read(fd, buf, 1) == 0) | |
2021 | return 0; | |
2022 | if (*buf == '\0') | |
914cc65c | 2023 | return 0; |
20caffd2 | 2024 | if (*buf == '\n') |
f0fca04e | 2025 | break; |
20caffd2 | 2026 | if (*buf != '\r') { |
f0fca04e | 2027 | buf++; |
67b8f3df | 2028 | bufsiz--; |
f0fca04e AT |
2029 | } |
2030 | } | |
6ed6d7f5 | 2031 | *buf = '\0'; |
67b8f3df | 2032 | return bufsiz > 0; |
f0fca04e AT |
2033 | } |
2034 | ||
f0fca04e AT |
2035 | void io_printf(int fd, const char *format, ...) |
2036 | { | |
d62bcc17 | 2037 | va_list ap; |
33544bf4 | 2038 | char buf[BIGPATHBUFLEN]; |
f0fca04e | 2039 | int len; |
3151cbae | 2040 | |
f0fca04e | 2041 | va_start(ap, format); |
3151cbae | 2042 | len = vsnprintf(buf, sizeof buf, format, ap); |
f0fca04e AT |
2043 | va_end(ap); |
2044 | ||
f89b9368 WD |
2045 | if (len < 0) |
2046 | exit_cleanup(RERR_STREAMIO); | |
f0fca04e | 2047 | |
33544bf4 WD |
2048 | if (len > (int)sizeof buf) { |
2049 | rprintf(FERROR, "io_printf() was too long for the buffer.\n"); | |
2050 | exit_cleanup(RERR_STREAMIO); | |
2051 | } | |
2052 | ||
f0fca04e AT |
2053 | write_sbuf(fd, buf); |
2054 | } | |
8d9dc9f9 | 2055 | |
20caffd2 WD |
2056 | /* Setup for multiplexing a MSG_* stream with the data stream. */ |
2057 | void io_start_multiplex_out(int fd) | |
8d9dc9f9 | 2058 | { |
705132bc WD |
2059 | io_flush(FULL_FLUSH); |
2060 | ||
20caffd2 WD |
2061 | if (msgs2stderr && DEBUG_GTE(IO, 2)) |
2062 | rprintf(FINFO, "[%s] io_start_multiplex_out(%d)\n", who_am_i(), fd); | |
8d9dc9f9 | 2063 | |
20caffd2 WD |
2064 | iobuf.out_empty_len = 4; /* See also OUT_MULTIPLEXED */ |
2065 | io_start_buffering_out(fd); | |
2066 | ||
2067 | iobuf.raw_data_header_pos = iobuf.out.pos + iobuf.out.len; | |
2068 | iobuf.out.len += 4; | |
8d9dc9f9 AT |
2069 | } |
2070 | ||
20caffd2 WD |
2071 | /* Setup for multiplexing a MSG_* stream with the data stream. */ |
2072 | void io_start_multiplex_in(int fd) | |
8d9dc9f9 | 2073 | { |
20caffd2 WD |
2074 | if (msgs2stderr && DEBUG_GTE(IO, 2)) |
2075 | rprintf(FINFO, "[%s] io_start_multiplex_in(%d)\n", who_am_i(), fd); | |
2076 | ||
2077 | iobuf.in_multiplexed = True; /* See also IN_MULTIPLEXED */ | |
2078 | io_start_buffering_in(fd); | |
8d9dc9f9 AT |
2079 | } |
2080 | ||
8e6b4ddb | 2081 | int io_end_multiplex_in(int mode) |
7f459268 | 2082 | { |
8e6b4ddb WD |
2083 | int ret = iobuf.in_multiplexed ? iobuf.in_fd : -1; |
2084 | ||
705132bc WD |
2085 | if (msgs2stderr && DEBUG_GTE(IO, 2)) |
2086 | rprintf(FINFO, "[%s] io_end_multiplex_in(mode=%d)\n", who_am_i(), mode); | |
20caffd2 WD |
2087 | |
2088 | iobuf.in_multiplexed = False; | |
705132bc WD |
2089 | if (mode == MPLX_SWITCHING) |
2090 | iobuf.raw_input_ends_before = 0; | |
2091 | else | |
2092 | assert(iobuf.raw_input_ends_before == 0); | |
2093 | if (mode != MPLX_TO_BUFFERED) | |
2094 | io_end_buffering_in(mode); | |
8e6b4ddb WD |
2095 | |
2096 | return ret; | |
7f459268 WD |
2097 | } |
2098 | ||
8e6b4ddb | 2099 | int io_end_multiplex_out(int mode) |
554e0a8d | 2100 | { |
8e6b4ddb WD |
2101 | int ret = iobuf.out_empty_len ? iobuf.out_fd : -1; |
2102 | ||
705132bc WD |
2103 | if (msgs2stderr && DEBUG_GTE(IO, 2)) |
2104 | rprintf(FINFO, "[%s] io_end_multiplex_out(mode=%d)\n", who_am_i(), mode); | |
2105 | ||
2106 | if (mode != MPLX_TO_BUFFERED) | |
2107 | io_end_buffering_out(mode); | |
2108 | else | |
2109 | io_flush(FULL_FLUSH); | |
20caffd2 | 2110 | |
705132bc | 2111 | iobuf.out.len = 0; |
20caffd2 | 2112 | iobuf.out_empty_len = 0; |
8e6b4ddb WD |
2113 | |
2114 | return ret; | |
554e0a8d AT |
2115 | } |
2116 | ||
b9f592fb WD |
2117 | void start_write_batch(int fd) |
2118 | { | |
2119 | /* Some communication has already taken place, but we don't | |
2120 | * enable batch writing until here so that we can write a | |
2121 | * canonical record of the communication even though the | |
2122 | * actual communication so far depends on whether a daemon | |
2123 | * is involved. */ | |
2124 | write_int(batch_fd, protocol_version); | |
71456d30 WD |
2125 | if (protocol_version >= 30) |
2126 | write_byte(batch_fd, inc_recurse); | |
b9f592fb | 2127 | write_int(batch_fd, checksum_seed); |
b9f592fb WD |
2128 | |
2129 | if (am_sender) | |
2130 | write_batch_monitor_out = fd; | |
2131 | else | |
2132 | write_batch_monitor_in = fd; | |
2133 | } | |
2134 | ||
2135 | void stop_write_batch(void) | |
2136 | { | |
2137 | write_batch_monitor_out = -1; | |
2138 | write_batch_monitor_in = -1; | |
2139 | } |