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