Fixed failing hunks.
[rsync/rsync-patches.git] / threaded-receiver.diff
CommitLineData
3ca259e2
WD
1This patch changes the receiving side to have the receiving code use a thread
2instead of a forked process. This extra thread does read from the socket, but
3it sends any stdout/stderr messages to the generator (main thread) to output.
4
5** This is very new code. ** Yes, it passes the "make test" testsuite, but
6there 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
8messages that arrive on the receiving side during the --delete-after phase!)
9
10This code just uses pthread.h directly, so configure changes will probably be
11needed to make this compatible with more systems. I have also tested that
12this code works fine using the GNU pth library without any code changes if
13you configured it with --enable-syscall-soft --enable-pthread (you may need
14to twiddle the Makefile options if you didn't install the library, though).
15
4a65fe72
WD
16NOTE: we still need to duplicate the partial_fname static in util.c!
17
3ca259e2
WD
18If you try this out, please send some email to wayned@samba.org or the rsync
19mailing list with your results, build changes, bug reports, etc. Thanks!
20
21Be sure to run "make proto" before running "make".
22
93ca4d27 23--- orig/Makefile.in 2006-01-14 08:14:29
3ca259e2
WD
24+++ Makefile.in 2005-12-10 18:35:39
25@@ -6,7 +6,7 @@ exec_prefix=@exec_prefix@
26 bindir=@bindir@
27 mandir=@mandir@
28
29-LIBS=@LIBS@
30+LIBS=@LIBS@ -lpthread
31 CC=@CC@
32 CFLAGS=@CFLAGS@
33 CPPFLAGS=@CPPFLAGS@
4a65fe72 34--- orig/cleanup.c 2006-01-30 07:18:27
3ca259e2
WD
35+++ cleanup.c 2005-12-08 23:17:08
36@@ -94,9 +94,6 @@ void _exit_cleanup(int code, const char
37 }
38 inside_cleanup++;
39
40- signal(SIGUSR1, SIG_IGN);
41- signal(SIGUSR2, SIG_IGN);
42-
43 if (verbose > 3) {
44 rprintf(FINFO,"_exit_cleanup(code=%d, file=%s, line=%d): entered\n",
93ca4d27 45 code, file, line);
3ca259e2
WD
46@@ -127,8 +124,6 @@ void _exit_cleanup(int code, const char
47 io_flush(FULL_FLUSH);
48 if (cleanup_fname)
49 do_unlink(cleanup_fname);
50- if (code)
51- kill_all(SIGUSR1);
52 if (cleanup_pid && cleanup_pid == getpid()) {
53 char *pidf = lp_pid_file();
54 if (pidf && *pidf)
8a0123e5
WD
55--- orig/errcode.h 2005-12-16 23:48:43
56+++ errcode.h 2005-12-16 23:50:02
57@@ -37,7 +37,6 @@
58 #define RERR_CRASHED 15 /* sibling crashed */
59 #define RERR_TERMINATED 16 /* sibling terminated abnormally */
60
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 */
4a65fe72 65--- orig/generator.c 2006-01-31 02:30:18
3ca259e2 66+++ generator.c 2005-12-08 23:17:08
4a65fe72 67@@ -67,7 +67,6 @@ extern OFF_T min_size;
3ca259e2
WD
68 extern int io_error;
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;
4a65fe72 75@@ -98,6 +97,11 @@ static int deletion_count = 0; /* used t
14824301
WD
76 static int can_link_symlinks = 1; /* start out optimistic */
77 static int can_link_devices = 1;
3ca259e2
WD
78
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;
83+
84 /* For calling delete_file() */
85 #define DEL_FORCE_RECURSE (1<<1) /* recurse even w/o --force */
86 #define DEL_TERSE (1<<3)
4a65fe72 87@@ -447,8 +451,8 @@ static void sum_sizes_sqroot(struct sum_
3ca259e2
WD
88 }
89
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;
96 } else {
97 int32 c;
4a65fe72 98@@ -458,7 +462,7 @@ static void sum_sizes_sqroot(struct sum_
3ca259e2
WD
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);
105 }
106
4a65fe72 107@@ -492,7 +496,7 @@ static void generate_and_send_sums(int f
3ca259e2
WD
108 sum_sizes_sqroot(&sum, len);
109 write_sum_head(f_out, &sum);
110
111- if (append_mode > 0 && f_copy < 0)
112+ if (GEN_append_mode > 0 && f_copy < 0)
113 return;
114
115 if (len > 0)
4a65fe72 116@@ -511,7 +515,7 @@ static void generate_and_send_sums(int f
3ca259e2
WD
117
118 if (f_copy >= 0) {
119 full_write(f_copy, map, n1);
120- if (append_mode > 0)
121+ if (GEN_append_mode > 0)
122 continue;
123 }
124
4a65fe72 125@@ -1141,7 +1145,7 @@ static void recv_generator(char *fname,
3ca259e2
WD
126 return;
127 }
128
129- if (append_mode && st.st_size > file->length)
130+ if (GEN_append_mode && st.st_size > file->length)
131 return;
132
14824301 133 if (fnamecmp_type <= FNAMECMP_BASIS_DIR_HIGH)
4a65fe72 134@@ -1196,7 +1200,7 @@ static void recv_generator(char *fname,
3ca259e2
WD
135 goto notify_others;
136 }
137
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))) {
141 close(fd);
142 return;
4a65fe72 143@@ -1285,7 +1289,10 @@ void generate_files(int f_out, struct fi
3ca259e2
WD
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;
149+
150+ GEN_append_mode = append_mode;
151+ GEN_csum_length = csum_length;
152
153 if (protocol_version >= 29) {
154 itemizing = 1;
4a65fe72 155@@ -1314,7 +1321,7 @@ void generate_files(int f_out, struct fi
3ca259e2
WD
156 do_delete_pass(flist);
157 do_progress = 0;
158
159- if (append_mode || whole_file < 0)
160+ if (GEN_append_mode || whole_file < 0)
161 whole_file = 0;
162 if (verbose >= 2) {
163 rprintf(FINFO, "delta-transmission %s\n",
4a65fe72 164@@ -1323,12 +1330,6 @@ void generate_files(int f_out, struct fi
3ca259e2
WD
165 : "enabled");
166 }
167
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;
173-
174 for (i = 0; i < flist->count; i++) {
175 struct file_struct *file = flist->files[i];
176
4a65fe72
WD
177@@ -1372,23 +1373,34 @@ void generate_files(int f_out, struct fi
178 delete_in_dir(NULL, NULL, NULL, NULL);
3ca259e2
WD
179
180 phase++;
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;
185 ignore_times = 1;
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 */
192
193 if (verbose > 2)
194 rprintf(FINFO,"generate_files phase=%d\n",phase);
195
196 write_int(f_out, -1);
197+ io_flush(NORMAL_FLUSH);
198
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];
203+ while (1) {
204+ struct file_struct *file;
205+ if (preserve_hard_links)
206+ check_for_finished_hlinks(itemizing, code);
207+ if ((i = get_redo_num()) < 0) {
208+ if (i == -2)
209+ break;
210+ io_flush(NORMAL_FLUSH);
211+ msleep(20);
212+ continue;
213+ }
214+ file = flist->files[i];
215 if (local_name)
216 strlcpy(fbuf, local_name, sizeof fbuf);
217 else
4a65fe72 218@@ -1400,27 +1412,43 @@ void generate_files(int f_out, struct fi
3ca259e2
WD
219 phase++;
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;
224
225 if (verbose > 2)
226 rprintf(FINFO,"generate_files phase=%d\n",phase);
227
228 write_int(f_out, -1);
229+ io_flush(NORMAL_FLUSH);
230+
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);
236+ }
237
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). */
241+ while (1) {
242+ if (preserve_hard_links)
243+ check_for_finished_hlinks(itemizing, code);
244+ if (get_redo_num() == -2)
245+ break;
246+ io_flush(NORMAL_FLUSH);
247+ msleep(20);
248+ }
249
250 if (protocol_version >= 29) {
251 phase++;
252 if (verbose > 2)
253 rprintf(FINFO, "generate_files phase=%d\n", phase);
254- if (delay_updates)
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);
260+ }
261+ /* Read end marker for delay-updates phase & prior messages. */
262+ while (get_redo_num() != -2) {
263+ io_flush(NORMAL_FLUSH);
264+ msleep(20);
265+ }
266 }
267
268 do_progress = save_do_progress;
269--- orig/io.c 2005-12-08 21:19:31
270+++ io.c 2005-12-10 19:03:08
271@@ -47,7 +47,6 @@ extern int allowed_lull;
272 extern int am_server;
273 extern int am_daemon;
274 extern int am_sender;
275-extern int am_generator;
276 extern int eol_nulls;
277 extern int read_batch;
278 extern int csum_length;
279@@ -60,7 +59,6 @@ extern struct stats stats;
280 extern struct file_list *the_file_list;
281
282 const char phase_unknown[] = "unknown";
283-int ignore_timeout = 0;
284 int batch_fd = -1;
285 int batch_gen_fd = -1;
286
287@@ -84,7 +82,6 @@ const char *io_read_phase = phase_unknow
288 int kluge_around_eof = 0;
289
290 int msg_fd_in = -1;
291-int msg_fd_out = -1;
292 int sock_f_in = -1;
293 int sock_f_out = -1;
294
295@@ -109,27 +106,32 @@ static int select_timeout = SELECT_TIMEO
296 static void read_loop(int fd, char *buf, size_t len);
297
298 struct flist_ndx_item {
299- struct flist_ndx_item *next;
300+ volatile struct flist_ndx_item *next;
301 int ndx;
302 };
303
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;
308 };
309
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 };
313
314 struct msg_list_item {
315- struct msg_list_item *next;
316+ volatile struct msg_list_item *next;
317+ pthread_mutex_t mutex;
318 char *buf;
319 int len;
320+ enum msgcode code;
321 };
322
323 struct msg_list {
324- struct msg_list_item *head, *tail;
325+ volatile struct msg_list_item *head, *tail;
326+ pthread_mutex_t mutex;
327 };
328
329-static struct msg_list msg_list;
330+static struct msg_list msg_list = { NULL, NULL, PTHREAD_MUTEX_INITIALIZER };
331
332 static void flist_ndx_push(struct flist_ndx_list *lp, int ndx)
333 {
334@@ -139,27 +141,31 @@ static void flist_ndx_push(struct flist_
335 out_of_memory("flist_ndx_push");
336 item->next = NULL;
337 item->ndx = ndx;
338+ pthread_mutex_lock(&redo_list.mutex);
339 if (lp->tail)
340 lp->tail->next = item;
341 else
342 lp->head = item;
343 lp->tail = item;
344+ pthread_mutex_unlock(&redo_list.mutex);
345 }
346
347 static int flist_ndx_pop(struct flist_ndx_list *lp)
348 {
349- struct flist_ndx_item *next;
350+ struct flist_ndx_item *head, *next;
351 int ndx;
352
353 if (!lp->head)
354 return -1;
355
356- ndx = lp->head->ndx;
357- next = lp->head->next;
358- free(lp->head);
359- lp->head = next;
360- if (!next)
361+ pthread_mutex_lock(&hlink_list.mutex);
362+ head = (struct flist_ndx_item *)lp->head;
363+ next = (struct flist_ndx_item *)head->next;
364+ ndx = head->ndx;
365+ if (!(lp->head = next))
366 lp->tail = NULL;
367+ pthread_mutex_unlock(&hlink_list.mutex);
368+ free(head);
369
370 return ndx;
371 }
372@@ -168,7 +174,7 @@ static void check_timeout(void)
373 {
374 time_t t;
375
376- if (!io_timeout || ignore_timeout)
377+ if (!io_timeout)
378 return;
379
380 if (!last_io_in) {
381@@ -209,45 +215,40 @@ void set_io_timeout(int secs)
382
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). */
387+ * file list). */
388 void set_msg_fd_in(int fd)
389 {
390 msg_fd_in = fd;
391 }
392
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)
396-{
397- msg_fd_out = fd;
398- set_nonblocking(msg_fd_out);
399-}
400-
401 /* Add a message to the pending MSG_* list. */
402 static void msg_list_add(int code, char *buf, int len)
403 {
404 struct msg_list_item *ml;
405
406+ assert(am_receiver());
407 if (!(ml = new(struct msg_list_item)))
408 out_of_memory("msg_list_add");
409 ml->next = NULL;
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);
416- ml->len = len+4;
417+ memcpy(ml->buf, buf, len);
418+ ml->len = len;
419+ ml->code = code;
420+
421+ pthread_mutex_lock(&msg_list.mutex);
422 if (msg_list.tail)
423 msg_list.tail->next = ml;
424 else
425 msg_list.head = ml;
426 msg_list.tail = ml;
427+ pthread_mutex_unlock(&msg_list.mutex);
428 }
429
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)
437 {
438 char buf[2048];
439@@ -266,40 +267,6 @@ static void read_msg_fd(void)
440 tag = (tag >> 24) - MPLEX_BASE;
441
442 switch (tag) {
443- case MSG_DONE:
444- if (len != 0 || !am_generator) {
445- rprintf(FERROR, "invalid message %d:%d\n", tag, len);
446- exit_cleanup(RERR_STREAMIO);
447- }
448- flist_ndx_push(&redo_list, -1);
449- break;
450- case MSG_REDO:
451- if (len != 4 || !am_generator) {
452- rprintf(FERROR, "invalid message %d:%d\n", tag, len);
453- exit_cleanup(RERR_STREAMIO);
454- }
455- read_loop(fd, buf, 4);
456- flist_ndx_push(&redo_list, IVAL(buf,0));
457- break;
458- case MSG_DELETED:
459- if (len >= (int)sizeof buf || !am_generator) {
460- rprintf(FERROR, "invalid message %d:%d\n", tag, len);
461- exit_cleanup(RERR_STREAMIO);
462- }
463- read_loop(fd, buf, len);
464- io_multiplex_write(MSG_DELETED, buf, len);
465- break;
466- case MSG_SUCCESS:
467- if (len != 4 || !am_generator) {
468- rprintf(FERROR, "invalid message %d:%d\n", tag, len);
469- exit_cleanup(RERR_STREAMIO);
470- }
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));
476- break;
477 case MSG_INFO:
478 case MSG_ERROR:
479 case MSG_LOG:
480@@ -320,71 +287,72 @@ static void read_msg_fd(void)
481 msg_fd_in = fd;
482 }
483
484-/* Try to push messages off the list onto the wire. If we leave with more
485+/* Try to pop messages off the list onto the wire. If we leave with more
486 * to do, return 0. On error, return -1. If everything flushed, return 1.
487- * This is only active in the receiver. */
488+ * This is only called by the generator. */
489 static int msg_list_flush(int flush_it_all)
490 {
491- static int written = 0;
492- struct timeval tv;
493- fd_set fds;
494-
495- if (msg_fd_out < 0)
496- return -1;
497-
498+ assert(am_generator());
499+ no_flush++;
500 while (msg_list.head) {
501- struct msg_list_item *ml = msg_list.head;
502- int n = write(msg_fd_out, ml->buf + written, ml->len - written);
503- if (n < 0) {
504- if (errno == EINTR)
505- continue;
506- if (errno != EWOULDBLOCK && errno != EAGAIN)
507- return -1;
508- if (!flush_it_all)
509- return 0;
510- FD_ZERO(&fds);
511- FD_SET(msg_fd_out, &fds);
512- tv.tv_sec = select_timeout;
513- tv.tv_usec = 0;
514- if (!select(msg_fd_out+1, NULL, &fds, NULL, &tv))
515- check_timeout();
516- } else if ((written += n) == ml->len) {
517- free(ml->buf);
518- msg_list.head = ml->next;
519- if (!msg_list.head)
520- msg_list.tail = NULL;
521- free(ml);
522- written = 0;
523+ struct msg_list_item *ml = (struct msg_list_item *)msg_list.head;
524+ switch (ml->code) {
525+ case MSG_INFO:
526+ case MSG_ERROR:
527+ case MSG_LOG:
528+ rwrite(ml->code, ml->buf, ml->len);
529+ break;
530+ default:
531+ io_multiplex_write(ml->code, ml->buf, ml->len);
532+ break;
533 }
534+ pthread_mutex_lock(&msg_list.mutex);
535+ if (!(msg_list.head = ml->next))
536+ msg_list.tail = NULL;
537+ pthread_mutex_unlock(&msg_list.mutex);
538+ free(ml->buf);
539+ free(ml);
540+ if (!flush_it_all)
541+ break;
542 }
543+ no_flush--;
544+
545 return 1;
546 }
547
548 void send_msg(enum msgcode code, char *buf, int len)
549 {
550- if (msg_fd_out < 0) {
551+ if (am_receiver())
552+ msg_list_add(code, buf, len);
553+ else
554 io_multiplex_write(code, buf, len);
555- return;
556- }
557- msg_list_add(code, buf, len);
558- msg_list_flush(NORMAL_FLUSH);
559 }
560
561-int get_redo_num(int itemizing, enum logcode code)
562+/* This is only used by the receiver. */
563+void push_redo_num(int ndx)
564 {
565- while (1) {
566- if (hlink_list.head)
567- check_for_finished_hlinks(itemizing, code);
568- if (redo_list.head)
569- break;
570- read_msg_fd();
571- }
572+ assert(am_receiver());
573+ flist_ndx_push(&redo_list, ndx);
574+}
575
576+/* This is only used by the generator. */
577+int get_redo_num(void)
578+{
579+ assert(am_generator());
580 return flist_ndx_pop(&redo_list);
581 }
582
583+/* This is only used by the receiver. */
584+void push_hlink_num(int ndx)
585+{
586+ assert(am_receiver());
587+ flist_ndx_push(&hlink_list, ndx);
588+}
589+
590+/* This is only used by the generator. */
591 int get_hlink_num(void)
592 {
593+ assert(am_generator());
594 return flist_ndx_pop(&hlink_list);
595 }
596
597@@ -465,11 +433,6 @@ static int read_timeout(int fd, char *bu
598 FD_ZERO(&r_fds);
599 FD_ZERO(&w_fds);
600 FD_SET(fd, &r_fds);
601- if (msg_list.head) {
602- FD_SET(msg_fd_out, &w_fds);
603- if (msg_fd_out > maxfd)
604- maxfd = msg_fd_out;
605- }
606 if (io_filesfrom_f_out >= 0) {
607 int new_fd;
608 if (io_filesfrom_buflen == 0) {
609@@ -502,9 +465,6 @@ static int read_timeout(int fd, char *bu
610 continue;
611 }
612
613- if (msg_list.head && FD_ISSET(msg_fd_out, &w_fds))
614- msg_list_flush(NORMAL_FLUSH);
615-
616 if (io_filesfrom_f_out >= 0) {
617 if (io_filesfrom_buflen) {
618 if (FD_ISSET(io_filesfrom_f_out, &w_fds)) {
619@@ -832,6 +792,8 @@ static void readfd(int fd, char *buffer,
620 }
621
622 if (fd == write_batch_monitor_in) {
623+ if (am_generator())
624+ rprintf(FINFO, "writing %d bytes to batch file from generator\n", total);
625 if ((size_t)write(batch_fd, buffer, total) != total)
626 exit_cleanup(RERR_FILEIO);
627 }
628@@ -1091,7 +1053,6 @@ static void writefd_unbuffered(int fd,ch
629 * to grab any messages they sent before they died. */
630 while (fd == sock_f_out && io_multiplexing_in) {
631 set_io_timeout(30);
632- ignore_timeout = 0;
633 readfd_unbuffered(sock_f_in, io_filesfrom_buf,
634 sizeof io_filesfrom_buf);
635 }
636@@ -1101,7 +1062,7 @@ static void writefd_unbuffered(int fd,ch
637 total += ret;
638
639 if (fd == sock_f_out) {
640- if (io_timeout || am_generator)
641+ if (io_timeout || am_generator())
642 last_io_out = time(NULL);
643 sleep_for_bwlimit(ret);
644 }
645@@ -1126,7 +1087,7 @@ static void mplex_write(enum msgcode cod
646 * cause output to occur down the socket. Setting contiguous_write_len
647 * prevents the reading of msg_fd_in once we actually start to write
648 * this sequence of data (though we might read it before the start). */
649- if (am_generator && msg_fd_in >= 0)
650+ if (am_generator() && msg_fd_in >= 0)
651 contiguous_write_len = len + 4;
652
653 if (n > sizeof buffer - 4)
654@@ -1141,33 +1102,31 @@ static void mplex_write(enum msgcode cod
655 if (len)
656 writefd_unbuffered(sock_f_out, buf, len);
657
658- if (am_generator && msg_fd_in >= 0)
659+ if (am_generator() && msg_fd_in >= 0)
660 contiguous_write_len = 0;
661 }
662
663
664 void io_flush(int flush_it_all)
665 {
666- msg_list_flush(flush_it_all);
667-
668- if (!iobuf_out_cnt || no_flush)
669+ if (no_flush)
670 return;
671
672- if (io_multiplexing_out)
673- mplex_write(MSG_DATA, iobuf_out, iobuf_out_cnt);
674- else
675- writefd_unbuffered(sock_f_out, iobuf_out, iobuf_out_cnt);
676- iobuf_out_cnt = 0;
677+ if (iobuf_out_cnt) {
678+ if (io_multiplexing_out)
679+ mplex_write(MSG_DATA, iobuf_out, iobuf_out_cnt);
680+ else
681+ writefd_unbuffered(sock_f_out, iobuf_out, iobuf_out_cnt);
682+ iobuf_out_cnt = 0;
683+ }
684+
685+ if (am_generator())
686+ msg_list_flush(flush_it_all);
687 }
688
689
690 static void writefd(int fd,char *buf,size_t len)
691 {
692- if (fd == msg_fd_out) {
693- rprintf(FERROR, "Internal error: wrong write used in receiver.\n");
694- exit_cleanup(RERR_PROTOCOL);
695- }
696-
697 if (fd == sock_f_out)
698 stats.total_written += len;
699
700@@ -1387,9 +1346,3 @@ void start_write_batch(int fd)
701 else
702 write_batch_monitor_in = fd;
703 }
704-
705-void stop_write_batch(void)
706-{
707- write_batch_monitor_out = -1;
708- write_batch_monitor_in = -1;
709-}
4a65fe72 710--- orig/log.c 2006-01-26 10:45:39
8a0123e5 711+++ log.c 2005-12-16 23:49:57
3ca259e2
WD
712@@ -35,7 +35,6 @@ extern int am_sender;
713 extern int local_server;
714 extern int quiet;
715 extern int module_id;
716-extern int msg_fd_out;
717 extern int protocol_version;
718 extern int preserve_times;
14824301 719 extern int log_format_has_i;
8a0123e5 720@@ -68,7 +67,6 @@ struct {
3ca259e2
WD
721 { RERR_IPC , "error in IPC code" },
722 { RERR_CRASHED , "sibling process crashed" },
723 { RERR_TERMINATED , "sibling process terminated abnormally" },
8a0123e5
WD
724- { RERR_SIGNAL1 , "received SIGUSR1" },
725 { RERR_SIGNAL , "received SIGINT, SIGTERM, or SIGHUP" },
3ca259e2
WD
726 { RERR_WAITCHILD , "waitpid() failed" },
727 { RERR_MALLOC , "error allocating core memory buffers" },
79d14d37
WD
728@@ -224,8 +222,8 @@ void rwrite(enum logcode code, char *buf
729 if (len < 0)
730 exit_cleanup(RERR_MESSAGEIO);
3ca259e2
WD
731
732- if (am_server && msg_fd_out >= 0) {
733- /* Pass the message to our sibling. */
734+ if (am_receiver()) {
735+ /* Pass the message to the generator thread. */
736 send_msg((enum msgcode)code, buf, len);
737 return;
738 }
79d14d37 739--- orig/main.c 2006-01-15 14:46:15
8a0123e5 740+++ main.c 2005-12-16 23:50:33
3ca259e2
WD
741@@ -30,7 +30,6 @@ extern int list_only;
742 extern int am_root;
743 extern int am_server;
744 extern int am_sender;
745-extern int am_generator;
746 extern int am_daemon;
747 extern int blocking_io;
748 extern int remove_sent_files;
749@@ -75,9 +74,20 @@ struct pid_status {
750
751 static time_t starttime, endtime;
752 static int64 total_read, total_written;
753+static pthread_t receiver_tid;
754
755 static void show_malloc_stats(void);
756
757+int am_generator()
758+{
759+ return receiver_tid != 0 && pthread_self() != receiver_tid;
760+}
761+
762+int am_receiver()
763+{
764+ return receiver_tid != 0 && pthread_self() == receiver_tid;
765+}
766+
767 /* Works like waitpid(), but if we already harvested the child pid in our
768 * sigchld_handler(), we succeed instead of returning an error. */
769 pid_t wait_process(pid_t pid, int *status_ptr, int flags)
770@@ -154,7 +164,7 @@ static void handle_stats(int f)
771 show_flist_stats();
772 }
773
774- if (am_generator)
775+ if (am_generator())
776 return;
777
778 if (am_daemon) {
79d14d37 779@@ -609,12 +619,30 @@ static void do_server_sender(int f_in, i
3ca259e2
WD
780 exit_cleanup(0);
781 }
782
783+struct thread_args {
784+ struct file_list *flist;
785+ char *local_name;
786+ int f_in;
787+};
788+
789+static void *start_receiver_thread(void *arg)
790+{
791+ static int exit_code;
792+ struct thread_args *ta = (struct thread_args *)arg;
793+
794+ recv_files(ta->f_in, ta->flist, ta->local_name);
795+ handle_stats(ta->f_in);
796+
797+ push_redo_num(-2);
798+
799+ exit_code = log_got_error ? RERR_PARTIAL : 0;
800+ return &exit_code;
801+}
802
803 static int do_recv(int f_in,int f_out,struct file_list *flist,char *local_name)
804 {
805- int pid;
806- int exit_code = 0;
807- int error_pipe[2];
808+ void *value_ptr;
809+ struct thread_args args;
810
811 /* The receiving side mustn't obey this, or an existing symlink that
812 * points to an identical file won't be replaced by the referent. */
79d14d37 813@@ -623,70 +651,16 @@ static int do_recv(int f_in,int f_out,st
3ca259e2
WD
814 if (preserve_hard_links)
815 init_hard_links();
816
817- if (fd_pair(error_pipe) < 0) {
818- rsyserr(FERROR, errno, "pipe failed in do_recv");
819+ args.f_in = f_in;
820+ args.flist = flist;
821+ args.local_name = local_name;
822+ if (pthread_create(&receiver_tid, NULL, start_receiver_thread, &args) < 0) {
823+ rsyserr(FERROR, errno, "pthread_create failed in do_recv");
824 exit_cleanup(RERR_IPC);
825 }
826
827- io_flush(NORMAL_FLUSH);
828-
829- if ((pid = do_fork()) == -1) {
830- rsyserr(FERROR, errno, "fork failed in do_recv");
831- exit_cleanup(RERR_IPC);
832- }
833-
834- if (pid == 0) {
835- close(error_pipe[0]);
836- if (f_in != f_out)
837- close(f_out);
838-
839- /* we can't let two processes write to the socket at one time */
840- close_multiplexing_out();
841-
842- /* set place to send errors */
843- set_msg_fd_out(error_pipe[1]);
844-
845- recv_files(f_in, flist, local_name);
846- io_flush(FULL_FLUSH);
847- handle_stats(f_in);
848-
849- send_msg(MSG_DONE, "", 0);
850- io_flush(FULL_FLUSH);
851-
852- /* Handle any keep-alive packets from the post-processing work
853- * that the generator does. */
854- if (protocol_version >= 29) {
855- kluge_around_eof = -1;
856-
857- /* This should only get stopped via a USR2 signal. */
858- while (read_int(f_in) == flist->count
859- && read_shortint(f_in) == ITEM_IS_NEW) {}
860-
861- rprintf(FERROR, "Invalid packet at end of run [%s]\n",
862- who_am_i());
863- exit_cleanup(RERR_PROTOCOL);
864- }
865-
866- /* Finally, we go to sleep until our parent kills us with a
867- * USR2 signal. We sleep for a short time, as on some OSes
868- * a signal won't interrupt a sleep! */
869- while (1)
870- msleep(20);
871- }
872-
873- am_generator = 1;
874- close_multiplexing_in();
875- if (write_batch && !am_server)
876- stop_write_batch();
877-
878- close(error_pipe[1]);
879- if (f_in != f_out)
880- close(f_in);
881-
882 io_start_buffering_out();
883
884- set_msg_fd_in(error_pipe[0]);
885-
886 generate_files(f_out, flist, local_name);
887
888 handle_stats(-1);
79d14d37 889@@ -697,10 +671,13 @@ static int do_recv(int f_in,int f_out,st
3ca259e2
WD
890 }
891 io_flush(FULL_FLUSH);
892
893- set_msg_fd_in(-1);
894- kill(pid, SIGUSR2);
895- wait_process_with_flush(pid, &exit_code);
896- return exit_code;
897+ pthread_join(receiver_tid, &value_ptr);
898+ if (!am_server)
899+ output_summary();
900+
901+ close_all();
902+
903+ return *(int*)value_ptr;
904 }
905
906
79d14d37 907@@ -1062,22 +1039,6 @@ static int start_client(int argc, char *
3ca259e2
WD
908 return ret;
909 }
910
911-
912-static RETSIGTYPE sigusr1_handler(UNUSED(int val))
913-{
8a0123e5 914- exit_cleanup(RERR_SIGNAL1);
3ca259e2
WD
915-}
916-
917-static RETSIGTYPE sigusr2_handler(UNUSED(int val))
918-{
919- if (!am_server)
920- output_summary();
921- close_all();
922- if (log_got_error)
923- _exit(RERR_PARTIAL);
924- _exit(0);
925-}
926-
927 static RETSIGTYPE sigchld_handler(UNUSED(int val))
928 {
929 #ifdef WNOHANG
79d14d37 930@@ -1160,8 +1121,6 @@ int main(int argc,char *argv[])
3ca259e2
WD
931 int orig_argc = argc;
932 char **orig_argv = argv;
933
934- signal(SIGUSR1, sigusr1_handler);
935- signal(SIGUSR2, sigusr2_handler);
936 signal(SIGCHLD, sigchld_handler);
937 #ifdef MAINTAINER_MODE
938 signal(SIGSEGV, rsync_panic_handler);
939--- orig/match.c 2005-11-10 16:58:36
940+++ match.c 2005-12-08 23:17:09
941@@ -21,7 +21,7 @@
942
943 extern int verbose;
944 extern int am_server;
945-extern int do_progress;
946+extern int recv_progress;
947 extern int checksum_seed;
948 extern int append_mode;
949
950@@ -133,7 +133,7 @@ static void matched(int f, struct sum_st
951 else
952 last_match = offset;
953
954- if (buf && do_progress)
955+ if (buf && recv_progress)
956 show_progress(last_match, buf->file_size);
957 }
958
959@@ -333,7 +333,7 @@ void match_sums(int f, struct sum_struct
960 if (append_mode) {
961 OFF_T j = 0;
962 for (j = CHUNK_SIZE; j < s->flength; j += CHUNK_SIZE) {
963- if (buf && do_progress)
964+ if (buf && recv_progress)
965 show_progress(last_match, buf->file_size);
966 sum_update(map_ptr(buf, last_match, CHUNK_SIZE),
967 CHUNK_SIZE);
968@@ -341,7 +341,7 @@ void match_sums(int f, struct sum_struct
969 }
970 if (last_match < s->flength) {
971 int32 len = s->flength - last_match;
972- if (buf && do_progress)
973+ if (buf && recv_progress)
974 show_progress(last_match, buf->file_size);
975 sum_update(map_ptr(buf, last_match, len), len);
976 last_match = s->flength;
4a65fe72 977--- orig/options.c 2006-01-31 03:11:30
3ca259e2 978+++ options.c 2005-12-08 23:17:09
4a65fe72 979@@ -73,7 +73,6 @@ int def_compress_level = Z_DEFAULT_COMPR
3ca259e2
WD
980 int am_root = 0;
981 int am_server = 0;
982 int am_sender = 0;
983-int am_generator = 0;
984 int am_starting_up = 1;
985 int orig_umask = 0;
986 int relative_paths = -1;
4a65fe72 987@@ -94,6 +93,7 @@ int am_daemon = 0;
3ca259e2
WD
988 int daemon_over_rsh = 0;
989 int do_stats = 0;
990 int do_progress = 0;
991+int recv_progress = 0;
992 int keep_partial = 0;
993 int safe_symlinks = 0;
994 int copy_unsafe_links = 0;
4a65fe72 995@@ -1290,6 +1290,7 @@ int parse_arguments(int *argc, const cha
3ca259e2
WD
996 if ((do_progress || dry_run) && !verbose && !log_before_transfer
997 && !am_server)
998 verbose = 1;
999+ recv_progress = do_progress;
1000
1001 if (dry_run)
1002 do_xfers = 0;
4a65fe72 1003--- orig/pipe.c 2006-01-21 08:03:40
3ca259e2 1004+++ pipe.c 2005-12-08 23:17:09
4a65fe72 1005@@ -56,7 +56,7 @@ pid_t piped_child(char **command, int *f
3ca259e2
WD
1006 exit_cleanup(RERR_IPC);
1007 }
1008
1009- pid = do_fork();
1010+ pid = fork();
1011 if (pid == -1) {
1012 rsyserr(FERROR, errno, "fork");
1013 exit_cleanup(RERR_IPC);
4a65fe72 1014@@ -120,7 +120,7 @@ pid_t local_child(int argc, char **argv,
3ca259e2
WD
1015 exit_cleanup(RERR_IPC);
1016 }
1017
1018- pid = do_fork();
1019+ pid = fork();
1020 if (pid == -1) {
1021 rsyserr(FERROR, errno, "fork");
1022 exit_cleanup(RERR_IPC);
4a65fe72 1023--- orig/receiver.c 2006-01-31 02:30:18
93ca4d27 1024+++ receiver.c 2006-01-14 08:30:29
3ca259e2
WD
1025@@ -24,7 +24,7 @@ extern int verbose;
1026 extern int do_xfers;
1027 extern int am_daemon;
1028 extern int am_server;
1029-extern int do_progress;
1030+extern int recv_progress;
1031 extern int log_before_transfer;
1032 extern int log_format_has_i;
1033 extern int daemon_log_format_has_i;
93ca4d27 1034@@ -219,7 +219,7 @@ static int receive_data(int f_in, char *
3ca259e2
WD
1035 if (sum.remainder)
1036 sum.flength -= sum.blength - sum.remainder;
1037 for (j = CHUNK_SIZE; j < sum.flength; j += CHUNK_SIZE) {
1038- if (do_progress)
1039+ if (recv_progress)
1040 show_progress(offset, total_size);
1041 sum_update(map_ptr(mapbuf, offset, CHUNK_SIZE),
1042 CHUNK_SIZE);
93ca4d27 1043@@ -227,7 +227,7 @@ static int receive_data(int f_in, char *
3ca259e2
WD
1044 }
1045 if (offset < sum.flength) {
1046 int32 len = sum.flength - offset;
1047- if (do_progress)
1048+ if (recv_progress)
1049 show_progress(offset, total_size);
1050 sum_update(map_ptr(mapbuf, offset, len), len);
1051 offset = sum.flength;
93ca4d27 1052@@ -240,7 +240,7 @@ static int receive_data(int f_in, char *
3ca259e2
WD
1053 }
1054
1055 while ((i = recv_token(f_in, &data)) != 0) {
1056- if (do_progress)
1057+ if (recv_progress)
1058 show_progress(offset, total_size);
1059
1060 if (i > 0) {
93ca4d27 1061@@ -308,7 +308,7 @@ static int receive_data(int f_in, char *
3ca259e2
WD
1062 ftruncate(fd, offset);
1063 #endif
1064
1065- if (do_progress)
1066+ if (recv_progress)
1067 end_progress(total_size);
1068
1069 if (fd != -1 && offset > 0 && sparse_end(fd) != 0) {
93ca4d27
WD
1070@@ -359,12 +359,12 @@ static void handle_delayed_updates(struc
1071 "rename failed for %s (from %s)",
1072 full_fname(fname), partialptr);
3ca259e2
WD
1073 } else {
1074- if (remove_sent_files
1075- || (preserve_hard_links
1076- && file->link_u.links)) {
1077+ if (remove_sent_files) {
1078 SIVAL(numbuf, 0, i);
1079 send_msg(MSG_SUCCESS,numbuf,4);
1080 }
3ca259e2
WD
1081+ if (preserve_hard_links && file->link_u.links)
1082+ push_hlink_num(i);
93ca4d27 1083 handle_partial_dir(partialptr, PDIR_DELETE);
3ca259e2
WD
1084 }
1085 }
93ca4d27 1086@@ -415,11 +415,6 @@ int recv_files(int f_in, struct file_lis
3ca259e2
WD
1087 if (verbose > 2)
1088 rprintf(FINFO,"recv_files(%d) starting\n",flist->count);
1089
1090- if (flist->hlink_pool) {
1091- pool_destroy(flist->hlink_pool);
1092- flist->hlink_pool = NULL;
1093- }
1094-
1095 if (delay_updates)
1096 init_delayed_bits(flist->count);
1097
93ca4d27 1098@@ -440,7 +435,7 @@ int recv_files(int f_in, struct file_lis
3ca259e2
WD
1099 rprintf(FINFO, "recv_files phase=%d\n", phase);
1100 if (phase == 2 && delay_updates)
1101 handle_delayed_updates(flist, local_name);
1102- send_msg(MSG_DONE, "", 0);
1103+ push_redo_num(-2);
1104 if (keep_partial && !partial_dir)
1105 make_backups = 0; /* prevents double backup */
1106 if (append_mode) {
93ca4d27 1107@@ -662,7 +657,7 @@ int recv_files(int f_in, struct file_lis
3ca259e2
WD
1108 /* log the transfer */
1109 if (log_before_transfer)
1110 log_item(file, &initial_stats, iflags, NULL);
1111- else if (!am_server && verbose && do_progress)
1112+ else if (!am_server && verbose && recv_progress)
93ca4d27 1113 rprintf(FINFO, "%s\n", fname);
3ca259e2
WD
1114
1115 /* recv file data */
4a65fe72 1116@@ -705,11 +700,12 @@ int recv_files(int f_in, struct file_lis
3ca259e2
WD
1117 cleanup_disable();
1118
1119 if (recv_ok > 0) {
1120- if (remove_sent_files
1121- || (preserve_hard_links && file->link_u.links)) {
1122+ if (remove_sent_files) {
1123 SIVAL(numbuf, 0, i);
1124 send_msg(MSG_SUCCESS, numbuf, 4);
1125 }
1126+ if (preserve_hard_links && file->link_u.links)
1127+ push_hlink_num(i);
1128 } else if (!recv_ok) {
1129 int msgtype = phase || read_batch ? FERROR : FINFO;
1130 if (msgtype == FERROR || verbose) {
4a65fe72 1131@@ -731,10 +727,8 @@ int recv_files(int f_in, struct file_lis
93ca4d27
WD
1132 "%s: %s failed verification -- update %s%s.\n",
1133 errstr, fname, keptstr, redostr);
3ca259e2
WD
1134 }
1135- if (!phase) {
1136- SIVAL(numbuf, 0, i);
1137- send_msg(MSG_REDO, numbuf, 4);
1138- }
1139+ if (!phase)
1140+ push_redo_num(i);
1141 }
1142 }
1143 make_backups = save_make_backups;
4a65fe72 1144--- orig/rsync.c 2006-01-31 02:30:18
3ca259e2 1145+++ rsync.c 2005-12-08 23:17:10
4a65fe72 1146@@ -32,7 +32,6 @@ extern int orig_umask;
3ca259e2
WD
1147 extern int am_root;
1148 extern int am_server;
1149 extern int am_sender;
1150-extern int am_generator;
1151 extern int am_starting_up;
1152 extern int preserve_uid;
1153 extern int preserve_gid;
4a65fe72 1154@@ -247,5 +246,5 @@ const char *who_am_i(void)
3ca259e2
WD
1155 {
1156 if (am_starting_up)
1157 return am_server ? "server" : "client";
1158- return am_sender ? "sender" : am_generator ? "generator" : "receiver";
1159+ return am_sender ? "sender" : am_generator() ? "generator" : "receiver";
1160 }
4a65fe72 1161--- orig/rsync.h 2006-01-30 20:39:09
79d14d37
WD
1162+++ rsync.h 2006-01-17 02:46:03
1163@@ -166,10 +166,8 @@ enum msgcode {
3ca259e2
WD
1164 MSG_DATA=0, /* raw data on the multiplexed stream */
1165 MSG_ERROR=FERROR, MSG_INFO=FINFO, /* remote logging */
79d14d37 1166 MSG_LOG=FLOG, /* sibling logging */
3ca259e2
WD
1167- MSG_REDO=9, /* reprocess indicated flist index */
1168 MSG_SUCCESS=100,/* successfully updated indicated flist index */
1169 MSG_DELETED=101,/* successfully deleted a file on receiving side */
1170- MSG_DONE=86 /* current phase is done */
1171 };
1172
1173 #include "errcode.h"
79d14d37 1174@@ -320,6 +318,7 @@ enum msgcode {
3ca259e2
WD
1175 #endif
1176
1177 #include <assert.h>
1178+#include <pthread.h>
1179
1180 #include "lib/pool_alloc.h"
1181
4a65fe72 1182--- orig/util.c 2006-01-30 07:18:28
3ca259e2 1183+++ util.c 2005-12-08 23:17:10
4a65fe72 1184@@ -413,51 +413,6 @@ int robust_rename(char *from, char *to,
3ca259e2
WD
1185 return -1;
1186 }
1187
1188-
1189-static pid_t all_pids[10];
1190-static int num_pids;
1191-
1192-/** Fork and record the pid of the child. **/
1193-pid_t do_fork(void)
1194-{
1195- pid_t newpid = fork();
1196-
1197- if (newpid != 0 && newpid != -1) {
1198- all_pids[num_pids++] = newpid;
1199- }
1200- return newpid;
1201-}
1202-
1203-/**
1204- * Kill all children.
1205- *
1206- * @todo It would be kind of nice to make sure that they are actually
1207- * all our children before we kill them, because their pids may have
1208- * been recycled by some other process. Perhaps when we wait for a
1209- * child, we should remove it from this array. Alternatively we could
1210- * perhaps use process groups, but I think that would not work on
1211- * ancient Unix versions that don't support them.
1212- **/
1213-void kill_all(int sig)
1214-{
1215- int i;
1216-
1217- for (i = 0; i < num_pids; i++) {
1218- /* Let's just be a little careful where we
1219- * point that gun, hey? See kill(2) for the
1220- * magic caused by negative values. */
1221- pid_t p = all_pids[i];
1222-
1223- if (p == getpid())
1224- continue;
1225- if (p <= 0)
1226- continue;
1227-
1228- kill(p, sig);
1229- }
1230-}
1231-
1232-
1233 /** Turn a user name into a uid */
1234 int name_to_uid(char *name, uid_t *uid)
1235 {