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