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