| 1 | /* -*- c-file-style: "linux" -*- |
| 2 | * |
| 3 | * Copyright (C) 1996-2001 by Andrew Tridgell |
| 4 | * Copyright (C) Paul Mackerras 1996 |
| 5 | * Copyright (C) 2001, 2002 by Martin Pool <mbp@samba.org> |
| 6 | * |
| 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. |
| 11 | * |
| 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. |
| 16 | * |
| 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 | */ |
| 21 | |
| 22 | /** |
| 23 | * @file io.c |
| 24 | * |
| 25 | * Socket and pipe IO utilities used in rsync. |
| 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 | **/ |
| 36 | |
| 37 | #include "rsync.h" |
| 38 | |
| 39 | /** If no timeout is specified then use a 60 second select timeout */ |
| 40 | #define SELECT_TIMEOUT 60 |
| 41 | |
| 42 | static int io_multiplexing_out; |
| 43 | static int io_multiplexing_in; |
| 44 | static int multiplex_in_fd = -1; |
| 45 | static int multiplex_out_fd = -1; |
| 46 | static time_t last_io; |
| 47 | static int no_flush; |
| 48 | |
| 49 | extern int bwlimit; |
| 50 | extern int verbose; |
| 51 | extern int io_timeout; |
| 52 | extern int am_server; |
| 53 | extern int am_daemon; |
| 54 | extern int am_sender; |
| 55 | extern struct stats stats; |
| 56 | |
| 57 | |
| 58 | const char phase_unknown[] = "unknown"; |
| 59 | |
| 60 | /** |
| 61 | * The connection might be dropped at some point; perhaps because the |
| 62 | * remote instance crashed. Just giving the offset on the stream is |
| 63 | * not very helpful. So instead we try to make io_phase_name point to |
| 64 | * something useful. |
| 65 | * |
| 66 | * For buffered/multiplexed IO these names will be somewhat |
| 67 | * approximate; perhaps for ease of support we would rather make the |
| 68 | * buffer always flush when a single application-level IO finishes. |
| 69 | * |
| 70 | * @todo Perhaps we want some simple stack functionality, but there's |
| 71 | * no need to overdo it. |
| 72 | **/ |
| 73 | const char *io_write_phase = phase_unknown; |
| 74 | const char *io_read_phase = phase_unknown; |
| 75 | |
| 76 | /** Ignore EOF errors while reading a module listing if the remote |
| 77 | version is 24 or less. */ |
| 78 | int kludge_around_eof = False; |
| 79 | |
| 80 | int msg_fd_in = -1; |
| 81 | int msg_fd_out = -1; |
| 82 | |
| 83 | static int io_filesfrom_f_in = -1; |
| 84 | static int io_filesfrom_f_out = -1; |
| 85 | static char io_filesfrom_buf[2048]; |
| 86 | static char *io_filesfrom_bp; |
| 87 | static char io_filesfrom_lastchar; |
| 88 | static int io_filesfrom_buflen; |
| 89 | |
| 90 | static void read_loop(int fd, char *buf, size_t len); |
| 91 | |
| 92 | struct redo_list { |
| 93 | struct redo_list *next; |
| 94 | int num; |
| 95 | }; |
| 96 | |
| 97 | static struct redo_list *redo_list_head; |
| 98 | static struct redo_list *redo_list_tail; |
| 99 | |
| 100 | struct msg_list { |
| 101 | struct msg_list *next; |
| 102 | char *buf; |
| 103 | int len; |
| 104 | }; |
| 105 | |
| 106 | static struct msg_list *msg_list_head; |
| 107 | static struct msg_list *msg_list_tail; |
| 108 | |
| 109 | static void redo_list_add(int num) |
| 110 | { |
| 111 | struct redo_list *rl; |
| 112 | |
| 113 | if (!(rl = new(struct redo_list))) |
| 114 | exit_cleanup(RERR_MALLOC); |
| 115 | rl->next = NULL; |
| 116 | rl->num = num; |
| 117 | if (redo_list_tail) |
| 118 | redo_list_tail->next = rl; |
| 119 | else |
| 120 | redo_list_head = rl; |
| 121 | redo_list_tail = rl; |
| 122 | } |
| 123 | |
| 124 | static void check_timeout(void) |
| 125 | { |
| 126 | time_t t; |
| 127 | |
| 128 | if (!io_timeout) |
| 129 | return; |
| 130 | |
| 131 | if (!last_io) { |
| 132 | last_io = time(NULL); |
| 133 | return; |
| 134 | } |
| 135 | |
| 136 | t = time(NULL); |
| 137 | |
| 138 | if (last_io && io_timeout && (t-last_io) >= io_timeout) { |
| 139 | if (!am_server && !am_daemon) { |
| 140 | rprintf(FERROR,"io timeout after %d seconds - exiting\n", |
| 141 | (int)(t-last_io)); |
| 142 | } |
| 143 | exit_cleanup(RERR_TIMEOUT); |
| 144 | } |
| 145 | } |
| 146 | |
| 147 | /** Setup the fd used to receive MSG_* messages. Only needed when |
| 148 | * we're the generator because the sender and receiver both use the |
| 149 | * multiplexed IO setup. */ |
| 150 | void set_msg_fd_in(int fd) |
| 151 | { |
| 152 | msg_fd_in = fd; |
| 153 | } |
| 154 | |
| 155 | /** Setup the fd used to send our MSG_* messages. Only needed when |
| 156 | * we're the receiver because the generator and the sender both use |
| 157 | * the multiplexed IO setup. */ |
| 158 | void set_msg_fd_out(int fd) |
| 159 | { |
| 160 | msg_fd_out = fd; |
| 161 | set_nonblocking(msg_fd_out); |
| 162 | } |
| 163 | |
| 164 | /* Add a message to the pending MSG_* list. */ |
| 165 | static void msg_list_add(int code, char *buf, int len) |
| 166 | { |
| 167 | struct msg_list *ml; |
| 168 | |
| 169 | if (!(ml = new(struct msg_list))) |
| 170 | exit_cleanup(RERR_MALLOC); |
| 171 | ml->next = NULL; |
| 172 | if (!(ml->buf = new_array(char, len+4))) |
| 173 | exit_cleanup(RERR_MALLOC); |
| 174 | SIVAL(ml->buf, 0, ((code+MPLEX_BASE)<<24) | len); |
| 175 | memcpy(ml->buf+4, buf, len); |
| 176 | ml->len = len+4; |
| 177 | if (msg_list_tail) |
| 178 | msg_list_tail->next = ml; |
| 179 | else |
| 180 | msg_list_head = ml; |
| 181 | msg_list_tail = ml; |
| 182 | } |
| 183 | |
| 184 | void send_msg(enum msgcode code, char *buf, int len) |
| 185 | { |
| 186 | msg_list_add(code, buf, len); |
| 187 | msg_list_push(NORMAL_FLUSH); |
| 188 | } |
| 189 | |
| 190 | /** Read a message from the MSG_* fd and dispatch it. This is only |
| 191 | * called by the generator. */ |
| 192 | static void read_msg_fd(void) |
| 193 | { |
| 194 | char buf[200]; |
| 195 | size_t n; |
| 196 | int fd = msg_fd_in; |
| 197 | int tag, len; |
| 198 | |
| 199 | /* Temporarily disable msg_fd_in. This is needed because we |
| 200 | * may call a write routine that could try to call us back. */ |
| 201 | msg_fd_in = -1; |
| 202 | |
| 203 | read_loop(fd, buf, 4); |
| 204 | tag = IVAL(buf, 0); |
| 205 | |
| 206 | len = tag & 0xFFFFFF; |
| 207 | tag = (tag >> 24) - MPLEX_BASE; |
| 208 | |
| 209 | switch (tag) { |
| 210 | case MSG_DONE: |
| 211 | if (len != 0) |
| 212 | exit_cleanup(RERR_STREAMIO); |
| 213 | redo_list_add(-1); |
| 214 | break; |
| 215 | case MSG_REDO: |
| 216 | if (len != 4) |
| 217 | exit_cleanup(RERR_STREAMIO); |
| 218 | read_loop(fd, buf, 4); |
| 219 | redo_list_add(IVAL(buf,0)); |
| 220 | break; |
| 221 | case MSG_INFO: |
| 222 | case MSG_ERROR: |
| 223 | case MSG_LOG: |
| 224 | while (len) { |
| 225 | n = len; |
| 226 | if (n >= sizeof buf) |
| 227 | n = sizeof buf - 1; |
| 228 | read_loop(fd, buf, n); |
| 229 | rwrite((enum logcode)tag, buf, n); |
| 230 | len -= n; |
| 231 | } |
| 232 | break; |
| 233 | default: |
| 234 | exit_cleanup(RERR_STREAMIO); |
| 235 | } |
| 236 | |
| 237 | msg_fd_in = fd; |
| 238 | } |
| 239 | |
| 240 | /* Try to push messages off the list onto the wire. If we leave with more |
| 241 | * to do, return 0. On error, return -1. If everything flushed, return 1. |
| 242 | * This is only called by the receiver. */ |
| 243 | int msg_list_push(int flush_it_all) |
| 244 | { |
| 245 | static int written = 0; |
| 246 | struct timeval tv; |
| 247 | fd_set fds; |
| 248 | |
| 249 | if (msg_fd_out < 0) |
| 250 | return -1; |
| 251 | |
| 252 | while (msg_list_head) { |
| 253 | struct msg_list *ml = msg_list_head; |
| 254 | int n = write(msg_fd_out, ml->buf + written, ml->len - written); |
| 255 | if (n < 0) { |
| 256 | if (errno == EINTR) |
| 257 | continue; |
| 258 | if (errno != EWOULDBLOCK && errno != EAGAIN) |
| 259 | return -1; |
| 260 | if (!flush_it_all) |
| 261 | return 0; |
| 262 | FD_ZERO(&fds); |
| 263 | FD_SET(msg_fd_out, &fds); |
| 264 | tv.tv_sec = io_timeout ? io_timeout : SELECT_TIMEOUT; |
| 265 | tv.tv_usec = 0; |
| 266 | if (!select(msg_fd_out+1, NULL, &fds, NULL, &tv)) |
| 267 | check_timeout(); |
| 268 | } else if ((written += n) == ml->len) { |
| 269 | free(ml->buf); |
| 270 | msg_list_head = ml->next; |
| 271 | if (!msg_list_head) |
| 272 | msg_list_tail = NULL; |
| 273 | free(ml); |
| 274 | written = 0; |
| 275 | } |
| 276 | } |
| 277 | return 1; |
| 278 | } |
| 279 | |
| 280 | int get_redo_num(void) |
| 281 | { |
| 282 | struct redo_list *next; |
| 283 | int num; |
| 284 | |
| 285 | while (!redo_list_head) |
| 286 | read_msg_fd(); |
| 287 | |
| 288 | num = redo_list_head->num; |
| 289 | next = redo_list_head->next; |
| 290 | free(redo_list_head); |
| 291 | redo_list_head = next; |
| 292 | if (!next) |
| 293 | redo_list_tail = NULL; |
| 294 | |
| 295 | return num; |
| 296 | } |
| 297 | |
| 298 | /** |
| 299 | * When we're the receiver and we have a local --files-from list of names |
| 300 | * that needs to be sent over the socket to the sender, we have to do two |
| 301 | * things at the same time: send the sender a list of what files we're |
| 302 | * processing and read the incoming file+info list from the sender. We do |
| 303 | * this by augmenting the read_timeout() function to copy this data. It |
| 304 | * uses the io_filesfrom_buf to read a block of data from f_in (when it is |
| 305 | * ready, since it might be a pipe) and then blast it out f_out (when it |
| 306 | * is ready to receive more data). |
| 307 | */ |
| 308 | void io_set_filesfrom_fds(int f_in, int f_out) |
| 309 | { |
| 310 | io_filesfrom_f_in = f_in; |
| 311 | io_filesfrom_f_out = f_out; |
| 312 | io_filesfrom_bp = io_filesfrom_buf; |
| 313 | io_filesfrom_lastchar = '\0'; |
| 314 | io_filesfrom_buflen = 0; |
| 315 | } |
| 316 | |
| 317 | /** |
| 318 | * It's almost always an error to get an EOF when we're trying to read |
| 319 | * from the network, because the protocol is self-terminating. |
| 320 | * |
| 321 | * However, there is one unfortunate cases where it is not, which is |
| 322 | * rsync <2.4.6 sending a list of modules on a server, since the list |
| 323 | * is terminated by closing the socket. So, for the section of the |
| 324 | * program where that is a problem (start_socket_client), |
| 325 | * kludge_around_eof is True and we just exit. |
| 326 | */ |
| 327 | static void whine_about_eof(void) |
| 328 | { |
| 329 | if (kludge_around_eof) |
| 330 | exit_cleanup(0); |
| 331 | else { |
| 332 | rprintf(FERROR, |
| 333 | "%s: connection unexpectedly closed " |
| 334 | "(%.0f bytes read so far)\n", |
| 335 | RSYNC_NAME, (double)stats.total_read); |
| 336 | |
| 337 | exit_cleanup(RERR_STREAMIO); |
| 338 | } |
| 339 | } |
| 340 | |
| 341 | |
| 342 | static void die_from_readerr(int err) |
| 343 | { |
| 344 | /* this prevents us trying to write errors on a dead socket */ |
| 345 | io_multiplexing_close(); |
| 346 | |
| 347 | rprintf(FERROR, "%s: read error: %s\n", |
| 348 | RSYNC_NAME, strerror(err)); |
| 349 | exit_cleanup(RERR_STREAMIO); |
| 350 | } |
| 351 | |
| 352 | |
| 353 | /** |
| 354 | * Read from a socket with IO timeout. return the number of bytes |
| 355 | * read. If no bytes can be read then exit, never return a number <= 0. |
| 356 | * |
| 357 | * TODO: If the remote shell connection fails, then current versions |
| 358 | * actually report an "unexpected EOF" error here. Since it's a |
| 359 | * fairly common mistake to try to use rsh when ssh is required, we |
| 360 | * should trap that: if we fail to read any data at all, we should |
| 361 | * give a better explanation. We can tell whether the connection has |
| 362 | * started by looking e.g. at whether the remote version is known yet. |
| 363 | */ |
| 364 | static int read_timeout(int fd, char *buf, size_t len) |
| 365 | { |
| 366 | int n, ret=0; |
| 367 | |
| 368 | io_flush(NORMAL_FLUSH); |
| 369 | |
| 370 | while (ret == 0) { |
| 371 | /* until we manage to read *something* */ |
| 372 | fd_set r_fds, w_fds; |
| 373 | struct timeval tv; |
| 374 | int fd_count = fd+1; |
| 375 | int count; |
| 376 | |
| 377 | FD_ZERO(&r_fds); |
| 378 | FD_SET(fd, &r_fds); |
| 379 | if (msg_fd_in >= 0) { |
| 380 | FD_SET(msg_fd_in, &r_fds); |
| 381 | if (msg_fd_in >= fd_count) |
| 382 | fd_count = msg_fd_in+1; |
| 383 | } |
| 384 | if (io_filesfrom_f_out >= 0) { |
| 385 | int new_fd; |
| 386 | if (io_filesfrom_buflen == 0) { |
| 387 | if (io_filesfrom_f_in >= 0) { |
| 388 | FD_SET(io_filesfrom_f_in, &r_fds); |
| 389 | new_fd = io_filesfrom_f_in; |
| 390 | } else { |
| 391 | io_filesfrom_f_out = -1; |
| 392 | new_fd = -1; |
| 393 | } |
| 394 | } else { |
| 395 | FD_ZERO(&w_fds); |
| 396 | FD_SET(io_filesfrom_f_out, &w_fds); |
| 397 | new_fd = io_filesfrom_f_out; |
| 398 | } |
| 399 | if (new_fd >= fd_count) |
| 400 | fd_count = new_fd+1; |
| 401 | } |
| 402 | |
| 403 | tv.tv_sec = io_timeout?io_timeout:SELECT_TIMEOUT; |
| 404 | tv.tv_usec = 0; |
| 405 | |
| 406 | errno = 0; |
| 407 | |
| 408 | count = select(fd_count, &r_fds, |
| 409 | io_filesfrom_buflen? &w_fds : NULL, |
| 410 | NULL, &tv); |
| 411 | |
| 412 | if (count == 0) { |
| 413 | msg_list_push(NORMAL_FLUSH); |
| 414 | check_timeout(); |
| 415 | } |
| 416 | |
| 417 | if (count <= 0) { |
| 418 | if (errno == EBADF) { |
| 419 | exit_cleanup(RERR_SOCKETIO); |
| 420 | } |
| 421 | continue; |
| 422 | } |
| 423 | |
| 424 | if (msg_fd_in >= 0 && FD_ISSET(msg_fd_in, &r_fds)) |
| 425 | read_msg_fd(); |
| 426 | |
| 427 | if (io_filesfrom_f_out >= 0) { |
| 428 | if (io_filesfrom_buflen) { |
| 429 | if (FD_ISSET(io_filesfrom_f_out, &w_fds)) { |
| 430 | int l = write(io_filesfrom_f_out, |
| 431 | io_filesfrom_bp, |
| 432 | io_filesfrom_buflen); |
| 433 | if (l > 0) { |
| 434 | if (!(io_filesfrom_buflen -= l)) |
| 435 | io_filesfrom_bp = io_filesfrom_buf; |
| 436 | else |
| 437 | io_filesfrom_bp += l; |
| 438 | } else { |
| 439 | /* XXX should we complain? */ |
| 440 | io_filesfrom_f_out = -1; |
| 441 | } |
| 442 | } |
| 443 | } else if (io_filesfrom_f_in >= 0) { |
| 444 | if (FD_ISSET(io_filesfrom_f_in, &r_fds)) { |
| 445 | int l = read(io_filesfrom_f_in, |
| 446 | io_filesfrom_buf, |
| 447 | sizeof io_filesfrom_buf); |
| 448 | if (l <= 0) { |
| 449 | /* Send end-of-file marker */ |
| 450 | io_filesfrom_buf[0] = '\0'; |
| 451 | io_filesfrom_buf[1] = '\0'; |
| 452 | io_filesfrom_buflen = io_filesfrom_lastchar? 2 : 1; |
| 453 | io_filesfrom_f_in = -1; |
| 454 | } else { |
| 455 | extern int eol_nulls; |
| 456 | if (!eol_nulls) { |
| 457 | char *s = io_filesfrom_buf + l; |
| 458 | /* Transform CR and/or LF into '\0' */ |
| 459 | while (s-- > io_filesfrom_buf) { |
| 460 | if (*s == '\n' || *s == '\r') |
| 461 | *s = '\0'; |
| 462 | } |
| 463 | } |
| 464 | if (!io_filesfrom_lastchar) { |
| 465 | /* Last buf ended with a '\0', so don't |
| 466 | * let this buf start with one. */ |
| 467 | while (l && !*io_filesfrom_bp) |
| 468 | io_filesfrom_bp++, l--; |
| 469 | } |
| 470 | if (!l) |
| 471 | io_filesfrom_bp = io_filesfrom_buf; |
| 472 | else { |
| 473 | char *f = io_filesfrom_bp; |
| 474 | char *t = f; |
| 475 | char *eob = f + l; |
| 476 | /* Eliminate any multi-'\0' runs. */ |
| 477 | while (f != eob) { |
| 478 | if (!(*t++ = *f++)) { |
| 479 | while (f != eob && !*f) |
| 480 | f++, l--; |
| 481 | } |
| 482 | } |
| 483 | io_filesfrom_lastchar = f[-1]; |
| 484 | } |
| 485 | io_filesfrom_buflen = l; |
| 486 | } |
| 487 | } |
| 488 | } |
| 489 | } |
| 490 | |
| 491 | if (!FD_ISSET(fd, &r_fds)) continue; |
| 492 | |
| 493 | n = read(fd, buf, len); |
| 494 | |
| 495 | if (n > 0) { |
| 496 | buf += n; |
| 497 | len -= n; |
| 498 | ret += n; |
| 499 | if (io_timeout) |
| 500 | last_io = time(NULL); |
| 501 | continue; |
| 502 | } else if (n == 0) { |
| 503 | whine_about_eof(); |
| 504 | return -1; /* doesn't return */ |
| 505 | } else if (n < 0) { |
| 506 | if (errno == EINTR || errno == EWOULDBLOCK || |
| 507 | errno == EAGAIN) |
| 508 | continue; |
| 509 | die_from_readerr(errno); |
| 510 | } |
| 511 | } |
| 512 | |
| 513 | return ret; |
| 514 | } |
| 515 | |
| 516 | /** |
| 517 | * Read a line into the "fname" buffer (which must be at least MAXPATHLEN |
| 518 | * characters long). |
| 519 | */ |
| 520 | int read_filesfrom_line(int fd, char *fname) |
| 521 | { |
| 522 | char ch, *s, *eob = fname + MAXPATHLEN - 1; |
| 523 | int cnt; |
| 524 | extern int io_timeout; |
| 525 | extern int eol_nulls; |
| 526 | extern char *remote_filesfrom_file; |
| 527 | int reading_remotely = remote_filesfrom_file != NULL; |
| 528 | int nulls = eol_nulls || reading_remotely; |
| 529 | |
| 530 | start: |
| 531 | s = fname; |
| 532 | while (1) { |
| 533 | cnt = read(fd, &ch, 1); |
| 534 | if (cnt < 0 && (errno == EWOULDBLOCK |
| 535 | || errno == EINTR || errno == EAGAIN)) { |
| 536 | struct timeval tv; |
| 537 | fd_set fds; |
| 538 | FD_ZERO(&fds); |
| 539 | FD_SET(fd, &fds); |
| 540 | tv.tv_sec = io_timeout? io_timeout : SELECT_TIMEOUT; |
| 541 | tv.tv_usec = 0; |
| 542 | if (!select(fd+1, &fds, NULL, NULL, &tv)) |
| 543 | check_timeout(); |
| 544 | continue; |
| 545 | } |
| 546 | if (cnt != 1) |
| 547 | break; |
| 548 | if (nulls? !ch : (ch == '\r' || ch == '\n')) { |
| 549 | /* Skip empty lines if reading locally. */ |
| 550 | if (!reading_remotely && s == fname) |
| 551 | continue; |
| 552 | break; |
| 553 | } |
| 554 | if (s < eob) |
| 555 | *s++ = ch; |
| 556 | } |
| 557 | *s = '\0'; |
| 558 | |
| 559 | /* Dump comments. */ |
| 560 | if (*fname == '#' || *fname == ';') |
| 561 | goto start; |
| 562 | |
| 563 | return s - fname; |
| 564 | } |
| 565 | |
| 566 | |
| 567 | /** |
| 568 | * Continue trying to read len bytes - don't return until len has been |
| 569 | * read. |
| 570 | **/ |
| 571 | static void read_loop(int fd, char *buf, size_t len) |
| 572 | { |
| 573 | while (len) { |
| 574 | int n = read_timeout(fd, buf, len); |
| 575 | |
| 576 | buf += n; |
| 577 | len -= n; |
| 578 | } |
| 579 | } |
| 580 | |
| 581 | |
| 582 | /** |
| 583 | * Read from the file descriptor handling multiplexing - return number |
| 584 | * of bytes read. |
| 585 | * |
| 586 | * Never returns <= 0. |
| 587 | */ |
| 588 | static int read_unbuffered(int fd, char *buf, size_t len) |
| 589 | { |
| 590 | static size_t remaining; |
| 591 | int tag, ret = 0; |
| 592 | char line[1024]; |
| 593 | static char *buffer; |
| 594 | static size_t bufferIdx = 0; |
| 595 | static size_t bufferSz; |
| 596 | |
| 597 | if (fd != multiplex_in_fd) |
| 598 | return read_timeout(fd, buf, len); |
| 599 | |
| 600 | if (!io_multiplexing_in && remaining == 0) { |
| 601 | if (!buffer) { |
| 602 | bufferSz = 2 * IO_BUFFER_SIZE; |
| 603 | buffer = new_array(char, bufferSz); |
| 604 | if (!buffer) out_of_memory("read_unbuffered"); |
| 605 | } |
| 606 | remaining = read_timeout(fd, buffer, bufferSz); |
| 607 | bufferIdx = 0; |
| 608 | } |
| 609 | |
| 610 | while (ret == 0) { |
| 611 | if (remaining) { |
| 612 | len = MIN(len, remaining); |
| 613 | memcpy(buf, buffer + bufferIdx, len); |
| 614 | bufferIdx += len; |
| 615 | remaining -= len; |
| 616 | ret = len; |
| 617 | break; |
| 618 | } |
| 619 | |
| 620 | read_loop(fd, line, 4); |
| 621 | tag = IVAL(line, 0); |
| 622 | |
| 623 | remaining = tag & 0xFFFFFF; |
| 624 | tag = (tag >> 24) - MPLEX_BASE; |
| 625 | |
| 626 | switch (tag) { |
| 627 | case MSG_DATA: |
| 628 | if (!buffer || remaining > bufferSz) { |
| 629 | buffer = realloc_array(buffer, char, remaining); |
| 630 | if (!buffer) out_of_memory("read_unbuffered"); |
| 631 | bufferSz = remaining; |
| 632 | } |
| 633 | read_loop(fd, buffer, remaining); |
| 634 | bufferIdx = 0; |
| 635 | break; |
| 636 | case MSG_INFO: |
| 637 | case MSG_ERROR: |
| 638 | if (remaining >= sizeof line) { |
| 639 | rprintf(FERROR, "multiplexing overflow %d:%ld\n\n", |
| 640 | tag, (long)remaining); |
| 641 | exit_cleanup(RERR_STREAMIO); |
| 642 | } |
| 643 | read_loop(fd, line, remaining); |
| 644 | rwrite((enum logcode)tag, line, remaining); |
| 645 | remaining = 0; |
| 646 | break; |
| 647 | default: |
| 648 | rprintf(FERROR, "unexpected tag %d\n", tag); |
| 649 | exit_cleanup(RERR_STREAMIO); |
| 650 | } |
| 651 | } |
| 652 | |
| 653 | if (remaining == 0) |
| 654 | io_flush(NORMAL_FLUSH); |
| 655 | |
| 656 | return ret; |
| 657 | } |
| 658 | |
| 659 | |
| 660 | |
| 661 | /** |
| 662 | * Do a buffered read from @p fd. Don't return until all @p n bytes |
| 663 | * have been read. If all @p n can't be read then exit with an |
| 664 | * error. |
| 665 | **/ |
| 666 | static void readfd(int fd, char *buffer, size_t N) |
| 667 | { |
| 668 | int ret; |
| 669 | size_t total=0; |
| 670 | |
| 671 | while (total < N) { |
| 672 | ret = read_unbuffered(fd, buffer + total, N-total); |
| 673 | total += ret; |
| 674 | } |
| 675 | |
| 676 | stats.total_read += total; |
| 677 | } |
| 678 | |
| 679 | |
| 680 | int32 read_int(int f) |
| 681 | { |
| 682 | char b[4]; |
| 683 | int32 ret; |
| 684 | |
| 685 | readfd(f,b,4); |
| 686 | ret = IVAL(b,0); |
| 687 | if (ret == (int32)0xffffffff) return -1; |
| 688 | return ret; |
| 689 | } |
| 690 | |
| 691 | int64 read_longint(int f) |
| 692 | { |
| 693 | int64 ret; |
| 694 | char b[8]; |
| 695 | ret = read_int(f); |
| 696 | |
| 697 | if ((int32)ret != (int32)0xffffffff) { |
| 698 | return ret; |
| 699 | } |
| 700 | |
| 701 | #ifdef NO_INT64 |
| 702 | rprintf(FERROR,"Integer overflow - attempted 64 bit offset\n"); |
| 703 | exit_cleanup(RERR_UNSUPPORTED); |
| 704 | #else |
| 705 | readfd(f,b,8); |
| 706 | ret = IVAL(b,0) | (((int64)IVAL(b,4))<<32); |
| 707 | #endif |
| 708 | |
| 709 | return ret; |
| 710 | } |
| 711 | |
| 712 | void read_buf(int f,char *buf,size_t len) |
| 713 | { |
| 714 | readfd(f,buf,len); |
| 715 | } |
| 716 | |
| 717 | void read_sbuf(int f,char *buf,size_t len) |
| 718 | { |
| 719 | read_buf(f,buf,len); |
| 720 | buf[len] = 0; |
| 721 | } |
| 722 | |
| 723 | unsigned char read_byte(int f) |
| 724 | { |
| 725 | unsigned char c; |
| 726 | read_buf(f, (char *)&c, 1); |
| 727 | return c; |
| 728 | } |
| 729 | |
| 730 | |
| 731 | /** |
| 732 | * Sleep after writing to limit I/O bandwidth usage. |
| 733 | * |
| 734 | * @todo Rather than sleeping after each write, it might be better to |
| 735 | * use some kind of averaging. The current algorithm seems to always |
| 736 | * use a bit less bandwidth than specified, because it doesn't make up |
| 737 | * for slow periods. But arguably this is a feature. In addition, we |
| 738 | * ought to take the time used to write the data into account. |
| 739 | **/ |
| 740 | static void sleep_for_bwlimit(int bytes_written) |
| 741 | { |
| 742 | struct timeval tv; |
| 743 | |
| 744 | if (!bwlimit) |
| 745 | return; |
| 746 | |
| 747 | assert(bytes_written > 0); |
| 748 | assert(bwlimit > 0); |
| 749 | |
| 750 | tv.tv_usec = bytes_written * 1000 / bwlimit; |
| 751 | tv.tv_sec = tv.tv_usec / 1000000; |
| 752 | tv.tv_usec = tv.tv_usec % 1000000; |
| 753 | |
| 754 | select(0, NULL, NULL, NULL, &tv); |
| 755 | } |
| 756 | |
| 757 | |
| 758 | /** |
| 759 | * Write len bytes to the file descriptor @p fd. |
| 760 | * |
| 761 | * This function underlies the multiplexing system. The body of the |
| 762 | * application never calls this function directly. |
| 763 | **/ |
| 764 | static void writefd_unbuffered(int fd,char *buf,size_t len) |
| 765 | { |
| 766 | size_t total = 0; |
| 767 | fd_set w_fds, r_fds; |
| 768 | int fd_count, count; |
| 769 | struct timeval tv; |
| 770 | |
| 771 | msg_list_push(NORMAL_FLUSH); |
| 772 | |
| 773 | no_flush++; |
| 774 | |
| 775 | while (total < len) { |
| 776 | FD_ZERO(&w_fds); |
| 777 | FD_SET(fd,&w_fds); |
| 778 | fd_count = fd; |
| 779 | |
| 780 | if (msg_fd_in >= 0) { |
| 781 | FD_ZERO(&r_fds); |
| 782 | FD_SET(msg_fd_in,&r_fds); |
| 783 | if (msg_fd_in > fd_count) |
| 784 | fd_count = msg_fd_in; |
| 785 | } |
| 786 | |
| 787 | tv.tv_sec = io_timeout?io_timeout:SELECT_TIMEOUT; |
| 788 | tv.tv_usec = 0; |
| 789 | |
| 790 | errno = 0; |
| 791 | count = select(fd_count+1, msg_fd_in >= 0 ? &r_fds : NULL, |
| 792 | &w_fds, NULL, &tv); |
| 793 | |
| 794 | if (count == 0) { |
| 795 | msg_list_push(NORMAL_FLUSH); |
| 796 | check_timeout(); |
| 797 | } |
| 798 | |
| 799 | if (count <= 0) { |
| 800 | if (errno == EBADF) { |
| 801 | exit_cleanup(RERR_SOCKETIO); |
| 802 | } |
| 803 | continue; |
| 804 | } |
| 805 | |
| 806 | if (msg_fd_in >= 0 && FD_ISSET(msg_fd_in, &r_fds)) |
| 807 | read_msg_fd(); |
| 808 | |
| 809 | if (FD_ISSET(fd, &w_fds)) { |
| 810 | int ret; |
| 811 | size_t n = len-total; |
| 812 | ret = write(fd,buf+total,n); |
| 813 | |
| 814 | if (ret < 0) { |
| 815 | if (errno == EINTR) |
| 816 | continue; |
| 817 | if (errno == EWOULDBLOCK || errno == EAGAIN) { |
| 818 | msleep(1); |
| 819 | continue; |
| 820 | } |
| 821 | } |
| 822 | |
| 823 | if (ret <= 0) { |
| 824 | /* Don't try to write errors back |
| 825 | * across the stream */ |
| 826 | io_multiplexing_close(); |
| 827 | rprintf(FERROR, RSYNC_NAME |
| 828 | ": writefd_unbuffered failed to write %ld bytes: phase \"%s\": %s\n", |
| 829 | (long) len, io_write_phase, |
| 830 | strerror(errno)); |
| 831 | exit_cleanup(RERR_STREAMIO); |
| 832 | } |
| 833 | |
| 834 | sleep_for_bwlimit(ret); |
| 835 | |
| 836 | total += ret; |
| 837 | |
| 838 | if (io_timeout) |
| 839 | last_io = time(NULL); |
| 840 | } |
| 841 | } |
| 842 | |
| 843 | no_flush--; |
| 844 | } |
| 845 | |
| 846 | |
| 847 | static char *io_buffer; |
| 848 | static int io_buffer_count; |
| 849 | |
| 850 | void io_start_buffering_out(int fd) |
| 851 | { |
| 852 | if (io_buffer) return; |
| 853 | multiplex_out_fd = fd; |
| 854 | io_buffer = new_array(char, IO_BUFFER_SIZE); |
| 855 | if (!io_buffer) out_of_memory("writefd"); |
| 856 | io_buffer_count = 0; |
| 857 | } |
| 858 | |
| 859 | void io_start_buffering_in(int fd) |
| 860 | { |
| 861 | multiplex_in_fd = fd; |
| 862 | } |
| 863 | |
| 864 | /** |
| 865 | * Write an message to a multiplexed stream. If this fails then rsync |
| 866 | * exits. |
| 867 | **/ |
| 868 | static void mplex_write(int fd, enum msgcode code, char *buf, size_t len) |
| 869 | { |
| 870 | char buffer[4096]; |
| 871 | size_t n = len; |
| 872 | |
| 873 | SIVAL(buffer, 0, ((MPLEX_BASE + (int)code)<<24) + len); |
| 874 | |
| 875 | if (n > (sizeof buffer - 4)) { |
| 876 | n = sizeof buffer - 4; |
| 877 | } |
| 878 | |
| 879 | memcpy(&buffer[4], buf, n); |
| 880 | writefd_unbuffered(fd, buffer, n+4); |
| 881 | |
| 882 | len -= n; |
| 883 | buf += n; |
| 884 | |
| 885 | if (len) { |
| 886 | writefd_unbuffered(fd, buf, len); |
| 887 | } |
| 888 | } |
| 889 | |
| 890 | |
| 891 | void io_flush(int flush_it_all) |
| 892 | { |
| 893 | int fd = multiplex_out_fd; |
| 894 | |
| 895 | msg_list_push(flush_it_all); |
| 896 | |
| 897 | if (!io_buffer_count || no_flush) |
| 898 | return; |
| 899 | |
| 900 | if (io_multiplexing_out) |
| 901 | mplex_write(fd, MSG_DATA, io_buffer, io_buffer_count); |
| 902 | else |
| 903 | writefd_unbuffered(fd, io_buffer, io_buffer_count); |
| 904 | io_buffer_count = 0; |
| 905 | } |
| 906 | |
| 907 | |
| 908 | void io_end_buffering(void) |
| 909 | { |
| 910 | io_flush(NORMAL_FLUSH); |
| 911 | if (!io_multiplexing_out) { |
| 912 | free(io_buffer); |
| 913 | io_buffer = NULL; |
| 914 | } |
| 915 | } |
| 916 | |
| 917 | static void writefd(int fd,char *buf,size_t len) |
| 918 | { |
| 919 | stats.total_written += len; |
| 920 | |
| 921 | msg_list_push(NORMAL_FLUSH); |
| 922 | |
| 923 | if (!io_buffer || fd != multiplex_out_fd) { |
| 924 | writefd_unbuffered(fd, buf, len); |
| 925 | return; |
| 926 | } |
| 927 | |
| 928 | while (len) { |
| 929 | int n = MIN((int) len, IO_BUFFER_SIZE-io_buffer_count); |
| 930 | if (n > 0) { |
| 931 | memcpy(io_buffer+io_buffer_count, buf, n); |
| 932 | buf += n; |
| 933 | len -= n; |
| 934 | io_buffer_count += n; |
| 935 | } |
| 936 | |
| 937 | if (io_buffer_count == IO_BUFFER_SIZE) |
| 938 | io_flush(NORMAL_FLUSH); |
| 939 | } |
| 940 | } |
| 941 | |
| 942 | |
| 943 | void write_int(int f,int32 x) |
| 944 | { |
| 945 | char b[4]; |
| 946 | SIVAL(b,0,x); |
| 947 | writefd(f,b,4); |
| 948 | } |
| 949 | |
| 950 | |
| 951 | void write_int_named(int f, int32 x, const char *phase) |
| 952 | { |
| 953 | io_write_phase = phase; |
| 954 | write_int(f, x); |
| 955 | io_write_phase = phase_unknown; |
| 956 | } |
| 957 | |
| 958 | |
| 959 | /* |
| 960 | * Note: int64 may actually be a 32-bit type if ./configure couldn't find any |
| 961 | * 64-bit types on this platform. |
| 962 | */ |
| 963 | void write_longint(int f, int64 x) |
| 964 | { |
| 965 | char b[8]; |
| 966 | |
| 967 | if (x <= 0x7FFFFFFF) { |
| 968 | write_int(f, (int)x); |
| 969 | return; |
| 970 | } |
| 971 | |
| 972 | #ifdef NO_INT64 |
| 973 | rprintf(FERROR,"Integer overflow - attempted 64 bit offset\n"); |
| 974 | exit_cleanup(RERR_UNSUPPORTED); |
| 975 | #else |
| 976 | write_int(f, (int32)0xFFFFFFFF); |
| 977 | SIVAL(b,0,(x&0xFFFFFFFF)); |
| 978 | SIVAL(b,4,((x>>32)&0xFFFFFFFF)); |
| 979 | |
| 980 | writefd(f,b,8); |
| 981 | #endif |
| 982 | } |
| 983 | |
| 984 | void write_buf(int f,char *buf,size_t len) |
| 985 | { |
| 986 | writefd(f,buf,len); |
| 987 | } |
| 988 | |
| 989 | /** Write a string to the connection */ |
| 990 | static void write_sbuf(int f,char *buf) |
| 991 | { |
| 992 | write_buf(f, buf, strlen(buf)); |
| 993 | } |
| 994 | |
| 995 | |
| 996 | void write_byte(int f,unsigned char c) |
| 997 | { |
| 998 | write_buf(f,(char *)&c,1); |
| 999 | } |
| 1000 | |
| 1001 | |
| 1002 | |
| 1003 | /** |
| 1004 | * Read a line of up to @p maxlen characters into @p buf. Does not |
| 1005 | * contain a trailing newline or carriage return. |
| 1006 | * |
| 1007 | * @return 1 for success; 0 for io error or truncation. |
| 1008 | **/ |
| 1009 | int read_line(int f, char *buf, size_t maxlen) |
| 1010 | { |
| 1011 | while (maxlen) { |
| 1012 | buf[0] = 0; |
| 1013 | read_buf(f, buf, 1); |
| 1014 | if (buf[0] == 0) |
| 1015 | return 0; |
| 1016 | if (buf[0] == '\n') { |
| 1017 | buf[0] = 0; |
| 1018 | break; |
| 1019 | } |
| 1020 | if (buf[0] != '\r') { |
| 1021 | buf++; |
| 1022 | maxlen--; |
| 1023 | } |
| 1024 | } |
| 1025 | if (maxlen == 0) { |
| 1026 | *buf = 0; |
| 1027 | return 0; |
| 1028 | } |
| 1029 | |
| 1030 | return 1; |
| 1031 | } |
| 1032 | |
| 1033 | |
| 1034 | void io_printf(int fd, const char *format, ...) |
| 1035 | { |
| 1036 | va_list ap; |
| 1037 | char buf[1024]; |
| 1038 | int len; |
| 1039 | |
| 1040 | va_start(ap, format); |
| 1041 | len = vsnprintf(buf, sizeof buf, format, ap); |
| 1042 | va_end(ap); |
| 1043 | |
| 1044 | if (len < 0) exit_cleanup(RERR_STREAMIO); |
| 1045 | |
| 1046 | write_sbuf(fd, buf); |
| 1047 | } |
| 1048 | |
| 1049 | |
| 1050 | /** Setup for multiplexing a MSG_* stream with the data stream. */ |
| 1051 | void io_start_multiplex_out(int fd) |
| 1052 | { |
| 1053 | multiplex_out_fd = fd; |
| 1054 | io_flush(NORMAL_FLUSH); |
| 1055 | io_start_buffering_out(fd); |
| 1056 | io_multiplexing_out = 1; |
| 1057 | } |
| 1058 | |
| 1059 | /** Setup for multiplexing a MSG_* stream with the data stream. */ |
| 1060 | void io_start_multiplex_in(int fd) |
| 1061 | { |
| 1062 | multiplex_in_fd = fd; |
| 1063 | io_flush(NORMAL_FLUSH); |
| 1064 | io_multiplexing_in = 1; |
| 1065 | } |
| 1066 | |
| 1067 | /** Write an message to the multiplexed data stream. */ |
| 1068 | int io_multiplex_write(enum msgcode code, char *buf, size_t len) |
| 1069 | { |
| 1070 | if (!io_multiplexing_out) return 0; |
| 1071 | |
| 1072 | io_flush(NORMAL_FLUSH); |
| 1073 | stats.total_written += (len+4); |
| 1074 | mplex_write(multiplex_out_fd, code, buf, len); |
| 1075 | return 1; |
| 1076 | } |
| 1077 | |
| 1078 | /** Stop output multiplexing. */ |
| 1079 | void io_multiplexing_close(void) |
| 1080 | { |
| 1081 | io_multiplexing_out = 0; |
| 1082 | } |
| 1083 | |