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