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!
23 @@ -7,7 +7,7 @@ exec_prefix=@exec_prefix@
28 +LIBS=@LIBS@ -lpthread
34 @@ -31,10 +31,6 @@ extern int log_got_error;
35 extern char *partial_dir;
36 extern char *logfile_name;
38 -#ifdef HAVE_SIGACTION
39 -static struct sigaction sigact;
43 * Close all open sockets and files, allowing a (somewhat) graceful
44 * shutdown() of socket connections. This eliminates the abortive
45 @@ -98,9 +94,6 @@ NORETURN void _exit_cleanup(int code, co
46 static int exit_code = 0;
47 static int unmodified_code = 0;
49 - SIGACTION(SIGUSR1, SIG_IGN);
50 - SIGACTION(SIGUSR2, SIG_IGN);
52 if (exit_code) /* Preserve first error code when recursing. */
55 @@ -157,8 +150,6 @@ NORETURN void _exit_cleanup(int code, co
58 do_unlink(cleanup_fname);
61 if (cleanup_pid && cleanup_pid == getpid()) {
62 char *pidf = lp_pid_file();
67 #define RERR_CRASHED 15 /* sibling crashed */
68 #define RERR_TERMINATED 16 /* sibling terminated abnormally */
70 -#define RERR_SIGNAL1 19 /* status returned when sent SIGUSR1 */
71 #define RERR_SIGNAL 20 /* status returned when sent SIGINT, SIGTERM, SIGHUP */
72 #define RERR_WAITCHILD 21 /* some error returned by waitpid() */
73 #define RERR_MALLOC 22 /* error allocating core memory buffers */
76 @@ -66,7 +66,6 @@ extern OFF_T min_size;
78 extern int allowed_lull;
79 extern int sock_f_out;
80 -extern int ignore_timeout;
81 extern int protocol_version;
82 extern int fuzzy_basis;
83 extern int always_checksum;
84 @@ -95,6 +94,11 @@ extern struct filter_list_struct server_
86 static int deletion_count = 0; /* used to implement --max-delete */
88 +/* These vars are local copies so that the receiver can use the originals. */
89 +static int GEN_append_mode;
90 +static int GEN_make_backups;
91 +static int GEN_csum_length;
93 /* For calling delete_file() */
94 #define DEL_FORCE_RECURSE (1<<1) /* recurse even w/o --force */
95 #define DEL_TERSE (1<<3)
96 @@ -445,8 +449,8 @@ static void sum_sizes_sqroot(struct sum_
99 if (protocol_version < 27) {
100 - s2length = csum_length;
101 - } else if (csum_length == SUM_LENGTH) {
102 + s2length = GEN_csum_length;
103 + } else if (GEN_csum_length == SUM_LENGTH) {
104 s2length = SUM_LENGTH;
107 @@ -456,7 +460,7 @@ static void sum_sizes_sqroot(struct sum_
108 for (c = blength; (c >>= 1) && b; b--) {}
109 /* add a bit, subtract rollsum, round up. */
110 s2length = (b + 1 - 32 + 7) / 8; /* --optimize in compiler-- */
111 - s2length = MAX(s2length, csum_length);
112 + s2length = MAX(s2length, GEN_csum_length);
113 s2length = MIN(s2length, SUM_LENGTH);
116 @@ -490,7 +494,7 @@ static void generate_and_send_sums(int f
117 sum_sizes_sqroot(&sum, len);
118 write_sum_head(f_out, &sum);
120 - if (append_mode > 0 && f_copy < 0)
121 + if (GEN_append_mode > 0 && f_copy < 0)
125 @@ -509,7 +513,7 @@ static void generate_and_send_sums(int f
128 full_write(f_copy, map, n1);
129 - if (append_mode > 0)
130 + if (GEN_append_mode > 0)
134 @@ -1205,7 +1209,7 @@ static void recv_generator(char *fname,
138 - if (append_mode && st.st_size > file->length)
139 + if (GEN_append_mode && st.st_size > file->length)
142 if (fnamecmp_type <= FNAMECMP_BASIS_DIR_HIGH)
143 @@ -1268,7 +1272,7 @@ static void recv_generator(char *fname,
147 - if (inplace && make_backups && fnamecmp_type == FNAMECMP_FNAME) {
148 + if (inplace && GEN_make_backups && fnamecmp_type == FNAMECMP_FNAME) {
149 if (!(backupptr = get_backup_name(fname))) {
152 @@ -1359,9 +1363,12 @@ void generate_files(int f_out, struct fi
153 int save_ignore_existing = ignore_existing;
154 int save_ignore_non_existing = ignore_non_existing;
155 int save_do_progress = do_progress;
156 - int save_make_backups = make_backups;
157 + int save_make_backups = GEN_make_backups = make_backups;
158 int dir_tweaking = !(list_only || local_name || dry_run);
160 + GEN_append_mode = append_mode;
161 + GEN_csum_length = csum_length;
163 if (protocol_version >= 29) {
165 maybe_ATTRS_REPORT = stdout_format_has_i ? 0 : ATTRS_REPORT;
166 @@ -1389,7 +1396,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 @@ -1398,12 +1405,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 @@ -1447,23 +1448,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 @@ -1475,27 +1487,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;
282 @@ -40,20 +40,17 @@ 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 extern int checksum_seed;
291 extern int protocol_version;
292 -extern int remove_source_files;
293 extern int preserve_hard_links;
294 extern char *filesfrom_host;
295 extern struct stats stats;
296 extern struct file_list *the_file_list;
298 const char phase_unknown[] = "unknown";
299 -int ignore_timeout = 0;
301 int batch_gen_fd = -1;
303 @@ -61,7 +58,6 @@ int batch_gen_fd = -1;
304 int kluge_around_eof = 0;
307 -int msg_fd_out = -1;
311 @@ -88,27 +84,31 @@ static OFF_T active_bytecnt = 0;
312 static void read_loop(int fd, char *buf, size_t len);
314 struct flist_ndx_item {
315 - struct flist_ndx_item *next;
316 + volatile struct flist_ndx_item *next;
320 struct flist_ndx_list {
321 - struct flist_ndx_item *head, *tail;
322 + volatile struct flist_ndx_item *head, *tail;
323 + pthread_mutex_t mutex;
326 -static struct flist_ndx_list redo_list, hlink_list;
327 +static struct flist_ndx_list redo_list = { NULL, NULL, PTHREAD_MUTEX_INITIALIZER };
328 +static struct flist_ndx_list hlink_list = { NULL, NULL, PTHREAD_MUTEX_INITIALIZER };
330 struct msg_list_item {
331 - struct msg_list_item *next;
332 + volatile struct msg_list_item *next;
339 - struct msg_list_item *head, *tail;
340 + volatile struct msg_list_item *head, *tail;
341 + pthread_mutex_t mutex;
344 -static struct msg_list msg2genr, msg2sndr;
345 +static struct msg_list msg_list = { NULL, NULL, PTHREAD_MUTEX_INITIALIZER };
347 static void flist_ndx_push(struct flist_ndx_list *lp, int ndx)
349 @@ -118,27 +118,31 @@ static void flist_ndx_push(struct flist_
350 out_of_memory("flist_ndx_push");
353 + pthread_mutex_lock(&redo_list.mutex);
355 lp->tail->next = item;
359 + pthread_mutex_unlock(&redo_list.mutex);
362 static int flist_ndx_pop(struct flist_ndx_list *lp)
364 - struct flist_ndx_item *next;
365 + struct flist_ndx_item *head, *next;
371 - ndx = lp->head->ndx;
372 - next = lp->head->next;
376 + pthread_mutex_lock(&hlink_list.mutex);
377 + head = (struct flist_ndx_item *)lp->head;
378 + next = (struct flist_ndx_item *)head->next;
380 + if (!(lp->head = next))
382 + pthread_mutex_unlock(&hlink_list.mutex);
387 @@ -147,7 +151,7 @@ static void check_timeout(void)
391 - if (!io_timeout || ignore_timeout)
396 @@ -188,44 +192,38 @@ void set_io_timeout(int secs)
398 /* Setup the fd used to receive MSG_* messages. Only needed during the
399 * early stages of being a local sender (up through the sending of the
400 - * file list) or when we're the generator (to fetch the messages from
401 - * the receiver). */
403 void set_msg_fd_in(int fd)
408 -/* Setup the fd used to send our MSG_* messages. Only needed when
409 - * we're the receiver (to send our messages to the generator). */
410 -void set_msg_fd_out(int fd)
413 - set_nonblocking(msg_fd_out);
416 /* Add a message to the pending MSG_* list. */
417 -static void msg_list_add(struct msg_list *lst, int code, char *buf, int len)
418 +static void msg_list_add(int code, char *buf, int len)
420 struct msg_list_item *m;
421 - int sz = len + 4 + sizeof m[0] - 1;
422 + int sz = len + sizeof m[0] - 1;
424 + assert(am_receiver());
425 if (!(m = (struct msg_list_item *)new_array(char, sz)))
426 out_of_memory("msg_list_add");
429 - SIVAL(m->buf, 0, ((code+MPLEX_BASE)<<24) | len);
430 - memcpy(m->buf + 4, buf, len);
432 - lst->tail->next = m;
435 + memcpy(m->buf, buf, len);
437 + pthread_mutex_lock(&msg_list.mutex);
439 + msg_list.tail->next = m;
445 + pthread_mutex_unlock(&msg_list.mutex);
448 -/* Read a message from the MSG_* fd and handle it. This is called either
449 +/* Read a message from the MSG_* fd and handle it. This is only called
450 * during the early stages of being a local sender (up through the sending
451 - * of the file list) or when we're the generator (to fetch the messages
452 - * from the receiver). */
453 + * of the file list). */
454 static void read_msg_fd(void)
457 @@ -244,51 +242,6 @@ static void read_msg_fd(void)
458 tag = (tag >> 24) - MPLEX_BASE;
462 - if (len != 0 || !am_generator) {
463 - rprintf(FERROR, "invalid message %d:%d\n", tag, len);
464 - exit_cleanup(RERR_STREAMIO);
466 - flist_ndx_push(&redo_list, -1);
469 - if (len != 4 || !am_generator) {
470 - rprintf(FERROR, "invalid message %d:%d\n", tag, len);
471 - exit_cleanup(RERR_STREAMIO);
473 - read_loop(fd, buf, 4);
474 - if (remove_source_files)
475 - decrement_active_files(IVAL(buf,0));
476 - flist_ndx_push(&redo_list, IVAL(buf,0));
479 - if (len >= (int)sizeof buf || !am_generator) {
480 - rprintf(FERROR, "invalid message %d:%d\n", tag, len);
481 - exit_cleanup(RERR_STREAMIO);
483 - read_loop(fd, buf, len);
484 - send_msg(MSG_DELETED, buf, len);
487 - if (len != 4 || !am_generator) {
488 - rprintf(FERROR, "invalid message %d:%d\n", tag, len);
489 - exit_cleanup(RERR_STREAMIO);
491 - read_loop(fd, buf, len);
492 - if (remove_source_files) {
493 - decrement_active_files(IVAL(buf,0));
494 - send_msg(MSG_SUCCESS, buf, len);
496 - if (preserve_hard_links)
497 - flist_ndx_push(&hlink_list, IVAL(buf,0));
500 - if (!am_generator) {
501 - rprintf(FERROR, "invalid message %d:%d\n", tag, len);
502 - exit_cleanup(RERR_STREAMIO);
504 - close_multiplexing_out();
509 @@ -332,75 +285,80 @@ void decrement_active_files(int ndx)
510 active_bytecnt -= the_file_list->files[ndx]->length;
513 -/* Try to push messages off the list onto the wire. If we leave with more
514 +/* Try to pop messages off the list onto the wire. If we leave with more
515 * to do, return 0. On error, return -1. If everything flushed, return 1.
516 - * This is only active in the receiver. */
517 -static int msg2genr_flush(int flush_it_all)
518 + * This is only called by the generator. */
519 +static void msg_list_flush(void)
521 - static int written = 0;
524 + assert(am_generator());
526 - if (msg_fd_out < 0)
528 + if (defer_forwarding_messages)
531 - while (msg2genr.head) {
532 - struct msg_list_item *m = msg2genr.head;
533 - int n = write(msg_fd_out, m->buf + written, m->len - written);
535 - if (errno == EINTR)
537 - if (errno != EWOULDBLOCK && errno != EAGAIN)
542 - FD_SET(msg_fd_out, &fds);
543 - tv.tv_sec = select_timeout;
545 - if (!select(msg_fd_out+1, NULL, &fds, NULL, &tv))
547 - } else if ((written += n) == m->len) {
548 - msg2genr.head = m->next;
549 - if (!msg2genr.head)
550 - msg2genr.tail = NULL;
554 + defer_forwarding_messages = 1;
555 + while (msg_list.head) {
556 + struct msg_list_item *m = (struct msg_list_item *)msg_list.head;
557 + pthread_mutex_lock(&msg_list.mutex);
558 + if (!(msg_list.head = m->next))
559 + msg_list.tail = NULL;
560 + pthread_mutex_unlock(&msg_list.mutex);
563 + close_multiplexing_out();
568 + rwrite(m->code, m->buf, m->len);
571 + io_multiplex_write(m->code, m->buf, m->len);
577 + defer_forwarding_messages = 0;
581 int send_msg(enum msgcode code, char *buf, int len)
583 - if (msg_fd_out < 0) {
584 + if (!am_receiver()) {
585 if (!defer_forwarding_messages)
586 return io_multiplex_write(code, buf, len);
587 if (!io_multiplexing_out)
589 - msg_list_add(&msg2sndr, code, buf, len);
592 - msg_list_add(&msg2genr, code, buf, len);
593 - msg2genr_flush(NORMAL_FLUSH);
594 + msg_list_add(code, buf, len);
598 -int get_redo_num(int itemizing, enum logcode code)
599 +/* This is only used by the receiver. */
600 +void push_redo_num(int ndx)
603 - if (hlink_list.head)
604 - check_for_finished_hlinks(itemizing, code);
605 - if (redo_list.head)
609 + assert(am_receiver());
610 + flist_ndx_push(&redo_list, ndx);
613 +/* This is only used by the generator. */
614 +int get_redo_num(void)
616 + assert(am_generator());
617 return flist_ndx_pop(&redo_list);
620 +/* This is only used by the receiver. */
621 +void push_hlink_num(int ndx)
623 + assert(am_receiver());
624 + flist_ndx_push(&hlink_list, ndx);
627 +/* This is only used by the generator. */
628 int get_hlink_num(void)
630 + assert(am_generator());
631 return flist_ndx_pop(&hlink_list);
634 @@ -480,11 +438,6 @@ static int read_timeout(int fd, char *bu
638 - if (msg2genr.head) {
639 - FD_SET(msg_fd_out, &w_fds);
640 - if (msg_fd_out > maxfd)
641 - maxfd = msg_fd_out;
643 if (io_filesfrom_f_out >= 0) {
645 if (io_filesfrom_buflen == 0) {
646 @@ -517,9 +470,6 @@ static int read_timeout(int fd, char *bu
650 - if (msg2genr.head && FD_ISSET(msg_fd_out, &w_fds))
651 - msg2genr_flush(NORMAL_FLUSH);
653 if (io_filesfrom_f_out >= 0) {
654 if (io_filesfrom_buflen) {
655 if (FD_ISSET(io_filesfrom_f_out, &w_fds)) {
656 @@ -847,6 +797,8 @@ static void readfd(int fd, char *buffer,
659 if (fd == write_batch_monitor_in) {
660 + if (am_generator())
661 + rprintf(FINFO, "writing %d bytes to batch file from generator\n", total);
662 if ((size_t)write(batch_fd, buffer, total) != total)
663 exit_cleanup(RERR_FILEIO);
665 @@ -1115,7 +1067,6 @@ static void writefd_unbuffered(int fd,ch
666 * to grab any messages they sent before they died. */
667 while (fd == sock_f_out && io_multiplexing_in) {
669 - ignore_timeout = 0;
670 readfd_unbuffered(sock_f_in, io_filesfrom_buf,
671 sizeof io_filesfrom_buf);
673 @@ -1126,7 +1077,7 @@ static void writefd_unbuffered(int fd,ch
674 defer_forwarding_messages = 1;
676 if (fd == sock_f_out) {
677 - if (io_timeout || am_generator)
678 + if (io_timeout || am_generator())
679 last_io_out = time(NULL);
680 sleep_for_bwlimit(cnt);
682 @@ -1136,23 +1087,6 @@ static void writefd_unbuffered(int fd,ch
686 -static void msg2sndr_flush(void)
688 - if (defer_forwarding_messages)
691 - while (msg2sndr.head && io_multiplexing_out) {
692 - struct msg_list_item *m = msg2sndr.head;
693 - if (!(msg2sndr.head = m->next))
694 - msg2sndr.tail = NULL;
695 - stats.total_written += m->len;
696 - defer_forwarding_messages = 1;
697 - writefd_unbuffered(sock_f_out, m->buf, m->len);
698 - defer_forwarding_messages = 0;
704 * Write an message to a multiplexed stream. If this fails then rsync
706 @@ -1178,14 +1112,15 @@ static void mplex_write(enum msgcode cod
707 defer_forwarding_messages = 1;
708 writefd_unbuffered(sock_f_out, buf, len);
709 defer_forwarding_messages = 0;
711 + if (am_generator())
716 -void io_flush(int flush_it_all)
717 +void io_flush(UNUSED(int flush_it_all))
719 - msg2genr_flush(flush_it_all);
721 + if (am_generator())
724 if (!iobuf_out_cnt || no_flush)
726 @@ -1199,11 +1134,6 @@ void io_flush(int flush_it_all)
728 static void writefd(int fd,char *buf,size_t len)
730 - if (fd == msg_fd_out) {
731 - rprintf(FERROR, "Internal error: wrong write used in receiver.\n");
732 - exit_cleanup(RERR_PROTOCOL);
735 if (fd == sock_f_out)
736 stats.total_written += len;
738 @@ -1409,9 +1339,3 @@ void start_write_batch(int fd)
740 write_batch_monitor_in = fd;
743 -void stop_write_batch(void)
745 - write_batch_monitor_out = -1;
746 - write_batch_monitor_in = -1;
750 @@ -33,7 +33,6 @@ extern int am_sender;
751 extern int local_server;
753 extern int module_id;
754 -extern int msg_fd_out;
755 extern int allow_8bit_chars;
756 extern int protocol_version;
757 extern int preserve_times;
758 @@ -75,7 +74,6 @@ struct {
759 { RERR_IPC , "error in IPC code" },
760 { RERR_CRASHED , "sibling process crashed" },
761 { RERR_TERMINATED , "sibling process terminated abnormally" },
762 - { RERR_SIGNAL1 , "received SIGUSR1" },
763 { RERR_SIGNAL , "received SIGINT, SIGTERM, or SIGHUP" },
764 { RERR_WAITCHILD , "waitpid() failed" },
765 { RERR_MALLOC , "error allocating core memory buffers" },
766 @@ -241,8 +239,8 @@ void rwrite(enum logcode code, char *buf
768 exit_cleanup(RERR_MESSAGEIO);
770 - if (am_server && msg_fd_out >= 0) {
771 - /* Pass the message to our sibling. */
772 + if (am_receiver()) {
773 + /* Pass the message to the generator thread. */
774 send_msg((enum msgcode)code, buf, len);
779 @@ -32,7 +32,6 @@ extern int list_only;
781 extern int am_server;
782 extern int am_sender;
783 -extern int am_generator;
784 extern int am_daemon;
785 extern int blocking_io;
786 extern int remove_source_files;
787 @@ -96,9 +95,20 @@ struct pid_status {
789 static time_t starttime, endtime;
790 static int64 total_read, total_written;
791 +static pthread_t receiver_tid;
793 static void show_malloc_stats(void);
797 + return receiver_tid != 0 && pthread_self() != receiver_tid;
802 + return receiver_tid != 0 && pthread_self() == receiver_tid;
805 /* Works like waitpid(), but if we already harvested the child pid in our
806 * remember_children(), we succeed instead of returning an error. */
807 pid_t wait_process(pid_t pid, int *status_ptr, int flags)
808 @@ -175,7 +185,7 @@ static void handle_stats(int f)
813 + if (am_generator())
817 @@ -683,12 +693,30 @@ static void do_server_sender(int f_in, i
821 +struct thread_args {
822 + struct file_list *flist;
827 +static void *start_receiver_thread(void *arg)
829 + static int exit_code;
830 + struct thread_args *ta = (struct thread_args *)arg;
832 + recv_files(ta->f_in, ta->flist, ta->local_name);
833 + handle_stats(ta->f_in);
837 + exit_code = log_got_error ? RERR_PARTIAL : 0;
841 static int do_recv(int f_in,int f_out,struct file_list *flist,char *local_name)
847 + struct thread_args args;
849 /* The receiving side mustn't obey this, or an existing symlink that
850 * points to an identical file won't be replaced by the referent. */
851 @@ -697,70 +725,16 @@ static int do_recv(int f_in,int f_out,st
852 if (preserve_hard_links)
855 - if (fd_pair(error_pipe) < 0) {
856 - rsyserr(FERROR, errno, "pipe failed in do_recv");
858 + args.flist = flist;
859 + args.local_name = local_name;
860 + if (pthread_create(&receiver_tid, NULL, start_receiver_thread, &args) < 0) {
861 + rsyserr(FERROR, errno, "pthread_create failed in do_recv");
862 exit_cleanup(RERR_IPC);
865 - io_flush(NORMAL_FLUSH);
867 - if ((pid = do_fork()) == -1) {
868 - rsyserr(FERROR, errno, "fork failed in do_recv");
869 - exit_cleanup(RERR_IPC);
873 - close(error_pipe[0]);
877 - /* we can't let two processes write to the socket at one time */
878 - close_multiplexing_out();
880 - /* set place to send errors */
881 - set_msg_fd_out(error_pipe[1]);
883 - recv_files(f_in, flist, local_name);
884 - io_flush(FULL_FLUSH);
885 - handle_stats(f_in);
887 - send_msg(MSG_DONE, "", 0);
888 - io_flush(FULL_FLUSH);
890 - /* Handle any keep-alive packets from the post-processing work
891 - * that the generator does. */
892 - if (protocol_version >= 29) {
893 - kluge_around_eof = -1;
895 - /* This should only get stopped via a USR2 signal. */
896 - while (read_int(f_in) == flist->count
897 - && read_shortint(f_in) == ITEM_IS_NEW) {}
899 - rprintf(FERROR, "Invalid packet at end of run [%s]\n",
901 - exit_cleanup(RERR_PROTOCOL);
904 - /* Finally, we go to sleep until our parent kills us with a
905 - * USR2 signal. We sleep for a short time, as on some OSes
906 - * a signal won't interrupt a sleep! */
912 - close_multiplexing_in();
913 - if (write_batch && !am_server)
914 - stop_write_batch();
916 - close(error_pipe[1]);
920 io_start_buffering_out();
922 - set_msg_fd_in(error_pipe[0]);
924 generate_files(f_out, flist, local_name);
927 @@ -771,10 +745,13 @@ static int do_recv(int f_in,int f_out,st
929 io_flush(FULL_FLUSH);
932 - kill(pid, SIGUSR2);
933 - wait_process_with_flush(pid, &exit_code);
935 + pthread_join(receiver_tid, &value_ptr);
941 + return *(int*)value_ptr;
945 @@ -1176,22 +1153,6 @@ static int start_client(int argc, char *
950 -static RETSIGTYPE sigusr1_handler(UNUSED(int val))
952 - exit_cleanup(RERR_SIGNAL1);
955 -static RETSIGTYPE sigusr2_handler(UNUSED(int val))
961 - _exit(RERR_PARTIAL);
965 RETSIGTYPE remember_children(UNUSED(int val))
968 @@ -1283,8 +1244,6 @@ int main(int argc,char *argv[])
970 sigact.sa_flags = SA_NOCLDSTOP;
972 - SIGACTMASK(SIGUSR1, sigusr1_handler);
973 - SIGACTMASK(SIGUSR2, sigusr2_handler);
974 SIGACTMASK(SIGCHLD, remember_children);
975 #ifdef MAINTAINER_MODE
976 SIGACTMASK(SIGSEGV, rsync_panic_handler);
983 -extern int do_progress;
984 +extern int recv_progress;
985 extern int checksum_seed;
986 extern int append_mode;
988 @@ -113,7 +113,7 @@ static void matched(int f, struct sum_st
992 - if (buf && do_progress)
993 + if (buf && recv_progress)
994 show_progress(last_match, buf->file_size);
997 @@ -317,7 +317,7 @@ void match_sums(int f, struct sum_struct
1000 for (j = CHUNK_SIZE; j < s->flength; j += CHUNK_SIZE) {
1001 - if (buf && do_progress)
1002 + if (buf && recv_progress)
1003 show_progress(last_match, buf->file_size);
1004 sum_update(map_ptr(buf, last_match, CHUNK_SIZE),
1006 @@ -325,7 +325,7 @@ void match_sums(int f, struct sum_struct
1008 if (last_match < s->flength) {
1009 int32 len = s->flength - last_match;
1010 - if (buf && do_progress)
1011 + if (buf && recv_progress)
1012 show_progress(last_match, buf->file_size);
1013 sum_update(map_ptr(buf, last_match, len), len);
1014 last_match = s->flength;
1017 @@ -74,7 +74,6 @@ int def_compress_level = Z_DEFAULT_COMPR
1021 -int am_generator = 0;
1022 int am_starting_up = 1;
1023 int relative_paths = -1;
1024 int implied_dirs = 1;
1025 @@ -95,6 +94,7 @@ int am_daemon = 0;
1026 int daemon_over_rsh = 0;
1028 int do_progress = 0;
1029 +int recv_progress = 0;
1030 int keep_partial = 0;
1031 int safe_symlinks = 0;
1032 int copy_unsafe_links = 0;
1033 @@ -1302,6 +1302,7 @@ int parse_arguments(int *argc, const cha
1035 if (do_progress && !verbose && !log_before_transfer && !am_server)
1037 + recv_progress = do_progress;
1043 @@ -59,7 +59,7 @@ pid_t piped_child(char **command, int *f
1044 exit_cleanup(RERR_IPC);
1050 rsyserr(FERROR, errno, "fork");
1051 exit_cleanup(RERR_IPC);
1052 @@ -123,7 +123,7 @@ pid_t local_child(int argc, char **argv,
1053 exit_cleanup(RERR_IPC);
1059 rsyserr(FERROR, errno, "fork");
1060 exit_cleanup(RERR_IPC);
1065 extern int do_xfers;
1066 extern int am_server;
1067 -extern int do_progress;
1068 +extern int recv_progress;
1069 extern int log_before_transfer;
1070 extern int stdout_format_has_i;
1071 extern int logfile_format_has_i;
1072 @@ -157,7 +157,7 @@ static int receive_data(int f_in, char *
1074 sum.flength -= sum.blength - sum.remainder;
1075 for (j = CHUNK_SIZE; j < sum.flength; j += CHUNK_SIZE) {
1077 + if (recv_progress)
1078 show_progress(offset, total_size);
1079 sum_update(map_ptr(mapbuf, offset, CHUNK_SIZE),
1081 @@ -165,7 +165,7 @@ static int receive_data(int f_in, char *
1083 if (offset < sum.flength) {
1084 int32 len = sum.flength - offset;
1086 + if (recv_progress)
1087 show_progress(offset, total_size);
1088 sum_update(map_ptr(mapbuf, offset, len), len);
1089 offset = sum.flength;
1090 @@ -178,7 +178,7 @@ static int receive_data(int f_in, char *
1093 while ((i = recv_token(f_in, &data)) != 0) {
1095 + if (recv_progress)
1096 show_progress(offset, total_size);
1099 @@ -248,7 +248,7 @@ static int receive_data(int f_in, char *
1100 ftruncate(fd, offset);
1104 + if (recv_progress)
1105 end_progress(total_size);
1107 if (fd != -1 && offset > 0 && sparse_end(fd) != 0) {
1108 @@ -299,12 +299,12 @@ static void handle_delayed_updates(struc
1109 "rename failed for %s (from %s)",
1110 full_fname(fname), partialptr);
1112 - if (remove_source_files
1113 - || (preserve_hard_links
1114 - && file->link_u.links)) {
1115 + if (remove_source_files) {
1116 SIVAL(numbuf, 0, i);
1117 send_msg(MSG_SUCCESS,numbuf,4);
1119 + if (preserve_hard_links && file->link_u.links)
1120 + push_hlink_num(i);
1121 handle_partial_dir(partialptr, PDIR_DELETE);
1124 @@ -355,11 +355,6 @@ int recv_files(int f_in, struct file_lis
1126 rprintf(FINFO,"recv_files(%d) starting\n",flist->count);
1128 - if (flist->hlink_pool) {
1129 - pool_destroy(flist->hlink_pool);
1130 - flist->hlink_pool = NULL;
1134 delayed_bits = bitbag_create(flist->count);
1136 @@ -382,7 +377,7 @@ int recv_files(int f_in, struct file_lis
1137 rprintf(FINFO, "recv_files phase=%d\n", phase);
1138 if (phase == 2 && delay_updates)
1139 handle_delayed_updates(flist, local_name);
1140 - send_msg(MSG_DONE, "", 0);
1141 + push_redo_num(-2);
1142 if (keep_partial && !partial_dir)
1143 make_backups = 0; /* prevents double backup */
1145 @@ -607,7 +602,7 @@ int recv_files(int f_in, struct file_lis
1146 /* log the transfer */
1147 if (log_before_transfer)
1148 log_item(FCLIENT, file, &initial_stats, iflags, NULL);
1149 - else if (!am_server && verbose && do_progress)
1150 + else if (!am_server && verbose && recv_progress)
1151 rprintf(FINFO, "%s\n", fname);
1153 /* recv file data */
1154 @@ -654,11 +649,13 @@ int recv_files(int f_in, struct file_lis
1158 - if (remove_source_files
1159 - || (preserve_hard_links && file->link_u.links)) {
1160 + if (remove_source_files) {
1161 + decrement_active_files(i);
1162 SIVAL(numbuf, 0, i);
1163 send_msg(MSG_SUCCESS, numbuf, 4);
1165 + if (preserve_hard_links && file->link_u.links)
1166 + push_hlink_num(i);
1167 } else if (!recv_ok) {
1168 int msgtype = phase || read_batch ? FERROR : FINFO;
1169 if (msgtype == FERROR || verbose) {
1170 @@ -681,8 +678,8 @@ int recv_files(int f_in, struct file_lis
1171 errstr, fname, keptstr, redostr);
1174 - SIVAL(numbuf, 0, i);
1175 - send_msg(MSG_REDO, numbuf, 4);
1176 + decrement_active_files(i);
1183 @@ -39,7 +39,6 @@ extern int omit_dir_times;
1185 extern int am_server;
1186 extern int am_sender;
1187 -extern int am_generator;
1188 extern int am_starting_up;
1189 extern int allow_8bit_chars;
1190 extern int preserve_uid;
1191 @@ -305,5 +304,5 @@ const char *who_am_i(void)
1194 return am_server ? "server" : "client";
1195 - return am_sender ? "sender" : am_generator ? "generator" : "receiver";
1196 + return am_sender ? "sender" : am_generator() ? "generator" : "receiver";
1200 @@ -169,10 +169,8 @@ enum msgcode {
1201 MSG_DATA=0, /* raw data on the multiplexed stream */
1202 MSG_ERROR=FERROR, MSG_INFO=FINFO, /* remote logging */
1203 MSG_LOG=FLOG, MSG_SOCKERR=FSOCKERR, /* sibling logging */
1204 - MSG_REDO=9, /* reprocess indicated flist index */
1205 MSG_SUCCESS=100,/* successfully updated indicated flist index */
1206 MSG_DELETED=101,/* successfully deleted a file on receiving side */
1207 - MSG_DONE=86 /* current phase is done */
1210 #include "errcode.h"
1211 @@ -329,6 +327,7 @@ enum msgcode {
1215 +#include <pthread.h>
1217 #include "lib/pool_alloc.h"
1221 @@ -415,49 +415,6 @@ int robust_rename(char *from, char *to,
1225 -static pid_t all_pids[10];
1226 -static int num_pids;
1228 -/** Fork and record the pid of the child. **/
1229 -pid_t do_fork(void)
1231 - pid_t newpid = fork();
1233 - if (newpid != 0 && newpid != -1) {
1234 - all_pids[num_pids++] = newpid;
1240 - * Kill all children.
1242 - * @todo It would be kind of nice to make sure that they are actually
1243 - * all our children before we kill them, because their pids may have
1244 - * been recycled by some other process. Perhaps when we wait for a
1245 - * child, we should remove it from this array. Alternatively we could
1246 - * perhaps use process groups, but I think that would not work on
1247 - * ancient Unix versions that don't support them.
1249 -void kill_all(int sig)
1253 - for (i = 0; i < num_pids; i++) {
1254 - /* Let's just be a little careful where we
1255 - * point that gun, hey? See kill(2) for the
1256 - * magic caused by negative values. */
1257 - pid_t p = all_pids[i];
1259 - if (p == getpid())
1268 /** Turn a user name into a uid */
1269 int name_to_uid(char *name, uid_t *uid)