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 If you try this out, please send some email to wayned@samba.org or the rsync
17 mailing list with your results, build changes, bug reports, etc. Thanks!
19 Be sure to run "make proto" before running "make".
21 --- orig/Makefile.in 2005-11-07 04:29:00
22 +++ Makefile.in 2005-12-10 18:35:39
23 @@ -6,7 +6,7 @@ exec_prefix=@exec_prefix@
28 +LIBS=@LIBS@ -lpthread
32 --- orig/cleanup.c 2005-11-10 16:58:36
33 +++ cleanup.c 2005-12-08 23:17:08
34 @@ -94,9 +94,6 @@ void _exit_cleanup(int code, const char
38 - signal(SIGUSR1, SIG_IGN);
39 - signal(SIGUSR2, SIG_IGN);
42 rprintf(FINFO,"_exit_cleanup(code=%d, file=%s, line=%d): entered\n",
43 code, safe_fname(file), line);
44 @@ -127,8 +124,6 @@ void _exit_cleanup(int code, const char
47 do_unlink(cleanup_fname);
50 if (cleanup_pid && cleanup_pid == getpid()) {
51 char *pidf = lp_pid_file();
53 --- orig/generator.c 2005-12-16 04:03:06
54 +++ generator.c 2005-12-08 23:17:08
55 @@ -65,7 +65,6 @@ extern OFF_T min_size;
57 extern int allowed_lull;
58 extern int sock_f_out;
59 -extern int ignore_timeout;
60 extern int protocol_version;
61 extern int fuzzy_basis;
62 extern int always_checksum;
63 @@ -96,6 +95,11 @@ static int deletion_count = 0; /* used t
64 static int can_link_symlinks = 1; /* start out optimistic */
65 static int can_link_devices = 1;
67 +/* These vars are local copies so that the receiver can use the originals. */
68 +static int GEN_append_mode;
69 +static int GEN_make_backups;
70 +static int GEN_csum_length;
72 /* For calling delete_file() */
73 #define DEL_FORCE_RECURSE (1<<1) /* recurse even w/o --force */
74 #define DEL_TERSE (1<<3)
75 @@ -445,8 +449,8 @@ static void sum_sizes_sqroot(struct sum_
78 if (protocol_version < 27) {
79 - s2length = csum_length;
80 - } else if (csum_length == SUM_LENGTH) {
81 + s2length = GEN_csum_length;
82 + } else if (GEN_csum_length == SUM_LENGTH) {
83 s2length = SUM_LENGTH;
86 @@ -456,7 +460,7 @@ static void sum_sizes_sqroot(struct sum_
87 for (c = blength; c >>= 1 && b; b--) {}
88 /* add a bit, subtract rollsum, round up. */
89 s2length = (b + 1 - 32 + 7) / 8; /* --optimize in compiler-- */
90 - s2length = MAX(s2length, csum_length);
91 + s2length = MAX(s2length, GEN_csum_length);
92 s2length = MIN(s2length, SUM_LENGTH);
95 @@ -490,7 +494,7 @@ static void generate_and_send_sums(int f
96 sum_sizes_sqroot(&sum, len);
97 write_sum_head(f_out, &sum);
99 - if (append_mode > 0 && f_copy < 0)
100 + if (GEN_append_mode > 0 && f_copy < 0)
104 @@ -509,7 +513,7 @@ static void generate_and_send_sums(int f
107 full_write(f_copy, map, n1);
108 - if (append_mode > 0)
109 + if (GEN_append_mode > 0)
113 @@ -1143,7 +1147,7 @@ static void recv_generator(char *fname,
117 - if (append_mode && st.st_size > file->length)
118 + if (GEN_append_mode && st.st_size > file->length)
121 if (fnamecmp_type <= FNAMECMP_BASIS_DIR_HIGH)
122 @@ -1198,7 +1202,7 @@ static void recv_generator(char *fname,
126 - if (inplace && make_backups && fnamecmp_type == FNAMECMP_FNAME) {
127 + if (inplace && GEN_make_backups && fnamecmp_type == FNAMECMP_FNAME) {
128 if (!(backupptr = get_backup_name(fname))) {
131 @@ -1288,7 +1292,10 @@ void generate_files(int f_out, struct fi
132 int save_ignore_existing = ignore_existing;
133 int save_ignore_non_existing = ignore_non_existing;
134 int save_do_progress = do_progress;
135 - int save_make_backups = make_backups;
136 + int save_make_backups = GEN_make_backups = make_backups;
138 + GEN_append_mode = append_mode;
139 + GEN_csum_length = csum_length;
141 if (protocol_version >= 29) {
143 @@ -1317,7 +1324,7 @@ void generate_files(int f_out, struct fi
144 do_delete_pass(flist);
147 - if (append_mode || whole_file < 0)
148 + if (GEN_append_mode || whole_file < 0)
151 rprintf(FINFO, "delta-transmission %s\n",
152 @@ -1326,12 +1333,6 @@ void generate_files(int f_out, struct fi
156 - /* Since we often fill up the outgoing socket and then just sit around
157 - * waiting for the other 2 processes to do their thing, we don't want
158 - * to exit on a timeout. If the data stops flowing, the receiver will
159 - * notice that and let us know via the redo pipe (or its closing). */
160 - ignore_timeout = 1;
162 for (i = 0; i < flist->count; i++) {
163 struct file_struct *file = flist->files[i];
165 @@ -1375,23 +1376,34 @@ void generate_files(int f_out, struct fi
166 delete_in_dir(NULL, NULL, NULL);
169 - csum_length = SUM_LENGTH;
170 + GEN_csum_length = SUM_LENGTH; /* csum_length is set by the receiver */
171 max_size = min_size = ignore_existing = ignore_non_existing = 0;
172 update_only = always_checksum = size_only = 0;
174 - if (append_mode) /* resend w/o append mode */
175 - append_mode = -1; /* ... but only longer files */
176 - make_backups = 0; /* avoid a duplicate backup for inplace processing */
177 + if (GEN_append_mode) /* resend w/o append mode */
178 + GEN_append_mode = -1; /* ... but only longer files */
179 + GEN_make_backups = 0; /* avoid a duplicate backup for inplace processing */
182 rprintf(FINFO,"generate_files phase=%d\n",phase);
184 write_int(f_out, -1);
185 + io_flush(NORMAL_FLUSH);
187 /* files can cycle through the system more than once
188 * to catch initial checksum errors */
189 - while ((i = get_redo_num(itemizing, code)) != -1) {
190 - struct file_struct *file = flist->files[i];
192 + struct file_struct *file;
193 + if (preserve_hard_links)
194 + check_for_finished_hlinks(itemizing, code);
195 + if ((i = get_redo_num()) < 0) {
198 + io_flush(NORMAL_FLUSH);
202 + file = flist->files[i];
204 strlcpy(fbuf, local_name, sizeof fbuf);
206 @@ -1403,27 +1415,43 @@ void generate_files(int f_out, struct fi
208 ignore_non_existing = save_ignore_non_existing;
209 ignore_existing = save_ignore_existing;
210 - make_backups = save_make_backups;
211 + GEN_make_backups = save_make_backups;
214 rprintf(FINFO,"generate_files phase=%d\n",phase);
216 write_int(f_out, -1);
217 + io_flush(NORMAL_FLUSH);
219 /* Reduce round-trip lag-time for a useless delay-updates phase. */
220 - if (protocol_version >= 29 && !delay_updates)
221 + if (protocol_version >= 29 && !delay_updates) {
222 write_int(f_out, -1);
223 + io_flush(NORMAL_FLUSH);
226 - /* Read MSG_DONE for the redo phase (and any prior messages). */
227 - get_redo_num(itemizing, code);
228 + /* Read end marker for the redo phase (and any prior messages). */
230 + if (preserve_hard_links)
231 + check_for_finished_hlinks(itemizing, code);
232 + if (get_redo_num() == -2)
234 + io_flush(NORMAL_FLUSH);
238 if (protocol_version >= 29) {
241 rprintf(FINFO, "generate_files phase=%d\n", phase);
243 + if (delay_updates) {
244 write_int(f_out, -1);
245 - /* Read MSG_DONE for delay-updates phase & prior messages. */
246 - get_redo_num(itemizing, code);
247 + io_flush(NORMAL_FLUSH);
249 + /* Read end marker for delay-updates phase & prior messages. */
250 + while (get_redo_num() != -2) {
251 + io_flush(NORMAL_FLUSH);
256 do_progress = save_do_progress;
257 --- orig/io.c 2005-12-08 21:19:31
258 +++ io.c 2005-12-10 19:03:08
259 @@ -47,7 +47,6 @@ extern int allowed_lull;
260 extern int am_server;
261 extern int am_daemon;
262 extern int am_sender;
263 -extern int am_generator;
264 extern int eol_nulls;
265 extern int read_batch;
266 extern int csum_length;
267 @@ -60,7 +59,6 @@ extern struct stats stats;
268 extern struct file_list *the_file_list;
270 const char phase_unknown[] = "unknown";
271 -int ignore_timeout = 0;
273 int batch_gen_fd = -1;
275 @@ -84,7 +82,6 @@ const char *io_read_phase = phase_unknow
276 int kluge_around_eof = 0;
279 -int msg_fd_out = -1;
283 @@ -109,27 +106,32 @@ static int select_timeout = SELECT_TIMEO
284 static void read_loop(int fd, char *buf, size_t len);
286 struct flist_ndx_item {
287 - struct flist_ndx_item *next;
288 + volatile struct flist_ndx_item *next;
292 struct flist_ndx_list {
293 - struct flist_ndx_item *head, *tail;
294 + volatile struct flist_ndx_item *head, *tail;
295 + pthread_mutex_t mutex;
298 -static struct flist_ndx_list redo_list, hlink_list;
299 +static struct flist_ndx_list redo_list = { NULL, NULL, PTHREAD_MUTEX_INITIALIZER };
300 +static struct flist_ndx_list hlink_list = { NULL, NULL, PTHREAD_MUTEX_INITIALIZER };
302 struct msg_list_item {
303 - struct msg_list_item *next;
304 + volatile struct msg_list_item *next;
305 + pthread_mutex_t mutex;
312 - struct msg_list_item *head, *tail;
313 + volatile struct msg_list_item *head, *tail;
314 + pthread_mutex_t mutex;
317 -static struct msg_list msg_list;
318 +static struct msg_list msg_list = { NULL, NULL, PTHREAD_MUTEX_INITIALIZER };
320 static void flist_ndx_push(struct flist_ndx_list *lp, int ndx)
322 @@ -139,27 +141,31 @@ static void flist_ndx_push(struct flist_
323 out_of_memory("flist_ndx_push");
326 + pthread_mutex_lock(&redo_list.mutex);
328 lp->tail->next = item;
332 + pthread_mutex_unlock(&redo_list.mutex);
335 static int flist_ndx_pop(struct flist_ndx_list *lp)
337 - struct flist_ndx_item *next;
338 + struct flist_ndx_item *head, *next;
344 - ndx = lp->head->ndx;
345 - next = lp->head->next;
349 + pthread_mutex_lock(&hlink_list.mutex);
350 + head = (struct flist_ndx_item *)lp->head;
351 + next = (struct flist_ndx_item *)head->next;
353 + if (!(lp->head = next))
355 + pthread_mutex_unlock(&hlink_list.mutex);
360 @@ -168,7 +174,7 @@ static void check_timeout(void)
364 - if (!io_timeout || ignore_timeout)
369 @@ -209,45 +215,40 @@ void set_io_timeout(int secs)
371 /* Setup the fd used to receive MSG_* messages. Only needed during the
372 * early stages of being a local sender (up through the sending of the
373 - * file list) or when we're the generator (to fetch the messages from
374 - * the receiver). */
376 void set_msg_fd_in(int fd)
381 -/* Setup the fd used to send our MSG_* messages. Only needed when
382 - * we're the receiver (to send our messages to the generator). */
383 -void set_msg_fd_out(int fd)
386 - set_nonblocking(msg_fd_out);
389 /* Add a message to the pending MSG_* list. */
390 static void msg_list_add(int code, char *buf, int len)
392 struct msg_list_item *ml;
394 + assert(am_receiver());
395 if (!(ml = new(struct msg_list_item)))
396 out_of_memory("msg_list_add");
398 - if (!(ml->buf = new_array(char, len+4)))
399 + /* NOTE: the "+ 1" allows rwrite() to use the buf! */
400 + if (!(ml->buf = new_array(char, len + 1)))
401 out_of_memory("msg_list_add");
402 - SIVAL(ml->buf, 0, ((code+MPLEX_BASE)<<24) | len);
403 - memcpy(ml->buf+4, buf, len);
405 + memcpy(ml->buf, buf, len);
409 + pthread_mutex_lock(&msg_list.mutex);
411 msg_list.tail->next = ml;
415 + pthread_mutex_unlock(&msg_list.mutex);
418 -/* Read a message from the MSG_* fd and handle it. This is called either
419 +/* Read a message from the MSG_* fd and handle it. This is only called
420 * during the early stages of being a local sender (up through the sending
421 - * of the file list) or when we're the generator (to fetch the messages
422 - * from the receiver). */
423 + * of the file list). */
424 static void read_msg_fd(void)
427 @@ -266,40 +267,6 @@ static void read_msg_fd(void)
428 tag = (tag >> 24) - MPLEX_BASE;
432 - if (len != 0 || !am_generator) {
433 - rprintf(FERROR, "invalid message %d:%d\n", tag, len);
434 - exit_cleanup(RERR_STREAMIO);
436 - flist_ndx_push(&redo_list, -1);
439 - if (len != 4 || !am_generator) {
440 - rprintf(FERROR, "invalid message %d:%d\n", tag, len);
441 - exit_cleanup(RERR_STREAMIO);
443 - read_loop(fd, buf, 4);
444 - flist_ndx_push(&redo_list, IVAL(buf,0));
447 - if (len >= (int)sizeof buf || !am_generator) {
448 - rprintf(FERROR, "invalid message %d:%d\n", tag, len);
449 - exit_cleanup(RERR_STREAMIO);
451 - read_loop(fd, buf, len);
452 - io_multiplex_write(MSG_DELETED, buf, len);
455 - if (len != 4 || !am_generator) {
456 - rprintf(FERROR, "invalid message %d:%d\n", tag, len);
457 - exit_cleanup(RERR_STREAMIO);
459 - read_loop(fd, buf, len);
460 - if (remove_sent_files)
461 - io_multiplex_write(MSG_SUCCESS, buf, len);
462 - if (preserve_hard_links)
463 - flist_ndx_push(&hlink_list, IVAL(buf,0));
468 @@ -320,71 +287,72 @@ static void read_msg_fd(void)
472 -/* Try to push messages off the list onto the wire. If we leave with more
473 +/* Try to pop messages off the list onto the wire. If we leave with more
474 * to do, return 0. On error, return -1. If everything flushed, return 1.
475 - * This is only active in the receiver. */
476 + * This is only called by the generator. */
477 static int msg_list_flush(int flush_it_all)
479 - static int written = 0;
483 - if (msg_fd_out < 0)
486 + assert(am_generator());
488 while (msg_list.head) {
489 - struct msg_list_item *ml = msg_list.head;
490 - int n = write(msg_fd_out, ml->buf + written, ml->len - written);
492 - if (errno == EINTR)
494 - if (errno != EWOULDBLOCK && errno != EAGAIN)
499 - FD_SET(msg_fd_out, &fds);
500 - tv.tv_sec = select_timeout;
502 - if (!select(msg_fd_out+1, NULL, &fds, NULL, &tv))
504 - } else if ((written += n) == ml->len) {
506 - msg_list.head = ml->next;
507 - if (!msg_list.head)
508 - msg_list.tail = NULL;
511 + struct msg_list_item *ml = (struct msg_list_item *)msg_list.head;
512 + switch (ml->code) {
516 + rwrite(ml->code, ml->buf, ml->len);
519 + io_multiplex_write(ml->code, ml->buf, ml->len);
522 + pthread_mutex_lock(&msg_list.mutex);
523 + if (!(msg_list.head = ml->next))
524 + msg_list.tail = NULL;
525 + pthread_mutex_unlock(&msg_list.mutex);
536 void send_msg(enum msgcode code, char *buf, int len)
538 - if (msg_fd_out < 0) {
540 + msg_list_add(code, buf, len);
542 io_multiplex_write(code, buf, len);
545 - msg_list_add(code, buf, len);
546 - msg_list_flush(NORMAL_FLUSH);
549 -int get_redo_num(int itemizing, enum logcode code)
550 +/* This is only used by the receiver. */
551 +void push_redo_num(int ndx)
554 - if (hlink_list.head)
555 - check_for_finished_hlinks(itemizing, code);
556 - if (redo_list.head)
560 + assert(am_receiver());
561 + flist_ndx_push(&redo_list, ndx);
564 +/* This is only used by the generator. */
565 +int get_redo_num(void)
567 + assert(am_generator());
568 return flist_ndx_pop(&redo_list);
571 +/* This is only used by the receiver. */
572 +void push_hlink_num(int ndx)
574 + assert(am_receiver());
575 + flist_ndx_push(&hlink_list, ndx);
578 +/* This is only used by the generator. */
579 int get_hlink_num(void)
581 + assert(am_generator());
582 return flist_ndx_pop(&hlink_list);
585 @@ -465,11 +433,6 @@ static int read_timeout(int fd, char *bu
589 - if (msg_list.head) {
590 - FD_SET(msg_fd_out, &w_fds);
591 - if (msg_fd_out > maxfd)
592 - maxfd = msg_fd_out;
594 if (io_filesfrom_f_out >= 0) {
596 if (io_filesfrom_buflen == 0) {
597 @@ -502,9 +465,6 @@ static int read_timeout(int fd, char *bu
601 - if (msg_list.head && FD_ISSET(msg_fd_out, &w_fds))
602 - msg_list_flush(NORMAL_FLUSH);
604 if (io_filesfrom_f_out >= 0) {
605 if (io_filesfrom_buflen) {
606 if (FD_ISSET(io_filesfrom_f_out, &w_fds)) {
607 @@ -832,6 +792,8 @@ static void readfd(int fd, char *buffer,
610 if (fd == write_batch_monitor_in) {
611 + if (am_generator())
612 + rprintf(FINFO, "writing %d bytes to batch file from generator\n", total);
613 if ((size_t)write(batch_fd, buffer, total) != total)
614 exit_cleanup(RERR_FILEIO);
616 @@ -1091,7 +1053,6 @@ static void writefd_unbuffered(int fd,ch
617 * to grab any messages they sent before they died. */
618 while (fd == sock_f_out && io_multiplexing_in) {
620 - ignore_timeout = 0;
621 readfd_unbuffered(sock_f_in, io_filesfrom_buf,
622 sizeof io_filesfrom_buf);
624 @@ -1101,7 +1062,7 @@ static void writefd_unbuffered(int fd,ch
627 if (fd == sock_f_out) {
628 - if (io_timeout || am_generator)
629 + if (io_timeout || am_generator())
630 last_io_out = time(NULL);
631 sleep_for_bwlimit(ret);
633 @@ -1126,7 +1087,7 @@ static void mplex_write(enum msgcode cod
634 * cause output to occur down the socket. Setting contiguous_write_len
635 * prevents the reading of msg_fd_in once we actually start to write
636 * this sequence of data (though we might read it before the start). */
637 - if (am_generator && msg_fd_in >= 0)
638 + if (am_generator() && msg_fd_in >= 0)
639 contiguous_write_len = len + 4;
641 if (n > sizeof buffer - 4)
642 @@ -1141,33 +1102,31 @@ static void mplex_write(enum msgcode cod
644 writefd_unbuffered(sock_f_out, buf, len);
646 - if (am_generator && msg_fd_in >= 0)
647 + if (am_generator() && msg_fd_in >= 0)
648 contiguous_write_len = 0;
652 void io_flush(int flush_it_all)
654 - msg_list_flush(flush_it_all);
656 - if (!iobuf_out_cnt || no_flush)
660 - if (io_multiplexing_out)
661 - mplex_write(MSG_DATA, iobuf_out, iobuf_out_cnt);
663 - writefd_unbuffered(sock_f_out, iobuf_out, iobuf_out_cnt);
665 + if (iobuf_out_cnt) {
666 + if (io_multiplexing_out)
667 + mplex_write(MSG_DATA, iobuf_out, iobuf_out_cnt);
669 + writefd_unbuffered(sock_f_out, iobuf_out, iobuf_out_cnt);
673 + if (am_generator())
674 + msg_list_flush(flush_it_all);
678 static void writefd(int fd,char *buf,size_t len)
680 - if (fd == msg_fd_out) {
681 - rprintf(FERROR, "Internal error: wrong write used in receiver.\n");
682 - exit_cleanup(RERR_PROTOCOL);
685 if (fd == sock_f_out)
686 stats.total_written += len;
688 @@ -1387,9 +1346,3 @@ void start_write_batch(int fd)
690 write_batch_monitor_in = fd;
693 -void stop_write_batch(void)
695 - write_batch_monitor_out = -1;
696 - write_batch_monitor_in = -1;
698 --- orig/log.c 2005-12-16 04:03:06
699 +++ log.c 2005-12-16 04:04:49
700 @@ -35,7 +35,6 @@ extern int am_sender;
701 extern int local_server;
703 extern int module_id;
704 -extern int msg_fd_out;
705 extern int protocol_version;
706 extern int preserve_times;
707 extern int log_format_has_i;
708 @@ -68,7 +67,7 @@ struct {
709 { RERR_IPC , "error in IPC code" },
710 { RERR_CRASHED , "sibling process crashed" },
711 { RERR_TERMINATED , "sibling process terminated abnormally" },
712 - { RERR_SIGNAL , "received SIGINT, SIGTERM, SIGHUP, or SIGUSR1" },
713 + { RERR_SIGNAL , "received SIGINT, SIGTERM, or SIGHUP" },
714 { RERR_WAITCHILD , "waitpid() failed" },
715 { RERR_MALLOC , "error allocating core memory buffers" },
716 { RERR_PARTIAL , "some files could not be transferred" },
717 @@ -205,8 +204,8 @@ void rwrite(enum logcode code, char *buf
721 - if (am_server && msg_fd_out >= 0) {
722 - /* Pass the message to our sibling. */
723 + if (am_receiver()) {
724 + /* Pass the message to the generator thread. */
725 send_msg((enum msgcode)code, buf, len);
728 --- orig/main.c 2005-12-15 07:55:39
729 +++ main.c 2005-12-10 19:02:56
730 @@ -30,7 +30,6 @@ extern int list_only;
732 extern int am_server;
733 extern int am_sender;
734 -extern int am_generator;
735 extern int am_daemon;
736 extern int blocking_io;
737 extern int remove_sent_files;
738 @@ -75,9 +74,20 @@ struct pid_status {
740 static time_t starttime, endtime;
741 static int64 total_read, total_written;
742 +static pthread_t receiver_tid;
744 static void show_malloc_stats(void);
748 + return receiver_tid != 0 && pthread_self() != receiver_tid;
753 + return receiver_tid != 0 && pthread_self() == receiver_tid;
756 /* Works like waitpid(), but if we already harvested the child pid in our
757 * sigchld_handler(), we succeed instead of returning an error. */
758 pid_t wait_process(pid_t pid, int *status_ptr, int flags)
759 @@ -154,7 +164,7 @@ static void handle_stats(int f)
764 + if (am_generator())
768 @@ -558,12 +568,30 @@ static void do_server_sender(int f_in, i
772 +struct thread_args {
773 + struct file_list *flist;
778 +static void *start_receiver_thread(void *arg)
780 + static int exit_code;
781 + struct thread_args *ta = (struct thread_args *)arg;
783 + recv_files(ta->f_in, ta->flist, ta->local_name);
784 + handle_stats(ta->f_in);
788 + exit_code = log_got_error ? RERR_PARTIAL : 0;
792 static int do_recv(int f_in,int f_out,struct file_list *flist,char *local_name)
798 + struct thread_args args;
800 /* The receiving side mustn't obey this, or an existing symlink that
801 * points to an identical file won't be replaced by the referent. */
802 @@ -572,70 +600,16 @@ static int do_recv(int f_in,int f_out,st
803 if (preserve_hard_links)
806 - if (fd_pair(error_pipe) < 0) {
807 - rsyserr(FERROR, errno, "pipe failed in do_recv");
809 + args.flist = flist;
810 + args.local_name = local_name;
811 + if (pthread_create(&receiver_tid, NULL, start_receiver_thread, &args) < 0) {
812 + rsyserr(FERROR, errno, "pthread_create failed in do_recv");
813 exit_cleanup(RERR_IPC);
816 - io_flush(NORMAL_FLUSH);
818 - if ((pid = do_fork()) == -1) {
819 - rsyserr(FERROR, errno, "fork failed in do_recv");
820 - exit_cleanup(RERR_IPC);
824 - close(error_pipe[0]);
828 - /* we can't let two processes write to the socket at one time */
829 - close_multiplexing_out();
831 - /* set place to send errors */
832 - set_msg_fd_out(error_pipe[1]);
834 - recv_files(f_in, flist, local_name);
835 - io_flush(FULL_FLUSH);
836 - handle_stats(f_in);
838 - send_msg(MSG_DONE, "", 0);
839 - io_flush(FULL_FLUSH);
841 - /* Handle any keep-alive packets from the post-processing work
842 - * that the generator does. */
843 - if (protocol_version >= 29) {
844 - kluge_around_eof = -1;
846 - /* This should only get stopped via a USR2 signal. */
847 - while (read_int(f_in) == flist->count
848 - && read_shortint(f_in) == ITEM_IS_NEW) {}
850 - rprintf(FERROR, "Invalid packet at end of run [%s]\n",
852 - exit_cleanup(RERR_PROTOCOL);
855 - /* Finally, we go to sleep until our parent kills us with a
856 - * USR2 signal. We sleep for a short time, as on some OSes
857 - * a signal won't interrupt a sleep! */
863 - close_multiplexing_in();
864 - if (write_batch && !am_server)
865 - stop_write_batch();
867 - close(error_pipe[1]);
871 io_start_buffering_out();
873 - set_msg_fd_in(error_pipe[0]);
875 generate_files(f_out, flist, local_name);
878 @@ -646,10 +620,13 @@ static int do_recv(int f_in,int f_out,st
880 io_flush(FULL_FLUSH);
883 - kill(pid, SIGUSR2);
884 - wait_process_with_flush(pid, &exit_code);
886 + pthread_join(receiver_tid, &value_ptr);
892 + return *(int*)value_ptr;
896 @@ -1011,22 +988,6 @@ static int start_client(int argc, char *
901 -static RETSIGTYPE sigusr1_handler(UNUSED(int val))
903 - exit_cleanup(RERR_SIGNAL);
906 -static RETSIGTYPE sigusr2_handler(UNUSED(int val))
912 - _exit(RERR_PARTIAL);
916 static RETSIGTYPE sigchld_handler(UNUSED(int val))
919 @@ -1109,8 +1070,6 @@ int main(int argc,char *argv[])
920 int orig_argc = argc;
921 char **orig_argv = argv;
923 - signal(SIGUSR1, sigusr1_handler);
924 - signal(SIGUSR2, sigusr2_handler);
925 signal(SIGCHLD, sigchld_handler);
926 #ifdef MAINTAINER_MODE
927 signal(SIGSEGV, rsync_panic_handler);
928 --- orig/match.c 2005-11-10 16:58:36
929 +++ match.c 2005-12-08 23:17:09
933 extern int am_server;
934 -extern int do_progress;
935 +extern int recv_progress;
936 extern int checksum_seed;
937 extern int append_mode;
939 @@ -133,7 +133,7 @@ static void matched(int f, struct sum_st
943 - if (buf && do_progress)
944 + if (buf && recv_progress)
945 show_progress(last_match, buf->file_size);
948 @@ -333,7 +333,7 @@ void match_sums(int f, struct sum_struct
951 for (j = CHUNK_SIZE; j < s->flength; j += CHUNK_SIZE) {
952 - if (buf && do_progress)
953 + if (buf && recv_progress)
954 show_progress(last_match, buf->file_size);
955 sum_update(map_ptr(buf, last_match, CHUNK_SIZE),
957 @@ -341,7 +341,7 @@ void match_sums(int f, struct sum_struct
959 if (last_match < s->flength) {
960 int32 len = s->flength - last_match;
961 - if (buf && do_progress)
962 + if (buf && recv_progress)
963 show_progress(last_match, buf->file_size);
964 sum_update(map_ptr(buf, last_match, len), len);
965 last_match = s->flength;
966 --- orig/options.c 2005-11-15 18:21:22
967 +++ options.c 2005-12-08 23:17:09
968 @@ -69,7 +69,6 @@ int def_compress_level = Z_DEFAULT_COMPR
972 -int am_generator = 0;
973 int am_starting_up = 1;
975 int relative_paths = -1;
976 @@ -89,6 +88,7 @@ int am_daemon = 0;
977 int daemon_over_rsh = 0;
980 +int recv_progress = 0;
981 int keep_partial = 0;
982 int safe_symlinks = 0;
983 int copy_unsafe_links = 0;
984 @@ -1232,6 +1232,7 @@ int parse_arguments(int *argc, const cha
985 if ((do_progress || dry_run) && !verbose && !log_before_transfer
988 + recv_progress = do_progress;
992 --- orig/pipe.c 2005-10-24 21:04:45
993 +++ pipe.c 2005-12-08 23:17:09
994 @@ -55,7 +55,7 @@ pid_t piped_child(char **command, int *f
995 exit_cleanup(RERR_IPC);
1001 rsyserr(FERROR, errno, "fork");
1002 exit_cleanup(RERR_IPC);
1003 @@ -117,7 +117,7 @@ pid_t local_child(int argc, char **argv,
1004 exit_cleanup(RERR_IPC);
1010 rsyserr(FERROR, errno, "fork");
1011 exit_cleanup(RERR_IPC);
1012 --- orig/receiver.c 2005-11-10 16:58:36
1013 +++ receiver.c 2005-12-08 23:17:10
1014 @@ -24,7 +24,7 @@ extern int verbose;
1015 extern int do_xfers;
1016 extern int am_daemon;
1017 extern int am_server;
1018 -extern int do_progress;
1019 +extern int recv_progress;
1020 extern int log_before_transfer;
1021 extern int log_format_has_i;
1022 extern int daemon_log_format_has_i;
1023 @@ -220,7 +220,7 @@ static int receive_data(int f_in, char *
1025 sum.flength -= sum.blength - sum.remainder;
1026 for (j = CHUNK_SIZE; j < sum.flength; j += CHUNK_SIZE) {
1028 + if (recv_progress)
1029 show_progress(offset, total_size);
1030 sum_update(map_ptr(mapbuf, offset, CHUNK_SIZE),
1032 @@ -228,7 +228,7 @@ static int receive_data(int f_in, char *
1034 if (offset < sum.flength) {
1035 int32 len = sum.flength - offset;
1037 + if (recv_progress)
1038 show_progress(offset, total_size);
1039 sum_update(map_ptr(mapbuf, offset, len), len);
1040 offset = sum.flength;
1041 @@ -241,7 +241,7 @@ static int receive_data(int f_in, char *
1044 while ((i = recv_token(f_in, &data)) != 0) {
1046 + if (recv_progress)
1047 show_progress(offset, total_size);
1050 @@ -309,7 +309,7 @@ static int receive_data(int f_in, char *
1051 ftruncate(fd, offset);
1055 + if (recv_progress)
1056 end_progress(total_size);
1058 if (fd != -1 && offset > 0 && sparse_end(fd) != 0) {
1059 @@ -362,14 +362,13 @@ static void handle_delayed_updates(struc
1061 safe_fname(partialptr));
1063 - if (remove_sent_files
1064 - || (preserve_hard_links
1065 - && file->link_u.links)) {
1066 + if (remove_sent_files) {
1067 SIVAL(numbuf, 0, i);
1068 send_msg(MSG_SUCCESS,numbuf,4);
1070 - handle_partial_dir(partialptr,
1072 + if (preserve_hard_links && file->link_u.links)
1073 + push_hlink_num(i);
1074 + handle_partial_dir(partialptr, PDIR_DELETE);
1078 @@ -419,11 +418,6 @@ int recv_files(int f_in, struct file_lis
1080 rprintf(FINFO,"recv_files(%d) starting\n",flist->count);
1082 - if (flist->hlink_pool) {
1083 - pool_destroy(flist->hlink_pool);
1084 - flist->hlink_pool = NULL;
1088 init_delayed_bits(flist->count);
1090 @@ -444,7 +438,7 @@ int recv_files(int f_in, struct file_lis
1091 rprintf(FINFO, "recv_files phase=%d\n", phase);
1092 if (phase == 2 && delay_updates)
1093 handle_delayed_updates(flist, local_name);
1094 - send_msg(MSG_DONE, "", 0);
1095 + push_redo_num(-2);
1096 if (keep_partial && !partial_dir)
1097 make_backups = 0; /* prevents double backup */
1099 @@ -665,7 +659,7 @@ int recv_files(int f_in, struct file_lis
1100 /* log the transfer */
1101 if (log_before_transfer)
1102 log_item(file, &initial_stats, iflags, NULL);
1103 - else if (!am_server && verbose && do_progress)
1104 + else if (!am_server && verbose && recv_progress)
1105 rprintf(FINFO, "%s\n", safe_fname(fname));
1107 /* recv file data */
1108 @@ -705,11 +699,12 @@ int recv_files(int f_in, struct file_lis
1112 - if (remove_sent_files
1113 - || (preserve_hard_links && file->link_u.links)) {
1114 + if (remove_sent_files) {
1115 SIVAL(numbuf, 0, i);
1116 send_msg(MSG_SUCCESS, numbuf, 4);
1118 + if (preserve_hard_links && file->link_u.links)
1119 + push_hlink_num(i);
1120 } else if (!recv_ok) {
1121 int msgtype = phase || read_batch ? FERROR : FINFO;
1122 if (msgtype == FERROR || verbose) {
1123 @@ -732,10 +727,8 @@ int recv_files(int f_in, struct file_lis
1124 errstr, safe_fname(fname),
1128 - SIVAL(numbuf, 0, i);
1129 - send_msg(MSG_REDO, numbuf, 4);
1135 make_backups = save_make_backups;
1136 --- orig/rsync.c 2005-07-27 23:31:12
1137 +++ rsync.c 2005-12-08 23:17:10
1138 @@ -30,7 +30,6 @@ extern int omit_dir_times;
1140 extern int am_server;
1141 extern int am_sender;
1142 -extern int am_generator;
1143 extern int am_starting_up;
1144 extern int preserve_uid;
1145 extern int preserve_gid;
1146 @@ -210,5 +209,5 @@ const char *who_am_i(void)
1149 return am_server ? "server" : "client";
1150 - return am_sender ? "sender" : am_generator ? "generator" : "receiver";
1151 + return am_sender ? "sender" : am_generator() ? "generator" : "receiver";
1153 --- orig/rsync.h 2005-12-15 23:00:49
1154 +++ rsync.h 2005-12-10 19:02:58
1155 @@ -165,10 +165,8 @@ enum msgcode {
1156 MSG_DATA=0, /* raw data on the multiplexed stream */
1157 MSG_ERROR=FERROR, MSG_INFO=FINFO, /* remote logging */
1158 MSG_LOG=FLOG, MSG_FCLIENT=FCLIENT, /* sibling logging */
1159 - MSG_REDO=9, /* reprocess indicated flist index */
1160 MSG_SUCCESS=100,/* successfully updated indicated flist index */
1161 MSG_DELETED=101,/* successfully deleted a file on receiving side */
1162 - MSG_DONE=86 /* current phase is done */
1165 #include "errcode.h"
1166 @@ -319,6 +317,7 @@ enum msgcode {
1170 +#include <pthread.h>
1172 #include "lib/pool_alloc.h"
1174 --- orig/util.c 2005-11-12 20:13:05
1175 +++ util.c 2005-12-08 23:17:10
1176 @@ -405,51 +405,6 @@ int robust_rename(char *from, char *to,
1181 -static pid_t all_pids[10];
1182 -static int num_pids;
1184 -/** Fork and record the pid of the child. **/
1185 -pid_t do_fork(void)
1187 - pid_t newpid = fork();
1189 - if (newpid != 0 && newpid != -1) {
1190 - all_pids[num_pids++] = newpid;
1196 - * Kill all children.
1198 - * @todo It would be kind of nice to make sure that they are actually
1199 - * all our children before we kill them, because their pids may have
1200 - * been recycled by some other process. Perhaps when we wait for a
1201 - * child, we should remove it from this array. Alternatively we could
1202 - * perhaps use process groups, but I think that would not work on
1203 - * ancient Unix versions that don't support them.
1205 -void kill_all(int sig)
1209 - for (i = 0; i < num_pids; i++) {
1210 - /* Let's just be a little careful where we
1211 - * point that gun, hey? See kill(2) for the
1212 - * magic caused by negative values. */
1213 - pid_t p = all_pids[i];
1215 - if (p == getpid())
1225 /** Turn a user name into a uid */
1226 int name_to_uid(char *name, uid_t *uid)