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