remove PID caching
[uclibc-ng.git] / libpthread / nptl / init.c
blob09220c2206fefff6247381776edfde73fe922cdb
1 /* Copyright (C) 2002-2007, 2008, 2009 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 <errno.h>
21 #include <limits.h>
22 #include <signal.h>
23 #include <stdlib.h>
24 #include <unistd.h>
25 #include <sys/param.h>
26 #include <sys/resource.h>
27 #include <pthreadP.h>
28 #include <atomic.h>
29 #include <ldsodefs.h>
30 #include <tls.h>
31 #include <fork.h>
32 #include <version.h>
33 #include <smp.h>
34 #include <lowlevellock.h>
35 #include <bits/kernel-features.h>
36 #include <stdio.h>
38 /* Size and alignment of static TLS block. */
39 size_t __static_tls_size;
40 size_t __static_tls_align_m1;
42 #ifndef __ASSUME_SET_ROBUST_LIST
43 /* Negative if we do not have the system call and we can use it. */
44 int __set_robust_list_avail;
45 # define set_robust_list_not_avail() \
46 __set_robust_list_avail = -1
47 #else
48 # define set_robust_list_not_avail() do { } while (0)
49 #endif
51 #ifndef __ASSUME_FUTEX_CLOCK_REALTIME
52 /* Nonzero if we do not have FUTEX_CLOCK_REALTIME. */
53 int __have_futex_clock_realtime;
54 # define __set_futex_clock_realtime() \
55 __have_futex_clock_realtime = 1
56 #else
57 #define __set_futex_clock_realtime() do { } while (0)
58 #endif
60 /* Version of the library, used in libthread_db to detect mismatches. */
61 static const char nptl_version[] __attribute_used__ = VERSION;
63 /* For asynchronous cancellation we use a signal. This is the handler. */
64 static void
65 sigcancel_handler (int sig, siginfo_t *si, void *ctx)
68 /* Safety check. It would be possible to call this function for
69 other signals and send a signal from another process. This is not
70 correct and might even be a security problem. Try to catch as
71 many incorrect invocations as possible. */
72 if (sig != SIGCANCEL
73 || si->si_pid != getpid()
74 || si->si_code != SI_TKILL)
75 return;
77 struct pthread *self = THREAD_SELF;
79 int oldval = THREAD_GETMEM (self, cancelhandling);
80 while (1)
82 /* We are canceled now. When canceled by another thread this flag
83 is already set but if the signal is directly send (internally or
84 from another process) is has to be done here. */
85 int newval = oldval | CANCELING_BITMASK | CANCELED_BITMASK;
87 if (oldval == newval || (oldval & EXITING_BITMASK) != 0)
88 /* Already canceled or exiting. */
89 break;
91 int curval = THREAD_ATOMIC_CMPXCHG_VAL (self, cancelhandling, newval,
92 oldval);
93 if (curval == oldval)
95 /* Set the return value. */
96 THREAD_SETMEM (self, result, PTHREAD_CANCELED);
98 /* Make sure asynchronous cancellation is still enabled. */
99 if ((newval & CANCELTYPE_BITMASK) != 0)
100 /* Run the registered destructors and terminate the thread. */
101 __do_cancel ();
103 break;
106 oldval = curval;
111 struct xid_command *__xidcmd attribute_hidden;
113 /* For asynchronous cancellation we use a signal. This is the handler. */
114 static void
115 sighandler_setxid (int sig, siginfo_t *si, void *ctx)
118 /* Safety check. It would be possible to call this function for
119 other signals and send a signal from another process. This is not
120 correct and might even be a security problem. Try to catch as
121 many incorrect invocations as possible. */
122 if (sig != SIGSETXID
123 || si->si_pid != getpid()
124 || si->si_code != SI_TKILL)
125 return;
127 INTERNAL_SYSCALL_DECL (err);
128 INTERNAL_SYSCALL_NCS (__xidcmd->syscall_no, err, 3, __xidcmd->id[0],
129 __xidcmd->id[1], __xidcmd->id[2]);
131 /* Reset the SETXID flag. */
132 struct pthread *self = THREAD_SELF;
133 int flags, newval;
136 flags = THREAD_GETMEM (self, cancelhandling);
137 newval = THREAD_ATOMIC_CMPXCHG_VAL (self, cancelhandling,
138 flags & ~SETXID_BITMASK, flags);
140 while (flags != newval);
142 /* And release the futex. */
143 self->setxid_futex = 1;
144 lll_futex_wake (&self->setxid_futex, 1, LLL_PRIVATE);
146 if (atomic_decrement_val (&__xidcmd->cntr) == 0)
147 lll_futex_wake (&__xidcmd->cntr, 1, LLL_PRIVATE);
151 /* When using __thread for this, we do it in libc so as not
152 to give libpthread its own TLS segment just for this. */
153 extern void **__libc_dl_error_tsd (void) __attribute__ ((const));
156 /* This can be set by the debugger before initialization is complete. */
157 static bool __nptl_initial_report_events __attribute_used__;
159 void __pthread_initialize_minimal_internal (void) attribute_hidden;
160 void
161 __pthread_initialize_minimal_internal (void)
163 static int initialized = 0;
165 if (initialized)
166 return;
167 initialized = 1;
169 /* Minimal initialization of the thread descriptor. */
170 struct pthread *pd = THREAD_SELF;
171 INTERNAL_SYSCALL_DECL (err);
172 pd->tid = INTERNAL_SYSCALL (set_tid_address, err, 1, &pd->tid);
173 THREAD_SETMEM (pd, specific[0], &pd->specific_1stblock[0]);
174 THREAD_SETMEM (pd, user_stack, true);
175 if (LLL_LOCK_INITIALIZER != 0)
176 THREAD_SETMEM (pd, lock, LLL_LOCK_INITIALIZER);
177 #if HP_TIMING_AVAIL
178 THREAD_SETMEM (pd, cpuclock_offset, GL(dl_cpuclock_offset));
179 #endif
181 /* Initialize the robust mutex data. */
182 #ifdef __PTHREAD_MUTEX_HAVE_PREV
183 pd->robust_prev = &pd->robust_head;
184 #endif
185 pd->robust_head.list = &pd->robust_head;
186 #ifdef __NR_set_robust_list
187 pd->robust_head.futex_offset = (offsetof (pthread_mutex_t, __data.__lock)
188 - offsetof (pthread_mutex_t,
189 __data.__list.__next));
190 int res = INTERNAL_SYSCALL (set_robust_list, err, 2, &pd->robust_head,
191 sizeof (struct robust_list_head));
192 if (INTERNAL_SYSCALL_ERROR_P (res, err))
193 #endif
194 set_robust_list_not_avail ();
196 #ifndef __ASSUME_PRIVATE_FUTEX
197 /* Private futexes are always used (at least internally) so that
198 doing the test once this early is beneficial. */
200 int word = 0;
201 word = INTERNAL_SYSCALL (futex, err, 3, &word,
202 FUTEX_WAKE | FUTEX_PRIVATE_FLAG, 1);
203 if (!INTERNAL_SYSCALL_ERROR_P (word, err))
204 THREAD_SETMEM (pd, header.private_futex, FUTEX_PRIVATE_FLAG);
207 /* Private futexes have been introduced earlier than the
208 FUTEX_CLOCK_REALTIME flag. We don't have to run the test if we
209 know the former are not supported. This also means we know the
210 kernel will return ENOSYS for unknown operations. */
211 if (THREAD_GETMEM (pd, header.private_futex) != 0)
212 #endif
213 #ifndef __ASSUME_FUTEX_CLOCK_REALTIME
215 int word = 0;
216 /* NB: the syscall actually takes six parameters. The last is the
217 bit mask. But since we will not actually wait at all the value
218 is irrelevant. Given that passing six parameters is difficult
219 on some architectures we just pass whatever random value the
220 calling convention calls for to the kernel. It causes no harm. */
221 word = INTERNAL_SYSCALL (futex, err, 5, &word,
222 FUTEX_WAIT_BITSET | FUTEX_CLOCK_REALTIME
223 | FUTEX_PRIVATE_FLAG, 1, NULL, 0);
224 assert (INTERNAL_SYSCALL_ERROR_P (word, err));
225 if (INTERNAL_SYSCALL_ERRNO (word, err) != ENOSYS)
226 __set_futex_clock_realtime ();
228 #endif
230 /* Set initial thread's stack block from 0 up to __libc_stack_end.
231 It will be bigger than it actually is, but for unwind.c/pt-longjmp.c
232 purposes this is good enough. */
233 THREAD_SETMEM (pd, stackblock_size, (size_t) __libc_stack_end);
235 /* Initialize the list of all running threads with the main thread. */
236 INIT_LIST_HEAD (&__stack_user);
237 list_add (&pd->list, &__stack_user);
239 /* Before initializing __stack_user, the debugger could not find us and
240 had to set __nptl_initial_report_events. Propagate its setting. */
241 THREAD_SETMEM (pd, report_events, __nptl_initial_report_events);
243 /* Install the cancellation signal handler. If for some reason we
244 cannot install the handler we do not abort. Maybe we should, but
245 it is only asynchronous cancellation which is affected. */
246 struct sigaction sa;
247 sa.sa_sigaction = sigcancel_handler;
248 sa.sa_flags = SA_SIGINFO;
249 __sigemptyset (&sa.sa_mask);
251 (void) __libc_sigaction (SIGCANCEL, &sa, NULL);
253 /* Install the handle to change the threads' uid/gid. */
254 sa.sa_sigaction = sighandler_setxid;
255 sa.sa_flags = SA_SIGINFO | SA_RESTART;
257 (void) __libc_sigaction (SIGSETXID, &sa, NULL);
259 /* The parent process might have left the signals blocked. Just in
260 case, unblock it. We reuse the signal mask in the sigaction
261 structure. It is already cleared. */
262 __sigaddset (&sa.sa_mask, SIGCANCEL);
263 __sigaddset (&sa.sa_mask, SIGSETXID);
264 (void) INTERNAL_SYSCALL (rt_sigprocmask, err, 4, SIG_UNBLOCK, &sa.sa_mask,
265 NULL, _NSIG / 8);
267 /* Get the size of the static and alignment requirements for the TLS
268 block. */
269 size_t static_tls_align;
270 _dl_get_tls_static_info (&__static_tls_size, &static_tls_align);
272 /* Make sure the size takes all the alignments into account. */
273 if (STACK_ALIGN > static_tls_align)
274 static_tls_align = STACK_ALIGN;
275 __static_tls_align_m1 = static_tls_align - 1;
277 __static_tls_size = roundup (__static_tls_size, static_tls_align);
279 /* Determine the default allowed stack size. This is the size used
280 in case the user does not specify one. */
281 struct rlimit limit;
282 if (getrlimit (RLIMIT_STACK, &limit) != 0
283 || limit.rlim_cur == RLIM_INFINITY)
284 /* The system limit is not usable. Use an architecture-specific
285 default. */
286 limit.rlim_cur = ARCH_STACK_DEFAULT_SIZE;
287 else if (limit.rlim_cur < PTHREAD_STACK_MIN)
288 /* The system limit is unusably small.
289 Use the minimal size acceptable. */
290 limit.rlim_cur = PTHREAD_STACK_MIN;
292 /* Do not exceed architecture specific default */
293 if (limit.rlim_cur > ARCH_STACK_DEFAULT_SIZE)
294 limit.rlim_cur = ARCH_STACK_DEFAULT_SIZE;
296 /* Make sure it meets the minimum size that allocate_stack
297 (allocatestack.c) will demand, which depends on the page size. */
298 const uintptr_t pagesz = sysconf (_SC_PAGESIZE);
299 const size_t minstack = pagesz + __static_tls_size + MINIMAL_REST_STACK;
300 if (limit.rlim_cur < minstack)
301 limit.rlim_cur = minstack;
303 /* Round the resource limit up to page size. */
304 limit.rlim_cur = (limit.rlim_cur + pagesz - 1) & -pagesz;
305 __default_stacksize = limit.rlim_cur;
307 #ifdef SHARED
308 /* Transfer the old value from the dynamic linker's internal location. */
309 *__libc_dl_error_tsd () = *(*GL(dl_error_catch_tsd)) ();
310 GL(dl_error_catch_tsd) = &__libc_dl_error_tsd;
312 #endif
314 GL(dl_init_static_tls) = &__pthread_init_static_tls;
316 /* Register the fork generation counter with the libc. */
317 #ifndef TLS_MULTIPLE_THREADS_IN_TCB
318 __libc_multiple_threads_ptr =
319 #endif
320 __libc_pthread_init (&__fork_generation, __reclaim_stacks);
322 /* Determine whether the machine is SMP or not. */
323 __is_smp = is_smp_system ();
325 /* uClibc-specific stdio initialization for threads. */
327 FILE *fp;
328 _stdio_user_locking = 0; /* 2 if threading not initialized */
329 for (fp = _stdio_openlist; fp != NULL; fp = fp->__nextopen) {
330 if (fp->__user_locking != 1) {
331 fp->__user_locking = 0;
336 strong_alias (__pthread_initialize_minimal_internal,
337 __pthread_initialize_minimal)