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-02-03 20:00:35
35 +++ cleanup.c 2006-02-03 20:07:44
36 @@ -26,10 +26,6 @@ extern int keep_partial;
37 extern int log_got_error;
38 extern char *partial_dir;
40 -#ifdef HAVE_SIGACTION
41 -static struct sigaction sigact;
45 * Close all open sockets and files, allowing a (somewhat) graceful
46 * shutdown() of socket connections. This eliminates the abortive
47 @@ -98,9 +94,6 @@ void _exit_cleanup(int code, const char
51 - SIGACTION(SIGUSR1, SIG_IGN);
52 - SIGACTION(SIGUSR2, SIG_IGN);
55 rprintf(FINFO,"_exit_cleanup(code=%d, file=%s, line=%d): entered\n",
57 @@ -132,8 +125,6 @@ void _exit_cleanup(int code, const char
60 do_unlink(cleanup_fname);
63 if (cleanup_pid && cleanup_pid == getpid()) {
64 char *pidf = lp_pid_file();
66 --- orig/errcode.h 2005-12-16 23:48:43
67 +++ errcode.h 2005-12-16 23:50:02
69 #define RERR_CRASHED 15 /* sibling crashed */
70 #define RERR_TERMINATED 16 /* sibling terminated abnormally */
72 -#define RERR_SIGNAL1 19 /* status returned when sent SIGUSR1 */
73 #define RERR_SIGNAL 20 /* status returned when sent SIGINT, SIGTERM, SIGHUP */
74 #define RERR_WAITCHILD 21 /* some error returned by waitpid() */
75 #define RERR_MALLOC 22 /* error allocating core memory buffers */
76 --- orig/generator.c 2006-01-31 18:59:39
77 +++ generator.c 2005-12-08 23:17:08
78 @@ -68,7 +68,6 @@ extern OFF_T min_size;
80 extern int allowed_lull;
81 extern int sock_f_out;
82 -extern int ignore_timeout;
83 extern int protocol_version;
84 extern int fuzzy_basis;
85 extern int always_checksum;
86 @@ -99,6 +98,11 @@ static int deletion_count = 0; /* used t
87 static int can_link_symlinks = 1; /* start out optimistic */
88 static int can_link_devices = 1;
90 +/* These vars are local copies so that the receiver can use the originals. */
91 +static int GEN_append_mode;
92 +static int GEN_make_backups;
93 +static int GEN_csum_length;
95 /* For calling delete_file() */
96 #define DEL_FORCE_RECURSE (1<<1) /* recurse even w/o --force */
97 #define DEL_TERSE (1<<3)
98 @@ -448,8 +452,8 @@ static void sum_sizes_sqroot(struct sum_
101 if (protocol_version < 27) {
102 - s2length = csum_length;
103 - } else if (csum_length == SUM_LENGTH) {
104 + s2length = GEN_csum_length;
105 + } else if (GEN_csum_length == SUM_LENGTH) {
106 s2length = SUM_LENGTH;
109 @@ -459,7 +463,7 @@ static void sum_sizes_sqroot(struct sum_
110 for (c = blength; c >>= 1 && b; b--) {}
111 /* add a bit, subtract rollsum, round up. */
112 s2length = (b + 1 - 32 + 7) / 8; /* --optimize in compiler-- */
113 - s2length = MAX(s2length, csum_length);
114 + s2length = MAX(s2length, GEN_csum_length);
115 s2length = MIN(s2length, SUM_LENGTH);
118 @@ -493,7 +497,7 @@ static void generate_and_send_sums(int f
119 sum_sizes_sqroot(&sum, len);
120 write_sum_head(f_out, &sum);
122 - if (append_mode > 0 && f_copy < 0)
123 + if (GEN_append_mode > 0 && f_copy < 0)
127 @@ -512,7 +516,7 @@ static void generate_and_send_sums(int f
130 full_write(f_copy, map, n1);
131 - if (append_mode > 0)
132 + if (GEN_append_mode > 0)
136 @@ -1142,7 +1146,7 @@ static void recv_generator(char *fname,
140 - if (append_mode && st.st_size > file->length)
141 + if (GEN_append_mode && st.st_size > file->length)
144 if (fnamecmp_type <= FNAMECMP_BASIS_DIR_HIGH)
145 @@ -1197,7 +1201,7 @@ static void recv_generator(char *fname,
149 - if (inplace && make_backups && fnamecmp_type == FNAMECMP_FNAME) {
150 + if (inplace && GEN_make_backups && fnamecmp_type == FNAMECMP_FNAME) {
151 if (!(backupptr = get_backup_name(fname))) {
154 @@ -1286,7 +1290,10 @@ void generate_files(int f_out, struct fi
155 int save_ignore_existing = ignore_existing;
156 int save_ignore_non_existing = ignore_non_existing;
157 int save_do_progress = do_progress;
158 - int save_make_backups = make_backups;
159 + int save_make_backups = GEN_make_backups = make_backups;
161 + GEN_append_mode = append_mode;
162 + GEN_csum_length = csum_length;
164 if (protocol_version >= 29) {
166 @@ -1315,7 +1322,7 @@ void generate_files(int f_out, struct fi
167 do_delete_pass(flist);
170 - if (append_mode || whole_file < 0)
171 + if (GEN_append_mode || whole_file < 0)
174 rprintf(FINFO, "delta-transmission %s\n",
175 @@ -1324,12 +1331,6 @@ void generate_files(int f_out, struct fi
179 - /* Since we often fill up the outgoing socket and then just sit around
180 - * waiting for the other 2 processes to do their thing, we don't want
181 - * to exit on a timeout. If the data stops flowing, the receiver will
182 - * notice that and let us know via the redo pipe (or its closing). */
183 - ignore_timeout = 1;
185 for (i = 0; i < flist->count; i++) {
186 struct file_struct *file = flist->files[i];
188 @@ -1373,23 +1374,34 @@ void generate_files(int f_out, struct fi
189 delete_in_dir(NULL, NULL, NULL, NULL);
192 - csum_length = SUM_LENGTH;
193 + GEN_csum_length = SUM_LENGTH; /* csum_length is set by the receiver */
194 max_size = min_size = ignore_existing = ignore_non_existing = 0;
195 update_only = always_checksum = size_only = 0;
197 - if (append_mode) /* resend w/o append mode */
198 - append_mode = -1; /* ... but only longer files */
199 - make_backups = 0; /* avoid a duplicate backup for inplace processing */
200 + if (GEN_append_mode) /* resend w/o append mode */
201 + GEN_append_mode = -1; /* ... but only longer files */
202 + GEN_make_backups = 0; /* avoid a duplicate backup for inplace processing */
205 rprintf(FINFO,"generate_files phase=%d\n",phase);
207 write_int(f_out, -1);
208 + io_flush(NORMAL_FLUSH);
210 /* files can cycle through the system more than once
211 * to catch initial checksum errors */
212 - while ((i = get_redo_num(itemizing, code)) != -1) {
213 - struct file_struct *file = flist->files[i];
215 + struct file_struct *file;
216 + if (preserve_hard_links)
217 + check_for_finished_hlinks(itemizing, code);
218 + if ((i = get_redo_num()) < 0) {
221 + io_flush(NORMAL_FLUSH);
225 + file = flist->files[i];
227 strlcpy(fbuf, local_name, sizeof fbuf);
229 @@ -1401,27 +1413,43 @@ void generate_files(int f_out, struct fi
231 ignore_non_existing = save_ignore_non_existing;
232 ignore_existing = save_ignore_existing;
233 - make_backups = save_make_backups;
234 + GEN_make_backups = save_make_backups;
237 rprintf(FINFO,"generate_files phase=%d\n",phase);
239 write_int(f_out, -1);
240 + io_flush(NORMAL_FLUSH);
242 /* Reduce round-trip lag-time for a useless delay-updates phase. */
243 - if (protocol_version >= 29 && !delay_updates)
244 + if (protocol_version >= 29 && !delay_updates) {
245 write_int(f_out, -1);
246 + io_flush(NORMAL_FLUSH);
249 - /* Read MSG_DONE for the redo phase (and any prior messages). */
250 - get_redo_num(itemizing, code);
251 + /* Read end marker for the redo phase (and any prior messages). */
253 + if (preserve_hard_links)
254 + check_for_finished_hlinks(itemizing, code);
255 + if (get_redo_num() == -2)
257 + io_flush(NORMAL_FLUSH);
261 if (protocol_version >= 29) {
264 rprintf(FINFO, "generate_files phase=%d\n", phase);
266 + if (delay_updates) {
267 write_int(f_out, -1);
268 - /* Read MSG_DONE for delay-updates phase & prior messages. */
269 - get_redo_num(itemizing, code);
270 + io_flush(NORMAL_FLUSH);
272 + /* Read end marker for delay-updates phase & prior messages. */
273 + while (get_redo_num() != -2) {
274 + io_flush(NORMAL_FLUSH);
279 do_progress = save_do_progress;
280 --- orig/io.c 2006-02-04 21:53:39
281 +++ io.c 2006-02-01 19:50:09
282 @@ -47,7 +47,6 @@ extern int allowed_lull;
283 extern int am_server;
284 extern int am_daemon;
285 extern int am_sender;
286 -extern int am_generator;
287 extern int eol_nulls;
288 extern int read_batch;
289 extern int csum_length;
290 @@ -60,7 +59,6 @@ extern struct stats stats;
291 extern struct file_list *the_file_list;
293 const char phase_unknown[] = "unknown";
294 -int ignore_timeout = 0;
296 int batch_gen_fd = -1;
298 @@ -84,7 +82,6 @@ const char *io_read_phase = phase_unknow
299 int kluge_around_eof = 0;
302 -int msg_fd_out = -1;
306 @@ -109,27 +106,32 @@ static int select_timeout = SELECT_TIMEO
307 static void read_loop(int fd, char *buf, size_t len);
309 struct flist_ndx_item {
310 - struct flist_ndx_item *next;
311 + volatile struct flist_ndx_item *next;
315 struct flist_ndx_list {
316 - struct flist_ndx_item *head, *tail;
317 + volatile struct flist_ndx_item *head, *tail;
318 + pthread_mutex_t mutex;
321 -static struct flist_ndx_list redo_list, hlink_list;
322 +static struct flist_ndx_list redo_list = { NULL, NULL, PTHREAD_MUTEX_INITIALIZER };
323 +static struct flist_ndx_list hlink_list = { NULL, NULL, PTHREAD_MUTEX_INITIALIZER };
325 struct msg_list_item {
326 - struct msg_list_item *next;
327 + volatile struct msg_list_item *next;
328 + pthread_mutex_t mutex;
335 - struct msg_list_item *head, *tail;
336 + volatile struct msg_list_item *head, *tail;
337 + pthread_mutex_t mutex;
340 -static struct msg_list msg_list;
341 +static struct msg_list msg_list = { NULL, NULL, PTHREAD_MUTEX_INITIALIZER };
343 static void flist_ndx_push(struct flist_ndx_list *lp, int ndx)
345 @@ -139,27 +141,31 @@ static void flist_ndx_push(struct flist_
346 out_of_memory("flist_ndx_push");
349 + pthread_mutex_lock(&redo_list.mutex);
351 lp->tail->next = item;
355 + pthread_mutex_unlock(&redo_list.mutex);
358 static int flist_ndx_pop(struct flist_ndx_list *lp)
360 - struct flist_ndx_item *next;
361 + struct flist_ndx_item *head, *next;
367 - ndx = lp->head->ndx;
368 - next = lp->head->next;
372 + pthread_mutex_lock(&hlink_list.mutex);
373 + head = (struct flist_ndx_item *)lp->head;
374 + next = (struct flist_ndx_item *)head->next;
376 + if (!(lp->head = next))
378 + pthread_mutex_unlock(&hlink_list.mutex);
383 @@ -168,7 +174,7 @@ static void check_timeout(void)
387 - if (!io_timeout || ignore_timeout)
392 @@ -209,45 +215,40 @@ void set_io_timeout(int secs)
394 /* Setup the fd used to receive MSG_* messages. Only needed during the
395 * early stages of being a local sender (up through the sending of the
396 - * file list) or when we're the generator (to fetch the messages from
397 - * the receiver). */
399 void set_msg_fd_in(int fd)
404 -/* Setup the fd used to send our MSG_* messages. Only needed when
405 - * we're the receiver (to send our messages to the generator). */
406 -void set_msg_fd_out(int fd)
409 - set_nonblocking(msg_fd_out);
412 /* Add a message to the pending MSG_* list. */
413 static void msg_list_add(int code, char *buf, int len)
415 struct msg_list_item *ml;
417 + assert(am_receiver());
418 if (!(ml = new(struct msg_list_item)))
419 out_of_memory("msg_list_add");
421 - if (!(ml->buf = new_array(char, len+4)))
422 + /* NOTE: the "+ 1" allows rwrite() to use the buf! */
423 + if (!(ml->buf = new_array(char, len + 1)))
424 out_of_memory("msg_list_add");
425 - SIVAL(ml->buf, 0, ((code+MPLEX_BASE)<<24) | len);
426 - memcpy(ml->buf+4, buf, len);
428 + memcpy(ml->buf, buf, len);
432 + pthread_mutex_lock(&msg_list.mutex);
434 msg_list.tail->next = ml;
438 + pthread_mutex_unlock(&msg_list.mutex);
441 -/* Read a message from the MSG_* fd and handle it. This is called either
442 +/* Read a message from the MSG_* fd and handle it. This is only called
443 * during the early stages of being a local sender (up through the sending
444 - * of the file list) or when we're the generator (to fetch the messages
445 - * from the receiver). */
446 + * of the file list). */
447 static void read_msg_fd(void)
450 @@ -266,47 +267,6 @@ static void read_msg_fd(void)
451 tag = (tag >> 24) - MPLEX_BASE;
455 - if (len != 0 || !am_generator) {
456 - rprintf(FERROR, "invalid message %d:%d\n", tag, len);
457 - exit_cleanup(RERR_STREAMIO);
459 - flist_ndx_push(&redo_list, -1);
462 - if (len != 4 || !am_generator) {
463 - rprintf(FERROR, "invalid message %d:%d\n", tag, len);
464 - exit_cleanup(RERR_STREAMIO);
466 - read_loop(fd, buf, 4);
467 - flist_ndx_push(&redo_list, IVAL(buf,0));
470 - if (len >= (int)sizeof buf || !am_generator) {
471 - rprintf(FERROR, "invalid message %d:%d\n", tag, len);
472 - exit_cleanup(RERR_STREAMIO);
474 - read_loop(fd, buf, len);
475 - io_multiplex_write(MSG_DELETED, buf, len);
478 - if (len != 4 || !am_generator) {
479 - rprintf(FERROR, "invalid message %d:%d\n", tag, len);
480 - exit_cleanup(RERR_STREAMIO);
482 - read_loop(fd, buf, len);
483 - if (remove_sent_files)
484 - io_multiplex_write(MSG_SUCCESS, buf, len);
485 - if (preserve_hard_links)
486 - flist_ndx_push(&hlink_list, IVAL(buf,0));
489 - if (!am_generator) {
490 - rprintf(FERROR, "invalid message %d:%d\n", tag, len);
491 - exit_cleanup(RERR_STREAMIO);
493 - close_multiplexing_out();
498 @@ -327,71 +287,75 @@ static void read_msg_fd(void)
502 -/* Try to push messages off the list onto the wire. If we leave with more
503 +/* Try to pop messages off the list onto the wire. If we leave with more
504 * to do, return 0. On error, return -1. If everything flushed, return 1.
505 - * This is only active in the receiver. */
506 + * This is only called by the generator. */
507 static int msg_list_flush(int flush_it_all)
509 - static int written = 0;
513 - if (msg_fd_out < 0)
516 + assert(am_generator());
518 while (msg_list.head) {
519 - struct msg_list_item *ml = msg_list.head;
520 - int n = write(msg_fd_out, ml->buf + written, ml->len - written);
522 - if (errno == EINTR)
524 - if (errno != EWOULDBLOCK && errno != EAGAIN)
529 - FD_SET(msg_fd_out, &fds);
530 - tv.tv_sec = select_timeout;
532 - if (!select(msg_fd_out+1, NULL, &fds, NULL, &tv))
534 - } else if ((written += n) == ml->len) {
536 - msg_list.head = ml->next;
537 - if (!msg_list.head)
538 - msg_list.tail = NULL;
541 + struct msg_list_item *ml = (struct msg_list_item *)msg_list.head;
542 + switch (ml->code) {
544 + close_multiplexing_out();
549 + rwrite(ml->code, ml->buf, ml->len);
552 + io_multiplex_write(ml->code, ml->buf, ml->len);
555 + pthread_mutex_lock(&msg_list.mutex);
556 + if (!(msg_list.head = ml->next))
557 + msg_list.tail = NULL;
558 + pthread_mutex_unlock(&msg_list.mutex);
569 void send_msg(enum msgcode code, char *buf, int len)
571 - if (msg_fd_out < 0) {
573 + msg_list_add(code, buf, len);
575 io_multiplex_write(code, buf, len);
578 - msg_list_add(code, buf, len);
579 - msg_list_flush(NORMAL_FLUSH);
582 -int get_redo_num(int itemizing, enum logcode code)
583 +/* This is only used by the receiver. */
584 +void push_redo_num(int ndx)
587 - if (hlink_list.head)
588 - check_for_finished_hlinks(itemizing, code);
589 - if (redo_list.head)
593 + assert(am_receiver());
594 + flist_ndx_push(&redo_list, ndx);
597 +/* This is only used by the generator. */
598 +int get_redo_num(void)
600 + assert(am_generator());
601 return flist_ndx_pop(&redo_list);
604 +/* This is only used by the receiver. */
605 +void push_hlink_num(int ndx)
607 + assert(am_receiver());
608 + flist_ndx_push(&hlink_list, ndx);
611 +/* This is only used by the generator. */
612 int get_hlink_num(void)
614 + assert(am_generator());
615 return flist_ndx_pop(&hlink_list);
618 @@ -471,11 +435,6 @@ static int read_timeout(int fd, char *bu
622 - if (msg_list.head) {
623 - FD_SET(msg_fd_out, &w_fds);
624 - if (msg_fd_out > maxfd)
625 - maxfd = msg_fd_out;
627 if (io_filesfrom_f_out >= 0) {
629 if (io_filesfrom_buflen == 0) {
630 @@ -508,9 +467,6 @@ static int read_timeout(int fd, char *bu
634 - if (msg_list.head && FD_ISSET(msg_fd_out, &w_fds))
635 - msg_list_flush(NORMAL_FLUSH);
637 if (io_filesfrom_f_out >= 0) {
638 if (io_filesfrom_buflen) {
639 if (FD_ISSET(io_filesfrom_f_out, &w_fds)) {
640 @@ -832,6 +788,8 @@ static void readfd(int fd, char *buffer,
643 if (fd == write_batch_monitor_in) {
644 + if (am_generator())
645 + rprintf(FINFO, "writing %d bytes to batch file from generator\n", total);
646 if ((size_t)write(batch_fd, buffer, total) != total)
647 exit_cleanup(RERR_FILEIO);
649 @@ -1087,7 +1045,6 @@ static void writefd_unbuffered(int fd,ch
650 * to grab any messages they sent before they died. */
651 while (fd == sock_f_out && io_multiplexing_in) {
653 - ignore_timeout = 0;
654 readfd_unbuffered(sock_f_in, io_filesfrom_buf,
655 sizeof io_filesfrom_buf);
657 @@ -1097,7 +1054,7 @@ static void writefd_unbuffered(int fd,ch
660 if (fd == sock_f_out) {
661 - if (io_timeout || am_generator)
662 + if (io_timeout || am_generator())
663 last_io_out = time(NULL);
664 sleep_for_bwlimit(cnt);
666 @@ -1121,7 +1078,7 @@ static void mplex_write(enum msgcode cod
667 * cause output to occur down the socket. Setting contiguous_write_len
668 * prevents the reading of msg_fd_in once we actually start to write
669 * this sequence of data (though we might read it before the start). */
670 - if (am_generator && msg_fd_in >= 0)
671 + if (am_generator() && msg_fd_in >= 0)
672 contiguous_write_len = len + 4;
674 if (n > sizeof buffer - 4)
675 @@ -1137,31 +1094,29 @@ static void mplex_write(enum msgcode cod
677 writefd_unbuffered(sock_f_out, buf, len);
679 - if (am_generator && msg_fd_in >= 0)
680 + if (am_generator() && msg_fd_in >= 0)
681 contiguous_write_len = 0;
684 void io_flush(int flush_it_all)
686 - msg_list_flush(flush_it_all);
688 - if (!iobuf_out_cnt || no_flush)
692 - if (io_multiplexing_out)
693 - mplex_write(MSG_DATA, iobuf_out, iobuf_out_cnt);
695 - writefd_unbuffered(sock_f_out, iobuf_out, iobuf_out_cnt);
697 + if (iobuf_out_cnt) {
698 + if (io_multiplexing_out)
699 + mplex_write(MSG_DATA, iobuf_out, iobuf_out_cnt);
701 + writefd_unbuffered(sock_f_out, iobuf_out, iobuf_out_cnt);
705 + if (am_generator())
706 + msg_list_flush(flush_it_all);
709 static void writefd(int fd,char *buf,size_t len)
711 - if (fd == msg_fd_out) {
712 - rprintf(FERROR, "Internal error: wrong write used in receiver.\n");
713 - exit_cleanup(RERR_PROTOCOL);
716 if (fd == sock_f_out)
717 stats.total_written += len;
719 @@ -1374,9 +1329,3 @@ void start_write_batch(int fd)
721 write_batch_monitor_in = fd;
724 -void stop_write_batch(void)
726 - write_batch_monitor_out = -1;
727 - write_batch_monitor_in = -1;
729 --- orig/log.c 2006-02-05 04:53:34
730 +++ log.c 2005-12-16 23:49:57
731 @@ -38,7 +38,6 @@ extern int am_sender;
732 extern int local_server;
734 extern int module_id;
735 -extern int msg_fd_out;
736 extern int protocol_version;
737 extern int preserve_times;
738 extern int log_format_has_i;
739 @@ -74,7 +73,6 @@ struct {
740 { RERR_IPC , "error in IPC code" },
741 { RERR_CRASHED , "sibling process crashed" },
742 { RERR_TERMINATED , "sibling process terminated abnormally" },
743 - { RERR_SIGNAL1 , "received SIGUSR1" },
744 { RERR_SIGNAL , "received SIGINT, SIGTERM, or SIGHUP" },
745 { RERR_WAITCHILD , "waitpid() failed" },
746 { RERR_MALLOC , "error allocating core memory buffers" },
747 @@ -234,8 +232,8 @@ void rwrite(enum logcode code, char *buf
748 if (quiet && code == FINFO)
751 - if (am_server && msg_fd_out >= 0) {
752 - /* Pass the message to our sibling. */
753 + if (am_receiver()) {
754 + /* Pass the message to the generator thread. */
755 send_msg((enum msgcode)code, buf, len);
758 --- orig/main.c 2006-02-05 04:53:34
759 +++ main.c 2006-02-02 02:43:44
760 @@ -30,7 +30,6 @@ extern int list_only;
762 extern int am_server;
763 extern int am_sender;
764 -extern int am_generator;
765 extern int am_daemon;
766 extern int blocking_io;
767 extern int remove_sent_files;
768 @@ -84,9 +83,20 @@ struct pid_status {
770 static time_t starttime, endtime;
771 static int64 total_read, total_written;
772 +static pthread_t receiver_tid;
774 static void show_malloc_stats(void);
778 + return receiver_tid != 0 && pthread_self() != receiver_tid;
783 + return receiver_tid != 0 && pthread_self() == receiver_tid;
786 /* Works like waitpid(), but if we already harvested the child pid in our
787 * sigchld_handler(), we succeed instead of returning an error. */
788 pid_t wait_process(pid_t pid, int *status_ptr, int flags)
789 @@ -163,7 +173,7 @@ static void handle_stats(int f)
794 + if (am_generator())
798 @@ -618,12 +628,30 @@ static void do_server_sender(int f_in, i
802 +struct thread_args {
803 + struct file_list *flist;
808 +static void *start_receiver_thread(void *arg)
810 + static int exit_code;
811 + struct thread_args *ta = (struct thread_args *)arg;
813 + recv_files(ta->f_in, ta->flist, ta->local_name);
814 + handle_stats(ta->f_in);
818 + exit_code = log_got_error ? RERR_PARTIAL : 0;
822 static int do_recv(int f_in,int f_out,struct file_list *flist,char *local_name)
828 + struct thread_args args;
830 /* The receiving side mustn't obey this, or an existing symlink that
831 * points to an identical file won't be replaced by the referent. */
832 @@ -632,70 +660,16 @@ static int do_recv(int f_in,int f_out,st
833 if (preserve_hard_links)
836 - if (fd_pair(error_pipe) < 0) {
837 - rsyserr(FERROR, errno, "pipe failed in do_recv");
839 + args.flist = flist;
840 + args.local_name = local_name;
841 + if (pthread_create(&receiver_tid, NULL, start_receiver_thread, &args) < 0) {
842 + rsyserr(FERROR, errno, "pthread_create failed in do_recv");
843 exit_cleanup(RERR_IPC);
846 - io_flush(NORMAL_FLUSH);
848 - if ((pid = do_fork()) == -1) {
849 - rsyserr(FERROR, errno, "fork failed in do_recv");
850 - exit_cleanup(RERR_IPC);
854 - close(error_pipe[0]);
858 - /* we can't let two processes write to the socket at one time */
859 - close_multiplexing_out();
861 - /* set place to send errors */
862 - set_msg_fd_out(error_pipe[1]);
864 - recv_files(f_in, flist, local_name);
865 - io_flush(FULL_FLUSH);
866 - handle_stats(f_in);
868 - send_msg(MSG_DONE, "", 0);
869 - io_flush(FULL_FLUSH);
871 - /* Handle any keep-alive packets from the post-processing work
872 - * that the generator does. */
873 - if (protocol_version >= 29) {
874 - kluge_around_eof = -1;
876 - /* This should only get stopped via a USR2 signal. */
877 - while (read_int(f_in) == flist->count
878 - && read_shortint(f_in) == ITEM_IS_NEW) {}
880 - rprintf(FERROR, "Invalid packet at end of run [%s]\n",
882 - exit_cleanup(RERR_PROTOCOL);
885 - /* Finally, we go to sleep until our parent kills us with a
886 - * USR2 signal. We sleep for a short time, as on some OSes
887 - * a signal won't interrupt a sleep! */
893 - close_multiplexing_in();
894 - if (write_batch && !am_server)
895 - stop_write_batch();
897 - close(error_pipe[1]);
901 io_start_buffering_out();
903 - set_msg_fd_in(error_pipe[0]);
905 generate_files(f_out, flist, local_name);
908 @@ -706,10 +680,13 @@ static int do_recv(int f_in,int f_out,st
910 io_flush(FULL_FLUSH);
913 - kill(pid, SIGUSR2);
914 - wait_process_with_flush(pid, &exit_code);
916 + pthread_join(receiver_tid, &value_ptr);
922 + return *(int*)value_ptr;
926 @@ -1077,22 +1054,6 @@ static int start_client(int argc, char *
931 -static RETSIGTYPE sigusr1_handler(UNUSED(int val))
933 - exit_cleanup(RERR_SIGNAL1);
936 -static RETSIGTYPE sigusr2_handler(UNUSED(int val))
942 - _exit(RERR_PARTIAL);
946 static RETSIGTYPE sigchld_handler(UNUSED(int val))
949 @@ -1184,8 +1145,6 @@ int main(int argc,char *argv[])
951 sigact.sa_flags = SA_NOCLDSTOP;
953 - SIGACTMASK(SIGUSR1, sigusr1_handler);
954 - SIGACTMASK(SIGUSR2, sigusr2_handler);
955 SIGACTMASK(SIGCHLD, sigchld_handler);
956 #ifdef MAINTAINER_MODE
957 SIGACTMASK(SIGSEGV, rsync_panic_handler);
958 --- orig/match.c 2005-11-10 16:58:36
959 +++ match.c 2005-12-08 23:17:09
963 extern int am_server;
964 -extern int do_progress;
965 +extern int recv_progress;
966 extern int checksum_seed;
967 extern int append_mode;
969 @@ -133,7 +133,7 @@ static void matched(int f, struct sum_st
973 - if (buf && do_progress)
974 + if (buf && recv_progress)
975 show_progress(last_match, buf->file_size);
978 @@ -333,7 +333,7 @@ void match_sums(int f, struct sum_struct
981 for (j = CHUNK_SIZE; j < s->flength; j += CHUNK_SIZE) {
982 - if (buf && do_progress)
983 + if (buf && recv_progress)
984 show_progress(last_match, buf->file_size);
985 sum_update(map_ptr(buf, last_match, CHUNK_SIZE),
987 @@ -341,7 +341,7 @@ void match_sums(int f, struct sum_struct
989 if (last_match < s->flength) {
990 int32 len = s->flength - last_match;
991 - if (buf && do_progress)
992 + if (buf && recv_progress)
993 show_progress(last_match, buf->file_size);
994 sum_update(map_ptr(buf, last_match, len), len);
995 last_match = s->flength;
996 --- orig/options.c 2006-02-03 23:51:57
997 +++ options.c 2005-12-08 23:17:09
998 @@ -73,7 +73,6 @@ int def_compress_level = Z_DEFAULT_COMPR
1002 -int am_generator = 0;
1003 int am_starting_up = 1;
1005 int relative_paths = -1;
1006 @@ -94,6 +93,7 @@ int am_daemon = 0;
1007 int daemon_over_rsh = 0;
1009 int do_progress = 0;
1010 +int recv_progress = 0;
1011 int keep_partial = 0;
1012 int safe_symlinks = 0;
1013 int copy_unsafe_links = 0;
1014 @@ -1290,6 +1290,7 @@ int parse_arguments(int *argc, const cha
1015 if ((do_progress || dry_run) && !verbose && !log_before_transfer
1018 + recv_progress = do_progress;
1022 --- orig/pipe.c 2006-01-21 08:03:40
1023 +++ pipe.c 2005-12-08 23:17:09
1024 @@ -56,7 +56,7 @@ pid_t piped_child(char **command, int *f
1025 exit_cleanup(RERR_IPC);
1031 rsyserr(FERROR, errno, "fork");
1032 exit_cleanup(RERR_IPC);
1033 @@ -120,7 +120,7 @@ pid_t local_child(int argc, char **argv,
1034 exit_cleanup(RERR_IPC);
1040 rsyserr(FERROR, errno, "fork");
1041 exit_cleanup(RERR_IPC);
1042 --- orig/receiver.c 2006-01-31 02:30:18
1043 +++ receiver.c 2006-01-14 08:30:29
1044 @@ -24,7 +24,7 @@ extern int verbose;
1045 extern int do_xfers;
1046 extern int am_daemon;
1047 extern int am_server;
1048 -extern int do_progress;
1049 +extern int recv_progress;
1050 extern int log_before_transfer;
1051 extern int log_format_has_i;
1052 extern int daemon_log_format_has_i;
1053 @@ -219,7 +219,7 @@ static int receive_data(int f_in, char *
1055 sum.flength -= sum.blength - sum.remainder;
1056 for (j = CHUNK_SIZE; j < sum.flength; j += CHUNK_SIZE) {
1058 + if (recv_progress)
1059 show_progress(offset, total_size);
1060 sum_update(map_ptr(mapbuf, offset, CHUNK_SIZE),
1062 @@ -227,7 +227,7 @@ static int receive_data(int f_in, char *
1064 if (offset < sum.flength) {
1065 int32 len = sum.flength - offset;
1067 + if (recv_progress)
1068 show_progress(offset, total_size);
1069 sum_update(map_ptr(mapbuf, offset, len), len);
1070 offset = sum.flength;
1071 @@ -240,7 +240,7 @@ static int receive_data(int f_in, char *
1074 while ((i = recv_token(f_in, &data)) != 0) {
1076 + if (recv_progress)
1077 show_progress(offset, total_size);
1080 @@ -308,7 +308,7 @@ static int receive_data(int f_in, char *
1081 ftruncate(fd, offset);
1085 + if (recv_progress)
1086 end_progress(total_size);
1088 if (fd != -1 && offset > 0 && sparse_end(fd) != 0) {
1089 @@ -359,12 +359,12 @@ static void handle_delayed_updates(struc
1090 "rename failed for %s (from %s)",
1091 full_fname(fname), partialptr);
1093 - if (remove_sent_files
1094 - || (preserve_hard_links
1095 - && file->link_u.links)) {
1096 + if (remove_sent_files) {
1097 SIVAL(numbuf, 0, i);
1098 send_msg(MSG_SUCCESS,numbuf,4);
1100 + if (preserve_hard_links && file->link_u.links)
1101 + push_hlink_num(i);
1102 handle_partial_dir(partialptr, PDIR_DELETE);
1105 @@ -415,11 +415,6 @@ int recv_files(int f_in, struct file_lis
1107 rprintf(FINFO,"recv_files(%d) starting\n",flist->count);
1109 - if (flist->hlink_pool) {
1110 - pool_destroy(flist->hlink_pool);
1111 - flist->hlink_pool = NULL;
1115 init_delayed_bits(flist->count);
1117 @@ -440,7 +435,7 @@ int recv_files(int f_in, struct file_lis
1118 rprintf(FINFO, "recv_files phase=%d\n", phase);
1119 if (phase == 2 && delay_updates)
1120 handle_delayed_updates(flist, local_name);
1121 - send_msg(MSG_DONE, "", 0);
1122 + push_redo_num(-2);
1123 if (keep_partial && !partial_dir)
1124 make_backups = 0; /* prevents double backup */
1126 @@ -662,7 +657,7 @@ int recv_files(int f_in, struct file_lis
1127 /* log the transfer */
1128 if (log_before_transfer)
1129 log_item(file, &initial_stats, iflags, NULL);
1130 - else if (!am_server && verbose && do_progress)
1131 + else if (!am_server && verbose && recv_progress)
1132 rprintf(FINFO, "%s\n", fname);
1134 /* recv file data */
1135 @@ -705,11 +700,12 @@ int recv_files(int f_in, struct file_lis
1139 - if (remove_sent_files
1140 - || (preserve_hard_links && file->link_u.links)) {
1141 + if (remove_sent_files) {
1142 SIVAL(numbuf, 0, i);
1143 send_msg(MSG_SUCCESS, numbuf, 4);
1145 + if (preserve_hard_links && file->link_u.links)
1146 + push_hlink_num(i);
1147 } else if (!recv_ok) {
1148 int msgtype = phase || read_batch ? FERROR : FINFO;
1149 if (msgtype == FERROR || verbose) {
1150 @@ -731,10 +727,8 @@ int recv_files(int f_in, struct file_lis
1151 "%s: %s failed verification -- update %s%s.\n",
1152 errstr, fname, keptstr, redostr);
1155 - SIVAL(numbuf, 0, i);
1156 - send_msg(MSG_REDO, numbuf, 4);
1162 make_backups = save_make_backups;
1163 --- orig/rsync.c 2006-02-05 04:53:34
1164 +++ rsync.c 2005-12-08 23:17:10
1165 @@ -41,7 +41,6 @@ extern int orig_umask;
1167 extern int am_server;
1168 extern int am_sender;
1169 -extern int am_generator;
1170 extern int am_starting_up;
1171 extern int preserve_uid;
1172 extern int preserve_gid;
1173 @@ -288,5 +287,5 @@ const char *who_am_i(void)
1176 return am_server ? "server" : "client";
1177 - return am_sender ? "sender" : am_generator ? "generator" : "receiver";
1178 + return am_sender ? "sender" : am_generator() ? "generator" : "receiver";
1180 --- orig/rsync.h 2006-02-03 20:00:36
1181 +++ rsync.h 2006-01-17 02:46:03
1182 @@ -166,10 +166,8 @@ enum msgcode {
1183 MSG_DATA=0, /* raw data on the multiplexed stream */
1184 MSG_ERROR=FERROR, MSG_INFO=FINFO, /* remote logging */
1185 MSG_LOG=FLOG, MSG_SOCKERR=FSOCKERR, /* sibling logging */
1186 - MSG_REDO=9, /* reprocess indicated flist index */
1187 MSG_SUCCESS=100,/* successfully updated indicated flist index */
1188 MSG_DELETED=101,/* successfully deleted a file on receiving side */
1189 - MSG_DONE=86 /* current phase is done */
1192 #include "errcode.h"
1193 @@ -320,6 +318,7 @@ enum msgcode {
1197 +#include <pthread.h>
1199 #include "lib/pool_alloc.h"
1201 --- orig/util.c 2006-02-03 20:00:36
1202 +++ util.c 2005-12-08 23:17:10
1203 @@ -413,51 +413,6 @@ int robust_rename(char *from, char *to,
1208 -static pid_t all_pids[10];
1209 -static int num_pids;
1211 -/** Fork and record the pid of the child. **/
1212 -pid_t do_fork(void)
1214 - pid_t newpid = fork();
1216 - if (newpid != 0 && newpid != -1) {
1217 - all_pids[num_pids++] = newpid;
1223 - * Kill all children.
1225 - * @todo It would be kind of nice to make sure that they are actually
1226 - * all our children before we kill them, because their pids may have
1227 - * been recycled by some other process. Perhaps when we wait for a
1228 - * child, we should remove it from this array. Alternatively we could
1229 - * perhaps use process groups, but I think that would not work on
1230 - * ancient Unix versions that don't support them.
1232 -void kill_all(int sig)
1236 - for (i = 0; i < num_pids; i++) {
1237 - /* Let's just be a little careful where we
1238 - * point that gun, hey? See kill(2) for the
1239 - * magic caused by negative values. */
1240 - pid_t p = all_pids[i];
1242 - if (p == getpid())
1252 /** Turn a user name into a uid */
1253 int name_to_uid(char *name, uid_t *uid)