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 Be sure to run "make proto" before running "make".
23 --- orig/Makefile.in 2006-01-14 08:14:29
24 +++ Makefile.in 2005-12-10 18:35:39
25 @@ -6,7 +6,7 @@ exec_prefix=@exec_prefix@
30 +LIBS=@LIBS@ -lpthread
34 --- orig/cleanup.c 2006-01-30 07:18:27
35 +++ cleanup.c 2005-12-08 23:17:08
36 @@ -94,9 +94,6 @@ void _exit_cleanup(int code, const char
40 - signal(SIGUSR1, SIG_IGN);
41 - signal(SIGUSR2, SIG_IGN);
44 rprintf(FINFO,"_exit_cleanup(code=%d, file=%s, line=%d): entered\n",
46 @@ -127,8 +124,6 @@ void _exit_cleanup(int code, const char
49 do_unlink(cleanup_fname);
52 if (cleanup_pid && cleanup_pid == getpid()) {
53 char *pidf = lp_pid_file();
55 --- orig/errcode.h 2005-12-16 23:48:43
56 +++ errcode.h 2005-12-16 23:50:02
58 #define RERR_CRASHED 15 /* sibling crashed */
59 #define RERR_TERMINATED 16 /* sibling terminated abnormally */
61 -#define RERR_SIGNAL1 19 /* status returned when sent SIGUSR1 */
62 #define RERR_SIGNAL 20 /* status returned when sent SIGINT, SIGTERM, SIGHUP */
63 #define RERR_WAITCHILD 21 /* some error returned by waitpid() */
64 #define RERR_MALLOC 22 /* error allocating core memory buffers */
65 --- orig/generator.c 2006-01-31 02:30:18
66 +++ generator.c 2005-12-08 23:17:08
67 @@ -67,7 +67,6 @@ extern OFF_T min_size;
69 extern int allowed_lull;
70 extern int sock_f_out;
71 -extern int ignore_timeout;
72 extern int protocol_version;
73 extern int fuzzy_basis;
74 extern int always_checksum;
75 @@ -98,6 +97,11 @@ static int deletion_count = 0; /* used t
76 static int can_link_symlinks = 1; /* start out optimistic */
77 static int can_link_devices = 1;
79 +/* These vars are local copies so that the receiver can use the originals. */
80 +static int GEN_append_mode;
81 +static int GEN_make_backups;
82 +static int GEN_csum_length;
84 /* For calling delete_file() */
85 #define DEL_FORCE_RECURSE (1<<1) /* recurse even w/o --force */
86 #define DEL_TERSE (1<<3)
87 @@ -447,8 +451,8 @@ static void sum_sizes_sqroot(struct sum_
90 if (protocol_version < 27) {
91 - s2length = csum_length;
92 - } else if (csum_length == SUM_LENGTH) {
93 + s2length = GEN_csum_length;
94 + } else if (GEN_csum_length == SUM_LENGTH) {
95 s2length = SUM_LENGTH;
98 @@ -458,7 +462,7 @@ static void sum_sizes_sqroot(struct sum_
99 for (c = blength; c >>= 1 && b; b--) {}
100 /* add a bit, subtract rollsum, round up. */
101 s2length = (b + 1 - 32 + 7) / 8; /* --optimize in compiler-- */
102 - s2length = MAX(s2length, csum_length);
103 + s2length = MAX(s2length, GEN_csum_length);
104 s2length = MIN(s2length, SUM_LENGTH);
107 @@ -492,7 +496,7 @@ static void generate_and_send_sums(int f
108 sum_sizes_sqroot(&sum, len);
109 write_sum_head(f_out, &sum);
111 - if (append_mode > 0 && f_copy < 0)
112 + if (GEN_append_mode > 0 && f_copy < 0)
116 @@ -511,7 +515,7 @@ static void generate_and_send_sums(int f
119 full_write(f_copy, map, n1);
120 - if (append_mode > 0)
121 + if (GEN_append_mode > 0)
125 @@ -1141,7 +1145,7 @@ static void recv_generator(char *fname,
129 - if (append_mode && st.st_size > file->length)
130 + if (GEN_append_mode && st.st_size > file->length)
133 if (fnamecmp_type <= FNAMECMP_BASIS_DIR_HIGH)
134 @@ -1196,7 +1200,7 @@ static void recv_generator(char *fname,
138 - if (inplace && make_backups && fnamecmp_type == FNAMECMP_FNAME) {
139 + if (inplace && GEN_make_backups && fnamecmp_type == FNAMECMP_FNAME) {
140 if (!(backupptr = get_backup_name(fname))) {
143 @@ -1285,7 +1289,10 @@ void generate_files(int f_out, struct fi
144 int save_ignore_existing = ignore_existing;
145 int save_ignore_non_existing = ignore_non_existing;
146 int save_do_progress = do_progress;
147 - int save_make_backups = make_backups;
148 + int save_make_backups = GEN_make_backups = make_backups;
150 + GEN_append_mode = append_mode;
151 + GEN_csum_length = csum_length;
153 if (protocol_version >= 29) {
155 @@ -1314,7 +1321,7 @@ void generate_files(int f_out, struct fi
156 do_delete_pass(flist);
159 - if (append_mode || whole_file < 0)
160 + if (GEN_append_mode || whole_file < 0)
163 rprintf(FINFO, "delta-transmission %s\n",
164 @@ -1323,12 +1330,6 @@ void generate_files(int f_out, struct fi
168 - /* Since we often fill up the outgoing socket and then just sit around
169 - * waiting for the other 2 processes to do their thing, we don't want
170 - * to exit on a timeout. If the data stops flowing, the receiver will
171 - * notice that and let us know via the redo pipe (or its closing). */
172 - ignore_timeout = 1;
174 for (i = 0; i < flist->count; i++) {
175 struct file_struct *file = flist->files[i];
177 @@ -1372,23 +1373,34 @@ void generate_files(int f_out, struct fi
178 delete_in_dir(NULL, NULL, NULL, NULL);
181 - csum_length = SUM_LENGTH;
182 + GEN_csum_length = SUM_LENGTH; /* csum_length is set by the receiver */
183 max_size = min_size = ignore_existing = ignore_non_existing = 0;
184 update_only = always_checksum = size_only = 0;
186 - if (append_mode) /* resend w/o append mode */
187 - append_mode = -1; /* ... but only longer files */
188 - make_backups = 0; /* avoid a duplicate backup for inplace processing */
189 + if (GEN_append_mode) /* resend w/o append mode */
190 + GEN_append_mode = -1; /* ... but only longer files */
191 + GEN_make_backups = 0; /* avoid a duplicate backup for inplace processing */
194 rprintf(FINFO,"generate_files phase=%d\n",phase);
196 write_int(f_out, -1);
197 + io_flush(NORMAL_FLUSH);
199 /* files can cycle through the system more than once
200 * to catch initial checksum errors */
201 - while ((i = get_redo_num(itemizing, code)) != -1) {
202 - struct file_struct *file = flist->files[i];
204 + struct file_struct *file;
205 + if (preserve_hard_links)
206 + check_for_finished_hlinks(itemizing, code);
207 + if ((i = get_redo_num()) < 0) {
210 + io_flush(NORMAL_FLUSH);
214 + file = flist->files[i];
216 strlcpy(fbuf, local_name, sizeof fbuf);
218 @@ -1400,27 +1412,43 @@ void generate_files(int f_out, struct fi
220 ignore_non_existing = save_ignore_non_existing;
221 ignore_existing = save_ignore_existing;
222 - make_backups = save_make_backups;
223 + GEN_make_backups = save_make_backups;
226 rprintf(FINFO,"generate_files phase=%d\n",phase);
228 write_int(f_out, -1);
229 + io_flush(NORMAL_FLUSH);
231 /* Reduce round-trip lag-time for a useless delay-updates phase. */
232 - if (protocol_version >= 29 && !delay_updates)
233 + if (protocol_version >= 29 && !delay_updates) {
234 write_int(f_out, -1);
235 + io_flush(NORMAL_FLUSH);
238 - /* Read MSG_DONE for the redo phase (and any prior messages). */
239 - get_redo_num(itemizing, code);
240 + /* Read end marker for the redo phase (and any prior messages). */
242 + if (preserve_hard_links)
243 + check_for_finished_hlinks(itemizing, code);
244 + if (get_redo_num() == -2)
246 + io_flush(NORMAL_FLUSH);
250 if (protocol_version >= 29) {
253 rprintf(FINFO, "generate_files phase=%d\n", phase);
255 + if (delay_updates) {
256 write_int(f_out, -1);
257 - /* Read MSG_DONE for delay-updates phase & prior messages. */
258 - get_redo_num(itemizing, code);
259 + io_flush(NORMAL_FLUSH);
261 + /* Read end marker for delay-updates phase & prior messages. */
262 + while (get_redo_num() != -2) {
263 + io_flush(NORMAL_FLUSH);
268 do_progress = save_do_progress;
269 --- orig/io.c 2005-12-08 21:19:31
270 +++ io.c 2005-12-10 19:03:08
271 @@ -47,7 +47,6 @@ extern int allowed_lull;
272 extern int am_server;
273 extern int am_daemon;
274 extern int am_sender;
275 -extern int am_generator;
276 extern int eol_nulls;
277 extern int read_batch;
278 extern int csum_length;
279 @@ -60,7 +59,6 @@ extern struct stats stats;
280 extern struct file_list *the_file_list;
282 const char phase_unknown[] = "unknown";
283 -int ignore_timeout = 0;
285 int batch_gen_fd = -1;
287 @@ -84,7 +82,6 @@ const char *io_read_phase = phase_unknow
288 int kluge_around_eof = 0;
291 -int msg_fd_out = -1;
295 @@ -109,27 +106,32 @@ static int select_timeout = SELECT_TIMEO
296 static void read_loop(int fd, char *buf, size_t len);
298 struct flist_ndx_item {
299 - struct flist_ndx_item *next;
300 + volatile struct flist_ndx_item *next;
304 struct flist_ndx_list {
305 - struct flist_ndx_item *head, *tail;
306 + volatile struct flist_ndx_item *head, *tail;
307 + pthread_mutex_t mutex;
310 -static struct flist_ndx_list redo_list, hlink_list;
311 +static struct flist_ndx_list redo_list = { NULL, NULL, PTHREAD_MUTEX_INITIALIZER };
312 +static struct flist_ndx_list hlink_list = { NULL, NULL, PTHREAD_MUTEX_INITIALIZER };
314 struct msg_list_item {
315 - struct msg_list_item *next;
316 + volatile struct msg_list_item *next;
317 + pthread_mutex_t mutex;
324 - struct msg_list_item *head, *tail;
325 + volatile struct msg_list_item *head, *tail;
326 + pthread_mutex_t mutex;
329 -static struct msg_list msg_list;
330 +static struct msg_list msg_list = { NULL, NULL, PTHREAD_MUTEX_INITIALIZER };
332 static void flist_ndx_push(struct flist_ndx_list *lp, int ndx)
334 @@ -139,27 +141,31 @@ static void flist_ndx_push(struct flist_
335 out_of_memory("flist_ndx_push");
338 + pthread_mutex_lock(&redo_list.mutex);
340 lp->tail->next = item;
344 + pthread_mutex_unlock(&redo_list.mutex);
347 static int flist_ndx_pop(struct flist_ndx_list *lp)
349 - struct flist_ndx_item *next;
350 + struct flist_ndx_item *head, *next;
356 - ndx = lp->head->ndx;
357 - next = lp->head->next;
361 + pthread_mutex_lock(&hlink_list.mutex);
362 + head = (struct flist_ndx_item *)lp->head;
363 + next = (struct flist_ndx_item *)head->next;
365 + if (!(lp->head = next))
367 + pthread_mutex_unlock(&hlink_list.mutex);
372 @@ -168,7 +174,7 @@ static void check_timeout(void)
376 - if (!io_timeout || ignore_timeout)
381 @@ -209,45 +215,40 @@ void set_io_timeout(int secs)
383 /* Setup the fd used to receive MSG_* messages. Only needed during the
384 * early stages of being a local sender (up through the sending of the
385 - * file list) or when we're the generator (to fetch the messages from
386 - * the receiver). */
388 void set_msg_fd_in(int fd)
393 -/* Setup the fd used to send our MSG_* messages. Only needed when
394 - * we're the receiver (to send our messages to the generator). */
395 -void set_msg_fd_out(int fd)
398 - set_nonblocking(msg_fd_out);
401 /* Add a message to the pending MSG_* list. */
402 static void msg_list_add(int code, char *buf, int len)
404 struct msg_list_item *ml;
406 + assert(am_receiver());
407 if (!(ml = new(struct msg_list_item)))
408 out_of_memory("msg_list_add");
410 - if (!(ml->buf = new_array(char, len+4)))
411 + /* NOTE: the "+ 1" allows rwrite() to use the buf! */
412 + if (!(ml->buf = new_array(char, len + 1)))
413 out_of_memory("msg_list_add");
414 - SIVAL(ml->buf, 0, ((code+MPLEX_BASE)<<24) | len);
415 - memcpy(ml->buf+4, buf, len);
417 + memcpy(ml->buf, buf, len);
421 + pthread_mutex_lock(&msg_list.mutex);
423 msg_list.tail->next = ml;
427 + pthread_mutex_unlock(&msg_list.mutex);
430 -/* Read a message from the MSG_* fd and handle it. This is called either
431 +/* Read a message from the MSG_* fd and handle it. This is only called
432 * during the early stages of being a local sender (up through the sending
433 - * of the file list) or when we're the generator (to fetch the messages
434 - * from the receiver). */
435 + * of the file list). */
436 static void read_msg_fd(void)
439 @@ -266,40 +267,6 @@ static void read_msg_fd(void)
440 tag = (tag >> 24) - MPLEX_BASE;
444 - if (len != 0 || !am_generator) {
445 - rprintf(FERROR, "invalid message %d:%d\n", tag, len);
446 - exit_cleanup(RERR_STREAMIO);
448 - flist_ndx_push(&redo_list, -1);
451 - if (len != 4 || !am_generator) {
452 - rprintf(FERROR, "invalid message %d:%d\n", tag, len);
453 - exit_cleanup(RERR_STREAMIO);
455 - read_loop(fd, buf, 4);
456 - flist_ndx_push(&redo_list, IVAL(buf,0));
459 - if (len >= (int)sizeof buf || !am_generator) {
460 - rprintf(FERROR, "invalid message %d:%d\n", tag, len);
461 - exit_cleanup(RERR_STREAMIO);
463 - read_loop(fd, buf, len);
464 - io_multiplex_write(MSG_DELETED, buf, len);
467 - if (len != 4 || !am_generator) {
468 - rprintf(FERROR, "invalid message %d:%d\n", tag, len);
469 - exit_cleanup(RERR_STREAMIO);
471 - read_loop(fd, buf, len);
472 - if (remove_sent_files)
473 - io_multiplex_write(MSG_SUCCESS, buf, len);
474 - if (preserve_hard_links)
475 - flist_ndx_push(&hlink_list, IVAL(buf,0));
480 @@ -320,71 +287,72 @@ static void read_msg_fd(void)
484 -/* Try to push messages off the list onto the wire. If we leave with more
485 +/* Try to pop messages off the list onto the wire. If we leave with more
486 * to do, return 0. On error, return -1. If everything flushed, return 1.
487 - * This is only active in the receiver. */
488 + * This is only called by the generator. */
489 static int msg_list_flush(int flush_it_all)
491 - static int written = 0;
495 - if (msg_fd_out < 0)
498 + assert(am_generator());
500 while (msg_list.head) {
501 - struct msg_list_item *ml = msg_list.head;
502 - int n = write(msg_fd_out, ml->buf + written, ml->len - written);
504 - if (errno == EINTR)
506 - if (errno != EWOULDBLOCK && errno != EAGAIN)
511 - FD_SET(msg_fd_out, &fds);
512 - tv.tv_sec = select_timeout;
514 - if (!select(msg_fd_out+1, NULL, &fds, NULL, &tv))
516 - } else if ((written += n) == ml->len) {
518 - msg_list.head = ml->next;
519 - if (!msg_list.head)
520 - msg_list.tail = NULL;
523 + struct msg_list_item *ml = (struct msg_list_item *)msg_list.head;
524 + switch (ml->code) {
528 + rwrite(ml->code, ml->buf, ml->len);
531 + io_multiplex_write(ml->code, ml->buf, ml->len);
534 + pthread_mutex_lock(&msg_list.mutex);
535 + if (!(msg_list.head = ml->next))
536 + msg_list.tail = NULL;
537 + pthread_mutex_unlock(&msg_list.mutex);
548 void send_msg(enum msgcode code, char *buf, int len)
550 - if (msg_fd_out < 0) {
552 + msg_list_add(code, buf, len);
554 io_multiplex_write(code, buf, len);
557 - msg_list_add(code, buf, len);
558 - msg_list_flush(NORMAL_FLUSH);
561 -int get_redo_num(int itemizing, enum logcode code)
562 +/* This is only used by the receiver. */
563 +void push_redo_num(int ndx)
566 - if (hlink_list.head)
567 - check_for_finished_hlinks(itemizing, code);
568 - if (redo_list.head)
572 + assert(am_receiver());
573 + flist_ndx_push(&redo_list, ndx);
576 +/* This is only used by the generator. */
577 +int get_redo_num(void)
579 + assert(am_generator());
580 return flist_ndx_pop(&redo_list);
583 +/* This is only used by the receiver. */
584 +void push_hlink_num(int ndx)
586 + assert(am_receiver());
587 + flist_ndx_push(&hlink_list, ndx);
590 +/* This is only used by the generator. */
591 int get_hlink_num(void)
593 + assert(am_generator());
594 return flist_ndx_pop(&hlink_list);
597 @@ -465,11 +433,6 @@ static int read_timeout(int fd, char *bu
601 - if (msg_list.head) {
602 - FD_SET(msg_fd_out, &w_fds);
603 - if (msg_fd_out > maxfd)
604 - maxfd = msg_fd_out;
606 if (io_filesfrom_f_out >= 0) {
608 if (io_filesfrom_buflen == 0) {
609 @@ -502,9 +465,6 @@ static int read_timeout(int fd, char *bu
613 - if (msg_list.head && FD_ISSET(msg_fd_out, &w_fds))
614 - msg_list_flush(NORMAL_FLUSH);
616 if (io_filesfrom_f_out >= 0) {
617 if (io_filesfrom_buflen) {
618 if (FD_ISSET(io_filesfrom_f_out, &w_fds)) {
619 @@ -832,6 +792,8 @@ static void readfd(int fd, char *buffer,
622 if (fd == write_batch_monitor_in) {
623 + if (am_generator())
624 + rprintf(FINFO, "writing %d bytes to batch file from generator\n", total);
625 if ((size_t)write(batch_fd, buffer, total) != total)
626 exit_cleanup(RERR_FILEIO);
628 @@ -1091,7 +1053,6 @@ static void writefd_unbuffered(int fd,ch
629 * to grab any messages they sent before they died. */
630 while (fd == sock_f_out && io_multiplexing_in) {
632 - ignore_timeout = 0;
633 readfd_unbuffered(sock_f_in, io_filesfrom_buf,
634 sizeof io_filesfrom_buf);
636 @@ -1101,7 +1062,7 @@ static void writefd_unbuffered(int fd,ch
639 if (fd == sock_f_out) {
640 - if (io_timeout || am_generator)
641 + if (io_timeout || am_generator())
642 last_io_out = time(NULL);
643 sleep_for_bwlimit(ret);
645 @@ -1126,7 +1087,7 @@ static void mplex_write(enum msgcode cod
646 * cause output to occur down the socket. Setting contiguous_write_len
647 * prevents the reading of msg_fd_in once we actually start to write
648 * this sequence of data (though we might read it before the start). */
649 - if (am_generator && msg_fd_in >= 0)
650 + if (am_generator() && msg_fd_in >= 0)
651 contiguous_write_len = len + 4;
653 if (n > sizeof buffer - 4)
654 @@ -1141,33 +1102,31 @@ static void mplex_write(enum msgcode cod
656 writefd_unbuffered(sock_f_out, buf, len);
658 - if (am_generator && msg_fd_in >= 0)
659 + if (am_generator() && msg_fd_in >= 0)
660 contiguous_write_len = 0;
664 void io_flush(int flush_it_all)
666 - msg_list_flush(flush_it_all);
668 - if (!iobuf_out_cnt || no_flush)
672 - if (io_multiplexing_out)
673 - mplex_write(MSG_DATA, iobuf_out, iobuf_out_cnt);
675 - writefd_unbuffered(sock_f_out, iobuf_out, iobuf_out_cnt);
677 + if (iobuf_out_cnt) {
678 + if (io_multiplexing_out)
679 + mplex_write(MSG_DATA, iobuf_out, iobuf_out_cnt);
681 + writefd_unbuffered(sock_f_out, iobuf_out, iobuf_out_cnt);
685 + if (am_generator())
686 + msg_list_flush(flush_it_all);
690 static void writefd(int fd,char *buf,size_t len)
692 - if (fd == msg_fd_out) {
693 - rprintf(FERROR, "Internal error: wrong write used in receiver.\n");
694 - exit_cleanup(RERR_PROTOCOL);
697 if (fd == sock_f_out)
698 stats.total_written += len;
700 @@ -1387,9 +1346,3 @@ void start_write_batch(int fd)
702 write_batch_monitor_in = fd;
705 -void stop_write_batch(void)
707 - write_batch_monitor_out = -1;
708 - write_batch_monitor_in = -1;
710 --- orig/log.c 2006-01-26 10:45:39
711 +++ log.c 2005-12-16 23:49:57
712 @@ -35,7 +35,6 @@ extern int am_sender;
713 extern int local_server;
715 extern int module_id;
716 -extern int msg_fd_out;
717 extern int protocol_version;
718 extern int preserve_times;
719 extern int log_format_has_i;
720 @@ -68,7 +67,6 @@ struct {
721 { RERR_IPC , "error in IPC code" },
722 { RERR_CRASHED , "sibling process crashed" },
723 { RERR_TERMINATED , "sibling process terminated abnormally" },
724 - { RERR_SIGNAL1 , "received SIGUSR1" },
725 { RERR_SIGNAL , "received SIGINT, SIGTERM, or SIGHUP" },
726 { RERR_WAITCHILD , "waitpid() failed" },
727 { RERR_MALLOC , "error allocating core memory buffers" },
728 @@ -224,8 +222,8 @@ void rwrite(enum logcode code, char *buf
730 exit_cleanup(RERR_MESSAGEIO);
732 - if (am_server && msg_fd_out >= 0) {
733 - /* Pass the message to our sibling. */
734 + if (am_receiver()) {
735 + /* Pass the message to the generator thread. */
736 send_msg((enum msgcode)code, buf, len);
739 --- orig/main.c 2006-01-15 14:46:15
740 +++ main.c 2005-12-16 23:50:33
741 @@ -30,7 +30,6 @@ extern int list_only;
743 extern int am_server;
744 extern int am_sender;
745 -extern int am_generator;
746 extern int am_daemon;
747 extern int blocking_io;
748 extern int remove_sent_files;
749 @@ -75,9 +74,20 @@ struct pid_status {
751 static time_t starttime, endtime;
752 static int64 total_read, total_written;
753 +static pthread_t receiver_tid;
755 static void show_malloc_stats(void);
759 + return receiver_tid != 0 && pthread_self() != receiver_tid;
764 + return receiver_tid != 0 && pthread_self() == receiver_tid;
767 /* Works like waitpid(), but if we already harvested the child pid in our
768 * sigchld_handler(), we succeed instead of returning an error. */
769 pid_t wait_process(pid_t pid, int *status_ptr, int flags)
770 @@ -154,7 +164,7 @@ static void handle_stats(int f)
775 + if (am_generator())
779 @@ -609,12 +619,30 @@ static void do_server_sender(int f_in, i
783 +struct thread_args {
784 + struct file_list *flist;
789 +static void *start_receiver_thread(void *arg)
791 + static int exit_code;
792 + struct thread_args *ta = (struct thread_args *)arg;
794 + recv_files(ta->f_in, ta->flist, ta->local_name);
795 + handle_stats(ta->f_in);
799 + exit_code = log_got_error ? RERR_PARTIAL : 0;
803 static int do_recv(int f_in,int f_out,struct file_list *flist,char *local_name)
809 + struct thread_args args;
811 /* The receiving side mustn't obey this, or an existing symlink that
812 * points to an identical file won't be replaced by the referent. */
813 @@ -623,70 +651,16 @@ static int do_recv(int f_in,int f_out,st
814 if (preserve_hard_links)
817 - if (fd_pair(error_pipe) < 0) {
818 - rsyserr(FERROR, errno, "pipe failed in do_recv");
820 + args.flist = flist;
821 + args.local_name = local_name;
822 + if (pthread_create(&receiver_tid, NULL, start_receiver_thread, &args) < 0) {
823 + rsyserr(FERROR, errno, "pthread_create failed in do_recv");
824 exit_cleanup(RERR_IPC);
827 - io_flush(NORMAL_FLUSH);
829 - if ((pid = do_fork()) == -1) {
830 - rsyserr(FERROR, errno, "fork failed in do_recv");
831 - exit_cleanup(RERR_IPC);
835 - close(error_pipe[0]);
839 - /* we can't let two processes write to the socket at one time */
840 - close_multiplexing_out();
842 - /* set place to send errors */
843 - set_msg_fd_out(error_pipe[1]);
845 - recv_files(f_in, flist, local_name);
846 - io_flush(FULL_FLUSH);
847 - handle_stats(f_in);
849 - send_msg(MSG_DONE, "", 0);
850 - io_flush(FULL_FLUSH);
852 - /* Handle any keep-alive packets from the post-processing work
853 - * that the generator does. */
854 - if (protocol_version >= 29) {
855 - kluge_around_eof = -1;
857 - /* This should only get stopped via a USR2 signal. */
858 - while (read_int(f_in) == flist->count
859 - && read_shortint(f_in) == ITEM_IS_NEW) {}
861 - rprintf(FERROR, "Invalid packet at end of run [%s]\n",
863 - exit_cleanup(RERR_PROTOCOL);
866 - /* Finally, we go to sleep until our parent kills us with a
867 - * USR2 signal. We sleep for a short time, as on some OSes
868 - * a signal won't interrupt a sleep! */
874 - close_multiplexing_in();
875 - if (write_batch && !am_server)
876 - stop_write_batch();
878 - close(error_pipe[1]);
882 io_start_buffering_out();
884 - set_msg_fd_in(error_pipe[0]);
886 generate_files(f_out, flist, local_name);
889 @@ -697,10 +671,13 @@ static int do_recv(int f_in,int f_out,st
891 io_flush(FULL_FLUSH);
894 - kill(pid, SIGUSR2);
895 - wait_process_with_flush(pid, &exit_code);
897 + pthread_join(receiver_tid, &value_ptr);
903 + return *(int*)value_ptr;
907 @@ -1062,22 +1039,6 @@ static int start_client(int argc, char *
912 -static RETSIGTYPE sigusr1_handler(UNUSED(int val))
914 - exit_cleanup(RERR_SIGNAL1);
917 -static RETSIGTYPE sigusr2_handler(UNUSED(int val))
923 - _exit(RERR_PARTIAL);
927 static RETSIGTYPE sigchld_handler(UNUSED(int val))
930 @@ -1160,8 +1121,6 @@ int main(int argc,char *argv[])
931 int orig_argc = argc;
932 char **orig_argv = argv;
934 - signal(SIGUSR1, sigusr1_handler);
935 - signal(SIGUSR2, sigusr2_handler);
936 signal(SIGCHLD, sigchld_handler);
937 #ifdef MAINTAINER_MODE
938 signal(SIGSEGV, rsync_panic_handler);
939 --- orig/match.c 2005-11-10 16:58:36
940 +++ match.c 2005-12-08 23:17:09
944 extern int am_server;
945 -extern int do_progress;
946 +extern int recv_progress;
947 extern int checksum_seed;
948 extern int append_mode;
950 @@ -133,7 +133,7 @@ static void matched(int f, struct sum_st
954 - if (buf && do_progress)
955 + if (buf && recv_progress)
956 show_progress(last_match, buf->file_size);
959 @@ -333,7 +333,7 @@ void match_sums(int f, struct sum_struct
962 for (j = CHUNK_SIZE; j < s->flength; j += CHUNK_SIZE) {
963 - if (buf && do_progress)
964 + if (buf && recv_progress)
965 show_progress(last_match, buf->file_size);
966 sum_update(map_ptr(buf, last_match, CHUNK_SIZE),
968 @@ -341,7 +341,7 @@ void match_sums(int f, struct sum_struct
970 if (last_match < s->flength) {
971 int32 len = s->flength - last_match;
972 - if (buf && do_progress)
973 + if (buf && recv_progress)
974 show_progress(last_match, buf->file_size);
975 sum_update(map_ptr(buf, last_match, len), len);
976 last_match = s->flength;
977 --- orig/options.c 2006-01-31 03:11:30
978 +++ options.c 2005-12-08 23:17:09
979 @@ -73,7 +73,6 @@ int def_compress_level = Z_DEFAULT_COMPR
983 -int am_generator = 0;
984 int am_starting_up = 1;
986 int relative_paths = -1;
987 @@ -94,6 +93,7 @@ int am_daemon = 0;
988 int daemon_over_rsh = 0;
991 +int recv_progress = 0;
992 int keep_partial = 0;
993 int safe_symlinks = 0;
994 int copy_unsafe_links = 0;
995 @@ -1290,6 +1290,7 @@ int parse_arguments(int *argc, const cha
996 if ((do_progress || dry_run) && !verbose && !log_before_transfer
999 + recv_progress = do_progress;
1003 --- orig/pipe.c 2006-01-21 08:03:40
1004 +++ pipe.c 2005-12-08 23:17:09
1005 @@ -56,7 +56,7 @@ pid_t piped_child(char **command, int *f
1006 exit_cleanup(RERR_IPC);
1012 rsyserr(FERROR, errno, "fork");
1013 exit_cleanup(RERR_IPC);
1014 @@ -120,7 +120,7 @@ pid_t local_child(int argc, char **argv,
1015 exit_cleanup(RERR_IPC);
1021 rsyserr(FERROR, errno, "fork");
1022 exit_cleanup(RERR_IPC);
1023 --- orig/receiver.c 2006-01-31 02:30:18
1024 +++ receiver.c 2006-01-14 08:30:29
1025 @@ -24,7 +24,7 @@ extern int verbose;
1026 extern int do_xfers;
1027 extern int am_daemon;
1028 extern int am_server;
1029 -extern int do_progress;
1030 +extern int recv_progress;
1031 extern int log_before_transfer;
1032 extern int log_format_has_i;
1033 extern int daemon_log_format_has_i;
1034 @@ -219,7 +219,7 @@ static int receive_data(int f_in, char *
1036 sum.flength -= sum.blength - sum.remainder;
1037 for (j = CHUNK_SIZE; j < sum.flength; j += CHUNK_SIZE) {
1039 + if (recv_progress)
1040 show_progress(offset, total_size);
1041 sum_update(map_ptr(mapbuf, offset, CHUNK_SIZE),
1043 @@ -227,7 +227,7 @@ static int receive_data(int f_in, char *
1045 if (offset < sum.flength) {
1046 int32 len = sum.flength - offset;
1048 + if (recv_progress)
1049 show_progress(offset, total_size);
1050 sum_update(map_ptr(mapbuf, offset, len), len);
1051 offset = sum.flength;
1052 @@ -240,7 +240,7 @@ static int receive_data(int f_in, char *
1055 while ((i = recv_token(f_in, &data)) != 0) {
1057 + if (recv_progress)
1058 show_progress(offset, total_size);
1061 @@ -308,7 +308,7 @@ static int receive_data(int f_in, char *
1062 ftruncate(fd, offset);
1066 + if (recv_progress)
1067 end_progress(total_size);
1069 if (fd != -1 && offset > 0 && sparse_end(fd) != 0) {
1070 @@ -359,12 +359,12 @@ static void handle_delayed_updates(struc
1071 "rename failed for %s (from %s)",
1072 full_fname(fname), partialptr);
1074 - if (remove_sent_files
1075 - || (preserve_hard_links
1076 - && file->link_u.links)) {
1077 + if (remove_sent_files) {
1078 SIVAL(numbuf, 0, i);
1079 send_msg(MSG_SUCCESS,numbuf,4);
1081 + if (preserve_hard_links && file->link_u.links)
1082 + push_hlink_num(i);
1083 handle_partial_dir(partialptr, PDIR_DELETE);
1086 @@ -415,11 +415,6 @@ int recv_files(int f_in, struct file_lis
1088 rprintf(FINFO,"recv_files(%d) starting\n",flist->count);
1090 - if (flist->hlink_pool) {
1091 - pool_destroy(flist->hlink_pool);
1092 - flist->hlink_pool = NULL;
1096 init_delayed_bits(flist->count);
1098 @@ -440,7 +435,7 @@ int recv_files(int f_in, struct file_lis
1099 rprintf(FINFO, "recv_files phase=%d\n", phase);
1100 if (phase == 2 && delay_updates)
1101 handle_delayed_updates(flist, local_name);
1102 - send_msg(MSG_DONE, "", 0);
1103 + push_redo_num(-2);
1104 if (keep_partial && !partial_dir)
1105 make_backups = 0; /* prevents double backup */
1107 @@ -662,7 +657,7 @@ int recv_files(int f_in, struct file_lis
1108 /* log the transfer */
1109 if (log_before_transfer)
1110 log_item(file, &initial_stats, iflags, NULL);
1111 - else if (!am_server && verbose && do_progress)
1112 + else if (!am_server && verbose && recv_progress)
1113 rprintf(FINFO, "%s\n", fname);
1115 /* recv file data */
1116 @@ -705,11 +700,12 @@ int recv_files(int f_in, struct file_lis
1120 - if (remove_sent_files
1121 - || (preserve_hard_links && file->link_u.links)) {
1122 + if (remove_sent_files) {
1123 SIVAL(numbuf, 0, i);
1124 send_msg(MSG_SUCCESS, numbuf, 4);
1126 + if (preserve_hard_links && file->link_u.links)
1127 + push_hlink_num(i);
1128 } else if (!recv_ok) {
1129 int msgtype = phase || read_batch ? FERROR : FINFO;
1130 if (msgtype == FERROR || verbose) {
1131 @@ -731,10 +727,8 @@ int recv_files(int f_in, struct file_lis
1132 "%s: %s failed verification -- update %s%s.\n",
1133 errstr, fname, keptstr, redostr);
1136 - SIVAL(numbuf, 0, i);
1137 - send_msg(MSG_REDO, numbuf, 4);
1143 make_backups = save_make_backups;
1144 --- orig/rsync.c 2006-01-31 02:30:18
1145 +++ rsync.c 2005-12-08 23:17:10
1146 @@ -32,7 +32,6 @@ extern int orig_umask;
1148 extern int am_server;
1149 extern int am_sender;
1150 -extern int am_generator;
1151 extern int am_starting_up;
1152 extern int preserve_uid;
1153 extern int preserve_gid;
1154 @@ -247,5 +246,5 @@ const char *who_am_i(void)
1157 return am_server ? "server" : "client";
1158 - return am_sender ? "sender" : am_generator ? "generator" : "receiver";
1159 + return am_sender ? "sender" : am_generator() ? "generator" : "receiver";
1161 --- orig/rsync.h 2006-01-30 20:39:09
1162 +++ rsync.h 2006-01-17 02:46:03
1163 @@ -166,10 +166,8 @@ enum msgcode {
1164 MSG_DATA=0, /* raw data on the multiplexed stream */
1165 MSG_ERROR=FERROR, MSG_INFO=FINFO, /* remote logging */
1166 MSG_LOG=FLOG, /* sibling logging */
1167 - MSG_REDO=9, /* reprocess indicated flist index */
1168 MSG_SUCCESS=100,/* successfully updated indicated flist index */
1169 MSG_DELETED=101,/* successfully deleted a file on receiving side */
1170 - MSG_DONE=86 /* current phase is done */
1173 #include "errcode.h"
1174 @@ -320,6 +318,7 @@ enum msgcode {
1178 +#include <pthread.h>
1180 #include "lib/pool_alloc.h"
1182 --- orig/util.c 2006-01-30 07:18:28
1183 +++ util.c 2005-12-08 23:17:10
1184 @@ -413,51 +413,6 @@ int robust_rename(char *from, char *to,
1189 -static pid_t all_pids[10];
1190 -static int num_pids;
1192 -/** Fork and record the pid of the child. **/
1193 -pid_t do_fork(void)
1195 - pid_t newpid = fork();
1197 - if (newpid != 0 && newpid != -1) {
1198 - all_pids[num_pids++] = newpid;
1204 - * Kill all children.
1206 - * @todo It would be kind of nice to make sure that they are actually
1207 - * all our children before we kill them, because their pids may have
1208 - * been recycled by some other process. Perhaps when we wait for a
1209 - * child, we should remove it from this array. Alternatively we could
1210 - * perhaps use process groups, but I think that would not work on
1211 - * ancient Unix versions that don't support them.
1213 -void kill_all(int sig)
1217 - for (i = 0; i < num_pids; i++) {
1218 - /* Let's just be a little careful where we
1219 - * point that gun, hey? See kill(2) for the
1220 - * magic caused by negative values. */
1221 - pid_t p = all_pids[i];
1223 - if (p == getpid())
1233 /** Turn a user name into a uid */
1234 int name_to_uid(char *name, uid_t *uid)