init.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  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.
  4. The GNU C Library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Lesser General Public
  6. License as published by the Free Software Foundation; either
  7. version 2.1 of the License, or (at your option) any later version.
  8. The GNU C Library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with the GNU C Library; if not, see
  14. <http://www.gnu.org/licenses/>. */
  15. #include <assert.h>
  16. #include <errno.h>
  17. #include <limits.h>
  18. #include <signal.h>
  19. #include <stdlib.h>
  20. #include <unistd.h>
  21. #include <sys/param.h>
  22. #include <sys/resource.h>
  23. #include <pthreadP.h>
  24. #include <atomic.h>
  25. #include <ldsodefs.h>
  26. #include <tls.h>
  27. #include <fork.h>
  28. #include <version.h>
  29. #include <smp.h>
  30. #include <lowlevellock.h>
  31. #include <bits/kernel-features.h>
  32. #include <stdio.h>
  33. /* Size and alignment of static TLS block. */
  34. size_t __static_tls_size;
  35. size_t __static_tls_align_m1;
  36. #ifndef __ASSUME_SET_ROBUST_LIST
  37. /* Negative if we do not have the system call and we can use it. */
  38. int __set_robust_list_avail;
  39. # define set_robust_list_not_avail() \
  40. __set_robust_list_avail = -1
  41. #else
  42. # define set_robust_list_not_avail() do { } while (0)
  43. #endif
  44. #ifndef __ASSUME_FUTEX_CLOCK_REALTIME
  45. /* Nonzero if we do not have FUTEX_CLOCK_REALTIME. */
  46. int __have_futex_clock_realtime;
  47. # define __set_futex_clock_realtime() \
  48. __have_futex_clock_realtime = 1
  49. #else
  50. #define __set_futex_clock_realtime() do { } while (0)
  51. #endif
  52. /* Version of the library, used in libthread_db to detect mismatches. */
  53. static const char nptl_version[] __attribute_used__ = VERSION;
  54. /* For asynchronous cancellation we use a signal. This is the handler. */
  55. static void
  56. sigcancel_handler (int sig, siginfo_t *si, void *ctx)
  57. {
  58. #ifdef __ASSUME_CORRECT_SI_PID
  59. /* Determine the process ID. It might be negative if the thread is
  60. in the middle of a fork() call. */
  61. pid_t pid = THREAD_GETMEM (THREAD_SELF, pid);
  62. if (__builtin_expect (pid < 0, 0))
  63. pid = -pid;
  64. #endif
  65. /* Safety check. It would be possible to call this function for
  66. other signals and send a signal from another process. This is not
  67. correct and might even be a security problem. Try to catch as
  68. many incorrect invocations as possible. */
  69. if (sig != SIGCANCEL
  70. #ifdef __ASSUME_CORRECT_SI_PID
  71. /* Kernels before 2.5.75 stored the thread ID and not the process
  72. ID in si_pid so we skip this test. */
  73. || si->si_pid != pid
  74. #endif
  75. || si->si_code != SI_TKILL)
  76. return;
  77. struct pthread *self = THREAD_SELF;
  78. int oldval = THREAD_GETMEM (self, cancelhandling);
  79. while (1)
  80. {
  81. /* We are canceled now. When canceled by another thread this flag
  82. is already set but if the signal is directly send (internally or
  83. from another process) is has to be done here. */
  84. int newval = oldval | CANCELING_BITMASK | CANCELED_BITMASK;
  85. if (oldval == newval || (oldval & EXITING_BITMASK) != 0)
  86. /* Already canceled or exiting. */
  87. break;
  88. int curval = THREAD_ATOMIC_CMPXCHG_VAL (self, cancelhandling, newval,
  89. oldval);
  90. if (curval == oldval)
  91. {
  92. /* Set the return value. */
  93. THREAD_SETMEM (self, result, PTHREAD_CANCELED);
  94. /* Make sure asynchronous cancellation is still enabled. */
  95. if ((newval & CANCELTYPE_BITMASK) != 0)
  96. /* Run the registered destructors and terminate the thread. */
  97. __do_cancel ();
  98. break;
  99. }
  100. oldval = curval;
  101. }
  102. }
  103. struct xid_command *__xidcmd attribute_hidden;
  104. /* For asynchronous cancellation we use a signal. This is the handler. */
  105. static void
  106. sighandler_setxid (int sig, siginfo_t *si, void *ctx)
  107. {
  108. #ifdef __ASSUME_CORRECT_SI_PID
  109. /* Determine the process ID. It might be negative if the thread is
  110. in the middle of a fork() call. */
  111. pid_t pid = THREAD_GETMEM (THREAD_SELF, pid);
  112. if (__builtin_expect (pid < 0, 0))
  113. pid = -pid;
  114. #endif
  115. /* Safety check. It would be possible to call this function for
  116. other signals and send a signal from another process. This is not
  117. correct and might even be a security problem. Try to catch as
  118. many incorrect invocations as possible. */
  119. if (sig != SIGSETXID
  120. #ifdef __ASSUME_CORRECT_SI_PID
  121. /* Kernels before 2.5.75 stored the thread ID and not the process
  122. ID in si_pid so we skip this test. */
  123. || si->si_pid != pid
  124. #endif
  125. || si->si_code != SI_TKILL)
  126. 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]);
  130. /* Reset the SETXID flag. */
  131. struct pthread *self = THREAD_SELF;
  132. int flags, newval;
  133. do
  134. {
  135. flags = THREAD_GETMEM (self, cancelhandling);
  136. newval = THREAD_ATOMIC_CMPXCHG_VAL (self, cancelhandling,
  137. flags & ~SETXID_BITMASK, flags);
  138. }
  139. while (flags != newval);
  140. /* And release the futex. */
  141. self->setxid_futex = 1;
  142. lll_futex_wake (&self->setxid_futex, 1, LLL_PRIVATE);
  143. if (atomic_decrement_val (&__xidcmd->cntr) == 0)
  144. lll_futex_wake (&__xidcmd->cntr, 1, LLL_PRIVATE);
  145. }
  146. /* When using __thread for this, we do it in libc so as not
  147. to give libpthread its own TLS segment just for this. */
  148. extern void **__libc_dl_error_tsd (void) __attribute__ ((const));
  149. /* This can be set by the debugger before initialization is complete. */
  150. static bool __nptl_initial_report_events __attribute_used__;
  151. void __pthread_initialize_minimal_internal (void) attribute_hidden;
  152. void
  153. __pthread_initialize_minimal_internal (void)
  154. {
  155. static int initialized = 0;
  156. if (initialized)
  157. return;
  158. initialized = 1;
  159. /* Minimal initialization of the thread descriptor. */
  160. struct pthread *pd = THREAD_SELF;
  161. INTERNAL_SYSCALL_DECL (err);
  162. pd->pid = pd->tid = INTERNAL_SYSCALL (set_tid_address, err, 1, &pd->tid);
  163. THREAD_SETMEM (pd, specific[0], &pd->specific_1stblock[0]);
  164. THREAD_SETMEM (pd, user_stack, true);
  165. if (LLL_LOCK_INITIALIZER != 0)
  166. THREAD_SETMEM (pd, lock, LLL_LOCK_INITIALIZER);
  167. #if HP_TIMING_AVAIL
  168. THREAD_SETMEM (pd, cpuclock_offset, GL(dl_cpuclock_offset));
  169. #endif
  170. /* Initialize the robust mutex data. */
  171. #ifdef __PTHREAD_MUTEX_HAVE_PREV
  172. pd->robust_prev = &pd->robust_head;
  173. #endif
  174. pd->robust_head.list = &pd->robust_head;
  175. #ifdef __NR_set_robust_list
  176. pd->robust_head.futex_offset = (offsetof (pthread_mutex_t, __data.__lock)
  177. - offsetof (pthread_mutex_t,
  178. __data.__list.__next));
  179. int res = INTERNAL_SYSCALL (set_robust_list, err, 2, &pd->robust_head,
  180. sizeof (struct robust_list_head));
  181. if (INTERNAL_SYSCALL_ERROR_P (res, err))
  182. #endif
  183. set_robust_list_not_avail ();
  184. #ifndef __ASSUME_PRIVATE_FUTEX
  185. /* Private futexes are always used (at least internally) so that
  186. doing the test once this early is beneficial. */
  187. {
  188. int word = 0;
  189. word = INTERNAL_SYSCALL (futex, err, 3, &word,
  190. FUTEX_WAKE | FUTEX_PRIVATE_FLAG, 1);
  191. if (!INTERNAL_SYSCALL_ERROR_P (word, err))
  192. THREAD_SETMEM (pd, header.private_futex, FUTEX_PRIVATE_FLAG);
  193. }
  194. /* Private futexes have been introduced earlier than the
  195. FUTEX_CLOCK_REALTIME flag. We don't have to run the test if we
  196. know the former are not supported. This also means we know the
  197. kernel will return ENOSYS for unknown operations. */
  198. if (THREAD_GETMEM (pd, header.private_futex) != 0)
  199. #endif
  200. #ifndef __ASSUME_FUTEX_CLOCK_REALTIME
  201. {
  202. int word = 0;
  203. /* NB: the syscall actually takes six parameters. The last is the
  204. bit mask. But since we will not actually wait at all the value
  205. is irrelevant. Given that passing six parameters is difficult
  206. on some architectures we just pass whatever random value the
  207. calling convention calls for to the kernel. It causes no harm. */
  208. word = INTERNAL_SYSCALL (futex, err, 5, &word,
  209. FUTEX_WAIT_BITSET | FUTEX_CLOCK_REALTIME
  210. | FUTEX_PRIVATE_FLAG, 1, NULL, 0);
  211. assert (INTERNAL_SYSCALL_ERROR_P (word, err));
  212. if (INTERNAL_SYSCALL_ERRNO (word, err) != ENOSYS)
  213. __set_futex_clock_realtime ();
  214. }
  215. #endif
  216. /* Set initial thread's stack block from 0 up to __libc_stack_end.
  217. It will be bigger than it actually is, but for unwind.c/pt-longjmp.c
  218. purposes this is good enough. */
  219. THREAD_SETMEM (pd, stackblock_size, (size_t) __libc_stack_end);
  220. /* Initialize the list of all running threads with the main thread. */
  221. INIT_LIST_HEAD (&__stack_user);
  222. list_add (&pd->list, &__stack_user);
  223. /* Before initializing __stack_user, the debugger could not find us and
  224. had to set __nptl_initial_report_events. Propagate its setting. */
  225. THREAD_SETMEM (pd, report_events, __nptl_initial_report_events);
  226. /* Install the cancellation signal handler. If for some reason we
  227. cannot install the handler we do not abort. Maybe we should, but
  228. it is only asynchronous cancellation which is affected. */
  229. struct sigaction sa;
  230. sa.sa_sigaction = sigcancel_handler;
  231. sa.sa_flags = SA_SIGINFO;
  232. __sigemptyset (&sa.sa_mask);
  233. (void) __libc_sigaction (SIGCANCEL, &sa, NULL);
  234. /* Install the handle to change the threads' uid/gid. */
  235. sa.sa_sigaction = sighandler_setxid;
  236. sa.sa_flags = SA_SIGINFO | SA_RESTART;
  237. (void) __libc_sigaction (SIGSETXID, &sa, NULL);
  238. /* The parent process might have left the signals blocked. Just in
  239. case, unblock it. We reuse the signal mask in the sigaction
  240. structure. It is already cleared. */
  241. __sigaddset (&sa.sa_mask, SIGCANCEL);
  242. __sigaddset (&sa.sa_mask, SIGSETXID);
  243. (void) INTERNAL_SYSCALL (rt_sigprocmask, err, 4, SIG_UNBLOCK, &sa.sa_mask,
  244. NULL, _NSIG / 8);
  245. /* Get the size of the static and alignment requirements for the TLS
  246. block. */
  247. size_t static_tls_align;
  248. _dl_get_tls_static_info (&__static_tls_size, &static_tls_align);
  249. /* Make sure the size takes all the alignments into account. */
  250. if (STACK_ALIGN > static_tls_align)
  251. static_tls_align = STACK_ALIGN;
  252. __static_tls_align_m1 = static_tls_align - 1;
  253. __static_tls_size = roundup (__static_tls_size, static_tls_align);
  254. /* Determine the default allowed stack size. This is the size used
  255. in case the user does not specify one. */
  256. struct rlimit limit;
  257. if (getrlimit (RLIMIT_STACK, &limit) != 0
  258. || limit.rlim_cur == RLIM_INFINITY)
  259. /* The system limit is not usable. Use an architecture-specific
  260. default. */
  261. limit.rlim_cur = ARCH_STACK_DEFAULT_SIZE;
  262. else if (limit.rlim_cur < PTHREAD_STACK_MIN)
  263. /* The system limit is unusably small.
  264. Use the minimal size acceptable. */
  265. limit.rlim_cur = PTHREAD_STACK_MIN;
  266. /* Do not exceed architecture specific default */
  267. if (limit.rlim_cur > ARCH_STACK_DEFAULT_SIZE)
  268. limit.rlim_cur = ARCH_STACK_DEFAULT_SIZE;
  269. /* Make sure it meets the minimum size that allocate_stack
  270. (allocatestack.c) will demand, which depends on the page size. */
  271. const uintptr_t pagesz = sysconf (_SC_PAGESIZE);
  272. const size_t minstack = pagesz + __static_tls_size + MINIMAL_REST_STACK;
  273. if (limit.rlim_cur < minstack)
  274. limit.rlim_cur = minstack;
  275. /* Round the resource limit up to page size. */
  276. limit.rlim_cur = (limit.rlim_cur + pagesz - 1) & -pagesz;
  277. __default_stacksize = limit.rlim_cur;
  278. #ifdef SHARED
  279. /* Transfer the old value from the dynamic linker's internal location. */
  280. *__libc_dl_error_tsd () = *(*GL(dl_error_catch_tsd)) ();
  281. GL(dl_error_catch_tsd) = &__libc_dl_error_tsd;
  282. #endif
  283. GL(dl_init_static_tls) = &__pthread_init_static_tls;
  284. /* Register the fork generation counter with the libc. */
  285. #ifndef TLS_MULTIPLE_THREADS_IN_TCB
  286. __libc_multiple_threads_ptr =
  287. #endif
  288. __libc_pthread_init (&__fork_generation, __reclaim_stacks);
  289. /* Determine whether the machine is SMP or not. */
  290. __is_smp = is_smp_system ();
  291. /* uClibc-specific stdio initialization for threads. */
  292. {
  293. FILE *fp;
  294. _stdio_user_locking = 0; /* 2 if threading not initialized */
  295. for (fp = _stdio_openlist; fp != NULL; fp = fp->__nextopen) {
  296. if (fp->__user_locking != 1) {
  297. fp->__user_locking = 0;
  298. }
  299. }
  300. }
  301. }
  302. strong_alias (__pthread_initialize_minimal_internal,
  303. __pthread_initialize_minimal)