+ int pid;
+ int exit_code = 0;
+ int error_pipe[2];
+
+ /* The receiving side mustn't obey this, or an existing symlink that
+ * points to an identical file won't be replaced by the referent. */
+ copy_links = copy_dirlinks = copy_unsafe_links = 0;
+
+#ifdef SUPPORT_HARD_LINKS
+ if (preserve_hard_links && !inc_recurse)
+ match_hard_links(first_flist);
+#endif
+
+ if (fd_pair(error_pipe) < 0) {
+ rsyserr(FERROR, errno, "pipe failed in do_recv");
+ exit_cleanup(RERR_IPC);
+ }
+
+ io_flush(NORMAL_FLUSH);
+
+ if ((pid = do_fork()) == -1) {
+ rsyserr(FERROR, errno, "fork failed in do_recv");
+ exit_cleanup(RERR_IPC);
+ }
+
+ if (pid == 0) {
+ close(error_pipe[0]);
+ if (f_in != f_out)
+ close(f_out);
+
+ /* we can't let two processes write to the socket at one time */
+ io_end_multiplex_out();
+
+ /* set place to send errors */
+ set_msg_fd_out(error_pipe[1]);
+ io_start_buffering_out(error_pipe[1]);
+
+ recv_files(f_in, local_name);
+ io_flush(FULL_FLUSH);
+ handle_stats(f_in);
+
+ send_msg(MSG_DONE, "", 1, 0);
+ write_varlong(error_pipe[1], stats.total_read, 3);
+ io_flush(FULL_FLUSH);
+
+ /* Handle any keep-alive packets from the post-processing work
+ * that the generator does. */
+ if (protocol_version >= 29) {
+ int iflags, xlen;
+ uchar fnamecmp_type;
+ char xname[MAXPATHLEN];
+
+ kluge_around_eof = -1;
+
+ /* This should only get stopped via a USR2 signal. */
+ read_ndx_and_attrs(f_in, &iflags, &fnamecmp_type,
+ xname, &xlen);
+
+ rprintf(FERROR, "Invalid packet at end of run [%s]\n",
+ who_am_i());
+ exit_cleanup(RERR_PROTOCOL);
+ }
+
+ /* Finally, we go to sleep until our parent kills us with a
+ * USR2 signal. We sleep for a short time, as on some OSes
+ * a signal won't interrupt a sleep! */
+ while (1)
+ msleep(20);
+ }
+
+ am_generator = 1;
+
+ io_end_multiplex_in();
+ if (write_batch && !am_server)
+ stop_write_batch();
+
+ close(error_pipe[1]);
+ if (f_in != f_out)
+ close(f_in);
+
+ io_start_buffering_out(f_out);
+
+ set_msg_fd_in(error_pipe[0]);
+ io_start_buffering_in(error_pipe[0]);
+
+#ifdef SUPPORT_HARD_LINKS
+ if (preserve_hard_links && inc_recurse) {
+ struct file_list *flist;
+ for (flist = first_flist; flist; flist = flist->next)
+ match_hard_links(flist);
+ }
+#endif
+
+ generate_files(f_out, local_name);
+
+ handle_stats(-1);
+ io_flush(FULL_FLUSH);
+ if (protocol_version >= 24) {
+ /* send a final goodbye message */
+ write_ndx(f_out, NDX_DONE);
+ }
+ io_flush(FULL_FLUSH);
+
+ set_msg_fd_in(-1);
+ kill(pid, SIGUSR2);
+ wait_process_with_flush(pid, &exit_code);
+ return exit_code;
+}
+
+static void do_server_recv(int f_in, int f_out, int argc, char *argv[])
+{
+ int exit_code;
+ struct file_list *flist;
+ char *local_name = NULL;
+ int save_verbose = verbose;
+
+ if (filesfrom_fd >= 0) {
+ /* We can't mix messages with files-from data on the socket,
+ * so temporarily turn off verbose messages. */
+ verbose = 0;
+ }
+
+ if (verbose > 2) {
+ rprintf(FINFO, "server_recv(%d) starting pid=%ld\n",
+ argc, (long)getpid());
+ }
+
+ if (am_daemon && lp_read_only(module_id)) {
+ rprintf(FERROR,"ERROR: module is read only\n");
+ exit_cleanup(RERR_SYNTAX);
+ return;
+ }
+
+ if (argc > 0) {
+ char *dir = argv[0];
+ argc--;
+ argv++;
+ if (!am_daemon && !change_dir(dir, CD_NORMAL)) {
+ rsyserr(FERROR, errno, "change_dir#4 %s failed",
+ full_fname(dir));
+ exit_cleanup(RERR_FILESELECT);
+ }
+ }
+
+ if (protocol_version >= 30)
+ io_start_multiplex_in();
+ else
+ io_start_buffering_in(f_in);
+ recv_filter_list(f_in);
+
+ if (filesfrom_fd >= 0) {
+ /* We need to send the files-from names to the sender at the
+ * same time that we receive the file-list from them, so we
+ * need the IO routines to automatically write out the names
+ * onto our f_out socket as we read the file-list. This
+ * avoids both deadlock and extra delays/buffers. */
+ io_set_filesfrom_fds(filesfrom_fd, f_out);
+ filesfrom_fd = -1;
+ }
+
+ flist = recv_file_list(f_in);
+ if (!flist) {
+ rprintf(FERROR,"server_recv: recv_file_list error\n");
+ exit_cleanup(RERR_FILESELECT);
+ }
+ if (inc_recurse && file_total == 1)
+ recv_additional_file_list(f_in);
+ verbose = save_verbose;
+
+ if (argc > 0)
+ local_name = get_local_name(flist,argv[0]);
+
+ /* Now that we know what our destination directory turned out to be,
+ * we can sanitize the --link-/copy-/compare-dest args correctly. */
+ if (sanitize_paths) {
+ char **dir_p;
+ for (dir_p = basis_dir; *dir_p; dir_p++)
+ *dir_p = sanitize_path(NULL, *dir_p, NULL, curr_dir_depth, SP_DEFAULT);
+ if (partial_dir)
+ partial_dir = sanitize_path(NULL, partial_dir, NULL, curr_dir_depth, SP_DEFAULT);
+ }
+ check_alt_basis_dirs();
+
+ if (daemon_filter_list.head) {
+ char **dir_p;
+ struct filter_list_struct *elp = &daemon_filter_list;
+
+ for (dir_p = basis_dir; *dir_p; dir_p++) {
+ char *dir = *dir_p;
+ if (*dir == '/')
+ dir += module_dirlen;
+ if (check_filter(elp, FLOG, dir, 1) < 0)
+ goto options_rejected;
+ }
+ if (partial_dir && *partial_dir == '/'
+ && check_filter(elp, FLOG, partial_dir + module_dirlen, 1) < 0) {
+ options_rejected:
+ rprintf(FERROR,
+ "Your options have been rejected by the server.\n");
+ exit_cleanup(RERR_SYNTAX);
+ }
+ }
+
+ exit_code = do_recv(f_in, f_out, local_name);
+ exit_cleanup(exit_code);
+}
+
+
+int child_main(int argc, char *argv[])
+{
+ start_server(STDIN_FILENO, STDOUT_FILENO, argc, argv);
+ return 0;