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