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