1 This patch changes the receiving side to have the receiving code use a thread
2 instead of a forked process. This extra thread does read from the socket, but
3 it sends any stdout/stderr messages to the generator (main thread) to output.
5 ** This is very new code. ** Yes, it passes the "make test" testsuite, but
6 there may still be some problems, especially in some of the untested features.
7 (For one thing, I haven't yet added code to properly handle any keep-alive
8 messages that arrive on the receiving side during the --delete-after phase!)
10 This code just uses pthread.h directly, so configure changes will probably be
11 needed to make this compatible with more systems. I have also tested that
12 this code works fine using the GNU pth library without any code changes if
13 you configured it with --enable-syscall-soft --enable-pthread (you may need
14 to twiddle the Makefile options if you didn't install the library, though).
16 NOTE: we still need to duplicate the partial_fname static in util.c!
18 If you try this out, please send some email to wayned@samba.org or the rsync
19 mailing list with your results, build changes, bug reports, etc. Thanks!
21 After applying this patch, run these commands for a successful build:
24 ./configure (optional if already run)
29 @@ -6,7 +6,7 @@ exec_prefix=@exec_prefix@
34 +LIBS=@LIBS@ -lpthread
40 @@ -28,10 +28,6 @@ extern int keep_partial;
41 extern int log_got_error;
42 extern char *partial_dir;
44 -#ifdef HAVE_SIGACTION
45 -static struct sigaction sigact;
49 * Close all open sockets and files, allowing a (somewhat) graceful
50 * shutdown() of socket connections. This eliminates the abortive
51 @@ -100,9 +96,6 @@ void _exit_cleanup(int code, const char
55 - SIGACTION(SIGUSR1, SIG_IGN);
56 - SIGACTION(SIGUSR2, SIG_IGN);
59 rprintf(FINFO,"_exit_cleanup(code=%d, file=%s, line=%d): entered\n",
61 @@ -134,8 +127,6 @@ void _exit_cleanup(int code, const char
64 do_unlink(cleanup_fname);
67 if (cleanup_pid && cleanup_pid == getpid()) {
68 char *pidf = lp_pid_file();
73 #define RERR_CRASHED 15 /* sibling crashed */
74 #define RERR_TERMINATED 16 /* sibling terminated abnormally */
76 -#define RERR_SIGNAL1 19 /* status returned when sent SIGUSR1 */
77 #define RERR_SIGNAL 20 /* status returned when sent SIGINT, SIGTERM, SIGHUP */
78 #define RERR_WAITCHILD 21 /* some error returned by waitpid() */
79 #define RERR_MALLOC 22 /* error allocating core memory buffers */
82 @@ -67,7 +67,6 @@ extern OFF_T min_size;
84 extern int allowed_lull;
85 extern int sock_f_out;
86 -extern int ignore_timeout;
87 extern int protocol_version;
88 extern int fuzzy_basis;
89 extern int always_checksum;
90 @@ -95,6 +94,11 @@ extern struct filter_list_struct server_
92 static int deletion_count = 0; /* used to implement --max-delete */
94 +/* These vars are local copies so that the receiver can use the originals. */
95 +static int GEN_append_mode;
96 +static int GEN_make_backups;
97 +static int GEN_csum_length;
99 /* For calling delete_file() */
100 #define DEL_FORCE_RECURSE (1<<1) /* recurse even w/o --force */
101 #define DEL_TERSE (1<<3)
102 @@ -443,8 +447,8 @@ static void sum_sizes_sqroot(struct sum_
105 if (protocol_version < 27) {
106 - s2length = csum_length;
107 - } else if (csum_length == SUM_LENGTH) {
108 + s2length = GEN_csum_length;
109 + } else if (GEN_csum_length == SUM_LENGTH) {
110 s2length = SUM_LENGTH;
113 @@ -454,7 +458,7 @@ static void sum_sizes_sqroot(struct sum_
114 for (c = blength; c >>= 1 && b; b--) {}
115 /* add a bit, subtract rollsum, round up. */
116 s2length = (b + 1 - 32 + 7) / 8; /* --optimize in compiler-- */
117 - s2length = MAX(s2length, csum_length);
118 + s2length = MAX(s2length, GEN_csum_length);
119 s2length = MIN(s2length, SUM_LENGTH);
122 @@ -488,7 +492,7 @@ static void generate_and_send_sums(int f
123 sum_sizes_sqroot(&sum, len);
124 write_sum_head(f_out, &sum);
126 - if (append_mode > 0 && f_copy < 0)
127 + if (GEN_append_mode > 0 && f_copy < 0)
131 @@ -507,7 +511,7 @@ static void generate_and_send_sums(int f
134 full_write(f_copy, map, n1);
135 - if (append_mode > 0)
136 + if (GEN_append_mode > 0)
140 @@ -1184,7 +1188,7 @@ static void recv_generator(char *fname,
144 - if (append_mode && st.st_size > file->length)
145 + if (GEN_append_mode && st.st_size > file->length)
148 if (fnamecmp_type <= FNAMECMP_BASIS_DIR_HIGH)
149 @@ -1239,7 +1243,7 @@ static void recv_generator(char *fname,
153 - if (inplace && make_backups && fnamecmp_type == FNAMECMP_FNAME) {
154 + if (inplace && GEN_make_backups && fnamecmp_type == FNAMECMP_FNAME) {
155 if (!(backupptr = get_backup_name(fname))) {
158 @@ -1330,9 +1334,12 @@ void generate_files(int f_out, struct fi
159 int save_ignore_existing = ignore_existing;
160 int save_ignore_non_existing = ignore_non_existing;
161 int save_do_progress = do_progress;
162 - int save_make_backups = make_backups;
163 + int save_make_backups = GEN_make_backups = make_backups;
164 int dir_tweaking = !(list_only || local_name || dry_run);
166 + GEN_append_mode = append_mode;
167 + GEN_csum_length = csum_length;
169 if (protocol_version >= 29) {
171 maybe_ATTRS_REPORT = log_format_has_i ? 0 : ATTRS_REPORT;
172 @@ -1360,7 +1367,7 @@ void generate_files(int f_out, struct fi
173 do_delete_pass(flist);
176 - if (append_mode || whole_file < 0)
177 + if (GEN_append_mode || whole_file < 0)
180 rprintf(FINFO, "delta-transmission %s\n",
181 @@ -1369,12 +1376,6 @@ void generate_files(int f_out, struct fi
185 - /* Since we often fill up the outgoing socket and then just sit around
186 - * waiting for the other 2 processes to do their thing, we don't want
187 - * to exit on a timeout. If the data stops flowing, the receiver will
188 - * notice that and let us know via the redo pipe (or its closing). */
189 - ignore_timeout = 1;
191 for (i = 0; i < flist->count; i++) {
192 struct file_struct *file = flist->files[i];
194 @@ -1418,23 +1419,34 @@ void generate_files(int f_out, struct fi
195 delete_in_dir(NULL, NULL, NULL, NULL);
198 - csum_length = SUM_LENGTH;
199 + GEN_csum_length = SUM_LENGTH; /* csum_length is set by the receiver */
200 max_size = min_size = ignore_existing = ignore_non_existing = 0;
201 update_only = always_checksum = size_only = 0;
203 - if (append_mode) /* resend w/o append mode */
204 - append_mode = -1; /* ... but only longer files */
205 - make_backups = 0; /* avoid a duplicate backup for inplace processing */
206 + if (GEN_append_mode) /* resend w/o append mode */
207 + GEN_append_mode = -1; /* ... but only longer files */
208 + GEN_make_backups = 0; /* avoid a duplicate backup for inplace processing */
211 rprintf(FINFO,"generate_files phase=%d\n",phase);
213 write_int(f_out, -1);
214 + io_flush(NORMAL_FLUSH);
216 /* files can cycle through the system more than once
217 * to catch initial checksum errors */
218 - while ((i = get_redo_num(itemizing, code)) != -1) {
219 - struct file_struct *file = flist->files[i];
221 + struct file_struct *file;
222 + if (preserve_hard_links)
223 + check_for_finished_hlinks(itemizing, code);
224 + if ((i = get_redo_num()) < 0) {
227 + io_flush(NORMAL_FLUSH);
231 + file = flist->files[i];
233 strlcpy(fbuf, local_name, sizeof fbuf);
235 @@ -1446,27 +1458,43 @@ void generate_files(int f_out, struct fi
237 ignore_non_existing = save_ignore_non_existing;
238 ignore_existing = save_ignore_existing;
239 - make_backups = save_make_backups;
240 + GEN_make_backups = save_make_backups;
243 rprintf(FINFO,"generate_files phase=%d\n",phase);
245 write_int(f_out, -1);
246 + io_flush(NORMAL_FLUSH);
248 /* Reduce round-trip lag-time for a useless delay-updates phase. */
249 - if (protocol_version >= 29 && !delay_updates)
250 + if (protocol_version >= 29 && !delay_updates) {
251 write_int(f_out, -1);
252 + io_flush(NORMAL_FLUSH);
255 - /* Read MSG_DONE for the redo phase (and any prior messages). */
256 - get_redo_num(itemizing, code);
257 + /* Read end marker for the redo phase (and any prior messages). */
259 + if (preserve_hard_links)
260 + check_for_finished_hlinks(itemizing, code);
261 + if (get_redo_num() == -2)
263 + io_flush(NORMAL_FLUSH);
267 if (protocol_version >= 29) {
270 rprintf(FINFO, "generate_files phase=%d\n", phase);
272 + if (delay_updates) {
273 write_int(f_out, -1);
274 - /* Read MSG_DONE for delay-updates phase & prior messages. */
275 - get_redo_num(itemizing, code);
276 + io_flush(NORMAL_FLUSH);
278 + /* Read end marker for delay-updates phase & prior messages. */
279 + while (get_redo_num() != -2) {
280 + io_flush(NORMAL_FLUSH);
285 do_progress = save_do_progress;
288 @@ -40,20 +40,17 @@ extern int allowed_lull;
289 extern int am_server;
290 extern int am_daemon;
291 extern int am_sender;
292 -extern int am_generator;
293 extern int eol_nulls;
294 extern int read_batch;
295 extern int csum_length;
296 extern int checksum_seed;
297 extern int protocol_version;
298 -extern int remove_sent_files;
299 extern int preserve_hard_links;
300 extern char *filesfrom_host;
301 extern struct stats stats;
302 extern struct file_list *the_file_list;
304 const char phase_unknown[] = "unknown";
305 -int ignore_timeout = 0;
307 int batch_gen_fd = -1;
309 @@ -61,7 +58,6 @@ int batch_gen_fd = -1;
310 int kluge_around_eof = 0;
313 -int msg_fd_out = -1;
317 @@ -88,27 +84,31 @@ static OFF_T active_bytecnt = 0;
318 static void read_loop(int fd, char *buf, size_t len);
320 struct flist_ndx_item {
321 - struct flist_ndx_item *next;
322 + volatile struct flist_ndx_item *next;
326 struct flist_ndx_list {
327 - struct flist_ndx_item *head, *tail;
328 + volatile struct flist_ndx_item *head, *tail;
329 + pthread_mutex_t mutex;
332 -static struct flist_ndx_list redo_list, hlink_list;
333 +static struct flist_ndx_list redo_list = { NULL, NULL, PTHREAD_MUTEX_INITIALIZER };
334 +static struct flist_ndx_list hlink_list = { NULL, NULL, PTHREAD_MUTEX_INITIALIZER };
336 struct msg_list_item {
337 - struct msg_list_item *next;
338 + volatile struct msg_list_item *next;
345 - struct msg_list_item *head, *tail;
346 + volatile struct msg_list_item *head, *tail;
347 + pthread_mutex_t mutex;
350 -static struct msg_list msg2genr, msg2sndr;
351 +static struct msg_list msg_list = { NULL, NULL, PTHREAD_MUTEX_INITIALIZER };
353 static void flist_ndx_push(struct flist_ndx_list *lp, int ndx)
355 @@ -118,27 +118,31 @@ static void flist_ndx_push(struct flist_
356 out_of_memory("flist_ndx_push");
359 + pthread_mutex_lock(&redo_list.mutex);
361 lp->tail->next = item;
365 + pthread_mutex_unlock(&redo_list.mutex);
368 static int flist_ndx_pop(struct flist_ndx_list *lp)
370 - struct flist_ndx_item *next;
371 + struct flist_ndx_item *head, *next;
377 - ndx = lp->head->ndx;
378 - next = lp->head->next;
382 + pthread_mutex_lock(&hlink_list.mutex);
383 + head = (struct flist_ndx_item *)lp->head;
384 + next = (struct flist_ndx_item *)head->next;
386 + if (!(lp->head = next))
388 + pthread_mutex_unlock(&hlink_list.mutex);
393 @@ -147,7 +151,7 @@ static void check_timeout(void)
397 - if (!io_timeout || ignore_timeout)
402 @@ -188,44 +192,38 @@ void set_io_timeout(int secs)
404 /* Setup the fd used to receive MSG_* messages. Only needed during the
405 * early stages of being a local sender (up through the sending of the
406 - * file list) or when we're the generator (to fetch the messages from
407 - * the receiver). */
409 void set_msg_fd_in(int fd)
414 -/* Setup the fd used to send our MSG_* messages. Only needed when
415 - * we're the receiver (to send our messages to the generator). */
416 -void set_msg_fd_out(int fd)
419 - set_nonblocking(msg_fd_out);
422 /* Add a message to the pending MSG_* list. */
423 -static void msg_list_add(struct msg_list *lst, int code, char *buf, int len)
424 +static void msg_list_add(int code, char *buf, int len)
426 struct msg_list_item *m;
427 - int sz = len + 4 + sizeof m[0] - 1;
428 + int sz = len + sizeof m[0] - 1;
430 + assert(am_receiver());
431 if (!(m = (struct msg_list_item *)new_array(char, sz)))
432 out_of_memory("msg_list_add");
435 - SIVAL(m->buf, 0, ((code+MPLEX_BASE)<<24) | len);
436 - memcpy(m->buf + 4, buf, len);
438 - lst->tail->next = m;
441 + memcpy(m->buf, buf, len);
443 + pthread_mutex_lock(&msg_list.mutex);
445 + msg_list.tail->next = m;
451 + pthread_mutex_unlock(&msg_list.mutex);
454 -/* Read a message from the MSG_* fd and handle it. This is called either
455 +/* Read a message from the MSG_* fd and handle it. This is only called
456 * during the early stages of being a local sender (up through the sending
457 - * of the file list) or when we're the generator (to fetch the messages
458 - * from the receiver). */
459 + * of the file list). */
460 static void read_msg_fd(void)
463 @@ -244,58 +242,6 @@ static void read_msg_fd(void)
464 tag = (tag >> 24) - MPLEX_BASE;
468 - if (len != 0 || !am_generator) {
469 - rprintf(FERROR, "invalid message %d:%d\n", tag, len);
470 - exit_cleanup(RERR_STREAMIO);
472 - flist_ndx_push(&redo_list, -1);
475 - if (len != 4 || !am_generator) {
476 - rprintf(FERROR, "invalid message %d:%d\n", tag, len);
477 - exit_cleanup(RERR_STREAMIO);
479 - read_loop(fd, buf, 4);
480 - if (remove_sent_files)
481 - decrement_active_files(IVAL(buf,0));
482 - flist_ndx_push(&redo_list, IVAL(buf,0));
485 - if (len >= (int)sizeof buf || !am_generator) {
486 - rprintf(FERROR, "invalid message %d:%d\n", tag, len);
487 - exit_cleanup(RERR_STREAMIO);
489 - read_loop(fd, buf, len);
490 - if (defer_forwarding_messages)
491 - msg_list_add(&msg2sndr, MSG_DELETED, buf, len);
493 - io_multiplex_write(MSG_DELETED, buf, len);
496 - if (len != 4 || !am_generator) {
497 - rprintf(FERROR, "invalid message %d:%d\n", tag, len);
498 - exit_cleanup(RERR_STREAMIO);
500 - read_loop(fd, buf, len);
501 - if (remove_sent_files) {
502 - decrement_active_files(IVAL(buf,0));
503 - if (defer_forwarding_messages)
504 - msg_list_add(&msg2sndr, MSG_SUCCESS, buf, len);
506 - io_multiplex_write(MSG_SUCCESS, buf, len);
508 - if (preserve_hard_links)
509 - flist_ndx_push(&hlink_list, IVAL(buf,0));
512 - if (!am_generator) {
513 - rprintf(FERROR, "invalid message %d:%d\n", tag, len);
514 - exit_cleanup(RERR_STREAMIO);
516 - close_multiplexing_out();
517 - defer_forwarding_messages = 0;
522 @@ -304,11 +250,7 @@ static void read_msg_fd(void)
525 read_loop(fd, buf, n);
526 - if (am_generator && am_server
527 - && defer_forwarding_messages && tag != MSG_LOG)
528 - msg_list_add(&msg2sndr, tag, buf, n);
530 - rwrite((enum logcode)tag, buf, n);
531 + rwrite((enum logcode)tag, buf, n);
535 @@ -343,70 +285,76 @@ void decrement_active_files(int ndx)
536 active_bytecnt -= the_file_list->files[ndx]->length;
539 -/* Try to push messages off the list onto the wire. If we leave with more
540 +/* Try to pop messages off the list onto the wire. If we leave with more
541 * to do, return 0. On error, return -1. If everything flushed, return 1.
542 - * This is only active in the receiver. */
543 -static int msg2genr_flush(int flush_it_all)
544 + * This is only called by the generator. */
545 +static void msg_list_flush(void)
547 - static int written = 0;
550 + assert(am_generator());
552 - if (msg_fd_out < 0)
554 + if (defer_forwarding_messages)
557 - while (msg2genr.head) {
558 - struct msg_list_item *m = msg2genr.head;
559 - int n = write(msg_fd_out, m->buf + written, m->len - written);
561 - if (errno == EINTR)
563 - if (errno != EWOULDBLOCK && errno != EAGAIN)
568 - FD_SET(msg_fd_out, &fds);
569 - tv.tv_sec = select_timeout;
571 - if (!select(msg_fd_out+1, NULL, &fds, NULL, &tv))
573 - } else if ((written += n) == m->len) {
574 - msg2genr.head = m->next;
575 - if (!msg2genr.head)
576 - msg2genr.tail = NULL;
580 + defer_forwarding_messages = 1;
581 + while (msg_list.head) {
582 + struct msg_list_item *m = (struct msg_list_item *)msg_list.head;
583 + pthread_mutex_lock(&msg_list.mutex);
584 + if (!(msg_list.head = m->next))
585 + msg_list.tail = NULL;
586 + pthread_mutex_unlock(&msg_list.mutex);
589 + close_multiplexing_out();
594 + rwrite(m->code, m->buf, m->len);
597 + io_multiplex_write(m->code, m->buf, m->len);
603 + defer_forwarding_messages = 0;
607 void send_msg(enum msgcode code, char *buf, int len)
609 - if (msg_fd_out < 0) {
611 + msg_list_add(code, buf, len);
613 io_multiplex_write(code, buf, len);
616 - msg_list_add(&msg2genr, code, buf, len);
617 - msg2genr_flush(NORMAL_FLUSH);
620 -int get_redo_num(int itemizing, enum logcode code)
621 +/* This is only used by the receiver. */
622 +void push_redo_num(int ndx)
625 - if (hlink_list.head)
626 - check_for_finished_hlinks(itemizing, code);
627 - if (redo_list.head)
631 + assert(am_receiver());
632 + flist_ndx_push(&redo_list, ndx);
635 +/* This is only used by the generator. */
636 +int get_redo_num(void)
638 + assert(am_generator());
639 return flist_ndx_pop(&redo_list);
642 +/* This is only used by the receiver. */
643 +void push_hlink_num(int ndx)
645 + assert(am_receiver());
646 + flist_ndx_push(&hlink_list, ndx);
649 +/* This is only used by the generator. */
650 int get_hlink_num(void)
652 + assert(am_generator());
653 return flist_ndx_pop(&hlink_list);
656 @@ -486,11 +434,6 @@ static int read_timeout(int fd, char *bu
660 - if (msg2genr.head) {
661 - FD_SET(msg_fd_out, &w_fds);
662 - if (msg_fd_out > maxfd)
663 - maxfd = msg_fd_out;
665 if (io_filesfrom_f_out >= 0) {
667 if (io_filesfrom_buflen == 0) {
668 @@ -523,9 +466,6 @@ static int read_timeout(int fd, char *bu
672 - if (msg2genr.head && FD_ISSET(msg_fd_out, &w_fds))
673 - msg2genr_flush(NORMAL_FLUSH);
675 if (io_filesfrom_f_out >= 0) {
676 if (io_filesfrom_buflen) {
677 if (FD_ISSET(io_filesfrom_f_out, &w_fds)) {
678 @@ -847,6 +787,8 @@ static void readfd(int fd, char *buffer,
681 if (fd == write_batch_monitor_in) {
682 + if (am_generator())
683 + rprintf(FINFO, "writing %d bytes to batch file from generator\n", total);
684 if ((size_t)write(batch_fd, buffer, total) != total)
685 exit_cleanup(RERR_FILEIO);
687 @@ -1108,7 +1050,6 @@ static void writefd_unbuffered(int fd,ch
688 * to grab any messages they sent before they died. */
689 while (fd == sock_f_out && io_multiplexing_in) {
691 - ignore_timeout = 0;
692 readfd_unbuffered(sock_f_in, io_filesfrom_buf,
693 sizeof io_filesfrom_buf);
695 @@ -1119,7 +1060,7 @@ static void writefd_unbuffered(int fd,ch
696 defer_forwarding_messages = 1;
698 if (fd == sock_f_out) {
699 - if (io_timeout || am_generator)
700 + if (io_timeout || am_generator())
701 last_io_out = time(NULL);
702 sleep_for_bwlimit(cnt);
704 @@ -1129,32 +1070,6 @@ static void writefd_unbuffered(int fd,ch
708 -static void msg2sndr_flush(void)
710 - if (defer_forwarding_messages)
713 - while (msg2sndr.head && io_multiplexing_out) {
714 - struct msg_list_item *m = msg2sndr.head;
715 - int tag = *((uchar*)m->buf+3) - MPLEX_BASE;
716 - if (!(msg2sndr.head = m->next))
717 - msg2sndr.tail = NULL;
718 - defer_forwarding_messages = 1;
722 - rwrite((enum logcode)tag, m->buf + 4, m->len - 4);
725 - stats.total_written += m->len;
726 - writefd_unbuffered(sock_f_out, m->buf, m->len);
729 - defer_forwarding_messages = 0;
735 * Write an message to a multiplexed stream. If this fails then rsync
737 @@ -1180,14 +1095,15 @@ static void mplex_write(enum msgcode cod
738 defer_forwarding_messages = 1;
739 writefd_unbuffered(sock_f_out, buf, len);
740 defer_forwarding_messages = 0;
742 + if (am_generator())
747 void io_flush(int flush_it_all)
749 - msg2genr_flush(flush_it_all);
751 + if (am_generator())
754 if (!iobuf_out_cnt || no_flush)
756 @@ -1201,11 +1117,6 @@ void io_flush(int flush_it_all)
758 static void writefd(int fd,char *buf,size_t len)
760 - if (fd == msg_fd_out) {
761 - rprintf(FERROR, "Internal error: wrong write used in receiver.\n");
762 - exit_cleanup(RERR_PROTOCOL);
765 if (fd == sock_f_out)
766 stats.total_written += len;
768 @@ -1411,9 +1322,3 @@ void start_write_batch(int fd)
770 write_batch_monitor_in = fd;
773 -void stop_write_batch(void)
775 - write_batch_monitor_out = -1;
776 - write_batch_monitor_in = -1;
780 @@ -33,7 +33,6 @@ extern int am_sender;
781 extern int local_server;
783 extern int module_id;
784 -extern int msg_fd_out;
785 extern int allow_8bit_chars;
786 extern int protocol_version;
787 extern int preserve_times;
788 @@ -71,7 +70,6 @@ struct {
789 { RERR_IPC , "error in IPC code" },
790 { RERR_CRASHED , "sibling process crashed" },
791 { RERR_TERMINATED , "sibling process terminated abnormally" },
792 - { RERR_SIGNAL1 , "received SIGUSR1" },
793 { RERR_SIGNAL , "received SIGINT, SIGTERM, or SIGHUP" },
794 { RERR_WAITCHILD , "waitpid() failed" },
795 { RERR_MALLOC , "error allocating core memory buffers" },
796 @@ -224,8 +222,8 @@ void rwrite(enum logcode code, char *buf
797 if (quiet && code == FINFO)
800 - if (am_server && msg_fd_out >= 0) {
801 - /* Pass the message to our sibling. */
802 + if (am_receiver()) {
803 + /* Pass the message to the generator thread. */
804 send_msg((enum msgcode)code, buf, len);
809 @@ -32,7 +32,6 @@ extern int list_only;
811 extern int am_server;
812 extern int am_sender;
813 -extern int am_generator;
814 extern int am_daemon;
815 extern int blocking_io;
816 extern int remove_sent_files;
817 @@ -91,9 +90,20 @@ struct pid_status {
819 static time_t starttime, endtime;
820 static int64 total_read, total_written;
821 +static pthread_t receiver_tid;
823 static void show_malloc_stats(void);
827 + return receiver_tid != 0 && pthread_self() != receiver_tid;
832 + return receiver_tid != 0 && pthread_self() == receiver_tid;
835 /* Works like waitpid(), but if we already harvested the child pid in our
836 * remember_children(), we succeed instead of returning an error. */
837 pid_t wait_process(pid_t pid, int *status_ptr, int flags)
838 @@ -170,7 +180,7 @@ static void handle_stats(int f)
843 + if (am_generator())
847 @@ -634,12 +644,30 @@ static void do_server_sender(int f_in, i
851 +struct thread_args {
852 + struct file_list *flist;
857 +static void *start_receiver_thread(void *arg)
859 + static int exit_code;
860 + struct thread_args *ta = (struct thread_args *)arg;
862 + recv_files(ta->f_in, ta->flist, ta->local_name);
863 + handle_stats(ta->f_in);
867 + exit_code = log_got_error ? RERR_PARTIAL : 0;
871 static int do_recv(int f_in,int f_out,struct file_list *flist,char *local_name)
877 + struct thread_args args;
879 /* The receiving side mustn't obey this, or an existing symlink that
880 * points to an identical file won't be replaced by the referent. */
881 @@ -648,70 +676,16 @@ static int do_recv(int f_in,int f_out,st
882 if (preserve_hard_links)
885 - if (fd_pair(error_pipe) < 0) {
886 - rsyserr(FERROR, errno, "pipe failed in do_recv");
888 + args.flist = flist;
889 + args.local_name = local_name;
890 + if (pthread_create(&receiver_tid, NULL, start_receiver_thread, &args) < 0) {
891 + rsyserr(FERROR, errno, "pthread_create failed in do_recv");
892 exit_cleanup(RERR_IPC);
895 - io_flush(NORMAL_FLUSH);
897 - if ((pid = do_fork()) == -1) {
898 - rsyserr(FERROR, errno, "fork failed in do_recv");
899 - exit_cleanup(RERR_IPC);
903 - close(error_pipe[0]);
907 - /* we can't let two processes write to the socket at one time */
908 - close_multiplexing_out();
910 - /* set place to send errors */
911 - set_msg_fd_out(error_pipe[1]);
913 - recv_files(f_in, flist, local_name);
914 - io_flush(FULL_FLUSH);
915 - handle_stats(f_in);
917 - send_msg(MSG_DONE, "", 0);
918 - io_flush(FULL_FLUSH);
920 - /* Handle any keep-alive packets from the post-processing work
921 - * that the generator does. */
922 - if (protocol_version >= 29) {
923 - kluge_around_eof = -1;
925 - /* This should only get stopped via a USR2 signal. */
926 - while (read_int(f_in) == flist->count
927 - && read_shortint(f_in) == ITEM_IS_NEW) {}
929 - rprintf(FERROR, "Invalid packet at end of run [%s]\n",
931 - exit_cleanup(RERR_PROTOCOL);
934 - /* Finally, we go to sleep until our parent kills us with a
935 - * USR2 signal. We sleep for a short time, as on some OSes
936 - * a signal won't interrupt a sleep! */
942 - close_multiplexing_in();
943 - if (write_batch && !am_server)
944 - stop_write_batch();
946 - close(error_pipe[1]);
950 io_start_buffering_out();
952 - set_msg_fd_in(error_pipe[0]);
954 generate_files(f_out, flist, local_name);
957 @@ -722,10 +696,13 @@ static int do_recv(int f_in,int f_out,st
959 io_flush(FULL_FLUSH);
962 - kill(pid, SIGUSR2);
963 - wait_process_with_flush(pid, &exit_code);
965 + pthread_join(receiver_tid, &value_ptr);
971 + return *(int*)value_ptr;
975 @@ -1103,22 +1080,6 @@ static int start_client(int argc, char *
980 -static RETSIGTYPE sigusr1_handler(UNUSED(int val))
982 - exit_cleanup(RERR_SIGNAL1);
985 -static RETSIGTYPE sigusr2_handler(UNUSED(int val))
991 - _exit(RERR_PARTIAL);
995 RETSIGTYPE remember_children(UNUSED(int val))
998 @@ -1210,8 +1171,6 @@ int main(int argc,char *argv[])
1000 sigact.sa_flags = SA_NOCLDSTOP;
1002 - SIGACTMASK(SIGUSR1, sigusr1_handler);
1003 - SIGACTMASK(SIGUSR2, sigusr2_handler);
1004 SIGACTMASK(SIGCHLD, remember_children);
1005 #ifdef MAINTAINER_MODE
1006 SIGACTMASK(SIGSEGV, rsync_panic_handler);
1013 -extern int do_progress;
1014 +extern int recv_progress;
1015 extern int checksum_seed;
1016 extern int append_mode;
1018 @@ -113,7 +113,7 @@ static void matched(int f, struct sum_st
1020 last_match = offset;
1022 - if (buf && do_progress)
1023 + if (buf && recv_progress)
1024 show_progress(last_match, buf->file_size);
1027 @@ -317,7 +317,7 @@ void match_sums(int f, struct sum_struct
1030 for (j = CHUNK_SIZE; j < s->flength; j += CHUNK_SIZE) {
1031 - if (buf && do_progress)
1032 + if (buf && recv_progress)
1033 show_progress(last_match, buf->file_size);
1034 sum_update(map_ptr(buf, last_match, CHUNK_SIZE),
1036 @@ -325,7 +325,7 @@ void match_sums(int f, struct sum_struct
1038 if (last_match < s->flength) {
1039 int32 len = s->flength - last_match;
1040 - if (buf && do_progress)
1041 + if (buf && recv_progress)
1042 show_progress(last_match, buf->file_size);
1043 sum_update(map_ptr(buf, last_match, len), len);
1044 last_match = s->flength;
1047 @@ -74,7 +74,6 @@ int def_compress_level = Z_DEFAULT_COMPR
1051 -int am_generator = 0;
1052 int am_starting_up = 1;
1053 int relative_paths = -1;
1054 int implied_dirs = 1;
1055 @@ -95,6 +94,7 @@ int am_daemon = 0;
1056 int daemon_over_rsh = 0;
1058 int do_progress = 0;
1059 +int recv_progress = 0;
1060 int keep_partial = 0;
1061 int safe_symlinks = 0;
1062 int copy_unsafe_links = 0;
1063 @@ -1294,6 +1294,7 @@ int parse_arguments(int *argc, const cha
1065 if (do_progress && !verbose && !log_before_transfer && !am_server)
1067 + recv_progress = do_progress;
1073 @@ -58,7 +58,7 @@ pid_t piped_child(char **command, int *f
1074 exit_cleanup(RERR_IPC);
1080 rsyserr(FERROR, errno, "fork");
1081 exit_cleanup(RERR_IPC);
1082 @@ -122,7 +122,7 @@ pid_t local_child(int argc, char **argv,
1083 exit_cleanup(RERR_IPC);
1089 rsyserr(FERROR, errno, "fork");
1090 exit_cleanup(RERR_IPC);
1093 @@ -26,7 +26,7 @@ extern int verbose;
1094 extern int do_xfers;
1095 extern int am_daemon;
1096 extern int am_server;
1097 -extern int do_progress;
1098 +extern int recv_progress;
1099 extern int log_before_transfer;
1100 extern int log_format_has_i;
1101 extern int daemon_log_format_has_i;
1102 @@ -155,7 +155,7 @@ static int receive_data(int f_in, char *
1104 sum.flength -= sum.blength - sum.remainder;
1105 for (j = CHUNK_SIZE; j < sum.flength; j += CHUNK_SIZE) {
1107 + if (recv_progress)
1108 show_progress(offset, total_size);
1109 sum_update(map_ptr(mapbuf, offset, CHUNK_SIZE),
1111 @@ -163,7 +163,7 @@ static int receive_data(int f_in, char *
1113 if (offset < sum.flength) {
1114 int32 len = sum.flength - offset;
1116 + if (recv_progress)
1117 show_progress(offset, total_size);
1118 sum_update(map_ptr(mapbuf, offset, len), len);
1119 offset = sum.flength;
1120 @@ -176,7 +176,7 @@ static int receive_data(int f_in, char *
1123 while ((i = recv_token(f_in, &data)) != 0) {
1125 + if (recv_progress)
1126 show_progress(offset, total_size);
1129 @@ -244,7 +244,7 @@ static int receive_data(int f_in, char *
1130 ftruncate(fd, offset);
1134 + if (recv_progress)
1135 end_progress(total_size);
1137 if (fd != -1 && offset > 0 && sparse_end(fd) != 0) {
1138 @@ -295,12 +295,12 @@ static void handle_delayed_updates(struc
1139 "rename failed for %s (from %s)",
1140 full_fname(fname), partialptr);
1142 - if (remove_sent_files
1143 - || (preserve_hard_links
1144 - && file->link_u.links)) {
1145 + if (remove_sent_files) {
1146 SIVAL(numbuf, 0, i);
1147 send_msg(MSG_SUCCESS,numbuf,4);
1149 + if (preserve_hard_links && file->link_u.links)
1150 + push_hlink_num(i);
1151 handle_partial_dir(partialptr, PDIR_DELETE);
1154 @@ -351,11 +351,6 @@ int recv_files(int f_in, struct file_lis
1156 rprintf(FINFO,"recv_files(%d) starting\n",flist->count);
1158 - if (flist->hlink_pool) {
1159 - pool_destroy(flist->hlink_pool);
1160 - flist->hlink_pool = NULL;
1164 delayed_bits = bitbag_create(flist->count);
1166 @@ -376,7 +371,7 @@ int recv_files(int f_in, struct file_lis
1167 rprintf(FINFO, "recv_files phase=%d\n", phase);
1168 if (phase == 2 && delay_updates)
1169 handle_delayed_updates(flist, local_name);
1170 - send_msg(MSG_DONE, "", 0);
1171 + push_redo_num(-2);
1172 if (keep_partial && !partial_dir)
1173 make_backups = 0; /* prevents double backup */
1175 @@ -598,7 +593,7 @@ int recv_files(int f_in, struct file_lis
1176 /* log the transfer */
1177 if (log_before_transfer)
1178 log_item(file, &initial_stats, iflags, NULL);
1179 - else if (!am_server && verbose && do_progress)
1180 + else if (!am_server && verbose && recv_progress)
1181 rprintf(FINFO, "%s\n", fname);
1183 /* recv file data */
1184 @@ -641,11 +636,13 @@ int recv_files(int f_in, struct file_lis
1188 - if (remove_sent_files
1189 - || (preserve_hard_links && file->link_u.links)) {
1190 + if (remove_sent_files) {
1191 + decrement_active_files(i);
1192 SIVAL(numbuf, 0, i);
1193 send_msg(MSG_SUCCESS, numbuf, 4);
1195 + if (preserve_hard_links && file->link_u.links)
1196 + push_hlink_num(i);
1197 } else if (!recv_ok) {
1198 int msgtype = phase || read_batch ? FERROR : FINFO;
1199 if (msgtype == FERROR || verbose) {
1200 @@ -668,8 +665,8 @@ int recv_files(int f_in, struct file_lis
1201 errstr, fname, keptstr, redostr);
1204 - SIVAL(numbuf, 0, i);
1205 - send_msg(MSG_REDO, numbuf, 4);
1206 + decrement_active_files(i);
1213 @@ -40,7 +40,6 @@ extern int omit_dir_times;
1215 extern int am_server;
1216 extern int am_sender;
1217 -extern int am_generator;
1218 extern int am_starting_up;
1219 extern int allow_8bit_chars;
1220 extern int preserve_uid;
1221 @@ -303,5 +302,5 @@ const char *who_am_i(void)
1224 return am_server ? "server" : "client";
1225 - return am_sender ? "sender" : am_generator ? "generator" : "receiver";
1226 + return am_sender ? "sender" : am_generator() ? "generator" : "receiver";
1230 @@ -168,10 +168,8 @@ enum msgcode {
1231 MSG_DATA=0, /* raw data on the multiplexed stream */
1232 MSG_ERROR=FERROR, MSG_INFO=FINFO, /* remote logging */
1233 MSG_LOG=FLOG, MSG_SOCKERR=FSOCKERR, /* sibling logging */
1234 - MSG_REDO=9, /* reprocess indicated flist index */
1235 MSG_SUCCESS=100,/* successfully updated indicated flist index */
1236 MSG_DELETED=101,/* successfully deleted a file on receiving side */
1237 - MSG_DONE=86 /* current phase is done */
1240 #include "errcode.h"
1241 @@ -322,6 +320,7 @@ enum msgcode {
1245 +#include <pthread.h>
1247 #include "lib/pool_alloc.h"
1251 @@ -415,49 +415,6 @@ int robust_rename(char *from, char *to,
1255 -static pid_t all_pids[10];
1256 -static int num_pids;
1258 -/** Fork and record the pid of the child. **/
1259 -pid_t do_fork(void)
1261 - pid_t newpid = fork();
1263 - if (newpid != 0 && newpid != -1) {
1264 - all_pids[num_pids++] = newpid;
1270 - * Kill all children.
1272 - * @todo It would be kind of nice to make sure that they are actually
1273 - * all our children before we kill them, because their pids may have
1274 - * been recycled by some other process. Perhaps when we wait for a
1275 - * child, we should remove it from this array. Alternatively we could
1276 - * perhaps use process groups, but I think that would not work on
1277 - * ancient Unix versions that don't support them.
1279 -void kill_all(int sig)
1283 - for (i = 0; i < num_pids; i++) {
1284 - /* Let's just be a little careful where we
1285 - * point that gun, hey? See kill(2) for the
1286 - * magic caused by negative values. */
1287 - pid_t p = all_pids[i];
1289 - if (p == getpid())
1298 /** Turn a user name into a uid */
1299 int name_to_uid(char *name, uid_t *uid)