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