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