- preserve_perms=1;
- preserve_times=1;
- preserve_gid=1;
- if (getuid() == 0) {
- preserve_devices=1;
- preserve_uid=1;
- }
- break;
-
- case OPT_SERVER:
- am_server = 1;
- break;
-
- case OPT_SENDER:
- if (!am_server) {
- usage(FERROR);
- exit_cleanup(1);
- }
- sender = 1;
- break;
-
- case 'r':
- recurse = 1;
- break;
-
- case 'e':
- shell_cmd = optarg;
- break;
-
- case 'B':
- block_size = atoi(optarg);
- break;
-
- default:
- fprintf(FERROR,"bad option -%c\n",opt);
- exit_cleanup(1);
- }
- }
-
- while (optind--) {
- argc--;
- argv++;
- }
-
- signal(SIGCHLD,SIG_IGN);
- signal(SIGINT,SIGNAL_CAST sig_int);
- signal(SIGPIPE,SIGNAL_CAST sig_int);
- signal(SIGHUP,SIGNAL_CAST sig_int);
-
- if (dry_run)
- verbose = MAX(verbose,1);
-
- if (am_server) {
- remote_version = read_int(STDIN_FILENO);
- if (remote_version < MIN_PROTOCOL_VERSION ||
- remote_version > MAX_PROTOCOL_VERSION) {
- fprintf(FERROR,"protocol version mismatch - is your shell clean?\n");
- exit_cleanup(1);
- }
- write_int(STDOUT_FILENO,PROTOCOL_VERSION);
- write_flush(STDOUT_FILENO);
-
- setup_protocol();
-
- if (sender) {
- recv_exclude_list(STDIN_FILENO);
- if (cvs_exclude)
- add_cvs_excludes();
- do_server_sender(argc,argv);
- } else {
- do_server_recv(argc,argv);
- }
- exit_cleanup(0);
- }
-
- if (argc < 2) {
- usage(FERROR);
- exit_cleanup(1);
- }
-
- p = strchr(argv[0],':');
-
- if (p) {
- sender = 0;
- *p = 0;
- shell_machine = argv[0];
- shell_path = p+1;
- argc--;
- argv++;
- } else {
- sender = 1;
-
- p = strchr(argv[argc-1],':');
- if (!p) {
- local_server = 1;
- }
-
- if (local_server) {
- shell_machine = NULL;
- shell_path = argv[argc-1];
- } else {
- *p = 0;
- shell_machine = argv[argc-1];
- shell_path = p+1;
- }
- argc--;
- }
-
- if (shell_machine) {
- p = strchr(shell_machine,'@');
- if (p) {
- *p = 0;
- shell_user = shell_machine;
- shell_machine = p+1;
- }
- }
-
- if (verbose > 3) {
- fprintf(FERROR,"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:"");
- }
-
- if (!sender && argc != 1) {
- usage(FERROR);
- exit_cleanup(1);
- }
-
- pid = do_cmd(shell_cmd,shell_machine,shell_user,shell_path,&f_in,&f_out);
-
- write_int(f_out,PROTOCOL_VERSION);
- write_flush(f_out);
- {
- remote_version = read_int(f_in);
- if (remote_version < MIN_PROTOCOL_VERSION ||
- remote_version > MAX_PROTOCOL_VERSION) {
- fprintf(FERROR,"protocol version mismatch - is your shell clean?\n");
- exit_cleanup(1);
- }
- }
-
- setup_protocol();
-
- if (verbose > 3)
- fprintf(FERROR,"parent=%d child=%d sender=%d recurse=%d\n",
- (int)getpid(),pid,sender,recurse);
-
- if (sender) {
- if (cvs_exclude)
- add_cvs_excludes();
- if (delete_mode)
- send_exclude_list(f_out);
- flist = send_file_list(f_out,recurse,argc,argv);
- if (verbose > 3)
- fprintf(FERROR,"file list sent\n");
- send_files(flist,f_out,f_in);
- if (verbose > 3)
- fprintf(FERROR,"waiting on %d\n",pid);
- waitpid(pid, &status, 0);
- report(-1);
- exit_cleanup(status);
- }
-
- send_exclude_list(f_out);
-
- flist = recv_file_list(f_in);
- if (!flist || flist->count == 0) {
- fprintf(FERROR,"nothing to do\n");
- exit_cleanup(0);
- }
-
- local_name = get_local_name(flist,argv[0]);
-
- status2 = do_recv(f_in,f_out,flist,local_name);
-
- report(f_in);
-
- waitpid(pid, &status, 0);
-
- return status | status2;
+
+
+int main(int argc,char *argv[])
+{
+ int ret;
+ int orig_argc;
+ char **orig_argv;
+
+ orig_argc = argc;
+ orig_argv = argv;
+
+ signal(SIGUSR1, sigusr1_handler);
+ signal(SIGUSR2, sigusr2_handler);
+ signal(SIGCHLD, sigchld_handler);
+#ifdef MAINTAINER_MODE
+ signal(SIGSEGV, rsync_panic_handler);
+ signal(SIGFPE, rsync_panic_handler);
+ signal(SIGABRT, rsync_panic_handler);
+ signal(SIGBUS, rsync_panic_handler);
+#endif /* def MAINTAINER_MODE */
+
+ starttime = time(NULL);
+ am_root = (MY_UID() == 0);
+
+ memset(&stats, 0, sizeof(stats));
+
+ if (argc < 2) {
+ usage(FERROR);
+ exit_cleanup(RERR_SYNTAX);
+ }
+
+ /* we set a 0 umask so that correct file permissions can be
+ * carried across */
+ orig_umask = (int)umask(0);
+
+ if (!parse_arguments(&argc, (const char ***) &argv, 1)) {
+ /* FIXME: We ought to call the same error-handling
+ * code here, rather than relying on getopt. */
+ option_error();
+ exit_cleanup(RERR_SYNTAX);
+ }
+
+ signal(SIGINT,SIGNAL_CAST sig_int);
+ signal(SIGHUP,SIGNAL_CAST sig_int);
+ signal(SIGTERM,SIGNAL_CAST sig_int);
+
+ /* Ignore SIGPIPE; we consistently check error codes and will
+ * see the EPIPE. */
+ signal(SIGPIPE, SIG_IGN);
+
+ /* Initialize push_dir here because on some old systems getcwd
+ * (implemented by forking "pwd" and reading its output) doesn't
+ * work when there are other child processes. Also, on all systems
+ * that implement getcwd that way "pwd" can't be found after chroot. */
+ push_dir(NULL);
+
+ init_flist();
+
+ if (write_batch && !am_server) {
+ write_batch_argvs_file(orig_argc, orig_argv);
+ }
+
+ if (am_daemon && !am_server)
+ return daemon_main();
+
+ if (argc < 1) {
+ usage(FERROR);
+ exit_cleanup(RERR_SYNTAX);
+ }
+
+ if (dry_run)
+ verbose = MAX(verbose,1);
+
+ if (am_server) {
+ set_nonblocking(STDIN_FILENO);
+ set_nonblocking(STDOUT_FILENO);
+ if (am_daemon)
+ return start_daemon(STDIN_FILENO, STDOUT_FILENO);
+ start_server(STDIN_FILENO, STDOUT_FILENO, argc, argv);
+ }
+
+ ret = start_client(argc, argv);
+ if (ret == -1)
+ exit_cleanup(RERR_STARTCLIENT);
+ else
+ exit_cleanup(ret);
+
+ return ret;