Remove extra space at end of line.
[glibc.git] / sysdeps / nptl / fork.c
blobec56a827eba483b6a6e3226a8c4ba7e1199d10a5
1 /* Copyright (C) 2002-2018 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, see
17 <http://www.gnu.org/licenses/>. */
19 #include <assert.h>
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 <hp-timing.h>
27 #include <ldsodefs.h>
28 #include <stdio-lock.h>
29 #include <atomic.h>
30 #include <nptl/pthreadP.h>
31 #include <fork.h>
32 #include <arch-fork.h>
33 #include <futex-internal.h>
34 #include <malloc/malloc-internal.h>
36 static void
37 fresetlockfiles (void)
39 _IO_ITER i;
41 for (i = _IO_iter_begin(); i != _IO_iter_end(); i = _IO_iter_next(i))
42 if ((_IO_iter_file (i)->_flags & _IO_USER_LOCK) == 0)
43 _IO_lock_init (*((_IO_lock_t *) _IO_iter_file(i)->_lock));
47 pid_t
48 __libc_fork (void)
50 pid_t pid;
52 /* Determine if we are running multiple threads. We skip some fork
53 handlers in the single-thread case, to make fork safer to use in
54 signal handlers. POSIX requires that fork is async-signal-safe,
55 but our current fork implementation is not. */
56 bool multiple_threads = THREAD_GETMEM (THREAD_SELF, header.multiple_threads);
58 __run_fork_handlers (atfork_run_prepare);
60 /* If we are not running multiple threads, we do not have to
61 preserve lock state. If fork runs from a signal handler, only
62 async-signal-safe functions can be used in the child. These data
63 structures are only used by unsafe functions, so their state does
64 not matter if fork was called from a signal handler. */
65 if (multiple_threads)
67 _IO_list_lock ();
69 /* Acquire malloc locks. This needs to come last because fork
70 handlers may use malloc, and the libio list lock has an
71 indirect malloc dependency as well (via the getdelim
72 function). */
73 call_function_static_weak (__malloc_fork_lock_parent);
76 pid = arch_fork (&THREAD_SELF->tid);
78 if (pid == 0)
80 struct pthread *self = THREAD_SELF;
82 /* See __pthread_once. */
83 if (__fork_generation_pointer != NULL)
84 *__fork_generation_pointer += __PTHREAD_ONCE_FORK_GEN_INCR;
86 #if HP_TIMING_AVAIL
87 /* The CPU clock of the thread and process have to be set to zero. */
88 hp_timing_t now;
89 HP_TIMING_NOW (now);
90 THREAD_SETMEM (self, cpuclock_offset, now);
91 GL(dl_cpuclock_offset) = now;
92 #endif
94 #ifdef __NR_set_robust_list
95 /* Initialize the robust mutex list setting in the kernel which has
96 been reset during the fork. We do not check for errors because if
97 it fails here, it must have failed at process startup as well and
98 nobody could have used robust mutexes.
99 Before we do that, we have to clear the list of robust mutexes
100 because we do not inherit ownership of mutexes from the parent.
101 We do not have to set self->robust_head.futex_offset since we do
102 inherit the correct value from the parent. We do not need to clear
103 the pending operation because it must have been zero when fork was
104 called. */
105 # if __PTHREAD_MUTEX_HAVE_PREV
106 self->robust_prev = &self->robust_head;
107 # endif
108 self->robust_head.list = &self->robust_head;
109 # ifdef SHARED
110 if (__builtin_expect (__libc_pthread_functions_init, 0))
111 PTHFCT_CALL (ptr_set_robust, (self));
112 # else
113 extern __typeof (__nptl_set_robust) __nptl_set_robust
114 __attribute__((weak));
115 if (__builtin_expect (__nptl_set_robust != NULL, 0))
116 __nptl_set_robust (self);
117 # endif
118 #endif
120 /* Reset the lock state in the multi-threaded case. */
121 if (multiple_threads)
123 /* Release malloc locks. */
124 call_function_static_weak (__malloc_fork_unlock_child);
126 /* Reset the file list. These are recursive mutexes. */
127 fresetlockfiles ();
129 /* Reset locks in the I/O code. */
130 _IO_list_resetlock ();
133 /* Reset the lock the dynamic loader uses to protect its data. */
134 __rtld_lock_initialize (GL(dl_load_lock));
136 /* Run the handlers registered for the child. */
137 __run_fork_handlers (atfork_run_child);
139 else
141 /* Release acquired locks in the multi-threaded case. */
142 if (multiple_threads)
144 /* Release malloc locks, parent process variant. */
145 call_function_static_weak (__malloc_fork_unlock_parent);
147 /* We execute this even if the 'fork' call failed. */
148 _IO_list_unlock ();
151 /* Run the handlers registered for the parent. */
152 __run_fork_handlers (atfork_run_parent);
155 return pid;
157 weak_alias (__libc_fork, __fork)
158 libc_hidden_def (__fork)
159 weak_alias (__libc_fork, fork)