Fix -Wgnu-designator clang warnings
[uclibc-ng.git] / libpthread / nptl / init.c
blobddc552f2e870cbdef731a56ce0c93ba5a4a912aa
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);
178 /* Initialize the robust mutex data. */
179 #ifdef __PTHREAD_MUTEX_HAVE_PREV
180 pd->robust_prev = &pd->robust_head;
181 #endif
182 pd->robust_head.list = &pd->robust_head;
183 #ifdef __NR_set_robust_list
184 pd->robust_head.futex_offset = (offsetof (pthread_mutex_t, __data.__lock)
185 - offsetof (pthread_mutex_t,
186 __data.__list.__next));
187 int res = INTERNAL_SYSCALL (set_robust_list, err, 2, &pd->robust_head,
188 sizeof (struct robust_list_head));
189 if (INTERNAL_SYSCALL_ERROR_P (res, err))
190 #endif
191 set_robust_list_not_avail ();
193 #ifndef __ASSUME_PRIVATE_FUTEX
194 /* Private futexes are always used (at least internally) so that
195 doing the test once this early is beneficial. */
197 int word = 0;
198 word = INTERNAL_SYSCALL (futex, err, 3, &word,
199 FUTEX_WAKE | FUTEX_PRIVATE_FLAG, 1);
200 if (!INTERNAL_SYSCALL_ERROR_P (word, err))
201 THREAD_SETMEM (pd, header.private_futex, FUTEX_PRIVATE_FLAG);
204 /* Private futexes have been introduced earlier than the
205 FUTEX_CLOCK_REALTIME flag. We don't have to run the test if we
206 know the former are not supported. This also means we know the
207 kernel will return ENOSYS for unknown operations. */
208 if (THREAD_GETMEM (pd, header.private_futex) != 0)
209 #endif
210 #ifndef __ASSUME_FUTEX_CLOCK_REALTIME
212 int word = 0;
213 /* NB: the syscall actually takes six parameters. The last is the
214 bit mask. But since we will not actually wait at all the value
215 is irrelevant. Given that passing six parameters is difficult
216 on some architectures we just pass whatever random value the
217 calling convention calls for to the kernel. It causes no harm. */
218 word = INTERNAL_SYSCALL (futex, err, 5, &word,
219 FUTEX_WAIT_BITSET | FUTEX_CLOCK_REALTIME
220 | FUTEX_PRIVATE_FLAG, 1, NULL, 0);
221 assert (INTERNAL_SYSCALL_ERROR_P (word, err));
222 if (INTERNAL_SYSCALL_ERRNO (word, err) != ENOSYS)
223 __set_futex_clock_realtime ();
225 #endif
227 /* Set initial thread's stack block from 0 up to __libc_stack_end.
228 It will be bigger than it actually is, but for unwind.c/pt-longjmp.c
229 purposes this is good enough. */
230 THREAD_SETMEM (pd, stackblock_size, (size_t) __libc_stack_end);
232 /* Initialize the list of all running threads with the main thread. */
233 INIT_LIST_HEAD (&__stack_user);
234 list_add (&pd->list, &__stack_user);
236 /* Before initializing __stack_user, the debugger could not find us and
237 had to set __nptl_initial_report_events. Propagate its setting. */
238 THREAD_SETMEM (pd, report_events, __nptl_initial_report_events);
240 /* Install the cancellation signal handler. If for some reason we
241 cannot install the handler we do not abort. Maybe we should, but
242 it is only asynchronous cancellation which is affected. */
243 struct sigaction sa;
244 sa.sa_sigaction = sigcancel_handler;
245 sa.sa_flags = SA_SIGINFO;
246 __sigemptyset (&sa.sa_mask);
248 (void) __libc_sigaction (SIGCANCEL, &sa, NULL);
250 /* Install the handle to change the threads' uid/gid. */
251 sa.sa_sigaction = sighandler_setxid;
252 sa.sa_flags = SA_SIGINFO | SA_RESTART;
254 (void) __libc_sigaction (SIGSETXID, &sa, NULL);
256 /* The parent process might have left the signals blocked. Just in
257 case, unblock it. We reuse the signal mask in the sigaction
258 structure. It is already cleared. */
259 __sigaddset (&sa.sa_mask, SIGCANCEL);
260 __sigaddset (&sa.sa_mask, SIGSETXID);
261 (void) INTERNAL_SYSCALL (rt_sigprocmask, err, 4, SIG_UNBLOCK, &sa.sa_mask,
262 NULL, _NSIG / 8);
264 /* Get the size of the static and alignment requirements for the TLS
265 block. */
266 size_t static_tls_align;
267 _dl_get_tls_static_info (&__static_tls_size, &static_tls_align);
269 /* Make sure the size takes all the alignments into account. */
270 if (STACK_ALIGN > static_tls_align)
271 static_tls_align = STACK_ALIGN;
272 __static_tls_align_m1 = static_tls_align - 1;
274 __static_tls_size = roundup (__static_tls_size, static_tls_align);
276 /* Determine the default allowed stack size. This is the size used
277 in case the user does not specify one. */
278 struct rlimit limit;
279 if (getrlimit (RLIMIT_STACK, &limit) != 0
280 || limit.rlim_cur == RLIM_INFINITY)
281 /* The system limit is not usable. Use a user-specified or
282 architecture-specific default. */
283 limit.rlim_cur = __PTHREADS_STACK_DEFAULT_SIZE__;
284 if (limit.rlim_cur < PTHREAD_STACK_MIN)
285 /* The system limit is unusably small.
286 Use the minimal size acceptable. */
287 limit.rlim_cur = PTHREAD_STACK_MIN;
289 /* Do not exceed the user-specified or architecture-specific default */
290 if (limit.rlim_cur > __PTHREADS_STACK_DEFAULT_SIZE__)
291 limit.rlim_cur = __PTHREADS_STACK_DEFAULT_SIZE__;
293 /* Make sure it meets the minimum size that allocate_stack
294 (allocatestack.c) will demand, which depends on the page size. */
295 #ifdef SHARED
296 extern size_t GLRO(dl_pagesize);
297 const uintptr_t pagesz = GLRO(dl_pagesize);
298 #else
299 const uintptr_t pagesz = sysconf (_SC_PAGESIZE);
300 #endif
301 const size_t minstack = pagesz + __static_tls_size + MINIMAL_REST_STACK;
302 if (limit.rlim_cur < minstack)
303 limit.rlim_cur = minstack;
305 /* Round the resource limit up to page size. */
306 limit.rlim_cur = (limit.rlim_cur + pagesz - 1) & -pagesz;
307 __default_stacksize = limit.rlim_cur;
309 #ifdef SHARED
310 /* Transfer the old value from the dynamic linker's internal location. */
311 *__libc_dl_error_tsd () = *(*GL(dl_error_catch_tsd)) ();
312 GL(dl_error_catch_tsd) = &__libc_dl_error_tsd;
314 #endif
316 GL(dl_init_static_tls) = &__pthread_init_static_tls;
318 /* Register the fork generation counter with the libc. */
319 #ifndef TLS_MULTIPLE_THREADS_IN_TCB
320 __libc_multiple_threads_ptr =
321 #endif
322 __libc_pthread_init (&__fork_generation, __reclaim_stacks);
324 /* Determine whether the machine is SMP or not. */
325 __is_smp = is_smp_system ();
327 /* uClibc-specific stdio initialization for threads. */
329 FILE *fp;
330 _stdio_user_locking = 0; /* 2 if threading not initialized */
331 for (fp = _stdio_openlist; fp != NULL; fp = fp->__nextopen) {
332 if (fp->__user_locking != 1) {
333 fp->__user_locking = 0;
338 strong_alias (__pthread_initialize_minimal_internal,
339 __pthread_initialize_minimal)
341 size_t
342 __pthread_get_minstack (const pthread_attr_t *attr)
344 return __static_tls_size + PTHREAD_STACK_MIN;