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