Commit | Line | Data |
---|---|---|
7a24c346 | 1 | /* -*- c-file-style: "linux" -*- |
d62bcc17 WD |
2 | * |
3 | * Copyright (C) 1996-2001 by Andrew Tridgell | |
880da007 MP |
4 | * Copyright (C) Paul Mackerras 1996 |
5 | * Copyright (C) 2001, 2002 by Martin Pool <mbp@samba.org> | |
d62bcc17 | 6 | * |
880da007 MP |
7 | * This program is free software; you can redistribute it and/or modify |
8 | * it under the terms of the GNU General Public License as published by | |
9 | * the Free Software Foundation; either version 2 of the License, or | |
10 | * (at your option) any later version. | |
d62bcc17 | 11 | * |
880da007 MP |
12 | * This program is distributed in the hope that it will be useful, |
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 | * GNU General Public License for more details. | |
d62bcc17 | 16 | * |
880da007 MP |
17 | * You should have received a copy of the GNU General Public License |
18 | * along with this program; if not, write to the Free Software | |
19 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. | |
20 | */ | |
720b47f2 | 21 | |
87ee2481 | 22 | /** |
87ee2481 MP |
23 | * @file io.c |
24 | * | |
6ed6d7f5 | 25 | * Socket and pipe I/O utilities used in rsync. |
87ee2481 MP |
26 | * |
27 | * rsync provides its own multiplexing system, which is used to send | |
28 | * stderr and stdout over a single socket. We need this because | |
29 | * stdout normally carries the binary data stream, and stderr all our | |
30 | * error messages. | |
31 | * | |
32 | * For historical reasons this is off during the start of the | |
33 | * connection, but it's switched on quite early using | |
34 | * io_start_multiplex_out() and io_start_multiplex_in(). | |
35 | **/ | |
720b47f2 | 36 | |
720b47f2 AT |
37 | #include "rsync.h" |
38 | ||
880da007 | 39 | /** If no timeout is specified then use a 60 second select timeout */ |
8cd9fd4e AT |
40 | #define SELECT_TIMEOUT 60 |
41 | ||
7a55d06e | 42 | extern int bwlimit; |
71e58630 | 43 | extern size_t bwlimit_writemax; |
720b47f2 | 44 | extern int verbose; |
6ba9279f | 45 | extern int io_timeout; |
d17e1dd2 WD |
46 | extern int am_server; |
47 | extern int am_daemon; | |
48 | extern int am_sender; | |
98f8c9a5 | 49 | extern int am_generator; |
e626b29e | 50 | extern int eol_nulls; |
188fed95 | 51 | extern int csum_length; |
b9f592fb WD |
52 | extern int checksum_seed; |
53 | extern int protocol_version; | |
e626b29e | 54 | extern char *remote_filesfrom_file; |
a800434a | 55 | extern struct stats stats; |
720b47f2 | 56 | |
a86179f4 | 57 | const char phase_unknown[] = "unknown"; |
e626b29e | 58 | int select_timeout = SELECT_TIMEOUT; |
b9f592fb | 59 | int batch_fd = -1; |
b0ad5429 | 60 | int batch_gen_fd = -1; |
805edf9d | 61 | |
98b332ed MP |
62 | /** |
63 | * The connection might be dropped at some point; perhaps because the | |
64 | * remote instance crashed. Just giving the offset on the stream is | |
65 | * not very helpful. So instead we try to make io_phase_name point to | |
66 | * something useful. | |
eca2adb4 | 67 | * |
6ed6d7f5 | 68 | * For buffered/multiplexed I/O these names will be somewhat |
805edf9d | 69 | * approximate; perhaps for ease of support we would rather make the |
6ed6d7f5 | 70 | * buffer always flush when a single application-level I/O finishes. |
805edf9d | 71 | * |
eca2adb4 MP |
72 | * @todo Perhaps we want some simple stack functionality, but there's |
73 | * no need to overdo it. | |
98b332ed | 74 | **/ |
805edf9d MP |
75 | const char *io_write_phase = phase_unknown; |
76 | const char *io_read_phase = phase_unknown; | |
98b332ed | 77 | |
7a55d06e MP |
78 | /** Ignore EOF errors while reading a module listing if the remote |
79 | version is 24 or less. */ | |
80 | int kludge_around_eof = False; | |
81 | ||
d17e1dd2 WD |
82 | int msg_fd_in = -1; |
83 | int msg_fd_out = -1; | |
7a55d06e | 84 | |
1f75bb10 WD |
85 | static int io_multiplexing_out; |
86 | static int io_multiplexing_in; | |
87 | static int sock_f_in = -1; | |
88 | static int sock_f_out = -1; | |
89 | static time_t last_io; | |
90 | static int no_flush; | |
91 | ||
b9f592fb WD |
92 | static int write_batch_monitor_in = -1; |
93 | static int write_batch_monitor_out = -1; | |
94 | ||
56014c8c WD |
95 | static int io_filesfrom_f_in = -1; |
96 | static int io_filesfrom_f_out = -1; | |
97 | static char io_filesfrom_buf[2048]; | |
98 | static char *io_filesfrom_bp; | |
99 | static char io_filesfrom_lastchar; | |
100 | static int io_filesfrom_buflen; | |
720b47f2 | 101 | |
9dd891bb | 102 | static void read_loop(int fd, char *buf, size_t len); |
ff41a59f | 103 | |
d17e1dd2 WD |
104 | struct redo_list { |
105 | struct redo_list *next; | |
106 | int num; | |
107 | }; | |
108 | ||
109 | static struct redo_list *redo_list_head; | |
110 | static struct redo_list *redo_list_tail; | |
111 | ||
112 | struct msg_list { | |
113 | struct msg_list *next; | |
114 | char *buf; | |
115 | int len; | |
116 | }; | |
117 | ||
118 | static struct msg_list *msg_list_head; | |
119 | static struct msg_list *msg_list_tail; | |
120 | ||
121 | static void redo_list_add(int num) | |
122 | { | |
123 | struct redo_list *rl; | |
124 | ||
125 | if (!(rl = new(struct redo_list))) | |
126 | exit_cleanup(RERR_MALLOC); | |
127 | rl->next = NULL; | |
128 | rl->num = num; | |
129 | if (redo_list_tail) | |
130 | redo_list_tail->next = rl; | |
131 | else | |
132 | redo_list_head = rl; | |
133 | redo_list_tail = rl; | |
134 | } | |
135 | ||
8d9dc9f9 AT |
136 | static void check_timeout(void) |
137 | { | |
138 | time_t t; | |
90ba34e2 | 139 | |
d17e1dd2 WD |
140 | if (!io_timeout) |
141 | return; | |
8d9dc9f9 AT |
142 | |
143 | if (!last_io) { | |
144 | last_io = time(NULL); | |
145 | return; | |
146 | } | |
147 | ||
148 | t = time(NULL); | |
149 | ||
1f75bb10 | 150 | if (t - last_io >= io_timeout) { |
0adb99b9 | 151 | if (!am_server && !am_daemon) { |
4ccfd96c | 152 | rprintf(FERROR, "io timeout after %d seconds -- exiting\n", |
0adb99b9 AT |
153 | (int)(t-last_io)); |
154 | } | |
65417579 | 155 | exit_cleanup(RERR_TIMEOUT); |
8d9dc9f9 AT |
156 | } |
157 | } | |
158 | ||
1f75bb10 WD |
159 | /* Note the fds used for the main socket (which might really be a pipe |
160 | * for a local transfer, but we can ignore that). */ | |
161 | void io_set_sock_fds(int f_in, int f_out) | |
162 | { | |
163 | sock_f_in = f_in; | |
164 | sock_f_out = f_out; | |
165 | } | |
166 | ||
98f8c9a5 WD |
167 | /* Setup the fd used to receive MSG_* messages. Only needed during the |
168 | * early stages of being a local sender (up through the sending of the | |
169 | * file list) or when we're the generator (to fetch the messages from | |
170 | * the receiver). */ | |
d17e1dd2 WD |
171 | void set_msg_fd_in(int fd) |
172 | { | |
173 | msg_fd_in = fd; | |
174 | } | |
175 | ||
98f8c9a5 WD |
176 | /* Setup the fd used to send our MSG_* messages. Only needed when |
177 | * we're the receiver (to send our messages to the generator). */ | |
d17e1dd2 WD |
178 | void set_msg_fd_out(int fd) |
179 | { | |
180 | msg_fd_out = fd; | |
181 | set_nonblocking(msg_fd_out); | |
182 | } | |
183 | ||
184 | /* Add a message to the pending MSG_* list. */ | |
185 | static void msg_list_add(int code, char *buf, int len) | |
554e0a8d | 186 | { |
d17e1dd2 WD |
187 | struct msg_list *ml; |
188 | ||
189 | if (!(ml = new(struct msg_list))) | |
190 | exit_cleanup(RERR_MALLOC); | |
191 | ml->next = NULL; | |
192 | if (!(ml->buf = new_array(char, len+4))) | |
193 | exit_cleanup(RERR_MALLOC); | |
194 | SIVAL(ml->buf, 0, ((code+MPLEX_BASE)<<24) | len); | |
195 | memcpy(ml->buf+4, buf, len); | |
196 | ml->len = len+4; | |
197 | if (msg_list_tail) | |
198 | msg_list_tail->next = ml; | |
199 | else | |
200 | msg_list_head = ml; | |
201 | msg_list_tail = ml; | |
554e0a8d AT |
202 | } |
203 | ||
d17e1dd2 WD |
204 | void send_msg(enum msgcode code, char *buf, int len) |
205 | { | |
0d67e00a WD |
206 | if (msg_fd_out < 0) { |
207 | io_multiplex_write(code, buf, len); | |
208 | return; | |
209 | } | |
d17e1dd2 WD |
210 | msg_list_add(code, buf, len); |
211 | msg_list_push(NORMAL_FLUSH); | |
212 | } | |
213 | ||
98f8c9a5 WD |
214 | /* Read a message from the MSG_* fd and handle it. This is called either |
215 | * during the early stages of being a local sender (up through the sending | |
216 | * of the file list) or when we're the generator (to fetch the messages | |
217 | * from the receiver). */ | |
d17e1dd2 | 218 | static void read_msg_fd(void) |
554e0a8d | 219 | { |
f9c6b3e7 | 220 | char buf[2048]; |
06ce139f | 221 | size_t n; |
d17e1dd2 | 222 | int fd = msg_fd_in; |
ff41a59f AT |
223 | int tag, len; |
224 | ||
00bdf899 WD |
225 | /* Temporarily disable msg_fd_in. This is needed to avoid looping back |
226 | * to this routine from read_timeout() and writefd_unbuffered(). */ | |
d17e1dd2 | 227 | msg_fd_in = -1; |
554e0a8d | 228 | |
ff41a59f AT |
229 | read_loop(fd, buf, 4); |
230 | tag = IVAL(buf, 0); | |
231 | ||
232 | len = tag & 0xFFFFFF; | |
d17e1dd2 | 233 | tag = (tag >> 24) - MPLEX_BASE; |
ff41a59f | 234 | |
d17e1dd2 WD |
235 | switch (tag) { |
236 | case MSG_DONE: | |
98f8c9a5 | 237 | if (len != 0 || !am_generator) { |
13c7bcbb | 238 | rprintf(FERROR, "invalid message %d:%d\n", tag, len); |
d17e1dd2 | 239 | exit_cleanup(RERR_STREAMIO); |
13c7bcbb | 240 | } |
d17e1dd2 WD |
241 | redo_list_add(-1); |
242 | break; | |
243 | case MSG_REDO: | |
98f8c9a5 | 244 | if (len != 4 || !am_generator) { |
13c7bcbb | 245 | rprintf(FERROR, "invalid message %d:%d\n", tag, len); |
d17e1dd2 | 246 | exit_cleanup(RERR_STREAMIO); |
13c7bcbb | 247 | } |
d17e1dd2 WD |
248 | read_loop(fd, buf, 4); |
249 | redo_list_add(IVAL(buf,0)); | |
250 | break; | |
0d67e00a WD |
251 | case MSG_DELETED: |
252 | if (len >= (int)sizeof buf || !am_generator) { | |
253 | rprintf(FERROR, "invalid message %d:%d\n", tag, len); | |
254 | exit_cleanup(RERR_STREAMIO); | |
255 | } | |
256 | read_loop(fd, buf, len); | |
257 | io_multiplex_write(MSG_DELETED, buf, len); | |
258 | break; | |
d17e1dd2 WD |
259 | case MSG_INFO: |
260 | case MSG_ERROR: | |
261 | case MSG_LOG: | |
262 | while (len) { | |
263 | n = len; | |
264 | if (n >= sizeof buf) | |
265 | n = sizeof buf - 1; | |
266 | read_loop(fd, buf, n); | |
267 | rwrite((enum logcode)tag, buf, n); | |
268 | len -= n; | |
269 | } | |
270 | break; | |
271 | default: | |
13c7bcbb | 272 | rprintf(FERROR, "unknown message %d:%d\n", tag, len); |
d17e1dd2 WD |
273 | exit_cleanup(RERR_STREAMIO); |
274 | } | |
275 | ||
276 | msg_fd_in = fd; | |
277 | } | |
278 | ||
279 | /* Try to push messages off the list onto the wire. If we leave with more | |
280 | * to do, return 0. On error, return -1. If everything flushed, return 1. | |
f9c6b3e7 | 281 | * This is only active in the receiver. */ |
d17e1dd2 WD |
282 | int msg_list_push(int flush_it_all) |
283 | { | |
284 | static int written = 0; | |
285 | struct timeval tv; | |
286 | fd_set fds; | |
287 | ||
288 | if (msg_fd_out < 0) | |
289 | return -1; | |
290 | ||
291 | while (msg_list_head) { | |
292 | struct msg_list *ml = msg_list_head; | |
293 | int n = write(msg_fd_out, ml->buf + written, ml->len - written); | |
294 | if (n < 0) { | |
295 | if (errno == EINTR) | |
296 | continue; | |
297 | if (errno != EWOULDBLOCK && errno != EAGAIN) | |
298 | return -1; | |
299 | if (!flush_it_all) | |
300 | return 0; | |
301 | FD_ZERO(&fds); | |
302 | FD_SET(msg_fd_out, &fds); | |
e626b29e | 303 | tv.tv_sec = select_timeout; |
d17e1dd2 WD |
304 | tv.tv_usec = 0; |
305 | if (!select(msg_fd_out+1, NULL, &fds, NULL, &tv)) | |
306 | check_timeout(); | |
307 | } else if ((written += n) == ml->len) { | |
308 | free(ml->buf); | |
309 | msg_list_head = ml->next; | |
310 | if (!msg_list_head) | |
311 | msg_list_tail = NULL; | |
312 | free(ml); | |
313 | written = 0; | |
314 | } | |
554e0a8d | 315 | } |
d17e1dd2 WD |
316 | return 1; |
317 | } | |
318 | ||
319 | int get_redo_num(void) | |
320 | { | |
321 | struct redo_list *next; | |
322 | int num; | |
323 | ||
324 | while (!redo_list_head) | |
325 | read_msg_fd(); | |
554e0a8d | 326 | |
d17e1dd2 WD |
327 | num = redo_list_head->num; |
328 | next = redo_list_head->next; | |
329 | free(redo_list_head); | |
330 | redo_list_head = next; | |
331 | if (!next) | |
332 | redo_list_tail = NULL; | |
333 | ||
334 | return num; | |
554e0a8d AT |
335 | } |
336 | ||
56014c8c WD |
337 | /** |
338 | * When we're the receiver and we have a local --files-from list of names | |
339 | * that needs to be sent over the socket to the sender, we have to do two | |
340 | * things at the same time: send the sender a list of what files we're | |
341 | * processing and read the incoming file+info list from the sender. We do | |
342 | * this by augmenting the read_timeout() function to copy this data. It | |
343 | * uses the io_filesfrom_buf to read a block of data from f_in (when it is | |
344 | * ready, since it might be a pipe) and then blast it out f_out (when it | |
345 | * is ready to receive more data). | |
346 | */ | |
347 | void io_set_filesfrom_fds(int f_in, int f_out) | |
348 | { | |
349 | io_filesfrom_f_in = f_in; | |
350 | io_filesfrom_f_out = f_out; | |
351 | io_filesfrom_bp = io_filesfrom_buf; | |
352 | io_filesfrom_lastchar = '\0'; | |
353 | io_filesfrom_buflen = 0; | |
354 | } | |
720b47f2 | 355 | |
880da007 MP |
356 | /** |
357 | * It's almost always an error to get an EOF when we're trying to read | |
358 | * from the network, because the protocol is self-terminating. | |
359 | * | |
360 | * However, there is one unfortunate cases where it is not, which is | |
361 | * rsync <2.4.6 sending a list of modules on a server, since the list | |
362 | * is terminated by closing the socket. So, for the section of the | |
363 | * program where that is a problem (start_socket_client), | |
364 | * kludge_around_eof is True and we just exit. | |
365 | */ | |
1f75bb10 | 366 | static void whine_about_eof(int fd) |
7a55d06e | 367 | { |
1f75bb10 | 368 | if (kludge_around_eof && fd == sock_f_in) |
3151cbae | 369 | exit_cleanup(0); |
3151cbae | 370 | |
00bdf899 | 371 | rprintf(FERROR, RSYNC_NAME ": connection unexpectedly closed " |
dca68b0a WD |
372 | "(%.0f bytes received so far) [%s]\n", |
373 | (double)stats.total_read, who_am_i()); | |
00bdf899 WD |
374 | |
375 | exit_cleanup(RERR_STREAMIO); | |
7a55d06e | 376 | } |
720b47f2 | 377 | |
7a55d06e | 378 | |
880da007 | 379 | /** |
6ed6d7f5 | 380 | * Read from a socket with I/O timeout. return the number of bytes |
c3563c46 MP |
381 | * read. If no bytes can be read then exit, never return a number <= 0. |
382 | * | |
8886f8d0 MP |
383 | * TODO: If the remote shell connection fails, then current versions |
384 | * actually report an "unexpected EOF" error here. Since it's a | |
385 | * fairly common mistake to try to use rsh when ssh is required, we | |
386 | * should trap that: if we fail to read any data at all, we should | |
387 | * give a better explanation. We can tell whether the connection has | |
388 | * started by looking e.g. at whether the remote version is known yet. | |
c3563c46 | 389 | */ |
3151cbae | 390 | static int read_timeout(int fd, char *buf, size_t len) |
8d9dc9f9 | 391 | { |
d62bcc17 | 392 | int n, ret = 0; |
4c36ddbe | 393 | |
d17e1dd2 | 394 | io_flush(NORMAL_FLUSH); |
ea2111d1 | 395 | |
4c36ddbe | 396 | while (ret == 0) { |
7a55d06e | 397 | /* until we manage to read *something* */ |
56014c8c | 398 | fd_set r_fds, w_fds; |
4c36ddbe | 399 | struct timeval tv; |
1ea087a7 | 400 | int maxfd = fd; |
a57873b7 | 401 | int count; |
4c36ddbe | 402 | |
56014c8c | 403 | FD_ZERO(&r_fds); |
a7026ba9 | 404 | FD_ZERO(&w_fds); |
56014c8c | 405 | FD_SET(fd, &r_fds); |
d17e1dd2 WD |
406 | if (msg_fd_in >= 0) { |
407 | FD_SET(msg_fd_in, &r_fds); | |
1ea087a7 WD |
408 | if (msg_fd_in > maxfd) |
409 | maxfd = msg_fd_in; | |
4033b80b WD |
410 | } else if (msg_list_head) { |
411 | FD_SET(msg_fd_out, &w_fds); | |
1ea087a7 WD |
412 | if (msg_fd_out > maxfd) |
413 | maxfd = msg_fd_out; | |
56014c8c | 414 | } |
3309507d | 415 | if (io_filesfrom_f_out >= 0) { |
56014c8c WD |
416 | int new_fd; |
417 | if (io_filesfrom_buflen == 0) { | |
3309507d | 418 | if (io_filesfrom_f_in >= 0) { |
56014c8c WD |
419 | FD_SET(io_filesfrom_f_in, &r_fds); |
420 | new_fd = io_filesfrom_f_in; | |
421 | } else { | |
422 | io_filesfrom_f_out = -1; | |
423 | new_fd = -1; | |
424 | } | |
425 | } else { | |
56014c8c WD |
426 | FD_SET(io_filesfrom_f_out, &w_fds); |
427 | new_fd = io_filesfrom_f_out; | |
428 | } | |
1ea087a7 WD |
429 | if (new_fd > maxfd) |
430 | maxfd = new_fd; | |
554e0a8d AT |
431 | } |
432 | ||
e626b29e | 433 | tv.tv_sec = select_timeout; |
4c36ddbe AT |
434 | tv.tv_usec = 0; |
435 | ||
554e0a8d AT |
436 | errno = 0; |
437 | ||
a7026ba9 | 438 | count = select(maxfd + 1, &r_fds, &w_fds, NULL, &tv); |
a57873b7 | 439 | |
a57873b7 | 440 | if (count <= 0) { |
f89b9368 | 441 | if (errno == EBADF) |
554e0a8d | 442 | exit_cleanup(RERR_SOCKETIO); |
bd717af8 | 443 | check_timeout(); |
4c36ddbe AT |
444 | continue; |
445 | } | |
446 | ||
d17e1dd2 WD |
447 | if (msg_fd_in >= 0 && FD_ISSET(msg_fd_in, &r_fds)) |
448 | read_msg_fd(); | |
4033b80b WD |
449 | else if (msg_list_head && FD_ISSET(msg_fd_out, &w_fds)) |
450 | msg_list_push(NORMAL_FLUSH); | |
554e0a8d | 451 | |
3309507d | 452 | if (io_filesfrom_f_out >= 0) { |
56014c8c WD |
453 | if (io_filesfrom_buflen) { |
454 | if (FD_ISSET(io_filesfrom_f_out, &w_fds)) { | |
455 | int l = write(io_filesfrom_f_out, | |
456 | io_filesfrom_bp, | |
457 | io_filesfrom_buflen); | |
458 | if (l > 0) { | |
459 | if (!(io_filesfrom_buflen -= l)) | |
460 | io_filesfrom_bp = io_filesfrom_buf; | |
461 | else | |
462 | io_filesfrom_bp += l; | |
463 | } else { | |
464 | /* XXX should we complain? */ | |
465 | io_filesfrom_f_out = -1; | |
466 | } | |
467 | } | |
3309507d | 468 | } else if (io_filesfrom_f_in >= 0) { |
56014c8c WD |
469 | if (FD_ISSET(io_filesfrom_f_in, &r_fds)) { |
470 | int l = read(io_filesfrom_f_in, | |
471 | io_filesfrom_buf, | |
472 | sizeof io_filesfrom_buf); | |
473 | if (l <= 0) { | |
474 | /* Send end-of-file marker */ | |
475 | io_filesfrom_buf[0] = '\0'; | |
476 | io_filesfrom_buf[1] = '\0'; | |
477 | io_filesfrom_buflen = io_filesfrom_lastchar? 2 : 1; | |
478 | io_filesfrom_f_in = -1; | |
479 | } else { | |
56014c8c WD |
480 | if (!eol_nulls) { |
481 | char *s = io_filesfrom_buf + l; | |
482 | /* Transform CR and/or LF into '\0' */ | |
483 | while (s-- > io_filesfrom_buf) { | |
484 | if (*s == '\n' || *s == '\r') | |
485 | *s = '\0'; | |
486 | } | |
487 | } | |
488 | if (!io_filesfrom_lastchar) { | |
489 | /* Last buf ended with a '\0', so don't | |
490 | * let this buf start with one. */ | |
491 | while (l && !*io_filesfrom_bp) | |
492 | io_filesfrom_bp++, l--; | |
493 | } | |
494 | if (!l) | |
495 | io_filesfrom_bp = io_filesfrom_buf; | |
496 | else { | |
497 | char *f = io_filesfrom_bp; | |
498 | char *t = f; | |
499 | char *eob = f + l; | |
500 | /* Eliminate any multi-'\0' runs. */ | |
501 | while (f != eob) { | |
502 | if (!(*t++ = *f++)) { | |
503 | while (f != eob && !*f) | |
504 | f++, l--; | |
505 | } | |
506 | } | |
507 | io_filesfrom_lastchar = f[-1]; | |
508 | } | |
509 | io_filesfrom_buflen = l; | |
510 | } | |
511 | } | |
512 | } | |
513 | } | |
514 | ||
f89b9368 WD |
515 | if (!FD_ISSET(fd, &r_fds)) |
516 | continue; | |
554e0a8d | 517 | |
4c36ddbe AT |
518 | n = read(fd, buf, len); |
519 | ||
1ea087a7 WD |
520 | if (n <= 0) { |
521 | if (n == 0) | |
1f75bb10 | 522 | whine_about_eof(fd); /* Doesn't return. */ |
d62bcc17 WD |
523 | if (errno == EINTR || errno == EWOULDBLOCK |
524 | || errno == EAGAIN) | |
7a55d06e | 525 | continue; |
1f75bb10 WD |
526 | |
527 | /* Don't write errors on a dead socket. */ | |
528 | if (fd == sock_f_in) | |
7f459268 | 529 | close_multiplexing_out(); |
1f75bb10 WD |
530 | rsyserr(FERROR, errno, "read error"); |
531 | exit_cleanup(RERR_STREAMIO); | |
8d9dc9f9 | 532 | } |
00bdf899 WD |
533 | |
534 | buf += n; | |
535 | len -= n; | |
536 | ret += n; | |
1f75bb10 WD |
537 | |
538 | if (io_timeout && fd == sock_f_in) | |
00bdf899 | 539 | last_io = time(NULL); |
4c36ddbe | 540 | } |
8d9dc9f9 | 541 | |
4c36ddbe AT |
542 | return ret; |
543 | } | |
8d9dc9f9 | 544 | |
56014c8c WD |
545 | /** |
546 | * Read a line into the "fname" buffer (which must be at least MAXPATHLEN | |
547 | * characters long). | |
548 | */ | |
549 | int read_filesfrom_line(int fd, char *fname) | |
550 | { | |
551 | char ch, *s, *eob = fname + MAXPATHLEN - 1; | |
552 | int cnt; | |
55d5937d | 553 | int reading_remotely = remote_filesfrom_file != NULL; |
56014c8c WD |
554 | int nulls = eol_nulls || reading_remotely; |
555 | ||
556 | start: | |
557 | s = fname; | |
558 | while (1) { | |
559 | cnt = read(fd, &ch, 1); | |
560 | if (cnt < 0 && (errno == EWOULDBLOCK | |
561 | || errno == EINTR || errno == EAGAIN)) { | |
562 | struct timeval tv; | |
563 | fd_set fds; | |
564 | FD_ZERO(&fds); | |
565 | FD_SET(fd, &fds); | |
e626b29e | 566 | tv.tv_sec = select_timeout; |
56014c8c WD |
567 | tv.tv_usec = 0; |
568 | if (!select(fd+1, &fds, NULL, NULL, &tv)) | |
569 | check_timeout(); | |
570 | continue; | |
571 | } | |
572 | if (cnt != 1) | |
573 | break; | |
574 | if (nulls? !ch : (ch == '\r' || ch == '\n')) { | |
575 | /* Skip empty lines if reading locally. */ | |
576 | if (!reading_remotely && s == fname) | |
577 | continue; | |
578 | break; | |
579 | } | |
580 | if (s < eob) | |
581 | *s++ = ch; | |
582 | } | |
583 | *s = '\0'; | |
7a55d06e | 584 | |
6b45fcf1 WD |
585 | /* Dump comments. */ |
586 | if (*fname == '#' || *fname == ';') | |
56014c8c WD |
587 | goto start; |
588 | ||
589 | return s - fname; | |
590 | } | |
7a55d06e MP |
591 | |
592 | ||
1f75bb10 WD |
593 | static char *iobuf_out; |
594 | static int iobuf_out_cnt; | |
595 | ||
596 | void io_start_buffering_out(void) | |
597 | { | |
598 | if (iobuf_out) | |
599 | return; | |
600 | if (!(iobuf_out = new_array(char, IO_BUFFER_SIZE))) | |
601 | out_of_memory("io_start_buffering_out"); | |
602 | iobuf_out_cnt = 0; | |
603 | } | |
604 | ||
605 | ||
606 | static char *iobuf_in; | |
607 | static size_t iobuf_in_siz; | |
608 | ||
609 | void io_start_buffering_in(void) | |
610 | { | |
611 | if (iobuf_in) | |
612 | return; | |
613 | iobuf_in_siz = 2 * IO_BUFFER_SIZE; | |
614 | if (!(iobuf_in = new_array(char, iobuf_in_siz))) | |
615 | out_of_memory("io_start_buffering_in"); | |
616 | } | |
617 | ||
618 | ||
619 | void io_end_buffering(void) | |
620 | { | |
621 | io_flush(NORMAL_FLUSH); | |
622 | if (!io_multiplexing_out) { | |
623 | free(iobuf_out); | |
624 | iobuf_out = NULL; | |
625 | } | |
626 | } | |
627 | ||
628 | ||
880da007 MP |
629 | /** |
630 | * Continue trying to read len bytes - don't return until len has been | |
631 | * read. | |
632 | **/ | |
3151cbae | 633 | static void read_loop(int fd, char *buf, size_t len) |
4c36ddbe AT |
634 | { |
635 | while (len) { | |
636 | int n = read_timeout(fd, buf, len); | |
637 | ||
638 | buf += n; | |
639 | len -= n; | |
8d9dc9f9 AT |
640 | } |
641 | } | |
642 | ||
7a55d06e MP |
643 | |
644 | /** | |
645 | * Read from the file descriptor handling multiplexing - return number | |
646 | * of bytes read. | |
d62bcc17 WD |
647 | * |
648 | * Never returns <= 0. | |
7a55d06e | 649 | */ |
c399d22a | 650 | static int readfd_unbuffered(int fd, char *buf, size_t len) |
8d9dc9f9 | 651 | { |
6fe25398 | 652 | static size_t remaining; |
1f75bb10 | 653 | static size_t iobuf_in_ndx; |
909ce14f | 654 | int tag, ret = 0; |
0d67e00a | 655 | char line[MAXPATHLEN+1]; |
8d9dc9f9 | 656 | |
1f75bb10 | 657 | if (!iobuf_in || fd != sock_f_in) |
4c36ddbe | 658 | return read_timeout(fd, buf, len); |
8d9dc9f9 | 659 | |
76c21947 | 660 | if (!io_multiplexing_in && remaining == 0) { |
1f75bb10 WD |
661 | remaining = read_timeout(fd, iobuf_in, iobuf_in_siz); |
662 | iobuf_in_ndx = 0; | |
76c21947 WD |
663 | } |
664 | ||
8d9dc9f9 AT |
665 | while (ret == 0) { |
666 | if (remaining) { | |
667 | len = MIN(len, remaining); | |
1f75bb10 WD |
668 | memcpy(buf, iobuf_in + iobuf_in_ndx, len); |
669 | iobuf_in_ndx += len; | |
8d9dc9f9 AT |
670 | remaining -= len; |
671 | ret = len; | |
76c21947 | 672 | break; |
8d9dc9f9 AT |
673 | } |
674 | ||
909ce14f | 675 | read_loop(fd, line, 4); |
ff41a59f | 676 | tag = IVAL(line, 0); |
679e7657 | 677 | |
8d9dc9f9 | 678 | remaining = tag & 0xFFFFFF; |
d17e1dd2 | 679 | tag = (tag >> 24) - MPLEX_BASE; |
8d9dc9f9 | 680 | |
d17e1dd2 WD |
681 | switch (tag) { |
682 | case MSG_DATA: | |
1f75bb10 WD |
683 | if (remaining > iobuf_in_siz) { |
684 | if (!(iobuf_in = realloc_array(iobuf_in, char, | |
685 | remaining))) | |
c399d22a | 686 | out_of_memory("readfd_unbuffered"); |
1f75bb10 | 687 | iobuf_in_siz = remaining; |
76c21947 | 688 | } |
1f75bb10 WD |
689 | read_loop(fd, iobuf_in, remaining); |
690 | iobuf_in_ndx = 0; | |
d17e1dd2 | 691 | break; |
0d67e00a WD |
692 | case MSG_DELETED: |
693 | if (remaining >= sizeof line) { | |
694 | rprintf(FERROR, "invalid multi-message %d:%ld\n", | |
695 | tag, (long)remaining); | |
696 | exit_cleanup(RERR_STREAMIO); | |
697 | } | |
698 | read_loop(fd, line, remaining); | |
699 | line[remaining] = '\0'; | |
700 | /* A directory name was sent with the trailing null */ | |
701 | if (remaining > 0 && !line[remaining-1]) | |
702 | log_delete(line, S_IFDIR); | |
703 | else | |
704 | log_delete(line, S_IFREG); | |
705 | remaining = 0; | |
706 | break; | |
d17e1dd2 WD |
707 | case MSG_INFO: |
708 | case MSG_ERROR: | |
709 | if (remaining >= sizeof line) { | |
bd9fca47 WD |
710 | rprintf(FERROR, |
711 | "[%s] multiplexing overflow %d:%ld\n\n", | |
712 | who_am_i(), tag, (long)remaining); | |
d17e1dd2 WD |
713 | exit_cleanup(RERR_STREAMIO); |
714 | } | |
715 | read_loop(fd, line, remaining); | |
716 | rwrite((enum logcode)tag, line, remaining); | |
717 | remaining = 0; | |
718 | break; | |
719 | default: | |
bd9fca47 WD |
720 | rprintf(FERROR, "[%s] unexpected tag %d\n", |
721 | who_am_i(), tag); | |
65417579 | 722 | exit_cleanup(RERR_STREAMIO); |
8d9dc9f9 | 723 | } |
8d9dc9f9 AT |
724 | } |
725 | ||
76c21947 | 726 | if (remaining == 0) |
d17e1dd2 | 727 | io_flush(NORMAL_FLUSH); |
76c21947 | 728 | |
8d9dc9f9 AT |
729 | return ret; |
730 | } | |
731 | ||
732 | ||
909ce14f | 733 | |
880da007 MP |
734 | /** |
735 | * Do a buffered read from @p fd. Don't return until all @p n bytes | |
736 | * have been read. If all @p n can't be read then exit with an | |
737 | * error. | |
738 | **/ | |
3151cbae | 739 | static void readfd(int fd, char *buffer, size_t N) |
720b47f2 | 740 | { |
6ba9279f | 741 | int ret; |
d62bcc17 | 742 | size_t total = 0; |
3151cbae | 743 | |
6ba9279f | 744 | while (total < N) { |
c399d22a | 745 | ret = readfd_unbuffered(fd, buffer + total, N-total); |
6ba9279f | 746 | total += ret; |
7f28dbee | 747 | } |
1b7c47cb | 748 | |
b9f592fb WD |
749 | if (fd == write_batch_monitor_in) { |
750 | if ((size_t)write(batch_fd, buffer, total) != total) | |
751 | exit_cleanup(RERR_FILEIO); | |
752 | } | |
1f75bb10 WD |
753 | |
754 | if (fd == sock_f_in) | |
755 | stats.total_read += total; | |
720b47f2 AT |
756 | } |
757 | ||
758 | ||
0d67e00a | 759 | int read_shortint(int f) |
9361f839 WD |
760 | { |
761 | uchar b[2]; | |
762 | readfd(f, (char *)b, 2); | |
763 | return (b[1] << 8) + b[0]; | |
764 | } | |
765 | ||
766 | ||
b7922338 | 767 | int32 read_int(int f) |
720b47f2 | 768 | { |
4c36ddbe | 769 | char b[4]; |
d730b113 AT |
770 | int32 ret; |
771 | ||
4c36ddbe | 772 | readfd(f,b,4); |
d730b113 | 773 | ret = IVAL(b,0); |
f89b9368 WD |
774 | if (ret == (int32)0xffffffff) |
775 | return -1; | |
d730b113 | 776 | return ret; |
720b47f2 AT |
777 | } |
778 | ||
71c46176 | 779 | int64 read_longint(int f) |
3a6a366f | 780 | { |
71c46176 | 781 | int64 ret; |
3a6a366f AT |
782 | char b[8]; |
783 | ret = read_int(f); | |
71c46176 | 784 | |
f89b9368 | 785 | if ((int32)ret != (int32)0xffffffff) |
8de330a3 | 786 | return ret; |
71c46176 | 787 | |
031fa9ad WD |
788 | #if SIZEOF_INT64 < 8 |
789 | rprintf(FERROR, "Integer overflow: attempted 64-bit offset\n"); | |
790 | exit_cleanup(RERR_UNSUPPORTED); | |
791 | #else | |
91c4da3f S |
792 | readfd(f,b,8); |
793 | ret = IVAL(b,0) | (((int64)IVAL(b,4))<<32); | |
031fa9ad | 794 | #endif |
71c46176 | 795 | |
3a6a366f AT |
796 | return ret; |
797 | } | |
798 | ||
9dd891bb | 799 | void read_buf(int f,char *buf,size_t len) |
720b47f2 | 800 | { |
4c36ddbe | 801 | readfd(f,buf,len); |
720b47f2 AT |
802 | } |
803 | ||
9dd891bb | 804 | void read_sbuf(int f,char *buf,size_t len) |
575f2fca | 805 | { |
93095cbe | 806 | readfd(f, buf, len); |
575f2fca AT |
807 | buf[len] = 0; |
808 | } | |
809 | ||
9361f839 | 810 | uchar read_byte(int f) |
182dca5c | 811 | { |
9361f839 | 812 | uchar c; |
93095cbe | 813 | readfd(f, (char *)&c, 1); |
4c36ddbe | 814 | return c; |
182dca5c | 815 | } |
720b47f2 | 816 | |
188fed95 WD |
817 | /* Populate a sum_struct with values from the socket. This is |
818 | * called by both the sender and the receiver. */ | |
819 | void read_sum_head(int f, struct sum_struct *sum) | |
820 | { | |
821 | sum->count = read_int(f); | |
822 | sum->blength = read_int(f); | |
823 | if (sum->blength < 0 || sum->blength > MAX_BLOCK_SIZE) { | |
67dde161 WD |
824 | rprintf(FERROR, "[%s] Invalid block length %ld\n", |
825 | who_am_i(), (long)sum->blength); | |
188fed95 WD |
826 | exit_cleanup(RERR_PROTOCOL); |
827 | } | |
828 | sum->s2length = protocol_version < 27 ? csum_length : (int)read_int(f); | |
829 | if (sum->s2length < 0 || sum->s2length > MD4_SUM_LENGTH) { | |
67dde161 WD |
830 | rprintf(FERROR, "[%s] Invalid checksum length %d\n", |
831 | who_am_i(), sum->s2length); | |
188fed95 WD |
832 | exit_cleanup(RERR_PROTOCOL); |
833 | } | |
834 | sum->remainder = read_int(f); | |
835 | if (sum->remainder < 0 || sum->remainder > sum->blength) { | |
67dde161 WD |
836 | rprintf(FERROR, "[%s] Invalid remainder length %ld\n", |
837 | who_am_i(), (long)sum->remainder); | |
188fed95 WD |
838 | exit_cleanup(RERR_PROTOCOL); |
839 | } | |
840 | } | |
841 | ||
c207d7ec WD |
842 | /* Send the values from a sum_struct over the socket. Set sum to |
843 | * NULL if there are no checksums to send. This is called by both | |
844 | * the generator and the sender. */ | |
845 | void write_sum_head(int f, struct sum_struct *sum) | |
846 | { | |
847 | static struct sum_struct null_sum; | |
848 | ||
849 | if (sum == NULL) | |
850 | sum = &null_sum; | |
851 | ||
852 | write_int(f, sum->count); | |
853 | write_int(f, sum->blength); | |
854 | if (protocol_version >= 27) | |
855 | write_int(f, sum->s2length); | |
856 | write_int(f, sum->remainder); | |
857 | } | |
858 | ||
880da007 | 859 | |
08571358 MP |
860 | /** |
861 | * Sleep after writing to limit I/O bandwidth usage. | |
862 | * | |
863 | * @todo Rather than sleeping after each write, it might be better to | |
864 | * use some kind of averaging. The current algorithm seems to always | |
865 | * use a bit less bandwidth than specified, because it doesn't make up | |
866 | * for slow periods. But arguably this is a feature. In addition, we | |
867 | * ought to take the time used to write the data into account. | |
71e58630 WD |
868 | * |
869 | * During some phases of big transfers (file FOO is uptodate) this is | |
870 | * called with a small bytes_written every time. As the kernel has to | |
871 | * round small waits up to guarantee that we actually wait at least the | |
872 | * requested number of microseconds, this can become grossly inaccurate. | |
873 | * We therefore keep track of the bytes we've written over time and only | |
874 | * sleep when the accumulated delay is at least 1 tenth of a second. | |
08571358 MP |
875 | **/ |
876 | static void sleep_for_bwlimit(int bytes_written) | |
877 | { | |
71e58630 WD |
878 | static struct timeval prior_tv; |
879 | static long total_written = 0; | |
880 | struct timeval tv, start_tv; | |
881 | long elapsed_usec, sleep_usec; | |
882 | ||
883 | #define ONE_SEC 1000000L /* # of microseconds in a second */ | |
08571358 MP |
884 | |
885 | if (!bwlimit) | |
886 | return; | |
e681e820 | 887 | |
71e58630 WD |
888 | total_written += bytes_written; |
889 | ||
890 | gettimeofday(&start_tv, NULL); | |
891 | if (prior_tv.tv_sec) { | |
892 | elapsed_usec = (start_tv.tv_sec - prior_tv.tv_sec) * ONE_SEC | |
893 | + (start_tv.tv_usec - prior_tv.tv_usec); | |
894 | total_written -= elapsed_usec * bwlimit / (ONE_SEC/1024); | |
895 | if (total_written < 0) | |
896 | total_written = 0; | |
897 | } | |
3151cbae | 898 | |
71e58630 WD |
899 | sleep_usec = total_written * (ONE_SEC/1024) / bwlimit; |
900 | if (sleep_usec < ONE_SEC / 10) { | |
901 | prior_tv = start_tv; | |
902 | return; | |
903 | } | |
08571358 | 904 | |
71e58630 WD |
905 | tv.tv_sec = sleep_usec / ONE_SEC; |
906 | tv.tv_usec = sleep_usec % ONE_SEC; | |
98b332ed | 907 | select(0, NULL, NULL, NULL, &tv); |
71e58630 WD |
908 | |
909 | gettimeofday(&prior_tv, NULL); | |
910 | elapsed_usec = (prior_tv.tv_sec - start_tv.tv_sec) * ONE_SEC | |
911 | + (prior_tv.tv_usec - start_tv.tv_usec); | |
912 | total_written = (sleep_usec - elapsed_usec) * bwlimit / (ONE_SEC/1024); | |
08571358 MP |
913 | } |
914 | ||
915 | ||
1f75bb10 WD |
916 | /* Write len bytes to the file descriptor fd, looping as necessary to get |
917 | * the job done and also (in the generator) reading any data on msg_fd_in | |
918 | * (to avoid deadlock). | |
880da007 MP |
919 | * |
920 | * This function underlies the multiplexing system. The body of the | |
1f75bb10 | 921 | * application never calls this function directly. */ |
9dd891bb | 922 | static void writefd_unbuffered(int fd,char *buf,size_t len) |
720b47f2 | 923 | { |
1ea087a7 | 924 | size_t n, total = 0; |
8d9dc9f9 | 925 | fd_set w_fds, r_fds; |
1ea087a7 | 926 | int maxfd, count, ret; |
8d9dc9f9 | 927 | struct timeval tv; |
720b47f2 | 928 | |
e44f9a12 AT |
929 | no_flush++; |
930 | ||
4c36ddbe | 931 | while (total < len) { |
8d9dc9f9 | 932 | FD_ZERO(&w_fds); |
8d9dc9f9 | 933 | FD_SET(fd,&w_fds); |
1ea087a7 | 934 | maxfd = fd; |
4c36ddbe | 935 | |
d17e1dd2 | 936 | if (msg_fd_in >= 0) { |
56014c8c | 937 | FD_ZERO(&r_fds); |
d17e1dd2 | 938 | FD_SET(msg_fd_in,&r_fds); |
1ea087a7 WD |
939 | if (msg_fd_in > maxfd) |
940 | maxfd = msg_fd_in; | |
8d9dc9f9 | 941 | } |
41cfde6b WD |
942 | if (fd != sock_f_out && iobuf_out_cnt && no_flush == 1) { |
943 | FD_SET(sock_f_out, &w_fds); | |
944 | if (sock_f_out > maxfd) | |
945 | maxfd = sock_f_out; | |
946 | } | |
8d9dc9f9 | 947 | |
e626b29e | 948 | tv.tv_sec = select_timeout; |
8d9dc9f9 | 949 | tv.tv_usec = 0; |
4c36ddbe | 950 | |
554e0a8d | 951 | errno = 0; |
1ea087a7 | 952 | count = select(maxfd + 1, msg_fd_in >= 0 ? &r_fds : NULL, |
d17e1dd2 | 953 | &w_fds, NULL, &tv); |
4c36ddbe AT |
954 | |
955 | if (count <= 0) { | |
1ea087a7 | 956 | if (count < 0 && errno == EBADF) |
554e0a8d | 957 | exit_cleanup(RERR_SOCKETIO); |
bd717af8 | 958 | check_timeout(); |
8d9dc9f9 AT |
959 | continue; |
960 | } | |
4c36ddbe | 961 | |
d17e1dd2 WD |
962 | if (msg_fd_in >= 0 && FD_ISSET(msg_fd_in, &r_fds)) |
963 | read_msg_fd(); | |
554e0a8d | 964 | |
41cfde6b WD |
965 | if (!FD_ISSET(fd, &w_fds)) { |
966 | if (fd != sock_f_out && iobuf_out_cnt) { | |
967 | no_flush--; | |
968 | io_flush(NORMAL_FLUSH); | |
969 | no_flush++; | |
970 | } | |
1ea087a7 | 971 | continue; |
41cfde6b | 972 | } |
4c36ddbe | 973 | |
1ea087a7 WD |
974 | n = len - total; |
975 | if (bwlimit && n > bwlimit_writemax) | |
976 | n = bwlimit_writemax; | |
977 | ret = write(fd, buf + total, n); | |
978 | ||
979 | if (ret <= 0) { | |
3309507d WD |
980 | if (ret < 0) { |
981 | if (errno == EINTR) | |
982 | continue; | |
983 | if (errno == EWOULDBLOCK || errno == EAGAIN) { | |
984 | msleep(1); | |
985 | continue; | |
986 | } | |
f0359dd0 AT |
987 | } |
988 | ||
1ea087a7 | 989 | /* Don't try to write errors back across the stream. */ |
1f75bb10 | 990 | if (fd == sock_f_out) |
7f459268 | 991 | close_multiplexing_out(); |
1ea087a7 | 992 | rsyserr(FERROR, errno, |
dca68b0a WD |
993 | "writefd_unbuffered failed to write %ld bytes: phase \"%s\" [%s]", |
994 | (long)len, io_write_phase, who_am_i()); | |
d1b31da7 WD |
995 | /* If the other side is sending us error messages, try |
996 | * to grab any messages they sent before they died. */ | |
7f459268 | 997 | while (fd == sock_f_out && io_multiplexing_in) { |
ef0c03ff | 998 | io_timeout = select_timeout = 30; |
d1b31da7 WD |
999 | readfd_unbuffered(sock_f_in, io_filesfrom_buf, |
1000 | sizeof io_filesfrom_buf); | |
1001 | } | |
1ea087a7 WD |
1002 | exit_cleanup(RERR_STREAMIO); |
1003 | } | |
4c36ddbe | 1004 | |
1ea087a7 | 1005 | total += ret; |
a800434a | 1006 | |
1f75bb10 WD |
1007 | if (fd == sock_f_out) { |
1008 | if (io_timeout) | |
1009 | last_io = time(NULL); | |
1010 | sleep_for_bwlimit(ret); | |
1011 | } | |
4c36ddbe | 1012 | } |
e44f9a12 AT |
1013 | |
1014 | no_flush--; | |
720b47f2 AT |
1015 | } |
1016 | ||
8d9dc9f9 | 1017 | |
880da007 MP |
1018 | /** |
1019 | * Write an message to a multiplexed stream. If this fails then rsync | |
1020 | * exits. | |
1021 | **/ | |
1f75bb10 | 1022 | static void mplex_write(enum msgcode code, char *buf, size_t len) |
ff41a59f AT |
1023 | { |
1024 | char buffer[4096]; | |
06ce139f | 1025 | size_t n = len; |
8d9dc9f9 | 1026 | |
ff41a59f AT |
1027 | SIVAL(buffer, 0, ((MPLEX_BASE + (int)code)<<24) + len); |
1028 | ||
c399d22a | 1029 | if (n > sizeof buffer - 4) |
3151cbae | 1030 | n = sizeof buffer - 4; |
ff41a59f AT |
1031 | |
1032 | memcpy(&buffer[4], buf, n); | |
1f75bb10 | 1033 | writefd_unbuffered(sock_f_out, buffer, n+4); |
ff41a59f AT |
1034 | |
1035 | len -= n; | |
1036 | buf += n; | |
1037 | ||
1ea087a7 | 1038 | if (len) |
1f75bb10 | 1039 | writefd_unbuffered(sock_f_out, buf, len); |
d6dead6b AT |
1040 | } |
1041 | ||
ff41a59f | 1042 | |
d17e1dd2 | 1043 | void io_flush(int flush_it_all) |
d6dead6b | 1044 | { |
d17e1dd2 | 1045 | msg_list_push(flush_it_all); |
90ba34e2 | 1046 | |
1f75bb10 | 1047 | if (!iobuf_out_cnt || no_flush) |
d17e1dd2 | 1048 | return; |
8d9dc9f9 | 1049 | |
d17e1dd2 | 1050 | if (io_multiplexing_out) |
1f75bb10 | 1051 | mplex_write(MSG_DATA, iobuf_out, iobuf_out_cnt); |
d17e1dd2 | 1052 | else |
1f75bb10 WD |
1053 | writefd_unbuffered(sock_f_out, iobuf_out, iobuf_out_cnt); |
1054 | iobuf_out_cnt = 0; | |
8d9dc9f9 AT |
1055 | } |
1056 | ||
0ba48136 | 1057 | |
9dd891bb | 1058 | static void writefd(int fd,char *buf,size_t len) |
d6dead6b | 1059 | { |
4033b80b WD |
1060 | if (fd == msg_fd_out) { |
1061 | rprintf(FERROR, "Internal error: wrong write used in receiver.\n"); | |
1062 | exit_cleanup(RERR_PROTOCOL); | |
1063 | } | |
90ba34e2 | 1064 | |
1f75bb10 WD |
1065 | if (fd == sock_f_out) |
1066 | stats.total_written += len; | |
1067 | ||
b9f592fb WD |
1068 | if (fd == write_batch_monitor_out) { |
1069 | if ((size_t)write(batch_fd, buf, len) != len) | |
1070 | exit_cleanup(RERR_FILEIO); | |
1071 | } | |
1072 | ||
1f75bb10 | 1073 | if (!iobuf_out || fd != sock_f_out) { |
4c36ddbe AT |
1074 | writefd_unbuffered(fd, buf, len); |
1075 | return; | |
1076 | } | |
d6dead6b AT |
1077 | |
1078 | while (len) { | |
1f75bb10 | 1079 | int n = MIN((int)len, IO_BUFFER_SIZE - iobuf_out_cnt); |
d6dead6b | 1080 | if (n > 0) { |
1f75bb10 | 1081 | memcpy(iobuf_out+iobuf_out_cnt, buf, n); |
d6dead6b AT |
1082 | buf += n; |
1083 | len -= n; | |
1f75bb10 | 1084 | iobuf_out_cnt += n; |
d6dead6b | 1085 | } |
3151cbae | 1086 | |
1f75bb10 | 1087 | if (iobuf_out_cnt == IO_BUFFER_SIZE) |
d17e1dd2 | 1088 | io_flush(NORMAL_FLUSH); |
d6dead6b | 1089 | } |
d6dead6b | 1090 | } |
720b47f2 AT |
1091 | |
1092 | ||
0d67e00a | 1093 | void write_shortint(int f, int x) |
9361f839 WD |
1094 | { |
1095 | uchar b[2]; | |
1096 | b[0] = x; | |
1097 | b[1] = x >> 8; | |
1098 | writefd(f, (char *)b, 2); | |
1099 | } | |
1100 | ||
1101 | ||
b7922338 | 1102 | void write_int(int f,int32 x) |
720b47f2 | 1103 | { |
8d9dc9f9 AT |
1104 | char b[4]; |
1105 | SIVAL(b,0,x); | |
4c36ddbe | 1106 | writefd(f,b,4); |
720b47f2 AT |
1107 | } |
1108 | ||
7a24c346 | 1109 | |
805edf9d MP |
1110 | void write_int_named(int f, int32 x, const char *phase) |
1111 | { | |
1112 | io_write_phase = phase; | |
1113 | write_int(f, x); | |
1114 | io_write_phase = phase_unknown; | |
1115 | } | |
1116 | ||
1117 | ||
7a24c346 MP |
1118 | /* |
1119 | * Note: int64 may actually be a 32-bit type if ./configure couldn't find any | |
1120 | * 64-bit types on this platform. | |
1121 | */ | |
71c46176 | 1122 | void write_longint(int f, int64 x) |
3a6a366f | 1123 | { |
3a6a366f | 1124 | char b[8]; |
3a6a366f | 1125 | |
91c4da3f | 1126 | if (x <= 0x7FFFFFFF) { |
3a6a366f AT |
1127 | write_int(f, (int)x); |
1128 | return; | |
1129 | } | |
1130 | ||
031fa9ad WD |
1131 | #if SIZEOF_INT64 < 8 |
1132 | rprintf(FERROR, "Integer overflow: attempted 64-bit offset\n"); | |
1133 | exit_cleanup(RERR_UNSUPPORTED); | |
1134 | #else | |
8de330a3 | 1135 | write_int(f, (int32)0xFFFFFFFF); |
3a6a366f AT |
1136 | SIVAL(b,0,(x&0xFFFFFFFF)); |
1137 | SIVAL(b,4,((x>>32)&0xFFFFFFFF)); | |
1138 | ||
4c36ddbe | 1139 | writefd(f,b,8); |
031fa9ad | 1140 | #endif |
3a6a366f AT |
1141 | } |
1142 | ||
9dd891bb | 1143 | void write_buf(int f,char *buf,size_t len) |
720b47f2 | 1144 | { |
4c36ddbe | 1145 | writefd(f,buf,len); |
720b47f2 AT |
1146 | } |
1147 | ||
9361f839 | 1148 | |
880da007 | 1149 | /** Write a string to the connection */ |
cf338ab1 | 1150 | void write_sbuf(int f, char *buf) |
f0fca04e | 1151 | { |
93095cbe | 1152 | writefd(f, buf, strlen(buf)); |
f0fca04e AT |
1153 | } |
1154 | ||
9361f839 WD |
1155 | |
1156 | void write_byte(int f, uchar c) | |
182dca5c | 1157 | { |
93095cbe | 1158 | writefd(f, (char *)&c, 1); |
182dca5c AT |
1159 | } |
1160 | ||
7a55d06e | 1161 | |
914cc65c | 1162 | /** |
6ed6d7f5 WD |
1163 | * Read a line of up to @p maxlen characters into @p buf (not counting |
1164 | * the trailing null). Strips the (required) trailing newline and all | |
1165 | * carriage returns. | |
914cc65c | 1166 | * |
6ed6d7f5 | 1167 | * @return 1 for success; 0 for I/O error or truncation. |
914cc65c | 1168 | **/ |
9dd891bb | 1169 | int read_line(int f, char *buf, size_t maxlen) |
f0fca04e AT |
1170 | { |
1171 | while (maxlen) { | |
528bfcd7 | 1172 | buf[0] = 0; |
f0fca04e | 1173 | read_buf(f, buf, 1); |
914cc65c MP |
1174 | if (buf[0] == 0) |
1175 | return 0; | |
6ed6d7f5 | 1176 | if (buf[0] == '\n') |
f0fca04e | 1177 | break; |
f0fca04e AT |
1178 | if (buf[0] != '\r') { |
1179 | buf++; | |
1180 | maxlen--; | |
1181 | } | |
1182 | } | |
6ed6d7f5 WD |
1183 | *buf = '\0'; |
1184 | return maxlen > 0; | |
f0fca04e AT |
1185 | } |
1186 | ||
1187 | ||
1188 | void io_printf(int fd, const char *format, ...) | |
1189 | { | |
d62bcc17 | 1190 | va_list ap; |
f0fca04e AT |
1191 | char buf[1024]; |
1192 | int len; | |
3151cbae | 1193 | |
f0fca04e | 1194 | va_start(ap, format); |
3151cbae | 1195 | len = vsnprintf(buf, sizeof buf, format, ap); |
f0fca04e AT |
1196 | va_end(ap); |
1197 | ||
f89b9368 WD |
1198 | if (len < 0) |
1199 | exit_cleanup(RERR_STREAMIO); | |
f0fca04e AT |
1200 | |
1201 | write_sbuf(fd, buf); | |
1202 | } | |
8d9dc9f9 AT |
1203 | |
1204 | ||
d17e1dd2 | 1205 | /** Setup for multiplexing a MSG_* stream with the data stream. */ |
1f75bb10 | 1206 | void io_start_multiplex_out(void) |
8d9dc9f9 | 1207 | { |
d17e1dd2 | 1208 | io_flush(NORMAL_FLUSH); |
1f75bb10 | 1209 | io_start_buffering_out(); |
8d9dc9f9 AT |
1210 | io_multiplexing_out = 1; |
1211 | } | |
1212 | ||
d17e1dd2 | 1213 | /** Setup for multiplexing a MSG_* stream with the data stream. */ |
1f75bb10 | 1214 | void io_start_multiplex_in(void) |
8d9dc9f9 | 1215 | { |
d17e1dd2 | 1216 | io_flush(NORMAL_FLUSH); |
1f75bb10 | 1217 | io_start_buffering_in(); |
8d9dc9f9 AT |
1218 | io_multiplexing_in = 1; |
1219 | } | |
1220 | ||
d17e1dd2 WD |
1221 | /** Write an message to the multiplexed data stream. */ |
1222 | int io_multiplex_write(enum msgcode code, char *buf, size_t len) | |
8d9dc9f9 | 1223 | { |
f89b9368 WD |
1224 | if (!io_multiplexing_out) |
1225 | return 0; | |
8d9dc9f9 | 1226 | |
d17e1dd2 | 1227 | io_flush(NORMAL_FLUSH); |
1b7c47cb | 1228 | stats.total_written += (len+4); |
1f75bb10 | 1229 | mplex_write(code, buf, len); |
8d9dc9f9 AT |
1230 | return 1; |
1231 | } | |
1232 | ||
7f459268 WD |
1233 | void close_multiplexing_in(void) |
1234 | { | |
1235 | io_multiplexing_in = 0; | |
1236 | } | |
1237 | ||
d17e1dd2 | 1238 | /** Stop output multiplexing. */ |
7f459268 | 1239 | void close_multiplexing_out(void) |
554e0a8d AT |
1240 | { |
1241 | io_multiplexing_out = 0; | |
1242 | } | |
1243 | ||
b9f592fb WD |
1244 | void start_write_batch(int fd) |
1245 | { | |
741d6544 WD |
1246 | write_stream_flags(batch_fd); |
1247 | ||
b9f592fb WD |
1248 | /* Some communication has already taken place, but we don't |
1249 | * enable batch writing until here so that we can write a | |
1250 | * canonical record of the communication even though the | |
1251 | * actual communication so far depends on whether a daemon | |
1252 | * is involved. */ | |
1253 | write_int(batch_fd, protocol_version); | |
1254 | write_int(batch_fd, checksum_seed); | |
b9f592fb WD |
1255 | |
1256 | if (am_sender) | |
1257 | write_batch_monitor_out = fd; | |
1258 | else | |
1259 | write_batch_monitor_in = fd; | |
1260 | } | |
1261 | ||
1262 | void stop_write_batch(void) | |
1263 | { | |
1264 | write_batch_monitor_out = -1; | |
1265 | write_batch_monitor_in = -1; | |
1266 | } |