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