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