1 /* Linuxthreads - a simple clone()-based implementation of Posix        */
2 /* threads for Linux.                                                   */
3 /* Copyright (C) 1996 Xavier Leroy (Xavier.Leroy@inria.fr)              */
4 /*                                                                      */
5 /* This program is free software; you can redistribute it and/or        */
6 /* modify it under the terms of the GNU Library General Public License  */
7 /* as published by the Free Software Foundation; either version 2       */
8 /* of the License, or (at your option) any later version.               */
9 /*                                                                      */
10 /* This program is distributed in the hope that it will be useful,      */
11 /* but WITHOUT ANY WARRANTY; without even the implied warranty of       */
12 /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the        */
13 /* GNU Library General Public License for more details.                 */
14 
15 /* The "atfork" stuff */
16 
17 #include <errno.h>
18 #include <stddef.h>
19 #include <stdlib.h>
20 #include <unistd.h>
21 #include "pthread.h"
22 #include "internals.h"
23 #include <bits/libc-lock.h>
24 #include "fork.h"
25 
__pthread_fork(struct fork_block * b)26 pid_t __pthread_fork (struct fork_block *b)
27 {
28   pid_t pid;
29   list_t *runp;
30 
31   __libc_lock_lock (b->lock);
32 
33   /* Run all the registered preparation handlers.  In reverse order.  */
34   list_for_each_prev (runp, &b->prepare_list)
35     {
36       struct fork_handler *curp;
37       curp = list_entry (runp, struct fork_handler, list);
38       curp->handler ();
39     }
40 
41   __pthread_once_fork_prepare();
42   __flockfilelist();
43 
44   pid = ARCH_FORK ();
45 
46   if (pid == 0) {
47     __pthread_reset_main_thread();
48 
49     __fresetlockfiles();
50     __pthread_once_fork_child();
51 
52     /* Run the handlers registered for the child.  */
53     list_for_each (runp, &b->child_list)
54       {
55 	struct fork_handler *curp;
56 	curp = list_entry (runp, struct fork_handler, list);
57 	curp->handler ();
58       }
59 
60     __libc_lock_init (b->lock);
61   } else {
62     __funlockfilelist();
63     __pthread_once_fork_parent();
64 
65     /* Run the handlers registered for the parent.  */
66     list_for_each (runp, &b->parent_list)
67       {
68 	struct fork_handler *curp;
69 	curp = list_entry (runp, struct fork_handler, list);
70 	curp->handler ();
71       }
72 
73     __libc_lock_unlock (b->lock);
74   }
75 
76   return pid;
77 }
78 
79 /* psm: have no idea why these are here, sjhill? */
80 #if 0 /*def SHARED*/
81 pid_t __fork (void)
82 {
83   return __libc_fork ();
84 }
85 weak_alias (__fork, fork)
86 
87 pid_t __vfork(void)
88 {
89   return __libc_fork ();
90 }
91 weak_alias (__vfork, vfork)
92 #endif
93