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