+void child_main(int argc, char *argv[])
+{
+ start_server(STDIN_FILENO, STDOUT_FILENO, argc, argv);
+}
+
+
+void start_server(int f_in, int f_out, int argc, char *argv[])
+{
+ extern int cvs_exclude;
+ extern int am_sender;
+ extern int remote_version;
+ extern int read_batch;
+
+ setup_protocol(f_out, f_in);
+
+ set_nonblocking(f_in);
+ set_nonblocking(f_out);
+
+ if (remote_version >= 23)
+ io_start_multiplex_out(f_out);
+
+ if (am_sender) {
+ if (!read_batch) {
+ recv_exclude_list(f_in);
+ if (cvs_exclude)
+ add_cvs_excludes();
+ }
+ do_server_sender(f_in, f_out, argc, argv);
+ } else {
+ do_server_recv(f_in, f_out, argc, argv);
+ }
+ exit_cleanup(0);
+}
+
+
+/*
+ * This is called once the connection has been negotiated. It is used
+ * for rsyncd, remote-shell, and local connections.
+ */
+int client_run(int f_in, int f_out, pid_t pid, int argc, char *argv[])
+{
+ struct file_list *flist = NULL;
+ int status = 0, status2 = 0;
+ char *local_name = NULL;
+ extern int am_sender;
+ extern int remote_version;
+ extern pid_t cleanup_child_pid;
+ extern int write_batch;
+ extern int read_batch;
+ extern struct file_list *batch_flist;
+
+ cleanup_child_pid = pid;
+ if (read_batch)
+ flist = batch_flist;
+
+ set_nonblocking(f_in);
+ set_nonblocking(f_out);
+
+ setup_protocol(f_out,f_in);
+
+ if (remote_version >= 23)
+ io_start_multiplex_in(f_in);
+
+ if (am_sender) {
+ extern int cvs_exclude;
+ extern int delete_mode;
+ extern int delete_excluded;
+ if (cvs_exclude)
+ add_cvs_excludes();
+ if (delete_mode && !delete_excluded)
+ send_exclude_list(f_out);
+ if (!read_batch) /* dw -- don't write to pipe */
+ flist = send_file_list(f_out,argc,argv);
+ if (verbose > 3)
+ rprintf(FINFO,"file list sent\n");
+
+ send_files(flist,f_out,f_in);
+ if (remote_version >= 24) {
+ /* final goodbye message */
+ read_int(f_in);
+ }
+ if (pid != -1) {
+ if (verbose > 3)
+ rprintf(FINFO,"client_run waiting on %d\n", (int) pid);
+ io_flush();
+ wait_process(pid, &status);
+ }
+ report(-1);
+ exit_cleanup(status);
+ }
+
+ if (argc == 0) {
+ extern int list_only;
+ list_only = 1;
+ }
+
+ if (!write_batch)
+ send_exclude_list(f_out);
+
+ flist = recv_file_list(f_in);
+ if (!flist || flist->count == 0) {
+ rprintf(FINFO, "client: nothing to do: "
+ "perhaps you need to specify some filenames or "
+ "the --recursive option?\n");
+ exit_cleanup(0);
+ }
+
+ local_name = get_local_name(flist,argv[0]);
+
+ status2 = do_recv(f_in,f_out,flist,local_name);
+
+ if (pid != -1) {
+ if (verbose > 3)
+ rprintf(FINFO,"client_run2 waiting on %d\n", (int) pid);
+ io_flush();
+ wait_process(pid, &status);
+ }
+
+ return MAX(status, status2);
+}
+
+static char *find_colon(char *s)
+{
+ char *p, *p2;
+
+ p = strchr(s,':');
+ if (!p) return NULL;
+
+ /* now check to see if there is a / in the string before the : - if there is then
+ discard the colon on the assumption that the : is part of a filename */
+ p2 = strchr(s,'/');
+ if (p2 && p2 < p) return NULL;
+
+ return p;
+}
+
+
+static int copy_argv (char *argv[])
+{
+ 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;
+ extern int local_server;
+ extern int am_sender;
+ extern char *shell_cmd;
+ extern int rsync_port;
+ extern int whole_file;
+ extern int write_batch;
+ extern int read_batch;
+ 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 (strncasecmp(URL_PREFIX, argv[0], strlen(URL_PREFIX)) == 0) {
+ char *host, *path;
+
+ host = argv[0] + strlen(URL_PREFIX);
+ p = strchr(host,'/');
+ if (p) {
+ *p = 0;
+ path = p+1;
+ } else {
+ path="";
+ }
+ p = strchr(host,':');
+ if (p) {
+ rsync_port = atoi(p+1);
+ *p = 0;
+ }
+ return start_socket_client(host, path, argc-1, argv+1);
+ }
+
+ if (!read_batch) {
+ p = find_colon(argv[0]);
+
+ if (p) {
+ if (p[1] == ':') { /* double colon */
+ *p = 0;
+ return start_socket_client(argv[0], p+2, argc-1, argv+1);
+ }
+
+ if (argc < 1) {
+ usage(FERROR);
+ exit_cleanup(RERR_SYNTAX);
+ }
+
+ am_sender = 0;
+ *p = 0;
+ shell_machine = argv[0];
+ shell_path = p+1;
+ argc--;
+ argv++;
+ } else {
+ am_sender = 1;
+
+ p = find_colon(argv[argc-1]);
+ if (!p) {
+ local_server = 1;
+ } else if (p[1] == ':') {
+ *p = 0;
+ return start_socket_client(argv[argc-1], p+2, argc-1, argv);
+ }
+
+ if (argc < 2) {
+ usage(FERROR);
+ exit_cleanup(RERR_SYNTAX);
+ }
+
+ if (local_server) {
+ shell_machine = NULL;
+ shell_path = argv[argc-1];
+ } else {
+ *p = 0;
+ shell_machine = argv[argc-1];
+ shell_path = p+1;
+ }
+ argc--;
+ }
+ } else {
+ am_sender = 1;
+ local_server = 1;
+ shell_path = argv[argc-1];
+ }
+
+ if (shell_machine) {
+ p = strchr(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:"");
+ }
+
+ if (!am_sender && argc > 1) {
+ usage(FERROR);
+ exit_cleanup(RERR_SYNTAX);
+ }
+
+ if (argc == 0 && !am_sender) {
+ extern int list_only;
+ list_only = 1;
+ }
+
+ pid = do_cmd(shell_cmd,shell_machine,shell_user,shell_path,&f_in,&f_out);
+
+ ret = client_run(f_in, f_out, pid, argc, argv);
+
+ fflush(stdout);
+ fflush(stderr);
+
+ return ret;
+}
+
+
+static RETSIGTYPE sigusr1_handler(int UNUSED(val)) {
+ exit_cleanup(RERR_SIGNAL);
+}
+
+static RETSIGTYPE sigusr2_handler(int UNUSED(val)) {
+ extern int log_got_error;
+ if (log_got_error) _exit(RERR_PARTIAL);
+ _exit(0);
+}
+
+static RETSIGTYPE sigchld_handler(int UNUSED(val)) {
+#ifdef WNOHANG
+ while (waitpid(-1, NULL, WNOHANG) > 0) ;
+#endif
+}
+
+
+/**
+ * This routine catches signals and tries to send them to gdb.
+ *
+ * Because it's called from inside a signal handler it ought not to
+ * use too many library routines.
+ *
+ * @todo Perhaps use "screen -X" instead/as well, to help people
+ * debugging without easy access to X. Perhaps use an environment
+ * variable, or just call a script?
+ *
+ * @todo The /proc/ magic probably only works on Linux (and
+ * Solaris?) Can we be more portable?
+ **/
+#ifdef MAINTAINER_MODE
+static RETSIGTYPE rsync_panic_handler(int UNUSED(whatsig))