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