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