Initial revision
[glibc.git] / nptl / sysdeps / unix / sysv / linux / fork.c
blobc1aa23c5c509b3f1ed67ce74232fd75aa90e8bcb
1 /* Copyright (C) 2002 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Contributed by Ulrich Drepper <drepper@redhat.com>, 2002.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 The GNU C Library 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 GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, write to the Free
17 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
18 02111-1307 USA. */
20 #include <stdlib.h>
21 #include <unistd.h>
22 #include <sys/types.h>
23 #include <sysdep.h>
24 #include <libio/libioP.h>
25 #include <tls.h>
26 #include "fork.h"
29 unsigned long int *__fork_generation_pointer;
32 lll_lock_t __fork_lock = LLL_LOCK_INITIALIZER;
33 LIST_HEAD (__fork_prepare_list);
34 LIST_HEAD (__fork_parent_list);
35 LIST_HEAD (__fork_child_list);
38 static void
39 fresetlockfiles (void)
41 _IO_ITER i;
43 for (i = _IO_iter_begin(); i != _IO_iter_end(); i = _IO_iter_next(i))
44 *((pthread_mutex_t *) _IO_iter_file(i)->_lock)
45 = (pthread_mutex_t) PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP;
49 pid_t
50 __libc_fork (void)
52 pid_t pid;
53 list_t *runp;
55 /* Get the lock so that the set of registered handlers is not
56 inconsistent or changes beneath us. */
57 lll_lock (__fork_lock);
59 /* Run all the registered preparation handlers. In reverse order. */
60 list_for_each_prev (runp, &__fork_prepare_list)
62 struct fork_handler *curp;
64 curp = list_entry (runp, struct fork_handler, list);
66 curp->handler ();
69 _IO_list_lock ();
71 #ifdef ARCH_FORK
72 pid = ARCH_FORK ();
73 #else
74 # error "ARCH_FORK must be defined so that the CLONE_SETTID flag is used"
75 pid = INLINE_SYSCALL (fork, 0);
76 #endif
78 if (pid == 0)
80 if (__fork_generation_pointer != NULL)
81 *__fork_generation_pointer += 4;
83 /* Reset the file list. These are recursive mutexes. */
84 fresetlockfiles ();
86 /* We execute this even if the 'fork' call failed. */
87 _IO_list_resetlock ();
89 /* Run the handlers registered for the child. */
90 list_for_each (runp, &__fork_child_list)
92 struct fork_handler *curp;
94 curp = list_entry (runp, struct fork_handler, list);
96 curp->handler ();
99 /* Initialize the fork lock. */
100 __fork_lock = (lll_lock_t) LLL_LOCK_INITIALIZER;
102 else
104 /* We execute this even if the 'fork' call failed. */
105 _IO_list_unlock ();
107 /* Run the handlers registered for the parent. */
108 list_for_each (runp, &__fork_parent_list)
110 struct fork_handler *curp;
112 curp = list_entry (runp, struct fork_handler, list);
114 curp->handler ();
117 /* Release the for lock. */
118 lll_unlock (__fork_lock);
121 return pid;
123 weak_alias (__libc_fork, __fork)
124 weak_alias (__libc_fork, fork)