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