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