1 This patch changes the receiving side to have the receiving code use a thread
2 instead of a forked process. This extra thread does read from the socket, but
3 it sends any stdout/stderr messages to the generator (main thread) to output.
5 ** This is very new code. ** Yes, it passes the "make test" testsuite, but
6 there may still be some problems, especially in some of the untested features.
7 (For one thing, I haven't yet added code to properly handle any keep-alive
8 messages that arrive on the receiving side during the --delete-after phase!)
10 This code just uses pthread.h directly, so configure changes will probably be
11 needed to make this compatible with more systems. I have also tested that
12 this code works fine using the GNU pth library without any code changes if
13 you configured it with --enable-syscall-soft --enable-pthread (you may need
14 to twiddle the Makefile options if you didn't install the library, though).
16 If you try this out, please send some email to wayned@samba.org or the rsync
17 mailing list with your results, build changes, bug reports, etc. Thanks!
19 Be sure to run "make proto" before running "make".
21 --- orig/Makefile.in 2005-11-07 04:29:00
22 +++ Makefile.in 2005-12-10 18:35:39
23 @@ -6,7 +6,7 @@ exec_prefix=@exec_prefix@
28 +LIBS=@LIBS@ -lpthread
32 --- orig/cleanup.c 2005-11-10 16:58:36
33 +++ cleanup.c 2005-12-08 23:17:08
34 @@ -94,9 +94,6 @@ void _exit_cleanup(int code, const char
38 - signal(SIGUSR1, SIG_IGN);
39 - signal(SIGUSR2, SIG_IGN);
42 rprintf(FINFO,"_exit_cleanup(code=%d, file=%s, line=%d): entered\n",
43 code, safe_fname(file), line);
44 @@ -127,8 +124,6 @@ void _exit_cleanup(int code, const char
47 do_unlink(cleanup_fname);
50 if (cleanup_pid && cleanup_pid == getpid()) {
51 char *pidf = lp_pid_file();
53 --- orig/errcode.h 2005-12-16 23:48:43
54 +++ errcode.h 2005-12-16 23:50:02
56 #define RERR_CRASHED 15 /* sibling crashed */
57 #define RERR_TERMINATED 16 /* sibling terminated abnormally */
59 -#define RERR_SIGNAL1 19 /* status returned when sent SIGUSR1 */
60 #define RERR_SIGNAL 20 /* status returned when sent SIGINT, SIGTERM, SIGHUP */
61 #define RERR_WAITCHILD 21 /* some error returned by waitpid() */
62 #define RERR_MALLOC 22 /* error allocating core memory buffers */
63 --- orig/generator.c 2005-12-16 04:03:06
64 +++ generator.c 2005-12-08 23:17:08
65 @@ -65,7 +65,6 @@ extern OFF_T min_size;
67 extern int allowed_lull;
68 extern int sock_f_out;
69 -extern int ignore_timeout;
70 extern int protocol_version;
71 extern int fuzzy_basis;
72 extern int always_checksum;
73 @@ -96,6 +95,11 @@ static int deletion_count = 0; /* used t
74 static int can_link_symlinks = 1; /* start out optimistic */
75 static int can_link_devices = 1;
77 +/* These vars are local copies so that the receiver can use the originals. */
78 +static int GEN_append_mode;
79 +static int GEN_make_backups;
80 +static int GEN_csum_length;
82 /* For calling delete_file() */
83 #define DEL_FORCE_RECURSE (1<<1) /* recurse even w/o --force */
84 #define DEL_TERSE (1<<3)
85 @@ -445,8 +449,8 @@ static void sum_sizes_sqroot(struct sum_
88 if (protocol_version < 27) {
89 - s2length = csum_length;
90 - } else if (csum_length == SUM_LENGTH) {
91 + s2length = GEN_csum_length;
92 + } else if (GEN_csum_length == SUM_LENGTH) {
93 s2length = SUM_LENGTH;
96 @@ -456,7 +460,7 @@ static void sum_sizes_sqroot(struct sum_
97 for (c = blength; c >>= 1 && b; b--) {}
98 /* add a bit, subtract rollsum, round up. */
99 s2length = (b + 1 - 32 + 7) / 8; /* --optimize in compiler-- */
100 - s2length = MAX(s2length, csum_length);
101 + s2length = MAX(s2length, GEN_csum_length);
102 s2length = MIN(s2length, SUM_LENGTH);
105 @@ -490,7 +494,7 @@ static void generate_and_send_sums(int f
106 sum_sizes_sqroot(&sum, len);
107 write_sum_head(f_out, &sum);
109 - if (append_mode > 0 && f_copy < 0)
110 + if (GEN_append_mode > 0 && f_copy < 0)
114 @@ -509,7 +513,7 @@ static void generate_and_send_sums(int f
117 full_write(f_copy, map, n1);
118 - if (append_mode > 0)
119 + if (GEN_append_mode > 0)
123 @@ -1143,7 +1147,7 @@ static void recv_generator(char *fname,
127 - if (append_mode && st.st_size > file->length)
128 + if (GEN_append_mode && st.st_size > file->length)
131 if (fnamecmp_type <= FNAMECMP_BASIS_DIR_HIGH)
132 @@ -1198,7 +1202,7 @@ static void recv_generator(char *fname,
136 - if (inplace && make_backups && fnamecmp_type == FNAMECMP_FNAME) {
137 + if (inplace && GEN_make_backups && fnamecmp_type == FNAMECMP_FNAME) {
138 if (!(backupptr = get_backup_name(fname))) {
141 @@ -1288,7 +1292,10 @@ void generate_files(int f_out, struct fi
142 int save_ignore_existing = ignore_existing;
143 int save_ignore_non_existing = ignore_non_existing;
144 int save_do_progress = do_progress;
145 - int save_make_backups = make_backups;
146 + int save_make_backups = GEN_make_backups = make_backups;
148 + GEN_append_mode = append_mode;
149 + GEN_csum_length = csum_length;
151 if (protocol_version >= 29) {
153 @@ -1317,7 +1324,7 @@ void generate_files(int f_out, struct fi
154 do_delete_pass(flist);
157 - if (append_mode || whole_file < 0)
158 + if (GEN_append_mode || whole_file < 0)
161 rprintf(FINFO, "delta-transmission %s\n",
162 @@ -1326,12 +1333,6 @@ void generate_files(int f_out, struct fi
166 - /* Since we often fill up the outgoing socket and then just sit around
167 - * waiting for the other 2 processes to do their thing, we don't want
168 - * to exit on a timeout. If the data stops flowing, the receiver will
169 - * notice that and let us know via the redo pipe (or its closing). */
170 - ignore_timeout = 1;
172 for (i = 0; i < flist->count; i++) {
173 struct file_struct *file = flist->files[i];
175 @@ -1375,23 +1376,34 @@ void generate_files(int f_out, struct fi
176 delete_in_dir(NULL, NULL, NULL);
179 - csum_length = SUM_LENGTH;
180 + GEN_csum_length = SUM_LENGTH; /* csum_length is set by the receiver */
181 max_size = min_size = ignore_existing = ignore_non_existing = 0;
182 update_only = always_checksum = size_only = 0;
184 - if (append_mode) /* resend w/o append mode */
185 - append_mode = -1; /* ... but only longer files */
186 - make_backups = 0; /* avoid a duplicate backup for inplace processing */
187 + if (GEN_append_mode) /* resend w/o append mode */
188 + GEN_append_mode = -1; /* ... but only longer files */
189 + GEN_make_backups = 0; /* avoid a duplicate backup for inplace processing */
192 rprintf(FINFO,"generate_files phase=%d\n",phase);
194 write_int(f_out, -1);
195 + io_flush(NORMAL_FLUSH);
197 /* files can cycle through the system more than once
198 * to catch initial checksum errors */
199 - while ((i = get_redo_num(itemizing, code)) != -1) {
200 - struct file_struct *file = flist->files[i];
202 + struct file_struct *file;
203 + if (preserve_hard_links)
204 + check_for_finished_hlinks(itemizing, code);
205 + if ((i = get_redo_num()) < 0) {
208 + io_flush(NORMAL_FLUSH);
212 + file = flist->files[i];
214 strlcpy(fbuf, local_name, sizeof fbuf);
216 @@ -1403,27 +1415,43 @@ void generate_files(int f_out, struct fi
218 ignore_non_existing = save_ignore_non_existing;
219 ignore_existing = save_ignore_existing;
220 - make_backups = save_make_backups;
221 + GEN_make_backups = save_make_backups;
224 rprintf(FINFO,"generate_files phase=%d\n",phase);
226 write_int(f_out, -1);
227 + io_flush(NORMAL_FLUSH);
229 /* Reduce round-trip lag-time for a useless delay-updates phase. */
230 - if (protocol_version >= 29 && !delay_updates)
231 + if (protocol_version >= 29 && !delay_updates) {
232 write_int(f_out, -1);
233 + io_flush(NORMAL_FLUSH);
236 - /* Read MSG_DONE for the redo phase (and any prior messages). */
237 - get_redo_num(itemizing, code);
238 + /* Read end marker for the redo phase (and any prior messages). */
240 + if (preserve_hard_links)
241 + check_for_finished_hlinks(itemizing, code);
242 + if (get_redo_num() == -2)
244 + io_flush(NORMAL_FLUSH);
248 if (protocol_version >= 29) {
251 rprintf(FINFO, "generate_files phase=%d\n", phase);
253 + if (delay_updates) {
254 write_int(f_out, -1);
255 - /* Read MSG_DONE for delay-updates phase & prior messages. */
256 - get_redo_num(itemizing, code);
257 + io_flush(NORMAL_FLUSH);
259 + /* Read end marker for delay-updates phase & prior messages. */
260 + while (get_redo_num() != -2) {
261 + io_flush(NORMAL_FLUSH);
266 do_progress = save_do_progress;
267 --- orig/io.c 2005-12-08 21:19:31
268 +++ io.c 2005-12-10 19:03:08
269 @@ -47,7 +47,6 @@ extern int allowed_lull;
270 extern int am_server;
271 extern int am_daemon;
272 extern int am_sender;
273 -extern int am_generator;
274 extern int eol_nulls;
275 extern int read_batch;
276 extern int csum_length;
277 @@ -60,7 +59,6 @@ extern struct stats stats;
278 extern struct file_list *the_file_list;
280 const char phase_unknown[] = "unknown";
281 -int ignore_timeout = 0;
283 int batch_gen_fd = -1;
285 @@ -84,7 +82,6 @@ const char *io_read_phase = phase_unknow
286 int kluge_around_eof = 0;
289 -int msg_fd_out = -1;
293 @@ -109,27 +106,32 @@ static int select_timeout = SELECT_TIMEO
294 static void read_loop(int fd, char *buf, size_t len);
296 struct flist_ndx_item {
297 - struct flist_ndx_item *next;
298 + volatile struct flist_ndx_item *next;
302 struct flist_ndx_list {
303 - struct flist_ndx_item *head, *tail;
304 + volatile struct flist_ndx_item *head, *tail;
305 + pthread_mutex_t mutex;
308 -static struct flist_ndx_list redo_list, hlink_list;
309 +static struct flist_ndx_list redo_list = { NULL, NULL, PTHREAD_MUTEX_INITIALIZER };
310 +static struct flist_ndx_list hlink_list = { NULL, NULL, PTHREAD_MUTEX_INITIALIZER };
312 struct msg_list_item {
313 - struct msg_list_item *next;
314 + volatile struct msg_list_item *next;
315 + pthread_mutex_t mutex;
322 - struct msg_list_item *head, *tail;
323 + volatile struct msg_list_item *head, *tail;
324 + pthread_mutex_t mutex;
327 -static struct msg_list msg_list;
328 +static struct msg_list msg_list = { NULL, NULL, PTHREAD_MUTEX_INITIALIZER };
330 static void flist_ndx_push(struct flist_ndx_list *lp, int ndx)
332 @@ -139,27 +141,31 @@ static void flist_ndx_push(struct flist_
333 out_of_memory("flist_ndx_push");
336 + pthread_mutex_lock(&redo_list.mutex);
338 lp->tail->next = item;
342 + pthread_mutex_unlock(&redo_list.mutex);
345 static int flist_ndx_pop(struct flist_ndx_list *lp)
347 - struct flist_ndx_item *next;
348 + struct flist_ndx_item *head, *next;
354 - ndx = lp->head->ndx;
355 - next = lp->head->next;
359 + pthread_mutex_lock(&hlink_list.mutex);
360 + head = (struct flist_ndx_item *)lp->head;
361 + next = (struct flist_ndx_item *)head->next;
363 + if (!(lp->head = next))
365 + pthread_mutex_unlock(&hlink_list.mutex);
370 @@ -168,7 +174,7 @@ static void check_timeout(void)
374 - if (!io_timeout || ignore_timeout)
379 @@ -209,45 +215,40 @@ void set_io_timeout(int secs)
381 /* Setup the fd used to receive MSG_* messages. Only needed during the
382 * early stages of being a local sender (up through the sending of the
383 - * file list) or when we're the generator (to fetch the messages from
384 - * the receiver). */
386 void set_msg_fd_in(int fd)
391 -/* Setup the fd used to send our MSG_* messages. Only needed when
392 - * we're the receiver (to send our messages to the generator). */
393 -void set_msg_fd_out(int fd)
396 - set_nonblocking(msg_fd_out);
399 /* Add a message to the pending MSG_* list. */
400 static void msg_list_add(int code, char *buf, int len)
402 struct msg_list_item *ml;
404 + assert(am_receiver());
405 if (!(ml = new(struct msg_list_item)))
406 out_of_memory("msg_list_add");
408 - if (!(ml->buf = new_array(char, len+4)))
409 + /* NOTE: the "+ 1" allows rwrite() to use the buf! */
410 + if (!(ml->buf = new_array(char, len + 1)))
411 out_of_memory("msg_list_add");
412 - SIVAL(ml->buf, 0, ((code+MPLEX_BASE)<<24) | len);
413 - memcpy(ml->buf+4, buf, len);
415 + memcpy(ml->buf, buf, len);
419 + pthread_mutex_lock(&msg_list.mutex);
421 msg_list.tail->next = ml;
425 + pthread_mutex_unlock(&msg_list.mutex);
428 -/* Read a message from the MSG_* fd and handle it. This is called either
429 +/* Read a message from the MSG_* fd and handle it. This is only called
430 * during the early stages of being a local sender (up through the sending
431 - * of the file list) or when we're the generator (to fetch the messages
432 - * from the receiver). */
433 + * of the file list). */
434 static void read_msg_fd(void)
437 @@ -266,40 +267,6 @@ static void read_msg_fd(void)
438 tag = (tag >> 24) - MPLEX_BASE;
442 - if (len != 0 || !am_generator) {
443 - rprintf(FERROR, "invalid message %d:%d\n", tag, len);
444 - exit_cleanup(RERR_STREAMIO);
446 - flist_ndx_push(&redo_list, -1);
449 - if (len != 4 || !am_generator) {
450 - rprintf(FERROR, "invalid message %d:%d\n", tag, len);
451 - exit_cleanup(RERR_STREAMIO);
453 - read_loop(fd, buf, 4);
454 - flist_ndx_push(&redo_list, IVAL(buf,0));
457 - if (len >= (int)sizeof buf || !am_generator) {
458 - rprintf(FERROR, "invalid message %d:%d\n", tag, len);
459 - exit_cleanup(RERR_STREAMIO);
461 - read_loop(fd, buf, len);
462 - io_multiplex_write(MSG_DELETED, buf, len);
465 - if (len != 4 || !am_generator) {
466 - rprintf(FERROR, "invalid message %d:%d\n", tag, len);
467 - exit_cleanup(RERR_STREAMIO);
469 - read_loop(fd, buf, len);
470 - if (remove_sent_files)
471 - io_multiplex_write(MSG_SUCCESS, buf, len);
472 - if (preserve_hard_links)
473 - flist_ndx_push(&hlink_list, IVAL(buf,0));
478 @@ -320,71 +287,72 @@ static void read_msg_fd(void)
482 -/* Try to push messages off the list onto the wire. If we leave with more
483 +/* Try to pop messages off the list onto the wire. If we leave with more
484 * to do, return 0. On error, return -1. If everything flushed, return 1.
485 - * This is only active in the receiver. */
486 + * This is only called by the generator. */
487 static int msg_list_flush(int flush_it_all)
489 - static int written = 0;
493 - if (msg_fd_out < 0)
496 + assert(am_generator());
498 while (msg_list.head) {
499 - struct msg_list_item *ml = msg_list.head;
500 - int n = write(msg_fd_out, ml->buf + written, ml->len - written);
502 - if (errno == EINTR)
504 - if (errno != EWOULDBLOCK && errno != EAGAIN)
509 - FD_SET(msg_fd_out, &fds);
510 - tv.tv_sec = select_timeout;
512 - if (!select(msg_fd_out+1, NULL, &fds, NULL, &tv))
514 - } else if ((written += n) == ml->len) {
516 - msg_list.head = ml->next;
517 - if (!msg_list.head)
518 - msg_list.tail = NULL;
521 + struct msg_list_item *ml = (struct msg_list_item *)msg_list.head;
522 + switch (ml->code) {
526 + rwrite(ml->code, ml->buf, ml->len);
529 + io_multiplex_write(ml->code, ml->buf, ml->len);
532 + pthread_mutex_lock(&msg_list.mutex);
533 + if (!(msg_list.head = ml->next))
534 + msg_list.tail = NULL;
535 + pthread_mutex_unlock(&msg_list.mutex);
546 void send_msg(enum msgcode code, char *buf, int len)
548 - if (msg_fd_out < 0) {
550 + msg_list_add(code, buf, len);
552 io_multiplex_write(code, buf, len);
555 - msg_list_add(code, buf, len);
556 - msg_list_flush(NORMAL_FLUSH);
559 -int get_redo_num(int itemizing, enum logcode code)
560 +/* This is only used by the receiver. */
561 +void push_redo_num(int ndx)
564 - if (hlink_list.head)
565 - check_for_finished_hlinks(itemizing, code);
566 - if (redo_list.head)
570 + assert(am_receiver());
571 + flist_ndx_push(&redo_list, ndx);
574 +/* This is only used by the generator. */
575 +int get_redo_num(void)
577 + assert(am_generator());
578 return flist_ndx_pop(&redo_list);
581 +/* This is only used by the receiver. */
582 +void push_hlink_num(int ndx)
584 + assert(am_receiver());
585 + flist_ndx_push(&hlink_list, ndx);
588 +/* This is only used by the generator. */
589 int get_hlink_num(void)
591 + assert(am_generator());
592 return flist_ndx_pop(&hlink_list);
595 @@ -465,11 +433,6 @@ static int read_timeout(int fd, char *bu
599 - if (msg_list.head) {
600 - FD_SET(msg_fd_out, &w_fds);
601 - if (msg_fd_out > maxfd)
602 - maxfd = msg_fd_out;
604 if (io_filesfrom_f_out >= 0) {
606 if (io_filesfrom_buflen == 0) {
607 @@ -502,9 +465,6 @@ static int read_timeout(int fd, char *bu
611 - if (msg_list.head && FD_ISSET(msg_fd_out, &w_fds))
612 - msg_list_flush(NORMAL_FLUSH);
614 if (io_filesfrom_f_out >= 0) {
615 if (io_filesfrom_buflen) {
616 if (FD_ISSET(io_filesfrom_f_out, &w_fds)) {
617 @@ -832,6 +792,8 @@ static void readfd(int fd, char *buffer,
620 if (fd == write_batch_monitor_in) {
621 + if (am_generator())
622 + rprintf(FINFO, "writing %d bytes to batch file from generator\n", total);
623 if ((size_t)write(batch_fd, buffer, total) != total)
624 exit_cleanup(RERR_FILEIO);
626 @@ -1091,7 +1053,6 @@ static void writefd_unbuffered(int fd,ch
627 * to grab any messages they sent before they died. */
628 while (fd == sock_f_out && io_multiplexing_in) {
630 - ignore_timeout = 0;
631 readfd_unbuffered(sock_f_in, io_filesfrom_buf,
632 sizeof io_filesfrom_buf);
634 @@ -1101,7 +1062,7 @@ static void writefd_unbuffered(int fd,ch
637 if (fd == sock_f_out) {
638 - if (io_timeout || am_generator)
639 + if (io_timeout || am_generator())
640 last_io_out = time(NULL);
641 sleep_for_bwlimit(ret);
643 @@ -1126,7 +1087,7 @@ static void mplex_write(enum msgcode cod
644 * cause output to occur down the socket. Setting contiguous_write_len
645 * prevents the reading of msg_fd_in once we actually start to write
646 * this sequence of data (though we might read it before the start). */
647 - if (am_generator && msg_fd_in >= 0)
648 + if (am_generator() && msg_fd_in >= 0)
649 contiguous_write_len = len + 4;
651 if (n > sizeof buffer - 4)
652 @@ -1141,33 +1102,31 @@ static void mplex_write(enum msgcode cod
654 writefd_unbuffered(sock_f_out, buf, len);
656 - if (am_generator && msg_fd_in >= 0)
657 + if (am_generator() && msg_fd_in >= 0)
658 contiguous_write_len = 0;
662 void io_flush(int flush_it_all)
664 - msg_list_flush(flush_it_all);
666 - if (!iobuf_out_cnt || no_flush)
670 - if (io_multiplexing_out)
671 - mplex_write(MSG_DATA, iobuf_out, iobuf_out_cnt);
673 - writefd_unbuffered(sock_f_out, iobuf_out, iobuf_out_cnt);
675 + if (iobuf_out_cnt) {
676 + if (io_multiplexing_out)
677 + mplex_write(MSG_DATA, iobuf_out, iobuf_out_cnt);
679 + writefd_unbuffered(sock_f_out, iobuf_out, iobuf_out_cnt);
683 + if (am_generator())
684 + msg_list_flush(flush_it_all);
688 static void writefd(int fd,char *buf,size_t len)
690 - if (fd == msg_fd_out) {
691 - rprintf(FERROR, "Internal error: wrong write used in receiver.\n");
692 - exit_cleanup(RERR_PROTOCOL);
695 if (fd == sock_f_out)
696 stats.total_written += len;
698 @@ -1387,9 +1346,3 @@ void start_write_batch(int fd)
700 write_batch_monitor_in = fd;
703 -void stop_write_batch(void)
705 - write_batch_monitor_out = -1;
706 - write_batch_monitor_in = -1;
708 --- orig/log.c 2005-12-16 23:48:44
709 +++ log.c 2005-12-16 23:49:57
710 @@ -35,7 +35,6 @@ extern int am_sender;
711 extern int local_server;
713 extern int module_id;
714 -extern int msg_fd_out;
715 extern int protocol_version;
716 extern int preserve_times;
717 extern int log_format_has_i;
718 @@ -68,7 +67,6 @@ struct {
719 { RERR_IPC , "error in IPC code" },
720 { RERR_CRASHED , "sibling process crashed" },
721 { RERR_TERMINATED , "sibling process terminated abnormally" },
722 - { RERR_SIGNAL1 , "received SIGUSR1" },
723 { RERR_SIGNAL , "received SIGINT, SIGTERM, or SIGHUP" },
724 { RERR_WAITCHILD , "waitpid() failed" },
725 { RERR_MALLOC , "error allocating core memory buffers" },
726 @@ -206,8 +204,8 @@ void rwrite(enum logcode code, char *buf
730 - if (am_server && msg_fd_out >= 0) {
731 - /* Pass the message to our sibling. */
732 + if (am_receiver()) {
733 + /* Pass the message to the generator thread. */
734 send_msg((enum msgcode)code, buf, len);
737 --- orig/main.c 2005-12-16 23:48:44
738 +++ main.c 2005-12-16 23:50:33
739 @@ -30,7 +30,6 @@ extern int list_only;
741 extern int am_server;
742 extern int am_sender;
743 -extern int am_generator;
744 extern int am_daemon;
745 extern int blocking_io;
746 extern int remove_sent_files;
747 @@ -75,9 +74,20 @@ struct pid_status {
749 static time_t starttime, endtime;
750 static int64 total_read, total_written;
751 +static pthread_t receiver_tid;
753 static void show_malloc_stats(void);
757 + return receiver_tid != 0 && pthread_self() != receiver_tid;
762 + return receiver_tid != 0 && pthread_self() == receiver_tid;
765 /* Works like waitpid(), but if we already harvested the child pid in our
766 * sigchld_handler(), we succeed instead of returning an error. */
767 pid_t wait_process(pid_t pid, int *status_ptr, int flags)
768 @@ -154,7 +164,7 @@ static void handle_stats(int f)
773 + if (am_generator())
777 @@ -558,12 +568,30 @@ static void do_server_sender(int f_in, i
781 +struct thread_args {
782 + struct file_list *flist;
787 +static void *start_receiver_thread(void *arg)
789 + static int exit_code;
790 + struct thread_args *ta = (struct thread_args *)arg;
792 + recv_files(ta->f_in, ta->flist, ta->local_name);
793 + handle_stats(ta->f_in);
797 + exit_code = log_got_error ? RERR_PARTIAL : 0;
801 static int do_recv(int f_in,int f_out,struct file_list *flist,char *local_name)
807 + struct thread_args args;
809 /* The receiving side mustn't obey this, or an existing symlink that
810 * points to an identical file won't be replaced by the referent. */
811 @@ -572,70 +600,16 @@ static int do_recv(int f_in,int f_out,st
812 if (preserve_hard_links)
815 - if (fd_pair(error_pipe) < 0) {
816 - rsyserr(FERROR, errno, "pipe failed in do_recv");
818 + args.flist = flist;
819 + args.local_name = local_name;
820 + if (pthread_create(&receiver_tid, NULL, start_receiver_thread, &args) < 0) {
821 + rsyserr(FERROR, errno, "pthread_create failed in do_recv");
822 exit_cleanup(RERR_IPC);
825 - io_flush(NORMAL_FLUSH);
827 - if ((pid = do_fork()) == -1) {
828 - rsyserr(FERROR, errno, "fork failed in do_recv");
829 - exit_cleanup(RERR_IPC);
833 - close(error_pipe[0]);
837 - /* we can't let two processes write to the socket at one time */
838 - close_multiplexing_out();
840 - /* set place to send errors */
841 - set_msg_fd_out(error_pipe[1]);
843 - recv_files(f_in, flist, local_name);
844 - io_flush(FULL_FLUSH);
845 - handle_stats(f_in);
847 - send_msg(MSG_DONE, "", 0);
848 - io_flush(FULL_FLUSH);
850 - /* Handle any keep-alive packets from the post-processing work
851 - * that the generator does. */
852 - if (protocol_version >= 29) {
853 - kluge_around_eof = -1;
855 - /* This should only get stopped via a USR2 signal. */
856 - while (read_int(f_in) == flist->count
857 - && read_shortint(f_in) == ITEM_IS_NEW) {}
859 - rprintf(FERROR, "Invalid packet at end of run [%s]\n",
861 - exit_cleanup(RERR_PROTOCOL);
864 - /* Finally, we go to sleep until our parent kills us with a
865 - * USR2 signal. We sleep for a short time, as on some OSes
866 - * a signal won't interrupt a sleep! */
872 - close_multiplexing_in();
873 - if (write_batch && !am_server)
874 - stop_write_batch();
876 - close(error_pipe[1]);
880 io_start_buffering_out();
882 - set_msg_fd_in(error_pipe[0]);
884 generate_files(f_out, flist, local_name);
887 @@ -646,10 +620,13 @@ static int do_recv(int f_in,int f_out,st
889 io_flush(FULL_FLUSH);
892 - kill(pid, SIGUSR2);
893 - wait_process_with_flush(pid, &exit_code);
895 + pthread_join(receiver_tid, &value_ptr);
901 + return *(int*)value_ptr;
905 @@ -1011,22 +988,6 @@ static int start_client(int argc, char *
910 -static RETSIGTYPE sigusr1_handler(UNUSED(int val))
912 - exit_cleanup(RERR_SIGNAL1);
915 -static RETSIGTYPE sigusr2_handler(UNUSED(int val))
921 - _exit(RERR_PARTIAL);
925 static RETSIGTYPE sigchld_handler(UNUSED(int val))
928 @@ -1109,8 +1070,6 @@ int main(int argc,char *argv[])
929 int orig_argc = argc;
930 char **orig_argv = argv;
932 - signal(SIGUSR1, sigusr1_handler);
933 - signal(SIGUSR2, sigusr2_handler);
934 signal(SIGCHLD, sigchld_handler);
935 #ifdef MAINTAINER_MODE
936 signal(SIGSEGV, rsync_panic_handler);
937 --- orig/match.c 2005-11-10 16:58:36
938 +++ match.c 2005-12-08 23:17:09
942 extern int am_server;
943 -extern int do_progress;
944 +extern int recv_progress;
945 extern int checksum_seed;
946 extern int append_mode;
948 @@ -133,7 +133,7 @@ static void matched(int f, struct sum_st
952 - if (buf && do_progress)
953 + if (buf && recv_progress)
954 show_progress(last_match, buf->file_size);
957 @@ -333,7 +333,7 @@ void match_sums(int f, struct sum_struct
960 for (j = CHUNK_SIZE; j < s->flength; j += CHUNK_SIZE) {
961 - if (buf && do_progress)
962 + if (buf && recv_progress)
963 show_progress(last_match, buf->file_size);
964 sum_update(map_ptr(buf, last_match, CHUNK_SIZE),
966 @@ -341,7 +341,7 @@ void match_sums(int f, struct sum_struct
968 if (last_match < s->flength) {
969 int32 len = s->flength - last_match;
970 - if (buf && do_progress)
971 + if (buf && recv_progress)
972 show_progress(last_match, buf->file_size);
973 sum_update(map_ptr(buf, last_match, len), len);
974 last_match = s->flength;
975 --- orig/options.c 2005-12-16 23:48:44
976 +++ options.c 2005-12-08 23:17:09
977 @@ -69,7 +69,6 @@ int def_compress_level = Z_DEFAULT_COMPR
981 -int am_generator = 0;
982 int am_starting_up = 1;
984 int relative_paths = -1;
985 @@ -89,6 +88,7 @@ int am_daemon = 0;
986 int daemon_over_rsh = 0;
989 +int recv_progress = 0;
990 int keep_partial = 0;
991 int safe_symlinks = 0;
992 int copy_unsafe_links = 0;
993 @@ -1236,6 +1236,7 @@ int parse_arguments(int *argc, const cha
994 if ((do_progress || dry_run) && !verbose && !log_before_transfer
997 + recv_progress = do_progress;
1001 --- orig/pipe.c 2005-10-24 21:04:45
1002 +++ pipe.c 2005-12-08 23:17:09
1003 @@ -55,7 +55,7 @@ pid_t piped_child(char **command, int *f
1004 exit_cleanup(RERR_IPC);
1010 rsyserr(FERROR, errno, "fork");
1011 exit_cleanup(RERR_IPC);
1012 @@ -117,7 +117,7 @@ pid_t local_child(int argc, char **argv,
1013 exit_cleanup(RERR_IPC);
1019 rsyserr(FERROR, errno, "fork");
1020 exit_cleanup(RERR_IPC);
1021 --- orig/receiver.c 2005-11-10 16:58:36
1022 +++ receiver.c 2005-12-08 23:17:10
1023 @@ -24,7 +24,7 @@ extern int verbose;
1024 extern int do_xfers;
1025 extern int am_daemon;
1026 extern int am_server;
1027 -extern int do_progress;
1028 +extern int recv_progress;
1029 extern int log_before_transfer;
1030 extern int log_format_has_i;
1031 extern int daemon_log_format_has_i;
1032 @@ -220,7 +220,7 @@ static int receive_data(int f_in, char *
1034 sum.flength -= sum.blength - sum.remainder;
1035 for (j = CHUNK_SIZE; j < sum.flength; j += CHUNK_SIZE) {
1037 + if (recv_progress)
1038 show_progress(offset, total_size);
1039 sum_update(map_ptr(mapbuf, offset, CHUNK_SIZE),
1041 @@ -228,7 +228,7 @@ static int receive_data(int f_in, char *
1043 if (offset < sum.flength) {
1044 int32 len = sum.flength - offset;
1046 + if (recv_progress)
1047 show_progress(offset, total_size);
1048 sum_update(map_ptr(mapbuf, offset, len), len);
1049 offset = sum.flength;
1050 @@ -241,7 +241,7 @@ static int receive_data(int f_in, char *
1053 while ((i = recv_token(f_in, &data)) != 0) {
1055 + if (recv_progress)
1056 show_progress(offset, total_size);
1059 @@ -309,7 +309,7 @@ static int receive_data(int f_in, char *
1060 ftruncate(fd, offset);
1064 + if (recv_progress)
1065 end_progress(total_size);
1067 if (fd != -1 && offset > 0 && sparse_end(fd) != 0) {
1068 @@ -362,14 +362,13 @@ static void handle_delayed_updates(struc
1070 safe_fname(partialptr));
1072 - if (remove_sent_files
1073 - || (preserve_hard_links
1074 - && file->link_u.links)) {
1075 + if (remove_sent_files) {
1076 SIVAL(numbuf, 0, i);
1077 send_msg(MSG_SUCCESS,numbuf,4);
1079 - handle_partial_dir(partialptr,
1081 + if (preserve_hard_links && file->link_u.links)
1082 + push_hlink_num(i);
1083 + handle_partial_dir(partialptr, PDIR_DELETE);
1087 @@ -419,11 +418,6 @@ int recv_files(int f_in, struct file_lis
1089 rprintf(FINFO,"recv_files(%d) starting\n",flist->count);
1091 - if (flist->hlink_pool) {
1092 - pool_destroy(flist->hlink_pool);
1093 - flist->hlink_pool = NULL;
1097 init_delayed_bits(flist->count);
1099 @@ -444,7 +438,7 @@ int recv_files(int f_in, struct file_lis
1100 rprintf(FINFO, "recv_files phase=%d\n", phase);
1101 if (phase == 2 && delay_updates)
1102 handle_delayed_updates(flist, local_name);
1103 - send_msg(MSG_DONE, "", 0);
1104 + push_redo_num(-2);
1105 if (keep_partial && !partial_dir)
1106 make_backups = 0; /* prevents double backup */
1108 @@ -665,7 +659,7 @@ int recv_files(int f_in, struct file_lis
1109 /* log the transfer */
1110 if (log_before_transfer)
1111 log_item(file, &initial_stats, iflags, NULL);
1112 - else if (!am_server && verbose && do_progress)
1113 + else if (!am_server && verbose && recv_progress)
1114 rprintf(FINFO, "%s\n", safe_fname(fname));
1116 /* recv file data */
1117 @@ -705,11 +699,12 @@ int recv_files(int f_in, struct file_lis
1121 - if (remove_sent_files
1122 - || (preserve_hard_links && file->link_u.links)) {
1123 + if (remove_sent_files) {
1124 SIVAL(numbuf, 0, i);
1125 send_msg(MSG_SUCCESS, numbuf, 4);
1127 + if (preserve_hard_links && file->link_u.links)
1128 + push_hlink_num(i);
1129 } else if (!recv_ok) {
1130 int msgtype = phase || read_batch ? FERROR : FINFO;
1131 if (msgtype == FERROR || verbose) {
1132 @@ -732,10 +727,8 @@ int recv_files(int f_in, struct file_lis
1133 errstr, safe_fname(fname),
1137 - SIVAL(numbuf, 0, i);
1138 - send_msg(MSG_REDO, numbuf, 4);
1144 make_backups = save_make_backups;
1145 --- orig/rsync.c 2005-07-27 23:31:12
1146 +++ rsync.c 2005-12-08 23:17:10
1147 @@ -30,7 +30,6 @@ extern int omit_dir_times;
1149 extern int am_server;
1150 extern int am_sender;
1151 -extern int am_generator;
1152 extern int am_starting_up;
1153 extern int preserve_uid;
1154 extern int preserve_gid;
1155 @@ -210,5 +209,5 @@ const char *who_am_i(void)
1158 return am_server ? "server" : "client";
1159 - return am_sender ? "sender" : am_generator ? "generator" : "receiver";
1160 + return am_sender ? "sender" : am_generator() ? "generator" : "receiver";
1162 --- orig/rsync.h 2005-12-15 23:00:49
1163 +++ rsync.h 2005-12-10 19:02:58
1164 @@ -165,10 +165,8 @@ enum msgcode {
1165 MSG_DATA=0, /* raw data on the multiplexed stream */
1166 MSG_ERROR=FERROR, MSG_INFO=FINFO, /* remote logging */
1167 MSG_LOG=FLOG, MSG_FCLIENT=FCLIENT, /* sibling logging */
1168 - MSG_REDO=9, /* reprocess indicated flist index */
1169 MSG_SUCCESS=100,/* successfully updated indicated flist index */
1170 MSG_DELETED=101,/* successfully deleted a file on receiving side */
1171 - MSG_DONE=86 /* current phase is done */
1174 #include "errcode.h"
1175 @@ -319,6 +317,7 @@ enum msgcode {
1179 +#include <pthread.h>
1181 #include "lib/pool_alloc.h"
1183 --- orig/util.c 2005-11-12 20:13:05
1184 +++ util.c 2005-12-08 23:17:10
1185 @@ -405,51 +405,6 @@ int robust_rename(char *from, char *to,
1190 -static pid_t all_pids[10];
1191 -static int num_pids;
1193 -/** Fork and record the pid of the child. **/
1194 -pid_t do_fork(void)
1196 - pid_t newpid = fork();
1198 - if (newpid != 0 && newpid != -1) {
1199 - all_pids[num_pids++] = newpid;
1205 - * Kill all children.
1207 - * @todo It would be kind of nice to make sure that they are actually
1208 - * all our children before we kill them, because their pids may have
1209 - * been recycled by some other process. Perhaps when we wait for a
1210 - * child, we should remove it from this array. Alternatively we could
1211 - * perhaps use process groups, but I think that would not work on
1212 - * ancient Unix versions that don't support them.
1214 -void kill_all(int sig)
1218 - for (i = 0; i < num_pids; i++) {
1219 - /* Let's just be a little careful where we
1220 - * point that gun, hey? See kill(2) for the
1221 - * magic caused by negative values. */
1222 - pid_t p = all_pids[i];
1224 - if (p == getpid())
1234 /** Turn a user name into a uid */
1235 int name_to_uid(char *name, uid_t *uid)