Commit | Line | Data |
---|---|---|
7a24c346 | 1 | /* -*- c-file-style: "linux" -*- |
d62bcc17 WD |
2 | * |
3 | * Copyright (C) 1996-2001 by Andrew Tridgell | |
880da007 MP |
4 | * Copyright (C) Paul Mackerras 1996 |
5 | * Copyright (C) 2001, 2002 by Martin Pool <mbp@samba.org> | |
d62bcc17 | 6 | * |
880da007 MP |
7 | * This program is free software; you can redistribute it and/or modify |
8 | * it under the terms of the GNU General Public License as published by | |
9 | * the Free Software Foundation; either version 2 of the License, or | |
10 | * (at your option) any later version. | |
d62bcc17 | 11 | * |
880da007 MP |
12 | * This program is distributed in the hope that it will be useful, |
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 | * GNU General Public License for more details. | |
d62bcc17 | 16 | * |
880da007 MP |
17 | * You should have received a copy of the GNU General Public License |
18 | * along with this program; if not, write to the Free Software | |
19 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. | |
20 | */ | |
720b47f2 | 21 | |
87ee2481 | 22 | /** |
87ee2481 MP |
23 | * @file io.c |
24 | * | |
6ed6d7f5 | 25 | * Socket and pipe I/O utilities used in rsync. |
87ee2481 MP |
26 | * |
27 | * rsync provides its own multiplexing system, which is used to send | |
28 | * stderr and stdout over a single socket. We need this because | |
29 | * stdout normally carries the binary data stream, and stderr all our | |
30 | * error messages. | |
31 | * | |
32 | * For historical reasons this is off during the start of the | |
33 | * connection, but it's switched on quite early using | |
34 | * io_start_multiplex_out() and io_start_multiplex_in(). | |
35 | **/ | |
720b47f2 | 36 | |
720b47f2 AT |
37 | #include "rsync.h" |
38 | ||
880da007 | 39 | /** If no timeout is specified then use a 60 second select timeout */ |
8cd9fd4e AT |
40 | #define SELECT_TIMEOUT 60 |
41 | ||
7a55d06e | 42 | extern int bwlimit; |
71e58630 | 43 | extern size_t bwlimit_writemax; |
720b47f2 | 44 | extern int verbose; |
6ba9279f | 45 | extern int io_timeout; |
cdf236aa | 46 | extern int allowed_lull; |
d17e1dd2 WD |
47 | extern int am_server; |
48 | extern int am_daemon; | |
49 | extern int am_sender; | |
98f8c9a5 | 50 | extern int am_generator; |
e626b29e | 51 | extern int eol_nulls; |
3b0a30eb | 52 | extern int read_batch; |
188fed95 | 53 | extern int csum_length; |
b9f592fb WD |
54 | extern int checksum_seed; |
55 | extern int protocol_version; | |
cdf236aa WD |
56 | extern int remove_sent_files; |
57 | extern int preserve_hard_links; | |
d19320fd | 58 | extern char *filesfrom_host; |
a800434a | 59 | extern struct stats stats; |
cdf236aa | 60 | extern struct file_list *the_file_list; |
720b47f2 | 61 | |
a86179f4 | 62 | const char phase_unknown[] = "unknown"; |
9e2409ab | 63 | int ignore_timeout = 0; |
b9f592fb | 64 | int batch_fd = -1; |
b0ad5429 | 65 | int batch_gen_fd = -1; |
805edf9d | 66 | |
98b332ed MP |
67 | /** |
68 | * The connection might be dropped at some point; perhaps because the | |
69 | * remote instance crashed. Just giving the offset on the stream is | |
70 | * not very helpful. So instead we try to make io_phase_name point to | |
71 | * something useful. | |
eca2adb4 | 72 | * |
6ed6d7f5 | 73 | * For buffered/multiplexed I/O these names will be somewhat |
805edf9d | 74 | * approximate; perhaps for ease of support we would rather make the |
6ed6d7f5 | 75 | * buffer always flush when a single application-level I/O finishes. |
805edf9d | 76 | * |
eca2adb4 MP |
77 | * @todo Perhaps we want some simple stack functionality, but there's |
78 | * no need to overdo it. | |
98b332ed | 79 | **/ |
805edf9d MP |
80 | const char *io_write_phase = phase_unknown; |
81 | const char *io_read_phase = phase_unknown; | |
98b332ed | 82 | |
cb2e1f18 | 83 | /* Ignore an EOF error if non-zero. See whine_about_eof(). */ |
574c2500 | 84 | int kluge_around_eof = 0; |
7a55d06e | 85 | |
d17e1dd2 WD |
86 | int msg_fd_in = -1; |
87 | int msg_fd_out = -1; | |
cdf236aa WD |
88 | int sock_f_in = -1; |
89 | int sock_f_out = -1; | |
7a55d06e | 90 | |
1f75bb10 WD |
91 | static int io_multiplexing_out; |
92 | static int io_multiplexing_in; | |
3b0a30eb WD |
93 | static time_t last_io_in; |
94 | static time_t last_io_out; | |
1f75bb10 WD |
95 | static int no_flush; |
96 | ||
b9f592fb WD |
97 | static int write_batch_monitor_in = -1; |
98 | static int write_batch_monitor_out = -1; | |
99 | ||
56014c8c WD |
100 | static int io_filesfrom_f_in = -1; |
101 | static int io_filesfrom_f_out = -1; | |
102 | static char io_filesfrom_buf[2048]; | |
103 | static char *io_filesfrom_bp; | |
104 | static char io_filesfrom_lastchar; | |
105 | static int io_filesfrom_buflen; | |
af436313 | 106 | static size_t contiguous_write_len = 0; |
3b0a30eb | 107 | static int select_timeout = SELECT_TIMEOUT; |
d6081c82 WD |
108 | static int active_filecnt = 0; |
109 | static OFF_T active_bytecnt = 0; | |
720b47f2 | 110 | |
9dd891bb | 111 | static void read_loop(int fd, char *buf, size_t len); |
ff41a59f | 112 | |
c6816b94 WD |
113 | struct flist_ndx_item { |
114 | struct flist_ndx_item *next; | |
115 | int ndx; | |
d17e1dd2 WD |
116 | }; |
117 | ||
c6816b94 WD |
118 | struct flist_ndx_list { |
119 | struct flist_ndx_item *head, *tail; | |
120 | }; | |
121 | ||
cdf236aa | 122 | static struct flist_ndx_list redo_list, hlink_list; |
d17e1dd2 | 123 | |
08c88178 WD |
124 | struct msg_list_item { |
125 | struct msg_list_item *next; | |
d17e1dd2 WD |
126 | char *buf; |
127 | int len; | |
128 | }; | |
129 | ||
08c88178 WD |
130 | struct msg_list { |
131 | struct msg_list_item *head, *tail; | |
132 | }; | |
133 | ||
134 | static struct msg_list msg_list; | |
d17e1dd2 | 135 | |
c6816b94 | 136 | static void flist_ndx_push(struct flist_ndx_list *lp, int ndx) |
d17e1dd2 | 137 | { |
c6816b94 WD |
138 | struct flist_ndx_item *item; |
139 | ||
140 | if (!(item = new(struct flist_ndx_item))) | |
141 | out_of_memory("flist_ndx_push"); | |
142 | item->next = NULL; | |
143 | item->ndx = ndx; | |
144 | if (lp->tail) | |
145 | lp->tail->next = item; | |
d17e1dd2 | 146 | else |
c6816b94 WD |
147 | lp->head = item; |
148 | lp->tail = item; | |
149 | } | |
150 | ||
151 | static int flist_ndx_pop(struct flist_ndx_list *lp) | |
152 | { | |
153 | struct flist_ndx_item *next; | |
154 | int ndx; | |
155 | ||
156 | if (!lp->head) | |
157 | return -1; | |
158 | ||
159 | ndx = lp->head->ndx; | |
160 | next = lp->head->next; | |
161 | free(lp->head); | |
162 | lp->head = next; | |
163 | if (!next) | |
164 | lp->tail = NULL; | |
165 | ||
166 | return ndx; | |
d17e1dd2 WD |
167 | } |
168 | ||
8d9dc9f9 AT |
169 | static void check_timeout(void) |
170 | { | |
171 | time_t t; | |
90ba34e2 | 172 | |
9e2409ab | 173 | if (!io_timeout || ignore_timeout) |
d17e1dd2 | 174 | return; |
8d9dc9f9 | 175 | |
3b0a30eb WD |
176 | if (!last_io_in) { |
177 | last_io_in = time(NULL); | |
8d9dc9f9 AT |
178 | return; |
179 | } | |
180 | ||
181 | t = time(NULL); | |
182 | ||
3b0a30eb | 183 | if (t - last_io_in >= io_timeout) { |
0adb99b9 | 184 | if (!am_server && !am_daemon) { |
4ccfd96c | 185 | rprintf(FERROR, "io timeout after %d seconds -- exiting\n", |
3b0a30eb | 186 | (int)(t-last_io_in)); |
0adb99b9 | 187 | } |
65417579 | 188 | exit_cleanup(RERR_TIMEOUT); |
8d9dc9f9 AT |
189 | } |
190 | } | |
191 | ||
1f75bb10 WD |
192 | /* Note the fds used for the main socket (which might really be a pipe |
193 | * for a local transfer, but we can ignore that). */ | |
194 | void io_set_sock_fds(int f_in, int f_out) | |
195 | { | |
196 | sock_f_in = f_in; | |
197 | sock_f_out = f_out; | |
198 | } | |
199 | ||
3b0a30eb WD |
200 | void set_io_timeout(int secs) |
201 | { | |
202 | io_timeout = secs; | |
203 | ||
204 | if (!io_timeout || io_timeout > SELECT_TIMEOUT) | |
205 | select_timeout = SELECT_TIMEOUT; | |
206 | else | |
207 | select_timeout = io_timeout; | |
208 | ||
209 | allowed_lull = read_batch ? 0 : (io_timeout + 1) / 2; | |
210 | } | |
211 | ||
98f8c9a5 WD |
212 | /* Setup the fd used to receive MSG_* messages. Only needed during the |
213 | * early stages of being a local sender (up through the sending of the | |
214 | * file list) or when we're the generator (to fetch the messages from | |
215 | * the receiver). */ | |
d17e1dd2 WD |
216 | void set_msg_fd_in(int fd) |
217 | { | |
218 | msg_fd_in = fd; | |
219 | } | |
220 | ||
98f8c9a5 WD |
221 | /* Setup the fd used to send our MSG_* messages. Only needed when |
222 | * we're the receiver (to send our messages to the generator). */ | |
d17e1dd2 WD |
223 | void set_msg_fd_out(int fd) |
224 | { | |
225 | msg_fd_out = fd; | |
226 | set_nonblocking(msg_fd_out); | |
227 | } | |
228 | ||
229 | /* Add a message to the pending MSG_* list. */ | |
230 | static void msg_list_add(int code, char *buf, int len) | |
554e0a8d | 231 | { |
08c88178 | 232 | struct msg_list_item *ml; |
d17e1dd2 | 233 | |
08c88178 | 234 | if (!(ml = new(struct msg_list_item))) |
c6816b94 | 235 | out_of_memory("msg_list_add"); |
d17e1dd2 WD |
236 | ml->next = NULL; |
237 | if (!(ml->buf = new_array(char, len+4))) | |
c6816b94 | 238 | out_of_memory("msg_list_add"); |
d17e1dd2 WD |
239 | SIVAL(ml->buf, 0, ((code+MPLEX_BASE)<<24) | len); |
240 | memcpy(ml->buf+4, buf, len); | |
241 | ml->len = len+4; | |
08c88178 WD |
242 | if (msg_list.tail) |
243 | msg_list.tail->next = ml; | |
d17e1dd2 | 244 | else |
08c88178 WD |
245 | msg_list.head = ml; |
246 | msg_list.tail = ml; | |
554e0a8d AT |
247 | } |
248 | ||
98f8c9a5 WD |
249 | /* Read a message from the MSG_* fd and handle it. This is called either |
250 | * during the early stages of being a local sender (up through the sending | |
251 | * of the file list) or when we're the generator (to fetch the messages | |
252 | * from the receiver). */ | |
d17e1dd2 | 253 | static void read_msg_fd(void) |
554e0a8d | 254 | { |
f9c6b3e7 | 255 | char buf[2048]; |
06ce139f | 256 | size_t n; |
d17e1dd2 | 257 | int fd = msg_fd_in; |
ff41a59f AT |
258 | int tag, len; |
259 | ||
00bdf899 | 260 | /* Temporarily disable msg_fd_in. This is needed to avoid looping back |
af436313 | 261 | * to this routine from writefd_unbuffered(). */ |
d17e1dd2 | 262 | msg_fd_in = -1; |
554e0a8d | 263 | |
ff41a59f AT |
264 | read_loop(fd, buf, 4); |
265 | tag = IVAL(buf, 0); | |
266 | ||
267 | len = tag & 0xFFFFFF; | |
d17e1dd2 | 268 | tag = (tag >> 24) - MPLEX_BASE; |
ff41a59f | 269 | |
d17e1dd2 WD |
270 | switch (tag) { |
271 | case MSG_DONE: | |
98f8c9a5 | 272 | if (len != 0 || !am_generator) { |
13c7bcbb | 273 | rprintf(FERROR, "invalid message %d:%d\n", tag, len); |
d17e1dd2 | 274 | exit_cleanup(RERR_STREAMIO); |
13c7bcbb | 275 | } |
c6816b94 | 276 | flist_ndx_push(&redo_list, -1); |
d17e1dd2 WD |
277 | break; |
278 | case MSG_REDO: | |
98f8c9a5 | 279 | if (len != 4 || !am_generator) { |
13c7bcbb | 280 | rprintf(FERROR, "invalid message %d:%d\n", tag, len); |
d17e1dd2 | 281 | exit_cleanup(RERR_STREAMIO); |
13c7bcbb | 282 | } |
d17e1dd2 | 283 | read_loop(fd, buf, 4); |
d6081c82 WD |
284 | if (remove_sent_files) |
285 | decrement_active_files(IVAL(buf,0)); | |
c6816b94 | 286 | flist_ndx_push(&redo_list, IVAL(buf,0)); |
d17e1dd2 | 287 | break; |
0d67e00a WD |
288 | case MSG_DELETED: |
289 | if (len >= (int)sizeof buf || !am_generator) { | |
290 | rprintf(FERROR, "invalid message %d:%d\n", tag, len); | |
291 | exit_cleanup(RERR_STREAMIO); | |
292 | } | |
293 | read_loop(fd, buf, len); | |
294 | io_multiplex_write(MSG_DELETED, buf, len); | |
295 | break; | |
9981c27e WD |
296 | case MSG_SUCCESS: |
297 | if (len != 4 || !am_generator) { | |
298 | rprintf(FERROR, "invalid message %d:%d\n", tag, len); | |
299 | exit_cleanup(RERR_STREAMIO); | |
300 | } | |
301 | read_loop(fd, buf, len); | |
d6081c82 WD |
302 | if (remove_sent_files) { |
303 | decrement_active_files(IVAL(buf,0)); | |
cdf236aa | 304 | io_multiplex_write(MSG_SUCCESS, buf, len); |
d6081c82 | 305 | } |
cdf236aa WD |
306 | if (preserve_hard_links) |
307 | flist_ndx_push(&hlink_list, IVAL(buf,0)); | |
9981c27e | 308 | break; |
72f2d1b3 WD |
309 | case MSG_SOCKERR: |
310 | if (!am_generator) { | |
311 | rprintf(FERROR, "invalid message %d:%d\n", tag, len); | |
312 | exit_cleanup(RERR_STREAMIO); | |
313 | } | |
314 | close_multiplexing_out(); | |
315 | /* FALL THROUGH */ | |
d17e1dd2 WD |
316 | case MSG_INFO: |
317 | case MSG_ERROR: | |
318 | case MSG_LOG: | |
319 | while (len) { | |
320 | n = len; | |
321 | if (n >= sizeof buf) | |
322 | n = sizeof buf - 1; | |
323 | read_loop(fd, buf, n); | |
324 | rwrite((enum logcode)tag, buf, n); | |
325 | len -= n; | |
326 | } | |
327 | break; | |
328 | default: | |
13c7bcbb | 329 | rprintf(FERROR, "unknown message %d:%d\n", tag, len); |
d17e1dd2 WD |
330 | exit_cleanup(RERR_STREAMIO); |
331 | } | |
332 | ||
333 | msg_fd_in = fd; | |
334 | } | |
335 | ||
d6081c82 WD |
336 | /* This is used by the generator to limit how many file transfers can |
337 | * be active at once when --remove-sent-files is specified. Without | |
338 | * this, sender-side deletions were mostly happening at the end. */ | |
339 | void increment_active_files(int ndx, int itemizing, enum logcode code) | |
340 | { | |
341 | /* TODO: tune these limits? */ | |
5b986297 | 342 | while (active_filecnt >= (active_bytecnt >= 128*1024 ? 10 : 50)) { |
d6081c82 WD |
343 | if (hlink_list.head) |
344 | check_for_finished_hlinks(itemizing, code); | |
345 | read_msg_fd(); | |
346 | } | |
347 | ||
348 | active_filecnt++; | |
349 | active_bytecnt += the_file_list->files[ndx]->length; | |
350 | } | |
351 | ||
352 | void decrement_active_files(int ndx) | |
353 | { | |
354 | active_filecnt--; | |
355 | active_bytecnt -= the_file_list->files[ndx]->length; | |
356 | } | |
357 | ||
d17e1dd2 WD |
358 | /* Try to push messages off the list onto the wire. If we leave with more |
359 | * to do, return 0. On error, return -1. If everything flushed, return 1. | |
f9c6b3e7 | 360 | * This is only active in the receiver. */ |
37f35d89 | 361 | static int msg_list_flush(int flush_it_all) |
d17e1dd2 WD |
362 | { |
363 | static int written = 0; | |
364 | struct timeval tv; | |
365 | fd_set fds; | |
366 | ||
367 | if (msg_fd_out < 0) | |
368 | return -1; | |
369 | ||
08c88178 WD |
370 | while (msg_list.head) { |
371 | struct msg_list_item *ml = msg_list.head; | |
d17e1dd2 WD |
372 | int n = write(msg_fd_out, ml->buf + written, ml->len - written); |
373 | if (n < 0) { | |
374 | if (errno == EINTR) | |
375 | continue; | |
376 | if (errno != EWOULDBLOCK && errno != EAGAIN) | |
377 | return -1; | |
378 | if (!flush_it_all) | |
379 | return 0; | |
380 | FD_ZERO(&fds); | |
381 | FD_SET(msg_fd_out, &fds); | |
e626b29e | 382 | tv.tv_sec = select_timeout; |
d17e1dd2 WD |
383 | tv.tv_usec = 0; |
384 | if (!select(msg_fd_out+1, NULL, &fds, NULL, &tv)) | |
385 | check_timeout(); | |
386 | } else if ((written += n) == ml->len) { | |
387 | free(ml->buf); | |
08c88178 WD |
388 | msg_list.head = ml->next; |
389 | if (!msg_list.head) | |
390 | msg_list.tail = NULL; | |
d17e1dd2 WD |
391 | free(ml); |
392 | written = 0; | |
393 | } | |
554e0a8d | 394 | } |
d17e1dd2 WD |
395 | return 1; |
396 | } | |
397 | ||
37f35d89 WD |
398 | void send_msg(enum msgcode code, char *buf, int len) |
399 | { | |
400 | if (msg_fd_out < 0) { | |
401 | io_multiplex_write(code, buf, len); | |
402 | return; | |
403 | } | |
404 | msg_list_add(code, buf, len); | |
405 | msg_list_flush(NORMAL_FLUSH); | |
406 | } | |
407 | ||
cdf236aa | 408 | int get_redo_num(int itemizing, enum logcode code) |
d17e1dd2 | 409 | { |
cdf236aa WD |
410 | while (1) { |
411 | if (hlink_list.head) | |
412 | check_for_finished_hlinks(itemizing, code); | |
413 | if (redo_list.head) | |
414 | break; | |
d17e1dd2 | 415 | read_msg_fd(); |
c6816b94 | 416 | } |
554e0a8d | 417 | |
c6816b94 | 418 | return flist_ndx_pop(&redo_list); |
554e0a8d AT |
419 | } |
420 | ||
cdf236aa WD |
421 | int get_hlink_num(void) |
422 | { | |
423 | return flist_ndx_pop(&hlink_list); | |
424 | } | |
425 | ||
56014c8c WD |
426 | /** |
427 | * When we're the receiver and we have a local --files-from list of names | |
428 | * that needs to be sent over the socket to the sender, we have to do two | |
429 | * things at the same time: send the sender a list of what files we're | |
430 | * processing and read the incoming file+info list from the sender. We do | |
431 | * this by augmenting the read_timeout() function to copy this data. It | |
432 | * uses the io_filesfrom_buf to read a block of data from f_in (when it is | |
433 | * ready, since it might be a pipe) and then blast it out f_out (when it | |
434 | * is ready to receive more data). | |
435 | */ | |
436 | void io_set_filesfrom_fds(int f_in, int f_out) | |
437 | { | |
438 | io_filesfrom_f_in = f_in; | |
439 | io_filesfrom_f_out = f_out; | |
440 | io_filesfrom_bp = io_filesfrom_buf; | |
441 | io_filesfrom_lastchar = '\0'; | |
442 | io_filesfrom_buflen = 0; | |
443 | } | |
720b47f2 | 444 | |
cb2e1f18 WD |
445 | /* It's almost always an error to get an EOF when we're trying to read from the |
446 | * network, because the protocol is (for the most part) self-terminating. | |
880da007 | 447 | * |
cb2e1f18 WD |
448 | * There is one case for the receiver when it is at the end of the transfer |
449 | * (hanging around reading any keep-alive packets that might come its way): if | |
450 | * the sender dies before the generator's kill-signal comes through, we can end | |
451 | * up here needing to loop until the kill-signal arrives. In this situation, | |
452 | * kluge_around_eof will be < 0. | |
453 | * | |
454 | * There is another case for older protocol versions (< 24) where the module | |
455 | * listing was not terminated, so we must ignore an EOF error in that case and | |
456 | * exit. In this situation, kluge_around_eof will be > 0. */ | |
1f75bb10 | 457 | static void whine_about_eof(int fd) |
7a55d06e | 458 | { |
574c2500 | 459 | if (kluge_around_eof && fd == sock_f_in) { |
55bb7fff | 460 | int i; |
574c2500 WD |
461 | if (kluge_around_eof > 0) |
462 | exit_cleanup(0); | |
55bb7fff WD |
463 | /* If we're still here after 10 seconds, exit with an error. */ |
464 | for (i = 10*1000/20; i--; ) | |
574c2500 WD |
465 | msleep(20); |
466 | } | |
3151cbae | 467 | |
00bdf899 | 468 | rprintf(FERROR, RSYNC_NAME ": connection unexpectedly closed " |
dca68b0a WD |
469 | "(%.0f bytes received so far) [%s]\n", |
470 | (double)stats.total_read, who_am_i()); | |
00bdf899 WD |
471 | |
472 | exit_cleanup(RERR_STREAMIO); | |
7a55d06e | 473 | } |
720b47f2 | 474 | |
880da007 | 475 | /** |
6ed6d7f5 | 476 | * Read from a socket with I/O timeout. return the number of bytes |
c3563c46 MP |
477 | * read. If no bytes can be read then exit, never return a number <= 0. |
478 | * | |
8886f8d0 MP |
479 | * TODO: If the remote shell connection fails, then current versions |
480 | * actually report an "unexpected EOF" error here. Since it's a | |
481 | * fairly common mistake to try to use rsh when ssh is required, we | |
482 | * should trap that: if we fail to read any data at all, we should | |
483 | * give a better explanation. We can tell whether the connection has | |
484 | * started by looking e.g. at whether the remote version is known yet. | |
c3563c46 | 485 | */ |
3151cbae | 486 | static int read_timeout(int fd, char *buf, size_t len) |
8d9dc9f9 | 487 | { |
d8aeda1e | 488 | int n, cnt = 0; |
4c36ddbe | 489 | |
d17e1dd2 | 490 | io_flush(NORMAL_FLUSH); |
ea2111d1 | 491 | |
d8aeda1e | 492 | while (cnt == 0) { |
7a55d06e | 493 | /* until we manage to read *something* */ |
56014c8c | 494 | fd_set r_fds, w_fds; |
4c36ddbe | 495 | struct timeval tv; |
1ea087a7 | 496 | int maxfd = fd; |
a57873b7 | 497 | int count; |
4c36ddbe | 498 | |
56014c8c | 499 | FD_ZERO(&r_fds); |
a7026ba9 | 500 | FD_ZERO(&w_fds); |
56014c8c | 501 | FD_SET(fd, &r_fds); |
08c88178 | 502 | if (msg_list.head) { |
4033b80b | 503 | FD_SET(msg_fd_out, &w_fds); |
1ea087a7 WD |
504 | if (msg_fd_out > maxfd) |
505 | maxfd = msg_fd_out; | |
56014c8c | 506 | } |
3309507d | 507 | if (io_filesfrom_f_out >= 0) { |
56014c8c WD |
508 | int new_fd; |
509 | if (io_filesfrom_buflen == 0) { | |
3309507d | 510 | if (io_filesfrom_f_in >= 0) { |
56014c8c WD |
511 | FD_SET(io_filesfrom_f_in, &r_fds); |
512 | new_fd = io_filesfrom_f_in; | |
513 | } else { | |
514 | io_filesfrom_f_out = -1; | |
515 | new_fd = -1; | |
516 | } | |
517 | } else { | |
56014c8c WD |
518 | FD_SET(io_filesfrom_f_out, &w_fds); |
519 | new_fd = io_filesfrom_f_out; | |
520 | } | |
1ea087a7 WD |
521 | if (new_fd > maxfd) |
522 | maxfd = new_fd; | |
554e0a8d AT |
523 | } |
524 | ||
e626b29e | 525 | tv.tv_sec = select_timeout; |
4c36ddbe AT |
526 | tv.tv_usec = 0; |
527 | ||
554e0a8d AT |
528 | errno = 0; |
529 | ||
a7026ba9 | 530 | count = select(maxfd + 1, &r_fds, &w_fds, NULL, &tv); |
a57873b7 | 531 | |
a57873b7 | 532 | if (count <= 0) { |
f89b9368 | 533 | if (errno == EBADF) |
554e0a8d | 534 | exit_cleanup(RERR_SOCKETIO); |
bd717af8 | 535 | check_timeout(); |
4c36ddbe AT |
536 | continue; |
537 | } | |
538 | ||
08c88178 | 539 | if (msg_list.head && FD_ISSET(msg_fd_out, &w_fds)) |
37f35d89 | 540 | msg_list_flush(NORMAL_FLUSH); |
554e0a8d | 541 | |
3309507d | 542 | if (io_filesfrom_f_out >= 0) { |
56014c8c WD |
543 | if (io_filesfrom_buflen) { |
544 | if (FD_ISSET(io_filesfrom_f_out, &w_fds)) { | |
545 | int l = write(io_filesfrom_f_out, | |
546 | io_filesfrom_bp, | |
547 | io_filesfrom_buflen); | |
548 | if (l > 0) { | |
549 | if (!(io_filesfrom_buflen -= l)) | |
550 | io_filesfrom_bp = io_filesfrom_buf; | |
551 | else | |
552 | io_filesfrom_bp += l; | |
553 | } else { | |
554 | /* XXX should we complain? */ | |
555 | io_filesfrom_f_out = -1; | |
556 | } | |
557 | } | |
3309507d | 558 | } else if (io_filesfrom_f_in >= 0) { |
56014c8c WD |
559 | if (FD_ISSET(io_filesfrom_f_in, &r_fds)) { |
560 | int l = read(io_filesfrom_f_in, | |
561 | io_filesfrom_buf, | |
562 | sizeof io_filesfrom_buf); | |
563 | if (l <= 0) { | |
564 | /* Send end-of-file marker */ | |
565 | io_filesfrom_buf[0] = '\0'; | |
566 | io_filesfrom_buf[1] = '\0'; | |
567 | io_filesfrom_buflen = io_filesfrom_lastchar? 2 : 1; | |
568 | io_filesfrom_f_in = -1; | |
569 | } else { | |
56014c8c WD |
570 | if (!eol_nulls) { |
571 | char *s = io_filesfrom_buf + l; | |
572 | /* Transform CR and/or LF into '\0' */ | |
573 | while (s-- > io_filesfrom_buf) { | |
574 | if (*s == '\n' || *s == '\r') | |
575 | *s = '\0'; | |
576 | } | |
577 | } | |
578 | if (!io_filesfrom_lastchar) { | |
579 | /* Last buf ended with a '\0', so don't | |
580 | * let this buf start with one. */ | |
581 | while (l && !*io_filesfrom_bp) | |
582 | io_filesfrom_bp++, l--; | |
583 | } | |
584 | if (!l) | |
585 | io_filesfrom_bp = io_filesfrom_buf; | |
586 | else { | |
587 | char *f = io_filesfrom_bp; | |
588 | char *t = f; | |
589 | char *eob = f + l; | |
590 | /* Eliminate any multi-'\0' runs. */ | |
591 | while (f != eob) { | |
592 | if (!(*t++ = *f++)) { | |
593 | while (f != eob && !*f) | |
594 | f++, l--; | |
595 | } | |
596 | } | |
597 | io_filesfrom_lastchar = f[-1]; | |
598 | } | |
599 | io_filesfrom_buflen = l; | |
600 | } | |
601 | } | |
602 | } | |
603 | } | |
604 | ||
f89b9368 WD |
605 | if (!FD_ISSET(fd, &r_fds)) |
606 | continue; | |
554e0a8d | 607 | |
4c36ddbe AT |
608 | n = read(fd, buf, len); |
609 | ||
1ea087a7 WD |
610 | if (n <= 0) { |
611 | if (n == 0) | |
1f75bb10 | 612 | whine_about_eof(fd); /* Doesn't return. */ |
d62bcc17 WD |
613 | if (errno == EINTR || errno == EWOULDBLOCK |
614 | || errno == EAGAIN) | |
7a55d06e | 615 | continue; |
1f75bb10 WD |
616 | |
617 | /* Don't write errors on a dead socket. */ | |
72f2d1b3 | 618 | if (fd == sock_f_in) { |
7f459268 | 619 | close_multiplexing_out(); |
72f2d1b3 WD |
620 | rsyserr(FSOCKERR, errno, "read error"); |
621 | } else | |
622 | rsyserr(FERROR, errno, "read error"); | |
1f75bb10 | 623 | exit_cleanup(RERR_STREAMIO); |
8d9dc9f9 | 624 | } |
00bdf899 WD |
625 | |
626 | buf += n; | |
627 | len -= n; | |
d8aeda1e | 628 | cnt += n; |
1f75bb10 | 629 | |
3b0a30eb WD |
630 | if (fd == sock_f_in && io_timeout) |
631 | last_io_in = time(NULL); | |
4c36ddbe | 632 | } |
8d9dc9f9 | 633 | |
d8aeda1e | 634 | return cnt; |
4c36ddbe | 635 | } |
8d9dc9f9 | 636 | |
56014c8c WD |
637 | /** |
638 | * Read a line into the "fname" buffer (which must be at least MAXPATHLEN | |
639 | * characters long). | |
640 | */ | |
641 | int read_filesfrom_line(int fd, char *fname) | |
642 | { | |
643 | char ch, *s, *eob = fname + MAXPATHLEN - 1; | |
644 | int cnt; | |
d19320fd | 645 | int reading_remotely = filesfrom_host != NULL; |
56014c8c WD |
646 | int nulls = eol_nulls || reading_remotely; |
647 | ||
648 | start: | |
649 | s = fname; | |
650 | while (1) { | |
651 | cnt = read(fd, &ch, 1); | |
652 | if (cnt < 0 && (errno == EWOULDBLOCK | |
653 | || errno == EINTR || errno == EAGAIN)) { | |
654 | struct timeval tv; | |
655 | fd_set fds; | |
656 | FD_ZERO(&fds); | |
657 | FD_SET(fd, &fds); | |
e626b29e | 658 | tv.tv_sec = select_timeout; |
56014c8c WD |
659 | tv.tv_usec = 0; |
660 | if (!select(fd+1, &fds, NULL, NULL, &tv)) | |
661 | check_timeout(); | |
662 | continue; | |
663 | } | |
664 | if (cnt != 1) | |
665 | break; | |
666 | if (nulls? !ch : (ch == '\r' || ch == '\n')) { | |
667 | /* Skip empty lines if reading locally. */ | |
668 | if (!reading_remotely && s == fname) | |
669 | continue; | |
670 | break; | |
671 | } | |
672 | if (s < eob) | |
673 | *s++ = ch; | |
674 | } | |
675 | *s = '\0'; | |
7a55d06e | 676 | |
6b45fcf1 WD |
677 | /* Dump comments. */ |
678 | if (*fname == '#' || *fname == ';') | |
56014c8c WD |
679 | goto start; |
680 | ||
681 | return s - fname; | |
682 | } | |
7a55d06e | 683 | |
1f75bb10 WD |
684 | static char *iobuf_out; |
685 | static int iobuf_out_cnt; | |
686 | ||
687 | void io_start_buffering_out(void) | |
688 | { | |
689 | if (iobuf_out) | |
690 | return; | |
691 | if (!(iobuf_out = new_array(char, IO_BUFFER_SIZE))) | |
692 | out_of_memory("io_start_buffering_out"); | |
693 | iobuf_out_cnt = 0; | |
694 | } | |
695 | ||
1f75bb10 WD |
696 | static char *iobuf_in; |
697 | static size_t iobuf_in_siz; | |
698 | ||
699 | void io_start_buffering_in(void) | |
700 | { | |
701 | if (iobuf_in) | |
702 | return; | |
703 | iobuf_in_siz = 2 * IO_BUFFER_SIZE; | |
704 | if (!(iobuf_in = new_array(char, iobuf_in_siz))) | |
705 | out_of_memory("io_start_buffering_in"); | |
706 | } | |
707 | ||
1f75bb10 WD |
708 | void io_end_buffering(void) |
709 | { | |
710 | io_flush(NORMAL_FLUSH); | |
711 | if (!io_multiplexing_out) { | |
712 | free(iobuf_out); | |
713 | iobuf_out = NULL; | |
714 | } | |
715 | } | |
716 | ||
626bec8e WD |
717 | void maybe_flush_socket(void) |
718 | { | |
3b0a30eb | 719 | if (iobuf_out && iobuf_out_cnt && time(NULL) - last_io_out >= 5) |
626bec8e WD |
720 | io_flush(NORMAL_FLUSH); |
721 | } | |
722 | ||
cdf236aa | 723 | void maybe_send_keepalive(void) |
9e2409ab | 724 | { |
3b0a30eb | 725 | if (time(NULL) - last_io_out >= allowed_lull) { |
9e2409ab | 726 | if (!iobuf_out || !iobuf_out_cnt) { |
3221f451 WD |
727 | if (protocol_version < 29) |
728 | return; /* there's nothing we can do */ | |
cdf236aa | 729 | write_int(sock_f_out, the_file_list->count); |
9e2409ab WD |
730 | write_shortint(sock_f_out, ITEM_IS_NEW); |
731 | } | |
732 | if (iobuf_out) | |
733 | io_flush(NORMAL_FLUSH); | |
734 | } | |
735 | } | |
736 | ||
880da007 MP |
737 | /** |
738 | * Continue trying to read len bytes - don't return until len has been | |
739 | * read. | |
740 | **/ | |
3151cbae | 741 | static void read_loop(int fd, char *buf, size_t len) |
4c36ddbe AT |
742 | { |
743 | while (len) { | |
744 | int n = read_timeout(fd, buf, len); | |
745 | ||
746 | buf += n; | |
747 | len -= n; | |
8d9dc9f9 AT |
748 | } |
749 | } | |
750 | ||
7a55d06e MP |
751 | /** |
752 | * Read from the file descriptor handling multiplexing - return number | |
753 | * of bytes read. | |
d62bcc17 WD |
754 | * |
755 | * Never returns <= 0. | |
7a55d06e | 756 | */ |
c399d22a | 757 | static int readfd_unbuffered(int fd, char *buf, size_t len) |
8d9dc9f9 | 758 | { |
6fe25398 | 759 | static size_t remaining; |
1f75bb10 | 760 | static size_t iobuf_in_ndx; |
f2a4853c | 761 | size_t msg_bytes; |
d8aeda1e | 762 | int tag, cnt = 0; |
33544bf4 | 763 | char line[BIGPATHBUFLEN]; |
8d9dc9f9 | 764 | |
1f75bb10 | 765 | if (!iobuf_in || fd != sock_f_in) |
4c36ddbe | 766 | return read_timeout(fd, buf, len); |
8d9dc9f9 | 767 | |
76c21947 | 768 | if (!io_multiplexing_in && remaining == 0) { |
1f75bb10 WD |
769 | remaining = read_timeout(fd, iobuf_in, iobuf_in_siz); |
770 | iobuf_in_ndx = 0; | |
76c21947 WD |
771 | } |
772 | ||
d8aeda1e | 773 | while (cnt == 0) { |
8d9dc9f9 AT |
774 | if (remaining) { |
775 | len = MIN(len, remaining); | |
1f75bb10 WD |
776 | memcpy(buf, iobuf_in + iobuf_in_ndx, len); |
777 | iobuf_in_ndx += len; | |
8d9dc9f9 | 778 | remaining -= len; |
d8aeda1e | 779 | cnt = len; |
76c21947 | 780 | break; |
8d9dc9f9 AT |
781 | } |
782 | ||
909ce14f | 783 | read_loop(fd, line, 4); |
ff41a59f | 784 | tag = IVAL(line, 0); |
679e7657 | 785 | |
f2a4853c | 786 | msg_bytes = tag & 0xFFFFFF; |
d17e1dd2 | 787 | tag = (tag >> 24) - MPLEX_BASE; |
8d9dc9f9 | 788 | |
d17e1dd2 WD |
789 | switch (tag) { |
790 | case MSG_DATA: | |
f2a4853c | 791 | if (msg_bytes > iobuf_in_siz) { |
1f75bb10 | 792 | if (!(iobuf_in = realloc_array(iobuf_in, char, |
f2a4853c | 793 | msg_bytes))) |
c399d22a | 794 | out_of_memory("readfd_unbuffered"); |
f2a4853c | 795 | iobuf_in_siz = msg_bytes; |
76c21947 | 796 | } |
f2a4853c WD |
797 | read_loop(fd, iobuf_in, msg_bytes); |
798 | remaining = msg_bytes; | |
1f75bb10 | 799 | iobuf_in_ndx = 0; |
d17e1dd2 | 800 | break; |
0d67e00a | 801 | case MSG_DELETED: |
f2a4853c WD |
802 | if (msg_bytes >= sizeof line) |
803 | goto overflow; | |
804 | read_loop(fd, line, msg_bytes); | |
0d67e00a | 805 | /* A directory name was sent with the trailing null */ |
f2a4853c | 806 | if (msg_bytes > 0 && !line[msg_bytes-1]) |
0d67e00a | 807 | log_delete(line, S_IFDIR); |
12bda6f7 WD |
808 | else { |
809 | line[msg_bytes] = '\0'; | |
0d67e00a | 810 | log_delete(line, S_IFREG); |
12bda6f7 | 811 | } |
0d67e00a | 812 | break; |
9981c27e | 813 | case MSG_SUCCESS: |
f2a4853c | 814 | if (msg_bytes != 4) { |
cdf236aa | 815 | rprintf(FERROR, "invalid multi-message %d:%ld [%s]\n", |
f2a4853c | 816 | tag, (long)msg_bytes, who_am_i()); |
9981c27e WD |
817 | exit_cleanup(RERR_STREAMIO); |
818 | } | |
f2a4853c | 819 | read_loop(fd, line, msg_bytes); |
9981c27e | 820 | successful_send(IVAL(line, 0)); |
9981c27e | 821 | break; |
d17e1dd2 WD |
822 | case MSG_INFO: |
823 | case MSG_ERROR: | |
f2a4853c WD |
824 | if (msg_bytes >= sizeof line) { |
825 | overflow: | |
bd9fca47 | 826 | rprintf(FERROR, |
cdf236aa | 827 | "multiplexing overflow %d:%ld [%s]\n", |
f2a4853c | 828 | tag, (long)msg_bytes, who_am_i()); |
d17e1dd2 WD |
829 | exit_cleanup(RERR_STREAMIO); |
830 | } | |
f2a4853c WD |
831 | read_loop(fd, line, msg_bytes); |
832 | rwrite((enum logcode)tag, line, msg_bytes); | |
d17e1dd2 WD |
833 | break; |
834 | default: | |
cdf236aa WD |
835 | rprintf(FERROR, "unexpected tag %d [%s]\n", |
836 | tag, who_am_i()); | |
65417579 | 837 | exit_cleanup(RERR_STREAMIO); |
8d9dc9f9 | 838 | } |
8d9dc9f9 AT |
839 | } |
840 | ||
76c21947 | 841 | if (remaining == 0) |
d17e1dd2 | 842 | io_flush(NORMAL_FLUSH); |
76c21947 | 843 | |
d8aeda1e | 844 | return cnt; |
8d9dc9f9 AT |
845 | } |
846 | ||
880da007 MP |
847 | /** |
848 | * Do a buffered read from @p fd. Don't return until all @p n bytes | |
849 | * have been read. If all @p n can't be read then exit with an | |
850 | * error. | |
851 | **/ | |
3151cbae | 852 | static void readfd(int fd, char *buffer, size_t N) |
720b47f2 | 853 | { |
d8aeda1e | 854 | int cnt; |
d62bcc17 | 855 | size_t total = 0; |
3151cbae | 856 | |
6ba9279f | 857 | while (total < N) { |
d8aeda1e WD |
858 | cnt = readfd_unbuffered(fd, buffer + total, N-total); |
859 | total += cnt; | |
7f28dbee | 860 | } |
1b7c47cb | 861 | |
b9f592fb WD |
862 | if (fd == write_batch_monitor_in) { |
863 | if ((size_t)write(batch_fd, buffer, total) != total) | |
864 | exit_cleanup(RERR_FILEIO); | |
865 | } | |
1f75bb10 WD |
866 | |
867 | if (fd == sock_f_in) | |
868 | stats.total_read += total; | |
720b47f2 AT |
869 | } |
870 | ||
0d67e00a | 871 | int read_shortint(int f) |
9361f839 WD |
872 | { |
873 | uchar b[2]; | |
874 | readfd(f, (char *)b, 2); | |
875 | return (b[1] << 8) + b[0]; | |
876 | } | |
877 | ||
b7922338 | 878 | int32 read_int(int f) |
720b47f2 | 879 | { |
4c36ddbe | 880 | char b[4]; |
d8aeda1e | 881 | int32 num; |
d730b113 | 882 | |
4c36ddbe | 883 | readfd(f,b,4); |
d8aeda1e WD |
884 | num = IVAL(b,0); |
885 | if (num == (int32)0xffffffff) | |
f89b9368 | 886 | return -1; |
d8aeda1e | 887 | return num; |
720b47f2 AT |
888 | } |
889 | ||
71c46176 | 890 | int64 read_longint(int f) |
3a6a366f | 891 | { |
d8aeda1e | 892 | int64 num; |
3a6a366f | 893 | char b[8]; |
d8aeda1e | 894 | num = read_int(f); |
71c46176 | 895 | |
d8aeda1e WD |
896 | if ((int32)num != (int32)0xffffffff) |
897 | return num; | |
71c46176 | 898 | |
031fa9ad WD |
899 | #if SIZEOF_INT64 < 8 |
900 | rprintf(FERROR, "Integer overflow: attempted 64-bit offset\n"); | |
901 | exit_cleanup(RERR_UNSUPPORTED); | |
902 | #else | |
91c4da3f | 903 | readfd(f,b,8); |
d8aeda1e | 904 | num = IVAL(b,0) | (((int64)IVAL(b,4))<<32); |
031fa9ad | 905 | #endif |
71c46176 | 906 | |
d8aeda1e | 907 | return num; |
3a6a366f AT |
908 | } |
909 | ||
9dd891bb | 910 | void read_buf(int f,char *buf,size_t len) |
720b47f2 | 911 | { |
4c36ddbe | 912 | readfd(f,buf,len); |
720b47f2 AT |
913 | } |
914 | ||
9dd891bb | 915 | void read_sbuf(int f,char *buf,size_t len) |
575f2fca | 916 | { |
93095cbe | 917 | readfd(f, buf, len); |
af436313 | 918 | buf[len] = '\0'; |
575f2fca AT |
919 | } |
920 | ||
9361f839 | 921 | uchar read_byte(int f) |
182dca5c | 922 | { |
9361f839 | 923 | uchar c; |
93095cbe | 924 | readfd(f, (char *)&c, 1); |
4c36ddbe | 925 | return c; |
182dca5c | 926 | } |
720b47f2 | 927 | |
46e99b09 WD |
928 | int read_vstring(int f, char *buf, int bufsize) |
929 | { | |
930 | int len = read_byte(f); | |
931 | ||
932 | if (len & 0x80) | |
933 | len = (len & ~0x80) * 0x100 + read_byte(f); | |
934 | ||
935 | if (len >= bufsize) { | |
936 | rprintf(FERROR, "over-long vstring received (%d > %d)\n", | |
937 | len, bufsize - 1); | |
9a6ed83f | 938 | return -1; |
46e99b09 WD |
939 | } |
940 | ||
941 | if (len) | |
942 | readfd(f, buf, len); | |
943 | buf[len] = '\0'; | |
944 | return len; | |
945 | } | |
946 | ||
188fed95 WD |
947 | /* Populate a sum_struct with values from the socket. This is |
948 | * called by both the sender and the receiver. */ | |
949 | void read_sum_head(int f, struct sum_struct *sum) | |
950 | { | |
951 | sum->count = read_int(f); | |
952 | sum->blength = read_int(f); | |
953 | if (sum->blength < 0 || sum->blength > MAX_BLOCK_SIZE) { | |
cdf236aa WD |
954 | rprintf(FERROR, "Invalid block length %ld [%s]\n", |
955 | (long)sum->blength, who_am_i()); | |
188fed95 WD |
956 | exit_cleanup(RERR_PROTOCOL); |
957 | } | |
958 | sum->s2length = protocol_version < 27 ? csum_length : (int)read_int(f); | |
959 | if (sum->s2length < 0 || sum->s2length > MD4_SUM_LENGTH) { | |
cdf236aa WD |
960 | rprintf(FERROR, "Invalid checksum length %d [%s]\n", |
961 | sum->s2length, who_am_i()); | |
188fed95 WD |
962 | exit_cleanup(RERR_PROTOCOL); |
963 | } | |
964 | sum->remainder = read_int(f); | |
965 | if (sum->remainder < 0 || sum->remainder > sum->blength) { | |
cdf236aa WD |
966 | rprintf(FERROR, "Invalid remainder length %ld [%s]\n", |
967 | (long)sum->remainder, who_am_i()); | |
188fed95 WD |
968 | exit_cleanup(RERR_PROTOCOL); |
969 | } | |
970 | } | |
971 | ||
c207d7ec WD |
972 | /* Send the values from a sum_struct over the socket. Set sum to |
973 | * NULL if there are no checksums to send. This is called by both | |
974 | * the generator and the sender. */ | |
975 | void write_sum_head(int f, struct sum_struct *sum) | |
976 | { | |
977 | static struct sum_struct null_sum; | |
978 | ||
979 | if (sum == NULL) | |
980 | sum = &null_sum; | |
981 | ||
982 | write_int(f, sum->count); | |
983 | write_int(f, sum->blength); | |
984 | if (protocol_version >= 27) | |
985 | write_int(f, sum->s2length); | |
986 | write_int(f, sum->remainder); | |
987 | } | |
988 | ||
08571358 MP |
989 | /** |
990 | * Sleep after writing to limit I/O bandwidth usage. | |
991 | * | |
992 | * @todo Rather than sleeping after each write, it might be better to | |
993 | * use some kind of averaging. The current algorithm seems to always | |
994 | * use a bit less bandwidth than specified, because it doesn't make up | |
995 | * for slow periods. But arguably this is a feature. In addition, we | |
996 | * ought to take the time used to write the data into account. | |
71e58630 WD |
997 | * |
998 | * During some phases of big transfers (file FOO is uptodate) this is | |
999 | * called with a small bytes_written every time. As the kernel has to | |
1000 | * round small waits up to guarantee that we actually wait at least the | |
1001 | * requested number of microseconds, this can become grossly inaccurate. | |
1002 | * We therefore keep track of the bytes we've written over time and only | |
1003 | * sleep when the accumulated delay is at least 1 tenth of a second. | |
08571358 MP |
1004 | **/ |
1005 | static void sleep_for_bwlimit(int bytes_written) | |
1006 | { | |
71e58630 WD |
1007 | static struct timeval prior_tv; |
1008 | static long total_written = 0; | |
1009 | struct timeval tv, start_tv; | |
1010 | long elapsed_usec, sleep_usec; | |
1011 | ||
1012 | #define ONE_SEC 1000000L /* # of microseconds in a second */ | |
08571358 | 1013 | |
9a0cfff5 | 1014 | if (!bwlimit_writemax) |
08571358 | 1015 | return; |
e681e820 | 1016 | |
71e58630 WD |
1017 | total_written += bytes_written; |
1018 | ||
1019 | gettimeofday(&start_tv, NULL); | |
1020 | if (prior_tv.tv_sec) { | |
1021 | elapsed_usec = (start_tv.tv_sec - prior_tv.tv_sec) * ONE_SEC | |
1022 | + (start_tv.tv_usec - prior_tv.tv_usec); | |
1023 | total_written -= elapsed_usec * bwlimit / (ONE_SEC/1024); | |
1024 | if (total_written < 0) | |
1025 | total_written = 0; | |
1026 | } | |
3151cbae | 1027 | |
71e58630 WD |
1028 | sleep_usec = total_written * (ONE_SEC/1024) / bwlimit; |
1029 | if (sleep_usec < ONE_SEC / 10) { | |
1030 | prior_tv = start_tv; | |
1031 | return; | |
1032 | } | |
08571358 | 1033 | |
71e58630 WD |
1034 | tv.tv_sec = sleep_usec / ONE_SEC; |
1035 | tv.tv_usec = sleep_usec % ONE_SEC; | |
98b332ed | 1036 | select(0, NULL, NULL, NULL, &tv); |
71e58630 WD |
1037 | |
1038 | gettimeofday(&prior_tv, NULL); | |
1039 | elapsed_usec = (prior_tv.tv_sec - start_tv.tv_sec) * ONE_SEC | |
1040 | + (prior_tv.tv_usec - start_tv.tv_usec); | |
1041 | total_written = (sleep_usec - elapsed_usec) * bwlimit / (ONE_SEC/1024); | |
08571358 MP |
1042 | } |
1043 | ||
1f75bb10 | 1044 | /* Write len bytes to the file descriptor fd, looping as necessary to get |
0fb2fc4a | 1045 | * the job done and also (in certain circumstances) reading any data on |
af436313 | 1046 | * msg_fd_in to avoid deadlock. |
880da007 MP |
1047 | * |
1048 | * This function underlies the multiplexing system. The body of the | |
1f75bb10 | 1049 | * application never calls this function directly. */ |
9dd891bb | 1050 | static void writefd_unbuffered(int fd,char *buf,size_t len) |
720b47f2 | 1051 | { |
1ea087a7 | 1052 | size_t n, total = 0; |
8d9dc9f9 | 1053 | fd_set w_fds, r_fds; |
d8aeda1e | 1054 | int maxfd, count, cnt, using_r_fds; |
8d9dc9f9 | 1055 | struct timeval tv; |
720b47f2 | 1056 | |
e44f9a12 AT |
1057 | no_flush++; |
1058 | ||
4c36ddbe | 1059 | while (total < len) { |
8d9dc9f9 | 1060 | FD_ZERO(&w_fds); |
8d9dc9f9 | 1061 | FD_SET(fd,&w_fds); |
1ea087a7 | 1062 | maxfd = fd; |
4c36ddbe | 1063 | |
af436313 | 1064 | if (msg_fd_in >= 0 && len-total >= contiguous_write_len) { |
56014c8c | 1065 | FD_ZERO(&r_fds); |
d17e1dd2 | 1066 | FD_SET(msg_fd_in,&r_fds); |
1ea087a7 WD |
1067 | if (msg_fd_in > maxfd) |
1068 | maxfd = msg_fd_in; | |
3eeac9bc WD |
1069 | using_r_fds = 1; |
1070 | } else | |
1071 | using_r_fds = 0; | |
8d9dc9f9 | 1072 | |
e626b29e | 1073 | tv.tv_sec = select_timeout; |
8d9dc9f9 | 1074 | tv.tv_usec = 0; |
4c36ddbe | 1075 | |
554e0a8d | 1076 | errno = 0; |
3eeac9bc | 1077 | count = select(maxfd + 1, using_r_fds ? &r_fds : NULL, |
d17e1dd2 | 1078 | &w_fds, NULL, &tv); |
4c36ddbe AT |
1079 | |
1080 | if (count <= 0) { | |
1ea087a7 | 1081 | if (count < 0 && errno == EBADF) |
554e0a8d | 1082 | exit_cleanup(RERR_SOCKETIO); |
bd717af8 | 1083 | check_timeout(); |
8d9dc9f9 AT |
1084 | continue; |
1085 | } | |
4c36ddbe | 1086 | |
3eeac9bc | 1087 | if (using_r_fds && FD_ISSET(msg_fd_in, &r_fds)) |
d17e1dd2 | 1088 | read_msg_fd(); |
554e0a8d | 1089 | |
9a6ed83f | 1090 | if (!FD_ISSET(fd, &w_fds)) |
1ea087a7 | 1091 | continue; |
4c36ddbe | 1092 | |
1ea087a7 | 1093 | n = len - total; |
9a0cfff5 | 1094 | if (bwlimit_writemax && n > bwlimit_writemax) |
1ea087a7 | 1095 | n = bwlimit_writemax; |
d8aeda1e | 1096 | cnt = write(fd, buf + total, n); |
1ea087a7 | 1097 | |
d8aeda1e WD |
1098 | if (cnt <= 0) { |
1099 | if (cnt < 0) { | |
3309507d WD |
1100 | if (errno == EINTR) |
1101 | continue; | |
1102 | if (errno == EWOULDBLOCK || errno == EAGAIN) { | |
1103 | msleep(1); | |
1104 | continue; | |
1105 | } | |
f0359dd0 AT |
1106 | } |
1107 | ||
1ea087a7 | 1108 | /* Don't try to write errors back across the stream. */ |
1f75bb10 | 1109 | if (fd == sock_f_out) |
7f459268 | 1110 | close_multiplexing_out(); |
1ea087a7 | 1111 | rsyserr(FERROR, errno, |
dca68b0a WD |
1112 | "writefd_unbuffered failed to write %ld bytes: phase \"%s\" [%s]", |
1113 | (long)len, io_write_phase, who_am_i()); | |
d1b31da7 WD |
1114 | /* If the other side is sending us error messages, try |
1115 | * to grab any messages they sent before they died. */ | |
7f459268 | 1116 | while (fd == sock_f_out && io_multiplexing_in) { |
3b0a30eb | 1117 | set_io_timeout(30); |
9e2409ab | 1118 | ignore_timeout = 0; |
d1b31da7 WD |
1119 | readfd_unbuffered(sock_f_in, io_filesfrom_buf, |
1120 | sizeof io_filesfrom_buf); | |
1121 | } | |
1ea087a7 WD |
1122 | exit_cleanup(RERR_STREAMIO); |
1123 | } | |
4c36ddbe | 1124 | |
d8aeda1e | 1125 | total += cnt; |
a800434a | 1126 | |
1f75bb10 | 1127 | if (fd == sock_f_out) { |
626bec8e | 1128 | if (io_timeout || am_generator) |
3b0a30eb | 1129 | last_io_out = time(NULL); |
d8aeda1e | 1130 | sleep_for_bwlimit(cnt); |
1f75bb10 | 1131 | } |
4c36ddbe | 1132 | } |
e44f9a12 AT |
1133 | |
1134 | no_flush--; | |
720b47f2 AT |
1135 | } |
1136 | ||
880da007 MP |
1137 | /** |
1138 | * Write an message to a multiplexed stream. If this fails then rsync | |
1139 | * exits. | |
1140 | **/ | |
1f75bb10 | 1141 | static void mplex_write(enum msgcode code, char *buf, size_t len) |
ff41a59f | 1142 | { |
12bda6f7 | 1143 | char buffer[1024]; |
06ce139f | 1144 | size_t n = len; |
8d9dc9f9 | 1145 | |
ff41a59f AT |
1146 | SIVAL(buffer, 0, ((MPLEX_BASE + (int)code)<<24) + len); |
1147 | ||
af436313 WD |
1148 | /* When the generator reads messages from the msg_fd_in pipe, it can |
1149 | * cause output to occur down the socket. Setting contiguous_write_len | |
1150 | * prevents the reading of msg_fd_in once we actually start to write | |
1151 | * this sequence of data (though we might read it before the start). */ | |
1152 | if (am_generator && msg_fd_in >= 0) | |
1153 | contiguous_write_len = len + 4; | |
1154 | ||
c399d22a | 1155 | if (n > sizeof buffer - 4) |
12bda6f7 WD |
1156 | n = 0; |
1157 | else | |
1158 | memcpy(buffer + 4, buf, n); | |
ff41a59f | 1159 | |
1f75bb10 | 1160 | writefd_unbuffered(sock_f_out, buffer, n+4); |
ff41a59f AT |
1161 | |
1162 | len -= n; | |
1163 | buf += n; | |
1164 | ||
1ea087a7 | 1165 | if (len) |
1f75bb10 | 1166 | writefd_unbuffered(sock_f_out, buf, len); |
af436313 WD |
1167 | |
1168 | if (am_generator && msg_fd_in >= 0) | |
1169 | contiguous_write_len = 0; | |
d6dead6b AT |
1170 | } |
1171 | ||
d17e1dd2 | 1172 | void io_flush(int flush_it_all) |
d6dead6b | 1173 | { |
37f35d89 | 1174 | msg_list_flush(flush_it_all); |
90ba34e2 | 1175 | |
1f75bb10 | 1176 | if (!iobuf_out_cnt || no_flush) |
d17e1dd2 | 1177 | return; |
8d9dc9f9 | 1178 | |
d17e1dd2 | 1179 | if (io_multiplexing_out) |
1f75bb10 | 1180 | mplex_write(MSG_DATA, iobuf_out, iobuf_out_cnt); |
d17e1dd2 | 1181 | else |
1f75bb10 WD |
1182 | writefd_unbuffered(sock_f_out, iobuf_out, iobuf_out_cnt); |
1183 | iobuf_out_cnt = 0; | |
8d9dc9f9 AT |
1184 | } |
1185 | ||
9dd891bb | 1186 | static void writefd(int fd,char *buf,size_t len) |
d6dead6b | 1187 | { |
4033b80b WD |
1188 | if (fd == msg_fd_out) { |
1189 | rprintf(FERROR, "Internal error: wrong write used in receiver.\n"); | |
1190 | exit_cleanup(RERR_PROTOCOL); | |
1191 | } | |
90ba34e2 | 1192 | |
1f75bb10 WD |
1193 | if (fd == sock_f_out) |
1194 | stats.total_written += len; | |
1195 | ||
b9f592fb WD |
1196 | if (fd == write_batch_monitor_out) { |
1197 | if ((size_t)write(batch_fd, buf, len) != len) | |
1198 | exit_cleanup(RERR_FILEIO); | |
1199 | } | |
1200 | ||
1f75bb10 | 1201 | if (!iobuf_out || fd != sock_f_out) { |
4c36ddbe AT |
1202 | writefd_unbuffered(fd, buf, len); |
1203 | return; | |
1204 | } | |
d6dead6b AT |
1205 | |
1206 | while (len) { | |
1f75bb10 | 1207 | int n = MIN((int)len, IO_BUFFER_SIZE - iobuf_out_cnt); |
d6dead6b | 1208 | if (n > 0) { |
1f75bb10 | 1209 | memcpy(iobuf_out+iobuf_out_cnt, buf, n); |
d6dead6b AT |
1210 | buf += n; |
1211 | len -= n; | |
1f75bb10 | 1212 | iobuf_out_cnt += n; |
d6dead6b | 1213 | } |
3151cbae | 1214 | |
1f75bb10 | 1215 | if (iobuf_out_cnt == IO_BUFFER_SIZE) |
d17e1dd2 | 1216 | io_flush(NORMAL_FLUSH); |
d6dead6b | 1217 | } |
d6dead6b | 1218 | } |
720b47f2 | 1219 | |
0d67e00a | 1220 | void write_shortint(int f, int x) |
9361f839 WD |
1221 | { |
1222 | uchar b[2]; | |
1223 | b[0] = x; | |
1224 | b[1] = x >> 8; | |
1225 | writefd(f, (char *)b, 2); | |
1226 | } | |
1227 | ||
b7922338 | 1228 | void write_int(int f,int32 x) |
720b47f2 | 1229 | { |
8d9dc9f9 AT |
1230 | char b[4]; |
1231 | SIVAL(b,0,x); | |
4c36ddbe | 1232 | writefd(f,b,4); |
720b47f2 AT |
1233 | } |
1234 | ||
805edf9d MP |
1235 | void write_int_named(int f, int32 x, const char *phase) |
1236 | { | |
1237 | io_write_phase = phase; | |
1238 | write_int(f, x); | |
1239 | io_write_phase = phase_unknown; | |
1240 | } | |
1241 | ||
7a24c346 MP |
1242 | /* |
1243 | * Note: int64 may actually be a 32-bit type if ./configure couldn't find any | |
1244 | * 64-bit types on this platform. | |
1245 | */ | |
71c46176 | 1246 | void write_longint(int f, int64 x) |
3a6a366f | 1247 | { |
3a6a366f | 1248 | char b[8]; |
3a6a366f | 1249 | |
91c4da3f | 1250 | if (x <= 0x7FFFFFFF) { |
3a6a366f AT |
1251 | write_int(f, (int)x); |
1252 | return; | |
1253 | } | |
1254 | ||
031fa9ad WD |
1255 | #if SIZEOF_INT64 < 8 |
1256 | rprintf(FERROR, "Integer overflow: attempted 64-bit offset\n"); | |
1257 | exit_cleanup(RERR_UNSUPPORTED); | |
1258 | #else | |
8de330a3 | 1259 | write_int(f, (int32)0xFFFFFFFF); |
3a6a366f AT |
1260 | SIVAL(b,0,(x&0xFFFFFFFF)); |
1261 | SIVAL(b,4,((x>>32)&0xFFFFFFFF)); | |
1262 | ||
4c36ddbe | 1263 | writefd(f,b,8); |
031fa9ad | 1264 | #endif |
3a6a366f AT |
1265 | } |
1266 | ||
9dd891bb | 1267 | void write_buf(int f,char *buf,size_t len) |
720b47f2 | 1268 | { |
4c36ddbe | 1269 | writefd(f,buf,len); |
720b47f2 AT |
1270 | } |
1271 | ||
880da007 | 1272 | /** Write a string to the connection */ |
cf338ab1 | 1273 | void write_sbuf(int f, char *buf) |
f0fca04e | 1274 | { |
93095cbe | 1275 | writefd(f, buf, strlen(buf)); |
f0fca04e AT |
1276 | } |
1277 | ||
9361f839 | 1278 | void write_byte(int f, uchar c) |
182dca5c | 1279 | { |
93095cbe | 1280 | writefd(f, (char *)&c, 1); |
182dca5c AT |
1281 | } |
1282 | ||
46e99b09 WD |
1283 | void write_vstring(int f, char *str, int len) |
1284 | { | |
1285 | uchar lenbuf[3], *lb = lenbuf; | |
1286 | ||
1287 | if (len > 0x7F) { | |
1288 | if (len > 0x7FFF) { | |
1289 | rprintf(FERROR, | |
1290 | "attempting to send over-long vstring (%d > %d)\n", | |
1291 | len, 0x7FFF); | |
1292 | exit_cleanup(RERR_PROTOCOL); | |
1293 | } | |
1294 | *lb++ = len / 0x100 + 0x80; | |
1295 | } | |
1296 | *lb = len; | |
1297 | ||
1298 | writefd(f, (char*)lenbuf, lb - lenbuf + 1); | |
1299 | if (len) | |
1300 | writefd(f, str, len); | |
1301 | } | |
1302 | ||
914cc65c | 1303 | /** |
6ed6d7f5 WD |
1304 | * Read a line of up to @p maxlen characters into @p buf (not counting |
1305 | * the trailing null). Strips the (required) trailing newline and all | |
1306 | * carriage returns. | |
914cc65c | 1307 | * |
6ed6d7f5 | 1308 | * @return 1 for success; 0 for I/O error or truncation. |
914cc65c | 1309 | **/ |
9dd891bb | 1310 | int read_line(int f, char *buf, size_t maxlen) |
f0fca04e AT |
1311 | { |
1312 | while (maxlen) { | |
528bfcd7 | 1313 | buf[0] = 0; |
f0fca04e | 1314 | read_buf(f, buf, 1); |
914cc65c MP |
1315 | if (buf[0] == 0) |
1316 | return 0; | |
6ed6d7f5 | 1317 | if (buf[0] == '\n') |
f0fca04e | 1318 | break; |
f0fca04e AT |
1319 | if (buf[0] != '\r') { |
1320 | buf++; | |
1321 | maxlen--; | |
1322 | } | |
1323 | } | |
6ed6d7f5 WD |
1324 | *buf = '\0'; |
1325 | return maxlen > 0; | |
f0fca04e AT |
1326 | } |
1327 | ||
f0fca04e AT |
1328 | void io_printf(int fd, const char *format, ...) |
1329 | { | |
d62bcc17 | 1330 | va_list ap; |
33544bf4 | 1331 | char buf[BIGPATHBUFLEN]; |
f0fca04e | 1332 | int len; |
3151cbae | 1333 | |
f0fca04e | 1334 | va_start(ap, format); |
3151cbae | 1335 | len = vsnprintf(buf, sizeof buf, format, ap); |
f0fca04e AT |
1336 | va_end(ap); |
1337 | ||
f89b9368 WD |
1338 | if (len < 0) |
1339 | exit_cleanup(RERR_STREAMIO); | |
f0fca04e | 1340 | |
33544bf4 WD |
1341 | if (len > (int)sizeof buf) { |
1342 | rprintf(FERROR, "io_printf() was too long for the buffer.\n"); | |
1343 | exit_cleanup(RERR_STREAMIO); | |
1344 | } | |
1345 | ||
f0fca04e AT |
1346 | write_sbuf(fd, buf); |
1347 | } | |
8d9dc9f9 | 1348 | |
d17e1dd2 | 1349 | /** Setup for multiplexing a MSG_* stream with the data stream. */ |
1f75bb10 | 1350 | void io_start_multiplex_out(void) |
8d9dc9f9 | 1351 | { |
d17e1dd2 | 1352 | io_flush(NORMAL_FLUSH); |
1f75bb10 | 1353 | io_start_buffering_out(); |
8d9dc9f9 AT |
1354 | io_multiplexing_out = 1; |
1355 | } | |
1356 | ||
d17e1dd2 | 1357 | /** Setup for multiplexing a MSG_* stream with the data stream. */ |
1f75bb10 | 1358 | void io_start_multiplex_in(void) |
8d9dc9f9 | 1359 | { |
d17e1dd2 | 1360 | io_flush(NORMAL_FLUSH); |
1f75bb10 | 1361 | io_start_buffering_in(); |
8d9dc9f9 AT |
1362 | io_multiplexing_in = 1; |
1363 | } | |
1364 | ||
d17e1dd2 WD |
1365 | /** Write an message to the multiplexed data stream. */ |
1366 | int io_multiplex_write(enum msgcode code, char *buf, size_t len) | |
8d9dc9f9 | 1367 | { |
f89b9368 WD |
1368 | if (!io_multiplexing_out) |
1369 | return 0; | |
8d9dc9f9 | 1370 | |
d17e1dd2 | 1371 | io_flush(NORMAL_FLUSH); |
1b7c47cb | 1372 | stats.total_written += (len+4); |
1f75bb10 | 1373 | mplex_write(code, buf, len); |
8d9dc9f9 AT |
1374 | return 1; |
1375 | } | |
1376 | ||
7f459268 WD |
1377 | void close_multiplexing_in(void) |
1378 | { | |
1379 | io_multiplexing_in = 0; | |
1380 | } | |
1381 | ||
d17e1dd2 | 1382 | /** Stop output multiplexing. */ |
7f459268 | 1383 | void close_multiplexing_out(void) |
554e0a8d AT |
1384 | { |
1385 | io_multiplexing_out = 0; | |
1386 | } | |
1387 | ||
b9f592fb WD |
1388 | void start_write_batch(int fd) |
1389 | { | |
741d6544 WD |
1390 | write_stream_flags(batch_fd); |
1391 | ||
b9f592fb WD |
1392 | /* Some communication has already taken place, but we don't |
1393 | * enable batch writing until here so that we can write a | |
1394 | * canonical record of the communication even though the | |
1395 | * actual communication so far depends on whether a daemon | |
1396 | * is involved. */ | |
1397 | write_int(batch_fd, protocol_version); | |
1398 | write_int(batch_fd, checksum_seed); | |
b9f592fb WD |
1399 | |
1400 | if (am_sender) | |
1401 | write_batch_monitor_out = fd; | |
1402 | else | |
1403 | write_batch_monitor_in = fd; | |
1404 | } | |
1405 | ||
1406 | void stop_write_batch(void) | |
1407 | { | |
1408 | write_batch_monitor_out = -1; | |
1409 | write_batch_monitor_in = -1; | |
1410 | } |