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-01 19:37:05
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 @@ -128,8 +125,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 18:59:39
66 +++ generator.c 2005-12-08 23:17:08
67 @@ -68,7 +68,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 @@ -99,6 +98,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 @@ -448,8 +452,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 @@ -459,7 +463,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 @@ -493,7 +497,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 @@ -512,7 +516,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 @@ -1142,7 +1146,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 @@ -1197,7 +1201,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 @@ -1286,7 +1290,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 @@ -1315,7 +1322,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 @@ -1324,12 +1331,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 @@ -1373,23 +1374,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 @@ -1401,27 +1413,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 2006-02-01 19:37:05
270 +++ io.c 2006-02-01 19:50:09
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,47 +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));
478 - if (!am_generator) {
479 - rprintf(FERROR, "invalid message %d:%d\n", tag, len);
480 - exit_cleanup(RERR_STREAMIO);
482 - close_multiplexing_out();
487 @@ -327,71 +287,75 @@ static void read_msg_fd(void)
491 -/* Try to push messages off the list onto the wire. If we leave with more
492 +/* Try to pop messages off the list onto the wire. If we leave with more
493 * to do, return 0. On error, return -1. If everything flushed, return 1.
494 - * This is only active in the receiver. */
495 + * This is only called by the generator. */
496 static int msg_list_flush(int flush_it_all)
498 - static int written = 0;
502 - if (msg_fd_out < 0)
505 + assert(am_generator());
507 while (msg_list.head) {
508 - struct msg_list_item *ml = msg_list.head;
509 - int n = write(msg_fd_out, ml->buf + written, ml->len - written);
511 - if (errno == EINTR)
513 - if (errno != EWOULDBLOCK && errno != EAGAIN)
518 - FD_SET(msg_fd_out, &fds);
519 - tv.tv_sec = select_timeout;
521 - if (!select(msg_fd_out+1, NULL, &fds, NULL, &tv))
523 - } else if ((written += n) == ml->len) {
525 - msg_list.head = ml->next;
526 - if (!msg_list.head)
527 - msg_list.tail = NULL;
530 + struct msg_list_item *ml = (struct msg_list_item *)msg_list.head;
531 + switch (ml->code) {
533 + close_multiplexing_out();
538 + rwrite(ml->code, ml->buf, ml->len);
541 + io_multiplex_write(ml->code, ml->buf, ml->len);
544 + pthread_mutex_lock(&msg_list.mutex);
545 + if (!(msg_list.head = ml->next))
546 + msg_list.tail = NULL;
547 + pthread_mutex_unlock(&msg_list.mutex);
558 void send_msg(enum msgcode code, char *buf, int len)
560 - if (msg_fd_out < 0) {
562 + msg_list_add(code, buf, len);
564 io_multiplex_write(code, buf, len);
567 - msg_list_add(code, buf, len);
568 - msg_list_flush(NORMAL_FLUSH);
571 -int get_redo_num(int itemizing, enum logcode code)
572 +/* This is only used by the receiver. */
573 +void push_redo_num(int ndx)
576 - if (hlink_list.head)
577 - check_for_finished_hlinks(itemizing, code);
578 - if (redo_list.head)
582 + assert(am_receiver());
583 + flist_ndx_push(&redo_list, ndx);
586 +/* This is only used by the generator. */
587 +int get_redo_num(void)
589 + assert(am_generator());
590 return flist_ndx_pop(&redo_list);
593 +/* This is only used by the receiver. */
594 +void push_hlink_num(int ndx)
596 + assert(am_receiver());
597 + flist_ndx_push(&hlink_list, ndx);
600 +/* This is only used by the generator. */
601 int get_hlink_num(void)
603 + assert(am_generator());
604 return flist_ndx_pop(&hlink_list);
607 @@ -471,11 +435,6 @@ static int read_timeout(int fd, char *bu
611 - if (msg_list.head) {
612 - FD_SET(msg_fd_out, &w_fds);
613 - if (msg_fd_out > maxfd)
614 - maxfd = msg_fd_out;
616 if (io_filesfrom_f_out >= 0) {
618 if (io_filesfrom_buflen == 0) {
619 @@ -508,9 +467,6 @@ static int read_timeout(int fd, char *bu
623 - if (msg_list.head && FD_ISSET(msg_fd_out, &w_fds))
624 - msg_list_flush(NORMAL_FLUSH);
626 if (io_filesfrom_f_out >= 0) {
627 if (io_filesfrom_buflen) {
628 if (FD_ISSET(io_filesfrom_f_out, &w_fds)) {
629 @@ -831,6 +787,8 @@ static void readfd(int fd, char *buffer,
632 if (fd == write_batch_monitor_in) {
633 + if (am_generator())
634 + rprintf(FINFO, "writing %d bytes to batch file from generator\n", total);
635 if ((size_t)write(batch_fd, buffer, total) != total)
636 exit_cleanup(RERR_FILEIO);
638 @@ -1086,7 +1044,6 @@ static void writefd_unbuffered(int fd,ch
639 * to grab any messages they sent before they died. */
640 while (fd == sock_f_out && io_multiplexing_in) {
642 - ignore_timeout = 0;
643 readfd_unbuffered(sock_f_in, io_filesfrom_buf,
644 sizeof io_filesfrom_buf);
646 @@ -1096,7 +1053,7 @@ static void writefd_unbuffered(int fd,ch
649 if (fd == sock_f_out) {
650 - if (io_timeout || am_generator)
651 + if (io_timeout || am_generator())
652 last_io_out = time(NULL);
653 sleep_for_bwlimit(cnt);
655 @@ -1120,7 +1077,7 @@ static void mplex_write(enum msgcode cod
656 * cause output to occur down the socket. Setting contiguous_write_len
657 * prevents the reading of msg_fd_in once we actually start to write
658 * this sequence of data (though we might read it before the start). */
659 - if (am_generator && msg_fd_in >= 0)
660 + if (am_generator() && msg_fd_in >= 0)
661 contiguous_write_len = len + 4;
663 if (n > sizeof buffer - 4)
664 @@ -1135,31 +1092,29 @@ static void mplex_write(enum msgcode cod
666 writefd_unbuffered(sock_f_out, buf, len);
668 - if (am_generator && msg_fd_in >= 0)
669 + if (am_generator() && msg_fd_in >= 0)
670 contiguous_write_len = 0;
673 void io_flush(int flush_it_all)
675 - msg_list_flush(flush_it_all);
677 - if (!iobuf_out_cnt || no_flush)
681 - if (io_multiplexing_out)
682 - mplex_write(MSG_DATA, iobuf_out, iobuf_out_cnt);
684 - writefd_unbuffered(sock_f_out, iobuf_out, iobuf_out_cnt);
686 + if (iobuf_out_cnt) {
687 + if (io_multiplexing_out)
688 + mplex_write(MSG_DATA, iobuf_out, iobuf_out_cnt);
690 + writefd_unbuffered(sock_f_out, iobuf_out, iobuf_out_cnt);
694 + if (am_generator())
695 + msg_list_flush(flush_it_all);
698 static void writefd(int fd,char *buf,size_t len)
700 - if (fd == msg_fd_out) {
701 - rprintf(FERROR, "Internal error: wrong write used in receiver.\n");
702 - exit_cleanup(RERR_PROTOCOL);
705 if (fd == sock_f_out)
706 stats.total_written += len;
708 @@ -1372,9 +1327,3 @@ void start_write_batch(int fd)
710 write_batch_monitor_in = fd;
713 -void stop_write_batch(void)
715 - write_batch_monitor_out = -1;
716 - write_batch_monitor_in = -1;
718 --- orig/log.c 2006-02-01 19:37:05
719 +++ log.c 2005-12-16 23:49:57
720 @@ -35,7 +35,6 @@ extern int am_sender;
721 extern int local_server;
723 extern int module_id;
724 -extern int msg_fd_out;
725 extern int protocol_version;
726 extern int preserve_times;
727 extern int log_format_has_i;
728 @@ -68,7 +67,6 @@ struct {
729 { RERR_IPC , "error in IPC code" },
730 { RERR_CRASHED , "sibling process crashed" },
731 { RERR_TERMINATED , "sibling process terminated abnormally" },
732 - { RERR_SIGNAL1 , "received SIGUSR1" },
733 { RERR_SIGNAL , "received SIGINT, SIGTERM, or SIGHUP" },
734 { RERR_WAITCHILD , "waitpid() failed" },
735 { RERR_MALLOC , "error allocating core memory buffers" },
736 @@ -224,8 +222,8 @@ void rwrite(enum logcode code, char *buf
737 if (quiet && code == FINFO)
740 - if (am_server && msg_fd_out >= 0) {
741 - /* Pass the message to our sibling. */
742 + if (am_receiver()) {
743 + /* Pass the message to the generator thread. */
744 send_msg((enum msgcode)code, buf, len);
747 --- orig/main.c 2006-01-15 14:46:15
748 +++ main.c 2005-12-16 23:50:33
749 @@ -30,7 +30,6 @@ extern int list_only;
751 extern int am_server;
752 extern int am_sender;
753 -extern int am_generator;
754 extern int am_daemon;
755 extern int blocking_io;
756 extern int remove_sent_files;
757 @@ -75,9 +74,20 @@ struct pid_status {
759 static time_t starttime, endtime;
760 static int64 total_read, total_written;
761 +static pthread_t receiver_tid;
763 static void show_malloc_stats(void);
767 + return receiver_tid != 0 && pthread_self() != receiver_tid;
772 + return receiver_tid != 0 && pthread_self() == receiver_tid;
775 /* Works like waitpid(), but if we already harvested the child pid in our
776 * sigchld_handler(), we succeed instead of returning an error. */
777 pid_t wait_process(pid_t pid, int *status_ptr, int flags)
778 @@ -154,7 +164,7 @@ static void handle_stats(int f)
783 + if (am_generator())
787 @@ -609,12 +619,30 @@ static void do_server_sender(int f_in, i
791 +struct thread_args {
792 + struct file_list *flist;
797 +static void *start_receiver_thread(void *arg)
799 + static int exit_code;
800 + struct thread_args *ta = (struct thread_args *)arg;
802 + recv_files(ta->f_in, ta->flist, ta->local_name);
803 + handle_stats(ta->f_in);
807 + exit_code = log_got_error ? RERR_PARTIAL : 0;
811 static int do_recv(int f_in,int f_out,struct file_list *flist,char *local_name)
817 + struct thread_args args;
819 /* The receiving side mustn't obey this, or an existing symlink that
820 * points to an identical file won't be replaced by the referent. */
821 @@ -623,70 +651,16 @@ static int do_recv(int f_in,int f_out,st
822 if (preserve_hard_links)
825 - if (fd_pair(error_pipe) < 0) {
826 - rsyserr(FERROR, errno, "pipe failed in do_recv");
828 + args.flist = flist;
829 + args.local_name = local_name;
830 + if (pthread_create(&receiver_tid, NULL, start_receiver_thread, &args) < 0) {
831 + rsyserr(FERROR, errno, "pthread_create failed in do_recv");
832 exit_cleanup(RERR_IPC);
835 - io_flush(NORMAL_FLUSH);
837 - if ((pid = do_fork()) == -1) {
838 - rsyserr(FERROR, errno, "fork failed in do_recv");
839 - exit_cleanup(RERR_IPC);
843 - close(error_pipe[0]);
847 - /* we can't let two processes write to the socket at one time */
848 - close_multiplexing_out();
850 - /* set place to send errors */
851 - set_msg_fd_out(error_pipe[1]);
853 - recv_files(f_in, flist, local_name);
854 - io_flush(FULL_FLUSH);
855 - handle_stats(f_in);
857 - send_msg(MSG_DONE, "", 0);
858 - io_flush(FULL_FLUSH);
860 - /* Handle any keep-alive packets from the post-processing work
861 - * that the generator does. */
862 - if (protocol_version >= 29) {
863 - kluge_around_eof = -1;
865 - /* This should only get stopped via a USR2 signal. */
866 - while (read_int(f_in) == flist->count
867 - && read_shortint(f_in) == ITEM_IS_NEW) {}
869 - rprintf(FERROR, "Invalid packet at end of run [%s]\n",
871 - exit_cleanup(RERR_PROTOCOL);
874 - /* Finally, we go to sleep until our parent kills us with a
875 - * USR2 signal. We sleep for a short time, as on some OSes
876 - * a signal won't interrupt a sleep! */
882 - close_multiplexing_in();
883 - if (write_batch && !am_server)
884 - stop_write_batch();
886 - close(error_pipe[1]);
890 io_start_buffering_out();
892 - set_msg_fd_in(error_pipe[0]);
894 generate_files(f_out, flist, local_name);
897 @@ -697,10 +671,13 @@ static int do_recv(int f_in,int f_out,st
899 io_flush(FULL_FLUSH);
902 - kill(pid, SIGUSR2);
903 - wait_process_with_flush(pid, &exit_code);
905 + pthread_join(receiver_tid, &value_ptr);
911 + return *(int*)value_ptr;
915 @@ -1062,22 +1039,6 @@ static int start_client(int argc, char *
920 -static RETSIGTYPE sigusr1_handler(UNUSED(int val))
922 - exit_cleanup(RERR_SIGNAL1);
925 -static RETSIGTYPE sigusr2_handler(UNUSED(int val))
931 - _exit(RERR_PARTIAL);
935 static RETSIGTYPE sigchld_handler(UNUSED(int val))
938 @@ -1160,8 +1121,6 @@ int main(int argc,char *argv[])
939 int orig_argc = argc;
940 char **orig_argv = argv;
942 - signal(SIGUSR1, sigusr1_handler);
943 - signal(SIGUSR2, sigusr2_handler);
944 signal(SIGCHLD, sigchld_handler);
945 #ifdef MAINTAINER_MODE
946 signal(SIGSEGV, rsync_panic_handler);
947 --- orig/match.c 2005-11-10 16:58:36
948 +++ match.c 2005-12-08 23:17:09
952 extern int am_server;
953 -extern int do_progress;
954 +extern int recv_progress;
955 extern int checksum_seed;
956 extern int append_mode;
958 @@ -133,7 +133,7 @@ static void matched(int f, struct sum_st
962 - if (buf && do_progress)
963 + if (buf && recv_progress)
964 show_progress(last_match, buf->file_size);
967 @@ -333,7 +333,7 @@ void match_sums(int f, struct sum_struct
970 for (j = CHUNK_SIZE; j < s->flength; j += CHUNK_SIZE) {
971 - if (buf && do_progress)
972 + if (buf && recv_progress)
973 show_progress(last_match, buf->file_size);
974 sum_update(map_ptr(buf, last_match, CHUNK_SIZE),
976 @@ -341,7 +341,7 @@ void match_sums(int f, struct sum_struct
978 if (last_match < s->flength) {
979 int32 len = s->flength - last_match;
980 - if (buf && do_progress)
981 + if (buf && recv_progress)
982 show_progress(last_match, buf->file_size);
983 sum_update(map_ptr(buf, last_match, len), len);
984 last_match = s->flength;
985 --- orig/options.c 2006-01-31 03:11:30
986 +++ options.c 2005-12-08 23:17:09
987 @@ -73,7 +73,6 @@ int def_compress_level = Z_DEFAULT_COMPR
991 -int am_generator = 0;
992 int am_starting_up = 1;
994 int relative_paths = -1;
995 @@ -94,6 +93,7 @@ int am_daemon = 0;
996 int daemon_over_rsh = 0;
999 +int recv_progress = 0;
1000 int keep_partial = 0;
1001 int safe_symlinks = 0;
1002 int copy_unsafe_links = 0;
1003 @@ -1290,6 +1290,7 @@ int parse_arguments(int *argc, const cha
1004 if ((do_progress || dry_run) && !verbose && !log_before_transfer
1007 + recv_progress = do_progress;
1011 --- orig/pipe.c 2006-01-21 08:03:40
1012 +++ pipe.c 2005-12-08 23:17:09
1013 @@ -56,7 +56,7 @@ pid_t piped_child(char **command, int *f
1014 exit_cleanup(RERR_IPC);
1020 rsyserr(FERROR, errno, "fork");
1021 exit_cleanup(RERR_IPC);
1022 @@ -120,7 +120,7 @@ pid_t local_child(int argc, char **argv,
1023 exit_cleanup(RERR_IPC);
1029 rsyserr(FERROR, errno, "fork");
1030 exit_cleanup(RERR_IPC);
1031 --- orig/receiver.c 2006-01-31 02:30:18
1032 +++ receiver.c 2006-01-14 08:30:29
1033 @@ -24,7 +24,7 @@ extern int verbose;
1034 extern int do_xfers;
1035 extern int am_daemon;
1036 extern int am_server;
1037 -extern int do_progress;
1038 +extern int recv_progress;
1039 extern int log_before_transfer;
1040 extern int log_format_has_i;
1041 extern int daemon_log_format_has_i;
1042 @@ -219,7 +219,7 @@ static int receive_data(int f_in, char *
1044 sum.flength -= sum.blength - sum.remainder;
1045 for (j = CHUNK_SIZE; j < sum.flength; j += CHUNK_SIZE) {
1047 + if (recv_progress)
1048 show_progress(offset, total_size);
1049 sum_update(map_ptr(mapbuf, offset, CHUNK_SIZE),
1051 @@ -227,7 +227,7 @@ static int receive_data(int f_in, char *
1053 if (offset < sum.flength) {
1054 int32 len = sum.flength - offset;
1056 + if (recv_progress)
1057 show_progress(offset, total_size);
1058 sum_update(map_ptr(mapbuf, offset, len), len);
1059 offset = sum.flength;
1060 @@ -240,7 +240,7 @@ static int receive_data(int f_in, char *
1063 while ((i = recv_token(f_in, &data)) != 0) {
1065 + if (recv_progress)
1066 show_progress(offset, total_size);
1069 @@ -308,7 +308,7 @@ static int receive_data(int f_in, char *
1070 ftruncate(fd, offset);
1074 + if (recv_progress)
1075 end_progress(total_size);
1077 if (fd != -1 && offset > 0 && sparse_end(fd) != 0) {
1078 @@ -359,12 +359,12 @@ static void handle_delayed_updates(struc
1079 "rename failed for %s (from %s)",
1080 full_fname(fname), partialptr);
1082 - if (remove_sent_files
1083 - || (preserve_hard_links
1084 - && file->link_u.links)) {
1085 + if (remove_sent_files) {
1086 SIVAL(numbuf, 0, i);
1087 send_msg(MSG_SUCCESS,numbuf,4);
1089 + if (preserve_hard_links && file->link_u.links)
1090 + push_hlink_num(i);
1091 handle_partial_dir(partialptr, PDIR_DELETE);
1094 @@ -415,11 +415,6 @@ int recv_files(int f_in, struct file_lis
1096 rprintf(FINFO,"recv_files(%d) starting\n",flist->count);
1098 - if (flist->hlink_pool) {
1099 - pool_destroy(flist->hlink_pool);
1100 - flist->hlink_pool = NULL;
1104 init_delayed_bits(flist->count);
1106 @@ -440,7 +435,7 @@ int recv_files(int f_in, struct file_lis
1107 rprintf(FINFO, "recv_files phase=%d\n", phase);
1108 if (phase == 2 && delay_updates)
1109 handle_delayed_updates(flist, local_name);
1110 - send_msg(MSG_DONE, "", 0);
1111 + push_redo_num(-2);
1112 if (keep_partial && !partial_dir)
1113 make_backups = 0; /* prevents double backup */
1115 @@ -662,7 +657,7 @@ int recv_files(int f_in, struct file_lis
1116 /* log the transfer */
1117 if (log_before_transfer)
1118 log_item(file, &initial_stats, iflags, NULL);
1119 - else if (!am_server && verbose && do_progress)
1120 + else if (!am_server && verbose && recv_progress)
1121 rprintf(FINFO, "%s\n", fname);
1123 /* recv file data */
1124 @@ -705,11 +700,12 @@ int recv_files(int f_in, struct file_lis
1128 - if (remove_sent_files
1129 - || (preserve_hard_links && file->link_u.links)) {
1130 + if (remove_sent_files) {
1131 SIVAL(numbuf, 0, i);
1132 send_msg(MSG_SUCCESS, numbuf, 4);
1134 + if (preserve_hard_links && file->link_u.links)
1135 + push_hlink_num(i);
1136 } else if (!recv_ok) {
1137 int msgtype = phase || read_batch ? FERROR : FINFO;
1138 if (msgtype == FERROR || verbose) {
1139 @@ -731,10 +727,8 @@ int recv_files(int f_in, struct file_lis
1140 "%s: %s failed verification -- update %s%s.\n",
1141 errstr, fname, keptstr, redostr);
1144 - SIVAL(numbuf, 0, i);
1145 - send_msg(MSG_REDO, numbuf, 4);
1151 make_backups = save_make_backups;
1152 --- orig/rsync.c 2006-01-31 02:30:18
1153 +++ rsync.c 2005-12-08 23:17:10
1154 @@ -32,7 +32,6 @@ extern int orig_umask;
1156 extern int am_server;
1157 extern int am_sender;
1158 -extern int am_generator;
1159 extern int am_starting_up;
1160 extern int preserve_uid;
1161 extern int preserve_gid;
1162 @@ -247,5 +246,5 @@ const char *who_am_i(void)
1165 return am_server ? "server" : "client";
1166 - return am_sender ? "sender" : am_generator ? "generator" : "receiver";
1167 + return am_sender ? "sender" : am_generator() ? "generator" : "receiver";
1169 --- orig/rsync.h 2006-02-01 19:37:05
1170 +++ rsync.h 2006-01-17 02:46:03
1171 @@ -166,10 +166,8 @@ enum msgcode {
1172 MSG_DATA=0, /* raw data on the multiplexed stream */
1173 MSG_ERROR=FERROR, MSG_INFO=FINFO, /* remote logging */
1174 MSG_LOG=FLOG, MSG_SOCKERR=FSOCKERR, /* sibling logging */
1175 - MSG_REDO=9, /* reprocess indicated flist index */
1176 MSG_SUCCESS=100,/* successfully updated indicated flist index */
1177 MSG_DELETED=101,/* successfully deleted a file on receiving side */
1178 - MSG_DONE=86 /* current phase is done */
1181 #include "errcode.h"
1182 @@ -320,6 +318,7 @@ enum msgcode {
1186 +#include <pthread.h>
1188 #include "lib/pool_alloc.h"
1190 --- orig/util.c 2006-01-30 07:18:28
1191 +++ util.c 2005-12-08 23:17:10
1192 @@ -413,51 +413,6 @@ int robust_rename(char *from, char *to,
1197 -static pid_t all_pids[10];
1198 -static int num_pids;
1200 -/** Fork and record the pid of the child. **/
1201 -pid_t do_fork(void)
1203 - pid_t newpid = fork();
1205 - if (newpid != 0 && newpid != -1) {
1206 - all_pids[num_pids++] = newpid;
1212 - * Kill all children.
1214 - * @todo It would be kind of nice to make sure that they are actually
1215 - * all our children before we kill them, because their pids may have
1216 - * been recycled by some other process. Perhaps when we wait for a
1217 - * child, we should remove it from this array. Alternatively we could
1218 - * perhaps use process groups, but I think that would not work on
1219 - * ancient Unix versions that don't support them.
1221 -void kill_all(int sig)
1225 - for (i = 0; i < num_pids; i++) {
1226 - /* Let's just be a little careful where we
1227 - * point that gun, hey? See kill(2) for the
1228 - * magic caused by negative values. */
1229 - pid_t p = all_pids[i];
1231 - if (p == getpid())
1241 /** Turn a user name into a uid */
1242 int name_to_uid(char *name, uid_t *uid)