1/* 2 * The startup routines, including main(), for rsync. 3 * 4 * Copyright (C) 1996-2001 Andrew Tridgell <tridge@samba.org> 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 8 * 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. 13 * 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. 18 * 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. 22 */ 23 24#include "rsync.h" 25#if defined CONFIG_LOCALE && defined HAVE_LOCALE_H 26#include <locale.h> 27#endif 28 29extern int verbose; 30extern int dry_run; 31extern int list_only; 32extern int am_root; 33extern int am_server; 34extern int am_sender; 35extern int am_generator; 36extern int am_daemon; 37extern int blocking_io; 38extern int remove_source_files; 39extern int daemon_over_rsh; 40extern int need_messages_from_generator; 41extern int kluge_around_eof; 42extern int do_stats; 43extern int log_got_error; 44extern int module_id; 45extern int copy_links; 46extern int copy_dirlinks; 47extern int keep_dirlinks; 48extern int preserve_hard_links; 49extern int protocol_version; 50extern int recurse; 51extern int relative_paths; 52extern int sanitize_paths; 53extern int curr_dir_depth; 54extern int curr_dir_len; 55extern int module_id; 56extern int rsync_port; 57extern int whole_file; 58extern int read_batch; 59extern int write_batch; 60extern int batch_fd; 61extern int batch_gen_fd; 62extern int filesfrom_fd; 63#ifdef EA_SUPPORT 64extern int extended_attributes; 65#endif 66extern pid_t cleanup_child_pid; 67extern struct stats stats; 68extern char *filesfrom_host; 69extern char *partial_dir; 70extern char *basis_dir[]; 71extern char *rsync_path; 72extern char *shell_cmd; 73extern char *batch_name; 74extern char curr_dir[MAXPATHLEN]; 75extern struct filter_list_struct server_filter_list; 76 77int local_server = 0; 78int new_root_dir = 0; 79mode_t orig_umask = 0; 80struct file_list *the_file_list; 81 82/* There's probably never more than at most 2 outstanding child processes, 83 * but set it higher, just in case. */ 84#define MAXCHILDPROCS 7 85 86#ifdef HAVE_SIGACTION 87# ifdef HAVE_SIGPROCMASK 88# define SIGACTMASK(n,h) SIGACTION(n,h), sigaddset(&sigmask,(n)) 89# else 90# define SIGACTMASK(n,h) SIGACTION(n,h) 91# endif 92static struct sigaction sigact; 93#endif 94 95struct pid_status { 96 pid_t pid; 97 int status; 98} pid_stat_table[MAXCHILDPROCS]; 99 100static time_t starttime, endtime; 101static int64 total_read, total_written; 102 103static void show_malloc_stats(void); 104 105/* Works like waitpid(), but if we already harvested the child pid in our 106 * remember_children(), we succeed instead of returning an error. */ 107pid_t wait_process(pid_t pid, int *status_ptr, int flags) 108{ 109 pid_t waited_pid = waitpid(pid, status_ptr, flags); 110 111 if (waited_pid == -1 && errno == ECHILD) { 112 /* Status of requested child no longer available: check to 113 * see if it was processed by remember_children(). */ 114 int cnt; 115 for (cnt = 0; cnt < MAXCHILDPROCS; cnt++) { 116 if (pid == pid_stat_table[cnt].pid) { 117 *status_ptr = pid_stat_table[cnt].status; 118 pid_stat_table[cnt].pid = 0; 119 return pid; 120 } 121 } 122 } 123 124 return waited_pid; 125} 126 127/* Wait for a process to exit, calling io_flush while waiting. */ 128static void wait_process_with_flush(pid_t pid, int *exit_code_ptr) 129{ 130 pid_t waited_pid; 131 int status; 132 133 while ((waited_pid = wait_process(pid, &status, WNOHANG)) == 0) { 134 msleep(20); 135 io_flush(FULL_FLUSH); 136 } 137 138 /* TODO: If the child exited on a signal, then log an 139 * appropriate error message. Perhaps we should also accept a 140 * message describing the purpose of the child. Also indicate 141 * this to the caller so that they know something went wrong. */ 142 if (waited_pid < 0) { 143 rsyserr(FERROR, errno, "waitpid"); 144 *exit_code_ptr = RERR_WAITCHILD; 145 } else if (!WIFEXITED(status)) { 146#ifdef WCOREDUMP 147 if (WCOREDUMP(status)) 148 *exit_code_ptr = RERR_CRASHED; 149 else 150#endif 151 if (WIFSIGNALED(status)) 152 *exit_code_ptr = RERR_TERMINATED; 153 else 154 *exit_code_ptr = RERR_WAITCHILD; 155 } else 156 *exit_code_ptr = WEXITSTATUS(status); 157} 158 159/* This function gets called from all 3 processes. We want the client side 160 * to actually output the text, but the sender is the only process that has 161 * all the stats we need. So, if we're a client sender, we do the report. 162 * If we're a server sender, we write the stats on the supplied fd. If 163 * we're the client receiver we read the stats from the supplied fd and do 164 * the report. All processes might also generate a set of debug stats, if 165 * the verbose level is high enough (this is the only thing that the 166 * generator process and the server receiver ever do here). */ 167static void handle_stats(int f) 168{ 169 endtime = time(NULL); 170 171 /* Cache two stats because the read/write code can change it. */ 172 total_read = stats.total_read; 173 total_written = stats.total_written; 174 175 if (do_stats && verbose > 1) { 176 /* These come out from every process */ 177 show_malloc_stats(); 178 show_flist_stats(); 179 } 180 181 if (am_generator) 182 return; 183 184 if (am_daemon) { 185 if (f == -1 || !am_sender) 186 return; 187 } 188 189 if (am_server) { 190 if (am_sender) { 191 write_longint(f, total_read); 192 write_longint(f, total_written); 193 write_longint(f, stats.total_size); 194 if (protocol_version >= 29) { 195 write_longint(f, stats.flist_buildtime); 196 write_longint(f, stats.flist_xfertime); 197 } 198 } 199 return; 200 } 201 202 /* this is the client */ 203 204 if (f < 0 && !am_sender) /* e.g. when we got an empty file list. */ 205 ; 206 else if (!am_sender) { 207 /* Read the first two in opposite order because the meaning of 208 * read/write swaps when switching from sender to receiver. */ 209 total_written = read_longint(f); 210 total_read = read_longint(f); 211 stats.total_size = read_longint(f); 212 if (protocol_version >= 29) { 213 stats.flist_buildtime = read_longint(f); 214 stats.flist_xfertime = read_longint(f); 215 } 216 } else if (write_batch) { 217 /* The --read-batch process is going to be a client 218 * receiver, so we need to give it the stats. */ 219 write_longint(batch_fd, total_read); 220 write_longint(batch_fd, total_written); 221 write_longint(batch_fd, stats.total_size); 222 if (protocol_version >= 29) { 223 write_longint(batch_fd, stats.flist_buildtime); 224 write_longint(batch_fd, stats.flist_xfertime); 225 } 226 } 227} 228 229static void output_summary(void) 230{ 231 if (do_stats) { 232 rprintf(FCLIENT, "\n"); 233 rprintf(FINFO,"Number of files: %d\n", stats.num_files); 234 rprintf(FINFO,"Number of files transferred: %d\n", 235 stats.num_transferred_files); 236 rprintf(FINFO,"Total file size: %s bytes\n", 237 human_num(stats.total_size)); 238 rprintf(FINFO,"Total transferred file size: %s bytes\n", 239 human_num(stats.total_transferred_size)); 240 rprintf(FINFO,"Literal data: %s bytes\n", 241 human_num(stats.literal_data)); 242 rprintf(FINFO,"Matched data: %s bytes\n", 243 human_num(stats.matched_data)); 244 rprintf(FINFO,"File list size: %d\n", stats.flist_size); 245 if (stats.flist_buildtime) { 246 rprintf(FINFO, 247 "File list generation time: %.3f seconds\n", 248 (double)stats.flist_buildtime / 1000); 249 rprintf(FINFO, 250 "File list transfer time: %.3f seconds\n", 251 (double)stats.flist_xfertime / 1000); 252 } 253 rprintf(FINFO,"Total bytes sent: %s\n", 254 human_num(total_written)); 255 rprintf(FINFO,"Total bytes received: %s\n", 256 human_num(total_read)); 257 } 258 259 if (verbose || do_stats) { 260 rprintf(FCLIENT, "\n"); 261 rprintf(FINFO, 262 "sent %s bytes received %s bytes %s bytes/sec\n", 263 human_num(total_written), human_num(total_read), 264 human_dnum((total_written + total_read)/(0.5 + (endtime - starttime)), 2)); 265 rprintf(FINFO, "total size is %s speedup is %.2f\n", 266 human_num(stats.total_size), 267 (double)stats.total_size / (total_written+total_read)); 268 } 269 270 fflush(stdout); 271 fflush(stderr); 272} 273 274 275/** 276 * If our C library can get malloc statistics, then show them to FINFO 277 **/ 278static void show_malloc_stats(void) 279{ 280#ifdef HAVE_MALLINFO 281 struct mallinfo mi; 282 283 mi = mallinfo(); 284 285 rprintf(FCLIENT, "\n"); 286 rprintf(FINFO, RSYNC_NAME "[%d] (%s%s%s) heap statistics:\n", 287 getpid(), am_server ? "server " : "", 288 am_daemon ? "daemon " : "", who_am_i()); 289 rprintf(FINFO, " arena: %10ld (bytes from sbrk)\n", 290 (long)mi.arena); 291 rprintf(FINFO, " ordblks: %10ld (chunks not in use)\n", 292 (long)mi.ordblks); 293 rprintf(FINFO, " smblks: %10ld\n", 294 (long)mi.smblks); 295 rprintf(FINFO, " hblks: %10ld (chunks from mmap)\n", 296 (long)mi.hblks); 297 rprintf(FINFO, " hblkhd: %10ld (bytes from mmap)\n", 298 (long)mi.hblkhd); 299 rprintf(FINFO, " allmem: %10ld (bytes from sbrk + mmap)\n", 300 (long)mi.arena + mi.hblkhd); 301 rprintf(FINFO, " usmblks: %10ld\n", 302 (long)mi.usmblks); 303 rprintf(FINFO, " fsmblks: %10ld\n", 304 (long)mi.fsmblks); 305 rprintf(FINFO, " uordblks: %10ld (bytes used)\n", 306 (long)mi.uordblks); 307 rprintf(FINFO, " fordblks: %10ld (bytes free)\n", 308 (long)mi.fordblks); 309 rprintf(FINFO, " keepcost: %10ld (bytes in releasable chunk)\n", 310 (long)mi.keepcost); 311#endif /* HAVE_MALLINFO */ 312} 313 314 315/* Start the remote shell. cmd may be NULL to use the default. */ 316static pid_t do_cmd(char *cmd, char *machine, char *user, char *path, 317 int *f_in, int *f_out) 318{ 319 int i, argc = 0; 320 char *args[MAX_ARGS]; 321 pid_t ret; 322 char *dir = NULL; 323 int dash_l_set = 0; 324 325 if (!read_batch && !local_server) { 326 char *t, *f, in_quote = '\0'; 327 char *rsh_env = getenv(RSYNC_RSH_ENV); 328 if (!cmd) 329 cmd = rsh_env; 330 if (!cmd) 331 cmd = RSYNC_RSH; 332 cmd = strdup(cmd); 333 if (!cmd) 334 goto oom; 335 336 for (t = f = cmd; *f; f++) { 337 if (*f == ' ') 338 continue; 339 /* Comparison leaves rooms for server_options(). */ 340 if (argc >= MAX_ARGS - MAX_SERVER_ARGS) { 341 rprintf(FERROR, "internal: args[] overflowed in do_cmd()\n"); 342 exit_cleanup(RERR_SYNTAX); 343 } 344 args[argc++] = t; 345 while (*f != ' ' || in_quote) { 346 if (!*f) { 347 if (in_quote) { 348 rprintf(FERROR, 349 "Missing trailing-%c in remote-shell command.\n", 350 in_quote); 351 exit_cleanup(RERR_SYNTAX); 352 } 353 f--; 354 break; 355 } 356 if (*f == '\'' || *f == '"') { 357 if (!in_quote) { 358 in_quote = *f++; 359 continue; 360 } 361 if (*f == in_quote && *++f != in_quote) { 362 in_quote = '\0'; 363 continue; 364 } 365 } 366 *t++ = *f++; 367 } 368 *t++ = '\0'; 369 } 370 371 /* check to see if we've already been given '-l user' in 372 * the remote-shell command */ 373 for (i = 0; i < argc-1; i++) { 374 if (!strcmp(args[i], "-l") && args[i+1][0] != '-') 375 dash_l_set = 1; 376 } 377 378#ifdef HAVE_REMSH 379 /* remsh (on HPUX) takes the arguments the other way around */ 380 args[argc++] = machine; 381 if (user && !(daemon_over_rsh && dash_l_set)) { 382 args[argc++] = "-l"; 383 args[argc++] = user; 384 } 385#else 386 if (user && !(daemon_over_rsh && dash_l_set)) { 387 args[argc++] = "-l"; 388 args[argc++] = user; 389 } 390 args[argc++] = machine; 391#endif 392 393 args[argc++] = rsync_path; 394 395 if (blocking_io < 0) { 396 char *cp; 397 if ((cp = strrchr(cmd, '/')) != NULL) 398 cp++; 399 else 400 cp = cmd; 401 if (strcmp(cp, "rsh") == 0 || strcmp(cp, "remsh") == 0) 402 blocking_io = 1; 403 } 404 405 server_options(args,&argc); 406 407 if (argc >= MAX_ARGS - 2) { 408 rprintf(FERROR, "internal: args[] overflowed in do_cmd()\n"); 409 exit_cleanup(RERR_SYNTAX); 410 } 411 } 412 413 args[argc++] = "."; 414 415 if (!daemon_over_rsh && path && *path) 416 args[argc++] = path; 417 418 args[argc] = NULL; 419 420 if (verbose > 3) { 421 for (i = 0; i < argc; i++) 422 rprintf(FCLIENT, "cmd[%d]=%s ", i, args[i]); 423 rprintf(FCLIENT, "\n"); 424 } 425 426 if (read_batch) { 427 int from_gen_pipe[2]; 428 if (fd_pair(from_gen_pipe) < 0) { 429 rsyserr(FERROR, errno, "pipe"); 430 exit_cleanup(RERR_IPC); 431 } 432 batch_gen_fd = from_gen_pipe[0]; 433 *f_out = from_gen_pipe[1]; 434 *f_in = batch_fd; 435 ret = -1; /* no child pid */ 436 } else if (local_server) { 437 /* If the user didn't request --[no-]whole-file, force 438 * it on, but only if we're not batch processing. */ 439 if (whole_file < 0 && !write_batch) 440 whole_file = 1; 441 ret = local_child(argc, args, f_in, f_out, child_main); 442 } else 443 ret = piped_child(args,f_in,f_out); 444 445 if (dir) 446 free(dir); 447 448 return ret; 449 450 oom: 451 out_of_memory("do_cmd"); 452 return 0; /* not reached */ 453} 454 455/* The receiving side operates in one of two modes: 456 * 457 * 1. it receives any number of files into a destination directory, 458 * placing them according to their names in the file-list. 459 * 460 * 2. it receives a single file and saves it using the name in the 461 * destination path instead of its file-list name. This requires a 462 * "local name" for writing out the destination file. 463 * 464 * So, our task is to figure out what mode/local-name we need. 465 * For mode 1, we change into the destination directory and return NULL. 466 * For mode 2, we change into the directory containing the destination 467 * file (if we aren't already there) and return the local-name. */ 468static char *get_local_name(struct file_list *flist, char *dest_path) 469{ 470 STRUCT_STAT st; 471 int statret; 472 char *cp; 473 474 if (verbose > 2) { 475 rprintf(FINFO, "get_local_name count=%d %s\n", 476 flist->count, NS(dest_path)); 477 } 478 479 if (!dest_path || list_only) 480 return NULL; 481 482 /* See what currently exists at the destination. */ 483 if ((statret = do_stat(dest_path, &st)) == 0) { 484 /* If the destination is a dir, enter it and use mode 1. */ 485 if (S_ISDIR(st.st_mode)) { 486 if (!push_dir(dest_path, 0)) { 487 rsyserr(FERROR, errno, "push_dir#1 %s failed", 488 full_fname(dest_path)); 489 exit_cleanup(RERR_FILESELECT); 490 } 491 return NULL; 492 } 493#ifdef EA_SUPPORT 494 if (extended_attributes) { 495 if(flist->count == 2 && !strncmp(flist->files[0]->basename, "._", 2)) 496 return dest_path; 497 } 498#endif 499 if (flist->count > 1) { 500 rprintf(FERROR, 501 "ERROR: destination must be a directory when" 502 " copying more than 1 file\n"); 503 exit_cleanup(RERR_FILESELECT); 504 } 505 /* Caution: flist->count could be 0! */ 506 if (flist->count == 1 && S_ISDIR(flist->files[0]->mode)) { 507 rprintf(FERROR, 508 "ERROR: cannot overwrite non-directory" 509 " with a directory\n"); 510 exit_cleanup(RERR_FILESELECT); 511 } 512 } else if (errno != ENOENT) { 513 /* If we don't know what's at the destination, fail. */ 514 rsyserr(FERROR, errno, "ERROR: cannot stat destination %s", 515 full_fname(dest_path)); 516 exit_cleanup(RERR_FILESELECT); 517 } 518#ifdef EA_SUPPORT 519 if (extended_attributes) { 520 if(flist->count == 2 && !strncmp(flist->files[0]->basename, "._", 2)) 521 return dest_path; 522 } 523#endif 524 525 cp = strrchr(dest_path, '/'); 526 527 /* If we need a destination directory because the transfer is not 528 * of a single non-directory or the user has requested one via a 529 * destination path ending in a slash, create one and use mode 1. */ 530 if (flist->count > 1 || (cp && !cp[1])) { 531 /* Lop off the final slash (if any). */ 532 if (cp && !cp[1]) 533 *cp = '\0'; 534 535 if (statret == 0) { 536 rprintf(FERROR, 537 "ERROR: destination path is not a directory\n"); 538 exit_cleanup(RERR_SYNTAX); 539 } 540 541 if (mkdir_defmode(dest_path) != 0) { 542 rsyserr(FERROR, errno, "mkdir %s failed", 543 full_fname(dest_path)); 544 exit_cleanup(RERR_FILEIO); 545 } 546 547 new_root_dir = 1; 548 549 if (verbose) 550 rprintf(FINFO, "created directory %s\n", dest_path); 551 552 if (dry_run) { 553 /* Indicate that dest dir doesn't really exist. */ 554 dry_run++; 555 } 556 557 if (!push_dir(dest_path, dry_run > 1)) { 558 rsyserr(FERROR, errno, "push_dir#2 %s failed", 559 full_fname(dest_path)); 560 exit_cleanup(RERR_FILESELECT); 561 } 562 563 return NULL; 564 } 565 566 /* Otherwise, we are writing a single file, possibly on top of an 567 * existing non-directory. Change to the item's parent directory 568 * (if it has a path component), return the basename of the 569 * destination file as the local name, and use mode 2. */ 570 if (!cp) 571 return dest_path; 572 573 if (cp == dest_path) 574 dest_path = "/"; 575 576 *cp = '\0'; 577 if (!push_dir(dest_path, 0)) { 578 rsyserr(FERROR, errno, "push_dir#3 %s failed", 579 full_fname(dest_path)); 580 exit_cleanup(RERR_FILESELECT); 581 } 582 *cp = '/'; 583 584 return cp + 1; 585} 586 587/* Call this if the destination dir (which is assumed to be in curr_dir) 588 * does not yet exist and we can't create it due to being in dry-run 589 * mode. We'll fix dirs that can be relative to the non-existent dir. */ 590static void fix_basis_dirs(void) 591{ 592 char **dir, *new, *slash; 593 int len; 594 595 if (dry_run <= 1) 596 return; 597 598 slash = strrchr(curr_dir, '/'); 599 600 for (dir = basis_dir; *dir; dir++) { 601 if (**dir == '/') 602 continue; 603 len = curr_dir_len + 1 + strlen(*dir) + 1; 604 if (!(new = new_array(char, len))) 605 out_of_memory("fix_basis_dirs"); 606 if (slash && strncmp(*dir, "../", 3) == 0) { 607 /* We want to remove only one leading "../" prefix for 608 * the directory we couldn't create in dry-run mode: 609 * this ensures that any other ".." references get 610 * evaluated the same as they would for a live copy. */ 611 *slash = '\0'; 612 pathjoin(new, len, curr_dir, *dir + 3); 613 *slash = '/'; 614 } else 615 pathjoin(new, len, curr_dir, *dir); 616 *dir = new; 617 } 618} 619 620/* This is only called by the sender. */ 621static void read_final_goodbye(int f_in, int f_out) 622{ 623 int i; 624 625 if (protocol_version < 29) 626 i = read_int(f_in); 627 else { 628 while ((i = read_int(f_in)) == the_file_list->count 629 && read_shortint(f_in) == ITEM_IS_NEW) { 630 /* Forward the keep-alive (no-op) to the receiver. */ 631 write_int(f_out, the_file_list->count); 632 write_shortint(f_out, ITEM_IS_NEW); 633 } 634 } 635 636 if (i != -1) { 637 rprintf(FERROR, "Invalid packet at end of run (%d) [%s]\n", 638 i, who_am_i()); 639 exit_cleanup(RERR_PROTOCOL); 640 } 641} 642 643 644static void do_server_sender(int f_in, int f_out, int argc, char *argv[]) 645{ 646 struct file_list *flist; 647 char *dir = argv[0]; 648 649 if (verbose > 2) { 650 rprintf(FINFO, "server_sender starting pid=%ld\n", 651 (long)getpid()); 652 } 653 654 if (am_daemon && lp_write_only(module_id)) { 655 rprintf(FERROR, "ERROR: module is write only\n"); 656 exit_cleanup(RERR_SYNTAX); 657 return; 658 } 659 if (am_daemon && lp_read_only(module_id) && remove_source_files) { 660 rprintf(FERROR, 661 "ERROR: --remove-%s-files cannot be used with a read-only module\n", 662 remove_source_files == 1 ? "source" : "sent"); 663 exit_cleanup(RERR_SYNTAX); 664 return; 665 } 666 667 if (!relative_paths) { 668 if (!push_dir(dir, 0)) { 669 rsyserr(FERROR, errno, "push_dir#3 %s failed", 670 full_fname(dir)); 671 exit_cleanup(RERR_FILESELECT); 672 } 673 } 674 argc--; 675 argv++; 676 677 if (argc == 0 && (recurse || list_only)) { 678 argc = 1; 679 argv--; 680 argv[0] = "."; 681 } 682 683 flist = send_file_list(f_out,argc,argv); 684 if (!flist || flist->count == 0) { 685 exit_cleanup(0); 686 } 687 the_file_list = flist; 688 689 io_start_buffering_in(); 690 io_start_buffering_out(); 691 692 send_files(flist,f_out,f_in); 693 io_flush(FULL_FLUSH); 694 handle_stats(f_out); 695 if (protocol_version >= 24) 696 read_final_goodbye(f_in, f_out); 697 io_flush(FULL_FLUSH); 698 exit_cleanup(0); 699} 700 701 702static int do_recv(int f_in,int f_out,struct file_list *flist,char *local_name) 703{ 704 int pid; 705 int exit_code = 0; 706 int error_pipe[2]; 707 708 /* The receiving side mustn't obey this, or an existing symlink that 709 * points to an identical file won't be replaced by the referent. */ 710 copy_links = copy_dirlinks = 0; 711 712 if (preserve_hard_links) 713 init_hard_links(); 714 715 if (fd_pair(error_pipe) < 0) { 716 rsyserr(FERROR, errno, "pipe failed in do_recv"); 717 exit_cleanup(RERR_IPC); 718 } 719 720 io_flush(NORMAL_FLUSH); 721 722 if ((pid = do_fork()) == -1) { 723 rsyserr(FERROR, errno, "fork failed in do_recv"); 724 exit_cleanup(RERR_IPC); 725 } 726 727 if (pid == 0) { 728 close(error_pipe[0]); 729 if (f_in != f_out) 730 close(f_out); 731 732 /* we can't let two processes write to the socket at one time */ 733 close_multiplexing_out(); 734 735 /* set place to send errors */ 736 set_msg_fd_out(error_pipe[1]); 737 738 recv_files(f_in, flist, local_name); 739 io_flush(FULL_FLUSH); 740 handle_stats(f_in); 741 742 send_msg(MSG_DONE, "", 0); 743 io_flush(FULL_FLUSH); 744 745 /* Handle any keep-alive packets from the post-processing work 746 * that the generator does. */ 747 if (protocol_version >= 29) { 748 kluge_around_eof = -1; 749 750 /* This should only get stopped via a USR2 signal. */ 751 while (read_int(f_in) == flist->count 752 && read_shortint(f_in) == ITEM_IS_NEW) {} 753 754 rprintf(FERROR, "Invalid packet at end of run [%s]\n", 755 who_am_i()); 756 exit_cleanup(RERR_PROTOCOL); 757 } 758 759 /* Finally, we go to sleep until our parent kills us with a 760 * USR2 signal. We sleep for a short time, as on some OSes 761 * a signal won't interrupt a sleep! */ 762 while (1) 763 msleep(20); 764 } 765 766 am_generator = 1; 767 close_multiplexing_in(); 768 if (write_batch && !am_server) 769 stop_write_batch(); 770 771 close(error_pipe[1]); 772 if (f_in != f_out) 773 close(f_in); 774 775 io_start_buffering_out(); 776 777 set_msg_fd_in(error_pipe[0]); 778 779 generate_files(f_out, flist, local_name); 780 781 handle_stats(-1); 782 io_flush(FULL_FLUSH); 783 if (protocol_version >= 24) { 784 /* send a final goodbye message */ 785 write_int(f_out, -1); 786 } 787 io_flush(FULL_FLUSH); 788 789 set_msg_fd_in(-1); 790 kill(pid, SIGUSR2); 791 wait_process_with_flush(pid, &exit_code); 792 return exit_code; 793} 794 795 796static void do_server_recv(int f_in, int f_out, int argc,char *argv[]) 797{ 798 int exit_code; 799 struct file_list *flist; 800 char *local_name = NULL; 801 char *dir = NULL; 802 int save_verbose = verbose; 803 804 if (filesfrom_fd >= 0) { 805 /* We can't mix messages with files-from data on the socket, 806 * so temporarily turn off verbose messages. */ 807 verbose = 0; 808 } 809 810 if (verbose > 2) { 811 rprintf(FINFO, "server_recv(%d) starting pid=%ld\n", 812 argc, (long)getpid()); 813 } 814 815 if (am_daemon && lp_read_only(module_id)) { 816 rprintf(FERROR,"ERROR: module is read only\n"); 817 exit_cleanup(RERR_SYNTAX); 818 return; 819 } 820 821 if (argc > 0) { 822 dir = argv[0]; 823 argc--; 824 argv++; 825 if (!am_daemon && !push_dir(dir, 0)) { 826 rsyserr(FERROR, errno, "push_dir#4 %s failed", 827 full_fname(dir)); 828 exit_cleanup(RERR_FILESELECT); 829 } 830 } 831 832 io_start_buffering_in(); 833 recv_filter_list(f_in); 834 835 if (filesfrom_fd >= 0) { 836 /* We need to send the files-from names to the sender at the 837 * same time that we receive the file-list from them, so we 838 * need the IO routines to automatically write out the names 839 * onto our f_out socket as we read the file-list. This 840 * avoids both deadlock and extra delays/buffers. */ 841 io_set_filesfrom_fds(filesfrom_fd, f_out); 842 filesfrom_fd = -1; 843 } 844 845 flist = recv_file_list(f_in); 846 verbose = save_verbose; 847 if (!flist) { 848 rprintf(FERROR,"server_recv: recv_file_list error\n"); 849 exit_cleanup(RERR_FILESELECT); 850 } 851 the_file_list = flist; 852 853 if (argc > 0) 854 local_name = get_local_name(flist,argv[0]); 855 856 /* Now that we know what our destination directory turned out to be, 857 * we can sanitize the --link-/copy-/compare-dest args correctly. */ 858 if (sanitize_paths) { 859 char **dir; 860 for (dir = basis_dir; *dir; dir++) { 861 *dir = sanitize_path(NULL, *dir, NULL, curr_dir_depth, NULL); 862 } 863 if (partial_dir) { 864 partial_dir = sanitize_path(NULL, partial_dir, NULL, curr_dir_depth, NULL); 865 } 866 } 867 fix_basis_dirs(); 868 869 if (server_filter_list.head) { 870 char **dir; 871 struct filter_list_struct *elp = &server_filter_list; 872 873 for (dir = basis_dir; *dir; dir++) { 874 if (check_filter(elp, *dir, 1) < 0) 875 goto options_rejected; 876 } 877 if (partial_dir && *partial_dir == '/' 878 && check_filter(elp, partial_dir, 1) < 0) { 879 options_rejected: 880 rprintf(FERROR, 881 "Your options have been rejected by the server.\n"); 882 exit_cleanup(RERR_SYNTAX); 883 } 884 } 885 886 exit_code = do_recv(f_in,f_out,flist,local_name); 887 exit_cleanup(exit_code); 888} 889 890 891int child_main(int argc, char *argv[]) 892{ 893 start_server(STDIN_FILENO, STDOUT_FILENO, argc, argv); 894 return 0; 895} 896 897 898void start_server(int f_in, int f_out, int argc, char *argv[]) 899{ 900 set_nonblocking(f_in); 901 set_nonblocking(f_out); 902 903 io_set_sock_fds(f_in, f_out); 904 setup_protocol(f_out, f_in); 905#if defined HAVE_ICONV_OPEN && defined HAVE_ICONV_H 906 setup_iconv(); 907#endif 908 909 if (protocol_version >= 23) 910 io_start_multiplex_out(); 911 912 if (am_sender) { 913 keep_dirlinks = 0; /* Must be disabled on the sender. */ 914 if (need_messages_from_generator) 915 io_start_multiplex_in(); 916 917 recv_filter_list(f_in); 918 do_server_sender(f_in, f_out, argc, argv); 919 } else { 920 do_server_recv(f_in, f_out, argc, argv); 921 } 922 exit_cleanup(0); 923} 924 925 926/* 927 * This is called once the connection has been negotiated. It is used 928 * for rsyncd, remote-shell, and local connections. 929 */ 930int client_run(int f_in, int f_out, pid_t pid, int argc, char *argv[]) 931{ 932 struct file_list *flist = NULL; 933 int exit_code = 0, exit_code2 = 0; 934 char *local_name = NULL; 935 936 cleanup_child_pid = pid; 937 if (!read_batch) { 938 set_nonblocking(f_in); 939 set_nonblocking(f_out); 940 } 941 942 io_set_sock_fds(f_in, f_out); 943 setup_protocol(f_out,f_in); 944#if defined HAVE_ICONV_OPEN && defined HAVE_ICONV_H 945 setup_iconv(); 946#endif 947 948 if (protocol_version >= 23 && !read_batch) 949 io_start_multiplex_in(); 950 951 /* We set our stderr file handle to blocking because ssh might have 952 * set it to non-blocking. This can be particularly troublesome if 953 * stderr is a clone of stdout, because ssh would have set our stdout 954 * to non-blocking at the same time (which can easily cause us to lose 955 * output from our print statements). This kluge shouldn't cause ssh 956 * any problems for how we use it. Note also that we delayed setting 957 * this until after the above protocol setup so that we know for sure 958 * that ssh is done twiddling its file descriptors. */ 959 set_blocking(STDERR_FILENO); 960 961 if (am_sender) { 962 keep_dirlinks = 0; /* Must be disabled on the sender. */ 963 io_start_buffering_out(); 964 if (!filesfrom_host) 965 set_msg_fd_in(f_in); 966 send_filter_list(f_out); 967 if (filesfrom_host) 968 filesfrom_fd = f_in; 969 970 if (write_batch && !am_server) 971 start_write_batch(f_out); 972 flist = send_file_list(f_out, argc, argv); 973 set_msg_fd_in(-1); 974 if (verbose > 3) 975 rprintf(FINFO,"file list sent\n"); 976 the_file_list = flist; 977 978 io_flush(NORMAL_FLUSH); 979 send_files(flist,f_out,f_in); 980 io_flush(FULL_FLUSH); 981 handle_stats(-1); 982 if (protocol_version >= 24) 983 read_final_goodbye(f_in, f_out); 984 if (pid != -1) { 985 if (verbose > 3) 986 rprintf(FINFO,"client_run waiting on %d\n", (int) pid); 987 io_flush(FULL_FLUSH); 988 wait_process_with_flush(pid, &exit_code); 989 } 990 output_summary(); 991 io_flush(FULL_FLUSH); 992 exit_cleanup(exit_code); 993 } 994 995 if (need_messages_from_generator && !read_batch) 996 io_start_multiplex_out(); 997 998 if (argc == 0) 999 list_only |= 1; 1000 1001 send_filter_list(read_batch ? -1 : f_out); 1002 1003 if (filesfrom_fd >= 0) { 1004 io_set_filesfrom_fds(filesfrom_fd, f_out); 1005 filesfrom_fd = -1; 1006 } 1007 1008 if (write_batch && !am_server) 1009 start_write_batch(f_in); 1010 flist = recv_file_list(f_in); 1011 the_file_list = flist; 1012 1013 if (flist && flist->count > 0) { 1014 local_name = get_local_name(flist, argv[0]); 1015 1016 fix_basis_dirs(); 1017 1018 exit_code2 = do_recv(f_in, f_out, flist, local_name); 1019 } else { 1020 handle_stats(-1); 1021 output_summary(); 1022 } 1023 1024 if (pid != -1) { 1025 if (verbose > 3) 1026 rprintf(FINFO,"client_run2 waiting on %d\n", (int) pid); 1027 io_flush(FULL_FLUSH); 1028 wait_process_with_flush(pid, &exit_code); 1029 } 1030 1031 return MAX(exit_code, exit_code2); 1032} 1033 1034static int copy_argv (char *argv[]) 1035{ 1036 int i; 1037 1038 for (i = 0; argv[i]; i++) { 1039 if (!(argv[i] = strdup(argv[i]))) { 1040 rprintf (FERROR, "out of memory at %s(%d)\n", 1041 __FILE__, __LINE__); 1042 return RERR_MALLOC; 1043 } 1044 } 1045 1046 return 0; 1047} 1048 1049 1050/** 1051 * Start a client for either type of remote connection. Work out 1052 * whether the arguments request a remote shell or rsyncd connection, 1053 * and call the appropriate connection function, then run_client. 1054 * 1055 * Calls either start_socket_client (for sockets) or do_cmd and 1056 * client_run (for ssh). 1057 **/ 1058static int start_client(int argc, char *argv[]) 1059{ 1060 char *p; 1061 char *shell_machine = NULL; 1062 char *shell_path = NULL; 1063 char *shell_user = NULL; 1064 int ret; 1065 pid_t pid; 1066 int f_in,f_out; 1067 int rc; 1068 1069 /* Don't clobber argv[] so that ps(1) can still show the right 1070 * command line. */ 1071 if ((rc = copy_argv(argv))) 1072 return rc; 1073 1074 if (!read_batch) { /* for read_batch, NO source is specified */ 1075 shell_path = check_for_hostspec(argv[0], &shell_machine, &rsync_port); 1076 if (shell_path) { /* source is remote */ 1077 char *dummy1; 1078 int dummy2; 1079 if (--argc 1080 && check_for_hostspec(argv[argc], &dummy1, &dummy2)) { 1081 rprintf(FERROR, 1082 "The source and destination cannot both be remote.\n"); 1083 exit_cleanup(RERR_SYNTAX); 1084 } 1085 argv++; 1086 if (filesfrom_host && *filesfrom_host 1087 && strcmp(filesfrom_host, shell_machine) != 0) { 1088 rprintf(FERROR, 1089 "--files-from hostname is not the same as the transfer hostname\n"); 1090 exit_cleanup(RERR_SYNTAX); 1091 } 1092 if (rsync_port) { 1093 if (!shell_cmd) { 1094 return start_socket_client(shell_machine, 1095 shell_path, 1096 argc, argv); 1097 } 1098 daemon_over_rsh = 1; 1099 } 1100 1101 am_sender = 0; 1102 } else { /* source is local, check dest arg */ 1103 am_sender = 1; 1104 1105 if (argc > 1) 1106 p = argv[--argc]; 1107 else { 1108 p = "."; 1109 list_only = 1; 1110 } 1111 1112 shell_path = check_for_hostspec(p, &shell_machine, &rsync_port); 1113 if (shell_path && filesfrom_host && *filesfrom_host 1114 && strcmp(filesfrom_host, shell_machine) != 0) { 1115 rprintf(FERROR, 1116 "--files-from hostname is not the same as the transfer hostname\n"); 1117 exit_cleanup(RERR_SYNTAX); 1118 } 1119 if (!shell_path) { /* no hostspec found, so src & dest are local */ 1120 local_server = 1; 1121 if (filesfrom_host) { 1122 rprintf(FERROR, 1123 "--files-from cannot be remote when the transfer is local\n"); 1124 exit_cleanup(RERR_SYNTAX); 1125 } 1126 shell_machine = NULL; 1127 shell_path = p; 1128 } else if (rsync_port) { 1129 if (!shell_cmd) { 1130 return start_socket_client(shell_machine, 1131 shell_path, 1132 argc, argv); 1133 } 1134 daemon_over_rsh = 1; 1135 } 1136 } 1137 } else { /* read_batch */ 1138 local_server = 1; 1139 shell_path = argv[argc-1]; 1140 if (check_for_hostspec(shell_path, &shell_machine, &rsync_port)) { 1141 rprintf(FERROR, "remote destination is not allowed with --read-batch\n"); 1142 exit_cleanup(RERR_SYNTAX); 1143 } 1144 } 1145 1146 if (shell_machine) { 1147 p = strrchr(shell_machine,'@'); 1148 if (p) { 1149 *p = 0; 1150 shell_user = shell_machine; 1151 shell_machine = p+1; 1152 } 1153 } 1154 1155 if (verbose > 3) { 1156 rprintf(FINFO,"cmd=%s machine=%s user=%s path=%s\n", 1157 shell_cmd ? shell_cmd : "", 1158 shell_machine ? shell_machine : "", 1159 shell_user ? shell_user : "", 1160 shell_path ? shell_path : ""); 1161 } 1162 1163 /* for remote source, only single dest arg can remain ... */ 1164 if (!am_sender && argc > 1) { 1165 usage(FERROR); 1166 exit_cleanup(RERR_SYNTAX); 1167 } 1168 1169 /* ... or no dest at all */ 1170 if (!am_sender && argc == 0) 1171 list_only |= 1; 1172 1173 pid = do_cmd(shell_cmd,shell_machine,shell_user,shell_path, 1174 &f_in,&f_out); 1175 1176 /* if we're running an rsync server on the remote host over a 1177 * remote shell command, we need to do the RSYNCD protocol first */ 1178 if (daemon_over_rsh) { 1179 int tmpret; 1180 tmpret = start_inband_exchange(shell_user, shell_path, 1181 f_in, f_out, argc); 1182 if (tmpret < 0) 1183 return tmpret; 1184 } 1185 1186 ret = client_run(f_in, f_out, pid, argc, argv); 1187 1188 fflush(stdout); 1189 fflush(stderr); 1190 1191 return ret; 1192} 1193 1194 1195static RETSIGTYPE sigusr1_handler(UNUSED(int val)) 1196{ 1197 exit_cleanup(RERR_SIGNAL1); 1198} 1199 1200static RETSIGTYPE sigusr2_handler(UNUSED(int val)) 1201{ 1202 if (!am_server) 1203 output_summary(); 1204 close_all(); 1205 if (log_got_error) 1206 _exit(RERR_PARTIAL); 1207 _exit(0); 1208} 1209 1210RETSIGTYPE remember_children(UNUSED(int val)) 1211{ 1212#ifdef WNOHANG 1213 int cnt, status; 1214 pid_t pid; 1215 /* An empty waitpid() loop was put here by Tridge and we could never 1216 * get him to explain why he put it in, so rather than taking it 1217 * out we're instead saving the child exit statuses for later use. 1218 * The waitpid() loop presumably eliminates all possibility of leaving 1219 * zombie children, maybe that's why he did it. */ 1220 while ((pid = waitpid(-1, &status, WNOHANG)) > 0) { 1221 /* save the child's exit status */ 1222 for (cnt = 0; cnt < MAXCHILDPROCS; cnt++) { 1223 if (pid_stat_table[cnt].pid == 0) { 1224 pid_stat_table[cnt].pid = pid; 1225 pid_stat_table[cnt].status = status; 1226 break; 1227 } 1228 } 1229 } 1230#endif 1231#ifndef HAVE_SIGACTION 1232 signal(SIGCHLD, remember_children); 1233#endif 1234} 1235 1236 1237/** 1238 * This routine catches signals and tries to send them to gdb. 1239 * 1240 * Because it's called from inside a signal handler it ought not to 1241 * use too many library routines. 1242 * 1243 * @todo Perhaps use "screen -X" instead/as well, to help people 1244 * debugging without easy access to X. Perhaps use an environment 1245 * variable, or just call a script? 1246 * 1247 * @todo The /proc/ magic probably only works on Linux (and 1248 * Solaris?) Can we be more portable? 1249 **/ 1250#ifdef MAINTAINER_MODE 1251const char *get_panic_action(void) 1252{ 1253 const char *cmd_fmt = getenv("RSYNC_PANIC_ACTION"); 1254 1255 if (cmd_fmt) 1256 return cmd_fmt; 1257 else 1258 return "xterm -display :0 -T Panic -n Panic " 1259 "-e gdb /proc/%d/exe %d"; 1260} 1261 1262 1263/** 1264 * Handle a fatal signal by launching a debugger, controlled by $RSYNC_PANIC_ACTION. 1265 * 1266 * This signal handler is only installed if we were configured with 1267 * --enable-maintainer-mode. Perhaps it should always be on and we 1268 * should just look at the environment variable, but I'm a bit leery 1269 * of a signal sending us into a busy loop. 1270 **/ 1271static RETSIGTYPE rsync_panic_handler(UNUSED(int whatsig)) 1272{ 1273 char cmd_buf[300]; 1274 int ret; 1275 1276 snprintf(cmd_buf, sizeof cmd_buf, get_panic_action(), 1277 getpid(), getpid()); 1278 1279 /* Unless we failed to execute gdb, we allow the process to 1280 * continue. I'm not sure if that's right. */ 1281 ret = system(cmd_buf); 1282 if (ret) 1283 _exit(ret); 1284} 1285#endif 1286 1287 1288int main(int argc,char *argv[]) 1289{ 1290 int ret; 1291 int orig_argc = argc; 1292 char **orig_argv = argv; 1293#ifdef HAVE_SIGACTION 1294# ifdef HAVE_SIGPROCMASK 1295 sigset_t sigmask; 1296 1297 sigemptyset(&sigmask); 1298# endif 1299 sigact.sa_flags = SA_NOCLDSTOP; 1300#endif 1301 SIGACTMASK(SIGUSR1, sigusr1_handler); 1302 SIGACTMASK(SIGUSR2, sigusr2_handler); 1303 SIGACTMASK(SIGCHLD, remember_children); 1304#ifdef MAINTAINER_MODE 1305 SIGACTMASK(SIGSEGV, rsync_panic_handler); 1306 SIGACTMASK(SIGFPE, rsync_panic_handler); 1307 SIGACTMASK(SIGABRT, rsync_panic_handler); 1308 SIGACTMASK(SIGBUS, rsync_panic_handler); 1309#endif 1310 1311 starttime = time(NULL); 1312 am_root = (MY_UID() == 0); 1313 1314 memset(&stats, 0, sizeof(stats)); 1315 1316 if (argc < 2) { 1317 usage(FERROR); 1318 exit_cleanup(RERR_SYNTAX); 1319 } 1320 1321 /* we set a 0 umask so that correct file permissions can be 1322 * carried across */ 1323 orig_umask = umask(0); 1324 1325#if defined CONFIG_LOCALE && defined HAVE_SETLOCALE 1326 setlocale(LC_CTYPE, ""); 1327#endif 1328 1329 if (!parse_arguments(&argc, (const char ***) &argv, 1)) { 1330 /* FIXME: We ought to call the same error-handling 1331 * code here, rather than relying on getopt. */ 1332 option_error(); 1333 exit_cleanup(RERR_SYNTAX); 1334 } 1335 1336 SIGACTMASK(SIGINT, sig_int); 1337 SIGACTMASK(SIGHUP, sig_int); 1338 SIGACTMASK(SIGTERM, sig_int); 1339#if defined HAVE_SIGACTION && HAVE_SIGPROCMASK 1340 sigprocmask(SIG_UNBLOCK, &sigmask, NULL); 1341#endif 1342 1343 /* Ignore SIGPIPE; we consistently check error codes and will 1344 * see the EPIPE. */ 1345 SIGACTION(SIGPIPE, SIG_IGN); 1346#ifdef SIGXFSZ 1347 SIGACTION(SIGXFSZ, SIG_IGN); 1348#endif 1349 1350 /* Initialize push_dir here because on some old systems getcwd 1351 * (implemented by forking "pwd" and reading its output) doesn't 1352 * work when there are other child processes. Also, on all systems 1353 * that implement getcwd that way "pwd" can't be found after chroot. */ 1354 push_dir(NULL, 0); 1355 1356 init_flist(); 1357 1358 if ((write_batch || read_batch) && !am_server) { 1359 if (write_batch) 1360 write_batch_shell_file(orig_argc, orig_argv, argc); 1361 1362 if (read_batch && strcmp(batch_name, "-") == 0) 1363 batch_fd = STDIN_FILENO; 1364 else { 1365 batch_fd = do_open(batch_name, 1366 write_batch ? O_WRONLY | O_CREAT | O_TRUNC 1367 : O_RDONLY, S_IRUSR | S_IWUSR); 1368 } 1369 if (batch_fd < 0) { 1370 rsyserr(FERROR, errno, "Batch file %s open error", 1371 full_fname(batch_name)); 1372 exit_cleanup(RERR_FILEIO); 1373 } 1374 if (read_batch) 1375 read_stream_flags(batch_fd); 1376 } 1377 if (write_batch < 0) 1378 dry_run = 1; 1379 1380 if (am_daemon && !am_server) 1381 return daemon_main(); 1382 1383 if (argc < 1) { 1384 usage(FERROR); 1385 exit_cleanup(RERR_SYNTAX); 1386 } 1387 1388 if (am_server) { 1389 set_nonblocking(STDIN_FILENO); 1390 set_nonblocking(STDOUT_FILENO); 1391 if (am_daemon) 1392 return start_daemon(STDIN_FILENO, STDOUT_FILENO); 1393 start_server(STDIN_FILENO, STDOUT_FILENO, argc, argv); 1394 } 1395 1396 ret = start_client(argc, argv); 1397 if (ret == -1) 1398 exit_cleanup(RERR_STARTCLIENT); 1399 else 1400 exit_cleanup(ret); 1401 1402 return ret; 1403} 1404