- int pid, status = 0, status2 = 0;
- int opt;
- int option_index;
- char *shell_cmd = NULL;
- char *shell_machine = NULL;
- char *shell_path = NULL;
- char *shell_user = NULL;
- char *p;
- int f_in,f_out;
- struct file_list *flist;
- char *local_name = NULL;
-
- signal(SIGUSR1, sigusr1_handler);
-
- starttime = time(NULL);
- am_root = (getuid() == 0);
-
- /* we set a 0 umask so that correct file permissions can be
- carried across */
- orig_umask = (int)umask(0);
-
- while ((opt = getopt_long(argc, argv,
- short_options, long_options, &option_index))
- != -1) {
- switch (opt)
- {
- case OPT_VERSION:
- printf("rsync version %s protocol version %d\n",
- VERSION,PROTOCOL_VERSION);
- exit_cleanup(0);
-
- case OPT_SUFFIX:
- backup_suffix = optarg;
- break;
-
- case OPT_RSYNC_PATH:
- rsync_path = optarg;
- break;
-
- case 'I':
- ignore_times = 1;
- break;
-
- case 'x':
- one_file_system=1;
- break;
-
- case OPT_DELETE:
- delete_mode = 1;
- break;
-
- case OPT_FORCE:
- force_delete = 1;
- break;
-
- case OPT_NUMERIC_IDS:
- numeric_ids = 1;
- break;
-
- case OPT_EXCLUDE:
- add_exclude(optarg);
- break;
-
- case OPT_EXCLUDE_FROM:
- add_exclude_file(optarg,1);
- break;
-
- case 'h':
- usage(FINFO);
- exit_cleanup(0);
-
- case 'b':
- make_backups=1;
- break;
-
- case 'n':
- dry_run=1;
- break;
-
- case 'S':
- sparse_files=1;
- break;
-
- case 'C':
- cvs_exclude=1;
- break;
-
- case 'u':
- update_only=1;
- break;
-
- case 'l':
- preserve_links=1;
- break;
-
- case 'L':
- copy_links=1;
- break;
-
- case 'W':
- whole_file=1;
- break;
-
- case 'H':
-#if SUPPORT_HARD_LINKS
- preserve_hard_links=1;
-#else
- fprintf(FERROR,"ERROR: hard links not supported on this platform\n");
- exit_cleanup(1);
+ int i;
+
+ for (i = 0; argv[i]; i++) {
+ if (!(argv[i] = strdup(argv[i]))) {
+ rprintf (FERROR, "out of memory at %s(%d)\n",
+ __FILE__, __LINE__);
+ return RERR_MALLOC;
+ }
+ }
+
+ return 0;
+}
+
+
+/**
+ * Start a client for either type of remote connection. Work out
+ * whether the arguments request a remote shell or rsyncd connection,
+ * and call the appropriate connection function, then run_client.
+ *
+ * Calls either start_socket_client (for sockets) or do_cmd and
+ * client_run (for ssh).
+ **/
+static int start_client(int argc, char *argv[])
+{
+ char *p;
+ char *shell_machine = NULL;
+ char *shell_path = NULL;
+ char *shell_user = NULL;
+ int ret;
+ pid_t pid;
+ int f_in,f_out;
+ int rc;
+
+ /* Don't clobber argv[] so that ps(1) can still show the right
+ * command line. */
+ if ((rc = copy_argv(argv)))
+ return rc;
+
+ if (!read_batch) { /* for read_batch, NO source is specified */
+ shell_path = check_for_hostspec(argv[0], &shell_machine, &rsync_port);
+ if (shell_path) { /* source is remote */
+ char *dummy1;
+ int dummy2;
+ if (--argc
+ && check_for_hostspec(argv[argc], &dummy1, &dummy2)) {
+ rprintf(FERROR,
+ "The source and destination cannot both be remote.\n");
+ exit_cleanup(RERR_SYNTAX);
+ }
+ argv++;
+ if (filesfrom_host && *filesfrom_host
+ && strcmp(filesfrom_host, shell_machine) != 0) {
+ rprintf(FERROR,
+ "--files-from hostname is not the same as the transfer hostname\n");
+ exit_cleanup(RERR_SYNTAX);
+ }
+ if (rsync_port) {
+ if (!shell_cmd) {
+ return start_socket_client(shell_machine,
+ shell_path,
+ argc, argv);
+ }
+ daemon_over_rsh = 1;
+ }
+
+ am_sender = 0;
+ } else { /* source is local, check dest arg */
+ am_sender = 1;
+
+ if (argc > 1)
+ p = argv[--argc];
+ else {
+ p = ".";
+ list_only = 1;
+ }
+
+ shell_path = check_for_hostspec(p, &shell_machine, &rsync_port);
+ if (shell_path && filesfrom_host && *filesfrom_host
+ && strcmp(filesfrom_host, shell_machine) != 0) {
+ rprintf(FERROR,
+ "--files-from hostname is not the same as the transfer hostname\n");
+ exit_cleanup(RERR_SYNTAX);
+ }
+ if (!shell_path) { /* no hostspec found, so src & dest are local */
+ local_server = 1;
+ if (filesfrom_host) {
+ rprintf(FERROR,
+ "--files-from cannot be remote when the transfer is local\n");
+ exit_cleanup(RERR_SYNTAX);
+ }
+ shell_machine = NULL;
+ shell_path = p;
+ } else if (rsync_port) {
+ if (!shell_cmd) {
+ return start_socket_client(shell_machine,
+ shell_path,
+ argc, argv);
+ }
+ daemon_over_rsh = 1;
+ }
+ }
+ } else { /* read_batch */
+ local_server = 1;
+ shell_path = argv[argc-1];
+ if (check_for_hostspec(shell_path, &shell_machine, &rsync_port)) {
+ rprintf(FERROR, "remote destination is not allowed with --read-batch\n");
+ exit_cleanup(RERR_SYNTAX);
+ }
+ }
+
+ if (shell_machine) {
+ p = strrchr(shell_machine,'@');
+ if (p) {
+ *p = 0;
+ shell_user = shell_machine;
+ shell_machine = p+1;
+ }
+ }
+
+ if (verbose > 3) {
+ rprintf(FINFO,"cmd=%s machine=%s user=%s path=%s\n",
+ shell_cmd ? shell_cmd : "",
+ shell_machine ? shell_machine : "",
+ shell_user ? shell_user : "",
+ shell_path ? shell_path : "");
+ }
+
+ /* for remote source, only single dest arg can remain ... */
+ if (!am_sender && argc > 1) {
+ usage(FERROR);
+ exit_cleanup(RERR_SYNTAX);
+ }
+
+ /* ... or no dest at all */
+ if (!am_sender && argc == 0)
+ list_only |= 1;
+
+ pid = do_cmd(shell_cmd,shell_machine,shell_user,shell_path,
+ &f_in,&f_out);
+
+ /* if we're running an rsync server on the remote host over a
+ * remote shell command, we need to do the RSYNCD protocol first */
+ if (daemon_over_rsh) {
+ int tmpret;
+ tmpret = start_inband_exchange(shell_user, shell_path,
+ f_in, f_out, argc);
+ if (tmpret < 0)
+ return tmpret;
+ }
+
+ ret = client_run(f_in, f_out, pid, argc, argv);
+
+ fflush(stdout);
+ fflush(stderr);
+
+ return ret;
+}
+
+
+static RETSIGTYPE sigusr1_handler(UNUSED(int val))
+{
+ exit_cleanup(RERR_SIGNAL1);
+}
+
+static RETSIGTYPE sigusr2_handler(UNUSED(int val))
+{
+ if (!am_server)
+ output_summary();
+ close_all();
+ if (log_got_error)
+ _exit(RERR_PARTIAL);
+ _exit(0);
+}
+
+RETSIGTYPE remember_children(UNUSED(int val))
+{
+#ifdef WNOHANG
+ int cnt, status;
+ pid_t pid;
+ /* An empty waitpid() loop was put here by Tridge and we could never
+ * get him to explain why he put it in, so rather than taking it
+ * out we're instead saving the child exit statuses for later use.
+ * The waitpid() loop presumably eliminates all possibility of leaving
+ * zombie children, maybe that's why he did it. */
+ while ((pid = waitpid(-1, &status, WNOHANG)) > 0) {
+ /* save the child's exit status */
+ for (cnt = 0; cnt < MAXCHILDPROCS; cnt++) {
+ if (pid_stat_table[cnt].pid == 0) {
+ pid_stat_table[cnt].pid = pid;
+ pid_stat_table[cnt].status = status;
+ break;
+ }
+ }
+ }
+#endif
+#ifndef HAVE_SIGACTION
+ signal(SIGCHLD, remember_children);